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

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