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

1.94    ! bcallah     1: /*     $OpenBSD: buffer.c,v 1.93 2014/03/20 07:47:29 lum 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.
1.91      lum       127:  * Ask for the name (unless called by dired mode). Look it up (don't
                    128:  * get too upset if it isn't there at all!). Clear the buffer (ask
1.1       deraadt   129:  * if the buffer has been changed). Then free the header
1.91      lum       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.91      lum       139:        if (f & FFRAND) /* dired mode 'q' */
                    140:                bp = curbp;
                    141:        else if ((bufp = eread("Kill buffer: (default %s) ", bufn, NBUFN,
1.40      kjell     142:            EFNUL | EFNEW | EFBUF, curbp->b_bname)) == NULL)
1.37      db        143:                return (ABORT);
1.44      kjell     144:        else if (bufp[0] == '\0')
1.3       millert   145:                bp = curbp;
                    146:        else if ((bp = bfind(bufn, FALSE)) == NULL)
1.37      db        147:                return (FALSE);
                    148:        return (killbuffer(bp));
1.35      jfb       149: }
                    150:
                    151: int
1.52      deraadt   152: killbuffer(struct buffer *bp)
1.35      jfb       153: {
1.52      deraadt   154:        struct buffer *bp1;
                    155:        struct buffer *bp2;
                    156:        struct mgwin  *wp;
1.41      kjell     157:        int s;
1.90      florian   158:        struct undo_rec *rec;
1.3       millert   159:
                    160:        /*
1.37      db        161:         * Find some other buffer to display. Try the alternate buffer,
1.3       millert   162:         * then the first different buffer in the buffer list.  If there's
                    163:         * only one buffer, create buffer *scratch* and make it the alternate
                    164:         * buffer.  Return if *scratch* is only buffer...
1.1       deraadt   165:         */
                    166:        if ((bp1 = bp->b_altb) == NULL) {
                    167:                bp1 = (bp == bheadp) ? bp->b_bufp : bheadp;
                    168:                if (bp1 == NULL) {
                    169:                        /* only one buffer. see if it's *scratch* */
1.3       millert   170:                        if (bp == bfind("*scratch*", FALSE))
1.49      kjell     171:                                return (TRUE);
1.1       deraadt   172:                        /* create *scratch* for alternate buffer */
1.3       millert   173:                        if ((bp1 = bfind("*scratch*", TRUE)) == NULL)
1.37      db        174:                                return (FALSE);
1.1       deraadt   175:                }
                    176:        }
1.41      kjell     177:        if ((s = bclear(bp)) != TRUE)
                    178:                return (s);
1.1       deraadt   179:        for (wp = wheadp; bp->b_nwnd > 0; wp = wp->w_wndp) {
1.3       millert   180:                if (wp->w_bufp == bp) {
                    181:                        bp2 = bp1->b_altb;      /* save alternate buffer */
1.58      kjell     182:                        if (showbuffer(bp1, wp, WFMODE | WFFRAME | WFFULL))
1.3       millert   183:                                bp1->b_altb = bp2;
                    184:                        else
                    185:                                bp1 = bp2;
                    186:                }
                    187:        }
                    188:        if (bp == curbp)
                    189:                curbp = bp1;
1.62      kjell     190:        free(bp->b_headp);                      /* Release header line.  */
1.3       millert   191:        bp2 = NULL;                             /* Find the header.      */
1.1       deraadt   192:        bp1 = bheadp;
                    193:        while (bp1 != bp) {
                    194:                if (bp1->b_altb == bp)
                    195:                        bp1->b_altb = (bp->b_altb == bp1) ? NULL : bp->b_altb;
                    196:                bp2 = bp1;
                    197:                bp1 = bp1->b_bufp;
                    198:        }
1.3       millert   199:        bp1 = bp1->b_bufp;                      /* Next one in chain.    */
                    200:        if (bp2 == NULL)                        /* Unlink it.            */
1.1       deraadt   201:                bheadp = bp1;
                    202:        else
                    203:                bp2->b_bufp = bp1;
1.3       millert   204:        while (bp1 != NULL) {                   /* Finish with altb's    */
1.1       deraadt   205:                if (bp1->b_altb == bp)
                    206:                        bp1->b_altb = (bp->b_altb == bp1) ? NULL : bp->b_altb;
                    207:                bp1 = bp1->b_bufp;
                    208:        }
1.74      oga       209:
1.90      florian   210:        while ((rec = TAILQ_FIRST(&bp->b_undo))) {
                    211:                TAILQ_REMOVE(&bp->b_undo, rec, next);
1.46      kjell     212:                free_undo_record(rec);
                    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] == '$') {
1.93      lum       375:                dobeep();
1.19      art       376:                ewprintf("buffer name truncated");
1.37      db        377:                return (FALSE);
1.19      art       378:        }
                    379:
                    380:        if ((line = malloc(listbuf_ncol/2)) == NULL)
