From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1JOC5I-0005FI-5x for mharc-grub-devel@gnu.org; Sun, 10 Feb 2008 08:19:04 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1JOC5F-0005Ej-8E for grub-devel@gnu.org; Sun, 10 Feb 2008 08:19:01 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1JOC5E-0005EH-Mr for grub-devel@gnu.org; Sun, 10 Feb 2008 08:19:00 -0500 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JOC5E-0005EC-Kh for grub-devel@gnu.org; Sun, 10 Feb 2008 08:19:00 -0500 Received: from mx20.gnu.org ([199.232.41.8]) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1JOC5E-00042r-9N for grub-devel@gnu.org; Sun, 10 Feb 2008 08:19:00 -0500 Received: from aybabtu.com ([69.60.117.155]) by mx20.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1JOC5D-0007Ub-Bf for grub-devel@gnu.org; Sun, 10 Feb 2008 08:18:59 -0500 Received: from [192.168.10.6] (helo=thorin) by aybabtu.com with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1JOC51-0002yJ-CE for grub-devel@gnu.org; Sun, 10 Feb 2008 14:18:50 +0100 Received: from rmh by thorin with local (Exim 4.63) (envelope-from ) id 1JOC3E-00015r-EP for grub-devel@gnu.org; Sun, 10 Feb 2008 14:16:56 +0100 Date: Sun, 10 Feb 2008 14:16:56 +0100 From: Robert Millan To: grub-devel@gnu.org Message-ID: <20080210131656.GA4168@thorin> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="17pEHd4RhPHOinZp" Content-Disposition: inline Content-Transfer-Encoding: 8bit Organization: free as in freedom X-Message-Flag: Worried about Outlook viruses? Switch to Thunderbird! www.mozilla.com/thunderbird X-Debbugs-No-Ack: true User-Agent: Mutt/1.5.13 (2006-08-11) X-detected-kernel: by mx20.gnu.org: Genre and OS details not recognized. X-detected-kernel: by monty-python.gnu.org: Linux 2.6, seldom 2.4 (older, 4) Subject: [PATCH] read --echo=[yes|no|wildcard] X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 10 Feb 2008 13:19:02 -0000 --17pEHd4RhPHOinZp Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit Adds a parameter to define echoing behaviour in read. Then one can use --echo=no or --echo=wildcard to make it suitable for reading passwords. -- Robert Millan I know my rights; I want my phone call! What use is a phone call… if you are unable to speak? (as seen on /.) --17pEHd4RhPHOinZp Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="read_echoing.diff" 2008-02-10 Robert Millan * commands/read.c (options): New struct. (grub_getline): Receive a hook as argument, that will be invoked for each character read instead of directly printing it. This lets the caller choose which action is appropiate. Echo the final newline. (grub_cmd_read): Parse --echo parameter to determine echoing behaviour. Pass a hook to grub_getline() that implements behaviour determined by --echo. (grub_read_init): Pass `options' parameter to grub_register_command(). diff -x configure -x config.h.in -x CVS -x '*~' -x '*.mk' -urp ../grub2/commands/read.c ./commands/read.c --- ../grub2/commands/read.c 2008-02-02 21:35:08.000000000 +0100 +++ ./commands/read.c 2008-02-09 22:46:56.000000000 +0100 @@ -24,8 +24,14 @@ #include #include +static const struct grub_arg_option options[] = + { + {"echo", 'e', 0, "whether to echo user input", "[yes|no|wildcard]", ARG_TYPE_STRING}, + {0, 0, 0, 0, 0, 0} + }; + static char * -grub_getline (void) +grub_getline (void (*hook) (char c)) { int i; char *line; @@ -39,8 +45,7 @@ grub_getline (void) while ((line[i - 1] != '\n') && (line[i - 1] != '\r')) { line[i] = grub_getkey (); - if (grub_isprint (line[i])) - grub_putchar (line[i]); + hook (line[i]); i++; tmp = grub_realloc (line, 1 + i + sizeof('\0')); if (! tmp) @@ -51,14 +56,53 @@ grub_getline (void) line = tmp; } line[i] = '\0'; + grub_putchar ('\n'); return line; } +enum +{ + ECHO_YES, + ECHO_NO, + ECHO_WILDCARD, +}; + static grub_err_t -grub_cmd_read (struct grub_arg_list *state UNUSED, int argc, char **args) +grub_cmd_read (struct grub_arg_list *state, int argc, char **args) { - char *line = grub_getline (); + char *line; + int echo = ECHO_YES; + + if (state[0].set) + { + grub_printf ("%s\n", state[0].arg); + if (! grub_strcmp (state[0].arg, "no")) + echo = ECHO_NO; + else if (! grub_strcmp (state[0].arg, "wildcard")) + echo = ECHO_WILDCARD; + else if (grub_strcmp (state[0].arg, "yes")) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument"); + } + + auto void hook (char); + void hook (char c) + { + if (! grub_isprint (c)) + return; + + switch (echo) + { + case ECHO_YES: + grub_putchar (c); + break; + case ECHO_WILDCARD: + grub_putchar ('*'); + break; + } + } + + line = grub_getline (hook); if (! line) return grub_errno; if (argc > 0) @@ -72,7 +116,7 @@ grub_cmd_read (struct grub_arg_list *sta GRUB_MOD_INIT(read) { grub_register_command ("read", grub_cmd_read, GRUB_COMMAND_FLAG_CMDLINE, - "read [ENVVAR]", "Set variable with user input", 0); + "read [ENVVAR]", "Set variable with user input", options); } GRUB_MOD_FINI(read) --17pEHd4RhPHOinZp--