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

MySQL数据库

开发平台:

Visual C++

  1. /* input.c -- character input functions for readline. */
  2. /* Copyright (C) 1994 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 2, 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 <fcntl.h>
  23. #if defined (HAVE_SYS_FILE_H)
  24. #  include <sys/file.h>
  25. #endif /* HAVE_SYS_FILE_H */
  26. #if defined (HAVE_UNISTD_H)
  27. #  include <unistd.h>
  28. #endif /* HAVE_UNISTD_H */
  29. #if defined (HAVE_STDLIB_H)
  30. #  include <stdlib.h>
  31. #else
  32. #  include "ansi_stdlib.h"
  33. #endif /* HAVE_STDLIB_H */
  34. #if defined (HAVE_SELECT)
  35. #  if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX) || defined(TIME_WITH_SYS_TIME)
  36. #    include <sys/time.h>
  37. #  endif
  38. #endif /* HAVE_SELECT */
  39. #if defined (HAVE_SYS_SELECT_H)
  40. #  include <sys/select.h>
  41. #endif
  42. #if defined (FIONREAD_IN_SYS_IOCTL)
  43. #  include <sys/ioctl.h>
  44. #endif
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #if !defined (errno)
  48. extern int errno;
  49. #endif /* !errno */
  50. /* System-specific feature definitions and include files. */
  51. #include "rldefs.h"
  52. /* Some standard library routines. */
  53. #include "readline.h"
  54. /* What kind of non-blocking I/O do we have? */
  55. #if !defined (O_NDELAY) && defined (O_NONBLOCK)
  56. #  define O_NDELAY O_NONBLOCK /* Posix style */
  57. #endif
  58. /* Functions imported from other files in the library. */
  59. extern char *xmalloc (), *xrealloc ();
  60. /* Variables and functions from macro.c. */
  61. extern void _rl_add_macro_char ();
  62. extern void _rl_with_macro_input ();
  63. extern int _rl_next_macro_key ();
  64. extern int _rl_defining_kbd_macro;
  65. #if defined (VI_MODE)
  66. extern void _rl_vi_set_last ();
  67. extern int _rl_vi_textmod_command ();
  68. #endif /* VI_MODE */
  69. extern FILE *rl_instream, *rl_outstream;
  70. extern Function *rl_last_func;
  71. extern int rl_key_sequence_length;
  72. extern int rl_pending_input;
  73. extern int rl_editing_mode;
  74. extern Keymap _rl_keymap;
  75. extern int _rl_convert_meta_chars_to_ascii;
  76. #if defined (__GO32__)
  77. #  include <pc.h>
  78. #endif /* __GO32__ */
  79. /* Non-null means it is a pointer to a function to run while waiting for
  80.    character input. */
  81. Function *rl_event_hook = (Function *)NULL;
  82. Function *rl_getc_function = rl_getc;
  83. /* **************************************************************** */
  84. /*     */
  85. /* Character Input Buffering            */
  86. /*     */
  87. /* **************************************************************** */
  88. static int pop_index, push_index;
  89. static unsigned char ibuffer[512];
  90. static int ibuffer_len = sizeof (ibuffer) - 1;
  91. #define any_typein (push_index != pop_index)
  92. int
  93. _rl_any_typein ()
  94. {
  95.   return any_typein;
  96. }
  97. /* Return the amount of space available in the buffer for stuffing
  98.    characters. */
  99. static int
  100. ibuffer_space ()
  101. {
  102.   if (pop_index > push_index)
  103.     return (pop_index - push_index - 1);
  104.   else
  105.     return (ibuffer_len - (push_index - pop_index));
  106. }
  107. /* Get a key from the buffer of characters to be read.
  108.    Return the key in KEY.
  109.    Result is KEY if there was a key, or 0 if there wasn't. */
  110. static int
  111. rl_get_char (key)
  112.      int *key;
  113. {
  114.   if (push_index == pop_index)
  115.     return (0);
  116.   *key = ibuffer[pop_index++];
  117.   if (pop_index >= ibuffer_len)
  118.     pop_index = 0;
  119.   return (1);
  120. }
  121. /* Stuff KEY into the *front* of the input buffer.
  122.    Returns non-zero if successful, zero if there is
  123.    no space left in the buffer. */
  124. static int
  125. rl_unget_char (key)
  126.      int key;
  127. {
  128.   if (ibuffer_space ())
  129.     {
  130.       pop_index--;
  131.       if (pop_index < 0)
  132. pop_index = ibuffer_len - 1;
  133.       ibuffer[pop_index] = key;
  134.       return (1);
  135.     }
  136.   return (0);
  137. }
  138. #if defined(__EMX__)
  139. int waiting_char = -1;
  140. #endif
  141. /* If a character is available to be read, then read it
  142.    and stuff it into IBUFFER.  Otherwise, just return. */
  143. static void
  144. rl_gather_tyi ()
  145. {
  146. #if defined (__EMX__)
  147.   if (isatty (0) && (waiting_char = _read_kbd(0, 0, 0)) != -1 && ibuffer_space ())
  148.     {
  149.       int i;
  150.       i = (*rl_getc_function) (rl_instream);
  151.       rl_stuff_char (i);
  152.     }
  153. #elif defined (__GO32__)
  154.   char input;
  155.   if (isatty (0) && kbhit () && ibuffer_space ())
  156.     {
  157.       int i;
  158.       i = (*rl_getc_function) (rl_instream);
  159.       rl_stuff_char (i);
  160.     }
  161. #else /* !__GO32__ */
  162.   int tty;
  163.   register int tem, result;
  164.   int chars_avail;
  165.   char input;
  166. #if defined(HAVE_SELECT)
  167.   fd_set readfds, exceptfds;
  168.   struct timeval timeout;
  169. #endif
  170.   tty = fileno (rl_instream);
  171. #if defined (HAVE_SELECT)
  172.   FD_ZERO (&readfds);
  173.   FD_ZERO (&exceptfds);
  174.   FD_SET (tty, &readfds);
  175.   FD_SET (tty, &exceptfds);
  176.   timeout.tv_sec = 0;
  177.   timeout.tv_usec = 100000; /* 0.1 seconds */
  178.   if (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) <= 0)
  179.     return; /* Nothing to read. */
  180. #endif
  181.   result = -1;
  182. #if defined (FIONREAD)
  183.   result = ioctl (tty, FIONREAD, &chars_avail);
  184. #endif
  185. #if defined (O_NDELAY)
  186.   if (result == -1)
  187.     {
  188.       tem = fcntl (tty, F_GETFL, 0);
  189.       fcntl (tty, F_SETFL, (tem | O_NDELAY));
  190.       chars_avail = read (tty, &input, 1);
  191.       fcntl (tty, F_SETFL, tem);
  192.       if (chars_avail == -1 && errno == EAGAIN)
  193. return;
  194.     }
  195. #endif /* O_NDELAY */
  196.   /* If there's nothing available, don't waste time trying to read
  197.      something. */
  198.   if (chars_avail <= 0)
  199.     return;
  200.   tem = ibuffer_space ();
  201.   if (chars_avail > tem)
  202.     chars_avail = tem;
  203.   /* One cannot read all of the available input.  I can only read a single
  204.      character at a time, or else programs which require input can be
  205.      thwarted.  If the buffer is larger than one character, I lose.
  206.      Damn! */
  207.   if (tem < ibuffer_len)
  208.     chars_avail = 0;
  209.   if (result != -1)
  210.     {
  211.       while (chars_avail--)
  212. rl_stuff_char ((*rl_getc_function) (rl_instream));
  213.     }
  214.   else
  215.     {
  216.       if (chars_avail)
  217. rl_stuff_char (input);
  218.     }
  219. #endif /* !__GO32__ */
  220. }
  221. /* Is there input available to be read on the readline input file
  222.    descriptor?  Only works if the system has select(2) or FIONREAD. */
  223. int
  224. _rl_input_available ()
  225. {
  226. #if defined (__EMX__)
  227.   if (isatty (0) && (waiting_char = _read_kbd(0, 0, 0)) != -1)
  228.     return 1;
  229. #else /*  __EMX__ */
  230. #if defined(HAVE_SELECT)
  231.   fd_set readfds, exceptfds;
  232.   struct timeval timeout;
  233. #endif
  234. #if defined(FIONREAD)
  235.   int chars_avail;
  236. #endif
  237.   int tty;
  238.   tty = fileno (rl_instream);
  239. #if defined (HAVE_SELECT)
  240.   FD_ZERO (&readfds);
  241.   FD_ZERO (&exceptfds);
  242.   FD_SET (tty, &readfds);
  243.   FD_SET (tty, &exceptfds);
  244.   timeout.tv_sec = 0;
  245.   timeout.tv_usec = 100000; /* 0.1 seconds */
  246.   return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
  247. #endif
  248. #if defined (FIONREAD)
  249.   if (ioctl (tty, FIONREAD, &chars_avail) == 0)
  250.     return (chars_avail);
  251. #endif
  252. #endif /* !__EMX__ */
  253.   return 0;
  254. }
  255. void
  256. _rl_insert_typein (c)
  257.      int c;     
  258. {    
  259.   int key, t, i;
  260.   char *string;
  261.   i = key = 0;
  262.   string = xmalloc (ibuffer_len + 1);
  263.   string[i++] = (char) c;
  264.   while ((t = rl_get_char (&key)) &&
  265.  _rl_keymap[key].type == ISFUNC &&
  266.  _rl_keymap[key].function == rl_insert)
  267.     string[i++] = key;
  268.   if (t)
  269.     rl_unget_char (key);
  270.   string[i] = '';
  271.   rl_insert_text (string);
  272.   free (string);
  273. }
  274. /* Add KEY to the buffer of characters to be read.  Returns 1 if the
  275.    character was stuffed correctly; 0 otherwise. */
  276. int
  277. rl_stuff_char (key)
  278.      int key;
  279. {
  280.   if (ibuffer_space () == 0)
  281.     return 0;
  282.   if (key == EOF)
  283.     {
  284.       key = NEWLINE;
  285.       rl_pending_input = EOF;
  286.     }
  287.   ibuffer[push_index++] = key;
  288.   if (push_index >= ibuffer_len)
  289.     push_index = 0;
  290.   return 1;
  291. }
  292. /* Make C be the next command to be executed. */
  293. int
  294. rl_execute_next (c)
  295.      int c;
  296. {
  297.   rl_pending_input = c;
  298.   return 0;
  299. }
  300. /* **************************************************************** */
  301. /*     */
  302. /*      Character Input     */
  303. /*     */
  304. /* **************************************************************** */
  305. /* Read a key, including pending input. */
  306. int
  307. rl_read_key ()
  308. {
  309.   int c;
  310.   rl_key_sequence_length++;
  311.   if (rl_pending_input)
  312.     {
  313.       c = rl_pending_input;
  314.       rl_pending_input = 0;
  315.     }
  316.   else
  317.     {
  318.       /* If input is coming from a macro, then use that. */
  319.       if ((c = _rl_next_macro_key ()))
  320. return (c);
  321.       /* If the user has an event function, then call it periodically. */
  322.       if (rl_event_hook)
  323. {
  324.   while (rl_event_hook && rl_get_char (&c) == 0)
  325.     {
  326.       (*rl_event_hook) ();
  327.       rl_gather_tyi ();
  328.     }
  329. }
  330.       else
  331. {
  332.   if (rl_get_char (&c) == 0)
  333.     c = (*rl_getc_function) (rl_instream);
  334. }
  335.     }
  336.   return (c);
  337. }
  338. int
  339. rl_getc (stream)
  340.      FILE *stream;
  341. {
  342.   int result, flags;
  343.   unsigned char c;
  344. #if defined (__EMX__)
  345.   if (isatty (0))
  346.   {
  347.     int key;
  348.     if (waiting_char != -1)
  349.     {
  350.       key = waiting_char;
  351.       waiting_char = -1;
  352.     }
  353.     else
  354.     {
  355. #ifdef __RSXNT__
  356.       pc_flush();
  357. #endif
  358.       key = _read_kbd(0, 1, 0);
  359.     }
  360.     while (key == 0)
  361.     {
  362.       key |= (_read_kbd(0, 1, 0) << 8);
  363.       /* printf("<%04X> ", key);
  364.       fflush(stdout); */
  365.       switch (key)
  366.       {
  367.       case 0x4B00: /* left arrow */
  368. key = 'B' - 64;
  369. break;
  370.       case 0x4D00: /* right arrow */
  371. key = 'F' - 64;
  372. break;
  373.       case 0x7300: /* ctrl left arrow */
  374. key = 27;
  375. waiting_char = 'B';
  376. break;
  377.       case 0x7400: /* ctrl right arrow */
  378. key = 27;
  379. waiting_char = 'F';
  380. break;
  381.       case 0x4800: /* up arrow */
  382. key = 'P' - 64;
  383. break;
  384.       case 0x5000: /* down arrow */
  385. key = 'N' - 64;
  386. break;
  387.       case 0x8D00: /* ctrl up arrow */
  388. key = 'R' - 64;
  389. break;
  390.       case 0x9100: /* ctrl down arrow */
  391. key = 'S' - 64;
  392. break;
  393.       case 0x4700: /* home key */
  394. key = 'A' - 64;
  395. break;
  396.       case 0x4F00: /* end key */
  397. key = 'E' - 64;
  398. break;
  399.       case 0x7700: /* ctrl home key */
  400. key = 27;
  401. waiting_char = '<';
  402. break;
  403.       case 0x7500: /* ctrl end key */
  404. key = 27;
  405. waiting_char = '>';
  406. break;
  407.       case 0x5300: /* delete key */
  408. key = 'D' - 64;
  409. break;
  410.       case 0x5200: /* insert key */
  411. key = 'V' - 64;
  412. break;
  413.       default: /* ignore all other special keys, read next */
  414. key = _read_kbd(0, 1, 0);
  415. break;
  416.       }
  417.     }
  418.     return (key & 0xFF);
  419.   }
  420. #endif /* __EMX__ */
  421. #if defined (__GO32__)
  422.   if (isatty (0))
  423.     return (getkey () & 0x7F);
  424. #endif /* __GO32__ */
  425.   while (1)
  426.     {
  427.       result = read (fileno (stream), &c, sizeof (unsigned char));
  428.       if (result == sizeof (unsigned char))
  429. return (c);
  430.       /* If zero characters are returned, then the file that we are
  431.  reading from is empty!  Return EOF in that case. */
  432.       if (result == 0)
  433. return (EOF);
  434. #if defined (__BEOS__)
  435.       if (errno == EINTR)
  436. continue;
  437. #endif
  438. #if defined (EWOULDBLOCK)
  439.       if (errno == EWOULDBLOCK)
  440. {
  441.   if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
  442.     return (EOF);
  443.   if (flags & O_NDELAY)
  444.     {
  445.       flags &= ~O_NDELAY;
  446.       fcntl (fileno (stream), F_SETFL, flags);
  447.       continue;
  448.     }
  449.   continue;
  450. }
  451. #endif /* EWOULDBLOCK */
  452. #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
  453.       if (errno == EAGAIN)
  454. {
  455.   if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
  456.     return (EOF);
  457.   if (flags & O_NONBLOCK)
  458.     {
  459.       flags &= ~O_NONBLOCK;
  460.       fcntl (fileno (stream), F_SETFL, flags);
  461.       continue;
  462.     }
  463. }
  464. #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
  465. #if !defined (__GO32__)
  466.       /* If the error that we received was SIGINT, then try again,
  467.  this is simply an interrupted system call to read ().
  468.  Otherwise, some error ocurred, also signifying EOF. */
  469.       if (errno != EINTR)
  470. return (EOF);
  471. #endif /* !__GO32__ */
  472.     }
  473. }