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

Annotation of src/usr.bin/mg/dired.c, Revision 1.57

1.57    ! lum         1: /*     $OpenBSD: dired.c,v 1.56 2013/05/29 19:16:48 lum Exp $  */
1.6       niklas      2:
1.20      kjell       3: /* This file is in the public domain. */
                      4:
                      5: /* dired module for mg 2a
                      6:  * by Robert A. Larson
                      7:  */
1.1       deraadt     8:
                      9: #include "def.h"
1.28      kjell      10: #include "funmap.h"
1.9       vincent    11: #include "kbd.h"
1.27      kjell      12:
1.12      vincent    13: #include <sys/types.h>
                     14: #include <sys/stat.h>
1.25      deraadt    15: #include <sys/time.h>
                     16: #include <sys/resource.h>
1.12      vincent    17: #include <sys/wait.h>
                     18:
1.24      kjell      19: #include <ctype.h>
1.12      vincent    20: #include <signal.h>
                     21: #include <fcntl.h>
1.54      lum        22: #include <err.h>
1.12      vincent    23: #include <errno.h>
                     24: #include <libgen.h>
1.50      lum        25: #include <stdarg.h>
1.1       deraadt    26:
1.24      kjell      27: void            dired_init(void);
1.27      kjell      28: static int      dired(int, int);
                     29: static int      d_otherwindow(int, int);
                     30: static int      d_undel(int, int);
                     31: static int      d_undelbak(int, int);
                     32: static int      d_findfile(int, int);
                     33: static int      d_ffotherwindow(int, int);
                     34: static int      d_expunge(int, int);
                     35: static int      d_copy(int, int);
                     36: static int      d_del(int, int);
                     37: static int      d_rename(int, int);
1.50      lum        38: static int      d_exec(int, struct buffer *, const char *, const char *, ...);
1.27      kjell      39: static int      d_shell_command(int, int);
                     40: static int      d_create_directory(int, int);
1.34      kjell      41: static int      d_makename(struct line *, char *, size_t);
1.49      lum        42: static int      d_warpdot(struct line *, int *);
                     43: static int      d_forwpage(int, int);
                     44: static int      d_backpage(int, int);
                     45: static int      d_forwline(int, int);
                     46: static int      d_backline(int, int);
1.47      lum        47: static void     reaper(int);
1.54      lum        48: static struct buffer   *refreshbuffer(struct buffer *);
1.1       deraadt    49:
1.23      kjell      50: extern struct keymap_s helpmap, cXmap, metamap;
                     51:
                     52: static PF dirednul[] = {
                     53:        setmark,                /* ^@ */
                     54:        gotobol,                /* ^A */
                     55:        backchar,               /* ^B */
                     56:        rescan,                 /* ^C */
                     57:        d_del,                  /* ^D */
                     58:        gotoeol,                /* ^E */
                     59:        forwchar,               /* ^F */
                     60:        ctrlg,                  /* ^G */
                     61:        NULL,                   /* ^H */
1.12      vincent    62: };
                     63:
1.23      kjell      64: static PF diredcl[] = {
                     65:        reposition,             /* ^L */
1.15      db         66:        d_findfile,             /* ^M */
1.49      lum        67:        d_forwline,             /* ^N */
1.23      kjell      68:        rescan,                 /* ^O */
1.49      lum        69:        d_backline,             /* ^P */
1.23      kjell      70:        rescan,                 /* ^Q */
                     71:        backisearch,            /* ^R */
                     72:        forwisearch,            /* ^S */
                     73:        rescan,                 /* ^T */
                     74:        universal_argument,     /* ^U */
1.49      lum        75:        d_forwpage,             /* ^V */
1.23      kjell      76:        rescan,                 /* ^W */
                     77:        NULL                    /* ^X */
                     78: };
                     79:
                     80: static PF diredcz[] = {
                     81:        spawncli,               /* ^Z */
                     82:        NULL,                   /* esc */
                     83:        rescan,                 /* ^\ */
                     84:        rescan,                 /* ^] */
                     85:        rescan,                 /* ^^ */
                     86:        rescan,                 /* ^_ */
1.49      lum        87:        d_forwline,             /* SP */
1.27      kjell      88:        d_shell_command,        /* ! */
                     89:        rescan,                 /* " */
                     90:        rescan,                 /* # */
                     91:        rescan,                 /* $ */
                     92:        rescan,                 /* % */
                     93:        rescan,                 /* & */
                     94:        rescan,                 /* ' */
                     95:        rescan,                 /* ( */
                     96:        rescan,                 /* ) */
                     97:        rescan,                 /* * */
                     98:        d_create_directory      /* + */
1.23      kjell      99: };
                    100:
                    101: static PF diredc[] = {
                    102:        d_copy,                 /* c */
                    103:        d_del,                  /* d */
                    104:        d_findfile,             /* e */
                    105:        d_findfile              /* f */
                    106: };
                    107:
                    108: static PF diredn[] = {
1.49      lum       109:        d_forwline,             /* n */
1.23      kjell     110:        d_ffotherwindow,        /* o */
1.49      lum       111:        d_backline,             /* p */
1.23      kjell     112:        rescan,                 /* q */
                    113:        d_rename,               /* r */
                    114:        rescan,                 /* s */
                    115:        rescan,                 /* t */
                    116:        d_undel,                /* u */
                    117:        rescan,                 /* v */
                    118:        rescan,                 /* w */
                    119:        d_expunge               /* x */
                    120: };
                    121:
                    122: static PF direddl[] = {
                    123:        d_undelbak              /* del */
1.9       vincent   124: };
1.13      deraadt   125:
1.49      lum       126: static PF diredbp[] = {
                    127:        d_backpage              /* v */
                    128: };
                    129:
                    130: static PF dirednull[] = {
                    131:        NULL
                    132: };
                    133:
