Annotation of src/usr.bin/mg/file.c, Revision 1.45
1.45 ! deraadt 1: /* $OpenBSD: file.c,v 1.44 2005/11/13 07:24:48 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.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: #ifndef NO_DIRED
253: if (fisdir(fname) == TRUE)
254: ro = TRUE;
255: #endif
256: if (ro == TRUE)
1.16 vincent 257: curbp->b_flag |= BFREADONLY;
258: else
259: curbp->b_flag &=~ BFREADONLY;
1.25 deraadt 260:
1.24 deraadt 261: if (startrow)
262: gotoline(FFARG, startrow);
1.16 vincent 263:
1.32 db 264: return (status);
1.1 deraadt 265: }
1.4 millert 266:
1.1 deraadt 267: /*
268: * NB, getting file attributes is done here under control of a flag
269: * rather than in readin, which would be cleaner. I was concerned
270: * that some operating system might require the file to be open
271: * in order to get the information. Similarly for writing.
272: */
273:
274: /*
1.39 kjell 275: * Insert a file in the current buffer, after dot. If file is a directory,
276: * and 'replacebuf' is TRUE, invoke dired mode, else die with an error.
277: * If file is a regular file, set mark at the end of the text inserted;
278: * point at the beginning. Return a standard status.
1.31 henning 279: * Print a summary (lines read, error message) out as well. Unless the
1.32 db 280: * NO_BACKUP conditional is set, this routine also does the read end of
1.31 henning 281: * backup processing. The BFBAK flag, if set in a buffer, says that a
282: * backup should be taken. It is set when a file is read in, but not on
283: * a new file. (You don't need to make a backup copy of nothing.)
1.1 deraadt 284: */
1.9 mickey 285: static char *line = NULL;
286: static int linesize = 0;
1.1 deraadt 287:
1.3 millert 288: int
1.39 kjell 289: insertfile(char *fname, char *newname, int replacebuf)
1.3 millert 290: {
1.45 ! deraadt 291: struct buffer *bp;
! 292: struct line *lp1, *lp2;
! 293: struct line *olp; /* line we started at */
! 294: struct mgwin *wp;
1.23 vincent 295: int nbytes, s, nline, siz, x = -1, x2;
1.19 vincent 296: int opos; /* offset we started at */
1.4 millert 297:
1.39 kjell 298: if (replacebuf == TRUE)
1.23 vincent 299: x = undo_enable(FALSE);
300:
1.4 millert 301: lp1 = NULL;
1.1 deraadt 302: if (line == NULL) {
303: line = malloc(NLINE);
1.21 vincent 304: if (line == NULL)
305: panic("out of memory");
1.1 deraadt 306: linesize = NLINE;
307: }
1.4 millert 308:
309: /* cheap */
310: bp = curbp;
1.8 art 311: if (newname != NULL)
1.10 vincent 312: (void)strlcpy(bp->b_fname, newname, sizeof bp->b_fname);
1.4 millert 313:
314: /* hard file open */
1.39 kjell 315: if ((s = ffropen(fname, (replacebuf == TRUE) ? bp : NULL)) == FIOERR)
1.1 deraadt 316: goto out;
1.4 millert 317: if (s == FIOFNF) {
318: /* file not found */
1.1 deraadt 319: if (newname != NULL)
320: ewprintf("(New file)");
1.3 millert 321: else
322: ewprintf("(File not found)");
1.1 deraadt 323: goto out;
1.40 kjell 324: #ifndef NO_DIRED
325: } else if (s == FIODIR) {
326: /* file was a directory */
327: if (replacebuf == FALSE) {
328: ewprintf("Cannot insert: file is a directory, %s",
329: fname);
330: goto cleanup;
331: }
332: killbuffer(bp);
333: if ((bp = dired_(fname)) == NULL)
334: return (FALSE);
335: undo_enable(x);
336: curbp = bp;
337: return (showbuffer(bp, curwp, WFHARD | WFMODE));
338: #endif /* !NO_DIRED */
1.1 deraadt 339: }
340: opos = curwp->w_doto;
1.4 millert 341:
342: /* open a new line, at point, and start inserting after it */
1.23 vincent 343: x2 = undo_enable(FALSE);
1.20 vincent 344: (void)lnewline();
1.1 deraadt 345: olp = lback(curwp->w_dotp);
1.3 millert 346: if (olp == curbp->b_linep) {
1.1 deraadt 347: /* if at end of buffer, create a line to insert before */
1.7 art 348: (void)lnewline();
1.1 deraadt 349: curwp->w_dotp = lback(curwp->w_dotp);
350: }
1.23 vincent 351: undo_enable(x2);
1.4 millert 352:
353: /* don't count fake lines at the end */
354: nline = 0;
1.19 vincent 355: siz = 0;
1.3 millert 356: while ((s = ffgetline(line, linesize, &nbytes)) != FIOERR) {
1.1 deraadt 357: doneread:
1.19 vincent 358: siz += nbytes + 1;
1.3 millert 359: switch (s) {
360: case FIOSUC:
361: ++nline;
362: /* and continue */
1.4 millert 363: case FIOEOF:
364: /* the last line of the file */
1.3 millert 365: if ((lp1 = lalloc(nbytes)) == NULL) {
1.4 millert 366: /* keep message on the display */
367: s = FIOERR;
368: goto endoffile;
1.3 millert 369: }
370: bcopy(line, <ext(lp1)[0], nbytes);
371: lp2 = lback(curwp->w_dotp);
372: lp2->l_fp = lp1;
373: lp1->l_fp = curwp->w_dotp;
374: lp1->l_bp = lp2;
375: curwp->w_dotp->l_bp = lp1;
376: if (s == FIOEOF)
377: goto endoffile;
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.4 millert 386: (cp = malloc((unsigned)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;
401: goto doneread;
402: }
403: default:
404: ewprintf("Unknown code %d reading file", s);
405: s = FIOERR;
406: break;
1.1 deraadt 407: }
408: }
409: endoffile:
1.28 vincent 410: undo_add_insert(olp, opos, siz-1);
1.19 vincent 411:
1.4 millert 412: /* ignore errors */
1.8 art 413: ffclose(NULL);
1.4 millert 414: /* don't zap an error */
415: if (s == FIOEOF) {
1.3 millert 416: if (nline == 1)
417: ewprintf("(Read 1 line)");
418: else
419: ewprintf("(Read %d lines)", nline);
1.1 deraadt 420: }
1.4 millert 421: /* set mark at the end of the text */
1.1 deraadt 422: curwp->w_dotp = curwp->w_markp = lback(curwp->w_dotp);
423: curwp->w_marko = llength(curwp->w_markp);
1.7 art 424: (void)ldelnewline();
1.1 deraadt 425: curwp->w_dotp = olp;
426: curwp->w_doto = opos;
1.3 millert 427: if (olp == curbp->b_linep)
428: curwp->w_dotp = lforw(olp);
1.1 deraadt 429: #ifndef NO_BACKUP
430: if (newname != NULL)
1.3 millert 431: bp->b_flag |= BFCHG | BFBAK; /* Need a backup. */
432: else
433: bp->b_flag |= BFCHG;
1.4 millert 434: #else /* !NO_BACKUP */
1.1 deraadt 435: bp->b_flag |= BFCHG;
1.4 millert 436: #endif /* !NO_BACKUP */
1.3 millert 437: /*
1.32 db 438: * If the insert was at the end of buffer, set lp1 to the end of
1.3 millert 439: * buffer line, and lp2 to the beginning of the newly inserted text.
440: * (Otherwise lp2 is set to NULL.) This is used below to set
441: * pointers in other windows correctly if they are also at the end of
442: * buffer.
1.1 deraadt 443: */
444: lp1 = bp->b_linep;
445: if (curwp->w_markp == lp1) {
446: lp2 = curwp->w_dotp;
447: } else {
1.4 millert 448: /* delete extraneous newline */
1.7 art 449: (void)ldelnewline();
1.1 deraadt 450: out: lp2 = NULL;
451: }
1.3 millert 452: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 453: if (wp->w_bufp == curbp) {
1.3 millert 454: wp->w_flag |= WFMODE | WFEDIT;
1.1 deraadt 455: if (wp != curwp && lp2 != NULL) {
1.3 millert 456: if (wp->w_dotp == lp1)
457: wp->w_dotp = lp2;
458: if (wp->w_markp == lp1)
459: wp->w_markp = lp2;
460: if (wp->w_linep == lp1)
461: wp->w_linep = lp2;
1.1 deraadt 462: }
463: }
464: }
1.40 kjell 465: cleanup:
1.39 kjell 466: undo_enable(x);
1.23 vincent 467:
1.39 kjell 468: /* return FALSE if error */
1.32 db 469: return (s != FIOERR);
1.1 deraadt 470: }
471:
472: /*
1.9 mickey 473: * Ask for a file name and write the contents of the current buffer to that
474: * file. Update the remembered file name and clear the buffer changed flag.
1.4 millert 475: * This handling of file names is different from the earlier versions and
1.27 jmc 476: * is more compatible with Gosling EMACS than with ITS EMACS.
1.1 deraadt 477: */
1.3 millert 478: /* ARGSUSED */
479: int
1.20 vincent 480: filewrite(int f, int n)
1.1 deraadt 481: {
1.4 millert 482: int s;
483: char fname[NFILEN];
1.29 vincent 484: char *adjfname, *p, *bufp;
485:
486: if ((bufp = eread("Write file: ", fname, NFILEN,
487: EFNEW | EFCR | EFFILE)) == NULL)
1.32 db 488: return (ABORT);
1.29 vincent 489: else if (bufp[0] == '\0')
1.32 db 490: return (FALSE);
1.1 deraadt 491:
492: adjfname = adjustname(fname);
1.22 vincent 493: if (adjfname == NULL)
494: return (FALSE);
1.1 deraadt 495: /* old attributes are no longer current */
496: bzero(&curbp->b_fi, sizeof(curbp->b_fi));
1.3 millert 497: if ((s = writeout(curbp, adjfname)) == TRUE) {
1.10 vincent 498: (void)strlcpy(curbp->b_fname, adjfname, sizeof curbp->b_fname);
1.26 vincent 499: p = strrchr(curbp->b_fname, '/');
500: if (p)
501: p++;
502: else
503: p = curbp->b_fname;
504: if (curbp->b_bname)
505: free((char *)curbp->b_bname);
506: curbp->b_bname = strdup(p);
1.1 deraadt 507: #ifndef NO_BACKUP
508: curbp->b_flag &= ~(BFBAK | BFCHG);
1.4 millert 509: #else /* !NO_BACKUP */
1.1 deraadt 510: curbp->b_flag &= ~BFCHG;
1.4 millert 511: #endif /* !NO_BACKUP */
1.1 deraadt 512: upmodes(curbp);
513: }
1.32 db 514: return (s);
1.1 deraadt 515: }
516:
517: /*
1.4 millert 518: * Save the contents of the current buffer back into its associated file.
1.1 deraadt 519: */
520: #ifndef NO_BACKUP
521: #ifndef MAKEBACKUP
522: #define MAKEBACKUP TRUE
1.4 millert 523: #endif /* !MAKEBACKUP */
1.9 mickey 524: static int makebackup = MAKEBACKUP;
1.4 millert 525: #endif /* !NO_BACKUP */
1.1 deraadt 526:
1.3 millert 527: /* ARGSUSED */
528: int
1.20 vincent 529: filesave(int f, int n)
1.1 deraadt 530: {
1.32 db 531: return (buffsave(curbp));
1.1 deraadt 532: }
533:
534: /*
1.9 mickey 535: * Save the contents of the buffer argument into its associated file. Do
536: * nothing if there have been no changes (is this a bug, or a feature?).
537: * Error if there is no remembered file name. If this is the first write
1.4 millert 538: * since the read or visit, then a backup copy of the file is made.
1.9 mickey 539: * Allow user to select whether or not to make backup files by looking at
1.4 millert 540: * the value of makebackup.
1.1 deraadt 541: */
1.3 millert 542: int
1.45 ! deraadt 543: buffsave(struct buffer *bp)
1.3 millert 544: {
1.4 millert 545: int s;
1.1 deraadt 546:
1.4 millert 547: /* return, no changes */
548: if ((bp->b_flag & BFCHG) == 0) {
1.1 deraadt 549: ewprintf("(No changes need to be saved)");
1.32 db 550: return (TRUE);
1.1 deraadt 551: }
1.4 millert 552:
553: /* must have a name */
554: if (bp->b_fname[0] == '\0') {
1.1 deraadt 555: ewprintf("No file name");
556: return (FALSE);
557: }
1.4 millert 558:
1.1 deraadt 559: #ifndef NO_BACKUP
1.3 millert 560: if (makebackup && (bp->b_flag & BFBAK)) {
1.1 deraadt 561: s = fbackupfile(bp->b_fname);
1.4 millert 562: /* hard error */
563: if (s == ABORT)
1.32 db 564: return (FALSE);
1.4 millert 565: /* softer error */
1.9 mickey 566: if (s == FALSE &&
1.4 millert 567: (s = eyesno("Backup error, save anyway")) != TRUE)
1.32 db 568: return (s);
1.1 deraadt 569: }
1.4 millert 570: #endif /* !NO_BACKUP */
1.3 millert 571: if ((s = writeout(bp, bp->b_fname)) == TRUE) {
1.1 deraadt 572: #ifndef NO_BACKUP
573: bp->b_flag &= ~(BFCHG | BFBAK);
1.4 millert 574: #else /* !NO_BACKUP */
1.1 deraadt 575: bp->b_flag &= ~BFCHG;
1.4 millert 576: #endif /* !NO_BACKUP */
1.1 deraadt 577: upmodes(bp);
578: }
1.32 db 579: return (s);
1.1 deraadt 580: }
581:
582: #ifndef NO_BACKUP
1.3 millert 583: /*
584: * Since we don't have variables (we probably should) this is a command
585: * processor for changing the value of the make backup flag. If no argument
586: * is given, sets makebackup to true, so backups are made. If an argument is
587: * given, no backup files are made when saving a new version of a file. Only
588: * used when BACKUP is #defined.
1.1 deraadt 589: */
1.3 millert 590: /* ARGSUSED */
591: int
1.20 vincent 592: makebkfile(int f, int n)
1.1 deraadt 593: {
1.3 millert 594: if (f & FFARG)
595: makebackup = n > 0;
596: else
597: makebackup = !makebackup;
1.1 deraadt 598: ewprintf("Backup files %sabled", makebackup ? "en" : "dis");
1.32 db 599: return (TRUE);
1.1 deraadt 600: }
1.4 millert 601: #endif /* !NO_BACKUP */
1.1 deraadt 602:
603: /*
604: * NB: bp is passed to both ffwopen and ffclose because some
605: * attribute information may need to be updated at open time
606: * and others after the close. This is OS-dependent. Note
607: * that the ff routines are assumed to be able to tell whether
608: * the attribute information has been set up in this buffer
609: * or not.
610: */
611:
612: /*
1.9 mickey 613: * This function performs the details of file writing; writing the file
614: * in buffer bp to file fn. Uses the file management routines in the
1.4 millert 615: * "fileio.c" package. Most of the grief is checking of some sort.
1.1 deraadt 616: */
1.3 millert 617: int
1.45 ! deraadt 618: writeout(struct buffer *bp, char *fn)
1.3 millert 619: {
1.4 millert 620: int s;
1.1 deraadt 621:
1.4 millert 622: /* open writes message */
623: if ((s = ffwopen(fn, bp)) != FIOSUC)
1.1 deraadt 624: return (FALSE);
625: s = ffputbuf(bp);
1.4 millert 626: if (s == FIOSUC) {
627: /* no write error */
1.1 deraadt 628: s = ffclose(bp);
1.3 millert 629: if (s == FIOSUC)
1.1 deraadt 630: ewprintf("Wrote %s", fn);
1.4 millert 631: } else
632: /* ignore close error if it is a write error */
1.7 art 633: (void)ffclose(bp);
1.32 db 634: return (s == FIOSUC);
1.1 deraadt 635: }
636:
637: /*
1.4 millert 638: * Tag all windows for bp (all windows if bp == NULL) as needing their
1.1 deraadt 639: * mode line updated.
640: */
1.7 art 641: void
1.45 ! deraadt 642: upmodes(struct buffer *bp)
1.3 millert 643: {
1.45 ! deraadt 644: struct mgwin *wp;
1.1 deraadt 645:
646: for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
1.3 millert 647: if (bp == NULL || curwp->w_bufp == bp)
648: wp->w_flag |= WFMODE;
1.1 deraadt 649: }