64a55c6f90d4af4d5775eb7a397976a9a56a1a61
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 Function
*rl_event_hook
= (Function
*)NULL
;
83 Function
*rl_getc_function
= rl_getc
;
85 /* **************************************************************** */
87 /* Character Input Buffering */
89 /* **************************************************************** */
91 static int pop_index
, push_index
;
92 static unsigned char ibuffer
[512];
93 static int ibuffer_len
= sizeof (ibuffer
) - 1;
95 #define any_typein (push_index != pop_index)
103 /* Return the amount of space available in the buffer for stuffing
108 if (pop_index
> push_index
)
109 return (pop_index
- push_index
- 1);
111 return (ibuffer_len
- (push_index
- pop_index
));
114 /* Get a key from the buffer of characters to be read.
115 Return the key in KEY.
116 Result is KEY if there was a key, or 0 if there wasn't. */
121 if (push_index
== pop_index
)
124 *key
= ibuffer
[pop_index
++];
126 if (pop_index
>= ibuffer_len
)
132 /* Stuff KEY into the *front* of the input buffer.
133 Returns non-zero if successful, zero if there is
134 no space left in the buffer. */
139 if (ibuffer_space ())
143 pop_index
= ibuffer_len
- 1;
144 ibuffer
[pop_index
] = key
;
150 /* If a character is available to be read, then read it
151 and stuff it into IBUFFER. Otherwise, just return. */
156 register int tem
, result
;
159 #if defined(HAVE_SELECT)
160 fd_set readfds
, exceptfds
;
161 struct timeval timeout
;
164 tty
= fileno (rl_instream
);
166 #if defined (HAVE_SELECT)
168 FD_ZERO (&exceptfds
);
169 FD_SET (tty
, &readfds
);
170 FD_SET (tty
, &exceptfds
);
172 timeout
.tv_usec
= 100000; /* 0.1 seconds */
173 if (select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
) <= 0)
174 return; /* Nothing to read. */
178 #if defined (FIONREAD)
179 result
= ioctl (tty
, FIONREAD
, &chars_avail
);
182 #if defined (O_NDELAY)
185 tem
= fcntl (tty
, F_GETFL
, 0);
187 fcntl (tty
, F_SETFL
, (tem
| O_NDELAY
));
188 chars_avail
= read (tty
, &input
, 1);
190 fcntl (tty
, F_SETFL
, tem
);
191 if (chars_avail
== -1 && errno
== EAGAIN
)
194 #endif /* O_NDELAY */
196 /* If there's nothing available, don't waste time trying to read
198 if (chars_avail
<= 0)
201 tem
= ibuffer_space ();
203 if (chars_avail
> tem
)
206 /* One cannot read all of the available input. I can only read a single
207 character at a time, or else programs which require input can be
208 thwarted. If the buffer is larger than one character, I lose.
210 if (tem
< ibuffer_len
)
215 while (chars_avail
--)
216 rl_stuff_char ((*rl_getc_function
) (rl_instream
));
221 rl_stuff_char (input
);
225 /* Is there input available to be read on the readline input file
226 descriptor? Only works if the system has select(2) or FIONREAD. */
228 _rl_input_available ()
230 #if defined(HAVE_SELECT)
231 fd_set readfds
, exceptfds
;
232 struct timeval timeout
;
234 #if defined(FIONREAD)
239 tty
= fileno (rl_instream
);
241 #if defined (HAVE_SELECT)
243 FD_ZERO (&exceptfds
);
244 FD_SET (tty
, &readfds
);
245 FD_SET (tty
, &exceptfds
);
247 timeout
.tv_usec
= 100000; /* 0.1 seconds */
248 return (select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
) > 0);
251 #if defined (FIONREAD)
252 if (ioctl (tty
, FIONREAD
, &chars_avail
) == 0)
253 return (chars_avail
);
260 _rl_insert_typein (c
)
267 string
= xmalloc (ibuffer_len
+ 1);
268 string
[i
++] = (char) c
;
270 while ((t
= rl_get_char (&key
)) &&
271 _rl_keymap
[key
].type
== ISFUNC
&&
272 _rl_keymap
[key
].function
== rl_insert
)
279 rl_insert_text (string
);
283 /* Add KEY to the buffer of characters to be read. Returns 1 if the
284 character was stuffed correctly; 0 otherwise. */
289 if (ibuffer_space () == 0)
295 rl_pending_input
= EOF
;
297 ibuffer
[push_index
++] = key
;
298 if (push_index
>= ibuffer_len
)
304 /* Make C be the next command to be executed. */
309 rl_pending_input
= c
;
313 /* **************************************************************** */
315 /* Character Input */
317 /* **************************************************************** */
319 /* Read a key, including pending input. */
325 rl_key_sequence_length
++;
327 if (rl_pending_input
)
329 c
= rl_pending_input
;
330 rl_pending_input
= 0;
334 /* If input is coming from a macro, then use that. */
335 if (c
= _rl_next_macro_key ())
338 /* If the user has an event function, then call it periodically. */
341 while (rl_event_hook
&& rl_get_char (&c
) == 0)
349 if (rl_get_char (&c
) == 0)
350 c
= (*rl_getc_function
) (rl_instream
);
366 result
= read (fileno (stream
), &c
, sizeof (unsigned char));
368 if (result
== sizeof (unsigned char))
371 /* If zero characters are returned, then the file that we are
372 reading from is empty! Return EOF in that case. */
376 #if defined (__BEOS__)
381 #if defined (EWOULDBLOCK)
382 # define X_EWOULDBLOCK EWOULDBLOCK
384 # define X_EWOULDBLOCK -99
388 # define X_EAGAIN EAGAIN
390 # define X_EAGAIN -99
393 if (errno
== X_EWOULDBLOCK
|| errno
== X_EAGAIN
)
395 if (unset_nodelay_mode (fileno (stream
)) < 0)
403 /* If the error that we received was SIGINT, then try again,
404 this is simply an interrupted system call to read ().
405 Otherwise, some error ocurred, also signifying EOF. */