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

1.52    ! deraadt     1: /*     $OpenBSD: file.c,v 1.51 2006/04/03 00:40:56 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];
        !           186:        unsigned int    count;
        !           187:        size_t          i, remain;
1.1       deraadt   188:
1.44      kjell     189:        if (strlcpy(fname, fn, sizeof(fname)) >= sizeof(fname)) {
                    190:                ewprintf("filename too long");
                    191:                return (NULL);
                    192:        }
                    193:
1.3       millert   194:        for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1.6       art       195:                if (strcmp(bp->b_fname, fname) == 0)
1.32      db        196:                        return (bp);
1.1       deraadt   197:        }
1.14      vincent   198:        i = strlcpy(bname, basename(fname), sizeof(bname));
1.33      beck      199:        if (i >= sizeof(bname))
1.44      kjell     200:                return (NULL);
1.14      vincent   201:        remain = sizeof(bname) - i;
                    202:        for (count = 2; bfind(bname, FALSE) != NULL; count++)
                    203:                snprintf(&bname[i], remain, "<%d>", count);
1.10      vincent   204:
1.32      db        205:        return (bfind(bname, TRUE));
1.1       deraadt   206: }
                    207:
                    208: /*
1.9       mickey    209:  * Read the file "fname" into the current buffer.  Make all of the text
                    210:  * in the buffer go away, after checking for unsaved changes.  This is
                    211:  * called by the "read" command, the "visit" command, and the mainline
1.19      vincent   212:  * (for "mg file").
1.1       deraadt   213:  */
1.3       millert   214: int
1.19      vincent   215: readin(char *fname)
1.3       millert   216: {
1.45      deraadt   217:        struct mgwin    *wp;
1.41      kjell     218:        int      status, i, ro = FALSE;
1.20      vincent   219:        PF      *ael;
1.1       deraadt   220:
1.4       millert   221:        /* might be old */
                    222:        if (bclear(curbp) != TRUE)
1.32      db        223:                return (TRUE);
1.30      jfb       224:        if ((status = insertfile(fname, fname, TRUE)) != TRUE) {
                    225:                ewprintf("File is not readable: %s", fname);
1.32      db        226:                return (FALSE);
1.30      jfb       227:        }
1.18      vincent   228:
                    229:        /*
                    230:         * Call auto-executing function if we need to.
                    231:         */
                    232:        if ((ael = find_autoexec(fname)) != NULL) {
                    233:                for (i = 0; ael[i] != NULL; i++)
                    234:                        (*ael[i])(0, 1);
                    235:                free(ael);
                    236:        }
1.4       millert   237:
                    238:        /* no change */
                    239:        curbp->b_flag &= ~BFCHG;
1.3       millert   240:        for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1       deraadt   241:                if (wp->w_bufp == curbp) {
1.3       millert   242:                        wp->w_dotp = wp->w_linep = lforw(curbp->b_linep);
                    243:                        wp->w_doto = 0;
1.1       deraadt   244:                        wp->w_markp = NULL;
                    245:                        wp->w_marko = 0;
                    246:                }
                    247:        }
1.16      vincent   248:
1.41      kjell     249:        /*
                    250:         * We need to set the READONLY flag after we insert the file,
                    251:         * unless the file is a directory.
                    252:         */
1.16      vincent   253:        if (access(fname, W_OK) && errno != ENOENT)
1.41      kjell     254:                ro = TRUE;
                    255:        if (fisdir(fname) == TRUE)
                    256:                ro = TRUE;
                    257:        if (ro == TRUE)
1.16      vincent   258:                curbp->b_flag |= BFREADONLY;
                    259:        else
                    260:                curbp->b_flag &=~ BFREADONLY;
1.25      deraadt   261:
1.24      deraadt   262:        if (startrow)
                    263:                gotoline(FFARG, startrow);
