* Fix to tr so it recognizes standard escape sequences. Merged common
authorEric Andersen <andersen@codepoet.org>
Wed, 5 Jul 2000 17:26:35 +0000 (17:26 -0000)
committerEric Andersen <andersen@codepoet.org>
Wed, 5 Jul 2000 17:26:35 +0000 (17:26 -0000)
    escape seq. code from tr and echo into utility.c.  Fix thanks to
    Matt Kraai <kraai@alumni.carnegiemellon.edu>.
* This should close Bug #1015.  Please test.
 -Erik

Changelog
coreutils/echo.c
coreutils/tr.c
echo.c
internal.h
tr.c
utility.c

index d18244215d9be3acc4d43a024dae884061ba3974..d147196cf49784c5e18aa12ffb83720e79f70874 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -24,6 +24,9 @@
            of thing.  Patch thanks to David Vrabel <dvrabel@arcom.co.uk>
        * Fix to init.c from Stuart Menefy <Stuart.Menefy@st.com> so that
            it always sets the controlling terminal before running any programs
+       * Fix to tr so it recognizes standard escape sequences.  Merged common
+           escape seq. code from tr and echo into utility.c.  Fix thanks to
+           Matt Kraai <kraai@alumni.carnegiemellon.edu>.
 
         -Erik Andersen
 
