[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.59

1.59    ! lum         1: /*     $OpenBSD: dired.c,v 1.58 2013/05/30 04:27:18 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.59    ! lum       401:        char             frname[NFILEN], toname[NFILEN], sname[NFILEN];
        !           402:        char            *topath, *bufp;
        !           403:        int              ret;
        !           404:        size_t           off;
        !           405:        struct buffer   *bp;
1.1       deraadt   406:
1.15      db        407:        if (d_makename(curwp->w_dotp, frname, sizeof(frname)) != FALSE) {
1.5       millert   408:                ewprintf("Not a file");
1.15      db        409:                return (FALSE);
1.5       millert   410:        }
1.15      db        411:        off = strlcpy(toname, curbp->b_fname, sizeof(toname));
                    412:        if (off >= sizeof(toname) - 1) {        /* can't happen, really */
1.18      cloder    413:                ewprintf("Directory name too long");
1.12      vincent   414:                return (FALSE);
                    415:        }
1.48      kjell     416:        (void)xbasename(sname, frname, NFILEN);
                    417:        bufp = eread("Copy %s to: ", toname, sizeof(toname),
                    418:            EFDEF | EFNEW | EFCR, sname);
                    419:        if (bufp == NULL)
1.15      db        420:                return (ABORT);
1.14      vincent   421:        else if (bufp[0] == '\0')
1.17      otto      422:                return (FALSE);
1.59    ! lum       423:
        !           424:        topath = adjustname(toname, TRUE);
        !           425:        ret = (copy(frname, topath) >= 0) ? TRUE : FALSE;
1.33      kjell     426:        if (ret != TRUE)
                    427:                return (ret);
1.54      lum       428:        if ((bp = refreshbuffer(curbp)) == NULL)
                    429:                return (FALSE);
1.37      kjell     430:        return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.1       deraadt   431: }
                    432:
1.5       millert   433: /* ARGSUSED */
                    434: int
1.10      vincent   435: d_rename(int f, int n)
1.1       deraadt   436: {
1.59    ! lum       437:        char             frname[NFILEN], toname[NFILEN];
        !           438:        char            *topath, *bufp;
1.33      kjell     439:        int              ret;
1.31      deraadt   440:        size_t           off;
                    441:        struct buffer   *bp;
1.48      kjell     442:        char             sname[NFILEN];
1.1       deraadt   443:
1.15      db        444:        if (d_makename(curwp->w_dotp, frname, sizeof(frname)) != FALSE) {
1.5       millert   445:                ewprintf("Not a file");
1.15      db        446:                return (FALSE);
1.5       millert   447:        }
1.15      db        448:        off = strlcpy(toname, curbp->b_fname, sizeof(toname));
                    449:        if (off >= sizeof(toname) - 1) {        /* can't happen, really */
1.18      cloder    450:                ewprintf("Directory name too long");
1.12      vincent   451:                return (FALSE);
                    452:        }
1.48      kjell     453:        (void)xbasename(sname, frname, NFILEN);
                    454:        bufp = eread("Rename %s to: ", toname,
                    455:            sizeof(toname), EFDEF | EFNEW | EFCR, sname);
                    456:        if (bufp == NULL)
1.15      db        457:                return (ABORT);
1.14      vincent   458:        else if (bufp[0] == '\0')
1.15      db        459:                return (FALSE);
1.59    ! lum       460:
        !           461:        topath = adjustname(toname, TRUE);
        !           462:        ret = (rename(frname, topath) >= 0) ? TRUE : FALSE;
1.33      kjell     463:        if (ret != TRUE)
                    464:                return (ret);
1.54      lum       465:        if ((bp = refreshbuffer(curbp)) == NULL)
                    466:                return (FALSE);
1.37      kjell     467:        return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.1       deraadt   468: }
1.12      vincent   469:
1.31      deraadt   470: /* ARGSUSED */
1.12      vincent   471: void
                    472: reaper(int signo __attribute__((unused)))
                    473: {
1.22      deraadt   474:        int     save_errno = errno, status;
1.12      vincent   475:
1.25      deraadt   476:        while (waitpid(-1, &status, WNOHANG) >= 0)
1.12      vincent   477:                ;
1.22      deraadt   478:        errno = save_errno;
1.12      vincent   479: }
                    480:
                    481: /*
                    482:  * Pipe the currently selected file through a shell command.
                    483:  */
1.26      kjell     484: /* ARGSUSED */
1.12      vincent   485: int
                    486: d_shell_command(int f, int n)
                    487: {
1.50      lum       488:        char             command[512], fname[MAXPATHLEN], *bufp;
1.31      deraadt   489:        struct buffer   *bp;
                    490:        struct mgwin    *wp;
1.50      lum       491:        char             sname[NFILEN];
1.12      vincent   492:
                    493:        bp = bfind("*Shell Command Output*", TRUE);
                    494:        if (bclear(bp) != TRUE)
                    495:                return (ABORT);
                    496:
1.15      db        497:        if (d_makename(curwp->w_dotp, fname, sizeof(fname)) != FALSE) {
1.12      vincent   498:                ewprintf("bad line");
                    499:                return (ABORT);
                    500:        }
                    501:
                    502:        command[0] = '\0';
1.48      kjell     503:        (void)xbasename(sname, fname, NFILEN);
                    504:        bufp = eread("! on %s: ", command, sizeof(command), EFNEW, sname);
                    505:        if (bufp == NULL)
1.12      vincent   506:                return (ABORT);
1.50      lum       507:
                    508:        if (d_exec(0, bp, fname, "sh", "-c", command, NULL) != TRUE)
                    509:                return (ABORT);
                    510:
                    511:        if ((wp = popbuf(bp, WNONE)) == NULL)
                    512:                return (ABORT); /* XXX - free the buffer?? */
                    513:        curwp = wp;
                    514:        curbp = wp->w_bufp;
                    515:        return (TRUE);
                    516: }
                    517:
                    518: /*
                    519:  * Pipe input file to cmd and insert the command's output in the
                    520:  * given buffer.  Each line will be prefixed with the given
                    521:  * number of spaces.
                    522:  */
                    523: static int
                    524: d_exec(int space, struct buffer *bp, const char *input, const char *cmd, ...)
                    525: {
                    526:        char     buf[BUFSIZ];
                    527:        va_list  ap;
                    528:        struct   sigaction olda, newa;
                    529:        char    **argv = NULL, *cp;
                    530:        FILE    *fin;
                    531:        int      fds[2] = { -1, -1 };
                    532:        int      infd = -1;
                    533:        int      ret = (ABORT), n;
                    534:        pid_t    pid;
                    535:
                    536:        if (sigaction(SIGCHLD, NULL, &olda) == -1)
                    537:                return (ABORT);
                    538:
                    539:        /* Find the number of arguments. */
                    540:        va_start(ap, cmd);
                    541:        for (n = 2; va_arg(ap, char *) != NULL; n++)
                    542:                ;
                    543:        va_end(ap);
                    544:
                    545:        /* Allocate and build the argv. */
                    546:        if ((argv = calloc(n, sizeof(*argv))) == NULL) {
                    547:                ewprintf("Can't allocate argv : %s", strerror(errno));
                    548:                goto out;
                    549:        }
                    550:
                    551:        n = 1;
                    552:        argv[0] = (char *)cmd;
                    553:        va_start(ap, cmd);
                    554:        while ((argv[n] = va_arg(ap, char *)) != NULL)
                    555:                n++;
                    556:        va_end(ap);
                    557:
                    558:        if (input == NULL)
                    559:                input = "/dev/null";
                    560:
                    561:        if ((infd = open(input, O_RDONLY)) == -1) {
1.12      vincent   562:                ewprintf("Can't open input file : %s", strerror(errno));
1.50      lum       563:                goto out;
1.12      vincent   564:        }
1.50      lum       565:
1.12      vincent   566:        if (pipe(fds) == -1) {
                    567:                ewprintf("Can't create pipe : %s", strerror(errno));
1.50      lum       568:                goto out;
1.12      vincent   569:        }
                    570:
                    571:        newa.sa_handler = reaper;
                    572:        newa.sa_flags = 0;
1.50      lum       573:        if (sigaction(SIGCHLD, &newa, NULL) == -1)
                    574:                goto out;
                    575:
                    576:        if ((pid = fork()) == -1) {
                    577:                ewprintf("Can't fork");
                    578:                goto out;
1.12      vincent   579:        }
1.50      lum       580:
1.12      vincent   581:        switch (pid) {
1.50      lum       582:        case 0: /* Child */
1.12      vincent   583:                close(fds[0]);
                    584:                dup2(infd, STDIN_FILENO);
                    585:                dup2(fds[1], STDOUT_FILENO);
                    586:                dup2(fds[1], STDERR_FILENO);
1.50      lum       587:                if (execvp(argv[0], argv) == -1)
                    588:                        ewprintf("Can't exec %s: %s", argv[0], strerror(errno));
1.12      vincent   589:                exit(1);
1.29      deraadt   590:                break;
1.50      lum       591:        default: /* Parent */
1.12      vincent   592:                close(infd);
                    593:                close(fds[1]);
1.50      lum       594:                infd = fds[1] = -1;
                    595:                if ((fin = fdopen(fds[0], "r")) == NULL)
                    596:                        goto out;
1.15      db        597:                while (fgets(buf, sizeof(buf), fin) != NULL) {
1.12      vincent   598:                        cp = strrchr(buf, '\n');
                    599:                        if (cp == NULL && !feof(fin)) { /* too long a line */
                    600:                                int c;
1.50      lum       601:                                addlinef(bp, "%*s%s...", space, "", buf);
1.12      vincent   602:                                while ((c = getc(fin)) != EOF && c != '\n')
                    603:                                        ;
                    604:                                continue;
                    605:                        } else if (cp)
                    606:                                *cp = '\0';
1.50      lum       607:                        addlinef(bp, "%*s%s", space, "", buf);
1.12      vincent   608:                }
                    609:                fclose(fin);
                    610:                break;
                    611:        }
1.50      lum       612:        ret = (TRUE);
                    613:
                    614: out:
1.12      vincent   615:        if (sigaction(SIGCHLD, &olda, NULL) == -1)
                    616:                ewprintf("Warning, couldn't reset previous signal handler");
1.50      lum       617:        if (fds[0] != -1)
                    618:                close(fds[0]);
                    619:        if (fds[1] != -1)
                    620:                close(fds[1]);
                    621:        if (infd != -1)
                    622:                close(infd);
                    623:        if (argv != NULL)
                    624:                free(argv);
                    625:        return ret;
1.12      vincent   626: }
                    627:
