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. */
68 /* Some standard library routines. */
71 #include "rlprivate.h"
75 /* What kind of non-blocking I/O do we have? */
76 #if !defined (O_NDELAY) && defined (O_NONBLOCK)
77 # define O_NDELAY O_NONBLOCK /* Posix style */
80 /* Non-null means it is a pointer to a function to run while waiting for
82 rl_hook_func_t
*rl_event_hook
= (rl_hook_func_t
*)NULL
;
84 rl_getc_func_t
*rl_getc_function
= rl_getc
;
86 static int _keyboard_input_timeout
= 100000; /* 0.1 seconds; it's in usec */
88 static int ibuffer_space
PARAMS((void));
89 static int rl_get_char
PARAMS((int *));
90 static int 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 and stuff it into
158 IBUFFER. Otherwise, just return. Returns number of characters read
159 (0 if none available) and -1 on error (EIO). */
164 register int tem
, result
;
167 #if defined(HAVE_SELECT)
168 fd_set readfds
, exceptfds
;
169 struct timeval timeout
;
172 tty
= fileno (rl_instream
);
174 #if defined (HAVE_SELECT)
176 FD_ZERO (&exceptfds
);
177 FD_SET (tty
, &readfds
);
178 FD_SET (tty
, &exceptfds
);
180 timeout
.tv_usec
= _keyboard_input_timeout
;
181 result
= select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
);
183 return 0; /* Nothing to read. */
187 #if defined (FIONREAD)
189 result
= ioctl (tty
, FIONREAD
, &chars_avail
);
190 if (result
== -1 && errno
== EIO
)
194 #if defined (O_NDELAY)
197 tem
= fcntl (tty
, F_GETFL
, 0);
199 fcntl (tty
, F_SETFL
, (tem
| O_NDELAY
));
200 chars_avail
= read (tty
, &input
, 1);
202 fcntl (tty
, F_SETFL
, tem
);
203 if (chars_avail
== -1 && errno
== EAGAIN
)
206 #endif /* O_NDELAY */
208 /* If there's nothing available, don't waste time trying to read
210 if (chars_avail
<= 0)
213 tem
= ibuffer_space ();
215 if (chars_avail
> tem
)
218 /* One cannot read all of the available input. I can only read a single
219 character at a time, or else programs which require input can be
220 thwarted. If the buffer is larger than one character, I lose.
222 if (tem
< ibuffer_len
)
227 while (chars_avail
--)
228 rl_stuff_char ((*rl_getc_function
) (rl_instream
));
233 rl_stuff_char (input
);
240 rl_set_keyboard_input_timeout (u
)
245 o
= _keyboard_input_timeout
;
247 _keyboard_input_timeout
= u
;
251 /* Is there input available to be read on the readline input file
252 descriptor? Only works if the system has select(2) or FIONREAD.
253 Uses the value of _keyboard_input_timeout as the timeout; if another
254 readline function wants to specify a timeout and not leave it up to
255 the user, it should use _rl_input_queued(timeout_value_in_microseconds)
258 _rl_input_available ()
260 #if defined(HAVE_SELECT)
261 fd_set readfds
, exceptfds
;
262 struct timeval timeout
;
264 #if !defined (HAVE_SELECT) && defined(FIONREAD)
269 tty
= fileno (rl_instream
);
271 #if defined (HAVE_SELECT)
273 FD_ZERO (&exceptfds
);
274 FD_SET (tty
, &readfds
);
275 FD_SET (tty
, &exceptfds
);
277 timeout
.tv_usec
= _keyboard_input_timeout
;
278 return (select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
) > 0);
281 #if defined (FIONREAD)
282 if (ioctl (tty
, FIONREAD
, &chars_avail
) == 0)
283 return (chars_avail
);
297 old_timeout
= rl_set_keyboard_input_timeout (t
);
298 r
= _rl_input_available ();
299 rl_set_keyboard_input_timeout (old_timeout
);
304 _rl_insert_typein (c
)
311 string
= (char *)xmalloc (ibuffer_len
+ 1);
312 string
[i
++] = (char) c
;
314 while ((t
= rl_get_char (&key
)) &&
315 _rl_keymap
[key
].type
== ISFUNC
&&
316 _rl_keymap
[key
].function
== rl_insert
)
320 _rl_unget_char (key
);
323 rl_insert_text (string
);
327 /* Add KEY to the buffer of characters to be read. Returns 1 if the
328 character was stuffed correctly; 0 otherwise. */
333 if (ibuffer_space () == 0)
339 rl_pending_input
= EOF
;
340 RL_SETSTATE (RL_STATE_INPUTPENDING
);
342 ibuffer
[push_index
++] = key
;
343 if (push_index
>= ibuffer_len
)
349 /* Make C be the next command to be executed. */
354 rl_pending_input
= c
;
355 RL_SETSTATE (RL_STATE_INPUTPENDING
);
359 /* Clear any pending input pushed with rl_execute_next() */
361 rl_clear_pending_input ()
363 rl_pending_input
= 0;
364 RL_UNSETSTATE (RL_STATE_INPUTPENDING
);
368 /* **************************************************************** */
370 /* Character Input */
372 /* **************************************************************** */
374 /* Read a key, including pending input. */
380 rl_key_sequence_length
++;
382 if (rl_pending_input
)
384 c
= rl_pending_input
;
385 rl_clear_pending_input ();
389 /* If input is coming from a macro, then use that. */
390 if (c
= _rl_next_macro_key ())
393 /* If the user has an event function, then call it periodically. */
396 while (rl_event_hook
&& rl_get_char (&c
) == 0)
399 if (rl_done
) /* XXX - experimental */
401 if (rl_gather_tyi () < 0) /* XXX - EIO */
410 if (rl_get_char (&c
) == 0)
411 c
= (*rl_getc_function
) (rl_instream
);
427 result
= read (fileno (stream
), &c
, sizeof (unsigned char));
429 if (result
== sizeof (unsigned char))
432 /* If zero characters are returned, then the file that we are
433 reading from is empty! Return EOF in that case. */
437 #if defined (__BEOS__)
442 #if defined (EWOULDBLOCK)
443 # define X_EWOULDBLOCK EWOULDBLOCK
445 # define X_EWOULDBLOCK -99
449 # define X_EAGAIN EAGAIN
451 # define X_EAGAIN -99
454 if (errno
== X_EWOULDBLOCK
|| errno
== X_EAGAIN
)
456 if (sh_unset_nodelay_mode (fileno (stream
)) < 0)
464 /* If the error that we received was SIGINT, then try again,
465 this is simply an interrupted system call to read ().
466 Otherwise, some error ocurred, also signifying EOF. */
472 #if defined (HANDLE_MULTIBYTE)
473 /* read multibyte char */
475 _rl_read_mbchar (mbchar
, size
)
480 size_t mbchar_bytes_length
;
482 mbstate_t ps
, ps_back
;
484 memset(&ps
, 0, sizeof (mbstate_t));
485 memset(&ps_back
, 0, sizeof (mbstate_t));
487 while (mb_len
< size
)
489 RL_SETSTATE(RL_STATE_MOREINPUT
);
490 mbchar
[mb_len
++] = rl_read_key ();
491 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
493 mbchar_bytes_length
= mbrtowc (&wc
, mbchar
, mb_len
, &ps
);
494 if (mbchar_bytes_length
== (size_t)(-1))
495 break; /* invalid byte sequence for the current locale */
496 else if (mbchar_bytes_length
== (size_t)(-2))
502 else if (mbchar_bytes_length
> (size_t)(0))
509 /* Read a multibyte-character string whose first character is FIRST into
510 the buffer MB of length MBLEN. Returns the last character read, which
511 may be FIRST. Used by the search functions, among others. Very similar
512 to _rl_read_mbchar. */
514 _rl_read_mbstring (first
, mb
, mblen
)
523 memset (mb
, 0, mblen
);
524 for (i
= 0; i
< mblen
; i
++)
527 memset (&ps
, 0, sizeof (mbstate_t));
528 if (_rl_get_char_len (mb
, &ps
) == -2)
530 /* Read more for multibyte character */
531 RL_SETSTATE (RL_STATE_MOREINPUT
);
533 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
540 #endif /* HANDLE_MULTIBYTE */
This page took 0.040811 seconds and 4 git commands to generate.