Mercurial > hg > toybox
annotate toys/pending/grep.c @ 999:0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 12 Aug 2013 01:48:27 -0500 |
parents | b570b2bfbf7d |
children | 99dad9fb5613 |
rev | line source |
---|---|
948 | 1 /* grep.c - print lines what match given regular expression |
2 * | |
3 * Copyright 2013 CE Strake <strake888 at gmail.com> | |
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 | 6 |
999
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
7 USE_GREP(NEWTOY(grep, "EFHabhinosvwclqe*f*m#x[!wx][!EFw]", TOYFLAG_BIN)) |
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 | 10 |
11 config GREP | |
12 bool "grep" | |
13 default n | |
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 | 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 | 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) |
c38c25282b88
Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents:
982
diff
changeset
|
26 -i case insensitive -v invert match |
999
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
27 -w whole word (implies -E) -m stop after this many lines matched |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
28 -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 | 38 */ |
39 | |
40 #define FOR_grep | |
41 #include "toys.h" | |
42 #include <regex.h> | |
43 | |
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 |
999
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
49 char *regstr; |
948 | 50 ) |
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 | 57 |
999
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
58 if (!file) { |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
59 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
|
60 return; |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
61 } |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
62 |
948 | 63 for (;;) { |
987
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
64 char *line = 0, *start; |
970 | 65 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
|
66 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
|
67 long len; |
987
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
68 |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
69 lcount++; |
999
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
70 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
|
71 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
|
72 |
987
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
73 start = line; |
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 for (;;) |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
76 { |
999
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
77 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
|
78 |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
79 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
|
80 struct arg_list *seek; |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
81 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
|
82 |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
83 for (seek = TT.e; seek; seek = seek->next) { |
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_i) { |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
86 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
|
87 |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
88 // 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
|
89 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
|
90 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
|
91 } 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
|
92 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
|
93 } |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
94 |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
95 if (s) { |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
96 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
|
97 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
|
98 } 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
|
99 } else { |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
100 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
|
101 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
|
102 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
|
103 } |
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 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
|
106 if (matches[which].rm_so || line[matches[which].rm_eo]) rc = 1; |
948 | 107 |
987
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
108 if (toys.optflags & FLAG_v) { |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
109 if (toys.optflags & FLAG_o) { |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
110 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
|
111 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
|
112 start += skip; |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
113 continue; |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
114 } 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
|
115 } else { |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
116 if (!rc) break; |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
117 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
|
118 } |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
119 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
|
120 } else if (rc) break; |
948 | 121 |
987
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
122 mcount++; |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
123 if (toys.optflags & FLAG_q) { |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
124 toys.exitval = 0; |
983
c38c25282b88
Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents:
982
diff
changeset
|
125 xexit(); |
987
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
126 } |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
127 if (toys.optflags & FLAG_l) { |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
128 printf("%s\n", name); |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
129 free(line); |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
130 fclose(file); |
948 | 131 return; |
987
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 if (!(toys.optflags & FLAG_c)) { |
983
c38c25282b88
Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents:
982
diff
changeset
|
134 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
|
135 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
|
136 if (toys.optflags & FLAG_b) |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
137 printf("%ld:", offset + (start-line) + |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
138 ((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
|
139 if (!(toys.optflags & FLAG_o)) xputs(line); |
948 | 140 else { |
987
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
141 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
|
142 start + matches[which].rm_so); |
948 | 143 } |
144 } | |
145 | |
987
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
146 start += skip; |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
147 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
|
148 } |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
149 offset += len; |
948 | 150 |
987
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
151 free(line); |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
152 |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
153 if ((toys.optflags & FLAG_m) && mcount >= TT.m) break; |
948 | 154 } |
155 | |
987
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
156 if (toys.optflags & FLAG_c) { |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
157 if (!(toys.optflags & FLAG_h)) printf("%s:", name); |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
158 xprintf("%d\n", mcount); |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
159 } |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
160 |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
161 // 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
|
162 fclose(file); |
948 | 163 } |
164 | |
999
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
165 static void parse_regex(void) |
983
c38c25282b88
Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents:
982
diff
changeset
|
166 { |
999
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
167 struct arg_list *al; |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
168 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
|
169 char *s, *ss; |
948 | 170 |
999
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
171 // 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
|
172 // exit to free. Not supporting nofork for this command any time soon.) |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
173 for (al = TT.f; 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
|
174 s = ss = xreadfile(al->arg); |
948 | 175 |
999
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
176 while (ss && *s) { |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
177 ss = strchr(s, '\n'); |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
178 if (ss) *ss = 0; |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
179 al = xmalloc(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
|
180 al->next = TT.e; |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
181 al->arg = s; |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
182 TT.e = al; |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
183 s = ss; |
948 | 184 } |
185 } | |
186 | |
999
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
187 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
|
188 int w = toys.optflags & FLAG_w; |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
189 |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
190 // Convert strings to one big regex string. |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
191 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
|
192 if (w) len = 36; |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
193 |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
194 TT.regstr = s = xmalloc(len); |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
195 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
|
196 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
|
197 s = stpcpy(s, al->arg); |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
198 *(s++) = '|'; |
959 | 199 } |
999
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
200 *(--s) = 0; |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
201 if (w) strcpy(s, ")($|[^_[:alnum:]])"); |
948 | 202 |
999
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
203 w = regcomp((regex_t *)toybuf, TT.regstr, |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
204 ((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
|
205 ((toys.optflags & FLAG_i) ? REG_ICASE : 0)); |
970 | 206 |
999
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
207 if (w) { |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
208 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
|
209 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
|
210 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
|
211 } |
948 | 212 } |
213 } | |
214 | |
983
c38c25282b88
Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents:
982
diff
changeset
|
215 void grep_main(void) |
c38c25282b88
Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents:
982
diff
changeset
|
216 { |
987
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
217 // Handle egrep and fgrep |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
218 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
|
219 toys.optflags |= FLAG_E; |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
220 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
|
221 |
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 (!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
|
223 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
|
224 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
|
225 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
|
226 toys.optc--; |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
227 } |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
228 |
0af2375a8ef8
More grep cleanup, and make OPTSTR_command macros for use with OLDTOY()
Rob Landley <rob@landley.net>
parents:
987
diff
changeset
|
229 parse_regex(); |
948 | 230 |
959 | 231 if (!(toys.optflags & FLAG_H) && (toys.optc < 2)) toys.optflags |= FLAG_h; |
948 | 232 |
959 | 233 toys.exitval = 1; |
987
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
234 if (toys.optflags & FLAG_s) { |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
235 close(2); |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
236 xopen("/dev/null", O_RDWR); |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
237 } |
b570b2bfbf7d
Cleanup grep, make it pass the current test suite.
Rob Landley <rob@landley.net>
parents:
983
diff
changeset
|
238 loopfiles_rw(toys.optargs, O_RDONLY, 0, 1, do_grep); |
983
c38c25282b88
Cleanup grep: help text, whitespace, add parentheses.
Rob Landley <rob@landley.net>
parents:
982
diff
changeset
|
239 xexit(); |
948 | 240 } |