1.16      vincent   264:
1.32      db        265:        return (status);
1.1       deraadt   266: }
1.4       millert   267:
1.1       deraadt   268: /*
                    269:  * NB, getting file attributes is done here under control of a flag
                    270:  * rather than in readin, which would be cleaner.  I was concerned
                    271:  * that some operating system might require the file to be open
                    272:  * in order to get the information.  Similarly for writing.
                    273:  */
                    274:
                    275: /*
1.39      kjell     276:  * Insert a file in the current buffer, after dot. If file is a directory,
                    277:  * and 'replacebuf' is TRUE, invoke dired mode, else die with an error.
                    278:  * If file is a regular file, set mark at the end of the text inserted;
1.47      kjell     279:  * point at the beginning.  Return a standard status. Print a summary
                    280:  * (lines read, error message) out as well. This routine also does the
                    281:  * read end of backup processing.  The BFBAK flag, if set in a buffer,
                    282:  * says that a backup should be taken.  It is set when a file is read in,
                    283:  * but not on a new file. You don't need to make a backup copy of nothing.
1.1       deraadt   284:  */
1.47      kjell     285:
1.9       mickey    286: static char    *line = NULL;
                    287: static int     linesize = 0;
1.1       deraadt   288:
1.3       millert   289: int
1.39      kjell     290: insertfile(char *fname, char *newname, int replacebuf)
1.3       millert   291: {
1.45      deraadt   292:        struct buffer   *bp;
                    293:        struct line     *lp1, *lp2;
                    294:        struct line     *olp;                   /* line we started at */
                    295:        struct mgwin    *wp;
1.23      vincent   296:        int      nbytes, s, nline, siz, x = -1, x2;
1.19      vincent   297:        int      opos;                  /* offset we started at */
1.4       millert   298:
1.39      kjell     299:        if (replacebuf == TRUE)
1.23      vincent   300:                x = undo_enable(FALSE);
                    301:
1.4       millert   302:        lp1 = NULL;
1.1       deraadt   303:        if (line == NULL) {
                    304:                line = malloc(NLINE);
1.21      vincent   305:                if (line == NULL)
                    306:                        panic("out of memory");
1.1       deraadt   307:                linesize = NLINE;
                    308:        }
1.4       millert   309:
                    310:        /* cheap */
                    311:        bp = curbp;
1.8       art       312:        if (newname != NULL)
1.10      vincent   313:                (void)strlcpy(bp->b_fname, newname, sizeof bp->b_fname);
1.4       millert   314:
                    315:        /* hard file open */
1.39      kjell     316:        if ((s = ffropen(fname, (replacebuf == TRUE) ? bp : NULL)) == FIOERR)
1.1       deraadt   317:                goto out;
1.4       millert   318:        if (s == FIOFNF) {
                    319:                /* file not found */
1.1       deraadt   320:                if (newname != NULL)
                    321:                        ewprintf("(New file)");
1.3       millert   322:                else
                    323:                        ewprintf("(File not found)");
1.1       deraadt   324:                goto out;
1.40      kjell     325:        } else if (s == FIODIR) {
                    326:                /* file was a directory */
                    327:                if (replacebuf == FALSE) {
                    328:                        ewprintf("Cannot insert: file is a directory, %s",
                    329:                            fname);
                    330:                        goto cleanup;
                    331:                }
                    332:                killbuffer(bp);
                    333:                if ((bp = dired_(fname)) == NULL)
                    334:                        return (FALSE);
                    335:                undo_enable(x);
                    336:                curbp = bp;
                    337:                return (showbuffer(bp, curwp, WFHARD | WFMODE));
1.1       deraadt   338:        }
                    339:        opos = curwp->w_doto;
1.4       millert   340:
                    341:        /* open a new line, at point, and start inserting after it */
1.23      vincent   342:        x2 = undo_enable(FALSE);
1.20      vincent   343:        (void)lnewline();
1.1       deraadt   344:        olp = lback(curwp->w_dotp);
1.3       millert   345:        if (olp == curbp->b_linep) {
1.1       deraadt   346:                /* if at end of buffer, create a line to insert before */
1.7       art       347:                (void)lnewline();
1.1       deraadt   348:                curwp->w_dotp = lback(curwp->w_dotp);
                    349:        }
1.23      vincent   350:        undo_enable(x2);
1.4       millert   351:
                    352:        /* don't count fake lines at the end */
                    353:        nline = 0;
1.19      vincent   354:        siz = 0;
1.3       millert   355:        while ((s = ffgetline(line, linesize, &nbytes)) != FIOERR) {
1.1       deraadt   356: doneread:
1.19      vincent   357:                siz += nbytes + 1;
1.3       millert   358:                switch (s) {
                    359:                case FIOSUC:
                    360:                        ++nline;
1.48      kjell     361:                        /* FALLTHRU */
1.4       millert   362:                case FIOEOF:
                    363:                        /* the last line of the file */
1.3       millert   364:                        if ((lp1 = lalloc(nbytes)) == NULL) {
1.4       millert   365:                                /* keep message on the display */
                    366:                                s = FIOERR;
                    367:                                goto endoffile;
1.3       millert   368:                        }
                    369:                        bcopy(line, &ltext(lp1)[0], nbytes);
                    370:                        lp2 = lback(curwp->w_dotp);
                    371:                        lp2->l_fp = lp1;
                    372:                        lp1->l_fp = curwp->w_dotp;
                    373:                        lp1->l_bp = lp2;
                    374:                        curwp->w_dotp->l_bp = lp1;
                    375:                        if (s == FIOEOF)
                    376:                                goto endoffile;
                    377:                        break;
1.19      vincent   378:                case FIOLONG: {
1.4       millert   379:                                /* a line too long to fit in our buffer */
1.9       mickey    380:                                char    *cp;
                    381:                                int     newsize;
1.3       millert   382:
                    383:                                newsize = linesize * 2;
                    384:                                if (newsize < 0 ||
1.51      deraadt   385:                                    (cp = malloc(newsize)) == NULL) {
1.3       millert   386:                                        ewprintf("Could not allocate %d bytes",
1.4       millert   387:                                            newsize);
                    388:                                                s = FIOERR;
                    389:                                                goto endoffile;
1.3       millert   390:                                }
                    391:                                bcopy(line, cp, linesize);
                    392:                                free(line);
                    393:                                line = cp;
1.9       mickey    394:                                s = ffgetline(line + linesize, linesize,
1.4       millert   395:                                    &nbytes);
1.3       millert   396:                                nbytes += linesize;
                    397:                                linesize = newsize;
                    398:                                if (s == FIOERR)
                    399:                                        goto endoffile;
                    400:                                goto doneread;
                    401:                        }
                    402:                default:
                    403:                        ewprintf("Unknown code %d reading file", s);
                    404:                        s = FIOERR;
                    405:                        break;
1.1       deraadt   406:                }
                    407:        }
                    408: endoffile:
1.28      vincent   409:        undo_add_insert(olp, opos, siz-1);
1.19      vincent   410:
1.4       millert   411:        /* ignore errors */
1.8       art       412:        ffclose(NULL);
1.4       millert   413:        /* don't zap an error */
                    414:        if (s == FIOEOF) {
1.3       millert   415:                if (nline == 1)
                    416:                        ewprintf("(Read 1 line)");
                    417:                else
                    418:                        ewprintf("(Read %d lines)", nline);
1.1       deraadt   419:        }
1.4       millert   420:        /* set mark at the end of the text */
1.1       deraadt   421:        curwp->w_dotp = curwp->w_markp = lback(curwp->w_dotp);
                    422:        curwp->w_marko = llength(curwp->w_markp);
1.7       art       423:        (void)ldelnewline();
1.1       deraadt   424:        curwp->w_dotp = olp;
                    425:        curwp->w_doto = opos;
1.3       millert   426:        if (olp == curbp->b_linep)
                    427:                curwp->w_dotp = lforw(olp);
1.1       deraadt   428:        if (newname != NULL)
1.3       millert   429:                bp->b_flag |= BFCHG | BFBAK;    /* Need a backup.        */
                    430:        else
                    431:                bp->b_flag |= BFCHG;
                    432:        /*
1.32      db        433:         * If the insert was at the end of buffer, set lp1 to the end of
1.3       millert   434:         * buffer line, and lp2 to the beginning of the newly inserted text.
                    435:         * (Otherwise lp2 is set to NULL.)  This is used below to set
                    436:         * pointers in other windows correctly if they are also at the end of
                    437:         * buffer.
1.1       deraadt   438:         */
                    439:        lp1 = bp->b_linep;
                    440:        if (curwp->w_markp == lp1) {
                    441:                lp2 = curwp->w_dotp;
                    442:        } else {
1.4       millert   443:                /* delete extraneous newline */
1.7       art       444:                (void)ldelnewline();
1.1       deraadt   445: out:           lp2 = NULL;
                    446:        }
1.3       millert   447:        for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1       deraadt   448:                if (wp->w_bufp == curbp) {
1.3       millert   449:                        wp->w_flag |= WFMODE | WFEDIT;
1.1       deraadt   450:                        if (wp != curwp && lp2 != NULL) {
1.3       millert   451:                                if (wp->w_dotp == lp1)
                    452:                                        wp->w_dotp = lp2;
                    453:                                if (wp->w_markp == lp1)
                    454:                                        wp->w_markp = lp2;
                    455:                                if (wp->w_linep == lp1)
                    456:                                        wp->w_linep = lp2;
1.1       deraadt   457:                        }
                    458:                }
                    459:        }
