1 |
|
|
/* $OpenBSD: grep.c,v 1.45 2017/10/12 14:12:00 florian Exp $ */ |
2 |
|
|
|
3 |
|
|
/* This file is in the public domain */ |
4 |
|
|
|
5 |
|
|
#include <sys/queue.h> |
6 |
|
|
#include <sys/types.h> |
7 |
|
|
#include <ctype.h> |
8 |
|
|
#include <libgen.h> |
9 |
|
|
#include <limits.h> |
10 |
|
|
#include <signal.h> |
11 |
|
|
#include <stdio.h> |
12 |
|
|
#include <stdlib.h> |
13 |
|
|
#include <string.h> |
14 |
|
|
#include <time.h> |
15 |
|
|
#include <unistd.h> |
16 |
|
|
|
17 |
|
|
#include "def.h" |
18 |
|
|
#include "kbd.h" |
19 |
|
|
#include "funmap.h" |
20 |
|
|
|
21 |
|
|
int globalwd = FALSE; |
22 |
|
|
static int compile_goto_error(int, int); |
23 |
|
|
int next_error(int, int); |
24 |
|
|
static int grep(int, int); |
25 |
|
|
static int gid(int, int); |
26 |
|
|
static struct buffer *compile_mode(const char *, const char *); |
27 |
|
|
void grep_init(void); |
28 |
|
|
|
29 |
|
|
static char compile_last_command[NFILEN] = "make "; |
30 |
|
|
|
31 |
|
|
/* |
32 |
|
|
* Hints for next-error |
33 |
|
|
* |
34 |
|
|
* XXX - need some kind of callback to find out when those get killed. |
35 |
|
|
*/ |
36 |
|
|
struct mgwin *compile_win; |
37 |
|
|
struct buffer *compile_buffer; |
38 |
|
|
|
39 |
|
|
static PF compile_pf[] = { |
40 |
|
|
compile_goto_error |
41 |
|
|
}; |
42 |
|
|
|
43 |
|
|
static struct KEYMAPE (1) compilemap = { |
44 |
|
|
1, |
45 |
|
|
1, |
46 |
|
|
rescan, |
47 |
|
|
{ |
48 |
|
|
{ CCHR('M'), CCHR('M'), compile_pf, NULL } |
49 |
|
|
} |
50 |
|
|
}; |
51 |
|
|
|
52 |
|
|
void |
53 |
|
|
grep_init(void) |
54 |
|
|
{ |
55 |
|
|
funmap_add(compile_goto_error, "compile-goto-error"); |
56 |
|
|
funmap_add(next_error, "next-error"); |
57 |
|
|
funmap_add(grep, "grep"); |
58 |
|
|
funmap_add(compile, "compile"); |
59 |
|
|
funmap_add(gid, "gid"); |
60 |
|
|
maps_add((KEYMAP *)&compilemap, "compile"); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
/* ARGSUSED */ |
64 |
|
|
static int |
65 |
|
|
grep(int f, int n) |
66 |
|
|
{ |
67 |
|
|
char cprompt[NFILEN], *bufp; |
68 |
|
|
struct buffer *bp; |
69 |
|
|
struct mgwin *wp; |
70 |
|
|
|
71 |
|
|
(void)strlcpy(cprompt, "grep -n ", sizeof(cprompt)); |
72 |
|
|
if ((bufp = eread("Run grep: ", cprompt, NFILEN, |
73 |
|
|
EFDEF | EFNEW | EFCR)) == NULL) |
74 |
|
|
return (ABORT); |
75 |
|
|
else if (bufp[0] == '\0') |
76 |
|
|
return (FALSE); |
77 |
|
|
if (strlcat(cprompt, " /dev/null", sizeof(cprompt)) >= sizeof(cprompt)) |
78 |
|
|
return (FALSE); |
79 |
|
|
|
80 |
|
|
if ((bp = compile_mode("*grep*", cprompt)) == NULL) |
81 |
|
|
return (FALSE); |
82 |
|
|
if ((wp = popbuf(bp, WNONE)) == NULL) |
83 |
|
|
return (FALSE); |
84 |
|
|
curbp = bp; |
85 |
|
|
compile_win = curwp = wp; |
86 |
|
|
return (TRUE); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
/* ARGSUSED */ |
90 |
|
|
int |
91 |
|
|
compile(int f, int n) |
92 |
|
|
{ |
93 |
|
|
char cprompt[NFILEN], *bufp; |
94 |
|
|
struct buffer *bp; |
95 |
|
|
struct mgwin *wp; |
96 |
|
|
|
97 |
|
|
(void)strlcpy(cprompt, compile_last_command, sizeof(cprompt)); |
98 |
|
|
if ((bufp = eread("Compile command: ", cprompt, NFILEN, |
99 |
|
|
EFDEF | EFNEW | EFCR)) == NULL) |
100 |
|
|
return (ABORT); |
101 |
|
|
else if (bufp[0] == '\0') |
102 |
|
|
return (FALSE); |
103 |
|
|
if (savebuffers(f, n) == ABORT) |
104 |
|
|
return (ABORT); |
105 |
|
|
(void)strlcpy(compile_last_command, bufp, sizeof(compile_last_command)); |
106 |
|
|
|
107 |
|
|
if ((bp = compile_mode("*compile*", cprompt)) == NULL) |
108 |
|
|
return (FALSE); |
109 |
|
|
if ((wp = popbuf(bp, WNONE)) == NULL) |
110 |
|
|
return (FALSE); |
111 |
|
|
curbp = bp; |
112 |
|
|
compile_win = curwp = wp; |
113 |
|
|
gotoline(FFARG, 0); |
114 |
|
|
return (TRUE); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
/* id-utils foo. */ |
118 |
|
|
/* ARGSUSED */ |
119 |
|
|
static int |
120 |
|
|
gid(int f, int n) |
121 |
|
|
{ |
122 |
|
|
char command[NFILEN]; |
123 |
|
|
char cprompt[NFILEN], *bufp; |
124 |
|
|
int c; |
125 |
|
|
struct buffer *bp; |
126 |
|
|
struct mgwin *wp; |
127 |
|
|
int i, j, len; |
128 |
|
|
|
129 |
|
|
/* catch ([^\s(){}]+)[\s(){}]* */ |
130 |
|
|
|
131 |
|
|
i = curwp->w_doto; |
132 |
|
|
/* Skip backwards over delimiters we are currently on */ |
133 |
|
|
while (i > 0) { |
134 |
|
|
c = lgetc(curwp->w_dotp, i); |
135 |
|
|
if (isalnum(c) || c == '_') |
136 |
|
|
break; |
137 |
|
|
|
138 |
|
|
i--; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
/* Skip the symbol itself */ |
142 |
|
|
for (; i > 0; i--) { |
143 |
|
|
c = lgetc(curwp->w_dotp, i - 1); |
144 |
|
|
if (!isalnum(c) && c != '_') |
145 |
|
|
break; |
146 |
|
|
} |
147 |
|
|
/* Fill the symbol in cprompt[] */ |
148 |
|
|
for (j = 0; j < sizeof(cprompt) - 1 && i < llength(curwp->w_dotp); |
149 |
|
|
j++, i++) { |
150 |
|
|
c = lgetc(curwp->w_dotp, i); |
151 |
|
|
if (!isalnum(c) && c != '_') |
152 |
|
|
break; |
153 |
|
|
cprompt[j] = c; |
154 |
|
|
} |
155 |
|
|
cprompt[j] = '\0'; |
156 |
|
|
|
157 |
|
|
if ((bufp = eread("Run gid (with args): ", cprompt, NFILEN, |
158 |
|
|
(j ? EFDEF : 0) | EFNEW | EFCR)) == NULL) |
159 |
|
|
return (ABORT); |
160 |
|
|
else if (bufp[0] == '\0') |
161 |
|
|
return (FALSE); |
162 |
|
|
len = snprintf(command, sizeof(command), "gid %s", cprompt); |
163 |
|
|
if (len < 0 || len >= sizeof(command)) |
164 |
|
|
return (FALSE); |
165 |
|
|
|
166 |
|
|
if ((bp = compile_mode("*gid*", command)) == NULL) |
167 |
|
|
return (FALSE); |
168 |
|
|
if ((wp = popbuf(bp, WNONE)) == NULL) |
169 |
|
|
return (FALSE); |
170 |
|
|
curbp = bp; |
171 |
|
|
compile_win = curwp = wp; |
172 |
|
|
return (TRUE); |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
struct buffer * |
176 |
|
|
compile_mode(const char *name, const char *command) |
177 |
|
|
{ |
178 |
|
|
struct buffer *bp; |
179 |
|
|
FILE *fpipe; |
180 |
|
|
char *buf; |
181 |
|
|
size_t sz; |
182 |
|
|
ssize_t len; |
183 |
|
|
int ret, n; |
184 |
|
|
char cwd[NFILEN], qcmd[NFILEN]; |
185 |
|
|
char timestr[NTIME]; |
186 |
|
|
time_t t; |
187 |
|
|
|
188 |
|
|
buf = NULL; |
189 |
|
|
sz = 0; |
190 |
|
|
|
191 |
|
|
n = snprintf(qcmd, sizeof(qcmd), "%s 2>&1", command); |
192 |
|
|
if (n < 0 || n >= sizeof(qcmd)) |
193 |
|
|
return (NULL); |
194 |
|
|
|
195 |
|
|
bp = bfind(name, TRUE); |
196 |
|
|
if (bclear(bp) != TRUE) |
197 |
|
|
return (NULL); |
198 |
|
|
|
199 |
|
|
if (getbufcwd(bp->b_cwd, sizeof(bp->b_cwd)) != TRUE) |
200 |
|
|
return (NULL); |
201 |
|
|
addlinef(bp, "cd %s", bp->b_cwd); |
202 |
|
|
addline(bp, qcmd); |
203 |
|
|
addline(bp, ""); |
204 |
|
|
|
205 |
|
|
if (getcwd(cwd, sizeof(cwd)) == NULL) |
206 |
|
|
panic("Can't get current directory!"); |
207 |
|
|
if (chdir(bp->b_cwd) == -1) { |
208 |
|
|
dobeep(); |
209 |
|
|
ewprintf("Can't change dir to %s", bp->b_cwd); |
210 |
|
|
return (NULL); |
211 |
|
|
} |
212 |
|
|
if ((fpipe = popen(qcmd, "r")) == NULL) { |
213 |
|
|
dobeep(); |
214 |
|
|
ewprintf("Problem opening pipe"); |
215 |
|
|
return (NULL); |
216 |
|
|
} |
217 |
|
|
while ((len = getline(&buf, &sz, fpipe)) != -1) { |
218 |
|
|
if (buf[len - 1] == '\n') |
219 |
|
|
buf[len - 1] = '\0'; |
220 |
|
|
addline(bp, buf); |
221 |
|
|
} |
222 |
|
|
free(buf); |
223 |
|
|
if (ferror(fpipe)) |
224 |
|
|
ewprintf("Problem reading pipe"); |
225 |
|
|
ret = pclose(fpipe); |
226 |
|
|
t = time(NULL); |
227 |
|
|
strftime(timestr, sizeof(timestr), "%a %b %e %T %Y", localtime(&t)); |
228 |
|
|
addline(bp, ""); |
229 |
|
|
if (ret != 0) |
230 |
|
|
addlinef(bp, "Command exited abnormally with code %d" |
231 |
|
|
" at %s", ret, timestr); |
232 |
|
|
else |
233 |
|
|
addlinef(bp, "Command finished at %s", timestr); |
234 |
|
|
|
235 |
|
|
bp->b_dotp = bfirstlp(bp); |
236 |
|
|
bp->b_modes[0] = name_mode("fundamental"); |
237 |
|
|
bp->b_modes[1] = name_mode("compile"); |
238 |
|
|
bp->b_nmodes = 1; |
239 |
|
|
|
240 |
|
|
compile_buffer = bp; |
241 |
|
|
|
242 |
|
|
if (chdir(cwd) == -1) { |
243 |
|
|
dobeep(); |
244 |
|
|
ewprintf("Can't change dir back to %s", cwd); |
245 |
|
|
return (NULL); |
246 |
|
|
} |
247 |
|
|
return (bp); |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
/* ARGSUSED */ |
251 |
|
|
static int |
252 |
|
|
compile_goto_error(int f, int n) |
253 |
|
|
{ |
254 |
|
|
struct buffer *bp; |
255 |
|
|
struct mgwin *wp; |
256 |
|
|
char *fname, *line, *lp, *ln; |
257 |
|
|
int lineno; |
258 |
|
|
char *adjf, path[NFILEN]; |
259 |
|
|
const char *errstr; |
260 |
|
|
struct line *last; |
261 |
|
|
|
262 |
|
|
compile_win = curwp; |
263 |
|
|
compile_buffer = curbp; |
264 |
|
|
last = blastlp(compile_buffer); |
265 |
|
|
|
266 |
|
|
retry: |
267 |
|
|
/* last line is compilation result */ |
268 |
|
|
if (curwp->w_dotp == last) |
269 |
|
|
return (FALSE); |
270 |
|
|
|
271 |
|
|
if ((line = linetostr(curwp->w_dotp)) == NULL) |
272 |
|
|
return (FALSE); |
273 |
|
|
lp = line; |
274 |
|
|
if ((fname = strsep(&lp, ":")) == NULL || *fname == '\0') |
275 |
|
|
goto fail; |
276 |
|
|
if ((ln = strsep(&lp, ":")) == NULL || *ln == '\0') |
277 |
|
|
goto fail; |
278 |
|
|
lineno = (int)strtonum(ln, INT_MIN, INT_MAX, &errstr); |
279 |
|
|
if (errstr) |
280 |
|
|
goto fail; |
281 |
|
|
|
282 |
|
|
if (fname && fname[0] != '/') { |
283 |
|
|
if (getbufcwd(path, sizeof(path)) == FALSE) |
284 |
|
|
goto fail; |
285 |
|
|
if (strlcat(path, fname, sizeof(path)) >= sizeof(path)) |
286 |
|
|
goto fail; |
287 |
|
|
adjf = path; |
288 |
|
|
} else { |
289 |
|
|
adjf = adjustname(fname, TRUE); |
290 |
|
|
} |
291 |
|
|
free(line); |
292 |
|
|
|
293 |
|
|
if (adjf == NULL) |
294 |
|
|
return (FALSE); |
295 |
|
|
|
296 |
|
|
if ((bp = findbuffer(adjf)) == NULL) |
297 |
|
|
return (FALSE); |
298 |
|
|
if ((wp = popbuf(bp, WNONE)) == NULL) |
299 |
|
|
return (FALSE); |
300 |
|
|
curbp = bp; |
301 |
|
|
curwp = wp; |
302 |
|
|
if (bp->b_fname[0] == '\0') |
303 |
|
|
readin(adjf); |
304 |
|
|
gotoline(FFARG, lineno); |
305 |
|
|
return (TRUE); |
306 |
|
|
fail: |
307 |
|
|
free(line); |
308 |
|
|
if (curwp->w_dotp != blastlp(curbp)) { |
309 |
|
|
curwp->w_dotp = lforw(curwp->w_dotp); |
310 |
|
|
curwp->w_rflag |= WFMOVE; |
311 |
|
|
goto retry; |
312 |
|
|
} |
313 |
|
|
dobeep(); |
314 |
|
|
ewprintf("No more hits"); |
315 |
|
|
return (FALSE); |
316 |
|
|
} |
317 |
|
|
|
318 |
|
|
/* ARGSUSED */ |
319 |
|
|
int |
320 |
|
|
next_error(int f, int n) |
321 |
|
|
{ |
322 |
|
|
if (compile_win == NULL || compile_buffer == NULL) { |
323 |
|
|
dobeep(); |
324 |
|
|
ewprintf("No compilation active"); |
325 |
|
|
return (FALSE); |
326 |
|
|
} |
327 |
|
|
curwp = compile_win; |
328 |
|
|
curbp = compile_buffer; |
329 |
|
|
if (curwp->w_dotp == blastlp(curbp)) { |
330 |
|
|
dobeep(); |
331 |
|
|
ewprintf("No more hits"); |
332 |
|
|
return (FALSE); |
333 |
|
|
} |
334 |
|
|
curwp->w_dotp = lforw(curwp->w_dotp); |
335 |
|
|
curwp->w_rflag |= WFMOVE; |
336 |
|
|
|
337 |
|
|
return (compile_goto_error(f, n)); |
338 |
|
|
} |
339 |
|
|
|
340 |
|
|
/* |
341 |
|
|
* Since we don't have variables (we probably should) these are command |
342 |
|
|
* processors for changing the values of mode flags. |
343 |
|
|
*/ |
344 |
|
|
/* ARGSUSED */ |
345 |
|
|
int |
346 |
|
|
globalwdtoggle(int f, int n) |
347 |
|
|
{ |
348 |
|
|
if (f & FFARG) |
349 |
|
|
globalwd = n > 0; |
350 |
|
|
else |
351 |
|
|
globalwd = !globalwd; |
352 |
|
|
|
353 |
|
|
sgarbf = TRUE; |
354 |
|
|
|
355 |
|
|
return (TRUE); |
356 |
|
|
} |