1.37      db        381:                return (FALSE);
1.19      art       382:
                    383:        memcpy(line, curwp->w_dotp->l_text + 4, listbuf_ncol/2 - 5);
                    384:        for (i = listbuf_ncol/2 - 6; i > 0; i--) {
                    385:                if (line[i] != ' ') {
                    386:                        line[i + 1] = '\0';
                    387:                        break;
                    388:                }
                    389:        }
1.37      db        390:        if (i == 0)
1.42      cloder    391:                goto cleanup;
1.19      art       392:
                    393:        for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
                    394:                if (strcmp(bp->b_bname, line) == 0)
                    395:                        break;
                    396:        }
1.37      db        397:        if (bp == NULL)
1.42      cloder    398:                goto cleanup;
1.37      db        399:
1.71      kjell     400:        if ((wp = popbuf(bp, WNONE)) == NULL)
1.42      cloder    401:                goto cleanup;
1.19      art       402:        curbp = bp;
                    403:        curwp = wp;
1.39      jason     404:
                    405:        if (only)
1.42      cloder    406:                ret = (onlywind(f, n));
                    407:        else
                    408:                ret = TRUE;
                    409:
                    410: cleanup:
                    411:        free(line);
1.19      art       412:
1.42      cloder    413:        return (ret);
1.1       deraadt   414: }
                    415:
                    416: /*
1.47      kjell     417:  * The argument "fmt" points to a format string.  Append this line to the
1.3       millert   418:  * buffer. Handcraft the EOL on the end.  Return TRUE if it worked and
1.1       deraadt   419:  * FALSE if you ran out of room.
                    420:  */
1.3       millert   421: int
1.52      deraadt   422: addlinef(struct buffer *bp, char *fmt, ...)
1.3       millert   423: {
1.52      deraadt   424:        va_list          ap;
                    425:        struct line     *lp;
1.1       deraadt   426:
1.30      vincent   427:        if ((lp = lalloc(0)) == NULL)
                    428:                return (FALSE);
1.8       art       429:        va_start(ap, fmt);
1.30      vincent   430:        if (vasprintf(&lp->l_text, fmt, ap) == -1) {
                    431:                lfree(lp);
1.16      deraadt   432:                va_end(ap);
1.30      vincent   433:                return (FALSE);
1.16      deraadt   434:        }
1.30      vincent   435:        lp->l_used = strlen(lp->l_text);
1.8       art       436:        va_end(ap);
                    437:
1.62      kjell     438:        bp->b_headp->l_bp->l_fp = lp;           /* Hook onto the end     */
                    439:        lp->l_bp = bp->b_headp->l_bp;
                    440:        bp->b_headp->l_bp = lp;
                    441:        lp->l_fp = bp->b_headp;
1.60      kjell     442:        bp->b_lines++;
1.8       art       443:
1.37      db        444:        return (TRUE);
1.1       deraadt   445: }
                    446:
                    447: /*
1.3       millert   448:  * Look through the list of buffers, giving the user a chance to save them.
1.51      kjell     449:  * Return TRUE if there are any changed buffers afterwards.  Buffers that don't
                    450:  * have an associated file don't count.  Return FALSE if there are no changed
                    451:  * buffers.  Return ABORT if an error occurs or if the user presses c-g.
1.3       millert   452:  */
                    453: int
