[BACK]Return to buffer.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/buffer.c, Revision 1.89

1.89    ! florian     1: /*     $OpenBSD: buffer.c,v 1.88 2012/12/27 18:51:52 florian Exp $     */
1.43      kjell       2:
                      3: /* This file is in the public domain. */
1.5       niklas      4:
1.1       deraadt     5: /*
                      6:  *             Buffer handling.
                      7:  */
1.4       millert     8:
1.76      kjell       9: #include "def.h"
                     10: #include "kbd.h"               /* needed for modes */
                     11:
1.56      kjell      12: #include <libgen.h>
                     13: #include <stdarg.h>
1.1       deraadt    14:
1.88      florian    15: #ifndef DIFFTOOL
                     16: #define DIFFTOOL "/usr/bin/diff"
                     17: #endif /* !DIFFTOOL */
                     18:
1.52      deraadt    19: static struct buffer  *makelist(void);
1.65      kjell      20: static struct buffer *bnew(const char *);
1.75      lum        21:
                     22: static int usebufname(const char *);
1.1       deraadt    23:
1.67      kjell      24: /* Flag for global working dir */
                     25: extern int globalwd;
                     26:
1.50      kjell      27: /* ARGSUSED */
1.27      vincent    28: int
1.31      vincent    29: togglereadonly(int f, int n)
1.27      vincent    30: {
1.68      kjell      31:        int s;
                     32:
                     33:        if ((s = checkdirty(curbp)) != TRUE)
                     34:                return (s);
1.47      kjell      35:        if (!(curbp->b_flag & BFREADONLY))
1.27      vincent    36:                curbp->b_flag |= BFREADONLY;
1.47      kjell      37:        else {
1.68      kjell      38:                curbp->b_flag &= ~BFREADONLY;
1.27      vincent    39:                if (curbp->b_flag & BFCHG)
                     40:                        ewprintf("Warning: Buffer was modified");
                     41:        }
1.70      kjell      42:        curwp->w_rflag |= WFMODE;
1.27      vincent    43:
1.47      kjell      44:        return (TRUE);
1.27      vincent    45: }
                     46:
1.73      deraadt    47: /* Switch to the named buffer.
                     48:  * If no name supplied, switch to the default (alternate) buffer.
                     49:  */
                     50: int
                     51: usebufname(const char *bufp)
                     52: {
                     53:        struct buffer *bp;
                     54:
                     55:        if (bufp == NULL)
                     56:                return (ABORT);
                     57:        if (bufp[0] == '\0' && curbp->b_altb != NULL)
                     58:                bp = curbp->b_altb;
                     59:        else if ((bp = bfind(bufp, TRUE)) == NULL)
                     60:                return (FALSE);
                     61:
                     62:        /* and put it in current window */
                     63:        curbp = bp;
                     64:        return (showbuffer(bp, curwp, WFFRAME | WFFULL));
                     65: }
                     66:
1.1       deraadt    67: /*
                     68:  * Attach a buffer to a window. The values of dot and mark come
                     69:  * from the buffer if the use count is 0. Otherwise, they come
                     70:  * from some other window.  *scratch* is the default alternate
                     71:  * buffer.
                     72:  */
1.3       millert    73: /* ARGSUSED */
                     74: int
1.26      vincent    75: usebuffer(int f, int n)
1.1       deraadt    76: {
1.34      vincent    77:        char    bufn[NBUFN], *bufp;
1.1       deraadt    78:
                     79:        /* Get buffer to use from user */
1.21      deraadt    80:        if ((curbp->b_altb == NULL) &&
                     81:            ((curbp->b_altb = bfind("*scratch*", TRUE)) == NULL))
1.34      vincent    82:                bufp = eread("Switch to buffer: ", bufn, NBUFN, EFNEW | EFBUF);
1.1       deraadt    83:        else
1.34      vincent    84:                bufp = eread("Switch to buffer: (default %s) ", bufn, NBUFN,
1.55      deraadt    85:                    EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname);
1.1       deraadt    86:
1.72      kjell      87:        return (usebufname(bufp));
1.1       deraadt    88: }
                     89:
                     90: /*
                     91:  * pop to buffer asked for by the user.
                     92:  */
1.3       millert    93: /* ARGSUSED */
                     94: int
1.26      vincent    95: poptobuffer(int f, int n)
1.1       deraadt    96: {
1.52      deraadt    97:        struct buffer *bp;
                     98:        struct mgwin  *wp;
1.34      vincent    99:        char    bufn[NBUFN], *bufp;
1.1       deraadt   100:
                    101:        /* Get buffer to use from user */
1.21      deraadt   102:        if ((curbp->b_altb == NULL) &&
                    103:            ((curbp->b_altb = bfind("*scratch*", TRUE)) == NULL))
1.34      vincent   104:                bufp = eread("Switch to buffer in other window: ", bufn, NBUFN,
1.55      deraadt   105:                    EFNEW | EFBUF);
1.1       deraadt   106:        else
1.34      vincent   107:                bufp = eread("Switch to buffer in other window: (default %s) ",
1.55      deraadt   108:                    bufn, NBUFN, EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname);
1.34      vincent   109:        if (bufp == NULL)
1.37      db        110:                return (ABORT);
1.44      kjell     111:        if (bufp[0] == '\0' && curbp->b_altb != NULL)
1.3       millert   112:                bp = curbp->b_altb;
                    113:        else if ((bp = bfind(bufn, TRUE)) == NULL)
1.37      db        114:                return (FALSE);
1.59      kjell     115:        if (bp == curbp)
                    116:                return (splitwind(f, n));
1.71      kjell     117:        /* and put it in a new, non-ephemeral window */
                    118:        if ((wp = popbuf(bp, WNONE)) == NULL)
1.37      db        119:                return (FALSE);
1.1       deraadt   120:        curbp = bp;
                    121:        curwp = wp;
1.37      db        122:        return (TRUE);
1.1       deraadt   123: }
                    124:
                    125: /*
                    126:  * Dispose of a buffer, by name.
                    127:  * Ask for the name. Look it up (don't get too
                    128:  * upset if it isn't there at all!). Clear the buffer (ask
                    129:  * if the buffer has been changed). Then free the header
1.69      kjell     130:  * line and the buffer header. Bound to "C-X k".
1.1       deraadt   131:  */
1.3       millert   132: /* ARGSUSED */
                    133: int
