1*56bb7041Schristos /* input.c -- character input functions for readline. */
2*56bb7041Schristos
3*56bb7041Schristos /* Copyright (C) 1994-2017 Free Software Foundation, Inc.
4*56bb7041Schristos
5*56bb7041Schristos This file is part of the GNU Readline Library (Readline), a library
6*56bb7041Schristos for reading lines of text with interactive input and history editing.
7*56bb7041Schristos
8*56bb7041Schristos Readline is free software: you can redistribute it and/or modify
9*56bb7041Schristos it under the terms of the GNU General Public License as published by
10*56bb7041Schristos the Free Software Foundation, either version 3 of the License, or
11*56bb7041Schristos (at your option) any later version.
12*56bb7041Schristos
13*56bb7041Schristos Readline is distributed in the hope that it will be useful,
14*56bb7041Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
15*56bb7041Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*56bb7041Schristos GNU General Public License for more details.
17*56bb7041Schristos
18*56bb7041Schristos You should have received a copy of the GNU General Public License
19*56bb7041Schristos along with Readline. If not, see <http://www.gnu.org/licenses/>.
20*56bb7041Schristos */
21*56bb7041Schristos
22*56bb7041Schristos #define READLINE_LIBRARY
23*56bb7041Schristos
24*56bb7041Schristos #if defined (__TANDEM)
25*56bb7041Schristos # include <floss.h>
26*56bb7041Schristos #endif
27*56bb7041Schristos
28*56bb7041Schristos #if defined (HAVE_CONFIG_H)
29*56bb7041Schristos # include <config.h>
30*56bb7041Schristos #endif
31*56bb7041Schristos
32*56bb7041Schristos #include <sys/types.h>
33*56bb7041Schristos #include <fcntl.h>
34*56bb7041Schristos #if defined (HAVE_SYS_FILE_H)
35*56bb7041Schristos # include <sys/file.h>
36*56bb7041Schristos #endif /* HAVE_SYS_FILE_H */
37*56bb7041Schristos
38*56bb7041Schristos #if defined (HAVE_UNISTD_H)
39*56bb7041Schristos # include <unistd.h>
40*56bb7041Schristos #endif /* HAVE_UNISTD_H */
41*56bb7041Schristos
42*56bb7041Schristos #if defined (HAVE_STDLIB_H)
43*56bb7041Schristos # include <stdlib.h>
44*56bb7041Schristos #else
45*56bb7041Schristos # include "ansi_stdlib.h"
46*56bb7041Schristos #endif /* HAVE_STDLIB_H */
47*56bb7041Schristos
48*56bb7041Schristos #include <signal.h>
49*56bb7041Schristos
50*56bb7041Schristos #include "posixselect.h"
51*56bb7041Schristos
52*56bb7041Schristos #if defined (FIONREAD_IN_SYS_IOCTL)
53*56bb7041Schristos # include <sys/ioctl.h>
54*56bb7041Schristos #endif
55*56bb7041Schristos
56*56bb7041Schristos #include <stdio.h>
57*56bb7041Schristos #include <errno.h>
58*56bb7041Schristos
59*56bb7041Schristos #if !defined (errno)
60*56bb7041Schristos extern int errno;
61*56bb7041Schristos #endif /* !errno */
62*56bb7041Schristos
63*56bb7041Schristos /* System-specific feature definitions and include files. */
64*56bb7041Schristos #include "rldefs.h"
65*56bb7041Schristos #include "rlmbutil.h"
66*56bb7041Schristos
67*56bb7041Schristos /* Some standard library routines. */
68*56bb7041Schristos #include "readline.h"
69*56bb7041Schristos
70*56bb7041Schristos #include "rlprivate.h"
71*56bb7041Schristos #include "rlshell.h"
72*56bb7041Schristos #include "xmalloc.h"
73*56bb7041Schristos
74*56bb7041Schristos /* What kind of non-blocking I/O do we have? */
75*56bb7041Schristos #if !defined (O_NDELAY) && defined (O_NONBLOCK)
76*56bb7041Schristos # define O_NDELAY O_NONBLOCK /* Posix style */
77*56bb7041Schristos #endif
78*56bb7041Schristos
79*56bb7041Schristos #if defined (HAVE_PSELECT)
80*56bb7041Schristos extern sigset_t _rl_orig_sigset;
81*56bb7041Schristos #endif
82*56bb7041Schristos
83*56bb7041Schristos /* Non-null means it is a pointer to a function to run while waiting for
84*56bb7041Schristos character input. */
85*56bb7041Schristos rl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL;
86*56bb7041Schristos
87*56bb7041Schristos /* A function to call if a read(2) is interrupted by a signal. */
88*56bb7041Schristos rl_hook_func_t *rl_signal_event_hook = (rl_hook_func_t *)NULL;
89*56bb7041Schristos
90*56bb7041Schristos /* A function to replace _rl_input_available for applications using the
91*56bb7041Schristos callback interface. */
92*56bb7041Schristos rl_hook_func_t *rl_input_available_hook = (rl_hook_func_t *)NULL;
93*56bb7041Schristos
94*56bb7041Schristos rl_getc_func_t *rl_getc_function = rl_getc;
95*56bb7041Schristos
96*56bb7041Schristos static int _keyboard_input_timeout = 100000; /* 0.1 seconds; it's in usec */
97*56bb7041Schristos
98*56bb7041Schristos static int ibuffer_space PARAMS((void));
99*56bb7041Schristos static int rl_get_char PARAMS((int *));
100*56bb7041Schristos static int rl_gather_tyi PARAMS((void));
101*56bb7041Schristos
102*56bb7041Schristos #if defined (_WIN32) && !defined (__CYGWIN__)
103*56bb7041Schristos
104*56bb7041Schristos /* 'isatty' in the Windows runtime returns non-zero for every
105*56bb7041Schristos character device, including the null device. Repair that. */
106*56bb7041Schristos #include <io.h>
107*56bb7041Schristos #include <conio.h>
108*56bb7041Schristos #define WIN32_LEAN_AND_MEAN 1
109*56bb7041Schristos #include <windows.h>
110*56bb7041Schristos
w32_isatty(int fd)111*56bb7041Schristos int w32_isatty (int fd)
112*56bb7041Schristos {
113*56bb7041Schristos if (_isatty(fd))
114*56bb7041Schristos {
115*56bb7041Schristos HANDLE h;
116*56bb7041Schristos DWORD ignored;
117*56bb7041Schristos
118*56bb7041Schristos if ((h = (HANDLE) _get_osfhandle (fd)) == INVALID_HANDLE_VALUE)
119*56bb7041Schristos {
120*56bb7041Schristos errno = EBADF;
121*56bb7041Schristos return 0;
122*56bb7041Schristos }
123*56bb7041Schristos if (GetConsoleMode (h, &ignored) != 0)
124*56bb7041Schristos return 1;
125*56bb7041Schristos }
126*56bb7041Schristos errno = ENOTTY;
127*56bb7041Schristos return 0;
128*56bb7041Schristos }
129*56bb7041Schristos
130*56bb7041Schristos #define isatty(x) w32_isatty(x)
131*56bb7041Schristos #endif
132*56bb7041Schristos
133*56bb7041Schristos /* **************************************************************** */
134*56bb7041Schristos /* */
135*56bb7041Schristos /* Character Input Buffering */
136*56bb7041Schristos /* */
137*56bb7041Schristos /* **************************************************************** */
138*56bb7041Schristos
139*56bb7041Schristos static int pop_index, push_index;
140*56bb7041Schristos static unsigned char ibuffer[512];
141*56bb7041Schristos static int ibuffer_len = sizeof (ibuffer) - 1;
142*56bb7041Schristos
143*56bb7041Schristos #define any_typein (push_index != pop_index)
144*56bb7041Schristos
145*56bb7041Schristos int
_rl_any_typein(void)146*56bb7041Schristos _rl_any_typein (void)
147*56bb7041Schristos {
148*56bb7041Schristos return any_typein;
149*56bb7041Schristos }
150*56bb7041Schristos
151*56bb7041Schristos int
_rl_pushed_input_available(void)152*56bb7041Schristos _rl_pushed_input_available (void)
153*56bb7041Schristos {
154*56bb7041Schristos return (push_index != pop_index);
155*56bb7041Schristos }
156*56bb7041Schristos
157*56bb7041Schristos /* Return the amount of space available in the buffer for stuffing
158*56bb7041Schristos characters. */
159*56bb7041Schristos static int
ibuffer_space(void)160*56bb7041Schristos ibuffer_space (void)
161*56bb7041Schristos {
162*56bb7041Schristos if (pop_index > push_index)
163*56bb7041Schristos return (pop_index - push_index - 1);
164*56bb7041Schristos else
165*56bb7041Schristos return (ibuffer_len - (push_index - pop_index));
166*56bb7041Schristos }
167*56bb7041Schristos
168*56bb7041Schristos /* Get a key from the buffer of characters to be read.
169*56bb7041Schristos Return the key in KEY.
170*56bb7041Schristos Result is non-zero if there was a key, or 0 if there wasn't. */
171*56bb7041Schristos static int
rl_get_char(int * key)172*56bb7041Schristos rl_get_char (int *key)
173*56bb7041Schristos {
174*56bb7041Schristos if (push_index == pop_index)
175*56bb7041Schristos return (0);
176*56bb7041Schristos
177*56bb7041Schristos *key = ibuffer[pop_index++];
178*56bb7041Schristos #if 0
179*56bb7041Schristos if (pop_index >= ibuffer_len)
180*56bb7041Schristos #else
181*56bb7041Schristos if (pop_index > ibuffer_len)
182*56bb7041Schristos #endif
183*56bb7041Schristos pop_index = 0;
184*56bb7041Schristos
185*56bb7041Schristos return (1);
186*56bb7041Schristos }
187*56bb7041Schristos
188*56bb7041Schristos /* Stuff KEY into the *front* of the input buffer.
189*56bb7041Schristos Returns non-zero if successful, zero if there is
190*56bb7041Schristos no space left in the buffer. */
191*56bb7041Schristos int
_rl_unget_char(int key)192*56bb7041Schristos _rl_unget_char (int key)
193*56bb7041Schristos {
194*56bb7041Schristos if (ibuffer_space ())
195*56bb7041Schristos {
196*56bb7041Schristos pop_index--;
197*56bb7041Schristos if (pop_index < 0)
198*56bb7041Schristos pop_index = ibuffer_len;
199*56bb7041Schristos ibuffer[pop_index] = key;
200*56bb7041Schristos return (1);
201*56bb7041Schristos }
202*56bb7041Schristos return (0);
203*56bb7041Schristos }
204*56bb7041Schristos
205*56bb7041Schristos /* If a character is available to be read, then read it and stuff it into
206*56bb7041Schristos IBUFFER. Otherwise, just return. Returns number of characters read
207*56bb7041Schristos (0 if none available) and -1 on error (EIO). */
208*56bb7041Schristos static int
rl_gather_tyi(void)209*56bb7041Schristos rl_gather_tyi (void)
210*56bb7041Schristos {
211*56bb7041Schristos int tty;
212*56bb7041Schristos register int tem, result;
213*56bb7041Schristos int chars_avail, k;
214*56bb7041Schristos char input;
215*56bb7041Schristos #if defined(HAVE_SELECT)
216*56bb7041Schristos fd_set readfds, exceptfds;
217*56bb7041Schristos struct timeval timeout;
218*56bb7041Schristos #endif
219*56bb7041Schristos
220*56bb7041Schristos chars_avail = 0;
221*56bb7041Schristos input = 0;
222*56bb7041Schristos tty = fileno (rl_instream);
223*56bb7041Schristos
224*56bb7041Schristos #if defined (HAVE_SELECT)
225*56bb7041Schristos FD_ZERO (&readfds);
226*56bb7041Schristos FD_ZERO (&exceptfds);
227*56bb7041Schristos FD_SET (tty, &readfds);
228*56bb7041Schristos FD_SET (tty, &exceptfds);
229*56bb7041Schristos USEC_TO_TIMEVAL (_keyboard_input_timeout, timeout);
230*56bb7041Schristos result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
231*56bb7041Schristos if (result <= 0)
232*56bb7041Schristos return 0; /* Nothing to read. */
233*56bb7041Schristos #endif
234*56bb7041Schristos
235*56bb7041Schristos result = -1;
236*56bb7041Schristos errno = 0;
237*56bb7041Schristos #if defined (FIONREAD)
238*56bb7041Schristos result = ioctl (tty, FIONREAD, &chars_avail);
239*56bb7041Schristos if (result == -1 && errno == EIO)
240*56bb7041Schristos return -1;
241*56bb7041Schristos if (result == -1)
242*56bb7041Schristos chars_avail = 0;
243*56bb7041Schristos #endif
244*56bb7041Schristos
245*56bb7041Schristos #if defined (O_NDELAY)
246*56bb7041Schristos if (result == -1)
247*56bb7041Schristos {
248*56bb7041Schristos tem = fcntl (tty, F_GETFL, 0);
249*56bb7041Schristos
250*56bb7041Schristos fcntl (tty, F_SETFL, (tem | O_NDELAY));
251*56bb7041Schristos chars_avail = read (tty, &input, 1);
252*56bb7041Schristos
253*56bb7041Schristos fcntl (tty, F_SETFL, tem);
254*56bb7041Schristos if (chars_avail == -1 && errno == EAGAIN)
255*56bb7041Schristos return 0;
256*56bb7041Schristos if (chars_avail == -1 && errno == EIO)
257*56bb7041Schristos return -1;
258*56bb7041Schristos if (chars_avail == 0) /* EOF */
259*56bb7041Schristos {
260*56bb7041Schristos rl_stuff_char (EOF);
261*56bb7041Schristos return (0);
262*56bb7041Schristos }
263*56bb7041Schristos }
264*56bb7041Schristos #endif /* O_NDELAY */
265*56bb7041Schristos
266*56bb7041Schristos #if defined (__MINGW32__)
267*56bb7041Schristos /* Use getch/_kbhit to check for available console input, in the same way
268*56bb7041Schristos that we read it normally. */
269*56bb7041Schristos chars_avail = isatty (tty) ? _kbhit () : 0;
270*56bb7041Schristos result = 0;
271*56bb7041Schristos #endif
272*56bb7041Schristos
273*56bb7041Schristos /* If there's nothing available, don't waste time trying to read
274*56bb7041Schristos something. */
275*56bb7041Schristos if (chars_avail <= 0)
276*56bb7041Schristos return 0;
277*56bb7041Schristos
278*56bb7041Schristos tem = ibuffer_space ();
279*56bb7041Schristos
280*56bb7041Schristos if (chars_avail > tem)
281*56bb7041Schristos chars_avail = tem;
282*56bb7041Schristos
283*56bb7041Schristos /* One cannot read all of the available input. I can only read a single
284*56bb7041Schristos character at a time, or else programs which require input can be
285*56bb7041Schristos thwarted. If the buffer is larger than one character, I lose.
286*56bb7041Schristos Damn! */
287*56bb7041Schristos if (tem < ibuffer_len)
288*56bb7041Schristos chars_avail = 0;
289*56bb7041Schristos
290*56bb7041Schristos if (result != -1)
291*56bb7041Schristos {
292*56bb7041Schristos while (chars_avail--)
293*56bb7041Schristos {
294*56bb7041Schristos RL_CHECK_SIGNALS ();
295*56bb7041Schristos k = (*rl_getc_function) (rl_instream);
296*56bb7041Schristos if (rl_stuff_char (k) == 0)
297*56bb7041Schristos break; /* some problem; no more room */
298*56bb7041Schristos if (k == NEWLINE || k == RETURN)
299*56bb7041Schristos break;
300*56bb7041Schristos }
301*56bb7041Schristos }
302*56bb7041Schristos else
303*56bb7041Schristos {
304*56bb7041Schristos if (chars_avail)
305*56bb7041Schristos rl_stuff_char (input);
306*56bb7041Schristos }
307*56bb7041Schristos
308*56bb7041Schristos return 1;
309*56bb7041Schristos }
310*56bb7041Schristos
311*56bb7041Schristos int
rl_set_keyboard_input_timeout(int u)312*56bb7041Schristos rl_set_keyboard_input_timeout (int u)
313*56bb7041Schristos {
314*56bb7041Schristos int o;
315*56bb7041Schristos
316*56bb7041Schristos o = _keyboard_input_timeout;
317*56bb7041Schristos if (u >= 0)
318*56bb7041Schristos _keyboard_input_timeout = u;
319*56bb7041Schristos return (o);
320*56bb7041Schristos }
321*56bb7041Schristos
322*56bb7041Schristos /* Is there input available to be read on the readline input file
323*56bb7041Schristos descriptor? Only works if the system has select(2) or FIONREAD.
324*56bb7041Schristos Uses the value of _keyboard_input_timeout as the timeout; if another
325*56bb7041Schristos readline function wants to specify a timeout and not leave it up to
326*56bb7041Schristos the user, it should use _rl_input_queued(timeout_value_in_microseconds)
327*56bb7041Schristos instead. */
328*56bb7041Schristos int
_rl_input_available(void)329*56bb7041Schristos _rl_input_available (void)
330*56bb7041Schristos {
331*56bb7041Schristos #if defined(HAVE_SELECT)
332*56bb7041Schristos fd_set readfds, exceptfds;
333*56bb7041Schristos struct timeval timeout;
334*56bb7041Schristos #endif
335*56bb7041Schristos #if !defined (HAVE_SELECT) && defined(FIONREAD)
336*56bb7041Schristos int chars_avail;
337*56bb7041Schristos #endif
338*56bb7041Schristos int tty;
339*56bb7041Schristos
340*56bb7041Schristos if (rl_input_available_hook)
341*56bb7041Schristos return (*rl_input_available_hook) ();
342*56bb7041Schristos
343*56bb7041Schristos tty = fileno (rl_instream);
344*56bb7041Schristos
345*56bb7041Schristos #if defined (HAVE_SELECT)
346*56bb7041Schristos FD_ZERO (&readfds);
347*56bb7041Schristos FD_ZERO (&exceptfds);
348*56bb7041Schristos FD_SET (tty, &readfds);
349*56bb7041Schristos FD_SET (tty, &exceptfds);
350*56bb7041Schristos timeout.tv_sec = 0;
351*56bb7041Schristos timeout.tv_usec = _keyboard_input_timeout;
352*56bb7041Schristos return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
353*56bb7041Schristos #else
354*56bb7041Schristos
355*56bb7041Schristos #if defined (FIONREAD)
356*56bb7041Schristos if (ioctl (tty, FIONREAD, &chars_avail) == 0)
357*56bb7041Schristos return (chars_avail);
358*56bb7041Schristos #endif
359*56bb7041Schristos
360*56bb7041Schristos #endif
361*56bb7041Schristos
362*56bb7041Schristos #if defined (__MINGW32__)
363*56bb7041Schristos if (isatty (tty))
364*56bb7041Schristos return (_kbhit ());
365*56bb7041Schristos #endif
366*56bb7041Schristos
367*56bb7041Schristos return 0;
368*56bb7041Schristos }
369*56bb7041Schristos
370*56bb7041Schristos int
_rl_input_queued(int t)371*56bb7041Schristos _rl_input_queued (int t)
372*56bb7041Schristos {
373*56bb7041Schristos int old_timeout, r;
374*56bb7041Schristos
375*56bb7041Schristos old_timeout = rl_set_keyboard_input_timeout (t);
376*56bb7041Schristos r = _rl_input_available ();
377*56bb7041Schristos rl_set_keyboard_input_timeout (old_timeout);
378*56bb7041Schristos return r;
379*56bb7041Schristos }
380*56bb7041Schristos
381*56bb7041Schristos void
_rl_insert_typein(int c)382*56bb7041Schristos _rl_insert_typein (int c)
383*56bb7041Schristos {
384*56bb7041Schristos int key, t, i;
385*56bb7041Schristos char *string;
386*56bb7041Schristos
387*56bb7041Schristos i = key = 0;
388*56bb7041Schristos string = (char *)xmalloc (ibuffer_len + 1);
389*56bb7041Schristos string[i++] = (char) c;
390*56bb7041Schristos
391*56bb7041Schristos while ((t = rl_get_char (&key)) &&
392*56bb7041Schristos _rl_keymap[key].type == ISFUNC &&
393*56bb7041Schristos _rl_keymap[key].function == rl_insert)
394*56bb7041Schristos string[i++] = key;
395*56bb7041Schristos
396*56bb7041Schristos if (t)
397*56bb7041Schristos _rl_unget_char (key);
398*56bb7041Schristos
399*56bb7041Schristos string[i] = '\0';
400*56bb7041Schristos rl_insert_text (string);
401*56bb7041Schristos xfree (string);
402*56bb7041Schristos }
403*56bb7041Schristos
404*56bb7041Schristos /* Add KEY to the buffer of characters to be read. Returns 1 if the
405*56bb7041Schristos character was stuffed correctly; 0 otherwise. */
406*56bb7041Schristos int
rl_stuff_char(int key)407*56bb7041Schristos rl_stuff_char (int key)
408*56bb7041Schristos {
409*56bb7041Schristos if (ibuffer_space () == 0)
410*56bb7041Schristos return 0;
411*56bb7041Schristos
412*56bb7041Schristos if (key == EOF)
413*56bb7041Schristos {
414*56bb7041Schristos key = NEWLINE;
415*56bb7041Schristos rl_pending_input = EOF;
416*56bb7041Schristos RL_SETSTATE (RL_STATE_INPUTPENDING);
417*56bb7041Schristos }
418*56bb7041Schristos ibuffer[push_index++] = key;
419*56bb7041Schristos #if 0
420*56bb7041Schristos if (push_index >= ibuffer_len)
421*56bb7041Schristos #else
422*56bb7041Schristos if (push_index > ibuffer_len)
423*56bb7041Schristos #endif
424*56bb7041Schristos push_index = 0;
425*56bb7041Schristos
426*56bb7041Schristos return 1;
427*56bb7041Schristos }
428*56bb7041Schristos
429*56bb7041Schristos /* Make C be the next command to be executed. */
430*56bb7041Schristos int
rl_execute_next(int c)431*56bb7041Schristos rl_execute_next (int c)
432*56bb7041Schristos {
433*56bb7041Schristos rl_pending_input = c;
434*56bb7041Schristos RL_SETSTATE (RL_STATE_INPUTPENDING);
435*56bb7041Schristos return 0;
436*56bb7041Schristos }
437*56bb7041Schristos
438*56bb7041Schristos /* Clear any pending input pushed with rl_execute_next() */
439*56bb7041Schristos int
rl_clear_pending_input(void)440*56bb7041Schristos rl_clear_pending_input (void)
441*56bb7041Schristos {
442*56bb7041Schristos rl_pending_input = 0;
443*56bb7041Schristos RL_UNSETSTATE (RL_STATE_INPUTPENDING);
444*56bb7041Schristos return 0;
445*56bb7041Schristos }
446*56bb7041Schristos
447*56bb7041Schristos /* **************************************************************** */
448*56bb7041Schristos /* */
449*56bb7041Schristos /* Character Input */
450*56bb7041Schristos /* */
451*56bb7041Schristos /* **************************************************************** */
452*56bb7041Schristos
453*56bb7041Schristos /* Read a key, including pending input. */
454*56bb7041Schristos int
rl_read_key(void)455*56bb7041Schristos rl_read_key (void)
456*56bb7041Schristos {
457*56bb7041Schristos int c, r;
458*56bb7041Schristos
459*56bb7041Schristos if (rl_pending_input)
460*56bb7041Schristos {
461*56bb7041Schristos c = rl_pending_input; /* XXX - cast to unsigned char if > 0? */
462*56bb7041Schristos rl_clear_pending_input ();
463*56bb7041Schristos }
464*56bb7041Schristos else
465*56bb7041Schristos {
466*56bb7041Schristos /* If input is coming from a macro, then use that. */
467*56bb7041Schristos if (c = _rl_next_macro_key ())
468*56bb7041Schristos return ((unsigned char)c);
469*56bb7041Schristos
470*56bb7041Schristos /* If the user has an event function, then call it periodically. */
471*56bb7041Schristos if (rl_event_hook)
472*56bb7041Schristos {
473*56bb7041Schristos while (rl_event_hook)
474*56bb7041Schristos {
475*56bb7041Schristos if (rl_get_char (&c) != 0)
476*56bb7041Schristos break;
477*56bb7041Schristos
478*56bb7041Schristos if ((r = rl_gather_tyi ()) < 0) /* XXX - EIO */
479*56bb7041Schristos {
480*56bb7041Schristos rl_done = 1;
481*56bb7041Schristos return (errno == EIO ? (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF) : '\n');
482*56bb7041Schristos }
483*56bb7041Schristos else if (r > 0) /* read something */
484*56bb7041Schristos continue;
485*56bb7041Schristos
486*56bb7041Schristos RL_CHECK_SIGNALS ();
487*56bb7041Schristos if (rl_done) /* XXX - experimental */
488*56bb7041Schristos return ('\n');
489*56bb7041Schristos (*rl_event_hook) ();
490*56bb7041Schristos }
491*56bb7041Schristos }
492*56bb7041Schristos else
493*56bb7041Schristos {
494*56bb7041Schristos if (rl_get_char (&c) == 0)
495*56bb7041Schristos c = (*rl_getc_function) (rl_instream);
496*56bb7041Schristos /* fprintf(stderr, "rl_read_key: calling RL_CHECK_SIGNALS: _rl_caught_signal = %d", _rl_caught_signal); */
497*56bb7041Schristos RL_CHECK_SIGNALS ();
498*56bb7041Schristos }
499*56bb7041Schristos }
500*56bb7041Schristos
501*56bb7041Schristos return (c);
502*56bb7041Schristos }
503*56bb7041Schristos
504*56bb7041Schristos int
rl_getc(FILE * stream)505*56bb7041Schristos rl_getc (FILE *stream)
506*56bb7041Schristos {
507*56bb7041Schristos int result;
508*56bb7041Schristos unsigned char c;
509*56bb7041Schristos #if defined (HAVE_PSELECT)
510*56bb7041Schristos sigset_t empty_set;
511*56bb7041Schristos fd_set readfds;
512*56bb7041Schristos #endif
513*56bb7041Schristos
514*56bb7041Schristos while (1)
515*56bb7041Schristos {
516*56bb7041Schristos RL_CHECK_SIGNALS ();
517*56bb7041Schristos
518*56bb7041Schristos /* We know at this point that _rl_caught_signal == 0 */
519*56bb7041Schristos
520*56bb7041Schristos #if defined (__MINGW32__)
521*56bb7041Schristos if (isatty (fileno (stream)))
522*56bb7041Schristos return (_getch ()); /* "There is no error return." */
523*56bb7041Schristos #endif
524*56bb7041Schristos result = 0;
525*56bb7041Schristos #if defined (HAVE_PSELECT)
526*56bb7041Schristos FD_ZERO (&readfds);
527*56bb7041Schristos FD_SET (fileno (stream), &readfds);
528*56bb7041Schristos # if defined (HANDLE_SIGNALS)
529*56bb7041Schristos result = pselect (fileno (stream) + 1, &readfds, NULL, NULL, NULL, &_rl_orig_sigset);
530*56bb7041Schristos # else
531*56bb7041Schristos sigemptyset (&empty_set);
532*56bb7041Schristos sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &empty_set);
533*56bb7041Schristos result = pselect (fileno (stream) + 1, &readfds, NULL, NULL, NULL, &empty_set);
534*56bb7041Schristos # endif /* HANDLE_SIGNALS */
535*56bb7041Schristos #endif
536*56bb7041Schristos if (result >= 0)
537*56bb7041Schristos result = read (fileno (stream), &c, sizeof (unsigned char));
538*56bb7041Schristos
539*56bb7041Schristos if (result == sizeof (unsigned char))
540*56bb7041Schristos return (c);
541*56bb7041Schristos
542*56bb7041Schristos /* If zero characters are returned, then the file that we are
543*56bb7041Schristos reading from is empty! Return EOF in that case. */
544*56bb7041Schristos if (result == 0)
545*56bb7041Schristos return (EOF);
546*56bb7041Schristos
547*56bb7041Schristos #if defined (__BEOS__)
548*56bb7041Schristos if (errno == EINTR)
549*56bb7041Schristos continue;
550*56bb7041Schristos #endif
551*56bb7041Schristos
552*56bb7041Schristos #if defined (EWOULDBLOCK)
553*56bb7041Schristos # define X_EWOULDBLOCK EWOULDBLOCK
554*56bb7041Schristos #else
555*56bb7041Schristos # define X_EWOULDBLOCK -99
556*56bb7041Schristos #endif
557*56bb7041Schristos
558*56bb7041Schristos #if defined (EAGAIN)
559*56bb7041Schristos # define X_EAGAIN EAGAIN
560*56bb7041Schristos #else
561*56bb7041Schristos # define X_EAGAIN -99
562*56bb7041Schristos #endif
563*56bb7041Schristos
564*56bb7041Schristos if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
565*56bb7041Schristos {
566*56bb7041Schristos if (sh_unset_nodelay_mode (fileno (stream)) < 0)
567*56bb7041Schristos return (EOF);
568*56bb7041Schristos continue;
569*56bb7041Schristos }
570*56bb7041Schristos
571*56bb7041Schristos #undef X_EWOULDBLOCK
572*56bb7041Schristos #undef X_EAGAIN
573*56bb7041Schristos
574*56bb7041Schristos /* fprintf(stderr, "rl_getc: result = %d errno = %d\n", result, errno); */
575*56bb7041Schristos
576*56bb7041Schristos handle_error:
577*56bb7041Schristos /* If the error that we received was EINTR, then try again,
578*56bb7041Schristos this is simply an interrupted system call to read (). We allow
579*56bb7041Schristos the read to be interrupted if we caught SIGHUP, SIGTERM, or any
580*56bb7041Schristos of the other signals readline treats specially. If the
581*56bb7041Schristos application sets an event hook, call it for other signals.
582*56bb7041Schristos Otherwise (not EINTR), some error occurred, also signifying EOF. */
583*56bb7041Schristos if (errno != EINTR)
584*56bb7041Schristos return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
585*56bb7041Schristos /* fatal signals of interest */
586*56bb7041Schristos #if defined (SIGHUP)
587*56bb7041Schristos else if (_rl_caught_signal == SIGHUP || _rl_caught_signal == SIGTERM)
588*56bb7041Schristos #else
589*56bb7041Schristos else if (_rl_caught_signal == SIGTERM)
590*56bb7041Schristos #endif
591*56bb7041Schristos return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
592*56bb7041Schristos /* keyboard-generated signals of interest */
593*56bb7041Schristos #if defined (SIGQUIT)
594*56bb7041Schristos else if (_rl_caught_signal == SIGINT || _rl_caught_signal == SIGQUIT)
595*56bb7041Schristos #else
596*56bb7041Schristos else if (_rl_caught_signal == SIGINT)
597*56bb7041Schristos #endif
598*56bb7041Schristos RL_CHECK_SIGNALS ();
599*56bb7041Schristos /* non-keyboard-generated signals of interest */
600*56bb7041Schristos #if defined (SIGWINCH)
601*56bb7041Schristos else if (_rl_caught_signal == SIGWINCH)
602*56bb7041Schristos RL_CHECK_SIGNALS ();
603*56bb7041Schristos #endif /* SIGWINCH */
604*56bb7041Schristos #if defined (SIGALRM)
605*56bb7041Schristos else if (_rl_caught_signal == SIGALRM
606*56bb7041Schristos # if defined (SIGVTALRM)
607*56bb7041Schristos || _rl_caught_signal == SIGVTALRM
608*56bb7041Schristos # endif
609*56bb7041Schristos )
610*56bb7041Schristos RL_CHECK_SIGNALS ();
611*56bb7041Schristos #endif /* SIGALRM */
612*56bb7041Schristos
613*56bb7041Schristos if (rl_signal_event_hook)
614*56bb7041Schristos (*rl_signal_event_hook) ();
615*56bb7041Schristos }
616*56bb7041Schristos }
617*56bb7041Schristos
618*56bb7041Schristos #if defined (HANDLE_MULTIBYTE)
619*56bb7041Schristos /* read multibyte char */
620*56bb7041Schristos int
_rl_read_mbchar(char * mbchar,int size)621*56bb7041Schristos _rl_read_mbchar (char *mbchar, int size)
622*56bb7041Schristos {
623*56bb7041Schristos int mb_len, c;
624*56bb7041Schristos size_t mbchar_bytes_length;
625*56bb7041Schristos wchar_t wc;
626*56bb7041Schristos mbstate_t ps, ps_back;
627*56bb7041Schristos
628*56bb7041Schristos memset(&ps, 0, sizeof (mbstate_t));
629*56bb7041Schristos memset(&ps_back, 0, sizeof (mbstate_t));
630*56bb7041Schristos
631*56bb7041Schristos mb_len = 0;
632*56bb7041Schristos while (mb_len < size)
633*56bb7041Schristos {
634*56bb7041Schristos RL_SETSTATE(RL_STATE_MOREINPUT);
635*56bb7041Schristos c = rl_read_key ();
636*56bb7041Schristos RL_UNSETSTATE(RL_STATE_MOREINPUT);
637*56bb7041Schristos
638*56bb7041Schristos if (c < 0)
639*56bb7041Schristos break;
640*56bb7041Schristos
641*56bb7041Schristos mbchar[mb_len++] = c;
642*56bb7041Schristos
643*56bb7041Schristos mbchar_bytes_length = mbrtowc (&wc, mbchar, mb_len, &ps);
644*56bb7041Schristos if (mbchar_bytes_length == (size_t)(-1))
645*56bb7041Schristos break; /* invalid byte sequence for the current locale */
646*56bb7041Schristos else if (mbchar_bytes_length == (size_t)(-2))
647*56bb7041Schristos {
648*56bb7041Schristos /* shorted bytes */
649*56bb7041Schristos ps = ps_back;
650*56bb7041Schristos continue;
651*56bb7041Schristos }
652*56bb7041Schristos else if (mbchar_bytes_length == 0)
653*56bb7041Schristos {
654*56bb7041Schristos mbchar[0] = '\0'; /* null wide character */
655*56bb7041Schristos mb_len = 1;
656*56bb7041Schristos break;
657*56bb7041Schristos }
658*56bb7041Schristos else if (mbchar_bytes_length > (size_t)(0))
659*56bb7041Schristos break;
660*56bb7041Schristos }
661*56bb7041Schristos
662*56bb7041Schristos return mb_len;
663*56bb7041Schristos }
664*56bb7041Schristos
665*56bb7041Schristos /* Read a multibyte-character string whose first character is FIRST into
666*56bb7041Schristos the buffer MB of length MLEN. Returns the last character read, which
667*56bb7041Schristos may be FIRST. Used by the search functions, among others. Very similar
668*56bb7041Schristos to _rl_read_mbchar. */
669*56bb7041Schristos int
_rl_read_mbstring(int first,char * mb,int mlen)670*56bb7041Schristos _rl_read_mbstring (int first, char *mb, int mlen)
671*56bb7041Schristos {
672*56bb7041Schristos int i, c, n;
673*56bb7041Schristos mbstate_t ps;
674*56bb7041Schristos
675*56bb7041Schristos c = first;
676*56bb7041Schristos memset (mb, 0, mlen);
677*56bb7041Schristos for (i = 0; c >= 0 && i < mlen; i++)
678*56bb7041Schristos {
679*56bb7041Schristos mb[i] = (char)c;
680*56bb7041Schristos memset (&ps, 0, sizeof (mbstate_t));
681*56bb7041Schristos n = _rl_get_char_len (mb, &ps);
682*56bb7041Schristos if (n == -2)
683*56bb7041Schristos {
684*56bb7041Schristos /* Read more for multibyte character */
685*56bb7041Schristos RL_SETSTATE (RL_STATE_MOREINPUT);
686*56bb7041Schristos c = rl_read_key ();
687*56bb7041Schristos RL_UNSETSTATE (RL_STATE_MOREINPUT);
688*56bb7041Schristos }
689*56bb7041Schristos else
690*56bb7041Schristos break;
691*56bb7041Schristos }
692*56bb7041Schristos return c;
693*56bb7041Schristos }
694*56bb7041Schristos #endif /* HANDLE_MULTIBYTE */
695