callback.c revision 119610
1/* callback.c -- functions to use readline as an X `callback' mechanism. */
2
3/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
4
5   This file is part of the GNU Readline Library, a library for
6   reading lines of text with interactive input and history editing.
7
8   The GNU Readline Library is free software; you can redistribute it
9   and/or modify it under the terms of the GNU General Public License
10   as published by the Free Software Foundation; either version 2, or
11   (at your option) any later version.
12
13   The GNU Readline Library is distributed in the hope that it will be
14   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   The GNU General Public License is often shipped with GNU software, and
19   is generally kept in a file called COPYING or LICENSE.  If you do not
20   have a copy of the license, write to the Free Software Foundation,
21   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22#define READLINE_LIBRARY
23
24#if defined (HAVE_CONFIG_H)
25#  include <config.h>
26#endif
27
28#include "rlconf.h"
29
30#if defined (READLINE_CALLBACKS)
31
32#include <sys/types.h>
33
34#ifdef HAVE_STDLIB_H
35#  include <stdlib.h>
36#else
37#  include "ansi_stdlib.h"
38#endif
39
40#include <stdio.h>
41
42/* System-specific feature definitions and include files. */
43#include "rldefs.h"
44#include "readline.h"
45#include "rlprivate.h"
46
47/* **************************************************************** */
48/*								    */
49/*			Callback Readline Functions                 */
50/*								    */
51/* **************************************************************** */
52
53/* Allow using readline in situations where a program may have multiple
54   things to handle at once, and dispatches them via select().  Call
55   rl_callback_handler_install() with the prompt and a function to call
56   whenever a complete line of input is ready.  The user must then
57   call rl_callback_read_char() every time some input is available, and
58   rl_callback_read_char() will call the user's function with the complete
59   text read in at each end of line.  The terminal is kept prepped and
60   signals handled all the time, except during calls to the user's function. */
61
62rl_vcpfunc_t *rl_linefunc;		/* user callback function */
63static int in_handler;		/* terminal_prepped and signals set? */
64
65/* Make sure the terminal is set up, initialize readline, and prompt. */
66static void
67_rl_callback_newline ()
68{
69  rl_initialize ();
70
71  if (in_handler == 0)
72    {
73      in_handler = 1;
74
75      (*rl_prep_term_function) (_rl_meta_flag);
76
77#if defined (HANDLE_SIGNALS)
78      rl_set_signals ();
79#endif
80    }
81
82  readline_internal_setup ();
83}
84
85/* Install a readline handler, set up the terminal, and issue the prompt. */
86void
87rl_callback_handler_install (prompt, linefunc)
88     const char *prompt;
89     rl_vcpfunc_t *linefunc;
90{
91  rl_set_prompt (prompt);
92  rl_linefunc = linefunc;
93  _rl_callback_newline ();
94}
95
96/* Read one character, and dispatch to the handler if it ends the line. */
97void
98rl_callback_read_char ()
99{
100  char *line;
101  int eof;
102
103  if (rl_linefunc == NULL)
104    {
105      fprintf (stderr, "readline: readline_callback_read_char() called with no handler!\r\n");
106      abort ();
107    }
108
109  eof = readline_internal_char ();
110
111  /* We loop in case some function has pushed input back with rl_execute_next. */
112  for (;;)
113    {
114      if (rl_done)
115	{
116	  line = readline_internal_teardown (eof);
117
118	  (*rl_deprep_term_function) ();
119#if defined (HANDLE_SIGNALS)
120	  rl_clear_signals ();
121#endif
122	  in_handler = 0;
123	  (*rl_linefunc) (line);
124
125	  /* If the user did not clear out the line, do it for him. */
126	  if (rl_line_buffer[0])
127	    _rl_init_line_state ();
128
129	  /* Redisplay the prompt if readline_handler_{install,remove}
130	     not called. */
131	  if (in_handler == 0 && rl_linefunc)
132	    _rl_callback_newline ();
133	}
134      if (rl_pending_input)
135	eof = readline_internal_char ();
136      else
137        break;
138    }
139}
140
141/* Remove the handler, and make sure the terminal is in its normal state. */
142void
143rl_callback_handler_remove ()
144{
145  rl_linefunc = NULL;
146  if (in_handler)
147    {
148      in_handler = 0;
149      (*rl_deprep_term_function) ();
150#if defined (HANDLE_SIGNALS)
151      rl_clear_signals ();
152#endif
153    }
154}
155
156#endif
157