[Toybox] Still poking at grep -ABC

Rob Landley rob at landley.net
Wed Jan 1 13:17:24 PST 2014


More grep corner cases:

   echo -e "one\ntwo\none\none\ntwo\none" | grep -C 1 two
   one
   two
   one
   one
   two
   one

Does not have a -- between the blocks, so printing the -- only happens 
after exhausting the trailing lines, refilling the -B list, and then the 
next line _not_ matching.

Ooh, here's a fun one:

$ grep -B 2 -b cross README
673-The CROSS_COMPILE argument is optional, and without it builds a 
version of
748-toybox to run on the current machine. Cross compiling requires an 
appropriately
828:prefixed cross compiler toolchain, several example toolchains are 
available at:
--
945-
946-For the "CROSS_COMPILE=armv5l-" example above, download
1002:cross-compiler-armv5l.tar.bz2, extract it, and add its "bin" 
subdirectory to
--
1151-includes a dash.)
1169-
1170:For more about cross compiling, see:
1207-
1208:  http://landley.net/writing/docs/cross-compiling.html

So I have to record the byte offsets of previous lines, and note the : 
vs - distinction in the output.

On the bright side, the -o partial line displaying logic only shows 
matches so we don't have to track trailing stuff for that. (Which makes 
it a lot easier becaue the prefix display logic is full of -o machinery, 
and we want to feed the trailing lines into the prefix display logic 
rather than reimplent it, so if we just set TT.B = 0 when FLAG_o is set 
(or FLAG_l because that returns early and would leak the trailing line 
allocations, and _also_ never displays them)...)


Hmmm, while we're at it:

       mmatch++;
       toys.exitval = 0;
       if (toys.optflags & FLAG_q) xexit();
       if (toys.optflags & FLAG_l) {
         printf("%s%c", name, outdelim);
         free(line);
         fclose(file);
         return;
       }
       if (toys.optflags & FLAG_o)
         if (matches[which].rm_eo == matches[which].rm_so)
           break;

We got a match, and incremented the counter of the number of matches. 
But if the start and end of a -o match are equal, we don't display 
anything. So... does it still count as a match? Apparently so. What 
would a test case for that look like. Matching a whole blank line with 
-o. Let's see...

   $ echo -e "one\n\ntwo" | grep -o ""
   $ echo -e "one\n\ntwo" | grep -oc ""
   3
   $ echo -e "one\n\ntwo" | grep -o 'o*$'
   o
   $ echo -e "one\n\ntwo" | grep -oc 'o*$'
   3


Ok, that's just _sad_, but it's implementing the correct behavior...

I don't suppose anybody would like to save me the trouble of updating 
the test suite for this giant digression and the previous messages on 
this topic? :)


More information about the Toybox mailing list