3 /* pcterm.c -- How to handle the PC terminal for Info under MS-DOS/MS-Windows.
4 Id: pcterm.c,v 1.4 2004/04/11 17:56:46 karl Exp
6 Copyright (C) 1998, 1999, 2003, 2004 Free Software Foundation, Inc.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* WARNING WARNING WARNING!!!
24 This probably won't work as is with anything but DJGPP! However, Borland
25 should come close, and other PC compilers will need minor modifications. */
27 /* intl/libintl.h defines a macro `gettext' which
28 conflicts with conio.h header. */
31 # define gettext _gettext
38 #include "variables.h"
40 extern int speech_friendly
; /* defined in info.c */
42 /* **************************************************************** */
44 /* PC Terminal Output Functions */
46 /* **************************************************************** */
48 static struct text_info outside_info
; /* holds screen params outside Info */
49 static unsigned char norm_attr
, inv_attr
;
51 static unsigned const char * find_sequence (int);
53 /* Turn on reverse video. */
55 pc_begin_inverse (void)
60 /* Turn off reverse video. */
67 /* Move the cursor up one line. */
72 ScreenGetCursor (&y
, &x
);
73 ScreenSetCursor (MAX (y
-1, 0), x
);
76 /* Move the cursor down one line. */
81 ScreenGetCursor (&y
, &x
);
82 ScreenSetCursor (MIN (screenheight
-1, y
+1), x
);
85 /* Clear the entire terminal screen. */
87 pc_clear_screen (void)
92 /* Clear from the current position of the cursor to the end of the line. */
94 pc_clear_to_eol (void)
96 clreol (); /* perhaps to be replaced by a loop */
99 /* Set the global variables SCREENWIDTH and SCREENHEIGHT. */
101 pc_get_screen_size(void)
103 /* Current screen dimensions are the default. */
104 if (!outside_info
.screenheight
) /* paranoia */
105 gettextinfo (&outside_info
);
106 screenwidth
= outside_info
.screenwidth
;
107 screenheight
= outside_info
.screenheight
;
109 /* Environment variable "LINES" overrides the default. */
110 if (getenv ("LINES") != NULL
)
111 screenheight
= atoi (getenv ("LINES"));
113 /* Environment variable "INFO_LINES" overrides "LINES". */
114 if (getenv ("INFO_LINES") != NULL
)
115 screenheight
= atoi (getenv ("INFO_LINES"));
118 /* Move the cursor to the terminal location of X and Y. */
123 ScreenSetCursor (y
, x
); /* yes, pc.h says ScreenSetCursor (row, column) !! */
126 /* Print STRING to the terminal at the current position. */
132 fputs (string
, stdout
);
137 /* Ring the terminal bell. The bell is rung visibly if the terminal is
138 capable of doing that, and if terminal_use_visible_bell_p is non-zero. */
142 if (terminal_has_visible_bell_p
&& terminal_use_visible_bell_p
)
151 /* Print NCHARS from STRING to the terminal at the current position. */
153 pc_write_chars (string
, nchars
)
161 printf ("%.*s",nchars
, string
);
163 cprintf ("%..*s",nchars
, string
);
166 /* Scroll an area of the terminal from START to (and excluding) END,
167 AMOUNT lines. If AMOUNT is negative, the lines are scrolled
168 towards the top of the screen, else they are scrolled towards the
169 bottom of the screen. The lines of the old region which do not
170 overlap the new region are cleared, to mimic terminal operation. */
172 pc_scroll_terminal (start
, end
, amount
)
173 int start
, end
, amount
;
175 int line_to_clear
= amount
> 0 ? start
: end
+ amount
;
177 /* Move the text. Note that `movetext' expects 1-based coordinates. */
178 movetext (1, start
+ 1, ScreenCols (), end
, 1, start
+ amount
+ 1);
180 /* Now clear the lines which were left unoccupied. */
185 ScreenSetCursor (line_to_clear
++, 0);
190 /* Put the screen in the video mode and colors which Info will use.
191 Prepare to start using the terminal to read characters singly. */
193 pc_prep_terminal (void)
197 /* Do not set screen height if we already have it, because
198 doing so erases the screen. */
199 if (screenheight
!= ScreenRows ())
200 _set_screen_lines (screenheight
);
202 /* Don't fail if they asked for screen dimensions that their
203 hardware cannot support. */
204 screenheight
= ScreenRows ();
205 screenwidth
= ScreenCols ();
207 /* Try setting the colors user asked for. */
208 textattr (norm_attr
);
211 /* Switch console reads to binary mode. */
212 tty
= fileno (stdin
);
214 setmode (tty
, O_BINARY
);
215 __djgpp_set_ctrl_c (1); /* re-enable SIGINT generation by Ctrl-C */
219 /* Restore the tty settings back to what they were before we started using
222 pc_unprep_terminal (void)
226 textattr (outside_info
.normattr
);
228 /* Do not set screen height if we already have it, because
229 doing so erases the screen. */
230 if (outside_info
.screenheight
!= ScreenRows ())
232 _set_screen_lines (outside_info
.screenheight
);
236 pc_clear_to_eol (); /* for text attributes to really take effect */
238 /* Switch back to text mode on stdin. */
239 tty
= fileno (stdin
);
241 setmode (tty
, O_TEXT
);
245 /* Initialize the terminal which is known as TERMINAL_NAME. If this
246 terminal doesn't have cursor addressability, `terminal_is_dumb_p'
247 becomes nonzero. The variables SCREENHEIGHT and SCREENWIDTH are set
248 to the dimensions that this terminal actually has. The variable
249 TERMINAL_HAS_META_P becomes nonzero if this terminal supports a Meta
250 key. Finally, the terminal screen is cleared. */
252 pc_initialize_terminal (term_name
)
259 term_name
= getenv ("TERM");
261 term_name
= "pc-dos"; /* ``what's in a name?'' */
264 /* Get current video information, to be restored later. */
265 if (outside_info
.screenwidth
== 0)
266 gettextinfo (&outside_info
);
268 /* Current screen colors are the default. */
269 norm_attr
= outside_info
.normattr
;
270 inv_attr
= (((outside_info
.normattr
& 7) << 4) |
271 ((outside_info
.normattr
& 0x7f) >> 4));
273 /* Does the user want non-default colors? */
274 info_colors
= getenv ("INFO_COLORS");
275 if ((info_colors
!= (char *)0) && !speech_friendly
)
277 /* Decode a color from a string descriptor.
278 The descriptor string is a sequence of color specifiers separated
279 by a non-numeric character. Each color specifier should represent
280 a small integer which fits into an unsigned char, and can be given
281 in any base supported by strtoul. Examples of valid descriptors:
287 The separator between two color specifiers can be any character which
288 cannot be used in a printed representation of an integer number. */
290 unsigned long color_desc
= strtoul (info_colors
, &endp
, 0);
292 if (color_desc
<= UCHAR_MAX
)
294 norm_attr
= (unsigned char)color_desc
;
295 color_desc
= strtoul (endp
+ 1, &endp
, 0);
296 if (color_desc
<= UCHAR_MAX
)
297 inv_attr
= (unsigned char)color_desc
;
302 terminal_can_scroll
= 1;
304 /* We know how to produce a visible bell, if somebody's looking... */
305 if (!speech_friendly
)
306 terminal_has_visible_bell_p
= 1;
308 /* We have a Meta key. */
309 terminal_has_meta_p
= 1;
311 /* We are *certainly* NOT dumb! */
312 terminal_is_dumb_p
= 0;
314 pc_get_screen_size ();
316 /* Store the arrow keys. */
317 term_ku
= (char *)find_sequence (K_Up
);
318 term_kd
= (char *)find_sequence (K_Down
);
319 term_kr
= (char *)find_sequence (K_Right
);
320 term_kl
= (char *)find_sequence (K_Left
);
322 term_kP
= (char *)find_sequence (K_PageUp
);
323 term_kN
= (char *)find_sequence (K_PageDown
);
326 term_kh
= (char *)find_sequence (K_Home
);
327 term_ke
= (char *)find_sequence (K_End
);
328 term_ki
= (char *)find_sequence (K_Insert
);
329 term_kx
= (char *)find_sequence (K_Delete
);
332 /* Set all the hooks to our PC-specific functions. */
333 terminal_begin_inverse_hook
= pc_begin_inverse
;
334 terminal_end_inverse_hook
= pc_end_inverse
;
335 terminal_prep_terminal_hook
= pc_prep_terminal
;
336 terminal_unprep_terminal_hook
= pc_unprep_terminal
;
337 terminal_up_line_hook
= pc_up_line
;
338 terminal_down_line_hook
= pc_down_line
;
339 terminal_clear_screen_hook
= pc_clear_screen
;
340 terminal_clear_to_eol_hook
= pc_clear_to_eol
;
341 terminal_get_screen_size_hook
= pc_get_screen_size
;
342 terminal_goto_xy_hook
= pc_goto_xy
;
343 terminal_put_text_hook
= pc_put_text
;
344 terminal_ring_bell_hook
= pc_ring_bell
;
345 terminal_write_chars_hook
= pc_write_chars
;
346 terminal_scroll_terminal_hook
= pc_scroll_terminal
;
349 /* **************************************************************** */
351 /* How to Read Characters From the PC Terminal */
353 /* **************************************************************** */
355 /* This will most certainly work ONLY with DJGPP. */
359 #include <sys/fsext.h>
362 /* Translation table for some special keys.
363 Arrow keys which are standard on other keyboards are translated into
364 standard ESC-sequences, in case somebody rebinds the simple keys
365 (like C-f, C-b, C-n, etc.).
367 The strange "\033\061" prefix in some keys is a numeric argument of
368 one, which means ``do the next command once''. It is here so that
369 when the according PC key is pressed in the middle of an incremental
370 search, Info doesn't see just an ASCII character like `n' or `B',
371 and doesn't add it to the search string; instead, it will exit the
372 incremental search and then perform the command. */
376 unsigned char const * const sequence
;
377 } DJGPP_keytab
[] = { /* these are for moving between nodes... */
378 {K_Control_PageDown
, "\033\061n"},
379 {K_Control_PageUp
, "\033\061p"},
380 {K_Control_Up
, "\033\061u"},
381 {K_Control_Down
, "\033\061m"},
382 {K_Control_Center
, "\033\061l"},
385 {K_Home
, "\033[H"}, /* ...and these are for moving IN a node */
386 {K_End
, "\033[F"}, /* they're Numeric-Keypad-Keys, so */
391 {K_Left
, "\033[D"}, /* NUMLOCK should be off !! */
395 {K_PageDown
, "\033[G"},
396 {K_PageUp
, "\033[I"},
397 {K_Control_Left
, "\033b"},
398 {K_Control_Right
, "\033f"},
399 {K_Control_Home
, "\033<"},
400 {K_Control_End
, "\033>"},
403 {K_EHome
, "\033[H"}, /* these are also for moving IN a node */
404 {K_EEnd
, "\033[F"}, /* they're the "extended" (Grey) keys */
410 {K_ERight
, "\033[C"},
413 {K_EPageDown
, "\033[G"},
414 {K_EPageUp
, "\033[I"},
415 {K_Control_ELeft
, "\033b"},
416 {K_Control_ERight
, "\033f"},
417 {K_Control_EHome
, "\033<"},
418 {K_Control_EEnd
, "\033>"},
420 {K_BackTab
, "\033\011"},
421 {K_F1
, "\10"}, /* YEAH, gimme that good old F-one-thing */
422 {K_Delete
, "\177"}, /* to make Kp-Del be DEL (0x7f) */
423 {K_EDelete
, "\177"}, /* to make Delete be DEL (0x7f) */
425 {K_Insert
, "\033[L"},
426 {K_EInsert
, "\033[L"},
429 /* These are here to map more Alt-X keys to ESC X sequences. */
440 {K_Alt_LBracket
, "\033["},
441 {K_Alt_RBracket
, "\033]"},
442 {K_Alt_Return
, "\033\015"},
452 {K_Alt_Semicolon
, "\033;"},
453 {K_Alt_Quote
, "\033'"},
454 {K_Alt_Backquote
, "\033`"},
455 {K_Alt_Backslash
, "\033\\"},
463 {K_Alt_Comma
, "\033<"}, /* our reader cannot distinguish between */
464 {K_Alt_Period
, "\033>"}, /* Alt-. and Alt->, so we cheat a little */
465 {K_Alt_Slash
, "\033?"}, /* ditto, to get Alt-? */
466 {K_Alt_Backspace
, "\033\177"}, /* M-DEL, to delete word backwards */
467 {K_Alt_1
, "\033\061"},
468 {K_Alt_2
, "\033\062"},
469 {K_Alt_3
, "\033\063"},
470 {K_Alt_4
, "\033\064"},
471 {K_Alt_5
, "\033\065"},
472 {K_Alt_6
, "\033\066"},
473 {K_Alt_7
, "\033\067"},
474 {K_Alt_8
, "\033\070"},
475 {K_Alt_9
, "\033\071"},
476 {K_Alt_0
, "\033\060"},
477 {K_Alt_Dash
, "\033\055"},
478 {K_Alt_EPageUp
, "\033\033[I"},
479 {K_Alt_EPageDown
, "\033\033[G"},
480 {K_Alt_Equals
, "\033\075"},
481 {K_Alt_EDelete
, "\033\177"},
482 {K_Alt_Tab
, "\033\011"},
486 /* Given a key, return the sequence of characters which
487 our keyboard driver generates. */
488 static unsigned const char *
489 find_sequence (int key
)
493 for (i
= 0; DJGPP_keytab
[i
].inkey
; i
++)
494 if (key
== DJGPP_keytab
[i
].inkey
)
495 return DJGPP_keytab
[i
].sequence
;
497 return (unsigned const char *)NULL
;
500 /* Return zero if a key is pending in the
501 keyboard buffer, non-zero otherwise. */
503 kbd_buffer_empty (void)
508 r
.h
.ah
= 0x11; /* Get enhanced keyboard status */
509 __dpmi_int (0x16, &r
);
511 /* If the keyboard buffer is empty, the Zero Flag will be set. */
512 return (r
.x
.flags
& 0x40) == 0x40;
515 /* The buffered characters pending to be read.
516 Actually, Info usually reads a single character, but when we
517 translate a key into a sequence of characters, we keep them here. */
518 static unsigned char buffered
[512];
520 /* Index of the next buffered character to be returned. */
523 /* Return the number of characters waiting to be read. */
525 pc_term_chars_avail (void)
527 if (buf_idx
>= sizeof (buffered
)) /* paranoia */
530 buffered
[buf_idx
] = '\0';
534 return (long)strlen (buffered
+ buf_idx
);
537 /* Our special terminal keyboard reader. It will be called by
538 low-level libc functions when the application calls `read' or
539 the ANSI-standard stream-oriented read functions. If the
540 caller wants to read the terminal, we redirect the call to
541 the BIOS keyboard functions, since that lets us recognize more
542 keys than DOS does. */
544 keyboard_read (__FSEXT_Fnumber func
, int *retval
, va_list rest_args
)
546 /* When we are called, REST_ARGS are: file_descriptor, buf, nbytes. */
548 size_t nbytes
, nread
= 0;
549 int fd
= va_arg (rest_args
, int);
551 /* Is this call for us? */
552 if (func
!= __FSEXT_read
|| !isatty (fd
))
553 return 0; /* and the usual DOS call will be issued */
555 buf
= va_arg (rest_args
, unsigned char *);
556 nbytes
= va_arg (rest_args
, size_t);
570 /* Loop here until enough bytes has been read. */
575 /* If any ``buffered characters'' are left, return as much
576 of them as the caller wanted. */
577 while (buffered
[buf_idx
] && nbytes
)
579 *buf
++ = buffered
[buf_idx
++];
587 /* Wait for another key.
588 We do that in a busy-waiting loop so we don't get parked
589 inside a BIOS call, which will effectively disable signals.
590 While we wait for them to type something, we repeatedly
591 release the rest of our time slice, so that other programs
592 in a multitasking environment, such as Windows, get more cycles. */
593 while (kbd_buffer_empty ())
598 /* Translate the key if necessary.
599 Untranslated non-ASCII keys are silently ignored. */
600 if ((key
& 0x300) != 0)
602 unsigned char const * key_sequence
= find_sequence (key
);
604 if (key_sequence
!= NULL
)
606 strcpy (buffered
, key_sequence
);
610 else if (key
== K_Control_Z
)
611 raise (SIGUSR1
); /* we don't have SIGTSTP, so simulate it */
612 else if (key
<= 0xff)
622 return 1; /* meaning that we handled the call */
625 /* Install our keyboard handler.
626 This is called by the startup code before `main'. */
627 static void __attribute__((constructor
))
628 install_keyboard_handler (void)
630 __FSEXT_set_function (fileno (stdin
), keyboard_read
);
632 /* We need to set this single hook here; the rest
633 will be set by pc_initialize_terminal when it is called. */
634 terminal_initialize_terminal_hook
= pc_initialize_terminal
;
637 #endif /* __DJGPP__ */
639 /* **************************************************************** */
641 /* Emulation of SIGTSTP on Ctrl-Z */
643 /* **************************************************************** */
650 # define PATH_MAX 512
653 /* Effectively disable signals which aren't defined
654 (assuming no signal can ever be zero).
655 SIGINT is ANSI, so we expect it to be always defined. */
664 kill (pid_t pid
, int sig
)
666 static char interrupted_msg
[] = "Interrupted\r\n";
667 static char stopped_msg
[] = "Stopped. Type `exit RET' to return.\r\n";
668 char cwd
[PATH_MAX
+ 1];
673 || pid
== -getpid ())
677 RETSIGTYPE (*old_INT
)(int), (*old_QUIT
)(int);
681 /* If SIGINT was generated by a readable key, we want to remove
682 it from the PC keyboard buffer, so that DOS and other
683 programs never see it. DJGPP signal-handling mechanism
684 doesn't remove the INT key from the keyboard buffer. */
685 if (!kbd_buffer_empty ())
688 pc_write_chars (interrupted_msg
, sizeof (interrupted_msg
) - 1);
691 /* Simulate SIGTSTP by invoking a subsidiary shell. */
692 pc_goto_xy (0, outside_info
.screenheight
- 1);
694 pc_write_chars (stopped_msg
, sizeof (stopped_msg
) - 1);
696 /* The child shell can change the working directory, so
697 we need to save and restore it, since it is global. */
698 if (!getcwd (cwd
, PATH_MAX
)) /* should never happen */
701 /* We don't want to get fatal signals while the subshell runs. */
702 old_INT
= signal (SIGINT
, SIG_IGN
);
703 old_QUIT
= signal (SIGQUIT
, SIG_IGN
);
707 signal (SIGINT
, old_INT
);
708 signal (SIGQUIT
, old_QUIT
);
721 /* These should never be called, but they make the linker happy. */
723 void tputs (char *a
, int b
, int (*c
)())
728 char* tgoto (char*a
, int b
, int c
)
730 perror ("tgoto"); return 0; /* here and below, added dummy retvals */
735 perror ("tgetnum"); return 0;
738 int tgetflag (char*a
)
740 perror ("tgetflag"); return 0;
743 char* tgetstr (char *a
, char **b
)
745 perror ("tgetstr"); return 0;
748 int tgetent (char*a
, char*b
)
750 perror ("tgetent"); return 0;
753 int tcgetattr(int fildes
, struct termios
*termios_p
)
755 perror ("tcgetattr"); return 0;
758 int tcsetattr(int fd
, int opt_actions
, const struct termios
*termios_p
)
760 perror ("tcsetattr"); return 0;