1.23      kjell     134: #ifndef        DIRED_XMAPS
                    135: #define        NDIRED_XMAPS    0       /* number of extra map sections */
                    136: #endif /* DIRED_XMAPS */
                    137:
1.49      lum       138: static struct KEYMAPE (1 + IMAPEXT) d_backpagemap = {
                    139:        1,
                    140:        1 + IMAPEXT,
                    141:        rescan,
                    142:        {
                    143:                {
                    144:                'v', 'v', diredbp, NULL
                    145:                }
                    146:        }
                    147: };
                    148:
                    149: static struct KEYMAPE (7 + NDIRED_XMAPS + IMAPEXT) diredmap = {
                    150:        7 + NDIRED_XMAPS,
                    151:        7 + NDIRED_XMAPS + IMAPEXT,
1.9       vincent   152:        rescan,
                    153:        {
1.23      kjell     154:                {
                    155:                        CCHR('@'), CCHR('H'), dirednul, (KEYMAP *) & helpmap
                    156:                },
                    157:                {
                    158:                        CCHR('L'), CCHR('X'), diredcl, (KEYMAP *) & cXmap
                    159:                },
                    160:                {
1.49      lum       161:                        CCHR('['), CCHR('['), dirednull, (KEYMAP *) &
                    162:                        d_backpagemap
                    163:                },
                    164:                {
1.27      kjell     165:                        CCHR('Z'), '+', diredcz, (KEYMAP *) & metamap
1.23      kjell     166:                },
                    167:                {
                    168:                        'c', 'f', diredc, NULL
                    169:                },
                    170:                {
                    171:                        'n', 'x', diredn, NULL
                    172:                },
                    173:                {
                    174:                        CCHR('?'), CCHR('?'), direddl, NULL
                    175:                },
                    176: #ifdef DIRED_XMAPS
                    177:                DIRED_XMAPS,    /* map sections for dired mode keys      */
                    178: #endif /* DIRED_XMAPS */
1.9       vincent   179:        }
                    180: };
                    181:
1.23      kjell     182: void
                    183: dired_init(void)
                    184: {
1.27      kjell     185:        funmap_add(dired, "dired");
                    186:        funmap_add(d_undelbak, "dired-backup-unflag");
1.53      lum       187:        funmap_add(d_create_directory, "dired-create-directory");
1.27      kjell     188:        funmap_add(d_copy, "dired-copy-file");
                    189:        funmap_add(d_expunge, "dired-do-deletions");
                    190:        funmap_add(d_findfile, "dired-find-file");
                    191:        funmap_add(d_ffotherwindow, "dired-find-file-other-window");
                    192:        funmap_add(d_del, "dired-flag-file-deleted");
1.49      lum       193:        funmap_add(d_forwline, "dired-next-line");
1.27      kjell     194:        funmap_add(d_otherwindow, "dired-other-window");
1.49      lum       195:        funmap_add(d_backline, "dired-previous-line");
1.27      kjell     196:        funmap_add(d_rename, "dired-rename-file");
1.49      lum       197:        funmap_add(d_backpage, "dired-scroll-down");
                    198:        funmap_add(d_forwpage, "dired-scroll-up");
1.27      kjell     199:        funmap_add(d_undel, "dired-unflag");
1.23      kjell     200:        maps_add((KEYMAP *)&diredmap, "dired");
1.27      kjell     201:        dobindkey(fundamental_map, "dired", "^Xd");
1.23      kjell     202: }
1.12      vincent   203:
1.5       millert   204: /* ARGSUSED */
                    205: int
