#include "less.h"
#define IS_CONT(c) (((c) & 0xC0) == 0x80)
#define LINENUM_WIDTH 8
public char *linebuf = NULL;
static char *attr = NULL;
public int size_linebuf = 0;
public int cshift;
public int hshift;
public int tabstops[TABSTOP_MAX] = { 0 };
public int ntabstops = 1;
public int tabdefault = 8;
static int curr;
static int column;
static int overstrike;
static int is_null_line;
static int lmargin;
static int hilites;
static char pendc;
static POSITION pendpos;
static char *end_ansi_chars;
static int do_append();
extern int bs_mode;
extern int linenums;
extern int ctldisp;
extern int twiddle;
extern int binattr;
extern int status_col;
extern int auto_wrap, ignaw;
extern int bo_s_width, bo_e_width;
extern int ul_s_width, ul_e_width;
extern int bl_s_width, bl_e_width;
extern int so_s_width, so_e_width;
extern int sc_width, sc_height;
extern int utf_mode;
extern POSITION start_attnpos;
extern POSITION end_attnpos;
public void
init_line()
{
end_ansi_chars = lgetenv("LESSANSIENDCHARS");
if (end_ansi_chars == NULL || *end_ansi_chars == '\0')
end_ansi_chars = "m";
linebuf = (char *) ecalloc(LINEBUF_SIZE, sizeof(char));
attr = (char *) ecalloc(LINEBUF_SIZE, sizeof(char));
size_linebuf = LINEBUF_SIZE;
}
static int
expand_linebuf()
{
int new_size = size_linebuf + LINEBUF_SIZE;
char *new_buf = (char *) calloc(new_size, sizeof(char));
char *new_attr = (char *) calloc(new_size, sizeof(char));
if (new_buf == NULL || new_attr == NULL)
{
if (new_attr != NULL)
free(new_attr);
if (new_buf != NULL)
free(new_buf);
return 1;
}
memcpy(new_buf, linebuf, size_linebuf * sizeof(char));
memcpy(new_attr, attr, size_linebuf * sizeof(char));
free(attr);
free(linebuf);
linebuf = new_buf;
attr = new_attr;
size_linebuf = new_size;
return 0;
}
public void
prewind()
{
curr = 0;
column = 0;
overstrike = 0;
is_null_line = 0;
pendc = '\0';
lmargin = 0;
#if HILITE_SEARCH
hilites = 0;
#endif
if (status_col)
lmargin += 1;
if (linenums == OPT_ONPLUS)
lmargin += LINENUM_WIDTH+1;
}
public void
plinenum(pos)
POSITION pos;
{
register int lno;
register int i;
if (linenums == OPT_ONPLUS)
{
lno = find_linenum(pos);
}
if (status_col)
{
linebuf[curr] = ' ';
if (start_attnpos != NULL_POSITION &&
pos >= start_attnpos && pos < end_attnpos)
attr[curr] = AT_STANDOUT;
else
attr[curr] = 0;
curr++;
column++;
}
if (linenums == OPT_ONPLUS)
{
sprintf(&linebuf[curr], "%*d", LINENUM_WIDTH, lno);
column += LINENUM_WIDTH;
for (i = 0; i < LINENUM_WIDTH; i++)
attr[curr++] = 0;
}
while (column < lmargin)
{
linebuf[curr] = ' ';
attr[curr++] = AT_NORMAL;
column++;
}
}
static int
shift_chars(s, len)
char *s;
int len;
{
char *p = s;
while (*p != '\0' && len > 0)
{
if (*p++ != ESC)
{
len--;
} else
{
while (*p != '\0')
{
if (is_ansi_end(*p++))
break;
}
}
}
return (p - s);
}
static int
utf_shift_chars(s, len)
char *s;
int len;
{
int ulen = 0;
while (*s != '\0' && len > 0)
{
if (!IS_CONT(*s))
len--;
s++;
ulen++;
}
while (IS_CONT(*s))
{
s++;
ulen++;
}
return (ulen);
}
static void
pshift(shift)
int shift;
{
int i;
int nchars;
if (shift > column - lmargin)
shift = column - lmargin;
if (shift > curr - lmargin)
shift = curr - lmargin;
if (utf_mode)
nchars = utf_shift_chars(linebuf + lmargin, shift);
else
nchars = shift_chars(linebuf + lmargin, shift);
if (nchars > curr)
nchars = curr;
for (i = 0; i < curr - nchars; i++)
{
linebuf[lmargin + i] = linebuf[lmargin + i + nchars];
attr[lmargin + i] = attr[lmargin + i + nchars];
}
curr -= nchars;
column -= shift;
cshift += shift;
}
static int
attr_swidth(a)
int a;
{
switch (a)
{
case AT_BOLD: return (bo_s_width);
case AT_UNDERLINE: return (ul_s_width);
case AT_BLINK: return (bl_s_width);
case AT_STANDOUT: return (so_s_width);
}
return (0);
}
static int
attr_ewidth(a)
int a;
{
switch (a)
{
case AT_BOLD: return (bo_e_width);
case AT_UNDERLINE: return (ul_e_width);
case AT_BLINK: return (bl_e_width);
case AT_STANDOUT: return (so_e_width);
}
return (0);
}
static int
pwidth(c, a)
int c;
int a;
{
register int w;
if (utf_mode && IS_CONT(c))
return (0);
if (c == '\b')
return (-1);
if (control_char(c))
return (0);
w = 1;
if (curr > 0 && attr[curr-1] != a)
w += attr_ewidth(attr[curr-1]);
if (a && (curr == 0 || attr[curr-1] != a))
w += attr_swidth(a);
return (w);
}
static void
backc()
{
curr--;
column -= pwidth(linebuf[curr], attr[curr]);
}
static int
in_ansi_esc_seq()
{
int i;
for (i = curr-1; i >= 0; i--)
{
if (linebuf[i] == ESC)
return (1);
if (is_ansi_end(linebuf[i]))
return (0);
}
return (0);
}
public int
is_ansi_end(c)
char c;
{
return (strchr(end_ansi_chars, c) != NULL);
}
#define STORE_CHAR(c,a,pos) \
do { if (store_char((c),(a),(pos))) return (1); else curr++; } while (0)
static int
store_char(c, a, pos)
int c;
int a;
POSITION pos;
{
register int w;
#if HILITE_SEARCH
if (is_hilited(pos, pos+1, 0))
{
a = AT_STANDOUT;
hilites++;
}
#endif
if (ctldisp == OPT_ONPLUS && in_ansi_esc_seq())
w = 0;
else
w = pwidth(c, a);
if (ctldisp != OPT_ON && column + w + attr_ewidth(a) > sc_width)
return (1);
if (curr >= size_linebuf-2)
{
if (expand_linebuf())
return (1);
}
if (curr > 0 && a != AT_NORMAL &&
linebuf[curr-1] == ' ' && attr[curr-1] == AT_NORMAL &&
attr_swidth(a) > 0)
{
attr[curr-1] = AT_INVIS;
column--;
} else if (curr > 0 && attr[curr-1] != AT_NORMAL &&
attr[curr-1] != AT_INVIS && c == ' ' && a == AT_NORMAL &&
attr_ewidth(attr[curr-1]) > 0)
{
a = AT_INVIS;
column--;
}
linebuf[curr] = c;
attr[curr] = a;
column += w;
return (0);
}
#define STORE_TAB(a,pos) \
do { if (store_tab((a),(pos))) return (1); } while (0)
static int
store_tab(attr, pos)
int attr;
POSITION pos;
{
int to_tab = column + cshift - lmargin;
int i;
if (ntabstops < 2 || to_tab >= tabstops[ntabstops-1])
to_tab = tabdefault -
((to_tab - tabstops[ntabstops-1]) % tabdefault);
else
{
for (i = ntabstops - 2; i >= 0; i--)
if (to_tab >= tabstops[i])
break;
to_tab = tabstops[i+1] - to_tab;
}
do {
STORE_CHAR(' ', attr, pos);
} while (--to_tab > 0);
return 0;
}
public int
pappend(c, pos)
register int c;
POSITION pos;
{
int r;
if (pendc)
{
if (do_append(pendc, pendpos))
return (1);
pendc = '\0';
}
if (c == '\r' && bs_mode == BS_SPECIAL)
{
pendc = c;
pendpos = pos;
return (0);
}
r = do_append(c, pos);
if (cshift < hshift && column > sc_width / 2)
{
linebuf[curr] = '\0';
pshift(hshift - cshift);
}
return (r);
}
#define IS_UTF8_4BYTE(c) ( ((c) & 0xf8) == 0xf0 )
#define IS_UTF8_3BYTE(c) ( ((c) & 0xf0) == 0xe0 )
#define IS_UTF8_2BYTE(c) ( ((c) & 0xe0) == 0xc0 )
#define IS_UTF8_TRAIL(c) ( ((c) & 0xc0) == 0x80 )
static int
do_append(c, pos)
int c;
POSITION pos;
{
register char *s;
register int a;
#define STOREC(c,a) \
if ((c) == '\t') STORE_TAB((a),pos); else STORE_CHAR((c),(a),pos)
if (c == '\b')
{
switch (bs_mode)
{
case BS_NORMAL:
STORE_CHAR(c, AT_NORMAL, pos);
break;
case BS_CONTROL:
goto do_control_char;
case BS_SPECIAL:
if (curr == 0)
break;
backc();
overstrike = 1;
break;
}
} else if (overstrike)
{
overstrike--;
if (utf_mode && IS_UTF8_4BYTE(c) && curr > 2 && (char)c == linebuf[curr-3])
{
backc();
backc();
backc();
STORE_CHAR(linebuf[curr], AT_BOLD, pos);
overstrike = 3;
} else if (utf_mode && (IS_UTF8_3BYTE(c) || (overstrike==2 && IS_UTF8_TRAIL(c))) && curr > 1 && (char)c == linebuf[curr-2])
{
backc();
backc();
STORE_CHAR(linebuf[curr], AT_BOLD, pos);
overstrike = 2;
} else if (utf_mode && curr > 0 && (IS_UTF8_2BYTE(c) || (overstrike==1 && IS_UTF8_TRAIL(c))) && (char)c == linebuf[curr-1])
{
backc();
STORE_CHAR(linebuf[curr], AT_BOLD, pos);
overstrike = 1;
} else if ((char)c == linebuf[curr])
{
STOREC(c, AT_BOLD);
} else if (c == '_')
{
if (utf_mode)
{
if (curr > 0 && IS_CONT(linebuf[curr]))
attr[curr-1] = AT_UNDERLINE;
if (curr > 1 && IS_CONT(linebuf[curr-1]))
attr[curr-2] = AT_UNDERLINE;
if (curr > 2 && IS_CONT(linebuf[curr-2]))
attr[curr-3] = AT_UNDERLINE;
if (curr > 3 && IS_CONT(linebuf[curr-3]))
attr[curr-4] = AT_UNDERLINE;
if (curr > 4 && IS_CONT(linebuf[curr-4]))
attr[curr-5] = AT_UNDERLINE;
}
STOREC(linebuf[curr], AT_UNDERLINE);
} else if (linebuf[curr] == '_')
{
STOREC(c, AT_UNDERLINE);
} else if (control_char(c))
goto do_control_char;
else
STOREC(c, AT_NORMAL);
} else if (c == '\t')
{
switch (bs_mode)
{
case BS_CONTROL:
goto do_control_char;
case BS_NORMAL:
case BS_SPECIAL:
STORE_TAB(AT_NORMAL, pos);
break;
}
} else if (control_char(c))
{
do_control_char:
if (ctldisp == OPT_ON || (ctldisp == OPT_ONPLUS && c == ESC))
{
STORE_CHAR(c, AT_NORMAL, pos);
} else
{
s = prchar(c);
a = binattr;
if (column + (int) strlen(s) +
attr_swidth(a) + attr_ewidth(a) > sc_width)
return (1);
for ( ; *s != 0; s++)
STORE_CHAR(*s, a, pos);
}
} else
{
STOREC(c, AT_NORMAL);
}
return (0);
}
public void
pdone(endline)
int endline;
{
if (pendc && (pendc != '\r' || !endline))
(void) do_append(pendc, pendpos);
if (cshift < hshift)
pshift(hshift - cshift);
if (column < sc_width || !auto_wrap || ignaw || ctldisp == OPT_ON)
{
linebuf[curr] = '\n';
attr[curr] = AT_NORMAL;
curr++;
}
linebuf[curr] = '\0';
attr[curr] = AT_NORMAL;
#if HILITE_SEARCH
if (status_col && hilites > 0)
{
linebuf[0] = '*';
attr[0] = AT_STANDOUT;
}
#endif
if (endline)
cshift = 0;
}
public int
gline(i, ap)
register int i;
register int *ap;
{
char *s;
if (is_null_line)
{
*ap = AT_BOLD;
s = (twiddle) ? "~\n" : "\n";
return (s[i]);
}
*ap = attr[i];
return (linebuf[i] & 0377);
}
public void
null_line()
{
is_null_line = 1;
cshift = 0;
}
public POSITION
forw_raw_line(curr_pos, linep)
POSITION curr_pos;
char **linep;
{
register int n;
register int c;
POSITION new_pos;
if (curr_pos == NULL_POSITION || ch_seek(curr_pos) ||
(c = ch_forw_get()) == EOI)
return (NULL_POSITION);
n = 0;
for (;;)
{
if (c == '\n' || c == EOI)
{
new_pos = ch_tell();
break;
}
if (n >= size_linebuf-1)
{
if (expand_linebuf())
{
new_pos = ch_tell() - 1;
break;
}
}
linebuf[n++] = c;
c = ch_forw_get();
}
linebuf[n] = '\0';
if (linep != NULL)
*linep = linebuf;
return (new_pos);
}
public POSITION
back_raw_line(curr_pos, linep)
POSITION curr_pos;
char **linep;
{
register int n;
register int c;
POSITION new_pos;
if (curr_pos == NULL_POSITION || curr_pos <= ch_zero() ||
ch_seek(curr_pos-1))
return (NULL_POSITION);
n = size_linebuf;
linebuf[--n] = '\0';
for (;;)
{
c = ch_back_get();
if (c == '\n')
{
new_pos = ch_tell() + 1;
break;
}
if (c == EOI)
{
new_pos = ch_zero();
break;
}
if (n <= 0)
{
int old_size_linebuf = size_linebuf;
char *fm;
char *to;
if (expand_linebuf())
{
new_pos = ch_tell() + 1;
break;
}
for (fm = linebuf + old_size_linebuf,
to = linebuf + size_linebuf;
fm >= linebuf; fm--, to--)
*to = *fm;
n = size_linebuf - old_size_linebuf;
}
linebuf[--n] = c;
}
if (linep != NULL)
*linep = &linebuf[n];
return (new_pos);
}