1 /* input.c -- character input functions for readline. */
3 /* Copyright (C) 1994 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
30 #if defined (HAVE_SYS_FILE_H)
31 # include <sys/file.h>
32 #endif /* HAVE_SYS_FILE_H */
34 #if defined (HAVE_UNISTD_H)
36 #endif /* HAVE_UNISTD_H */
38 #if defined (HAVE_STDLIB_H)
41 # include "ansi_stdlib.h"
42 #endif /* HAVE_STDLIB_H */
44 #if defined (HAVE_SELECT)
45 # if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX)
46 # include <sys/time.h>
48 #endif /* HAVE_SELECT */
49 #if defined (HAVE_SYS_SELECT_H)
50 # include <sys/select.h>
53 #if defined (FIONREAD_IN_SYS_IOCTL)
54 # include <sys/ioctl.h>
64 /* System-specific feature definitions and include files. */
67 /* Some standard library routines. */
70 #include "rlprivate.h"
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 */
79 /* Non-null means it is a pointer to a function to run while waiting for
81 rl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL;
83 rl_getc_func_t *rl_getc_function = rl_getc;
85 static int _keyboard_input_timeout = 100000; /* 0.1 seconds; it's in usec */
87 static int ibuffer_space PARAMS((void));
88 static int rl_get_char PARAMS((int *));
89 static int rl_unget_char PARAMS((int));
90 static void rl_gather_tyi PARAMS((void));
92 /* **************************************************************** */
94 /* Character Input Buffering */
96 /* **************************************************************** */
98 static int pop_index, push_index;
99 static unsigned char ibuffer[512];
100 static int ibuffer_len = sizeof (ibuffer) - 1;
102 #define any_typein (push_index != pop_index)
110 /* Return the amount of space available in the buffer for stuffing
115 if (pop_index > push_index)
116 return (pop_index - push_index - 1);
118 return (ibuffer_len - (push_index - pop_index));
121 /* Get a key from the buffer of characters to be read.
122 Return the key in KEY.
123 Result is KEY if there was a key, or 0 if there wasn't. */
128 if (push_index == pop_index)
131 *key = ibuffer[pop_index++];
133 if (pop_index >= ibuffer_len)
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. */
146 if (ibuffer_space ())
150 pop_index = ibuffer_len - 1;
151 ibuffer[pop_index] = key;
157 /* If a character is available to be read, then read it
158 and stuff it into IBUFFER. Otherwise, just return. */
163 register int tem, result;
166 #if defined(HAVE_SELECT)
167 fd_set readfds, exceptfds;
168 struct timeval timeout;
171 tty = fileno (rl_instream);
173 #if defined (HAVE_SELECT)
175 FD_ZERO (&exceptfds);
176 FD_SET (tty, &readfds);
177 FD_SET (tty, &exceptfds);
179 timeout.tv_usec = _keyboard_input_timeout;
180 if (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) <= 0)
181 return; /* Nothing to read. */
185 #if defined (FIONREAD)
186 result = ioctl (tty, FIONREAD, &chars_avail);
189 #if defined (O_NDELAY)
192 tem = fcntl (tty, F_GETFL, 0);
194 fcntl (tty, F_SETFL, (tem | O_NDELAY));
195 chars_avail = read (tty, &input, 1);
197 fcntl (tty, F_SETFL, tem);
198 if (chars_avail == -1 && errno == EAGAIN)
201 #endif /* O_NDELAY */
203 /* If there's nothing available, don't waste time trying to read
205 if (chars_avail <= 0)
208 tem = ibuffer_space ();
210 if (chars_avail > tem)
213 /* One cannot read all of the available input. I can only read a single
214 character at a time, or else programs which require input can be
215 thwarted. If the buffer is larger than one character, I lose.
217 if (tem < ibuffer_len)
222 while (chars_avail--)
223 rl_stuff_char ((*rl_getc_function) (rl_instream));
228 rl_stuff_char (input);
233 rl_set_keyboard_input_timeout (u)
238 o = _keyboard_input_timeout;
240 _keyboard_input_timeout = u;
244 /* Is there input available to be read on the readline input file
245 descriptor? Only works if the system has select(2) or FIONREAD. */
247 _rl_input_available ()
249 #if defined(HAVE_SELECT)
250 fd_set readfds, exceptfds;
251 struct timeval timeout;
253 #if !defined (HAVE_SELECT) && defined(FIONREAD)
258 tty = fileno (rl_instream);
260 #if defined (HAVE_SELECT)
262 FD_ZERO (&exceptfds);
263 FD_SET (tty, &readfds);
264 FD_SET (tty, &exceptfds);
266 timeout.tv_usec = _keyboard_input_timeout;
267 return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
270 #if defined (FIONREAD)
271 if (ioctl (tty, FIONREAD, &chars_avail) == 0)
272 return (chars_avail);
281 _rl_insert_typein (c)
288 string = (char *)xmalloc (ibuffer_len + 1);
289 string[i++] = (char) c;
291 while ((t = rl_get_char (&key)) &&
292 _rl_keymap[key].type == ISFUNC &&
293 _rl_keymap[key].function == rl_insert)
300 rl_insert_text (string);
304 /* Add KEY to the buffer of characters to be read. Returns 1 if the
305 character was stuffed correctly; 0 otherwise. */
310 if (ibuffer_space () == 0)
316 rl_pending_input = EOF;
317 RL_SETSTATE (RL_STATE_INPUTPENDING);
319 ibuffer[push_index++] = key;
320 if (push_index >= ibuffer_len)
326 /* Make C be the next command to be executed. */
331 rl_pending_input = c;
332 RL_SETSTATE (RL_STATE_INPUTPENDING);
336 /* Clear any pending input pushed with rl_execute_next() */
338 rl_clear_pending_input ()
340 rl_pending_input = 0;
341 RL_UNSETSTATE (RL_STATE_INPUTPENDING);
345 /* **************************************************************** */
347 /* Character Input */
349 /* **************************************************************** */
351 /* Read a key, including pending input. */
357 rl_key_sequence_length++;
359 if (rl_pending_input)
361 c = rl_pending_input;
362 rl_clear_pending_input ();
366 /* If input is coming from a macro, then use that. */
367 if (c = _rl_next_macro_key ())
370 /* If the user has an event function, then call it periodically. */
373 while (rl_event_hook && rl_get_char (&c) == 0)
376 if (rl_done) /* XXX - experimental */
383 if (rl_get_char (&c) == 0)
384 c = (*rl_getc_function) (rl_instream);
400 result = read (fileno (stream), &c, sizeof (unsigned char));
402 if (result == sizeof (unsigned char))
405 /* If zero characters are returned, then the file that we are
406 reading from is empty! Return EOF in that case. */
410 #if defined (__BEOS__)
415 #if defined (EWOULDBLOCK)
416 # define X_EWOULDBLOCK EWOULDBLOCK
418 # define X_EWOULDBLOCK -99
422 # define X_EAGAIN EAGAIN
424 # define X_EAGAIN -99
427 if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
429 if (sh_unset_nodelay_mode (fileno (stream)) < 0)
437 /* If the error that we received was SIGINT, then try again,
438 this is simply an interrupted system call to read ().
439 Otherwise, some error ocurred, also signifying EOF. */