1.10      vincent   206: dired(int f, int n)
1.1       deraadt   207: {
1.33      kjell     208:        char             dname[NFILEN], *bufp, *slash;
1.31      deraadt   209:        struct buffer   *bp;
1.1       deraadt   210:
1.19      cloder    211:        if (curbp->b_fname && curbp->b_fname[0] != '\0') {
1.33      kjell     212:                (void)strlcpy(dname, curbp->b_fname, sizeof(dname));
                    213:                if ((slash = strrchr(dname, '/')) != NULL) {
1.19      cloder    214:                        *(slash + 1) = '\0';
                    215:                }
                    216:        } else {
1.33      kjell     217:                if (getcwd(dname, sizeof(dname)) == NULL)
                    218:                        dname[0] = '\0';
1.19      cloder    219:        }
                    220:
1.33      kjell     221:        if ((bufp = eread("Dired: ", dname, NFILEN,
1.21      kjell     222:            EFDEF | EFNEW | EFCR)) == NULL)
1.15      db        223:                return (ABORT);
1.21      kjell     224:        if (bufp[0] == '\0')
                    225:                return (FALSE);
1.14      vincent   226:        if ((bp = dired_(bufp)) == NULL)
1.15      db        227:                return (FALSE);
1.23      kjell     228:
1.5       millert   229:        curbp = bp;
1.37      kjell     230:        return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.1       deraadt   231: }
                    232:
1.5       millert   233: /* ARGSUSED */
                    234: int
1.10      vincent   235: d_otherwindow(int f, int n)
1.1       deraadt   236: {
1.33      kjell     237:        char             dname[NFILEN], *bufp, *slash;
1.31      deraadt   238:        struct buffer   *bp;
                    239:        struct mgwin    *wp;
1.5       millert   240:
1.23      kjell     241:        if (curbp->b_fname && curbp->b_fname[0] != '\0') {
1.33      kjell     242:                (void)strlcpy(dname, curbp->b_fname, sizeof(dname));
                    243:                if ((slash = strrchr(dname, '/')) != NULL) {
1.23      kjell     244:                        *(slash + 1) = '\0';
                    245:                }
                    246:        } else {
1.33      kjell     247:                if (getcwd(dname, sizeof(dname)) == NULL)
                    248:                        dname[0] = '\0';
1.23      kjell     249:        }
                    250:
1.33      kjell     251:        if ((bufp = eread("Dired other window: ", dname, NFILEN,
1.21      kjell     252:            EFDEF | EFNEW | EFCR)) == NULL)
1.15      db        253:                return (ABORT);
1.21      kjell     254:        else if (bufp[0] == '\0')
                    255:                return (FALSE);
1.14      vincent   256:        if ((bp = dired_(bufp)) == NULL)
1.15      db        257:                return (FALSE);
1.45      kjell     258:        if ((wp = popbuf(bp, WNONE)) == NULL)
1.15      db        259:                return (FALSE);
1.5       millert   260:        curbp = bp;
                    261:        curwp = wp;
1.15      db        262:        return (TRUE);
1.1       deraadt   263: }
                    264:
1.5       millert   265: /* ARGSUSED */
                    266: int
1.10      vincent   267: d_del(int f, int n)
1.1       deraadt   268: {
1.5       millert   269:        if (n < 0)
1.15      db        270:                return (FALSE);
1.5       millert   271:        while (n--) {
                    272:                if (llength(curwp->w_dotp) > 0)
                    273:                        lputc(curwp->w_dotp, 0, 'D');
1.40      kjell     274:                if (lforw(curwp->w_dotp) != curbp->b_headp)
1.5       millert   275:                        curwp->w_dotp = lforw(curwp->w_dotp);
                    276:        }
1.44      kjell     277:        curwp->w_rflag |= WFEDIT | WFMOVE;
1.56      lum       278:        return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
1.1       deraadt   279: }
                    280:
1.5       millert   281: /* ARGSUSED */
                    282: int
1.10      vincent   283: d_undel(int f, int n)
1.1       deraadt   284: {
1.5       millert   285:        if (n < 0)
1.15      db        286:                return (d_undelbak(f, -n));
1.5       millert   287:        while (n--) {
                    288:                if (llength(curwp->w_dotp) > 0)
                    289:                        lputc(curwp->w_dotp, 0, ' ');
1.40      kjell     290:                if (lforw(curwp->w_dotp) != curbp->b_headp)
1.5       millert   291:                        curwp->w_dotp = lforw(curwp->w_dotp);
                    292:        }
1.44      kjell     293:        curwp->w_rflag |= WFEDIT | WFMOVE;
1.56      lum       294:        return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
1.1       deraadt   295: }
                    296:
1.5       millert   297: /* ARGSUSED */
                    298: int
1.10      vincent   299: d_undelbak(int f, int n)
1.1       deraadt   300: {
1.5       millert   301:        if (n < 0)
1.15      db        302:                return (d_undel(f, -n));
1.5       millert   303:        while (n--) {
                    304:                if (llength(curwp->w_dotp) > 0)
                    305:                        lputc(curwp->w_dotp, 0, ' ');
1.40      kjell     306:                if (lback(curwp->w_dotp) != curbp->b_headp)
1.5       millert   307:                        curwp->w_dotp = lback(curwp->w_dotp);
                    308:        }
1.44      kjell     309:        curwp->w_rflag |= WFEDIT | WFMOVE;
1.56      lum       310:        return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
1.1       deraadt   311: }
                    312:
1.5       millert   313: /* ARGSUSED */
                    314: int
1.10      vincent   315: d_findfile(int f, int n)
1.1       deraadt   316: {
1.31      deraadt   317:        struct buffer   *bp;
                    318:        int              s;
                    319:        char             fname[NFILEN];
1.5       millert   320:
1.15      db        321:        if ((s = d_makename(curwp->w_dotp, fname, sizeof(fname))) == ABORT)
                    322:                return (FALSE);
1.12      vincent   323:        if (s == TRUE)
                    324:                bp = dired_(fname);
                    325:        else
                    326:                bp = findbuffer(fname);
                    327:        if (bp == NULL)
1.15      db        328:                return (FALSE);
1.5       millert   329:        curbp = bp;
1.37      kjell     330:        if (showbuffer(bp, curwp, WFFULL) != TRUE)
1.15      db        331:                return (FALSE);
1.5       millert   332:        if (bp->b_fname[0] != 0)
1.15      db        333:                return (TRUE);
                    334:        return (readin(fname));
1.1       deraadt   335: }
                    336:
1.5       millert   337: /* ARGSUSED */
                    338: int
1.10      vincent   339: d_ffotherwindow(int f, int n)
1.1       deraadt   340: {
1.31      deraadt   341:        char             fname[NFILEN];
                    342:        int              s;
                    343:        struct buffer   *bp;
                    344:        struct mgwin    *wp;
1.5       millert   345:
1.15      db        346:        if ((s = d_makename(curwp->w_dotp, fname, sizeof(fname))) == ABORT)
                    347:                return (FALSE);
1.5       millert   348:        if ((bp = (s ? dired_(fname) : findbuffer(fname))) == NULL)
1.15      db        349:                return (FALSE);
1.45      kjell     350:        if ((wp = popbuf(bp, WNONE)) == NULL)
1.15      db        351:                return (FALSE);
1.5       millert   352:        curbp = bp;
                    353:        curwp = wp;
                    354:        if (bp->b_fname[0] != 0)
1.15      db        355:                return (TRUE);  /* never true for dired buffers */
                    356:        return (readin(fname));
1.1       deraadt   357: }
                    358:
1.5       millert   359: /* ARGSUSED */
                    360: int
1.10      vincent   361: d_expunge(int f, int n)
1.1       deraadt   362: {
1.31      deraadt   363:        struct line     *lp, *nlp;
1.48      kjell     364:        char             fname[NFILEN], sname[NFILEN];
1.5       millert   365:
1.41      kjell     366:        for (lp = bfirstlp(curbp); lp != curbp->b_headp; lp = nlp) {
1.5       millert   367:                nlp = lforw(lp);
                    368:                if (llength(lp) && lgetc(lp, 0) == 'D') {
1.15      db        369:                        switch (d_makename(lp, fname, sizeof(fname))) {
1.5       millert   370:                        case ABORT:
                    371:                                ewprintf("Bad line in dired buffer");
1.15      db        372:                                return (FALSE);
1.5       millert   373:                        case FALSE:
                    374:                                if (unlink(fname) < 0) {
1.48      kjell     375:                                        (void)xbasename(sname, fname, NFILEN);
                    376:                                        ewprintf("Could not delete '%s'", sname);
1.15      db        377:                                        return (FALSE);
1.5       millert   378:                                }
                    379:                                break;
                    380:                        case TRUE:
                    381:                                if (rmdir(fname) < 0) {
1.48      kjell     382:                                        (void)xbasename(sname, fname, NFILEN);
                    383:                                        ewprintf("Could not delete directory "
                    384:                                            "'%s'", sname);
1.15      db        385:                                        return (FALSE);
1.5       millert   386:                                }
                    387:                                break;
                    388:                        }
                    389:                        lfree(lp);
1.39      kjell     390:                        curwp->w_bufp->b_lines--;
1.44      kjell     391:                        curwp->w_rflag |= WFFULL;
1.5       millert   392:                }
1.1       deraadt   393:        }
1.15      db        394:        return (TRUE);
1.1       deraadt   395: }
                    396:
