[BACK]Return to grep.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/grep.c, Revision 1.40

1.40    ! lum         1: /*     $OpenBSD: grep.c,v 1.39 2013/06/01 17:31:11 lum 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) {
1.40    ! lum       196:                dobeep();
1.28      kjell     197:                ewprintf("Can't change dir to %s", bp->b_cwd);
1.18      kjell     198:                return (NULL);
1.19      deraadt   199:        }
1.29      kjell     200:        if ((fpipe = popen(qcmd, "r")) == NULL) {
1.40    ! lum       201:                dobeep();
1.1       art       202:                ewprintf("Problem opening pipe");
1.13      db        203:                return (NULL);
1.1       art       204:        }
                    205:        /*
                    206:         * We know that our commands are nice and the last line will end with
                    207:         * a \n, so we don't need to try to deal with the last line problem
                    208:         * in fgetln.
                    209:         */
1.26      kjell     210:        while ((buf = fgetln(fpipe, &len)) != NULL) {
1.1       art       211:                buf[len - 1] = '\0';
                    212:                addline(bp, buf);
                    213:        }
1.26      kjell     214:        ret = pclose(fpipe);
1.18      kjell     215:        t = time(NULL);
                    216:        strftime(timestr, sizeof(timestr), "%a %b %e %T %Y", localtime(&t));
1.1       art       217:        addline(bp, "");
1.18      kjell     218:        if (ret != 0)
                    219:                addlinef(bp, "Command exited abnormally with code %d"
                    220:                    " at %s", ret, timestr);
                    221:        else
                    222:                addlinef(bp, "Command finished at %s", timestr);
                    223:
1.32      kjell     224:        bp->b_dotp = bfirstlp(bp);
1.1       art       225:        bp->b_modes[0] = name_mode("fundamental");
                    226:        bp->b_modes[1] = name_mode("compile");
                    227:        bp->b_nmodes = 1;
                    228:
                    229:        compile_buffer = bp;
                    230:
1.18      kjell     231:        if (chdir(cwd) == -1) {
1.40    ! lum       232:                dobeep();
1.18      kjell     233:                ewprintf("Can't change dir back to %s", cwd);
                    234:                return (NULL);
1.19      deraadt   235:        }
1.13      db        236:        return (bp);
1.1       art       237: }
                    238:
1.20      kjell     239: /* ARGSUSED */
1.1       art       240: static int
                    241: compile_goto_error(int f, int n)
                    242: {
1.24      deraadt   243:        struct buffer   *bp;
                    244:        struct mgwin    *wp;
1.21      kjell     245:        char    *fname, *line, *lp, *ln;
1.29      kjell     246:        int      lineno;
1.28      kjell     247:        char    *adjf, path[NFILEN];
1.21      kjell     248:        const char *errstr;
1.24      deraadt   249:        struct line     *last;
1.1       art       250:
                    251:        compile_win = curwp;
                    252:        compile_buffer = curbp;
1.32      kjell     253:        last = blastlp(compile_buffer);
1.22      deraadt   254:
1.21      kjell     255:  retry:
                    256:        /* last line is compilation result */
                    257:        if (curwp->w_dotp == last)
                    258:                return (FALSE);
1.33      deraadt   259:
1.29      kjell     260:        if ((line = linetostr(curwp->w_dotp)) == NULL)
1.13      db        261:                return (FALSE);
1.1       art       262:        lp = line;
1.21      kjell     263:        if ((fname = strsep(&lp, ":")) == NULL || *fname == '\0')
1.1       art       264:                goto fail;
1.21      kjell     265:        if ((ln = strsep(&lp, ":")) == NULL || *ln == '\0')
1.1       art       266:                goto fail;
1.24      deraadt   267:        lineno = (int)strtonum(ln, INT_MIN, INT_MAX, &errstr);
1.21      kjell     268:        if (errstr)
1.1       art       269:                goto fail;
1.33      deraadt   270:
1.28      kjell     271:        if (fname && fname[0] != '/') {
1.34      kjell     272:                if (getbufcwd(path, sizeof(path)) == FALSE)
                    273:                        goto fail;
1.28      kjell     274:                if (strlcat(path, fname, sizeof(path)) >= sizeof(path))
                    275:                        goto fail;
                    276:                adjf = path;
                    277:        } else {
1.30      jason     278:                adjf = adjustname(fname, TRUE);
1.28      kjell     279:        }
1.1       art       280:        free(line);
                    281:
1.7       vincent   282:        if (adjf == NULL)
                    283:                return (FALSE);
1.10      vincent   284:
1.1       art       285:        if ((bp = findbuffer(adjf)) == NULL)
1.13      db        286:                return (FALSE);
1.38      kjell     287:        if ((wp = popbuf(bp, WNONE)) == NULL)
1.13      db        288:                return (FALSE);
1.1       art       289:        curbp = bp;
                    290:        curwp = wp;
1.27      kjell     291:        if (bp->b_fname[0] == '\0')
1.1       art       292:                readin(adjf);
                    293:        gotoline(FFARG, lineno);
1.13      db        294:        return (TRUE);
1.1       art       295: fail:
1.3       deraadt   296:        free(line);
1.32      kjell     297:        if (curwp->w_dotp != blastlp(curbp)) {
1.1       art       298:                curwp->w_dotp = lforw(curwp->w_dotp);
1.37      kjell     299:                curwp->w_rflag |= WFMOVE;
1.1       art       300:                goto retry;
                    301:        }
1.40    ! lum       302:        dobeep();
1.1       art       303:        ewprintf("No more hits");
1.13      db        304:        return (FALSE);
1.1       art       305: }
                    306:
1.20      kjell     307: /* ARGSUSED */
1.17      kjell     308: int
1.1       art       309: next_error(int f, int n)
                    310: {
                    311:        if (compile_win == NULL || compile_buffer == NULL) {
1.40    ! lum       312:                dobeep();
1.1       art       313:                ewprintf("No compilation active");
1.13      db        314:                return (FALSE);
1.1       art       315:        }
                    316:        curwp = compile_win;
                    317:        curbp = compile_buffer;
1.32      kjell     318:        if (curwp->w_dotp == blastlp(curbp)) {
1.40    ! lum       319:                dobeep();
1.1       art       320:                ewprintf("No more hits");
1.13      db        321:                return (FALSE);
1.1       art       322:        }
                    323:        curwp->w_dotp = lforw(curwp->w_dotp);
1.37      kjell     324:        curwp->w_rflag |= WFMOVE;
1.1       art       325:
1.13      db        326:        return (compile_goto_error(f, n));
1.34      kjell     327: }
                    328:
                    329: /*
                    330:  * Since we don't have variables (we probably should) these are command
                    331:  * processors for changing the values of mode flags.
                    332:  */
                    333: /* ARGSUSED */
                    334: int
                    335: globalwdtoggle(int f, int n)
                    336: {
                    337:        if (f & FFARG)
                    338:                globalwd = n > 0;
                    339:        else
                    340:                globalwd = !globalwd;
                    341:
                    342:        sgarbf = TRUE;
                    343:
                    344:        return (TRUE);
1.1       art       345: }