Merge from vendor branch GCC:
[dragonfly.git] / contrib / libreadline / callback.c
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 #include <stdio.h>
34
35 /* System-specific feature definitions and include files. */
36 #include "rldefs.h"
37 #include "readline.h"
38 #include "rlprivate.h"
39
40 /* **************************************************************** */
41 /*                                                                  */
42 /*                      Callback Readline Functions                 */
43 /*                                                                  */
44 /* **************************************************************** */
45
46 /* Allow using readline in situations where a program may have multiple
47    things to handle at once, and dispatches them via select().  Call
48    rl_callback_handler_install() with the prompt and a function to call
49    whenever a complete line of input is ready.  The user must then
50    call rl_callback_read_char() every time some input is available, and 
51    rl_callback_read_char() will call the user's function with the complete
52    text read in at each end of line.  The terminal is kept prepped and
53    signals handled all the time, except during calls to the user's function. */
54
55 VFunction *rl_linefunc;         /* user callback function */
56 static int in_handler;          /* terminal_prepped and signals set? */
57
58 /* Make sure the terminal is set up, initialize readline, and prompt. */
59 static void
60 _rl_callback_newline ()
61 {
62   rl_initialize ();
63
64   if (in_handler == 0)
65     {
66       in_handler = 1;
67
68       (*rl_prep_term_function) (_rl_meta_flag);
69
70 #if defined (HANDLE_SIGNALS)
71       rl_set_signals ();
72 #endif
73     }
74
75   readline_internal_setup ();
76 }
77
78 /* Install a readline handler, set up the terminal, and issue the prompt. */
79 void
80 rl_callback_handler_install (prompt, linefunc)
81      char *prompt;
82      VFunction *linefunc;
83 {
84   rl_prompt = prompt;
85   rl_visible_prompt_length = rl_prompt ? rl_expand_prompt (rl_prompt) : 0;
86   rl_linefunc = linefunc;
87   _rl_callback_newline ();
88 }
89
90 /* Read one character, and dispatch to the handler if it ends the line. */
91 void
92 rl_callback_read_char ()
93 {
94   char *line;
95   int eof;
96
97   if (rl_linefunc == NULL)
98     {
99       fprintf (stderr, "readline: readline_callback_read_char() called with no handler!\r\n");
100       abort ();
101     }
102
103   eof = readline_internal_char ();
104
105   if (rl_done)
106     {
107       line = readline_internal_teardown (eof);
108
109       (*rl_deprep_term_function) ();
110 #if defined (HANDLE_SIGNALS)
111       rl_clear_signals ();
112 #endif
113       in_handler = 0;
114       (*rl_linefunc) (line);
115
116     /* If the user did not clear out the line, do it for him. */
117     if (rl_line_buffer[0])
118       _rl_init_line_state ();
119
120     /* Redisplay the prompt if readline_handler_{install,remove} not called. */
121       if (in_handler == 0 && rl_linefunc)
122         _rl_callback_newline ();
123     }
124 }
125
126 /* Remove the handler, and make sure the terminal is in its normal state. */
127 void
128 rl_callback_handler_remove ()
129 {
130   rl_linefunc = NULL;
131   if (in_handler)
132     {
133       in_handler = 0;
134       (*rl_deprep_term_function) ();
135 #if defined (HANDLE_SIGNALS)
136       rl_clear_signals ();
137 #endif
138     }
139 }
140
141 #endif