1.5       millert   397: /* ARGSUSED */
                    398: int
1.10      vincent   399: d_copy(int f, int n)
1.1       deraadt   400: {
1.48      kjell     401:        char    frname[NFILEN], toname[NFILEN], sname[NFILEN], *bufp;
1.33      kjell     402:        int     ret;
1.18      cloder    403:        size_t  off;
1.31      deraadt   404:        struct buffer *bp;
1.1       deraadt   405:
1.15      db        406:        if (d_makename(curwp->w_dotp, frname, sizeof(frname)) != FALSE) {
1.5       millert   407:                ewprintf("Not a file");
1.15      db        408:                return (FALSE);
1.5       millert   409:        }
1.15      db        410:        off = strlcpy(toname, curbp->b_fname, sizeof(toname));
                    411:        if (off >= sizeof(toname) - 1) {        /* can't happen, really */
1.18      cloder    412:                ewprintf("Directory name too long");
1.12      vincent   413:                return (FALSE);
                    414:        }
1.48      kjell     415:        (void)xbasename(sname, frname, NFILEN);
                    416:        bufp = eread("Copy %s to: ", toname, sizeof(toname),
                    417:            EFDEF | EFNEW | EFCR, sname);
                    418:        if (bufp == NULL)
1.15      db        419:                return (ABORT);
1.14      vincent   420:        else if (bufp[0] == '\0')
1.17      otto      421:                return (FALSE);
1.33      kjell     422:        ret = (copy(frname, toname) >= 0) ? TRUE : FALSE;
                    423:        if (ret != TRUE)
                    424:                return (ret);
1.54      lum       425:        if ((bp = refreshbuffer(curbp)) == NULL)
                    426:                return (FALSE);
1.37      kjell     427:        return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.1       deraadt   428: }
                    429:
1.5       millert   430: /* ARGSUSED */
                    431: int
1.10      vincent   432: d_rename(int f, int n)
1.1       deraadt   433: {
1.31      deraadt   434:        char             frname[NFILEN], toname[NFILEN], *bufp;
1.33      kjell     435:        int              ret;
1.31      deraadt   436:        size_t           off;
                    437:        struct buffer   *bp;
1.48      kjell     438:        char             sname[NFILEN];
1.1       deraadt   439:
1.15      db        440:        if (d_makename(curwp->w_dotp, frname, sizeof(frname)) != FALSE) {
1.5       millert   441:                ewprintf("Not a file");
1.15      db        442:                return (FALSE);
1.5       millert   443:        }
1.15      db        444:        off = strlcpy(toname, curbp->b_fname, sizeof(toname));
                    445:        if (off >= sizeof(toname) - 1) {        /* can't happen, really */
1.18      cloder    446:                ewprintf("Directory name too long");
1.12      vincent   447:                return (FALSE);
                    448:        }
1.48      kjell     449:        (void)xbasename(sname, frname, NFILEN);
                    450:        bufp = eread("Rename %s to: ", toname,
                    451:            sizeof(toname), EFDEF | EFNEW | EFCR, sname);
                    452:        if (bufp == NULL)
1.15      db        453:                return (ABORT);
1.14      vincent   454:        else if (bufp[0] == '\0')
1.15      db        455:                return (FALSE);
1.33      kjell     456:        ret = (rename(frname, toname) >= 0) ? TRUE : FALSE;
                    457:        if (ret != TRUE)
                    458:                return (ret);
1.54      lum       459:        if ((bp = refreshbuffer(curbp)) == NULL)
                    460:                return (FALSE);
1.37      kjell     461:        return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.1       deraadt   462: }
1.12      vincent   463:
1.31      deraadt   464: /* ARGSUSED */
1.12      vincent   465: void
                    466: reaper(int signo __attribute__((unused)))
                    467: {
1.22      deraadt   468:        int     save_errno = errno, status;
1.12      vincent   469:
1.25      deraadt   470:        while (waitpid(-1, &status, WNOHANG) >= 0)
1.12      vincent   471:                ;
1.22      deraadt   472:        errno = save_errno;
1.12      vincent   473: }
                    474:
                    475: /*
                    476:  * Pipe the currently selected file through a shell command.
                    477:  */
