annotate toys/pending/grep.c @ 972:e33f0312de6e

grep: s/astrcat/x&/g
author Strake <strake888@gmail.com>
date Sat, 27 Jul 2013 00:50:29 -0500
parents 31e91deb0824
children 0666d42df954
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 *
55e587acefa9 add grep
Strake
parents:
diff changeset
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/
55e587acefa9 add grep
Strake
parents:
diff changeset
6 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html
55e587acefa9 add grep
Strake
parents:
diff changeset
7
971
31e91deb0824 grep: add -b flag
Strake
parents: 970
diff changeset
8 USE_GREP(NEWTOY(grep, "EFHabhinosvwclqe*f*m#", TOYFLAG_BIN))
948
55e587acefa9 add grep
Strake
parents:
diff changeset
9
55e587acefa9 add grep
Strake
parents:
diff changeset
10 config GREP
55e587acefa9 add grep
Strake
parents:
diff changeset
11 bool "grep"
55e587acefa9 add grep
Strake
parents:
diff changeset
12 default n
55e587acefa9 add grep
Strake
parents:
diff changeset
13 help
971
31e91deb0824 grep: add -b flag
Strake
parents: 970
diff changeset
14 usage: grep [-clq] [-EFHbhinosvw] (-e RE | -f REfile | RE) [file...]
948
55e587acefa9 add grep
Strake
parents:
diff changeset
15
55e587acefa9 add grep
Strake
parents:
diff changeset
16 modes:
55e587acefa9 add grep
Strake
parents:
diff changeset
17 default: print lines from each file what match regular expression RE.
55e587acefa9 add grep
Strake
parents:
diff changeset
18 -c: print the number of matching lines in each file.
55e587acefa9 add grep
Strake
parents:
diff changeset
19 -l: print all matching file names.
55e587acefa9 add grep
Strake
parents:
diff changeset
20 -q: print nil; quit with code 0 when match found.
55e587acefa9 add grep
Strake
parents:
diff changeset
21
55e587acefa9 add grep
Strake
parents:
diff changeset
22 flags:
55e587acefa9 add grep
Strake
parents:
diff changeset
23 -E: extended RE syntax
55e587acefa9 add grep
Strake
parents:
diff changeset
24 -F: fixed RE syntax, i.e. all characters literal
959
Strake
parents: 948
diff changeset
25 -H: print file name
971
31e91deb0824 grep: add -b flag
Strake
parents: 970
diff changeset
26 -b: print byte offset of match
948
55e587acefa9 add grep
Strake
parents:
diff changeset
27 -h: not print file name
55e587acefa9 add grep
Strake
parents:
diff changeset
28 -i: case insensitive
55e587acefa9 add grep
Strake
parents:
diff changeset
29 -n: print line numbers
55e587acefa9 add grep
Strake
parents:
diff changeset
30 -o: print only matching part
959
Strake
parents: 948
diff changeset
31 -s: keep silent on error
948
55e587acefa9 add grep
Strake
parents:
diff changeset
32 -v: invert match
970
55794a3d35c5 grep: add -w flag
Strake
parents: 959
diff changeset
33 -w: match full word only
948
55e587acefa9 add grep
Strake
parents:
diff changeset
34 */
55e587acefa9 add grep
Strake
parents:
diff changeset
35
55e587acefa9 add grep
Strake
parents:
diff changeset
36 #define FOR_grep
55e587acefa9 add grep
Strake
parents:
diff changeset
37 #include "toys.h"
55e587acefa9 add grep
Strake
parents:
diff changeset
38 #include <regex.h>
55e587acefa9 add grep
Strake
parents:
diff changeset
39
55e587acefa9 add grep
Strake
parents:
diff changeset
40 static regex_t re; /* fails in GLOBALS */
55e587acefa9 add grep
Strake
parents:
diff changeset
41
55e587acefa9 add grep
Strake
parents:
diff changeset
42 GLOBALS(
55e587acefa9 add grep
Strake
parents:
diff changeset
43 long mArgu;
55e587acefa9 add grep
Strake
parents:
diff changeset
44 struct arg_list *fArgu, *eArgu;
959
Strake
parents: 948
diff changeset
45 char mode, *re_xs;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
46 )
55e587acefa9 add grep
Strake
parents:
diff changeset
47
55e587acefa9 add grep
Strake
parents:
diff changeset
48 static void do_grep (int fd, char *name) {
55e587acefa9 add grep
Strake
parents:
diff changeset
49 int n = 0, nMatch = 0;
55e587acefa9 add grep
Strake
parents:
diff changeset
50
55e587acefa9 add grep
Strake
parents:
diff changeset
51 for (;;) {
55e587acefa9 add grep
Strake
parents:
diff changeset
52 char *x, *y;
970
55794a3d35c5 grep: add -w flag
Strake
parents: 959
diff changeset
53 regmatch_t matches[3];
948
55e587acefa9 add grep
Strake
parents:
diff changeset
54 int atBOL = 1;
55e587acefa9 add grep
Strake
parents:
diff changeset
55
55e587acefa9 add grep
Strake
parents:
diff changeset
56 x = get_rawline (fd, 0, '\n');
55e587acefa9 add grep
Strake
parents:
diff changeset
57 if (!x) break;
55e587acefa9 add grep
Strake
parents:
diff changeset
58 y = x;
55e587acefa9 add grep
Strake
parents:
diff changeset
59 n++; /* start at 1 */
55e587acefa9 add grep
Strake
parents:
diff changeset
60
970
55794a3d35c5 grep: add -w flag
Strake
parents: 959
diff changeset
61 while (regexec (&re, y, 3, matches, atBOL ? 0 : REG_NOTBOL) == 0) {
948
55e587acefa9 add grep
Strake
parents:
diff changeset
62 if (atBOL) nMatch++;
959
Strake
parents: 948
diff changeset
63 toys.exitval = 0;
Strake
parents: 948
diff changeset
64 atBOL = 0;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
65 switch (TT.mode) {
55e587acefa9 add grep
Strake
parents:
diff changeset
66 case 'q':
959
Strake
parents: 948
diff changeset
67 xexit ();
948
55e587acefa9 add grep
Strake
parents:
diff changeset
68 case 'l':
55e587acefa9 add grep
Strake
parents:
diff changeset
69 if (!(toys.optflags & FLAG_h)) printf ("%s\n", name);
55e587acefa9 add grep
Strake
parents:
diff changeset
70 free (x);
55e587acefa9 add grep
Strake
parents:
diff changeset
71 return;
55e587acefa9 add grep
Strake
parents:
diff changeset
72 case 'c':
55e587acefa9 add grep
Strake
parents:
diff changeset
73 break;
55e587acefa9 add grep
Strake
parents:
diff changeset
74 default:
55e587acefa9 add grep
Strake
parents:
diff changeset
75 if (!(toys.optflags & FLAG_h)) printf ("%s:", name);
55e587acefa9 add grep
Strake
parents:
diff changeset
76 if ( (toys.optflags & FLAG_n)) printf ("%d:", n);
971
31e91deb0824 grep: add -b flag
Strake
parents: 970
diff changeset
77 if ( (toys.optflags & FLAG_b)) printf ("%ld:", lseek (0, 0, SEEK_CUR) - strlen (y) +
31e91deb0824 grep: add -b flag
Strake
parents: 970
diff changeset
78 (toys.optflags & FLAG_o ? matches[2].rm_so : 0));
948
55e587acefa9 add grep
Strake
parents:
diff changeset
79 if (!(toys.optflags & FLAG_o)) fputs (x, stdout);
55e587acefa9 add grep
Strake
parents:
diff changeset
80 else {
970
55794a3d35c5 grep: add -w flag
Strake
parents: 959
diff changeset
81 y += matches[2].rm_so;
55794a3d35c5 grep: add -w flag
Strake
parents: 959
diff changeset
82 printf ("%.*s\n", matches[2].rm_eo - matches[2].rm_so, y++);
948
55e587acefa9 add grep
Strake
parents:
diff changeset
83 }
55e587acefa9 add grep
Strake
parents:
diff changeset
84 }
55e587acefa9 add grep
Strake
parents:
diff changeset
85 if (!(toys.optflags & FLAG_o)) break;
55e587acefa9 add grep
Strake
parents:
diff changeset
86 }
55e587acefa9 add grep
Strake
parents:
diff changeset
87
55e587acefa9 add grep
Strake
parents:
diff changeset
88 free (x);
55e587acefa9 add grep
Strake
parents:
diff changeset
89
55e587acefa9 add grep
Strake
parents:
diff changeset
90 if ((toys.optflags & FLAG_m) && nMatch >= TT.mArgu) break;
55e587acefa9 add grep
Strake
parents:
diff changeset
91 }
55e587acefa9 add grep
Strake
parents:
diff changeset
92
55e587acefa9 add grep
Strake
parents:
diff changeset
93 if (TT.mode == 'c') printf ("%s:%d\n", name, nMatch);
55e587acefa9 add grep
Strake
parents:
diff changeset
94 }
55e587acefa9 add grep
Strake
parents:
diff changeset
95
55e587acefa9 add grep
Strake
parents:
diff changeset
96 char *regfix (char *re_xs) {
55e587acefa9 add grep
Strake
parents:
diff changeset
97 char *re_ys;
55e587acefa9 add grep
Strake
parents:
diff changeset
98 int ii, jj = 0;
55e587acefa9 add grep
Strake
parents:
diff changeset
99 re_ys = xmalloc (2*strlen (re_xs) + 1);
55e587acefa9 add grep
Strake
parents:
diff changeset
100 for (ii = 0; re_xs[ii]; ii++) {
55e587acefa9 add grep
Strake
parents:
diff changeset
101 if (strchr ("^.[]$()|*+?{}\\", re_xs[ii])) re_ys[jj++] = '\\';
55e587acefa9 add grep
Strake
parents:
diff changeset
102 re_ys[jj++] = re_xs[ii];
55e587acefa9 add grep
Strake
parents:
diff changeset
103 }
55e587acefa9 add grep
Strake
parents:
diff changeset
104 re_ys[jj] = 0;
55e587acefa9 add grep
Strake
parents:
diff changeset
105 return re_ys;
55e587acefa9 add grep
Strake
parents:
diff changeset
106 }
55e587acefa9 add grep
Strake
parents:
diff changeset
107
959
Strake
parents: 948
diff changeset
108 void addRE (char *x) {
Strake
parents: 948
diff changeset
109 if (toys.optflags & FLAG_F) x = regfix (x);
972
e33f0312de6e grep: s/astrcat/x&/g
Strake <strake888@gmail.com>
parents: 971
diff changeset
110 if (TT.re_xs) TT.re_xs = xastrcat (TT.re_xs, "|");
e33f0312de6e grep: s/astrcat/x&/g
Strake <strake888@gmail.com>
parents: 971
diff changeset
111 TT.re_xs = xastrcat (TT.re_xs, x);
959
Strake
parents: 948
diff changeset
112 if (toys.optflags & FLAG_F) free (x);
Strake
parents: 948
diff changeset
113 }
948
55e587acefa9 add grep
Strake
parents:
diff changeset
114
959
Strake
parents: 948
diff changeset
115 void buildRE (void) {
Strake
parents: 948
diff changeset
116 for (; TT.eArgu; TT.eArgu = TT.eArgu -> next) addRE (TT.eArgu -> arg);
948
55e587acefa9 add grep
Strake
parents:
diff changeset
117 for (; TT.fArgu; TT.fArgu = TT.fArgu -> next) {
55e587acefa9 add grep
Strake
parents:
diff changeset
118 FILE *f;
55e587acefa9 add grep
Strake
parents:
diff changeset
119 char *x, *y;
55e587acefa9 add grep
Strake
parents:
diff changeset
120 size_t l;
55e587acefa9 add grep
Strake
parents:
diff changeset
121
55e587acefa9 add grep
Strake
parents:
diff changeset
122 f = xfopen (TT.fArgu -> arg, "r");
55e587acefa9 add grep
Strake
parents:
diff changeset
123 x = 0;
55e587acefa9 add grep
Strake
parents:
diff changeset
124 for (;;) {
55e587acefa9 add grep
Strake
parents:
diff changeset
125 if (getline (&x, &l, f) < 0) {
55e587acefa9 add grep
Strake
parents:
diff changeset
126 if (feof (f)) break;
959
Strake
parents: 948
diff changeset
127 toys.exitval = 2;
Strake
parents: 948
diff changeset
128 perror_exit ("failed to read");
948
55e587acefa9 add grep
Strake
parents:
diff changeset
129 }
55e587acefa9 add grep
Strake
parents:
diff changeset
130 y = x + strlen (x) - 1;
55e587acefa9 add grep
Strake
parents:
diff changeset
131 if (y[0] == '\n') y[0] = 0;
55e587acefa9 add grep
Strake
parents:
diff changeset
132
959
Strake
parents: 948
diff changeset
133 addRE (x);
948
55e587acefa9 add grep
Strake
parents:
diff changeset
134 }
55e587acefa9 add grep
Strake
parents:
diff changeset
135 free (x);
55e587acefa9 add grep
Strake
parents:
diff changeset
136 fclose (f);
55e587acefa9 add grep
Strake
parents:
diff changeset
137 }
55e587acefa9 add grep
Strake
parents:
diff changeset
138
959
Strake
parents: 948
diff changeset
139 if (!TT.re_xs) {
Strake
parents: 948
diff changeset
140 if (toys.optc < 1) {
Strake
parents: 948
diff changeset
141 toys.exitval = 2;
Strake
parents: 948
diff changeset
142 error_exit ("no RE");
Strake
parents: 948
diff changeset
143 }
Strake
parents: 948
diff changeset
144 TT.re_xs = toys.optflags & FLAG_F ? regfix (toys.optargs[0]) : toys.optargs[0];
948
55e587acefa9 add grep
Strake
parents:
diff changeset
145 toys.optc--; toys.optargs++;
55e587acefa9 add grep
Strake
parents:
diff changeset
146 }
55e587acefa9 add grep
Strake
parents:
diff changeset
147
970
55794a3d35c5 grep: add -w flag
Strake
parents: 959
diff changeset
148 TT.re_xs = xmsprintf (toys.optflags & FLAG_w ? "(^|[^_[:alnum:]])(%s)($|[^_[:alnum:]])" : "()(%s)()", TT.re_xs);
55794a3d35c5 grep: add -w flag
Strake
parents: 959
diff changeset
149
959
Strake
parents: 948
diff changeset
150 if (regcomp (&re, TT.re_xs,
948
55e587acefa9 add grep
Strake
parents:
diff changeset
151 (toys.optflags & (FLAG_E | FLAG_F) ? REG_EXTENDED : 0) |
55e587acefa9 add grep
Strake
parents:
diff changeset
152 (toys.optflags & FLAG_i ? REG_ICASE : 0)) != 0) {
959
Strake
parents: 948
diff changeset
153 toys.exitval = 2;
Strake
parents: 948
diff changeset
154 error_exit ("bad RE");
948
55e587acefa9 add grep
Strake
parents:
diff changeset
155 }
55e587acefa9 add grep
Strake
parents:
diff changeset
156 }
55e587acefa9 add grep
Strake
parents:
diff changeset
157
55e587acefa9 add grep
Strake
parents:
diff changeset
158 void grep_main (void) {
55e587acefa9 add grep
Strake
parents:
diff changeset
159 buildRE ();
55e587acefa9 add grep
Strake
parents:
diff changeset
160
55e587acefa9 add grep
Strake
parents:
diff changeset
161 if (toys.optflags & FLAG_c) TT.mode = 'c';
55e587acefa9 add grep
Strake
parents:
diff changeset
162 if (toys.optflags & FLAG_l) TT.mode = 'l';
55e587acefa9 add grep
Strake
parents:
diff changeset
163 if (toys.optflags & FLAG_q) TT.mode = 'q';
55e587acefa9 add grep
Strake
parents:
diff changeset
164
959
Strake
parents: 948
diff changeset
165 if (!(toys.optflags & FLAG_H) && (toys.optc < 2)) toys.optflags |= FLAG_h;
948
55e587acefa9 add grep
Strake
parents:
diff changeset
166
959
Strake
parents: 948
diff changeset
167 toys.exitval = 1;
Strake
parents: 948
diff changeset
168 loopfiles_rw (toys.optargs, O_RDONLY, 0, toys.optflags & FLAG_s, do_grep);
Strake
parents: 948
diff changeset
169 xexit ();
948
55e587acefa9 add grep
Strake
parents:
diff changeset
170 }