1.26      vincent   454: anycb(int f)
1.3       millert   455: {
1.52      deraadt   456:        struct buffer   *bp;
1.79      lum       457:        int              s = FALSE, save = FALSE, save2 = FALSE, ret;
1.53      kjell     458:        char             pbuf[NFILEN + 11];
1.1       deraadt   459:
                    460:        for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1.94    ! bcallah   461:                if (*(bp->b_fname) != '\0' && (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.93      lum       465:                                dobeep();
1.51      kjell     466:                                ewprintf("Error: filename too long!");
1.81      lum       467:                                return (UERROR);
1.51      kjell     468:                        }
1.53      kjell     469:                        if ((f == TRUE || (save = eyorn(pbuf)) == TRUE) &&
1.79      lum       470:                            (save2 = buffsave(bp)) == TRUE) {
1.1       deraadt   471:                                bp->b_flag &= ~BFCHG;
                    472:                                upmodes(bp);
1.79      lum       473:                        } else {
                    474:                                if (save2 == FIOERR)
                    475:                                        return (save2);
1.3       millert   476:                                s = TRUE;
1.79      lum       477:                        }
1.3       millert   478:                        if (save == ABORT)
                    479:                                return (save);
1.1       deraadt   480:                        save = TRUE;
                    481:                }
                    482:        }
                    483:        if (save == FALSE /* && kbdmop == NULL */ )     /* experimental */
                    484:                ewprintf("(No files need saving)");
1.37      db        485:        return (s);
1.1       deraadt   486: }
                    487:
                    488: /*
                    489:  * Search for a buffer, by name.
                    490:  * If not found, and the "cflag" is TRUE,
1.57      kjell     491:  * create a new buffer. Return pointer to the found
                    492:  * (or new) buffer.
1.1       deraadt   493:  */
1.52      deraadt   494: struct buffer *
1.26      vincent   495: bfind(const char *bname, int cflag)
1.3       millert   496: {
1.52      deraadt   497:        struct buffer   *bp;
1.1       deraadt   498:
                    499:        bp = bheadp;
                    500:        while (bp != NULL) {
1.7       art       501:                if (strcmp(bname, bp->b_bname) == 0)
1.37      db        502:                        return (bp);
1.1       deraadt   503:                bp = bp->b_bufp;
                    504:        }
1.3       millert   505:        if (cflag != TRUE)
1.37      db        506:                return (NULL);
1.29      vincent   507:
1.65      kjell     508:        bp = bnew(bname);
1.57      kjell     509:
                    510:        return (bp);
                    511: }
                    512:
                    513: /*
                    514:  * Create a new buffer and put it in the list of
1.66      deraadt   515:  * all buffers.
1.57      kjell     516:  */
                    517: static struct buffer *
1.65      kjell     518: bnew(const char *bname)
1.57      kjell     519: {
1.66      deraadt   520:        struct buffer   *bp;
1.57      kjell     521:        struct line     *lp;
                    522:        int              i;
1.72      kjell     523:        size_t          len;
1.57      kjell     524:
1.52      deraadt   525:        bp = calloc(1, sizeof(struct buffer));
1.32      vincent   526:        if (bp == NULL) {
1.93      lum       527:                dobeep();
1.52      deraadt   528:                ewprintf("Can't get %d bytes", sizeof(struct buffer));
1.37      db        529:                return (NULL);
1.1       deraadt   530:        }
                    531:        if ((lp = lalloc(0)) == NULL) {
1.29      vincent   532:                free(bp);
1.37      db        533:                return (NULL);
1.1       deraadt   534:        }
1.3       millert   535:        bp->b_altb = bp->b_bufp = NULL;
                    536:        bp->b_dotp = lp;
                    537:        bp->b_doto = 0;
1.1       deraadt   538:        bp->b_markp = NULL;
                    539:        bp->b_marko = 0;
1.3       millert   540:        bp->b_flag = defb_flag;
1.72      kjell     541:        /* if buffer name starts and ends with '*', we ignore changes */
                    542:        len = strlen(bname);
                    543:        if (len) {
                    544:                if (bname[0] == '*' && bname[len - 1] == '*')
                    545:                        bp->b_flag |= BFIGNDIRTY;
                    546:        }
1.3       millert   547:        bp->b_nwnd = 0;
1.62      kjell     548:        bp->b_headp = lp;
1.1       deraadt   549:        bp->b_nmodes = defb_nmodes;
1.74      oga       550:        TAILQ_INIT(&bp->b_undo);
1.46      kjell     551:        bp->b_undoptr = NULL;
1.1       deraadt   552:        i = 0;
                    553:        do {
1.3       millert   554:                bp->b_modes[i] = defb_modes[i];
                    555:        } while (i++ < defb_nmodes);
1.1       deraadt   556:        bp->b_fname[0] = '\0';
1.57      kjell     557:        bp->b_cwd[0] = '\0';
1.1       deraadt   558:        bzero(&bp->b_fi, sizeof(bp->b_fi));
                    559:        lp->l_fp = lp;
                    560:        lp->l_bp = lp;
                    561:        bp->b_bufp = bheadp;
                    562:        bheadp = bp;
1.60      kjell     563:        bp->b_dotline = bp->b_markline = 1;
1.61      kjell     564:        bp->b_lines = 1;
1.65      kjell     565:        if ((bp->b_bname = strdup(bname)) == NULL) {
1.93      lum       566:                dobeep();
1.65      kjell     567:                ewprintf("Can't get %d bytes", strlen(bname) + 1);
                    568:                return (NULL);
                    569:        }
1.57      kjell     570:
1.37      db        571:        return (bp);
1.1       deraadt   572: }
                    573:
                    574: /*
                    575:  * This routine blows away all of the text
                    576:  * in a buffer. If the buffer is marked as changed
                    577:  * then we ask if it is ok to blow it away; this is
                    578:  * to save the user the grief of losing text. The
                    579:  * window chain is nearly always wrong if this gets
                    580:  * called; the caller must arrange for the updates
                    581:  * that are required. Return TRUE if everything
                    582:  * looks good.
                    583:  */
