1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/* $OpenBSD: grep.c,v 1.24 2005/11/18 20:56:53 deraadt Exp $ */
/*
* Copyright (c) 2001 Artur Grabowski <art@openbsd.org>.
* Copyright (c) 2005 Kjell Wooding <kjell@openbsd.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "def.h"
#include "kbd.h"
#include "funmap.h"
#include <sys/types.h>
#include <ctype.h>
#include <libgen.h>
#include <time.h>
static int compile_goto_error(int, int);
int next_error(int, int);
static int grep(int, int);
static int compile(int, int);
static int gid(int, int);
static struct buffer *compile_mode(const char *, const char *, const char *);
static int getbufcwd(char *, size_t);
void grep_init(void);
static char compile_last_command[NFILEN] = "make ";
/*
* Hints for next-error
*
* XXX - need some kind of callback to find out when those get killed.
*/
struct mgwin *compile_win;
struct buffer *compile_buffer;
static PF compile_pf[] = {
compile_goto_error
};
static struct KEYMAPE (1 + IMAPEXT) compilemap = {
1,
1 + IMAPEXT,
rescan,
{
{ CCHR('M'), CCHR('M'), compile_pf, NULL }
}
};
void
grep_init(void)
{
funmap_add(compile_goto_error, "compile-goto-error");
funmap_add(next_error, "next-error");
funmap_add(grep, "grep");
funmap_add(compile, "compile");
funmap_add(gid, "gid");
maps_add((KEYMAP *)&compilemap, "compile");
}
/* ARGSUSED */
static int
grep(int f, int n)
{
char command[NFILEN + 20];
char prompt[NFILEN], *bufp;
struct buffer *bp;
struct mgwin *wp;
char path[NFILEN];
/* get buffer cwd */
if (getbufcwd(path, sizeof(path)) == FALSE) {
ewprintf("Failed. "
"Can't get working directory of current buffer.");
return (FALSE);
}
(void)strlcpy(prompt, "grep -n ", sizeof(prompt));
if ((bufp = eread("Run grep: ", prompt, NFILEN,
EFDEF | EFNEW | EFCR)) == NULL)
return (ABORT);
else if (bufp[0] == '\0')
return (FALSE);
(void)snprintf(command, sizeof(command), "%s /dev/null", bufp);
if ((bp = compile_mode("*grep*", command, path)) == NULL)
return (FALSE);
if ((wp = popbuf(bp)) == NULL)
return (FALSE);
curbp = bp;
compile_win = curwp = wp;
return (TRUE);
}
/* ARGSUSED */
static int
compile(int f, int n)
{
char command[NFILEN + 20];
char prompt[NFILEN], *bufp;
struct buffer *bp;
struct mgwin *wp;
char path[NFILEN];
/* get buffer cwd */
if (getbufcwd(path, sizeof(path)) == FALSE) {
ewprintf("Failed. "
"Can't get working directory of current buffer.");
return (FALSE);
}
(void)strlcpy(prompt, compile_last_command, sizeof(prompt));
if ((bufp = eread("Compile command: ", prompt, NFILEN,
EFDEF | EFNEW | EFCR)) == NULL)
return (ABORT);
else if (bufp[0] == '\0')
return (FALSE);
if (savebuffers(f, n) == ABORT)
return (ABORT);
(void)strlcpy(compile_last_command, bufp, sizeof(compile_last_command));
(void)snprintf(command, sizeof(command), "%s 2>&1", bufp);
if ((bp = compile_mode("*compile*", command, path)) == NULL)
return (FALSE);
if ((wp = popbuf(bp)) == NULL)
return (FALSE);
curbp = bp;
compile_win = curwp = wp;
return (TRUE);
}
/* id-utils foo. */
/* ARGSUSED */
static int
gid(int f, int n)
{
char command[NFILEN + 20];
char prompt[NFILEN], c, *bufp;
struct buffer *bp;
struct mgwin *wp;
int i, j;
char path[NFILEN];
/* get buffer cwd */
if (getbufcwd(path, sizeof(path)) == FALSE) {
ewprintf("Failed. "
"Can't get working directory of current buffer.");
return (FALSE);
}
/* catch ([^\s(){}]+)[\s(){}]* */
i = curwp->w_doto;
/* Skip backwards over delimiters we are currently on */
while (i > 0) {
c = lgetc(curwp->w_dotp, i);
if (isalnum(c) || c == '_')
break;
i--;
}
/* Skip the symbol itself */
for (; i > 0; i--) {
c = lgetc(curwp->w_dotp, i - 1);
if (!isalnum(c) && c != '_')
break;
}
/* Fill the symbol in prompt[] */
for (j = 0; j < sizeof(prompt) - 1 && i < llength(curwp->w_dotp);
j++, i++) {
c = lgetc(curwp->w_dotp, i);
if (!isalnum(c) && c != '_')
break;
prompt[j] = c;
}
prompt[j] = '\0';
if ((bufp = eread("Run gid (with args): ", prompt, NFILEN,
(j ? EFDEF : 0) | EFNEW | EFCR)) == NULL)
return (ABORT);
else if (bufp[0] == '\0')
return (FALSE);
(void)snprintf(command, sizeof(command), "gid %s", prompt);
if ((bp = compile_mode("*gid*", command, path)) == NULL)
return (FALSE);
if ((wp = popbuf(bp)) == NULL)
return (FALSE);
curbp = bp;
compile_win = curwp = wp;
return (TRUE);
}
struct buffer *
compile_mode(const char *name, const char *command, const char *path)
{
struct buffer *bp;
FILE *pipe;
char *buf;
size_t len;
int ret;
char cwd[NFILEN];
char timestr[NTIME];
time_t t;
bp = bfind(name, TRUE);
if (bclear(bp) != TRUE)
return (NULL);
addlinef(bp, "cd %s", path);
addline(bp, command);
addline(bp, "");
if (getcwd(cwd, sizeof(cwd)) == NULL)
panic("Can't get current directory!");
if (chdir(path) == -1) {
ewprintf("Can't change dir to %s", path);
return (NULL);
}
if ((pipe = popen(command, "r")) == NULL) {
ewprintf("Problem opening pipe");
return (NULL);
}
/*
* We know that our commands are nice and the last line will end with
* a \n, so we don't need to try to deal with the last line problem
* in fgetln.
*/
while ((buf = fgetln(pipe, &len)) != NULL) {
buf[len - 1] = '\0';
addline(bp, buf);
}
ret = pclose(pipe);
t = time(NULL);
strftime(timestr, sizeof(timestr), "%a %b %e %T %Y", localtime(&t));
addline(bp, "");
if (ret != 0)
addlinef(bp, "Command exited abnormally with code %d"
" at %s", ret, timestr);
else
addlinef(bp, "Command finished at %s", timestr);
bp->b_dotp = lforw(bp->b_linep); /* go to first line */
bp->b_modes[0] = name_mode("fundamental");
bp->b_modes[1] = name_mode("compile");
bp->b_nmodes = 1;
compile_buffer = bp;
if (chdir(cwd) == -1) {
ewprintf("Can't change dir back to %s", cwd);
return (NULL);
}
return (bp);
}
/* ARGSUSED */
static int
compile_goto_error(int f, int n)
{
struct buffer *bp;
struct mgwin *wp;
char *fname, *line, *lp, *ln;
int lineno, len;
char *adjf;
const char *errstr;
struct line *last;
compile_win = curwp;
compile_buffer = curbp;
last = lback(compile_buffer->b_linep);
retry:
/* last line is compilation result */
if (curwp->w_dotp == last)
return (FALSE);
len = llength(curwp->w_dotp);
if ((line = malloc(len + 1)) == NULL)
return (FALSE);
(void)memcpy(line, curwp->w_dotp->l_text, len);
line[len] = '\0';
lp = line;
if ((fname = strsep(&lp, ":")) == NULL || *fname == '\0')
goto fail;
if ((ln = strsep(&lp, ":")) == NULL || *ln == '\0')
goto fail;
lineno = (int)strtonum(ln, INT_MIN, INT_MAX, &errstr);
if (errstr)
goto fail;
adjf = adjustname(fname);
free(line);
if (adjf == NULL)
return (FALSE);
if ((bp = findbuffer(adjf)) == NULL)
return (FALSE);
if ((wp = popbuf(bp)) == NULL)
return (FALSE);
curbp = bp;
curwp = wp;
if (bp->b_fname[0] == 0)
readin(adjf);
gotoline(FFARG, lineno);
return (TRUE);
fail:
free(line);
if (curwp->w_dotp != lback(curbp->b_linep)) {
curwp->w_dotp = lforw(curwp->w_dotp);
curwp->w_flag |= WFMOVE;
goto retry;
}
ewprintf("No more hits");
return (FALSE);
}
/* ARGSUSED */
int
next_error(int f, int n)
{
if (compile_win == NULL || compile_buffer == NULL) {
ewprintf("No compilation active");
return (FALSE);
}
curwp = compile_win;
curbp = compile_buffer;
if (curwp->w_dotp == lback(curbp->b_linep)) {
ewprintf("No more hits");
return (FALSE);
}
curwp->w_dotp = lforw(curwp->w_dotp);
curwp->w_flag |= WFMOVE;
return (compile_goto_error(f, n));
}
/*
* Return the working directory for the current buffer, terminated
* with a '/'. First, try to extract it from the current buffer's
* filename. If that fails, use global cwd.
*/
static int
getbufcwd(char *path, size_t plen)
{
char *dname, cwd[NFILEN];
if (plen == 0)
goto error;
if (curbp->b_fname && curbp->b_fname[0] != '\0' &&
(dname = dirname(curbp->b_fname)) != NULL) {
if (strlcpy(path, dname, plen) >= plen)
goto error;
if (strlcat(path, "/", plen) >= plen)
goto error;
} else {
if ((dname = getcwd(cwd, sizeof(cwd))) == NULL)
goto error;
if (strlcpy(path, dname, plen) >= plen)
goto error;
}
return (TRUE);
error:
path = NULL;
return (FALSE);
}
|