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

Annotation of src/usr.bin/less/line.c, Revision 1.23

1.1       etheisen    1: /*
1.12      shadchin    2:  * Copyright (C) 1984-2012  Mark Nudelman
1.14      nicm        3:  * Modified for use with illumos by Garrett D'Amore.
                      4:  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
1.1       etheisen    5:  *
1.5       millert     6:  * You may distribute under the terms of either the GNU General Public
                      7:  * License or the Less License, as specified in the README file.
1.1       etheisen    8:  *
1.12      shadchin    9:  * For more information, see the README file.
1.13      nicm       10:  */
1.1       etheisen   11:
                     12: /*
                     13:  * Routines to manipulate the "line buffer".
                     14:  * The line buffer holds a line of output as it is being built
                     15:  * in preparation for output to the screen.
                     16:  */
                     17:
1.23    ! schwarze   18: #include <wchar.h>
        !            19:
1.19      mmcc       20: #include "charset.h"
1.1       etheisen   21: #include "less.h"
                     22:
1.10      shadchin   23: static char *linebuf = NULL;   /* Buffer which holds the current output line */
1.5       millert    24: static char *attr = NULL;      /* Extension of linebuf to hold attributes */
1.13      nicm       25: int size_linebuf = 0;          /* Size of line buffer (and attr buffer) */
1.5       millert    26:
1.10      shadchin   27: static int cshift;             /* Current left-shift of output line buffer */
1.13      nicm       28: int hshift;                    /* Desired left-shift of output line buffer */
                     29: int tabstops[TABSTOP_MAX] = { 0 }; /* Custom tabstops */
                     30: int ntabstops = 1;             /* Number of tabstops */
                     31: int tabdefault = 8;            /* Default repeated tabstops */
                     32: off_t highest_hilite;          /* Pos of last hilite in file found so far */
1.1       etheisen   33:
                     34: static int curr;               /* Index into linebuf */
1.13      nicm       35: static int column;     /* Printable length, accounting for backspaces, etc. */
1.1       etheisen   36: static int overstrike;         /* Next char should overstrike previous char */
                     37: static int is_null_line;       /* There is no current line */
1.5       millert    38: static int lmargin;            /* Left margin */
1.1       etheisen   39: static char pendc;
1.13      nicm       40: static off_t pendpos;
1.5       millert    41: static char *end_ansi_chars;
1.10      shadchin   42: static char *mid_ansi_chars;
1.1       etheisen   43:
1.13      nicm       44: static int attr_swidth(int);
                     45: static int attr_ewidth(int);
                     46: static int do_append(LWCHAR, char *, off_t);
1.1       etheisen   47:
1.11      millert    48: extern volatile sig_atomic_t sigs;
1.1       etheisen   49: extern int bs_mode;
                     50: extern int linenums;
                     51: extern int ctldisp;
                     52: extern int twiddle;
                     53: extern int binattr;
1.5       millert    54: extern int status_col;
1.1       etheisen   55: extern int auto_wrap, ignaw;
                     56: extern int bo_s_width, bo_e_width;
                     57: extern int ul_s_width, ul_e_width;
                     58: extern int bl_s_width, bl_e_width;
                     59: extern int so_s_width, so_e_width;
                     60: extern int sc_width, sc_height;
1.5       millert    61: extern int utf_mode;
1.13      nicm       62: extern off_t start_attnpos;
                     63: extern off_t end_attnpos;
1.5       millert    64:
1.10      shadchin   65: static char mbc_buf[MAX_UTF_CHAR_LEN];
                     66: static int mbc_buf_len = 0;
                     67: static int mbc_buf_index = 0;
1.13      nicm       68: static off_t mbc_pos;
1.10      shadchin   69:
1.5       millert    70: /*
                     71:  * Initialize from environment variables.
                     72:  */
1.13      nicm       73: void
                     74: init_line(void)
1.5       millert    75: {
                     76:        end_ansi_chars = lgetenv("LESSANSIENDCHARS");
                     77:        if (end_ansi_chars == NULL || *end_ansi_chars == '\0')
                     78:                end_ansi_chars = "m";
1.10      shadchin   79:
                     80:        mid_ansi_chars = lgetenv("LESSANSIMIDCHARS");
                     81:        if (mid_ansi_chars == NULL || *mid_ansi_chars == '\0')
                     82:                mid_ansi_chars = "0123456789;[?!\"'#%()*+ ";
                     83:
1.13      nicm       84:        linebuf = ecalloc(LINEBUF_SIZE, sizeof (char));
                     85:        attr = ecalloc(LINEBUF_SIZE, sizeof (char));
1.5       millert    86:        size_linebuf = LINEBUF_SIZE;
                     87: }
                     88:
                     89: /*
                     90:  * Expand the line buffer.
                     91:  */
1.13      nicm       92: static int
                     93: expand_linebuf(void)
1.5       millert    94: {
1.10      shadchin   95:        /* Double the size of the line buffer. */
1.7       millert    96:        int new_size = size_linebuf * 2;
1.10      shadchin   97:
                     98:        /* Just realloc to expand the buffer, if we can. */
1.22      deraadt    99:        char *new_buf = recallocarray(linebuf, size_linebuf, new_size, 1);
                    100:        char *new_attr = recallocarray(attr, size_linebuf, new_size, 1);
1.13      nicm      101:        if (new_buf == NULL || new_attr == NULL) {
1.15      mmcc      102:                free(new_attr);
                    103:                free(new_buf);
1.13      nicm      104:                return (1);
1.5       millert   105:        }
                    106:        linebuf = new_buf;
                    107:        attr = new_attr;
                    108:        size_linebuf = new_size;
1.13      nicm      109:        return (0);
1.5       millert   110: }
1.1       etheisen  111:
                    112: /*
1.10      shadchin  113:  * Is a character ASCII?
                    114:  */
1.13      nicm      115: int
                    116: is_ascii_char(LWCHAR ch)
1.10      shadchin  117: {
                    118:        return (ch <= 0x7F);
                    119: }
                    120:
                    121: /*
1.1       etheisen  122:  * Rewind the line buffer.
                    123:  */
1.13      nicm      124: void
                    125: prewind(void)