1.26      kjell     478: /* ARGSUSED */
1.12      vincent   479: int
                    480: d_shell_command(int f, int n)
                    481: {
1.50      lum       482:        char             command[512], fname[MAXPATHLEN], *bufp;
1.31      deraadt   483:        struct buffer   *bp;
                    484:        struct mgwin    *wp;
1.50      lum       485:        char             sname[NFILEN];
1.12      vincent   486:
                    487:        bp = bfind("*Shell Command Output*", TRUE);
                    488:        if (bclear(bp) != TRUE)
                    489:                return (ABORT);
                    490:
1.15      db        491:        if (d_makename(curwp->w_dotp, fname, sizeof(fname)) != FALSE) {
1.12      vincent   492:                ewprintf("bad line");
                    493:                return (ABORT);
                    494:        }
                    495:
                    496:        command[0] = '\0';
1.48      kjell     497:        (void)xbasename(sname, fname, NFILEN);
                    498:        bufp = eread("! on %s: ", command, sizeof(command), EFNEW, sname);
                    499:        if (bufp == NULL)
1.12      vincent   500:                return (ABORT);
1.50      lum       501:
                    502:        if (d_exec(0, bp, fname, "sh", "-c", command, NULL) != TRUE)
                    503:                return (ABORT);
                    504:
                    505:        if ((wp = popbuf(bp, WNONE)) == NULL)
                    506:                return (ABORT); /* XXX - free the buffer?? */
                    507:        curwp = wp;
                    508:        curbp = wp->w_bufp;
                    509:        return (TRUE);
                    510: }
                    511:
                    512: /*
                    513:  * Pipe input file to cmd and insert the command's output in the
                    514:  * given buffer.  Each line will be prefixed with the given
                    515:  * number of spaces.
                    516:  */
                    517: static int
                    518: d_exec(int space, struct buffer *bp, const char *input, const char *cmd, ...)
                    519: {
                    520:        char     buf[BUFSIZ];
                    521:        va_list  ap;
                    522:        struct   sigaction olda, newa;
                    523:        char    **argv = NULL, *cp;
                    524:        FILE    *fin;
                    525:        int      fds[2] = { -1, -1 };
                    526:        int      infd = -1;
                    527:        int      ret = (ABORT), n;
                    528:        pid_t    pid;
                    529:
                    530:        if (sigaction(SIGCHLD, NULL, &olda) == -1)
                    531:                return (ABORT);
                    532:
                    533:        /* Find the number of arguments. */
                    534:        va_start(ap, cmd);
                    535:        for (n = 2; va_arg(ap, char *) != NULL; n++)
                    536:                ;
                    537:        va_end(ap);
                    538:
                    539:        /* Allocate and build the argv. */
                    540:        if ((argv = calloc(n, sizeof(*argv))) == NULL) {
                    541:                ewprintf("Can't allocate argv : %s", strerror(errno));
                    542:                goto out;
                    543:        }
                    544:
                    545:        n = 1;
                    546:        argv[0] = (char *)cmd;
                    547:        va_start(ap, cmd);
                    548:        while ((argv[n] = va_arg(ap, char *)) != NULL)
                    549:                n++;
                    550:        va_end(ap);
                    551:
                    552:        if (input == NULL)
                    553:                input = "/dev/null";
                    554:
                    555:        if ((infd = open(input, O_RDONLY)) == -1) {
1.12      vincent   556:                ewprintf("Can't open input file : %s", strerror(errno));
1.50      lum       557:                goto out;
1.12      vincent   558:        }
1.50      lum       559:
1.12      vincent   560:        if (pipe(fds) == -1) {
                    561:                ewprintf("Can't create pipe : %s", strerror(errno));
1.50      lum       562:                goto out;
1.12      vincent   563:        }
                    564:
                    565:        newa.sa_handler = reaper;
                    566:        newa.sa_flags = 0;
1.50      lum       567:        if (sigaction(SIGCHLD, &newa, NULL) == -1)
                    568:                goto out;
                    569:
                    570:        if ((pid = fork()) == -1) {
                    571:                ewprintf("Can't fork");
                    572:                goto out;
1.12      vincent   573:        }
1.50      lum       574:
1.12      vincent   575:        switch (pid) {
1.50      lum       576:        case 0: /* Child */
1.12      vincent   577:                close(fds[0]);
                    578:                dup2(infd, STDIN_FILENO);
                    579:                dup2(fds[1], STDOUT_FILENO);
                    580:                dup2(fds[1], STDERR_FILENO);
1.50      lum       581:                if (execvp(argv[0], argv) == -1)
                    582:                        ewprintf("Can't exec %s: %s", argv[0], strerror(errno));
1.12      vincent   583:                exit(1);
1.29      deraadt   584:                break;
1.50      lum       585:        default: /* Parent */
1.12      vincent   586:                close(infd);
                    587:                close(fds[1]);
1.50      lum       588:                infd = fds[1] = -1;
                    589:                if ((fin = fdopen(fds[0], "r")) == NULL)
                    590:                        goto out;
1.15      db        591:                while (fgets(buf, sizeof(buf), fin) != NULL) {
1.12      vincent   592:                        cp = strrchr(buf, '\n');
                    593:                        if (cp == NULL && !feof(fin)) { /* too long a line */
                    594:                                int c;
1.50      lum       595:                                addlinef(bp, "%*s%s...", space, "", buf);
1.12      vincent   596:                                while ((c = getc(fin)) != EOF && c != '\n')
                    597:                                        ;
                    598:                                continue;
                    599:                        } else if (cp)
                    600:                                *cp = '\0';
1.50      lum       601:                        addlinef(bp, "%*s%s", space, "", buf);
1.12      vincent   602:                }
                    603:                fclose(fin);
                    604:                break;
                    605:        }
1.50      lum       606:        ret = (TRUE);
                    607:
                    608: out:
1.12      vincent   609:        if (sigaction(SIGCHLD, &olda, NULL) == -1)
                    610:                ewprintf("Warning, couldn't reset previous signal handler");
1.50      lum       611:        if (fds[0] != -1)
                    612:                close(fds[0]);
                    613:        if (fds[1] != -1)
                    614:                close(fds[1]);
                    615:        if (infd != -1)
                    616:                close(infd);
                    617:        if (argv != NULL)
                    618:                free(argv);
                    619:        return ret;
1.12      vincent   620: }
                    621:
