Annotation of src/usr.bin/mg/file.c, Revision 1.34
1.34 ! cloder 1: /* $OpenBSD: file.c,v 1.33 2005/04/21 19:16:21 beck Exp $ */
1.5 niklas 2:
1.1 deraadt 3: /*
1.4 millert 4: * File commands.
1.1 deraadt 5: */
6:
1.6 art 7: #include <libgen.h>
1.4 millert 8: #include "def.h"
9:
1.1 deraadt 10: /*
1.4 millert 11: * Insert a file into the current buffer. Real easy - just call the
1.1 deraadt 12: * insertfile routine with the file name.
13: */
1.3 millert 14: /* ARGSUSED */
15: int
1.15 vincent 16: fileinsert(int f, int n)
1.1 deraadt 17: {
1.29 vincent 18: char fname[NFILEN], *bufp, *adjf;
1.1 deraadt 19:
1.29 vincent 20: bufp = eread("Insert file: ", fname, NFILEN, EFNEW | EFCR | EFFILE);
21: if (bufp == NULL)
1.32 db 22: return (ABORT);
1.29 vincent 23: else if (bufp[0] == '\0')
1.32 db 24: return (FALSE);
1.29 vincent 25: adjf = adjustname(bufp);
1.22 vincent 26: if (adjf == NULL)
27: return (FALSE);
1.32 db 28: return (insertfile(adjf, NULL, FALSE));
1.1 deraadt 29: }
30:
31: /*
1.9 mickey 32: * Select a file for editing. Look around to see if you can find the file
1.4 millert 33: * in another buffer; if you can find it, just switch to the buffer. If
1.9 mickey 34: * you cannot find the file, create a new buffer, read in the text, and
1.4 millert 35: * switch to the new buffer.
1.1 deraadt 36: */
1.3 millert 37: /* ARGSUSED */
38: int
1.15 vincent 39: filevisit(int f, int n)
1.1 deraadt 40: {
1.4 millert 41: BUFFER *bp;
1.34 ! cloder 42: char fname[NFILEN], *bufp, *adjf, *slash;
! 43:
! 44: if (curbp->b_fname && curbp->b_fname[0] != '\0') {
! 45: strlcpy(fname, curbp->b_fname, sizeof(fname));
! 46: if ((slash = strrchr(fname, '/')) != NULL) {
! 47: *(slash + 1) = '\0';
! 48: }
! 49: }
! 50: else
! 51: fname[0] = '\0';
1.1 deraadt 52:
1.34 ! cloder 53: bufp = eread("Find file: ", fname, NFILEN, EFNEW | EFCR | EFFILE | EFDEF);
1.29 vincent 54: if (bufp == NULL)
1.32 db 55: return (ABORT);
1.29 vincent 56: else if (bufp[0] == '\0')
1.32 db 57: return (FALSE);
1.1 deraadt 58: adjf = adjustname(fname);
1.22 vincent 59: if (adjf == NULL)
60: return (FALSE);
1.3 millert 61: if ((bp = findbuffer(adjf)) == NULL)
1.32 db 62: return (FALSE);
1.1 deraadt 63: curbp = bp;
1.3 millert 64: if (showbuffer(bp, curwp, WFHARD) != TRUE)
1.32 db 65: return (FALSE);
1.30 jfb 66: if (bp->b_fname[0] == 0) {
67: int status;
68:
69: if ((status = readin(adjf)) != TRUE)
70: killbuffer(bp);
1.32 db 71: return (status);
1.30 jfb 72: }
1.32 db 73: return (TRUE);
1.1 deraadt 74: }
75:
1.17 vincent 76: int
77: filevisitro(int f, int n)
78: {
79: int error;
80:
81: error = filevisit(f, n);
82: if (error != TRUE)
83: return (error);
84: curbp->b_flag |= BFREADONLY;
85: return (TRUE);
86: }
1.32 db 87:
1.1 deraadt 88: /*
1.4 millert 89: * Pop to a file in the other window. Same as the last function, but uses
1.1 deraadt 90: * popbuf instead of showbuffer.
91: */
1.3 millert 92: /* ARGSUSED */
93: int
1.15 vincent 94: poptofile(int f, int n)
1.1 deraadt 95: {
1.4 millert 96: BUFFER *bp;
97: MGWIN *wp;
1.29 vincent 98: char fname[NFILEN], *adjf, *bufp;
1.1 deraadt 99:
1.29 vincent 100: if ((bufp = eread("Find file in other window: ", fname, NFILEN,
101: EFNEW | EFCR | EFFILE)) == NULL)
1.32 db 102: return (ABORT);
1.29 vincent 103: else if (bufp[0] == '\0')
1.32 db 104: return (FALSE);
1.1 deraadt 105: adjf = adjustname(fname);
1.22 vincent 106: if (adjf == NULL)
107: return (FALSE);
1.3 millert 108: if ((bp = findbuffer(adjf)) == NULL)
1.32 db 109: return (FALSE);
1.3 millert 110: if ((wp = popbuf(bp)) == NULL)
1.32 db 111: return (FALSE);
1.1 deraadt 112: curbp = bp;
113: curwp = wp;
1.30 jfb 114: if (bp->b_fname[0] == 0) {
115: int status;
116:
117: if ((status = readin(adjf)) != TRUE)
118: killbuffer(bp);
1.32 db 119: return (status);
1.30 jfb 120: }
1.32 db 121: return (TRUE);
1.1 deraadt 122: }
123:
124: /*
1.32 db 125: * Given a file name, either find the buffer it uses, or create a new
1.1 deraadt 126: * empty buffer to put it in.
127: */
128: BUFFER *
1.14 vincent 129: findbuffer(char *fname)
1.1 deraadt 130: {
1.4 millert 131: BUFFER *bp;
1.10 vincent 132: char bname[NBUFN];
1.20 vincent 133: unsigned int count, remain, i;
1.1 deraadt 134:
1.3 millert 135: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1.6 art 136: if (strcmp(bp->b_fname, fname) == 0)
1.32 db 137: return (bp);
1.1 deraadt 138: }
1.14 vincent 139: i = strlcpy(bname, basename(fname), sizeof(bname));
1.33 beck 140: if (i >= sizeof(bname))
141: return NULL;
1.14 vincent 142: remain = sizeof(bname) - i;
143: for (count = 2; bfind(bname, FALSE) != NULL; count++)
144: snprintf(&bname[i], remain, "<%d>", count);
1.10 vincent 145:
1.32 db 146: return (bfind(bname, TRUE));
1.1 deraadt 147: }
148:
149: /*
1.9 mickey 150: * Read the file "fname" into the current buffer. Make all of the text
151: * in the buffer go away, after checking for unsaved changes. This is
152: * called by the "read" command, the "visit" command, and the mainline
1.19 vincent 153: * (for "mg file").
1.1 deraadt 154: */
1.3 millert 155: int
1.19 vincent 156: readin(char *fname)
1.3 millert 157: {
1.4 millert 158: MGWIN *wp;
1.18 vincent 159: int status, i;
1.20 vincent 160: PF *ael;
1.1 deraadt 161:
1.4 millert 162: /* might be old */
163: if (bclear(curbp) != TRUE)
1.32 db 164: return (TRUE);
1.30 jfb 165: if ((status = insertfile(fname, fname, TRUE)) != TRUE) {
166: ewprintf("File is not readable: %s", fname);
1.32 db 167: return (FALSE);
1.30 jfb 168: }
1.18 vincent 169:
170: /*
171: * Call auto-executing function if we need to.
172: */
173: if ((ael = find_autoexec(fname)) != NULL) {
174: for (i = 0; ael[i] != NULL; i++)
175: (*ael[i])(0, 1);
176: free(ael);
177: }
1.4 millert 178:
179: /* no change */
180: curbp->b_flag &= ~BFCHG;
1.3 millert 181: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 182: if (wp->w_bufp == curbp) {
1.3 millert 183: wp->w_dotp = wp->w_linep = lforw(curbp->b_linep);
184: wp->w_doto = 0;
1.1 deraadt 185: wp->w_markp = NULL;
186: wp->w_marko = 0;
187: }
188: }
1.16 vincent 189:
1.32 db 190: /* We need to set the READONLY flag after we insert the file. */
1.16 vincent 191: if (access(fname, W_OK) && errno != ENOENT)
192: curbp->b_flag |= BFREADONLY;
193: else
194: curbp->b_flag &=~ BFREADONLY;
1.25 deraadt 195:
1.24 deraadt 196: if (startrow)
197: gotoline(FFARG, startrow);
1.16 vincent 198:
1.32 db 199: return (status);
1.1 deraadt 200: }
1.4 millert 201:
1.1 deraadt 202: /*
203: * NB, getting file attributes is done here under control of a flag
204: * rather than in readin, which would be cleaner. I was concerned
205: * that some operating system might require the file to be open
206: * in order to get the information. Similarly for writing.
207: */
208:
209: /*
1.9 mickey 210: * Insert a file in the current buffer, after dot. Set mark at the end of
211: * the text inserted; point at the beginning. Return a standard status.
1.31 henning 212: * Print a summary (lines read, error message) out as well. Unless the
1.32 db 213: * NO_BACKUP conditional is set, this routine also does the read end of
1.31 henning 214: * backup processing. The BFBAK flag, if set in a buffer, says that a
215: * backup should be taken. It is set when a file is read in, but not on
216: * a new file. (You don't need to make a backup copy of nothing.)
1.1 deraadt 217: */
1.9 mickey 218: static char *line = NULL;
219: static int linesize = 0;
1.1 deraadt 220:
1.3 millert 221: int
1.20 vincent 222: insertfile(char *fname, char *newname, int needinfo)
1.3 millert 223: {
1.4 millert 224: BUFFER *bp;
225: LINE *lp1, *lp2;
226: LINE *olp; /* line we started at */
227: MGWIN *wp;
1.23 vincent 228: int nbytes, s, nline, siz, x = -1, x2;
1.19 vincent 229: int opos; /* offset we started at */
1.4 millert 230:
1.23 vincent 231: if (needinfo)
232: x = undo_enable(FALSE);
233:
1.4 millert 234: lp1 = NULL;
1.1 deraadt 235: if (line == NULL) {
236: line = malloc(NLINE);
1.21 vincent 237: if (line == NULL)
238: panic("out of memory");
1.1 deraadt 239: linesize = NLINE;
240: }
1.4 millert 241:
242: /* cheap */
243: bp = curbp;
1.8 art 244: if (newname != NULL)
1.10 vincent 245: (void)strlcpy(bp->b_fname, newname, sizeof bp->b_fname);
1.4 millert 246:
247: /* hard file open */
1.8 art 248: if ((s = ffropen(fname, needinfo ? bp : NULL)) == FIOERR)
1.1 deraadt 249: goto out;
1.4 millert 250: if (s == FIOFNF) {
251: /* file not found */
1.1 deraadt 252: if (newname != NULL)
253: ewprintf("(New file)");
1.3 millert 254: else
255: ewprintf("(File not found)");
1.1 deraadt 256: goto out;
257: }
258: opos = curwp->w_doto;
1.4 millert 259:
260: /* open a new line, at point, and start inserting after it */
1.23 vincent 261: x2 = undo_enable(FALSE);
1.20 vincent 262: (void)lnewline();
1.1 deraadt 263: olp = lback(curwp->w_dotp);
1.3 millert 264: if (olp == curbp->b_linep) {
1.1 deraadt 265: /* if at end of buffer, create a line to insert before */
1.7 art 266: (void)lnewline();
1.1 deraadt 267: curwp->w_dotp = lback(curwp->w_dotp);
268: }
1.23 vincent 269: undo_enable(x2);
1.4 millert 270:
271: /* don't count fake lines at the end */
272: nline = 0;
1.19 vincent 273: siz = 0;
1.3 millert 274: while ((s = ffgetline(line, linesize, &nbytes)) != FIOERR) {
1.1 deraadt 275: doneread:
1.19 vincent 276: siz += nbytes + 1;
1.3 millert 277: switch (s) {
278: case FIOSUC:
279: ++nline;
280: /* and continue */
1.4 millert 281: case FIOEOF:
282: /* the last line of the file */
1.3 millert 283: if ((lp1 = lalloc(nbytes)) == NULL) {
1.4 millert 284: /* keep message on the display */
285: s = FIOERR;
286: goto endoffile;
1.3 millert 287: }
288: bcopy(line, <ext(lp1)[0], nbytes);
289: lp2 = lback(curwp->w_dotp);
290: lp2->l_fp = lp1;
291: lp1->l_fp = curwp->w_dotp;
292: lp1->l_bp = lp2;
293: curwp->w_dotp->l_bp = lp1;
294: if (s == FIOEOF)
295: goto endoffile;
296: break;
1.19 vincent 297: case FIOLONG: {
1.4 millert 298: /* a line too long to fit in our buffer */
1.9 mickey 299: char *cp;
300: int newsize;
1.3 millert 301:
302: newsize = linesize * 2;
303: if (newsize < 0 ||
1.4 millert 304: (cp = malloc((unsigned)newsize)) == NULL) {
1.3 millert 305: ewprintf("Could not allocate %d bytes",
1.4 millert 306: newsize);
307: s = FIOERR;
308: goto endoffile;
1.3 millert 309: }
310: bcopy(line, cp, linesize);
311: free(line);
312: line = cp;
1.9 mickey 313: s = ffgetline(line + linesize, linesize,
1.4 millert 314: &nbytes);
1.3 millert 315: nbytes += linesize;
316: linesize = newsize;
317: if (s == FIOERR)
318: goto endoffile;
319: goto doneread;
320: }
321: default:
322: ewprintf("Unknown code %d reading file", s);
323: s = FIOERR;
324: break;
1.1 deraadt 325: }
326: }
327: endoffile:
1.28 vincent 328: undo_add_insert(olp, opos, siz-1);
1.19 vincent 329:
1.4 millert 330: /* ignore errors */
1.8 art 331: ffclose(NULL);
1.4 millert 332: /* don't zap an error */
333: if (s == FIOEOF) {
1.3 millert 334: if (nline == 1)
335: ewprintf("(Read 1 line)");
336: else
337: ewprintf("(Read %d lines)", nline);
1.1 deraadt 338: }
1.4 millert 339: /* set mark at the end of the text */
1.1 deraadt 340: curwp->w_dotp = curwp->w_markp = lback(curwp->w_dotp);
341: curwp->w_marko = llength(curwp->w_markp);
1.7 art 342: (void)ldelnewline();
1.1 deraadt 343: curwp->w_dotp = olp;
344: curwp->w_doto = opos;
1.3 millert 345: if (olp == curbp->b_linep)
346: curwp->w_dotp = lforw(olp);
1.1 deraadt 347: #ifndef NO_BACKUP
348: if (newname != NULL)
1.3 millert 349: bp->b_flag |= BFCHG | BFBAK; /* Need a backup. */
350: else
351: bp->b_flag |= BFCHG;
1.4 millert 352: #else /* !NO_BACKUP */
1.1 deraadt 353: bp->b_flag |= BFCHG;
1.4 millert 354: #endif /* !NO_BACKUP */
1.3 millert 355: /*
1.32 db 356: * If the insert was at the end of buffer, set lp1 to the end of
1.3 millert 357: * buffer line, and lp2 to the beginning of the newly inserted text.
358: * (Otherwise lp2 is set to NULL.) This is used below to set
359: * pointers in other windows correctly if they are also at the end of
360: * buffer.
1.1 deraadt 361: */
362: lp1 = bp->b_linep;
363: if (curwp->w_markp == lp1) {
364: lp2 = curwp->w_dotp;
365: } else {
1.4 millert 366: /* delete extraneous newline */
1.7 art 367: (void)ldelnewline();
1.1 deraadt 368: out: lp2 = NULL;
369: }
1.3 millert 370: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 371: if (wp->w_bufp == curbp) {
1.3 millert 372: wp->w_flag |= WFMODE | WFEDIT;
1.1 deraadt 373: if (wp != curwp && lp2 != NULL) {
1.3 millert 374: if (wp->w_dotp == lp1)
375: wp->w_dotp = lp2;
376: if (wp->w_markp == lp1)
377: wp->w_markp = lp2;
378: if (wp->w_linep == lp1)
379: wp->w_linep = lp2;
1.1 deraadt 380: }
381: }
382: }
1.23 vincent 383: if (x != -1)
384: undo_enable(x);
385:
1.4 millert 386: /* return false if error */
1.32 db 387: return (s != FIOERR);
1.1 deraadt 388: }
389:
390: /*
1.9 mickey 391: * Ask for a file name and write the contents of the current buffer to that
392: * file. Update the remembered file name and clear the buffer changed flag.
1.4 millert 393: * This handling of file names is different from the earlier versions and
1.27 jmc 394: * is more compatible with Gosling EMACS than with ITS EMACS.
1.1 deraadt 395: */
1.3 millert 396: /* ARGSUSED */
397: int
1.20 vincent 398: filewrite(int f, int n)
1.1 deraadt 399: {
1.4 millert 400: int s;
401: char fname[NFILEN];
1.29 vincent 402: char *adjfname, *p, *bufp;
403:
404: if ((bufp = eread("Write file: ", fname, NFILEN,
405: EFNEW | EFCR | EFFILE)) == NULL)
1.32 db 406: return (ABORT);
1.29 vincent 407: else if (bufp[0] == '\0')
1.32 db 408: return (FALSE);
1.1 deraadt 409:
410: adjfname = adjustname(fname);
1.22 vincent 411: if (adjfname == NULL)
412: return (FALSE);
1.1 deraadt 413: /* old attributes are no longer current */
414: bzero(&curbp->b_fi, sizeof(curbp->b_fi));
1.3 millert 415: if ((s = writeout(curbp, adjfname)) == TRUE) {
1.10 vincent 416: (void)strlcpy(curbp->b_fname, adjfname, sizeof curbp->b_fname);
1.26 vincent 417: p = strrchr(curbp->b_fname, '/');
418: if (p)
419: p++;
420: else
421: p = curbp->b_fname;
422: if (curbp->b_bname)
423: free((char *)curbp->b_bname);
424: curbp->b_bname = strdup(p);
1.1 deraadt 425: #ifndef NO_BACKUP
426: curbp->b_flag &= ~(BFBAK | BFCHG);
1.4 millert 427: #else /* !NO_BACKUP */
1.1 deraadt 428: curbp->b_flag &= ~BFCHG;
1.4 millert 429: #endif /* !NO_BACKUP */
1.1 deraadt 430: upmodes(curbp);
431: }
1.32 db 432: return (s);
1.1 deraadt 433: }
434:
435: /*
1.4 millert 436: * Save the contents of the current buffer back into its associated file.
1.1 deraadt 437: */
438: #ifndef NO_BACKUP
439: #ifndef MAKEBACKUP
440: #define MAKEBACKUP TRUE
1.4 millert 441: #endif /* !MAKEBACKUP */
1.9 mickey 442: static int makebackup = MAKEBACKUP;
1.4 millert 443: #endif /* !NO_BACKUP */
1.1 deraadt 444:
1.3 millert 445: /* ARGSUSED */
446: int
1.20 vincent 447: filesave(int f, int n)
1.1 deraadt 448: {
1.32 db 449: return (buffsave(curbp));
1.1 deraadt 450: }
451:
452: /*
1.9 mickey 453: * Save the contents of the buffer argument into its associated file. Do
454: * nothing if there have been no changes (is this a bug, or a feature?).
455: * Error if there is no remembered file name. If this is the first write
1.4 millert 456: * since the read or visit, then a backup copy of the file is made.
1.9 mickey 457: * Allow user to select whether or not to make backup files by looking at
1.4 millert 458: * the value of makebackup.
1.1 deraadt 459: */
1.3 millert 460: int
1.20 vincent 461: buffsave(BUFFER *bp)
1.3 millert 462: {
1.4 millert 463: int s;
1.1 deraadt 464:
1.4 millert 465: /* return, no changes */
466: if ((bp->b_flag & BFCHG) == 0) {
1.1 deraadt 467: ewprintf("(No changes need to be saved)");
1.32 db 468: return (TRUE);
1.1 deraadt 469: }
1.4 millert 470:
471: /* must have a name */
472: if (bp->b_fname[0] == '\0') {
1.1 deraadt 473: ewprintf("No file name");
474: return (FALSE);
475: }
1.4 millert 476:
1.1 deraadt 477: #ifndef NO_BACKUP
1.3 millert 478: if (makebackup && (bp->b_flag & BFBAK)) {
1.1 deraadt 479: s = fbackupfile(bp->b_fname);
1.4 millert 480: /* hard error */
481: if (s == ABORT)
1.32 db 482: return (FALSE);
1.4 millert 483: /* softer error */
1.9 mickey 484: if (s == FALSE &&
1.4 millert 485: (s = eyesno("Backup error, save anyway")) != TRUE)
1.32 db 486: return (s);
1.1 deraadt 487: }
1.4 millert 488: #endif /* !NO_BACKUP */
1.3 millert 489: if ((s = writeout(bp, bp->b_fname)) == TRUE) {
1.1 deraadt 490: #ifndef NO_BACKUP
491: bp->b_flag &= ~(BFCHG | BFBAK);
1.4 millert 492: #else /* !NO_BACKUP */
1.1 deraadt 493: bp->b_flag &= ~BFCHG;
1.4 millert 494: #endif /* !NO_BACKUP */
1.1 deraadt 495: upmodes(bp);
496: }
1.32 db 497: return (s);
1.1 deraadt 498: }
499:
500: #ifndef NO_BACKUP
1.3 millert 501: /*
502: * Since we don't have variables (we probably should) this is a command
503: * processor for changing the value of the make backup flag. If no argument
504: * is given, sets makebackup to true, so backups are made. If an argument is
505: * given, no backup files are made when saving a new version of a file. Only
506: * used when BACKUP is #defined.
1.1 deraadt 507: */
1.3 millert 508: /* ARGSUSED */
509: int
1.20 vincent 510: makebkfile(int f, int n)
1.1 deraadt 511: {
1.3 millert 512: if (f & FFARG)
513: makebackup = n > 0;
514: else
515: makebackup = !makebackup;
1.1 deraadt 516: ewprintf("Backup files %sabled", makebackup ? "en" : "dis");
1.32 db 517: return (TRUE);
1.1 deraadt 518: }
1.4 millert 519: #endif /* !NO_BACKUP */
1.1 deraadt 520:
521: /*
522: * NB: bp is passed to both ffwopen and ffclose because some
523: * attribute information may need to be updated at open time
524: * and others after the close. This is OS-dependent. Note
525: * that the ff routines are assumed to be able to tell whether
526: * the attribute information has been set up in this buffer
527: * or not.
528: */
529:
530: /*
1.9 mickey 531: * This function performs the details of file writing; writing the file
532: * in buffer bp to file fn. Uses the file management routines in the
1.4 millert 533: * "fileio.c" package. Most of the grief is checking of some sort.
1.1 deraadt 534: */
1.3 millert 535: int
1.20 vincent 536: writeout(BUFFER *bp, char *fn)
1.3 millert 537: {
1.4 millert 538: int s;
1.1 deraadt 539:
1.4 millert 540: /* open writes message */
541: if ((s = ffwopen(fn, bp)) != FIOSUC)
1.1 deraadt 542: return (FALSE);
543: s = ffputbuf(bp);
1.4 millert 544: if (s == FIOSUC) {
545: /* no write error */
1.1 deraadt 546: s = ffclose(bp);
1.3 millert 547: if (s == FIOSUC)
1.1 deraadt 548: ewprintf("Wrote %s", fn);
1.4 millert 549: } else
550: /* ignore close error if it is a write error */
1.7 art 551: (void)ffclose(bp);
1.32 db 552: return (s == FIOSUC);
1.1 deraadt 553: }
554:
555: /*
1.4 millert 556: * Tag all windows for bp (all windows if bp == NULL) as needing their
1.1 deraadt 557: * mode line updated.
558: */
1.7 art 559: void
1.20 vincent 560: upmodes(BUFFER *bp)
1.3 millert 561: {
1.4 millert 562: MGWIN *wp;
1.1 deraadt 563:
564: for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
1.3 millert 565: if (bp == NULL || curwp->w_bufp == bp)
566: wp->w_flag |= WFMODE;
1.1 deraadt 567: }