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