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