Mercurial > hg > toybox
comparison toys/pending/grep.c @ 970:55794a3d35c5
grep: add -w flag
author | Strake |
---|---|
date | Sun, 08 Jun 2003 10:09:05 -0500 |
parents | 8ad85a95f7c3 |
children | 31e91deb0824 |
comparison
equal
deleted
inserted
replaced
969:a47c6658210c | 970:55794a3d35c5 |
---|---|
3 * Copyright 2013 CE Strake <strake888 at gmail.com> | 3 * Copyright 2013 CE Strake <strake888 at gmail.com> |
4 * | 4 * |
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ | 5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ |
6 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html | 6 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html |
7 | 7 |
8 USE_GREP(NEWTOY(grep, "EFHahinosvclqe*f*m#", TOYFLAG_BIN)) | 8 USE_GREP(NEWTOY(grep, "EFHahinosvwclqe*f*m#", TOYFLAG_BIN)) |
9 | 9 |
10 config GREP | 10 config GREP |
11 bool "grep" | 11 bool "grep" |
12 default n | 12 default n |
13 help | 13 help |
14 usage: grep [-clq] [-EFHhinosv] (-e RE | -f REfile | RE) [file...] | 14 usage: grep [-clq] [-EFHhinosvw] (-e RE | -f REfile | RE) [file...] |
15 | 15 |
16 modes: | 16 modes: |
17 default: print lines from each file what match regular expression RE. | 17 default: print lines from each file what match regular expression RE. |
18 -c: print the number of matching lines in each file. | 18 -c: print the number of matching lines in each file. |
19 -l: print all matching file names. | 19 -l: print all matching file names. |
27 -i: case insensitive | 27 -i: case insensitive |
28 -n: print line numbers | 28 -n: print line numbers |
29 -o: print only matching part | 29 -o: print only matching part |
30 -s: keep silent on error | 30 -s: keep silent on error |
31 -v: invert match | 31 -v: invert match |
32 -w: match full word only | |
32 */ | 33 */ |
33 | 34 |
34 #define FOR_grep | 35 #define FOR_grep |
35 #include "toys.h" | 36 #include "toys.h" |
36 #include <regex.h> | 37 #include <regex.h> |
46 static void do_grep (int fd, char *name) { | 47 static void do_grep (int fd, char *name) { |
47 int n = 0, nMatch = 0; | 48 int n = 0, nMatch = 0; |
48 | 49 |
49 for (;;) { | 50 for (;;) { |
50 char *x, *y; | 51 char *x, *y; |
51 regmatch_t match; | 52 regmatch_t matches[3]; |
52 int atBOL = 1; | 53 int atBOL = 1; |
53 | 54 |
54 x = get_rawline (fd, 0, '\n'); | 55 x = get_rawline (fd, 0, '\n'); |
55 if (!x) break; | 56 if (!x) break; |
56 y = x; | 57 y = x; |
57 n++; /* start at 1 */ | 58 n++; /* start at 1 */ |
58 | 59 |
59 while (regexec (&re, y, 1, &match, atBOL ? 0 : REG_NOTBOL) == 0) { | 60 while (regexec (&re, y, 3, matches, atBOL ? 0 : REG_NOTBOL) == 0) { |
60 if (atBOL) nMatch++; | 61 if (atBOL) nMatch++; |
61 toys.exitval = 0; | 62 toys.exitval = 0; |
62 atBOL = 0; | 63 atBOL = 0; |
63 switch (TT.mode) { | 64 switch (TT.mode) { |
64 case 'q': | 65 case 'q': |
72 default: | 73 default: |
73 if (!(toys.optflags & FLAG_h)) printf ("%s:", name); | 74 if (!(toys.optflags & FLAG_h)) printf ("%s:", name); |
74 if ( (toys.optflags & FLAG_n)) printf ("%d:", n); | 75 if ( (toys.optflags & FLAG_n)) printf ("%d:", n); |
75 if (!(toys.optflags & FLAG_o)) fputs (x, stdout); | 76 if (!(toys.optflags & FLAG_o)) fputs (x, stdout); |
76 else { | 77 else { |
77 y += match.rm_so; | 78 y += matches[2].rm_so; |
78 printf ("%.*s\n", match.rm_eo - match.rm_so, y++); | 79 printf ("%.*s\n", matches[2].rm_eo - matches[2].rm_so, y++); |
79 } | 80 } |
80 } | 81 } |
81 if (!(toys.optflags & FLAG_o)) break; | 82 if (!(toys.optflags & FLAG_o)) break; |
82 } | 83 } |
83 | 84 |
139 } | 140 } |
140 TT.re_xs = toys.optflags & FLAG_F ? regfix (toys.optargs[0]) : toys.optargs[0]; | 141 TT.re_xs = toys.optflags & FLAG_F ? regfix (toys.optargs[0]) : toys.optargs[0]; |
141 toys.optc--; toys.optargs++; | 142 toys.optc--; toys.optargs++; |
142 } | 143 } |
143 | 144 |
145 TT.re_xs = xmsprintf (toys.optflags & FLAG_w ? "(^|[^_[:alnum:]])(%s)($|[^_[:alnum:]])" : "()(%s)()", TT.re_xs); | |
146 | |
144 if (regcomp (&re, TT.re_xs, | 147 if (regcomp (&re, TT.re_xs, |
145 (toys.optflags & (FLAG_E | FLAG_F) ? REG_EXTENDED : 0) | | 148 (toys.optflags & (FLAG_E | FLAG_F) ? REG_EXTENDED : 0) | |
146 (toys.optflags & FLAG_i ? REG_ICASE : 0)) != 0) { | 149 (toys.optflags & FLAG_i ? REG_ICASE : 0)) != 0) { |
147 toys.exitval = 2; | 150 toys.exitval = 2; |
148 error_exit ("bad RE"); | 151 error_exit ("bad RE"); |