1 /* $NetBSD: line.c,v 1.4 2003/08/07 09:27:59 agc Exp $ */
4 * Copyright (c) 1988 Mark Nudelman
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
36 static char sccsid
[] = "@(#)line.c 8.1 (Berkeley) 6/6/93";
38 __RCSID("$NetBSD: line.c,v 1.4 2003/08/07 09:27:59 agc Exp $");
43 * Routines to manipulate the "line buffer".
44 * The line buffer holds a line of output as it is being built
45 * in preparation for output to the screen.
46 * We keep track of the PRINTABLE length of the line as it is being built.
49 #include <sys/types.h>
55 static char linebuf
[1024]; /* Buffer which holds the current output line */
56 static char *curr
; /* Pointer into linebuf */
57 static int column
; /* Printable length, accounting for
60 * A ridiculously complex state machine takes care of backspaces. The
61 * complexity arises from the attempt to deal with all cases, especially
62 * involving long lines with underlining, boldfacing or whatever. There
63 * are still some cases which will break it.
65 * There are four states:
66 * LN_NORMAL is the normal state (not in underline mode).
67 * LN_UNDERLINE means we are in underline mode. We expect to get
68 * either a sequence like "_\bX" or "X\b_" to continue
69 * underline mode, or anything else to end underline mode.
70 * LN_BOLDFACE means we are in boldface mode. We expect to get sequences
71 * like "X\bX\b...X\bX" to continue boldface mode, or anything
72 * else to end boldface mode.
73 * LN_UL_X means we are one character after LN_UNDERLINE
74 * (we have gotten the '_' in "_\bX" or the 'X' in "X\b_").
75 * LN_UL_XB means we are one character after LN_UL_X
76 * (we have gotten the backspace in "_\bX" or "X\b_";
77 * we expect one more ordinary character,
78 * which will put us back in state LN_UNDERLINE).
79 * LN_BO_X means we are one character after LN_BOLDFACE
80 * (we have gotten the 'X' in "X\bX").
81 * LN_BO_XB means we are one character after LN_BO_X
82 * (we have gotten the backspace in "X\bX";
83 * we expect one more 'X' which will put us back
86 static int ln_state
; /* Currently in normal/underline/bold/etc mode? */
87 #define LN_NORMAL 0 /* Not in underline, boldface or whatever mode */
88 #define LN_UNDERLINE 1 /* In underline, need next char */
89 #define LN_UL_X 2 /* In underline, got char, need \b */
90 #define LN_UL_XB 3 /* In underline, got char & \b, need one more */
91 #define LN_BOLDFACE 4 /* In boldface, need next char */
92 #define LN_BO_X 5 /* In boldface, got char, need \b */
93 #define LN_BO_XB 6 /* In boldface, got char & \b, need same char */
95 char *line
; /* Pointer to the current line.
96 Usually points to linebuf. */
98 * Rewind the line buffer.
103 line
= curr
= linebuf
;
104 ln_state
= LN_NORMAL
;
109 * Append a character to the line buffer.
110 * Expand tabs into spaces, handle underlining, boldfacing, etc.
111 * Returns 0 if ok, 1 if couldn't fit in buffer.
113 #define NEW_COLUMN(addon) \
114 if (column + addon + (ln_state ? ue_width : 0) > sc_width) \
125 * Terminate any special modes, if necessary.
126 * Append a '\0' to the end of the line.
148 ln_state
= LN_NORMAL
;
153 if (curr
> linebuf
+ sizeof(linebuf
) - 12)
155 * Almost out of room in the line buffer.
156 * Don't take any chances.
157 * {{ Linebuf is supposed to be big enough that this
158 * will never happen, but may need to be made
159 * bigger for wide screens or lots of backspaces. }}
165 * Advance the state machine.
169 if (curr
<= linebuf
+ 1
170 || curr
[-1] != (char)('H' | 0200))
175 if (c
== '_' || curr
[-2] == '_')
176 goto enter_underline
;
182 * We have "X\bX" (including the current char).
183 * Switch into boldface mode.
186 if (column
+ bo_width
+ be_width
+ 1 >= sc_width
)
188 * Not enough room left on the screen to
189 * enter and exit boldface mode.
193 if (bo_width
> 0 && curr
> linebuf
+ 2
194 && curr
[-3] == ' ') {
196 * Special case for magic cookie terminals:
197 * if the previous char was a space, replace
198 * it with the "enter boldface" sequence.
201 column
+= bo_width
-1;
212 * We have either "_\bX" or "X\b_" (including
213 * the current char). Switch into underline mode.
216 if (column
+ ul_width
+ ue_width
+ 1 >= sc_width
)
218 * Not enough room left on the screen to
219 * enter and exit underline mode.
224 curr
> linebuf
+ 2 && curr
[-3] == ' ')
227 * Special case for magic cookie terminals:
228 * if the previous char was a space, replace
229 * it with the "enter underline" sequence.
232 column
+= ul_width
-1;
244 * Termination of a sequence "_\bX" or "X\b_".
246 if (c
!= '_' && curr
[-2] != '_' && c
== curr
[-2])
249 * We seem to have run on from underlining
250 * into boldfacing - this is a nasty fix, but
251 * until this whole routine is rewritten as a
252 * real DFA, ... well ...
257 curr
+= 2; /* char & non-existent backspace */
265 ln_state
= LN_UNDERLINE
;
269 * Termination of a sequnce "X\bX".
271 if (c
!= curr
[-2] && (c
== '_' || curr
[-2] == '_'))
274 * We seem to have run on from
275 * boldfacing into underlining.
280 curr
+= 2; /* char & non-existent backspace */
286 ln_state
= LN_BOLDFACE
;
289 if (column
+ ue_width
+ bo_width
+ 1 + be_width
>= sc_width
)
291 * We have just barely enough room to
292 * exit underline mode and handle a possible
293 * underline/boldface run on mixup.
304 if (column
+ be_width
+ ul_width
+ 1 + ue_width
>= sc_width
)
306 * We have just barely enough room to
307 * exit underline mode and handle a possible
308 * underline/boldface run on mixup.
319 * Exit underline mode.
320 * We have to shuffle the chars a bit
326 if (ue_width
> 0 && curr
[0] == ' ')
328 * Another special case for magic
329 * cookie terminals: if the next
330 * char is a space, replace it
331 * with the "exit underline" sequence.
336 ln_state
= LN_NORMAL
;
345 * Exit boldface mode.
346 * We have to shuffle the chars a bit
352 if (be_width
> 0 && curr
[0] == ' ')
354 * Another special case for magic
355 * cookie terminals: if the next
356 * char is a space, replace it
357 * with the "exit boldface" sequence.
362 ln_state
= LN_NORMAL
;
370 * Expand a tab into spaces.
374 } while ((column
% tabstop
) != 0);
380 if (ln_state
== LN_NORMAL
)
384 *curr
++ = ('H' | 0200);
388 if (CONTROL_CHAR(c
)) {
390 * Put a "^X" into the buffer. The 0200 bit is used to tell
391 * put_line() to prefix the char with a ^. We don't actually
392 * put the ^ in the buffer because we sometimes need to move
393 * chars around, and such movement might separate the ^ from
394 * its following character.
397 *curr
++ = (CARAT_CHAR(c
) | 0200);
402 * Ordinary character. Just put it in the buffer.
410 * Analogous to forw_line(), but deals with "raw lines":
411 * lines which are not split for screen width.
412 * {{ This is supposed to be more efficient than forw_line(). }}
415 forw_raw_line(curr_pos
)
422 if (curr_pos
== NULL_POSITION
|| ch_seek(curr_pos
) ||
423 (c
= ch_forw_get()) == EOI
)
424 return (NULL_POSITION
);
430 if (c
== '\n' || c
== EOI
)
435 if (p
>= &linebuf
[sizeof(linebuf
)-1])
438 * Overflowed the input buffer.
439 * Pretend the line ended here.
440 * {{ The line buffer is supposed to be big
441 * enough that this never happens. }}
443 new_pos
= ch_tell() - 1;
455 * Analogous to back_line(), but deals with "raw lines".
456 * {{ This is supposed to be more efficient than back_line(). }}
459 back_raw_line(curr_pos
)
466 if (curr_pos
== NULL_POSITION
|| curr_pos
<= (off_t
)0 ||
468 return (NULL_POSITION
);
470 p
= &linebuf
[sizeof(linebuf
)];
479 * This is the newline ending the previous line.
480 * We have hit the beginning of the line.
482 new_pos
= ch_tell() + 1;
488 * We have hit the beginning of the file.
489 * This must be the first line in the file.
490 * This must, of course, be the beginning of the line.
498 * Overflowed the input buffer.
499 * Pretend the line ended here.
501 new_pos
= ch_tell() + 1;