1.1       etheisen  126: {
                    127:        curr = 0;
                    128:        column = 0;
1.10      shadchin  129:        cshift = 0;
1.1       etheisen  130:        overstrike = 0;
1.10      shadchin  131:        mbc_buf_len = 0;
1.1       etheisen  132:        is_null_line = 0;
                    133:        pendc = '\0';
1.5       millert   134:        lmargin = 0;
                    135:        if (status_col)
                    136:                lmargin += 1;
1.1       etheisen  137: }
                    138:
                    139: /*
                    140:  * Insert the line number (of the given position) into the line buffer.
                    141:  */
1.13      nicm      142: void
                    143: plinenum(off_t pos)
1.1       etheisen  144: {
1.16      mmcc      145:        off_t linenum = 0;
1.13      nicm      146:        int i;
1.5       millert   147:
1.13      nicm      148:        if (linenums == OPT_ONPLUS) {
1.5       millert   149:                /*
                    150:                 * Get the line number and put it in the current line.
                    151:                 * {{ Note: since find_linenum calls forw_raw_line,
1.13      nicm      152:                 *    it may seek in the input file, requiring the caller
1.5       millert   153:                 *    of plinenum to re-seek if necessary. }}
                    154:                 * {{ Since forw_raw_line modifies linebuf, we must
                    155:                 *    do this first, before storing anything in linebuf. }}
                    156:                 */
                    157:                linenum = find_linenum(pos);
                    158:        }
1.1       etheisen  159:
                    160:        /*
1.5       millert   161:         * Display a status column if the -J option is set.
1.1       etheisen  162:         */
1.13      nicm      163:        if (status_col) {
1.5       millert   164:                linebuf[curr] = ' ';
1.13      nicm      165:                if (start_attnpos != -1 &&
1.5       millert   166:                    pos >= start_attnpos && pos < end_attnpos)
1.10      shadchin  167:                        attr[curr] = AT_NORMAL|AT_HILITE;
1.5       millert   168:                else
1.10      shadchin  169:                        attr[curr] = AT_NORMAL;
1.5       millert   170:                curr++;
                    171:                column++;
                    172:        }
1.1       etheisen  173:        /*
1.5       millert   174:         * Display the line number at the start of each line
                    175:         * if the -N option is set.
1.1       etheisen  176:         */
1.13      nicm      177:        if (linenums == OPT_ONPLUS) {
1.18      mmcc      178:                char buf[23];
1.5       millert   179:                int n;
1.1       etheisen  180:
1.17      mmcc      181:                postoa(linenum, buf, sizeof(buf));
1.5       millert   182:                n = strlen(buf);
                    183:                if (n < MIN_LINENUM_WIDTH)
                    184:                        n = MIN_LINENUM_WIDTH;
1.6       millert   185:                snprintf(linebuf+curr, size_linebuf-curr, "%*s ", n, buf);
1.21      deraadt   186:                n++;    /* One space after the line number. */
1.5       millert   187:                for (i = 0; i < n; i++)
                    188:                        attr[curr+i] = AT_NORMAL;
                    189:                curr += n;
                    190:                column += n;
                    191:                lmargin += n;
                    192:        }
1.1       etheisen  193:
                    194:        /*
1.5       millert   195:         * Append enough spaces to bring us to the lmargin.
1.1       etheisen  196:         */
1.13      nicm      197:        while (column < lmargin) {
1.1       etheisen  198:                linebuf[curr] = ' ';
                    199:                attr[curr++] = AT_NORMAL;
                    200:                column++;
1.5       millert   201:        }
                    202: }
                    203:
                    204: /*
1.10      shadchin  205:  * Shift the input line left.
                    206:  * This means discarding N printable chars at the start of the buffer.
1.5       millert   207:  */
1.13      nicm      208: static void
                    209: pshift(int shift)
1.10      shadchin  210: {
                    211:        LWCHAR prev_ch = 0;
                    212:        unsigned char c;
                    213:        int shifted = 0;
                    214:        int to;
                    215:        int from;
1.5       millert   216:        int len;
1.10      shadchin  217:        int width;
                    218:        int prev_attr;
                    219:        int next_attr;
                    220:
                    221:        if (shift > column - lmargin)
                    222:                shift = column - lmargin;
                    223:        if (shift > curr - lmargin)
                    224:                shift = curr - lmargin;
1.5       millert   225:
1.10      shadchin  226:        to = from = lmargin;
1.5       millert   227:        /*
1.10      shadchin  228:         * We keep on going when shifted == shift
                    229:         * to get all combining chars.
1.5       millert   230:         */
1.13      nicm      231:        while (shifted <= shift && from < curr) {
1.10      shadchin  232:                c = linebuf[from];
1.13      nicm      233:                if (ctldisp == OPT_ONPLUS && IS_CSI_START(c)) {
1.10      shadchin  234:                        /* Keep cumulative effect.  */
                    235:                        linebuf[to] = c;
                    236:                        attr[to++] = attr[from++];
1.13      nicm      237:                        while (from < curr && linebuf[from]) {
1.10      shadchin  238:                                linebuf[to] = linebuf[from];
                    239:                                attr[to++] = attr[from];
                    240:                                if (!is_ansi_middle(linebuf[from++]))
                    241:                                        break;
1.13      nicm      242:                        }
1.10      shadchin  243:                        continue;
                    244:                }
                    245:
                    246:                width = 0;
                    247:
1.13      nicm      248:                if (!IS_ASCII_OCTET(c) && utf_mode) {
1.10      shadchin  249:                        /* Assumes well-formedness validation already done.  */
                    250:                        LWCHAR ch;
                    251:
                    252:                        len = utf_len(c);
                    253:                        if (from + len > curr)
                    254:                                break;
                    255:                        ch = get_wchar(linebuf + from);
1.13      nicm      256:                        if (!is_composing_char(ch) &&
                    257:                            !is_combining_char(prev_ch, ch))
1.10      shadchin  258:                                width = is_wide_char(ch) ? 2 : 1;
                    259:                        prev_ch = ch;
1.13      nicm      260:                } else {
1.10      shadchin  261:                        len = 1;
                    262:                        if (c == '\b')
                    263:                                /* XXX - Incorrect if several '\b' in a row.  */
1.13      nicm      264:                                width = (utf_mode && is_wide_char(prev_ch)) ?
                    265:                                    -2 : -1;
1.10      shadchin  266:                        else if (!control_char(c))
                    267:                                width = 1;
                    268:                        prev_ch = 0;
                    269:                }
                    270:
                    271:                if (width == 2 && shift - shifted == 1) {
                    272:                        /* Should never happen when called by pshift_all().  */
                    273:                        attr[to] = attr[from];
                    274:                        /*
                    275:                         * Assume a wide_char will never be the first half of a
                    276:                         * combining_char pair, so reset prev_ch in case we're
                    277:                         * followed by a '\b'.
                    278:                         */
                    279:                        prev_ch = linebuf[to++] = ' ';
                    280:                        from += len;
                    281:                        shifted++;
                    282:                        continue;
                    283:                }
                    284:
                    285:                /* Adjust width for magic cookies. */
                    286:                prev_attr = (to > 0) ? attr[to-1] : AT_NORMAL;
                    287:                next_attr = (from + len < curr) ? attr[from + len] : prev_attr;
1.13      nicm      288:                if (!is_at_equiv(attr[from], prev_attr) &&
                    289:                    !is_at_equiv(attr[from], next_attr)) {
1.10      shadchin  290:                        width += attr_swidth(attr[from]);
                    291:                        if (from + len < curr)
                    292:                                width += attr_ewidth(attr[from]);
1.13      nicm      293:                        if (is_at_equiv(prev_attr, next_attr)) {
1.10      shadchin  294:                                width += attr_ewidth(prev_attr);
                    295:                                if (from + len < curr)
                    296:                                        width += attr_swidth(next_attr);
1.5       millert   297:                        }
                    298:                }
                    299:
1.10      shadchin  300:                if (shift - shifted < width)
                    301:                        break;
                    302:                from += len;
                    303:                shifted += width;
                    304:                if (shifted < 0)
                    305:                        shifted = 0;
                    306:        }
1.13      nicm      307:        while (from < curr) {
1.10      shadchin  308:                linebuf[to] = linebuf[from];
                    309:                attr[to++] = attr[from++];
                    310:        }
                    311:        curr = to;
                    312:        column -= shifted;
                    313:        cshift += shifted;
1.5       millert   314: }
                    315:
                    316: /*
1.10      shadchin  317:  *
1.5       millert   318:  */
1.13      nicm      319: void
                    320: pshift_all(void)
