]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/readline/input.c
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 /* **************************************************************** */
89 /* Character Input Buffering */
91 /* **************************************************************** */
93 static int pop_index
, push_index
;
94 static unsigned char ibuffer
[512];
95 static int ibuffer_len
= sizeof (ibuffer
) - 1;
97 #define any_typein (push_index != pop_index)
105 /* Return the amount of space available in the buffer for stuffing
110 if (pop_index
> push_index
)
111 return (pop_index
- push_index
- 1);
113 return (ibuffer_len
- (push_index
- pop_index
));
116 /* Get a key from the buffer of characters to be read.
117 Return the key in KEY.
118 Result is KEY if there was a key, or 0 if there wasn't. */
123 if (push_index
== pop_index
)
126 *key
= ibuffer
[pop_index
++];
128 if (pop_index
>= ibuffer_len
)
134 /* Stuff KEY into the *front* of the input buffer.
135 Returns non-zero if successful, zero if there is
136 no space left in the buffer. */
141 if (ibuffer_space ())
145 pop_index
= ibuffer_len
- 1;
146 ibuffer
[pop_index
] = key
;
152 /* If a character is available to be read, then read it
153 and stuff it into IBUFFER. Otherwise, just return. */
158 register int tem
, result
;
161 #if defined(HAVE_SELECT)
162 fd_set readfds
, exceptfds
;
163 struct timeval timeout
;
166 tty
= fileno (rl_instream
);
168 #if defined (HAVE_SELECT)
170 FD_ZERO (&exceptfds
);
171 FD_SET (tty
, &readfds
);
172 FD_SET (tty
, &exceptfds
);
174 timeout
.tv_usec
= _keyboard_input_timeout
;
175 if (select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
) <= 0)
176 return; /* Nothing to read. */
180 #if defined (FIONREAD)
181 result
= ioctl (tty
, FIONREAD
, &chars_avail
);
184 #if defined (O_NDELAY)
187 tem
= fcntl (tty
, F_GETFL
, 0);
189 fcntl (tty
, F_SETFL
, (tem
| O_NDELAY
));
190 chars_avail
= read (tty
, &input
, 1);
192 fcntl (tty
, F_SETFL
, tem
);
193 if (chars_avail
== -1 && errno
== EAGAIN
)
196 #endif /* O_NDELAY */
198 /* If there's nothing available, don't waste time trying to read
200 if (chars_avail
<= 0)
203 tem
= ibuffer_space ();
205 if (chars_avail
> tem
)
208 /* One cannot read all of the available input. I can only read a single
209 character at a time, or else programs which require input can be
210 thwarted. If the buffer is larger than one character, I lose.
212 if (tem
< ibuffer_len
)
217 while (chars_avail
--)
218 rl_stuff_char ((*rl_getc_function
) (rl_instream
));
223 rl_stuff_char (input
);
228 rl_set_keyboard_input_timeout (u
)
233 o
= _keyboard_input_timeout
;
235 _keyboard_input_timeout
= u
;
239 /* Is there input available to be read on the readline input file
240 descriptor? Only works if the system has select(2) or FIONREAD. */
242 _rl_input_available ()
244 #if defined(HAVE_SELECT)
245 fd_set readfds
, exceptfds
;
246 struct timeval timeout
;
248 #if defined(FIONREAD)
253 tty
= fileno (rl_instream
);
255 #if defined (HAVE_SELECT)
257 FD_ZERO (&exceptfds
);
258 FD_SET (tty
, &readfds
);
259 FD_SET (tty
, &exceptfds
);
261 timeout
.tv_usec
= _keyboard_input_timeout
;
262 return (select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
) > 0);
265 #if defined (FIONREAD)
266 if (ioctl (tty
, FIONREAD
, &chars_avail
) == 0)
267 return (chars_avail
);
274 _rl_insert_typein (c
)
281 string
= xmalloc (ibuffer_len
+ 1);
282 string
[i
++] = (char) c
;
284 while ((t
= rl_get_char (&key
)) &&
285 _rl_keymap
[key
].type
== ISFUNC
&&
286 _rl_keymap
[key
].function
== rl_insert
)
293 rl_insert_text (string
);
297 /* Add KEY to the buffer of characters to be read. Returns 1 if the
298 character was stuffed correctly; 0 otherwise. */
303 if (ibuffer_space () == 0)
309 rl_pending_input
= EOF
;
310 RL_SETSTATE (RL_STATE_INPUTPENDING
);
312 ibuffer
[push_index
++] = key
;
313 if (push_index
>= ibuffer_len
)
319 /* Make C be the next command to be executed. */
324 rl_pending_input
= c
;
325 RL_SETSTATE (RL_STATE_INPUTPENDING
);
329 /* Clear any pending input pushed with rl_execute_next() */
331 rl_clear_pending_input ()
333 rl_pending_input
= 0;
334 RL_UNSETSTATE (RL_STATE_INPUTPENDING
);
338 /* **************************************************************** */
340 /* Character Input */
342 /* **************************************************************** */
344 /* Read a key, including pending input. */
350 rl_key_sequence_length
++;
352 if (rl_pending_input
)
354 c
= rl_pending_input
;
355 rl_clear_pending_input ();
359 /* If input is coming from a macro, then use that. */
360 if (c
= _rl_next_macro_key ())
363 /* If the user has an event function, then call it periodically. */
366 while (rl_event_hook
&& rl_get_char (&c
) == 0)
369 if (rl_done
) /* XXX - experimental */
376 if (rl_get_char (&c
) == 0)
377 c
= (*rl_getc_function
) (rl_instream
);
393 result
= read (fileno (stream
), &c
, sizeof (unsigned char));
395 if (result
== sizeof (unsigned char))
398 /* If zero characters are returned, then the file that we are
399 reading from is empty! Return EOF in that case. */
403 #if defined (__BEOS__)
408 #if defined (EWOULDBLOCK)
409 # define X_EWOULDBLOCK EWOULDBLOCK
411 # define X_EWOULDBLOCK -99
415 # define X_EAGAIN EAGAIN
417 # define X_EAGAIN -99
420 if (errno
== X_EWOULDBLOCK
|| errno
== X_EAGAIN
)
422 if (sh_unset_nodelay_mode (fileno (stream
)) < 0)
430 /* If the error that we received was SIGINT, then try again,
431 this is simply an interrupted system call to read ().
432 Otherwise, some error ocurred, also signifying EOF. */