1.40      kjell     460: cleanup:
1.39      kjell     461:        undo_enable(x);
1.23      vincent   462:
1.39      kjell     463:        /* return FALSE if error */
1.32      db        464:        return (s != FIOERR);
1.1       deraadt   465: }
                    466:
                    467: /*
1.9       mickey    468:  * Ask for a file name and write the contents of the current buffer to that
                    469:  * file.  Update the remembered file name and clear the buffer changed flag.
1.4       millert   470:  * This handling of file names is different from the earlier versions and
1.27      jmc       471:  * is more compatible with Gosling EMACS than with ITS EMACS.
1.1       deraadt   472:  */
1.3       millert   473: /* ARGSUSED */
                    474: int
1.20      vincent   475: filewrite(int f, int n)
1.1       deraadt   476: {
1.4       millert   477:        int      s;
                    478:        char     fname[NFILEN];
1.29      vincent   479:        char    *adjfname, *p, *bufp;
                    480:
                    481:        if ((bufp = eread("Write file: ", fname, NFILEN,
                    482:            EFNEW | EFCR | EFFILE)) == NULL)
1.32      db        483:                return (ABORT);
1.29      vincent   484:        else if (bufp[0] == '\0')
1.32      db        485:                return (FALSE);
1.1       deraadt   486:
                    487:        adjfname = adjustname(fname);
1.22      vincent   488:        if (adjfname == NULL)
                    489:                return (FALSE);
1.1       deraadt   490:        /* old attributes are no longer current */
                    491:        bzero(&curbp->b_fi, sizeof(curbp->b_fi));
1.3       millert   492:        if ((s = writeout(curbp, adjfname)) == TRUE) {
1.10      vincent   493:                (void)strlcpy(curbp->b_fname, adjfname, sizeof curbp->b_fname);
1.26      vincent   494:                p = strrchr(curbp->b_fname, '/');
                    495:                if (p)
                    496:                        p++;
                    497:                else
                    498:                        p = curbp->b_fname;
1.50      kjell     499:                free(curbp->b_bname);
1.26      vincent   500:                curbp->b_bname = strdup(p);
1.1       deraadt   501:                curbp->b_flag &= ~(BFBAK | BFCHG);
                    502:                upmodes(curbp);
                    503:        }
1.32      db        504:        return (s);
1.1       deraadt   505: }
                    506:
                    507: /*
1.4       millert   508:  * Save the contents of the current buffer back into its associated file.
1.1       deraadt   509:  */
                    510: #ifndef        MAKEBACKUP
                    511: #define        MAKEBACKUP TRUE
