changeset 1558:47647eebc5f9 draft

Fix sed 'b' with no label and 'N' in general.
author Rob Landley <rob@landley.net>
date Sun, 16 Nov 2014 14:21:42 -0600
parents 6e720e1e833c
children 9b2402162464
files toys/pending/sed.c
diffstat 1 files changed, 9 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/toys/pending/sed.c	Sat Nov 15 17:19:23 2014 -0600
+++ b/toys/pending/sed.c	Sun Nov 16 14:21:42 2014 -0600
@@ -3,6 +3,9 @@
  * Copyright 2014 Rob Landley <rob@landley.net>
  *
  * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html
+ *
+ * TODO: lines > 2G could signed int wrap length counters. Not just getline()
+ * but N and s///
 
 USE_SED(NEWTOY(sed, "(version)e*f*inr", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE))
 
@@ -335,10 +338,8 @@
 
       if (c != 'b') tea = 0;
       if (c=='b' || t^(c=='T')) {
+        if (!logrus->arg1) break;
         str = logrus->arg1+(char *)logrus;
-
-        
-        if (!*str) break;
         for (logrus = (void *)TT.pattern; logrus; logrus = logrus->next)
           if (logrus->c == ':' && !strcmp(logrus->arg1+(char *)logrus, str))
             break;
@@ -388,14 +389,18 @@
 
       break;
     } else if (c=='N') {
+      // Can't just grab next line because we could have multiple N and
+      // we need to actually read ahead to get N;$p EOF detection right.
       if (pline) {
         TT.restart = logrus->next;
-        extend_string(&line, TT.nextline, plen, -TT.nextlen);
+        extend_string(&line, TT.nextline, len, -TT.nextlen);
         free(TT.nextline);
         TT.nextline = line;
+        TT.nextlen += len + 1;
         line = 0;
       }
 
+      // Pending append goes out right after N
       goto done; 
     } else if (c=='p') {
       if (emit(line, len, eol)) break;