xref: /dragonfly/contrib/less/line.c (revision 320d7c8a)
11133e27eSPeter Avalos /*
2*320d7c8aSAaron LI  * Copyright (C) 1984-2023  Mark Nudelman
31133e27eSPeter Avalos  *
41133e27eSPeter Avalos  * You may distribute under the terms of either the GNU General Public
51133e27eSPeter Avalos  * License or the Less License, as specified in the README file.
61133e27eSPeter Avalos  *
7e639dc31SJohn Marino  * For more information, see the README file.
81133e27eSPeter Avalos  */
91133e27eSPeter Avalos 
101133e27eSPeter Avalos /*
111133e27eSPeter Avalos  * Routines to manipulate the "line buffer".
121133e27eSPeter Avalos  * The line buffer holds a line of output as it is being built
131133e27eSPeter Avalos  * in preparation for output to the screen.
141133e27eSPeter Avalos  */
151133e27eSPeter Avalos 
161133e27eSPeter Avalos #include "less.h"
171133e27eSPeter Avalos #include "charset.h"
1802d62a0fSDaniel Fojt #include "position.h"
1902d62a0fSDaniel Fojt 
2002d62a0fSDaniel Fojt #if MSDOS_COMPILER==WIN32C
2102d62a0fSDaniel Fojt #define WIN32_LEAN_AND_MEAN
2202d62a0fSDaniel Fojt #include <windows.h>
2302d62a0fSDaniel Fojt #endif
241133e27eSPeter Avalos 
250c7ad07eSAntonio Huete Jimenez #define MAX_PFX_WIDTH (MAX_LINENUM_WIDTH + MAX_STATUSCOL_WIDTH + 1)
260c7ad07eSAntonio Huete Jimenez static struct {
270c7ad07eSAntonio Huete Jimenez 	char *buf;    /* Buffer which holds the current output line */
280c7ad07eSAntonio Huete Jimenez 	int *attr;   /* Parallel to buf, to hold attributes */
290c7ad07eSAntonio Huete Jimenez 	int print;    /* Index in buf of first printable char */
300c7ad07eSAntonio Huete Jimenez 	int end;      /* Number of chars in buf */
310c7ad07eSAntonio Huete Jimenez 	char pfx[MAX_PFX_WIDTH]; /* Holds status column and line number */
320c7ad07eSAntonio Huete Jimenez 	int pfx_attr[MAX_PFX_WIDTH];
330c7ad07eSAntonio Huete Jimenez 	int pfx_end;  /* Number of chars in pfx */
340c7ad07eSAntonio Huete Jimenez } linebuf;
351133e27eSPeter Avalos 
360c7ad07eSAntonio Huete Jimenez /*
370c7ad07eSAntonio Huete Jimenez  * Buffer of ansi sequences which have been shifted off the left edge
380c7ad07eSAntonio Huete Jimenez  * of the screen.
390c7ad07eSAntonio Huete Jimenez  */
40*320d7c8aSAaron LI static struct xbuffer shifted_ansi;
410c7ad07eSAntonio Huete Jimenez 
420c7ad07eSAntonio Huete Jimenez /*
430c7ad07eSAntonio Huete Jimenez  * Ring buffer of last ansi sequences sent.
440c7ad07eSAntonio Huete Jimenez  * While sending a line, these will be resent at the end
450c7ad07eSAntonio Huete Jimenez  * of any highlighted string, to restore text modes.
460c7ad07eSAntonio Huete Jimenez  * {{ Not ideal, since we don't really know how many to resend. }}
470c7ad07eSAntonio Huete Jimenez  */
480c7ad07eSAntonio Huete Jimenez #define NUM_LAST_ANSIS 3
490c7ad07eSAntonio Huete Jimenez static struct xbuffer last_ansi;
500c7ad07eSAntonio Huete Jimenez static struct xbuffer last_ansis[NUM_LAST_ANSIS];
510c7ad07eSAntonio Huete Jimenez static int curr_last_ansi;
520c7ad07eSAntonio Huete Jimenez 
530c7ad07eSAntonio Huete Jimenez public int size_linebuf = 0; /* Size of line buffer (and attr buffer) */
540c7ad07eSAntonio Huete Jimenez static struct ansi_state *line_ansi = NULL;
550c7ad07eSAntonio Huete Jimenez static int ansi_in_line;
560c7ad07eSAntonio Huete Jimenez static int hlink_in_line;
570c7ad07eSAntonio Huete Jimenez static int line_mark_attr;
581133e27eSPeter Avalos static int cshift;   /* Current left-shift of output line buffer */
591133e27eSPeter Avalos public int hshift;   /* Desired left-shift of output line buffer */
601133e27eSPeter Avalos public int tabstops[TABSTOP_MAX] = { 0 }; /* Custom tabstops */
611133e27eSPeter Avalos public int ntabstops = 1;        /* Number of tabstops */
621133e27eSPeter Avalos public int tabdefault = 8;       /* Default repeated tabstops */
63e639dc31SJohn Marino public POSITION highest_hilite;  /* Pos of last hilite in file found so far */
64*320d7c8aSAaron LI static POSITION line_pos;
651133e27eSPeter Avalos 
660c7ad07eSAntonio Huete Jimenez static int end_column;  /* Printable length, accounting for backspaces, etc. */
6702d62a0fSDaniel Fojt static int right_curr;
6802d62a0fSDaniel Fojt static int right_column;
691133e27eSPeter Avalos static int overstrike;  /* Next char should overstrike previous char */
701133e27eSPeter Avalos static int last_overstrike = AT_NORMAL;
711133e27eSPeter Avalos static int is_null_line;  /* There is no current line */
72fa0be7c5SJohn Marino static LWCHAR pendc;
731133e27eSPeter Avalos static POSITION pendpos;
741133e27eSPeter Avalos static char *end_ansi_chars;
751133e27eSPeter Avalos static char *mid_ansi_chars;
760c7ad07eSAntonio Huete Jimenez static int in_hilite;
771133e27eSPeter Avalos 
78*320d7c8aSAaron LI static int attr_swidth(int a);
79*320d7c8aSAaron LI static int attr_ewidth(int a);
80*320d7c8aSAaron LI static int do_append(LWCHAR ch, char *rep, POSITION pos);
811133e27eSPeter Avalos 
821133e27eSPeter Avalos extern int sigs;
831133e27eSPeter Avalos extern int bs_mode;
84*320d7c8aSAaron LI extern int proc_backspace;
85*320d7c8aSAaron LI extern int proc_tab;
86*320d7c8aSAaron LI extern int proc_return;
871133e27eSPeter Avalos extern int linenums;
881133e27eSPeter Avalos extern int ctldisp;
891133e27eSPeter Avalos extern int twiddle;
901133e27eSPeter Avalos extern int binattr;
911133e27eSPeter Avalos extern int status_col;
920c7ad07eSAntonio Huete Jimenez extern int status_col_width;
930c7ad07eSAntonio Huete Jimenez extern int linenum_width;
941133e27eSPeter Avalos extern int auto_wrap, ignaw;
951133e27eSPeter Avalos extern int bo_s_width, bo_e_width;
961133e27eSPeter Avalos extern int ul_s_width, ul_e_width;
971133e27eSPeter Avalos extern int bl_s_width, bl_e_width;
981133e27eSPeter Avalos extern int so_s_width, so_e_width;
991133e27eSPeter Avalos extern int sc_width, sc_height;
1001133e27eSPeter Avalos extern int utf_mode;
1011133e27eSPeter Avalos extern POSITION start_attnpos;
1021133e27eSPeter Avalos extern POSITION end_attnpos;
10302d62a0fSDaniel Fojt extern char rscroll_char;
10402d62a0fSDaniel Fojt extern int rscroll_attr;
1050c7ad07eSAntonio Huete Jimenez extern int use_color;
1060c7ad07eSAntonio Huete Jimenez extern int status_line;
1071133e27eSPeter Avalos 
1081133e27eSPeter Avalos static char mbc_buf[MAX_UTF_CHAR_LEN];
1091133e27eSPeter Avalos static int mbc_buf_len = 0;
1101133e27eSPeter Avalos static int mbc_buf_index = 0;
1111133e27eSPeter Avalos static POSITION mbc_pos;
112*320d7c8aSAaron LI static int saved_line_end;
113*320d7c8aSAaron LI static int saved_end_column;
1141133e27eSPeter Avalos 
1150c7ad07eSAntonio Huete Jimenez /* Configurable color map */
116*320d7c8aSAaron LI struct color_map { int attr; char color[12]; };
117*320d7c8aSAaron LI static struct color_map color_map[] = {
118*320d7c8aSAaron LI 	{ AT_UNDERLINE,            "" },
119*320d7c8aSAaron LI 	{ AT_BOLD,                 "" },
120*320d7c8aSAaron LI 	{ AT_BLINK,                "" },
121*320d7c8aSAaron LI 	{ AT_STANDOUT,             "" },
122*320d7c8aSAaron LI 	{ AT_COLOR_ATTN,           "Wm" },
123*320d7c8aSAaron LI 	{ AT_COLOR_BIN,            "kR" },
124*320d7c8aSAaron LI 	{ AT_COLOR_CTRL,           "kR" },
125*320d7c8aSAaron LI 	{ AT_COLOR_ERROR,          "kY" },
126*320d7c8aSAaron LI 	{ AT_COLOR_LINENUM,        "c" },
127*320d7c8aSAaron LI 	{ AT_COLOR_MARK,           "Wb" },
128*320d7c8aSAaron LI 	{ AT_COLOR_PROMPT,         "kC" },
129*320d7c8aSAaron LI 	{ AT_COLOR_RSCROLL,        "kc" },
130*320d7c8aSAaron LI 	{ AT_COLOR_HEADER,         "" },
131*320d7c8aSAaron LI 	{ AT_COLOR_SEARCH,         "kG" },
132*320d7c8aSAaron LI 	{ AT_COLOR_SUBSEARCH(1),   "ky" },
133*320d7c8aSAaron LI 	{ AT_COLOR_SUBSEARCH(2),   "wb" },
134*320d7c8aSAaron LI 	{ AT_COLOR_SUBSEARCH(3),   "YM" },
135*320d7c8aSAaron LI 	{ AT_COLOR_SUBSEARCH(4),   "Yr" },
136*320d7c8aSAaron LI 	{ AT_COLOR_SUBSEARCH(5),   "Wc" },
1370c7ad07eSAntonio Huete Jimenez };
1380c7ad07eSAntonio Huete Jimenez 
1390c7ad07eSAntonio Huete Jimenez /* State while processing an ANSI escape sequence */
1400c7ad07eSAntonio Huete Jimenez struct ansi_state {
1410c7ad07eSAntonio Huete Jimenez 	int hindex;   /* Index into hyperlink prefix */
1420c7ad07eSAntonio Huete Jimenez 	int hlink;    /* Processing hyperlink address? */
1430c7ad07eSAntonio Huete Jimenez 	int prev_esc; /* Prev char was ESC (to detect ESC-\ seq) */
1440c7ad07eSAntonio Huete Jimenez };
1450c7ad07eSAntonio Huete Jimenez 
1461133e27eSPeter Avalos /*
1471133e27eSPeter Avalos  * Initialize from environment variables.
1481133e27eSPeter Avalos  */
init_line(void)149*320d7c8aSAaron LI public void init_line(void)
1501133e27eSPeter Avalos {
1510c7ad07eSAntonio Huete Jimenez 	int ax;
1520c7ad07eSAntonio Huete Jimenez 
1531133e27eSPeter Avalos 	end_ansi_chars = lgetenv("LESSANSIENDCHARS");
15402d62a0fSDaniel Fojt 	if (isnullenv(end_ansi_chars))
1551133e27eSPeter Avalos 		end_ansi_chars = "m";
1561133e27eSPeter Avalos 
1571133e27eSPeter Avalos 	mid_ansi_chars = lgetenv("LESSANSIMIDCHARS");
15802d62a0fSDaniel Fojt 	if (isnullenv(mid_ansi_chars))
159fa0be7c5SJohn Marino 		mid_ansi_chars = "0123456789:;[?!\"'#%()*+ ";
1601133e27eSPeter Avalos 
1610c7ad07eSAntonio Huete Jimenez 	linebuf.buf = (char *) ecalloc(LINEBUF_SIZE, sizeof(char));
1620c7ad07eSAntonio Huete Jimenez 	linebuf.attr = (int *) ecalloc(LINEBUF_SIZE, sizeof(int));
1631133e27eSPeter Avalos 	size_linebuf = LINEBUF_SIZE;
1640c7ad07eSAntonio Huete Jimenez 	xbuf_init(&shifted_ansi);
1650c7ad07eSAntonio Huete Jimenez 	xbuf_init(&last_ansi);
1660c7ad07eSAntonio Huete Jimenez 	for (ax = 0;  ax < NUM_LAST_ANSIS;  ax++)
1670c7ad07eSAntonio Huete Jimenez 		xbuf_init(&last_ansis[ax]);
1680c7ad07eSAntonio Huete Jimenez 	curr_last_ansi = 0;
1691133e27eSPeter Avalos }
1701133e27eSPeter Avalos 
1711133e27eSPeter Avalos /*
1721133e27eSPeter Avalos  * Expand the line buffer.
1731133e27eSPeter Avalos  */
expand_linebuf(void)174*320d7c8aSAaron LI static int expand_linebuf(void)
1751133e27eSPeter Avalos {
1761133e27eSPeter Avalos 	/* Double the size of the line buffer. */
1771133e27eSPeter Avalos 	int new_size = size_linebuf * 2;
1781133e27eSPeter Avalos 	char *new_buf = (char *) calloc(new_size, sizeof(char));
1790c7ad07eSAntonio Huete Jimenez 	int *new_attr = (int *) calloc(new_size, sizeof(int));
1801133e27eSPeter Avalos 	if (new_buf == NULL || new_attr == NULL)
1811133e27eSPeter Avalos 	{
1821133e27eSPeter Avalos 		if (new_attr != NULL)
1831133e27eSPeter Avalos 			free(new_attr);
1841133e27eSPeter Avalos 		if (new_buf != NULL)
1851133e27eSPeter Avalos 			free(new_buf);
1861133e27eSPeter Avalos 		return 1;
1871133e27eSPeter Avalos 	}
1881133e27eSPeter Avalos 	/*
1891133e27eSPeter Avalos 	 * We just calloc'd the buffers; copy the old contents.
1901133e27eSPeter Avalos 	 */
1910c7ad07eSAntonio Huete Jimenez 	memcpy(new_buf, linebuf.buf, size_linebuf * sizeof(char));
1920c7ad07eSAntonio Huete Jimenez 	memcpy(new_attr, linebuf.attr, size_linebuf * sizeof(int));
1930c7ad07eSAntonio Huete Jimenez 	free(linebuf.attr);
1940c7ad07eSAntonio Huete Jimenez 	free(linebuf.buf);
1950c7ad07eSAntonio Huete Jimenez 	linebuf.buf = new_buf;
1960c7ad07eSAntonio Huete Jimenez 	linebuf.attr = new_attr;
1971133e27eSPeter Avalos 	size_linebuf = new_size;
1981133e27eSPeter Avalos 	return 0;
1991133e27eSPeter Avalos }
2001133e27eSPeter Avalos 
2011133e27eSPeter Avalos /*
2021133e27eSPeter Avalos  * Is a character ASCII?
2031133e27eSPeter Avalos  */
is_ascii_char(LWCHAR ch)204*320d7c8aSAaron LI public int is_ascii_char(LWCHAR ch)
2051133e27eSPeter Avalos {
2061133e27eSPeter Avalos 	return (ch <= 0x7F);
2071133e27eSPeter Avalos }
2081133e27eSPeter Avalos 
2091133e27eSPeter Avalos /*
2100c7ad07eSAntonio Huete Jimenez  */
inc_end_column(int w)211*320d7c8aSAaron LI static void inc_end_column(int w)
2120c7ad07eSAntonio Huete Jimenez {
2130c7ad07eSAntonio Huete Jimenez 	if (end_column > right_column && w > 0)
2140c7ad07eSAntonio Huete Jimenez 	{
2150c7ad07eSAntonio Huete Jimenez 		right_column = end_column;
2160c7ad07eSAntonio Huete Jimenez 		right_curr = linebuf.end;
2170c7ad07eSAntonio Huete Jimenez 	}
2180c7ad07eSAntonio Huete Jimenez 	end_column += w;
2190c7ad07eSAntonio Huete Jimenez }
2200c7ad07eSAntonio Huete Jimenez 
line_position(void)221*320d7c8aSAaron LI public POSITION line_position(void)
222*320d7c8aSAaron LI {
223*320d7c8aSAaron LI 	return line_pos;
224*320d7c8aSAaron LI }
225*320d7c8aSAaron LI 
2260c7ad07eSAntonio Huete Jimenez /*
2271133e27eSPeter Avalos  * Rewind the line buffer.
2281133e27eSPeter Avalos  */
prewind(void)229*320d7c8aSAaron LI public void prewind(void)
2301133e27eSPeter Avalos {
2310c7ad07eSAntonio Huete Jimenez 	int ax;
2320c7ad07eSAntonio Huete Jimenez 
2330c7ad07eSAntonio Huete Jimenez 	linebuf.print = 6; /* big enough for longest UTF-8 sequence */
2340c7ad07eSAntonio Huete Jimenez 	linebuf.pfx_end = 0;
2350c7ad07eSAntonio Huete Jimenez 	for (linebuf.end = 0; linebuf.end < linebuf.print; linebuf.end++)
2360c7ad07eSAntonio Huete Jimenez 	{
2370c7ad07eSAntonio Huete Jimenez 		linebuf.buf[linebuf.end] = '\0';
2380c7ad07eSAntonio Huete Jimenez 		linebuf.attr[linebuf.end] = 0;
2390c7ad07eSAntonio Huete Jimenez 	}
2400c7ad07eSAntonio Huete Jimenez 
2410c7ad07eSAntonio Huete Jimenez 	end_column = 0;
24202d62a0fSDaniel Fojt 	right_curr = 0;
24302d62a0fSDaniel Fojt 	right_column = 0;
2441133e27eSPeter Avalos 	cshift = 0;
2451133e27eSPeter Avalos 	overstrike = 0;
2461133e27eSPeter Avalos 	last_overstrike = AT_NORMAL;
2471133e27eSPeter Avalos 	mbc_buf_len = 0;
2481133e27eSPeter Avalos 	is_null_line = 0;
2491133e27eSPeter Avalos 	pendc = '\0';
2500c7ad07eSAntonio Huete Jimenez 	in_hilite = 0;
2510c7ad07eSAntonio Huete Jimenez 	ansi_in_line = 0;
2520c7ad07eSAntonio Huete Jimenez 	hlink_in_line = 0;
2530c7ad07eSAntonio Huete Jimenez 	line_mark_attr = 0;
254*320d7c8aSAaron LI 	line_pos = NULL_POSITION;
2550c7ad07eSAntonio Huete Jimenez 	xbuf_reset(&shifted_ansi);
2560c7ad07eSAntonio Huete Jimenez 	xbuf_reset(&last_ansi);
2570c7ad07eSAntonio Huete Jimenez 	for (ax = 0;  ax < NUM_LAST_ANSIS;  ax++)
2580c7ad07eSAntonio Huete Jimenez 		xbuf_reset(&last_ansis[ax]);
2590c7ad07eSAntonio Huete Jimenez 	curr_last_ansi = 0;
26002d62a0fSDaniel Fojt }
26102d62a0fSDaniel Fojt 
26202d62a0fSDaniel Fojt /*
26302d62a0fSDaniel Fojt  * Set a character in the line buffer.
26402d62a0fSDaniel Fojt  */
set_linebuf(int n,char ch,int attr)265*320d7c8aSAaron LI static void set_linebuf(int n, char ch, int attr)
26602d62a0fSDaniel Fojt {
267*320d7c8aSAaron LI 	if (n >= size_linebuf)
268*320d7c8aSAaron LI 	{
269*320d7c8aSAaron LI 		/*
270*320d7c8aSAaron LI 		 * Won't fit in line buffer.
271*320d7c8aSAaron LI 		 * Try to expand it.
272*320d7c8aSAaron LI 		 */
273*320d7c8aSAaron LI 		if (expand_linebuf())
274*320d7c8aSAaron LI 			return;
275*320d7c8aSAaron LI 	}
2760c7ad07eSAntonio Huete Jimenez 	linebuf.buf[n] = ch;
2770c7ad07eSAntonio Huete Jimenez 	linebuf.attr[n] = attr;
27802d62a0fSDaniel Fojt }
27902d62a0fSDaniel Fojt 
28002d62a0fSDaniel Fojt /*
28102d62a0fSDaniel Fojt  * Append a character to the line buffer.
28202d62a0fSDaniel Fojt  */
add_linebuf(char ch,int attr,int w)283*320d7c8aSAaron LI static void add_linebuf(char ch, int attr, int w)
28402d62a0fSDaniel Fojt {
2850c7ad07eSAntonio Huete Jimenez 	set_linebuf(linebuf.end++, ch, attr);
2860c7ad07eSAntonio Huete Jimenez 	inc_end_column(w);
2871133e27eSPeter Avalos }
2881133e27eSPeter Avalos 
2891133e27eSPeter Avalos /*
2900c7ad07eSAntonio Huete Jimenez  * Append a string to the line buffer.
2910c7ad07eSAntonio Huete Jimenez  */
addstr_linebuf(char * s,int attr,int cw)292*320d7c8aSAaron LI static void addstr_linebuf(char *s, int attr, int cw)
2930c7ad07eSAntonio Huete Jimenez {
2940c7ad07eSAntonio Huete Jimenez 	for ( ;  *s != '\0';  s++)
2950c7ad07eSAntonio Huete Jimenez 		add_linebuf(*s, attr, cw);
2960c7ad07eSAntonio Huete Jimenez }
2970c7ad07eSAntonio Huete Jimenez 
2980c7ad07eSAntonio Huete Jimenez /*
2990c7ad07eSAntonio Huete Jimenez  * Set a character in the line prefix buffer.
3000c7ad07eSAntonio Huete Jimenez  */
set_pfx(int n,char ch,int attr)301*320d7c8aSAaron LI static void set_pfx(int n, char ch, int attr)
3020c7ad07eSAntonio Huete Jimenez {
3030c7ad07eSAntonio Huete Jimenez 	linebuf.pfx[n] = ch;
3040c7ad07eSAntonio Huete Jimenez 	linebuf.pfx_attr[n] = attr;
3050c7ad07eSAntonio Huete Jimenez }
3060c7ad07eSAntonio Huete Jimenez 
3070c7ad07eSAntonio Huete Jimenez /*
3080c7ad07eSAntonio Huete Jimenez  * Append a character to the line prefix buffer.
3090c7ad07eSAntonio Huete Jimenez  */
add_pfx(char ch,int attr)310*320d7c8aSAaron LI static void add_pfx(char ch, int attr)
3110c7ad07eSAntonio Huete Jimenez {
3120c7ad07eSAntonio Huete Jimenez 	set_pfx(linebuf.pfx_end++, ch, attr);
3130c7ad07eSAntonio Huete Jimenez }
3140c7ad07eSAntonio Huete Jimenez 
3150c7ad07eSAntonio Huete Jimenez /*
3160c7ad07eSAntonio Huete Jimenez  * Insert the status column and line number into the line buffer.
3171133e27eSPeter Avalos  */
plinestart(POSITION pos)318*320d7c8aSAaron LI public void plinestart(POSITION pos)
3191133e27eSPeter Avalos {
32002d62a0fSDaniel Fojt 	LINENUM linenum = 0;
32102d62a0fSDaniel Fojt 	int i;
3221133e27eSPeter Avalos 
3231133e27eSPeter Avalos 	if (linenums == OPT_ONPLUS)
3241133e27eSPeter Avalos 	{
3251133e27eSPeter Avalos 		/*
3261133e27eSPeter Avalos 		 * Get the line number and put it in the current line.
3271133e27eSPeter Avalos 		 * {{ Note: since find_linenum calls forw_raw_line,
3281133e27eSPeter Avalos 		 *    it may seek in the input file, requiring the caller
3290c7ad07eSAntonio Huete Jimenez 		 *    of plinestart to re-seek if necessary. }}
3301133e27eSPeter Avalos 		 * {{ Since forw_raw_line modifies linebuf, we must
3311133e27eSPeter Avalos 		 *    do this first, before storing anything in linebuf. }}
3321133e27eSPeter Avalos 		 */
3331133e27eSPeter Avalos 		linenum = find_linenum(pos);
3341133e27eSPeter Avalos 	}
3351133e27eSPeter Avalos 
3361133e27eSPeter Avalos 	/*
3371133e27eSPeter Avalos 	 * Display a status column if the -J option is set.
3381133e27eSPeter Avalos 	 */
3390c7ad07eSAntonio Huete Jimenez 	if (status_col || status_line)
3401133e27eSPeter Avalos 	{
34102d62a0fSDaniel Fojt 		char c = posmark(pos);
34202d62a0fSDaniel Fojt 		if (c != 0)
3430c7ad07eSAntonio Huete Jimenez 			line_mark_attr = AT_HILITE|AT_COLOR_MARK;
3440c7ad07eSAntonio Huete Jimenez 		else if (start_attnpos != NULL_POSITION &&
34502d62a0fSDaniel Fojt 		         pos >= start_attnpos && pos <= end_attnpos)
3460c7ad07eSAntonio Huete Jimenez 			line_mark_attr = AT_HILITE|AT_COLOR_ATTN;
3470c7ad07eSAntonio Huete Jimenez 		if (status_col)
3480c7ad07eSAntonio Huete Jimenez 		{
3490c7ad07eSAntonio Huete Jimenez 			add_pfx(c ? c : ' ', line_mark_attr); /* column 0: status */
3500c7ad07eSAntonio Huete Jimenez 			while (linebuf.pfx_end < status_col_width)
3510c7ad07eSAntonio Huete Jimenez 				add_pfx(' ', AT_NORMAL);
3521133e27eSPeter Avalos 		}
35302d62a0fSDaniel Fojt 	}
35402d62a0fSDaniel Fojt 
3551133e27eSPeter Avalos 	/*
3561133e27eSPeter Avalos 	 * Display the line number at the start of each line
3571133e27eSPeter Avalos 	 * if the -N option is set.
3581133e27eSPeter Avalos 	 */
3591133e27eSPeter Avalos 	if (linenums == OPT_ONPLUS)
3601133e27eSPeter Avalos 	{
36102d62a0fSDaniel Fojt 		char buf[INT_STRLEN_BOUND(linenum) + 2];
3621133e27eSPeter Avalos 		int len;
3631133e27eSPeter Avalos 
3640c7ad07eSAntonio Huete Jimenez 		linenum = vlinenum(linenum);
3650c7ad07eSAntonio Huete Jimenez 		if (linenum == 0)
3660c7ad07eSAntonio Huete Jimenez 			len = 0;
3670c7ad07eSAntonio Huete Jimenez 		else
3680c7ad07eSAntonio Huete Jimenez 		{
369*320d7c8aSAaron LI 			linenumtoa(linenum, buf, 10);
3700c7ad07eSAntonio Huete Jimenez 			len = (int) strlen(buf);
3710c7ad07eSAntonio Huete Jimenez 		}
3720c7ad07eSAntonio Huete Jimenez 		for (i = 0; i < linenum_width - len; i++)
3730c7ad07eSAntonio Huete Jimenez 			add_pfx(' ', AT_NORMAL);
3740c7ad07eSAntonio Huete Jimenez 		for (i = 0; i < len; i++)
3750c7ad07eSAntonio Huete Jimenez 			add_pfx(buf[i], AT_BOLD|AT_COLOR_LINENUM);
3760c7ad07eSAntonio Huete Jimenez 		add_pfx(' ', AT_NORMAL);
3770c7ad07eSAntonio Huete Jimenez 	}
3780c7ad07eSAntonio Huete Jimenez 	end_column = linebuf.pfx_end;
3790c7ad07eSAntonio Huete Jimenez }
3801133e27eSPeter Avalos 
3811133e27eSPeter Avalos /*
3820c7ad07eSAntonio Huete Jimenez  * Return the width of the line prefix (status column and line number).
3830c7ad07eSAntonio Huete Jimenez  * {{ Actual line number can be wider than linenum_width. }}
3841133e27eSPeter Avalos  */
line_pfx_width(void)385*320d7c8aSAaron LI public int line_pfx_width(void)
3861133e27eSPeter Avalos {
3870c7ad07eSAntonio Huete Jimenez 	int width = 0;
3880c7ad07eSAntonio Huete Jimenez 	if (status_col)
3890c7ad07eSAntonio Huete Jimenez 		width += status_col_width;
3900c7ad07eSAntonio Huete Jimenez 	if (linenums == OPT_ONPLUS)
3910c7ad07eSAntonio Huete Jimenez 		width += linenum_width + 1;
3920c7ad07eSAntonio Huete Jimenez 	return width;
3931133e27eSPeter Avalos }
3941133e27eSPeter Avalos 
3951133e27eSPeter Avalos /*
3960c7ad07eSAntonio Huete Jimenez  * Shift line left so that the last char is just to the left
3970c7ad07eSAntonio Huete Jimenez  * of the first visible column.
3981133e27eSPeter Avalos  */
pshift_all(void)399*320d7c8aSAaron LI public void pshift_all(void)
4001133e27eSPeter Avalos {
4010c7ad07eSAntonio Huete Jimenez 	int i;
4020c7ad07eSAntonio Huete Jimenez 	for (i = linebuf.print;  i < linebuf.end;  i++)
4030c7ad07eSAntonio Huete Jimenez 		if (linebuf.attr[i] == AT_ANSI)
404*320d7c8aSAaron LI 			xbuf_add_byte(&shifted_ansi, (unsigned char) linebuf.buf[i]);
4050c7ad07eSAntonio Huete Jimenez 	linebuf.end = linebuf.print;
4060c7ad07eSAntonio Huete Jimenez 	end_column = linebuf.pfx_end;
4071133e27eSPeter Avalos }
4081133e27eSPeter Avalos 
4091133e27eSPeter Avalos /*
4101133e27eSPeter Avalos  * Return the printing width of the start (enter) sequence
4111133e27eSPeter Avalos  * for a given character attribute.
4121133e27eSPeter Avalos  */
attr_swidth(int a)413*320d7c8aSAaron LI static int attr_swidth(int a)
4141133e27eSPeter Avalos {
4151133e27eSPeter Avalos 	int w = 0;
4161133e27eSPeter Avalos 
4171133e27eSPeter Avalos 	a = apply_at_specials(a);
4181133e27eSPeter Avalos 
4191133e27eSPeter Avalos 	if (a & AT_UNDERLINE)
4201133e27eSPeter Avalos 		w += ul_s_width;
4211133e27eSPeter Avalos 	if (a & AT_BOLD)
4221133e27eSPeter Avalos 		w += bo_s_width;
4231133e27eSPeter Avalos 	if (a & AT_BLINK)
4241133e27eSPeter Avalos 		w += bl_s_width;
4251133e27eSPeter Avalos 	if (a & AT_STANDOUT)
4261133e27eSPeter Avalos 		w += so_s_width;
4271133e27eSPeter Avalos 
4281133e27eSPeter Avalos 	return w;
4291133e27eSPeter Avalos }
4301133e27eSPeter Avalos 
4311133e27eSPeter Avalos /*
4321133e27eSPeter Avalos  * Return the printing width of the end (exit) sequence
4331133e27eSPeter Avalos  * for a given character attribute.
4341133e27eSPeter Avalos  */
attr_ewidth(int a)435*320d7c8aSAaron LI static int attr_ewidth(int a)
4361133e27eSPeter Avalos {
4371133e27eSPeter Avalos 	int w = 0;
4381133e27eSPeter Avalos 
4391133e27eSPeter Avalos 	a = apply_at_specials(a);
4401133e27eSPeter Avalos 
4411133e27eSPeter Avalos 	if (a & AT_UNDERLINE)
4421133e27eSPeter Avalos 		w += ul_e_width;
4431133e27eSPeter Avalos 	if (a & AT_BOLD)
4441133e27eSPeter Avalos 		w += bo_e_width;
4451133e27eSPeter Avalos 	if (a & AT_BLINK)
4461133e27eSPeter Avalos 		w += bl_e_width;
4471133e27eSPeter Avalos 	if (a & AT_STANDOUT)
4481133e27eSPeter Avalos 		w += so_e_width;
4491133e27eSPeter Avalos 
4501133e27eSPeter Avalos 	return w;
4511133e27eSPeter Avalos }
4521133e27eSPeter Avalos 
4531133e27eSPeter Avalos /*
4541133e27eSPeter Avalos  * Return the printing width of a given character and attribute,
4550c7ad07eSAntonio Huete Jimenez  * if the character were added after prev_ch.
4561133e27eSPeter Avalos  * Adding a character with a given attribute may cause an enter or exit
4571133e27eSPeter Avalos  * attribute sequence to be inserted, so this must be taken into account.
4581133e27eSPeter Avalos  */
pwidth(LWCHAR ch,int a,LWCHAR prev_ch,int prev_a)459*320d7c8aSAaron LI public int pwidth(LWCHAR ch, int a, LWCHAR prev_ch, int prev_a)
4601133e27eSPeter Avalos {
4611133e27eSPeter Avalos 	int w;
4621133e27eSPeter Avalos 
4631133e27eSPeter Avalos 	if (ch == '\b')
4640c7ad07eSAntonio Huete Jimenez 	{
4651133e27eSPeter Avalos 		/*
4661133e27eSPeter Avalos 		 * Backspace moves backwards one or two positions.
4671133e27eSPeter Avalos 		 */
4680c7ad07eSAntonio Huete Jimenez 		if (prev_a & (AT_ANSI|AT_BINARY))
4690c7ad07eSAntonio Huete Jimenez 			return strlen(prchar('\b'));
4701133e27eSPeter Avalos 		return (utf_mode && is_wide_char(prev_ch)) ? -2 : -1;
4710c7ad07eSAntonio Huete Jimenez 	}
4721133e27eSPeter Avalos 
4731133e27eSPeter Avalos 	if (!utf_mode || is_ascii_char(ch))
4741133e27eSPeter Avalos 	{
4751133e27eSPeter Avalos 		if (control_char((char)ch))
4761133e27eSPeter Avalos 		{
4771133e27eSPeter Avalos 			/*
4781133e27eSPeter Avalos 			 * Control characters do unpredictable things,
4791133e27eSPeter Avalos 			 * so we don't even try to guess; say it doesn't move.
4801133e27eSPeter Avalos 			 * This can only happen if the -r flag is in effect.
4811133e27eSPeter Avalos 			 */
4821133e27eSPeter Avalos 			return (0);
4831133e27eSPeter Avalos 		}
4841133e27eSPeter Avalos 	} else
4851133e27eSPeter Avalos 	{
4861133e27eSPeter Avalos 		if (is_composing_char(ch) || is_combining_char(prev_ch, ch))
4871133e27eSPeter Avalos 		{
4881133e27eSPeter Avalos 			/*
4891133e27eSPeter Avalos 			 * Composing and combining chars take up no space.
4901133e27eSPeter Avalos 			 *
4911133e27eSPeter Avalos 			 * Some terminals, upon failure to compose a
4921133e27eSPeter Avalos 			 * composing character with the character(s) that
4930c7ad07eSAntonio Huete Jimenez 			 * precede(s) it will actually take up one end_column
4941133e27eSPeter Avalos 			 * for the composing character; there isn't much
4951133e27eSPeter Avalos 			 * we could do short of testing the (complex)
4961133e27eSPeter Avalos 			 * composition process ourselves and printing
4971133e27eSPeter Avalos 			 * a binary representation when it fails.
4981133e27eSPeter Avalos 			 */
4991133e27eSPeter Avalos 			return (0);
5001133e27eSPeter Avalos 		}
5011133e27eSPeter Avalos 	}
5021133e27eSPeter Avalos 
5031133e27eSPeter Avalos 	/*
5041133e27eSPeter Avalos 	 * Other characters take one or two columns,
5051133e27eSPeter Avalos 	 * plus the width of any attribute enter/exit sequence.
5061133e27eSPeter Avalos 	 */
5071133e27eSPeter Avalos 	w = 1;
5081133e27eSPeter Avalos 	if (is_wide_char(ch))
5091133e27eSPeter Avalos 		w++;
5100c7ad07eSAntonio Huete Jimenez 	if (linebuf.end > 0 && !is_at_equiv(linebuf.attr[linebuf.end-1], a))
5110c7ad07eSAntonio Huete Jimenez 		w += attr_ewidth(linebuf.attr[linebuf.end-1]);
5120c7ad07eSAntonio Huete Jimenez 	if (apply_at_specials(a) != AT_NORMAL &&
5130c7ad07eSAntonio Huete Jimenez 	    (linebuf.end == 0 || !is_at_equiv(linebuf.attr[linebuf.end-1], a)))
5141133e27eSPeter Avalos 		w += attr_swidth(a);
5151133e27eSPeter Avalos 	return (w);
5161133e27eSPeter Avalos }
5171133e27eSPeter Avalos 
5181133e27eSPeter Avalos /*
5191133e27eSPeter Avalos  * Delete to the previous base character in the line buffer.
5201133e27eSPeter Avalos  */
backc(void)521*320d7c8aSAaron LI static int backc(void)
5221133e27eSPeter Avalos {
5230c7ad07eSAntonio Huete Jimenez 	LWCHAR ch;
5241133e27eSPeter Avalos 	char *p;
5251133e27eSPeter Avalos 
5260c7ad07eSAntonio Huete Jimenez 	if (linebuf.end == 0)
5270c7ad07eSAntonio Huete Jimenez 		return (0);
5280c7ad07eSAntonio Huete Jimenez 	p = &linebuf.buf[linebuf.end];
5290c7ad07eSAntonio Huete Jimenez 	ch = step_char(&p, -1, linebuf.buf);
5300c7ad07eSAntonio Huete Jimenez 	/* Skip back to the next nonzero-width char. */
5310c7ad07eSAntonio Huete Jimenez 	while (p > linebuf.buf)
5321133e27eSPeter Avalos 	{
5330c7ad07eSAntonio Huete Jimenez 		LWCHAR prev_ch;
5340c7ad07eSAntonio Huete Jimenez 		int width;
5350c7ad07eSAntonio Huete Jimenez 		linebuf.end = (int) (p - linebuf.buf);
5360c7ad07eSAntonio Huete Jimenez 		prev_ch = step_char(&p, -1, linebuf.buf);
5370c7ad07eSAntonio Huete Jimenez 		width = pwidth(ch, linebuf.attr[linebuf.end], prev_ch, linebuf.attr[linebuf.end-1]);
5380c7ad07eSAntonio Huete Jimenez 		end_column -= width;
5390c7ad07eSAntonio Huete Jimenez 		/* {{ right_column? }} */
5400c7ad07eSAntonio Huete Jimenez 		if (width > 0)
5410c7ad07eSAntonio Huete Jimenez 			break;
5420c7ad07eSAntonio Huete Jimenez 		ch = prev_ch;
5431133e27eSPeter Avalos 	}
5440c7ad07eSAntonio Huete Jimenez 	return (1);
5451133e27eSPeter Avalos }
5461133e27eSPeter Avalos 
5471133e27eSPeter Avalos /*
548*320d7c8aSAaron LI  * Preserve the current position in the line buffer (for word wrapping).
549*320d7c8aSAaron LI  */
savec(void)550*320d7c8aSAaron LI public void savec(void)
551*320d7c8aSAaron LI {
552*320d7c8aSAaron LI 	saved_line_end = linebuf.end;
553*320d7c8aSAaron LI 	saved_end_column = end_column;
554*320d7c8aSAaron LI }
555*320d7c8aSAaron LI 
556*320d7c8aSAaron LI /*
557*320d7c8aSAaron LI  * Restore the position in the line buffer (start of line for word wrapping).
558*320d7c8aSAaron LI  */
loadc(void)559*320d7c8aSAaron LI public void loadc(void)
560*320d7c8aSAaron LI {
561*320d7c8aSAaron LI 	linebuf.end = saved_line_end;
562*320d7c8aSAaron LI 	end_column = saved_end_column;
563*320d7c8aSAaron LI }
564*320d7c8aSAaron LI 
565*320d7c8aSAaron LI /*
5661133e27eSPeter Avalos  * Is a character the end of an ANSI escape sequence?
5671133e27eSPeter Avalos  */
is_ansi_end(LWCHAR ch)568*320d7c8aSAaron LI public int is_ansi_end(LWCHAR ch)
5691133e27eSPeter Avalos {
5701133e27eSPeter Avalos 	if (!is_ascii_char(ch))
5711133e27eSPeter Avalos 		return (0);
5721133e27eSPeter Avalos 	return (strchr(end_ansi_chars, (char) ch) != NULL);
5731133e27eSPeter Avalos }
5741133e27eSPeter Avalos 
5751133e27eSPeter Avalos /*
57602d62a0fSDaniel Fojt  * Can a char appear in an ANSI escape sequence, before the end char?
5771133e27eSPeter Avalos  */
is_ansi_middle(LWCHAR ch)578*320d7c8aSAaron LI public int is_ansi_middle(LWCHAR ch)
5791133e27eSPeter Avalos {
5801133e27eSPeter Avalos 	if (!is_ascii_char(ch))
5811133e27eSPeter Avalos 		return (0);
5821133e27eSPeter Avalos 	if (is_ansi_end(ch))
5831133e27eSPeter Avalos 		return (0);
5841133e27eSPeter Avalos 	return (strchr(mid_ansi_chars, (char) ch) != NULL);
5851133e27eSPeter Avalos }
5861133e27eSPeter Avalos 
5871133e27eSPeter Avalos /*
58802d62a0fSDaniel Fojt  * Skip past an ANSI escape sequence.
58902d62a0fSDaniel Fojt  * pp is initially positioned just after the CSI_START char.
59002d62a0fSDaniel Fojt  */
skip_ansi(struct ansi_state * pansi,char ** pp,constant char * limit)591*320d7c8aSAaron LI public void skip_ansi(struct ansi_state *pansi, char **pp, constant char *limit)
59202d62a0fSDaniel Fojt {
59302d62a0fSDaniel Fojt 	LWCHAR c;
59402d62a0fSDaniel Fojt 	do {
59502d62a0fSDaniel Fojt 		c = step_char(pp, +1, limit);
5960c7ad07eSAntonio Huete Jimenez 	} while (*pp < limit && ansi_step(pansi, c) == ANSI_MID);
5970c7ad07eSAntonio Huete Jimenez 	/* Note that we discard final char, for which is_ansi_end is true. */
59802d62a0fSDaniel Fojt }
59902d62a0fSDaniel Fojt 
6000c7ad07eSAntonio Huete Jimenez /*
6010c7ad07eSAntonio Huete Jimenez  * Determine if a character starts an ANSI escape sequence.
6020c7ad07eSAntonio Huete Jimenez  * If so, return an ansi_state struct; otherwise return NULL.
6030c7ad07eSAntonio Huete Jimenez  */
ansi_start(LWCHAR ch)604*320d7c8aSAaron LI public struct ansi_state * ansi_start(LWCHAR ch)
6050c7ad07eSAntonio Huete Jimenez {
6060c7ad07eSAntonio Huete Jimenez 	struct ansi_state *pansi;
6070c7ad07eSAntonio Huete Jimenez 
6080c7ad07eSAntonio Huete Jimenez 	if (!IS_CSI_START(ch))
6090c7ad07eSAntonio Huete Jimenez 		return NULL;
6100c7ad07eSAntonio Huete Jimenez 	pansi = ecalloc(1, sizeof(struct ansi_state));
6110c7ad07eSAntonio Huete Jimenez 	pansi->hindex = 0;
6120c7ad07eSAntonio Huete Jimenez 	pansi->hlink = 0;
6130c7ad07eSAntonio Huete Jimenez 	pansi->prev_esc = 0;
6140c7ad07eSAntonio Huete Jimenez 	return pansi;
6150c7ad07eSAntonio Huete Jimenez }
6160c7ad07eSAntonio Huete Jimenez 
6170c7ad07eSAntonio Huete Jimenez /*
6180c7ad07eSAntonio Huete Jimenez  * Determine whether the next char in an ANSI escape sequence
6190c7ad07eSAntonio Huete Jimenez  * ends the sequence.
6200c7ad07eSAntonio Huete Jimenez  */
ansi_step(struct ansi_state * pansi,LWCHAR ch)621*320d7c8aSAaron LI public int ansi_step(struct ansi_state *pansi, LWCHAR ch)
6220c7ad07eSAntonio Huete Jimenez {
6230c7ad07eSAntonio Huete Jimenez 	if (pansi->hlink)
6240c7ad07eSAntonio Huete Jimenez 	{
6250c7ad07eSAntonio Huete Jimenez 		/* Hyperlink ends with \7 or ESC-backslash. */
6260c7ad07eSAntonio Huete Jimenez 		if (ch == '\7')
6270c7ad07eSAntonio Huete Jimenez 			return ANSI_END;
628*320d7c8aSAaron LI 		if (pansi->prev_esc)
629*320d7c8aSAaron LI 			return (ch == '\\') ? ANSI_END : ANSI_ERR;
6300c7ad07eSAntonio Huete Jimenez 		pansi->prev_esc = (ch == ESC);
6310c7ad07eSAntonio Huete Jimenez 		return ANSI_MID;
6320c7ad07eSAntonio Huete Jimenez 	}
6330c7ad07eSAntonio Huete Jimenez 	if (pansi->hindex >= 0)
6340c7ad07eSAntonio Huete Jimenez 	{
6350c7ad07eSAntonio Huete Jimenez 		static char hlink_prefix[] = ESCS "]8;";
6360c7ad07eSAntonio Huete Jimenez 		if (ch == hlink_prefix[pansi->hindex] ||
6370c7ad07eSAntonio Huete Jimenez 		    (pansi->hindex == 0 && IS_CSI_START(ch)))
6380c7ad07eSAntonio Huete Jimenez 		{
6390c7ad07eSAntonio Huete Jimenez 			pansi->hindex++;
6400c7ad07eSAntonio Huete Jimenez 			if (hlink_prefix[pansi->hindex] == '\0')
6410c7ad07eSAntonio Huete Jimenez 				pansi->hlink = 1; /* now processing hyperlink addr */
6420c7ad07eSAntonio Huete Jimenez 			return ANSI_MID;
6430c7ad07eSAntonio Huete Jimenez 		}
6440c7ad07eSAntonio Huete Jimenez 		pansi->hindex = -1; /* not a hyperlink */
6450c7ad07eSAntonio Huete Jimenez 	}
6460c7ad07eSAntonio Huete Jimenez 	/* Check for SGR sequences */
6470c7ad07eSAntonio Huete Jimenez 	if (is_ansi_middle(ch))
6480c7ad07eSAntonio Huete Jimenez 		return ANSI_MID;
6490c7ad07eSAntonio Huete Jimenez 	if (is_ansi_end(ch))
6500c7ad07eSAntonio Huete Jimenez 		return ANSI_END;
6510c7ad07eSAntonio Huete Jimenez 	return ANSI_ERR;
6520c7ad07eSAntonio Huete Jimenez }
6530c7ad07eSAntonio Huete Jimenez 
6540c7ad07eSAntonio Huete Jimenez /*
6550c7ad07eSAntonio Huete Jimenez  * Free an ansi_state structure.
6560c7ad07eSAntonio Huete Jimenez  */
ansi_done(struct ansi_state * pansi)657*320d7c8aSAaron LI public void ansi_done(struct ansi_state *pansi)
6580c7ad07eSAntonio Huete Jimenez {
6590c7ad07eSAntonio Huete Jimenez 	free(pansi);
6600c7ad07eSAntonio Huete Jimenez }
6610c7ad07eSAntonio Huete Jimenez 
6620c7ad07eSAntonio Huete Jimenez /*
6630c7ad07eSAntonio Huete Jimenez  * Will w characters in attribute a fit on the screen?
6640c7ad07eSAntonio Huete Jimenez  */
fits_on_screen(int w,int a)665*320d7c8aSAaron LI static int fits_on_screen(int w, int a)
6660c7ad07eSAntonio Huete Jimenez {
6670c7ad07eSAntonio Huete Jimenez 	if (ctldisp == OPT_ON)
6680c7ad07eSAntonio Huete Jimenez 		/* We're not counting, so say that everything fits. */
6690c7ad07eSAntonio Huete Jimenez 		return 1;
6700c7ad07eSAntonio Huete Jimenez 	return (end_column - cshift + w + attr_ewidth(a) <= sc_width);
6710c7ad07eSAntonio Huete Jimenez }
67202d62a0fSDaniel Fojt 
67302d62a0fSDaniel Fojt /*
6741133e27eSPeter Avalos  * Append a character and attribute to the line buffer.
6751133e27eSPeter Avalos  */
6761133e27eSPeter Avalos #define STORE_CHAR(ch,a,rep,pos) \
6771133e27eSPeter Avalos 	do { \
6781133e27eSPeter Avalos 		if (store_char((ch),(a),(rep),(pos))) return (1); \
6791133e27eSPeter Avalos 	} while (0)
6801133e27eSPeter Avalos 
store_char(LWCHAR ch,int a,char * rep,POSITION pos)681*320d7c8aSAaron LI static int store_char(LWCHAR ch, int a, char *rep, POSITION pos)
6821133e27eSPeter Avalos {
6831133e27eSPeter Avalos 	int w;
6840c7ad07eSAntonio Huete Jimenez 	int i;
6851133e27eSPeter Avalos 	int replen;
6861133e27eSPeter Avalos 	char cs;
6871133e27eSPeter Avalos 
6880c7ad07eSAntonio Huete Jimenez 	i = (a & (AT_UNDERLINE|AT_BOLD));
6890c7ad07eSAntonio Huete Jimenez 	if (i != AT_NORMAL)
6900c7ad07eSAntonio Huete Jimenez 		last_overstrike = i;
6911133e27eSPeter Avalos 
6921133e27eSPeter Avalos #if HILITE_SEARCH
6931133e27eSPeter Avalos 	{
6941133e27eSPeter Avalos 		int matches;
6950c7ad07eSAntonio Huete Jimenez 		int resend_last = 0;
696*320d7c8aSAaron LI 		int hl_attr = 0;
6970c7ad07eSAntonio Huete Jimenez 
6980c7ad07eSAntonio Huete Jimenez 		if (pos == NULL_POSITION)
6990c7ad07eSAntonio Huete Jimenez 		{
7000c7ad07eSAntonio Huete Jimenez 			/* Color the prompt unless it has ansi sequences in it. */
7010c7ad07eSAntonio Huete Jimenez 			hl_attr = ansi_in_line ? 0 : AT_STANDOUT|AT_COLOR_PROMPT;
702*320d7c8aSAaron LI 		} else if (a != AT_ANSI)
7030c7ad07eSAntonio Huete Jimenez 		{
7040c7ad07eSAntonio Huete Jimenez 			hl_attr = is_hilited_attr(pos, pos+1, 0, &matches);
7050c7ad07eSAntonio Huete Jimenez 			if (hl_attr == 0 && status_line)
7060c7ad07eSAntonio Huete Jimenez 				hl_attr = line_mark_attr;
7070c7ad07eSAntonio Huete Jimenez 		}
7080c7ad07eSAntonio Huete Jimenez 		if (hl_attr)
7091133e27eSPeter Avalos 		{
7101133e27eSPeter Avalos 			/*
7111133e27eSPeter Avalos 			 * This character should be highlighted.
7121133e27eSPeter Avalos 			 * Override the attribute passed in.
7131133e27eSPeter Avalos 			 */
714*320d7c8aSAaron LI 			a |= hl_attr;
7150c7ad07eSAntonio Huete Jimenez 			if (highest_hilite != NULL_POSITION && pos != NULL_POSITION && pos > highest_hilite)
716e639dc31SJohn Marino 				highest_hilite = pos;
7170c7ad07eSAntonio Huete Jimenez 			in_hilite = 1;
7180c7ad07eSAntonio Huete Jimenez 		} else
7190c7ad07eSAntonio Huete Jimenez 		{
7200c7ad07eSAntonio Huete Jimenez 			if (in_hilite)
7210c7ad07eSAntonio Huete Jimenez 			{
7220c7ad07eSAntonio Huete Jimenez 				/*
7230c7ad07eSAntonio Huete Jimenez 				 * This is the first non-hilited char after a hilite.
7240c7ad07eSAntonio Huete Jimenez 				 * Resend the last ANSI seq to restore color.
7250c7ad07eSAntonio Huete Jimenez 				 */
7260c7ad07eSAntonio Huete Jimenez 				resend_last = 1;
7270c7ad07eSAntonio Huete Jimenez 			}
7280c7ad07eSAntonio Huete Jimenez 			in_hilite = 0;
7290c7ad07eSAntonio Huete Jimenez 		}
7300c7ad07eSAntonio Huete Jimenez 		if (resend_last)
7310c7ad07eSAntonio Huete Jimenez 		{
7320c7ad07eSAntonio Huete Jimenez 			int ai;
7330c7ad07eSAntonio Huete Jimenez 			for (ai = 0;  ai < NUM_LAST_ANSIS;  ai++)
7340c7ad07eSAntonio Huete Jimenez 			{
7350c7ad07eSAntonio Huete Jimenez 				int ax = (curr_last_ansi + ai) % NUM_LAST_ANSIS;
7360c7ad07eSAntonio Huete Jimenez 				for (i = 0;  i < last_ansis[ax].end;  i++)
7370c7ad07eSAntonio Huete Jimenez 					STORE_CHAR(last_ansis[ax].data[i], AT_ANSI, NULL, pos);
7381133e27eSPeter Avalos 			}
7391133e27eSPeter Avalos 		}
740e639dc31SJohn Marino 	}
7411133e27eSPeter Avalos #endif
7421133e27eSPeter Avalos 
7430c7ad07eSAntonio Huete Jimenez 	if (a == AT_ANSI) {
7441133e27eSPeter Avalos 		w = 0;
7450c7ad07eSAntonio Huete Jimenez 	} else {
7460c7ad07eSAntonio Huete Jimenez 		char *p = &linebuf.buf[linebuf.end];
7470c7ad07eSAntonio Huete Jimenez 		LWCHAR prev_ch = (linebuf.end > 0) ? step_char(&p, -1, linebuf.buf) : 0;
7480c7ad07eSAntonio Huete Jimenez 		int prev_a = (linebuf.end > 0) ? linebuf.attr[linebuf.end-1] : 0;
7490c7ad07eSAntonio Huete Jimenez 		w = pwidth(ch, a, prev_ch, prev_a);
7501133e27eSPeter Avalos 	}
7511133e27eSPeter Avalos 
7520c7ad07eSAntonio Huete Jimenez 	if (!fits_on_screen(w, a))
7531133e27eSPeter Avalos 		return (1);
7541133e27eSPeter Avalos 
7551133e27eSPeter Avalos 	if (rep == NULL)
7561133e27eSPeter Avalos 	{
7571133e27eSPeter Avalos 		cs = (char) ch;
7581133e27eSPeter Avalos 		rep = &cs;
7591133e27eSPeter Avalos 		replen = 1;
7601133e27eSPeter Avalos 	} else
7611133e27eSPeter Avalos 	{
7621133e27eSPeter Avalos 		replen = utf_len(rep[0]);
7631133e27eSPeter Avalos 	}
7641133e27eSPeter Avalos 
765*320d7c8aSAaron LI 	if (cshift == hshift)
766*320d7c8aSAaron LI 	{
767*320d7c8aSAaron LI 		if (line_pos == NULL_POSITION)
768*320d7c8aSAaron LI 			line_pos = pos;
769*320d7c8aSAaron LI 		if (shifted_ansi.end > 0)
77002d62a0fSDaniel Fojt 		{
7710c7ad07eSAntonio Huete Jimenez 			/* Copy shifted ANSI sequences to beginning of line. */
7720c7ad07eSAntonio Huete Jimenez 			for (i = 0;  i < shifted_ansi.end;  i++)
7730c7ad07eSAntonio Huete Jimenez 				add_linebuf(shifted_ansi.data[i], AT_ANSI, 0);
7740c7ad07eSAntonio Huete Jimenez 			xbuf_reset(&shifted_ansi);
7750c7ad07eSAntonio Huete Jimenez 		}
776*320d7c8aSAaron LI 	}
777*320d7c8aSAaron LI 
7780c7ad07eSAntonio Huete Jimenez 	/* Add the char to the buf, even if we will left-shift it next. */
7790c7ad07eSAntonio Huete Jimenez 	inc_end_column(w);
7800c7ad07eSAntonio Huete Jimenez 	for (i = 0;  i < replen;  i++)
7810c7ad07eSAntonio Huete Jimenez 		add_linebuf(*rep++, a, 0);
7820c7ad07eSAntonio Huete Jimenez 
7830c7ad07eSAntonio Huete Jimenez 	if (cshift < hshift)
7840c7ad07eSAntonio Huete Jimenez 	{
7850c7ad07eSAntonio Huete Jimenez 		/* We haven't left-shifted enough yet. */
7860c7ad07eSAntonio Huete Jimenez 		if (a == AT_ANSI)
787*320d7c8aSAaron LI 			xbuf_add_byte(&shifted_ansi, (unsigned char) ch); /* Save ANSI attributes */
7880c7ad07eSAntonio Huete Jimenez 		if (linebuf.end > linebuf.print)
7890c7ad07eSAntonio Huete Jimenez 		{
7900c7ad07eSAntonio Huete Jimenez 			/* Shift left enough to put last byte of this char at print-1. */
7910c7ad07eSAntonio Huete Jimenez 			int i;
7920c7ad07eSAntonio Huete Jimenez 			for (i = 0; i < linebuf.print; i++)
7930c7ad07eSAntonio Huete Jimenez 			{
7940c7ad07eSAntonio Huete Jimenez 				linebuf.buf[i] = linebuf.buf[i+replen];
7950c7ad07eSAntonio Huete Jimenez 				linebuf.attr[i] = linebuf.attr[i+replen];
7960c7ad07eSAntonio Huete Jimenez 			}
7970c7ad07eSAntonio Huete Jimenez 			linebuf.end -= replen;
7980c7ad07eSAntonio Huete Jimenez 			cshift += w;
7990c7ad07eSAntonio Huete Jimenez 			/*
8000c7ad07eSAntonio Huete Jimenez 			 * If the char we just left-shifted was double width,
8010c7ad07eSAntonio Huete Jimenez 			 * the 2 spaces we shifted may be too much.
8020c7ad07eSAntonio Huete Jimenez 			 * Represent the "half char" at start of line with a highlighted space.
8030c7ad07eSAntonio Huete Jimenez 			 */
8040c7ad07eSAntonio Huete Jimenez 			while (cshift > hshift)
8050c7ad07eSAntonio Huete Jimenez 			{
8060c7ad07eSAntonio Huete Jimenez 				add_linebuf(' ', rscroll_attr, 0);
8070c7ad07eSAntonio Huete Jimenez 				cshift--;
8080c7ad07eSAntonio Huete Jimenez 			}
8090c7ad07eSAntonio Huete Jimenez 		}
8100c7ad07eSAntonio Huete Jimenez 	}
8110c7ad07eSAntonio Huete Jimenez 	return (0);
81202d62a0fSDaniel Fojt }
81302d62a0fSDaniel Fojt 
8140c7ad07eSAntonio Huete Jimenez #define STORE_STRING(s,a,pos) \
8150c7ad07eSAntonio Huete Jimenez 	do { if (store_string((s),(a),(pos))) return (1); } while (0)
8160c7ad07eSAntonio Huete Jimenez 
store_string(char * s,int a,POSITION pos)817*320d7c8aSAaron LI static int store_string(char *s, int a, POSITION pos)
8181133e27eSPeter Avalos {
8190c7ad07eSAntonio Huete Jimenez 	if (!fits_on_screen(strlen(s), a))
8200c7ad07eSAntonio Huete Jimenez 		return 1;
8210c7ad07eSAntonio Huete Jimenez 	for ( ;  *s != 0;  s++)
8220c7ad07eSAntonio Huete Jimenez 		STORE_CHAR(*s, a, NULL, pos);
8230c7ad07eSAntonio Huete Jimenez 	return 0;
8241133e27eSPeter Avalos }
8251133e27eSPeter Avalos 
8261133e27eSPeter Avalos /*
8271133e27eSPeter Avalos  * Append a tab to the line buffer.
8281133e27eSPeter Avalos  * Store spaces to represent the tab.
8291133e27eSPeter Avalos  */
8301133e27eSPeter Avalos #define STORE_TAB(a,pos) \
8311133e27eSPeter Avalos 	do { if (store_tab((a),(pos))) return (1); } while (0)
8321133e27eSPeter Avalos 
store_tab(int attr,POSITION pos)833*320d7c8aSAaron LI static int store_tab(int attr, POSITION pos)
8341133e27eSPeter Avalos {
8350c7ad07eSAntonio Huete Jimenez 	int to_tab = end_column - linebuf.pfx_end;
8361133e27eSPeter Avalos 
8371133e27eSPeter Avalos 	if (ntabstops < 2 || to_tab >= tabstops[ntabstops-1])
8381133e27eSPeter Avalos 		to_tab = tabdefault -
8391133e27eSPeter Avalos 		     ((to_tab - tabstops[ntabstops-1]) % tabdefault);
8401133e27eSPeter Avalos 	else
8411133e27eSPeter Avalos 	{
8420c7ad07eSAntonio Huete Jimenez 		int i;
8431133e27eSPeter Avalos 		for (i = ntabstops - 2;  i >= 0;  i--)
8441133e27eSPeter Avalos 			if (to_tab >= tabstops[i])
8451133e27eSPeter Avalos 				break;
8461133e27eSPeter Avalos 		to_tab = tabstops[i+1] - to_tab;
8471133e27eSPeter Avalos 	}
8481133e27eSPeter Avalos 
8491133e27eSPeter Avalos 	do {
8501133e27eSPeter Avalos 		STORE_CHAR(' ', attr, " ", pos);
8511133e27eSPeter Avalos 	} while (--to_tab > 0);
8521133e27eSPeter Avalos 	return 0;
8531133e27eSPeter Avalos }
8541133e27eSPeter Avalos 
8551133e27eSPeter Avalos #define STORE_PRCHAR(c, pos) \
8561133e27eSPeter Avalos 	do { if (store_prchar((c), (pos))) return 1; } while (0)
8571133e27eSPeter Avalos 
store_prchar(LWCHAR c,POSITION pos)858*320d7c8aSAaron LI static int store_prchar(LWCHAR c, POSITION pos)
8591133e27eSPeter Avalos {
8601133e27eSPeter Avalos 	/*
8611133e27eSPeter Avalos 	 * Convert to printable representation.
8621133e27eSPeter Avalos 	 */
8630c7ad07eSAntonio Huete Jimenez 	STORE_STRING(prchar(c), AT_BINARY|AT_COLOR_CTRL, pos);
8641133e27eSPeter Avalos 	return 0;
8651133e27eSPeter Avalos }
8661133e27eSPeter Avalos 
flush_mbc_buf(POSITION pos)867*320d7c8aSAaron LI static int flush_mbc_buf(POSITION pos)
8681133e27eSPeter Avalos {
8691133e27eSPeter Avalos 	int i;
8701133e27eSPeter Avalos 
8711133e27eSPeter Avalos 	for (i = 0; i < mbc_buf_index; i++)
8721133e27eSPeter Avalos 		if (store_prchar(mbc_buf[i], pos))
8731133e27eSPeter Avalos 			return mbc_buf_index - i;
8741133e27eSPeter Avalos 	return 0;
8751133e27eSPeter Avalos }
8761133e27eSPeter Avalos 
8771133e27eSPeter Avalos /*
8781133e27eSPeter Avalos  * Append a character to the line buffer.
8791133e27eSPeter Avalos  * Expand tabs into spaces, handle underlining, boldfacing, etc.
8801133e27eSPeter Avalos  * Returns 0 if ok, 1 if couldn't fit in buffer.
8811133e27eSPeter Avalos  */
pappend(int c,POSITION pos)882*320d7c8aSAaron LI public int pappend(int c, POSITION pos)
8831133e27eSPeter Avalos {
8841133e27eSPeter Avalos 	int r;
8851133e27eSPeter Avalos 
8861133e27eSPeter Avalos 	if (pendc)
8871133e27eSPeter Avalos 	{
888fa0be7c5SJohn Marino 		if (c == '\r' && pendc == '\r')
889fa0be7c5SJohn Marino 			return (0);
8901133e27eSPeter Avalos 		if (do_append(pendc, NULL, pendpos))
8911133e27eSPeter Avalos 			/*
8921133e27eSPeter Avalos 			 * Oops.  We've probably lost the char which
8931133e27eSPeter Avalos 			 * was in pendc, since caller won't back up.
8941133e27eSPeter Avalos 			 */
8951133e27eSPeter Avalos 			return (1);
8961133e27eSPeter Avalos 		pendc = '\0';
8971133e27eSPeter Avalos 	}
8981133e27eSPeter Avalos 
899*320d7c8aSAaron LI 	if (c == '\r' && (proc_return == OPT_ON || (bs_mode == BS_SPECIAL && proc_return == OPT_OFF)))
9001133e27eSPeter Avalos 	{
9011133e27eSPeter Avalos 		if (mbc_buf_len > 0)  /* utf_mode must be on. */
9021133e27eSPeter Avalos 		{
9031133e27eSPeter Avalos 			/* Flush incomplete (truncated) sequence. */
9041133e27eSPeter Avalos 			r = flush_mbc_buf(mbc_pos);
9051133e27eSPeter Avalos 			mbc_buf_index = r + 1;
9061133e27eSPeter Avalos 			mbc_buf_len = 0;
9071133e27eSPeter Avalos 			if (r)
9081133e27eSPeter Avalos 				return (mbc_buf_index);
9091133e27eSPeter Avalos 		}
9101133e27eSPeter Avalos 
9111133e27eSPeter Avalos 		/*
9121133e27eSPeter Avalos 		 * Don't put the CR into the buffer until we see
9131133e27eSPeter Avalos 		 * the next char.  If the next char is a newline,
9141133e27eSPeter Avalos 		 * discard the CR.
9151133e27eSPeter Avalos 		 */
9161133e27eSPeter Avalos 		pendc = c;
9171133e27eSPeter Avalos 		pendpos = pos;
9181133e27eSPeter Avalos 		return (0);
9191133e27eSPeter Avalos 	}
9201133e27eSPeter Avalos 
9211133e27eSPeter Avalos 	if (!utf_mode)
9221133e27eSPeter Avalos 	{
923fa0be7c5SJohn Marino 		r = do_append(c, NULL, pos);
9241133e27eSPeter Avalos 	} else
9251133e27eSPeter Avalos 	{
9261133e27eSPeter Avalos 		/* Perform strict validation in all possible cases. */
9271133e27eSPeter Avalos 		if (mbc_buf_len == 0)
9281133e27eSPeter Avalos 		{
9291133e27eSPeter Avalos 		retry:
9301133e27eSPeter Avalos 			mbc_buf_index = 1;
9311133e27eSPeter Avalos 			*mbc_buf = c;
9321133e27eSPeter Avalos 			if (IS_ASCII_OCTET(c))
933fa0be7c5SJohn Marino 				r = do_append(c, NULL, pos);
9341133e27eSPeter Avalos 			else if (IS_UTF8_LEAD(c))
9351133e27eSPeter Avalos 			{
9361133e27eSPeter Avalos 				mbc_buf_len = utf_len(c);
9371133e27eSPeter Avalos 				mbc_pos = pos;
9381133e27eSPeter Avalos 				return (0);
9391133e27eSPeter Avalos 			} else
9401133e27eSPeter Avalos 				/* UTF8_INVALID or stray UTF8_TRAIL */
9411133e27eSPeter Avalos 				r = flush_mbc_buf(pos);
9421133e27eSPeter Avalos 		} else if (IS_UTF8_TRAIL(c))
9431133e27eSPeter Avalos 		{
9441133e27eSPeter Avalos 			mbc_buf[mbc_buf_index++] = c;
9451133e27eSPeter Avalos 			if (mbc_buf_index < mbc_buf_len)
9461133e27eSPeter Avalos 				return (0);
9479b760066SJohn Marino 			if (is_utf8_well_formed(mbc_buf, mbc_buf_index))
9481133e27eSPeter Avalos 				r = do_append(get_wchar(mbc_buf), mbc_buf, mbc_pos);
9491133e27eSPeter Avalos 			else
9501133e27eSPeter Avalos 				/* Complete, but not shortest form, sequence. */
9511133e27eSPeter Avalos 				mbc_buf_index = r = flush_mbc_buf(mbc_pos);
9521133e27eSPeter Avalos 			mbc_buf_len = 0;
9531133e27eSPeter Avalos 		} else
9541133e27eSPeter Avalos 		{
9551133e27eSPeter Avalos 			/* Flush incomplete (truncated) sequence.  */
9561133e27eSPeter Avalos 			r = flush_mbc_buf(mbc_pos);
9571133e27eSPeter Avalos 			mbc_buf_index = r + 1;
9581133e27eSPeter Avalos 			mbc_buf_len = 0;
9591133e27eSPeter Avalos 			/* Handle new char.  */
9601133e27eSPeter Avalos 			if (!r)
9611133e27eSPeter Avalos 				goto retry;
9621133e27eSPeter Avalos 		}
9631133e27eSPeter Avalos 	}
9641133e27eSPeter Avalos 	if (r)
9651133e27eSPeter Avalos 	{
9661133e27eSPeter Avalos 		/* How many chars should caller back up? */
9671133e27eSPeter Avalos 		r = (!utf_mode) ? 1 : mbc_buf_index;
9681133e27eSPeter Avalos 	}
9691133e27eSPeter Avalos 	return (r);
9701133e27eSPeter Avalos }
9711133e27eSPeter Avalos 
store_control_char(LWCHAR ch,char * rep,POSITION pos)972*320d7c8aSAaron LI static int store_control_char(LWCHAR ch, char *rep, POSITION pos)
9731133e27eSPeter Avalos {
9740c7ad07eSAntonio Huete Jimenez 	if (ctldisp == OPT_ON)
9750c7ad07eSAntonio Huete Jimenez 	{
9760c7ad07eSAntonio Huete Jimenez 		/* Output the character itself. */
9770c7ad07eSAntonio Huete Jimenez 		STORE_CHAR(ch, AT_NORMAL, rep, pos);
9780c7ad07eSAntonio Huete Jimenez 	} else
9790c7ad07eSAntonio Huete Jimenez 	{
9800c7ad07eSAntonio Huete Jimenez 		/* Output a printable representation of the character. */
9810c7ad07eSAntonio Huete Jimenez 		STORE_PRCHAR((char) ch, pos);
9820c7ad07eSAntonio Huete Jimenez 	}
9830c7ad07eSAntonio Huete Jimenez 	return (0);
9840c7ad07eSAntonio Huete Jimenez }
9851133e27eSPeter Avalos 
store_ansi(LWCHAR ch,char * rep,POSITION pos)986*320d7c8aSAaron LI static int store_ansi(LWCHAR ch, char *rep, POSITION pos)
9870c7ad07eSAntonio Huete Jimenez {
9880c7ad07eSAntonio Huete Jimenez 	switch (ansi_step(line_ansi, ch))
9890c7ad07eSAntonio Huete Jimenez 	{
9900c7ad07eSAntonio Huete Jimenez 	case ANSI_MID:
9910c7ad07eSAntonio Huete Jimenez 		STORE_CHAR(ch, AT_ANSI, rep, pos);
9920c7ad07eSAntonio Huete Jimenez 		if (line_ansi->hlink)
9930c7ad07eSAntonio Huete Jimenez 			hlink_in_line = 1;
994*320d7c8aSAaron LI 		xbuf_add_byte(&last_ansi, (unsigned char) ch);
9950c7ad07eSAntonio Huete Jimenez 		break;
9960c7ad07eSAntonio Huete Jimenez 	case ANSI_END:
9970c7ad07eSAntonio Huete Jimenez 		STORE_CHAR(ch, AT_ANSI, rep, pos);
9980c7ad07eSAntonio Huete Jimenez 		ansi_done(line_ansi);
9990c7ad07eSAntonio Huete Jimenez 		line_ansi = NULL;
1000*320d7c8aSAaron LI 		xbuf_add_byte(&last_ansi, (unsigned char) ch);
10010c7ad07eSAntonio Huete Jimenez 		xbuf_set(&last_ansis[curr_last_ansi], &last_ansi);
10020c7ad07eSAntonio Huete Jimenez 		xbuf_reset(&last_ansi);
10030c7ad07eSAntonio Huete Jimenez 		curr_last_ansi = (curr_last_ansi + 1) % NUM_LAST_ANSIS;
10040c7ad07eSAntonio Huete Jimenez 		break;
10050c7ad07eSAntonio Huete Jimenez 	case ANSI_ERR:
10060c7ad07eSAntonio Huete Jimenez 		{
10070c7ad07eSAntonio Huete Jimenez 			/* Remove whole unrecognized sequence.  */
1008*320d7c8aSAaron LI 			char *start = (cshift < hshift) ? xbuf_char_data(&shifted_ansi): linebuf.buf;
10090c7ad07eSAntonio Huete Jimenez 			int *end = (cshift < hshift) ? &shifted_ansi.end : &linebuf.end;
10100c7ad07eSAntonio Huete Jimenez 			char *p = start + *end;
10110c7ad07eSAntonio Huete Jimenez 			LWCHAR bch;
10120c7ad07eSAntonio Huete Jimenez 			do {
10130c7ad07eSAntonio Huete Jimenez 				bch = step_char(&p, -1, start);
10140c7ad07eSAntonio Huete Jimenez 			} while (p > start && !IS_CSI_START(bch));
10150c7ad07eSAntonio Huete Jimenez 			*end = (int) (p - start);
10160c7ad07eSAntonio Huete Jimenez 		}
10170c7ad07eSAntonio Huete Jimenez 		xbuf_reset(&last_ansi);
10180c7ad07eSAntonio Huete Jimenez 		ansi_done(line_ansi);
10190c7ad07eSAntonio Huete Jimenez 		line_ansi = NULL;
10200c7ad07eSAntonio Huete Jimenez 		break;
10210c7ad07eSAntonio Huete Jimenez 	}
10220c7ad07eSAntonio Huete Jimenez 	return (0);
10230c7ad07eSAntonio Huete Jimenez }
10241133e27eSPeter Avalos 
store_bs(LWCHAR ch,char * rep,POSITION pos)1025*320d7c8aSAaron LI static int store_bs(LWCHAR ch, char *rep, POSITION pos)
10261133e27eSPeter Avalos {
1027*320d7c8aSAaron LI 	if (proc_backspace == OPT_ONPLUS || (bs_mode == BS_CONTROL && proc_backspace == OPT_OFF))
10280c7ad07eSAntonio Huete Jimenez 		return store_control_char(ch, rep, pos);
10290c7ad07eSAntonio Huete Jimenez 	if (linebuf.end > 0 &&
10300c7ad07eSAntonio Huete Jimenez 		((linebuf.end <= linebuf.print && linebuf.buf[linebuf.end-1] == '\0') ||
10310c7ad07eSAntonio Huete Jimenez 	     (linebuf.end > 0 && linebuf.attr[linebuf.end - 1] & (AT_ANSI|AT_BINARY))))
10321133e27eSPeter Avalos 		STORE_PRCHAR('\b', pos);
1033*320d7c8aSAaron LI 	else if (proc_backspace == OPT_OFF && bs_mode == BS_NORMAL)
10341133e27eSPeter Avalos 		STORE_CHAR(ch, AT_NORMAL, NULL, pos);
1035*320d7c8aSAaron LI 	else if (proc_backspace == OPT_ON || (bs_mode == BS_SPECIAL && proc_backspace == OPT_OFF))
10361133e27eSPeter Avalos 		overstrike = backc();
10371133e27eSPeter Avalos 	return 0;
10381133e27eSPeter Avalos }
10391133e27eSPeter Avalos 
do_append(LWCHAR ch,char * rep,POSITION pos)1040*320d7c8aSAaron LI static int do_append(LWCHAR ch, char *rep, POSITION pos)
10410c7ad07eSAntonio Huete Jimenez {
10420c7ad07eSAntonio Huete Jimenez 	int a = AT_NORMAL;
1043*320d7c8aSAaron LI 	int in_overstrike = overstrike;
10440c7ad07eSAntonio Huete Jimenez 
10450c7ad07eSAntonio Huete Jimenez 	if (ctldisp == OPT_ONPLUS && line_ansi == NULL)
10460c7ad07eSAntonio Huete Jimenez 	{
10470c7ad07eSAntonio Huete Jimenez 		line_ansi = ansi_start(ch);
10480c7ad07eSAntonio Huete Jimenez 		if (line_ansi != NULL)
10490c7ad07eSAntonio Huete Jimenez 			ansi_in_line = 1;
10500c7ad07eSAntonio Huete Jimenez 	}
10510c7ad07eSAntonio Huete Jimenez 
1052*320d7c8aSAaron LI 	overstrike = 0;
10530c7ad07eSAntonio Huete Jimenez 	if (line_ansi != NULL)
10540c7ad07eSAntonio Huete Jimenez 		return store_ansi(ch, rep, pos);
10550c7ad07eSAntonio Huete Jimenez 
10560c7ad07eSAntonio Huete Jimenez 	if (ch == '\b')
10570c7ad07eSAntonio Huete Jimenez 		return store_bs(ch, rep, pos);
10580c7ad07eSAntonio Huete Jimenez 
1059*320d7c8aSAaron LI 	if (in_overstrike > 0)
10601133e27eSPeter Avalos 	{
10611133e27eSPeter Avalos 		/*
10621133e27eSPeter Avalos 		 * Overstrike the character at the current position
10631133e27eSPeter Avalos 		 * in the line buffer.  This will cause either
10641133e27eSPeter Avalos 		 * underline (if a "_" is overstruck),
10651133e27eSPeter Avalos 		 * bold (if an identical character is overstruck),
10660c7ad07eSAntonio Huete Jimenez 		 * or just replacing the character in the buffer.
10671133e27eSPeter Avalos 		 */
10680c7ad07eSAntonio Huete Jimenez 		LWCHAR prev_ch;
10691133e27eSPeter Avalos 		overstrike = utf_mode ? -1 : 0;
1070fa0be7c5SJohn Marino 		if (utf_mode)
1071fa0be7c5SJohn Marino 		{
10721133e27eSPeter Avalos 			/* To be correct, this must be a base character.  */
10730c7ad07eSAntonio Huete Jimenez 			prev_ch = get_wchar(&linebuf.buf[linebuf.end]);
1074fa0be7c5SJohn Marino 		} else
1075fa0be7c5SJohn Marino 		{
10760c7ad07eSAntonio Huete Jimenez 			prev_ch = (unsigned char) linebuf.buf[linebuf.end];
1077fa0be7c5SJohn Marino 		}
10780c7ad07eSAntonio Huete Jimenez 		a = linebuf.attr[linebuf.end];
10791133e27eSPeter Avalos 		if (ch == prev_ch)
10801133e27eSPeter Avalos 		{
10811133e27eSPeter Avalos 			/*
10821133e27eSPeter Avalos 			 * Overstriking a char with itself means make it bold.
10831133e27eSPeter Avalos 			 * But overstriking an underscore with itself is
10841133e27eSPeter Avalos 			 * ambiguous.  It could mean make it bold, or
10851133e27eSPeter Avalos 			 * it could mean make it underlined.
10861133e27eSPeter Avalos 			 * Use the previous overstrike to resolve it.
10871133e27eSPeter Avalos 			 */
10881133e27eSPeter Avalos 			if (ch == '_')
10891133e27eSPeter Avalos 			{
10901133e27eSPeter Avalos 				if ((a & (AT_BOLD|AT_UNDERLINE)) != AT_NORMAL)
10911133e27eSPeter Avalos 					a |= (AT_BOLD|AT_UNDERLINE);
10921133e27eSPeter Avalos 				else if (last_overstrike != AT_NORMAL)
10931133e27eSPeter Avalos 					a |= last_overstrike;
10941133e27eSPeter Avalos 				else
10951133e27eSPeter Avalos 					a |= AT_BOLD;
10961133e27eSPeter Avalos 			} else
10971133e27eSPeter Avalos 				a |= AT_BOLD;
10981133e27eSPeter Avalos 		} else if (ch == '_')
10991133e27eSPeter Avalos 		{
11001133e27eSPeter Avalos 			a |= AT_UNDERLINE;
11011133e27eSPeter Avalos 			ch = prev_ch;
11020c7ad07eSAntonio Huete Jimenez 			rep = &linebuf.buf[linebuf.end];
11031133e27eSPeter Avalos 		} else if (prev_ch == '_')
11041133e27eSPeter Avalos 		{
11051133e27eSPeter Avalos 			a |= AT_UNDERLINE;
11061133e27eSPeter Avalos 		}
11071133e27eSPeter Avalos 		/* Else we replace prev_ch, but we keep its attributes.  */
1108*320d7c8aSAaron LI 	} else if (in_overstrike < 0)
11091133e27eSPeter Avalos 	{
11101133e27eSPeter Avalos 		if (   is_composing_char(ch)
11110c7ad07eSAntonio Huete Jimenez 		    || is_combining_char(get_wchar(&linebuf.buf[linebuf.end]), ch))
11121133e27eSPeter Avalos 			/* Continuation of the same overstrike.  */
11131133e27eSPeter Avalos 			a = last_overstrike;
11141133e27eSPeter Avalos 		else
11151133e27eSPeter Avalos 			overstrike = 0;
11161133e27eSPeter Avalos 	}
11171133e27eSPeter Avalos 
11181133e27eSPeter Avalos 	if (ch == '\t')
11191133e27eSPeter Avalos 	{
11201133e27eSPeter Avalos 		/*
11211133e27eSPeter Avalos 		 * Expand a tab into spaces.
11221133e27eSPeter Avalos 		 */
1123*320d7c8aSAaron LI 		if (proc_tab == OPT_ONPLUS || (bs_mode == BS_CONTROL && proc_tab == OPT_OFF))
11240c7ad07eSAntonio Huete Jimenez 			return store_control_char(ch, rep, pos);
11251133e27eSPeter Avalos 		STORE_TAB(a, pos);
11260c7ad07eSAntonio Huete Jimenez 		return (0);
11271133e27eSPeter Avalos 	}
11280c7ad07eSAntonio Huete Jimenez 	if ((!utf_mode || is_ascii_char(ch)) && control_char((char)ch))
11290c7ad07eSAntonio Huete Jimenez 	{
11300c7ad07eSAntonio Huete Jimenez 		return store_control_char(ch, rep, pos);
11311133e27eSPeter Avalos 	} else if (utf_mode && ctldisp != OPT_ON && is_ubin_char(ch))
11321133e27eSPeter Avalos 	{
11330c7ad07eSAntonio Huete Jimenez 		STORE_STRING(prutfchar(ch), AT_BINARY, pos);
11341133e27eSPeter Avalos 	} else
11351133e27eSPeter Avalos 	{
11361133e27eSPeter Avalos 		STORE_CHAR(ch, a, rep, pos);
11371133e27eSPeter Avalos 	}
11381133e27eSPeter Avalos 	return (0);
11391133e27eSPeter Avalos }
11401133e27eSPeter Avalos 
11411133e27eSPeter Avalos /*
11421133e27eSPeter Avalos  *
11431133e27eSPeter Avalos  */
pflushmbc(void)1144*320d7c8aSAaron LI public int pflushmbc(void)
11451133e27eSPeter Avalos {
11461133e27eSPeter Avalos 	int r = 0;
11471133e27eSPeter Avalos 
11481133e27eSPeter Avalos 	if (mbc_buf_len > 0)
11491133e27eSPeter Avalos 	{
11501133e27eSPeter Avalos 		/* Flush incomplete (truncated) sequence.  */
11511133e27eSPeter Avalos 		r = flush_mbc_buf(mbc_pos);
11521133e27eSPeter Avalos 		mbc_buf_len = 0;
11531133e27eSPeter Avalos 	}
11541133e27eSPeter Avalos 	return r;
11551133e27eSPeter Avalos }
11561133e27eSPeter Avalos 
11571133e27eSPeter Avalos /*
115802d62a0fSDaniel Fojt  * Switch to normal attribute at end of line.
115902d62a0fSDaniel Fojt  */
add_attr_normal(void)1160*320d7c8aSAaron LI static void add_attr_normal(void)
116102d62a0fSDaniel Fojt {
116202d62a0fSDaniel Fojt 	if (ctldisp != OPT_ONPLUS || !is_ansi_end('m'))
116302d62a0fSDaniel Fojt 		return;
11640c7ad07eSAntonio Huete Jimenez 	addstr_linebuf("\033[m", AT_ANSI, 0);
11650c7ad07eSAntonio Huete Jimenez 	if (hlink_in_line) /* Don't send hyperlink clear if we know we don't need to. */
11660c7ad07eSAntonio Huete Jimenez 		addstr_linebuf("\033]8;;\033\\", AT_ANSI, 0);
116702d62a0fSDaniel Fojt }
116802d62a0fSDaniel Fojt 
116902d62a0fSDaniel Fojt /*
11701133e27eSPeter Avalos  * Terminate the line in the line buffer.
11711133e27eSPeter Avalos  */
pdone(int endline,int chopped,int forw)1172*320d7c8aSAaron LI public void pdone(int endline, int chopped, int forw)
11731133e27eSPeter Avalos {
11741133e27eSPeter Avalos 	(void) pflushmbc();
11751133e27eSPeter Avalos 
11761133e27eSPeter Avalos 	if (pendc && (pendc != '\r' || !endline))
11771133e27eSPeter Avalos 		/*
11781133e27eSPeter Avalos 		 * If we had a pending character, put it in the buffer.
11791133e27eSPeter Avalos 		 * But discard a pending CR if we are at end of line
11801133e27eSPeter Avalos 		 * (that is, discard the CR in a CR/LF sequence).
11811133e27eSPeter Avalos 		 */
11821133e27eSPeter Avalos 		(void) do_append(pendc, NULL, pendpos);
11831133e27eSPeter Avalos 
118402d62a0fSDaniel Fojt 	if (chopped && rscroll_char)
11851133e27eSPeter Avalos 	{
118602d62a0fSDaniel Fojt 		/*
118702d62a0fSDaniel Fojt 		 * Display the right scrolling char.
118802d62a0fSDaniel Fojt 		 * If we've already filled the rightmost screen char
118902d62a0fSDaniel Fojt 		 * (in the buffer), overwrite it.
119002d62a0fSDaniel Fojt 		 */
11910c7ad07eSAntonio Huete Jimenez 		if (end_column >= sc_width + cshift)
11921133e27eSPeter Avalos 		{
119302d62a0fSDaniel Fojt 			/* We've already written in the rightmost char. */
11940c7ad07eSAntonio Huete Jimenez 			end_column = right_column;
11950c7ad07eSAntonio Huete Jimenez 			linebuf.end = right_curr;
11961133e27eSPeter Avalos 		}
119702d62a0fSDaniel Fojt 		add_attr_normal();
11980c7ad07eSAntonio Huete Jimenez 		while (end_column < sc_width-1 + cshift)
119902d62a0fSDaniel Fojt 		{
120002d62a0fSDaniel Fojt 			/*
120102d62a0fSDaniel Fojt 			 * Space to last (rightmost) char on screen.
120202d62a0fSDaniel Fojt 			 * This may be necessary if the char we overwrote
120302d62a0fSDaniel Fojt 			 * was double-width.
120402d62a0fSDaniel Fojt 			 */
12050c7ad07eSAntonio Huete Jimenez 			add_linebuf(' ', rscroll_attr, 1);
120602d62a0fSDaniel Fojt 		}
120702d62a0fSDaniel Fojt 		/* Print rscroll char. It must be single-width. */
120802d62a0fSDaniel Fojt 		add_linebuf(rscroll_char, rscroll_attr, 1);
120902d62a0fSDaniel Fojt 	} else
121002d62a0fSDaniel Fojt 	{
121102d62a0fSDaniel Fojt 		add_attr_normal();
12121133e27eSPeter Avalos 	}
12131133e27eSPeter Avalos 
12141133e27eSPeter Avalos 	/*
12150c7ad07eSAntonio Huete Jimenez 	 * If we're coloring a status line, fill out the line with spaces.
12160c7ad07eSAntonio Huete Jimenez 	 */
12170c7ad07eSAntonio Huete Jimenez 	if (status_line && line_mark_attr != 0) {
12180c7ad07eSAntonio Huete Jimenez 		while (end_column +1 < sc_width + cshift)
12190c7ad07eSAntonio Huete Jimenez 			add_linebuf(' ', line_mark_attr, 1);
12200c7ad07eSAntonio Huete Jimenez 	}
12210c7ad07eSAntonio Huete Jimenez 
12220c7ad07eSAntonio Huete Jimenez 	/*
12231133e27eSPeter Avalos 	 * Add a newline if necessary,
12241133e27eSPeter Avalos 	 * and append a '\0' to the end of the line.
12251133e27eSPeter Avalos 	 * We output a newline if we're not at the right edge of the screen,
12261133e27eSPeter Avalos 	 * or if the terminal doesn't auto wrap,
12271133e27eSPeter Avalos 	 * or if this is really the end of the line AND the terminal ignores
12281133e27eSPeter Avalos 	 * a newline at the right edge.
12291133e27eSPeter Avalos 	 * (In the last case we don't want to output a newline if the terminal
12301133e27eSPeter Avalos 	 * doesn't ignore it since that would produce an extra blank line.
12311133e27eSPeter Avalos 	 * But we do want to output a newline if the terminal ignores it in case
12321133e27eSPeter Avalos 	 * the next line is blank.  In that case the single newline output for
12331133e27eSPeter Avalos 	 * that blank line would be ignored!)
12341133e27eSPeter Avalos 	 */
12350c7ad07eSAntonio Huete Jimenez 	if (end_column < sc_width + cshift || !auto_wrap || (endline && ignaw) || ctldisp == OPT_ON)
12361133e27eSPeter Avalos 	{
123702d62a0fSDaniel Fojt 		add_linebuf('\n', AT_NORMAL, 0);
12381133e27eSPeter Avalos 	}
12390c7ad07eSAntonio Huete Jimenez 	else if (ignaw && end_column >= sc_width + cshift && forw)
12401133e27eSPeter Avalos 	{
12411133e27eSPeter Avalos 		/*
12428be36e5bSPeter Avalos 		 * Terminals with "ignaw" don't wrap until they *really* need
12438be36e5bSPeter Avalos 		 * to, i.e. when the character *after* the last one to fit on a
12448be36e5bSPeter Avalos 		 * line is output. But they are too hard to deal with when they
12458be36e5bSPeter Avalos 		 * get in the state where a full screen width of characters
12468be36e5bSPeter Avalos 		 * have been output but the cursor is sitting on the right edge
12478be36e5bSPeter Avalos 		 * instead of at the start of the next line.
1248a9adbba3SJan Lentfer 		 * So we nudge them into wrapping by outputting a space
1249a9adbba3SJan Lentfer 		 * character plus a backspace.  But do this only if moving
1250a9adbba3SJan Lentfer 		 * forward; if we're moving backward and drawing this line at
1251a9adbba3SJan Lentfer 		 * the top of the screen, the space would overwrite the first
1252a9adbba3SJan Lentfer 		 * char on the next line.  We don't need to do this "nudge"
1253a9adbba3SJan Lentfer 		 * at the top of the screen anyway.
12541133e27eSPeter Avalos 		 */
125502d62a0fSDaniel Fojt 		add_linebuf(' ', AT_NORMAL, 1);
125602d62a0fSDaniel Fojt 		add_linebuf('\b', AT_NORMAL, -1);
12571133e27eSPeter Avalos 	}
12580c7ad07eSAntonio Huete Jimenez 	set_linebuf(linebuf.end, '\0', AT_NORMAL);
12591133e27eSPeter Avalos }
12608be36e5bSPeter Avalos 
12618be36e5bSPeter Avalos /*
12620c7ad07eSAntonio Huete Jimenez  * Set an attribute on each char of the line in the line buffer.
12638be36e5bSPeter Avalos  */
set_attr_line(int a)1264*320d7c8aSAaron LI public void set_attr_line(int a)
12658be36e5bSPeter Avalos {
12660c7ad07eSAntonio Huete Jimenez 	int i;
12670c7ad07eSAntonio Huete Jimenez 
12680c7ad07eSAntonio Huete Jimenez 	for (i = linebuf.print;  i < linebuf.end;  i++)
1269*320d7c8aSAaron LI 		if ((linebuf.attr[i] & AT_COLOR) == 0 || (a & AT_COLOR) == 0)
12700c7ad07eSAntonio Huete Jimenez 			linebuf.attr[i] |= a;
12710c7ad07eSAntonio Huete Jimenez }
12720c7ad07eSAntonio Huete Jimenez 
12730c7ad07eSAntonio Huete Jimenez /*
12740c7ad07eSAntonio Huete Jimenez  * Set the char to be displayed in the status column.
12750c7ad07eSAntonio Huete Jimenez  */
set_status_col(char c,int attr)1276*320d7c8aSAaron LI public void set_status_col(char c, int attr)
12770c7ad07eSAntonio Huete Jimenez {
12780c7ad07eSAntonio Huete Jimenez 	set_pfx(0, c, attr);
12791133e27eSPeter Avalos }
12801133e27eSPeter Avalos 
12811133e27eSPeter Avalos /*
12821133e27eSPeter Avalos  * Get a character from the current line.
12831133e27eSPeter Avalos  * Return the character as the function return value,
12841133e27eSPeter Avalos  * and the character attribute in *ap.
12851133e27eSPeter Avalos  */
gline(int i,int * ap)1286*320d7c8aSAaron LI public int gline(int i, int *ap)
12871133e27eSPeter Avalos {
12881133e27eSPeter Avalos 	if (is_null_line)
12891133e27eSPeter Avalos 	{
12901133e27eSPeter Avalos 		/*
12911133e27eSPeter Avalos 		 * If there is no current line, we pretend the line is
12921133e27eSPeter Avalos 		 * either "~" or "", depending on the "twiddle" flag.
12931133e27eSPeter Avalos 		 */
12941133e27eSPeter Avalos 		if (twiddle)
12951133e27eSPeter Avalos 		{
12961133e27eSPeter Avalos 			if (i == 0)
12971133e27eSPeter Avalos 			{
12981133e27eSPeter Avalos 				*ap = AT_BOLD;
12991133e27eSPeter Avalos 				return '~';
13001133e27eSPeter Avalos 			}
13011133e27eSPeter Avalos 			--i;
13021133e27eSPeter Avalos 		}
13031133e27eSPeter Avalos 		/* Make sure we're back to AT_NORMAL before the '\n'.  */
13041133e27eSPeter Avalos 		*ap = AT_NORMAL;
13051133e27eSPeter Avalos 		return i ? '\0' : '\n';
13061133e27eSPeter Avalos 	}
13071133e27eSPeter Avalos 
13080c7ad07eSAntonio Huete Jimenez 	if (i < linebuf.pfx_end)
13090c7ad07eSAntonio Huete Jimenez 	{
13100c7ad07eSAntonio Huete Jimenez 		*ap = linebuf.pfx_attr[i];
13110c7ad07eSAntonio Huete Jimenez 		return linebuf.pfx[i];
13120c7ad07eSAntonio Huete Jimenez 	}
13130c7ad07eSAntonio Huete Jimenez 	i += linebuf.print - linebuf.pfx_end;
13140c7ad07eSAntonio Huete Jimenez 	*ap = linebuf.attr[i];
13150c7ad07eSAntonio Huete Jimenez 	return (linebuf.buf[i] & 0xFF);
13161133e27eSPeter Avalos }
13171133e27eSPeter Avalos 
13181133e27eSPeter Avalos /*
13191133e27eSPeter Avalos  * Indicate that there is no current line.
13201133e27eSPeter Avalos  */
null_line(void)1321*320d7c8aSAaron LI public void null_line(void)
13221133e27eSPeter Avalos {
13231133e27eSPeter Avalos 	is_null_line = 1;
13241133e27eSPeter Avalos 	cshift = 0;
13251133e27eSPeter Avalos }
13261133e27eSPeter Avalos 
13271133e27eSPeter Avalos /*
13281133e27eSPeter Avalos  * Analogous to forw_line(), but deals with "raw lines":
13291133e27eSPeter Avalos  * lines which are not split for screen width.
13301133e27eSPeter Avalos  * {{ This is supposed to be more efficient than forw_line(). }}
13311133e27eSPeter Avalos  */
forw_raw_line(POSITION curr_pos,char ** linep,int * line_lenp)1332*320d7c8aSAaron LI public POSITION forw_raw_line(POSITION curr_pos, char **linep, int *line_lenp)
13331133e27eSPeter Avalos {
133402d62a0fSDaniel Fojt 	int n;
133502d62a0fSDaniel Fojt 	int c;
13361133e27eSPeter Avalos 	POSITION new_pos;
13371133e27eSPeter Avalos 
13381133e27eSPeter Avalos 	if (curr_pos == NULL_POSITION || ch_seek(curr_pos) ||
13391133e27eSPeter Avalos 		(c = ch_forw_get()) == EOI)
13401133e27eSPeter Avalos 		return (NULL_POSITION);
13411133e27eSPeter Avalos 
13421133e27eSPeter Avalos 	n = 0;
13431133e27eSPeter Avalos 	for (;;)
13441133e27eSPeter Avalos 	{
13451133e27eSPeter Avalos 		if (c == '\n' || c == EOI || ABORT_SIGS())
13461133e27eSPeter Avalos 		{
13471133e27eSPeter Avalos 			new_pos = ch_tell();
13481133e27eSPeter Avalos 			break;
13491133e27eSPeter Avalos 		}
13501133e27eSPeter Avalos 		if (n >= size_linebuf-1)
13511133e27eSPeter Avalos 		{
13521133e27eSPeter Avalos 			if (expand_linebuf())
13531133e27eSPeter Avalos 			{
13541133e27eSPeter Avalos 				/*
13551133e27eSPeter Avalos 				 * Overflowed the input buffer.
13561133e27eSPeter Avalos 				 * Pretend the line ended here.
13571133e27eSPeter Avalos 				 */
13581133e27eSPeter Avalos 				new_pos = ch_tell() - 1;
13591133e27eSPeter Avalos 				break;
13601133e27eSPeter Avalos 			}
13611133e27eSPeter Avalos 		}
13620c7ad07eSAntonio Huete Jimenez 		linebuf.buf[n++] = c;
13631133e27eSPeter Avalos 		c = ch_forw_get();
13641133e27eSPeter Avalos 	}
13650c7ad07eSAntonio Huete Jimenez 	linebuf.buf[n] = '\0';
13661133e27eSPeter Avalos 	if (linep != NULL)
13670c7ad07eSAntonio Huete Jimenez 		*linep = linebuf.buf;
13681133e27eSPeter Avalos 	if (line_lenp != NULL)
13691133e27eSPeter Avalos 		*line_lenp = n;
13701133e27eSPeter Avalos 	return (new_pos);
13711133e27eSPeter Avalos }
13721133e27eSPeter Avalos 
13731133e27eSPeter Avalos /*
13741133e27eSPeter Avalos  * Analogous to back_line(), but deals with "raw lines".
13751133e27eSPeter Avalos  * {{ This is supposed to be more efficient than back_line(). }}
13761133e27eSPeter Avalos  */
back_raw_line(POSITION curr_pos,char ** linep,int * line_lenp)1377*320d7c8aSAaron LI public POSITION back_raw_line(POSITION curr_pos, char **linep, int *line_lenp)
13781133e27eSPeter Avalos {
137902d62a0fSDaniel Fojt 	int n;
138002d62a0fSDaniel Fojt 	int c;
13811133e27eSPeter Avalos 	POSITION new_pos;
13821133e27eSPeter Avalos 
13831133e27eSPeter Avalos 	if (curr_pos == NULL_POSITION || curr_pos <= ch_zero() ||
13841133e27eSPeter Avalos 		ch_seek(curr_pos-1))
13851133e27eSPeter Avalos 		return (NULL_POSITION);
13861133e27eSPeter Avalos 
13871133e27eSPeter Avalos 	n = size_linebuf;
13880c7ad07eSAntonio Huete Jimenez 	linebuf.buf[--n] = '\0';
13891133e27eSPeter Avalos 	for (;;)
13901133e27eSPeter Avalos 	{
13911133e27eSPeter Avalos 		c = ch_back_get();
13921133e27eSPeter Avalos 		if (c == '\n' || ABORT_SIGS())
13931133e27eSPeter Avalos 		{
13941133e27eSPeter Avalos 			/*
13951133e27eSPeter Avalos 			 * This is the newline ending the previous line.
13961133e27eSPeter Avalos 			 * We have hit the beginning of the line.
13971133e27eSPeter Avalos 			 */
13981133e27eSPeter Avalos 			new_pos = ch_tell() + 1;
13991133e27eSPeter Avalos 			break;
14001133e27eSPeter Avalos 		}
14011133e27eSPeter Avalos 		if (c == EOI)
14021133e27eSPeter Avalos 		{
14031133e27eSPeter Avalos 			/*
14041133e27eSPeter Avalos 			 * We have hit the beginning of the file.
14051133e27eSPeter Avalos 			 * This must be the first line in the file.
14061133e27eSPeter Avalos 			 * This must, of course, be the beginning of the line.
14071133e27eSPeter Avalos 			 */
14081133e27eSPeter Avalos 			new_pos = ch_zero();
14091133e27eSPeter Avalos 			break;
14101133e27eSPeter Avalos 		}
14111133e27eSPeter Avalos 		if (n <= 0)
14121133e27eSPeter Avalos 		{
14131133e27eSPeter Avalos 			int old_size_linebuf = size_linebuf;
14141133e27eSPeter Avalos 			char *fm;
14151133e27eSPeter Avalos 			char *to;
14161133e27eSPeter Avalos 			if (expand_linebuf())
14171133e27eSPeter Avalos 			{
14181133e27eSPeter Avalos 				/*
14191133e27eSPeter Avalos 				 * Overflowed the input buffer.
14201133e27eSPeter Avalos 				 * Pretend the line ended here.
14211133e27eSPeter Avalos 				 */
14221133e27eSPeter Avalos 				new_pos = ch_tell() + 1;
14231133e27eSPeter Avalos 				break;
14241133e27eSPeter Avalos 			}
14251133e27eSPeter Avalos 			/*
14261133e27eSPeter Avalos 			 * Shift the data to the end of the new linebuf.
14271133e27eSPeter Avalos 			 */
14280c7ad07eSAntonio Huete Jimenez 			for (fm = linebuf.buf + old_size_linebuf - 1,
14290c7ad07eSAntonio Huete Jimenez 			      to = linebuf.buf + size_linebuf - 1;
14300c7ad07eSAntonio Huete Jimenez 			     fm >= linebuf.buf;  fm--, to--)
14311133e27eSPeter Avalos 				*to = *fm;
14321133e27eSPeter Avalos 			n = size_linebuf - old_size_linebuf;
14331133e27eSPeter Avalos 		}
14340c7ad07eSAntonio Huete Jimenez 		linebuf.buf[--n] = c;
14351133e27eSPeter Avalos 	}
14361133e27eSPeter Avalos 	if (linep != NULL)
14370c7ad07eSAntonio Huete Jimenez 		*linep = &linebuf.buf[n];
14381133e27eSPeter Avalos 	if (line_lenp != NULL)
14391133e27eSPeter Avalos 		*line_lenp = size_linebuf - 1 - n;
14401133e27eSPeter Avalos 	return (new_pos);
14411133e27eSPeter Avalos }
144202d62a0fSDaniel Fojt 
144302d62a0fSDaniel Fojt /*
1444*320d7c8aSAaron LI  * Skip cols printable columns at the start of line.
1445*320d7c8aSAaron LI  * Return number of bytes skipped.
1446*320d7c8aSAaron LI  */
skip_columns(int cols,char ** linep,int * line_lenp)1447*320d7c8aSAaron LI public int skip_columns(int cols, char **linep, int *line_lenp)
1448*320d7c8aSAaron LI {
1449*320d7c8aSAaron LI 	char *line = *linep;
1450*320d7c8aSAaron LI 	char *eline = line + *line_lenp;
1451*320d7c8aSAaron LI 	LWCHAR pch = 0;
1452*320d7c8aSAaron LI 	int bytes;
1453*320d7c8aSAaron LI 
1454*320d7c8aSAaron LI 	while (cols > 0 && line < eline)
1455*320d7c8aSAaron LI 	{
1456*320d7c8aSAaron LI 		LWCHAR ch = step_char(&line, +1, eline);
1457*320d7c8aSAaron LI 		struct ansi_state *pansi = ansi_start(ch);
1458*320d7c8aSAaron LI 		if (pansi != NULL)
1459*320d7c8aSAaron LI 		{
1460*320d7c8aSAaron LI 			skip_ansi(pansi, &line, eline);
1461*320d7c8aSAaron LI 			ansi_done(pansi);
1462*320d7c8aSAaron LI 			pch = 0;
1463*320d7c8aSAaron LI 		} else
1464*320d7c8aSAaron LI 		{
1465*320d7c8aSAaron LI 			int w = pwidth(ch, 0, pch, 0);
1466*320d7c8aSAaron LI 			cols -= w;
1467*320d7c8aSAaron LI 			pch = ch;
1468*320d7c8aSAaron LI 		}
1469*320d7c8aSAaron LI 	}
1470*320d7c8aSAaron LI 	bytes = line - *linep;
1471*320d7c8aSAaron LI 	*linep = line;
1472*320d7c8aSAaron LI 	*line_lenp -= bytes;
1473*320d7c8aSAaron LI 	return (bytes);
1474*320d7c8aSAaron LI }
1475*320d7c8aSAaron LI 
1476*320d7c8aSAaron LI /*
14770c7ad07eSAntonio Huete Jimenez  * Append a string to the line buffer.
14780c7ad07eSAntonio Huete Jimenez  */
pappstr(constant char * str)1479*320d7c8aSAaron LI static int pappstr(constant char *str)
14800c7ad07eSAntonio Huete Jimenez {
14810c7ad07eSAntonio Huete Jimenez 	while (*str != '\0')
14820c7ad07eSAntonio Huete Jimenez 	{
14830c7ad07eSAntonio Huete Jimenez 		if (pappend(*str++, NULL_POSITION))
14840c7ad07eSAntonio Huete Jimenez 			/* Doesn't fit on screen. */
14850c7ad07eSAntonio Huete Jimenez 			return 1;
14860c7ad07eSAntonio Huete Jimenez 	}
14870c7ad07eSAntonio Huete Jimenez 	return 0;
14880c7ad07eSAntonio Huete Jimenez }
14890c7ad07eSAntonio Huete Jimenez 
14900c7ad07eSAntonio Huete Jimenez /*
14910c7ad07eSAntonio Huete Jimenez  * Load a string into the line buffer.
14920c7ad07eSAntonio Huete Jimenez  * If the string is too long to fit on the screen,
14930c7ad07eSAntonio Huete Jimenez  * truncate the beginning of the string to fit.
14940c7ad07eSAntonio Huete Jimenez  */
load_line(constant char * str)1495*320d7c8aSAaron LI public void load_line(constant char *str)
14960c7ad07eSAntonio Huete Jimenez {
14970c7ad07eSAntonio Huete Jimenez 	int save_hshift = hshift;
14980c7ad07eSAntonio Huete Jimenez 
14990c7ad07eSAntonio Huete Jimenez 	hshift = 0;
15000c7ad07eSAntonio Huete Jimenez 	for (;;)
15010c7ad07eSAntonio Huete Jimenez 	{
15020c7ad07eSAntonio Huete Jimenez 		prewind();
15030c7ad07eSAntonio Huete Jimenez 		if (pappstr(str) == 0)
15040c7ad07eSAntonio Huete Jimenez 			break;
15050c7ad07eSAntonio Huete Jimenez 		/*
15060c7ad07eSAntonio Huete Jimenez 		 * Didn't fit on screen; increase left shift by one.
15070c7ad07eSAntonio Huete Jimenez 		 * {{ This gets very inefficient if the string
15080c7ad07eSAntonio Huete Jimenez 		 * is much longer than the screen width. }}
15090c7ad07eSAntonio Huete Jimenez 		 */
15100c7ad07eSAntonio Huete Jimenez 		hshift += 1;
15110c7ad07eSAntonio Huete Jimenez 	}
15120c7ad07eSAntonio Huete Jimenez 	set_linebuf(linebuf.end, '\0', AT_NORMAL);
15130c7ad07eSAntonio Huete Jimenez 	hshift = save_hshift;
15140c7ad07eSAntonio Huete Jimenez }
15150c7ad07eSAntonio Huete Jimenez 
15160c7ad07eSAntonio Huete Jimenez /*
151702d62a0fSDaniel Fojt  * Find the shift necessary to show the end of the longest displayed line.
151802d62a0fSDaniel Fojt  */
rrshift(void)1519*320d7c8aSAaron LI public int rrshift(void)
152002d62a0fSDaniel Fojt {
152102d62a0fSDaniel Fojt 	POSITION pos;
152202d62a0fSDaniel Fojt 	int save_width;
152302d62a0fSDaniel Fojt 	int line;
152402d62a0fSDaniel Fojt 	int longest = 0;
152502d62a0fSDaniel Fojt 
152602d62a0fSDaniel Fojt 	save_width = sc_width;
152702d62a0fSDaniel Fojt 	sc_width = INT_MAX;
152802d62a0fSDaniel Fojt 	pos = position(TOP);
152902d62a0fSDaniel Fojt 	for (line = 0; line < sc_height && pos != NULL_POSITION; line++)
153002d62a0fSDaniel Fojt 	{
153102d62a0fSDaniel Fojt 		pos = forw_line(pos);
15320c7ad07eSAntonio Huete Jimenez 		if (end_column > longest)
15330c7ad07eSAntonio Huete Jimenez 			longest = end_column;
153402d62a0fSDaniel Fojt 	}
153502d62a0fSDaniel Fojt 	sc_width = save_width;
153602d62a0fSDaniel Fojt 	if (longest < sc_width)
153702d62a0fSDaniel Fojt 		return 0;
153802d62a0fSDaniel Fojt 	return longest - sc_width;
153902d62a0fSDaniel Fojt }
15400c7ad07eSAntonio Huete Jimenez 
15410c7ad07eSAntonio Huete Jimenez /*
15420c7ad07eSAntonio Huete Jimenez  * Get the color_map index associated with a given attribute.
15430c7ad07eSAntonio Huete Jimenez  */
lookup_color_index(int attr)1544*320d7c8aSAaron LI static int lookup_color_index(int attr)
15450c7ad07eSAntonio Huete Jimenez {
1546*320d7c8aSAaron LI 	int cx;
1547*320d7c8aSAaron LI 	for (cx = 0;  cx < sizeof(color_map)/sizeof(*color_map);  cx++)
1548*320d7c8aSAaron LI 		if (color_map[cx].attr == attr)
1549*320d7c8aSAaron LI 			return cx;
1550*320d7c8aSAaron LI 	return -1;
15510c7ad07eSAntonio Huete Jimenez }
1552*320d7c8aSAaron LI 
color_index(int attr)1553*320d7c8aSAaron LI static int color_index(int attr)
1554*320d7c8aSAaron LI {
1555*320d7c8aSAaron LI 	if (use_color && (attr & AT_COLOR))
1556*320d7c8aSAaron LI 		return lookup_color_index(attr & AT_COLOR);
15570c7ad07eSAntonio Huete Jimenez 	if (attr & AT_UNDERLINE)
1558*320d7c8aSAaron LI 		return lookup_color_index(AT_UNDERLINE);
15590c7ad07eSAntonio Huete Jimenez 	if (attr & AT_BOLD)
1560*320d7c8aSAaron LI 		return lookup_color_index(AT_BOLD);
15610c7ad07eSAntonio Huete Jimenez 	if (attr & AT_BLINK)
1562*320d7c8aSAaron LI 		return lookup_color_index(AT_BLINK);
15630c7ad07eSAntonio Huete Jimenez 	if (attr & AT_STANDOUT)
1564*320d7c8aSAaron LI 		return lookup_color_index(AT_STANDOUT);
15650c7ad07eSAntonio Huete Jimenez 	return -1;
15660c7ad07eSAntonio Huete Jimenez }
15670c7ad07eSAntonio Huete Jimenez 
15680c7ad07eSAntonio Huete Jimenez /*
15690c7ad07eSAntonio Huete Jimenez  * Set the color string to use for a given attribute.
15700c7ad07eSAntonio Huete Jimenez  */
set_color_map(int attr,char * colorstr)1571*320d7c8aSAaron LI public int set_color_map(int attr, char *colorstr)
15720c7ad07eSAntonio Huete Jimenez {
15730c7ad07eSAntonio Huete Jimenez 	int cx = color_index(attr);
15740c7ad07eSAntonio Huete Jimenez 	if (cx < 0)
15750c7ad07eSAntonio Huete Jimenez 		return -1;
1576*320d7c8aSAaron LI 	if (strlen(colorstr)+1 > sizeof(color_map[cx].color))
15770c7ad07eSAntonio Huete Jimenez 		return -1;
15780c7ad07eSAntonio Huete Jimenez 	if (*colorstr != '\0' && parse_color(colorstr, NULL, NULL) == CT_NULL)
15790c7ad07eSAntonio Huete Jimenez 		return -1;
1580*320d7c8aSAaron LI 	strcpy(color_map[cx].color, colorstr);
15810c7ad07eSAntonio Huete Jimenez 	return 0;
15820c7ad07eSAntonio Huete Jimenez }
15830c7ad07eSAntonio Huete Jimenez 
15840c7ad07eSAntonio Huete Jimenez /*
15850c7ad07eSAntonio Huete Jimenez  * Get the color string to use for a given attribute.
15860c7ad07eSAntonio Huete Jimenez  */
get_color_map(int attr)1587*320d7c8aSAaron LI public char * get_color_map(int attr)
15880c7ad07eSAntonio Huete Jimenez {
15890c7ad07eSAntonio Huete Jimenez 	int cx = color_index(attr);
15900c7ad07eSAntonio Huete Jimenez 	if (cx < 0)
15910c7ad07eSAntonio Huete Jimenez 		return NULL;
1592*320d7c8aSAaron LI 	return color_map[cx].color;
15930c7ad07eSAntonio Huete Jimenez }
1594