[BACK]Return to file.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/file.c, Revision 1.36

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