1.5       millert   321: {
1.10      shadchin  322:        pshift(column);
1.1       etheisen  323: }
                    324:
                    325: /*
                    326:  * Return the printing width of the start (enter) sequence
                    327:  * for a given character attribute.
                    328:  */
1.13      nicm      329: static int
                    330: attr_swidth(int a)
1.1       etheisen  331: {
1.10      shadchin  332:        int w = 0;
                    333:
                    334:        a = apply_at_specials(a);
                    335:
                    336:        if (a & AT_UNDERLINE)
                    337:                w += ul_s_width;
                    338:        if (a & AT_BOLD)
                    339:                w += bo_s_width;
                    340:        if (a & AT_BLINK)
                    341:                w += bl_s_width;
                    342:        if (a & AT_STANDOUT)
                    343:                w += so_s_width;
                    344:
1.13      nicm      345:        return (w);
1.1       etheisen  346: }
                    347:
                    348: /*
                    349:  * Return the printing width of the end (exit) sequence
                    350:  * for a given character attribute.
                    351:  */
1.13      nicm      352: static int
                    353: attr_ewidth(int a)
1.1       etheisen  354: {
1.10      shadchin  355:        int w = 0;
                    356:
                    357:        a = apply_at_specials(a);
                    358:
                    359:        if (a & AT_UNDERLINE)
                    360:                w += ul_e_width;
                    361:        if (a & AT_BOLD)
                    362:                w += bo_e_width;
                    363:        if (a & AT_BLINK)
                    364:                w += bl_e_width;
                    365:        if (a & AT_STANDOUT)
                    366:                w += so_e_width;
                    367:
1.13      nicm      368:        return (w);
1.1       etheisen  369: }
                    370:
                    371: /*
                    372:  * Return the printing width of a given character and attribute,
                    373:  * if the character were added to the current position in the line buffer.
                    374:  * Adding a character with a given attribute may cause an enter or exit
                    375:  * attribute sequence to be inserted, so this must be taken into account.
                    376:  */
1.13      nicm      377: static int
1.23    ! schwarze  378: pwidth(wchar_t ch, int a, wchar_t prev_ch)
1.1       etheisen  379: {
1.10      shadchin  380:        int w;
1.1       etheisen  381:
1.23    ! schwarze  382:        /*
        !           383:         * In case of a backspace, back up by the width of the previous
        !           384:         * character.  If that is non-printable (for example another
        !           385:         * backspace) or zero width (for example a combining accent),
        !           386:         * the terminal may actually back up to a character even further
        !           387:         * back, but we no longer know how wide that may have been.
        !           388:         * The best guess possible at this point is that it was
        !           389:         * hopefully width one.
        !           390:         */
        !           391:        if (ch == L'\b') {
        !           392:                w = wcwidth(prev_ch);
        !           393:                if (w <= 0)
        !           394:                        w = 1;
        !           395:                return (-w);
        !           396:        }
        !           397:
        !           398:        w = wcwidth(ch);
        !           399:
        !           400:        /*
        !           401:         * Non-printable characters can get here if the -r flag is in
        !           402:         * effect, and possibly in some other situations (XXX check that!).
        !           403:         * Treat them as zero width.
        !           404:         * That may not always match their actual behaviour,
        !           405:         * but there is no reasonable way to be more exact.
        !           406:         */
        !           407:        if (w == -1)
        !           408:                w = 0;
1.13      nicm      409:
1.23    ! schwarze  410:        /*
        !           411:         * Combining accents take up no space.
        !           412:         * Some terminals, upon failure to compose them with the
        !           413:         * characters that precede them, will actually take up one column
        !           414:         * for the combining accent; there isn't much we could do short
        !           415:         * of testing the (complex) composition process ourselves and
        !           416:         * printing a binary representation when it fails.
        !           417:         */
        !           418:        if (w == 0)
        !           419:                return (0);
1.1       etheisen  420:
                    421:        /*
1.10      shadchin  422:         * Other characters take one or two columns,
1.1       etheisen  423:         * plus the width of any attribute enter/exit sequence.
                    424:         */
1.10      shadchin  425:        if (curr > 0 && !is_at_equiv(attr[curr-1], a))
1.1       etheisen  426:                w += attr_ewidth(attr[curr-1]);
1.10      shadchin  427:        if ((apply_at_specials(a) != AT_NORMAL) &&
                    428:            (curr == 0 || !is_at_equiv(attr[curr-1], a)))
1.1       etheisen  429:                w += attr_swidth(a);
                    430:        return (w);
                    431: }
                    432:
                    433: /*
1.10      shadchin  434:  * Delete to the previous base character in the line buffer.
                    435:  * Return 1 if one is found.
1.1       etheisen  436:  */
1.13      nicm      437: static int
                    438: backc(void)
