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

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