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