1.1       etheisen  439: {
1.10      shadchin  440:        LWCHAR prev_ch;
                    441:        char *p = linebuf + curr;
                    442:        LWCHAR ch = step_char(&p, -1, linebuf + lmargin);
                    443:        int width;
                    444:
                    445:        /* This assumes that there is no '\b' in linebuf.  */
1.13      nicm      446:        while (curr > lmargin && column > lmargin &&
                    447:            (!(attr[curr - 1] & (AT_ANSI|AT_BINARY)))) {
1.10      shadchin  448:                curr = p - linebuf;
                    449:                prev_ch = step_char(&p, -1, linebuf + lmargin);
                    450:                width = pwidth(ch, attr[curr], prev_ch);
                    451:                column -= width;
                    452:                if (width > 0)
1.13      nicm      453:                        return (1);
1.10      shadchin  454:                ch = prev_ch;
                    455:        }
                    456:
1.13      nicm      457:        return (0);
1.1       etheisen  458: }
                    459:
                    460: /*
1.5       millert   461:  * Are we currently within a recognized ANSI escape sequence?
                    462:  */
1.13      nicm      463: static int
                    464: in_ansi_esc_seq(void)
1.5       millert   465: {
1.10      shadchin  466:        char *p;
1.5       millert   467:
                    468:        /*
                    469:         * Search backwards for either an ESC (which means we ARE in a seq);
                    470:         * or an end char (which means we're NOT in a seq).
                    471:         */
1.21      deraadt   472:        for (p = &linebuf[curr]; p > linebuf; ) {
1.10      shadchin  473:                LWCHAR ch = step_char(&p, -1, linebuf);
                    474:                if (IS_CSI_START(ch))
1.5       millert   475:                        return (1);
1.10      shadchin  476:                if (!is_ansi_middle(ch))
1.5       millert   477:                        return (0);
                    478:        }
                    479:        return (0);
                    480: }
                    481:
                    482: /*
                    483:  * Is a character the end of an ANSI escape sequence?
                    484:  */
1.13      nicm      485: int
                    486: is_ansi_end(LWCHAR ch)
1.10      shadchin  487: {
                    488:        if (!is_ascii_char(ch))
                    489:                return (0);
1.13      nicm      490:        return (strchr(end_ansi_chars, (char)ch) != NULL);
1.10      shadchin  491: }
                    492:
                    493: /*
                    494:  *
                    495:  */
1.13      nicm      496: int
                    497: is_ansi_middle(LWCHAR ch)
1.5       millert   498: {
1.10      shadchin  499:        if (!is_ascii_char(ch))
                    500:                return (0);
                    501:        if (is_ansi_end(ch))
                    502:                return (0);
1.13      nicm      503:        return (strchr(mid_ansi_chars, (char)ch) != NULL);
1.5       millert   504: }
                    505:
                    506: /*
1.1       etheisen  507:  * Append a character and attribute to the line buffer.
                    508:  */
1.13      nicm      509: #define        STORE_CHAR(ch, a, rep, pos)                             \
                    510:                if (store_char((ch), (a), (rep), (pos)))        \
                    511:                        return (1)
                    512:
                    513: static int
                    514: store_char(LWCHAR ch, char a, char *rep, off_t pos)
1.1       etheisen  515: {
1.10      shadchin  516:        int w;
                    517:        int replen;
                    518:        char cs;
1.13      nicm      519:        int matches;
1.10      shadchin  520:
1.13      nicm      521:        if (is_hilited(pos, pos+1, 0, &matches)) {
                    522:                /*
                    523:                 * This character should be highlighted.
                    524:                 * Override the attribute passed in.
                    525:                 */
                    526:                if (a != AT_ANSI) {
                    527:                        if (highest_hilite != -1 && pos > highest_hilite)
                    528:                                highest_hilite = pos;
                    529:                        a |= AT_HILITE;
1.10      shadchin  530:                }
1.5       millert   531:        }
1.10      shadchin  532:
1.13      nicm      533:        if (ctldisp == OPT_ONPLUS && in_ansi_esc_seq()) {
1.10      shadchin  534:                if (!is_ansi_end(ch) && !is_ansi_middle(ch)) {
                    535:                        /* Remove whole unrecognized sequence.  */
                    536:                        char *p = &linebuf[curr];
                    537:                        LWCHAR bch;
                    538:                        do {
                    539:                                bch = step_char(&p, -1, linebuf);
                    540:                        } while (p > linebuf && !IS_CSI_START(bch));
                    541:                        curr = p - linebuf;
1.13      nicm      542:                        return (0);
1.10      shadchin  543:                }
                    544:                a = AT_ANSI;    /* Will force re-AT_'ing around it.  */
1.5       millert   545:                w = 0;
1.13      nicm      546:        } else if (ctldisp == OPT_ONPLUS && IS_CSI_START(ch)) {
1.10      shadchin  547:                a = AT_ANSI;    /* Will force re-AT_'ing around it.  */
                    548:                w = 0;
1.13      nicm      549:        } else {
1.10      shadchin  550:                char *p = &linebuf[curr];
                    551:                LWCHAR prev_ch = step_char(&p, -1, linebuf);
                    552:                w = pwidth(ch, a, prev_ch);
                    553:        }
                    554:
1.5       millert   555:        if (ctldisp != OPT_ON && column + w + attr_ewidth(a) > sc_width)
1.1       etheisen  556:                /*
                    557:                 * Won't fit on screen.
                    558:                 */
                    559:                return (1);
                    560:
1.13      nicm      561:        if (rep == NULL) {
                    562:                cs = (char)ch;
1.10      shadchin  563:                rep = &cs;
                    564:                replen = 1;
1.13      nicm      565:        } else {
1.10      shadchin  566:                replen = utf_len(rep[0]);
                    567:        }
1.13      nicm      568:        if (curr + replen >= size_linebuf-6) {
1.1       etheisen  569:                /*
                    570:                 * Won't fit in line buffer.
1.5       millert   571:                 * Try to expand it.
1.1       etheisen  572:                 */
1.5       millert   573:                if (expand_linebuf())
                    574:                        return (1);
                    575:        }
1.1       etheisen  576:
1.13      nicm      577:        while (replen-- > 0) {
1.10      shadchin  578:                linebuf[curr] = *rep++;
                    579:                attr[curr] = a;
                    580:                curr++;
1.1       etheisen  581:        }
                    582:        column += w;
                    583:        return (0);
                    584: }
                    585:
                    586: /*
1.5       millert   587:  * Append a tab to the line buffer.
                    588:  * Store spaces to represent the tab.
                    589:  */
