Annotation of src/usr.bin/mg/file.c, Revision 1.64
1.64 ! kjell 1: /* $OpenBSD: file.c,v 1.63 2006/11/17 08:45:31 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.58 jason 31: adjf = adjustname(bufp, TRUE);
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.58 jason 59: adjf = adjustname(fname, TRUE);
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:
1.58 jason 102: adjf = adjustname(fname, TRUE);
1.36 kjell 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.58 jason 150: adjf = adjustname(fname, TRUE);
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.56 kjell 155: if (bp == curbp)
156: return (splitwind(f, n));
1.3 millert 157: if ((wp = popbuf(bp)) == NULL)
1.32 db 158: return (FALSE);
1.1 deraadt 159: curbp = bp;
160: curwp = wp;
1.39 kjell 161: if (bp->b_fname[0] == '\0') {
1.30 jfb 162: if ((status = readin(adjf)) != TRUE)
163: killbuffer(bp);
1.32 db 164: return (status);
1.30 jfb 165: }
1.32 db 166: return (TRUE);
1.1 deraadt 167: }
168:
169: /*
1.32 db 170: * Given a file name, either find the buffer it uses, or create a new
1.1 deraadt 171: * empty buffer to put it in.
172: */
1.45 deraadt 173: struct buffer *
1.44 kjell 174: findbuffer(char *fn)
1.1 deraadt 175: {
1.52 deraadt 176: struct buffer *bp;
177: char bname[NBUFN], fname[NBUFN];
1.1 deraadt 178:
1.44 kjell 179: if (strlcpy(fname, fn, sizeof(fname)) >= sizeof(fname)) {
180: ewprintf("filename too long");
181: return (NULL);
182: }
183:
1.3 millert 184: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1.6 art 185: if (strcmp(bp->b_fname, fname) == 0)
1.32 db 186: return (bp);
1.1 deraadt 187: }
1.53 kjell 188: /* Not found. Create a new one, adjusting name first */
1.54 kjell 189: if (augbname(bname, fname, sizeof(bname)) == FALSE)
1.44 kjell 190: return (NULL);
1.10 vincent 191:
1.53 kjell 192: bp = bfind(bname, TRUE);
193: return (bp);
1.1 deraadt 194: }
195:
196: /*
1.9 mickey 197: * Read the file "fname" into the current buffer. Make all of the text
198: * in the buffer go away, after checking for unsaved changes. This is
199: * called by the "read" command, the "visit" command, and the mainline
1.19 vincent 200: * (for "mg file").
1.1 deraadt 201: */
1.3 millert 202: int
1.19 vincent 203: readin(char *fname)
1.3 millert 204: {
1.45 deraadt 205: struct mgwin *wp;
1.41 kjell 206: int status, i, ro = FALSE;
1.20 vincent 207: PF *ael;
1.1 deraadt 208:
1.4 millert 209: /* might be old */
210: if (bclear(curbp) != TRUE)
1.32 db 211: return (TRUE);
1.57 kjell 212: /* Clear readonly. May be set by autoexec path */
213: curbp->b_flag &=~ BFREADONLY;
1.30 jfb 214: if ((status = insertfile(fname, fname, TRUE)) != TRUE) {
215: ewprintf("File is not readable: %s", fname);
1.32 db 216: return (FALSE);
1.30 jfb 217: }
1.18 vincent 218:
1.57 kjell 219: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
220: if (wp->w_bufp == curbp) {
1.62 kjell 221: wp->w_dotp = wp->w_linep = bfirstlp(curbp);
1.57 kjell 222: wp->w_doto = 0;
223: wp->w_markp = NULL;
224: wp->w_marko = 0;
225: }
226: }
227:
1.18 vincent 228: /*
229: * Call auto-executing function if we need to.
230: */
231: if ((ael = find_autoexec(fname)) != NULL) {
232: for (i = 0; ael[i] != NULL; i++)
233: (*ael[i])(0, 1);
234: free(ael);
235: }
1.4 millert 236:
237: /* no change */
238: curbp->b_flag &= ~BFCHG;
1.16 vincent 239:
1.41 kjell 240: /*
241: * We need to set the READONLY flag after we insert the file,
242: * unless the file is a directory.
243: */
1.16 vincent 244: if (access(fname, W_OK) && errno != ENOENT)
1.41 kjell 245: ro = TRUE;
246: if (fisdir(fname) == TRUE)
247: ro = TRUE;
248: if (ro == TRUE)
1.16 vincent 249: curbp->b_flag |= BFREADONLY;
1.25 deraadt 250:
1.24 deraadt 251: if (startrow)
252: gotoline(FFARG, startrow);
1.16 vincent 253:
1.60 kjell 254: undo_add_modified();
1.32 db 255: return (status);
1.1 deraadt 256: }
1.4 millert 257:
1.1 deraadt 258: /*
259: * NB, getting file attributes is done here under control of a flag
260: * rather than in readin, which would be cleaner. I was concerned
261: * that some operating system might require the file to be open
262: * in order to get the information. Similarly for writing.
263: */
264:
265: /*
1.39 kjell 266: * Insert a file in the current buffer, after dot. If file is a directory,
267: * and 'replacebuf' is TRUE, invoke dired mode, else die with an error.
268: * If file is a regular file, set mark at the end of the text inserted;
1.47 kjell 269: * point at the beginning. Return a standard status. Print a summary
270: * (lines read, error message) out as well. This routine also does the
271: * read end of backup processing. The BFBAK flag, if set in a buffer,
272: * says that a backup should be taken. It is set when a file is read in,
273: * but not on a new file. You don't need to make a backup copy of nothing.
1.1 deraadt 274: */
1.47 kjell 275:
1.9 mickey 276: static char *line = NULL;
277: static int linesize = 0;
1.1 deraadt 278:
1.3 millert 279: int
1.39 kjell 280: insertfile(char *fname, char *newname, int replacebuf)
1.3 millert 281: {
1.45 deraadt 282: struct buffer *bp;
283: struct line *lp1, *lp2;
284: struct line *olp; /* line we started at */
285: struct mgwin *wp;
1.63 kjell 286: int nbytes, s, nline = 0, siz, x, x2;
1.19 vincent 287: int opos; /* offset we started at */
1.59 kjell 288: int oline; /* original line number */
1.4 millert 289:
1.39 kjell 290: if (replacebuf == TRUE)
1.23 vincent 291: x = undo_enable(FALSE);
1.63 kjell 292: else
293: x = undo_enabled();
1.23 vincent 294:
1.4 millert 295: lp1 = NULL;
1.1 deraadt 296: if (line == NULL) {
297: line = malloc(NLINE);
1.21 vincent 298: if (line == NULL)
299: panic("out of memory");
1.1 deraadt 300: linesize = NLINE;
301: }
1.4 millert 302:
303: /* cheap */
304: bp = curbp;
1.54 kjell 305: if (newname != NULL) {
306: (void)strlcpy(bp->b_fname, newname, sizeof(bp->b_fname));
307: (void)strlcpy(bp->b_cwd, dirname(newname),
308: sizeof(bp->b_cwd));
309: (void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
310: }
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;
1.55 kjell 334: return (showbuffer(bp, curwp, WFFULL | WFMODE));
1.54 kjell 335: } else {
336: (void)strlcpy(bp->b_cwd, dirname(fname), sizeof(bp->b_cwd));
337: (void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
1.1 deraadt 338: }
339: opos = curwp->w_doto;
1.64 ! kjell 340: oline = curwp->w_dotline;
! 341: /*
! 342: * Open a new line at dot and start inserting after it.
! 343: * We will delete this newline after insertion.
! 344: * Disable undo, as we create the undo record manually.
! 345: */
1.23 vincent 346: x2 = undo_enable(FALSE);
1.20 vincent 347: (void)lnewline();
1.1 deraadt 348: olp = lback(curwp->w_dotp);
1.23 vincent 349: undo_enable(x2);
1.4 millert 350:
351: nline = 0;
1.19 vincent 352: siz = 0;
1.3 millert 353: while ((s = ffgetline(line, linesize, &nbytes)) != FIOERR) {
1.64 ! kjell 354: retry:
1.19 vincent 355: siz += nbytes + 1;
1.3 millert 356: switch (s) {
357: case FIOSUC:
1.48 kjell 358: /* FALLTHRU */
1.4 millert 359: case FIOEOF:
1.59 kjell 360: ++nline;
1.3 millert 361: if ((lp1 = lalloc(nbytes)) == NULL) {
1.4 millert 362: /* keep message on the display */
363: s = FIOERR;
1.64 ! kjell 364: undo_add_insert(olp, opos,
! 365: siz - nbytes - 1 - 1);
1.4 millert 366: goto endoffile;
1.3 millert 367: }
368: bcopy(line, <ext(lp1)[0], nbytes);
369: lp2 = lback(curwp->w_dotp);
370: lp2->l_fp = lp1;
371: lp1->l_fp = curwp->w_dotp;
372: lp1->l_bp = lp2;
373: curwp->w_dotp->l_bp = lp1;
1.64 ! kjell 374: if (s == FIOEOF) {
! 375: undo_add_insert(olp, opos, siz - 1);
1.3 millert 376: goto endoffile;
1.64 ! kjell 377: }
1.3 millert 378: break;
1.19 vincent 379: case FIOLONG: {
1.4 millert 380: /* a line too long to fit in our buffer */
1.9 mickey 381: char *cp;
382: int newsize;
1.3 millert 383:
384: newsize = linesize * 2;
385: if (newsize < 0 ||
1.51 deraadt 386: (cp = malloc(newsize)) == NULL) {
1.3 millert 387: ewprintf("Could not allocate %d bytes",
1.4 millert 388: newsize);
389: s = FIOERR;
390: goto endoffile;
1.3 millert 391: }
392: bcopy(line, cp, linesize);
393: free(line);
394: line = cp;
1.9 mickey 395: s = ffgetline(line + linesize, linesize,
1.4 millert 396: &nbytes);
1.3 millert 397: nbytes += linesize;
398: linesize = newsize;
399: if (s == FIOERR)
400: goto endoffile;
1.64 ! kjell 401: goto retry;
1.3 millert 402: }
403: default:
404: ewprintf("Unknown code %d reading file", s);
405: s = FIOERR;
406: break;
1.1 deraadt 407: }
408: }
409: endoffile:
1.4 millert 410: /* ignore errors */
1.8 art 411: ffclose(NULL);
1.4 millert 412: /* don't zap an error */
413: if (s == FIOEOF) {
1.3 millert 414: if (nline == 1)
415: ewprintf("(Read 1 line)");
416: else
417: ewprintf("(Read %d lines)", nline);
1.1 deraadt 418: }
1.4 millert 419: /* set mark at the end of the text */
1.1 deraadt 420: curwp->w_dotp = curwp->w_markp = lback(curwp->w_dotp);
421: curwp->w_marko = llength(curwp->w_markp);
1.64 ! kjell 422: curwp->w_markline = oline + nline + 1;
! 423: /*
! 424: * if we are at the end of the file, ldelnewline is a no-op,
! 425: * but we still need to decrement the line and markline counts
! 426: * as we've accounted for this fencepost in our arithmetic
! 427: */
! 428: if (lforw(curwp->w_dotp) == curwp->w_bufp->b_headp) {
! 429: curwp->w_bufp->b_lines--;
! 430: curwp->w_markline--;
! 431: } else
! 432: (void)ldelnewline();
1.1 deraadt 433: curwp->w_dotp = olp;
434: curwp->w_doto = opos;
1.59 kjell 435: curwp->w_dotline = oline;
1.61 kjell 436: if (olp == curbp->b_headp)
1.3 millert 437: curwp->w_dotp = lforw(olp);
1.1 deraadt 438: if (newname != NULL)
1.3 millert 439: bp->b_flag |= BFCHG | BFBAK; /* Need a backup. */
440: else
441: bp->b_flag |= BFCHG;
442: /*
1.32 db 443: * If the insert was at the end of buffer, set lp1 to the end of
1.3 millert 444: * buffer line, and lp2 to the beginning of the newly inserted text.
445: * (Otherwise lp2 is set to NULL.) This is used below to set
446: * pointers in other windows correctly if they are also at the end of
447: * buffer.
1.1 deraadt 448: */
1.61 kjell 449: lp1 = bp->b_headp;
1.1 deraadt 450: if (curwp->w_markp == lp1) {
451: lp2 = curwp->w_dotp;
452: } else {
1.4 millert 453: /* delete extraneous newline */
1.7 art 454: (void)ldelnewline();
1.1 deraadt 455: out: lp2 = NULL;
456: }
1.3 millert 457: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 458: if (wp->w_bufp == curbp) {
1.3 millert 459: wp->w_flag |= WFMODE | WFEDIT;
1.1 deraadt 460: if (wp != curwp && lp2 != NULL) {
1.3 millert 461: if (wp->w_dotp == lp1)
462: wp->w_dotp = lp2;
463: if (wp->w_markp == lp1)
464: wp->w_markp = lp2;
465: if (wp->w_linep == lp1)
466: wp->w_linep = lp2;
1.1 deraadt 467: }
468: }
469: }
1.59 kjell 470: bp->b_lines += nline;
1.40 kjell 471: cleanup:
1.39 kjell 472: undo_enable(x);
1.23 vincent 473:
1.39 kjell 474: /* return FALSE if error */
1.32 db 475: return (s != FIOERR);
1.1 deraadt 476: }
477:
478: /*
1.9 mickey 479: * Ask for a file name and write the contents of the current buffer to that
480: * file. Update the remembered file name and clear the buffer changed flag.
1.4 millert 481: * This handling of file names is different from the earlier versions and
1.27 jmc 482: * is more compatible with Gosling EMACS than with ITS EMACS.
1.1 deraadt 483: */
1.3 millert 484: /* ARGSUSED */
485: int
1.20 vincent 486: filewrite(int f, int n)
1.1 deraadt 487: {
1.4 millert 488: int s;
1.53 kjell 489: char fname[NFILEN], bn[NBUFN];
490: char *adjfname, *bufp;
1.29 vincent 491:
1.54 kjell 492: if (getbufcwd(fname, sizeof(fname)) != TRUE)
493: fname[0] = '\0';
1.29 vincent 494: if ((bufp = eread("Write file: ", fname, NFILEN,
1.54 kjell 495: EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
1.32 db 496: return (ABORT);
1.29 vincent 497: else if (bufp[0] == '\0')
1.32 db 498: return (FALSE);
1.1 deraadt 499:
1.58 jason 500: adjfname = adjustname(fname, TRUE);
1.22 vincent 501: if (adjfname == NULL)
502: return (FALSE);
1.1 deraadt 503: /* old attributes are no longer current */
504: bzero(&curbp->b_fi, sizeof(curbp->b_fi));
1.3 millert 505: if ((s = writeout(curbp, adjfname)) == TRUE) {
1.53 kjell 506: (void)strlcpy(curbp->b_fname, adjfname, sizeof(curbp->b_fname));
1.54 kjell 507: if (getbufcwd(curbp->b_cwd, sizeof(curbp->b_cwd)) != TRUE)
508: (void)strlcpy(curbp->b_cwd, "/", sizeof(curbp->b_cwd));
1.50 kjell 509: free(curbp->b_bname);
1.54 kjell 510: if (augbname(bn, basename(curbp->b_fname), sizeof(bn))
1.53 kjell 511: == FALSE)
512: return (FALSE);
513: if ((curbp->b_bname = strdup(bn)) == NULL)
514: return (FALSE);
1.1 deraadt 515: curbp->b_flag &= ~(BFBAK | BFCHG);
516: upmodes(curbp);
517: }
1.32 db 518: return (s);
1.1 deraadt 519: }
520:
521: /*
1.4 millert 522: * Save the contents of the current buffer back into its associated file.
1.1 deraadt 523: */
524: #ifndef MAKEBACKUP
525: #define MAKEBACKUP TRUE
1.4 millert 526: #endif /* !MAKEBACKUP */
1.9 mickey 527: static int makebackup = MAKEBACKUP;
1.1 deraadt 528:
1.3 millert 529: /* ARGSUSED */
530: int
1.20 vincent 531: filesave(int f, int n)
1.1 deraadt 532: {
1.32 db 533: return (buffsave(curbp));
1.1 deraadt 534: }
535:
536: /*
1.9 mickey 537: * Save the contents of the buffer argument into its associated file. Do
538: * nothing if there have been no changes (is this a bug, or a feature?).
539: * Error if there is no remembered file name. If this is the first write
1.4 millert 540: * since the read or visit, then a backup copy of the file is made.
1.9 mickey 541: * Allow user to select whether or not to make backup files by looking at
1.4 millert 542: * the value of makebackup.
1.1 deraadt 543: */
1.3 millert 544: int
1.45 deraadt 545: buffsave(struct buffer *bp)
1.3 millert 546: {
1.4 millert 547: int s;
1.1 deraadt 548:
1.4 millert 549: /* return, no changes */
550: if ((bp->b_flag & BFCHG) == 0) {
1.1 deraadt 551: ewprintf("(No changes need to be saved)");
1.32 db 552: return (TRUE);
1.1 deraadt 553: }
1.4 millert 554:
555: /* must have a name */
556: if (bp->b_fname[0] == '\0') {
1.1 deraadt 557: ewprintf("No file name");
558: return (FALSE);
559: }
1.4 millert 560:
1.3 millert 561: if (makebackup && (bp->b_flag & BFBAK)) {
1.1 deraadt 562: s = fbackupfile(bp->b_fname);
1.4 millert 563: /* hard error */
564: if (s == ABORT)
1.32 db 565: return (FALSE);
1.4 millert 566: /* softer error */
1.9 mickey 567: if (s == FALSE &&
1.4 millert 568: (s = eyesno("Backup error, save anyway")) != TRUE)
1.32 db 569: return (s);
1.1 deraadt 570: }
1.3 millert 571: if ((s = writeout(bp, bp->b_fname)) == TRUE) {
1.1 deraadt 572: bp->b_flag &= ~(BFCHG | BFBAK);
573: upmodes(bp);
574: }
1.32 db 575: return (s);
1.1 deraadt 576: }
577:
1.3 millert 578: /*
579: * Since we don't have variables (we probably should) this is a command
580: * processor for changing the value of the make backup flag. If no argument
581: * is given, sets makebackup to true, so backups are made. If an argument is
1.59 kjell 582: * given, no backup files are made when saving a new version of a file.
1.1 deraadt 583: */
1.3 millert 584: /* ARGSUSED */
585: int
1.20 vincent 586: makebkfile(int f, int n)
1.1 deraadt 587: {
1.3 millert 588: if (f & FFARG)
589: makebackup = n > 0;
590: else
591: makebackup = !makebackup;
1.1 deraadt 592: ewprintf("Backup files %sabled", makebackup ? "en" : "dis");
1.32 db 593: return (TRUE);
1.1 deraadt 594: }
595:
596: /*
597: * NB: bp is passed to both ffwopen and ffclose because some
598: * attribute information may need to be updated at open time
599: * and others after the close. This is OS-dependent. Note
600: * that the ff routines are assumed to be able to tell whether
601: * the attribute information has been set up in this buffer
602: * or not.
603: */
604:
605: /*
1.9 mickey 606: * This function performs the details of file writing; writing the file
607: * in buffer bp to file fn. Uses the file management routines in the
1.4 millert 608: * "fileio.c" package. Most of the grief is checking of some sort.
1.1 deraadt 609: */
1.3 millert 610: int
1.45 deraadt 611: writeout(struct buffer *bp, char *fn)
1.3 millert 612: {
1.4 millert 613: int s;
1.1 deraadt 614:
1.4 millert 615: /* open writes message */
616: if ((s = ffwopen(fn, bp)) != FIOSUC)
1.1 deraadt 617: return (FALSE);
618: s = ffputbuf(bp);
1.4 millert 619: if (s == FIOSUC) {
620: /* no write error */
1.1 deraadt 621: s = ffclose(bp);
1.3 millert 622: if (s == FIOSUC)
1.1 deraadt 623: ewprintf("Wrote %s", fn);
1.4 millert 624: } else
625: /* ignore close error if it is a write error */
1.7 art 626: (void)ffclose(bp);
1.32 db 627: return (s == FIOSUC);
1.1 deraadt 628: }
629:
630: /*
1.4 millert 631: * Tag all windows for bp (all windows if bp == NULL) as needing their
1.1 deraadt 632: * mode line updated.
633: */
1.7 art 634: void
1.45 deraadt 635: upmodes(struct buffer *bp)
1.3 millert 636: {
1.45 deraadt 637: struct mgwin *wp;
1.1 deraadt 638:
639: for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
1.3 millert 640: if (bp == NULL || curwp->w_bufp == bp)
641: wp->w_flag |= WFMODE;
1.1 deraadt 642: }