1.26      kjell     622: /* ARGSUSED */
1.12      vincent   623: int
                    624: d_create_directory(int f, int n)
                    625: {
1.57    ! lum       626:        int ret;
1.31      deraadt   627:        struct buffer   *bp;
1.12      vincent   628:
1.57    ! lum       629:        ret = do_makedir();
        !           630:        if (ret != TRUE)
        !           631:                return(ret);
        !           632:
1.54      lum       633:        if ((bp = refreshbuffer(curbp)) == NULL)
                    634:                return (FALSE);
1.57    ! lum       635:
1.37      kjell     636:        return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.54      lum       637: }
                    638:
                    639: struct buffer *
                    640: refreshbuffer(struct buffer *bp)
                    641: {
                    642:        char    *tmp;
                    643:
                    644:        tmp = strdup(bp->b_fname);
1.55      lum       645:        if (tmp == NULL) {
                    646:                ewprintf("Out of memory");
                    647:                return (FALSE);
                    648:        }
1.54      lum       649:
                    650:        killbuffer(bp);
                    651:
                    652:        /* dired_() uses findbuffer() to create new buffer */
                    653:        if ((bp = dired_(tmp)) == NULL) {
                    654:                free(tmp);
                    655:                return (NULL);
                    656:        }
                    657:        free(tmp);
                    658:        curbp = bp;
                    659:
                    660:        return (bp);
1.12      vincent   661: }
1.24      kjell     662:
                    663: static int
1.34      kjell     664: d_makename(struct line *lp, char *fn, size_t len)
1.24      kjell     665: {
1.50      lum       666:        int      start, nlen;
                    667:        char    *namep;
1.24      kjell     668:
1.50      lum       669:        if (d_warpdot(lp, &start) == FALSE)
1.24      kjell     670:                return (ABORT);
1.50      lum       671:        namep = &lp->l_text[start];
                    672:        nlen = llength(lp) - start;
                    673:
                    674:        if (snprintf(fn, len, "%s%.*s", curbp->b_fname, nlen, namep) >= len)
                    675:                return (ABORT); /* Name is too long. */
                    676:
                    677:        /* Return TRUE if the entry is a directory. */
1.24      kjell     678:        return ((lgetc(lp, 2) == 'd') ? TRUE : FALSE);
                    679: }
                    680:
1.50      lum       681: #define NAME_FIELD     9
                    682:
1.49      lum       683: static int
                    684: d_warpdot(struct line *dotp, int *doto)
                    685: {
                    686:        char *tp = dotp->l_text;
                    687:        int off = 0, field = 0, len;
                    688:
                    689:        /*
                    690:         * Find the byte offset to the (space-delimited) filename
                    691:         * field in formatted ls output.
                    692:         */
                    693:        len = llength(dotp);
                    694:        while (off < len) {
                    695:                if (tp[off++] == ' ') {
1.50      lum       696:                        if (++field == NAME_FIELD) {
                    697:                                *doto = off;
                    698:                                return (TRUE);
                    699:                        }
1.49      lum       700:                        /* Skip the space. */
                    701:                        while (off < len && tp[off] == ' ')
                    702:                                off++;
                    703:                }
                    704:        }
1.50      lum       705:        /* We didn't find the field. */
                    706:        *doto = 0;
                    707:        return (FALSE);
1.49      lum       708: }
                    709:
                    710: static int
                    711: d_forwpage(int f, int n)
                    712: {
                    713:        forwpage(f | FFRAND, n);
                    714:        return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
                    715: }
                    716:
                    717: static int
                    718: d_backpage (int f, int n)
                    719: {
                    720:        backpage(f | FFRAND, n);
                    721:        return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
                    722: }
                    723:
                    724: static int
                    725: d_forwline (int f, int n)
                    726: {
                    727:        forwline(f | FFRAND, n);
                    728:        return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
                    729: }
                    730:
                    731: static int
                    732: d_backline (int f, int n)
                    733: {
                    734:        backline(f | FFRAND, n);
                    735:        return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
                    736: }
                    737:
1.24      kjell     738: /*
1.33      kjell     739:  * XXX dname needs to have enough place to store an additional '/'.
1.24      kjell     740:  */
1.31      deraadt   741: struct buffer *
1.33      kjell     742: dired_(char *dname)
1.24      kjell     743: {
1.31      deraadt   744:        struct buffer   *bp;
1.52      haesbaer  745:        int              i;
                    746:        size_t           len;
1.46      kjell     747:
1.52      haesbaer  748:        if ((access(dname, R_OK | X_OK)) == -1) {
1.46      kjell     749:                if (errno == EACCES)
                    750:                        ewprintf("Permission denied");
                    751:                return (NULL);
                    752:        }
1.38      jason     753:        if ((dname = adjustname(dname, FALSE)) == NULL) {
1.24      kjell     754:                ewprintf("Bad directory name");
                    755:                return (NULL);
                    756:        }
                    757:        /* this should not be done, instead adjustname() should get a flag */
1.33      kjell     758:        len = strlen(dname);
                    759:        if (dname[len - 1] != '/') {
                    760:                dname[len++] = '/';
                    761:                dname[len] = '\0';
1.24      kjell     762:        }
1.33      kjell     763:        if ((bp = findbuffer(dname)) == NULL) {
1.24      kjell     764:                ewprintf("Could not create buffer");
                    765:                return (NULL);
                    766:        }
                    767:        if (bclear(bp) != TRUE)
                    768:                return (NULL);
                    769:        bp->b_flag |= BFREADONLY;
1.50      lum       770:
                    771:        if ((d_exec(2, bp, NULL, "ls", "-al", dname, NULL)) != TRUE)
1.24      kjell     772:                return (NULL);
1.49      lum       773:
                    774:        /* Find the line with ".." on it. */
1.41      kjell     775:        bp->b_dotp = bfirstlp(bp);
1.49      lum       776:        for (i = 0; i < bp->b_lines; i++) {
                    777:                bp->b_dotp = lforw(bp->b_dotp);
1.50      lum       778:                if (d_warpdot(bp->b_dotp, &bp->b_doto) == FALSE)
1.49      lum       779:                        continue;
                    780:                if (strcmp(ltext(bp->b_dotp) + bp->b_doto, "..") == 0)
                    781:                        break;
                    782:        }
                    783:
                    784:        /* We want dot on the entry right after "..", if possible. */
                    785:        if (++i < bp->b_lines - 2)
                    786:                bp->b_dotp = lforw(bp->b_dotp);
                    787:        d_warpdot(bp->b_dotp, &bp->b_doto);
                    788:
1.36      kjell     789:        (void)strlcpy(bp->b_fname, dname, sizeof(bp->b_fname));
                    790:        (void)strlcpy(bp->b_cwd, dname, sizeof(bp->b_cwd));
1.24      kjell     791:        if ((bp->b_modes[1] = name_mode("dired")) == NULL) {
                    792:                bp->b_modes[0] = name_mode("fundamental");
                    793:                ewprintf("Could not find mode dired");
                    794:                return (NULL);
                    795:        }
                    796:        bp->b_nmodes = 1;
                    797:        return (bp);
                    798: }