1.35      jfb       134: killbuffer_cmd(int f, int n)
1.1       deraadt   135: {
1.52      deraadt   136:        struct buffer *bp;
1.34      vincent   137:        char    bufn[NBUFN], *bufp;
1.3       millert   138:
1.40      kjell     139:        if ((bufp = eread("Kill buffer: (default %s) ", bufn, NBUFN,
                    140:            EFNUL | EFNEW | EFBUF, curbp->b_bname)) == NULL)
1.37      db        141:                return (ABORT);
1.44      kjell     142:        else if (bufp[0] == '\0')
1.3       millert   143:                bp = curbp;
                    144:        else if ((bp = bfind(bufn, FALSE)) == NULL)
1.37      db        145:                return (FALSE);
                    146:        return (killbuffer(bp));
1.35      jfb       147: }
                    148:
                    149: int
1.52      deraadt   150: killbuffer(struct buffer *bp)
1.35      jfb       151: {
1.52      deraadt   152:        struct buffer *bp1;
                    153:        struct buffer *bp2;
                    154:        struct mgwin  *wp;
1.41      kjell     155:        int s;
1.48      deraadt   156:        struct undo_rec *rec, *next;
1.3       millert   157:
                    158:        /*
1.37      db        159:         * Find some other buffer to display. Try the alternate buffer,
1.3       millert   160:         * then the first different buffer in the buffer list.  If there's
                    161:         * only one buffer, create buffer *scratch* and make it the alternate
                    162:         * buffer.  Return if *scratch* is only buffer...
1.1       deraadt   163:         */
                    164:        if ((bp1 = bp->b_altb) == NULL) {
                    165:                bp1 = (bp == bheadp) ? bp->b_bufp : bheadp;
                    166:                if (bp1 == NULL) {
                    167:                        /* only one buffer. see if it's *scratch* */
1.3       millert   168:                        if (bp == bfind("*scratch*", FALSE))
1.49      kjell     169:                                return (TRUE);
1.1       deraadt   170:                        /* create *scratch* for alternate buffer */
1.3       millert   171:                        if ((bp1 = bfind("*scratch*", TRUE)) == NULL)
1.37      db        172:                                return (FALSE);
1.1       deraadt   173:                }
                    174:        }
1.41      kjell     175:        if ((s = bclear(bp)) != TRUE)
                    176:                return (s);
1.1       deraadt   177:        for (wp = wheadp; bp->b_nwnd > 0; wp = wp->w_wndp) {
1.3       millert   178:                if (wp->w_bufp == bp) {
                    179:                        bp2 = bp1->b_altb;      /* save alternate buffer */
1.58      kjell     180:                        if (showbuffer(bp1, wp, WFMODE | WFFRAME | WFFULL))
1.3       millert   181:                                bp1->b_altb = bp2;
                    182:                        else
                    183:                                bp1 = bp2;
                    184:                }
                    185:        }
                    186:        if (bp == curbp)
                    187:                curbp = bp1;
1.62      kjell     188:        free(bp->b_headp);                      /* Release header line.  */
1.3       millert   189:        bp2 = NULL;                             /* Find the header.      */
1.1       deraadt   190:        bp1 = bheadp;
                    191:        while (bp1 != bp) {
                    192:                if (bp1->b_altb == bp)
                    193:                        bp1->b_altb = (bp->b_altb == bp1) ? NULL : bp->b_altb;
                    194:                bp2 = bp1;
                    195:                bp1 = bp1->b_bufp;
                    196:        }
1.3       millert   197:        bp1 = bp1->b_bufp;                      /* Next one in chain.    */
                    198:        if (bp2 == NULL)                        /* Unlink it.            */
1.1       deraadt   199:                bheadp = bp1;
                    200:        else
                    201:                bp2->b_bufp = bp1;
1.3       millert   202:        while (bp1 != NULL) {                   /* Finish with altb's    */
1.1       deraadt   203:                if (bp1->b_altb == bp)
                    204:                        bp1->b_altb = (bp->b_altb == bp1) ? NULL : bp->b_altb;
                    205:                bp1 = bp1->b_bufp;
                    206:        }
1.74      oga       207:        rec = TAILQ_FIRST(&bp->b_undo);
                    208:
1.46      kjell     209:        while (rec != NULL) {
1.74      oga       210:                next = TAILQ_NEXT(rec, next);
1.46      kjell     211:                free_undo_record(rec);
                    212:                rec = next;
                    213:        }
                    214:
1.54      kjell     215:        free(bp->b_bname);                      /* Release name block    */
1.3       millert   216:        free(bp);                               /* Release buffer block */
1.37      db        217:        return (TRUE);
1.1       deraadt   218: }
                    219:
                    220: /*
                    221:  * Save some buffers - just call anycb with the arg flag.
                    222:  */
1.3       millert   223: /* ARGSUSED */
                    224: int
1.26      vincent   225: savebuffers(int f, int n)
1.1       deraadt   226: {
1.3       millert   227:        if (anycb(f) == ABORT)
1.37      db        228:                return (ABORT);
                    229:        return (TRUE);
1.1       deraadt   230: }
                    231:
                    232: /*
1.19      art       233:  * Listing buffers.
                    234:  */
                    235: static int listbuf_ncol;
                    236:
                    237: static int     listbuf_goto_buffer(int f, int n);
1.39      jason     238: static int     listbuf_goto_buffer_one(int f, int n);
                    239: static int     listbuf_goto_buffer_helper(int f, int n, int only);
