blob: e5434f943c65eeba9fade1b060b4f5b8d5fc524e [file] [log] [blame]
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001/* input.c -- character input functions for readline. */
2
Chet Rameyac50fba2014-02-26 09:36:43 -05003/* Copyright (C) 1994-2013 Free Software Foundation, Inc.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004
Jari Aalto31859422009-01-12 13:36:28 +00005 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00007
Jari Aalto31859422009-01-12 13:36:28 +00008 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
Jari Aaltoccc6cda1996-12-23 17:02:34 +000011 (at your option) any later version.
12
Jari Aalto31859422009-01-12 13:36:28 +000013 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Jari Aaltoccc6cda1996-12-23 17:02:34 +000016 GNU General Public License for more details.
17
Jari Aalto31859422009-01-12 13:36:28 +000018 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
20*/
21
Jari Aaltoccc6cda1996-12-23 17:02:34 +000022#define READLINE_LIBRARY
23
Jari Aaltob80f6442004-07-27 13:29:18 +000024#if defined (__TANDEM)
25# include <floss.h>
26#endif
27
Jari Aaltoccc6cda1996-12-23 17:02:34 +000028#if defined (HAVE_CONFIG_H)
29# include <config.h>
30#endif
31
32#include <sys/types.h>
33#include <fcntl.h>
34#if defined (HAVE_SYS_FILE_H)
35# include <sys/file.h>
36#endif /* HAVE_SYS_FILE_H */
37
38#if defined (HAVE_UNISTD_H)
39# include <unistd.h>
40#endif /* HAVE_UNISTD_H */
41
42#if defined (HAVE_STDLIB_H)
43# include <stdlib.h>
44#else
45# include "ansi_stdlib.h"
46#endif /* HAVE_STDLIB_H */
47
Chet Rameyac50fba2014-02-26 09:36:43 -050048#include <signal.h>
49
Chet Ramey00018032011-11-21 20:51:19 -050050#include "posixselect.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000051
52#if defined (FIONREAD_IN_SYS_IOCTL)
53# include <sys/ioctl.h>
54#endif
55
56#include <stdio.h>
57#include <errno.h>
58
59#if !defined (errno)
Ricardo Cerqueiraa02fbff2013-07-25 22:35:34 +010060#include <errno.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000061#endif /* !errno */
62
63/* System-specific feature definitions and include files. */
64#include "rldefs.h"
Jari Aalto7117c2d2002-07-17 14:10:11 +000065#include "rlmbutil.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000066
67/* Some standard library routines. */
68#include "readline.h"
69
Jari Aaltobb706242000-03-17 21:46:59 +000070#include "rlprivate.h"
71#include "rlshell.h"
72#include "xmalloc.h"
73
Jari Aaltoccc6cda1996-12-23 17:02:34 +000074/* What kind of non-blocking I/O do we have? */
75#if !defined (O_NDELAY) && defined (O_NONBLOCK)
76# define O_NDELAY O_NONBLOCK /* Posix style */
77#endif
78
Jari Aaltoccc6cda1996-12-23 17:02:34 +000079/* Non-null means it is a pointer to a function to run while waiting for
80 character input. */
Jari Aalto28ef6c32001-04-06 19:14:31 +000081rl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000082
Chet Rameyac50fba2014-02-26 09:36:43 -050083/* A function to call if a read(2) is interrupted by a signal. */
84rl_hook_func_t *rl_signal_event_hook = (rl_hook_func_t *)NULL;
85
86/* A function to replace _rl_input_available for applications using the
87 callback interface. */
88rl_hook_func_t *rl_input_available_hook = (rl_hook_func_t *)NULL;
89
Jari Aalto28ef6c32001-04-06 19:14:31 +000090rl_getc_func_t *rl_getc_function = rl_getc;
91
92static int _keyboard_input_timeout = 100000; /* 0.1 seconds; it's in usec */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000093
Jari Aaltof73dda02001-11-13 17:56:06 +000094static int ibuffer_space PARAMS((void));
95static int rl_get_char PARAMS((int *));
Jari Aalto7117c2d2002-07-17 14:10:11 +000096static int rl_gather_tyi PARAMS((void));
Jari Aaltof73dda02001-11-13 17:56:06 +000097
Jari Aaltoccc6cda1996-12-23 17:02:34 +000098/* **************************************************************** */
99/* */
100/* Character Input Buffering */
101/* */
102/* **************************************************************** */
103
104static int pop_index, push_index;
105static unsigned char ibuffer[512];
106static int ibuffer_len = sizeof (ibuffer) - 1;
107
108#define any_typein (push_index != pop_index)
109
110int
111_rl_any_typein ()
112{
113 return any_typein;
114}
115
Chet Rameyac50fba2014-02-26 09:36:43 -0500116int
117_rl_pushed_input_available ()
118{
119 return (push_index != pop_index);
120}
121
Jari Aaltob72432f1999-02-19 17:11:39 +0000122/* Return the amount of space available in the buffer for stuffing
123 characters. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000124static int
125ibuffer_space ()
126{
127 if (pop_index > push_index)
Jari Aaltob72432f1999-02-19 17:11:39 +0000128 return (pop_index - push_index - 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000129 else
130 return (ibuffer_len - (push_index - pop_index));
131}
132
133/* Get a key from the buffer of characters to be read.
134 Return the key in KEY.
Chet Rameyac50fba2014-02-26 09:36:43 -0500135 Result is non-zero if there was a key, or 0 if there wasn't. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000136static int
137rl_get_char (key)
138 int *key;
139{
140 if (push_index == pop_index)
141 return (0);
142
143 *key = ibuffer[pop_index++];
Jari Aaltof1be6662008-11-18 13:15:12 +0000144#if 0
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000145 if (pop_index >= ibuffer_len)
Jari Aaltof1be6662008-11-18 13:15:12 +0000146#else
147 if (pop_index > ibuffer_len)
148#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000149 pop_index = 0;
150
151 return (1);
152}
153
154/* Stuff KEY into the *front* of the input buffer.
155 Returns non-zero if successful, zero if there is
156 no space left in the buffer. */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000157int
158_rl_unget_char (key)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000159 int key;
160{
161 if (ibuffer_space ())
162 {
163 pop_index--;
164 if (pop_index < 0)
Jari Aaltof1be6662008-11-18 13:15:12 +0000165 pop_index = ibuffer_len;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000166 ibuffer[pop_index] = key;
167 return (1);
168 }
169 return (0);
170}
171
Jari Aalto7117c2d2002-07-17 14:10:11 +0000172/* If a character is available to be read, then read it and stuff it into
173 IBUFFER. Otherwise, just return. Returns number of characters read
174 (0 if none available) and -1 on error (EIO). */
175static int
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000176rl_gather_tyi ()
177{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000178 int tty;
179 register int tem, result;
Jari Aaltob80f6442004-07-27 13:29:18 +0000180 int chars_avail, k;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000181 char input;
182#if defined(HAVE_SELECT)
183 fd_set readfds, exceptfds;
184 struct timeval timeout;
185#endif
186
Jari Aalto06285672006-10-10 14:15:34 +0000187 chars_avail = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000188 tty = fileno (rl_instream);
189
190#if defined (HAVE_SELECT)
191 FD_ZERO (&readfds);
192 FD_ZERO (&exceptfds);
193 FD_SET (tty, &readfds);
194 FD_SET (tty, &exceptfds);
Chet Ramey00018032011-11-21 20:51:19 -0500195 USEC_TO_TIMEVAL (_keyboard_input_timeout, timeout);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000196 result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
197 if (result <= 0)
198 return 0; /* Nothing to read. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000199#endif
200
201 result = -1;
202#if defined (FIONREAD)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000203 errno = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000204 result = ioctl (tty, FIONREAD, &chars_avail);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000205 if (result == -1 && errno == EIO)
206 return -1;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000207#endif
208
209#if defined (O_NDELAY)
210 if (result == -1)
211 {
212 tem = fcntl (tty, F_GETFL, 0);
213
214 fcntl (tty, F_SETFL, (tem | O_NDELAY));
215 chars_avail = read (tty, &input, 1);
216
217 fcntl (tty, F_SETFL, tem);
218 if (chars_avail == -1 && errno == EAGAIN)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000219 return 0;
Jari Aaltob80f6442004-07-27 13:29:18 +0000220 if (chars_avail == 0) /* EOF */
221 {
222 rl_stuff_char (EOF);
223 return (0);
224 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000225 }
226#endif /* O_NDELAY */
227
Jari Aalto06285672006-10-10 14:15:34 +0000228#if defined (__MINGW32__)
229 /* Use getch/_kbhit to check for available console input, in the same way
230 that we read it normally. */
231 chars_avail = isatty (tty) ? _kbhit () : 0;
232 result = 0;
233#endif
234
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000235 /* If there's nothing available, don't waste time trying to read
236 something. */
237 if (chars_avail <= 0)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000238 return 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000239
240 tem = ibuffer_space ();
241
242 if (chars_avail > tem)
243 chars_avail = tem;
244
245 /* One cannot read all of the available input. I can only read a single
246 character at a time, or else programs which require input can be
247 thwarted. If the buffer is larger than one character, I lose.
248 Damn! */
249 if (tem < ibuffer_len)
250 chars_avail = 0;
251
252 if (result != -1)
253 {
254 while (chars_avail--)
Jari Aaltob80f6442004-07-27 13:29:18 +0000255 {
Jari Aalto17345e52009-02-19 22:21:29 +0000256 RL_CHECK_SIGNALS ();
Jari Aaltob80f6442004-07-27 13:29:18 +0000257 k = (*rl_getc_function) (rl_instream);
Jari Aaltof1be6662008-11-18 13:15:12 +0000258 if (rl_stuff_char (k) == 0)
259 break; /* some problem; no more room */
Jari Aaltob80f6442004-07-27 13:29:18 +0000260 if (k == NEWLINE || k == RETURN)
261 break;
262 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000263 }
264 else
265 {
266 if (chars_avail)
267 rl_stuff_char (input);
268 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000269
270 return 1;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000271}
272
Jari Aalto28ef6c32001-04-06 19:14:31 +0000273int
274rl_set_keyboard_input_timeout (u)
275 int u;
276{
277 int o;
278
279 o = _keyboard_input_timeout;
Jari Aalto06285672006-10-10 14:15:34 +0000280 if (u >= 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000281 _keyboard_input_timeout = u;
282 return (o);
283}
284
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000285/* Is there input available to be read on the readline input file
Jari Aalto7117c2d2002-07-17 14:10:11 +0000286 descriptor? Only works if the system has select(2) or FIONREAD.
287 Uses the value of _keyboard_input_timeout as the timeout; if another
288 readline function wants to specify a timeout and not leave it up to
289 the user, it should use _rl_input_queued(timeout_value_in_microseconds)
290 instead. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000291int
292_rl_input_available ()
293{
294#if defined(HAVE_SELECT)
295 fd_set readfds, exceptfds;
296 struct timeval timeout;
297#endif
Jari Aaltof73dda02001-11-13 17:56:06 +0000298#if !defined (HAVE_SELECT) && defined(FIONREAD)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000299 int chars_avail;
300#endif
301 int tty;
302
Chet Rameyac50fba2014-02-26 09:36:43 -0500303 if (rl_input_available_hook)
304 return (*rl_input_available_hook) ();
305
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000306 tty = fileno (rl_instream);
307
308#if defined (HAVE_SELECT)
309 FD_ZERO (&readfds);
310 FD_ZERO (&exceptfds);
311 FD_SET (tty, &readfds);
312 FD_SET (tty, &exceptfds);
313 timeout.tv_sec = 0;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000314 timeout.tv_usec = _keyboard_input_timeout;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000315 return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
Jari Aaltof73dda02001-11-13 17:56:06 +0000316#else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000317
318#if defined (FIONREAD)
319 if (ioctl (tty, FIONREAD, &chars_avail) == 0)
320 return (chars_avail);
321#endif
322
Jari Aaltof73dda02001-11-13 17:56:06 +0000323#endif
324
Jari Aalto06285672006-10-10 14:15:34 +0000325#if defined (__MINGW32__)
326 if (isatty (tty))
327 return (_kbhit ());
328#endif
329
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000330 return 0;
331}
332
Jari Aalto7117c2d2002-07-17 14:10:11 +0000333int
334_rl_input_queued (t)
335 int t;
336{
337 int old_timeout, r;
338
339 old_timeout = rl_set_keyboard_input_timeout (t);
340 r = _rl_input_available ();
341 rl_set_keyboard_input_timeout (old_timeout);
342 return r;
343}
344
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000345void
346_rl_insert_typein (c)
347 int c;
348{
349 int key, t, i;
350 char *string;
351
352 i = key = 0;
Jari Aaltof73dda02001-11-13 17:56:06 +0000353 string = (char *)xmalloc (ibuffer_len + 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000354 string[i++] = (char) c;
355
356 while ((t = rl_get_char (&key)) &&
357 _rl_keymap[key].type == ISFUNC &&
358 _rl_keymap[key].function == rl_insert)
359 string[i++] = key;
360
361 if (t)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000362 _rl_unget_char (key);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000363
364 string[i] = '\0';
365 rl_insert_text (string);
Jari Aalto31859422009-01-12 13:36:28 +0000366 xfree (string);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000367}
368
Jari Aaltob72432f1999-02-19 17:11:39 +0000369/* Add KEY to the buffer of characters to be read. Returns 1 if the
370 character was stuffed correctly; 0 otherwise. */
371int
372rl_stuff_char (key)
373 int key;
374{
375 if (ibuffer_space () == 0)
376 return 0;
377
378 if (key == EOF)
379 {
380 key = NEWLINE;
381 rl_pending_input = EOF;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000382 RL_SETSTATE (RL_STATE_INPUTPENDING);
Jari Aaltob72432f1999-02-19 17:11:39 +0000383 }
384 ibuffer[push_index++] = key;
Jari Aaltof1be6662008-11-18 13:15:12 +0000385#if 0
Jari Aaltob72432f1999-02-19 17:11:39 +0000386 if (push_index >= ibuffer_len)
Jari Aaltof1be6662008-11-18 13:15:12 +0000387#else
388 if (push_index > ibuffer_len)
389#endif
Jari Aaltob72432f1999-02-19 17:11:39 +0000390 push_index = 0;
391
392 return 1;
393}
394
395/* Make C be the next command to be executed. */
396int
397rl_execute_next (c)
398 int c;
399{
400 rl_pending_input = c;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000401 RL_SETSTATE (RL_STATE_INPUTPENDING);
402 return 0;
403}
404
405/* Clear any pending input pushed with rl_execute_next() */
406int
407rl_clear_pending_input ()
408{
409 rl_pending_input = 0;
410 RL_UNSETSTATE (RL_STATE_INPUTPENDING);
Jari Aaltob72432f1999-02-19 17:11:39 +0000411 return 0;
412}
413
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000414/* **************************************************************** */
415/* */
416/* Character Input */
417/* */
418/* **************************************************************** */
419
420/* Read a key, including pending input. */
421int
422rl_read_key ()
423{
Chet Ramey7a127e72012-07-10 09:43:57 -0400424 int c, r;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000425
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000426 if (rl_pending_input)
427 {
428 c = rl_pending_input;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000429 rl_clear_pending_input ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000430 }
431 else
432 {
433 /* If input is coming from a macro, then use that. */
434 if (c = _rl_next_macro_key ())
435 return (c);
436
437 /* If the user has an event function, then call it periodically. */
438 if (rl_event_hook)
439 {
Chet Ramey495aee42011-11-22 19:11:26 -0500440 while (rl_event_hook)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000441 {
Chet Ramey7a127e72012-07-10 09:43:57 -0400442 if (rl_get_char (&c) != 0)
443 break;
444
445 if ((r = rl_gather_tyi ()) < 0) /* XXX - EIO */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000446 {
447 rl_done = 1;
448 return ('\n');
449 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500450 else if (r > 0) /* read something */
Chet Ramey7a127e72012-07-10 09:43:57 -0400451 continue;
452
Chet Ramey495aee42011-11-22 19:11:26 -0500453 RL_CHECK_SIGNALS ();
Chet Ramey495aee42011-11-22 19:11:26 -0500454 if (rl_done) /* XXX - experimental */
455 return ('\n');
456 (*rl_event_hook) ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000457 }
458 }
459 else
460 {
461 if (rl_get_char (&c) == 0)
462 c = (*rl_getc_function) (rl_instream);
Chet Rameyac50fba2014-02-26 09:36:43 -0500463/* fprintf(stderr, "rl_read_key: calling RL_CHECK_SIGNALS: _rl_caught_signal = %d", _rl_caught_signal); */
Jari Aalto17345e52009-02-19 22:21:29 +0000464 RL_CHECK_SIGNALS ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000465 }
466 }
467
468 return (c);
469}
470
471int
472rl_getc (stream)
473 FILE *stream;
474{
Jari Aaltobb706242000-03-17 21:46:59 +0000475 int result;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000476 unsigned char c;
477
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000478 while (1)
479 {
Jari Aalto17345e52009-02-19 22:21:29 +0000480 RL_CHECK_SIGNALS ();
481
Chet Rameyac50fba2014-02-26 09:36:43 -0500482 /* We know at this point that _rl_caught_signal == 0 */
483
Jari Aalto95732b42005-12-07 14:08:12 +0000484#if defined (__MINGW32__)
485 if (isatty (fileno (stream)))
486 return (getch ());
487#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000488 result = read (fileno (stream), &c, sizeof (unsigned char));
489
490 if (result == sizeof (unsigned char))
491 return (c);
492
493 /* If zero characters are returned, then the file that we are
494 reading from is empty! Return EOF in that case. */
495 if (result == 0)
496 return (EOF);
497
Jari Aaltob72432f1999-02-19 17:11:39 +0000498#if defined (__BEOS__)
499 if (errno == EINTR)
500 continue;
501#endif
502
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000503#if defined (EWOULDBLOCK)
Jari Aaltobb706242000-03-17 21:46:59 +0000504# define X_EWOULDBLOCK EWOULDBLOCK
505#else
506# define X_EWOULDBLOCK -99
507#endif
508
509#if defined (EAGAIN)
510# define X_EAGAIN EAGAIN
511#else
512# define X_EAGAIN -99
513#endif
514
515 if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000516 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000517 if (sh_unset_nodelay_mode (fileno (stream)) < 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000518 return (EOF);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000519 continue;
520 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000521
Jari Aaltobb706242000-03-17 21:46:59 +0000522#undef X_EWOULDBLOCK
523#undef X_EAGAIN
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000524
Chet Rameyac50fba2014-02-26 09:36:43 -0500525/* fprintf(stderr, "rl_getc: result = %d errno = %d\n", result, errno); */
526
527 /* If the error that we received was EINTR, then try again,
528 this is simply an interrupted system call to read (). We allow
529 the read to be interrupted if we caught SIGHUP or SIGTERM (but
530 not SIGINT; let the signal handler deal with that), but if the
531 application sets an event hook, call it for other signals.
532 Otherwise (not EINTR), some error occurred, also signifying EOF. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000533 if (errno != EINTR)
Jari Aalto06285672006-10-10 14:15:34 +0000534 return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
Chet Rameyac50fba2014-02-26 09:36:43 -0500535 else if (_rl_caught_signal == SIGHUP || _rl_caught_signal == SIGTERM)
536 return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
Chet Ramey9b86eb22014-08-01 15:30:33 -0400537 /* keyboard-generated signals of interest */
Chet Rameyac50fba2014-02-26 09:36:43 -0500538 else if (_rl_caught_signal == SIGINT || _rl_caught_signal == SIGQUIT)
539 RL_CHECK_SIGNALS ();
Chet Ramey9b86eb22014-08-01 15:30:33 -0400540 /* non-keyboard-generated signals of interest */
541 else if (_rl_caught_signal == SIGALRM
542#if defined (SIGVTALRM)
543 || _rl_caught_signal == SIGVTALRM
544#endif
545 )
546 RL_CHECK_SIGNALS ();
Chet Rameyac50fba2014-02-26 09:36:43 -0500547
548 if (rl_signal_event_hook)
549 (*rl_signal_event_hook) ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000550 }
551}
Jari Aalto7117c2d2002-07-17 14:10:11 +0000552
553#if defined (HANDLE_MULTIBYTE)
554/* read multibyte char */
555int
556_rl_read_mbchar (mbchar, size)
557 char *mbchar;
558 int size;
559{
Jari Aaltof1be6662008-11-18 13:15:12 +0000560 int mb_len, c;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000561 size_t mbchar_bytes_length;
562 wchar_t wc;
563 mbstate_t ps, ps_back;
564
565 memset(&ps, 0, sizeof (mbstate_t));
566 memset(&ps_back, 0, sizeof (mbstate_t));
Jari Aaltof1be6662008-11-18 13:15:12 +0000567
568 mb_len = 0;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000569 while (mb_len < size)
570 {
571 RL_SETSTATE(RL_STATE_MOREINPUT);
Jari Aaltof1be6662008-11-18 13:15:12 +0000572 c = rl_read_key ();
Jari Aalto7117c2d2002-07-17 14:10:11 +0000573 RL_UNSETSTATE(RL_STATE_MOREINPUT);
574
Jari Aaltof1be6662008-11-18 13:15:12 +0000575 if (c < 0)
576 break;
577
578 mbchar[mb_len++] = c;
579
Jari Aalto7117c2d2002-07-17 14:10:11 +0000580 mbchar_bytes_length = mbrtowc (&wc, mbchar, mb_len, &ps);
581 if (mbchar_bytes_length == (size_t)(-1))
582 break; /* invalid byte sequence for the current locale */
583 else if (mbchar_bytes_length == (size_t)(-2))
584 {
585 /* shorted bytes */
586 ps = ps_back;
587 continue;
588 }
Jari Aalto95732b42005-12-07 14:08:12 +0000589 else if (mbchar_bytes_length == 0)
590 {
591 mbchar[0] = '\0'; /* null wide character */
592 mb_len = 1;
593 break;
594 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000595 else if (mbchar_bytes_length > (size_t)(0))
596 break;
597 }
598
599 return mb_len;
600}
601
602/* Read a multibyte-character string whose first character is FIRST into
Jari Aalto06285672006-10-10 14:15:34 +0000603 the buffer MB of length MLEN. Returns the last character read, which
Jari Aalto7117c2d2002-07-17 14:10:11 +0000604 may be FIRST. Used by the search functions, among others. Very similar
605 to _rl_read_mbchar. */
606int
Jari Aalto06285672006-10-10 14:15:34 +0000607_rl_read_mbstring (first, mb, mlen)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000608 int first;
609 char *mb;
Jari Aalto06285672006-10-10 14:15:34 +0000610 int mlen;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000611{
612 int i, c;
613 mbstate_t ps;
614
615 c = first;
Jari Aalto06285672006-10-10 14:15:34 +0000616 memset (mb, 0, mlen);
Jari Aaltof1be6662008-11-18 13:15:12 +0000617 for (i = 0; c >= 0 && i < mlen; i++)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000618 {
619 mb[i] = (char)c;
620 memset (&ps, 0, sizeof (mbstate_t));
621 if (_rl_get_char_len (mb, &ps) == -2)
622 {
623 /* Read more for multibyte character */
624 RL_SETSTATE (RL_STATE_MOREINPUT);
625 c = rl_read_key ();
626 RL_UNSETSTATE (RL_STATE_MOREINPUT);
627 }
628 else
629 break;
630 }
631 return c;
632}
633#endif /* HANDLE_MULTIBYTE */