* input.c (rl_getc): Use getch to read console input on
[deliverable/binutils-gdb.git] / readline / input.c
CommitLineData
d60d9f65
SS
1/* input.c -- character input functions for readline. */
2
3/* Copyright (C) 1994 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,
1b17e766 21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
d60d9f65
SS
22#define READLINE_LIBRARY
23
24#if defined (HAVE_CONFIG_H)
25# include <config.h>
26#endif
27
28#include <sys/types.h>
29#include <fcntl.h>
30#if defined (HAVE_SYS_FILE_H)
31# include <sys/file.h>
32#endif /* HAVE_SYS_FILE_H */
33
34#if defined (HAVE_UNISTD_H)
35# include <unistd.h>
36#endif /* HAVE_UNISTD_H */
37
38#if defined (HAVE_STDLIB_H)
39# include <stdlib.h>
40#else
41# include "ansi_stdlib.h"
42#endif /* HAVE_STDLIB_H */
43
44#if defined (HAVE_SELECT)
45# if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX)
46# include <sys/time.h>
47# endif
48#endif /* HAVE_SELECT */
49#if defined (HAVE_SYS_SELECT_H)
50# include <sys/select.h>
51#endif
52
53#if defined (FIONREAD_IN_SYS_IOCTL)
54# include <sys/ioctl.h>
55#endif
56
57#include <stdio.h>
58#include <errno.h>
59
60#if !defined (errno)
61extern int errno;
62#endif /* !errno */
63
64/* System-specific feature definitions and include files. */
65#include "rldefs.h"
9255ee31 66#include "rlmbutil.h"
d60d9f65
SS
67
68/* Some standard library routines. */
69#include "readline.h"
70
1b17e766
EZ
71#include "rlprivate.h"
72#include "rlshell.h"
73#include "xmalloc.h"
74
d60d9f65
SS
75/* What kind of non-blocking I/O do we have? */
76#if !defined (O_NDELAY) && defined (O_NONBLOCK)
77# define O_NDELAY O_NONBLOCK /* Posix style */
78#endif
79
d60d9f65
SS
80/* Non-null means it is a pointer to a function to run while waiting for
81 character input. */
9255ee31 82rl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL;
d60d9f65 83
9255ee31
EZ
84rl_getc_func_t *rl_getc_function = rl_getc;
85
86static int _keyboard_input_timeout = 100000; /* 0.1 seconds; it's in usec */
87
88static int ibuffer_space PARAMS((void));
89static int rl_get_char PARAMS((int *));
90static int rl_gather_tyi PARAMS((void));
d60d9f65
SS
91
92/* **************************************************************** */
93/* */
94/* Character Input Buffering */
95/* */
96/* **************************************************************** */
97
98static int pop_index, push_index;
99static unsigned char ibuffer[512];
100static int ibuffer_len = sizeof (ibuffer) - 1;
101
102#define any_typein (push_index != pop_index)
103
104int
105_rl_any_typein ()
106{
107 return any_typein;
108}
109
c862e87b
JM
110/* Return the amount of space available in the buffer for stuffing
111 characters. */
d60d9f65
SS
112static int
113ibuffer_space ()
114{
115 if (pop_index > push_index)
c862e87b 116 return (pop_index - push_index - 1);
d60d9f65
SS
117 else
118 return (ibuffer_len - (push_index - pop_index));
119}
120
121/* Get a key from the buffer of characters to be read.
122 Return the key in KEY.
123 Result is KEY if there was a key, or 0 if there wasn't. */
124static int
125rl_get_char (key)
126 int *key;
127{
128 if (push_index == pop_index)
129 return (0);
130
131 *key = ibuffer[pop_index++];
132
133 if (pop_index >= ibuffer_len)
134 pop_index = 0;
135
136 return (1);
137}
138
139/* Stuff KEY into the *front* of the input buffer.
140 Returns non-zero if successful, zero if there is
141 no space left in the buffer. */
9255ee31
EZ
142int
143_rl_unget_char (key)
d60d9f65
SS
144 int key;
145{
146 if (ibuffer_space ())
147 {
148 pop_index--;
149 if (pop_index < 0)
150 pop_index = ibuffer_len - 1;
151 ibuffer[pop_index] = key;
152 return (1);
153 }
154 return (0);
155}
156
9255ee31
EZ
157/* If a character is available to be read, then read it and stuff it into
158 IBUFFER. Otherwise, just return. Returns number of characters read
159 (0 if none available) and -1 on error (EIO). */
160static int
d60d9f65
SS
161rl_gather_tyi ()
162{
d60d9f65
SS
163 int tty;
164 register int tem, result;
165 int chars_avail;
166 char input;
167#if defined(HAVE_SELECT)
168 fd_set readfds, exceptfds;
169 struct timeval timeout;
170#endif
171
172 tty = fileno (rl_instream);
173
174#if defined (HAVE_SELECT)
175 FD_ZERO (&readfds);
176 FD_ZERO (&exceptfds);
177 FD_SET (tty, &readfds);
178 FD_SET (tty, &exceptfds);
179 timeout.tv_sec = 0;
9255ee31
EZ
180 timeout.tv_usec = _keyboard_input_timeout;
181 result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
182 if (result <= 0)
183 return 0; /* Nothing to read. */
d60d9f65
SS
184#endif
185
186 result = -1;
187#if defined (FIONREAD)
9255ee31 188 errno = 0;
d60d9f65 189 result = ioctl (tty, FIONREAD, &chars_avail);
9255ee31
EZ
190 if (result == -1 && errno == EIO)
191 return -1;
d60d9f65
SS
192#endif
193
194#if defined (O_NDELAY)
195 if (result == -1)
196 {
197 tem = fcntl (tty, F_GETFL, 0);
198
199 fcntl (tty, F_SETFL, (tem | O_NDELAY));
200 chars_avail = read (tty, &input, 1);
201
202 fcntl (tty, F_SETFL, tem);
203 if (chars_avail == -1 && errno == EAGAIN)
9255ee31 204 return 0;
d60d9f65
SS
205 }
206#endif /* O_NDELAY */
207
208 /* If there's nothing available, don't waste time trying to read
209 something. */
210 if (chars_avail <= 0)
9255ee31 211 return 0;
d60d9f65
SS
212
213 tem = ibuffer_space ();
214
215 if (chars_avail > tem)
216 chars_avail = tem;
217
218 /* One cannot read all of the available input. I can only read a single
219 character at a time, or else programs which require input can be
220 thwarted. If the buffer is larger than one character, I lose.
221 Damn! */
222 if (tem < ibuffer_len)
223 chars_avail = 0;
224
225 if (result != -1)
226 {
227 while (chars_avail--)
228 rl_stuff_char ((*rl_getc_function) (rl_instream));
229 }
230 else
231 {
232 if (chars_avail)
233 rl_stuff_char (input);
234 }
9255ee31
EZ
235
236 return 1;
237}
238
239int
240rl_set_keyboard_input_timeout (u)
241 int u;
242{
243 int o;
244
245 o = _keyboard_input_timeout;
246 if (u > 0)
247 _keyboard_input_timeout = u;
248 return (o);
d60d9f65
SS
249}
250
251/* Is there input available to be read on the readline input file
9255ee31
EZ
252 descriptor? Only works if the system has select(2) or FIONREAD.
253 Uses the value of _keyboard_input_timeout as the timeout; if another
254 readline function wants to specify a timeout and not leave it up to
255 the user, it should use _rl_input_queued(timeout_value_in_microseconds)
256 instead. */
d60d9f65
SS
257int
258_rl_input_available ()
259{
260#if defined(HAVE_SELECT)
261 fd_set readfds, exceptfds;
262 struct timeval timeout;
263#endif
9255ee31 264#if !defined (HAVE_SELECT) && defined(FIONREAD)
d60d9f65
SS
265 int chars_avail;
266#endif
267 int tty;
268
269 tty = fileno (rl_instream);
270
271#if defined (HAVE_SELECT)
272 FD_ZERO (&readfds);
273 FD_ZERO (&exceptfds);
274 FD_SET (tty, &readfds);
275 FD_SET (tty, &exceptfds);
276 timeout.tv_sec = 0;
9255ee31 277 timeout.tv_usec = _keyboard_input_timeout;
d60d9f65 278 return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
9255ee31 279#else
d60d9f65
SS
280
281#if defined (FIONREAD)
282 if (ioctl (tty, FIONREAD, &chars_avail) == 0)
283 return (chars_avail);
9255ee31
EZ
284#endif
285
d60d9f65
SS
286#endif
287
288 return 0;
289}
290
9255ee31
EZ
291int
292_rl_input_queued (t)
293 int t;
294{
295 int old_timeout, r;
296
297 old_timeout = rl_set_keyboard_input_timeout (t);
298 r = _rl_input_available ();
299 rl_set_keyboard_input_timeout (old_timeout);
300 return r;
301}
302
d60d9f65
SS
303void
304_rl_insert_typein (c)
305 int c;
306{
307 int key, t, i;
308 char *string;
309
310 i = key = 0;
9255ee31 311 string = (char *)xmalloc (ibuffer_len + 1);
d60d9f65
SS
312 string[i++] = (char) c;
313
314 while ((t = rl_get_char (&key)) &&
315 _rl_keymap[key].type == ISFUNC &&
316 _rl_keymap[key].function == rl_insert)
317 string[i++] = key;
318
319 if (t)
9255ee31 320 _rl_unget_char (key);
d60d9f65
SS
321
322 string[i] = '\0';
323 rl_insert_text (string);
324 free (string);
325}
326
c862e87b
JM
327/* Add KEY to the buffer of characters to be read. Returns 1 if the
328 character was stuffed correctly; 0 otherwise. */
329int
330rl_stuff_char (key)
331 int key;
332{
333 if (ibuffer_space () == 0)
334 return 0;
335
336 if (key == EOF)
337 {
338 key = NEWLINE;
339 rl_pending_input = EOF;
9255ee31 340 RL_SETSTATE (RL_STATE_INPUTPENDING);
c862e87b
JM
341 }
342 ibuffer[push_index++] = key;
343 if (push_index >= ibuffer_len)
344 push_index = 0;
345
346 return 1;
347}
348
349/* Make C be the next command to be executed. */
350int
351rl_execute_next (c)
352 int c;
353{
354 rl_pending_input = c;
9255ee31
EZ
355 RL_SETSTATE (RL_STATE_INPUTPENDING);
356 return 0;
357}
358
359/* Clear any pending input pushed with rl_execute_next() */
360int
361rl_clear_pending_input ()
362{
363 rl_pending_input = 0;
364 RL_UNSETSTATE (RL_STATE_INPUTPENDING);
c862e87b
JM
365 return 0;
366}
367
d60d9f65
SS
368/* **************************************************************** */
369/* */
370/* Character Input */
371/* */
372/* **************************************************************** */
373
374/* Read a key, including pending input. */
375int
376rl_read_key ()
377{
378 int c;
379
380 rl_key_sequence_length++;
381
382 if (rl_pending_input)
383 {
384 c = rl_pending_input;
9255ee31 385 rl_clear_pending_input ();
d60d9f65
SS
386 }
387 else
388 {
389 /* If input is coming from a macro, then use that. */
390 if (c = _rl_next_macro_key ())
391 return (c);
392
393 /* If the user has an event function, then call it periodically. */
394 if (rl_event_hook)
395 {
396 while (rl_event_hook && rl_get_char (&c) == 0)
397 {
398 (*rl_event_hook) ();
9255ee31
EZ
399 if (rl_done) /* XXX - experimental */
400 return ('\n');
401 if (rl_gather_tyi () < 0) /* XXX - EIO */
402 {
403 rl_done = 1;
404 return ('\n');
405 }
d60d9f65
SS
406 }
407 }
408 else
409 {
410 if (rl_get_char (&c) == 0)
411 c = (*rl_getc_function) (rl_instream);
412 }
413 }
414
415 return (c);
416}
417
418int
419rl_getc (stream)
420 FILE *stream;
421{
1b17e766 422 int result;
d60d9f65
SS
423 unsigned char c;
424
d60d9f65
SS
425 while (1)
426 {
fd8be987
MM
427#ifdef __MINGW32__
428 /* On Windows, use a special routine to read a single character
429 from the console. (Otherwise, no characters are available
430 until the user hits the return key.) */
431 if (isatty (fileno (stream)))
432 return getch ();
433#endif
d60d9f65
SS
434 result = read (fileno (stream), &c, sizeof (unsigned char));
435
436 if (result == sizeof (unsigned char))
437 return (c);
438
439 /* If zero characters are returned, then the file that we are
440 reading from is empty! Return EOF in that case. */
441 if (result == 0)
442 return (EOF);
443
c862e87b
JM
444#if defined (__BEOS__)
445 if (errno == EINTR)
446 continue;
447#endif
448
d60d9f65 449#if defined (EWOULDBLOCK)
1b17e766
EZ
450# define X_EWOULDBLOCK EWOULDBLOCK
451#else
452# define X_EWOULDBLOCK -99
453#endif
454
455#if defined (EAGAIN)
456# define X_EAGAIN EAGAIN
457#else
458# define X_EAGAIN -99
459#endif
460
461 if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
d60d9f65 462 {
9255ee31 463 if (sh_unset_nodelay_mode (fileno (stream)) < 0)
d60d9f65 464 return (EOF);
d60d9f65
SS
465 continue;
466 }
d60d9f65 467
1b17e766
EZ
468#undef X_EWOULDBLOCK
469#undef X_EAGAIN
d60d9f65 470
d60d9f65
SS
471 /* If the error that we received was SIGINT, then try again,
472 this is simply an interrupted system call to read ().
473 Otherwise, some error ocurred, also signifying EOF. */
474 if (errno != EINTR)
475 return (EOF);
d60d9f65
SS
476 }
477}
9255ee31
EZ
478
479#if defined (HANDLE_MULTIBYTE)
480/* read multibyte char */
481int
482_rl_read_mbchar (mbchar, size)
483 char *mbchar;
484 int size;
485{
486 int mb_len = 0;
487 size_t mbchar_bytes_length;
488 wchar_t wc;
489 mbstate_t ps, ps_back;
490
491 memset(&ps, 0, sizeof (mbstate_t));
492 memset(&ps_back, 0, sizeof (mbstate_t));
493
494 while (mb_len < size)
495 {
496 RL_SETSTATE(RL_STATE_MOREINPUT);
497 mbchar[mb_len++] = rl_read_key ();
498 RL_UNSETSTATE(RL_STATE_MOREINPUT);
499
500 mbchar_bytes_length = mbrtowc (&wc, mbchar, mb_len, &ps);
501 if (mbchar_bytes_length == (size_t)(-1))
502 break; /* invalid byte sequence for the current locale */
503 else if (mbchar_bytes_length == (size_t)(-2))
504 {
505 /* shorted bytes */
506 ps = ps_back;
507 continue;
508 }
509 else if (mbchar_bytes_length > (size_t)(0))
510 break;
511 }
512
513 return mb_len;
514}
515
516/* Read a multibyte-character string whose first character is FIRST into
517 the buffer MB of length MBLEN. Returns the last character read, which
518 may be FIRST. Used by the search functions, among others. Very similar
519 to _rl_read_mbchar. */
520int
521_rl_read_mbstring (first, mb, mblen)
522 int first;
523 char *mb;
524 int mblen;
525{
526 int i, c;
527 mbstate_t ps;
528
529 c = first;
530 memset (mb, 0, mblen);
531 for (i = 0; i < mblen; i++)
532 {
533 mb[i] = (char)c;
534 memset (&ps, 0, sizeof (mbstate_t));
535 if (_rl_get_char_len (mb, &ps) == -2)
536 {
537 /* Read more for multibyte character */
538 RL_SETSTATE (RL_STATE_MOREINPUT);
539 c = rl_read_key ();
540 RL_UNSETSTATE (RL_STATE_MOREINPUT);
541 }
542 else
543 break;
544 }
545 return c;
546}
547#endif /* HANDLE_MULTIBYTE */
This page took 0.290244 seconds and 4 git commands to generate.