1.3       millert   584: int
1.52      deraadt   585: bclear(struct buffer *bp)
1.3       millert   586: {
1.52      deraadt   587:        struct line     *lp;
                    588:        int              s;
1.1       deraadt   589:
1.72      kjell     590:        /* Has buffer changed, and do we care? */
                    591:        if (!(bp->b_flag & BFIGNDIRTY) && (bp->b_flag & BFCHG) != 0 &&
1.21      deraadt   592:            (s = eyesno("Buffer modified; kill anyway")) != TRUE)
1.1       deraadt   593:                return (s);
1.3       millert   594:        bp->b_flag &= ~BFCHG;   /* Not changed           */
1.62      kjell     595:        while ((lp = lforw(bp->b_headp)) != bp->b_headp)
1.1       deraadt   596:                lfree(lp);
1.62      kjell     597:        bp->b_dotp = bp->b_headp;       /* Fix dot */
1.3       millert   598:        bp->b_doto = 0;
                    599:        bp->b_markp = NULL;     /* Invalidate "mark"     */
1.1       deraadt   600:        bp->b_marko = 0;
1.60      kjell     601:        bp->b_dotline = bp->b_markline = 1;
1.61      kjell     602:        bp->b_lines = 1;
1.60      kjell     603:
1.37      db        604:        return (TRUE);
1.1       deraadt   605: }
                    606:
                    607: /*
                    608:  * Display the given buffer in the given window. Flags indicated
1.68      kjell     609:  * action on redisplay. Update modified flag so insert loop can check it.
1.1       deraadt   610:  */
1.3       millert   611: int
1.52      deraadt   612: showbuffer(struct buffer *bp, struct mgwin *wp, int flags)
1.3       millert   613: {
1.52      deraadt   614:        struct buffer   *obp;
                    615:        struct mgwin    *owp;
1.1       deraadt   616:
1.68      kjell     617:        /* Ensure file has not been modified elsewhere */
                    618:        if (fchecktime(bp) != TRUE)
                    619:                bp->b_flag |= BFDIRTY;
                    620:
1.32      vincent   621:        if (wp->w_bufp == bp) { /* Easy case! */
1.70      kjell     622:                wp->w_rflag |= flags;
1.37      db        623:                return (TRUE);
1.1       deraadt   624:        }
1.37      db        625:        /* First, detach the old buffer from the window */
1.1       deraadt   626:        if ((bp->b_altb = obp = wp->w_bufp) != NULL) {
                    627:                if (--obp->b_nwnd == 0) {
1.3       millert   628:                        obp->b_dotp = wp->w_dotp;
                    629:                        obp->b_doto = wp->w_doto;
1.1       deraadt   630:                        obp->b_markp = wp->w_markp;
                    631:                        obp->b_marko = wp->w_marko;
1.60      kjell     632:                        obp->b_dotline = wp->w_dotline;
                    633:                        obp->b_markline = wp->w_markline;
1.1       deraadt   634:                }
                    635:        }
                    636:        /* Now, attach the new buffer to the window */
                    637:        wp->w_bufp = bp;
                    638:
1.3       millert   639:        if (bp->b_nwnd++ == 0) {        /* First use.            */
                    640:                wp->w_dotp = bp->b_dotp;
                    641:                wp->w_doto = bp->b_doto;
1.1       deraadt   642:                wp->w_markp = bp->b_markp;
                    643:                wp->w_marko = bp->b_marko;
1.60      kjell     644:                wp->w_dotline = bp->b_dotline;
                    645:                wp->w_markline = bp->b_markline;
1.1       deraadt   646:        } else
1.3       millert   647:                /* already on screen, steal values from other window */
1.1       deraadt   648:                for (owp = wheadp; owp != NULL; owp = wp->w_wndp)
                    649:                        if (wp->w_bufp == bp && owp != wp) {
1.3       millert   650:                                wp->w_dotp = owp->w_dotp;
                    651:                                wp->w_doto = owp->w_doto;
1.1       deraadt   652:                                wp->w_markp = owp->w_markp;
                    653:                                wp->w_marko = owp->w_marko;
1.60      kjell     654:                                wp->w_dotline = owp->w_dotline;
1.64      kjell     655:                                wp->w_markline = owp->w_markline;
1.1       deraadt   656:                                break;
                    657:                        }
1.70      kjell     658:        wp->w_rflag |= WFMODE | flags;
1.56      kjell     659:        return (TRUE);
                    660: }
                    661:
                    662: /*
                    663:  * Augment a buffer name with a number, if necessary
                    664:  *
                    665:  * If more than one file of the same basename() is open,
                    666:  * the additional buffers are named "file<2>", "file<3>", and
                    667:  * so forth.  This function adjusts a buffer name to
                    668:  * include the number, if necessary.
                    669:  */
                    670: int
