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