121308Sache/* input.c -- character input functions for readline. */ 221308Sache 3157184Sache/* Copyright (C) 1994-2005 Free Software Foundation, Inc. 421308Sache 521308Sache This file is part of the GNU Readline Library, a library for 621308Sache reading lines of text with interactive input and history editing. 721308Sache 821308Sache The GNU Readline Library is free software; you can redistribute it 921308Sache and/or modify it under the terms of the GNU General Public License 1021308Sache as published by the Free Software Foundation; either version 2, or 1121308Sache (at your option) any later version. 1221308Sache 1321308Sache The GNU Readline Library is distributed in the hope that it will be 1421308Sache useful, but WITHOUT ANY WARRANTY; without even the implied warranty 1521308Sache of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1621308Sache GNU General Public License for more details. 1721308Sache 1821308Sache The GNU General Public License is often shipped with GNU software, and 1921308Sache is generally kept in a file called COPYING or LICENSE. If you do not 2021308Sache have a copy of the license, write to the Free Software Foundation, 2158310Sache 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 2221308Sache#define READLINE_LIBRARY 2321308Sache 24136644Sache#if defined (__TANDEM) 25136644Sache# include <floss.h> 26136644Sache#endif 27136644Sache 2821308Sache#if defined (HAVE_CONFIG_H) 2921308Sache# include <config.h> 3021308Sache#endif 3121308Sache 3221308Sache#include <sys/types.h> 3321308Sache#include <fcntl.h> 3421308Sache#if defined (HAVE_SYS_FILE_H) 3521308Sache# include <sys/file.h> 3621308Sache#endif /* HAVE_SYS_FILE_H */ 3721308Sache 3821308Sache#if defined (HAVE_UNISTD_H) 3921308Sache# include <unistd.h> 4021308Sache#endif /* HAVE_UNISTD_H */ 4121308Sache 4221308Sache#if defined (HAVE_STDLIB_H) 4321308Sache# include <stdlib.h> 4421308Sache#else 4521308Sache# include "ansi_stdlib.h" 4621308Sache#endif /* HAVE_STDLIB_H */ 4721308Sache 4821308Sache#if defined (HAVE_SELECT) 4921308Sache# if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX) 5021308Sache# include <sys/time.h> 5121308Sache# endif 5221308Sache#endif /* HAVE_SELECT */ 5321308Sache#if defined (HAVE_SYS_SELECT_H) 5421308Sache# include <sys/select.h> 5521308Sache#endif 5621308Sache 5721308Sache#if defined (FIONREAD_IN_SYS_IOCTL) 5821308Sache# include <sys/ioctl.h> 5921308Sache#endif 6021308Sache 6121308Sache#include <stdio.h> 6221308Sache#include <errno.h> 6321308Sache 6421308Sache#if !defined (errno) 6521308Sacheextern int errno; 6621308Sache#endif /* !errno */ 6721308Sache 6821308Sache/* System-specific feature definitions and include files. */ 6921308Sache#include "rldefs.h" 70119610Sache#include "rlmbutil.h" 7121308Sache 7221308Sache/* Some standard library routines. */ 7321308Sache#include "readline.h" 7421308Sache 7558310Sache#include "rlprivate.h" 7658310Sache#include "rlshell.h" 7758310Sache#include "xmalloc.h" 7858310Sache 7921308Sache/* What kind of non-blocking I/O do we have? */ 8021308Sache#if !defined (O_NDELAY) && defined (O_NONBLOCK) 8121308Sache# define O_NDELAY O_NONBLOCK /* Posix style */ 8221308Sache#endif 8321308Sache 8421308Sache/* Non-null means it is a pointer to a function to run while waiting for 8521308Sache character input. */ 8675406Sacherl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL; 8721308Sache 8875406Sacherl_getc_func_t *rl_getc_function = rl_getc; 8921308Sache 9075406Sachestatic int _keyboard_input_timeout = 100000; /* 0.1 seconds; it's in usec */ 9175406Sache 92119610Sachestatic int ibuffer_space PARAMS((void)); 93119610Sachestatic int rl_get_char PARAMS((int *)); 94119610Sachestatic int rl_gather_tyi PARAMS((void)); 95119610Sache 9621308Sache/* **************************************************************** */ 9721308Sache/* */ 9821308Sache/* Character Input Buffering */ 9921308Sache/* */ 10021308Sache/* **************************************************************** */ 10121308Sache 10221308Sachestatic int pop_index, push_index; 10321308Sachestatic unsigned char ibuffer[512]; 10421308Sachestatic int ibuffer_len = sizeof (ibuffer) - 1; 10521308Sache 10621308Sache#define any_typein (push_index != pop_index) 10721308Sache 10821308Sacheint 10921308Sache_rl_any_typein () 11021308Sache{ 11121308Sache return any_typein; 11221308Sache} 11321308Sache 11447558Sache/* Return the amount of space available in the buffer for stuffing 11547558Sache characters. */ 11621308Sachestatic int 11721308Sacheibuffer_space () 11821308Sache{ 11921308Sache if (pop_index > push_index) 12047558Sache return (pop_index - push_index - 1); 12121308Sache else 12221308Sache return (ibuffer_len - (push_index - pop_index)); 12321308Sache} 12421308Sache 12521308Sache/* Get a key from the buffer of characters to be read. 12621308Sache Return the key in KEY. 12721308Sache Result is KEY if there was a key, or 0 if there wasn't. */ 12821308Sachestatic int 12921308Sacherl_get_char (key) 13021308Sache int *key; 13121308Sache{ 13221308Sache if (push_index == pop_index) 13321308Sache return (0); 13421308Sache 13521308Sache *key = ibuffer[pop_index++]; 136173403Sache#if 0 13721308Sache if (pop_index >= ibuffer_len) 138173403Sache#else 139173403Sache if (pop_index > ibuffer_len) 140173403Sache#endif 14121308Sache pop_index = 0; 14221308Sache 14321308Sache return (1); 14421308Sache} 14521308Sache 14621308Sache/* Stuff KEY into the *front* of the input buffer. 14721308Sache Returns non-zero if successful, zero if there is 14821308Sache no space left in the buffer. */ 149119610Sacheint 150119610Sache_rl_unget_char (key) 15121308Sache int key; 15221308Sache{ 15321308Sache if (ibuffer_space ()) 15421308Sache { 15521308Sache pop_index--; 15621308Sache if (pop_index < 0) 15721308Sache pop_index = ibuffer_len - 1; 15821308Sache ibuffer[pop_index] = key; 15921308Sache return (1); 16021308Sache } 16121308Sache return (0); 16221308Sache} 16321308Sache 164136644Sacheint 165136644Sache_rl_pushed_input_available () 166136644Sache{ 167136644Sache return (push_index != pop_index); 168136644Sache} 169136644Sache 170119610Sache/* If a character is available to be read, then read it and stuff it into 171119610Sache IBUFFER. Otherwise, just return. Returns number of characters read 172119610Sache (0 if none available) and -1 on error (EIO). */ 173119610Sachestatic int 17421308Sacherl_gather_tyi () 17521308Sache{ 17621308Sache int tty; 17721308Sache register int tem, result; 178136644Sache int chars_avail, k; 17921308Sache char input; 18021308Sache#if defined(HAVE_SELECT) 18121308Sache fd_set readfds, exceptfds; 18221308Sache struct timeval timeout; 18321308Sache#endif 18421308Sache 185165670Sache chars_avail = 0; 18621308Sache tty = fileno (rl_instream); 18721308Sache 18821308Sache#if defined (HAVE_SELECT) 18921308Sache FD_ZERO (&readfds); 19021308Sache FD_ZERO (&exceptfds); 19121308Sache FD_SET (tty, &readfds); 19221308Sache FD_SET (tty, &exceptfds); 19321308Sache timeout.tv_sec = 0; 19475406Sache timeout.tv_usec = _keyboard_input_timeout; 195119610Sache result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout); 196119610Sache if (result <= 0) 197119610Sache return 0; /* Nothing to read. */ 19821308Sache#endif 19921308Sache 20021308Sache result = -1; 20121308Sache#if defined (FIONREAD) 202119610Sache errno = 0; 20321308Sache result = ioctl (tty, FIONREAD, &chars_avail); 204119610Sache if (result == -1 && errno == EIO) 205119610Sache return -1; 20621308Sache#endif 20721308Sache 20821308Sache#if defined (O_NDELAY) 20921308Sache if (result == -1) 21021308Sache { 21121308Sache tem = fcntl (tty, F_GETFL, 0); 21221308Sache 21321308Sache fcntl (tty, F_SETFL, (tem | O_NDELAY)); 21421308Sache chars_avail = read (tty, &input, 1); 21521308Sache 21621308Sache fcntl (tty, F_SETFL, tem); 21721308Sache if (chars_avail == -1 && errno == EAGAIN) 218119610Sache return 0; 219136644Sache if (chars_avail == 0) /* EOF */ 220136644Sache { 221136644Sache rl_stuff_char (EOF); 222136644Sache return (0); 223136644Sache } 22421308Sache } 22521308Sache#endif /* O_NDELAY */ 22621308Sache 227165670Sache#if defined (__MINGW32__) 228165670Sache /* Use getch/_kbhit to check for available console input, in the same way 229165670Sache that we read it normally. */ 230165670Sache chars_avail = isatty (tty) ? _kbhit () : 0; 231165670Sache result = 0; 232165670Sache#endif 233165670Sache 23421308Sache /* If there's nothing available, don't waste time trying to read 23521308Sache something. */ 23621308Sache if (chars_avail <= 0) 237119610Sache return 0; 23821308Sache 23921308Sache tem = ibuffer_space (); 24021308Sache 24121308Sache if (chars_avail > tem) 24221308Sache chars_avail = tem; 24321308Sache 24421308Sache /* One cannot read all of the available input. I can only read a single 24521308Sache character at a time, or else programs which require input can be 24621308Sache thwarted. If the buffer is larger than one character, I lose. 24721308Sache Damn! */ 24821308Sache if (tem < ibuffer_len) 24921308Sache chars_avail = 0; 25021308Sache 25121308Sache if (result != -1) 25221308Sache { 25321308Sache while (chars_avail--) 254136644Sache { 255136644Sache k = (*rl_getc_function) (rl_instream); 256173403Sache if (rl_stuff_char (k) == 0) 257173403Sache break; /* some problem; no more room */ 258136644Sache if (k == NEWLINE || k == RETURN) 259136644Sache break; 260136644Sache } 26121308Sache } 26221308Sache else 26321308Sache { 26421308Sache if (chars_avail) 26521308Sache rl_stuff_char (input); 26621308Sache } 267119610Sache 268119610Sache return 1; 26921308Sache} 27021308Sache 27175406Sacheint 27275406Sacherl_set_keyboard_input_timeout (u) 27375406Sache int u; 27475406Sache{ 27575406Sache int o; 27675406Sache 27775406Sache o = _keyboard_input_timeout; 278165670Sache if (u >= 0) 27975406Sache _keyboard_input_timeout = u; 28075406Sache return (o); 28175406Sache} 28275406Sache 28321308Sache/* Is there input available to be read on the readline input file 284119610Sache descriptor? Only works if the system has select(2) or FIONREAD. 285119610Sache Uses the value of _keyboard_input_timeout as the timeout; if another 286119610Sache readline function wants to specify a timeout and not leave it up to 287119610Sache the user, it should use _rl_input_queued(timeout_value_in_microseconds) 288119610Sache instead. */ 28921308Sacheint 29021308Sache_rl_input_available () 29121308Sache{ 29221308Sache#if defined(HAVE_SELECT) 29321308Sache fd_set readfds, exceptfds; 29421308Sache struct timeval timeout; 29521308Sache#endif 296119610Sache#if !defined (HAVE_SELECT) && defined(FIONREAD) 29721308Sache int chars_avail; 29821308Sache#endif 29921308Sache int tty; 30021308Sache 30121308Sache tty = fileno (rl_instream); 30221308Sache 30321308Sache#if defined (HAVE_SELECT) 30421308Sache FD_ZERO (&readfds); 30521308Sache FD_ZERO (&exceptfds); 30621308Sache FD_SET (tty, &readfds); 30721308Sache FD_SET (tty, &exceptfds); 30821308Sache timeout.tv_sec = 0; 30975406Sache timeout.tv_usec = _keyboard_input_timeout; 31021308Sache return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0); 311119610Sache#else 31221308Sache 31321308Sache#if defined (FIONREAD) 31421308Sache if (ioctl (tty, FIONREAD, &chars_avail) == 0) 31521308Sache return (chars_avail); 31621308Sache#endif 31721308Sache 318119610Sache#endif 319119610Sache 320165670Sache#if defined (__MINGW32__) 321165670Sache if (isatty (tty)) 322165670Sache return (_kbhit ()); 323165670Sache#endif 324165670Sache 32521308Sache return 0; 32621308Sache} 32721308Sache 328119610Sacheint 329119610Sache_rl_input_queued (t) 330119610Sache int t; 331119610Sache{ 332119610Sache int old_timeout, r; 333119610Sache 334119610Sache old_timeout = rl_set_keyboard_input_timeout (t); 335119610Sache r = _rl_input_available (); 336119610Sache rl_set_keyboard_input_timeout (old_timeout); 337119610Sache return r; 338119610Sache} 339119610Sache 34021308Sachevoid 34121308Sache_rl_insert_typein (c) 34221308Sache int c; 34321308Sache{ 34421308Sache int key, t, i; 34521308Sache char *string; 34621308Sache 34721308Sache i = key = 0; 348119610Sache string = (char *)xmalloc (ibuffer_len + 1); 34921308Sache string[i++] = (char) c; 35021308Sache 35121308Sache while ((t = rl_get_char (&key)) && 35221308Sache _rl_keymap[key].type == ISFUNC && 35321308Sache _rl_keymap[key].function == rl_insert) 35421308Sache string[i++] = key; 35521308Sache 35621308Sache if (t) 357119610Sache _rl_unget_char (key); 35821308Sache 35921308Sache string[i] = '\0'; 36021308Sache rl_insert_text (string); 36121308Sache free (string); 36221308Sache} 36321308Sache 36447558Sache/* Add KEY to the buffer of characters to be read. Returns 1 if the 36547558Sache character was stuffed correctly; 0 otherwise. */ 36647558Sacheint 36747558Sacherl_stuff_char (key) 36847558Sache int key; 36947558Sache{ 37047558Sache if (ibuffer_space () == 0) 37147558Sache return 0; 37247558Sache 37347558Sache if (key == EOF) 37447558Sache { 37547558Sache key = NEWLINE; 37647558Sache rl_pending_input = EOF; 37775406Sache RL_SETSTATE (RL_STATE_INPUTPENDING); 37847558Sache } 37947558Sache ibuffer[push_index++] = key; 380173403Sache#if 0 38147558Sache if (push_index >= ibuffer_len) 382173403Sache#else 383173403Sache if (push_index > ibuffer_len) 384173403Sache#endif 38547558Sache push_index = 0; 38647558Sache 38747558Sache return 1; 38847558Sache} 38947558Sache 39047558Sache/* Make C be the next command to be executed. */ 39147558Sacheint 39247558Sacherl_execute_next (c) 39347558Sache int c; 39447558Sache{ 39547558Sache rl_pending_input = c; 39675406Sache RL_SETSTATE (RL_STATE_INPUTPENDING); 39747558Sache return 0; 39847558Sache} 39947558Sache 40075406Sache/* Clear any pending input pushed with rl_execute_next() */ 40175406Sacheint 40275406Sacherl_clear_pending_input () 40375406Sache{ 40475406Sache rl_pending_input = 0; 40575406Sache RL_UNSETSTATE (RL_STATE_INPUTPENDING); 40675406Sache return 0; 40775406Sache} 40875406Sache 40921308Sache/* **************************************************************** */ 41021308Sache/* */ 41121308Sache/* Character Input */ 41221308Sache/* */ 41321308Sache/* **************************************************************** */ 41421308Sache 41521308Sache/* Read a key, including pending input. */ 41621308Sacheint 41721308Sacherl_read_key () 41821308Sache{ 41921308Sache int c; 42021308Sache 42121308Sache rl_key_sequence_length++; 42221308Sache 42321308Sache if (rl_pending_input) 42421308Sache { 42521308Sache c = rl_pending_input; 42675406Sache rl_clear_pending_input (); 42721308Sache } 42821308Sache else 42921308Sache { 43021308Sache /* If input is coming from a macro, then use that. */ 43121308Sache if (c = _rl_next_macro_key ()) 43221308Sache return (c); 43321308Sache 43421308Sache /* If the user has an event function, then call it periodically. */ 43521308Sache if (rl_event_hook) 43621308Sache { 43721308Sache while (rl_event_hook && rl_get_char (&c) == 0) 43821308Sache { 43921308Sache (*rl_event_hook) (); 44075406Sache if (rl_done) /* XXX - experimental */ 44175406Sache return ('\n'); 442119610Sache if (rl_gather_tyi () < 0) /* XXX - EIO */ 443119610Sache { 444119610Sache rl_done = 1; 445119610Sache return ('\n'); 446119610Sache } 44721308Sache } 44821308Sache } 44921308Sache else 45021308Sache { 45121308Sache if (rl_get_char (&c) == 0) 45221308Sache c = (*rl_getc_function) (rl_instream); 45321308Sache } 45421308Sache } 45521308Sache 45621308Sache return (c); 45721308Sache} 45821308Sache 45921308Sacheint 46021308Sacherl_getc (stream) 46121308Sache FILE *stream; 46221308Sache{ 46358310Sache int result; 46421308Sache unsigned char c; 46521308Sache 46621308Sache while (1) 46721308Sache { 468157184Sache#if defined (__MINGW32__) 469157184Sache if (isatty (fileno (stream))) 470157184Sache return (getch ()); 471157184Sache#endif 47221308Sache result = read (fileno (stream), &c, sizeof (unsigned char)); 47321308Sache 47421308Sache if (result == sizeof (unsigned char)) 47521308Sache return (c); 47621308Sache 47721308Sache /* If zero characters are returned, then the file that we are 47821308Sache reading from is empty! Return EOF in that case. */ 47921308Sache if (result == 0) 48021308Sache return (EOF); 48121308Sache 48247558Sache#if defined (__BEOS__) 48347558Sache if (errno == EINTR) 48447558Sache continue; 48547558Sache#endif 48647558Sache 48721308Sache#if defined (EWOULDBLOCK) 48858310Sache# define X_EWOULDBLOCK EWOULDBLOCK 48958310Sache#else 49058310Sache# define X_EWOULDBLOCK -99 49158310Sache#endif 49258310Sache 49358310Sache#if defined (EAGAIN) 49458310Sache# define X_EAGAIN EAGAIN 49558310Sache#else 49658310Sache# define X_EAGAIN -99 49758310Sache#endif 49858310Sache 49958310Sache if (errno == X_EWOULDBLOCK || errno == X_EAGAIN) 50021308Sache { 50175406Sache if (sh_unset_nodelay_mode (fileno (stream)) < 0) 50221308Sache return (EOF); 50321308Sache continue; 50421308Sache } 50521308Sache 50658310Sache#undef X_EWOULDBLOCK 50758310Sache#undef X_EAGAIN 50821308Sache 50921308Sache /* If the error that we received was SIGINT, then try again, 51021308Sache this is simply an interrupted system call to read (). 51121308Sache Otherwise, some error ocurred, also signifying EOF. */ 51221308Sache if (errno != EINTR) 513165670Sache return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF); 51421308Sache } 51521308Sache} 516119610Sache 517119610Sache#if defined (HANDLE_MULTIBYTE) 518119610Sache/* read multibyte char */ 519119610Sacheint 520119610Sache_rl_read_mbchar (mbchar, size) 521119610Sache char *mbchar; 522119610Sache int size; 523119610Sache{ 524173403Sache int mb_len, c; 525119610Sache size_t mbchar_bytes_length; 526119610Sache wchar_t wc; 527119610Sache mbstate_t ps, ps_back; 528119610Sache 529119610Sache memset(&ps, 0, sizeof (mbstate_t)); 530119610Sache memset(&ps_back, 0, sizeof (mbstate_t)); 531173403Sache 532173403Sache mb_len = 0; 533119610Sache while (mb_len < size) 534119610Sache { 535119610Sache RL_SETSTATE(RL_STATE_MOREINPUT); 536173403Sache c = rl_read_key (); 537119610Sache RL_UNSETSTATE(RL_STATE_MOREINPUT); 538119610Sache 539173403Sache if (c < 0) 540173403Sache break; 541173403Sache 542173403Sache mbchar[mb_len++] = c; 543173403Sache 544119610Sache mbchar_bytes_length = mbrtowc (&wc, mbchar, mb_len, &ps); 545119610Sache if (mbchar_bytes_length == (size_t)(-1)) 546119610Sache break; /* invalid byte sequence for the current locale */ 547119610Sache else if (mbchar_bytes_length == (size_t)(-2)) 548119610Sache { 549119610Sache /* shorted bytes */ 550119610Sache ps = ps_back; 551119610Sache continue; 552119610Sache } 553157184Sache else if (mbchar_bytes_length == 0) 554157184Sache { 555157184Sache mbchar[0] = '\0'; /* null wide character */ 556157184Sache mb_len = 1; 557157184Sache break; 558157184Sache } 559119610Sache else if (mbchar_bytes_length > (size_t)(0)) 560119610Sache break; 561119610Sache } 562119610Sache 563119610Sache return mb_len; 564119610Sache} 565119610Sache 566119610Sache/* Read a multibyte-character string whose first character is FIRST into 567165670Sache the buffer MB of length MLEN. Returns the last character read, which 568119610Sache may be FIRST. Used by the search functions, among others. Very similar 569119610Sache to _rl_read_mbchar. */ 570119610Sacheint 571165670Sache_rl_read_mbstring (first, mb, mlen) 572119610Sache int first; 573119610Sache char *mb; 574165670Sache int mlen; 575119610Sache{ 576119610Sache int i, c; 577119610Sache mbstate_t ps; 578119610Sache 579119610Sache c = first; 580165670Sache memset (mb, 0, mlen); 581173403Sache for (i = 0; c >= 0 && i < mlen; i++) 582119610Sache { 583119610Sache mb[i] = (char)c; 584119610Sache memset (&ps, 0, sizeof (mbstate_t)); 585119610Sache if (_rl_get_char_len (mb, &ps) == -2) 586119610Sache { 587119610Sache /* Read more for multibyte character */ 588119610Sache RL_SETSTATE (RL_STATE_MOREINPUT); 589119610Sache c = rl_read_key (); 590119610Sache RL_UNSETSTATE (RL_STATE_MOREINPUT); 591119610Sache } 592119610Sache else 593119610Sache break; 594119610Sache } 595119610Sache return c; 596119610Sache} 597119610Sache#endif /* HANDLE_MULTIBYTE */ 598