1.19      art       240:
                    241: static PF listbuf_pf[] = {
1.37      db        242:        listbuf_goto_buffer
1.19      art       243: };
1.39      jason     244: static PF listbuf_one[] = {
                    245:        listbuf_goto_buffer_one
                    246: };
                    247:
1.19      art       248:
1.39      jason     249: static struct KEYMAPE (2 + IMAPEXT) listbufmap = {
                    250:        2,
                    251:        2 + IMAPEXT,
1.19      art       252:        rescan,
                    253:        {
1.39      jason     254:                {
1.45      deraadt   255:                        CCHR('M'), CCHR('M'), listbuf_pf, NULL
1.39      jason     256:                },
                    257:                {
1.45      deraadt   258:                        '1', '1', listbuf_one, NULL
1.39      jason     259:                }
1.19      art       260:        }
                    261: };
                    262:
                    263: /*
1.1       deraadt   264:  * Display the buffer list. This is done
                    265:  * in two parts. The "makelist" routine figures out
                    266:  * the text, and puts it in a buffer. "popbuf"
                    267:  * then pops the data onto the screen. Bound to
                    268:  * "C-X C-B".
                    269:  */
1.3       millert   270: /* ARGSUSED */
                    271: int
1.26      vincent   272: listbuffers(int f, int n)
1.1       deraadt   273: {
1.52      deraadt   274:        static int               initialized = 0;
                    275:        struct buffer           *bp;
                    276:        struct mgwin            *wp;
1.1       deraadt   277:
1.19      art       278:        if (!initialized) {
                    279:                maps_add((KEYMAP *)&listbufmap, "listbufmap");
                    280:                initialized = 1;
                    281:        }
                    282:
1.71      kjell     283:        if ((bp = makelist()) == NULL || (wp = popbuf(bp, WNONE)) == NULL)
1.37      db        284:                return (FALSE);
                    285:        wp->w_dotp = bp->b_dotp; /* fix up if window already on screen */
1.1       deraadt   286:        wp->w_doto = bp->b_doto;
1.19      art       287:        bp->b_modes[0] = name_mode("fundamental");
                    288:        bp->b_modes[1] = name_mode("listbufmap");
                    289:        bp->b_nmodes = 1;
                    290:
1.37      db        291:        return (TRUE);
1.1       deraadt   292: }
                    293:
                    294: /*
                    295:  * This routine rebuilds the text for the
1.37      db        296:  * list buffers command. Return pointer
                    297:  * to new list if everything works.
                    298:  * Return NULL if there is an error (if
                    299:  * there is no memory).
1.1       deraadt   300:  */
1.52      deraadt   301: static struct buffer *
1.26      vincent   302: makelist(void)
1.3       millert   303: {
1.52      deraadt   304:        int             w = ncol / 2;
                    305:        struct buffer   *bp, *blp;
                    306:        struct line     *lp;
1.19      art       307:
1.3       millert   308:        if ((blp = bfind("*Buffer List*", TRUE)) == NULL)
1.37      db        309:                return (NULL);
1.3       millert   310:        if (bclear(blp) != TRUE)
1.37      db        311:                return (NULL);
1.3       millert   312:        blp->b_flag &= ~BFCHG;          /* Blow away old.        */
1.28      vincent   313:        blp->b_flag |= BFREADONLY;
1.1       deraadt   314:
1.19      art       315:        listbuf_ncol = ncol;            /* cache ncol for listbuf_goto_buffer */
                    316:
1.11      art       317:        if (addlinef(blp, "%-*s%s", w, " MR Buffer", "Size   File") == FALSE ||
                    318:            addlinef(blp, "%-*s%s", w, " -- ------", "----   ----") == FALSE)
1.37      db        319:                return (NULL);
1.11      art       320:
                    321:        for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
                    322:                RSIZE nbytes;
                    323:
1.3       millert   324:                nbytes = 0;                     /* Count bytes in buf.   */
1.1       deraadt   325:                if (bp != blp) {
1.63      kjell     326:                        lp = bfirstlp(bp);
1.62      kjell     327:                        while (lp != bp->b_headp) {
1.3       millert   328:                                nbytes += llength(lp) + 1;
1.1       deraadt   329:                                lp = lforw(lp);
                    330:                        }
1.3       millert   331:                        if (nbytes)
                    332:                                nbytes--;       /* no bonus newline      */
1.1       deraadt   333:                }
1.11      art       334:
1.19      art       335:                if (addlinef(blp, "%c%c%c %-*.*s%c%-6d %-*s",
1.11      art       336:                    (bp == curbp) ? '.' : ' ',  /* current buffer ? */
                    337:                    ((bp->b_flag & BFCHG) != 0) ? '*' : ' ',    /* changed ? */
1.27      vincent   338:                    ((bp->b_flag & BFREADONLY) != 0) ? ' ' : '*',
1.19      art       339:                    w - 5,              /* four chars already written */
                    340:                    w - 5,              /* four chars already written */
1.11      art       341:                    bp->b_bname,        /* buffer name */
1.19      art       342:                    strlen(bp->b_bname) < w - 5 ? ' ' : '$', /* truncated? */
1.11      art       343:                    nbytes,             /* buffer size */
                    344:                    w - 7,              /* seven chars already written */
                    345:                    bp->b_fname) == FALSE)
1.37      db        346:                        return (NULL);
1.1       deraadt   347:        }
1.63      kjell     348:        blp->b_dotp = bfirstlp(blp);            /* put dot at beginning of
1.3       millert   349:                                                 * buffer */
1.1       deraadt   350:        blp->b_doto = 0;
1.37      db        351:        return (blp);                           /* All done              */
1.19      art       352: }
                    353:
                    354: static int
                    355: listbuf_goto_buffer(int f, int n)
                    356: {
1.39      jason     357:        return (listbuf_goto_buffer_helper(f, n, 0));
                    358: }
                    359:
                    360: static int
                    361: listbuf_goto_buffer_one(int f, int n)
                    362: {
                    363:        return (listbuf_goto_buffer_helper(f, n, 1));
                    364: }
                    365:
                    366: static int
                    367: listbuf_goto_buffer_helper(int f, int n, int only)
                    368: {
1.52      deraadt   369:        struct buffer   *bp;
                    370:        struct mgwin    *wp;
                    371:        char            *line = NULL;
                    372:        int              i, ret = FALSE;
1.19      art       373:
                    374:        if (curwp->w_dotp->l_text[listbuf_ncol/2 - 1] == '$') {
                    375:                ewprintf("buffer name truncated");
1.37      db        376:                return (FALSE);
1.19      art       377:        }
                    378:
                    379:        if ((line = malloc(listbuf_ncol/2)) == NULL)
1.37      db        380:                return (FALSE);
1.19      art       381:
                    382:        memcpy(line, curwp->w_dotp->l_text + 4, listbuf_ncol/2 - 5);
                    383:        for (i = listbuf_ncol/2 - 6; i > 0; i--) {
                    384:                if (line[i] != ' ') {
                    385:                        line[i + 1] = '\0';
                    386:                        break;
                    387:                }
                    388:        }
1.37      db        389:        if (i == 0)
1.42      cloder    390:                goto cleanup;
1.19      art       391:
                    392:        for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
                    393:                if (strcmp(bp->b_bname, line) == 0)
                    394:                        break;
                    395:        }
