Imported from ../bash-2.0.tar.gz.
[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    675 Mass Ave, Cambridge, MA 02139, 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 /* 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 */
73 #endif
74
75 /* Functions imported from other files in the library. */
76 extern char *xmalloc (), *xrealloc ();
77
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;
83
84 #if defined (VI_MODE)
85 extern void _rl_vi_set_last ();
86 extern int _rl_vi_textmod_command ();
87 #endif /* VI_MODE */
88
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;
94
95 extern Keymap _rl_keymap;
96
97 extern int _rl_convert_meta_chars_to_ascii;
98
99 #if defined (__GO32__)
100 #  include <pc.h>
101 #endif /* __GO32__ */
102
103 /* Non-null means it is a pointer to a function to run while waiting for
104    character input. */
105 Function *rl_event_hook = (Function *)NULL;
106
107 Function *rl_getc_function = rl_getc;
108
109 /* **************************************************************** */
110 /*                                                                  */
111 /*                      Character Input Buffering                   */
112 /*                                                                  */
113 /* **************************************************************** */
114
115 static int pop_index, push_index;
116 static unsigned char ibuffer[512];
117 static int ibuffer_len = sizeof (ibuffer) - 1;
118
119 #define any_typein (push_index != pop_index)
120
121 int
122 _rl_any_typein ()
123 {
124   return any_typein;
125 }
126
127 /* Add KEY to the buffer of characters to be read. */
128 int
129 rl_stuff_char (key)
130      int key;
131 {
132   if (key == EOF)
133     {
134       key = NEWLINE;
135       rl_pending_input = EOF;
136     }
137   ibuffer[push_index++] = key;
138   if (push_index >= ibuffer_len)
139     push_index = 0;
140   return push_index;
141 }
142
143 /* Make C be the next command to be executed. */
144 int
145 rl_execute_next (c)
146      int c;
147 {
148   rl_pending_input = c;
149   return 0;
150 }
151
152 /* Return the amount of space available in the
153    buffer for stuffing characters. */
154 static int
155 ibuffer_space ()
156 {
157   if (pop_index > push_index)
158     return (pop_index - push_index);
159   else
160     return (ibuffer_len - (push_index - pop_index));
161 }
162
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. */
166 static int
167 rl_get_char (key)
168      int *key;
169 {
170   if (push_index == pop_index)
171     return (0);
172
173   *key = ibuffer[pop_index++];
174
175   if (pop_index >= ibuffer_len)
176     pop_index = 0;
177
178   return (1);
179 }
180
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. */
184 static int
185 rl_unget_char (key)
186      int key;
187 {
188   if (ibuffer_space ())
189     {
190       pop_index--;
191       if (pop_index < 0)
192         pop_index = ibuffer_len - 1;
193       ibuffer[pop_index] = key;
194       return (1);
195     }
196   return (0);
197 }
198
199 /* If a character is available to be read, then read it
200    and stuff it into IBUFFER.  Otherwise, just return. */
201 static void
202 rl_gather_tyi ()
203 {
204 #if defined (__GO32__)
205   char input;
206
207   if (isatty (0) && kbhit () && ibuffer_space ())
208     {
209       int i;
210       i = (*rl_getc_function) (rl_instream);
211       rl_stuff_char (i);
212     }
213 #else /* !__GO32__ */
214
215   int tty;
216   register int tem, result;
217   int chars_avail;
218   char input;
219 #if defined(HAVE_SELECT)
220   fd_set readfds, exceptfds;
221   struct timeval timeout;
222 #endif
223
224   tty = fileno (rl_instream);
225
226 #if defined (HAVE_SELECT)
227   FD_ZERO (&readfds);
228   FD_ZERO (&exceptfds);
229   FD_SET (tty, &readfds);
230   FD_SET (tty, &exceptfds);
231   timeout.tv_sec = 0;
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. */
235 #endif
236
237   result = -1;
238 #if defined (FIONREAD)
239   result = ioctl (tty, FIONREAD, &chars_avail);
240 #endif
241
242 #if defined (O_NDELAY)
243   if (result == -1)
244     {
245       tem = fcntl (tty, F_GETFL, 0);
246
247       fcntl (tty, F_SETFL, (tem | O_NDELAY));
248       chars_avail = read (tty, &input, 1);
249
250       fcntl (tty, F_SETFL, tem);
251       if (chars_avail == -1 && errno == EAGAIN)
252         return;
253     }
254 #endif /* O_NDELAY */
255
256   /* If there's nothing available, don't waste time trying to read
257      something. */
258   if (chars_avail <= 0)
259     return;
260
261   tem = ibuffer_space ();
262
263   if (chars_avail > tem)
264     chars_avail = tem;
265
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.
269      Damn! */
270   if (tem < ibuffer_len)
271     chars_avail = 0;
272
273   if (result != -1)
274     {
275       while (chars_avail--)
276         rl_stuff_char ((*rl_getc_function) (rl_instream));
277     }
278   else
279     {
280       if (chars_avail)
281         rl_stuff_char (input);
282     }
283 #endif /* !__GO32__ */
284 }
285
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. */
288 int
289 _rl_input_available ()
290 {
291 #if defined(HAVE_SELECT)
292   fd_set readfds, exceptfds;
293   struct timeval timeout;
294 #endif
295 #if defined(FIONREAD)
296   int chars_avail;
297 #endif
298   int tty;
299
300   tty = fileno (rl_instream);
301
302 #if defined (HAVE_SELECT)
303   FD_ZERO (&readfds);
304   FD_ZERO (&exceptfds);
305   FD_SET (tty, &readfds);
306   FD_SET (tty, &exceptfds);
307   timeout.tv_sec = 0;
308   timeout.tv_usec = 100000;     /* 0.1 seconds */
309   return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
310 #endif
311
312 #if defined (FIONREAD)
313   if (ioctl (tty, FIONREAD, &chars_avail) == 0)
314     return (chars_avail);
315 #endif
316
317   return 0;
318 }
319
320 void
321 _rl_insert_typein (c)
322      int c;     
323 {       
324   int key, t, i;
325   char *string;
326
327   i = key = 0;
328   string = xmalloc (ibuffer_len + 1);
329   string[i++] = (char) c;
330
331   while ((t = rl_get_char (&key)) &&
332          _rl_keymap[key].type == ISFUNC &&
333          _rl_keymap[key].function == rl_insert)
334     string[i++] = key;
335
336   if (t)
337     rl_unget_char (key);
338
339   string[i] = '\0';
340   rl_insert_text (string);
341   free (string);
342 }
343
344 /* **************************************************************** */
345 /*                                                                  */
346 /*                           Character Input                        */
347 /*                                                                  */
348 /* **************************************************************** */
349
350 /* Read a key, including pending input. */
351 int
352 rl_read_key ()
353 {
354   int c;
355
356   rl_key_sequence_length++;
357
358   if (rl_pending_input)
359     {
360       c = rl_pending_input;
361       rl_pending_input = 0;
362     }
363   else
364     {
365       /* If input is coming from a macro, then use that. */
366       if (c = _rl_next_macro_key ())
367         return (c);
368
369       /* If the user has an event function, then call it periodically. */
370       if (rl_event_hook)
371         {
372           while (rl_event_hook && rl_get_char (&c) == 0)
373             {
374               (*rl_event_hook) ();
375               rl_gather_tyi ();
376             }
377         }
378       else
379         {
380           if (rl_get_char (&c) == 0)
381             c = (*rl_getc_function) (rl_instream);
382         }
383     }
384
385   return (c);
386 }
387
388 int
389 rl_getc (stream)
390      FILE *stream;
391 {
392   int result, flags;
393   unsigned char c;
394
395 #if defined (__GO32__)
396   if (isatty (0))
397     return (getkey () & 0x7F);
398 #endif /* __GO32__ */
399
400   while (1)
401     {
402       result = read (fileno (stream), &c, sizeof (unsigned char));
403
404       if (result == sizeof (unsigned char))
405         return (c);
406
407       /* If zero characters are returned, then the file that we are
408          reading from is empty!  Return EOF in that case. */
409       if (result == 0)
410         return (EOF);
411
412 #if defined (EWOULDBLOCK)
413       if (errno == EWOULDBLOCK)
414         {
415           if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
416             return (EOF);
417           if (flags & O_NDELAY)
418             {
419               flags &= ~O_NDELAY;
420               fcntl (fileno (stream), F_SETFL, flags);
421               continue;
422             }
423           continue;
424         }
425 #endif /* EWOULDBLOCK */
426
427 #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
428       if (errno == EAGAIN)
429         {
430           if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
431             return (EOF);
432           if (flags & O_NONBLOCK)
433             {
434               flags &= ~O_NONBLOCK;
435               fcntl (fileno (stream), F_SETFL, flags);
436               continue;
437             }
438         }
439 #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
440
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. */
445       if (errno != EINTR)
446         return (EOF);
447 #endif /* !__GO32__ */
448     }
449 }