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

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