1.37      db        396:        if (bp == NULL)
1.42      cloder    397:                goto cleanup;
1.37      db        398:
1.71      kjell     399:        if ((wp = popbuf(bp, WNONE)) == NULL)
1.42      cloder    400:                goto cleanup;
1.19      art       401:        curbp = bp;
                    402:        curwp = wp;
1.39      jason     403:
                    404:        if (only)
1.42      cloder    405:                ret = (onlywind(f, n));
                    406:        else
                    407:                ret = TRUE;
                    408:
                    409: cleanup:
                    410:        free(line);
1.19      art       411:
1.42      cloder    412:        return (ret);
1.1       deraadt   413: }
                    414:
                    415: /*
1.47      kjell     416:  * The argument "fmt" points to a format string.  Append this line to the
1.3       millert   417:  * buffer. Handcraft the EOL on the end.  Return TRUE if it worked and
1.1       deraadt   418:  * FALSE if you ran out of room.
                    419:  */
1.3       millert   420: int
1.52      deraadt   421: addlinef(struct buffer *bp, char *fmt, ...)
1.3       millert   422: {
1.52      deraadt   423:        va_list          ap;
                    424:        struct line     *lp;
1.1       deraadt   425:
1.30      vincent   426:        if ((lp = lalloc(0)) == NULL)
                    427:                return (FALSE);
1.8       art       428:        va_start(ap, fmt);
1.30      vincent   429:        if (vasprintf(&lp->l_text, fmt, ap) == -1) {
                    430:                lfree(lp);
1.16      deraadt   431:                va_end(ap);
1.30      vincent   432:                return (FALSE);
1.16      deraadt   433:        }
1.30      vincent   434:        lp->l_used = strlen(lp->l_text);
1.8       art       435:        va_end(ap);
                    436:
1.62      kjell     437:        bp->b_headp->l_bp->l_fp = lp;           /* Hook onto the end     */
                    438:        lp->l_bp = bp->b_headp->l_bp;
                    439:        bp->b_headp->l_bp = lp;
                    440:        lp->l_fp = bp->b_headp;
1.60      kjell     441:        bp->b_lines++;
1.8       art       442:
1.37      db        443:        return (TRUE);
1.1       deraadt   444: }
                    445:
                    446: /*
1.3       millert   447:  * Look through the list of buffers, giving the user a chance to save them.
1.51      kjell     448:  * Return TRUE if there are any changed buffers afterwards.  Buffers that don't
                    449:  * have an associated file don't count.  Return FALSE if there are no changed
                    450:  * buffers.  Return ABORT if an error occurs or if the user presses c-g.
1.3       millert   451:  */
                    452: int
1.26      vincent   453: anycb(int f)
1.3       millert   454: {
1.52      deraadt   455:        struct buffer   *bp;
1.79      lum       456:        int              s = FALSE, save = FALSE, save2 = FALSE, ret;
1.53      kjell     457:        char             pbuf[NFILEN + 11];
1.1       deraadt   458:
                    459:        for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1.21      deraadt   460:                if (bp->b_fname != NULL && *(bp->b_fname) != '\0' &&
                    461:                    (bp->b_flag & BFCHG) != 0) {
1.53      kjell     462:                        ret = snprintf(pbuf, sizeof(pbuf), "Save file %s",
1.17      deraadt   463:                            bp->b_fname);
1.53      kjell     464:                        if (ret < 0 || ret >= sizeof(pbuf)) {
1.51      kjell     465:                                ewprintf("Error: filename too long!");
1.81      lum       466:                                return (UERROR);
1.51      kjell     467:                        }
1.53      kjell     468:                        if ((f == TRUE || (save = eyorn(pbuf)) == TRUE) &&
1.79      lum       469:                            (save2 = buffsave(bp)) == TRUE) {
1.1       deraadt   470:                                bp->b_flag &= ~BFCHG;
                    471:                                upmodes(bp);
1.79      lum       472:                        } else {
                    473:                                if (save2 == FIOERR)
                    474:                                        return (save2);
1.3       millert   475:                                s = TRUE;
1.79      lum       476:                        }
1.3       millert   477:                        if (save == ABORT)
                    478:                                return (save);
1.1       deraadt   479:                        save = TRUE;
                    480:                }
                    481:        }
                    482:        if (save == FALSE /* && kbdmop == NULL */ )     /* experimental */
                    483:                ewprintf("(No files need saving)");
