1 /*
2 * Copyright (C) 1984-2026 Mark Nudelman
3 *
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.
6 *
7 * For more information, see the README file.
8 */
9
10
11 /*
12 * Routines which deal with the characteristics of the terminal.
13 * Uses termcap to be as terminal-independent as possible.
14 */
15
16 #include "less.h"
17 #include "cmd.h"
18
19 #if MSDOS_COMPILER
20 #include "pckeys.h"
21 #if MSDOS_COMPILER==MSOFTC
22 #include <graph.h>
23 #else
24 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
25 #include <conio.h>
26 #if MSDOS_COMPILER==DJGPPC
27 #include <pc.h>
28 extern int fd0;
29 #endif
30 #else
31 #if MSDOS_COMPILER==WIN32C
32 #include <windows.h>
33 #endif
34 #endif
35 #endif
36 #include <time.h>
37
38 #ifndef FOREGROUND_BLUE
39 #define FOREGROUND_BLUE 0x0001
40 #endif
41 #ifndef FOREGROUND_GREEN
42 #define FOREGROUND_GREEN 0x0002
43 #endif
44 #ifndef FOREGROUND_RED
45 #define FOREGROUND_RED 0x0004
46 #endif
47 #ifndef FOREGROUND_INTENSITY
48 #define FOREGROUND_INTENSITY 0x0008
49 #endif
50
51 #else
52
53 #if HAVE_SYS_IOCTL_H
54 #include <sys/ioctl.h>
55 #endif
56
57 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
58 #include <termios.h>
59 #else
60 #if HAVE_TERMIO_H
61 #include <termio.h>
62 #else
63 #if HAVE_SGSTAT_H
64 #include <sgstat.h>
65 #else
66 #include <sgtty.h>
67 #endif
68 #endif
69 #endif
70
71 #if HAVE_TERMINFO && !USE_TERMCAP
72 #define USE_TERMINFO 1
73 #endif
74
75 #if USE_TERMINFO
76 #include <curses.h>
77 #include <term.h>
78 #if HAVE_TPARM2
79 #define LTPARM2(s,p1,p2) tparm(s, p1, p2)
80 #else
81 #if HAVE_TPARM8
82 #define LTPARM2(s,p1,p2) tparm(s, p1, p2, 0, 0, 0, 0, 0, 0)
83 #else
84 #if HAVE_TPARM9
85 #define LTPARM2(s,p1,p2) tparm(s, p1, p2, 0, 0, 0, 0, 0, 0, 0)
86 #else
87 #endif
88 #endif
89 #endif
90 #else
91 #if HAVE_NCURSESW_TERMCAP_H
92 #include <ncursesw/termcap.h>
93 #else
94 #if HAVE_NCURSES_TERMCAP_H
95 #include <ncurses/termcap.h>
96 #else
97 #if HAVE_TERMCAP_H
98 #include <termcap.h>
99 #endif
100 #endif
101 #endif
102 #endif
103 #ifdef _OSK
104 #include <signal.h>
105 #endif
106 #if OS2
107 #include <sys/signal.h>
108 #include "pckeys.h"
109 #endif
110 #if HAVE_SYS_STREAM_H
111 #include <sys/stream.h>
112 #endif
113 #if HAVE_SYS_PTEM_H
114 #include <sys/ptem.h>
115 #endif
116
117 #endif /* MSDOS_COMPILER */
118
119 /*
120 * Check for broken termios package that forces you to manually
121 * set the line discipline.
122 */
123 #ifdef __ultrix__
124 #define MUST_SET_LINE_DISCIPLINE 1
125 #else
126 #define MUST_SET_LINE_DISCIPLINE 0
127 #endif
128
129 #if OS2
130 #define DEFAULT_TERM "ansi"
131 static char *windowid;
132 #else
133 #define DEFAULT_TERM "unknown"
134 #endif
135
136 #if MSDOS_COMPILER==MSOFTC
137 static int videopages;
138 static long msec_loops;
139 static int flash_created = 0;
140 #define SET_FG_COLOR(fg) _settextcolor(fg)
141 #define SET_BG_COLOR(bg) _setbkcolor(bg)
142 #define SETCOLORS(fg,bg) { SET_FG_COLOR(fg); SET_BG_COLOR(bg); }
143 #endif
144
145 #if MSDOS_COMPILER==BORLANDC
146 static unsigned short *whitescreen;
147 static int flash_created = 0;
148 #endif
149 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
150 #define _settextposition(y,x) gotoxy(x,y)
151 #define _clearscreen(m) clrscr()
152 #define _outtext(s) cputs(s)
153 #define SET_FG_COLOR(fg) textcolor(fg)
154 #define SET_BG_COLOR(bg) textbackground(bg)
155 #define SETCOLORS(fg,bg) { SET_FG_COLOR(fg); SET_BG_COLOR(bg); }
156 extern int sc_height;
157 #endif
158
159 #if MSDOS_COMPILER==WIN32C
160 #define UTF8_MAX_LENGTH 4
161
162 static WORD curr_attr;
163
164 static HANDLE con_out_save = INVALID_HANDLE_VALUE; /* previous console */
165 static HANDLE con_out_ours = INVALID_HANDLE_VALUE; /* our own */
166 HANDLE con_out = INVALID_HANDLE_VALUE; /* current console */
167
168 extern int utf_mode;
169 extern lbool quitting;
170 static void win32_init_term();
171 static void win32_deinit_term();
172
173 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
174 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 4
175 #endif
176
177 #define FG_COLORS (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
178 #define BG_COLORS (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
179 #define MAKEATTR(fg,bg) ((WORD)((fg)|((bg)<<4)))
180 #define APPLY_COLORS() { if (SetConsoleTextAttribute(con_out, curr_attr) == 0) \
181 error("SETCOLORS failed", NULL_PARG); }
182 #define SET_FG_COLOR(fg) { curr_attr &= ~0x0f; curr_attr |= (fg); APPLY_COLORS(); }
183 #define SET_BG_COLOR(bg) { curr_attr &= ~0xf0; curr_attr |= ((bg)<<4); APPLY_COLORS(); }
184 #define SETCOLORS(fg,bg) { curr_attr = MAKEATTR(fg,bg); APPLY_COLORS(); }
185 #endif
186
187 #if MSDOS_COMPILER
188 public int nm_fg_color = CV_ERROR; /* Color of normal text */
189 public int nm_bg_color = CV_ERROR;
190 public int nm_attr = 0;
191 public int bo_fg_color = CV_ERROR; /* Color of bold text */
192 public int bo_bg_color = CV_ERROR;
193 public int bo_attr = 0;
194 public int ul_fg_color = CV_ERROR; /* Color of underlined text */
195 public int ul_bg_color = CV_ERROR;
196 public int ul_attr = 0;
197 public int so_fg_color = CV_ERROR; /* Color of standout text */
198 public int so_bg_color = CV_ERROR;
199 public int so_attr = 0;
200 public int bl_fg_color = CV_ERROR; /* Color of blinking text */
201 public int bl_bg_color = CV_ERROR;
202 public int bl_attr = 0;
203 static int sy_fg_color; /* Color of system text (before less) */
204 static int sy_bg_color;
205 public int sgr_mode; /* Honor ANSI sequences rather than using above */
206 #if MSDOS_COMPILER==WIN32C
207 static DWORD init_console_output_mode;
208 extern DWORD init_console_input_mode;
209 extern DWORD curr_console_input_mode;
210 extern DWORD base_console_input_mode;
211 extern DWORD mouse_console_input_mode;
212 public int vt_enabled = -1; /* Is virtual terminal processing available? */
213 #endif
214 #else
215
216 /*
217 * These two variables are sometimes defined in,
218 * and needed by, the termcap library.
219 */
220 #if USE_TERMINFO
221 #undef HAVE_OSPEED
222 #else
223 #if MUST_DEFINE_OSPEED
224 extern short ospeed; /* Terminal output baud rate */
225 extern char PC; /* Pad character */
226 #endif
227 #ifdef _OSK
228 short ospeed;
229 char PC_, *UP, *BC;
230 #endif
231 #endif
232
233 /*
234 * Strings passed to tputs() to do various terminal functions.
235 */
236 static constant char
237 #if HAVE_OSPEED
238 *sc_pad, /* Pad string */
239 #endif
240 *sc_home, /* Cursor home */
241 *sc_addline, /* Add line, scroll down following lines */
242 *sc_lower_left, /* Cursor to last line, first column */
243 *sc_return, /* Cursor to beginning of current line */
244 *sc_move, /* General cursor positioning */
245 *sc_clear, /* Clear screen */
246 *sc_eol_clear, /* Clear to end of line */
247 *sc_eos_clear, /* Clear to end of screen */
248 *sc_s_in, /* Enter standout (highlighted) mode */
249 *sc_s_out, /* Exit standout mode */
250 *sc_u_in, /* Enter underline mode */
251 *sc_u_out, /* Exit underline mode */
252 *sc_b_in, /* Enter bold mode */
253 *sc_b_out, /* Exit bold mode */
254 *sc_bl_in, /* Enter blink mode */
255 *sc_bl_out, /* Exit blink mode */
256 *sc_visual_bell, /* Visual bell (flash screen) sequence */
257 *sc_backspace, /* Backspace cursor */
258 *sc_s_keypad, /* Start keypad mode */
259 *sc_e_keypad, /* End keypad mode */
260 *sc_s_mousecap, /* Start mouse capture mode */
261 *sc_e_mousecap, /* End mouse capture mode */
262 *sc_s_bracketed_paste, /* Start bracketed paste mode */
263 *sc_e_bracketed_paste, /* End bracketed paste mode */
264 *sc_suspend, /* Suspend screen updates */
265 *sc_resume, /* Resume screen updates */
266 *sc_init, /* Startup terminal initialization */
267 *sc_deinit; /* Exit terminal de-initialization */
268
269 /* attrbits is the current attribute mode of the terminal
270 * (AT_UNDERLINE, AT_BOLD, AT_STANDOUT, AT_BLINK).
271 * attrmode is the user-defined mode, which may include AT_COLOR values,
272 * and the mapping of the AT_COLOR value may include the 4 attribute bits. */
273 static int attrbits = 0;
274 static int attrcolor = -1;
275 #endif
276
277 /* term_init has been called; terminal is ready for use by less */
278 static lbool term_init_done = FALSE;
279 public lbool term_init_ever = FALSE;
280
281 public int auto_wrap; /* Terminal does \r\n when write past margin */
282 public int defer_wrap; /* After printing char in last column, doesn't wrap until next char */
283 public int erase_char; /* The user's erase char */
284 public int erase2_char; /* The user's other erase char */
285 public int kill_char; /* The user's line-kill char */
286 public int werase_char; /* The user's word-erase char */
287 public int sc_width, sc_height; /* Height & width of screen */
288 public int bo_s_width, bo_e_width; /* Printing width of boldface seq */
289 public int ul_s_width, ul_e_width; /* Printing width of underline seq */
290 public int so_s_width, so_e_width; /* Printing width of standout seq */
291 public int bl_s_width, bl_e_width; /* Printing width of blink seq */
292 public int above_mem, below_mem; /* Memory retained above/below screen */
293 public int can_goto_line; /* Can move cursor to any line */
294 public int clear_bg; /* Clear fills with background color */
295 public lbool missing_cap = FALSE; /* Some capability is missing */
296 public constant char *kent = NULL; /* Keypad ENTER sequence */
297 public lbool kent_mapped = FALSE; /* Keypad ENTER is mapped to a command */
298 public lbool term_addrs = FALSE; /* "ti" has been sent to terminal */
299 public lbool full_screen = TRUE; /* We're using all lines of terminal */
300
301 static int attrmode = AT_NORMAL; /* current attributes (AT_* bits) */
302 static int termcap_debug = -1;
303 static int no_alt_screen; /* sc_init does not switch to alt screen */
304 extern int binattr;
305 extern lbool one_screen;
306 extern int shell_lines;
307
308 #if !MSDOS_COMPILER
309 static constant char *cheaper(constant char *t1, constant char *t2, constant char *def);
310 static void tmodes(constant char *inti, constant char *outti, constant char *intc, constant char *outtc, constant char **instr,
311 constant char **outstr, constant char *def_instr, constant char *def_outstr, char **spp);
312 #endif
313
314 extern int quiet; /* If VERY_QUIET, use visual bell for bell */
315 extern int no_vbell;
316 extern lbool no_back_scroll;
317 extern int no_init;
318 extern int no_keypad;
319 extern int sigs;
320 extern int top_scroll;
321 extern int quit_if_one_screen;
322 extern int oldbot;
323 extern int emouse;
324 extern lbool is_tty;
325 extern int use_color;
326 extern int no_paste;
327 extern int wscroll;
328 #if HILITE_SEARCH
329 extern int hilite_search;
330 #endif
331 #if MSDOS_COMPILER==WIN32C
332 extern HANDLE tty;
333 #else
334 extern int tty;
335 #endif
336
337 #if (HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS) || defined(TCGETA)
338 /*
339 * Set termio flags for use by less.
340 */
set_termio_flags(struct termios * s)341 static void set_termio_flags(
342 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
343 struct termios *s
344 #else
345 struct termio *s
346 #endif
347 )
348 {
349 s->c_lflag &= ~(0
350 #ifdef ICANON
351 | ICANON
352 #endif
353 #ifdef ECHO
354 | ECHO
355 #endif
356 #ifdef ECHOE
357 | ECHOE
358 #endif
359 #ifdef ECHOK
360 | ECHOK
361 #endif
362 #ifdef ECHONL
363 | ECHONL
364 #endif
365 );
366
367 s->c_oflag |= (0
368 #ifdef OPOST
369 | OPOST
370 #endif
371 #ifdef ONLCR
372 | ONLCR
373 #endif
374 );
375
376 s->c_oflag &= ~(0
377 #ifdef ONOEOT
378 | ONOEOT
379 #endif
380 #ifdef OCRNL
381 | OCRNL
382 #endif
383 #ifdef ONOCR
384 | ONOCR
385 #endif
386 #ifdef ONLRET
387 | ONLRET
388 #endif
389 );
390
391 s->c_iflag &= ~(0
392 #ifdef ICRNL
393 | ICRNL
394 #endif
395 #ifdef INLCR
396 | INLCR
397 #endif
398 );
399 }
400 #endif
401
402 /*
403 * Change terminal to "raw mode", or restore to "normal" mode.
404 * "Raw mode" means
405 * 1. An outstanding read will complete on receipt of a single keystroke.
406 * 2. Input is not echoed.
407 * 3. On output, \n is mapped to \r\n.
408 * 4. \t is NOT expanded into spaces.
409 * 5. Signal-causing characters such as ctrl-C (interrupt),
410 * etc. are NOT disabled.
411 * 6. Input \r is not mapped to \n, nor vice versa.
412 */
raw_mode(int on)413 public void raw_mode(int on)
414 {
415 static int curr_on = 0;
416
417 if (on == curr_on)
418 return;
419 erase2_char = '\b'; /* in case OS doesn't know about erase2 */
420 #if LESSTEST
421 if (is_lesstest())
422 {
423 /* {{ For consistent conditions when running tests. }} */
424 erase_char = '\b';
425 kill_char = CONTROL('U');
426 werase_char = CONTROL('W');
427 } else
428 #endif /*LESSTEST*/
429 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
430 {
431 struct termios s;
432 static struct termios save_term;
433 static int saved_term = 0;
434
435 if (on)
436 {
437 /*
438 * Get terminal modes.
439 */
440 if (tcgetattr(tty, &s) < 0)
441 {
442 erase_char = '\b';
443 kill_char = CONTROL('U');
444 werase_char = CONTROL('W');
445 } else
446 {
447 /*
448 * Save modes and set certain variables dependent on modes.
449 */
450 if (!saved_term)
451 {
452 save_term = s;
453 saved_term = 1;
454 }
455 #if HAVE_OSPEED
456 switch (cfgetospeed(&s))
457 {
458 #ifdef B0
459 case B0: ospeed = 0; break;
460 #endif
461 #ifdef B50
462 case B50: ospeed = 1; break;
463 #endif
464 #ifdef B75
465 case B75: ospeed = 2; break;
466 #endif
467 #ifdef B110
468 case B110: ospeed = 3; break;
469 #endif
470 #ifdef B134
471 case B134: ospeed = 4; break;
472 #endif
473 #ifdef B150
474 case B150: ospeed = 5; break;
475 #endif
476 #ifdef B200
477 case B200: ospeed = 6; break;
478 #endif
479 #ifdef B300
480 case B300: ospeed = 7; break;
481 #endif
482 #ifdef B600
483 case B600: ospeed = 8; break;
484 #endif
485 #ifdef B1200
486 case B1200: ospeed = 9; break;
487 #endif
488 #ifdef B1800
489 case B1800: ospeed = 10; break;
490 #endif
491 #ifdef B2400
492 case B2400: ospeed = 11; break;
493 #endif
494 #ifdef B4800
495 case B4800: ospeed = 12; break;
496 #endif
497 #ifdef B9600
498 case B9600: ospeed = 13; break;
499 #endif
500 #ifdef EXTA
501 case EXTA: ospeed = 14; break;
502 #endif
503 #ifdef EXTB
504 case EXTB: ospeed = 15; break;
505 #endif
506 #ifdef B57600
507 case B57600: ospeed = 16; break;
508 #endif
509 #ifdef B115200
510 case B115200: ospeed = 17; break;
511 #endif
512 default: ;
513 }
514 #endif
515 erase_char = s.c_cc[VERASE];
516 #ifdef VERASE2
517 erase2_char = s.c_cc[VERASE2];
518 #endif
519 kill_char = s.c_cc[VKILL];
520 #ifdef VWERASE
521 werase_char = s.c_cc[VWERASE];
522 #else
523 werase_char = CONTROL('W');
524 #endif
525
526 /*
527 * Set the modes to the way we want them.
528 */
529 set_termio_flags(&s);
530 s.c_cc[VMIN] = 1;
531 s.c_cc[VTIME] = 0;
532 #ifdef VLNEXT
533 s.c_cc[VLNEXT] = 0;
534 #endif
535 #ifdef VDSUSP
536 s.c_cc[VDSUSP] = 0;
537 #endif
538 #ifdef VSTOP
539 s.c_cc[VSTOP] = 0;
540 #endif
541 #ifdef VSTART
542 s.c_cc[VSTART] = 0;
543 #endif
544 #ifdef VDISCARD
545 s.c_cc[VDISCARD] = 0;
546 #endif
547 #if MUST_SET_LINE_DISCIPLINE
548 /*
549 * System's termios is broken; need to explicitly
550 * request TERMIODISC line discipline.
551 */
552 s.c_line = TERMIODISC;
553 #endif
554 }
555 } else
556 {
557 /*
558 * Restore saved modes.
559 */
560 s = save_term;
561 }
562 #if HAVE_FSYNC
563 fsync(tty);
564 #endif
565 tcsetattr(tty, TCSADRAIN, &s);
566 #if MUST_SET_LINE_DISCIPLINE
567 if (!on)
568 {
569 /*
570 * Broken termios *ignores* any line discipline
571 * except TERMIODISC. A different old line discipline
572 * is therefore not restored, yet. Restore the old
573 * line discipline by hand.
574 */
575 ioctl(tty, TIOCSETD, &save_term.c_line);
576 }
577 #endif
578 }
579 #else
580 #ifdef TCGETA
581 {
582 struct termio s;
583 static struct termio save_term;
584 static int saved_term = 0;
585
586 if (on)
587 {
588 /*
589 * Get terminal modes.
590 */
591 ioctl(tty, TCGETA, &s);
592
593 /*
594 * Save modes and set certain variables dependent on modes.
595 */
596 if (!saved_term)
597 {
598 save_term = s;
599 saved_term = 1;
600 }
601 #if HAVE_OSPEED
602 ospeed = s.c_cflag & CBAUD;
603 #endif
604 erase_char = s.c_cc[VERASE];
605 kill_char = s.c_cc[VKILL];
606 #ifdef VWERASE
607 werase_char = s.c_cc[VWERASE];
608 #else
609 werase_char = CONTROL('W');
610 #endif
611
612 /*
613 * Set the modes to the way we want them.
614 */
615 set_termio_flags(&s);
616 s.c_cc[VMIN] = 1;
617 s.c_cc[VTIME] = 0;
618 #ifdef VSTOP
619 s.c_cc[VSTOP] = 0;
620 #endif
621 #ifdef VSTART
622 s.c_cc[VSTART] = 0;
623 #endif
624 } else
625 {
626 /*
627 * Restore saved modes.
628 */
629 s = save_term;
630 }
631 ioctl(tty, TCSETAW, &s);
632 }
633 #else
634 #ifdef TIOCGETP
635 {
636 struct sgttyb s;
637 static struct sgttyb save_term;
638 static int saved_term = 0;
639
640 if (on)
641 {
642 /*
643 * Get terminal modes.
644 */
645 ioctl(tty, TIOCGETP, &s);
646
647 /*
648 * Save modes and set certain variables dependent on modes.
649 */
650 if (!saved_term)
651 {
652 save_term = s;
653 saved_term = 1;
654 }
655 #if HAVE_OSPEED
656 ospeed = s.sg_ospeed;
657 #endif
658 erase_char = s.sg_erase;
659 kill_char = s.sg_kill;
660 werase_char = CONTROL('W');
661
662 /*
663 * Set the modes to the way we want them.
664 */
665 s.sg_flags |= CBREAK;
666 s.sg_flags &= ~(ECHO);
667 } else
668 {
669 /*
670 * Restore saved modes.
671 */
672 s = save_term;
673 }
674 ioctl(tty, TIOCSETN, &s);
675 }
676 #else
677 #ifdef _OSK
678 {
679 struct sgbuf s;
680 static struct sgbuf save_term;
681 static int saved_term = 0;
682
683 if (on)
684 {
685 /*
686 * Get terminal modes.
687 */
688 _gs_opt(tty, &s);
689
690 /*
691 * Save modes and set certain variables dependent on modes.
692 */
693 if (!saved_term)
694 {
695 save_term = s;
696 saved_term = 1;
697 }
698 erase_char = s.sg_bspch;
699 kill_char = s.sg_dlnch;
700 werase_char = CONTROL('W');
701
702 /*
703 * Set the modes to the way we want them.
704 */
705 s.sg_echo = 0;
706 s.sg_eofch = 0;
707 s.sg_pause = 0;
708 s.sg_psch = 0;
709 } else
710 {
711 /*
712 * Restore saved modes.
713 */
714 s = save_term;
715 }
716 _ss_opt(tty, &s);
717 }
718 #else
719 /* MS-DOS, Windows, or OS2 */
720 #if OS2
721 /* OS2 */
722 LSIGNAL(SIGINT, SIG_IGN);
723 #endif
724 erase_char = '\b';
725 #if MSDOS_COMPILER==DJGPPC
726 kill_char = CONTROL('U');
727 /*
728 * So that when we shell out or run another program, its
729 * stdin is in cooked mode. We do not switch stdin to binary
730 * mode if fd0 is zero, since that means we were called before
731 * tty was reopened in open_getchr, in which case we would be
732 * changing the original stdin device outside less.
733 */
734 if (fd0 != 0)
735 setmode(0, on ? O_BINARY : O_TEXT);
736 #else
737 kill_char = ESC;
738 #endif
739 werase_char = CONTROL('W');
740 #endif
741 #endif
742 #endif
743 #endif
744 curr_on = on;
745 }
746
747 #if !MSDOS_COMPILER
748
749 /*
750 * Some glue to prevent calling termcap functions if tgetent() failed.
751 */
752 static int hardcopy;
753
ltget_env(constant char * tiname,constant char * tcname)754 static constant char * ltget_env(constant char *tiname, constant char *tcname)
755 {
756 char envname[64];
757 constant char *s;
758
759 if (termcap_debug)
760 {
761 struct env { struct env *next; char *name; char *value; };
762 constant char *capname = tcname != NULL ? tcname : tiname;
763 static struct env *envs = NULL;
764 struct env *p;
765 for (p = envs; p != NULL; p = p->next)
766 if (strcmp(p->name, capname) == 0)
767 return p->value;
768 p = (struct env *) ecalloc(1, sizeof(struct env));
769 p->name = save(capname);
770 p->value = (char *) ecalloc(strlen(capname)+3, sizeof(char));
771 sprintf(p->value, "<%s>", capname);
772 p->next = envs;
773 envs = p;
774 return p->value;
775 }
776 if (tiname != NULL)
777 {
778 SNPRINTF1(envname, sizeof(envname), "LESS_TERMINFO_%s", tiname);
779 s = lgetenv(envname);
780 if (!isnullenv(s))
781 return s;
782 }
783 if (tcname != NULL)
784 {
785 SNPRINTF1(envname, sizeof(envname), "LESS_TERMCAP_%s", tcname);
786 s = lgetenv(envname);
787 if (!isnullenv(s))
788 return s;
789 }
790 return NULL;
791 }
792
ltgetflag(constant char * tiname,constant char * tcname)793 static int ltgetflag(constant char *tiname, constant char *tcname)
794 {
795 constant char *s;
796
797 if ((s = ltget_env(tiname, tcname)) != NULL)
798 return (*s != '\0' && *s != '0');
799 if (hardcopy)
800 return (0);
801 #if USE_TERMINFO
802 return tiname == NULL ? 0 : tigetflag(tiname);
803 #else
804 return tcname == NULL ? 0 : tgetflag(tcname);
805 #endif
806 }
807
ltgetnum(constant char * tiname,constant char * tcname)808 static int ltgetnum(constant char *tiname, constant char *tcname)
809 {
810 constant char *s;
811
812 if ((s = ltget_env(tiname, tcname)) != NULL)
813 return (atoi(s));
814 if (hardcopy)
815 return (-1);
816 #if USE_TERMINFO
817 return tiname == NULL ? -1 : tigetnum(tiname);
818 #else
819 return tcname == NULL ? -1 : tgetnum(tcname);
820 #endif
821 }
822
ltgetstr(constant char * tiname,constant char * tcname,char ** pp)823 static constant char * ltgetstr(constant char *tiname, constant char *tcname, char **pp)
824 {
825 constant char *s;
826
827 if ((s = ltget_env(tiname, tcname)) != NULL)
828 return (s);
829 if (hardcopy)
830 return (NULL);
831 #if USE_TERMINFO
832 if (tiname == NULL)
833 return (NULL);
834 s = tigetstr(tiname);
835 if (s == (constant char *)-1)
836 s = NULL;
837 return (s);
838 #else
839 if (tcname == NULL)
840 return (NULL);
841 return tgetstr(tcname, pp);
842 #endif
843 }
844 #endif /* MSDOS_COMPILER */
845
846 /*
847 * Get size of the output screen.
848 */
scrsize(void)849 public void scrsize(void)
850 {
851 constant char *s;
852 int sys_height;
853 int sys_width;
854 #if !MSDOS_COMPILER
855 int n;
856 #endif
857
858 #define DEF_SC_WIDTH 80
859 #if MSDOS_COMPILER
860 #define DEF_SC_HEIGHT 25
861 #else
862 #define DEF_SC_HEIGHT 24
863 #endif
864
865 sys_width = sys_height = 0;
866
867 #if LESSTEST
868 if (0) /* can't use is_lesstest(): ttyin_name may not be set by scan_option yet */
869 #endif /*LESSTEST*/
870 {
871 #if MSDOS_COMPILER==MSOFTC
872 {
873 struct videoconfig w;
874 _getvideoconfig(&w);
875 sys_height = w.numtextrows;
876 sys_width = w.numtextcols;
877 }
878 #else
879 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
880 {
881 struct text_info w;
882 gettextinfo(&w);
883 sys_height = w.screenheight;
884 sys_width = w.screenwidth;
885 }
886 #else
887 #if MSDOS_COMPILER==WIN32C
888 {
889 CONSOLE_SCREEN_BUFFER_INFO scr;
890 GetConsoleScreenBufferInfo(con_out, &scr);
891 sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1;
892 sys_width = scr.srWindow.Right - scr.srWindow.Left + 1;
893 }
894 #else
895 #if OS2
896 {
897 int s[2];
898 _scrsize(s);
899 sys_width = s[0];
900 sys_height = s[1];
901 /*
902 * When using terminal emulators for XFree86/OS2, the
903 * _scrsize function does not work well.
904 * Call the scrsize.exe program to get the window size.
905 */
906 windowid = getenv("WINDOWID");
907 if (windowid != NULL)
908 {
909 FILE *fd = popen("scrsize", "rt");
910 if (fd != NULL)
911 {
912 int w, h;
913 fscanf(fd, "%i %i", &w, &h);
914 if (w > 0 && h > 0)
915 {
916 sys_width = w;
917 sys_height = h;
918 }
919 pclose(fd);
920 }
921 }
922 }
923 #else
924 #ifdef TIOCGWINSZ
925 {
926 struct winsize w;
927 if (ioctl(2, TIOCGWINSZ, &w) == 0 || ioctl(1, TIOCGWINSZ, &w) == 0)
928 {
929 if (w.ws_row > 0)
930 sys_height = w.ws_row;
931 if (w.ws_col > 0)
932 sys_width = w.ws_col;
933 }
934 }
935 #else
936 #ifdef WIOCGETD
937 {
938 struct uwdata w;
939 if (ioctl(2, WIOCGETD, &w) == 0 || ioctl(1, WIOCGETD, &w) == 0)
940 {
941 if (w.uw_height > 0)
942 sys_height = w.uw_height / w.uw_vs;
943 if (w.uw_width > 0)
944 sys_width = w.uw_width / w.uw_hs;
945 }
946 }
947 #endif
948 #endif
949 #endif
950 #endif
951 #endif
952 #endif
953 }
954
955 if (sys_height > 0)
956 sc_height = sys_height;
957 else if ((s = lgetenv("LINES")) != NULL)
958 sc_height = atoi(s);
959 #if !MSDOS_COMPILER
960 else if ((n = ltgetnum("lines", "li")) > 0)
961 sc_height = n;
962 #endif
963 if ((s = lgetenv("LESS_LINES")) != NULL)
964 {
965 int height = atoi(s);
966 sc_height = (height < 0) ? sc_height + height : height;
967 full_screen = FALSE;
968 }
969 if (sc_height <= 0)
970 sc_height = DEF_SC_HEIGHT;
971
972 if (sys_width > 0)
973 sc_width = sys_width;
974 else if ((s = lgetenv("COLUMNS")) != NULL)
975 sc_width = atoi(s);
976 #if !MSDOS_COMPILER
977 else if ((n = ltgetnum("cols", "co")) > 0)
978 sc_width = n;
979 #endif
980 if ((s = lgetenv("LESS_COLUMNS")) != NULL)
981 {
982 int width = atoi(s);
983 sc_width = (width < 0) ? sc_width + width : width;
984 }
985 if (sc_width <= 0)
986 sc_width = DEF_SC_WIDTH;
987 screen_size_changed();
988 }
989
990 /*
991 * Recalculate things that depend on the screen size.
992 */
screen_size_changed(void)993 public void screen_size_changed(void)
994 {
995 constant char *env = lgetenv("LESS_SHELL_LINES");
996 shell_lines = isnullenv(env) ? 1 : atoi(env);
997 if (shell_lines >= sc_height)
998 shell_lines = sc_height - 1;
999 wscroll = (sc_height + 1) / 2;
1000 calc_jump_sline();
1001 calc_shift_count();
1002 calc_match_shift();
1003 }
1004
1005 #if MSDOS_COMPILER==MSOFTC
1006 /*
1007 * Figure out how many empty loops it takes to delay a millisecond.
1008 */
get_clock(void)1009 static void get_clock(void)
1010 {
1011 clock_t start;
1012
1013 /*
1014 * Get synchronized at the start of a tick.
1015 */
1016 start = clock();
1017 while (clock() == start)
1018 ;
1019 /*
1020 * Now count loops till the next tick.
1021 */
1022 start = clock();
1023 msec_loops = 0;
1024 while (clock() == start)
1025 msec_loops++;
1026 /*
1027 * Convert from (loops per clock) to (loops per millisecond).
1028 */
1029 msec_loops *= CLOCKS_PER_SEC;
1030 msec_loops /= 1000;
1031 }
1032
1033 /*
1034 * Delay for a specified number of milliseconds.
1035 */
delay(int msec)1036 static void delay(int msec)
1037 {
1038 long i;
1039
1040 while (msec-- > 0)
1041 {
1042 for (i = 0; i < msec_loops; i++)
1043 (void) clock();
1044 }
1045 }
1046 #endif
1047
1048 /*
1049 * Return the characters actually input by a "special" key.
1050 */
special_key_str(int key)1051 public constant char * special_key_str(int key)
1052 {
1053 static char tbuf[40];
1054 constant char *s;
1055 #if MSDOS_COMPILER || OS2
1056 static char k_right[] = { '\340', PCK_RIGHT, 0 };
1057 static char k_left[] = { '\340', PCK_LEFT, 0 };
1058 static char k_ctl_right[] = { '\340', PCK_CTL_RIGHT, 0 };
1059 static char k_ctl_left[] = { '\340', PCK_CTL_LEFT, 0 };
1060 static char k_shift_right[] = { '\340', PCK_SHIFT_RIGHT, 0 };
1061 static char k_shift_left[] = { '\340', PCK_SHIFT_LEFT, 0 };
1062 static char k_insert[] = { '\340', PCK_INSERT, 0 };
1063 static char k_delete[] = { '\340', PCK_DELETE, 0 };
1064 static char k_ctl_delete[] = { '\340', PCK_CTL_DELETE, 0 };
1065 static char k_ctl_backspace[] = { '\177', 0 };
1066 static char k_backspace[] = { '\b', 0 };
1067 static char k_home[] = { '\340', PCK_HOME, 0 };
1068 static char k_end[] = { '\340', PCK_END, 0 };
1069 static char k_up[] = { '\340', PCK_UP, 0 };
1070 static char k_down[] = { '\340', PCK_DOWN, 0 };
1071 static char k_backtab[] = { '\340', PCK_SHIFT_TAB, 0 };
1072 static char k_pagedown[] = { '\340', PCK_PAGEDOWN, 0 };
1073 static char k_pageup[] = { '\340', PCK_PAGEUP, 0 };
1074 static char k_ctl_home[] = { '\340', PCK_CTL_HOME, 0 };
1075 static char k_ctl_end[] = { '\340', PCK_CTL_END, 0 };
1076 static char k_shift_home[] = { '\340', PCK_SHIFT_HOME, 0 };
1077 static char k_shift_end[] = { '\340', PCK_SHIFT_END, 0 };
1078 static char k_f1[] = { '\340', PCK_F1, 0 };
1079 #endif
1080 #if !MSDOS_COMPILER
1081 char *sp = tbuf;
1082 #endif
1083
1084 switch (key)
1085 {
1086 #if OS2
1087 /*
1088 * If windowid is not NULL, assume less is executed in
1089 * the XFree86 environment.
1090 */
1091 case SK_RIGHT_ARROW:
1092 s = windowid ? ltgetstr("kcuf1", "kr", &sp) : k_right;
1093 break;
1094 case SK_LEFT_ARROW:
1095 s = windowid ? ltgetstr("kcub1", "kl", &sp) : k_left;
1096 break;
1097 case SK_UP_ARROW:
1098 s = windowid ? ltgetstr("kcuu1", "ku", &sp) : k_up;
1099 break;
1100 case SK_DOWN_ARROW:
1101 s = windowid ? ltgetstr("kcud1", "kd", &sp) : k_down;
1102 break;
1103 case SK_PAGE_UP:
1104 s = windowid ? ltgetstr("kpp", "kP", &sp) : k_pageup;
1105 break;
1106 case SK_PAGE_DOWN:
1107 s = windowid ? ltgetstr("knp", "kN", &sp) : k_pagedown;
1108 break;
1109 case SK_HOME:
1110 s = windowid ? ltgetstr("khome", "kh", &sp) : k_home;
1111 break;
1112 case SK_END:
1113 s = windowid ? ltgetstr("kend", "@7", &sp) : k_end;
1114 break;
1115 case SK_F1:
1116 s = windowid ? ltgetstr("kf1", "k1", &sp) : k_f1;
1117 break;
1118 case SK_DELETE:
1119 s = windowid ? ltgetstr("kdch1", "kD", &sp) : k_delete;
1120 if (s == NULL)
1121 {
1122 tbuf[0] = '\177';
1123 tbuf[1] = '\0';
1124 s = tbuf;
1125 }
1126 break;
1127 #endif
1128 #if MSDOS_COMPILER
1129 case SK_RIGHT_ARROW:
1130 s = k_right;
1131 break;
1132 case SK_LEFT_ARROW:
1133 s = k_left;
1134 break;
1135 case SK_UP_ARROW:
1136 s = k_up;
1137 break;
1138 case SK_DOWN_ARROW:
1139 s = k_down;
1140 break;
1141 case SK_PAGE_UP:
1142 s = k_pageup;
1143 break;
1144 case SK_PAGE_DOWN:
1145 s = k_pagedown;
1146 break;
1147 case SK_HOME:
1148 s = k_home;
1149 break;
1150 case SK_END:
1151 s = k_end;
1152 break;
1153 case SK_DELETE:
1154 s = k_delete;
1155 break;
1156 #endif
1157 #if MSDOS_COMPILER || OS2
1158 case SK_INSERT:
1159 s = k_insert;
1160 break;
1161 case SK_CTL_LEFT_ARROW:
1162 s = k_ctl_left;
1163 break;
1164 case SK_CTL_RIGHT_ARROW:
1165 s = k_ctl_right;
1166 break;
1167 case SK_SHIFT_LEFT_ARROW:
1168 s = k_shift_left;
1169 break;
1170 case SK_SHIFT_RIGHT_ARROW:
1171 s = k_shift_right;
1172 break;
1173 case SK_CTL_BACKSPACE:
1174 s = k_ctl_backspace;
1175 break;
1176 case SK_CTL_DELETE:
1177 s = k_ctl_delete;
1178 break;
1179 case SK_BACKSPACE:
1180 s = k_backspace;
1181 break;
1182 case SK_F1:
1183 s = k_f1;
1184 break;
1185 case SK_BACKTAB:
1186 s = k_backtab;
1187 break;
1188 case SK_SHIFT_HOME:
1189 s = k_shift_home;
1190 break;
1191 case SK_CTL_HOME:
1192 s = k_ctl_home;
1193 break;
1194 case SK_SHIFT_END:
1195 s = k_shift_end;
1196 break;
1197 case SK_CTL_END:
1198 s = k_ctl_end;
1199 break;
1200 #else
1201 case SK_RIGHT_ARROW:
1202 s = ltgetstr("kcuf1", "kr", &sp);
1203 break;
1204 case SK_LEFT_ARROW:
1205 s = ltgetstr("kcub1", "kl", &sp);
1206 break;
1207 case SK_UP_ARROW:
1208 s = ltgetstr("kcuu1", "ku", &sp);
1209 break;
1210 case SK_DOWN_ARROW:
1211 s = ltgetstr("kcud1", "kd", &sp);
1212 break;
1213 case SK_PAGE_UP:
1214 s = ltgetstr("kpp", "kP", &sp);
1215 break;
1216 case SK_PAGE_DOWN:
1217 s = ltgetstr("knp", "kN", &sp);
1218 break;
1219 case SK_HOME:
1220 s = ltgetstr("khome", "kh", &sp);
1221 break;
1222 case SK_SHIFT_HOME:
1223 s = ltgetstr("kHOM", "#2", &sp);
1224 break;
1225 case SK_CTL_HOME:
1226 s = ltgetstr("kHOM5", NULL, &sp);
1227 break;
1228 case SK_END:
1229 s = ltgetstr("kend", "@7", &sp);
1230 break;
1231 case SK_SHIFT_END:
1232 s = ltgetstr("kEND", "#7", &sp);
1233 break;
1234 case SK_CTL_END:
1235 s = ltgetstr("kEND5", NULL, &sp);
1236 break;
1237 case SK_INSERT:
1238 s = ltgetstr("kich1", "kI", &sp);
1239 break;
1240 case SK_BACKTAB:
1241 s = ltgetstr("kcbt", "kB", &sp);
1242 break;
1243 case SK_SHIFT_RIGHT_ARROW:
1244 s = ltgetstr("kRIT", NULL, &sp);
1245 break;
1246 case SK_SHIFT_LEFT_ARROW:
1247 s = ltgetstr("kLFT", NULL, &sp);
1248 break;
1249 case SK_CTL_RIGHT_ARROW:
1250 s = ltgetstr("kRIT5", NULL, &sp);
1251 break;
1252 case SK_CTL_LEFT_ARROW:
1253 s = ltgetstr("kLFT5", NULL, &sp);
1254 break;
1255 case SK_F1:
1256 s = ltgetstr("kf1", "k1", &sp);
1257 break;
1258 case SK_PAD_UL:
1259 s = ltgetstr("ka1", "K1", &sp);
1260 break;
1261 case SK_PAD_U:
1262 s = ltgetstr("ka2", NULL, &sp);
1263 break;
1264 case SK_PAD_UR:
1265 s = ltgetstr("ka3", "K3", &sp);
1266 break;
1267 case SK_PAD_L:
1268 s = ltgetstr("kb1", NULL, &sp);
1269 break;
1270 case SK_PAD_CENTER:
1271 s = ltgetstr("kb2", "K2", &sp);
1272 break;
1273 case SK_PAD_R:
1274 s = ltgetstr("kb3", NULL, &sp);
1275 break;
1276 case SK_PAD_DL:
1277 s = ltgetstr("kc1", "K4", &sp);
1278 break;
1279 case SK_PAD_D:
1280 s = ltgetstr("kc2", NULL, &sp);
1281 break;
1282 case SK_PAD_DR:
1283 s = ltgetstr("kc3", "K5", &sp);
1284 break;
1285 case SK_PAD_STAR:
1286 s = ltgetstr("kpMUL", NULL, &sp);
1287 break;
1288 case SK_PAD_SLASH:
1289 s = ltgetstr("kpDIV", NULL, &sp);
1290 break;
1291 case SK_PAD_DASH:
1292 s = ltgetstr("kpSUB", NULL, &sp);
1293 break;
1294 case SK_PAD_PLUS:
1295 s = ltgetstr("kpADD", NULL, &sp);
1296 break;
1297 case SK_PAD_DOT:
1298 s = ltgetstr("kpDOT", NULL, &sp);
1299 break;
1300 case SK_PAD_COMMA:
1301 s = ltgetstr("kpCMA", NULL, &sp);
1302 break;
1303 case SK_PAD_ZERO:
1304 s = ltgetstr("kpZRO", NULL, &sp);
1305 break;
1306 case SK_PAD_ENTER:
1307 s = kent;
1308 kent_mapped = TRUE;
1309 break;
1310 case SK_DELETE:
1311 s = ltgetstr("kdch1", "kD", &sp);
1312 if (s == NULL)
1313 {
1314 tbuf[0] = '\177';
1315 tbuf[1] = '\0';
1316 s = tbuf;
1317 }
1318 break;
1319 case SK_BACKSPACE:
1320 s = ltgetstr("kbs", "kb", &sp);
1321 if (s == NULL)
1322 {
1323 tbuf[0] = '\b';
1324 tbuf[1] = '\0';
1325 s = tbuf;
1326 }
1327 break;
1328 #endif
1329 case SK_CONTROL_K:
1330 tbuf[0] = CONTROL('K');
1331 tbuf[1] = '\0';
1332 s = tbuf;
1333 break;
1334 default:
1335 return (NULL);
1336 }
1337 if (s != NULL && *s == '\0')
1338 s = NULL;
1339 return (s);
1340 }
1341
1342 #if !MSDOS_COMPILER
ltgoto(constant char * cap,int col,int line)1343 static constant char *ltgoto(constant char *cap, int col, int line)
1344 {
1345 #if USE_TERMINFO
1346 return LTPARM2(cap, line, col);
1347 #else
1348 return tgoto(cap, col, line);
1349 #endif
1350 }
1351 #endif /* MSDOS_COMPILER */
1352
1353 #if MSDOS_COMPILER
init_win_colors(void)1354 public void init_win_colors(void)
1355 {
1356 if (nm_fg_color == CV_ERROR || nm_fg_color == CV_NOCHANGE) nm_fg_color = sy_fg_color;
1357 if (nm_bg_color == CV_ERROR || nm_bg_color == CV_NOCHANGE) nm_bg_color = sy_bg_color;
1358 if (bo_fg_color == CV_NOCHANGE) bo_fg_color = sy_fg_color; else if (bo_fg_color == CV_ERROR) bo_fg_color = sy_fg_color | 8;
1359 if (bo_bg_color == CV_NOCHANGE) bo_bg_color = sy_bg_color; else if (bo_bg_color == CV_ERROR) bo_bg_color = sy_bg_color;
1360 if (ul_fg_color == CV_NOCHANGE) ul_fg_color = sy_fg_color; else if (ul_fg_color == CV_ERROR) ul_fg_color = (sy_bg_color == 3 || sy_bg_color == 11) ? 0 : 11;
1361 if (ul_bg_color == CV_NOCHANGE) ul_bg_color = sy_bg_color; else if (ul_bg_color == CV_ERROR) ul_bg_color = sy_bg_color;
1362 if (so_fg_color == CV_NOCHANGE) so_fg_color = sy_fg_color; else if (so_fg_color == CV_ERROR) so_fg_color = sy_bg_color;
1363 if (so_bg_color == CV_NOCHANGE) so_bg_color = sy_bg_color; else if (so_bg_color == CV_ERROR) so_bg_color = sy_fg_color;
1364 if (bl_fg_color == CV_NOCHANGE) bl_fg_color = sy_fg_color; else if (bl_fg_color == CV_ERROR) bl_fg_color = ul_bg_color;
1365 if (bl_bg_color == CV_NOCHANGE) bl_bg_color = sy_bg_color; else if (bl_bg_color == CV_ERROR) bl_bg_color = ul_fg_color;
1366 nm_fg_color |= nm_attr;
1367 bo_fg_color |= bo_attr;
1368 ul_fg_color |= ul_attr;
1369 so_fg_color |= so_attr;
1370 bl_fg_color |= bl_attr;
1371 }
1372 #endif /* MSDOS_COMPILER */
1373
1374 /*
1375 * Get terminal capabilities via termcap.
1376 */
get_term(void)1377 public void get_term(void)
1378 {
1379 termcap_debug = !isnullenv(lgetenv("LESS_TERMCAP_DEBUG"));
1380 #if MSDOS_COMPILER
1381 auto_wrap = 1;
1382 defer_wrap = 0;
1383 can_goto_line = 1;
1384 clear_bg = 1;
1385 /*
1386 * Set up default colors.
1387 * The xx_s_width and xx_e_width vars are already initialized to 0.
1388 */
1389 #if MSDOS_COMPILER==MSOFTC
1390 sy_bg_color = _getbkcolor();
1391 sy_fg_color = _gettextcolor();
1392 get_clock();
1393 #else
1394 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1395 {
1396 struct text_info w;
1397 gettextinfo(&w);
1398 sy_bg_color = (w.attribute >> 4) & 0x0F;
1399 sy_fg_color = (w.attribute >> 0) & 0x0F;
1400 }
1401 #else
1402 #if MSDOS_COMPILER==WIN32C
1403 {
1404 CONSOLE_SCREEN_BUFFER_INFO scr;
1405
1406 con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE);
1407 /*
1408 * Always open stdin in binary. Note this *must* be done
1409 * before any file operations have been done on fd0.
1410 */
1411 SET_BINARY(0);
1412 GetConsoleMode(con_out, &init_console_output_mode);
1413 GetConsoleScreenBufferInfo(con_out, &scr);
1414 curr_attr = scr.wAttributes;
1415 sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */
1416 sy_fg_color = curr_attr & FG_COLORS;
1417 }
1418 #endif
1419 #endif
1420 #endif
1421 init_win_colors();
1422
1423 /*
1424 * Get size of the screen.
1425 */
1426 scrsize();
1427 pos_init();
1428
1429 #else /* !MSDOS_COMPILER */
1430 {
1431 constant char *t1;
1432 constant char *t2;
1433 constant char *term;
1434 char *sp;
1435 /*
1436 * Some termcap libraries assume termbuf is static
1437 * (accessible after tgetent returns).
1438 */
1439 #if !USE_TERMINFO
1440 static char termbuf[TERMBUF_SIZE];
1441 #endif
1442 static char sbuf[TERMSBUF_SIZE];
1443
1444 #if OS2
1445 /*
1446 * Make sure the termcap database is available.
1447 */
1448 constant char *cp = lgetenv("TERMCAP");
1449 if (isnullenv(cp))
1450 {
1451 char *termcap;
1452 if ((sp = homefile("termcap.dat")) != NULL)
1453 {
1454 termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char));
1455 sprintf(termcap, "TERMCAP=%s", sp);
1456 free(sp);
1457 putenv(termcap);
1458 }
1459 }
1460 #endif
1461 /*
1462 * Find out what kind of terminal this is.
1463 */
1464 if ((term = lgetenv("TERM")) == NULL && (term = getenv("TERM")) == NULL)
1465 term = DEFAULT_TERM;
1466 hardcopy = 0;
1467 #if USE_TERMINFO
1468 if (cur_term != NULL)
1469 del_curterm(cur_term);
1470 if (setupterm(term, -1, NULL) != OK)
1471 hardcopy = 1;
1472 #else
1473 if (tgetent(termbuf, term) != TGETENT_OK)
1474 hardcopy = 1;
1475 #endif
1476 if (!hardcopy && ltgetflag("hc", "hc"))
1477 hardcopy = 1;
1478
1479 /*
1480 * Get size of the screen.
1481 */
1482 scrsize();
1483 pos_init();
1484
1485 auto_wrap = ltgetflag("am", "am");
1486 defer_wrap = ltgetflag("xenl", "xn");
1487 above_mem = ltgetflag("da", "da");
1488 below_mem = ltgetflag("db", "db");
1489 clear_bg = ltgetflag("bce", "ut");
1490 no_alt_screen = ltgetflag("nrrmc", "NR");
1491
1492 /*
1493 * Assumes termcap variable "sg" is the printing width of:
1494 * the standout sequence, the end standout sequence,
1495 * the underline sequence, the end underline sequence,
1496 * the boldface sequence, and the end boldface sequence.
1497 */
1498 if ((so_s_width = ltgetnum("xmc", "sg")) < 0)
1499 so_s_width = 0;
1500 so_e_width = so_s_width;
1501
1502 bo_s_width = bo_e_width = so_s_width;
1503 ul_s_width = ul_e_width = so_s_width;
1504 bl_s_width = bl_e_width = so_s_width;
1505
1506 #if HILITE_SEARCH
1507 if (so_s_width > 0 || so_e_width > 0)
1508 /*
1509 * Disable highlighting by default on magic cookie terminals.
1510 * Turning on highlighting might change the displayed width
1511 * of a line, causing the display to get messed up.
1512 * The user can turn it back on with -g,
1513 * but she won't like the results.
1514 */
1515 hilite_search = 0;
1516 #endif
1517
1518 /*
1519 * Get various string-valued capabilities.
1520 */
1521 sp = sbuf;
1522
1523 #if HAVE_OSPEED
1524 sc_pad = ltgetstr("pad", "pc", &sp);
1525 if (sc_pad != NULL)
1526 PC = *sc_pad;
1527 #endif
1528
1529 sc_s_keypad = ltgetstr("smkx", "ks", &sp);
1530 if (sc_s_keypad == NULL)
1531 sc_s_keypad = "";
1532 sc_e_keypad = ltgetstr("rmkx", "ke", &sp);
1533 if (sc_e_keypad == NULL)
1534 sc_e_keypad = "";
1535 kent = ltgetstr("kent", "@8", &sp);
1536
1537 sc_s_mousecap = ltgetstr("MOUSE_START", "MOUSE_START", &sp);
1538 if (sc_s_mousecap == NULL)
1539 sc_s_mousecap = ESCS "[?1000h" ESCS "[?1002h" ESCS "[?1006h";
1540 sc_e_mousecap = ltgetstr("MOUSE_END", "MOUSE_END", &sp);
1541 if (sc_e_mousecap == NULL)
1542 sc_e_mousecap = ESCS "[?1006l" ESCS "[?1002l" ESCS "[?1000l";
1543
1544 sc_s_bracketed_paste = ltgetstr("BRACKETED_PASTE_START", "BRACKETED_PASTE_START", &sp);
1545 if (sc_s_bracketed_paste == NULL)
1546 sc_s_bracketed_paste = ESCS"[?2004h";
1547 sc_e_bracketed_paste = ltgetstr("BRACKETED_PASTE_END", "BRACKETED_PASTE_END", &sp);
1548 if (sc_e_bracketed_paste == NULL)
1549 sc_e_bracketed_paste = ESCS"[?2004l";
1550
1551 sc_suspend = ltgetstr("SUSPEND", "SUSPEND", &sp);
1552 if (sc_suspend == NULL)
1553 sc_suspend = "";
1554 sc_resume = ltgetstr("RESUME", "RESUME", &sp);
1555 if (sc_resume == NULL)
1556 sc_resume = "";
1557
1558 sc_init = ltgetstr("smcup", "ti", &sp);
1559 if (sc_init == NULL)
1560 sc_init = "";
1561
1562 sc_deinit= ltgetstr("rmcup", "te", &sp);
1563 if (sc_deinit == NULL)
1564 sc_deinit = "";
1565
1566 sc_eol_clear = ltgetstr("el", "ce", &sp);
1567 if (sc_eol_clear == NULL || *sc_eol_clear == '\0')
1568 {
1569 missing_cap = TRUE;
1570 sc_eol_clear = "";
1571 }
1572
1573 sc_eos_clear = ltgetstr("ed", "cd", &sp);
1574 if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0'))
1575 {
1576 missing_cap = TRUE;
1577 sc_eos_clear = "";
1578 }
1579
1580 sc_clear = ltgetstr("clear", "cl", &sp);
1581 if (sc_clear == NULL || *sc_clear == '\0')
1582 {
1583 missing_cap = TRUE;
1584 sc_clear = "\n\n";
1585 }
1586
1587 sc_move = ltgetstr("cup", "cm", &sp);
1588 if (sc_move == NULL || *sc_move == '\0')
1589 {
1590 /*
1591 * This is not an error here, because we don't
1592 * always need sc_move.
1593 * We need it only if we don't have home or lower-left.
1594 */
1595 sc_move = "";
1596 can_goto_line = 0;
1597 } else
1598 can_goto_line = 1;
1599
1600 tmodes("smso", "rmso", "so", "se", &sc_s_in, &sc_s_out, "", "", &sp);
1601 tmodes("smul", "rmul", "us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp);
1602 tmodes("bold", "sgr0", "md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp);
1603 tmodes("blink", "sgr0", "mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp);
1604
1605 sc_visual_bell = ltgetstr("flash", "vb", &sp);
1606 if (sc_visual_bell == NULL)
1607 sc_visual_bell = "";
1608
1609 if (ltgetflag(NULL, "bs"))
1610 sc_backspace = "\b";
1611 else
1612 {
1613 sc_backspace = ltgetstr("OTbc", "bc", &sp);
1614 if (sc_backspace == NULL || *sc_backspace == '\0')
1615 sc_backspace = "\b";
1616 }
1617
1618 /*
1619 * Choose between using "ho" and "cm" ("home" and "cursor move")
1620 * to move the cursor to the upper left corner of the screen.
1621 */
1622 t1 = ltgetstr("home", "ho", &sp);
1623 if (t1 == NULL)
1624 t1 = "";
1625 if (*sc_move == '\0')
1626 t2 = "";
1627 else
1628 {
1629 strcpy(sp, ltgoto(sc_move, 0, 0));
1630 t2 = sp;
1631 sp += strlen(sp) + 1;
1632 }
1633 sc_home = cheaper(t1, t2, "|\b^");
1634
1635 /*
1636 * Choose between using "ll" and "cm" ("lower left" and "cursor move")
1637 * to move the cursor to the lower left corner of the screen.
1638 */
1639 t1 = ltgetstr("ll", "ll", &sp);
1640 if (t1 == NULL || !full_screen)
1641 t1 = "";
1642 if (*sc_move == '\0')
1643 t2 = "";
1644 else
1645 {
1646 strcpy(sp, ltgoto(sc_move, 0, sc_height-1));
1647 t2 = sp;
1648 sp += strlen(sp) + 1;
1649 }
1650 sc_lower_left = cheaper(t1, t2, "\r");
1651
1652 /*
1653 * Get carriage return string.
1654 */
1655 sc_return = ltgetstr("cr", "cr", &sp);
1656 if (sc_return == NULL)
1657 sc_return = "\r";
1658
1659 /*
1660 * Choose between using "al" or "sr" ("add line" or "scroll reverse")
1661 * to add a line at the top of the screen.
1662 */
1663 t1 = ltgetstr("ill", "al", &sp);
1664 if (t1 == NULL)
1665 t1 = "";
1666 t2 = ltgetstr("ri", "sr", &sp);
1667 if (t2 == NULL)
1668 t2 = "";
1669 if (*t1 == '\0' && *t2 == '\0')
1670 sc_addline = "";
1671 else if (above_mem)
1672 sc_addline = t1;
1673 else
1674 sc_addline = cheaper(t1, t2, "");
1675 if (*sc_addline == '\0')
1676 {
1677 /*
1678 * Force repaint on any backward movement.
1679 */
1680 no_back_scroll = TRUE;
1681 }
1682 }
1683 #endif /* MSDOS_COMPILER */
1684 }
1685
1686 #if !MSDOS_COMPILER
1687 /*
1688 * Return the cost of displaying a termcap string.
1689 * We use the trick of calling tputs, but as a char printing function
1690 * we give it inc_costcount, which just increments "costcount".
1691 * This tells us how many chars would be printed by using this string.
1692 */
1693 static int costcount;
1694
1695 /*ARGSUSED*/
inc_costcount(int c)1696 static int inc_costcount(int c)
1697 {
1698 costcount++;
1699 return (c);
1700 }
1701
cost(constant char * t)1702 static int cost(constant char *t)
1703 {
1704 costcount = 0;
1705 tputs(t, sc_height, inc_costcount);
1706 return (costcount);
1707 }
1708
1709 /*
1710 * Return the "best" of the two given termcap strings.
1711 * The best, if both exist, is the one with the lower
1712 * cost (see cost() function).
1713 */
cheaper(constant char * t1,constant char * t2,constant char * def)1714 static constant char * cheaper(constant char *t1, constant char *t2, constant char *def)
1715 {
1716 if (*t1 == '\0' && *t2 == '\0')
1717 {
1718 missing_cap = TRUE;
1719 return (def);
1720 }
1721 if (*t1 == '\0')
1722 return (t2);
1723 if (*t2 == '\0')
1724 return (t1);
1725 if (cost(t1) < cost(t2))
1726 return (t1);
1727 return (t2);
1728 }
1729
tmodes(constant char * inti,constant char * outti,constant char * intc,constant char * outtc,constant char ** instr,constant char ** outstr,constant char * def_instr,constant char * def_outstr,char ** spp)1730 static void tmodes(constant char *inti, constant char *outti, constant char *intc, constant char *outtc, constant char **instr, constant char **outstr, constant char *def_instr, constant char *def_outstr, char **spp)
1731 {
1732 *instr = ltgetstr(inti, intc, spp);
1733 if (*instr == NULL)
1734 {
1735 /* Use defaults. */
1736 *instr = def_instr;
1737 *outstr = def_outstr;
1738 return;
1739 }
1740
1741 *outstr = ltgetstr(outti, outtc, spp);
1742 if (*outstr == NULL)
1743 /* No specific out capability; use "me". */
1744 *outstr = ltgetstr("sgr0", "me", spp);
1745 if (*outstr == NULL)
1746 /* Don't even have "me"; use a null string. */
1747 *outstr = "";
1748 }
1749
1750 #endif /* MSDOS_COMPILER */
1751
1752
1753 /*
1754 * Below are the functions which perform all the
1755 * terminal-specific screen manipulation.
1756 */
1757
1758
1759 #if MSDOS_COMPILER
1760
1761 #if MSDOS_COMPILER==WIN32C
_settextposition(int row,int col)1762 static void _settextposition(int row, int col)
1763 {
1764 COORD cpos;
1765 CONSOLE_SCREEN_BUFFER_INFO csbi;
1766
1767 GetConsoleScreenBufferInfo(con_out, &csbi);
1768 cpos.X = csbi.srWindow.Left + (col - 1);
1769 cpos.Y = csbi.srWindow.Top + (row - 1);
1770 SetConsoleCursorPosition(con_out, cpos);
1771 }
1772 #endif
1773
1774 /*
1775 * Initialize the screen to the correct color at startup.
1776 */
initcolor(void)1777 static void initcolor(void)
1778 {
1779 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1780 intensevideo();
1781 #endif
1782 SETCOLORS(nm_fg_color, nm_bg_color);
1783 #if 0
1784 /*
1785 * This clears the screen at startup. This is different from
1786 * the behavior of other versions of less. Disable it for now.
1787 */
1788 char *blanks;
1789 int row;
1790 int col;
1791
1792 /*
1793 * Create a complete, blank screen using "normal" colors.
1794 */
1795 SETCOLORS(nm_fg_color, nm_bg_color);
1796 blanks = (char *) ecalloc(width+1, sizeof(char));
1797 for (col = 0; col < sc_width; col++)
1798 blanks[col] = ' ';
1799 blanks[sc_width] = '\0';
1800 for (row = 0; row < sc_height; row++)
1801 _outtext(blanks);
1802 free(blanks);
1803 #endif
1804 }
1805 #endif
1806
1807 #if MSDOS_COMPILER==WIN32C
1808
1809 /*
1810 * Enable virtual terminal processing, if available.
1811 */
win32_init_vt_term(void)1812 static void win32_init_vt_term(void)
1813 {
1814 if (vt_enabled == 0 || (vt_enabled == 1 && con_out == con_out_ours))
1815 return; /* already initialized */
1816
1817 /* don't care about the initial mode, and win VT hard-enables am+xn */
1818 vt_enabled = SetConsoleMode(con_out, ENABLE_PROCESSED_OUTPUT |
1819 ENABLE_VIRTUAL_TERMINAL_PROCESSING |
1820 ENABLE_WRAP_AT_EOL_OUTPUT);
1821 if (vt_enabled)
1822 {
1823 auto_wrap = 1;
1824 defer_wrap = 1;
1825 }
1826 }
1827
win32_deinit_vt_term(void)1828 static void win32_deinit_vt_term(void)
1829 {
1830 if (vt_enabled == 1 && con_out == con_out_save)
1831 SetConsoleMode(con_out, init_console_output_mode);
1832 }
1833
1834 /*
1835 * Termcap-like init with a private win32 console.
1836 */
win32_init_term(void)1837 static void win32_init_term(void)
1838 {
1839 CONSOLE_SCREEN_BUFFER_INFO scr;
1840 COORD size;
1841
1842 if (con_out_save == INVALID_HANDLE_VALUE)
1843 return;
1844
1845 GetConsoleScreenBufferInfo(con_out_save, &scr);
1846
1847 if (con_out_ours == INVALID_HANDLE_VALUE)
1848 {
1849 /*
1850 * Create our own screen buffer, so that we
1851 * may restore the original when done.
1852 */
1853 con_out_ours = CreateConsoleScreenBuffer(
1854 GENERIC_WRITE | GENERIC_READ,
1855 FILE_SHARE_WRITE | FILE_SHARE_READ,
1856 (LPSECURITY_ATTRIBUTES) NULL,
1857 CONSOLE_TEXTMODE_BUFFER,
1858 (LPVOID) NULL);
1859
1860 /*
1861 * We don't care about the initial state. We need processed
1862 * output without anything else (no wrap at EOL, no VT,
1863 * no disabled auto-return).
1864 */
1865 if (SetConsoleMode(con_out_ours, ENABLE_PROCESSED_OUTPUT))
1866 auto_wrap = 0;
1867 }
1868
1869 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1870 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1871 SetConsoleScreenBufferSize(con_out_ours, size);
1872 SetConsoleActiveScreenBuffer(con_out_ours);
1873 con_out = con_out_ours;
1874 }
1875
1876 /*
1877 * Restore the startup console.
1878 */
win32_deinit_term(void)1879 static void win32_deinit_term(void)
1880 {
1881 if (con_out_save == INVALID_HANDLE_VALUE)
1882 return;
1883 if (quitting)
1884 (void) CloseHandle(con_out_ours);
1885 SetConsoleActiveScreenBuffer(con_out_save);
1886 con_out = con_out_save;
1887 }
1888
1889 #endif
1890
1891 #if !MSDOS_COMPILER
do_tputs(constant char * str,int affcnt,int (* f_putc)(int))1892 static void do_tputs(constant char *str, int affcnt, int (*f_putc)(int))
1893 {
1894 #if LESSTEST
1895 if (is_lesstest() && f_putc == putchr)
1896 putstr(str);
1897 else
1898 #endif /*LESSTEST*/
1899 tputs(str, affcnt, f_putc);
1900 }
1901
1902 /*
1903 * Like tputs but we handle $<...> delay strings here because
1904 * some implementations of tputs don't perform delays correctly.
1905 */
ltputs(constant char * str,int affcnt,int (* f_putc)(int))1906 static void ltputs(constant char *str, int affcnt, int (*f_putc)(int))
1907 {
1908 while (str != NULL && *str != '\0')
1909 {
1910 constant char *obrac = strstr(str, "$<");
1911 if (obrac != NULL)
1912 {
1913 char str2[64];
1914 size_t slen = ptr_diff(obrac, str);
1915 if (slen < sizeof(str2))
1916 {
1917 int delay;
1918 /* Output first part of string (before "$<"). */
1919 memcpy(str2, str, slen);
1920 str2[slen] = '\0';
1921 do_tputs(str2, affcnt, f_putc);
1922 str += slen + 2;
1923 /* Perform the delay. */
1924 delay = lstrtoic(str, &str, 10);
1925 if (*str == '*')
1926 if (ckd_mul(&delay, delay, affcnt))
1927 delay = INT_MAX;
1928 flush();
1929 sleep_ms(delay);
1930 /* Skip past closing ">" at end of delay string. */
1931 str = strstr(str, ">");
1932 if (str != NULL)
1933 str++;
1934 continue;
1935 }
1936 }
1937 /* Pass the rest of the string to tputs and we're done. */
1938 do_tputs(str, affcnt, f_putc);
1939 break;
1940 }
1941 }
1942 #endif /* MSDOS_COMPILER */
1943
1944 /*
1945 * Configure the terminal so mouse clicks and wheel moves
1946 * produce input to less.
1947 */
init_mouse(void)1948 public void init_mouse(void)
1949 {
1950 #if !MSDOS_COMPILER
1951 ltputs(sc_s_mousecap, sc_height, putchr);
1952 #else
1953 #if MSDOS_COMPILER==WIN32C
1954 curr_console_input_mode = mouse_console_input_mode;
1955 SetConsoleMode(tty, curr_console_input_mode);
1956 #endif
1957 #endif
1958 }
1959
1960 /*
1961 * Configure the terminal so mouse clicks and wheel moves
1962 * are handled by the system (so text can be selected, etc).
1963 */
deinit_mouse(void)1964 public void deinit_mouse(void)
1965 {
1966 #if !MSDOS_COMPILER
1967 ltputs(sc_e_mousecap, sc_height, putchr);
1968 #else
1969 #if MSDOS_COMPILER==WIN32C
1970 curr_console_input_mode = base_console_input_mode;
1971 SetConsoleMode(tty, curr_console_input_mode);
1972 #endif
1973 #endif
1974 }
1975
1976 /*
1977 * Suspend screen updates.
1978 */
suspend_screen(void)1979 public void suspend_screen(void)
1980 {
1981 #if !MSDOS_COMPILER
1982 ltputs(sc_suspend, 1, putchr);
1983 #endif
1984 }
1985
1986 /*
1987 * Resume screen updates.
1988 */
resume_screen(void)1989 public void resume_screen(void)
1990 {
1991 #if !MSDOS_COMPILER
1992 ltputs(sc_resume, 1, putchr);
1993 #endif
1994 }
1995
1996 /*
1997 * Initialize terminal
1998 */
term_init(void)1999 public void term_init(void)
2000 {
2001 if (term_init_done)
2002 return;
2003 term_init_done = term_init_ever = TRUE;
2004 clear_bot_if_needed();
2005 #if !MSDOS_COMPILER
2006 if (!(quit_if_one_screen && one_screen))
2007 {
2008 if (!no_init)
2009 {
2010 ltputs(sc_init, sc_height, putchr);
2011 /*
2012 * Some terminals leave the cursor unmoved when switching
2013 * to the alt screen. To avoid having the text appear at
2014 * a seemingly random line on the alt screen, move to
2015 * lower left if we are using an alt screen.
2016 */
2017 if (*sc_init != '\0' && *sc_deinit != '\0' && !no_alt_screen)
2018 lower_left();
2019 term_addrs = TRUE;
2020 }
2021 if (!no_keypad)
2022 ltputs(sc_s_keypad, sc_height, putchr);
2023 if (emouse != 0)
2024 init_mouse();
2025 if (no_paste)
2026 init_bracketed_paste();
2027 }
2028 if (top_scroll)
2029 {
2030 int i;
2031
2032 /*
2033 * This is nice to terminals with no alternate screen,
2034 * but with saved scrolled-off-the-top lines. This way,
2035 * no previous line is lost, but we start with a whole
2036 * screen to ourself.
2037 */
2038 for (i = 1; i < sc_height; i++)
2039 putchr('\n');
2040 } else
2041 line_left();
2042 #else
2043 #if MSDOS_COMPILER==WIN32C
2044 if (!(quit_if_one_screen && one_screen))
2045 {
2046 if (!no_init)
2047 {
2048 win32_init_term();
2049 term_addrs = TRUE;
2050 }
2051 if (emouse != 0)
2052 init_mouse();
2053
2054 }
2055 win32_init_vt_term();
2056 #endif
2057 initcolor();
2058 flush();
2059 #endif
2060 }
2061
2062 /*
2063 * Deinitialize terminal
2064 */
term_deinit(void)2065 public void term_deinit(void)
2066 {
2067 if (!term_init_done)
2068 return;
2069 #if !MSDOS_COMPILER
2070 if (!(quit_if_one_screen && one_screen))
2071 {
2072 if (emouse != 0)
2073 deinit_mouse();
2074 if (no_paste)
2075 deinit_bracketed_paste();
2076 if (!no_keypad)
2077 ltputs(sc_e_keypad, sc_height, putchr);
2078 if (!no_init)
2079 ltputs(sc_deinit, sc_height, putchr);
2080 }
2081 #else
2082 /* Restore system colors. */
2083 SETCOLORS(sy_fg_color, sy_bg_color);
2084 #if MSDOS_COMPILER==WIN32C
2085 win32_deinit_vt_term();
2086 if (!(quit_if_one_screen && one_screen))
2087 {
2088 if (emouse != 0)
2089 deinit_mouse();
2090 if (!no_init)
2091 win32_deinit_term();
2092 }
2093 #else
2094 /* Need clreol to make SETCOLORS take effect. */
2095 clreol();
2096 #endif
2097 #endif
2098 term_init_done = FALSE;
2099 }
2100
2101 /*
2102 * Are we interactive (ie. writing to an initialized tty)?
2103 */
interactive(void)2104 public lbool interactive(void)
2105 {
2106 return (is_tty && term_init_done);
2107 }
2108
assert_interactive(void)2109 static void assert_interactive(void)
2110 {
2111 }
2112
2113 /*
2114 * Home cursor (move to upper left corner of screen).
2115 */
home(void)2116 public void home(void)
2117 {
2118 assert_interactive();
2119 #if !MSDOS_COMPILER
2120 ltputs(sc_home, 1, putchr);
2121 #else
2122 flush();
2123 _settextposition(1,1);
2124 #endif
2125 }
2126
2127 #if LESSTEST
dump_screen(void)2128 public void dump_screen(void)
2129 {
2130 char dump_cmd[32];
2131 SNPRINTF1(dump_cmd, sizeof(dump_cmd), ESCS"0;0;%dR", sc_width * sc_height);
2132 ltputs(dump_cmd, sc_height, putchr);
2133 flush();
2134 }
2135 #endif /*LESSTEST*/
2136
2137 /*
2138 * Add a blank line (called with cursor at home).
2139 * Should scroll the display down.
2140 */
add_line(void)2141 public void add_line(void)
2142 {
2143 assert_interactive();
2144 #if !MSDOS_COMPILER
2145 ltputs(sc_addline, sc_height, putchr);
2146 #else
2147 flush();
2148 #if MSDOS_COMPILER==MSOFTC
2149 _scrolltextwindow(_GSCROLLDOWN);
2150 _settextposition(1,1);
2151 #else
2152 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2153 movetext(1,1, sc_width,sc_height-1, 1,2);
2154 gotoxy(1,1);
2155 clreol();
2156 #else
2157 #if MSDOS_COMPILER==WIN32C
2158 {
2159 CHAR_INFO fillchar;
2160 SMALL_RECT rcSrc, rcClip;
2161 COORD new_org;
2162 CONSOLE_SCREEN_BUFFER_INFO csbi;
2163
2164 GetConsoleScreenBufferInfo(con_out,&csbi);
2165
2166 /* The clip rectangle is the entire visible screen. */
2167 rcClip.Left = csbi.srWindow.Left;
2168 rcClip.Top = csbi.srWindow.Top;
2169 rcClip.Right = csbi.srWindow.Right;
2170 rcClip.Bottom = csbi.srWindow.Bottom;
2171
2172 /* The source rectangle is the visible screen minus the last line. */
2173 rcSrc = rcClip;
2174 rcSrc.Bottom--;
2175
2176 /* Move the top left corner of the source window down one row. */
2177 new_org.X = rcSrc.Left;
2178 new_org.Y = rcSrc.Top + 1;
2179
2180 /* Fill the right character and attributes. */
2181 fillchar.Char.AsciiChar = ' ';
2182 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
2183 fillchar.Attributes = curr_attr;
2184 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
2185 _settextposition(1,1);
2186 }
2187 #endif
2188 #endif
2189 #endif
2190 #endif
2191 }
2192
2193 #if 0
2194 /*
2195 * Remove the n topmost lines and scroll everything below it in the
2196 * window upward. This is needed to stop leaking the topmost line
2197 * into the scrollback buffer when we go down-one-line (in WIN32).
2198 */
2199 public void remove_top(int n)
2200 {
2201 #if MSDOS_COMPILER==WIN32C
2202 SMALL_RECT rcSrc, rcClip;
2203 CHAR_INFO fillchar;
2204 COORD new_org;
2205 CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
2206
2207 if (n >= sc_height - 1)
2208 {
2209 lclear();
2210 home();
2211 return;
2212 }
2213
2214 flush();
2215
2216 GetConsoleScreenBufferInfo(con_out, &csbi);
2217
2218 /* Get the extent of all-visible-rows-but-the-last. */
2219 rcSrc.Left = csbi.srWindow.Left;
2220 rcSrc.Top = csbi.srWindow.Top + n;
2221 rcSrc.Right = csbi.srWindow.Right;
2222 rcSrc.Bottom = csbi.srWindow.Bottom;
2223
2224 /* Get the clip rectangle. */
2225 rcClip.Left = rcSrc.Left;
2226 rcClip.Top = csbi.srWindow.Top;
2227 rcClip.Right = rcSrc.Right;
2228 rcClip.Bottom = rcSrc.Bottom ;
2229
2230 /* Move the source window up n rows. */
2231 new_org.X = rcSrc.Left;
2232 new_org.Y = rcSrc.Top - n;
2233
2234 /* Fill the right character and attributes. */
2235 fillchar.Char.AsciiChar = ' ';
2236 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
2237 fillchar.Attributes = curr_attr;
2238
2239 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
2240
2241 /* Position cursor on first blank line. */
2242 goto_line(sc_height - n - 1);
2243 #endif
2244 }
2245 #endif
2246
2247 #if MSDOS_COMPILER==WIN32C
2248 /*
2249 * Clear the screen.
2250 */
win32_clear(void)2251 static void win32_clear(void)
2252 {
2253 /*
2254 * This will clear only the currently visible rows of the NT
2255 * console buffer, which means none of the precious scrollback
2256 * rows are touched making for faster scrolling. Note that, if
2257 * the window has fewer columns than the console buffer (i.e.
2258 * there is a horizontal scrollbar as well), the entire width
2259 * of the visible rows will be cleared.
2260 */
2261 COORD topleft;
2262 DWORD nchars;
2263 DWORD winsz;
2264 CONSOLE_SCREEN_BUFFER_INFO csbi;
2265
2266 /* get the number of cells in the current buffer */
2267 GetConsoleScreenBufferInfo(con_out, &csbi);
2268 winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
2269 topleft.X = 0;
2270 topleft.Y = csbi.srWindow.Top;
2271
2272 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
2273 FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars);
2274 FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars);
2275 }
2276
2277 /*
2278 * Remove the n topmost lines and scroll everything below it in the
2279 * window upward.
2280 */
win32_scroll_up(int n)2281 public void win32_scroll_up(int n)
2282 {
2283 SMALL_RECT rcSrc, rcClip;
2284 CHAR_INFO fillchar;
2285 COORD topleft;
2286 COORD new_org;
2287 DWORD nchars;
2288 DWORD size;
2289 CONSOLE_SCREEN_BUFFER_INFO csbi;
2290
2291 if (n <= 0)
2292 return;
2293
2294 if (n >= sc_height - 1)
2295 {
2296 win32_clear();
2297 _settextposition(1,1);
2298 return;
2299 }
2300
2301 /* Get the extent of what will remain visible after scrolling. */
2302 GetConsoleScreenBufferInfo(con_out, &csbi);
2303 rcSrc.Left = csbi.srWindow.Left;
2304 rcSrc.Top = csbi.srWindow.Top + n;
2305 rcSrc.Right = csbi.srWindow.Right;
2306 rcSrc.Bottom = csbi.srWindow.Bottom;
2307
2308 /* Get the clip rectangle. */
2309 rcClip.Left = rcSrc.Left;
2310 rcClip.Top = csbi.srWindow.Top;
2311 rcClip.Right = rcSrc.Right;
2312 rcClip.Bottom = rcSrc.Bottom ;
2313
2314 /* Move the source text to the top of the screen. */
2315 new_org.X = rcSrc.Left;
2316 new_org.Y = rcClip.Top;
2317
2318 /* Fill the right character and attributes. */
2319 fillchar.Char.AsciiChar = ' ';
2320 fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color);
2321
2322 /* Scroll the window. */
2323 SetConsoleTextAttribute(con_out, fillchar.Attributes);
2324 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
2325
2326 /* Clear remaining lines at bottom. */
2327 topleft.X = csbi.dwCursorPosition.X;
2328 topleft.Y = rcSrc.Bottom - n;
2329 size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X);
2330 FillConsoleOutputCharacter(con_out, ' ', size, topleft,
2331 &nchars);
2332 FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft,
2333 &nchars);
2334 SetConsoleTextAttribute(con_out, curr_attr);
2335
2336 /* Move cursor n lines up from where it was. */
2337 csbi.dwCursorPosition.Y -= n;
2338 SetConsoleCursorPosition(con_out, csbi.dwCursorPosition);
2339 }
2340 #endif
2341
2342 /*
2343 * Move cursor to lower left corner of screen.
2344 */
lower_left(void)2345 public void lower_left(void)
2346 {
2347 assert_interactive();
2348 #if !MSDOS_COMPILER
2349 ltputs(sc_lower_left, 1, putchr);
2350 #else
2351 flush();
2352 _settextposition(sc_height, 1);
2353 #endif
2354 }
2355
2356 /*
2357 * Move cursor to left position of current line.
2358 */
line_left(void)2359 public void line_left(void)
2360 {
2361 assert_interactive();
2362 #if !MSDOS_COMPILER
2363 ltputs(sc_return, 1, putchr);
2364 #else
2365 {
2366 int row;
2367 flush();
2368 #if MSDOS_COMPILER==WIN32C
2369 {
2370 CONSOLE_SCREEN_BUFFER_INFO scr;
2371 GetConsoleScreenBufferInfo(con_out, &scr);
2372 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
2373 }
2374 #else
2375 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2376 row = wherey();
2377 #else
2378 {
2379 struct rccoord tpos = _gettextposition();
2380 row = tpos.row;
2381 }
2382 #endif
2383 #endif
2384 _settextposition(row, 1);
2385 }
2386 #endif
2387 }
2388
2389 /*
2390 * Check if the console size has changed and reset internals
2391 * (in lieu of SIGWINCH for WIN32).
2392 */
check_winch(void)2393 public void check_winch(void)
2394 {
2395 #if MSDOS_COMPILER==WIN32C
2396 CONSOLE_SCREEN_BUFFER_INFO scr;
2397 COORD size;
2398
2399 if (con_out == INVALID_HANDLE_VALUE)
2400 return;
2401
2402 flush();
2403 GetConsoleScreenBufferInfo(con_out, &scr);
2404 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
2405 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
2406 if (size.Y != sc_height || size.X != sc_width)
2407 {
2408 sc_height = size.Y;
2409 sc_width = size.X;
2410 if (!no_init && con_out_ours == con_out)
2411 SetConsoleScreenBufferSize(con_out, size);
2412 pos_init();
2413 screen_size_changed();
2414 screen_trashed();
2415 }
2416 #endif
2417 }
2418
2419 /*
2420 * Goto a specific line on the screen.
2421 */
goto_line(int sindex)2422 public void goto_line(int sindex)
2423 {
2424 assert_interactive();
2425 #if !MSDOS_COMPILER
2426 ltputs(ltgoto(sc_move, 0, sindex), 1, putchr);
2427 #else
2428 flush();
2429 _settextposition(sindex+1, 1);
2430 #endif
2431 }
2432
2433 #if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC
2434 /*
2435 * Create an alternate screen which is all white.
2436 * This screen is used to create a "flash" effect, by displaying it
2437 * briefly and then switching back to the normal screen.
2438 * {{ Yuck! There must be a better way to get a visual bell. }}
2439 */
create_flash(void)2440 static void create_flash(void)
2441 {
2442 #if MSDOS_COMPILER==MSOFTC
2443 struct videoconfig w;
2444 char *blanks;
2445 int row, col;
2446
2447 _getvideoconfig(&w);
2448 videopages = w.numvideopages;
2449 if (videopages < 2)
2450 {
2451 at_enter(AT_STANDOUT);
2452 at_exit();
2453 } else
2454 {
2455 _setactivepage(1);
2456 at_enter(AT_STANDOUT);
2457 blanks = (char *) ecalloc(w.numtextcols, sizeof(char));
2458 for (col = 0; col < w.numtextcols; col++)
2459 blanks[col] = ' ';
2460 for (row = w.numtextrows; row > 0; row--)
2461 _outmem(blanks, w.numtextcols);
2462 _setactivepage(0);
2463 _setvisualpage(0);
2464 free(blanks);
2465 at_exit();
2466 }
2467 #else
2468 #if MSDOS_COMPILER==BORLANDC
2469 int n;
2470
2471 whitescreen = (unsigned short *)
2472 malloc(sc_width * sc_height * sizeof(short));
2473 if (whitescreen == NULL)
2474 return;
2475 for (n = 0; n < sc_width * sc_height; n++)
2476 whitescreen[n] = 0x7020;
2477 #endif
2478 #endif
2479 flash_created = 1;
2480 }
2481 #endif /* MSDOS_COMPILER */
2482
2483 /*
2484 * Output the "visual bell", if there is one.
2485 */
vbell(void)2486 public void vbell(void)
2487 {
2488 if (no_vbell)
2489 return;
2490 #if !MSDOS_COMPILER
2491 if (*sc_visual_bell == '\0')
2492 return;
2493 ltputs(sc_visual_bell, sc_height, putchr);
2494 #else
2495 #if MSDOS_COMPILER==DJGPPC
2496 ScreenVisualBell();
2497 #else
2498 #if MSDOS_COMPILER==MSOFTC
2499 /*
2500 * Create a flash screen on the second video page.
2501 * Switch to that page, then switch back.
2502 */
2503 if (!flash_created)
2504 create_flash();
2505 if (videopages < 2)
2506 return;
2507 _setvisualpage(1);
2508 delay(100);
2509 _setvisualpage(0);
2510 #else
2511 #if MSDOS_COMPILER==BORLANDC
2512 unsigned short *currscreen;
2513
2514 /*
2515 * Get a copy of the current screen.
2516 * Display the flash screen.
2517 * Then restore the old screen.
2518 */
2519 if (!flash_created)
2520 create_flash();
2521 if (whitescreen == NULL)
2522 return;
2523 currscreen = (unsigned short *)
2524 malloc(sc_width * sc_height * sizeof(short));
2525 if (currscreen == NULL) return;
2526 gettext(1, 1, sc_width, sc_height, currscreen);
2527 puttext(1, 1, sc_width, sc_height, whitescreen);
2528 delay(100);
2529 puttext(1, 1, sc_width, sc_height, currscreen);
2530 free(currscreen);
2531 #else
2532 #if MSDOS_COMPILER==WIN32C
2533 /* paint screen with an inverse color */
2534 lclear();
2535
2536 /* leave it displayed for 100 msec. */
2537 Sleep(100);
2538
2539 /* restore with a redraw */
2540 repaint();
2541 #endif
2542 #endif
2543 #endif
2544 #endif
2545 #endif
2546 }
2547
2548 /*
2549 * Make a noise.
2550 */
lbeep(void)2551 static void lbeep(void)
2552 {
2553 #if !MSDOS_COMPILER
2554 putchr(CONTROL('G'));
2555 #else
2556 #if MSDOS_COMPILER==WIN32C
2557 MessageBeep(0);
2558 #else
2559 write(1, "\7", 1);
2560 #endif
2561 #endif
2562 }
2563
2564 /*
2565 * Ring the terminal bell.
2566 */
lbell(void)2567 public void lbell(void)
2568 {
2569 if (quiet == VERY_QUIET)
2570 vbell();
2571 else
2572 lbeep();
2573 }
2574
2575 /*
2576 * Clear the screen.
2577 */
lclear(void)2578 public void lclear(void)
2579 {
2580 assert_interactive();
2581 suspend_screen();
2582 #if !MSDOS_COMPILER
2583 ltputs(sc_clear, sc_height, putchr);
2584 #else
2585 flush();
2586 #if MSDOS_COMPILER==WIN32C
2587 win32_clear();
2588 #else
2589 _clearscreen(_GCLEARSCREEN);
2590 #endif
2591 #endif
2592 }
2593
2594 /*
2595 * Clear from the cursor to the end of the cursor's line.
2596 * {{ This must not move the cursor. }}
2597 */
clear_eol(void)2598 public void clear_eol(void)
2599 {
2600 /* assert_interactive();*/
2601 #if !MSDOS_COMPILER
2602 ltputs(sc_eol_clear, 1, putchr);
2603 #else
2604 #if MSDOS_COMPILER==MSOFTC
2605 short top, left;
2606 short bot, right;
2607 struct rccoord tpos;
2608
2609 flush();
2610 /*
2611 * Save current state.
2612 */
2613 tpos = _gettextposition();
2614 _gettextwindow(&top, &left, &bot, &right);
2615 /*
2616 * Set a temporary window to the current line,
2617 * from the cursor's position to the right edge of the screen.
2618 * Then clear that window.
2619 */
2620 _settextwindow(tpos.row, tpos.col, tpos.row, sc_width);
2621 _clearscreen(_GWINDOW);
2622 /*
2623 * Restore state.
2624 */
2625 _settextwindow(top, left, bot, right);
2626 _settextposition(tpos.row, tpos.col);
2627 #else
2628 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2629 flush();
2630 clreol();
2631 #else
2632 #if MSDOS_COMPILER==WIN32C
2633 DWORD nchars;
2634 COORD cpos;
2635 CONSOLE_SCREEN_BUFFER_INFO scr;
2636
2637 flush();
2638 memset(&scr, 0, sizeof(scr));
2639 GetConsoleScreenBufferInfo(con_out, &scr);
2640 cpos.X = scr.dwCursorPosition.X;
2641 cpos.Y = scr.dwCursorPosition.Y;
2642 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
2643 FillConsoleOutputAttribute(con_out, curr_attr,
2644 scr.dwSize.X - cpos.X, cpos, &nchars);
2645 FillConsoleOutputCharacter(con_out, ' ',
2646 scr.dwSize.X - cpos.X, cpos, &nchars);
2647 #endif
2648 #endif
2649 #endif
2650 #endif
2651 }
2652
2653 /*
2654 * Clear the current line.
2655 * Clear the screen if there's off-screen memory below the display.
2656 */
clear_eol_bot(void)2657 static void clear_eol_bot(void)
2658 {
2659 assert_interactive();
2660 #if MSDOS_COMPILER
2661 clear_eol();
2662 #else
2663 if (below_mem)
2664 ltputs(sc_eos_clear, 1, putchr);
2665 else
2666 ltputs(sc_eol_clear, 1, putchr);
2667 #endif
2668 }
2669
2670 /*
2671 * Clear the bottom line of the display.
2672 * Leave the cursor at the beginning of the bottom line.
2673 */
clear_bot(void)2674 public void clear_bot(void)
2675 {
2676 /*
2677 * If we're in a non-normal attribute mode, temporarily exit
2678 * the mode while we do the clear. Some terminals fill the
2679 * cleared area with the current attribute.
2680 */
2681 if (oldbot)
2682 lower_left();
2683 else
2684 line_left();
2685
2686 if (attrmode == AT_NORMAL)
2687 clear_eol_bot();
2688 else
2689 {
2690 int saved_attrmode = attrmode;
2691
2692 at_exit();
2693 clear_eol_bot();
2694 at_enter(saved_attrmode);
2695 }
2696 }
2697
2698 /*
2699 * Enable or disable bracketed paste mode.
2700 * When enabled, the terminal sends an "open bracket" sequence
2701 * before pasted content and "close bracket" after it.
2702 */
init_bracketed_paste(void)2703 public void init_bracketed_paste(void)
2704 {
2705 #if !MSDOS_COMPILER
2706 ltputs(sc_s_bracketed_paste, 1, putchr);
2707 #endif
2708 }
2709
deinit_bracketed_paste(void)2710 public void deinit_bracketed_paste(void)
2711 {
2712 #if !MSDOS_COMPILER
2713 ltputs(sc_e_bracketed_paste, 1, putchr);
2714 #endif
2715 }
2716
2717 /*
2718 * Color string may be "x[y]" where x and y are 4-bit color chars,
2719 * or "N[.M]" where N and M are decimal integers>
2720 * Any of x,y,N,M may also be "-" to mean "unchanged".
2721 */
2722
2723 /*
2724 * Parse a 4-bit color char.
2725 */
parse_color4(char ch)2726 static int parse_color4(char ch)
2727 {
2728 switch (ch)
2729 {
2730 case 'k': return 0;
2731 case 'r': return CV_RED;
2732 case 'g': return CV_GREEN;
2733 case 'y': return CV_RED|CV_GREEN;
2734 case 'b': return CV_BLUE;
2735 case 'm': return CV_RED|CV_BLUE;
2736 case 'c': return CV_GREEN|CV_BLUE;
2737 case 'w': return CV_RED|CV_GREEN|CV_BLUE;
2738 case 'K': return 0|CV_BRIGHT;
2739 case 'R': return CV_RED|CV_BRIGHT;
2740 case 'G': return CV_GREEN|CV_BRIGHT;
2741 case 'Y': return CV_RED|CV_GREEN|CV_BRIGHT;
2742 case 'B': return CV_BLUE|CV_BRIGHT;
2743 case 'M': return CV_RED|CV_BLUE|CV_BRIGHT;
2744 case 'C': return CV_GREEN|CV_BLUE|CV_BRIGHT;
2745 case 'W': return CV_RED|CV_GREEN|CV_BLUE|CV_BRIGHT;
2746 case '-': return CV_NOCHANGE;
2747 default: return CV_ERROR;
2748 }
2749 }
2750
2751 /*
2752 * Parse a color as a decimal integer.
2753 */
parse_color6(constant char ** ps)2754 static int parse_color6(constant char **ps)
2755 {
2756 if (**ps == '-')
2757 {
2758 (*ps)++;
2759 return CV_NOCHANGE;
2760 } else
2761 {
2762 constant char *os = *ps;
2763 int color = lstrtoic(os, ps, 10);
2764 if (color < 0 || *ps == os)
2765 return CV_ERROR;
2766 return color;
2767 }
2768 }
2769
2770 /*
2771 * Parse a color pair and return the foreground/background/attribute values.
2772 * Return type of color specifier:
2773 * CV_4BIT: fg/bg values are OR of CV_{RGB} bits.
2774 * CV_6BIT: fg/bg values are integers entered by user.
2775 */
parse_color(constant char * str,mutable int * p_fg,mutable int * p_bg,mutable CHAR_ATTR * p_cattr)2776 public COLOR_TYPE parse_color(constant char *str, mutable int *p_fg, mutable int *p_bg, mutable CHAR_ATTR *p_cattr)
2777 {
2778 int fg;
2779 int bg = CV_ERROR;
2780 CHAR_ATTR cattr = CATTR_NULL;
2781 COLOR_TYPE type = CT_NULL;
2782
2783 if (str == NULL || *str == '\0')
2784 return CT_NULL;
2785 if (*str == '+')
2786 str++; /* ignore leading + */
2787
2788 fg = parse_color4(*str);
2789 if (fg != CV_ERROR)
2790 {
2791 if (str[1] == '\0' || strchr("*~_&dsul", str[1]) != NULL)
2792 {
2793 bg = CV_NOCHANGE;
2794 str++; /* skip the fg char */
2795 } else
2796 {
2797 bg = parse_color4(str[1]);
2798 if (bg != CV_ERROR)
2799 str += 2; /* skip both fg and bg chars */
2800 }
2801 }
2802 if (fg != CV_ERROR && bg != CV_ERROR)
2803 type = CT_4BIT;
2804 else
2805 {
2806 fg = (*str == '.') ? CV_NOCHANGE : parse_color6(&str);
2807 if (fg != CV_ERROR)
2808 {
2809 if (*str != '.')
2810 bg = CV_NOCHANGE;
2811 else
2812 {
2813 str++; /* skip the dot */
2814 bg = parse_color6(&str);
2815 }
2816 }
2817 if (fg != CV_ERROR && bg != CV_ERROR)
2818 type = CT_6BIT;
2819 }
2820 if (type != CT_NULL)
2821 {
2822 for (;; str++)
2823 {
2824 if (*str == '*' || *str == 'd')
2825 cattr |= CATTR_BOLD;
2826 else if (*str == '~' || *str == 's')
2827 cattr |= CATTR_STANDOUT;
2828 else if (*str == '_' || *str == 'u')
2829 cattr |= CATTR_UNDERLINE;
2830 else if (*str == '&' || *str == 'l') /* can't use 'k' because of conflict with "black" */
2831 cattr |= CATTR_BLINK;
2832 else
2833 break;
2834 }
2835 if (p_fg != NULL) *p_fg = fg;
2836 if (p_bg != NULL) *p_bg = bg;
2837 if (p_cattr != NULL) *p_cattr = cattr;
2838 }
2839 return type;
2840 }
2841
2842 #if !MSDOS_COMPILER
2843
sgr_color(int color)2844 static int sgr_color(int color)
2845 {
2846 switch (color)
2847 {
2848 case 0: return 30;
2849 case CV_RED: return 31;
2850 case CV_GREEN: return 32;
2851 case CV_RED|CV_GREEN: return 33;
2852 case CV_BLUE: return 34;
2853 case CV_RED|CV_BLUE: return 35;
2854 case CV_GREEN|CV_BLUE: return 36;
2855 case CV_RED|CV_GREEN|CV_BLUE: return 37;
2856
2857 case CV_BRIGHT: return 90;
2858 case CV_RED|CV_BRIGHT: return 91;
2859 case CV_GREEN|CV_BRIGHT: return 92;
2860 case CV_RED|CV_GREEN|CV_BRIGHT: return 93;
2861 case CV_BLUE|CV_BRIGHT: return 94;
2862 case CV_RED|CV_BLUE|CV_BRIGHT: return 95;
2863 case CV_GREEN|CV_BLUE|CV_BRIGHT: return 96;
2864 case CV_RED|CV_GREEN|CV_BLUE|CV_BRIGHT: return 97;
2865
2866 default: return color;
2867 }
2868 }
2869
tput_fmt(constant char * fmt,int color,int (* f_putc)(int))2870 static void tput_fmt(constant char *fmt, int color, int (*f_putc)(int))
2871 {
2872 char buf[INT_STRLEN_BOUND(int)+16];
2873 if (color == attrcolor)
2874 return;
2875 SNPRINTF1(buf, sizeof(buf), fmt, color);
2876 ltputs(buf, 1, f_putc);
2877 attrcolor = color;
2878 }
2879
tput_char_cattr(CHAR_ATTR cattr,int (* f_putc)(int))2880 static void tput_char_cattr(CHAR_ATTR cattr, int (*f_putc)(int))
2881 {
2882 if (cattr & CATTR_UNDERLINE)
2883 {
2884 ltputs(sc_u_in, 1, f_putc);
2885 attrbits |= AT_UNDERLINE;
2886 }
2887 if (cattr & CATTR_BOLD)
2888 {
2889 ltputs(sc_b_in, 1, f_putc);
2890 attrbits |= AT_BOLD;
2891 }
2892 if (cattr & CATTR_BLINK)
2893 {
2894 ltputs(sc_bl_in, 1, f_putc);
2895 attrbits |= AT_BLINK;
2896 }
2897 if (cattr & CATTR_STANDOUT)
2898 {
2899 ltputs(sc_s_in, 1, f_putc);
2900 attrbits |= AT_STANDOUT;
2901 }
2902 }
2903
tput_color(constant char * str,int (* f_putc)(int))2904 static void tput_color(constant char *str, int (*f_putc)(int))
2905 {
2906 int fg;
2907 int bg;
2908 CHAR_ATTR cattr;
2909
2910 if (str != NULL && strcmp(str, "*") == 0)
2911 {
2912 /* Special case: reset to normal */
2913 tput_fmt(ESCS"[m", -1, f_putc);
2914 return;
2915 }
2916 switch (parse_color(str, &fg, &bg, &cattr))
2917 {
2918 case CT_4BIT:
2919 if (fg >= 0)
2920 tput_fmt(ESCS"[%dm", sgr_color(fg), f_putc);
2921 if (bg >= 0)
2922 tput_fmt(ESCS"[%dm", sgr_color(bg)+10, f_putc);
2923 tput_char_cattr(cattr, f_putc);
2924 break;
2925 case CT_6BIT:
2926 if (fg >= 0)
2927 tput_fmt(ESCS"[38;5;%dm", fg, f_putc);
2928 if (bg >= 0)
2929 tput_fmt(ESCS"[48;5;%dm", bg, f_putc);
2930 tput_char_cattr(cattr, f_putc);
2931 break;
2932 default:
2933 break;
2934 }
2935 }
2936
tput_inmode(constant char * mode_str,int attr,int attr_bit,int (* f_putc)(int))2937 static void tput_inmode(constant char *mode_str, int attr, int attr_bit, int (*f_putc)(int))
2938 {
2939 constant char *color_str;
2940 if ((attr & attr_bit) == 0)
2941 return;
2942 color_str = get_color_map(attr_bit);
2943 if (color_str == NULL || *color_str == '\0' || *color_str == '+')
2944 {
2945 ltputs(mode_str, 1, f_putc);
2946 attrbits |= attr_bit;
2947 if (color_str == NULL || *color_str++ != '+')
2948 return;
2949 }
2950 /* Color overrides mode string */
2951 tput_color(color_str, f_putc);
2952 }
2953
tput_outmode(constant char * mode_str,int attr_bit,int (* f_putc)(int))2954 static void tput_outmode(constant char *mode_str, int attr_bit, int (*f_putc)(int))
2955 {
2956 if ((attrbits & attr_bit) == 0)
2957 return;
2958 ltputs(mode_str, 1, f_putc);
2959 attrbits &= ~attr_bit;
2960 }
2961
2962 #else /* MSDOS_COMPILER */
2963
2964 #if MSDOS_COMPILER==WIN32C
WIN32put_fmt(constant char * fmt,int color)2965 static lbool WIN32put_fmt(constant char *fmt, int color)
2966 {
2967 char buf[INT_STRLEN_BOUND(int)+16];
2968 int len = (size_t) SNPRINTF1(buf, sizeof(buf), fmt, color);
2969 if (len > 0)
2970 WIN32textout(buf, (size_t) len);
2971 return TRUE;
2972 }
2973
win_set_cattr(CHAR_ATTR cattr)2974 static void win_set_cattr(CHAR_ATTR cattr)
2975 {
2976 if (cattr & CATTR_UNDERLINE)
2977 WIN32textout(ESCS"[4m", 4);
2978 if (cattr & CATTR_BOLD)
2979 WIN32textout(ESCS"[1m", 4);
2980 if (cattr & CATTR_BLINK)
2981 WIN32textout(ESCS"[5m", 4);
2982 if (cattr & CATTR_STANDOUT)
2983 WIN32textout(ESCS"[7m", 4);
2984 }
2985 #endif
2986
win_set_color(int attr)2987 static lbool win_set_color(int attr)
2988 {
2989 int fg;
2990 int bg;
2991 CHAR_ATTR cattr;
2992 lbool out = FALSE;
2993 constant char *str = get_color_map(attr);
2994 if (str == NULL || str[0] == '\0')
2995 return FALSE;
2996 switch (parse_color(str, &fg, &bg, &cattr))
2997 {
2998 case CT_4BIT:
2999 if (fg >= 0 && bg >= 0)
3000 {
3001 SETCOLORS(fg, bg);
3002 out = TRUE;
3003 } else if (fg >= 0)
3004 {
3005 SET_FG_COLOR(fg);
3006 out = TRUE;
3007 } else if (bg >= 0)
3008 {
3009 SET_BG_COLOR(bg);
3010 out = TRUE;
3011 }
3012 #if MSDOS_COMPILER==WIN32C
3013 if (vt_enabled)
3014 win_set_cattr(cattr);
3015 #endif
3016 break;
3017 #if MSDOS_COMPILER==WIN32C
3018 case CT_6BIT:
3019 if (vt_enabled)
3020 {
3021 if (fg > 0)
3022 out = WIN32put_fmt(ESCS"[38;5;%dm", fg);
3023 if (bg > 0)
3024 out = WIN32put_fmt(ESCS"[48;5;%dm", bg);
3025 win_set_cattr(cattr);
3026 }
3027 break;
3028 #endif
3029 default:
3030 break;
3031 }
3032 return out;
3033 }
3034
3035 #endif /* MSDOS_COMPILER */
3036
at_enter(int attr)3037 public void at_enter(int attr)
3038 {
3039 attr = apply_at_specials(attr);
3040 #if !MSDOS_COMPILER
3041 /* The one with the most priority is last. */
3042 tput_inmode(sc_u_in, attr, AT_UNDERLINE, putchr);
3043 tput_inmode(sc_b_in, attr, AT_BOLD, putchr);
3044 tput_inmode(sc_bl_in, attr, AT_BLINK, putchr);
3045 /* Don't use standout and color at the same time. */
3046 if (use_color && (attr & AT_COLOR))
3047 tput_color(get_color_map(attr), putchr);
3048 else
3049 tput_inmode(sc_s_in, attr, AT_STANDOUT, putchr);
3050 #else
3051 flush();
3052 /* The one with the most priority is first. */
3053 if ((attr & AT_COLOR) && use_color)
3054 {
3055 win_set_color(attr);
3056 } else if (attr & AT_STANDOUT)
3057 {
3058 SETCOLORS(so_fg_color, so_bg_color);
3059 } else if (attr & AT_BLINK)
3060 {
3061 SETCOLORS(bl_fg_color, bl_bg_color);
3062 } else if (attr & AT_BOLD)
3063 {
3064 SETCOLORS(bo_fg_color, bo_bg_color);
3065 } else if (attr & AT_UNDERLINE)
3066 {
3067 SETCOLORS(ul_fg_color, ul_bg_color);
3068 }
3069 #endif
3070 attrmode = attr;
3071 }
3072
at_exit(void)3073 public void at_exit(void)
3074 {
3075 #if !MSDOS_COMPILER
3076 /* Undo things in the reverse order we did them. */
3077 tput_color("*", putchr);
3078 tput_outmode(sc_s_out, AT_STANDOUT, putchr);
3079 tput_outmode(sc_bl_out, AT_BLINK, putchr);
3080 tput_outmode(sc_b_out, AT_BOLD, putchr);
3081 tput_outmode(sc_u_out, AT_UNDERLINE, putchr);
3082 #else
3083 flush();
3084 SETCOLORS(nm_fg_color, nm_bg_color);
3085 #endif
3086 attrmode = AT_NORMAL;
3087 }
3088
at_switch(int attr)3089 public void at_switch(int attr)
3090 {
3091 int new_attrmode = apply_at_specials(attr);
3092 int ignore_modes = AT_ANSI;
3093
3094 if ((new_attrmode & ~ignore_modes) != (attrmode & ~ignore_modes))
3095 {
3096 at_exit();
3097 at_enter(attr);
3098 }
3099 }
3100
is_at_equiv(int attr1,int attr2)3101 public lbool is_at_equiv(int attr1, int attr2)
3102 {
3103 attr1 = apply_at_specials(attr1);
3104 attr2 = apply_at_specials(attr2);
3105
3106 return (attr1 == attr2);
3107 }
3108
apply_at_specials(int attr)3109 public int apply_at_specials(int attr)
3110 {
3111 if (attr & AT_BINARY)
3112 attr |= binattr;
3113 if (attr & AT_HILITE)
3114 attr |= AT_STANDOUT;
3115 attr &= ~(AT_BINARY|AT_HILITE);
3116
3117 return attr;
3118 }
3119
3120 /*
3121 * Output a plain backspace, without erasing the previous char.
3122 */
putbs(void)3123 public void putbs(void)
3124 {
3125 if (termcap_debug)
3126 putstr("<bs>");
3127 else
3128 {
3129 #if !MSDOS_COMPILER
3130 ltputs(sc_backspace, 1, putchr);
3131 #else
3132 int row, col;
3133
3134 flush();
3135 {
3136 #if MSDOS_COMPILER==MSOFTC
3137 struct rccoord tpos;
3138 tpos = _gettextposition();
3139 row = tpos.row;
3140 col = tpos.col;
3141 #else
3142 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
3143 row = wherey();
3144 col = wherex();
3145 #else
3146 #if MSDOS_COMPILER==WIN32C
3147 CONSOLE_SCREEN_BUFFER_INFO scr;
3148 GetConsoleScreenBufferInfo(con_out, &scr);
3149 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
3150 col = scr.dwCursorPosition.X - scr.srWindow.Left + 1;
3151 #endif
3152 #endif
3153 #endif
3154 }
3155 if (col <= 1)
3156 return;
3157 _settextposition(row, col-1);
3158 #endif /* MSDOS_COMPILER */
3159 }
3160 }
3161
3162 #if MSDOS_COMPILER==WIN32C
3163
3164 #define WIN32_MAX_REPEAT 3
3165 #define LAST_DOWN_COUNT 8
3166 static LWCHAR last_downs[LAST_DOWN_COUNT] = { 0 };
3167 static int last_down_index = 0;
3168 static LWCHAR hi_surr = 0;
3169
3170 typedef struct XINPUT_RECORD {
3171 INPUT_RECORD ir;
3172 LWCHAR ichar; /* because ir...UnicodeChar is only 16 bits */
3173 } XINPUT_RECORD;
3174
3175 typedef struct WIN32_CHAR {
3176 struct WIN32_CHAR *wc_next;
3177 int wc_ch;
3178 } WIN32_CHAR;
3179
3180 static WIN32_CHAR *win32_queue = NULL;
3181
3182 /*
3183 * Is the win32_queue nonempty?
3184 */
win32_queued_char(void)3185 static lbool win32_queued_char(void)
3186 {
3187 return (win32_queue != NULL);
3188 }
3189
3190 /*
3191 * Push a char onto the back of the win32_queue.
3192 */
win32_enqueue(int ch)3193 public void win32_enqueue(int ch)
3194 {
3195 WIN32_CHAR *wch = (WIN32_CHAR *) ecalloc(1, sizeof(WIN32_CHAR));
3196 wch->wc_ch = ch;
3197 wch->wc_next = NULL;
3198 if (win32_queue == NULL)
3199 win32_queue = wch;
3200 else
3201 {
3202 WIN32_CHAR *pch;
3203 for (pch = win32_queue; pch->wc_next != NULL; pch = pch->wc_next)
3204 continue;
3205 pch->wc_next = wch;
3206 }
3207 }
3208
3209 /*
3210 * Push a char onto the front of the win32_queue.
3211 * Makes the next call to WIN32getch return ch.
3212 */
WIN32ungetch(int ch)3213 public void WIN32ungetch(int ch)
3214 {
3215 WIN32_CHAR *wch = (WIN32_CHAR *) ecalloc(1, sizeof(WIN32_CHAR));
3216 wch->wc_ch = ch;
3217 wch->wc_next = win32_queue;
3218 win32_queue = wch;
3219 }
3220
3221 /*
3222 * Get a char from the front of the win32_queue.
3223 */
win32_get_queue(void)3224 static int win32_get_queue(void)
3225 {
3226 WIN32_CHAR *wch = win32_queue;
3227 int ch = wch->wc_ch;
3228 win32_queue = wch->wc_next;
3229 free(wch);
3230 return ch;
3231 }
3232
win32_clear_queue(void)3233 public void win32_clear_queue(void)
3234 {
3235 while (win32_queued_char())
3236 (void) win32_get_queue();
3237 }
3238
3239 /*
3240 * Handle a mouse input event.
3241 */
win32_mouse_event(XINPUT_RECORD * xip)3242 static lbool win32_mouse_event(XINPUT_RECORD *xip)
3243 {
3244 char b;
3245
3246 if (emouse == 0 || xip->ir.EventType != MOUSE_EVENT)
3247 return (FALSE);
3248
3249 /* Generate an X11 mouse sequence from the mouse event. */
3250 /* TODO: switch to the 1006 protocol to allow specific-button-up reports */
3251 switch (xip->ir.Event.MouseEvent.dwEventFlags)
3252 {
3253 case 0: /* press or release */
3254 if (xip->ir.Event.MouseEvent.dwButtonState == 0)
3255 b = X11MOUSE_OFFSET + X11MOUSE_BUTTON_REL;
3256 else if (xip->ir.Event.MouseEvent.dwButtonState == 1) /* leftmost */
3257 b = X11MOUSE_OFFSET + X11MOUSE_BUTTON1;
3258 else if (xip->ir.Event.MouseEvent.dwButtonState == 2) /* rightmost */
3259 b = X11MOUSE_OFFSET + X11MOUSE_BUTTON3;
3260 else if (xip->ir.Event.MouseEvent.dwButtonState == 4) /* middle ("next-to-leftmost") */
3261 b = X11MOUSE_OFFSET + X11MOUSE_BUTTON2;
3262 else /* don't bother to figure out what changed */
3263 return (FALSE);
3264 break;
3265 case MOUSE_WHEELED:
3266 b = X11MOUSE_OFFSET + (((int)xip->ir.Event.MouseEvent.dwButtonState < 0) ? X11MOUSE_WHEEL_DOWN : X11MOUSE_WHEEL_UP);
3267 break;
3268 case MOUSE_MOVED:
3269 if (xip->ir.Event.MouseEvent.dwButtonState != 1)
3270 return (FALSE);
3271 /* Drag with left button down. */
3272 b = X11MOUSE_OFFSET + X11MOUSE_DRAG;
3273 break;
3274 default:
3275 return (FALSE);
3276 }
3277 /* {{ TODO: change to X11 1006 format. }} */
3278 win32_enqueue(ESC);
3279 win32_enqueue('[');
3280 win32_enqueue('M');
3281 win32_enqueue(b);
3282 win32_enqueue(X11MOUSE_OFFSET + xip->ir.Event.MouseEvent.dwMousePosition.X + 1);
3283 win32_enqueue(X11MOUSE_OFFSET + xip->ir.Event.MouseEvent.dwMousePosition.Y + 1);
3284 return (TRUE);
3285 }
3286
set_last_down(LWCHAR ch)3287 static void set_last_down(LWCHAR ch)
3288 {
3289 if (ch == 0) return;
3290 last_downs[last_down_index] = ch;
3291 if (++last_down_index >= LAST_DOWN_COUNT)
3292 last_down_index = 0;
3293 }
3294
find_last_down(LWCHAR ch)3295 static LWCHAR *find_last_down(LWCHAR ch)
3296 {
3297 int i;
3298 for (i = 0; i < LAST_DOWN_COUNT; ++i)
3299 if (last_downs[i] == ch)
3300 return &last_downs[i];
3301 return NULL;
3302 }
3303
3304 /*
3305 * Get an input char from an INPUT_RECORD and store in xip->ichar.
3306 * Handles surrogate chars, and KeyUp without previous corresponding KeyDown.
3307 */
win32_get_ichar(XINPUT_RECORD * xip)3308 static lbool win32_get_ichar(XINPUT_RECORD *xip)
3309 {
3310 LWCHAR ch = xip->ir.Event.KeyEvent.uChar.UnicodeChar;
3311 xip->ichar = ch;
3312 if (!is_ascii_char(ch))
3313 {
3314 int is_down = xip->ir.Event.KeyEvent.bKeyDown;
3315 LWCHAR *last_down = find_last_down(ch);
3316 if (last_down == NULL) { /* key was up */
3317 if (is_down) { /* key was up, now is down */
3318 set_last_down(ch);
3319 } else { /* key up without previous down: pretend this is a down. */
3320 xip->ir.Event.KeyEvent.bKeyDown = 1;
3321 }
3322 } else if (!is_down) { /* key was down, now is up */
3323 *last_down = 0; /* use this last_down only once */
3324 }
3325
3326 if (ch >= 0xD800 && ch < 0xDC00) { /* high surrogate */
3327 hi_surr = 0x10000 + ((ch - 0xD800) << 10);
3328 return (FALSE); /* get next input, which should be the low surrogate */
3329 }
3330 if (ch >= 0xDC00 && ch < 0xE000) { /* low surrogate */
3331 xip->ichar = hi_surr + (ch - 0xDC00);
3332 hi_surr = 0;
3333 }
3334 }
3335 return (TRUE);
3336 }
3337
3338 /*
3339 * Handle a scan code (non-ASCII) key input.
3340 */
win32_scan_code(XINPUT_RECORD * xip)3341 static lbool win32_scan_code(XINPUT_RECORD *xip)
3342 {
3343 int scan = -1;
3344 if (xip->ir.Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
3345 {
3346 switch (xip->ir.Event.KeyEvent.wVirtualScanCode)
3347 {
3348 case PCK_RIGHT: /* right arrow */
3349 scan = PCK_CTL_RIGHT;
3350 break;
3351 case PCK_LEFT: /* left arrow */
3352 scan = PCK_CTL_LEFT;
3353 break;
3354 case PCK_DELETE: /* delete */
3355 scan = PCK_CTL_DELETE;
3356 break;
3357 case PCK_HOME:
3358 scan = PCK_CTL_HOME;
3359 break;
3360 case PCK_END:
3361 scan = PCK_CTL_END;
3362 break;
3363 }
3364 } else if (xip->ir.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED)
3365 {
3366 if (xip->ichar == '\t')
3367 scan = PCK_SHIFT_TAB;
3368 else
3369 {
3370 switch (xip->ir.Event.KeyEvent.wVirtualScanCode)
3371 {
3372 case PCK_RIGHT:
3373 scan = PCK_SHIFT_RIGHT;
3374 break;
3375 case PCK_LEFT:
3376 scan = PCK_SHIFT_LEFT;
3377 break;
3378 case PCK_HOME:
3379 scan = PCK_SHIFT_HOME;
3380 break;
3381 case PCK_END:
3382 scan = PCK_SHIFT_END;
3383 break;
3384 }
3385 }
3386 }
3387 if (scan < 0 && xip->ichar == 0)
3388 scan = xip->ir.Event.KeyEvent.wVirtualScanCode;
3389 if (scan < 0)
3390 return (FALSE);
3391 /*
3392 * An extended key returns a 2 byte sequence consisting of
3393 * a zero byte followed by the scan code.
3394 */
3395 win32_enqueue('\0');
3396 win32_enqueue(scan);
3397 return (TRUE);
3398 }
3399
3400 /*
3401 * Handle a key input event.
3402 */
win32_key_event(XINPUT_RECORD * xip,char * pch)3403 static lbool win32_key_event(XINPUT_RECORD *xip, char *pch)
3404 {
3405 int repeat;
3406 char utf8[UTF8_MAX_LENGTH];
3407 char *up;
3408
3409 if (xip->ir.EventType != KEY_EVENT ||
3410 ((xip->ir.Event.KeyEvent.dwControlKeyState & (RIGHT_ALT_PRESSED|LEFT_CTRL_PRESSED)) == (RIGHT_ALT_PRESSED|LEFT_CTRL_PRESSED) && xip->ir.Event.KeyEvent.uChar.UnicodeChar == 0) ||
3411 (xip->ir.Event.KeyEvent.wVirtualScanCode == 0 && xip->ir.Event.KeyEvent.uChar.UnicodeChar == 0) ||
3412 xip->ir.Event.KeyEvent.wVirtualScanCode == PCK_CAPS_LOCK ||
3413 xip->ir.Event.KeyEvent.wVirtualScanCode == PCK_NUM_LOCK ||
3414 (xip->ir.Event.KeyEvent.wVirtualKeyCode == VK_MENU && xip->ir.Event.KeyEvent.uChar.UnicodeChar == 0) ||
3415 xip->ir.Event.KeyEvent.wVirtualKeyCode == VK_KANJI ||
3416 xip->ir.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
3417 xip->ir.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL)
3418 return (FALSE);
3419
3420 if (!win32_get_ichar(xip))
3421 return (FALSE);
3422 if (!xip->ir.Event.KeyEvent.bKeyDown)
3423 return (FALSE);
3424
3425 /*
3426 * If caller wants to see an immediate (unqueued) ASCII char from the
3427 * keyboard, return it now. Otherwise queue the char for WIN32getch to read.
3428 */
3429 if (pch != NULL && xip->ichar != '\0' && is_ascii_char(xip->ichar))
3430 {
3431 *pch = (char) xip->ichar;
3432 return (TRUE);
3433 }
3434
3435 if (win32_scan_code(xip))
3436 return (pch == NULL);
3437
3438 repeat = xip->ir.Event.KeyEvent.wRepeatCount;
3439 if (repeat > WIN32_MAX_REPEAT)
3440 repeat = WIN32_MAX_REPEAT;
3441 up = utf8;
3442 put_wchar(&up, xip->ichar);
3443 for (; repeat > 0; --repeat)
3444 {
3445 constant char *p;
3446 for (p = utf8; p < up; ++p)
3447 win32_enqueue((unsigned char) *p);
3448 }
3449 return (pch == NULL);
3450 }
3451
3452 /*
3453 * Handle a window input event.
3454 */
win32_window_event(XINPUT_RECORD * xip)3455 static lbool win32_window_event(XINPUT_RECORD *xip)
3456 {
3457 if (xip->ir.EventType != WINDOW_BUFFER_SIZE_EVENT)
3458 return (FALSE);
3459 win32_enqueue(READ_AGAIN);
3460 return (TRUE);
3461 }
3462
3463 /*
3464 * Determine whether an input character is waiting to be read.
3465 */
win32_kbhit2(char * pch)3466 public lbool win32_kbhit2(char *pch)
3467 {
3468 XINPUT_RECORD xip;
3469
3470 if (pch == NULL && win32_queued_char())
3471 return (TRUE);
3472
3473 for (;;)
3474 {
3475 DWORD nread;
3476 DWORD console_input_mode;
3477 /*
3478 * When an input pipe closes, cmd may reset the console mode,
3479 * so set the mode every time we read input.
3480 */
3481 if (GetConsoleMode(tty, &console_input_mode) && console_input_mode != curr_console_input_mode)
3482 SetConsoleMode(tty, curr_console_input_mode);
3483 PeekConsoleInputW(tty, &xip.ir, 1, &nread);
3484 if (nread == 0)
3485 return (FALSE);
3486 ReadConsoleInputW(tty, &xip.ir, 1, &nread);
3487 if (nread == 0)
3488 return (FALSE);
3489 if (win32_key_event(&xip, pch))
3490 return (TRUE);
3491 if (win32_mouse_event(&xip) || win32_window_event(&xip))
3492 return (pch == NULL);
3493 }
3494 }
3495
win32_kbhit(void)3496 public lbool win32_kbhit(void)
3497 {
3498 return win32_kbhit2(NULL);
3499 }
3500
3501 /*
3502 * Read a character from the keyboard.
3503 */
WIN32getch(void)3504 public int WIN32getch(void)
3505 {
3506 while (!win32_kbhit())
3507 {
3508 Sleep(20);
3509 if (ABORT_SIGS())
3510 return ('\003');
3511 }
3512 return (win32_get_queue());
3513 }
3514
win32_getch_clear(void)3515 public void win32_getch_clear(void)
3516 {
3517 while (win32_kbhit())
3518 (void) WIN32getch();
3519 }
3520
3521 #endif /* MSDOS_COMPILER==WIN32C */
3522
3523 #if MSDOS_COMPILER
3524 /*
3525 */
WIN32setcolors(int fg,int bg)3526 public void WIN32setcolors(int fg, int bg)
3527 {
3528 SETCOLORS(fg, bg);
3529 }
3530
3531 /*
3532 */
WIN32textout(constant char * text,size_t len)3533 public void WIN32textout(constant char *text, size_t len)
3534 {
3535 #if MSDOS_COMPILER==WIN32C
3536 DWORD written;
3537 if (utf_mode == 2)
3538 {
3539 /*
3540 * We've got UTF-8 text in a non-UTF-8 console. Convert it to
3541 * wide and use WriteConsoleW.
3542 * Biggest input len is OUTBUF_SIZE of obuf from win_flush,
3543 * which is also the biggest output count if it's ASCII.
3544 * "static" wtext is not a state - only avoid 16K on stack.
3545 */
3546 static WCHAR wtext[OUTBUF_SIZE];
3547 len = MultiByteToWideChar(CP_UTF8, 0, text, len, wtext, countof(wtext));
3548 WriteConsoleW(con_out, wtext, len, &written, NULL);
3549 } else
3550 WriteConsole(con_out, text, (DWORD) len, &written, NULL);
3551 #else
3552 char buf[2048];
3553 if (len >= sizeof(buf))
3554 len = sizeof(buf) - 1;
3555 memcpy(buf, text, len);
3556 buf[len] = 0;
3557 cputs(buf);
3558 #endif
3559 }
3560 #endif
3561