1.57      kjell     671: augbname(char *bn, const char *fn, size_t bs)
1.56      kjell     672: {
1.66      deraadt   673:        int      count;
1.56      kjell     674:        size_t   remain, len;
                    675:
1.77      kjell     676:        if ((len = xbasename(bn, fn, bs)) >= bs)
1.56      kjell     677:                return (FALSE);
                    678:
                    679:        remain = bs - len;
                    680:        for (count = 2; bfind(bn, FALSE) != NULL; count++)
                    681:                snprintf(bn + len, remain, "<%d>", count);
                    682:
1.37      db        683:        return (TRUE);
1.1       deraadt   684: }
                    685:
                    686: /*
                    687:  * Pop the buffer we got passed onto the screen.
                    688:  * Returns a status.
                    689:  */
1.52      deraadt   690: struct mgwin *
1.71      kjell     691: popbuf(struct buffer *bp, int flags)
1.3       millert   692: {
1.52      deraadt   693:        struct mgwin    *wp;
1.1       deraadt   694:
1.3       millert   695:        if (bp->b_nwnd == 0) {  /* Not on screen yet.    */
1.71      kjell     696:                /*
                    697:                 * Pick a window for a pop-up.
                    698:                 * If only one window, split the screen.
                    699:                 * Flag the new window as ephemeral
                    700:                 */
                    701:                if (wheadp->w_wndp == NULL &&
                    702:                    splitwind(FFOTHARG, flags) == FALSE)
                    703:                        return (NULL);
                    704:
                    705:                /*
                    706:                 * Pick the uppermost window that isn't
                    707:                 * the current window. An LRU algorithm
                    708:                 * might be better. Return a pointer, or NULL on error.
                    709:                 */
                    710:                wp = wheadp;
                    711:
                    712:                while (wp != NULL && wp == curwp)
                    713:                        wp = wp->w_wndp;
1.1       deraadt   714:        } else
                    715:                for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
                    716:                        if (wp->w_bufp == bp) {
1.70      kjell     717:                                wp->w_rflag |= WFFULL | WFFRAME;
1.37      db        718:                                return (wp);
1.1       deraadt   719:                        }
1.58      kjell     720:        if (showbuffer(bp, wp, WFFULL) != TRUE)
1.37      db        721:                return (NULL);
                    722:        return (wp);
1.1       deraadt   723: }
                    724:
                    725: /*
                    726:  * Insert another buffer at dot.  Very useful.
                    727:  */
1.3       millert   728: /* ARGSUSED */
                    729: int