1.37      db        484:        return (s);
1.1       deraadt   485: }
                    486:
                    487: /*
                    488:  * Search for a buffer, by name.
                    489:  * If not found, and the "cflag" is TRUE,
1.57      kjell     490:  * create a new buffer. Return pointer to the found
                    491:  * (or new) buffer.
1.1       deraadt   492:  */
1.52      deraadt   493: struct buffer *
1.26      vincent   494: bfind(const char *bname, int cflag)
1.3       millert   495: {
1.52      deraadt   496:        struct buffer   *bp;
1.1       deraadt   497:
                    498:        bp = bheadp;
                    499:        while (bp != NULL) {
1.7       art       500:                if (strcmp(bname, bp->b_bname) == 0)
1.37      db        501:                        return (bp);
1.1       deraadt   502:                bp = bp->b_bufp;
                    503:        }
1.3       millert   504:        if (cflag != TRUE)
1.37      db        505:                return (NULL);
1.29      vincent   506:
1.65      kjell     507:        bp = bnew(bname);
1.57      kjell     508:
                    509:        return (bp);
                    510: }
                    511:
                    512: /*
                    513:  * Create a new buffer and put it in the list of
1.66      deraadt   514:  * all buffers.
1.57      kjell     515:  */
                    516: static struct buffer *
1.65      kjell     517: bnew(const char *bname)
1.57      kjell     518: {
1.66      deraadt   519:        struct buffer   *bp;
1.57      kjell     520:        struct line     *lp;
                    521:        int              i;
1.72      kjell     522:        size_t          len;
1.57      kjell     523:
1.52      deraadt   524:        bp = calloc(1, sizeof(struct buffer));
1.32      vincent   525:        if (bp == NULL) {
1.52      deraadt   526:                ewprintf("Can't get %d bytes", sizeof(struct buffer));
1.37      db        527:                return (NULL);
1.1       deraadt   528:        }
                    529:        if ((lp = lalloc(0)) == NULL) {
1.29      vincent   530:                free(bp);
1.37      db        531:                return (NULL);
1.1       deraadt   532:        }
1.3       millert   533:        bp->b_altb = bp->b_bufp = NULL;
                    534:        bp->b_dotp = lp;
                    535:        bp->b_doto = 0;
1.1       deraadt   536:        bp->b_markp = NULL;
                    537:        bp->b_marko = 0;
1.3       millert   538:        bp->b_flag = defb_flag;
1.72      kjell     539:        /* if buffer name starts and ends with '*', we ignore changes */
                    540:        len = strlen(bname);
                    541:        if (len) {
                    542:                if (bname[0] == '*' && bname[len - 1] == '*')
                    543:                        bp->b_flag |= BFIGNDIRTY;
                    544:        }
1.3       millert   545:        bp->b_nwnd = 0;
1.62      kjell     546:        bp->b_headp = lp;
1.1       deraadt   547:        bp->b_nmodes = defb_nmodes;
1.74      oga       548:        TAILQ_INIT(&bp->b_undo);
1.46      kjell     549:        bp->b_undoptr = NULL;
1.1       deraadt   550:        i = 0;
                    551:        do {
1.3       millert   552:                bp->b_modes[i] = defb_modes[i];
                    553:        } while (i++ < defb_nmodes);
1.1       deraadt   554:        bp->b_fname[0] = '\0';
1.57      kjell     555:        bp->b_cwd[0] = '\0';
1.1       deraadt   556:        bzero(&bp->b_fi, sizeof(bp->b_fi));
                    557:        lp->l_fp = lp;
                    558:        lp->l_bp = lp;
                    559:        bp->b_bufp = bheadp;
                    560:        bheadp = bp;
1.60      kjell     561:        bp->b_dotline = bp->b_markline = 1;
1.61      kjell     562:        bp->b_lines = 1;
1.65      kjell     563:        if ((bp->b_bname = strdup(bname)) == NULL) {
                    564:                ewprintf("Can't get %d bytes", strlen(bname) + 1);
                    565:                return (NULL);
                    566:        }
1.57      kjell     567:
1.37      db        568:        return (bp);
1.1       deraadt   569: }
                    570:
                    571: /*
                    572:  * This routine blows away all of the text
                    573:  * in a buffer. If the buffer is marked as changed
                    574:  * then we ask if it is ok to blow it away; this is
                    575:  * to save the user the grief of losing text. The
                    576:  * window chain is nearly always wrong if this gets
                    577:  * called; the caller must arrange for the updates
                    578:  * that are required. Return TRUE if everything
                    579:  * looks good.
                    580:  */
