callback.c revision 21308
1200483Srwatson/* callback.c -- functions to use readline as an X `callback' mechanism. */
2200483Srwatson
3200483Srwatson/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
4200483Srwatson
5200483Srwatson   This file is part of the GNU Readline Library, a library for
6200483Srwatson   reading lines of text with interactive input and history editing.
7200483Srwatson
8200483Srwatson   The GNU Readline Library is free software; you can redistribute it
9200483Srwatson   and/or modify it under the terms of the GNU General Public License
10200483Srwatson   as published by the Free Software Foundation; either version 1, or
11200483Srwatson   (at your option) any later version.
12200483Srwatson
13200483Srwatson   The GNU Readline Library is distributed in the hope that it will be
14200483Srwatson   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15200483Srwatson   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16200483Srwatson   GNU General Public License for more details.
17200483Srwatson
18200483Srwatson   The GNU General Public License is often shipped with GNU software, and
19200483Srwatson   is generally kept in a file called COPYING or LICENSE.  If you do not
20200483Srwatson   have a copy of the license, write to the Free Software Foundation,
21200483Srwatson   675 Mass Ave, Cambridge, MA 02139, USA. */
22200483Srwatson#define READLINE_LIBRARY
23200483Srwatson
24200483Srwatson#if defined (HAVE_CONFIG_H)
25200483Srwatson#  include <config.h>
26200483Srwatson#endif
27200483Srwatson
28200483Srwatson#include "rlconf.h"
29200483Srwatson
30200483Srwatson#if defined (READLINE_CALLBACKS)
31200483Srwatson
32200483Srwatson#include <sys/types.h>
33200483Srwatson#include <stdio.h>
34200483Srwatson
35200483Srwatson/* System-specific feature definitions and include files. */
36200483Srwatson#include "rldefs.h"
37200483Srwatson#include "readline.h"
38200483Srwatson
39200483Srwatsonextern void readline_internal_startup ();
40200483Srwatsonextern char *readline_internal_teardown ();
41200483Srwatsonextern int readline_internal_char ();
42200483Srwatson
43200483Srwatsonextern int _rl_meta_flag;
44200483Srwatsonextern char *rl_prompt;
45200483Srwatsonextern int rl_visible_prompt_length;
46200483Srwatson
47200483Srwatson/* **************************************************************** */
48200573Srwatson/*								    */
49200573Srwatson/*			Callback Readline Functions                 */
50200483Srwatson/*								    */
51200573Srwatson/* **************************************************************** */
52200483Srwatson
53200483Srwatson/* Allow using readline in situations where a program may have multiple
54200483Srwatson   things to handle at once, and dispatches them via select().  Call
55200483Srwatson   rl_callback_handler_install() with the prompt and a function to call
56200573Srwatson   whenever a complete line of input is ready.  The user must then
57200483Srwatson   call readline_char() every time some input is available, and
58200483Srwatson   readline_char() will call the user's function with the complete text
59200483Srwatson   read in at each end of line.  The terminal is kept prepped and signals
60200483Srwatson   handled all the time, except during calls to the user's function. */
61200483Srwatson
62200483SrwatsonVFunction *rl_linefunc;		/* user callback function */
63200483Srwatsonstatic int in_handler;		/* terminal_prepped and signals set? */
64200483Srwatson
65200483Srwatson/* Make sure the terminal is set up, initialize readline, and prompt. */
66200483Srwatsonstatic void
67200483Srwatson_rl_callback_newline ()
68200483Srwatson{
69200483Srwatson  rl_initialize ();
70200483Srwatson
71200483Srwatson  if (in_handler == 0)
72200573Srwatson    {
73200573Srwatson      in_handler = 1;
74200483Srwatson
75200483Srwatson      (*rl_prep_term_function) (_rl_meta_flag);
76200483Srwatson
77200483Srwatson#if defined (HANDLE_SIGNALS)
78200483Srwatson      rl_set_signals ();
79200483Srwatson#endif
80200483Srwatson    }
81200483Srwatson
82200483Srwatson  readline_internal_setup ();
83200483Srwatson}
84200573Srwatson
85200483Srwatson/* Install a readline handler, set up the terminal, and issue the prompt. */
86200483Srwatsonvoid
87200483Srwatsonrl_callback_handler_install (prompt, linefunc)
88200483Srwatson     char *prompt;
89200483Srwatson     VFunction *linefunc;
90200483Srwatson{
91200483Srwatson  rl_prompt = prompt;
92200483Srwatson  rl_visible_prompt_length = rl_prompt ? rl_expand_prompt (rl_prompt) : 0;
93200483Srwatson  rl_linefunc = linefunc;
94200483Srwatson  _rl_callback_newline ();
95200483Srwatson}
96200483Srwatson
97200483Srwatson/* Read one character, and dispatch to the handler if it ends the line. */
98200483Srwatsonvoid
99200483Srwatsonrl_callback_read_char ()
100200573Srwatson{
101200483Srwatson  char *line;
102200573Srwatson  int eof;
103200573Srwatson
104200483Srwatson  if (rl_linefunc == NULL)
105200573Srwatson    {
106200483Srwatson      fprintf (stderr, "readline: readline_callback_read_char() called with no handler!\r\n");
107200573Srwatson      abort ();
108200483Srwatson    }
109200483Srwatson
110200483Srwatson  eof = readline_internal_char ();
111200483Srwatson
112200483Srwatson  if (rl_done)
113200483Srwatson    {
114200483Srwatson      line = readline_internal_teardown (eof);
115200483Srwatson
116200483Srwatson      (*rl_deprep_term_function) ();
117200483Srwatson#if defined (HANDLE_SIGNALS)
118200483Srwatson      rl_clear_signals ();
119200483Srwatson#endif
120200483Srwatson      in_handler = 0;
121200483Srwatson      (*rl_linefunc) (line);
122200483Srwatson
123200483Srwatson    /* Redisplay the prompt if readline_handler_{install,remove} not called. */
124200483Srwatson      if (in_handler == 0 && rl_linefunc)
125200483Srwatson	_rl_callback_newline ();
126200483Srwatson    }
127200483Srwatson}
128200483Srwatson
129200483Srwatson/* Remove the handler, and make sure the terminal is in its normal state. */
130void
131rl_callback_handler_remove ()
132{
133  rl_linefunc = NULL;
134  if (in_handler)
135    {
136      in_handler = 0;
137      (*rl_deprep_term_function) ();
138#if defined (HANDLE_SIGNALS)
139      rl_clear_signals ();
140#endif
141    }
142}
143
144#endif
145