1.13      nicm      590: #define        STORE_TAB(a, pos)               \
                    591:        if (store_tab((a), (pos)))      \
                    592:                return (1)
1.5       millert   593:
1.13      nicm      594: static int
                    595: store_tab(int attr, off_t pos)
1.5       millert   596: {
                    597:        int to_tab = column + cshift - lmargin;
                    598:        int i;
                    599:
                    600:        if (ntabstops < 2 || to_tab >= tabstops[ntabstops-1])
                    601:                to_tab = tabdefault -
1.13      nicm      602:                    ((to_tab - tabstops[ntabstops-1]) % tabdefault);
                    603:        else {
1.21      deraadt   604:                for (i = ntabstops - 2; i >= 0; i--)
1.5       millert   605:                        if (to_tab >= tabstops[i])
                    606:                                break;
                    607:                to_tab = tabstops[i+1] - to_tab;
                    608:        }
                    609:
1.13      nicm      610:        if (column + to_tab - 1 + pwidth(' ', attr, 0) +
                    611:            attr_ewidth(attr) > sc_width)
                    612:                return (1);
1.10      shadchin  613:
1.5       millert   614:        do {
1.10      shadchin  615:                STORE_CHAR(' ', attr, " ", pos);
1.5       millert   616:        } while (--to_tab > 0);
1.13      nicm      617:        return (0);
1.5       millert   618: }
                    619:
1.13      nicm      620: #define        STORE_PRCHAR(c, pos)            \
                    621:        if (store_prchar((c), (pos)))   \
                    622:                return (1)
1.10      shadchin  623:
1.13      nicm      624: static int
                    625: store_prchar(char c, off_t pos)
1.10      shadchin  626: {
                    627:        char *s;
                    628:
                    629:        /*
                    630:         * Convert to printable representation.
                    631:         */
                    632:        s = prchar(c);
                    633:
                    634:        /*
                    635:         * Make sure we can get the entire representation
                    636:         * of the character on this line.
                    637:         */
1.13      nicm      638:        if (column + (int)strlen(s) - 1 +
                    639:            pwidth(' ', binattr, 0) + attr_ewidth(binattr) > sc_width)
                    640:                return (1);
1.10      shadchin  641:
1.13      nicm      642:        for (; *s != 0; s++) {
1.10      shadchin  643:                STORE_CHAR(*s, AT_BINARY, NULL, pos);
1.13      nicm      644:        }
                    645:        return (0);
1.10      shadchin  646: }
                    647:
1.13      nicm      648: static int
                    649: flush_mbc_buf(off_t pos)
1.10      shadchin  650: {
                    651:        int i;
                    652:
                    653:        for (i = 0; i < mbc_buf_index; i++)
                    654:                if (store_prchar(mbc_buf[i], pos))
1.13      nicm      655:                        return (mbc_buf_index - i);
1.10      shadchin  656:
1.13      nicm      657:        return (0);
1.10      shadchin  658: }
                    659:
1.5       millert   660: /*
1.1       etheisen  661:  * Append a character to the line buffer.
                    662:  * Expand tabs into spaces, handle underlining, boldfacing, etc.
                    663:  * Returns 0 if ok, 1 if couldn't fit in buffer.
                    664:  */
1.13      nicm      665: int
                    666: pappend(char c, off_t pos)
1.1       etheisen  667: {
1.5       millert   668:        int r;
                    669:
1.13      nicm      670:        if (pendc) {
1.10      shadchin  671:                if (do_append(pendc, NULL, pendpos))
1.1       etheisen  672:                        /*
                    673:                         * Oops.  We've probably lost the char which
                    674:                         * was in pendc, since caller won't back up.
                    675:                         */
                    676:                        return (1);
                    677:                pendc = '\0';
                    678:        }
                    679:
1.13      nicm      680:        if (c == '\r' && bs_mode == BS_SPECIAL) {
                    681:                if (mbc_buf_len > 0)  /* utf_mode must be on. */ {
1.10      shadchin  682:                        /* Flush incomplete (truncated) sequence. */
                    683:                        r = flush_mbc_buf(mbc_pos);
                    684:                        mbc_buf_index = r + 1;
                    685:                        mbc_buf_len = 0;
                    686:                        if (r)
                    687:                                return (mbc_buf_index);
                    688:                }
                    689:
1.1       etheisen  690:                /*
1.13      nicm      691:                 * Don't put the CR into the buffer until we see
1.1       etheisen  692:                 * the next char.  If the next char is a newline,
                    693:                 * discard the CR.
                    694:                 */
                    695:                pendc = c;
                    696:                pendpos = pos;
                    697:                return (0);
                    698:        }
                    699:
1.13      nicm      700:        if (!utf_mode) {
1.10      shadchin  701:                r = do_append((LWCHAR) c, NULL, pos);
1.13      nicm      702:        } else {
1.10      shadchin  703:                /* Perform strict validation in all possible cases. */
1.13      nicm      704:                if (mbc_buf_len == 0) {
                    705: retry:
1.10      shadchin  706:                        mbc_buf_index = 1;
                    707:                        *mbc_buf = c;
1.13      nicm      708:                        if (IS_ASCII_OCTET(c)) {
1.10      shadchin  709:                                r = do_append((LWCHAR) c, NULL, pos);
1.13      nicm      710:                        } else if (IS_UTF8_LEAD(c)) {
1.10      shadchin  711:                                mbc_buf_len = utf_len(c);
                    712:                                mbc_pos = pos;
                    713:                                return (0);
1.13      nicm      714:                        } else {
1.10      shadchin  715:                                /* UTF8_INVALID or stray UTF8_TRAIL */
                    716:                                r = flush_mbc_buf(pos);
1.13      nicm      717:                        }
                    718:                } else if (IS_UTF8_TRAIL(c)) {
1.10      shadchin  719:                        mbc_buf[mbc_buf_index++] = c;
                    720:                        if (mbc_buf_index < mbc_buf_len)
                    721:                                return (0);
                    722:                        if (is_utf8_well_formed(mbc_buf))
1.13      nicm      723:                                r = do_append(get_wchar(mbc_buf), mbc_buf,
                    724:                                    mbc_pos);
1.10      shadchin  725:                        else
                    726:                                /* Complete, but not shortest form, sequence. */
                    727:                                mbc_buf_index = r = flush_mbc_buf(mbc_pos);
                    728:                        mbc_buf_len = 0;
1.13      nicm      729:                } else {
1.10      shadchin  730:                        /* Flush incomplete (truncated) sequence.  */
                    731:                        r = flush_mbc_buf(mbc_pos);
                    732:                        mbc_buf_index = r + 1;
                    733:                        mbc_buf_len = 0;
                    734:                        /* Handle new char.  */
                    735:                        if (!r)
                    736:                                goto retry;
1.13      nicm      737:                }
1.10      shadchin  738:        }
                    739:
1.5       millert   740:        /*
                    741:         * If we need to shift the line, do it.
                    742:         * But wait until we get to at least the middle of the screen,
                    743:         * so shifting it doesn't affect the chars we're currently
                    744:         * pappending.  (Bold & underline can get messed up otherwise.)
                    745:         */
1.13      nicm      746:        if (cshift < hshift && column > sc_width / 2) {
1.5       millert   747:                linebuf[curr] = '\0';
                    748:                pshift(hshift - cshift);
                    749:        }
1.13      nicm      750:        if (r) {
1.10      shadchin  751:                /* How many chars should caller back up? */
                    752:                r = (!utf_mode) ? 1 : mbc_buf_index;
                    753:        }
1.5       millert   754:        return (r);
1.1       etheisen  755: }
                    756:
1.13      nicm      757: static int
                    758: do_append(LWCHAR ch, char *rep, off_t pos)
1.1       etheisen  759: {
1.13      nicm      760:        int a;
1.10      shadchin  761:        LWCHAR prev_ch;
1.1       etheisen  762:
1.10      shadchin  763:        a = AT_NORMAL;
1.1       etheisen  764:
1.13      nicm      765:        if (ch == '\b') {
1.10      shadchin  766:                if (bs_mode == BS_CONTROL)
1.5       millert   767:                        goto do_control_char;
1.10      shadchin  768:
                    769:                /*
                    770:                 * A better test is needed here so we don't
                    771:                 * backspace over part of the printed
                    772:                 * representation of a binary character.
                    773:                 */
1.13      nicm      774:                if (curr <= lmargin ||
                    775:                    column <= lmargin ||
                    776:                    (attr[curr - 1] & (AT_ANSI|AT_BINARY))) {
1.10      shadchin  777:                        STORE_PRCHAR('\b', pos);
1.13      nicm      778:                } else if (bs_mode == BS_NORMAL) {
1.10      shadchin  779:                        STORE_CHAR(ch, AT_NORMAL, NULL, pos);
1.13      nicm      780:                } else if (bs_mode == BS_SPECIAL) {
1.10      shadchin  781:                        overstrike = backc();
1.13      nicm      782:                }
1.10      shadchin  783:
1.13      nicm      784:                return (0);
1.10      shadchin  785:        }
                    786:
1.13      nicm      787:        if (overstrike > 0) {
1.1       etheisen  788:                /*
                    789:                 * Overstrike the character at the current position
1.13      nicm      790:                 * in the line buffer.  This will cause either
                    791:                 * underline (if a "_" is overstruck),
1.1       etheisen  792:                 * bold (if an identical character is overstruck),
                    793:                 * or just deletion of the character in the buffer.
                    794:                 */
1.10      shadchin  795:                overstrike = utf_mode ? -1 : 0;
                    796:                /* To be correct, this must be a base character.  */
                    797:                prev_ch = get_wchar(linebuf + curr);
                    798:                a = attr[curr];
1.13      nicm      799:                if (ch == prev_ch) {
1.5       millert   800:                        /*
                    801:                         * Overstriking a char with itself means make it bold.
                    802:                         * But overstriking an underscore with itself is
                    803:                         * ambiguous.  It could mean make it bold, or
                    804:                         * it could mean make it underlined.
                    805:                         * Use the previous overstrike to resolve it.
                    806:                         */
1.13      nicm      807:                        if (ch == '_') {
1.10      shadchin  808:                                if ((a & (AT_BOLD|AT_UNDERLINE)) != AT_NORMAL)
                    809:                                        a |= (AT_BOLD|AT_UNDERLINE);
1.20      natano    810:                                else if (curr > 0 && attr[curr - 1] & AT_UNDERLINE)
                    811:                                        a |= AT_UNDERLINE;
                    812:                                else if (curr > 0 && attr[curr - 1] & AT_BOLD)
                    813:                                        a |= AT_BOLD;
1.10      shadchin  814:                                else
1.20      natano    815:                                        a |= AT_INDET;
1.13      nicm      816:                        } else {
1.10      shadchin  817:                                a |= AT_BOLD;
1.13      nicm      818:                        }
                    819:                } else if (ch == '_') {
1.10      shadchin  820:                        a |= AT_UNDERLINE;
                    821:                        ch = prev_ch;
                    822:                        rep = linebuf + curr;
1.13      nicm      823:                } else if (prev_ch == '_') {
1.10      shadchin  824:                        a |= AT_UNDERLINE;
                    825:                }
                    826:                /* Else we replace prev_ch, but we keep its attributes.  */
1.13      nicm      827:        } else if (overstrike < 0) {
                    828:                if (is_composing_char(ch) ||
1.20      natano    829:                    is_combining_char(get_wchar(linebuf + curr), ch)) {
1.10      shadchin  830:                        /* Continuation of the same overstrike.  */
1.20      natano    831:                        if (curr > 0)
                    832:                                a = attr[curr - 1] & (AT_UNDERLINE | AT_BOLD);
                    833:                        else
                    834:                                a = AT_NORMAL;
                    835:                } else
1.10      shadchin  836:                        overstrike = 0;
                    837:        }
                    838:
1.13      nicm      839:        if (ch == '\t') {
1.5       millert   840:                /*
                    841:                 * Expand a tab into spaces.
                    842:                 */
1.13      nicm      843:                switch (bs_mode) {
1.1       etheisen  844:                case BS_CONTROL:
                    845:                        goto do_control_char;
1.5       millert   846:                case BS_NORMAL:
1.1       etheisen  847:                case BS_SPECIAL:
1.10      shadchin  848:                        STORE_TAB(a, pos);
1.1       etheisen  849:                        break;
                    850:                }
1.13      nicm      851:        } else if ((!utf_mode || is_ascii_char(ch)) && control_char((char)ch)) {
                    852: do_control_char:
                    853:                if (ctldisp == OPT_ON ||
                    854:                    (ctldisp == OPT_ONPLUS && IS_CSI_START(ch))) {
1.1       etheisen  855:                        /*
                    856:                         * Output as a normal character.
                    857:                         */
1.10      shadchin  858:                        STORE_CHAR(ch, AT_NORMAL, rep, pos);
1.13      nicm      859:                } else {
                    860:                        STORE_PRCHAR((char)ch, pos);
1.10      shadchin  861:                }
1.13      nicm      862:        } else if (utf_mode && ctldisp != OPT_ON && is_ubin_char(ch)) {
1.10      shadchin  863:                char *s;
                    864:
                    865:                s = prutfchar(ch);
1.1       etheisen  866:
1.13      nicm      867:                if (column + (int)strlen(s) - 1 +
1.10      shadchin  868:                    pwidth(' ', binattr, 0) + attr_ewidth(binattr) > sc_width)
                    869:                        return (1);
1.1       etheisen  870:
1.13      nicm      871:                for (; *s != 0; s++)
1.10      shadchin  872:                        STORE_CHAR(*s, AT_BINARY, NULL, pos);
1.13      nicm      873:        } else {
1.10      shadchin  874:                STORE_CHAR(ch, a, rep, pos);
1.1       etheisen  875:        }
1.13      nicm      876:        return (0);
1.10      shadchin  877: }
                    878:
                    879: /*
                    880:  *
                    881:  */