1.26      vincent   730: bufferinsert(int f, int n)
1.1       deraadt   731: {
1.52      deraadt   732:        struct buffer *bp;
                    733:        struct line   *clp;
1.36      deraadt   734:        int     clo, nline;
                    735:        char    bufn[NBUFN], *bufp;
1.1       deraadt   736:
                    737:        /* Get buffer to use from user */
                    738:        if (curbp->b_altb != NULL)
1.34      vincent   739:                bufp = eread("Insert buffer: (default %s) ", bufn, NBUFN,
1.40      kjell     740:                    EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname);
1.1       deraadt   741:        else
1.38      cloder    742:                bufp = eread("Insert buffer: ", bufn, NBUFN, EFNEW | EFBUF);
1.34      vincent   743:        if (bufp == NULL)
1.37      db        744:                return (ABORT);
1.34      vincent   745:        if (bufp[0] == '\0' && curbp->b_altb != NULL)
1.3       millert   746:                bp = curbp->b_altb;
                    747:        else if ((bp = bfind(bufn, FALSE)) == NULL)
1.37      db        748:                return (FALSE);
1.1       deraadt   749:
1.3       millert   750:        if (bp == curbp) {
1.93      lum       751:                dobeep();
1.1       deraadt   752:                ewprintf("Cannot insert buffer into self");
1.37      db        753:                return (FALSE);
1.1       deraadt   754:        }
                    755:        /* insert the buffer */
                    756:        nline = 0;
1.63      kjell     757:        clp = bfirstlp(bp);
1.3       millert   758:        for (;;) {
1.1       deraadt   759:                for (clo = 0; clo < llength(clp); clo++)
                    760:                        if (linsert(1, lgetc(clp, clo)) == FALSE)
1.37      db        761:                                return (FALSE);
1.62      kjell     762:                if ((clp = lforw(clp)) == bp->b_headp)
1.3       millert   763:                        break;
                    764:                if (newline(FFRAND, 1) == FALSE)        /* fake newline */
1.37      db        765:                        return (FALSE);
1.1       deraadt   766:                nline++;
                    767:        }
1.3       millert   768:        if (nline == 1)
                    769:                ewprintf("[Inserted 1 line]");
                    770:        else
                    771:                ewprintf("[Inserted %d lines]", nline);
1.1       deraadt   772:
1.37      db        773:        clp = curwp->w_linep;           /* cosmetic adjustment  */
1.3       millert   774:        if (curwp->w_dotp == clp) {     /* for offscreen insert */
1.62      kjell     775:                while (nline-- && lback(clp) != curbp->b_headp)
1.1       deraadt   776:                        clp = lback(clp);
1.37      db        777:                curwp->w_linep = clp;   /* adjust framing.      */
1.70      kjell     778:                curwp->w_rflag |= WFFULL;
1.1       deraadt   779:        }
                    780:        return (TRUE);
                    781: }
                    782:
                    783: /*
                    784:  * Turn off the dirty bit on this buffer.
                    785:  */
1.3       millert   786: /* ARGSUSED */
                    787: int
1.26      vincent   788: notmodified(int f, int n)
1.1       deraadt   789: {
1.52      deraadt   790:        struct mgwin *wp;
1.1       deraadt   791:
                    792:        curbp->b_flag &= ~BFCHG;
1.3       millert   793:        wp = wheadp;            /* Update mode lines.    */
1.1       deraadt   794:        while (wp != NULL) {
                    795:                if (wp->w_bufp == curbp)
1.70      kjell     796:                        wp->w_rflag |= WFMODE;
1.1       deraadt   797:                wp = wp->w_wndp;
                    798:        }
                    799:        ewprintf("Modification-flag cleared");
1.37      db        800:        return (TRUE);
1.1       deraadt   801: }
                    802:
                    803: /*
1.78      lum       804:  * Popbuf and set all windows to top of buffer.
1.1       deraadt   805:  */
1.3       millert   806: int
1.71      kjell     807: popbuftop(struct buffer *bp, int flags)
1.1       deraadt   808: {
1.52      deraadt   809:        struct mgwin *wp;
1.1       deraadt   810:
1.63      kjell     811:        bp->b_dotp = bfirstlp(bp);
1.3       millert   812:        bp->b_doto = 0;
                    813:        if (bp->b_nwnd != 0) {
                    814:                for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
                    815:                        if (wp->w_bufp == bp) {
                    816:                                wp->w_dotp = bp->b_dotp;
                    817:                                wp->w_doto = 0;
1.70      kjell     818:                                wp->w_rflag |= WFFULL;
1.3       millert   819:                        }
                    820:        }
1.71      kjell     821:        return (popbuf(bp, flags) != NULL);
1.1       deraadt   822: }
1.57      kjell     823:
                    824: /*
                    825:  * Return the working directory for the current buffer, terminated
                    826:  * with a '/'. First, try to extract it from the current buffer's
                    827:  * filename. If that fails, use global cwd.
                    828:  */
                    829: int
                    830: getbufcwd(char *path, size_t plen)
                    831: {
                    832:        char cwd[NFILEN];
                    833:
                    834:        if (plen == 0)
                    835:                return (FALSE);
                    836:
1.67      kjell     837:        if (globalwd == FALSE && curbp->b_cwd[0] != '\0') {
1.57      kjell     838:                (void)strlcpy(path, curbp->b_cwd, plen);
                    839:        } else {
                    840:                if (getcwdir(cwd, sizeof(cwd)) == FALSE)
                    841:                        goto error;
                    842:                (void)strlcpy(path, cwd, plen);
                    843:        }
                    844:        return (TRUE);
                    845: error:
                    846:        path[0] = '\0';
                    847:        return (FALSE);
                    848: }
                    849:
1.68      kjell     850: /*
1.69      kjell     851:  * Ensures a buffer has not been modified elsewhere; e.g. on disk.
                    852:  * Prompt the user if it has.
                    853:  * Returns TRUE if it has NOT (i.e. buffer is ok to edit).
                    854:  * FALSE or ABORT otherwise
1.68      kjell     855:  */
                    856: int
                    857: checkdirty(struct buffer *bp)
                    858: {
                    859:        int s;
1.83      florian   860:
                    861:        if ((bp->b_flag & (BFCHG | BFDIRTY)) == 0)
                    862:                if (fchecktime(bp) != TRUE)
                    863:                        bp->b_flag |= BFDIRTY;
                    864:
1.68      kjell     865:        if ((bp->b_flag & (BFDIRTY | BFIGNDIRTY)) == BFDIRTY) {
1.84      florian   866:                s = eynorr("File changed on disk; really edit the buffer");
                    867:                switch (s) {
                    868:                case TRUE:
                    869:                        bp->b_flag &= ~BFDIRTY;
                    870:                        bp->b_flag |= BFIGNDIRTY;
                    871:                        return (TRUE);
                    872:                case REVERT:
                    873:                        dorevert();
                    874:                        return (FALSE);
                    875:                default:
1.68      kjell     876:                        return (s);
1.84      florian   877:                }
1.68      kjell     878:        }
                    879:
                    880:        return (TRUE);
                    881: }
1.82      jasper    882:
                    883: /*
                    884:  * Revert the current buffer to whatever is on disk.
                    885:  */
                    886: /* ARGSUSED */
                    887: int
                    888: revertbuffer(int f, int n)
                    889: {
                    890:        char fbuf[NFILEN + 32];
                    891:
1.85      florian   892:        if (curbp->b_fname[0] == 0) {
1.93      lum       893:                dobeep();
1.82      jasper    894:                ewprintf("Cannot revert buffer not associated with any files.");
                    895:                return (FALSE);
                    896:        }
                    897:
1.85      florian   898:        snprintf(fbuf, sizeof(fbuf), "Revert buffer from file %s",
                    899:            curbp->b_fname);
1.82      jasper    900:
1.85      florian   901:        if (eyorn(fbuf) == TRUE)
1.84      florian   902:                return dorevert();
1.82      jasper    903:
1.84      florian   904:        return (FALSE);
                    905: }
