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

1.39    ! kjell       1: /*     $OpenBSD: file.c,v 1.38 2005/08/09 00:53:48 kjell Exp $ */
1.37      kjell       2:
                      3: /* This file is in the public domain. */
1.5       niklas      4:
1.1       deraadt     5: /*
1.4       millert     6:  *     File commands.
1.1       deraadt     7:  */
                      8:
1.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;
1.39    ! kjell      45:        int      status;
1.34      cloder     46:
                     47:        if (curbp->b_fname && curbp->b_fname[0] != '\0') {
                     48:                strlcpy(fname, curbp->b_fname, sizeof(fname));
                     49:                if ((slash = strrchr(fname, '/')) != NULL) {
                     50:                        *(slash + 1) = '\0';
                     51:                }
                     52:        }
                     53:        else
                     54:                fname[0] = '\0';
1.1       deraadt    55:
1.38      kjell      56:        bufp = eread("Find file: ", fname, NFILEN,
                     57:            EFNEW | EFCR | EFFILE | EFDEF);
1.29      vincent    58:        if (bufp == NULL)
1.32      db         59:                return (ABORT);
1.29      vincent    60:        else if (bufp[0] == '\0')
1.32      db         61:                return (FALSE);
1.1       deraadt    62:        adjf = adjustname(fname);
1.22      vincent    63:        if (adjf == NULL)
                     64:                return (FALSE);
1.3       millert    65:        if ((bp = findbuffer(adjf)) == NULL)
1.32      db         66:                return (FALSE);
1.1       deraadt    67:        curbp = bp;
1.3       millert    68:        if (showbuffer(bp, curwp, WFHARD) != TRUE)
1.32      db         69:                return (FALSE);
1.39    ! kjell      70:        if (bp->b_fname[0] == '\0') {
1.30      jfb        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.39    ! kjell     149:        int      status;
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.39    ! kjell     165:        if (bp->b_fname[0] == '\0') {
1.30      jfb       166:                if ((status = readin(adjf)) != TRUE)
                    167:                        killbuffer(bp);
1.32      db        168:                return (status);
1.30      jfb       169:        }
1.32      db        170:        return (TRUE);
1.1       deraadt   171: }
                    172:
                    173: /*
1.32      db        174:  * Given a file name, either find the buffer it uses, or create a new
1.1       deraadt   175:  * empty buffer to put it in.
                    176:  */
                    177: BUFFER *
1.14      vincent   178: findbuffer(char *fname)
1.1       deraadt   179: {
1.4       millert   180:        BUFFER          *bp;
1.10      vincent   181:        char             bname[NBUFN];
1.20      vincent   182:        unsigned int     count, remain, i;
1.1       deraadt   183:
1.3       millert   184:        for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1.6       art       185:                if (strcmp(bp->b_fname, fname) == 0)
1.32      db        186:                        return (bp);
1.1       deraadt   187:        }
1.14      vincent   188:        i = strlcpy(bname, basename(fname), sizeof(bname));
1.33      beck      189:        if (i >= sizeof(bname))
                    190:                return NULL;
1.14      vincent   191:        remain = sizeof(bname) - i;
                    192:        for (count = 2; bfind(bname, FALSE) != NULL; count++)
                    193:                snprintf(&bname[i], remain, "<%d>", count);
1.10      vincent   194:
1.32      db        195:        return (bfind(bname, TRUE));
1.1       deraadt   196: }
                    197:
                    198: /*
1.9       mickey    199:  * Read the file "fname" into the current buffer.  Make all of the text
                    200:  * in the buffer go away, after checking for unsaved changes.  This is
                    201:  * called by the "read" command, the "visit" command, and the mainline
1.19      vincent   202:  * (for "mg file").
1.1       deraadt   203:  */
1.3       millert   204: int
1.19      vincent   205: readin(char *fname)
1.3       millert   206: {
1.4       millert   207:        MGWIN   *wp;
1.18      vincent   208:        int      status, i;
1.20      vincent   209:        PF      *ael;
1.1       deraadt   210:
1.4       millert   211:        /* might be old */
                    212:        if (bclear(curbp) != TRUE)
1.32      db        213:                return (TRUE);
1.30      jfb       214:        if ((status = insertfile(fname, fname, TRUE)) != TRUE) {
                    215:                ewprintf("File is not readable: %s", fname);
1.32      db        216:                return (FALSE);
1.30      jfb       217:        }
1.18      vincent   218:
                    219:        /*
                    220:         * Call auto-executing function if we need to.
                    221:         */
                    222:        if ((ael = find_autoexec(fname)) != NULL) {
                    223:                for (i = 0; ael[i] != NULL; i++)
                    224:                        (*ael[i])(0, 1);
                    225:                free(ael);
                    226:        }
1.4       millert   227:
                    228:        /* no change */
                    229:        curbp->b_flag &= ~BFCHG;
1.3       millert   230:        for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1       deraadt   231:                if (wp->w_bufp == curbp) {
1.3       millert   232:                        wp->w_dotp = wp->w_linep = lforw(curbp->b_linep);
                    233:                        wp->w_doto = 0;
1.1       deraadt   234:                        wp->w_markp = NULL;
                    235:                        wp->w_marko = 0;
                    236:                }
                    237:        }
1.16      vincent   238:
1.32      db        239:        /* We need to set the READONLY flag after we insert the file. */
1.16      vincent   240:        if (access(fname, W_OK) && errno != ENOENT)
                    241:                curbp->b_flag |= BFREADONLY;
                    242:        else
                    243:                curbp->b_flag &=~ BFREADONLY;
1.25      deraadt   244:
1.24      deraadt   245:        if (startrow)
                    246:                gotoline(FFARG, startrow);
1.16      vincent   247:
1.32      db        248:        return (status);
1.1       deraadt   249: }
1.4       millert   250:
1.1       deraadt   251: /*
                    252:  * NB, getting file attributes is done here under control of a flag
                    253:  * rather than in readin, which would be cleaner.  I was concerned
                    254:  * that some operating system might require the file to be open
                    255:  * in order to get the information.  Similarly for writing.
                    256:  */
                    257:
                    258: /*
1.39    ! kjell     259:  * Insert a file in the current buffer, after dot. If file is a directory,
        !           260:  * and 'replacebuf' is TRUE, invoke dired mode, else die with an error.
        !           261:  * If file is a regular file, set mark at the end of the text inserted;
        !           262:  * 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.39    ! kjell     273: insertfile(char *fname, char *newname, int replacebuf)
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.39    ! kjell     282:        if (replacebuf == TRUE)
1.23      vincent   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.39    ! kjell     299:        if ((s = ffropen(fname, (replacebuf == TRUE) ? 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.39    ! kjell     434:        undo_enable(x);
1.23      vincent   435:
1.39    ! kjell     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: }