]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/readline/callback.c
6915be483a4293d4f7890fa30ae44a11433dd1ab
[thirdparty/bash.git] / lib / readline / 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 1, 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 675 Mass Ave, Cambridge, MA 02139, 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
39 extern void readline_internal_setup ();
40 extern char *readline_internal_teardown ();
41 extern int readline_internal_char ();
42 extern void _rl_init_line_state ();
43
44 extern int _rl_meta_flag;
45 extern char *rl_prompt;
46 extern int rl_visible_prompt_length;
47
48 /* **************************************************************** */
49 /* */
50 /* Callback Readline Functions */
51 /* */
52 /* **************************************************************** */
53
54 /* Allow using readline in situations where a program may have multiple
55 things to handle at once, and dispatches them via select(). Call
56 rl_callback_handler_install() with the prompt and a function to call
57 whenever a complete line of input is ready. The user must then
58 call rl_callback_read_char() every time some input is available, and
59 rl_callback_read_char() will call the user's function with the complete
60 text read in at each end of line. The terminal is kept prepped and
61 signals handled all the time, except during calls to the user's function. */
62
63 VFunction *rl_linefunc; /* user callback function */
64 static int in_handler; /* terminal_prepped and signals set? */
65
66 /* Make sure the terminal is set up, initialize readline, and prompt. */
67 static void
68 _rl_callback_newline ()
69 {
70 rl_initialize ();
71
72 if (in_handler == 0)
73 {
74 in_handler = 1;
75
76 (*rl_prep_term_function) (_rl_meta_flag);
77
78 #if defined (HANDLE_SIGNALS)
79 rl_set_signals ();
80 #endif
81 }
82
83 readline_internal_setup ();
84 }
85
86 /* Install a readline handler, set up the terminal, and issue the prompt. */
87 void
88 rl_callback_handler_install (prompt, linefunc)
89 char *prompt;
90 VFunction *linefunc;
91 {
92 rl_prompt = prompt;
93 rl_visible_prompt_length = rl_prompt ? rl_expand_prompt (rl_prompt) : 0;
94 rl_linefunc = linefunc;
95 _rl_callback_newline ();
96 }
97
98 /* Read one character, and dispatch to the handler if it ends the line. */
99 void
100 rl_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. */
135 void
136 rl_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