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

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