1.4       millert   512: #endif /* !MAKEBACKUP */
1.9       mickey    513: static int     makebackup = MAKEBACKUP;
1.1       deraadt   514:
1.3       millert   515: /* ARGSUSED */
                    516: int
1.20      vincent   517: filesave(int f, int n)
1.1       deraadt   518: {
1.32      db        519:        return (buffsave(curbp));
1.1       deraadt   520: }
                    521:
                    522: /*
1.9       mickey    523:  * Save the contents of the buffer argument into its associated file.  Do
                    524:  * nothing if there have been no changes (is this a bug, or a feature?).
                    525:  * Error if there is no remembered file name. If this is the first write
1.4       millert   526:  * since the read or visit, then a backup copy of the file is made.
1.9       mickey    527:  * Allow user to select whether or not to make backup files by looking at
1.4       millert   528:  * the value of makebackup.
1.1       deraadt   529:  */
1.3       millert   530: int
1.45      deraadt   531: buffsave(struct buffer *bp)
1.3       millert   532: {
1.4       millert   533:        int      s;
1.1       deraadt   534:
1.4       millert   535:        /* return, no changes */
                    536:        if ((bp->b_flag & BFCHG) == 0) {
1.1       deraadt   537:                ewprintf("(No changes need to be saved)");
1.32      db        538:                return (TRUE);
1.1       deraadt   539:        }
1.4       millert   540:
                    541:        /* must have a name */
                    542:        if (bp->b_fname[0] == '\0') {
1.1       deraadt   543:                ewprintf("No file name");
                    544:                return (FALSE);
                    545:        }
1.4       millert   546:
1.3       millert   547:        if (makebackup && (bp->b_flag & BFBAK)) {
1.1       deraadt   548:                s = fbackupfile(bp->b_fname);
1.4       millert   549:                /* hard error */
                    550:                if (s == ABORT)
1.32      db        551:                        return (FALSE);
1.4       millert   552:                /* softer error */
1.9       mickey    553:                if (s == FALSE &&
1.4       millert   554:                    (s = eyesno("Backup error, save anyway")) != TRUE)
1.32      db        555:                        return (s);
1.1       deraadt   556:        }
1.3       millert   557:        if ((s = writeout(bp, bp->b_fname)) == TRUE) {
1.1       deraadt   558:                bp->b_flag &= ~(BFCHG | BFBAK);
                    559:                upmodes(bp);
                    560:        }
1.32      db        561:        return (s);
1.1       deraadt   562: }
                    563:
1.3       millert   564: /*
                    565:  * Since we don't have variables (we probably should) this is a command
                    566:  * processor for changing the value of the make backup flag.  If no argument
                    567:  * is given, sets makebackup to true, so backups are made.  If an argument is
                    568:  * given, no backup files are made when saving a new version of a file. Only
                    569:  * used when BACKUP is #defined.
1.1       deraadt   570:  */
1.3       millert   571: /* ARGSUSED */
                    572: int
1.20      vincent   573: makebkfile(int f, int n)
1.1       deraadt   574: {
1.3       millert   575:        if (f & FFARG)
                    576:                makebackup = n > 0;
                    577:        else
                    578:                makebackup = !makebackup;
1.1       deraadt   579:        ewprintf("Backup files %sabled", makebackup ? "en" : "dis");
1.32      db        580:        return (TRUE);
1.1       deraadt   581: }
                    582:
                    583: /*
                    584:  * NB: bp is passed to both ffwopen and ffclose because some
                    585:  * attribute information may need to be updated at open time
                    586:  * and others after the close.  This is OS-dependent.  Note
                    587:  * that the ff routines are assumed to be able to tell whether
                    588:  * the attribute information has been set up in this buffer
                    589:  * or not.
                    590:  */
                    591:
                    592: /*
1.9       mickey    593:  * This function performs the details of file writing; writing the file
                    594:  * in buffer bp to file fn. Uses the file management routines in the
1.4       millert   595:  * "fileio.c" package. Most of the grief is checking of some sort.
1.1       deraadt   596:  */
1.3       millert   597: int
1.45      deraadt   598: writeout(struct buffer *bp, char *fn)
1.3       millert   599: {
1.4       millert   600:        int      s;
1.1       deraadt   601:
1.4       millert   602:        /* open writes message */
                    603:        if ((s = ffwopen(fn, bp)) != FIOSUC)
1.1       deraadt   604:                return (FALSE);
                    605:        s = ffputbuf(bp);
1.4       millert   606:        if (s == FIOSUC) {
                    607:                /* no write error */
1.1       deraadt   608:                s = ffclose(bp);
1.3       millert   609:                if (s == FIOSUC)
1.1       deraadt   610:                        ewprintf("Wrote %s", fn);
1.4       millert   611:        } else
                    612:                /* ignore close error if it is a write error */
1.7       art       613:                (void)ffclose(bp);
1.32      db        614:        return (s == FIOSUC);
1.1       deraadt   615: }
                    616:
                    617: /*
1.4       millert   618:  * Tag all windows for bp (all windows if bp == NULL) as needing their
1.1       deraadt   619:  * mode line updated.
                    620:  */
1.7       art       621: void
1.45      deraadt   622: upmodes(struct buffer *bp)
1.3       millert   623: {
1.45      deraadt   624:        struct mgwin    *wp;
1.1       deraadt   625:
                    626:        for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
1.3       millert   627:                if (bp == NULL || curwp->w_bufp == bp)
                    628:                        wp->w_flag |= WFMODE;
1.1       deraadt   629: }