1.13      nicm      882: int
                    883: pflushmbc(void)
1.10      shadchin  884: {
                    885:        int r = 0;
1.1       etheisen  886:
1.13      nicm      887:        if (mbc_buf_len > 0) {
1.10      shadchin  888:                /* Flush incomplete (truncated) sequence.  */
                    889:                r = flush_mbc_buf(mbc_pos);
                    890:                mbc_buf_len = 0;
                    891:        }
1.13      nicm      892:        return (r);
1.1       etheisen  893: }
                    894:
                    895: /*
                    896:  * Terminate the line in the line buffer.
                    897:  */
1.13      nicm      898: void
                    899: pdone(int endline, int forw)
1.1       etheisen  900: {
1.20      natano    901:        int i;
                    902:
1.10      shadchin  903:        (void) pflushmbc();
                    904:
1.1       etheisen  905:        if (pendc && (pendc != '\r' || !endline))
                    906:                /*
                    907:                 * If we had a pending character, put it in the buffer.
                    908:                 * But discard a pending CR if we are at end of line
                    909:                 * (that is, discard the CR in a CR/LF sequence).
                    910:                 */
1.10      shadchin  911:                (void) do_append(pendc, NULL, pendpos);
1.20      natano    912:
                    913:        for (i = curr - 1; i >= 0; i--) {
                    914:                if (attr[i] & AT_INDET) {
                    915:                        attr[i] &= ~AT_INDET;
                    916:                        if (i < curr - 1 && attr[i + 1] & AT_BOLD)
                    917:                                attr[i] |= AT_BOLD;
                    918:                        else
                    919:                                attr[i] |= AT_UNDERLINE;
                    920:                }
                    921:        }
1.1       etheisen  922:
                    923:        /*
1.5       millert   924:         * Make sure we've shifted the line, if we need to.
                    925:         */
                    926:        if (cshift < hshift)
                    927:                pshift(hshift - cshift);
                    928:
1.13      nicm      929:        if (ctldisp == OPT_ONPLUS && is_ansi_end('m')) {
1.10      shadchin  930:                /* Switch to normal attribute at end of line. */
                    931:                char *p = "\033[m";
1.13      nicm      932:                for (; *p != '\0'; p++) {
1.10      shadchin  933:                        linebuf[curr] = *p;
                    934:                        attr[curr++] = AT_ANSI;
                    935:                }
                    936:        }
                    937:
1.5       millert   938:        /*
1.1       etheisen  939:         * Add a newline if necessary,
                    940:         * and append a '\0' to the end of the line.
1.10      shadchin  941:         * We output a newline if we're not at the right edge of the screen,
                    942:         * or if the terminal doesn't auto wrap,
                    943:         * or if this is really the end of the line AND the terminal ignores
                    944:         * a newline at the right edge.
1.13      nicm      945:         * (In the last case we don't want to output a newline if the terminal
1.10      shadchin  946:         * doesn't ignore it since that would produce an extra blank line.
                    947:         * But we do want to output a newline if the terminal ignores it in case
                    948:         * the next line is blank.  In that case the single newline output for
                    949:         * that blank line would be ignored!)
1.1       etheisen  950:         */
1.13      nicm      951:        if (column < sc_width || !auto_wrap || (endline && ignaw) ||
                    952:            ctldisp == OPT_ON) {
1.1       etheisen  953:                linebuf[curr] = '\n';
                    954:                attr[curr] = AT_NORMAL;
                    955:                curr++;
1.13      nicm      956:        } else if (ignaw && column >= sc_width && forw) {
1.10      shadchin  957:                /*
                    958:                 * Terminals with "ignaw" don't wrap until they *really* need
                    959:                 * to, i.e. when the character *after* the last one to fit on a
                    960:                 * line is output. But they are too hard to deal with when they
                    961:                 * get in the state where a full screen width of characters
                    962:                 * have been output but the cursor is sitting on the right edge
                    963:                 * instead of at the start of the next line.
1.13      nicm      964:                 * So we nudge them into wrapping by outputting a space
                    965:                 * character plus a backspace.  But do this only if moving
1.10      shadchin  966:                 * forward; if we're moving backward and drawing this line at
                    967:                 * the top of the screen, the space would overwrite the first
1.13      nicm      968:                 * char on the next line.  We don't need to do this "nudge"
1.10      shadchin  969:                 * at the top of the screen anyway.
                    970:                 */
                    971:                linebuf[curr] = ' ';
                    972:                attr[curr++] = AT_NORMAL;
1.13      nicm      973:                linebuf[curr] = '\b';
1.10      shadchin  974:                attr[curr++] = AT_NORMAL;
1.1       etheisen  975:        }
                    976:        linebuf[curr] = '\0';
                    977:        attr[curr] = AT_NORMAL;
1.10      shadchin  978: }
1.5       millert   979:
1.10      shadchin  980: /*
                    981:  *
                    982:  */
