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