index 4659e4bc627f9f07dae50515440fdc1a8489e4dd..6e279d1c6d332431249039bc7909ed76be635049 100644 (file)
@@ -40,7 +40,7 @@ extern int
 echo_main(int argc, char** argv)
 {
        register char **ap;
-       register char *p;
+       char *p;
        register char c;
        int nflag = 0;
        int eflag = 0;
@@ -65,28 +65,10 @@ echo_main(int argc, char** argv)
        while ((p = *ap++) != NULL) {
                while ((c = *p++) != '\0') {
                        if (c == '\\' && eflag) {
-                               switch (c = *p++) {
-                               case 'a':  c = '\007'; break;
-                               case 'b':  c = '\b';  break;
-                               case 'c':  exit( 0);            /* exit */
-                               case 'f':  c = '\f';  break;
-                               case 'n':  c = '\n';  break;
-                               case 'r':  c = '\r';  break;
-                               case 't':  c = '\t';  break;
-                               case 'v':  c = '\v';  break;
-                               case '\\':  break;              /* c = '\\' */
-                               case '0': case '1': case '2': case '3':
-                               case '4': case '5': case '6': case '7':
-                                       c -= '0';
-                                       if (*p >= '0' && *p <= '7')
-                                               c = c * 8 + (*p++ - '0');
-                                       if (*p >= '0' && *p <= '7')
-                                       c = c * 8 + (*p++ - '0');
-                                       break;
-                               default:
-                                       p--;
-                                       break;
-                               }
+                               if (*p == 'c')
+                                       exit(0);
+                               else
+                                       c = process_escape_sequence(&p);
                        }
                        putchar(c);
                }
index 3e7ba583c3eea6d0e317d814e23436e2d480fce2..48cdd47bddfe5503c48e18ea5a6870cf3c694c3b 100644 (file)
@@ -111,22 +111,14 @@ static void map(register unsigned char *string1, register unsigned char *string2
        }
 }
 
-static void expand(register char *arg, register unsigned char *buffer)
+static void expand(char *arg, register unsigned char *buffer)
 {
        int i, ac;
 
        while (*arg) {
                if (*arg == '\\') {
                        arg++;
-                       i = ac = 0;
-                       if (*arg >= '0' && *arg <= '7') {
-                               do {
-                                       ac = (ac << 3) + *arg++ - '0';
-                                       i++;
-                               } while (i < 4 && *arg >= '0' && *arg <= '7');
-                               *buffer++ = ac;
-                       } else if (*arg != '\0')
-                               *buffer++ = *arg++;
+                       *buffer++ = process_escape_sequence(&arg);
                } else if (*arg == '[') {
                        arg++;
                        i = *arg++;
diff --git a/echo.c b/echo.c
index 4659e4bc627f9f07dae50515440fdc1a8489e4dd..6e279d1c6d332431249039bc7909ed76be635049 100644 (file)
--- a/echo.c
+++ b/echo.c
@@ -40,7 +40,7 @@ extern int
 echo_main(int argc, char** argv)
 {
        register char **ap;
-       register char *p;
+       char *p;
        register char c;
        int nflag = 0;
        int eflag = 0;
@@ -65,28 +65,10 @@ echo_main(int argc, char** argv)
        while ((p = *ap++) != NULL) {
                while ((c = *p++) != '\0') {
                        if (c == '\\' && eflag) {
-                               switch (c = *p++) {
-                               case 'a':  c = '\007'; break;
-                               case 'b':  c = '\b';  break;
-                               case 'c':  exit( 0);            /* exit */
-                               case 'f':  c = '\f';  break;
-                               case 'n':  c = '\n';  break;
-                               case 'r':  c = '\r';  break;
-                               case 't':  c = '\t';  break;
-                               case 'v':  c = '\v';  break;
-                               case '\\':  break;              /* c = '\\' */
-                               case '0': case '1': case '2': case '3':
-                               case '4': case '5': case '6': case '7':
-                                       c -= '0';
-                                       if (*p >= '0' && *p <= '7')
-                                               c = c * 8 + (*p++ - '0');
-                                       if (*p >= '0' && *p <= '7')
-                                       c = c * 8 + (*p++ - '0');
-                                       break;
-                               default:
-                                       p--;
-                                       break;
-                               }
+                               if (*p == 'c')
+                                       exit(0);
+                               else
+                                       c = process_escape_sequence(&p);
                        }
                        putchar(c);
                }
index 41a72e18a79dd70f56bef98463aa79f7cdb5ec2f..27be05ee284bba86dc5fb1bc693ef00e1eb243c7 100644 (file)
@@ -259,6 +259,7 @@ extern pid_t* findPidByName( char* pidName);
 extern void *xmalloc (size_t size);
 extern int find_real_root_device_name(char* name);
 extern char *get_line_from_file(FILE *file);
+extern char process_escape_sequence(char **ptr);
 
 /* These parse entries in /etc/passwd and /etc/group.  This is desirable
  * for BusyBox since we want to avoid using the glibc NSS stuff, which
diff --git a/tr.c b/tr.c
index 3e7ba583c3eea6d0e317d814e23436e2d480fce2..48cdd47bddfe5503c48e18ea5a6870cf3c694c3b 100644 (file)
--- a/tr.c
+++ b/tr.c
@@ -111,22 +111,14 @@ static void map(register unsigned char *string1, register unsigned char *string2
        }
 }
 
-static void expand(register char *arg, register unsigned char *buffer)
+static void expand(char *arg, register unsigned char *buffer)
 {
        int i, ac;
 
        while (*arg) {
                if (*arg == '\\') {
                        arg++;
-                       i = ac = 0;
-                       if (*arg >= '0' && *arg <= '7') {
-                               do {
-                                       ac = (ac << 3) + *arg++ - '0';
-                                       i++;
-                               } while (i < 4 && *arg >= '0' && *arg <= '7');
-                               *buffer++ = ac;
-                       } else if (*arg != '\0')
-                               *buffer++ = *arg++;
+                       *buffer++ = process_escape_sequence(&arg);
                } else if (*arg == '[') {
                        arg++;
                        i = *arg++;
index de53dbda8a5ec9f6ad0e752f885f4ea63c6b537a..f3af01f44c375e5117f5b1322ba2ddb4ff291198 100644 (file)
--- a/utility.c
+++ b/utility.c
@@ -1617,6 +1617,51 @@ extern char *get_line_from_file(FILE *file)
        return linebuf;
 }
 
+#if defined BB_ECHO || defined BB_TR
+char process_escape_sequence(char **ptr)
+{
+       char c;
+
+       switch (c = *(*ptr)++) {
+       case 'a':
+               c = '\a';
+               break;
+       case 'b':
+               c = '\b';
+               break;
+       case 'f':
+               c = '\f';
+               break;
+       case 'n':
+               c = '\n';
+               break;
+       case 't':
+               c = '\t';
+               break;
+       case 'v':
+               c = '\v';
+               break;
+       case '\\':
+               c = '\\';
+               break;
+       case '0': case '1': case '2': case '3':
+       case '4': case '5': case '6': case '7':
+               c -= '0';
+               if ('0' <= **ptr && **ptr <= '7') {
+                       c = c * 8 + (*(*ptr)++ - '0');
+                       if ('0' <= **ptr && **ptr <= '7')
+                               c = c * 8 + (*(*ptr)++ - '0');
+               }
+               break;
+       default:
+               (*ptr)--;
+               c = '\\';
+               break;
+       }
+       return c;
+}
+#endif
+
 /* END CODE */
 /*
 Local Variables: