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