terminal.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:14k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* terminal.c -- controlling the terminal with termcap. */
  2. /* Copyright (C) 1996 Free Software Foundation, Inc.
  3.    This file is part of the GNU Readline Library, a library for
  4.    reading lines of text with interactive input and history editing.
  5.    The GNU Readline Library is free software; you can redistribute it
  6.    and/or modify it under the terms of the GNU General Public License
  7.    as published by the Free Software Foundation; either version 1, or
  8.    (at your option) any later version.
  9.    The GNU Readline Library is distributed in the hope that it will be
  10.    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  11.    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.    The GNU General Public License is often shipped with GNU software, and
  14.    is generally kept in a file called COPYING or LICENSE.  If you do not
  15.    have a copy of the license, write to the Free Software Foundation,
  16.    675 Mass Ave, Cambridge, MA 02139, USA. */
  17. #define READLINE_LIBRARY
  18. #if defined (HAVE_CONFIG_H)
  19. #  include <config.h>
  20. #endif
  21. #include <sys/types.h>
  22. #include "posixstat.h"
  23. #include <fcntl.h>
  24. #if defined (HAVE_SYS_FILE_H)
  25. #  include <sys/file.h>
  26. #endif /* HAVE_SYS_FILE_H */
  27. #if defined (HAVE_UNISTD_H)
  28. #  include <unistd.h>
  29. #endif /* HAVE_UNISTD_H */
  30. #if defined (HAVE_STDLIB_H)
  31. #  include <stdlib.h>
  32. #else
  33. #  include "ansi_stdlib.h"
  34. #endif /* HAVE_STDLIB_H */
  35. #if defined (HAVE_LOCALE_H)
  36. #  include <locale.h>
  37. #endif
  38. #include <signal.h>
  39. #include <stdio.h>
  40. #include <setjmp.h>
  41. /* System-specific feature definitions and include files. */
  42. #include "rldefs.h"
  43. #if defined (GWINSZ_IN_SYS_IOCTL) && !defined (TIOCGWINSZ)
  44. #  include <sys/ioctl.h>
  45. #endif /* GWINSZ_IN_SYS_IOCTL && !TIOCGWINSZ */
  46. #include "rltty.h"
  47. #include "tcap.h"
  48. /* Some standard library routines. */
  49. #include "readline.h"
  50. #include "history.h"
  51. /* Variables and functions imported from readline.c */
  52. extern FILE *_rl_in_stream, *_rl_out_stream;
  53. extern int readline_echoing_p;
  54. extern int _rl_bell_preference;
  55. extern Keymap _rl_keymap;
  56. /* Functions imported from bind.c */
  57. extern void _rl_bind_if_unbound ();
  58. /* Functions imported from shell.c */
  59. extern void set_lines_and_columns ();
  60. extern char *get_env_value ();
  61. /* **************************************************************** */
  62. /*     */
  63. /* Terminal and Termcap     */
  64. /*     */
  65. /* **************************************************************** */
  66. static char *term_buffer = (char *)NULL;
  67. static char *term_string_buffer = (char *)NULL;
  68. static int tcap_initialized;
  69. /* Non-zero means this terminal can't really do anything. */
  70. static int dumb_term;
  71. #if !defined (__linux__)
  72. #  if defined (__EMX__) || defined (NEED_EXTERN_PC)
  73. extern 
  74. #  endif /* __EMX__ || NEED_EXTERN_PC */
  75. char PC, *BC, *UP;
  76. #endif /* __linux__ */
  77. /* Some strings to control terminal actions.  These are output by tputs (). */
  78. char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
  79. char *term_pc;
  80. /* Non-zero if we determine that the terminal can do character insertion. */
  81. int terminal_can_insert = 0;
  82. /* How to insert characters. */
  83. char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
  84. /* How to delete characters. */
  85. char *term_dc, *term_DC;
  86. #if defined (HACK_TERMCAP_MOTION)
  87. char *term_forward_char;
  88. #endif  /* HACK_TERMCAP_MOTION */
  89. /* How to go up a line. */
  90. char *term_up;
  91. /* A visible bell, if the terminal can be made to flash the screen. */
  92. static char *visible_bell;
  93. /* Non-zero means the terminal can auto-wrap lines. */
  94. int _rl_term_autowrap;
  95. /* Non-zero means that this terminal has a meta key. */
  96. static int term_has_meta;
  97. /* The sequences to write to turn on and off the meta key, if this
  98.    terminal    has one. */
  99. static char *term_mm, *term_mo;
  100. /* The key sequences output by the arrow keys, if this terminal has any. */
  101. static char *term_ku, *term_kd, *term_kr, *term_kl;
  102. /* How to initialize and reset the arrow keys, if this terminal has any. */
  103. static char *term_ks, *term_ke;
  104. /* The key sequences sent by the Home and End keys, if any. */
  105. static char *term_kh, *term_kH;
  106. /* Variables that hold the screen dimensions, used by the display code. */
  107. int screenwidth, screenheight, screenchars;
  108. /* Non-zero means the user wants to enable the keypad. */
  109. int _rl_enable_keypad;
  110. /* Non-zero means the user wants to enable a meta key. */
  111. int _rl_enable_meta = 1;
  112. /* Get readline's idea of the screen size.  TTY is a file descriptor open
  113.    to the terminal.  If IGNORE_ENV is true, we do not pay attention to the
  114.    values of $LINES and $COLUMNS.  The tests for TERM_STRING_BUFFER being
  115.    non-null serve to check whether or not we have initialized termcap. */
  116. void
  117. _rl_get_screen_size (tty, ignore_env)
  118.      int tty, ignore_env;
  119. {
  120.   char *ss;
  121. #if defined (TIOCGWINSZ)
  122.   struct winsize window_size;
  123. #endif /* TIOCGWINSZ */
  124. #if defined (__EMX__)
  125.   int sz[2];
  126. #endif
  127. #if defined (TIOCGWINSZ)
  128.   if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
  129.     {
  130.       screenwidth = (int) window_size.ws_col;
  131.       screenheight = (int) window_size.ws_row;
  132.     }
  133. #endif /* TIOCGWINSZ */
  134. #if defined (__EMX__)
  135.   _scrsize (sz);
  136.   screenwidth = sz[0];
  137.   screenheight = sz[1];
  138. #endif
  139.   /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
  140.      is unset. */
  141.   if (screenwidth <= 0)
  142.     {
  143.       if (ignore_env == 0 && (ss = get_env_value ("COLUMNS")))
  144. screenwidth = atoi (ss);
  145.       if (screenwidth <= 0 && term_string_buffer)
  146. screenwidth = tgetnum ("co");
  147.     }
  148.   /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
  149.      is unset. */
  150.   if (screenheight <= 0)
  151.     {
  152.       if (ignore_env == 0 && (ss = get_env_value ("LINES")))
  153. screenheight = atoi (ss);
  154.       if (screenheight <= 0 && term_string_buffer)
  155. screenheight = tgetnum ("li");
  156.     }
  157.   /* If all else fails, default to 80x24 terminal. */
  158.   if (screenwidth <= 1)
  159.     screenwidth = 80;
  160.   if (screenheight <= 0)
  161.     screenheight = 24;
  162.   /* If we're being compiled as part of bash, set the environment
  163.      variables $LINES and $COLUMNS to new values.  Otherwise, just
  164.      do a pair of putenv () or setenv () calls. */
  165.   set_lines_and_columns (screenheight, screenwidth);
  166.   if (!_rl_term_autowrap)
  167.     screenwidth--;
  168.   screenchars = screenwidth * screenheight;
  169. }
  170. void
  171. _rl_set_screen_size (rows, cols)
  172.      int rows, cols;
  173. {
  174.   screenheight = rows;
  175.   screenwidth = cols;
  176.   if (_rl_term_autowrap == 0)
  177.     screenwidth--;
  178.   screenchars = screenwidth * screenheight;
  179. }
  180. extern void _rl_redisplay_after_sigwinch();
  181. void
  182. rl_resize_terminal ()
  183. {
  184.   if (readline_echoing_p)
  185.     {
  186.       _rl_get_screen_size (fileno (rl_instream), 1);
  187.       _rl_redisplay_after_sigwinch ();
  188.     }
  189. }
  190. struct _tc_string {
  191.      char *tc_var;
  192.      char **tc_value;
  193. };
  194. /* This should be kept sorted, just in case we decide to change the
  195.    search algorithm to something smarter. */
  196. static struct _tc_string tc_strings[] =
  197. {
  198.   {"DC", &term_DC},
  199.   {"IC", &term_IC},
  200.   {"ce", &term_clreol},
  201.   {"cl", &term_clrpag},
  202.   {"cr", &term_cr},
  203.   {"dc", &term_dc},
  204.   {"ei", &term_ei},
  205.   {"ic", &term_ic},
  206.   {"im", &term_im},
  207.   {"kd", &term_kd},
  208.   {"kh", &term_kh}, /* home */
  209.   {"kH", &term_kH}, /* end */
  210.   {"kl", &term_kl},
  211.   {"kr", &term_kr},
  212.   {"ku", &term_ku},
  213.   {"ks", &term_ks},
  214.   {"ke", &term_ke},
  215.   {"le", &term_backspace},
  216.   {"mm", &term_mm},
  217.   {"mo", &term_mo},
  218. #if defined (HACK_TERMCAP_MOTION)
  219.   {"nd", &term_forward_char},
  220. #endif
  221.   {"pc", &term_pc},
  222.   {"up", &term_up},
  223.   {"vb", &visible_bell},
  224. };
  225. #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
  226. /* Read the desired terminal capability strings into BP.  The capabilities
  227.    are described in the TC_STRINGS table. */
  228. static void
  229. get_term_capabilities (bp)
  230.      char **bp;
  231. {
  232.   register int i;
  233.   for (i = 0; i < NUM_TC_STRINGS; i++)
  234.     *(tc_strings[i].tc_value) = tgetstr (tc_strings[i].tc_var, bp);
  235.   tcap_initialized = 1;
  236. }
  237. int
  238. _rl_init_terminal_io (terminal_name)
  239.      char *terminal_name;
  240. {
  241. #if defined (__GO32__)
  242.   screenwidth = ScreenCols ();
  243.   screenheight = ScreenRows ();
  244.   screenchars = screenwidth * screenheight;
  245.   term_cr = "r";
  246.   term_im = term_ei = term_ic = term_IC = (char *)NULL;
  247.   term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  248.   /* Does the __GO32__ have a meta key?  I don't know. */
  249.   term_has_meta = 0;
  250.   term_mm = term_mo = (char *)NULL;
  251.   /* It probably has arrow keys, but I don't know what they are. */
  252.   term_ku = term_kd = term_kr = term_kl = (char *)NULL;
  253. #if defined (HACK_TERMCAP_MOTION)
  254.   term_forward_char = (char *)NULL;
  255. #endif /* HACK_TERMCAP_MOTION */
  256.   terminal_can_insert = _rl_term_autowrap = 0;
  257.   return;
  258. #else /* !__GO32__ */
  259.   char *term, *buffer;
  260.   int tty;
  261.   Keymap xkeymap;
  262.   term = terminal_name ? terminal_name : get_env_value ("TERM");
  263.   if (term_string_buffer == 0)
  264.     term_string_buffer = xmalloc (2032);
  265.   if (term_buffer == 0)
  266.     term_buffer = xmalloc (4080);
  267.   buffer = term_string_buffer;
  268.   term_clrpag = term_cr = term_clreol = (char *)NULL;
  269.   if (term == 0)
  270.     term = "dumb";
  271.   if (tgetent (term_buffer, term) <= 0)
  272.     {
  273.       dumb_term = 1;
  274.       screenwidth = 79;
  275.       screenheight = 24;
  276.       screenchars = 79 * 24;
  277.       term_cr = "r";
  278.       term_im = term_ei = term_ic = term_IC = (char *)NULL;
  279.       term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  280.       term_ku = term_kd = term_kl = term_kr = (char *)NULL;
  281. #if defined (HACK_TERMCAP_MOTION)
  282.       term_forward_char = (char *)NULL;
  283. #endif
  284.       terminal_can_insert = 0;
  285.       return 0;
  286.     }
  287.   get_term_capabilities (&buffer);
  288.   /* Set up the variables that the termcap library expects the application
  289.      to provide. */
  290.   PC = term_pc ? *term_pc : 0;
  291.   BC = term_backspace;
  292.   UP = term_up;
  293.   if (!term_cr)
  294.     term_cr = "r";
  295.   tty = rl_instream ? fileno (rl_instream) : 0;
  296.   screenwidth = screenheight = 0;
  297.   _rl_term_autowrap = tgetflag ("am") && tgetflag ("xn");
  298.   _rl_get_screen_size (tty, 0);
  299.   /* "An application program can assume that the terminal can do
  300.       character insertion if *any one of* the capabilities `IC',
  301.       `im', `ic' or `ip' is provided."  But we can't do anything if
  302.       only `ip' is provided, so... */
  303.   terminal_can_insert = (term_IC || term_im || term_ic);
  304.   /* Check to see if this terminal has a meta key and clear the capability
  305.      variables if there is none. */
  306.   term_has_meta = (tgetflag ("km") || tgetflag ("MT"));
  307.   if (!term_has_meta)
  308.     term_mm = term_mo = (char *)NULL;
  309.   /* Attempt to find and bind the arrow keys.  Do not override already
  310.      bound keys in an overzealous attempt, however. */
  311.   xkeymap = _rl_keymap;
  312.   _rl_keymap = emacs_standard_keymap;
  313.   _rl_bind_if_unbound (term_ku, rl_get_previous_history);
  314.   _rl_bind_if_unbound (term_kd, rl_get_next_history);
  315.   _rl_bind_if_unbound (term_kr, rl_forward);
  316.   _rl_bind_if_unbound (term_kl, rl_backward);
  317.   _rl_bind_if_unbound (term_kh, rl_beg_of_line); /* Home */
  318.   _rl_bind_if_unbound (term_kH, rl_end_of_line); /* End */
  319. #if defined (VI_MODE)
  320.   _rl_keymap = vi_movement_keymap;
  321.   _rl_bind_if_unbound (term_ku, rl_get_previous_history);
  322.   _rl_bind_if_unbound (term_kd, rl_get_next_history);
  323.   _rl_bind_if_unbound (term_kr, rl_forward);
  324.   _rl_bind_if_unbound (term_kl, rl_backward);
  325.   _rl_bind_if_unbound (term_kh, rl_beg_of_line); /* Home */
  326.   _rl_bind_if_unbound (term_kH, rl_end_of_line); /* End */
  327. #endif /* VI_MODE */
  328.   _rl_keymap = xkeymap;
  329. #endif /* !__GO32__ */
  330.   return 0;
  331. }
  332. char *
  333. rl_get_termcap (cap)
  334.      char *cap;
  335. {
  336.   register int i;
  337.   if (tcap_initialized == 0)
  338.     return ((char *)NULL);
  339.   for (i = 0; i < NUM_TC_STRINGS; i++)
  340.     {
  341.       if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
  342.         return *(tc_strings[i].tc_value);
  343.     }
  344.   return ((char *)NULL);
  345. }
  346. /* Re-initialize the terminal considering that the TERM/TERMCAP variable
  347.    has changed. */
  348. int
  349. rl_reset_terminal (terminal_name)
  350.      char *terminal_name;
  351. {
  352.   _rl_init_terminal_io (terminal_name);
  353.   return 0;
  354. }
  355. /* A function for the use of tputs () */
  356. #ifdef _MINIX
  357. void
  358. _rl_output_character_function (c)
  359.      int c;
  360. {
  361.   putc (c, _rl_out_stream);
  362. }
  363. #else /* !_MINIX */
  364. int
  365. _rl_output_character_function (c)
  366.      int c;
  367. {
  368.   return putc (c, _rl_out_stream);
  369. }
  370. #endif /* !_MINIX */
  371. /* Write COUNT characters from STRING to the output stream. */
  372. void
  373. _rl_output_some_chars (string, count)
  374.      char *string;
  375.      int count;
  376. {
  377.   fwrite (string, 1, count, _rl_out_stream);
  378. }
  379. /* Move the cursor back. */
  380. int
  381. _rl_backspace (count)
  382.      int count;
  383. {
  384.   register int i;
  385. #if !defined (__GO32__)
  386.   if (term_backspace)
  387.     for (i = 0; i < count; i++)
  388.       tputs (term_backspace, 1, _rl_output_character_function);
  389.   else
  390. #endif /* !__GO32__ */
  391.     for (i = 0; i < count; i++)
  392.       putc ('b', _rl_out_stream);
  393.   return 0;
  394. }
  395. /* Move to the start of the next line. */
  396. int
  397. crlf ()
  398. {
  399. #if defined (NEW_TTY_DRIVER)
  400.   if (term_cr)
  401.     tputs (term_cr, 1, _rl_output_character_function);
  402. #endif /* NEW_TTY_DRIVER */
  403.   putc ('n', _rl_out_stream);
  404.   return 0;
  405. }
  406. /* Ring the terminal bell. */
  407. int
  408. ding ()
  409. {
  410.   if (readline_echoing_p)
  411.     {
  412. #if !defined (__GO32__)
  413.       switch (_rl_bell_preference)
  414.         {
  415. case NO_BELL:
  416. default:
  417.   break;
  418. case VISIBLE_BELL:
  419.   if (visible_bell)
  420.     {
  421.       tputs (visible_bell, 1, _rl_output_character_function);
  422.       break;
  423.     }
  424.   /* FALLTHROUGH */
  425. case AUDIBLE_BELL:
  426.   fprintf (stderr, "07");
  427.   fflush (stderr);
  428.   break;
  429.         }
  430. #else /* __GO32__ */
  431.       fprintf (stderr, "07");
  432.       fflush (stderr);
  433. #endif /* __GO32__ */
  434.       return (0);
  435.     }
  436.   return (-1);
  437. }
  438. /* **************************************************************** */
  439. /*     */
  440. /*   Controlling the Meta Key and Keypad     */
  441. /*     */
  442. /* **************************************************************** */
  443. void
  444. _rl_enable_meta_key ()
  445. {
  446.   if (term_has_meta && term_mm)
  447.     tputs (term_mm, 1, _rl_output_character_function);
  448. }
  449. void
  450. _rl_control_keypad (on)
  451.      int on;
  452. {
  453.   if (on && term_ks)
  454.     tputs (term_ks, 1, _rl_output_character_function);
  455.   else if (!on && term_ke)
  456.     tputs (term_ke, 1, _rl_output_character_function);
  457. }