Improve MinGW support in Readline
[deliverable/binutils-gdb.git] / readline / input.c
... / ...
CommitLineData
1/* input.c -- character input functions for readline. */
2
3/* Copyright (C) 1994-2010 Free Software Foundation, Inc.
4
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.
7
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
11 (at your option) any later version.
12
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
16 GNU General Public License for more details.
17
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
22#define READLINE_LIBRARY
23
24#if defined (__TANDEM)
25# include <floss.h>
26#endif
27
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
48#include "posixselect.h"
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"
63#include "rlmbutil.h"
64
65/* Some standard library routines. */
66#include "readline.h"
67
68#include "rlprivate.h"
69#include "rlshell.h"
70#include "xmalloc.h"
71
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
77/* Non-null means it is a pointer to a function to run while waiting for
78 character input. */
79rl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL;
80
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));
88
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
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
137/* Return the amount of space available in the buffer for stuffing
138 characters. */
139static int
140ibuffer_space ()
141{
142 if (pop_index > push_index)
143 return (pop_index - push_index - 1);
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.
150 Result is KEY if there was a key, or 0 if there wasn't. */
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++];
159#if 0
160 if (pop_index >= ibuffer_len)
161#else
162 if (pop_index > ibuffer_len)
163#endif
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. */
172int
173_rl_unget_char (key)
174 int key;
175{
176 if (ibuffer_space ())
177 {
178 pop_index--;
179 if (pop_index < 0)
180 pop_index = ibuffer_len;
181 ibuffer[pop_index] = key;
182 return (1);
183 }
184 return (0);
185}
186
187int
188_rl_pushed_input_available ()
189{
190 return (push_index != pop_index);
191}
192
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
197rl_gather_tyi ()
198{
199 int tty;
200 register int tem, result;
201 int chars_avail, k;
202 char input;
203#if defined(HAVE_SELECT)
204 fd_set readfds, exceptfds;
205 struct timeval timeout;
206#endif
207
208 chars_avail = 0;
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);
216 USEC_TO_TIMEVAL (_keyboard_input_timeout, timeout);
217 result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
218 if (result <= 0)
219 return 0; /* Nothing to read. */
220#endif
221
222 result = -1;
223#if defined (FIONREAD)
224 errno = 0;
225 result = ioctl (tty, FIONREAD, &chars_avail);
226 if (result == -1 && errno == EIO)
227 return -1;
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)
240 return 0;
241 if (chars_avail == 0) /* EOF */
242 {
243 rl_stuff_char (EOF);
244 return (0);
245 }
246 }
247#endif /* O_NDELAY */
248
249#if defined (__MINGW32__)
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;
254#endif
255
256 /* If there's nothing available, don't waste time trying to read
257 something. */
258 if (chars_avail <= 0)
259 return 0;
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--)
276 {
277 RL_CHECK_SIGNALS ();
278 k = (*rl_getc_function) (rl_instream);
279 if (rl_stuff_char (k) == 0)
280 break; /* some problem; no more room */
281 if (k == NEWLINE || k == RETURN)
282 break;
283 }
284 }
285 else
286 {
287 if (chars_avail)
288 rl_stuff_char (input);
289 }
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;
301 if (u >= 0)
302 _keyboard_input_timeout = u;
303 return (o);
304}
305
306/* Is there input available to be read on the readline input file
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. */
312int
313_rl_input_available ()
314{
315#if defined(HAVE_SELECT)
316 fd_set readfds, exceptfds;
317 struct timeval timeout;
318#endif
319#if !defined (HAVE_SELECT) && defined(FIONREAD)
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;
332 timeout.tv_usec = _keyboard_input_timeout;
333 return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
334#else
335
336#if defined (FIONREAD)
337 if (ioctl (tty, FIONREAD, &chars_avail) == 0)
338 return (chars_avail);
339#endif
340
341#endif
342
343#if defined (__MINGW32__)
344 if (isatty (tty))
345 return (_kbhit ());
346#endif
347
348 return 0;
349}
350
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
363void
364_rl_insert_typein (c)
365 int c;
366{
367 int key, t, i;
368 char *string;
369
370 i = key = 0;
371 string = (char *)xmalloc (ibuffer_len + 1);
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)
380 _rl_unget_char (key);
381
382 string[i] = '\0';
383 rl_insert_text (string);
384 xfree (string);
385}
386
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;
400 RL_SETSTATE (RL_STATE_INPUTPENDING);
401 }
402 ibuffer[push_index++] = key;
403#if 0
404 if (push_index >= ibuffer_len)
405#else
406 if (push_index > ibuffer_len)
407#endif
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;
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);
429 return 0;
430}
431
432/* **************************************************************** */
433/* */
434/* Character Input */
435/* */
436/* **************************************************************** */
437
438/* Read a key, including pending input. */
439int
440rl_read_key ()
441{
442 int c;
443
444 rl_key_sequence_length++;
445
446 if (rl_pending_input)
447 {
448 c = rl_pending_input;
449 rl_clear_pending_input ();
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 {
460 while (rl_event_hook)
461 {
462 if (rl_gather_tyi () < 0) /* XXX - EIO */
463 {
464 rl_done = 1;
465 return ('\n');
466 }
467 RL_CHECK_SIGNALS ();
468 if (rl_get_char (&c) != 0)
469 break;
470 if (rl_done) /* XXX - experimental */
471 return ('\n');
472 (*rl_event_hook) ();
473 }
474 }
475 else
476 {
477 if (rl_get_char (&c) == 0)
478 c = (*rl_getc_function) (rl_instream);
479 RL_CHECK_SIGNALS ();
480 }
481 }
482
483 return (c);
484}
485
486int
487rl_getc (stream)
488 FILE *stream;
489{
490 int result;
491 unsigned char c;
492
493 while (1)
494 {
495 RL_CHECK_SIGNALS ();
496
497#if defined (__MINGW32__)
498 /* Use _getch to make sure we call the function from MS runtime,
499 even if some curses library is linked in. */
500 if (isatty (fileno (stream)))
501 return (_getch ());
502#endif
503 result = read (fileno (stream), &c, sizeof (unsigned char));
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
513#if defined (__BEOS__)
514 if (errno == EINTR)
515 continue;
516#endif
517
518#if defined (EWOULDBLOCK)
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)
531 {
532 if (sh_unset_nodelay_mode (fileno (stream)) < 0)
533 return (EOF);
534 continue;
535 }
536
537#undef X_EWOULDBLOCK
538#undef X_EAGAIN
539
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. */
543 if (errno != EINTR)
544 return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
545 }
546}
547
548#if defined (HANDLE_MULTIBYTE)
549/* read multibyte char */
550int
551_rl_read_mbchar (mbchar, size)
552 char *mbchar;
553 int size;
554{
555 int mb_len, c;
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));
562
563 mb_len = 0;
564 while (mb_len < size)
565 {
566 RL_SETSTATE(RL_STATE_MOREINPUT);
567 c = rl_read_key ();
568 RL_UNSETSTATE(RL_STATE_MOREINPUT);
569
570 if (c < 0)
571 break;
572
573 mbchar[mb_len++] = c;
574
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 }
584 else if (mbchar_bytes_length == 0)
585 {
586 mbchar[0] = '\0'; /* null wide character */
587 mb_len = 1;
588 break;
589 }
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
598 the buffer MB of length MLEN. Returns the last character read, which
599 may be FIRST. Used by the search functions, among others. Very similar
600 to _rl_read_mbchar. */
601int
602_rl_read_mbstring (first, mb, mlen)
603 int first;
604 char *mb;
605 int mlen;
606{
607 int i, c;
608 mbstate_t ps;
609
610 c = first;
611 memset (mb, 0, mlen);
612 for (i = 0; c >= 0 && i < mlen; i++)
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.041089 seconds and 4 git commands to generate.