00001 /* input.c -- character input functions for readline. */ 00002 00003 /* Copyright (C) 1994 Free Software Foundation, Inc. 00004 00005 This file is part of the GNU Readline Library, a library for 00006 reading lines of text with interactive input and history editing. 00007 00008 The GNU Readline Library is free software; you can redistribute it 00009 and/or modify it under the terms of the GNU General Public License 00010 as published by the Free Software Foundation; either version 2, or 00011 (at your option) any later version. 00012 00013 The GNU Readline Library is distributed in the hope that it will be 00014 useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00015 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 The GNU General Public License is often shipped with GNU software, and 00019 is generally kept in a file called COPYING or LICENSE. If you do not 00020 have a copy of the license, write to the Free Software Foundation, 00021 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 00022 #define READLINE_LIBRARY 00023 00024 #if defined (__TANDEM) 00025 # include <floss.h> 00026 #endif 00027 00028 #include "config_readline.h" 00029 00030 #include <sys/types.h> 00031 #include <fcntl.h> 00032 #if defined (HAVE_SYS_FILE_H) 00033 # include <sys/file.h> 00034 #endif /* HAVE_SYS_FILE_H */ 00035 00036 #if defined (HAVE_UNISTD_H) 00037 # include <unistd.h> 00038 #endif /* HAVE_UNISTD_H */ 00039 00040 #if defined (HAVE_STDLIB_H) 00041 # include <stdlib.h> 00042 #else 00043 # include "ansi_stdlib.h" 00044 #endif /* HAVE_STDLIB_H */ 00045 00046 #if defined (HAVE_SELECT) 00047 # if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX) 00048 # include <sys/time.h> 00049 # endif 00050 #endif /* HAVE_SELECT */ 00051 #if defined (HAVE_SYS_SELECT_H) 00052 # include <sys/select.h> 00053 #endif 00054 00055 #if defined (FIONREAD_IN_SYS_IOCTL) 00056 # include <sys/ioctl.h> 00057 #endif 00058 00059 #include <stdio.h> 00060 #include <errno.h> 00061 00062 #if !defined (errno) 00063 extern int errno; 00064 #endif /* !errno */ 00065 00066 /* System-specific feature definitions and include files. */ 00067 #include "rldefs.h" 00068 #include "rlmbutil.h" 00069 00070 /* Some standard library routines. */ 00071 #include "readline.h" 00072 00073 #include "rlprivate.h" 00074 #include "rlshell.h" 00075 #include "xmalloc.h" 00076 00077 /* What kind of non-blocking I/O do we have? */ 00078 #if !defined (O_NDELAY) && defined (O_NONBLOCK) 00079 # define O_NDELAY O_NONBLOCK /* Posix style */ 00080 #endif 00081 00082 /* Non-null means it is a pointer to a function to run while waiting for 00083 character input. */ 00084 rl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL; 00085 00086 rl_getc_func_t *rl_getc_function = rl_getc; 00087 00088 static int _keyboard_input_timeout = 100000; /* 0.1 seconds; it's in usec */ 00089 00090 static int ibuffer_space PARAMS((void)); 00091 static int rl_get_char PARAMS((int *)); 00092 static int rl_gather_tyi PARAMS((void)); 00093 00094 /* **************************************************************** */ 00095 /* */ 00096 /* Character Input Buffering */ 00097 /* */ 00098 /* **************************************************************** */ 00099 00100 static int pop_index, push_index; 00101 static unsigned char ibuffer[512]; 00102 static int ibuffer_len = sizeof (ibuffer) - 1; 00103 00104 #define any_typein (push_index != pop_index) 00105 00106 int 00107 _rl_any_typein () 00108 { 00109 return any_typein; 00110 } 00111 00112 /* Return the amount of space available in the buffer for stuffing 00113 characters. */ 00114 static int 00115 ibuffer_space () 00116 { 00117 if (pop_index > push_index) 00118 return (pop_index - push_index - 1); 00119 else 00120 return (ibuffer_len - (push_index - pop_index)); 00121 } 00122 00123 /* Get a key from the buffer of characters to be read. 00124 Return the key in KEY. 00125 Result is KEY if there was a key, or 0 if there wasn't. */ 00126 static int 00127 rl_get_char (key) 00128 int *key; 00129 { 00130 if (push_index == pop_index) 00131 return (0); 00132 00133 *key = ibuffer[pop_index++]; 00134 00135 if (pop_index >= ibuffer_len) 00136 pop_index = 0; 00137 00138 return (1); 00139 } 00140 00141 /* Stuff KEY into the *front* of the input buffer. 00142 Returns non-zero if successful, zero if there is 00143 no space left in the buffer. */ 00144 int 00145 _rl_unget_char (key) 00146 int key; 00147 { 00148 if (ibuffer_space ()) 00149 { 00150 pop_index--; 00151 if (pop_index < 0) 00152 pop_index = ibuffer_len - 1; 00153 ibuffer[pop_index] = key; 00154 return (1); 00155 } 00156 return (0); 00157 } 00158 00159 int 00160 _rl_pushed_input_available () 00161 { 00162 return (push_index != pop_index); 00163 } 00164 00165 /* If a character is available to be read, then read it and stuff it into 00166 IBUFFER. Otherwise, just return. Returns number of characters read 00167 (0 if none available) and -1 on error (EIO). */ 00168 static int 00169 rl_gather_tyi () 00170 { 00171 int tty; 00172 register int tem, result; 00173 int chars_avail, k; 00174 char input; 00175 #if defined(HAVE_SELECT) 00176 fd_set readfds, exceptfds; 00177 struct timeval timeout; 00178 #endif 00179 00180 tty = fileno (rl_instream); 00181 00182 #if defined (HAVE_SELECT) 00183 FD_ZERO (&readfds); 00184 FD_ZERO (&exceptfds); 00185 FD_SET (tty, &readfds); 00186 FD_SET (tty, &exceptfds); 00187 timeout.tv_sec = 0; 00188 timeout.tv_usec = _keyboard_input_timeout; 00189 result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout); 00190 if (result <= 0) 00191 return 0; /* Nothing to read. */ 00192 #endif 00193 00194 result = -1; 00195 #if defined (FIONREAD) 00196 errno = 0; 00197 result = ioctl (tty, FIONREAD, &chars_avail); 00198 if (result == -1 && errno == EIO) 00199 return -1; 00200 #endif 00201 00202 #if defined (O_NDELAY) 00203 if (result == -1) 00204 { 00205 tem = fcntl (tty, F_GETFL, 0); 00206 00207 fcntl (tty, F_SETFL, (tem | O_NDELAY)); 00208 chars_avail = read (tty, &input, 1); 00209 00210 fcntl (tty, F_SETFL, tem); 00211 if (chars_avail == -1 && errno == EAGAIN) 00212 return 0; 00213 if (chars_avail == 0) /* EOF */ 00214 { 00215 rl_stuff_char (EOF); 00216 return (0); 00217 } 00218 } 00219 #endif /* O_NDELAY */ 00220 00221 /* If there's nothing available, don't waste time trying to read 00222 something. */ 00223 if (chars_avail <= 0) 00224 return 0; 00225 00226 tem = ibuffer_space (); 00227 00228 if (chars_avail > tem) 00229 chars_avail = tem; 00230 00231 /* One cannot read all of the available input. I can only read a single 00232 character at a time, or else programs which require input can be 00233 thwarted. If the buffer is larger than one character, I lose. 00234 Damn! */ 00235 if (tem < ibuffer_len) 00236 chars_avail = 0; 00237 00238 if (result != -1) 00239 { 00240 while (chars_avail--) 00241 { 00242 k = (*rl_getc_function) (rl_instream); 00243 rl_stuff_char (k); 00244 if (k == NEWLINE || k == RETURN) 00245 break; 00246 } 00247 } 00248 else 00249 { 00250 if (chars_avail) 00251 rl_stuff_char (input); 00252 } 00253 00254 return 1; 00255 } 00256 00257 int 00258 rl_set_keyboard_input_timeout (u) 00259 int u; 00260 { 00261 int o; 00262 00263 o = _keyboard_input_timeout; 00264 if (u > 0) 00265 _keyboard_input_timeout = u; 00266 return (o); 00267 } 00268 00269 /* Is there input available to be read on the readline input file 00270 descriptor? Only works if the system has select(2) or FIONREAD. 00271 Uses the value of _keyboard_input_timeout as the timeout; if another 00272 readline function wants to specify a timeout and not leave it up to 00273 the user, it should use _rl_input_queued(timeout_value_in_microseconds) 00274 instead. */ 00275 int 00276 _rl_input_available () 00277 { 00278 #if defined(HAVE_SELECT) 00279 fd_set readfds, exceptfds; 00280 struct timeval timeout; 00281 #endif 00282 #if !defined (HAVE_SELECT) && defined(FIONREAD) 00283 int chars_avail; 00284 #endif 00285 int tty; 00286 00287 tty = fileno (rl_instream); 00288 00289 #if defined (HAVE_SELECT) 00290 FD_ZERO (&readfds); 00291 FD_ZERO (&exceptfds); 00292 FD_SET (tty, &readfds); 00293 FD_SET (tty, &exceptfds); 00294 timeout.tv_sec = 0; 00295 timeout.tv_usec = _keyboard_input_timeout; 00296 return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0); 00297 #else 00298 00299 #if defined (FIONREAD) 00300 if (ioctl (tty, FIONREAD, &chars_avail) == 0) 00301 return (chars_avail); 00302 #endif 00303 00304 #endif 00305 00306 return 0; 00307 } 00308 00309 int 00310 _rl_input_queued (t) 00311 int t; 00312 { 00313 int old_timeout, r; 00314 00315 old_timeout = rl_set_keyboard_input_timeout (t); 00316 r = _rl_input_available (); 00317 rl_set_keyboard_input_timeout (old_timeout); 00318 return r; 00319 } 00320 00321 void 00322 _rl_insert_typein (c) 00323 int c; 00324 { 00325 int key, t, i; 00326 char *string; 00327 00328 i = key = 0; 00329 string = (char *)xmalloc (ibuffer_len + 1); 00330 string[i++] = (char) c; 00331 00332 while ((t = rl_get_char (&key)) && 00333 _rl_keymap[key].type == ISFUNC && 00334 _rl_keymap[key].function == rl_insert) 00335 string[i++] = key; 00336 00337 if (t) 00338 _rl_unget_char (key); 00339 00340 string[i] = '\0'; 00341 rl_insert_text (string); 00342 free (string); 00343 } 00344 00345 /* Add KEY to the buffer of characters to be read. Returns 1 if the 00346 character was stuffed correctly; 0 otherwise. */ 00347 int 00348 rl_stuff_char (key) 00349 int key; 00350 { 00351 if (ibuffer_space () == 0) 00352 return 0; 00353 00354 if (key == EOF) 00355 { 00356 key = NEWLINE; 00357 rl_pending_input = EOF; 00358 RL_SETSTATE (RL_STATE_INPUTPENDING); 00359 } 00360 ibuffer[push_index++] = key; 00361 if (push_index >= ibuffer_len) 00362 push_index = 0; 00363 00364 return 1; 00365 } 00366 00367 /* Make C be the next command to be executed. */ 00368 int 00369 rl_execute_next (c) 00370 int c; 00371 { 00372 rl_pending_input = c; 00373 RL_SETSTATE (RL_STATE_INPUTPENDING); 00374 return 0; 00375 } 00376 00377 /* Clear any pending input pushed with rl_execute_next() */ 00378 int 00379 rl_clear_pending_input () 00380 { 00381 rl_pending_input = 0; 00382 RL_UNSETSTATE (RL_STATE_INPUTPENDING); 00383 return 0; 00384 } 00385 00386 /* **************************************************************** */ 00387 /* */ 00388 /* Character Input */ 00389 /* */ 00390 /* **************************************************************** */ 00391 00392 /* Read a key, including pending input. */ 00393 int 00394 rl_read_key () 00395 { 00396 int c; 00397 00398 rl_key_sequence_length++; 00399 00400 if (rl_pending_input) 00401 { 00402 c = rl_pending_input; 00403 rl_clear_pending_input (); 00404 } 00405 else 00406 { 00407 /* If input is coming from a macro, then use that. */ 00408 if (c = _rl_next_macro_key ()) 00409 return (c); 00410 00411 /* If the user has an event function, then call it periodically. */ 00412 if (rl_event_hook) 00413 { 00414 while (rl_event_hook && rl_get_char (&c) == 0) 00415 { 00416 (*rl_event_hook) (); 00417 if (rl_done) /* XXX - experimental */ 00418 return ('\n'); 00419 if (rl_gather_tyi () < 0) /* XXX - EIO */ 00420 { 00421 rl_done = 1; 00422 return ('\n'); 00423 } 00424 } 00425 } 00426 else 00427 { 00428 if (rl_get_char (&c) == 0) 00429 c = (*rl_getc_function) (rl_instream); 00430 } 00431 } 00432 00433 return (c); 00434 } 00435 00436 int 00437 rl_getc (stream) 00438 FILE *stream; 00439 { 00440 int result; 00441 unsigned char c; 00442 00443 while (1) 00444 { 00445 result = read (fileno (stream), &c, sizeof (unsigned char)); 00446 00447 if (result == sizeof (unsigned char)) 00448 return (c); 00449 00450 /* If zero characters are returned, then the file that we are 00451 reading from is empty! Return EOF in that case. */ 00452 if (result == 0) 00453 return (EOF); 00454 00455 #if defined (__BEOS__) 00456 if (errno == EINTR) 00457 continue; 00458 #endif 00459 00460 #if defined (EWOULDBLOCK) 00461 # define X_EWOULDBLOCK EWOULDBLOCK 00462 #else 00463 # define X_EWOULDBLOCK -99 00464 #endif 00465 00466 #if defined (EAGAIN) 00467 # define X_EAGAIN EAGAIN 00468 #else 00469 # define X_EAGAIN -99 00470 #endif 00471 00472 if (errno == X_EWOULDBLOCK || errno == X_EAGAIN) 00473 { 00474 if (sh_unset_nodelay_mode (fileno (stream)) < 0) 00475 return (EOF); 00476 continue; 00477 } 00478 00479 #undef X_EWOULDBLOCK 00480 #undef X_EAGAIN 00481 00482 /* If the error that we received was SIGINT, then try again, 00483 this is simply an interrupted system call to read (). 00484 Otherwise, some error ocurred, also signifying EOF. */ 00485 if (errno != EINTR) 00486 return (EOF); 00487 } 00488 } 00489 00490 #if defined (HANDLE_MULTIBYTE) 00491 /* read multibyte char */ 00492 int 00493 _rl_read_mbchar (mbchar, size) 00494 char *mbchar; 00495 int size; 00496 { 00497 int mb_len = 0; 00498 size_t mbchar_bytes_length; 00499 wchar_t wc; 00500 mbstate_t ps, ps_back; 00501 00502 memset(&ps, 0, sizeof (mbstate_t)); 00503 memset(&ps_back, 0, sizeof (mbstate_t)); 00504 00505 while (mb_len < size) 00506 { 00507 RL_SETSTATE(RL_STATE_MOREINPUT); 00508 mbchar[mb_len++] = rl_read_key (); 00509 RL_UNSETSTATE(RL_STATE_MOREINPUT); 00510 00511 mbchar_bytes_length = mbrtowc (&wc, mbchar, mb_len, &ps); 00512 if (mbchar_bytes_length == (size_t)(-1)) 00513 break; /* invalid byte sequence for the current locale */ 00514 else if (mbchar_bytes_length == (size_t)(-2)) 00515 { 00516 /* shorted bytes */ 00517 ps = ps_back; 00518 continue; 00519 } 00520 else if (mbchar_bytes_length > (size_t)(0)) 00521 break; 00522 } 00523 00524 return mb_len; 00525 } 00526 00527 /* Read a multibyte-character string whose first character is FIRST into 00528 the buffer MB of length MBLEN. Returns the last character read, which 00529 may be FIRST. Used by the search functions, among others. Very similar 00530 to _rl_read_mbchar. */ 00531 int 00532 _rl_read_mbstring (first, mb, mblen) 00533 int first; 00534 char *mb; 00535 int mblen; 00536 { 00537 int i, c; 00538 mbstate_t ps; 00539 00540 c = first; 00541 memset (mb, 0, mblen); 00542 for (i = 0; i < mblen; i++) 00543 { 00544 mb[i] = (char)c; 00545 memset (&ps, 0, sizeof (mbstate_t)); 00546 if (_rl_get_char_len (mb, &ps) == -2) 00547 { 00548 /* Read more for multibyte character */ 00549 RL_SETSTATE (RL_STATE_MOREINPUT); 00550 c = rl_read_key (); 00551 RL_UNSETSTATE (RL_STATE_MOREINPUT); 00552 } 00553 else 00554 break; 00555 } 00556 return c; 00557 } 00558 #endif /* HANDLE_MULTIBYTE */
1.4.7

