Annotation of src/usr.bin/mg/dired.c, Revision 1.91
1.91 ! lum 1: /* $OpenBSD: dired.c,v 1.90 2019/06/28 13:35:02 deraadt 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>
19: #include <limits.h>
20: #include <signal.h>
1.50 lum 21: #include <stdarg.h>
1.70 bcallah 22: #include <stdio.h>
23: #include <stdlib.h>
24: #include <string.h>
25: #include <unistd.h>
26:
27: #include "def.h"
28: #include "funmap.h"
29: #include "kbd.h"
1.1 deraadt 30:
1.24 kjell 31: void dired_init(void);
1.27 kjell 32: static int dired(int, int);
33: static int d_otherwindow(int, int);
34: static int d_undel(int, int);
35: static int d_undelbak(int, int);
36: static int d_findfile(int, int);
37: static int d_ffotherwindow(int, int);
38: static int d_expunge(int, int);
39: static int d_copy(int, int);
40: static int d_del(int, int);
41: static int d_rename(int, int);
1.50 lum 42: static int d_exec(int, struct buffer *, const char *, const char *, ...);
1.27 kjell 43: static int d_shell_command(int, int);
44: static int d_create_directory(int, int);
1.34 kjell 45: static int d_makename(struct line *, char *, size_t);
1.49 lum 46: static int d_warpdot(struct line *, int *);
47: static int d_forwpage(int, int);
48: static int d_backpage(int, int);
49: static int d_forwline(int, int);
50: static int d_backline(int, int);
1.62 lum 51: static int d_killbuffer_cmd(int, int);
1.63 lum 52: static int d_refreshbuffer(int, int);
1.82 lum 53: static int d_filevisitalt(int, int);
1.91 ! lum 54: static int d_gotofile(int, int);
1.47 lum 55: static void reaper(int);
1.54 lum 56: static struct buffer *refreshbuffer(struct buffer *);
1.78 lum 57: static int createlist(struct buffer *);
58: static void redelete(struct buffer *);
59: static char *findfname(struct line *, char *);
1.1 deraadt 60:
1.23 kjell 61: extern struct keymap_s helpmap, cXmap, metamap;
62:
1.78 lum 63: const char DDELCHAR = 'D';
64:
65: /*
66: * Structure which holds a linked list of file names marked for
67: * deletion. Used to maintain dired buffer 'state' between refreshes.
68: */
69: struct delentry {
70: SLIST_ENTRY(delentry) entry;
71: char *fn;
72: };
73: SLIST_HEAD(slisthead, delentry) delhead = SLIST_HEAD_INITIALIZER(delhead);
74:
1.23 kjell 75: static PF dirednul[] = {
76: setmark, /* ^@ */
77: gotobol, /* ^A */
78: backchar, /* ^B */
79: rescan, /* ^C */
80: d_del, /* ^D */
81: gotoeol, /* ^E */
82: forwchar, /* ^F */
83: ctrlg, /* ^G */
84: NULL, /* ^H */
1.12 vincent 85: };
86:
1.23 kjell 87: static PF diredcl[] = {
88: reposition, /* ^L */
1.15 db 89: d_findfile, /* ^M */
1.49 lum 90: d_forwline, /* ^N */
1.23 kjell 91: rescan, /* ^O */
1.49 lum 92: d_backline, /* ^P */
1.23 kjell 93: rescan, /* ^Q */
94: backisearch, /* ^R */
95: forwisearch, /* ^S */
96: rescan, /* ^T */
97: universal_argument, /* ^U */
1.49 lum 98: d_forwpage, /* ^V */
1.23 kjell 99: rescan, /* ^W */
100: NULL /* ^X */
101: };
102:
103: static PF diredcz[] = {
104: spawncli, /* ^Z */
105: NULL, /* esc */
106: rescan, /* ^\ */
107: rescan, /* ^] */
108: rescan, /* ^^ */
109: rescan, /* ^_ */
1.49 lum 110: d_forwline, /* SP */
1.27 kjell 111: d_shell_command, /* ! */
112: rescan, /* " */
113: rescan, /* # */
114: rescan, /* $ */
115: rescan, /* % */
116: rescan, /* & */
117: rescan, /* ' */
118: rescan, /* ( */
119: rescan, /* ) */
120: rescan, /* * */
121: d_create_directory /* + */
1.23 kjell 122: };
123:
1.82 lum 124: static PF direda[] = {
125: d_filevisitalt, /* a */
126: rescan, /* b */
1.23 kjell 127: d_copy, /* c */
128: d_del, /* d */
129: d_findfile, /* e */
1.63 lum 130: d_findfile, /* f */
1.91 ! lum 131: d_refreshbuffer, /* g */
! 132: rescan, /* h */
! 133: rescan, /* i */
! 134: d_gotofile /* j */
1.23 kjell 135: };
136:
137: static PF diredn[] = {
1.49 lum 138: d_forwline, /* n */
1.23 kjell 139: d_ffotherwindow, /* o */
1.49 lum 140: d_backline, /* p */
1.62 lum 141: d_killbuffer_cmd, /* q */
1.23 kjell 142: d_rename, /* r */
143: rescan, /* s */
144: rescan, /* t */
145: d_undel, /* u */
146: rescan, /* v */
147: rescan, /* w */
148: d_expunge /* x */
149: };
150:
151: static PF direddl[] = {
152: d_undelbak /* del */
1.9 vincent 153: };
1.13 deraadt 154:
1.49 lum 155: static PF diredbp[] = {
1.76 jasper 156: d_backpage /* v */
1.49 lum 157: };
158:
159: static PF dirednull[] = {
160: NULL
161: };
162:
1.71 bcallah 163: static struct KEYMAPE (1) d_backpagemap = {
164: 1,
1.49 lum 165: 1,
1.76 jasper 166: rescan,
1.49 lum 167: {
168: {
169: 'v', 'v', diredbp, NULL
170: }
171: }
172: };
173:
1.71 bcallah 174: static struct KEYMAPE (7) diredmap = {
175: 7,
176: 7,
1.9 vincent 177: rescan,
178: {
1.23 kjell 179: {
180: CCHR('@'), CCHR('H'), dirednul, (KEYMAP *) & helpmap
181: },
182: {
183: CCHR('L'), CCHR('X'), diredcl, (KEYMAP *) & cXmap
184: },
185: {
1.76 jasper 186: CCHR('['), CCHR('['), dirednull, (KEYMAP *) &
1.49 lum 187: d_backpagemap
188: },
189: {
1.27 kjell 190: CCHR('Z'), '+', diredcz, (KEYMAP *) & metamap
1.23 kjell 191: },
192: {
1.91 ! lum 193: 'a', 'j', direda, NULL
1.23 kjell 194: },
195: {
196: 'n', 'x', diredn, NULL
197: },
198: {
199: CCHR('?'), CCHR('?'), direddl, NULL
200: },
1.9 vincent 201: }
202: };
203:
1.23 kjell 204: void
205: dired_init(void)
206: {
1.27 kjell 207: funmap_add(dired, "dired");
1.53 lum 208: funmap_add(d_create_directory, "dired-create-directory");
1.60 lum 209: funmap_add(d_copy, "dired-do-copy");
210: funmap_add(d_expunge, "dired-do-flagged-delete");
1.85 lum 211: funmap_add(d_rename, "dired-do-rename");
1.27 kjell 212: funmap_add(d_findfile, "dired-find-file");
213: funmap_add(d_ffotherwindow, "dired-find-file-other-window");
1.60 lum 214: funmap_add(d_del, "dired-flag-file-deletion");
1.91 ! lum 215: funmap_add(d_gotofile, "dired-goto-file");
1.49 lum 216: funmap_add(d_forwline, "dired-next-line");
1.27 kjell 217: funmap_add(d_otherwindow, "dired-other-window");
1.49 lum 218: funmap_add(d_backline, "dired-previous-line");
1.85 lum 219: funmap_add(d_refreshbuffer, "dired-revert");
1.49 lum 220: funmap_add(d_backpage, "dired-scroll-down");
221: funmap_add(d_forwpage, "dired-scroll-up");
1.60 lum 222: funmap_add(d_undel, "dired-unmark");
1.85 lum 223: funmap_add(d_undelbak, "dired-unmark-backward");
1.62 lum 224: funmap_add(d_killbuffer_cmd, "quit-window");
1.23 kjell 225: maps_add((KEYMAP *)&diredmap, "dired");
1.27 kjell 226: dobindkey(fundamental_map, "dired", "^Xd");
1.23 kjell 227: }
1.12 vincent 228:
1.5 millert 229: /* ARGSUSED */
230: int
1.10 vincent 231: dired(int f, int n)
1.1 deraadt 232: {
1.33 kjell 233: char dname[NFILEN], *bufp, *slash;
1.31 deraadt 234: struct buffer *bp;
1.1 deraadt 235:
1.69 bcallah 236: if (curbp->b_fname[0] != '\0') {
1.33 kjell 237: (void)strlcpy(dname, curbp->b_fname, sizeof(dname));
238: if ((slash = strrchr(dname, '/')) != NULL) {
1.19 cloder 239: *(slash + 1) = '\0';
240: }
241: } else {
1.33 kjell 242: if (getcwd(dname, sizeof(dname)) == NULL)
243: dname[0] = '\0';
1.19 cloder 244: }
245:
1.33 kjell 246: if ((bufp = eread("Dired: ", dname, NFILEN,
1.21 kjell 247: EFDEF | EFNEW | EFCR)) == NULL)
1.15 db 248: return (ABORT);
1.21 kjell 249: if (bufp[0] == '\0')
250: return (FALSE);
1.14 vincent 251: if ((bp = dired_(bufp)) == NULL)
1.15 db 252: return (FALSE);
1.23 kjell 253:
1.5 millert 254: curbp = bp;
1.37 kjell 255: return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.1 deraadt 256: }
257:
1.5 millert 258: /* ARGSUSED */
259: int
1.10 vincent 260: d_otherwindow(int f, int n)
1.1 deraadt 261: {
1.33 kjell 262: char dname[NFILEN], *bufp, *slash;
1.31 deraadt 263: struct buffer *bp;
264: struct mgwin *wp;
1.5 millert 265:
1.69 bcallah 266: if (curbp->b_fname[0] != '\0') {
1.33 kjell 267: (void)strlcpy(dname, curbp->b_fname, sizeof(dname));
268: if ((slash = strrchr(dname, '/')) != NULL) {
1.23 kjell 269: *(slash + 1) = '\0';
270: }
271: } else {
1.33 kjell 272: if (getcwd(dname, sizeof(dname)) == NULL)
273: dname[0] = '\0';
1.23 kjell 274: }
275:
1.33 kjell 276: if ((bufp = eread("Dired other window: ", dname, NFILEN,
1.21 kjell 277: EFDEF | EFNEW | EFCR)) == NULL)
1.15 db 278: return (ABORT);
1.21 kjell 279: else if (bufp[0] == '\0')
280: return (FALSE);
1.14 vincent 281: if ((bp = dired_(bufp)) == NULL)
1.15 db 282: return (FALSE);
1.45 kjell 283: if ((wp = popbuf(bp, WNONE)) == NULL)
1.15 db 284: return (FALSE);
1.5 millert 285: curbp = bp;
286: curwp = wp;
1.15 db 287: return (TRUE);
1.1 deraadt 288: }
289:
1.5 millert 290: /* ARGSUSED */
291: int
1.10 vincent 292: d_del(int f, int n)
1.1 deraadt 293: {
1.5 millert 294: if (n < 0)
1.15 db 295: return (FALSE);
1.5 millert 296: while (n--) {
1.79 lum 297: if (d_warpdot(curwp->w_dotp, &curwp->w_doto) == TRUE) {
1.78 lum 298: lputc(curwp->w_dotp, 0, DDELCHAR);
299: curbp->b_flag |= BFDIREDDEL;
300: }
1.74 lum 301: if (lforw(curwp->w_dotp) != curbp->b_headp) {
1.5 millert 302: curwp->w_dotp = lforw(curwp->w_dotp);
1.74 lum 303: curwp->w_dotline++;
304: }
1.5 millert 305: }
1.44 kjell 306: curwp->w_rflag |= WFEDIT | WFMOVE;
1.56 lum 307: return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
1.1 deraadt 308: }
309:
1.5 millert 310: /* ARGSUSED */
311: int
1.10 vincent 312: d_undel(int f, int n)
1.1 deraadt 313: {
1.5 millert 314: if (n < 0)
1.15 db 315: return (d_undelbak(f, -n));
1.5 millert 316: while (n--) {
317: if (llength(curwp->w_dotp) > 0)
318: lputc(curwp->w_dotp, 0, ' ');
1.74 lum 319: if (lforw(curwp->w_dotp) != curbp->b_headp) {
1.5 millert 320: curwp->w_dotp = lforw(curwp->w_dotp);
1.74 lum 321: curwp->w_dotline++;
322: }
1.5 millert 323: }
1.44 kjell 324: curwp->w_rflag |= WFEDIT | WFMOVE;
1.56 lum 325: return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
1.1 deraadt 326: }
327:
1.5 millert 328: /* ARGSUSED */
329: int
1.10 vincent 330: d_undelbak(int f, int n)
1.1 deraadt 331: {
1.5 millert 332: if (n < 0)
1.15 db 333: return (d_undel(f, -n));
1.5 millert 334: while (n--) {
1.74 lum 335: if (lback(curwp->w_dotp) != curbp->b_headp) {
1.64 lum 336: curwp->w_dotp = lback(curwp->w_dotp);
1.74 lum 337: curwp->w_dotline--;
338: }
1.5 millert 339: if (llength(curwp->w_dotp) > 0)
340: lputc(curwp->w_dotp, 0, ' ');
341: }
1.44 kjell 342: curwp->w_rflag |= WFEDIT | WFMOVE;
1.56 lum 343: return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
1.1 deraadt 344: }
345:
1.5 millert 346: /* ARGSUSED */
347: int
1.10 vincent 348: d_findfile(int f, int n)
1.1 deraadt 349: {
1.31 deraadt 350: struct buffer *bp;
351: int s;
352: char fname[NFILEN];
1.5 millert 353:
1.15 db 354: if ((s = d_makename(curwp->w_dotp, fname, sizeof(fname))) == ABORT)
355: return (FALSE);
1.12 vincent 356: if (s == TRUE)
357: bp = dired_(fname);
358: else
359: bp = findbuffer(fname);
360: if (bp == NULL)
1.15 db 361: return (FALSE);
1.5 millert 362: curbp = bp;
1.37 kjell 363: if (showbuffer(bp, curwp, WFFULL) != TRUE)
1.15 db 364: return (FALSE);
1.5 millert 365: if (bp->b_fname[0] != 0)
1.15 db 366: return (TRUE);
367: return (readin(fname));
1.1 deraadt 368: }
369:
1.5 millert 370: /* ARGSUSED */
371: int
1.10 vincent 372: d_ffotherwindow(int f, int n)
1.1 deraadt 373: {
1.31 deraadt 374: char fname[NFILEN];
375: int s;
376: struct buffer *bp;
377: struct mgwin *wp;
1.5 millert 378:
1.15 db 379: if ((s = d_makename(curwp->w_dotp, fname, sizeof(fname))) == ABORT)
380: return (FALSE);
1.5 millert 381: if ((bp = (s ? dired_(fname) : findbuffer(fname))) == NULL)
1.15 db 382: return (FALSE);
1.45 kjell 383: if ((wp = popbuf(bp, WNONE)) == NULL)
1.15 db 384: return (FALSE);
1.5 millert 385: curbp = bp;
386: curwp = wp;
387: if (bp->b_fname[0] != 0)
1.15 db 388: return (TRUE); /* never true for dired buffers */
389: return (readin(fname));
1.1 deraadt 390: }
391:
1.5 millert 392: /* ARGSUSED */
393: int
1.10 vincent 394: d_expunge(int f, int n)
1.1 deraadt 395: {
1.31 deraadt 396: struct line *lp, *nlp;
1.48 kjell 397: char fname[NFILEN], sname[NFILEN];
1.74 lum 398: int tmp;
399:
400: tmp = curwp->w_dotline;
401: curwp->w_dotline = 0;
1.5 millert 402:
1.41 kjell 403: for (lp = bfirstlp(curbp); lp != curbp->b_headp; lp = nlp) {
1.74 lum 404: curwp->w_dotline++;
1.5 millert 405: nlp = lforw(lp);
406: if (llength(lp) && lgetc(lp, 0) == 'D') {
1.15 db 407: switch (d_makename(lp, fname, sizeof(fname))) {
1.5 millert 408: case ABORT:
1.66 lum 409: dobeep();
1.5 millert 410: ewprintf("Bad line in dired buffer");
1.74 lum 411: curwp->w_dotline = tmp;
1.15 db 412: return (FALSE);
1.5 millert 413: case FALSE:
1.90 deraadt 414: if (unlink(fname) == -1) {
1.48 kjell 415: (void)xbasename(sname, fname, NFILEN);
1.66 lum 416: dobeep();
1.48 kjell 417: ewprintf("Could not delete '%s'", sname);
1.74 lum 418: curwp->w_dotline = tmp;
1.15 db 419: return (FALSE);
1.5 millert 420: }
421: break;
422: case TRUE:
1.90 deraadt 423: if (rmdir(fname) == -1) {
1.48 kjell 424: (void)xbasename(sname, fname, NFILEN);
1.66 lum 425: dobeep();
1.48 kjell 426: ewprintf("Could not delete directory "
427: "'%s'", sname);
1.74 lum 428: curwp->w_dotline = tmp;
1.15 db 429: return (FALSE);
1.5 millert 430: }
431: break;
432: }
433: lfree(lp);
1.39 kjell 434: curwp->w_bufp->b_lines--;
1.74 lum 435: if (tmp > curwp->w_dotline)
436: tmp--;
1.44 kjell 437: curwp->w_rflag |= WFFULL;
1.5 millert 438: }
1.1 deraadt 439: }
1.74 lum 440: curwp->w_dotline = tmp;
1.75 lum 441: d_warpdot(curwp->w_dotp, &curwp->w_doto);
1.78 lum 442:
443: /* we have deleted all items successfully, remove del flag */
444: curbp->b_flag &= ~BFDIREDDEL;
445:
1.15 db 446: return (TRUE);
1.1 deraadt 447: }
448:
1.5 millert 449: /* ARGSUSED */
450: int
1.10 vincent 451: d_copy(int f, int n)
1.1 deraadt 452: {
1.86 lum 453: struct stat statbuf;
1.59 lum 454: char frname[NFILEN], toname[NFILEN], sname[NFILEN];
455: char *topath, *bufp;
456: int ret;
457: size_t off;
458: struct buffer *bp;
1.1 deraadt 459:
1.15 db 460: if (d_makename(curwp->w_dotp, frname, sizeof(frname)) != FALSE) {
1.66 lum 461: dobeep();
1.5 millert 462: ewprintf("Not a file");
1.15 db 463: return (FALSE);
1.5 millert 464: }
1.15 db 465: off = strlcpy(toname, curbp->b_fname, sizeof(toname));
466: if (off >= sizeof(toname) - 1) { /* can't happen, really */
1.66 lum 467: dobeep();
1.18 cloder 468: ewprintf("Directory name too long");
1.12 vincent 469: return (FALSE);
470: }
1.48 kjell 471: (void)xbasename(sname, frname, NFILEN);
472: bufp = eread("Copy %s to: ", toname, sizeof(toname),
473: EFDEF | EFNEW | EFCR, sname);
1.76 jasper 474: if (bufp == NULL)
1.15 db 475: return (ABORT);
1.14 vincent 476: else if (bufp[0] == '\0')
1.17 otto 477: return (FALSE);
1.59 lum 478:
479: topath = adjustname(toname, TRUE);
1.86 lum 480: if (stat(topath, &statbuf) == 0) {
481: if (S_ISDIR(statbuf.st_mode)) {
482: off = snprintf(toname, sizeof(toname), "%s/%s",
483: topath, sname);
484: if (off < 0 || off >= sizeof(toname) - 1) {
485: dobeep();
486: ewprintf("Directory name too long");
487: return (FALSE);
488: }
489: topath = adjustname(toname, TRUE);
490: }
491: }
1.89 lum 492: if (strcmp(frname, topath) == 0) {
493: ewprintf("Cannot copy to same file: %s", frname);
494: return (TRUE);
495: }
1.59 lum 496: ret = (copy(frname, topath) >= 0) ? TRUE : FALSE;
1.33 kjell 497: if (ret != TRUE)
498: return (ret);
1.54 lum 499: if ((bp = refreshbuffer(curbp)) == NULL)
500: return (FALSE);
1.88 lum 501:
502: ewprintf("Copy: 1 file");
1.37 kjell 503: return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.1 deraadt 504: }
505:
1.5 millert 506: /* ARGSUSED */
507: int
1.10 vincent 508: d_rename(int f, int n)
1.1 deraadt 509: {
1.87 lum 510: struct stat statbuf;
1.59 lum 511: char frname[NFILEN], toname[NFILEN];
512: char *topath, *bufp;
1.33 kjell 513: int ret;
1.31 deraadt 514: size_t off;
515: struct buffer *bp;
1.48 kjell 516: char sname[NFILEN];
1.1 deraadt 517:
1.15 db 518: if (d_makename(curwp->w_dotp, frname, sizeof(frname)) != FALSE) {
1.66 lum 519: dobeep();
1.5 millert 520: ewprintf("Not a file");
1.15 db 521: return (FALSE);
1.5 millert 522: }
1.15 db 523: off = strlcpy(toname, curbp->b_fname, sizeof(toname));
524: if (off >= sizeof(toname) - 1) { /* can't happen, really */
1.66 lum 525: dobeep();
1.18 cloder 526: ewprintf("Directory name too long");
1.12 vincent 527: return (FALSE);
528: }
1.48 kjell 529: (void)xbasename(sname, frname, NFILEN);
530: bufp = eread("Rename %s to: ", toname,
531: sizeof(toname), EFDEF | EFNEW | EFCR, sname);
532: if (bufp == NULL)
1.15 db 533: return (ABORT);
1.14 vincent 534: else if (bufp[0] == '\0')
1.15 db 535: return (FALSE);
1.59 lum 536:
537: topath = adjustname(toname, TRUE);
1.87 lum 538: if (stat(topath, &statbuf) == 0) {
539: if (S_ISDIR(statbuf.st_mode)) {
540: off = snprintf(toname, sizeof(toname), "%s/%s",
541: topath, sname);
542: if (off < 0 || off >= sizeof(toname) - 1) {
543: dobeep();
544: ewprintf("Directory name too long");
545: return (FALSE);
546: }
547: topath = adjustname(toname, TRUE);
548: }
1.89 lum 549: }
550: if (strcmp(frname, topath) == 0) {
551: ewprintf("Cannot move to same file: %s", frname);
552: return (TRUE);
1.87 lum 553: }
1.59 lum 554: ret = (rename(frname, topath) >= 0) ? TRUE : FALSE;
1.33 kjell 555: if (ret != TRUE)
556: return (ret);
1.54 lum 557: if ((bp = refreshbuffer(curbp)) == NULL)
558: return (FALSE);
1.87 lum 559:
560: ewprintf("Move: 1 file");
1.37 kjell 561: return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.1 deraadt 562: }
1.12 vincent 563:
1.31 deraadt 564: /* ARGSUSED */
1.12 vincent 565: void
566: reaper(int signo __attribute__((unused)))
567: {
1.22 deraadt 568: int save_errno = errno, status;
1.12 vincent 569:
1.25 deraadt 570: while (waitpid(-1, &status, WNOHANG) >= 0)
1.12 vincent 571: ;
1.22 deraadt 572: errno = save_errno;
1.12 vincent 573: }
574:
575: /*
576: * Pipe the currently selected file through a shell command.
577: */
1.26 kjell 578: /* ARGSUSED */
1.12 vincent 579: int
580: d_shell_command(int f, int n)
581: {
1.68 guenther 582: char command[512], fname[PATH_MAX], *bufp;
1.31 deraadt 583: struct buffer *bp;
584: struct mgwin *wp;
1.50 lum 585: char sname[NFILEN];
1.12 vincent 586:
587: bp = bfind("*Shell Command Output*", TRUE);
588: if (bclear(bp) != TRUE)
589: return (ABORT);
590:
1.15 db 591: if (d_makename(curwp->w_dotp, fname, sizeof(fname)) != FALSE) {
1.66 lum 592: dobeep();
1.12 vincent 593: ewprintf("bad line");
594: return (ABORT);
595: }
596:
597: command[0] = '\0';
1.48 kjell 598: (void)xbasename(sname, fname, NFILEN);
599: bufp = eread("! on %s: ", command, sizeof(command), EFNEW, sname);
600: if (bufp == NULL)
1.12 vincent 601: return (ABORT);
1.50 lum 602:
603: if (d_exec(0, bp, fname, "sh", "-c", command, NULL) != TRUE)
604: return (ABORT);
605:
606: if ((wp = popbuf(bp, WNONE)) == NULL)
607: return (ABORT); /* XXX - free the buffer?? */
608: curwp = wp;
609: curbp = wp->w_bufp;
610: return (TRUE);
611: }
612:
613: /*
614: * Pipe input file to cmd and insert the command's output in the
615: * given buffer. Each line will be prefixed with the given
616: * number of spaces.
617: */
618: static int
619: d_exec(int space, struct buffer *bp, const char *input, const char *cmd, ...)
620: {
621: char buf[BUFSIZ];
622: va_list ap;
623: struct sigaction olda, newa;
624: char **argv = NULL, *cp;
625: FILE *fin;
626: int fds[2] = { -1, -1 };
627: int infd = -1;
628: int ret = (ABORT), n;
629: pid_t pid;
630:
631: if (sigaction(SIGCHLD, NULL, &olda) == -1)
632: return (ABORT);
633:
634: /* Find the number of arguments. */
635: va_start(ap, cmd);
636: for (n = 2; va_arg(ap, char *) != NULL; n++)
637: ;
638: va_end(ap);
639:
640: /* Allocate and build the argv. */
641: if ((argv = calloc(n, sizeof(*argv))) == NULL) {
1.66 lum 642: dobeep();
1.50 lum 643: ewprintf("Can't allocate argv : %s", strerror(errno));
644: goto out;
645: }
646:
647: n = 1;
648: argv[0] = (char *)cmd;
649: va_start(ap, cmd);
650: while ((argv[n] = va_arg(ap, char *)) != NULL)
651: n++;
652: va_end(ap);
653:
654: if (input == NULL)
655: input = "/dev/null";
656:
657: if ((infd = open(input, O_RDONLY)) == -1) {
1.66 lum 658: dobeep();
1.12 vincent 659: ewprintf("Can't open input file : %s", strerror(errno));
1.50 lum 660: goto out;
1.12 vincent 661: }
1.50 lum 662:
1.12 vincent 663: if (pipe(fds) == -1) {
1.66 lum 664: dobeep();
1.12 vincent 665: ewprintf("Can't create pipe : %s", strerror(errno));
1.50 lum 666: goto out;
1.12 vincent 667: }
668:
669: newa.sa_handler = reaper;
670: newa.sa_flags = 0;
1.50 lum 671: if (sigaction(SIGCHLD, &newa, NULL) == -1)
672: goto out;
673:
674: if ((pid = fork()) == -1) {
1.66 lum 675: dobeep();
1.50 lum 676: ewprintf("Can't fork");
677: goto out;
1.12 vincent 678: }
1.50 lum 679:
1.12 vincent 680: switch (pid) {
1.50 lum 681: case 0: /* Child */
1.12 vincent 682: close(fds[0]);
683: dup2(infd, STDIN_FILENO);
684: dup2(fds[1], STDOUT_FILENO);
685: dup2(fds[1], STDERR_FILENO);
1.50 lum 686: if (execvp(argv[0], argv) == -1)
687: ewprintf("Can't exec %s: %s", argv[0], strerror(errno));
1.12 vincent 688: exit(1);
1.29 deraadt 689: break;
1.50 lum 690: default: /* Parent */
1.12 vincent 691: close(infd);
692: close(fds[1]);
1.50 lum 693: infd = fds[1] = -1;
694: if ((fin = fdopen(fds[0], "r")) == NULL)
695: goto out;
1.15 db 696: while (fgets(buf, sizeof(buf), fin) != NULL) {
1.12 vincent 697: cp = strrchr(buf, '\n');
698: if (cp == NULL && !feof(fin)) { /* too long a line */
699: int c;
1.50 lum 700: addlinef(bp, "%*s%s...", space, "", buf);
1.12 vincent 701: while ((c = getc(fin)) != EOF && c != '\n')
702: ;
703: continue;
704: } else if (cp)
705: *cp = '\0';
1.50 lum 706: addlinef(bp, "%*s%s", space, "", buf);
1.12 vincent 707: }
708: fclose(fin);
709: break;
710: }
1.50 lum 711: ret = (TRUE);
712:
713: out:
1.12 vincent 714: if (sigaction(SIGCHLD, &olda, NULL) == -1)
715: ewprintf("Warning, couldn't reset previous signal handler");
1.50 lum 716: if (fds[0] != -1)
717: close(fds[0]);
718: if (fds[1] != -1)
719: close(fds[1]);
720: if (infd != -1)
721: close(infd);
1.81 mmcc 722: free(argv);
1.50 lum 723: return ret;
1.12 vincent 724: }
725:
1.26 kjell 726: /* ARGSUSED */
1.12 vincent 727: int
728: d_create_directory(int f, int n)
729: {
1.57 lum 730: int ret;
1.31 deraadt 731: struct buffer *bp;
1.12 vincent 732:
1.67 lum 733: ret = ask_makedir();
1.57 lum 734: if (ret != TRUE)
735: return(ret);
736:
1.54 lum 737: if ((bp = refreshbuffer(curbp)) == NULL)
738: return (FALSE);
1.57 lum 739:
1.37 kjell 740: return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.62 lum 741: }
742:
743: /* ARGSUSED */
744: int
745: d_killbuffer_cmd(int f, int n)
746: {
747: return(killbuffer_cmd(FFRAND, 0));
1.63 lum 748: }
749:
750: int
751: d_refreshbuffer(int f, int n)
752: {
753: struct buffer *bp;
754:
755: if ((bp = refreshbuffer(curbp)) == NULL)
756: return (FALSE);
757:
758: return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.54 lum 759: }
760:
1.78 lum 761: /*
762: * Kill then re-open the requested dired buffer.
763: * If required, take a note of any files marked for deletion. Then once
764: * the buffer has been re-opened, remark the same files as deleted.
765: */
1.54 lum 766: struct buffer *
767: refreshbuffer(struct buffer *bp)
768: {
1.78 lum 769: char *tmp_b_fname;
770: int i, tmp_w_dotline, ddel = 0;
1.54 lum 771:
1.78 lum 772: /* remember directory path to open later */
773: tmp_b_fname = strdup(bp->b_fname);
774: if (tmp_b_fname == NULL) {
1.66 lum 775: dobeep();
1.55 lum 776: ewprintf("Out of memory");
1.61 lum 777: return (NULL);
1.55 lum 778: }
1.78 lum 779: tmp_w_dotline = curwp->w_dotline;
780:
781: /* create a list of files for deletion */
782: if (bp->b_flag & BFDIREDDEL)
783: ddel = createlist(bp);
1.54 lum 784:
785: killbuffer(bp);
786:
787: /* dired_() uses findbuffer() to create new buffer */
1.78 lum 788: if ((bp = dired_(tmp_b_fname)) == NULL) {
789: free(tmp_b_fname);
1.54 lum 790: return (NULL);
791: }
1.78 lum 792: free(tmp_b_fname);
793:
794: /* remark any previously deleted files with a 'D' */
795: if (ddel)
796: redelete(bp);
797:
798: /* find dot line */
799: bp->b_dotp = bfirstlp(bp);
800: if (tmp_w_dotline > bp->b_lines)
801: tmp_w_dotline = bp->b_lines - 1;
802: for (i = 1; i < tmp_w_dotline; i++)
803: bp->b_dotp = lforw(bp->b_dotp);
804:
805: bp->b_dotline = i;
806: bp->b_doto = 0;
807: d_warpdot(bp->b_dotp, &bp->b_doto);
808:
1.54 lum 809: curbp = bp;
810:
811: return (bp);
1.12 vincent 812: }
1.24 kjell 813:
814: static int
1.34 kjell 815: d_makename(struct line *lp, char *fn, size_t len)
1.24 kjell 816: {
1.50 lum 817: int start, nlen;
818: char *namep;
1.24 kjell 819:
1.50 lum 820: if (d_warpdot(lp, &start) == FALSE)
1.24 kjell 821: return (ABORT);
1.50 lum 822: namep = &lp->l_text[start];
823: nlen = llength(lp) - start;
824:
825: if (snprintf(fn, len, "%s%.*s", curbp->b_fname, nlen, namep) >= len)
826: return (ABORT); /* Name is too long. */
827:
828: /* Return TRUE if the entry is a directory. */
1.24 kjell 829: return ((lgetc(lp, 2) == 'd') ? TRUE : FALSE);
830: }
831:
1.50 lum 832: #define NAME_FIELD 9
833:
1.49 lum 834: static int
835: d_warpdot(struct line *dotp, int *doto)
836: {
837: char *tp = dotp->l_text;
838: int off = 0, field = 0, len;
839:
840: /*
841: * Find the byte offset to the (space-delimited) filename
842: * field in formatted ls output.
843: */
844: len = llength(dotp);
845: while (off < len) {
846: if (tp[off++] == ' ') {
1.50 lum 847: if (++field == NAME_FIELD) {
848: *doto = off;
849: return (TRUE);
850: }
1.49 lum 851: /* Skip the space. */
852: while (off < len && tp[off] == ' ')
853: off++;
854: }
855: }
1.50 lum 856: /* We didn't find the field. */
857: *doto = 0;
858: return (FALSE);
1.49 lum 859: }
860:
861: static int
1.76 jasper 862: d_forwpage(int f, int n)
1.49 lum 863: {
864: forwpage(f | FFRAND, n);
865: return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
866: }
867:
1.76 jasper 868: static int
1.49 lum 869: d_backpage (int f, int n)
870: {
871: backpage(f | FFRAND, n);
872: return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
873: }
874:
875: static int
876: d_forwline (int f, int n)
877: {
878: forwline(f | FFRAND, n);
879: return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
880: }
881:
882: static int
883: d_backline (int f, int n)
884: {
885: backline(f | FFRAND, n);
886: return (d_warpdot(curwp->w_dotp, &curwp->w_doto));
1.82 lum 887: }
888:
889: int
890: d_filevisitalt (int f, int n)
891: {
892: char fname[NFILEN];
893:
894: if (d_makename(curwp->w_dotp, fname, sizeof(fname)) == ABORT)
895: return (FALSE);
896:
897: return(do_filevisitalt(fname));
1.49 lum 898: }
899:
1.24 kjell 900: /*
1.33 kjell 901: * XXX dname needs to have enough place to store an additional '/'.
1.24 kjell 902: */
1.31 deraadt 903: struct buffer *
1.33 kjell 904: dired_(char *dname)
1.24 kjell 905: {
1.31 deraadt 906: struct buffer *bp;
1.52 haesbaer 907: int i;
908: size_t len;
1.46 kjell 909:
1.77 lum 910: if ((dname = adjustname(dname, TRUE)) == NULL) {
1.66 lum 911: dobeep();
1.24 kjell 912: ewprintf("Bad directory name");
913: return (NULL);
914: }
915: /* this should not be done, instead adjustname() should get a flag */
1.33 kjell 916: len = strlen(dname);
917: if (dname[len - 1] != '/') {
918: dname[len++] = '/';
919: dname[len] = '\0';
1.58 lum 920: }
921: if ((access(dname, R_OK | X_OK)) == -1) {
1.66 lum 922: if (errno == EACCES) {
923: dobeep();
1.80 lum 924: ewprintf("Permission denied: %s", dname);
1.66 lum 925: }
1.58 lum 926: return (NULL);
1.24 kjell 927: }
1.73 lum 928: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
929: if (strcmp(bp->b_fname, dname) == 0) {
930: if (fchecktime(bp) != TRUE)
931: ewprintf("Directory has changed on disk;"
932: " type g to update Dired");
933: return (bp);
934: }
935:
1.24 kjell 936: }
1.73 lum 937: bp = bfind(dname, TRUE);
1.65 lum 938: bp->b_flag |= BFREADONLY | BFIGNDIRTY;
1.50 lum 939:
940: if ((d_exec(2, bp, NULL, "ls", "-al", dname, NULL)) != TRUE)
1.24 kjell 941: return (NULL);
1.49 lum 942:
943: /* Find the line with ".." on it. */
1.41 kjell 944: bp->b_dotp = bfirstlp(bp);
1.72 lum 945: bp->b_dotline = 1;
1.49 lum 946: for (i = 0; i < bp->b_lines; i++) {
947: bp->b_dotp = lforw(bp->b_dotp);
1.72 lum 948: bp->b_dotline++;
1.50 lum 949: if (d_warpdot(bp->b_dotp, &bp->b_doto) == FALSE)
1.49 lum 950: continue;
951: if (strcmp(ltext(bp->b_dotp) + bp->b_doto, "..") == 0)
952: break;
953: }
954:
955: /* We want dot on the entry right after "..", if possible. */
1.72 lum 956: if (++i < bp->b_lines - 2) {
1.49 lum 957: bp->b_dotp = lforw(bp->b_dotp);
1.72 lum 958: bp->b_dotline++;
959: }
1.49 lum 960: d_warpdot(bp->b_dotp, &bp->b_doto);
961:
1.36 kjell 962: (void)strlcpy(bp->b_fname, dname, sizeof(bp->b_fname));
963: (void)strlcpy(bp->b_cwd, dname, sizeof(bp->b_cwd));
1.24 kjell 964: if ((bp->b_modes[1] = name_mode("dired")) == NULL) {
965: bp->b_modes[0] = name_mode("fundamental");
1.66 lum 966: dobeep();
1.24 kjell 967: ewprintf("Could not find mode dired");
968: return (NULL);
969: }
1.73 lum 970: (void)fupdstat(bp);
1.24 kjell 971: bp->b_nmodes = 1;
972: return (bp);
1.78 lum 973: }
974:
975: /*
976: * Iterate through the lines of the dired buffer looking for files
977: * collected in the linked list made in createlist(). If a line is found
978: * replace 'D' as first char in a line. As lines are found, remove the
979: * corresponding item from the linked list. Iterate for as long as there
980: * are items in the linked list or until end of buffer is found.
981: */
982: void
983: redelete(struct buffer *bp)
984: {
1.83 jsg 985: struct delentry *dt, *d1 = NULL;
1.78 lum 986: struct line *lp, *nlp;
987: char fname[NFILEN];
988: char *p = fname;
989: size_t plen, fnlen;
990: int finished = 0;
991:
992: /* reset the deleted file buffer flag until a deleted file is found */
993: bp->b_flag &= ~BFDIREDDEL;
994:
995: for (lp = bfirstlp(bp); lp != bp->b_headp; lp = nlp) {
996: bp->b_dotp = lp;
997: if ((p = findfname(lp, p)) == NULL) {
998: nlp = lforw(lp);
999: continue;
1000: }
1001: plen = strlen(p);
1.83 jsg 1002: SLIST_FOREACH_SAFE(d1, &delhead, entry, dt) {
1.78 lum 1003: fnlen = strlen(d1->fn);
1004: if ((plen == fnlen) &&
1005: (strncmp(p, d1->fn, plen) == 0)) {
1006: lputc(bp->b_dotp, 0, DDELCHAR);
1007: bp->b_flag |= BFDIREDDEL;
1008: SLIST_REMOVE(&delhead, d1, delentry, entry);
1009: if (SLIST_EMPTY(&delhead)) {
1010: finished = 1;
1011: break;
1012: }
1013: }
1014: }
1015: if (finished)
1016: break;
1017: nlp = lforw(lp);
1018: }
1019: while (!SLIST_EMPTY(&delhead)) {
1020: d1 = SLIST_FIRST(&delhead);
1021: SLIST_REMOVE_HEAD(&delhead, entry);
1022: free(d1->fn);
1023: free(d1);
1024: }
1025: return;
1026: }
1027:
1028: /*
1029: * Create a list of files marked for deletion.
1030: */
1031: int
1032: createlist(struct buffer *bp)
1033: {
1034: struct delentry *d1 = NULL, *d2;
1035: struct line *lp, *nlp;
1036: char fname[NFILEN];
1037: char *p = fname;
1038: int ret = FALSE;
1039:
1040: for (lp = bfirstlp(bp); lp != bp->b_headp; lp = nlp) {
1041: /*
1042: * Check if the line has 'D' on the first char and if a valid
1043: * filename can be extracted from it.
1044: */
1045: if (((lp->l_text[0] != DDELCHAR)) ||
1046: ((p = findfname(lp, p)) == NULL)) {
1047: nlp = lforw(lp);
1048: continue;
1049: }
1050: if (SLIST_EMPTY(&delhead)) {
1051: if ((d1 = malloc(sizeof(struct delentry)))
1052: == NULL)
1053: return (ABORT);
1054: if ((d1->fn = strdup(p)) == NULL) {
1055: free(d1);
1056: return (ABORT);
1057: }
1058: SLIST_INSERT_HEAD(&delhead, d1, entry);
1059: } else {
1060: if ((d2 = malloc(sizeof(struct delentry)))
1061: == NULL) {
1062: free(d1->fn);
1063: free(d1);
1064: return (ABORT);
1065: }
1066: if ((d2->fn = strdup(p)) == NULL) {
1067: free(d1->fn);
1068: free(d1);
1069: free(d2);
1070: return (ABORT);
1071: }
1072: SLIST_INSERT_AFTER(d1, d2, entry);
1073: d1 = d2;
1074: }
1075: ret = TRUE;
1076: nlp = lforw(lp);
1077: }
1078: return (ret);
1.91 ! lum 1079: }
! 1080:
! 1081: int
! 1082: d_gotofile(int f, int n)
! 1083: {
! 1084: struct line *lp, *nlp;
! 1085: struct buffer *curbp;
! 1086: char fpath[NFILEN], fname[NFILEN];
! 1087: char *p, *fnp = NULL;
! 1088: int tmp;
! 1089:
! 1090: if (getbufcwd(fpath, sizeof(fpath)) != TRUE)
! 1091: fpath[0] = '\0';
! 1092: fnp = eread("Goto file: ", fpath, NFILEN,
! 1093: EFNEW | EFCR | EFFILE | EFDEF);
! 1094: if (fnp == NULL)
! 1095: return (ABORT);
! 1096: else if (fnp[0] == '\0')
! 1097: return (FALSE);
! 1098:
! 1099: (void)xbasename(fname, fpath, NFILEN);
! 1100: curbp = curwp->w_bufp;
! 1101: tmp = 0;
! 1102: for (lp = bfirstlp(curbp); lp != curbp->b_headp; lp = nlp) {
! 1103: tmp++;
! 1104: if ((p = findfname(lp, p)) == NULL) {
! 1105: nlp = lforw(lp);
! 1106: continue;
! 1107: }
! 1108: if (strcmp(fname, p) == 0) {
! 1109: curwp->w_dotp = lp;
! 1110: curwp->w_dotline = tmp;
! 1111: (void)d_warpdot(curwp->w_dotp, &curwp->w_doto);
! 1112: tmp--;
! 1113: break;
! 1114: }
! 1115: nlp = lforw(lp);
! 1116: }
! 1117: if (tmp == curbp->b_lines - 1) {
! 1118: ewprintf("File not found %s", fname);
! 1119: return (FALSE);
! 1120: } else {
! 1121: ewprintf("");
! 1122: return (TRUE);
! 1123: }
1.78 lum 1124: }
1125:
1126: /*
1127: * Look for and extract a file name on a dired buffer line.
1128: */
1129: char *
1130: findfname(struct line *lp, char *fn)
1131: {
1132: int start;
1133:
1134: (void)d_warpdot(lp, &start);
1135: if (start < 1)
1136: return NULL;
1137: fn = &lp->l_text[start];
1138: return fn;
1.24 kjell 1139: }