1.3       millert   581: int
1.52      deraadt   582: bclear(struct buffer *bp)
1.3       millert   583: {
1.52      deraadt   584:        struct line     *lp;
                    585:        int              s;
1.1       deraadt   586:
1.72      kjell     587:        /* Has buffer changed, and do we care? */
                    588:        if (!(bp->b_flag & BFIGNDIRTY) && (bp->b_flag & BFCHG) != 0 &&
1.21      deraadt   589:            (s = eyesno("Buffer modified; kill anyway")) != TRUE)
1.1       deraadt   590:                return (s);
1.3       millert   591:        bp->b_flag &= ~BFCHG;   /* Not changed           */
1.62      kjell     592:        while ((lp = lforw(bp->b_headp)) != bp->b_headp)
1.1       deraadt   593:                lfree(lp);
1.62      kjell     594:        bp->b_dotp = bp->b_headp;       /* Fix dot */
1.3       millert   595:        bp->b_doto = 0;
                    596:        bp->b_markp = NULL;     /* Invalidate "mark"     */
1.1       deraadt   597:        bp->b_marko = 0;
1.60      kjell     598:        bp->b_dotline = bp->b_markline = 1;
1.61      kjell     599:        bp->b_lines = 1;
1.60      kjell     600:
1.37      db        601:        return (TRUE);
1.1       deraadt   602: }
                    603:
                    604: /*
                    605:  * Display the given buffer in the given window. Flags indicated
1.68      kjell     606:  * action on redisplay. Update modified flag so insert loop can check it.
1.1       deraadt   607:  */
1.3       millert   608: int
1.52      deraadt   609: showbuffer(struct buffer *bp, struct mgwin *wp, int flags)
1.3       millert   610: {
1.52      deraadt   611:        struct buffer   *obp;
                    612:        struct mgwin    *owp;
1.1       deraadt   613:
1.68      kjell     614:        /* Ensure file has not been modified elsewhere */
                    615:        if (fchecktime(bp) != TRUE)
                    616:                bp->b_flag |= BFDIRTY;
                    617:
1.32      vincent   618:        if (wp->w_bufp == bp) { /* Easy case! */
1.70      kjell     619:                wp->w_rflag |= flags;
1.37      db        620:                return (TRUE);
1.1       deraadt   621:        }
1.37      db        622:        /* First, detach the old buffer from the window */
1.1       deraadt   623:        if ((bp->b_altb = obp = wp->w_bufp) != NULL) {
                    624:                if (--obp->b_nwnd == 0) {
1.3       millert   625:                        obp->b_dotp = wp->w_dotp;
                    626:                        obp->b_doto = wp->w_doto;
1.1       deraadt   627:                        obp->b_markp = wp->w_markp;
                    628:                        obp->b_marko = wp->w_marko;
1.60      kjell     629:                        obp->b_dotline = wp->w_dotline;
                    630:                        obp->b_markline = wp->w_markline;
1.1       deraadt   631:                }
                    632:        }
                    633:        /* Now, attach the new buffer to the window */
                    634:        wp->w_bufp = bp;
                    635:
1.3       millert   636:        if (bp->b_nwnd++ == 0) {        /* First use.            */
                    637:                wp->w_dotp = bp->b_dotp;
                    638:                wp->w_doto = bp->b_doto;
1.1       deraadt   639:                wp->w_markp = bp->b_markp;
                    640:                wp->w_marko = bp->b_marko;
1.60      kjell     641:                wp->w_dotline = bp->b_dotline;
                    642:                wp->w_markline = bp->b_markline;
1.1       deraadt   643:        } else
1.3       millert   644:                /* already on screen, steal values from other window */
1.1       deraadt   645:                for (owp = wheadp; owp != NULL; owp = wp->w_wndp)
                    646:                        if (wp->w_bufp == bp && owp != wp) {
1.3       millert   647:                                wp->w_dotp = owp->w_dotp;
                    648:                                wp->w_doto = owp->w_doto;
1.1       deraadt   649:                                wp->w_markp = owp->w_markp;
                    650:                                wp->w_marko = owp->w_marko;
1.60      kjell     651:                                wp->w_dotline = owp->w_dotline;
1.64      kjell     652:                                wp->w_markline = owp->w_markline;
1.1       deraadt   653:                                break;
                    654:                        }
1.70      kjell     655:        wp->w_rflag |= WFMODE | flags;
1.56      kjell     656:        return (TRUE);
                    657: }
                    658:
                    659: /*
                    660:  * Augment a buffer name with a number, if necessary
                    661:  *
                    662:  * If more than one file of the same basename() is open,
                    663:  * the additional buffers are named "file<2>", "file<3>", and
                    664:  * so forth.  This function adjusts a buffer name to
                    665:  * include the number, if necessary.
                    666:  */
                    667: int
1.57      kjell     668: augbname(char *bn, const char *fn, size_t bs)
1.56      kjell     669: {
1.66      deraadt   670:        int      count;
1.56      kjell     671:        size_t   remain, len;
                    672:
1.77      kjell     673:        if ((len = xbasename(bn, fn, bs)) >= bs)
1.56      kjell     674:                return (FALSE);
                    675:
                    676:        remain = bs - len;
                    677:        for (count = 2; bfind(bn, FALSE) != NULL; count++)
                    678:                snprintf(bn + len, remain, "<%d>", count);
                    679:
1.37      db        680:        return (TRUE);
1.1       deraadt   681: }
                    682:
                    683: /*
                    684:  * Pop the buffer we got passed onto the screen.
                    685:  * Returns a status.
                    686:  */
1.52      deraadt   687: struct mgwin *
1.71      kjell     688: popbuf(struct buffer *bp, int flags)
1.3       millert   689: {
1.52      deraadt   690:        struct mgwin    *wp;
1.1       deraadt   691:
1.3       millert   692:        if (bp->b_nwnd == 0) {  /* Not on screen yet.    */
1.71      kjell     693:                /*
                    694:                 * Pick a window for a pop-up.
                    695:                 * If only one window, split the screen.
                    696:                 * Flag the new window as ephemeral
                    697:                 */
                    698:                if (wheadp->w_wndp == NULL &&
                    699:                    splitwind(FFOTHARG, flags) == FALSE)
                    700:                        return (NULL);
                    701:
                    702:                /*
                    703:                 * Pick the uppermost window that isn't
                    704:                 * the current window. An LRU algorithm
                    705:                 * might be better. Return a pointer, or NULL on error.
                    706:                 */
                    707:                wp = wheadp;
                    708:
                    709:                while (wp != NULL && wp == curwp)
                    710:                        wp = wp->w_wndp;
1.1       deraadt   711:        } else
                    712:                for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
                    713:                        if (wp->w_bufp == bp) {
1.70      kjell     714:                                wp->w_rflag |= WFFULL | WFFRAME;
1.37      db        715:                                return (wp);
1.1       deraadt   716:                        }
1.58      kjell     717:        if (showbuffer(bp, wp, WFFULL) != TRUE)
1.37      db        718:                return (NULL);
                    719:        return (wp);
1.1       deraadt   720: }
                    721:
                    722: /*
                    723:  * Insert another buffer at dot.  Very useful.
                    724:  */
1.3       millert   725: /* ARGSUSED */
                    726: int
