annotate toys/posix/grep.c @ 1173:140aa98d9816 draft

Add -zZ support to grep (NUL delimited input/output).
author Rob Landley <rob@landley.net>
date Sun, 29 Dec 2013 11:07:23 -0600
parents 8afe1fde9314
children 6ca31490f581
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
948
55e587acefa9 add grep
Strake
parents:
diff changeset
1 /* grep.c - print lines what match given regular expression
55e587acefa9 add grep
Strake
parents:
diff changeset
2 *
55e587acefa9 add grep
Strake
parents:
diff changeset
3 * Copyright 2013 CE Strake <strake888 at gmail.com>
55e587acefa9 add grep
Strake
parents:
diff changeset
4 *
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html
948
55e587acefa9 add grep
Strake
parents:
diff changeset
6
1173
140aa98d9816 Add -zZ support to grep (NUL delimited input/output).
Rob Landley <rob@landley.net>
parents: 1170
diff changeset
7 USE_GREP(NEWTOY(grep, "ZzEFHabhinorsvwclqe*f*m#x[!wx][!EFw]", TOYFLAG_BIN))
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
8 USE_GREP(OLDTOY(egrep, grep, OPTSTR_grep, TOYFLAG_BIN))
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
9 USE_GREP(OLDTOY(fgrep, grep, OPTSTR_grep, TOYFLAG_BIN))
948
55e587acefa9 add grep
Strake
parents:
diff changeset
10
55e587acefa9 add grep
Strake
parents:
diff changeset
11 config GREP
55e587acefa9 add grep
Strake
parents:
diff changeset
12 bool "grep"
1018
133ef0885cce Move grep from pending to posix, switch default to y.
Rob Landley <rob@landley.net>
parents: 1017
diff changeset
13 default y
948
55e587acefa9 add grep
Strake
parents:
diff changeset
14 help
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
15 usage: grep [-EFivwcloqsHbhn] [-m MAX] [-e REGEX]... [-f REGFILE] [FILE]...
948
55e587acefa9 add grep
Strake
parents:
diff changeset
16
983
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
17 Show lines matching regular expressions. If no -e, first argument is
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
18 regular expression to match. With no files (or "-" filename) read stdin.
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
19 Returns 0 if matched, 1 if no match found.
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
20
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
21 -e Regex to match. (May be repeated.)
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
22 -f File containing regular expressions to match.
948
55e587acefa9 add grep
Strake
parents:
diff changeset
23
983
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
24 match type:
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
25 -E extended regex syntax -F fixed (match literal string)
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
26 -i case insensitive -m stop after this many lines matched
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
27 -r recursive (on dir) -v invert match
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
28 -w whole word (implies -E) -x whole line
1173
140aa98d9816 Add -zZ support to grep (NUL delimited input/output).
Rob Landley <rob@landley.net>
parents: 1170
diff changeset
29 -z input NUL terminated
983
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
30
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
31 display modes: (default: matched line)
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
32 -c count of matching lines -l show matching filenames
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
33 -o only matching part -q quiet (errors only)
1173
140aa98d9816 Add -zZ support to grep (NUL delimited input/output).
Rob Landley <rob@landley.net>
parents: 1170
diff changeset
34 -s silent (no error msg) -Z output NUL terminated
983
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
35
1173
140aa98d9816 Add -zZ support to grep (NUL delimited input/output).
Rob Landley <rob@landley.net>
parents: 1170
diff changeset
36 output prefix (default: filename if checking more than 1 file)
983
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
37 -H force filename -b byte offset of match
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
38 -h hide filename -n line number of match
948
55e587acefa9 add grep
Strake
parents:
diff changeset
39 */
55e587acefa9 add grep
Strake
parents:
diff changeset
40
55e587acefa9 add grep
Strake
parents:
diff changeset
41 #define FOR_grep
55e587acefa9 add grep
Strake
parents:
diff changeset
42 #include "toys.h"
55e587acefa9 add grep
Strake
parents:
diff changeset
43 #include <regex.h>
55e587acefa9 add grep
Strake
parents:
diff changeset
44
55e587acefa9 add grep
Strake
parents:
diff changeset
45 GLOBALS(
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
46 long m;
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
47 struct arg_list *f;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
48 struct arg_list *e;
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
49
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
50 struct arg_list *regex;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
51 )
55e587acefa9 add grep
Strake
parents:
diff changeset
52
983
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
53 static void do_grep(int fd, char *name)
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
54 {
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
55 FILE *file = fdopen(fd, "r");
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
56 long offset = 0;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
57 int lcount = 0, mcount = 0, which = toys.optflags & FLAG_w ? 2 : 0;
1173
140aa98d9816 Add -zZ support to grep (NUL delimited input/output).
Rob Landley <rob@landley.net>
parents: 1170
diff changeset
58 char indelim = '\n' * !(toys.optflags&FLAG_z),
140aa98d9816 Add -zZ support to grep (NUL delimited input/output).
Rob Landley <rob@landley.net>
parents: 1170
diff changeset
59 outdelim = '\n' * !(toys.optflags&FLAG_Z);
948
55e587acefa9 add grep
Strake
parents:
diff changeset
60
1000
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
61 if (!fd) name = "(standard input)";
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
62
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
63 if (!file) {
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
64 perror_msg("%s", name);
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
65 return;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
66 }
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
67
948
55e587acefa9 add grep
Strake
parents:
diff changeset
68 for (;;) {
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
69 char *line = 0, *start;
970
55794a3d35c5 grep: add -w flag
Strake
parents: 959
diff changeset
70 regmatch_t matches[3];
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
71 size_t unused;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
72 long len;
1001
8b49ff103af9 grep: -om counts matching lines, not matching parts of lines.
Rob Landley <rob@landley.net>
parents: 1000
diff changeset
73 int mmatch = 0;
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
74
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
75 lcount++;
1173
140aa98d9816 Add -zZ support to grep (NUL delimited input/output).
Rob Landley <rob@landley.net>
parents: 1170
diff changeset
76 if (0 > (len = getdelim(&line, &unused, indelim, file))) break;
140aa98d9816 Add -zZ support to grep (NUL delimited input/output).
Rob Landley <rob@landley.net>
parents: 1170
diff changeset
77 if (line[len-1] == indelim) line[len-1] = 0;
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
78
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
79 start = line;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
80
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
81 for (;;)
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
82 {
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
83 int rc = 0, skip = 0;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
84
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
85 if (toys.optflags & FLAG_F) {
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
86 struct arg_list *seek, fseek;
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
87 char *s = 0;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
88
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
89 for (seek = TT.e; seek; seek = seek->next) {
1023
97383e221a4a Fix bug where exit code was only correct for -q.
Rob Landley <rob@landley.net>
parents: 1018
diff changeset
90 if (toys.optflags & FLAG_x) {
97383e221a4a Fix bug where exit code was only correct for -q.
Rob Landley <rob@landley.net>
parents: 1018
diff changeset
91 int i = (toys.optflags & FLAG_i);
97383e221a4a Fix bug where exit code was only correct for -q.
Rob Landley <rob@landley.net>
parents: 1018
diff changeset
92
97383e221a4a Fix bug where exit code was only correct for -q.
Rob Landley <rob@landley.net>
parents: 1018
diff changeset
93 if ((i ? strcasecmp : strcmp)(seek->arg, line)) s = line;
97383e221a4a Fix bug where exit code was only correct for -q.
Rob Landley <rob@landley.net>
parents: 1018
diff changeset
94 } else if (!*seek->arg) {
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
95 seek = &fseek;
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
96 fseek.arg = s = line;
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
97 break;
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
98 }
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
99 if (toys.optflags & FLAG_i) {
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
100 long ll = strlen(seek->arg);;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
101
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
102 // Alas, posix hasn't got strcasestr()
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
103 for (s = line; *s; s++) if (!strncasecmp(s, seek->arg, ll)) break;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
104 if (!*s) s = 0;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
105 } else s = strstr(line, seek->arg);
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
106 if (s) break;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
107 }
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
108
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
109 if (s) {
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
110 matches[which].rm_so = (s-line);
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
111 skip = matches[which].rm_eo = (s-line)+strlen(seek->arg);
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
112 } else rc = 1;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
113 } else {
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
114 rc = regexec((regex_t *)toybuf, start, 3, matches,
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
115 start==line ? 0 : REG_NOTBOL);
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
116 skip = matches[which].rm_eo;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
117 }
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
118
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
119 if (toys.optflags & FLAG_x)
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
120 if (matches[which].rm_so || line[matches[which].rm_eo]) rc = 1;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
121
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
122 if (toys.optflags & FLAG_v) {
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
123 if (toys.optflags & FLAG_o) {
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
124 if (rc) skip = matches[which].rm_eo = strlen(start);
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
125 else if (!matches[which].rm_so) {
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
126 start += skip;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
127 continue;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
128 } else matches[which].rm_eo = matches[which].rm_so;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
129 } else {
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
130 if (!rc) break;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
131 matches[which].rm_eo = strlen(start);
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
132 }
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
133 matches[which].rm_so = 0;
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
134 } else if (rc) break;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
135
1001
8b49ff103af9 grep: -om counts matching lines, not matching parts of lines.
Rob Landley <rob@landley.net>
parents: 1000
diff changeset
136 mmatch++;
1023
97383e221a4a Fix bug where exit code was only correct for -q.
Rob Landley <rob@landley.net>
parents: 1018
diff changeset
137 toys.exitval = 0;
97383e221a4a Fix bug where exit code was only correct for -q.
Rob Landley <rob@landley.net>
parents: 1018
diff changeset
138 if (toys.optflags & FLAG_q) xexit();
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
139 if (toys.optflags & FLAG_l) {
1173
140aa98d9816 Add -zZ support to grep (NUL delimited input/output).
Rob Landley <rob@landley.net>
parents: 1170
diff changeset
140 printf("%s%c", name, outdelim);
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
141 free(line);
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
142 fclose(file);
948
55e587acefa9 add grep
Strake
parents:
diff changeset
143 return;
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
144 }
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
145 if (toys.optflags & FLAG_o)
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
146 if (matches[which].rm_eo == matches[which].rm_so)
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
147 break;
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
148
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
149 if (!(toys.optflags & FLAG_c)) {
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
150 if (toys.optflags & FLAG_H) printf("%s:", name);
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
151 if (toys.optflags & FLAG_n) printf("%d:", lcount);
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
152 if (toys.optflags & FLAG_b)
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
153 printf("%ld:", offset + (start-line) +
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
154 ((toys.optflags & FLAG_o) ? matches[which].rm_so : 0));
1173
140aa98d9816 Add -zZ support to grep (NUL delimited input/output).
Rob Landley <rob@landley.net>
parents: 1170
diff changeset
155 if (!(toys.optflags & FLAG_o)) xprintf("%s%c", line, outdelim);
948
55e587acefa9 add grep
Strake
parents:
diff changeset
156 else {
1173
140aa98d9816 Add -zZ support to grep (NUL delimited input/output).
Rob Landley <rob@landley.net>
parents: 1170
diff changeset
157 xprintf("%.*s%c", matches[which].rm_eo - matches[which].rm_so,
140aa98d9816 Add -zZ support to grep (NUL delimited input/output).
Rob Landley <rob@landley.net>
parents: 1170
diff changeset
158 start + matches[which].rm_so, outdelim);
948
55e587acefa9 add grep
Strake
parents:
diff changeset
159 }
55e587acefa9 add grep
Strake
parents:
diff changeset
160 }
55e587acefa9 add grep
Strake
parents:
diff changeset
161
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
162 start += skip;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
163 if (!(toys.optflags & FLAG_o) || !*start) break;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
164 }
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
165 offset += len;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
166
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
167 free(line);
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
168
1001
8b49ff103af9 grep: -om counts matching lines, not matching parts of lines.
Rob Landley <rob@landley.net>
parents: 1000
diff changeset
169 if (mmatch) mcount++;
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
170 if ((toys.optflags & FLAG_m) && mcount >= TT.m) break;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
171 }
55e587acefa9 add grep
Strake
parents:
diff changeset
172
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
173 if (toys.optflags & FLAG_c) {
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
174 if (toys.optflags & FLAG_H) printf("%s:", name);
1173
140aa98d9816 Add -zZ support to grep (NUL delimited input/output).
Rob Landley <rob@landley.net>
parents: 1170
diff changeset
175 xprintf("%d%c", mcount, outdelim);
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
176 }
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
177
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
178 // loopfiles will also close the fd, but this frees an (opaque) struct.
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
179 fclose(file);
948
55e587acefa9 add grep
Strake
parents:
diff changeset
180 }
55e587acefa9 add grep
Strake
parents:
diff changeset
181
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
182 static void parse_regex(void)
983
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
183 {
1000
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
184 struct arg_list *al, *new, *list = NULL;
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
185 long len = 0;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
186 char *s, *ss;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
187
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
188 // Add all -f lines to -e list. (Yes, this is leaking allocation context for
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
189 // exit to free. Not supporting nofork for this command any time soon.)
1000
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
190 al = TT.f ? TT.f : TT.e;
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
191 while (al) {
1170
8afe1fde9314 Pass through all the readfile() arguments from xreadfile().
Rob Landley <rob@landley.net>
parents: 1110
diff changeset
192 if (TT.f) s = ss = xreadfile(al->arg, 0, 0);
1000
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
193 else s = ss = al->arg;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
194
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
195 do {
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
196 ss = strchr(s, '\n');
1000
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
197 if (ss) *(ss++) = 0;
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
198 new = xmalloc(sizeof(struct arg_list));
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
199 new->next = list;
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
200 new->arg = s;
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
201 list = new;
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
202 s = ss;
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
203 } while (ss && *s);
1000
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
204 al = al->next;
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
205 if (!al && TT.f) {
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
206 TT.f = 0;
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
207 al = TT.e;
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
208 }
948
55e587acefa9 add grep
Strake
parents:
diff changeset
209 }
1000
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
210 TT.e = list;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
211
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
212 if (!(toys.optflags & FLAG_F)) {
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
213 int w = toys.optflags & FLAG_w;
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
214 char *regstr;
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
215
1000
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
216 // Convert strings to one big regex
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
217 if (w) len = 36;
1110
241ee03473db grep doesn't allocate enough space
William Haddon <william@haddonthethird.net>
parents: 1023
diff changeset
218 for (al = TT.e; al; al = al->next)
241ee03473db grep doesn't allocate enough space
William Haddon <william@haddonthethird.net>
parents: 1023
diff changeset
219 len += strlen(al->arg)+1+!(toys.optflags & FLAG_E);
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
220
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
221 regstr = s = xmalloc(len);
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
222 if (w) s = stpcpy(s, "(^|[^_[:alnum:]])(");
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
223 for (al = TT.e; al; al = al->next) {
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
224 s = stpcpy(s, al->arg);
1000
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
225 if (!(toys.optflags & FLAG_E)) *(s++) = '\\';
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
226 *(s++) = '|';
959
Strake
parents: 948
diff changeset
227 }
1000
99dad9fb5613 More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Rob Landley <rob@landley.net>
parents: 999
diff changeset
228 *(s-=(1+!(toys.optflags & FLAG_E))) = 0;
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
229 if (w) strcpy(s, ")($|[^_[:alnum:]])");
948
55e587acefa9 add grep
Strake
parents:
diff changeset
230
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
231 w = regcomp((regex_t *)toybuf, regstr,
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
232 ((toys.optflags & FLAG_E) ? REG_EXTENDED : 0) |
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
233 ((toys.optflags & FLAG_i) ? REG_ICASE : 0));
970
55794a3d35c5 grep: add -w flag
Strake
parents: 959
diff changeset
234
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
235 if (w) {
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
236 regerror(w, (regex_t *)toybuf, toybuf+sizeof(regex_t),
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
237 sizeof(toybuf)-sizeof(regex_t));
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
238 error_exit("bad REGEX: %s", toybuf);
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
239 }
948
55e587acefa9 add grep
Strake
parents:
diff changeset
240 }
55e587acefa9 add grep
Strake
parents:
diff changeset
241 }
55e587acefa9 add grep
Strake
parents:
diff changeset
242
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
243 static int do_grep_r(struct dirtree *new)
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
244 {
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
245 char *name;
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
246
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
247 if (new->parent && !dirtree_notdotdot(new)) return 0;
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
248 if (S_ISDIR(new->st.st_mode)) return DIRTREE_RECURSE;
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
249
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
250 // "grep -r onefile" doesn't show filenames, but "grep -r onedir" should.
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
251 if (new->parent && !(toys.optflags & FLAG_h)) toys.optflags |= FLAG_H;
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
252
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
253 name = dirtree_path(new, 0);
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
254 do_grep(openat(dirtree_parentfd(new), new->name, 0), name);
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
255 free(name);
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
256
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
257 return 0;
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
258 }
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
259
983
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
260 void grep_main(void)
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
261 {
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
262 char **ss;
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
263
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
264 // Handle egrep and fgrep
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
265 if (*toys.which->name == 'e' || (toys.optflags & FLAG_w))
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
266 toys.optflags |= FLAG_E;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
267 if (*toys.which->name == 'f') toys.optflags |= FLAG_F;
982
0666d42df954 Found the fault. My method of -w fails sans -E, so I just disallow it.
M. Farkas-Dyck <strake888@gmail.com>
parents: 972
diff changeset
268
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
269 if (!TT.e && !TT.f) {
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
270 if (!*toys.optargs) error_exit("no REGEX");
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
271 TT.e = xzalloc(sizeof(struct arg_list));
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
272 TT.e->arg = *(toys.optargs++);
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
273 toys.optc--;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
274 }
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
275
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
276 parse_regex();
948
55e587acefa9 add grep
Strake
parents:
diff changeset
277
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
278 if (!(toys.optflags & FLAG_h) && toys.optc>1) toys.optflags |= FLAG_H;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
279
959
Strake
parents: 948
diff changeset
280 toys.exitval = 1;
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
281 if (toys.optflags & FLAG_s) {
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
282 close(2);
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
283 xopen("/dev/null", O_RDWR);
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
284 }
1017
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
285
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
286 if (toys.optflags & FLAG_r) {
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
287 for (ss=toys.optargs; *ss; ss++) {
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
288 if (!strcmp(*ss, "-")) do_grep(0, *ss);
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
289 else dirtree_read(*ss, do_grep_r);
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
290 }
059e1f30b80b Finish grep rewrite and fleshing out test suite. Several of the grep tests fail with the ubuntu version, I _think_ these are upstream bugs? (Second opinions welcome...)
Rob Landley <rob@landley.net>
parents: 1001
diff changeset
291 } else loopfiles_rw(toys.optargs, O_RDONLY, 0, 1, do_grep);
948
55e587acefa9 add grep
Strake
parents:
diff changeset
292 }