changeset 1000:99dad9fb5613

More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
author Rob Landley <rob@landley.net>
date Mon, 12 Aug 2013 03:08:56 -0500
parents 0af2375a8ef8
children 8b49ff103af9
files toys/pending/grep.c
diffstat 1 files changed, 22 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/toys/pending/grep.c	Mon Aug 12 01:48:27 2013 -0500
+++ b/toys/pending/grep.c	Mon Aug 12 03:08:56 2013 -0500
@@ -55,6 +55,8 @@
   long offset = 0;
   int lcount = 0, mcount = 0, which = toys.optflags & FLAG_w ? 2 : 0;
 
+  if (!fd) name = "(standard input)";
+
   if (!file) {
     perror_msg("%s", name);
     return;
@@ -164,40 +166,49 @@
 
 static void parse_regex(void)
 {
-  struct arg_list *al;
+  struct arg_list *al, *new, *list = NULL;
   long len = 0;
   char *s, *ss;
 
   // Add all -f lines to -e list. (Yes, this is leaking allocation context for
   // exit to free. Not supporting nofork for this command any time soon.)
-  for (al = TT.f; al; al = al->next) {
-    s = ss = xreadfile(al->arg);
+  al = TT.f ? TT.f : TT.e;
+  while (al) {
+    if (TT.f) s = ss = xreadfile(al->arg);
+    else s = ss = al->arg;
 
     while (ss && *s) {
       ss = strchr(s, '\n');
-      if (ss) *ss = 0;
-      al = xmalloc(sizeof(struct arg_list));
-      al->next = TT.e;
-      al->arg = s;
-      TT.e = al;
+      if (ss) *(ss++) = 0;
+      new = xmalloc(sizeof(struct arg_list));
+      new->next = list;
+      new->arg = s;
+      list = new;
       s = ss;
     }
+    al = al->next;
+    if (!al && TT.f) {
+      TT.f = 0;
+      al = TT.e;
+    }
   }
+  TT.e = list;
 
   if (!(toys.optflags & FLAG_F)) {
     int w = toys.optflags & FLAG_w;
 
-    // Convert strings to one big regex string.
+    // Convert strings to one big regex
+    if (w) len = 36;
     for (al = TT.e; al; al = al->next) len += strlen(al->arg)+1;
-    if (w) len = 36;
 
     TT.regstr = s = xmalloc(len);
     if (w) s = stpcpy(s, "(^|[^_[:alnum:]])(");
     for (al = TT.e; al; al = al->next) {
       s = stpcpy(s, al->arg);
+      if (!(toys.optflags & FLAG_E)) *(s++) = '\\';
       *(s++) = '|';
     }
-    *(--s) = 0;
+    *(s-=(1+!(toys.optflags & FLAG_E))) = 0;
     if (w) strcpy(s, ")($|[^_[:alnum:]])");
 
     w = regcomp((regex_t *)toybuf, TT.regstr,