All of lore.kernel.org
 help / color / mirror / Atom feed
From: Robert Millan <rmh@aybabtu.com>
To: grub-devel@gnu.org
Subject: [PATCH] read --echo=[yes|no|wildcard]
Date: Sun, 10 Feb 2008 14:16:56 +0100	[thread overview]
Message-ID: <20080210131656.GA4168@thorin> (raw)

[-- Attachment #1: Type: text/plain, Size: 291 bytes --]


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

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)

[-- Attachment #2: read_echoing.diff --]
[-- Type: text/x-diff, Size: 2824 bytes --]

2008-02-10  Robert Millan  <rmh@aybabtu.com>

	* 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 <grub/term.h>
 #include <grub/types.h>
 
+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)

             reply	other threads:[~2008-02-10 13:19 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-10 13:16 Robert Millan [this message]
2008-02-10 13:56 ` [PATCH] read --echo=[yes|no|wildcard] Isaac Dupree
2008-02-10 15:22   ` Robert Millan
2008-02-10 16:41     ` Isaac Dupree
2008-02-10 17:00       ` Robert Millan
2008-02-10 18:00         ` Isaac Dupree
2008-02-10 19:39           ` Robert Millan
2008-02-10 20:00             ` Isaac Dupree
2008-02-10 20:47               ` [PATCH] erase variable data on user unset Robert Millan
2008-02-10 21:00                 ` Robert Millan
2008-02-10 21:31                 ` Isaac Dupree
2008-02-10 21:38                   ` Isaac Dupree
2008-02-10 21:53                     ` Robert Millan
2008-02-10 20:16 ` [PATCH] read --echo=[yes|no|wildcard] Yoshinori K. Okuji
2008-02-10 20:49   ` Robert Millan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080210131656.GA4168@thorin \
    --to=rmh@aybabtu.com \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.