Fixed a bug where "sed 's/foo/bar/g'" (i.e. a script w/o a -e)
authorErik Andersen <andersen@codepoet.org>
Mon, 6 Mar 2000 19:20:35 +0000 (19:20 -0000)
committerErik Andersen <andersen@codepoet.org>
Mon, 6 Mar 2000 19:20:35 +0000 (19:20 -0000)
would go into an infinite loop.
 -Erik

Changelog
editors/sed.c
sed.c

index 14524410c6598baeace296259be88cdf88a053e9..f0c5e2f69cdc6edf1d64c2e4b87a3c8dad33a4ec 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -35,6 +35,8 @@
            - Fixes to the makefile for handling "strip"
        * An initial telnet implementation was added by 
            Randolph Chung <tausq@debian.org>.
+       * Fixed a bug where "sed 's/foo/bar/g'" would go into an 
+           infinite loop.
 
 
        -Erik Andersen
index 608f868130adefe7d30f0a7d9c714cca95891b7a..2beb2a5888e4948dbde4582ab580aad6a1dcb58e 100644 (file)
@@ -184,129 +184,129 @@ extern int sed_main(int argc, char **argv)
        }
 
        while (argc > 1) {
-           if (**argv == '-') {
-                   argc--;
-                   cp = *argv++;
-                   stopNow = FALSE;
-
-                   while (*++cp && stopNow == FALSE) {
-                           switch (*cp) {
-                           case 'n':
-                                   quietFlag = TRUE;
-                                   break;
-                       case 'e':
-                               if (*(cp + 1) == 0 && --argc < 0) {
-                                       usage(sed_usage);
-                               }
-                               if (*++cp != 's')
-                                       cp = *argv++;
-    
-                               /* Read address if present */
-                               SKIPSPACES(cp);
-                               if (*cp == '$') {
-                                       addr_line = LAST_LINE;
-                                       cp++;
-                               } else {
-                                       if (isdigit(*cp)) {     /* LINE ADDRESS   */
-                                               line_s = cp;
-                                               while (isdigit(*cp))
-                                                       cp++;
-                                               if (cp > line_s) {
-                                                       /* numeric line */
-                                                       saved = *cp;
-                                                       *cp = '\0';
-                                                       addr_line = atoi(line_s);
-                                                       *cp = saved;
-                                               }
-                                       } else if (*cp == '/') {        /* PATTERN ADDRESS */
-                                               pos = addr_pattern = cp + 1;
-                                               pos = strchr(pos, '/');
-                                               if (!pos)
-                                                       usage(sed_usage);
-                                               *pos = '\0';
-                                               cp = pos + 1;
-                                       }
-                               }
-    
-                               SKIPSPACES(cp);
-                               if (*cp == '!') {
-                                       negated++;
-                                       cp++;
-                               }
-    
-                               /* Read command */
-    
-                               SKIPSPACES(cp);
-                               switch (*cp) {
-                               case 's':               /* REPLACE */
-                                       if (strlen(cp) <= 3 || *(cp + 1) != '/')
-                                               break;
-                                       sed_f = f_replace;
-    
-                                       pos = needle = cp + 2;
-    
-                                       for (;;) {
-                                               pos = strchr(pos, '/');
-                                               if (pos == NULL) {
-                                                       usage(sed_usage);
-                                               }
-                                               if (*(pos - 1) == '\\') {
-                                                       pos++;
-                                                       continue;
-                                               }
-                                               break;
-                                       }
-                                       *pos = 0;
-                                       newNeedle = ++pos;
-                                       for (;;) {
-                                               pos = strchr(pos, '/');
-                                               if (pos == NULL) {
-                                                       usage(sed_usage);
-                                               }
-                                               if (*(pos - 1) == '\\') {
-                                                       pos++;
-                                                       continue;
-                                               }
-                                               break;
-                                       }
-                                       *pos = 0;
-                                       if (pos + 2 != 0) {
-                                               while (*++pos) {
-                                                       switch (*pos) {
-                                                       case 'i':
-                                                               ignoreCase = TRUE;
-                                                               break;
-                                                       case 'p':
-                                                               printFlag = TRUE;
-                                                               break;
-                                                       case 'g':
-                                                               break;
-                                                       default:
-                                                               usage(sed_usage);
-                                                       }
-                                               }
-                                       }
-                                       cp = pos;
-                                       /* fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle); */
-                                       break;
-    
-                               case 'a':               /* APPEND */
-                                       if (strlen(cp) < 2)
-                                               break;
-                                       sed_f = f_append;
-                                       appendline = ++cp;
-                                       /* fprintf(stderr, "append '%s'\n", appendline); */
-                                       break;
-                               }
-    
-                               stopNow = TRUE;
-                               break;
-    
-                       default:
-                               usage(sed_usage);
-                       }
-               }
-       }
+               if (**argv != '-')
+                       usage(sed_usage);
+               argc--;
+               cp = *argv++;
+               stopNow = FALSE;
+
+               while (*++cp && stopNow == FALSE) {
+                       switch (*cp) {
+                       case 'n':
+                               quietFlag = TRUE;
+                               break;
+                       case 'e':
+                               if (*(cp + 1) == 0 && --argc < 0) {
+                                       usage(sed_usage);
+                               }
+                               if (*++cp != 's')
+                                       cp = *argv++;
+
+                               /* Read address if present */
+                               SKIPSPACES(cp);
+                               if (*cp == '$') {
+                                       addr_line = LAST_LINE;
+                                       cp++;
+                               } else {
+                                       if (isdigit(*cp)) {     /* LINE ADDRESS   */
+                                               line_s = cp;
+                                               while (isdigit(*cp))
+                                                       cp++;
+                                               if (cp > line_s) {
+                                                       /* numeric line */
+                                                       saved = *cp;
+                                                       *cp = '\0';
+                                                       addr_line = atoi(line_s);
+                                                       *cp = saved;
+                                               }
+                                       } else if (*cp == '/') {        /* PATTERN ADDRESS */
+                                               pos = addr_pattern = cp + 1;
+                                               pos = strchr(pos, '/');
+                                               if (!pos)
+                                                       usage(sed_usage);
+                                               *pos = '\0';
+                                               cp = pos + 1;
+                                       }
+                               }
+
+                               SKIPSPACES(cp);
+                               if (*cp == '!') {
+                                       negated++;
+                                       cp++;
+                               }
+
+                               /* Read command */
+
+                               SKIPSPACES(cp);
+                               switch (*cp) {
+                               case 's':               /* REPLACE */
+                                       if (strlen(cp) <= 3 || *(cp + 1) != '/')
+                                               break;
+                                       sed_f = f_replace;
+
+                                       pos = needle = cp + 2;
+
+                                       for (;;) {
+                                               pos = strchr(pos, '/');
+                                               if (pos == NULL) {
+                                                       usage(sed_usage);
+                                               }
+                                               if (*(pos - 1) == '\\') {
+                                                       pos++;
+                                                       continue;
+                                               }
+                                               break;
+                                       }
+                                       *pos = 0;
+                                       newNeedle = ++pos;
+                                       for (;;) {
+                                               pos = strchr(pos, '/');
+                                               if (pos == NULL) {
+                                                       usage(sed_usage);
+                                               }
+                                               if (*(pos - 1) == '\\') {
+                                                       pos++;
+                                                       continue;
+                                               }
+                                               break;
+                                       }
+                                       *pos = 0;
+                                       if (pos + 2 != 0) {
+                                               while (*++pos) {
+                                                       switch (*pos) {
+                                                       case 'i':
+                                                               ignoreCase = TRUE;
+                                                               break;
+                                                       case 'p':
+                                                               printFlag = TRUE;
+                                                               break;
+                                                       case 'g':
+                                                               break;
+                                                       default:
+                                                               usage(sed_usage);
+                                                       }
+                                               }
+                                       }
+                                       cp = pos;
+                                       /* fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle); */
+                                       break;
+
+                               case 'a':               /* APPEND */
+                                       if (strlen(cp) < 2)
+                                               break;
+                                       sed_f = f_append;
+                                       appendline = ++cp;
+                                       /* fprintf(stderr, "append '%s'\n", appendline); */
+                                       break;
+                               }
+
+                               stopNow = TRUE;
+                               break;
+
+                       default:
+                               usage(sed_usage);
+                       }
+               }
     }
 
        if (argc == 0) {
diff --git a/sed.c b/sed.c
index 608f868130adefe7d30f0a7d9c714cca95891b7a..2beb2a5888e4948dbde4582ab580aad6a1dcb58e 100644 (file)
--- a/sed.c
+++ b/sed.c
@@ -184,129 +184,129 @@ extern int sed_main(int argc, char **argv)
        }
 
        while (argc > 1) {
-           if (**argv == '-') {
-                   argc--;
-                   cp = *argv++;
-                   stopNow = FALSE;
-
-                   while (*++cp && stopNow == FALSE) {
-                           switch (*cp) {
-                           case 'n':
-                                   quietFlag = TRUE;
-                                   break;
-                       case 'e':
-                               if (*(cp + 1) == 0 && --argc < 0) {
-                                       usage(sed_usage);
-                               }
-                               if (*++cp != 's')
-                                       cp = *argv++;
-    
-                               /* Read address if present */
-                               SKIPSPACES(cp);
-                               if (*cp == '$') {
-                                       addr_line = LAST_LINE;
-                                       cp++;
-                               } else {
-                                       if (isdigit(*cp)) {     /* LINE ADDRESS   */
-                                               line_s = cp;
-                                               while (isdigit(*cp))
-                                                       cp++;
-                                               if (cp > line_s) {
-                                                       /* numeric line */
-                                                       saved = *cp;
-                                                       *cp = '\0';
-                                                       addr_line = atoi(line_s);
-                                                       *cp = saved;
-                                               }
-                                       } else if (*cp == '/') {        /* PATTERN ADDRESS */
-                                               pos = addr_pattern = cp + 1;
-                                               pos = strchr(pos, '/');
-                                               if (!pos)
-                                                       usage(sed_usage);
-                                               *pos = '\0';
-                                               cp = pos + 1;
-                                       }
-                               }
-    
-                               SKIPSPACES(cp);
-                               if (*cp == '!') {
-                                       negated++;
-                                       cp++;
-                               }
-    
-                               /* Read command */
-    
-                               SKIPSPACES(cp);
-                               switch (*cp) {
-                               case 's':               /* REPLACE */
-                                       if (strlen(cp) <= 3 || *(cp + 1) != '/')
-                                               break;
-                                       sed_f = f_replace;
-    
-                                       pos = needle = cp + 2;
-    
-                                       for (;;) {
-                                               pos = strchr(pos, '/');
-                                               if (pos == NULL) {
-                                                       usage(sed_usage);
-                                               }
-                                               if (*(pos - 1) == '\\') {
-                                                       pos++;
-                                                       continue;
-                                               }
-                                               break;
-                                       }
-                                       *pos = 0;
-                                       newNeedle = ++pos;
-                                       for (;;) {
-                                               pos = strchr(pos, '/');
-                                               if (pos == NULL) {
-                                                       usage(sed_usage);
-                                               }
-                                               if (*(pos - 1) == '\\') {
-                                                       pos++;
-                                                       continue;
-                                               }
-                                               break;
-                                       }
-                                       *pos = 0;
-                                       if (pos + 2 != 0) {
-                                               while (*++pos) {
-                                                       switch (*pos) {
-                                                       case 'i':
-                                                               ignoreCase = TRUE;
-                                                               break;
-                                                       case 'p':
-                                                               printFlag = TRUE;
-                                                               break;
-                                                       case 'g':
-                                                               break;
-                                                       default:
-                                                               usage(sed_usage);
-                                                       }
-                                               }
-                                       }
-                                       cp = pos;
-                                       /* fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle); */
-                                       break;
-    
-                               case 'a':               /* APPEND */
-                                       if (strlen(cp) < 2)
-                                               break;
-                                       sed_f = f_append;
-                                       appendline = ++cp;
-                                       /* fprintf(stderr, "append '%s'\n", appendline); */
-                                       break;
-                               }
-    
-                               stopNow = TRUE;
-                               break;
-    
-                       default:
-                               usage(sed_usage);
-                       }
-               }
-       }
+               if (**argv != '-')
+                       usage(sed_usage);
+               argc--;
+               cp = *argv++;
+               stopNow = FALSE;
+
+               while (*++cp && stopNow == FALSE) {
+                       switch (*cp) {
+                       case 'n':
+                               quietFlag = TRUE;
+                               break;
+                       case 'e':
+                               if (*(cp + 1) == 0 && --argc < 0) {
+                                       usage(sed_usage);
+                               }
+                               if (*++cp != 's')
+                                       cp = *argv++;
+
+                               /* Read address if present */
+                               SKIPSPACES(cp);
+                               if (*cp == '$') {
+                                       addr_line = LAST_LINE;
+                                       cp++;
+                               } else {
+                                       if (isdigit(*cp)) {     /* LINE ADDRESS   */
+                                               line_s = cp;
+                                               while (isdigit(*cp))
+                                                       cp++;
+                                               if (cp > line_s) {
+                                                       /* numeric line */
+                                                       saved = *cp;
+                                                       *cp = '\0';
+                                                       addr_line = atoi(line_s);
+                                                       *cp = saved;
+                                               }
+                                       } else if (*cp == '/') {        /* PATTERN ADDRESS */
+                                               pos = addr_pattern = cp + 1;
+                                               pos = strchr(pos, '/');
+                                               if (!pos)
+                                                       usage(sed_usage);
+                                               *pos = '\0';
+                                               cp = pos + 1;
+                                       }
+                               }
+
+                               SKIPSPACES(cp);
+                               if (*cp == '!') {
+                                       negated++;
+                                       cp++;
+                               }
+
+                               /* Read command */
+
+                               SKIPSPACES(cp);
+                               switch (*cp) {
+                               case 's':               /* REPLACE */
+                                       if (strlen(cp) <= 3 || *(cp + 1) != '/')
+                                               break;
+                                       sed_f = f_replace;
+
+                                       pos = needle = cp + 2;
+
+                                       for (;;) {
+                                               pos = strchr(pos, '/');
+                                               if (pos == NULL) {
+                                                       usage(sed_usage);
+                                               }
+                                               if (*(pos - 1) == '\\') {
+                                                       pos++;
+                                                       continue;
+                                               }
+                                               break;
+                                       }
+                                       *pos = 0;
+                                       newNeedle = ++pos;
+                                       for (;;) {
+                                               pos = strchr(pos, '/');
+                                               if (pos == NULL) {
+                                                       usage(sed_usage);
+                                               }
+                                               if (*(pos - 1) == '\\') {
+                                                       pos++;
+                                                       continue;
+                                               }
+                                               break;
+                                       }
+                                       *pos = 0;
+                                       if (pos + 2 != 0) {
+                                               while (*++pos) {
+                                                       switch (*pos) {
+                                                       case 'i':
+                                                               ignoreCase = TRUE;
+                                                               break;
+                                                       case 'p':
+                                                               printFlag = TRUE;
+                                                               break;
+                                                       case 'g':
+                                                               break;
+                                                       default:
+                                                               usage(sed_usage);
+                                                       }
+                                               }
+                                       }
+                                       cp = pos;
+                                       /* fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle); */
+                                       break;
+
+                               case 'a':               /* APPEND */
+                                       if (strlen(cp) < 2)
+                                               break;
+                                       sed_f = f_append;
+                                       appendline = ++cp;
+                                       /* fprintf(stderr, "append '%s'\n", appendline); */
+                                       break;
+                               }
+
+                               stopNow = TRUE;
+                               break;
+
+                       default:
+                               usage(sed_usage);
+                       }
+               }
     }
 
        if (argc == 0) {