1.26      kjell     628: /* ARGSUSED */
1.12      vincent   629: int
                    630: d_create_directory(int f, int n)
                    631: {
1.57      lum       632:        int ret;
1.31      deraadt   633:        struct buffer   *bp;
1.12      vincent   634:
1.57      lum       635:        ret = do_makedir();
                    636:        if (ret != TRUE)
                    637:                return(ret);
                    638:
1.54      lum       639:        if ((bp = refreshbuffer(curbp)) == NULL)
                    640:                return (FALSE);
1.57      lum       641:
1.37      kjell     642:        return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.54      lum       643: }
                    644:
                    645: struct buffer *
                    646: refreshbuffer(struct buffer *bp)
                    647: {
                    648:        char    *tmp;
                    649:
                    650:        tmp = strdup(bp->b_fname);
1.55      lum       651:        if (tmp == NULL) {
                    652:                ewprintf("Out of memory");
                    653:                return (FALSE);
                    654:        }
1.54      lum       655:
                    656:        killbuffer(bp);
                    657:
                    658:        /* dired_() uses findbuffer() to create new buffer */
                    659:        if ((bp = dired_(tmp)) == NULL) {
                    660:                free(tmp);
                    661:                return (NULL);
                    662:        }
                    663:        free(tmp);
                    664:        curbp = bp;
                    665:
                    666:        return (bp);
1.12      vincent   667: }
1.24      kjell     668:
                    669: static int
1.34      kjell     670: d_makename(struct line *lp, char *fn, size_t len)
1.24      kjell     671: {
1.50      lum       672:        int      start, nlen;
                    673:        char    *namep;
1.24      kjell     674:
1.50      lum       675:        if (d_warpdot(lp, &start) == FALSE)
1.24      kjell     676:                return (ABORT);
1.50      lum       677:        namep = &lp->l_text[start];
                    678:        nlen = llength(lp) - start;
                    679:
                    680:        if (snprintf(fn, len, "%s%.*s", curbp->b_fname, nlen, namep) >= len)
                    681:                return (ABORT); /* Name is too long. */
                    682:
                    683:        /* Return TRUE if the entry is a directory. */
1.24      kjell     684:        return ((lgetc(lp, 2) == 'd') ? TRUE : FALSE);
                    685: }
                    686:
1.50      lum       687: #define NAME_FIELD     9
                    688:
1.49      lum       689: static int
                    690: d_warpdot(struct line *dotp, int *doto)
                    691: {
                    692:        char *tp = dotp->l_text;
                    693:        int off = 0, field = 0, len;
                    694:
                    695:        /*
                    696:         * Find the byte offset to the (space-delimited) filename
                    697:         * field in formatted ls output.
                    698:         */
                    699:        len = llength(dotp);
                    700:        while (off < len) {
                    701:                if (tp[off++] == ' ') {
1.50      lum       702:                        if (++field == NAME_FIELD) {
                    703:                                *doto = off;
                    704:                                return (TRUE);
                    705:                        }
1.49      lum       706:                        /* Skip the space. */
                    707:                        while (off < len && tp[off] == ' ')
                    708:                                off++;
                    709:                }
                    710:        }
1.50      lum       711:        /* We didn't find the field. */
                    712:        *doto = 0;
                    713:        return (FALSE);
1.49      lum       714: }
                    715:
                    716: static int
                    717: d_forwpage(int f, int n)
                    718: {
                    719:        forwpage(f | FFRAND, n);
                    720:        return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
                    721: }
                    722:
                    723: static int
                    724: d_backpage (int f, int n)
                    725: {
                    726:        backpage(f | FFRAND, n);
                    727:        return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
                    728: }
                    729:
                    730: static int
                    731: d_forwline (int f, int n)
                    732: {
                    733:        forwline(f | FFRAND, n);
                    734:        return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
                    735: }
                    736:
                    737: static int
                    738: d_backline (int f, int n)
                    739: {
                    740:        backline(f | FFRAND, n);
                    741:        return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
                    742: }
                    743:
1.24      kjell     744: /*
1.33      kjell     745:  * XXX dname needs to have enough place to store an additional '/'.
1.24      kjell     746:  */
1.31      deraadt   747: struct buffer *
1.33      kjell     748: dired_(char *dname)
1.24      kjell     749: {
1.31      deraadt   750:        struct buffer   *bp;
1.52      haesbaer  751:        int              i;
                    752:        size_t           len;
1.46      kjell     753:
1.38      jason     754:        if ((dname = adjustname(dname, FALSE)) == NULL) {
1.24      kjell     755:                ewprintf("Bad directory name");
                    756:                return (NULL);
                    757:        }
                    758:        /* this should not be done, instead adjustname() should get a flag */
1.33      kjell     759:        len = strlen(dname);
                    760:        if (dname[len - 1] != '/') {
                    761:                dname[len++] = '/';
                    762:                dname[len] = '\0';
1.58      lum       763:        }
                    764:        if ((access(dname, R_OK | X_OK)) == -1) {
                    765:                if (errno == EACCES)
                    766:                        ewprintf("Permission denied");
                    767:                return (NULL);
1.24      kjell     768:        }
1.33      kjell     769:        if ((bp = findbuffer(dname)) == NULL) {
1.24      kjell     770:                ewprintf("Could not create buffer");
                    771:                return (NULL);
                    772:        }
                    773:        if (bclear(bp) != TRUE)
                    774:                return (NULL);
                    775:        bp->b_flag |= BFREADONLY;
1.50      lum       776:
                    777:        if ((d_exec(2, bp, NULL, "ls", "-al", dname, NULL)) != TRUE)
1.24      kjell     778:                return (NULL);
1.49      lum       779:
                    780:        /* Find the line with ".." on it. */
1.41      kjell     781:        bp->b_dotp = bfirstlp(bp);
1.49      lum       782:        for (i = 0; i < bp->b_lines; i++) {
                    783:                bp->b_dotp = lforw(bp->b_dotp);
1.50      lum       784:                if (d_warpdot(bp->b_dotp, &bp->b_doto) == FALSE)
1.49      lum       785:                        continue;
                    786:                if (strcmp(ltext(bp->b_dotp) + bp->b_doto, "..") == 0)
                    787:                        break;
                    788:        }
                    789:
                    790:        /* We want dot on the entry right after "..", if possible. */
                    791:        if (++i < bp->b_lines - 2)
                    792:                bp->b_dotp = lforw(bp->b_dotp);
                    793:        d_warpdot(bp->b_dotp, &bp->b_doto);
                    794:
1.36      kjell     795:        (void)strlcpy(bp->b_fname, dname, sizeof(bp->b_fname));
                    796:        (void)strlcpy(bp->b_cwd, dname, sizeof(bp->b_cwd));
1.24      kjell     797:        if ((bp->b_modes[1] = name_mode("dired")) == NULL) {
                    798:                bp->b_modes[0] = name_mode("fundamental");
                    799:                ewprintf("Could not find mode dired");
                    800:                return (NULL);
                    801:        }
                    802:        bp->b_nmodes = 1;
                    803:        return (bp);
                    804: }