2 * Copyright (C) 1984-2007 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 about less, or for information on how to
8 * contact the author, see the README file.
13 * High level routines dealing with the output to the screen.
17 #if MSDOS_COMPILER==WIN32C
21 public int errmsgs
; /* Count of messages displayed by error() */
23 public int final_attr
;
28 extern int so_s_width
, so_e_width
;
29 extern int screen_trashed
;
30 extern int any_display
;
34 #if MSDOS_COMPILER==WIN32C || MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
36 extern int nm_fg_color
, nm_bg_color
;
37 extern int bo_fg_color
, bo_bg_color
;
38 extern int ul_fg_color
, ul_bg_color
;
39 extern int so_fg_color
, so_bg_color
;
40 extern int bl_fg_color
, bl_bg_color
;
44 * Display the line which is in the line buffer.
56 * Don't output if a signal is pending.
62 final_attr
= AT_NORMAL
;
64 for (i
= 0; (c
= gline(i
, &a
)) != '\0'; i
++)
77 static char obuf
[OUTBUF_SIZE
];
78 static char *ob
= obuf
;
81 * Flush buffered output.
83 * If we haven't displayed any file data yet,
84 * output messages on error output (file descriptor 2),
85 * otherwise output on standard output (file descriptor 1).
87 * This has the desirable effect of producing all
88 * error messages on error output if standard output
89 * is directed to a file. It also does the same if
90 * we never produce any real output; for example, if
91 * the input file(s) cannot be opened. If we do
92 * eventually produce output, code in edit() makes
93 * sure these messages can be seen before they are
94 * overwritten or scrolled away.
106 #if MSDOS_COMPILER==MSOFTC
107 if (is_tty
&& any_display
)
115 #if MSDOS_COMPILER==WIN32C || MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
116 if (is_tty
&& any_display
)
119 if (ctldisp
!= OPT_ONPLUS
)
120 WIN32textout(obuf
, ob
- obuf
);
124 * Look for SGR escape sequences, and convert them
125 * to color commands. Replace bold, underline,
126 * and italic escapes into colors specified via
127 * the -D command-line option.
129 char *anchor
, *p
, *p_next
;
130 unsigned char fg
, bg
;
131 #if MSDOS_COMPILER==WIN32C
132 /* Screen colors used by 3x and 4x SGR commands. */
133 static unsigned char screen_color
[] = {
137 FOREGROUND_RED
|FOREGROUND_GREEN
,
139 FOREGROUND_BLUE
|FOREGROUND_RED
,
140 FOREGROUND_BLUE
|FOREGROUND_GREEN
,
141 FOREGROUND_BLUE
|FOREGROUND_GREEN
|FOREGROUND_RED
144 static enum COLORS screen_color
[] = {
145 BLACK
, RED
, GREEN
, BROWN
,
146 BLUE
, MAGENTA
, CYAN
, LIGHTGRAY
150 for (anchor
= p_next
= obuf
;
151 (p_next
= memchr(p_next
, ESC
, ob
- p_next
)) != NULL
; )
154 if (p
[1] == '[') /* "ESC-[" sequence */
159 * If some chars seen since
160 * the last escape sequence,
161 * write them out to the screen.
163 WIN32textout(anchor
, p
-anchor
);
166 p
+= 2; /* Skip the "ESC-[" */
170 * Handle null escape sequence
171 * "ESC[m", which restores
176 WIN32setcolors(nm_fg_color
, nm_bg_color
);
182 * Select foreground/background colors
183 * based on the escape sequence.
187 while (!is_ansi_end(*p
))
190 long code
= strtol(p
, &q
, 10);
195 * Incomplete sequence.
196 * Leave it unprocessed
199 int slop
= q
- anchor
;
200 /* {{ strcpy args overlap! }} */
201 strcpy(obuf
, anchor
);
207 code
> 49 || code
< 0 ||
208 (!is_ansi_end(*q
) && *q
!= ';'))
219 /* case 0: all attrs off */
220 /* case 22: bold off */
221 /* case 23: italic off */
222 /* case 24: underline off */
223 /* case 27: inverse off */
227 case 1: /* bold on */
231 case 3: /* italic on */
232 case 7: /* inverse on */
236 case 4: /* underline on */
240 case 5: /* slow blink on */
241 case 6: /* fast blink on */
245 case 8: /* concealed on */
248 case 30: case 31: case 32:
249 case 33: case 34: case 35:
251 fg
= (fg
& 8) | (screen_color
[code
- 30]);
253 case 39: /* default fg */
256 case 40: case 41: case 42:
257 case 43: case 44: case 45:
259 bg
= (bg
& 8) | (screen_color
[code
- 40]);
261 case 49: /* default fg */
267 if (!is_ansi_end(*p
) || p
== p_next
)
271 WIN32setcolors(fg
, bg
);
272 p_next
= anchor
= p
+ 1;
277 /* Output what's left in the buffer. */
278 WIN32textout(anchor
, ob
- anchor
);
285 fd
= (any_display
) ? 1 : 2;
286 if (write(fd
, obuf
, n
) != n
)
292 * Output a character.
298 #if 0 /* fake UTF-8 output for testing */
302 static char ubuf
[MAX_UTF_CHAR_LEN
];
303 static int ubuf_len
= 0;
304 static int ubuf_index
= 0;
307 ubuf_len
= utf_len(c
);
310 ubuf
[ubuf_index
++] = c
;
311 if (ubuf_index
< ubuf_len
)
313 c
= get_wchar(ubuf
) & 0xFF;
323 if (c
== '\n' && is_tty
)
330 if (c
== '\n' && is_tty
) /* In OS-9, '\n' == 0x0D */
335 * Some versions of flush() write to *ob, so we must flush
336 * when we are still one char from the end of obuf.
338 if (ob
>= &obuf
[sizeof(obuf
)-1])
358 * Convert an integral type to a string.
360 #define TYPE_TO_A_FUNC(funcname, type) \
361 void funcname(num, buf) \
365 int neg = (num < 0); \
366 char tbuf[INT_STRLEN_BOUND(num)+2]; \
367 register char *s = tbuf + sizeof(tbuf); \
368 if (neg) num = -num; \
371 *--s = (num % 10) + '0'; \
372 } while ((num /= 10) != 0); \
373 if (neg) *--s = '-'; \
377 TYPE_TO_A_FUNC(postoa
, POSITION
)
378 TYPE_TO_A_FUNC(linenumtoa
, LINENUM
)
379 TYPE_TO_A_FUNC(inttoa
, int)
382 * Output an integer in a given radix.
388 char buf
[INT_STRLEN_BOUND(num
)];
392 return (strlen(buf
));
396 * Output a line number in a given radix.
402 char buf
[INT_STRLEN_BOUND(num
)];
404 linenumtoa(num
, buf
);
406 return (strlen(buf
));
410 * This function implements printf-like functionality
411 * using a more portable argument list mechanism than printf's.
414 less_printf(fmt
, parg
)
443 col
+= iprint_int(parg
->p_int
);
447 col
+= iprint_linenum(parg
->p_linenum
);
458 * If some other non-trivial char is pressed, unget it, so it will
459 * become the next command.
467 while ((c
= getchr()) != '\n' && c
!= '\r')
471 if (c
!= '\n' && c
!= '\r' && c
!= ' ' && c
!= READ_INTR
)
477 * Output a message in the lower left corner of the screen
478 * and wait for carriage return.
486 static char return_to_continue
[] = " (press RETURN)";
490 if (any_display
&& is_tty
)
496 at_enter(AT_STANDOUT
);
500 col
+= less_printf(fmt
, parg
);
502 if (!(any_display
&& is_tty
))
508 putstr(return_to_continue
);
510 col
+= sizeof(return_to_continue
) + so_e_width
;
517 * Printing the message has probably scrolled the screen.
518 * {{ Unless the terminal doesn't have auto margins,
519 * in which case we just hammered on the right margin. }}
526 static char intr_to_abort
[] = "... (interrupt to abort)";
529 * Output a message in the lower left corner of the screen
530 * and don't wait for carriage return.
531 * Usually used to warn that we are beginning a potentially
532 * time-consuming operation.
541 at_enter(AT_STANDOUT
);
542 (void) less_printf(fmt
, parg
);
543 putstr(intr_to_abort
);
550 * Output a message in the lower left corner of the screen
551 * and return a single-character response.
561 if (any_display
&& is_tty
)
564 (void) less_printf(fmt
, parg
);
567 if (!(any_display
&& is_tty
))