[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.38

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