echo, printf: interpret \e as the Escape character
authorPádraig Brady <P@draigBrady.com>
Tue, 27 Oct 2009 10:04:34 +0000 (10:04 +0000)
committerPádraig Brady <P@draigBrady.com>
Wed, 28 Oct 2009 16:37:07 +0000 (16:37 +0000)
Match gcc, perl, bash, ksh, tcsh, ... in supporting \e.
* src/printf.c (print_escape_char): Output \x1B when \e encountered.
* src/echo.c (main): Likewise.
* src/stat.c (print_escape_char): Likewise.
* doc/coreutils.texi (echo invocation): Add \e to the list.
* tests/misc/printf: Verify that \e outputs \x1B.
* NEWS: Mention the change in behaviour.

NEWS
doc/coreutils.texi
src/echo.c
src/printf.c
src/stat.c
tests/misc/printf

diff --git a/NEWS b/NEWS
index 35d1413..abf2466 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -43,6 +43,8 @@ GNU coreutils NEWS                                    -*- outline -*-
   with the invoked command failing with status 1.  Likewise, nohup
   fails with status 125 instead of 127.
 
+  echo and printf now interpret \e as the Escape character (0x1B).
+
 ** New features
 
   env and printenv now accept the option --null (-0), as a means to
index 9b9f73b..df7e963 100644 (file)
@@ -10800,6 +10800,8 @@ alert (bell)
 backspace
 @item \c
 produce no further output
+@item \e
+escape
 @item \f
 form feed
 @item \n
index 90b9786..65e8c80 100644 (file)
@@ -70,6 +70,7 @@ If -e is in effect, the following sequences are recognized:\n\
 "), stdout);
       fputs (_("\
   \\c     produce no further output\n\
+  \\e     escape\n\
   \\f     form feed\n\
   \\n     new line\n\
   \\r     carriage return\n\
@@ -203,6 +204,7 @@ just_echo:
                     case 'a': c = '\a'; break;
                     case 'b': c = '\b'; break;
                     case 'c': exit (EXIT_SUCCESS);
+                    case 'e': c = '\x1B'; break;
                     case 'f': c = '\f'; break;
                     case 'n': c = '\n'; break;
                     case 'r': c = '\r'; break;
index f6f86a5..540d7db 100644 (file)
@@ -25,6 +25,7 @@
    \a = alert (bell)
    \b = backspace
    \c = produce no further output
+   \e = escape
    \f = form feed
    \n = new line
    \r = carriage return
@@ -108,6 +109,7 @@ FORMAT controls the output as in C printf.  Interpreted sequences are:\n\
   \\a      alert (BEL)\n\
   \\b      backspace\n\
   \\c      produce no further output\n\
+  \\e      escape\n\
   \\f      form feed\n\
 "), stdout);
       fputs (_("\
@@ -200,6 +202,9 @@ print_esc_char (char c)
     case 'c':                  /* Cancel the rest of the output. */
       exit (EXIT_SUCCESS);
       break;
+    case 'e':                  /* Escape. */
+      putchar ('\x1B');
+      break;
     case 'f':                  /* Form feed. */
       putchar ('\f');
       break;
@@ -256,7 +261,7 @@ print_esc (const char *escstart, bool octal_0)
         esc_value = esc_value * 8 + octtobin (*p);
       putchar (esc_value);
     }
-  else if (*p && strchr ("\"\\abcfnrtv", *p))
+  else if (*p && strchr ("\"\\abcefnrtv", *p))
     print_esc_char (*p++);
   else if (*p == 'u' || *p == 'U')
     {
index ae54911..35f5d66 100644 (file)
@@ -723,6 +723,9 @@ print_esc_char (char c)
     case 'b':                  /* Backspace. */
       c ='\b';
       break;
+    case 'e':                  /* Escape. */
+      c ='\x1B';
+      break;
     case 'f':                  /* Form feed. */
       c ='\f';
       break;
index e02a132..8961b44 100755 (executable)
@@ -28,6 +28,9 @@ getlimits_
 
 fail=0
 
+# Verify the 3 methods of specifying "Escape":
+test $("$prog" "\x1b\n\33\n\e\n" | uniq -u) && fail=1
+
 # This would fail (by printing the `--') for printf in sh-utils
 # and in coreutils 4.5.1.
 "$prog" -- 'foo\n' > out || fail=1