Bring in an errno.9 manual page (based on NetBSD's).
[dragonfly.git] / contrib / less / line.c
blobe54835d4744b86a740843f2d21d0db499d1f82bb
1 /*
2 * Copyright (C) 1984-2015 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
8 */
12 * Routines to manipulate the "line buffer".
13 * The line buffer holds a line of output as it is being built
14 * in preparation for output to the screen.
17 #include "less.h"
18 #include "charset.h"
20 static char *linebuf = NULL; /* Buffer which holds the current output line */
21 static char *attr = NULL; /* Extension of linebuf to hold attributes */
22 public int size_linebuf = 0; /* Size of line buffer (and attr buffer) */
24 static int cshift; /* Current left-shift of output line buffer */
25 public int hshift; /* Desired left-shift of output line buffer */
26 public int tabstops[TABSTOP_MAX] = { 0 }; /* Custom tabstops */
27 public int ntabstops = 1; /* Number of tabstops */
28 public int tabdefault = 8; /* Default repeated tabstops */
29 public POSITION highest_hilite; /* Pos of last hilite in file found so far */
31 static int curr; /* Index into linebuf */
32 static int column; /* Printable length, accounting for
33 backspaces, etc. */
34 static int overstrike; /* Next char should overstrike previous char */
35 static int last_overstrike = AT_NORMAL;
36 static int is_null_line; /* There is no current line */
37 static int lmargin; /* Left margin */
38 static LWCHAR pendc;
39 static POSITION pendpos;
40 static char *end_ansi_chars;
41 static char *mid_ansi_chars;
43 static int attr_swidth();
44 static int attr_ewidth();
45 static int do_append();
47 extern int sigs;
48 extern int bs_mode;
49 extern int linenums;
50 extern int ctldisp;
51 extern int twiddle;
52 extern int binattr;
53 extern int status_col;
54 extern int auto_wrap, ignaw;
55 extern int bo_s_width, bo_e_width;
56 extern int ul_s_width, ul_e_width;
57 extern int bl_s_width, bl_e_width;
58 extern int so_s_width, so_e_width;
59 extern int sc_width, sc_height;
60 extern int utf_mode;
61 extern POSITION start_attnpos;
62 extern POSITION end_attnpos;
64 static char mbc_buf[MAX_UTF_CHAR_LEN];
65 static int mbc_buf_len = 0;
66 static int mbc_buf_index = 0;
67 static POSITION mbc_pos;
70 * Initialize from environment variables.
72 public void
73 init_line()
75 end_ansi_chars = lgetenv("LESSANSIENDCHARS");
76 if (end_ansi_chars == NULL || *end_ansi_chars == '\0')
77 end_ansi_chars = "m";
79 mid_ansi_chars = lgetenv("LESSANSIMIDCHARS");
80 if (mid_ansi_chars == NULL || *mid_ansi_chars == '\0')
81 mid_ansi_chars = "0123456789:;[?!\"'#%()*+ ";
83 linebuf = (char *) ecalloc(LINEBUF_SIZE, sizeof(char));
84 attr = (char *) ecalloc(LINEBUF_SIZE, sizeof(char));
85 size_linebuf = LINEBUF_SIZE;
89 * Expand the line buffer.
91 static int
92 expand_linebuf()
94 /* Double the size of the line buffer. */
95 int new_size = size_linebuf * 2;
97 /* Just realloc to expand the buffer, if we can. */
98 #if HAVE_REALLOC
99 char *new_buf = (char *) realloc(linebuf, new_size);
100 char *new_attr = (char *) realloc(attr, new_size);
101 #else
102 char *new_buf = (char *) calloc(new_size, sizeof(char));
103 char *new_attr = (char *) calloc(new_size, sizeof(char));
104 #endif
105 if (new_buf == NULL || new_attr == NULL)
107 if (new_attr != NULL)
108 free(new_attr);
109 if (new_buf != NULL)
110 free(new_buf);
111 return 1;
113 #if HAVE_REALLOC
115 * We realloc'd the buffers; they already have the old contents.
117 #if 0
118 memset(new_buf + size_linebuf, 0, new_size - size_linebuf);
119 memset(new_attr + size_linebuf, 0, new_size - size_linebuf);
120 #endif
121 #else
123 * We just calloc'd the buffers; copy the old contents.
125 memcpy(new_buf, linebuf, size_linebuf * sizeof(char));
126 memcpy(new_attr, attr, size_linebuf * sizeof(char));
127 free(attr);
128 free(linebuf);
129 #endif
130 linebuf = new_buf;
131 attr = new_attr;
132 size_linebuf = new_size;
133 return 0;
137 * Is a character ASCII?
139 public int
140 is_ascii_char(ch)
141 LWCHAR ch;
143 return (ch <= 0x7F);
147 * Rewind the line buffer.
149 public void
150 prewind()
152 curr = 0;
153 column = 0;
154 cshift = 0;
155 overstrike = 0;
156 last_overstrike = AT_NORMAL;
157 mbc_buf_len = 0;
158 is_null_line = 0;
159 pendc = '\0';
160 lmargin = 0;
161 if (status_col)
162 lmargin += 1;
166 * Insert the line number (of the given position) into the line buffer.
168 public void
169 plinenum(pos)
170 POSITION pos;
172 register LINENUM linenum = 0;
173 register int i;
175 if (linenums == OPT_ONPLUS)
178 * Get the line number and put it in the current line.
179 * {{ Note: since find_linenum calls forw_raw_line,
180 * it may seek in the input file, requiring the caller
181 * of plinenum to re-seek if necessary. }}
182 * {{ Since forw_raw_line modifies linebuf, we must
183 * do this first, before storing anything in linebuf. }}
185 linenum = find_linenum(pos);
189 * Display a status column if the -J option is set.
191 if (status_col)
193 linebuf[curr] = ' ';
194 if (start_attnpos != NULL_POSITION &&
195 pos >= start_attnpos && pos < end_attnpos)
196 attr[curr] = AT_NORMAL|AT_HILITE;
197 else
198 attr[curr] = AT_NORMAL;
199 curr++;
200 column++;
203 * Display the line number at the start of each line
204 * if the -N option is set.
206 if (linenums == OPT_ONPLUS)
208 char buf[INT_STRLEN_BOUND(pos) + 2];
209 int n;
211 linenumtoa(linenum, buf);
212 n = (int) strlen(buf);
213 if (n < MIN_LINENUM_WIDTH)
214 n = MIN_LINENUM_WIDTH;
215 sprintf(linebuf+curr, "%*s ", n, buf);
216 n++; /* One space after the line number. */
217 for (i = 0; i < n; i++)
218 attr[curr+i] = AT_NORMAL;
219 curr += n;
220 column += n;
221 lmargin += n;
225 * Append enough spaces to bring us to the lmargin.
227 while (column < lmargin)
229 linebuf[curr] = ' ';
230 attr[curr++] = AT_NORMAL;
231 column++;
236 * Shift the input line left.
237 * This means discarding N printable chars at the start of the buffer.
239 static void
240 pshift(shift)
241 int shift;
243 LWCHAR prev_ch = 0;
244 unsigned char c;
245 int shifted = 0;
246 int to;
247 int from;
248 int len;
249 int width;
250 int prev_attr;
251 int next_attr;
253 if (shift > column - lmargin)
254 shift = column - lmargin;
255 if (shift > curr - lmargin)
256 shift = curr - lmargin;
258 to = from = lmargin;
260 * We keep on going when shifted == shift
261 * to get all combining chars.
263 while (shifted <= shift && from < curr)
265 c = linebuf[from];
266 if (ctldisp == OPT_ONPLUS && IS_CSI_START(c))
268 /* Keep cumulative effect. */
269 linebuf[to] = c;
270 attr[to++] = attr[from++];
271 while (from < curr && linebuf[from])
273 linebuf[to] = linebuf[from];
274 attr[to++] = attr[from];
275 if (!is_ansi_middle(linebuf[from++]))
276 break;
278 continue;
281 width = 0;
283 if (!IS_ASCII_OCTET(c) && utf_mode)
285 /* Assumes well-formedness validation already done. */
286 LWCHAR ch;
288 len = utf_len(c);
289 if (from + len > curr)
290 break;
291 ch = get_wchar(linebuf + from);
292 if (!is_composing_char(ch) && !is_combining_char(prev_ch, ch))
293 width = is_wide_char(ch) ? 2 : 1;
294 prev_ch = ch;
295 } else
297 len = 1;
298 if (c == '\b')
299 /* XXX - Incorrect if several '\b' in a row. */
300 width = (utf_mode && is_wide_char(prev_ch)) ? -2 : -1;
301 else if (!control_char(c))
302 width = 1;
303 prev_ch = 0;
306 if (width == 2 && shift - shifted == 1) {
307 /* Should never happen when called by pshift_all(). */
308 attr[to] = attr[from];
310 * Assume a wide_char will never be the first half of a
311 * combining_char pair, so reset prev_ch in case we're
312 * followed by a '\b'.
314 prev_ch = linebuf[to++] = ' ';
315 from += len;
316 shifted++;
317 continue;
320 /* Adjust width for magic cookies. */
321 prev_attr = (to > 0) ? attr[to-1] : AT_NORMAL;
322 next_attr = (from + len < curr) ? attr[from + len] : prev_attr;
323 if (!is_at_equiv(attr[from], prev_attr) &&
324 !is_at_equiv(attr[from], next_attr))
326 width += attr_swidth(attr[from]);
327 if (from + len < curr)
328 width += attr_ewidth(attr[from]);
329 if (is_at_equiv(prev_attr, next_attr))
331 width += attr_ewidth(prev_attr);
332 if (from + len < curr)
333 width += attr_swidth(next_attr);
337 if (shift - shifted < width)
338 break;
339 from += len;
340 shifted += width;
341 if (shifted < 0)
342 shifted = 0;
344 while (from < curr)
346 linebuf[to] = linebuf[from];
347 attr[to++] = attr[from++];
349 curr = to;
350 column -= shifted;
351 cshift += shifted;
357 public void
358 pshift_all()
360 pshift(column);
364 * Return the printing width of the start (enter) sequence
365 * for a given character attribute.
367 static int
368 attr_swidth(a)
369 int a;
371 int w = 0;
373 a = apply_at_specials(a);
375 if (a & AT_UNDERLINE)
376 w += ul_s_width;
377 if (a & AT_BOLD)
378 w += bo_s_width;
379 if (a & AT_BLINK)
380 w += bl_s_width;
381 if (a & AT_STANDOUT)
382 w += so_s_width;
384 return w;
388 * Return the printing width of the end (exit) sequence
389 * for a given character attribute.
391 static int
392 attr_ewidth(a)
393 int a;
395 int w = 0;
397 a = apply_at_specials(a);
399 if (a & AT_UNDERLINE)
400 w += ul_e_width;
401 if (a & AT_BOLD)
402 w += bo_e_width;
403 if (a & AT_BLINK)
404 w += bl_e_width;
405 if (a & AT_STANDOUT)
406 w += so_e_width;
408 return w;
412 * Return the printing width of a given character and attribute,
413 * if the character were added to the current position in the line buffer.
414 * Adding a character with a given attribute may cause an enter or exit
415 * attribute sequence to be inserted, so this must be taken into account.
417 static int
418 pwidth(ch, a, prev_ch)
419 LWCHAR ch;
420 int a;
421 LWCHAR prev_ch;
423 int w;
425 if (ch == '\b')
427 * Backspace moves backwards one or two positions.
428 * XXX - Incorrect if several '\b' in a row.
430 return (utf_mode && is_wide_char(prev_ch)) ? -2 : -1;
432 if (!utf_mode || is_ascii_char(ch))
434 if (control_char((char)ch))
437 * Control characters do unpredictable things,
438 * so we don't even try to guess; say it doesn't move.
439 * This can only happen if the -r flag is in effect.
441 return (0);
443 } else
445 if (is_composing_char(ch) || is_combining_char(prev_ch, ch))
448 * Composing and combining chars take up no space.
450 * Some terminals, upon failure to compose a
451 * composing character with the character(s) that
452 * precede(s) it will actually take up one column
453 * for the composing character; there isn't much
454 * we could do short of testing the (complex)
455 * composition process ourselves and printing
456 * a binary representation when it fails.
458 return (0);
463 * Other characters take one or two columns,
464 * plus the width of any attribute enter/exit sequence.
466 w = 1;
467 if (is_wide_char(ch))
468 w++;
469 if (curr > 0 && !is_at_equiv(attr[curr-1], a))
470 w += attr_ewidth(attr[curr-1]);
471 if ((apply_at_specials(a) != AT_NORMAL) &&
472 (curr == 0 || !is_at_equiv(attr[curr-1], a)))
473 w += attr_swidth(a);
474 return (w);
478 * Delete to the previous base character in the line buffer.
479 * Return 1 if one is found.
481 static int
482 backc()
484 LWCHAR prev_ch;
485 char *p = linebuf + curr;
486 LWCHAR ch = step_char(&p, -1, linebuf + lmargin);
487 int width;
489 /* This assumes that there is no '\b' in linebuf. */
490 while ( curr > lmargin
491 && column > lmargin
492 && (!(attr[curr - 1] & (AT_ANSI|AT_BINARY))))
494 curr = (int) (p - linebuf);
495 prev_ch = step_char(&p, -1, linebuf + lmargin);
496 width = pwidth(ch, attr[curr], prev_ch);
497 column -= width;
498 if (width > 0)
499 return 1;
500 ch = prev_ch;
503 return 0;
507 * Are we currently within a recognized ANSI escape sequence?
509 static int
510 in_ansi_esc_seq()
512 char *p;
515 * Search backwards for either an ESC (which means we ARE in a seq);
516 * or an end char (which means we're NOT in a seq).
518 for (p = &linebuf[curr]; p > linebuf; )
520 LWCHAR ch = step_char(&p, -1, linebuf);
521 if (IS_CSI_START(ch))
522 return (1);
523 if (!is_ansi_middle(ch))
524 return (0);
526 return (0);
530 * Is a character the end of an ANSI escape sequence?
532 public int
533 is_ansi_end(ch)
534 LWCHAR ch;
536 if (!is_ascii_char(ch))
537 return (0);
538 return (strchr(end_ansi_chars, (char) ch) != NULL);
544 public int
545 is_ansi_middle(ch)
546 LWCHAR ch;
548 if (!is_ascii_char(ch))
549 return (0);
550 if (is_ansi_end(ch))
551 return (0);
552 return (strchr(mid_ansi_chars, (char) ch) != NULL);
556 * Append a character and attribute to the line buffer.
558 #define STORE_CHAR(ch,a,rep,pos) \
559 do { \
560 if (store_char((ch),(a),(rep),(pos))) return (1); \
561 } while (0)
563 static int
564 store_char(ch, a, rep, pos)
565 LWCHAR ch;
566 int a;
567 char *rep;
568 POSITION pos;
570 int w;
571 int replen;
572 char cs;
574 w = (a & (AT_UNDERLINE|AT_BOLD)); /* Pre-use w. */
575 if (w != AT_NORMAL)
576 last_overstrike = w;
578 #if HILITE_SEARCH
580 int matches;
581 if (is_hilited(pos, pos+1, 0, &matches))
584 * This character should be highlighted.
585 * Override the attribute passed in.
587 if (a != AT_ANSI)
589 if (highest_hilite != NULL_POSITION &&
590 pos > highest_hilite)
591 highest_hilite = pos;
592 a |= AT_HILITE;
596 #endif
598 if (ctldisp == OPT_ONPLUS && in_ansi_esc_seq())
600 if (!is_ansi_end(ch) && !is_ansi_middle(ch)) {
601 /* Remove whole unrecognized sequence. */
602 char *p = &linebuf[curr];
603 LWCHAR bch;
604 do {
605 bch = step_char(&p, -1, linebuf);
606 } while (p > linebuf && !IS_CSI_START(bch));
607 curr = (int) (p - linebuf);
608 return 0;
610 a = AT_ANSI; /* Will force re-AT_'ing around it. */
611 w = 0;
613 else if (ctldisp == OPT_ONPLUS && IS_CSI_START(ch))
615 a = AT_ANSI; /* Will force re-AT_'ing around it. */
616 w = 0;
618 else
620 char *p = &linebuf[curr];
621 LWCHAR prev_ch = step_char(&p, -1, linebuf);
622 w = pwidth(ch, a, prev_ch);
625 if (ctldisp != OPT_ON && column + w + attr_ewidth(a) > sc_width)
627 * Won't fit on screen.
629 return (1);
631 if (rep == NULL)
633 cs = (char) ch;
634 rep = &cs;
635 replen = 1;
636 } else
638 replen = utf_len(rep[0]);
640 if (curr + replen >= size_linebuf-6)
643 * Won't fit in line buffer.
644 * Try to expand it.
646 if (expand_linebuf())
647 return (1);
650 while (replen-- > 0)
652 linebuf[curr] = *rep++;
653 attr[curr] = a;
654 curr++;
656 column += w;
657 return (0);
661 * Append a tab to the line buffer.
662 * Store spaces to represent the tab.
664 #define STORE_TAB(a,pos) \
665 do { if (store_tab((a),(pos))) return (1); } while (0)
667 static int
668 store_tab(attr, pos)
669 int attr;
670 POSITION pos;
672 int to_tab = column + cshift - lmargin;
673 int i;
675 if (ntabstops < 2 || to_tab >= tabstops[ntabstops-1])
676 to_tab = tabdefault -
677 ((to_tab - tabstops[ntabstops-1]) % tabdefault);
678 else
680 for (i = ntabstops - 2; i >= 0; i--)
681 if (to_tab >= tabstops[i])
682 break;
683 to_tab = tabstops[i+1] - to_tab;
686 if (column + to_tab - 1 + pwidth(' ', attr, 0) + attr_ewidth(attr) > sc_width)
687 return 1;
689 do {
690 STORE_CHAR(' ', attr, " ", pos);
691 } while (--to_tab > 0);
692 return 0;
695 #define STORE_PRCHAR(c, pos) \
696 do { if (store_prchar((c), (pos))) return 1; } while (0)
698 static int
699 store_prchar(c, pos)
700 LWCHAR c;
701 POSITION pos;
703 char *s;
706 * Convert to printable representation.
708 s = prchar(c);
711 * Make sure we can get the entire representation
712 * of the character on this line.
714 if (column + (int) strlen(s) - 1 +
715 pwidth(' ', binattr, 0) + attr_ewidth(binattr) > sc_width)
716 return 1;
718 for ( ; *s != 0; s++)
719 STORE_CHAR(*s, AT_BINARY, NULL, pos);
721 return 0;
724 static int
725 flush_mbc_buf(pos)
726 POSITION pos;
728 int i;
730 for (i = 0; i < mbc_buf_index; i++)
731 if (store_prchar(mbc_buf[i], pos))
732 return mbc_buf_index - i;
734 return 0;
738 * Append a character to the line buffer.
739 * Expand tabs into spaces, handle underlining, boldfacing, etc.
740 * Returns 0 if ok, 1 if couldn't fit in buffer.
742 public int
743 pappend(c, pos)
744 unsigned char c;
745 POSITION pos;
747 int r;
749 if (pendc)
751 if (c == '\r' && pendc == '\r')
752 return (0);
753 if (do_append(pendc, NULL, pendpos))
755 * Oops. We've probably lost the char which
756 * was in pendc, since caller won't back up.
758 return (1);
759 pendc = '\0';
762 if (c == '\r' && bs_mode == BS_SPECIAL)
764 if (mbc_buf_len > 0) /* utf_mode must be on. */
766 /* Flush incomplete (truncated) sequence. */
767 r = flush_mbc_buf(mbc_pos);
768 mbc_buf_index = r + 1;
769 mbc_buf_len = 0;
770 if (r)
771 return (mbc_buf_index);
775 * Don't put the CR into the buffer until we see
776 * the next char. If the next char is a newline,
777 * discard the CR.
779 pendc = c;
780 pendpos = pos;
781 return (0);
784 if (!utf_mode)
786 r = do_append(c, NULL, pos);
787 } else
789 /* Perform strict validation in all possible cases. */
790 if (mbc_buf_len == 0)
792 retry:
793 mbc_buf_index = 1;
794 *mbc_buf = c;
795 if (IS_ASCII_OCTET(c))
796 r = do_append(c, NULL, pos);
797 else if (IS_UTF8_LEAD(c))
799 mbc_buf_len = utf_len(c);
800 mbc_pos = pos;
801 return (0);
802 } else
803 /* UTF8_INVALID or stray UTF8_TRAIL */
804 r = flush_mbc_buf(pos);
805 } else if (IS_UTF8_TRAIL(c))
807 mbc_buf[mbc_buf_index++] = c;
808 if (mbc_buf_index < mbc_buf_len)
809 return (0);
810 if (is_utf8_well_formed(mbc_buf, mbc_buf_index))
811 r = do_append(get_wchar(mbc_buf), mbc_buf, mbc_pos);
812 else
813 /* Complete, but not shortest form, sequence. */
814 mbc_buf_index = r = flush_mbc_buf(mbc_pos);
815 mbc_buf_len = 0;
816 } else
818 /* Flush incomplete (truncated) sequence. */
819 r = flush_mbc_buf(mbc_pos);
820 mbc_buf_index = r + 1;
821 mbc_buf_len = 0;
822 /* Handle new char. */
823 if (!r)
824 goto retry;
829 * If we need to shift the line, do it.
830 * But wait until we get to at least the middle of the screen,
831 * so shifting it doesn't affect the chars we're currently
832 * pappending. (Bold & underline can get messed up otherwise.)
834 if (cshift < hshift && column > sc_width / 2)
836 linebuf[curr] = '\0';
837 pshift(hshift - cshift);
839 if (r)
841 /* How many chars should caller back up? */
842 r = (!utf_mode) ? 1 : mbc_buf_index;
844 return (r);
847 static int
848 do_append(ch, rep, pos)
849 LWCHAR ch;
850 char *rep;
851 POSITION pos;
853 register int a;
854 LWCHAR prev_ch;
856 a = AT_NORMAL;
858 if (ch == '\b')
860 if (bs_mode == BS_CONTROL)
861 goto do_control_char;
864 * A better test is needed here so we don't
865 * backspace over part of the printed
866 * representation of a binary character.
868 if ( curr <= lmargin
869 || column <= lmargin
870 || (attr[curr - 1] & (AT_ANSI|AT_BINARY)))
871 STORE_PRCHAR('\b', pos);
872 else if (bs_mode == BS_NORMAL)
873 STORE_CHAR(ch, AT_NORMAL, NULL, pos);
874 else if (bs_mode == BS_SPECIAL)
875 overstrike = backc();
877 return 0;
880 if (overstrike > 0)
883 * Overstrike the character at the current position
884 * in the line buffer. This will cause either
885 * underline (if a "_" is overstruck),
886 * bold (if an identical character is overstruck),
887 * or just deletion of the character in the buffer.
889 overstrike = utf_mode ? -1 : 0;
890 if (utf_mode)
892 /* To be correct, this must be a base character. */
893 prev_ch = get_wchar(linebuf + curr);
894 } else
896 prev_ch = (unsigned char) linebuf[curr];
898 a = attr[curr];
899 if (ch == prev_ch)
902 * Overstriking a char with itself means make it bold.
903 * But overstriking an underscore with itself is
904 * ambiguous. It could mean make it bold, or
905 * it could mean make it underlined.
906 * Use the previous overstrike to resolve it.
908 if (ch == '_')
910 if ((a & (AT_BOLD|AT_UNDERLINE)) != AT_NORMAL)
911 a |= (AT_BOLD|AT_UNDERLINE);
912 else if (last_overstrike != AT_NORMAL)
913 a |= last_overstrike;
914 else
915 a |= AT_BOLD;
916 } else
917 a |= AT_BOLD;
918 } else if (ch == '_')
920 a |= AT_UNDERLINE;
921 ch = prev_ch;
922 rep = linebuf + curr;
923 } else if (prev_ch == '_')
925 a |= AT_UNDERLINE;
927 /* Else we replace prev_ch, but we keep its attributes. */
928 } else if (overstrike < 0)
930 if ( is_composing_char(ch)
931 || is_combining_char(get_wchar(linebuf + curr), ch))
932 /* Continuation of the same overstrike. */
933 a = last_overstrike;
934 else
935 overstrike = 0;
938 if (ch == '\t')
941 * Expand a tab into spaces.
943 switch (bs_mode)
945 case BS_CONTROL:
946 goto do_control_char;
947 case BS_NORMAL:
948 case BS_SPECIAL:
949 STORE_TAB(a, pos);
950 break;
952 } else if ((!utf_mode || is_ascii_char(ch)) && control_char((char)ch))
954 do_control_char:
955 if (ctldisp == OPT_ON || (ctldisp == OPT_ONPLUS && IS_CSI_START(ch)))
958 * Output as a normal character.
960 STORE_CHAR(ch, AT_NORMAL, rep, pos);
961 } else
963 STORE_PRCHAR((char) ch, pos);
965 } else if (utf_mode && ctldisp != OPT_ON && is_ubin_char(ch))
967 char *s;
969 s = prutfchar(ch);
971 if (column + (int) strlen(s) - 1 +
972 pwidth(' ', binattr, 0) + attr_ewidth(binattr) > sc_width)
973 return (1);
975 for ( ; *s != 0; s++)
976 STORE_CHAR(*s, AT_BINARY, NULL, pos);
977 } else
979 STORE_CHAR(ch, a, rep, pos);
981 return (0);
987 public int
988 pflushmbc()
990 int r = 0;
992 if (mbc_buf_len > 0)
994 /* Flush incomplete (truncated) sequence. */
995 r = flush_mbc_buf(mbc_pos);
996 mbc_buf_len = 0;
998 return r;
1002 * Terminate the line in the line buffer.
1004 public void
1005 pdone(endline, forw)
1006 int endline;
1007 int forw;
1009 (void) pflushmbc();
1011 if (pendc && (pendc != '\r' || !endline))
1013 * If we had a pending character, put it in the buffer.
1014 * But discard a pending CR if we are at end of line
1015 * (that is, discard the CR in a CR/LF sequence).
1017 (void) do_append(pendc, NULL, pendpos);
1020 * Make sure we've shifted the line, if we need to.
1022 if (cshift < hshift)
1023 pshift(hshift - cshift);
1025 if (ctldisp == OPT_ONPLUS && is_ansi_end('m'))
1027 /* Switch to normal attribute at end of line. */
1028 char *p = "\033[m";
1029 for ( ; *p != '\0'; p++)
1031 linebuf[curr] = *p;
1032 attr[curr++] = AT_ANSI;
1037 * Add a newline if necessary,
1038 * and append a '\0' to the end of the line.
1039 * We output a newline if we're not at the right edge of the screen,
1040 * or if the terminal doesn't auto wrap,
1041 * or if this is really the end of the line AND the terminal ignores
1042 * a newline at the right edge.
1043 * (In the last case we don't want to output a newline if the terminal
1044 * doesn't ignore it since that would produce an extra blank line.
1045 * But we do want to output a newline if the terminal ignores it in case
1046 * the next line is blank. In that case the single newline output for
1047 * that blank line would be ignored!)
1049 if (column < sc_width || !auto_wrap || (endline && ignaw) || ctldisp == OPT_ON)
1051 linebuf[curr] = '\n';
1052 attr[curr] = AT_NORMAL;
1053 curr++;
1055 else if (ignaw && column >= sc_width && forw)
1058 * Terminals with "ignaw" don't wrap until they *really* need
1059 * to, i.e. when the character *after* the last one to fit on a
1060 * line is output. But they are too hard to deal with when they
1061 * get in the state where a full screen width of characters
1062 * have been output but the cursor is sitting on the right edge
1063 * instead of at the start of the next line.
1064 * So we nudge them into wrapping by outputting a space
1065 * character plus a backspace. But do this only if moving
1066 * forward; if we're moving backward and drawing this line at
1067 * the top of the screen, the space would overwrite the first
1068 * char on the next line. We don't need to do this "nudge"
1069 * at the top of the screen anyway.
1071 linebuf[curr] = ' ';
1072 attr[curr++] = AT_NORMAL;
1073 linebuf[curr] = '\b';
1074 attr[curr++] = AT_NORMAL;
1076 linebuf[curr] = '\0';
1077 attr[curr] = AT_NORMAL;
1083 public void
1084 set_status_col(c)
1085 char c;
1087 linebuf[0] = c;
1088 attr[0] = AT_NORMAL|AT_HILITE;
1092 * Get a character from the current line.
1093 * Return the character as the function return value,
1094 * and the character attribute in *ap.
1096 public int
1097 gline(i, ap)
1098 register int i;
1099 register int *ap;
1101 if (is_null_line)
1104 * If there is no current line, we pretend the line is
1105 * either "~" or "", depending on the "twiddle" flag.
1107 if (twiddle)
1109 if (i == 0)
1111 *ap = AT_BOLD;
1112 return '~';
1114 --i;
1116 /* Make sure we're back to AT_NORMAL before the '\n'. */
1117 *ap = AT_NORMAL;
1118 return i ? '\0' : '\n';
1121 *ap = attr[i];
1122 return (linebuf[i] & 0xFF);
1126 * Indicate that there is no current line.
1128 public void
1129 null_line()
1131 is_null_line = 1;
1132 cshift = 0;
1136 * Analogous to forw_line(), but deals with "raw lines":
1137 * lines which are not split for screen width.
1138 * {{ This is supposed to be more efficient than forw_line(). }}
1140 public POSITION
1141 forw_raw_line(curr_pos, linep, line_lenp)
1142 POSITION curr_pos;
1143 char **linep;
1144 int *line_lenp;
1146 register int n;
1147 register int c;
1148 POSITION new_pos;
1150 if (curr_pos == NULL_POSITION || ch_seek(curr_pos) ||
1151 (c = ch_forw_get()) == EOI)
1152 return (NULL_POSITION);
1154 n = 0;
1155 for (;;)
1157 if (c == '\n' || c == EOI || ABORT_SIGS())
1159 new_pos = ch_tell();
1160 break;
1162 if (n >= size_linebuf-1)
1164 if (expand_linebuf())
1167 * Overflowed the input buffer.
1168 * Pretend the line ended here.
1170 new_pos = ch_tell() - 1;
1171 break;
1174 linebuf[n++] = c;
1175 c = ch_forw_get();
1177 linebuf[n] = '\0';
1178 if (linep != NULL)
1179 *linep = linebuf;
1180 if (line_lenp != NULL)
1181 *line_lenp = n;
1182 return (new_pos);
1186 * Analogous to back_line(), but deals with "raw lines".
1187 * {{ This is supposed to be more efficient than back_line(). }}
1189 public POSITION
1190 back_raw_line(curr_pos, linep, line_lenp)
1191 POSITION curr_pos;
1192 char **linep;
1193 int *line_lenp;
1195 register int n;
1196 register int c;
1197 POSITION new_pos;
1199 if (curr_pos == NULL_POSITION || curr_pos <= ch_zero() ||
1200 ch_seek(curr_pos-1))
1201 return (NULL_POSITION);
1203 n = size_linebuf;
1204 linebuf[--n] = '\0';
1205 for (;;)
1207 c = ch_back_get();
1208 if (c == '\n' || ABORT_SIGS())
1211 * This is the newline ending the previous line.
1212 * We have hit the beginning of the line.
1214 new_pos = ch_tell() + 1;
1215 break;
1217 if (c == EOI)
1220 * We have hit the beginning of the file.
1221 * This must be the first line in the file.
1222 * This must, of course, be the beginning of the line.
1224 new_pos = ch_zero();
1225 break;
1227 if (n <= 0)
1229 int old_size_linebuf = size_linebuf;
1230 char *fm;
1231 char *to;
1232 if (expand_linebuf())
1235 * Overflowed the input buffer.
1236 * Pretend the line ended here.
1238 new_pos = ch_tell() + 1;
1239 break;
1242 * Shift the data to the end of the new linebuf.
1244 for (fm = linebuf + old_size_linebuf - 1,
1245 to = linebuf + size_linebuf - 1;
1246 fm >= linebuf; fm--, to--)
1247 *to = *fm;
1248 n = size_linebuf - old_size_linebuf;
1250 linebuf[--n] = c;
1252 if (linep != NULL)
1253 *linep = &linebuf[n];
1254 if (line_lenp != NULL)
1255 *line_lenp = size_linebuf - 1 - n;
1256 return (new_pos);