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 675 Mass Ave, Cambridge, MA 02139, 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 /* What kind of non-blocking I/O do we have? */
71 #if !defined (O_NDELAY) && defined (O_NONBLOCK)
72 # define O_NDELAY O_NONBLOCK /* Posix style */
75 /* Functions imported from other files in the library. */
76 extern char *xmalloc (), *xrealloc ();
78 /* Variables and functions from macro.c. */
79 extern void _rl_add_macro_char ();
80 extern void _rl_with_macro_input ();
81 extern int _rl_next_macro_key ();
82 extern int _rl_defining_kbd_macro
;
85 extern void _rl_vi_set_last ();
86 extern int _rl_vi_textmod_command ();
89 extern FILE *rl_instream
, *rl_outstream
;
90 extern Function
*rl_last_func
;
91 extern int rl_key_sequence_length
;
92 extern int rl_pending_input
;
93 extern int rl_editing_mode
;
95 extern Keymap _rl_keymap
;
97 extern int _rl_convert_meta_chars_to_ascii
;
99 #if defined (__GO32__)
101 #endif /* __GO32__ */
103 /* Non-null means it is a pointer to a function to run while waiting for
105 Function
*rl_event_hook
= (Function
*)NULL
;
107 Function
*rl_getc_function
= rl_getc
;
109 /* **************************************************************** */
111 /* Character Input Buffering */
113 /* **************************************************************** */
115 static int pop_index
, push_index
;
116 static unsigned char ibuffer
[512];
117 static int ibuffer_len
= sizeof (ibuffer
) - 1;
119 #define any_typein (push_index != pop_index)
127 /* Add KEY to the buffer of characters to be read. */
135 rl_pending_input
= EOF
;
137 ibuffer
[push_index
++] = key
;
138 if (push_index
>= ibuffer_len
)
143 /* Make C be the next command to be executed. */
148 rl_pending_input
= c
;
152 /* Return the amount of space available in the
153 buffer for stuffing characters. */
157 if (pop_index
> push_index
)
158 return (pop_index
- push_index
);
160 return (ibuffer_len
- (push_index
- pop_index
));
163 /* Get a key from the buffer of characters to be read.
164 Return the key in KEY.
165 Result is KEY if there was a key, or 0 if there wasn't. */
170 if (push_index
== pop_index
)
173 *key
= ibuffer
[pop_index
++];
175 if (pop_index
>= ibuffer_len
)
181 /* Stuff KEY into the *front* of the input buffer.
182 Returns non-zero if successful, zero if there is
183 no space left in the buffer. */
188 if (ibuffer_space ())
192 pop_index
= ibuffer_len
- 1;
193 ibuffer
[pop_index
] = key
;
199 /* If a character is available to be read, then read it
200 and stuff it into IBUFFER. Otherwise, just return. */
204 #if defined (__GO32__)
207 if (isatty (0) && kbhit () && ibuffer_space ())
210 i
= (*rl_getc_function
) (rl_instream
);
213 #else /* !__GO32__ */
216 register int tem
, result
;
219 #if defined(HAVE_SELECT)
220 fd_set readfds
, exceptfds
;
221 struct timeval timeout
;
224 tty
= fileno (rl_instream
);
226 #if defined (HAVE_SELECT)
228 FD_ZERO (&exceptfds
);
229 FD_SET (tty
, &readfds
);
230 FD_SET (tty
, &exceptfds
);
232 timeout
.tv_usec
= 100000; /* 0.1 seconds */
233 if (select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
) <= 0)
234 return; /* Nothing to read. */
238 #if defined (FIONREAD)
239 result
= ioctl (tty
, FIONREAD
, &chars_avail
);
242 #if defined (O_NDELAY)
245 tem
= fcntl (tty
, F_GETFL
, 0);
247 fcntl (tty
, F_SETFL
, (tem
| O_NDELAY
));
248 chars_avail
= read (tty
, &input
, 1);
250 fcntl (tty
, F_SETFL
, tem
);
251 if (chars_avail
== -1 && errno
== EAGAIN
)
254 #endif /* O_NDELAY */
256 /* If there's nothing available, don't waste time trying to read
258 if (chars_avail
<= 0)
261 tem
= ibuffer_space ();
263 if (chars_avail
> tem
)
266 /* One cannot read all of the available input. I can only read a single
267 character at a time, or else programs which require input can be
268 thwarted. If the buffer is larger than one character, I lose.
270 if (tem
< ibuffer_len
)
275 while (chars_avail
--)
276 rl_stuff_char ((*rl_getc_function
) (rl_instream
));
281 rl_stuff_char (input
);
283 #endif /* !__GO32__ */
286 /* Is there input available to be read on the readline input file
287 descriptor? Only works if the system has select(2) or FIONREAD. */
289 _rl_input_available ()
291 #if defined(HAVE_SELECT)
292 fd_set readfds
, exceptfds
;
293 struct timeval timeout
;
295 #if defined(FIONREAD)
300 tty
= fileno (rl_instream
);
302 #if defined (HAVE_SELECT)
304 FD_ZERO (&exceptfds
);
305 FD_SET (tty
, &readfds
);
306 FD_SET (tty
, &exceptfds
);
308 timeout
.tv_usec
= 100000; /* 0.1 seconds */
309 return (select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
) > 0);
312 #if defined (FIONREAD)
313 if (ioctl (tty
, FIONREAD
, &chars_avail
) == 0)
314 return (chars_avail
);
321 _rl_insert_typein (c
)
328 string
= xmalloc (ibuffer_len
+ 1);
329 string
[i
++] = (char) c
;
331 while ((t
= rl_get_char (&key
)) &&
332 _rl_keymap
[key
].type
== ISFUNC
&&
333 _rl_keymap
[key
].function
== rl_insert
)
340 rl_insert_text (string
);
344 /* **************************************************************** */
346 /* Character Input */
348 /* **************************************************************** */
350 /* Read a key, including pending input. */
356 rl_key_sequence_length
++;
358 if (rl_pending_input
)
360 c
= rl_pending_input
;
361 rl_pending_input
= 0;
365 /* If input is coming from a macro, then use that. */
366 if (c
= _rl_next_macro_key ())
369 /* If the user has an event function, then call it periodically. */
372 while (rl_event_hook
&& rl_get_char (&c
) == 0)
380 if (rl_get_char (&c
) == 0)
381 c
= (*rl_getc_function
) (rl_instream
);
395 #if defined (__GO32__)
397 return (getkey () & 0x7F);
398 #endif /* __GO32__ */
402 result
= read (fileno (stream
), &c
, sizeof (unsigned char));
404 if (result
== sizeof (unsigned char))
407 /* If zero characters are returned, then the file that we are
408 reading from is empty! Return EOF in that case. */
412 #if defined (EWOULDBLOCK)
413 if (errno
== EWOULDBLOCK
)
415 if ((flags
= fcntl (fileno (stream
), F_GETFL
, 0)) < 0)
417 if (flags
& O_NDELAY
)
420 fcntl (fileno (stream
), F_SETFL
, flags
);
425 #endif /* EWOULDBLOCK */
427 #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
430 if ((flags
= fcntl (fileno (stream
), F_GETFL
, 0)) < 0)
432 if (flags
& O_NONBLOCK
)
434 flags
&= ~O_NONBLOCK
;
435 fcntl (fileno (stream
), F_SETFL
, flags
);
439 #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
441 #if !defined (__GO32__)
442 /* If the error that we received was SIGINT, then try again,
443 this is simply an interrupted system call to read ().
444 Otherwise, some error ocurred, also signifying EOF. */
447 #endif /* !__GO32__ */