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