TheGNUGeneralPublicLicenseisoftenshippedwithGNUsoftware,and isgenerallykeptinafilecalledCOPYINGorLICENSE.Ifyoudonot haveacopyofthelicense,writetotheFreeSoftwareFoundation,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA. */ #define READLINE_LIBRARY
#ifdefined (__TANDEM) # include <floss.h> #endif
#ifdefined (HAVE_CONFIG_H) # include "config_readline.h" #endif
/* Return the amount of space available in the buffer for stuffing
characters. */ staticint
ibuffer_space ()
{ if (pop_index > push_index) return (pop_index - push_index - 1); else return (ibuffer_len - (push_index - pop_index));
}
/* Get a key from the buffer of characters to be read. ReturnthekeyinKEY.
Result is KEY if there was a key, or 0 if there wasn't. */ staticint
rl_get_char (key) int *key;
{ if (push_index == pop_index) return (0);
*key = ibuffer[pop_index++];
if (pop_index >= ibuffer_len)
pop_index = 0;
return (1);
}
/* Stuff KEY into the *front* of the input buffer. Returnsnon-zeroifsuccessful,zeroifthereis
no space left in the buffer. */ int
_rl_unget_char (key) int key;
{ if (ibuffer_space ())
{
pop_index--; if (pop_index < 0)
pop_index = ibuffer_len - 1;
ibuffer[pop_index] = key; return (1);
} return (0);
}
int
_rl_pushed_input_available ()
{ return (push_index != pop_index);
}
/* If a character is available to be read, then read it and stuff it into IBUFFER.Otherwise,justreturn.Returnsnumberofcharactersread
(0 if none available) and -1 on error (EIO). */ staticint
rl_gather_tyi ()
{ int tty; registerint tem, result; int chars_avail, k; char input; #ifdefined(HAVE_SELECT)
fd_set readfds, exceptfds; struct timeval timeout; #endif
#ifdefined (__MINGW32__) /* Use getch/_kbhit to check for available console input, in the same way
that we read it normally. */
chars_avail = isatty (tty) ? _kbhit () : 0;
result = 0; #endif
/* If there's nothing available, don't waste time trying to read
something. */ if (chars_avail <= 0) return0;
tem = ibuffer_space ();
if (chars_avail > tem)
chars_avail = tem;
/* One cannot read all of the available input. I can only read a single characteratatime,orelseprogramswhichrequireinputcanbe thwarted.Ifthebufferislargerthanonecharacter,Ilose.
Damn! */ if (tem < ibuffer_len)
chars_avail = 0;
if (result != -1)
{ while (chars_avail--)
{
k = (*rl_getc_function) (rl_instream);
rl_stuff_char (k); if (k == NEWLINE || k == RETURN) break;
}
} else
{ if (chars_avail)
rl_stuff_char (input);
}
return1;
}
int
rl_set_keyboard_input_timeout (u) int u;
{ int o;
o = _keyboard_input_timeout; if (u >= 0)
_keyboard_input_timeout = u; return (o);
}
/* Is there input available to be read on the readline input file descriptor?Onlyworksifthesystemhasselect(2)orFIONREAD. Usesthevalueof_keyboard_input_timeoutasthetimeout;ifanother readlinefunctionwantstospecifyatimeoutandnotleaveitupto theuser,itshoulduse_rl_input_queued(timeout_value_in_microseconds)
instead. */ int
_rl_input_available ()
{ #ifdefined(HAVE_SELECT)
fd_set readfds, exceptfds; struct timeval timeout; #endif #if !defined (HAVE_SELECT) && defined(FIONREAD) int chars_avail; #endif int tty;
/* Add KEY to the buffer of characters to be read. Returns 1 if the
character was stuffed correctly; 0 otherwise. */ int
rl_stuff_char (key) int key;
{ if (ibuffer_space () == 0) return0;
/* Make C be the next command to be executed. */ int
rl_execute_next (c) int c;
{
rl_pending_input = c;
RL_SETSTATE (RL_STATE_INPUTPENDING); return0;
}
/* Clear any pending input pushed with rl_execute_next() */ int
rl_clear_pending_input ()
{
rl_pending_input = 0;
RL_UNSETSTATE (RL_STATE_INPUTPENDING); return0;
}
/* Read a key, including pending input. */ int
rl_read_key ()
{ int c;
rl_key_sequence_length++;
if (rl_pending_input)
{
c = rl_pending_input;
rl_clear_pending_input ();
} else
{ /* If input is coming from a macro, then use that. */ if ((c = _rl_next_macro_key ())) return (c);
/* If the user has an event function, then call it periodically. */ if (rl_event_hook)
{ while (rl_event_hook && rl_get_char (&c) == 0)
{
(*rl_event_hook) (); if (rl_done) /* XXX - experimental */ return ('\n'); if (rl_gather_tyi () < 0) /* XXX - EIO */
{
rl_done = 1; return ('\n');
}
}
} else
{ if (rl_get_char (&c) == 0)
c = (*rl_getc_function) (rl_instream);
}
}
return (c);
}
int
rl_getc (stream)
FILE *stream;
{ int result; unsignedchar c;
while (1)
{ #ifdefined (__MINGW32__) if (isatty (fileno (stream))) return (getch ()); #endif
result = read (fileno (stream), &c, sizeof (unsignedchar));
if (result == sizeof (unsignedchar)) return (c);
/* If zero characters are returned, then the file that we are
reading from is empty! Return EOF in that case. */ if (result == 0) return (EOF);
#ifdefined (__BEOS__) if (errno == EINTR) continue; #endif
if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
{ if (sh_unset_nodelay_mode (fileno (stream)) < 0) return (EOF); continue;
}
#undef X_EWOULDBLOCK #undef X_EAGAIN
/* If the error that we received was SIGINT, then try again, thisissimplyaninterruptedsystemcalltoread().
Otherwise, some error ocurred, also signifying EOF. */ if (errno != EINTR) return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
}
}
#ifdefined (HANDLE_MULTIBYTE) /* read multibyte char */ int
_rl_read_mbchar (mbchar, size) char *mbchar; int size;
{ int mb_len = 0;
size_t mbchar_bytes_length; wchar_t wc;
mbstate_t ps, ps_back;
/* Read a multibyte-character string whose first character is FIRST into thebufferMBoflengthMLEN.Returnsthelastcharacterread,which maybeFIRST.Usedbythesearchfunctions,amongothers.Verysimilar
to _rl_read_mbchar. */ int
_rl_read_mbstring (first, mb, mlen) int first; char *mb; int mlen;
{ int i, c;
mbstate_t ps;
c = first;
memset (mb, 0, mlen); for (i = 0; i < mlen; i++)
{
mb[i] = (char)c;
memset (&ps, 0, sizeof (mbstate_t)); if (_rl_get_char_len (mb, &ps) == -2)
{ /* Read more for multibyte character */
RL_SETSTATE (RL_STATE_MOREINPUT);
c = rl_read_key ();
RL_UNSETSTATE (RL_STATE_MOREINPUT);
} else break;
} return c;
} #endif/* HANDLE_MULTIBYTE */