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

MySQL数据库

开发平台:

Visual C++

  1. /* callback.c -- functions to use readline as an X `callback' mechanism. */
  2. /* Copyright (C) 1987, 1989, 1992 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 "rlconf.h"
  22. #if defined (READLINE_CALLBACKS)
  23. #include <sys/types.h>
  24. #include <stdio.h>
  25. /* System-specific feature definitions and include files. */
  26. #include "rldefs.h"
  27. #include "readline.h"
  28. extern void readline_internal_setup ();
  29. extern char *readline_internal_teardown ();
  30. extern int readline_internal_char ();
  31. extern void _rl_init_line_state ();
  32. extern int _rl_meta_flag;
  33. extern char *rl_prompt;
  34. extern int rl_visible_prompt_length;
  35. /* **************************************************************** */
  36. /*     */
  37. /* Callback Readline Functions                 */
  38. /*     */
  39. /* **************************************************************** */
  40. /* Allow using readline in situations where a program may have multiple
  41.    things to handle at once, and dispatches them via select().  Call
  42.    rl_callback_handler_install() with the prompt and a function to call
  43.    whenever a complete line of input is ready.  The user must then
  44.    call rl_callback_read_char() every time some input is available, and 
  45.    rl_callback_read_char() will call the user's function with the complete
  46.    text read in at each end of line.  The terminal is kept prepped and
  47.    signals handled all the time, except during calls to the user's function. */
  48. VFunction *rl_linefunc; /* user callback function */
  49. static int in_handler; /* terminal_prepped and signals set? */
  50. /* Make sure the terminal is set up, initialize readline, and prompt. */
  51. static void
  52. _rl_callback_newline ()
  53. {
  54.   rl_initialize ();
  55.   if (in_handler == 0)
  56.     {
  57.       in_handler = 1;
  58.       (*rl_prep_term_function) (_rl_meta_flag);
  59. #if defined (HANDLE_SIGNALS)
  60.       rl_set_signals ();
  61. #endif
  62.     }
  63.   readline_internal_setup ();
  64. }
  65. /* Install a readline handler, set up the terminal, and issue the prompt. */
  66. void
  67. rl_callback_handler_install (prompt, linefunc)
  68.      char *prompt;
  69.      VFunction *linefunc;
  70. {
  71.   rl_prompt = prompt;
  72.   rl_visible_prompt_length = rl_prompt ? rl_expand_prompt (rl_prompt) : 0;
  73.   rl_linefunc = linefunc;
  74.   _rl_callback_newline ();
  75. }
  76. /* Read one character, and dispatch to the handler if it ends the line. */
  77. void
  78. rl_callback_read_char ()
  79. {
  80.   char *line;
  81.   int eof;
  82.   if (rl_linefunc == NULL)
  83.     {
  84.       fprintf (stderr, "readline: readline_callback_read_char() called with no handler!rn");
  85.       abort ();
  86.     }
  87.   eof = readline_internal_char ();
  88.   if (rl_done)
  89.     {
  90.       line = readline_internal_teardown (eof);
  91.       (*rl_deprep_term_function) ();
  92. #if defined (HANDLE_SIGNALS)
  93.       rl_clear_signals ();
  94. #endif
  95.       in_handler = 0;
  96.       (*rl_linefunc) (line);
  97.     /* If the user did not clear out the line, do it for him. */
  98.     if (rl_line_buffer[0])
  99.       _rl_init_line_state ();
  100.     /* Redisplay the prompt if readline_handler_{install,remove} not called. */
  101.       if (in_handler == 0 && rl_linefunc)
  102. _rl_callback_newline ();
  103.     }
  104. }
  105. /* Remove the handler, and make sure the terminal is in its normal state. */
  106. void
  107. rl_callback_handler_remove ()
  108. {
  109.   rl_linefunc = NULL;
  110.   if (in_handler)
  111.     {
  112.       in_handler = 0;
  113.       (*rl_deprep_term_function) ();
  114. #if defined (HANDLE_SIGNALS)
  115.       rl_clear_signals ();
  116. #endif
  117.     }
  118. }
  119. #endif