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
/* Return the amount of space available in the buffer for stuffing
132
if (pop_index > push_index)
133
return (pop_index - push_index - 1);
135
return (ibuffer_len - (push_index - pop_index));
138
/* Get a key from the buffer of characters to be read.
139
Return the key in KEY.
140
Result is KEY if there was a key, or 0 if there wasn't. */
145
if (push_index == pop_index)
148
*key = ibuffer[pop_index++];
150
if (pop_index >= ibuffer_len)
156
/* Stuff KEY into the *front* of the input buffer.
157
Returns non-zero if successful, zero if there is
158
no space left in the buffer. */
163
if (ibuffer_space ())
167
pop_index = ibuffer_len - 1;
168
ibuffer[pop_index] = key;
174
/* If a character is available to be read, then read it
175
and stuff it into IBUFFER. Otherwise, just return. */
179
#if defined (__GO32__)
182
if (isatty (0) && kbhit () && ibuffer_space ())
185
i = (*rl_getc_function) (rl_instream);
188
#else /* !__GO32__ */
191
register int tem, result;
194
#if defined(HAVE_SELECT)
195
fd_set readfds, exceptfds;
196
struct timeval timeout;
199
tty = fileno (rl_instream);
201
#if defined (HAVE_SELECT)
203
FD_ZERO (&exceptfds);
204
FD_SET (tty, &readfds);
205
FD_SET (tty, &exceptfds);
207
timeout.tv_usec = 100000; /* 0.1 seconds */
208
if (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) <= 0)
209
return; /* Nothing to read. */
213
#if defined (FIONREAD)
214
result = ioctl (tty, FIONREAD, &chars_avail);
217
#if defined (O_NDELAY)
220
tem = fcntl (tty, F_GETFL, 0);
222
fcntl (tty, F_SETFL, (tem | O_NDELAY));
223
chars_avail = read (tty, &input, 1);
225
fcntl (tty, F_SETFL, tem);
226
if (chars_avail == -1 && errno == EAGAIN)
229
#endif /* O_NDELAY */
231
/* If there's nothing available, don't waste time trying to read
233
if (chars_avail <= 0)
236
tem = ibuffer_space ();
238
if (chars_avail > tem)
241
/* One cannot read all of the available input. I can only read a single
242
character at a time, or else programs which require input can be
243
thwarted. If the buffer is larger than one character, I lose.
245
if (tem < ibuffer_len)
250
while (chars_avail--)
251
rl_stuff_char ((*rl_getc_function) (rl_instream));
256
rl_stuff_char (input);
258
#endif /* !__GO32__ */
261
/* Is there input available to be read on the readline input file
262
descriptor? Only works if the system has select(2) or FIONREAD. */
264
_rl_input_available ()
266
#if defined(HAVE_SELECT)
267
fd_set readfds, exceptfds;
268
struct timeval timeout;
270
#if defined(FIONREAD)
275
tty = fileno (rl_instream);
277
#if defined (HAVE_SELECT)
279
FD_ZERO (&exceptfds);
280
FD_SET (tty, &readfds);
281
FD_SET (tty, &exceptfds);
283
timeout.tv_usec = 100000; /* 0.1 seconds */
284
return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
287
#if defined (FIONREAD)
288
if (ioctl (tty, FIONREAD, &chars_avail) == 0)
289
return (chars_avail);
296
_rl_insert_typein (c)
303
string = xmalloc (ibuffer_len + 1);
304
string[i++] = (char) c;
306
while ((t = rl_get_char (&key)) &&
307
_rl_keymap[key].type == ISFUNC &&
308
_rl_keymap[key].function == rl_insert)
315
rl_insert_text (string);
319
/* Add KEY to the buffer of characters to be read. Returns 1 if the
320
character was stuffed correctly; 0 otherwise. */
325
if (ibuffer_space () == 0)
331
rl_pending_input = EOF;
333
ibuffer[push_index++] = key;
334
if (push_index >= ibuffer_len)
340
/* Make C be the next command to be executed. */
345
rl_pending_input = c;
349
/* **************************************************************** */
351
/* Character Input */
353
/* **************************************************************** */
355
/* Read a key, including pending input. */
361
rl_key_sequence_length++;
363
if (rl_pending_input)
365
c = rl_pending_input;
366
rl_pending_input = 0;
370
/* If input is coming from a macro, then use that. */
371
if (c = _rl_next_macro_key ())
374
/* If the user has an event function, then call it periodically. */
377
while (rl_event_hook && rl_get_char (&c) == 0)
385
if (rl_get_char (&c) == 0)
386
c = (*rl_getc_function) (rl_instream);
400
#if defined (__GO32__)
402
return (getkey () & 0x7F);
403
#endif /* __GO32__ */
407
result = read (fileno (stream), &c, sizeof (unsigned char));
409
if (result == sizeof (unsigned char))
412
/* If zero characters are returned, then the file that we are
413
reading from is empty! Return EOF in that case. */
417
#if defined (__BEOS__)
422
#if defined (EWOULDBLOCK)
423
if (errno == EWOULDBLOCK)
425
if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
427
if (flags & O_NDELAY)
430
fcntl (fileno (stream), F_SETFL, flags);
435
#endif /* EWOULDBLOCK */
437
#if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
440
if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
442
if (flags & O_NONBLOCK)
444
flags &= ~O_NONBLOCK;
445
fcntl (fileno (stream), F_SETFL, flags);
449
#endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
451
#if !defined (__GO32__)
452
/* If the error that we received was SIGINT, then try again,
453
this is simply an interrupted system call to read ().
454
Otherwise, some error ocurred, also signifying EOF. */
457
#endif /* !__GO32__ */