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