=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/less/line.c,v retrieving revision 1.1.1.3 retrieving revision 1.1.1.4 diff -u -r1.1.1.3 -r1.1.1.4 --- src/usr.bin/less/line.c 2011/09/16 17:47:06 1.1.1.3 +++ src/usr.bin/less/line.c 2014/04/25 13:33:49 1.1.1.4 @@ -1,11 +1,10 @@ /* - * Copyright (C) 1984-2011 Mark Nudelman + * Copyright (C) 1984-2012 Mark Nudelman * * You may distribute under the terms of either the GNU General Public * License or the Less License, as specified in the README file. * - * For more information about less, or for information on how to - * contact the author, see the README file. + * For more information, see the README file. */ @@ -18,6 +17,8 @@ #include "less.h" #include "charset.h" +#include + static char *linebuf = NULL; /* Buffer which holds the current output line */ static char *attr = NULL; /* Extension of linebuf to hold attributes */ public int size_linebuf = 0; /* Size of line buffer (and attr buffer) */ @@ -27,6 +28,7 @@ public int tabstops[TABSTOP_MAX] = { 0 }; /* Custom tabstops */ public int ntabstops = 1; /* Number of tabstops */ public int tabdefault = 8; /* Default repeated tabstops */ +public POSITION highest_hilite; /* Pos of last hilite in file found so far */ static int curr; /* Index into linebuf */ static int column; /* Printable length, accounting for @@ -44,7 +46,7 @@ static int attr_ewidth(); static int do_append(); -extern int sigs; +extern volatile sig_atomic_t sigs; extern int bs_mode; extern int linenums; extern int ctldisp; @@ -95,38 +97,25 @@ int new_size = size_linebuf * 2; /* Just realloc to expand the buffer, if we can. */ -#if HAVE_REALLOC - char *new_buf = (char *) realloc(linebuf, new_size); - char *new_attr = (char *) realloc(attr, new_size); -#else - char *new_buf = (char *) calloc(new_size, sizeof(char)); - char *new_attr = (char *) calloc(new_size, sizeof(char)); -#endif - if (new_buf == NULL || new_attr == NULL) - { - if (new_attr != NULL) - free(new_attr); - if (new_buf != NULL) - free(new_buf); + char *new_buf; + char *new_attr; + + new_buf = realloc(linebuf, new_size); + if (new_buf == NULL) return 1; + new_attr = realloc(attr, new_size); + if (new_attr == NULL) { + /* realloc linebuf back to original size */ + linebuf = realloc(new_buf, size_linebuf); + if (linebuf == NULL) + err(1, NULL); + return 1; } -#if HAVE_REALLOC /* * We realloc'd the buffers; they already have the old contents. */ - #if 0 memset(new_buf + size_linebuf, 0, new_size - size_linebuf); memset(new_attr + size_linebuf, 0, new_size - size_linebuf); - #endif -#else - /* - * We just calloc'd the buffers; copy the old contents. - */ - memcpy(new_buf, linebuf, size_linebuf * sizeof(char)); - memcpy(new_attr, attr, size_linebuf * sizeof(char)); - free(attr); - free(linebuf); -#endif linebuf = new_buf; attr = new_attr; size_linebuf = new_size; @@ -208,11 +197,11 @@ char buf[INT_STRLEN_BOUND(pos) + 2]; int n; - linenumtoa(linenum, buf); + linenumtoa(linenum, buf, sizeof(buf)); n = strlen(buf); if (n < MIN_LINENUM_WIDTH) n = MIN_LINENUM_WIDTH; - sprintf(linebuf+curr, "%*s ", n, buf); + snprintf(linebuf+curr, size_linebuf-curr, "%*s ", n, buf); n++; /* One space after the line number. */ for (i = 0; i < n; i++) attr[curr+i] = AT_NORMAL; @@ -280,6 +269,7 @@ width = 0; +#if !SMALL if (!IS_ASCII_OCTET(c) && utf_mode) { /* Assumes well-formedness validation already done. */ @@ -293,11 +283,16 @@ width = is_wide_char(ch) ? 2 : 1; prev_ch = ch; } else +#endif /* !SMALL */ { len = 1; if (c == '\b') /* XXX - Incorrect if several '\b' in a row. */ +#if !SMALL width = (utf_mode && is_wide_char(prev_ch)) ? -2 : -1; +#else + width = -1; +#endif /* !SMALL */ else if (!control_char(c)) width = 1; prev_ch = 0; @@ -427,7 +422,11 @@ * Backspace moves backwards one or two positions. * XXX - Incorrect if several '\b' in a row. */ +#if !SMALL return (utf_mode && is_wide_char(prev_ch)) ? -2 : -1; +#else + return -1; +#endif /* !SMALL */ if (!utf_mode || is_ascii_char(ch)) { @@ -440,7 +439,9 @@ */ return (0); } - } else + } +#if !SMALL + else { if (is_composing_char(ch) || is_combining_char(prev_ch, ch)) { @@ -458,14 +459,17 @@ return (0); } } +#endif /* !SMALL */ /* * Other characters take one or two columns, * plus the width of any attribute enter/exit sequence. */ w = 1; +#if !SMALL if (is_wide_char(ch)) w++; +#endif /* !SMALL */ if (curr > 0 && !is_at_equiv(attr[curr-1], a)) w += attr_ewidth(attr[curr-1]); if ((apply_at_specials(a) != AT_NORMAL) && @@ -585,7 +589,12 @@ * Override the attribute passed in. */ if (a != AT_ANSI) + { + if (highest_hilite != NULL_POSITION && + pos > highest_hilite) + highest_hilite = pos; a |= AT_HILITE; + } } } #endif @@ -630,7 +639,11 @@ replen = 1; } else { +#if !SMALL replen = utf_len(rep[0]); +#else + replen = 1; +#endif /* !SMALL */ } if (curr + replen >= size_linebuf-6) { @@ -716,6 +729,7 @@ return 0; } +#if !SMALL static int flush_mbc_buf(pos) POSITION pos; @@ -728,6 +742,7 @@ return 0; } +#endif /* !SMALL */ /* * Append a character to the line buffer. @@ -754,6 +769,7 @@ if (c == '\r' && bs_mode == BS_SPECIAL) { +#if !SMALL if (mbc_buf_len > 0) /* utf_mode must be on. */ { /* Flush incomplete (truncated) sequence. */ @@ -763,6 +779,7 @@ if (r) return (mbc_buf_index); } +#endif /* !SMALL */ /* * Don't put the CR into the buffer until we see @@ -777,7 +794,9 @@ if (!utf_mode) { r = do_append((LWCHAR) c, NULL, pos); - } else + } +#if !SMALL + else { /* Perform strict validation in all possible cases. */ if (mbc_buf_len == 0) @@ -817,6 +836,7 @@ goto retry; } } +#endif /* !SMALL */ /* * If we need to shift the line, do it. @@ -881,7 +901,11 @@ */ overstrike = utf_mode ? -1 : 0; /* To be correct, this must be a base character. */ +#if !SMALL prev_ch = get_wchar(linebuf + curr); +#else + prev_ch = (LWCHAR)((char)(linebuf + curr)[0] & 0xFF); +#endif /* !SMALL */ a = attr[curr]; if (ch == prev_ch) { @@ -914,11 +938,13 @@ /* Else we replace prev_ch, but we keep its attributes. */ } else if (overstrike < 0) { +#if !SMALL if ( is_composing_char(ch) || is_combining_char(get_wchar(linebuf + curr), ch)) /* Continuation of the same overstrike. */ a = last_overstrike; else +#endif /* !SMALL */ overstrike = 0; } @@ -949,7 +975,9 @@ { STORE_PRCHAR((char) ch, pos); } - } else if (utf_mode && ctldisp != OPT_ON && is_ubin_char(ch)) + } +#if !SMALL + else if (utf_mode && ctldisp != OPT_ON && is_ubin_char(ch)) { char *s; @@ -961,7 +989,9 @@ for ( ; *s != 0; s++) STORE_CHAR(*s, AT_BINARY, NULL, pos); - } else + } +#endif /* !SMALL */ + else { STORE_CHAR(ch, a, rep, pos); } @@ -976,12 +1006,14 @@ { int r = 0; +#if !SMALL if (mbc_buf_len > 0) { /* Flush incomplete (truncated) sequence. */ r = flush_mbc_buf(mbc_pos); mbc_buf_len = 0; } +#endif /* !SMALL */ return r; } @@ -1214,8 +1246,6 @@ if (n <= 0) { int old_size_linebuf = size_linebuf; - char *fm; - char *to; if (expand_linebuf()) { /* @@ -1228,11 +1258,8 @@ /* * Shift the data to the end of the new linebuf. */ - for (fm = linebuf + old_size_linebuf - 1, - to = linebuf + size_linebuf - 1; - fm >= linebuf; fm--, to--) - *to = *fm; n = size_linebuf - old_size_linebuf; + memmove(linebuf + n, linebuf, old_size_linebuf); } linebuf[--n] = c; }