1.82      jasper    906:
1.84      florian   907: int
1.86      haesbaer  908: dorevert(void)
1.84      florian   909: {
                    910:        int lineno;
1.89      florian   911:        struct undo_rec *rec;
1.82      jasper    912:
1.85      florian   913:        if (access(curbp->b_fname, F_OK|R_OK) != 0) {
1.93      lum       914:                dobeep();
1.84      florian   915:                if (errno == ENOENT)
                    916:                        ewprintf("File %s no longer exists!",
1.85      florian   917:                            curbp->b_fname);
1.84      florian   918:                else
                    919:                        ewprintf("File %s is no longer readable!",
1.85      florian   920:                            curbp->b_fname);
1.84      florian   921:                return (FALSE);
1.82      jasper    922:        }
                    923:
1.84      florian   924:        /* Save our current line, so we can go back after reloading. */
1.85      florian   925:        lineno = curwp->w_dotline;
1.84      florian   926:
                    927:        /* Prevent readin from asking if we want to kill the buffer. */
                    928:        curbp->b_flag &= ~BFCHG;
1.89      florian   929:
                    930:        /* Clean up undo memory */
                    931:        while ((rec = TAILQ_FIRST(&curbp->b_undo))) {
                    932:                TAILQ_REMOVE(&curbp->b_undo, rec, next);
                    933:                free_undo_record(rec);
                    934:        }
1.84      florian   935:
1.85      florian   936:        if (readin(curbp->b_fname))
1.84      florian   937:                return(setlineno(lineno));
1.82      jasper    938:        return (FALSE);
1.88      florian   939: }
                    940:
                    941: /*
                    942:  * Diff the current buffer to what is on disk.
                    943:  */
                    944: /*ARGSUSED */
                    945: int
                    946: diffbuffer(int f, int n)
                    947: {
                    948:        struct buffer   *bp;
                    949:        struct line     *lp, *lpend;
                    950:        size_t           len;
                    951:        int              ret;
                    952:        char            *text, *ttext;
                    953:        char            * const argv[] =
                    954:            {DIFFTOOL, "-u", "-p", curbp->b_fname, "-", (char *)NULL};
                    955:
                    956:        len = 0;
                    957:
                    958:        /* C-u is not supported */
                    959:        if (n > 1)
                    960:                return (ABORT);
                    961:
                    962:        if (access(DIFFTOOL, X_OK) != 0) {
1.93      lum       963:                dobeep();
1.88      florian   964:                ewprintf("%s not found or not executable.", DIFFTOOL);
                    965:                return (FALSE);
                    966:        }
                    967:
                    968:        if (curbp->b_fname[0] == 0) {
1.93      lum       969:                dobeep();
1.88      florian   970:                ewprintf("Cannot diff buffer not associated with any files.");
                    971:                return (FALSE);
                    972:        }
                    973:
                    974:        lpend = curbp->b_headp;
                    975:        for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) {
                    976:                len+=llength(lp);
                    977:                if (lforw(lp) != lpend)         /* no implied \n on last line */
                    978:                        len++;
                    979:        }
                    980:        if ((text = calloc(len + 1, sizeof(char))) == NULL) {
1.93      lum       981:                dobeep();
1.88      florian   982:                ewprintf("Cannot allocate memory.");
                    983:                return (FALSE);
                    984:        }
                    985:        ttext = text;
                    986:
                    987:        for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) {
                    988:                if (llength(lp) != 0) {
                    989:                        memcpy(ttext, ltext(lp), llength(lp));
                    990:                        ttext += llength(lp);
                    991:                }
                    992:                if (lforw(lp) != lpend)         /* no implied \n on last line */
                    993:                        *ttext++ = '\n';
                    994:        }
                    995:
                    996:        bp = bfind("*Diff*", TRUE);
                    997:        bp->b_flag |= BFREADONLY;
                    998:        if (bclear(bp) != TRUE) {
                    999:                free(text);
                   1000:                return (FALSE);
                   1001:        }
                   1002:
                   1003:        ret = pipeio(DIFFTOOL, argv, text, len, bp);
                   1004:
                   1005:        if (ret == TRUE) {
                   1006:                eerase();
                   1007:                if (lforw(bp->b_headp) == bp->b_headp)
                   1008:                        addline(bp, "Diff finished (no differences).");
                   1009:        }
                   1010:
                   1011:        free(text);
                   1012:        return (ret);
1.92      lum      1013: }
                   1014:
                   1015: /*
                   1016:  * Given a file name, either find the buffer it uses, or create a new
                   1017:  * empty buffer to put it in.
                   1018:  */
                   1019: struct buffer *
                   1020: findbuffer(char *fn)
                   1021: {
                   1022:        struct buffer   *bp;
                   1023:        char            bname[NBUFN], fname[NBUFN];
                   1024:
                   1025:        if (strlcpy(fname, fn, sizeof(fname)) >= sizeof(fname)) {
1.93      lum      1026:                dobeep();
1.92      lum      1027:                ewprintf("filename too long");
                   1028:                return (NULL);
                   1029:        }
                   1030:
                   1031:        for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
                   1032:                if (strcmp(bp->b_fname, fname) == 0)
                   1033:                        return (bp);
                   1034:        }
                   1035:        /* Not found. Create a new one, adjusting name first */
                   1036:        if (augbname(bname, fname, sizeof(bname)) == FALSE)
                   1037:                return (NULL);
                   1038:
                   1039:        bp = bfind(bname, TRUE);
                   1040:        return (bp);
1.82      jasper   1041: }