1.26      vincent   727: bufferinsert(int f, int n)
1.1       deraadt   728: {
1.52      deraadt   729:        struct buffer *bp;
                    730:        struct line   *clp;
1.36      deraadt   731:        int     clo, nline;
                    732:        char    bufn[NBUFN], *bufp;
1.1       deraadt   733:
                    734:        /* Get buffer to use from user */
                    735:        if (curbp->b_altb != NULL)
1.34      vincent   736:                bufp = eread("Insert buffer: (default %s) ", bufn, NBUFN,
1.40      kjell     737:                    EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname);
1.1       deraadt   738:        else
1.38      cloder    739:                bufp = eread("Insert buffer: ", bufn, NBUFN, EFNEW | EFBUF);
1.34      vincent   740:        if (bufp == NULL)
1.37      db        741:                return (ABORT);
1.34      vincent   742:        if (bufp[0] == '\0' && curbp->b_altb != NULL)
1.3       millert   743:                bp = curbp->b_altb;
                    744:        else if ((bp = bfind(bufn, FALSE)) == NULL)
1.37      db        745:                return (FALSE);
1.1       deraadt   746:
1.3       millert   747:        if (bp == curbp) {
1.1       deraadt   748:                ewprintf("Cannot insert buffer into self");
1.37      db        749:                return (FALSE);
1.1       deraadt   750:        }
                    751:        /* insert the buffer */
                    752:        nline = 0;
1.63      kjell     753:        clp = bfirstlp(bp);
1.3       millert   754:        for (;;) {
1.1       deraadt   755:                for (clo = 0; clo < llength(clp); clo++)
                    756:                        if (linsert(1, lgetc(clp, clo)) == FALSE)
1.37      db        757:                                return (FALSE);
1.62      kjell     758:                if ((clp = lforw(clp)) == bp->b_headp)
1.3       millert   759:                        break;
                    760:                if (newline(FFRAND, 1) == FALSE)        /* fake newline */
1.37      db        761:                        return (FALSE);
1.1       deraadt   762:                nline++;
                    763:        }
1.3       millert   764:        if (nline == 1)
                    765:                ewprintf("[Inserted 1 line]");
                    766:        else
                    767:                ewprintf("[Inserted %d lines]", nline);
1.1       deraadt   768:
1.37      db        769:        clp = curwp->w_linep;           /* cosmetic adjustment  */
1.3       millert   770:        if (curwp->w_dotp == clp) {     /* for offscreen insert */
1.62      kjell     771:                while (nline-- && lback(clp) != curbp->b_headp)
1.1       deraadt   772:                        clp = lback(clp);
1.37      db        773:                curwp->w_linep = clp;   /* adjust framing.      */
1.70      kjell     774:                curwp->w_rflag |= WFFULL;
1.1       deraadt   775:        }
                    776:        return (TRUE);
                    777: }
                    778:
                    779: /*
                    780:  * Turn off the dirty bit on this buffer.
                    781:  */
1.3       millert   782: /* ARGSUSED */
                    783: int
1.26      vincent   784: notmodified(int f, int n)
1.1       deraadt   785: {
1.52      deraadt   786:        struct mgwin *wp;
1.1       deraadt   787:
                    788:        curbp->b_flag &= ~BFCHG;
1.3       millert   789:        wp = wheadp;            /* Update mode lines.    */
1.1       deraadt   790:        while (wp != NULL) {
                    791:                if (wp->w_bufp == curbp)
1.70      kjell     792:                        wp->w_rflag |= WFMODE;
1.1       deraadt   793:                wp = wp->w_wndp;
                    794:        }
                    795:        ewprintf("Modification-flag cleared");
1.37      db        796:        return (TRUE);
1.1       deraadt   797: }
                    798:
                    799: /*
1.78      lum       800:  * Popbuf and set all windows to top of buffer.
1.1       deraadt   801:  */
1.3       millert   802: int
1.71      kjell     803: popbuftop(struct buffer *bp, int flags)
1.1       deraadt   804: {
1.52      deraadt   805:        struct mgwin *wp;
1.1       deraadt   806:
1.63      kjell     807:        bp->b_dotp = bfirstlp(bp);
1.3       millert   808:        bp->b_doto = 0;
                    809:        if (bp->b_nwnd != 0) {
                    810:                for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
                    811:                        if (wp->w_bufp == bp) {
                    812:                                wp->w_dotp = bp->b_dotp;
                    813:                                wp->w_doto = 0;
1.70      kjell     814:                                wp->w_rflag |= WFFULL;
1.3       millert   815:                        }
                    816:        }
1.71      kjell     817:        return (popbuf(bp, flags) != NULL);
1.1       deraadt   818: }
1.57      kjell     819:
                    820: /*
                    821:  * Return the working directory for the current buffer, terminated
                    822:  * with a '/'. First, try to extract it from the current buffer's
                    823:  * filename. If that fails, use global cwd.
                    824:  */
                    825: int
                    826: getbufcwd(char *path, size_t plen)
                    827: {
                    828:        char cwd[NFILEN];
                    829:
                    830:        if (plen == 0)
                    831:                return (FALSE);
                    832:
1.67      kjell     833:        if (globalwd == FALSE && curbp->b_cwd[0] != '\0') {
1.57      kjell     834:                (void)strlcpy(path, curbp->b_cwd, plen);
                    835:        } else {
                    836:                if (getcwdir(cwd, sizeof(cwd)) == FALSE)
                    837:                        goto error;
                    838:                (void)strlcpy(path, cwd, plen);
                    839:        }
                    840:        return (TRUE);
                    841: error:
                    842:        path[0] = '\0';
                    843:        return (FALSE);
                    844: }
                    845:
1.68      kjell     846: /*
1.69      kjell     847:  * Ensures a buffer has not been modified elsewhere; e.g. on disk.
                    848:  * Prompt the user if it has.
                    849:  * Returns TRUE if it has NOT (i.e. buffer is ok to edit).
                    850:  * FALSE or ABORT otherwise
1.68      kjell     851:  */
                    852: int
                    853: checkdirty(struct buffer *bp)
                    854: {
                    855:        int s;
1.83      florian   856:
                    857:        if ((bp->b_flag & (BFCHG | BFDIRTY)) == 0)
                    858:                if (fchecktime(bp) != TRUE)
                    859:                        bp->b_flag |= BFDIRTY;
                    860:
1.68      kjell     861:        if ((bp->b_flag & (BFDIRTY | BFIGNDIRTY)) == BFDIRTY) {
1.84      florian   862:                s = eynorr("File changed on disk; really edit the buffer");
                    863:                switch (s) {
                    864:                case TRUE:
                    865:                        bp->b_flag &= ~BFDIRTY;
                    866:                        bp->b_flag |= BFIGNDIRTY;
                    867:                        return (TRUE);
                    868:                case REVERT:
                    869:                        dorevert();
                    870:                        return (FALSE);
                    871:                default:
1.68      kjell     872:                        return (s);
1.84      florian   873:                }
1.68      kjell     874:        }
                    875:
                    876:        return (TRUE);
                    877: }
1.82      jasper    878:
                    879: /*
                    880:  * Revert the current buffer to whatever is on disk.
                    881:  */
                    882: /* ARGSUSED */
                    883: int
                    884: revertbuffer(int f, int n)
                    885: {
                    886:        char fbuf[NFILEN + 32];
                    887:
1.85      florian   888:        if (curbp->b_fname[0] == 0) {
1.82      jasper    889:                ewprintf("Cannot revert buffer not associated with any files.");
                    890:                return (FALSE);
                    891:        }
                    892:
1.85      florian   893:        snprintf(fbuf, sizeof(fbuf), "Revert buffer from file %s",
                    894:            curbp->b_fname);
1.82      jasper    895:
1.85      florian   896:        if (eyorn(fbuf) == TRUE)
1.84      florian   897:                return dorevert();
1.82      jasper    898:
1.84      florian   899:        return (FALSE);
                    900: }
1.82      jasper    901:
1.84      florian   902: int
1.86      haesbaer  903: dorevert(void)
1.84      florian   904: {
                    905:        int lineno;
1.89    ! florian   906:        struct undo_rec *rec;
1.82      jasper    907:
1.85      florian   908:        if (access(curbp->b_fname, F_OK|R_OK) != 0) {
1.84      florian   909:                if (errno == ENOENT)
                    910:                        ewprintf("File %s no longer exists!",
1.85      florian   911:                            curbp->b_fname);
1.84      florian   912:                else
                    913:                        ewprintf("File %s is no longer readable!",
1.85      florian   914:                            curbp->b_fname);
1.84      florian   915:                return (FALSE);
1.82      jasper    916:        }
                    917:
1.84      florian   918:        /* Save our current line, so we can go back after reloading. */
1.85      florian   919:        lineno = curwp->w_dotline;
1.84      florian   920:
                    921:        /* Prevent readin from asking if we want to kill the buffer. */
                    922:        curbp->b_flag &= ~BFCHG;
1.89    ! florian   923:
        !           924:        /* Clean up undo memory */
        !           925:        while ((rec = TAILQ_FIRST(&curbp->b_undo))) {
        !           926:                TAILQ_REMOVE(&curbp->b_undo, rec, next);
        !           927:                free_undo_record(rec);
        !           928:        }
1.84      florian   929:
1.85      florian   930:        if (readin(curbp->b_fname))
1.84      florian   931:                return(setlineno(lineno));
1.82      jasper    932:        return (FALSE);
1.88      florian   933: }
                    934:
                    935: /*
                    936:  * Diff the current buffer to what is on disk.
                    937:  */
                    938: /*ARGSUSED */
                    939: int
                    940: diffbuffer(int f, int n)
                    941: {
                    942:        struct buffer   *bp;
                    943:        struct line     *lp, *lpend;
                    944:        size_t           len;
                    945:        int              ret;
                    946:        char            *text, *ttext;
                    947:        char            * const argv[] =
                    948:            {DIFFTOOL, "-u", "-p", curbp->b_fname, "-", (char *)NULL};
                    949:
                    950:        len = 0;
                    951:
                    952:        /* C-u is not supported */
                    953:        if (n > 1)
                    954:                return (ABORT);
                    955:
                    956:        if (access(DIFFTOOL, X_OK) != 0) {
                    957:                ewprintf("%s not found or not executable.", DIFFTOOL);
                    958:                return (FALSE);
                    959:        }
                    960:
                    961:        if (curbp->b_fname[0] == 0) {
                    962:                ewprintf("Cannot diff buffer not associated with any files.");
                    963:                return (FALSE);
                    964:        }
                    965:
                    966:        lpend = curbp->b_headp;
                    967:        for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) {
                    968:                len+=llength(lp);
                    969:                if (lforw(lp) != lpend)         /* no implied \n on last line */
                    970:                        len++;
                    971:        }
                    972:        if ((text = calloc(len + 1, sizeof(char))) == NULL) {
                    973:                ewprintf("Cannot allocate memory.");
                    974:                return (FALSE);
                    975:        }
                    976:        ttext = text;
                    977:
                    978:        for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) {
                    979:                if (llength(lp) != 0) {
                    980:                        memcpy(ttext, ltext(lp), llength(lp));
                    981:                        ttext += llength(lp);
                    982:                }
                    983:                if (lforw(lp) != lpend)         /* no implied \n on last line */
                    984:                        *ttext++ = '\n';
                    985:        }
                    986:
                    987:        bp = bfind("*Diff*", TRUE);
                    988:        bp->b_flag |= BFREADONLY;
                    989:        if (bclear(bp) != TRUE) {
                    990:                free(text);
                    991:                return (FALSE);
                    992:        }
                    993:
                    994:        ret = pipeio(DIFFTOOL, argv, text, len, bp);
                    995:
                    996:        if (ret == TRUE) {
                    997:                eerase();
                    998:                if (lforw(bp->b_headp) == bp->b_headp)
                    999:                        addline(bp, "Diff finished (no differences).");
                   1000:        }
                   1001:
                   1002:        free(text);
                   1003:        return (ret);
1.82      jasper   1004: }