grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Make echo UTF-8-clean
@ 2010-12-21 13:06 Colin Watson
  0 siblings, 0 replies; only message in thread
From: Colin Watson @ 2010-12-21 13:06 UTC (permalink / raw)
  To: grub-devel

While working on my previous patch, I noticed that echo wasn't
UTF-8-clean: the UTF-8 character 'à' was coming out as "??".  This patch
fixes that.

2010-12-21  Colin Watson  <cjwatson@ubuntu.com>

	* grub-core/commands/echo.c (grub_cmd_echo): Make UTF-8-clean by
	constructing a new unescaped string and passing it to grub_xputs in
	one go, rather than passing characters to grub_printf one at a time.

=== modified file 'grub-core/commands/echo.c'
--- grub-core/commands/echo.c	2010-11-30 15:23:41 +0000
+++ grub-core/commands/echo.c	2010-12-21 13:03:09 +0000
@@ -44,6 +44,9 @@ grub_cmd_echo (grub_extcmd_context_t ctx
   for (i = 0; i < argc; i++)
     {
       char *arg = *args;
+      /* Unescaping results in a string no longer than the original.  */
+      char *unescaped = grub_malloc (grub_strlen (arg) + 1);
+      char *p = unescaped;
       args++;
 
       while (*arg)
@@ -58,11 +61,11 @@ grub_cmd_echo (grub_extcmd_context_t ctx
 	      switch (*arg)
 		{
 		case '\\':
-		  grub_printf ("\\");
+		  *p++ = '\\';
 		  break;
 
 		case 'a':
-		  grub_printf ("\a");
+		  *p++ = '\a';
 		  break;
 
 		case 'c':
@@ -70,23 +73,23 @@ grub_cmd_echo (grub_extcmd_context_t ctx
 		  break;
 
 		case 'f':
-		  grub_printf ("\f");
+		  *p++ = '\f';
 		  break;
 
 		case 'n':
-		  grub_printf ("\n");
+		  *p++ = '\n';
 		  break;
 
 		case 'r':
-		  grub_printf ("\r");
+		  *p++ = '\r';
 		  break;
 
 		case 't':
-		  grub_printf ("\t");
+		  *p++ = '\t';
 		  break;
 
 		case 'v':
-		  grub_printf ("\v");
+		  *p++ = '\v';
 		  break;
 		}
 	      arg++;
@@ -95,10 +98,14 @@ grub_cmd_echo (grub_extcmd_context_t ctx
 
 	  /* This was not an escaped character, or escaping is not
 	     enabled.  */
-	  grub_printf ("%c", *arg);
+	  *p++ = *arg;
 	  arg++;
 	}
 
+      *p = '\0';
+      grub_xputs (unescaped);
+      grub_free (unescaped);
+
       /* If another argument follows, insert a space.  */
       if (i != argc - 1)
 	grub_printf (" " );

-- 
Colin Watson                                       [cjwatson@ubuntu.com]


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2010-12-21 13:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-21 13:06 [PATCH] Make echo UTF-8-clean Colin Watson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).