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