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

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