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

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