annotate toys/posix/grep.c @ 1023:97383e221a4a

Fix bug where exit code was only correct for -q.
author Rob Landley <rob@landley.net>
date Wed, 21 Aug 2013 05:38:53 -0500
parents 133ef0885cce
children 241ee03473db
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
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
7 USE_GREP(NEWTOY(grep, "EFHabhinorsvwclqe*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
983
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
29
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
30 display modes: (default: matched line)
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
31 -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
32 -o only matching part -q quiet (errors only)
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
33 -s silent (no error msg)
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
34
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
35 prefix modes (default: filename if checking more than 1 file)
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
36 -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
37 -h hide filename -n line number of match
948
55e587acefa9 add grep
Strake
parents:
diff changeset
38 */
55e587acefa9 add grep
Strake
parents:
diff changeset
39
55e587acefa9 add grep
Strake
parents:
diff changeset
40 #define FOR_grep
55e587acefa9 add grep
Strake
parents:
diff changeset
41 #include "toys.h"
55e587acefa9 add grep
Strake
parents:
diff changeset
42 #include <regex.h>
55e587acefa9 add grep
Strake
parents:
diff changeset
43
55e587acefa9 add grep
Strake
parents:
diff changeset
44 GLOBALS(
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
45 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
46 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
47 struct arg_list *e;
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
48
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
49 struct arg_list *regex;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
50 )
55e587acefa9 add grep
Strake
parents:
diff changeset
51
983
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
52 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
53 {
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
54 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
55 long offset = 0;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
56 int lcount = 0, mcount = 0, which = toys.optflags & FLAG_w ? 2 : 0;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
57
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
58 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
59
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
60 if (!file) {
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
61 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
62 return;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
63 }
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
64
948
55e587acefa9 add grep
Strake
parents:
diff changeset
65 for (;;) {
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
66 char *line = 0, *start;
970
55794a3d35c5 grep: add -w flag
Strake
parents: 959
diff changeset
67 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
68 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
69 long len;
1001
8b49ff103af9 grep: -om counts matching lines, not matching parts of lines.
Rob Landley <rob@landley.net>
parents: 1000
diff changeset
70 int mmatch = 0;
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
71
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
72 lcount++;
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
73 if (0 > (len = getline(&line, &unused, file))) break;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
74 if (line[len-1] == '\n') line[len-1] = 0;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
75
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
76 start = line;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
77
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
78 for (;;)
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
79 {
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
80 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
81
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
82 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
83 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
84 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
85
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
86 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
87 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
88 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
89
97383e221a4a Fix bug where exit code was only correct for -q.
Rob Landley <rob@landley.net>
parents: 1018
diff changeset
90 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
91 } 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
92 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
93 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
94 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
95 }
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
96 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
97 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
98
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
99 // 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
100 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
101 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
102 } 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
103 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
104 }
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
105
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) {
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
107 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
108 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
109 } 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
110 } else {
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
111 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
112 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
113 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
114 }
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
115
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
116 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
117 if (matches[which].rm_so || line[matches[which].rm_eo]) rc = 1;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
118
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
119 if (toys.optflags & FLAG_v) {
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
120 if (toys.optflags & FLAG_o) {
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
121 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
122 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
123 start += skip;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
124 continue;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
125 } 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
126 } else {
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
127 if (!rc) break;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
128 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
129 }
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
130 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
131 } else if (rc) break;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
132
1001
8b49ff103af9 grep: -om counts matching lines, not matching parts of lines.
Rob Landley <rob@landley.net>
parents: 1000
diff changeset
133 mmatch++;
1023
97383e221a4a Fix bug where exit code was only correct for -q.
Rob Landley <rob@landley.net>
parents: 1018
diff changeset
134 toys.exitval = 0;
97383e221a4a Fix bug where exit code was only correct for -q.
Rob Landley <rob@landley.net>
parents: 1018
diff changeset
135 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
136 if (toys.optflags & FLAG_l) {
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
137 printf("%s\n", name);
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
138 free(line);
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
139 fclose(file);
948
55e587acefa9 add grep
Strake
parents:
diff changeset
140 return;
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
141 }
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
142 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
143 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
144 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
145
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
146 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
147 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
148 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
149 if (toys.optflags & FLAG_b)
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
150 printf("%ld:", offset + (start-line) +
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
151 ((toys.optflags & FLAG_o) ? matches[which].rm_so : 0));
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
152 if (!(toys.optflags & FLAG_o)) xputs(line);
948
55e587acefa9 add grep
Strake
parents:
diff changeset
153 else {
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
154 xprintf("%.*s\n", 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
155 start + matches[which].rm_so);
948
55e587acefa9 add grep
Strake
parents:
diff changeset
156 }
55e587acefa9 add grep
Strake
parents:
diff changeset
157 }
55e587acefa9 add grep
Strake
parents:
diff changeset
158
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
159 start += skip;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
160 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
161 }
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
162 offset += len;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
163
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
164 free(line);
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
165
1001
8b49ff103af9 grep: -om counts matching lines, not matching parts of lines.
Rob Landley <rob@landley.net>
parents: 1000
diff changeset
166 if (mmatch) mcount++;
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
167 if ((toys.optflags & FLAG_m) && mcount >= TT.m) break;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
168 }
55e587acefa9 add grep
Strake
parents:
diff changeset
169
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_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
171 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
172 xprintf("%d\n", mcount);
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
173 }
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
174
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
175 // 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
176 fclose(file);
948
55e587acefa9 add grep
Strake
parents:
diff changeset
177 }
55e587acefa9 add grep
Strake
parents:
diff changeset
178
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
179 static void parse_regex(void)
983
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
180 {
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
181 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
182 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
183 char *s, *ss;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
184
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
185 // 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
186 // 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
187 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
188 while (al) {
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
189 if (TT.f) s = ss = xreadfile(al->arg);
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 else s = ss = al->arg;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
191
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
192 do {
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
193 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
194 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
195 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
196 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
197 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
198 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
199 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
200 } 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
201 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
202 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
203 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
204 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
205 }
948
55e587acefa9 add grep
Strake
parents:
diff changeset
206 }
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
207 TT.e = list;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
208
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
209 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
210 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
211 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
212
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
213 // 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
214 if (w) len = 36;
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
215 for (al = TT.e; al; al = al->next) len += strlen(al->arg)+1;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
216
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
217 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
218 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
219 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
220 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
221 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
222 *(s++) = '|';
959
Strake
parents: 948
diff changeset
223 }
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
224 *(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
225 if (w) strcpy(s, ")($|[^_[:alnum:]])");
948
55e587acefa9 add grep
Strake
parents:
diff changeset
226
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
227 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
228 ((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
229 ((toys.optflags & FLAG_i) ? REG_ICASE : 0));
970
55794a3d35c5 grep: add -w flag
Strake
parents: 959
diff changeset
230
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
231 if (w) {
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
232 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
233 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
234 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
235 }
948
55e587acefa9 add grep
Strake
parents:
diff changeset
236 }
55e587acefa9 add grep
Strake
parents:
diff changeset
237 }
55e587acefa9 add grep
Strake
parents:
diff changeset
238
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
239 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
240 {
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
241 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
242
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 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
244 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
245
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 // "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
247 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
248
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 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
250 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
251 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
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 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
254 }
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
983
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
256 void grep_main(void)
c38c25282b88 Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents: 982
diff changeset
257 {
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
258 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
259
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
260 // Handle egrep and fgrep
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
261 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
262 toys.optflags |= FLAG_E;
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
263 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
264
999
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
265 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
266 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
267 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
268 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
269 toys.optc--;
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
270 }
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
271
0af2375a8ef8 More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents: 987
diff changeset
272 parse_regex();
948
55e587acefa9 add grep
Strake
parents:
diff changeset
273
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
274 if (!(toys.optflags & FLAG_h) && toys.optc>1) toys.optflags |= FLAG_H;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
275
959
Strake
parents: 948
diff changeset
276 toys.exitval = 1;
987
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
277 if (toys.optflags & FLAG_s) {
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
278 close(2);
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
279 xopen("/dev/null", O_RDWR);
b570b2bfbf7d Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents: 983
diff changeset
280 }
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
281
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
282 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
283 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
284 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
285 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
286 }
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 } else loopfiles_rw(toys.optargs, O_RDONLY, 0, 1, do_grep);
948
55e587acefa9 add grep
Strake
parents:
diff changeset
288 }