e34558bf8586aaa21f8dfedf78f8b3d53eaf1d49
[platform/upstream/bash.git] / lib / readline / input.c
1 /* input.c -- character input functions for readline. */
2
3 /* Copyright (C) 1994 Free Software Foundation, Inc.
4
5    This file is part of the GNU Readline Library, a library for
6    reading lines of text with interactive input and history editing.
7
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.
12
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.
17
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
23
24 #if defined (HAVE_CONFIG_H)
25 #  include <config.h>
26 #endif
27
28 #include <sys/types.h>
29 #include <fcntl.h>
30 #if defined (HAVE_SYS_FILE_H)
31 #  include <sys/file.h>
32 #endif /* HAVE_SYS_FILE_H */
33
34 #if defined (HAVE_UNISTD_H)
35 #  include <unistd.h>
36 #endif /* HAVE_UNISTD_H */
37
38 #if defined (HAVE_STDLIB_H)
39 #  include <stdlib.h>
40 #else
41 #  include "ansi_stdlib.h"
42 #endif /* HAVE_STDLIB_H */
43
44 #if defined (HAVE_SELECT)
45 #  if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX)
46 #    include <sys/time.h>
47 #  endif
48 #endif /* HAVE_SELECT */
49 #if defined (HAVE_SYS_SELECT_H)
50 #  include <sys/select.h>
51 #endif
52
53 #if defined (FIONREAD_IN_SYS_IOCTL)
54 #  include <sys/ioctl.h>
55 #endif
56
57 #include <stdio.h>
58 #include <errno.h>
59
60 #if !defined (errno)
61 extern int errno;
62 #endif /* !errno */
63
64 /* System-specific feature definitions and include files. */
65 #include "rldefs.h"
66
67 /* Some standard library routines. */
68 #include "readline.h"
69
70 #include "rlprivate.h"
71 #include "rlshell.h"
72 #include "xmalloc.h"
73
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 */
77 #endif
78
79 /* Non-null means it is a pointer to a function to run while waiting for
80    character input. */
81 rl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL;
82
83 rl_getc_func_t *rl_getc_function = rl_getc;
84
85 static int _keyboard_input_timeout = 100000;            /* 0.1 seconds; it's in usec */
86
87 static int ibuffer_space PARAMS((void));
88 static int rl_get_char PARAMS((int *));
89 static int rl_unget_char PARAMS((int));
90 static void rl_gather_tyi PARAMS((void));
91
92 /* **************************************************************** */
93 /*                                                                  */
94 /*                      Character Input Buffering                   */
95 /*                                                                  */
96 /* **************************************************************** */
97
98 static int pop_index, push_index;
99 static unsigned char ibuffer[512];
100 static int ibuffer_len = sizeof (ibuffer) - 1;
101
102 #define any_typein (push_index != pop_index)
103
104 int
105 _rl_any_typein ()
106 {
107   return any_typein;
108 }
109
110 /* Return the amount of space available in the buffer for stuffing
111    characters. */
112 static int
113 ibuffer_space ()
114 {
115   if (pop_index > push_index)
116     return (pop_index - push_index - 1);
117   else
118     return (ibuffer_len - (push_index - pop_index));
119 }
120
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. */
124 static int
125 rl_get_char (key)
126      int *key;
127 {
128   if (push_index == pop_index)
129     return (0);
130
131   *key = ibuffer[pop_index++];
132
133   if (pop_index >= ibuffer_len)
134     pop_index = 0;
135
136   return (1);
137 }
138
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. */
142 static int
143 rl_unget_char (key)
144      int key;
145 {
146   if (ibuffer_space ())
147     {
148       pop_index--;
149       if (pop_index < 0)
150         pop_index = ibuffer_len - 1;
151       ibuffer[pop_index] = key;
152       return (1);
153     }
154   return (0);
155 }
156
157 /* If a character is available to be read, then read it
158    and stuff it into IBUFFER.  Otherwise, just return. */
159 static void
160 rl_gather_tyi ()
161 {
162   int tty;
163   register int tem, result;
164   int chars_avail;
165   char input;
166 #if defined(HAVE_SELECT)
167   fd_set readfds, exceptfds;
168   struct timeval timeout;
169 #endif
170
171   tty = fileno (rl_instream);
172
173 #if defined (HAVE_SELECT)
174   FD_ZERO (&readfds);
175   FD_ZERO (&exceptfds);
176   FD_SET (tty, &readfds);
177   FD_SET (tty, &exceptfds);
178   timeout.tv_sec = 0;
179   timeout.tv_usec = _keyboard_input_timeout;
180   if (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) <= 0)
181     return;     /* Nothing to read. */
182 #endif
183
184   result = -1;
185 #if defined (FIONREAD)
186   result = ioctl (tty, FIONREAD, &chars_avail);
187 #endif
188
189 #if defined (O_NDELAY)
190   if (result == -1)
191     {
192       tem = fcntl (tty, F_GETFL, 0);
193
194       fcntl (tty, F_SETFL, (tem | O_NDELAY));
195       chars_avail = read (tty, &input, 1);
196
197       fcntl (tty, F_SETFL, tem);
198       if (chars_avail == -1 && errno == EAGAIN)
199         return;
200     }
201 #endif /* O_NDELAY */
202
203   /* If there's nothing available, don't waste time trying to read
204      something. */
205   if (chars_avail <= 0)
206     return;
207
208   tem = ibuffer_space ();
209
210   if (chars_avail > tem)
211     chars_avail = tem;
212
213   /* One cannot read all of the available input.  I can only read a single
214      character at a time, or else programs which require input can be
215      thwarted.  If the buffer is larger than one character, I lose.
216      Damn! */
217   if (tem < ibuffer_len)
218     chars_avail = 0;
219
220   if (result != -1)
221     {
222       while (chars_avail--)
223         rl_stuff_char ((*rl_getc_function) (rl_instream));
224     }
225   else
226     {
227       if (chars_avail)
228         rl_stuff_char (input);
229     }
230 }
231
232 int
233 rl_set_keyboard_input_timeout (u)
234      int u;
235 {
236   int o;
237
238   o = _keyboard_input_timeout;
239   if (u > 0)
240     _keyboard_input_timeout = u;
241   return (o);
242 }
243
244 /* Is there input available to be read on the readline input file
245    descriptor?  Only works if the system has select(2) or FIONREAD. */
246 int
247 _rl_input_available ()
248 {
249 #if defined(HAVE_SELECT)
250   fd_set readfds, exceptfds;
251   struct timeval timeout;
252 #endif
253 #if !defined (HAVE_SELECT) && defined(FIONREAD)
254   int chars_avail;
255 #endif
256   int tty;
257
258   tty = fileno (rl_instream);
259
260 #if defined (HAVE_SELECT)
261   FD_ZERO (&readfds);
262   FD_ZERO (&exceptfds);
263   FD_SET (tty, &readfds);
264   FD_SET (tty, &exceptfds);
265   timeout.tv_sec = 0;
266   timeout.tv_usec = _keyboard_input_timeout;
267   return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
268 #else
269
270 #if defined (FIONREAD)
271   if (ioctl (tty, FIONREAD, &chars_avail) == 0)
272     return (chars_avail);
273 #endif
274
275 #endif
276
277   return 0;
278 }
279
280 void
281 _rl_insert_typein (c)
282      int c;     
283 {       
284   int key, t, i;
285   char *string;
286
287   i = key = 0;
288   string = (char *)xmalloc (ibuffer_len + 1);
289   string[i++] = (char) c;
290
291   while ((t = rl_get_char (&key)) &&
292          _rl_keymap[key].type == ISFUNC &&
293          _rl_keymap[key].function == rl_insert)
294     string[i++] = key;
295
296   if (t)
297     rl_unget_char (key);
298
299   string[i] = '\0';
300   rl_insert_text (string);
301   free (string);
302 }
303
304 /* Add KEY to the buffer of characters to be read.  Returns 1 if the
305    character was stuffed correctly; 0 otherwise. */
306 int
307 rl_stuff_char (key)
308      int key;
309 {
310   if (ibuffer_space () == 0)
311     return 0;
312
313   if (key == EOF)
314     {
315       key = NEWLINE;
316       rl_pending_input = EOF;
317       RL_SETSTATE (RL_STATE_INPUTPENDING);
318     }
319   ibuffer[push_index++] = key;
320   if (push_index >= ibuffer_len)
321     push_index = 0;
322
323   return 1;
324 }
325
326 /* Make C be the next command to be executed. */
327 int
328 rl_execute_next (c)
329      int c;
330 {
331   rl_pending_input = c;
332   RL_SETSTATE (RL_STATE_INPUTPENDING);
333   return 0;
334 }
335
336 /* Clear any pending input pushed with rl_execute_next() */
337 int
338 rl_clear_pending_input ()
339 {
340   rl_pending_input = 0;
341   RL_UNSETSTATE (RL_STATE_INPUTPENDING);
342   return 0;
343 }
344
345 /* **************************************************************** */
346 /*                                                                  */
347 /*                           Character Input                        */
348 /*                                                                  */
349 /* **************************************************************** */
350
351 /* Read a key, including pending input. */
352 int
353 rl_read_key ()
354 {
355   int c;
356
357   rl_key_sequence_length++;
358
359   if (rl_pending_input)
360     {
361       c = rl_pending_input;
362       rl_clear_pending_input ();
363     }
364   else
365     {
366       /* If input is coming from a macro, then use that. */
367       if (c = _rl_next_macro_key ())
368         return (c);
369
370       /* If the user has an event function, then call it periodically. */
371       if (rl_event_hook)
372         {
373           while (rl_event_hook && rl_get_char (&c) == 0)
374             {
375               (*rl_event_hook) ();
376               if (rl_done)              /* XXX - experimental */
377                 return ('\n');
378               rl_gather_tyi ();
379             }
380         }
381       else
382         {
383           if (rl_get_char (&c) == 0)
384             c = (*rl_getc_function) (rl_instream);
385         }
386     }
387
388   return (c);
389 }
390
391 int
392 rl_getc (stream)
393      FILE *stream;
394 {
395   int result;
396   unsigned char c;
397
398   while (1)
399     {
400       result = read (fileno (stream), &c, sizeof (unsigned char));
401
402       if (result == sizeof (unsigned char))
403         return (c);
404
405       /* If zero characters are returned, then the file that we are
406          reading from is empty!  Return EOF in that case. */
407       if (result == 0)
408         return (EOF);
409
410 #if defined (__BEOS__)
411       if (errno == EINTR)
412         continue;
413 #endif
414
415 #if defined (EWOULDBLOCK)
416 #  define X_EWOULDBLOCK EWOULDBLOCK
417 #else
418 #  define X_EWOULDBLOCK -99
419 #endif
420
421 #if defined (EAGAIN)
422 #  define X_EAGAIN EAGAIN
423 #else
424 #  define X_EAGAIN -99
425 #endif
426
427       if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
428         {
429           if (sh_unset_nodelay_mode (fileno (stream)) < 0)
430             return (EOF);
431           continue;
432         }
433
434 #undef X_EWOULDBLOCK
435 #undef X_EAGAIN
436
437       /* If the error that we received was SIGINT, then try again,
438          this is simply an interrupted system call to read ().
439          Otherwise, some error ocurred, also signifying EOF. */
440       if (errno != EINTR)
441         return (EOF);
442     }
443 }