callback.c revision 35486
1133488Sharti/* callback.c -- functions to use readline as an X `callback' mechanism. */
2133488Sharti
3133488Sharti/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
4133488Sharti
5133488Sharti   This file is part of the GNU Readline Library, a library for
6133488Sharti   reading lines of text with interactive input and history editing.
7133488Sharti
8133488Sharti   The GNU Readline Library is free software; you can redistribute it
9133488Sharti   and/or modify it under the terms of the GNU General Public License
10133488Sharti   as published by the Free Software Foundation; either version 1, or
11133488Sharti   (at your option) any later version.
12133488Sharti
13133488Sharti   The GNU Readline Library is distributed in the hope that it will be
14133488Sharti   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15133488Sharti   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16133488Sharti   GNU General Public License for more details.
17133488Sharti
18133488Sharti   The GNU General Public License is often shipped with GNU software, and
19133488Sharti   is generally kept in a file called COPYING or LICENSE.  If you do not
20133488Sharti   have a copy of the license, write to the Free Software Foundation,
21133488Sharti   675 Mass Ave, Cambridge, MA 02139, USA. */
22133488Sharti#define READLINE_LIBRARY
23133488Sharti
24133488Sharti#if defined (HAVE_CONFIG_H)
25133488Sharti#  include <config.h>
26133488Sharti#endif
27133488Sharti
28133488Sharti#include "rlconf.h"
29146532Sharti
30133488Sharti#if defined (READLINE_CALLBACKS)
31133488Sharti
32133488Sharti#include <sys/types.h>
33133488Sharti#include <stdio.h>
34133488Sharti
35133488Sharti/* System-specific feature definitions and include files. */
36133488Sharti#include "rldefs.h"
37133488Sharti#include "readline.h"
38133488Sharti
39146611Shartiextern void readline_internal_startup ();
40146611Shartiextern char *readline_internal_teardown ();
41146611Shartiextern int readline_internal_char ();
42133488Shartiextern void _rl_init_line_state ();
43133488Sharti
44133488Shartiextern int _rl_meta_flag;
45133488Shartiextern char *rl_prompt;
46133488Shartiextern int rl_visible_prompt_length;
47133488Sharti
48133488Sharti/* **************************************************************** */
49133488Sharti/*								    */
50133488Sharti/*			Callback Readline Functions                 */
51133488Sharti/*								    */
52133488Sharti/* **************************************************************** */
53133488Sharti
54133488Sharti/* Allow using readline in situations where a program may have multiple
55133488Sharti   things to handle at once, and dispatches them via select().  Call
56133488Sharti   rl_callback_handler_install() with the prompt and a function to call
57133488Sharti   whenever a complete line of input is ready.  The user must then
58133488Sharti   call rl_callback_read_char() every time some input is available, and
59133488Sharti   rl_callback_read_char() will call the user's function with the complete
60133488Sharti   text read in at each end of line.  The terminal is kept prepped and
61133488Sharti   signals handled all the time, except during calls to the user's function. */
62133488Sharti
63133488ShartiVFunction *rl_linefunc;		/* user callback function */
64133488Shartistatic int in_handler;		/* terminal_prepped and signals set? */
65133488Sharti
66133488Sharti/* Make sure the terminal is set up, initialize readline, and prompt. */
67133488Shartistatic void
68133488Sharti_rl_callback_newline ()
69133488Sharti{
70133488Sharti  rl_initialize ();
71133488Sharti
72133488Sharti  if (in_handler == 0)
73133488Sharti    {
74133488Sharti      in_handler = 1;
75133488Sharti
76133488Sharti      (*rl_prep_term_function) (_rl_meta_flag);
77133488Sharti
78133488Sharti#if defined (HANDLE_SIGNALS)
79133488Sharti      rl_set_signals ();
80133488Sharti#endif
81133488Sharti    }
82133488Sharti
83133488Sharti  readline_internal_setup ();
84133488Sharti}
85133488Sharti
86133488Sharti/* Install a readline handler, set up the terminal, and issue the prompt. */
87133488Shartivoid
88133488Shartirl_callback_handler_install (prompt, linefunc)
89133488Sharti     char *prompt;
90133488Sharti     VFunction *linefunc;
91133488Sharti{
92133488Sharti  rl_prompt = prompt;
93133488Sharti  rl_visible_prompt_length = rl_prompt ? rl_expand_prompt (rl_prompt) : 0;
94133488Sharti  rl_linefunc = linefunc;
95133488Sharti  _rl_callback_newline ();
96133488Sharti}
97
98/* Read one character, and dispatch to the handler if it ends the line. */
99void
100rl_callback_read_char ()
101{
102  char *line;
103  int eof;
104
105  if (rl_linefunc == NULL)
106    {
107      fprintf (stderr, "readline: readline_callback_read_char() called with no handler!\r\n");
108      abort ();
109    }
110
111  eof = readline_internal_char ();
112
113  if (rl_done)
114    {
115      line = readline_internal_teardown (eof);
116
117      (*rl_deprep_term_function) ();
118#if defined (HANDLE_SIGNALS)
119      rl_clear_signals ();
120#endif
121      in_handler = 0;
122      (*rl_linefunc) (line);
123
124    /* If the user did not clear out the line, do it for him. */
125    if (rl_line_buffer[0])
126      _rl_init_line_state ();
127
128    /* Redisplay the prompt if readline_handler_{install,remove} not called. */
129      if (in_handler == 0 && rl_linefunc)
130	_rl_callback_newline ();
131    }
132}
133
134/* Remove the handler, and make sure the terminal is in its normal state. */
135void
136rl_callback_handler_remove ()
137{
138  rl_linefunc = NULL;
139  if (in_handler)
140    {
141      in_handler = 0;
142      (*rl_deprep_term_function) ();
143#if defined (HANDLE_SIGNALS)
144      rl_clear_signals ();
145#endif
146    }
147}
148
149#endif
150