1 | /* terminal.c -- how to handle the physical terminal for Info.
|
---|
2 | $Id: terminal.c,v 1.3 2004/04/11 17:56:46 karl Exp $
|
---|
3 |
|
---|
4 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1996, 1997, 1998,
|
---|
5 | 1999, 2001, 2002, 2004 Free Software Foundation, Inc.
|
---|
6 |
|
---|
7 | This program is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 2, or (at your option)
|
---|
10 | any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program; if not, write to the Free Software
|
---|
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
20 |
|
---|
21 | Originally written by Brian Fox (bfox@ai.mit.edu). */
|
---|
22 |
|
---|
23 | #include "info.h"
|
---|
24 | #include "terminal.h"
|
---|
25 | #include "termdep.h"
|
---|
26 | #ifdef __EMX__
|
---|
27 | # include <stdlib.h>
|
---|
28 | # include "pc.h"
|
---|
29 | #endif /* __EMX__ */
|
---|
30 |
|
---|
31 | #include <sys/types.h>
|
---|
32 | #include <signal.h>
|
---|
33 |
|
---|
34 | /* The Unix termcap interface code. */
|
---|
35 | #ifdef HAVE_NCURSES_TERMCAP_H
|
---|
36 | #include <ncurses/termcap.h>
|
---|
37 | #else
|
---|
38 | #ifdef HAVE_TERMCAP_H
|
---|
39 | #include <termcap.h>
|
---|
40 | #else
|
---|
41 | /* On Solaris2, sys/types.h #includes sys/reg.h, which #defines PC.
|
---|
42 | Unfortunately, PC is a global variable used by the termcap library. */
|
---|
43 | #undef PC
|
---|
44 |
|
---|
45 | /* Termcap requires these variables, whether we access them or not. */
|
---|
46 | char *BC, *UP;
|
---|
47 | char PC; /* Pad character */
|
---|
48 | short ospeed; /* Terminal output baud rate */
|
---|
49 | extern int tgetnum (), tgetflag (), tgetent ();
|
---|
50 | extern char *tgetstr (), *tgoto ();
|
---|
51 | extern void tputs ();
|
---|
52 | #endif /* not HAVE_TERMCAP_H */
|
---|
53 | #endif /* not HAVE_NCURSES_TERMCAP_H */
|
---|
54 |
|
---|
55 | /* Function "hooks". If you make one of these point to a function, that
|
---|
56 | function is called when appropriate instead of its namesake. Your
|
---|
57 | function is called with exactly the same arguments that were passed
|
---|
58 | to the namesake function. */
|
---|
59 | VFunction *terminal_begin_inverse_hook = (VFunction *)NULL;
|
---|
60 | VFunction *terminal_end_inverse_hook = (VFunction *)NULL;
|
---|
61 | VFunction *terminal_prep_terminal_hook = (VFunction *)NULL;
|
---|
62 | VFunction *terminal_unprep_terminal_hook = (VFunction *)NULL;
|
---|
63 | VFunction *terminal_up_line_hook = (VFunction *)NULL;
|
---|
64 | VFunction *terminal_down_line_hook = (VFunction *)NULL;
|
---|
65 | VFunction *terminal_clear_screen_hook = (VFunction *)NULL;
|
---|
66 | VFunction *terminal_clear_to_eol_hook = (VFunction *)NULL;
|
---|
67 | VFunction *terminal_get_screen_size_hook = (VFunction *)NULL;
|
---|
68 | VFunction *terminal_goto_xy_hook = (VFunction *)NULL;
|
---|
69 | VFunction *terminal_initialize_terminal_hook = (VFunction *)NULL;
|
---|
70 | VFunction *terminal_new_terminal_hook = (VFunction *)NULL;
|
---|
71 | VFunction *terminal_put_text_hook = (VFunction *)NULL;
|
---|
72 | VFunction *terminal_ring_bell_hook = (VFunction *)NULL;
|
---|
73 | VFunction *terminal_write_chars_hook = (VFunction *)NULL;
|
---|
74 | VFunction *terminal_scroll_terminal_hook = (VFunction *)NULL;
|
---|
75 |
|
---|
76 | /* **************************************************************** */
|
---|
77 | /* */
|
---|
78 | /* Terminal and Termcap */
|
---|
79 | /* */
|
---|
80 | /* **************************************************************** */
|
---|
81 |
|
---|
82 | /* A buffer which holds onto the current terminal description, and a pointer
|
---|
83 | used to float within it. And the name of the terminal. */
|
---|
84 | static char *term_buffer = NULL;
|
---|
85 | static char *term_string_buffer = NULL;
|
---|
86 | static char *term_name;
|
---|
87 |
|
---|
88 | /* Some strings to control terminal actions. These are output by tputs (). */
|
---|
89 | static char *term_goto, *term_clreol, *term_cr, *term_clrpag;
|
---|
90 | static char *term_begin_use, *term_end_use;
|
---|
91 | static char *term_AL, *term_DL, *term_al, *term_dl;
|
---|
92 |
|
---|
93 | static char *term_keypad_on, *term_keypad_off;
|
---|
94 |
|
---|
95 | /* How to go up a line. */
|
---|
96 | static char *term_up;
|
---|
97 |
|
---|
98 | /* How to go down a line. */
|
---|
99 | static char *term_dn;
|
---|
100 |
|
---|
101 | /* An audible bell, if the terminal can be made to make noise. */
|
---|
102 | static char *audible_bell;
|
---|
103 |
|
---|
104 | /* A visible bell, if the terminal can be made to flash the screen. */
|
---|
105 | static char *visible_bell;
|
---|
106 |
|
---|
107 | /* The string to write to turn on the meta key, if this term has one. */
|
---|
108 | static char *term_mm;
|
---|
109 |
|
---|
110 | /* The string to turn on inverse mode, if this term has one. */
|
---|
111 | static char *term_invbeg;
|
---|
112 |
|
---|
113 | /* The string to turn off inverse mode, if this term has one. */
|
---|
114 | static char *term_invend;
|
---|
115 |
|
---|
116 | /* Although I can't find any documentation that says this is supposed to
|
---|
117 | return its argument, all the code I've looked at (termutils, less)
|
---|
118 | does so, so fine. */
|
---|
119 | static int
|
---|
120 | output_character_function (int c)
|
---|
121 | {
|
---|
122 | putc (c, stdout);
|
---|
123 | return c;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /* Macro to send STRING to the terminal. */
|
---|
127 | #define send_to_terminal(string) \
|
---|
128 | do { \
|
---|
129 | if (string) \
|
---|
130 | tputs (string, 1, output_character_function); \
|
---|
131 | } while (0)
|
---|
132 |
|
---|
133 | /* Tell the terminal that we will be doing cursor addressable motion. */
|
---|
134 | static void
|
---|
135 | terminal_begin_using_terminal (void)
|
---|
136 | {
|
---|
137 | RETSIGTYPE (*sigsave) (int signum);
|
---|
138 |
|
---|
139 | if (term_keypad_on)
|
---|
140 | send_to_terminal (term_keypad_on);
|
---|
141 |
|
---|
142 | if (!term_begin_use || !*term_begin_use)
|
---|
143 | return;
|
---|
144 |
|
---|
145 | #ifdef SIGWINCH
|
---|
146 | sigsave = signal (SIGWINCH, SIG_IGN);
|
---|
147 | #endif
|
---|
148 |
|
---|
149 | send_to_terminal (term_begin_use);
|
---|
150 | fflush (stdout);
|
---|
151 | if (STREQ (term_name, "sun-cmd"))
|
---|
152 | /* Without this fflush and sleep, running info in a shelltool or
|
---|
153 | cmdtool (TERM=sun-cmd) with scrollbars loses -- the scrollbars are
|
---|
154 | not restored properly.
|
---|
155 | From: strube@physik3.gwdg.de (Hans Werner Strube). */
|
---|
156 | sleep (1);
|
---|
157 |
|
---|
158 | #ifdef SIGWINCH
|
---|
159 | signal (SIGWINCH, sigsave);
|
---|
160 | #endif
|
---|
161 | }
|
---|
162 |
|
---|
163 | /* Tell the terminal that we will not be doing any more cursor
|
---|
164 | addressable motion. */
|
---|
165 | static void
|
---|
166 | terminal_end_using_terminal (void)
|
---|
167 | {
|
---|
168 | RETSIGTYPE (*sigsave) (int signum);
|
---|
169 |
|
---|
170 | if (term_keypad_off)
|
---|
171 | send_to_terminal (term_keypad_off);
|
---|
172 |
|
---|
173 | if (!term_end_use || !*term_end_use)
|
---|
174 | return;
|
---|
175 |
|
---|
176 | #ifdef SIGWINCH
|
---|
177 | sigsave = signal (SIGWINCH, SIG_IGN);
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | send_to_terminal (term_end_use);
|
---|
181 | fflush (stdout);
|
---|
182 | if (STREQ (term_name, "sun-cmd"))
|
---|
183 | /* See comments at other sleep. */
|
---|
184 | sleep (1);
|
---|
185 |
|
---|
186 | #ifdef SIGWINCH
|
---|
187 | signal (SIGWINCH, sigsave);
|
---|
188 | #endif
|
---|
189 | }
|
---|
190 | |
---|
191 |
|
---|
192 | /* **************************************************************** */
|
---|
193 | /* */
|
---|
194 | /* Necessary Terminal Functions */
|
---|
195 | /* */
|
---|
196 | /* **************************************************************** */
|
---|
197 |
|
---|
198 | /* The functions and variables on this page implement the user visible
|
---|
199 | portion of the terminal interface. */
|
---|
200 |
|
---|
201 | /* The width and height of the terminal. */
|
---|
202 | int screenwidth, screenheight;
|
---|
203 |
|
---|
204 | /* Non-zero means this terminal can't really do anything. */
|
---|
205 | int terminal_is_dumb_p = 0;
|
---|
206 |
|
---|
207 | /* Non-zero means that this terminal has a meta key. */
|
---|
208 | int terminal_has_meta_p = 0;
|
---|
209 |
|
---|
210 | /* Non-zero means that this terminal can produce a visible bell. */
|
---|
211 | int terminal_has_visible_bell_p = 0;
|
---|
212 |
|
---|
213 | /* Non-zero means to use that visible bell if at all possible. */
|
---|
214 | int terminal_use_visible_bell_p = 0;
|
---|
215 |
|
---|
216 | /* Non-zero means that the terminal can do scrolling. */
|
---|
217 | int terminal_can_scroll = 0;
|
---|
218 |
|
---|
219 | /* The key sequences output by the arrow keys, if this terminal has any. */
|
---|
220 | char *term_ku = NULL;
|
---|
221 | char *term_kd = NULL;
|
---|
222 | char *term_kr = NULL;
|
---|
223 | char *term_kl = NULL;
|
---|
224 | char *term_kP = NULL; /* page-up */
|
---|
225 | char *term_kN = NULL; /* page-down */
|
---|
226 | char *term_kh = NULL; /* home */
|
---|
227 | char *term_ke = NULL; /* end */
|
---|
228 | char *term_kD = NULL; /* delete */
|
---|
229 | char *term_ki = NULL; /* ins */
|
---|
230 | char *term_kx = NULL; /* del */
|
---|
231 |
|
---|
232 | /* Move the cursor to the terminal location of X and Y. */
|
---|
233 | void
|
---|
234 | terminal_goto_xy (int x, int y)
|
---|
235 | {
|
---|
236 | if (terminal_goto_xy_hook)
|
---|
237 | (*terminal_goto_xy_hook) (x, y);
|
---|
238 | else
|
---|
239 | {
|
---|
240 | if (term_goto)
|
---|
241 | tputs (tgoto (term_goto, x, y), 1, output_character_function);
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | /* Print STRING to the terminal at the current position. */
|
---|
246 | void
|
---|
247 | terminal_put_text (char *string)
|
---|
248 | {
|
---|
249 | if (terminal_put_text_hook)
|
---|
250 | (*terminal_put_text_hook) (string);
|
---|
251 | else
|
---|
252 | {
|
---|
253 | printf ("%s", string);
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | /* Print NCHARS from STRING to the terminal at the current position. */
|
---|
258 | void
|
---|
259 | terminal_write_chars (char *string, int nchars)
|
---|
260 | {
|
---|
261 | if (terminal_write_chars_hook)
|
---|
262 | (*terminal_write_chars_hook) (string, nchars);
|
---|
263 | else
|
---|
264 | {
|
---|
265 | if (nchars)
|
---|
266 | fwrite (string, 1, nchars, stdout);
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | /* Clear from the current position of the cursor to the end of the line. */
|
---|
271 | void
|
---|
272 | terminal_clear_to_eol (void)
|
---|
273 | {
|
---|
274 | if (terminal_clear_to_eol_hook)
|
---|
275 | (*terminal_clear_to_eol_hook) ();
|
---|
276 | else
|
---|
277 | {
|
---|
278 | send_to_terminal (term_clreol);
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | /* Clear the entire terminal screen. */
|
---|
283 | void
|
---|
284 | terminal_clear_screen (void)
|
---|
285 | {
|
---|
286 | if (terminal_clear_screen_hook)
|
---|
287 | (*terminal_clear_screen_hook) ();
|
---|
288 | else
|
---|
289 | {
|
---|
290 | send_to_terminal (term_clrpag);
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | /* Move the cursor up one line. */
|
---|
295 | void
|
---|
296 | terminal_up_line (void)
|
---|
297 | {
|
---|
298 | if (terminal_up_line_hook)
|
---|
299 | (*terminal_up_line_hook) ();
|
---|
300 | else
|
---|
301 | {
|
---|
302 | send_to_terminal (term_up);
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | /* Move the cursor down one line. */
|
---|
307 | void
|
---|
308 | terminal_down_line (void)
|
---|
309 | {
|
---|
310 | if (terminal_down_line_hook)
|
---|
311 | (*terminal_down_line_hook) ();
|
---|
312 | else
|
---|
313 | {
|
---|
314 | send_to_terminal (term_dn);
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | /* Turn on reverse video if possible. */
|
---|
319 | void
|
---|
320 | terminal_begin_inverse (void)
|
---|
321 | {
|
---|
322 | if (terminal_begin_inverse_hook)
|
---|
323 | (*terminal_begin_inverse_hook) ();
|
---|
324 | else
|
---|
325 | {
|
---|
326 | send_to_terminal (term_invbeg);
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | /* Turn off reverse video if possible. */
|
---|
331 | void
|
---|
332 | terminal_end_inverse (void)
|
---|
333 | {
|
---|
334 | if (terminal_end_inverse_hook)
|
---|
335 | (*terminal_end_inverse_hook) ();
|
---|
336 | else
|
---|
337 | {
|
---|
338 | send_to_terminal (term_invend);
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | /* Ring the terminal bell. The bell is run visibly if it both has one and
|
---|
343 | terminal_use_visible_bell_p is non-zero. */
|
---|
344 | void
|
---|
345 | terminal_ring_bell (void)
|
---|
346 | {
|
---|
347 | if (terminal_ring_bell_hook)
|
---|
348 | (*terminal_ring_bell_hook) ();
|
---|
349 | else
|
---|
350 | {
|
---|
351 | if (terminal_has_visible_bell_p && terminal_use_visible_bell_p)
|
---|
352 | send_to_terminal (visible_bell);
|
---|
353 | else
|
---|
354 | send_to_terminal (audible_bell);
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | /* At the line START, delete COUNT lines from the terminal display. */
|
---|
359 | static void
|
---|
360 | terminal_delete_lines (int start, int count)
|
---|
361 | {
|
---|
362 | int lines;
|
---|
363 |
|
---|
364 | /* Normalize arguments. */
|
---|
365 | if (start < 0)
|
---|
366 | start = 0;
|
---|
367 |
|
---|
368 | lines = screenheight - start;
|
---|
369 | terminal_goto_xy (0, start);
|
---|
370 | if (term_DL)
|
---|
371 | tputs (tgoto (term_DL, 0, count), lines, output_character_function);
|
---|
372 | else
|
---|
373 | {
|
---|
374 | while (count--)
|
---|
375 | tputs (term_dl, lines, output_character_function);
|
---|
376 | }
|
---|
377 |
|
---|
378 | fflush (stdout);
|
---|
379 | }
|
---|
380 |
|
---|
381 | /* At the line START, insert COUNT lines in the terminal display. */
|
---|
382 | static void
|
---|
383 | terminal_insert_lines (int start, int count)
|
---|
384 | {
|
---|
385 | int lines;
|
---|
386 |
|
---|
387 | /* Normalize arguments. */
|
---|
388 | if (start < 0)
|
---|
389 | start = 0;
|
---|
390 |
|
---|
391 | lines = screenheight - start;
|
---|
392 | terminal_goto_xy (0, start);
|
---|
393 |
|
---|
394 | if (term_AL)
|
---|
395 | tputs (tgoto (term_AL, 0, count), lines, output_character_function);
|
---|
396 | else
|
---|
397 | {
|
---|
398 | while (count--)
|
---|
399 | tputs (term_al, lines, output_character_function);
|
---|
400 | }
|
---|
401 |
|
---|
402 | fflush (stdout);
|
---|
403 | }
|
---|
404 |
|
---|
405 | /* Scroll an area of the terminal, starting with the region from START
|
---|
406 | to END, AMOUNT lines. If AMOUNT is negative, the lines are scrolled
|
---|
407 | towards the top of the screen, else they are scrolled towards the
|
---|
408 | bottom of the screen. */
|
---|
409 | void
|
---|
410 | terminal_scroll_terminal (int start, int end, int amount)
|
---|
411 | {
|
---|
412 | if (!terminal_can_scroll)
|
---|
413 | return;
|
---|
414 |
|
---|
415 | /* Any scrolling at all? */
|
---|
416 | if (amount == 0)
|
---|
417 | return;
|
---|
418 |
|
---|
419 | if (terminal_scroll_terminal_hook)
|
---|
420 | (*terminal_scroll_terminal_hook) (start, end, amount);
|
---|
421 | else
|
---|
422 | {
|
---|
423 | /* If we are scrolling down, delete AMOUNT lines at END. Then insert
|
---|
424 | AMOUNT lines at START. */
|
---|
425 | if (amount > 0)
|
---|
426 | {
|
---|
427 | terminal_delete_lines (end, amount);
|
---|
428 | terminal_insert_lines (start, amount);
|
---|
429 | }
|
---|
430 |
|
---|
431 | /* If we are scrolling up, delete AMOUNT lines before START. This
|
---|
432 | actually does the upwards scroll. Then, insert AMOUNT lines
|
---|
433 | after the already scrolled region (i.e., END - AMOUNT). */
|
---|
434 | if (amount < 0)
|
---|
435 | {
|
---|
436 | int abs_amount = -amount;
|
---|
437 | terminal_delete_lines (start - abs_amount, abs_amount);
|
---|
438 | terminal_insert_lines (end - abs_amount, abs_amount);
|
---|
439 | }
|
---|
440 | }
|
---|
441 | }
|
---|
442 |
|
---|
443 | /* Re-initialize the terminal considering that the TERM/TERMCAP variable
|
---|
444 | has changed. */
|
---|
445 | void
|
---|
446 | terminal_new_terminal (char *terminal_name)
|
---|
447 | {
|
---|
448 | if (terminal_new_terminal_hook)
|
---|
449 | (*terminal_new_terminal_hook) (terminal_name);
|
---|
450 | else
|
---|
451 | {
|
---|
452 | terminal_initialize_terminal (terminal_name);
|
---|
453 | }
|
---|
454 | }
|
---|
455 |
|
---|
456 | /* Set the global variables SCREENWIDTH and SCREENHEIGHT. */
|
---|
457 | void
|
---|
458 | terminal_get_screen_size (void)
|
---|
459 | {
|
---|
460 | if (terminal_get_screen_size_hook)
|
---|
461 | (*terminal_get_screen_size_hook) ();
|
---|
462 | else
|
---|
463 | {
|
---|
464 | screenwidth = screenheight = 0;
|
---|
465 |
|
---|
466 | #ifdef __EMX__
|
---|
467 | {
|
---|
468 | int s[2];
|
---|
469 | _scrsize (s);
|
---|
470 | screenwidth = s[0];
|
---|
471 | screenheight = s[1];
|
---|
472 | if (getenv ("WINDOWID"))
|
---|
473 | {
|
---|
474 | FILE *fd = popen ("scrsize","rt");
|
---|
475 | if (fd)
|
---|
476 | {
|
---|
477 | fscanf (fd, "%i %i", &screenwidth, &screenheight);
|
---|
478 | pclose (fd);
|
---|
479 | }
|
---|
480 | }
|
---|
481 | }
|
---|
482 | #endif /* __EMX__ */
|
---|
483 |
|
---|
484 | #if defined (TIOCGWINSZ)
|
---|
485 | {
|
---|
486 | struct winsize window_size;
|
---|
487 |
|
---|
488 | if (ioctl (fileno (stdout), TIOCGWINSZ, &window_size) == 0)
|
---|
489 | {
|
---|
490 | screenwidth = (int) window_size.ws_col;
|
---|
491 | screenheight = (int) window_size.ws_row;
|
---|
492 | }
|
---|
493 | }
|
---|
494 | #endif /* TIOCGWINSZ */
|
---|
495 |
|
---|
496 | /* Environment variable COLUMNS overrides setting of "co". */
|
---|
497 | if (screenwidth <= 0)
|
---|
498 | {
|
---|
499 | char *sw = getenv ("COLUMNS");
|
---|
500 |
|
---|
501 | if (sw)
|
---|
502 | screenwidth = atoi (sw);
|
---|
503 |
|
---|
504 | if (screenwidth <= 0)
|
---|
505 | screenwidth = tgetnum ("co");
|
---|
506 | }
|
---|
507 |
|
---|
508 | /* Environment variable LINES overrides setting of "li". */
|
---|
509 | if (screenheight <= 0)
|
---|
510 | {
|
---|
511 | char *sh = getenv ("LINES");
|
---|
512 |
|
---|
513 | if (sh)
|
---|
514 | screenheight = atoi (sh);
|
---|
515 |
|
---|
516 | if (screenheight <= 0)
|
---|
517 | screenheight = tgetnum ("li");
|
---|
518 | }
|
---|
519 |
|
---|
520 | /* If all else fails, default to 80x24 terminal. */
|
---|
521 | if (screenwidth <= 0)
|
---|
522 | screenwidth = 80;
|
---|
523 |
|
---|
524 | if (screenheight <= 0)
|
---|
525 | screenheight = 24;
|
---|
526 | }
|
---|
527 | }
|
---|
528 |
|
---|
529 | /* Initialize the terminal which is known as TERMINAL_NAME. If this
|
---|
530 | terminal doesn't have cursor addressability, `terminal_is_dumb_p'
|
---|
531 | becomes nonzero. The variables SCREENHEIGHT and SCREENWIDTH are set
|
---|
532 | to the dimensions that this terminal actually has. The variable
|
---|
533 | TERMINAL_HAS_META_P becomes nonzero if this terminal supports a Meta
|
---|
534 | key. Finally, the terminal screen is cleared. */
|
---|
535 | void
|
---|
536 | terminal_initialize_terminal (char *terminal_name)
|
---|
537 | {
|
---|
538 | char *buffer;
|
---|
539 |
|
---|
540 | terminal_is_dumb_p = 0;
|
---|
541 |
|
---|
542 | if (terminal_initialize_terminal_hook)
|
---|
543 | {
|
---|
544 | (*terminal_initialize_terminal_hook) (terminal_name);
|
---|
545 | return;
|
---|
546 | }
|
---|
547 |
|
---|
548 | term_name = terminal_name ? terminal_name : getenv ("TERM");
|
---|
549 | if (!term_name)
|
---|
550 | term_name = "dumb";
|
---|
551 |
|
---|
552 | if (!term_string_buffer)
|
---|
553 | term_string_buffer = xmalloc (2048);
|
---|
554 |
|
---|
555 | if (!term_buffer)
|
---|
556 | term_buffer = xmalloc (2048);
|
---|
557 |
|
---|
558 | buffer = term_string_buffer;
|
---|
559 |
|
---|
560 | term_clrpag = term_cr = term_clreol = NULL;
|
---|
561 |
|
---|
562 | /* HP-UX 11.x returns 0 for OK --jeff.hull@state.co.us. */
|
---|
563 | if (tgetent (term_buffer, term_name) < 0)
|
---|
564 | {
|
---|
565 | terminal_is_dumb_p = 1;
|
---|
566 | screenwidth = 80;
|
---|
567 | screenheight = 24;
|
---|
568 | term_cr = "\r";
|
---|
569 | term_up = term_dn = audible_bell = visible_bell = NULL;
|
---|
570 | term_ku = term_kd = term_kl = term_kr = NULL;
|
---|
571 | term_kP = term_kN = NULL;
|
---|
572 | term_kh = term_ke = NULL;
|
---|
573 | term_kD = NULL;
|
---|
574 | return;
|
---|
575 | }
|
---|
576 |
|
---|
577 | BC = tgetstr ("pc", &buffer);
|
---|
578 | PC = BC ? *BC : 0;
|
---|
579 |
|
---|
580 | #if defined (HAVE_TERMIOS_H)
|
---|
581 | {
|
---|
582 | struct termios ti;
|
---|
583 | if (tcgetattr (fileno(stdout), &ti) != -1)
|
---|
584 | ospeed = cfgetospeed (&ti);
|
---|
585 | else
|
---|
586 | ospeed = B9600;
|
---|
587 | }
|
---|
588 | #else
|
---|
589 | # if defined (TIOCGETP)
|
---|
590 | {
|
---|
591 | struct sgttyb sg;
|
---|
592 |
|
---|
593 | if (ioctl (fileno (stdout), TIOCGETP, &sg) != -1)
|
---|
594 | ospeed = sg.sg_ospeed;
|
---|
595 | else
|
---|
596 | ospeed = B9600;
|
---|
597 | }
|
---|
598 | # else
|
---|
599 | ospeed = B9600;
|
---|
600 | # endif /* !TIOCGETP */
|
---|
601 | #endif
|
---|
602 |
|
---|
603 | term_cr = tgetstr ("cr", &buffer);
|
---|
604 | term_clreol = tgetstr ("ce", &buffer);
|
---|
605 | term_clrpag = tgetstr ("cl", &buffer);
|
---|
606 | term_goto = tgetstr ("cm", &buffer);
|
---|
607 |
|
---|
608 | /* Find out about this terminal's scrolling capability. */
|
---|
609 | term_AL = tgetstr ("AL", &buffer);
|
---|
610 | term_DL = tgetstr ("DL", &buffer);
|
---|
611 | term_al = tgetstr ("al", &buffer);
|
---|
612 | term_dl = tgetstr ("dl", &buffer);
|
---|
613 |
|
---|
614 | terminal_can_scroll = ((term_AL || term_al) && (term_DL || term_dl));
|
---|
615 |
|
---|
616 | term_invbeg = tgetstr ("mr", &buffer);
|
---|
617 | if (term_invbeg)
|
---|
618 | term_invend = tgetstr ("me", &buffer);
|
---|
619 | else
|
---|
620 | term_invend = NULL;
|
---|
621 |
|
---|
622 | if (!term_cr)
|
---|
623 | term_cr = "\r";
|
---|
624 |
|
---|
625 | terminal_get_screen_size ();
|
---|
626 |
|
---|
627 | term_up = tgetstr ("up", &buffer);
|
---|
628 | term_dn = tgetstr ("dn", &buffer);
|
---|
629 | visible_bell = tgetstr ("vb", &buffer);
|
---|
630 | terminal_has_visible_bell_p = (visible_bell != NULL);
|
---|
631 | audible_bell = tgetstr ("bl", &buffer);
|
---|
632 | if (!audible_bell)
|
---|
633 | audible_bell = "\007";
|
---|
634 | term_begin_use = tgetstr ("ti", &buffer);
|
---|
635 | term_end_use = tgetstr ("te", &buffer);
|
---|
636 |
|
---|
637 | term_keypad_on = tgetstr ("ks", &buffer);
|
---|
638 | term_keypad_off = tgetstr ("ke", &buffer);
|
---|
639 |
|
---|
640 | /* Check to see if this terminal has a meta key. */
|
---|
641 | terminal_has_meta_p = (tgetflag ("km") || tgetflag ("MT"));
|
---|
642 | if (terminal_has_meta_p)
|
---|
643 | {
|
---|
644 | term_mm = tgetstr ("mm", &buffer);
|
---|
645 | }
|
---|
646 | else
|
---|
647 | {
|
---|
648 | term_mm = NULL;
|
---|
649 | }
|
---|
650 |
|
---|
651 | /* Attempt to find the arrow keys. */
|
---|
652 | term_ku = tgetstr ("ku", &buffer);
|
---|
653 | term_kd = tgetstr ("kd", &buffer);
|
---|
654 | term_kr = tgetstr ("kr", &buffer);
|
---|
655 | term_kl = tgetstr ("kl", &buffer);
|
---|
656 |
|
---|
657 | term_kP = tgetstr ("kP", &buffer);
|
---|
658 | term_kN = tgetstr ("kN", &buffer);
|
---|
659 |
|
---|
660 | #if defined(INFOKEY)
|
---|
661 | term_kh = tgetstr ("kh", &buffer);
|
---|
662 | term_ke = tgetstr ("@7", &buffer);
|
---|
663 | term_ki = tgetstr ("kI", &buffer);
|
---|
664 | term_kx = tgetstr ("kD", &buffer);
|
---|
665 | #endif /* defined(INFOKEY) */
|
---|
666 |
|
---|
667 | /* Home and end keys. */
|
---|
668 | term_kh = tgetstr ("kh", &buffer);
|
---|
669 | term_ke = tgetstr ("@7", &buffer);
|
---|
670 |
|
---|
671 | term_kD = tgetstr ("kD", &buffer);
|
---|
672 |
|
---|
673 | /* If this terminal is not cursor addressable, then it is really dumb. */
|
---|
674 | if (!term_goto)
|
---|
675 | terminal_is_dumb_p = 1;
|
---|
676 | }
|
---|
677 | |
---|
678 |
|
---|
679 | /* How to read characters from the terminal. */
|
---|
680 |
|
---|
681 | #if defined (HAVE_TERMIOS_H)
|
---|
682 | struct termios original_termios, ttybuff;
|
---|
683 | #else
|
---|
684 | # if defined (HAVE_TERMIO_H)
|
---|
685 | /* A buffer containing the terminal mode flags upon entry to info. */
|
---|
686 | struct termio original_termio, ttybuff;
|
---|
687 | # else /* !HAVE_TERMIO_H */
|
---|
688 | /* Buffers containing the terminal mode flags upon entry to info. */
|
---|
689 | int original_tty_flags = 0;
|
---|
690 | int original_lmode;
|
---|
691 | struct sgttyb ttybuff;
|
---|
692 |
|
---|
693 | # if defined(TIOCGETC) && defined(M_XENIX)
|
---|
694 | /* SCO 3.2v5.0.2 defines but does not support TIOCGETC. Gak. Maybe
|
---|
695 | better fix would be to use Posix termios in preference. --gildea,
|
---|
696 | 1jul99. */
|
---|
697 | # undef TIOCGETC
|
---|
698 | # endif
|
---|
699 |
|
---|
700 | # if defined (TIOCGETC)
|
---|
701 | /* A buffer containing the terminal interrupt characters upon entry
|
---|
702 | to Info. */
|
---|
703 | struct tchars original_tchars;
|
---|
704 | # endif
|
---|
705 |
|
---|
706 | # if defined (TIOCGLTC)
|
---|
707 | /* A buffer containing the local terminal mode characters upon entry
|
---|
708 | to Info. */
|
---|
709 | struct ltchars original_ltchars;
|
---|
710 | # endif
|
---|
711 | # endif /* !HAVE_TERMIO_H */
|
---|
712 | #endif /* !HAVE_TERMIOS_H */
|
---|
713 |
|
---|
714 | /* Prepare to start using the terminal to read characters singly. */
|
---|
715 | void
|
---|
716 | terminal_prep_terminal (void)
|
---|
717 | {
|
---|
718 | int tty;
|
---|
719 |
|
---|
720 | if (terminal_prep_terminal_hook)
|
---|
721 | {
|
---|
722 | (*terminal_prep_terminal_hook) ();
|
---|
723 | return;
|
---|
724 | }
|
---|
725 |
|
---|
726 | terminal_begin_using_terminal ();
|
---|
727 |
|
---|
728 | tty = fileno (stdin);
|
---|
729 |
|
---|
730 | #ifdef __EMX__
|
---|
731 | if (is_xfree86 == XFREE86_NO)
|
---|
732 | pc_init ();
|
---|
733 | else
|
---|
734 | {
|
---|
735 | #endif /* __EMX__ */
|
---|
736 |
|
---|
737 | #if defined (HAVE_TERMIOS_H)
|
---|
738 | tcgetattr (tty, &original_termios);
|
---|
739 | tcgetattr (tty, &ttybuff);
|
---|
740 | #else
|
---|
741 | # if defined (HAVE_TERMIO_H)
|
---|
742 | ioctl (tty, TCGETA, &original_termio);
|
---|
743 | ioctl (tty, TCGETA, &ttybuff);
|
---|
744 | # endif
|
---|
745 | #endif
|
---|
746 |
|
---|
747 | #if defined (HAVE_TERMIOS_H) || defined (HAVE_TERMIO_H)
|
---|
748 | ttybuff.c_iflag &= (~ISTRIP & ~INLCR & ~IGNCR & ~ICRNL & ~IXON);
|
---|
749 | /* These output flags are not part of POSIX, so only use them if they
|
---|
750 | are defined. */
|
---|
751 | #ifdef ONLCR
|
---|
752 | ttybuff.c_oflag &= ~ONLCR ;
|
---|
753 | #endif
|
---|
754 | #ifdef OCRNL
|
---|
755 | ttybuff.c_oflag &= ~OCRNL;
|
---|
756 | #endif
|
---|
757 | ttybuff.c_lflag &= (~ICANON & ~ECHO);
|
---|
758 |
|
---|
759 | ttybuff.c_cc[VMIN] = 1;
|
---|
760 | ttybuff.c_cc[VTIME] = 0;
|
---|
761 |
|
---|
762 | if (ttybuff.c_cc[VINTR] == '\177')
|
---|
763 | ttybuff.c_cc[VINTR] = -1;
|
---|
764 |
|
---|
765 | if (ttybuff.c_cc[VQUIT] == '\177')
|
---|
766 | ttybuff.c_cc[VQUIT] = -1;
|
---|
767 |
|
---|
768 | #ifdef VLNEXT
|
---|
769 | if (ttybuff.c_cc[VLNEXT] == '\026')
|
---|
770 | ttybuff.c_cc[VLNEXT] = -1;
|
---|
771 | #endif /* VLNEXT */
|
---|
772 | #endif /* TERMIOS or TERMIO */
|
---|
773 |
|
---|
774 | /* cf. emacs/src/sysdep.c for being sure output is on. */
|
---|
775 | #if defined (HAVE_TERMIOS_H)
|
---|
776 | /* linux kernel 2.2.x needs a TCOFF followed by a TCOON to turn output
|
---|
777 | back on if the user presses ^S at the very beginning; just a TCOON
|
---|
778 | doesn't work. --Kevin Ryde <user42@zip.com.au>, 16jun2000. */
|
---|
779 | tcsetattr (tty, TCSANOW, &ttybuff);
|
---|
780 | # ifdef TCOON
|
---|
781 | tcflow (tty, TCOOFF);
|
---|
782 | tcflow (tty, TCOON);
|
---|
783 | # endif
|
---|
784 | #else
|
---|
785 | # if defined (HAVE_TERMIO_H)
|
---|
786 | ioctl (tty, TCSETA, &ttybuff);
|
---|
787 | # ifdef TCXONC
|
---|
788 | ioctl (tty, TCXONC, 1);
|
---|
789 | # endif
|
---|
790 | # endif
|
---|
791 | #endif
|
---|
792 |
|
---|
793 | #if !defined (HAVE_TERMIOS_H) && !defined (HAVE_TERMIO_H)
|
---|
794 | ioctl (tty, TIOCGETP, &ttybuff);
|
---|
795 |
|
---|
796 | if (!original_tty_flags)
|
---|
797 | original_tty_flags = ttybuff.sg_flags;
|
---|
798 |
|
---|
799 | /* Make this terminal pass 8 bits around while we are using it. */
|
---|
800 | # if defined (PASS8)
|
---|
801 | ttybuff.sg_flags |= PASS8;
|
---|
802 | # endif /* PASS8 */
|
---|
803 |
|
---|
804 | # if defined (TIOCLGET) && defined (LPASS8)
|
---|
805 | {
|
---|
806 | int flags;
|
---|
807 | ioctl (tty, TIOCLGET, &flags);
|
---|
808 | original_lmode = flags;
|
---|
809 | flags |= LPASS8;
|
---|
810 | ioctl (tty, TIOCLSET, &flags);
|
---|
811 | }
|
---|
812 | # endif /* TIOCLGET && LPASS8 */
|
---|
813 |
|
---|
814 | # if defined (TIOCGETC)
|
---|
815 | {
|
---|
816 | struct tchars temp;
|
---|
817 |
|
---|
818 | ioctl (tty, TIOCGETC, &original_tchars);
|
---|
819 | temp = original_tchars;
|
---|
820 |
|
---|
821 | /* C-s and C-q. */
|
---|
822 | temp.t_startc = temp.t_stopc = -1;
|
---|
823 |
|
---|
824 | /* Often set to C-d. */
|
---|
825 | temp.t_eofc = -1;
|
---|
826 |
|
---|
827 | /* If the a quit or interrupt character conflicts with one of our
|
---|
828 | commands, then make it go away. */
|
---|
829 | if (temp.t_intrc == '\177')
|
---|
830 | temp.t_intrc = -1;
|
---|
831 |
|
---|
832 | if (temp.t_quitc == '\177')
|
---|
833 | temp.t_quitc = -1;
|
---|
834 |
|
---|
835 | ioctl (tty, TIOCSETC, &temp);
|
---|
836 | }
|
---|
837 | # endif /* TIOCGETC */
|
---|
838 |
|
---|
839 | # if defined (TIOCGLTC)
|
---|
840 | {
|
---|
841 | struct ltchars temp;
|
---|
842 |
|
---|
843 | ioctl (tty, TIOCGLTC, &original_ltchars);
|
---|
844 | temp = original_ltchars;
|
---|
845 |
|
---|
846 | /* Make the interrupt keys go away. Just enough to make people happy. */
|
---|
847 | temp.t_lnextc = -1; /* C-v. */
|
---|
848 | temp.t_dsuspc = -1; /* C-y. */
|
---|
849 | temp.t_flushc = -1; /* C-o. */
|
---|
850 | ioctl (tty, TIOCSLTC, &temp);
|
---|
851 | }
|
---|
852 | # endif /* TIOCGLTC */
|
---|
853 |
|
---|
854 | ttybuff.sg_flags &= ~ECHO;
|
---|
855 | ttybuff.sg_flags |= CBREAK;
|
---|
856 | ioctl (tty, TIOCSETN, &ttybuff);
|
---|
857 | #endif /* !HAVE_TERMIOS_H && !HAVE_TERMIO_H */
|
---|
858 | #ifdef __EMX__
|
---|
859 | }
|
---|
860 | #endif /* __EMX__ */
|
---|
861 | }
|
---|
862 |
|
---|
863 | /* Restore the tty settings back to what they were before we started using
|
---|
864 | this terminal. */
|
---|
865 | void
|
---|
866 | terminal_unprep_terminal (void)
|
---|
867 | {
|
---|
868 | int tty;
|
---|
869 |
|
---|
870 | if (terminal_unprep_terminal_hook)
|
---|
871 | {
|
---|
872 | (*terminal_unprep_terminal_hook) ();
|
---|
873 | return;
|
---|
874 | }
|
---|
875 |
|
---|
876 | tty = fileno (stdin);
|
---|
877 |
|
---|
878 | #ifdef __EMX__
|
---|
879 | if ( is_xfree86 == XFREE86_NO )
|
---|
880 | pc_exit ();
|
---|
881 | else
|
---|
882 | {
|
---|
883 | #endif /* __EMX__ */
|
---|
884 |
|
---|
885 | #if defined (HAVE_TERMIOS_H)
|
---|
886 | tcsetattr (tty, TCSANOW, &original_termios);
|
---|
887 | #else
|
---|
888 | # if defined (HAVE_TERMIO_H)
|
---|
889 | ioctl (tty, TCSETA, &original_termio);
|
---|
890 | # else /* !HAVE_TERMIO_H */
|
---|
891 | ioctl (tty, TIOCGETP, &ttybuff);
|
---|
892 | ttybuff.sg_flags = original_tty_flags;
|
---|
893 | ioctl (tty, TIOCSETN, &ttybuff);
|
---|
894 |
|
---|
895 | # if defined (TIOCGETC)
|
---|
896 | ioctl (tty, TIOCSETC, &original_tchars);
|
---|
897 | # endif /* TIOCGETC */
|
---|
898 |
|
---|
899 | # if defined (TIOCGLTC)
|
---|
900 | ioctl (tty, TIOCSLTC, &original_ltchars);
|
---|
901 | # endif /* TIOCGLTC */
|
---|
902 |
|
---|
903 | # if defined (TIOCLGET) && defined (LPASS8)
|
---|
904 | ioctl (tty, TIOCLSET, &original_lmode);
|
---|
905 | # endif /* TIOCLGET && LPASS8 */
|
---|
906 |
|
---|
907 | # endif /* !HAVE_TERMIO_H */
|
---|
908 | #endif /* !HAVE_TERMIOS_H */
|
---|
909 | #ifdef __EMX__
|
---|
910 | }
|
---|
911 | #endif /* __EMX__ */
|
---|
912 | terminal_end_using_terminal ();
|
---|
913 | }
|
---|
914 |
|
---|
915 | #ifdef __MSDOS__
|
---|
916 | # include "pcterm.c"
|
---|
917 | #endif
|
---|