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

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