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