1.13      nicm      983: void
                    984: set_status_col(char c)
1.10      shadchin  985: {
                    986:        linebuf[0] = c;
                    987:        attr[0] = AT_NORMAL|AT_HILITE;
1.1       etheisen  988: }
                    989:
                    990: /*
                    991:  * Get a character from the current line.
                    992:  * Return the character as the function return value,
                    993:  * and the character attribute in *ap.
                    994:  */
1.13      nicm      995: int
                    996: gline(int i, int *ap)
1.1       etheisen  997: {
1.13      nicm      998:        if (is_null_line) {
1.1       etheisen  999:                /*
                   1000:                 * If there is no current line, we pretend the line is
                   1001:                 * either "~" or "", depending on the "twiddle" flag.
                   1002:                 */
1.13      nicm     1003:                if (twiddle) {
                   1004:                        if (i == 0) {
1.10      shadchin 1005:                                *ap = AT_BOLD;
1.13      nicm     1006:                                return ('~');
1.10      shadchin 1007:                        }
                   1008:                        --i;
                   1009:                }
                   1010:                /* Make sure we're back to AT_NORMAL before the '\n'.  */
                   1011:                *ap = AT_NORMAL;
1.13      nicm     1012:                return (i ? '\0' : '\n');
1.1       etheisen 1013:        }
                   1014:
                   1015:        *ap = attr[i];
1.10      shadchin 1016:        return (linebuf[i] & 0xFF);
1.1       etheisen 1017: }
                   1018:
                   1019: /*
                   1020:  * Indicate that there is no current line.
                   1021:  */
1.13      nicm     1022: void
                   1023: null_line(void)
1.1       etheisen 1024: {
                   1025:        is_null_line = 1;
1.5       millert  1026:        cshift = 0;
1.1       etheisen 1027: }
                   1028:
                   1029: /*
                   1030:  * Analogous to forw_line(), but deals with "raw lines":
                   1031:  * lines which are not split for screen width.
                   1032:  * {{ This is supposed to be more efficient than forw_line(). }}
                   1033:  */
1.13      nicm     1034: off_t
                   1035: forw_raw_line(off_t curr_pos, char **linep, int *line_lenp)
                   1036: {
                   1037:        int n;
                   1038:        int c;
                   1039:        off_t new_pos;
                   1040:
                   1041:        if (curr_pos == -1 || ch_seek(curr_pos) ||
                   1042:            (c = ch_forw_get()) == EOI)
                   1043:                return (-1);
1.1       etheisen 1044:
1.5       millert  1045:        n = 0;
1.13      nicm     1046:        for (;;) {
                   1047:                if (c == '\n' || c == EOI || ABORT_SIGS()) {
1.1       etheisen 1048:                        new_pos = ch_tell();
                   1049:                        break;
                   1050:                }
1.13      nicm     1051:                if (n >= size_linebuf-1) {
                   1052:                        if (expand_linebuf()) {
1.5       millert  1053:                                /*
                   1054:                                 * Overflowed the input buffer.
                   1055:                                 * Pretend the line ended here.
                   1056:                                 */
                   1057:                                new_pos = ch_tell() - 1;
                   1058:                                break;
                   1059:                        }
1.1       etheisen 1060:                }
1.13      nicm     1061:                linebuf[n++] = (char)c;
1.1       etheisen 1062:                c = ch_forw_get();
                   1063:        }
1.5       millert  1064:        linebuf[n] = '\0';
1.1       etheisen 1065:        if (linep != NULL)
                   1066:                *linep = linebuf;
1.10      shadchin 1067:        if (line_lenp != NULL)
                   1068:                *line_lenp = n;
1.1       etheisen 1069:        return (new_pos);
                   1070: }
                   1071:
                   1072: /*
                   1073:  * Analogous to back_line(), but deals with "raw lines".
                   1074:  * {{ This is supposed to be more efficient than back_line(). }}
                   1075:  */
1.13      nicm     1076: off_t
                   1077: back_raw_line(off_t curr_pos, char **linep, int *line_lenp)
                   1078: {
                   1079:        int n;
                   1080:        int c;
                   1081:        off_t new_pos;
                   1082:
                   1083:        if (curr_pos == -1 || curr_pos <= ch_zero() || ch_seek(curr_pos - 1))
                   1084:                return (-1);
1.1       etheisen 1085:
1.5       millert  1086:        n = size_linebuf;
                   1087:        linebuf[--n] = '\0';
1.13      nicm     1088:        for (;;) {
1.1       etheisen 1089:                c = ch_back_get();
1.13      nicm     1090:                if (c == '\n' || ABORT_SIGS()) {
1.1       etheisen 1091:                        /*
                   1092:                         * This is the newline ending the previous line.
                   1093:                         * We have hit the beginning of the line.
                   1094:                         */
                   1095:                        new_pos = ch_tell() + 1;
                   1096:                        break;
                   1097:                }
1.13      nicm     1098:                if (c == EOI) {
1.1       etheisen 1099:                        /*
                   1100:                         * We have hit the beginning of the file.
                   1101:                         * This must be the first line in the file.
                   1102:                         * This must, of course, be the beginning of the line.
                   1103:                         */
                   1104:                        new_pos = ch_zero();
                   1105:                        break;
                   1106:                }
1.13      nicm     1107:                if (n <= 0) {
1.5       millert  1108:                        int old_size_linebuf = size_linebuf;
1.13      nicm     1109:                        if (expand_linebuf()) {
1.5       millert  1110:                                /*
                   1111:                                 * Overflowed the input buffer.
                   1112:                                 * Pretend the line ended here.
                   1113:                                 */
                   1114:                                new_pos = ch_tell() + 1;
                   1115:                                break;
                   1116:                        }
1.1       etheisen 1117:                        /*
1.5       millert  1118:                         * Shift the data to the end of the new linebuf.
1.1       etheisen 1119:                         */
1.5       millert  1120:                        n = size_linebuf - old_size_linebuf;
1.8       millert  1121:                        memmove(linebuf + n, linebuf, old_size_linebuf);
1.1       etheisen 1122:                }
1.5       millert  1123:                linebuf[--n] = c;
1.1       etheisen 1124:        }
                   1125:        if (linep != NULL)
1.5       millert  1126:                *linep = &linebuf[n];
1.10      shadchin 1127:        if (line_lenp != NULL)
                   1128:                *line_lenp = size_linebuf - 1 - n;
1.1       etheisen 1129:        return (new_pos);
                   1130: }