#define READLINE_LIBRARY
#if defined (HAVE_CONFIG_H)
# include <config.h>
#endif
#include <sys/types.h>
#include "posixstat.h"
#include <fcntl.h>
#if defined (HAVE_SYS_FILE_H)
# include <sys/file.h>
#endif
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#if defined (HAVE_STDLIB_H)
# include <stdlib.h>
#else
# include "ansi_stdlib.h"
#endif
#if defined (HAVE_LOCALE_H)
# include <locale.h>
#endif
#include <stdio.h>
#include "posixjmp.h"
#include "rldefs.h"
#include "rlmbutil.h"
#if defined (__EMX__)
# define INCL_DOSPROCESS
# include <os2.h>
#endif
#include "readline.h"
#include "history.h"
#include "rlprivate.h"
#include "rlshell.h"
#include "xmalloc.h"
#ifndef RL_LIBRARY_VERSION
# define RL_LIBRARY_VERSION "4.3"
#endif
#ifndef RL_READLINE_VERSION
# define RL_READLINE_VERSION 0x0403
#endif
extern void _rl_free_history_entry PARAMS((HIST_ENTRY *));
static char *readline_internal PARAMS((void));
static void readline_initialize_everything PARAMS((void));
static void bind_arrow_keys_internal PARAMS((Keymap));
static void bind_arrow_keys PARAMS((void));
static void readline_default_bindings PARAMS((void));
const char *rl_library_version = RL_LIBRARY_VERSION;
int rl_readline_version = RL_READLINE_VERSION;
int rl_gnu_readline_p = 1;
Keymap _rl_keymap = emacs_standard_keymap;
int rl_editing_mode = emacs_mode;
int rl_insert_mode = RL_IM_DEFAULT;
int rl_dispatching;
int _rl_last_command_was_kill = 0;
int rl_numeric_arg = 1;
int rl_explicit_arg = 0;
int rl_arg_sign = 1;
static int rl_initialized;
#if 0
static int running_in_emacs;
#endif
int rl_readline_state = RL_STATE_NONE;
int rl_point;
int rl_mark;
int rl_end;
int rl_done;
rl_command_func_t *rl_last_func = (rl_command_func_t *)NULL;
procenv_t readline_top_level;
FILE *_rl_in_stream, *_rl_out_stream;
FILE *rl_instream = (FILE *)NULL;
FILE *rl_outstream = (FILE *)NULL;
int readline_echoing_p = 0;
char *rl_prompt = (char *)NULL;
int rl_visible_prompt_length = 0;
int rl_already_prompted = 0;
int rl_key_sequence_length = 0;
rl_hook_func_t *rl_startup_hook = (rl_hook_func_t *)NULL;
rl_hook_func_t *rl_pre_input_hook = (rl_hook_func_t *)NULL;
static char *the_line;
int _rl_eof_char = CTRL ('D');
int rl_pending_input = 0;
const char *rl_terminal_name = (const char *)NULL;
int _rl_horizontal_scroll_mode = 0;
int _rl_mark_modified_lines = 0;
int _rl_bell_preference = AUDIBLE_BELL;
char *_rl_comment_begin;
Keymap rl_executing_keymap;
int rl_erase_empty_line = 0;
int rl_num_chars_to_read;
char *rl_line_buffer = (char *)NULL;
int rl_line_buffer_len = 0;
unsigned char _rl_parsing_conditionalized_out = 0;
int _rl_convert_meta_chars_to_ascii = 1;
int _rl_output_meta_chars = 0;
int _rl_meta_flag = 0;
int
rl_set_prompt (prompt)
const char *prompt;
{
FREE (rl_prompt);
rl_prompt = prompt ? savestring (prompt) : (char *)NULL;
rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
return 0;
}
char *
readline (prompt)
const char *prompt;
{
char *value;
if (rl_pending_input == EOF)
{
rl_clear_pending_input ();
return ((char *)NULL);
}
rl_set_prompt (prompt);
rl_initialize ();
(*rl_prep_term_function) (_rl_meta_flag);
#if defined (HANDLE_SIGNALS)
rl_set_signals ();
#endif
value = readline_internal ();
(*rl_deprep_term_function) ();
#if defined (HANDLE_SIGNALS)
rl_clear_signals ();
#endif
return (value);
}
#if defined (READLINE_CALLBACKS)
# define STATIC_CALLBACK
#else
# define STATIC_CALLBACK static
#endif
STATIC_CALLBACK void
readline_internal_setup ()
{
char *nprompt;
_rl_in_stream = rl_instream;
_rl_out_stream = rl_outstream;
if (rl_startup_hook)
(*rl_startup_hook) ();
if (readline_echoing_p == 0 && rl_redisplay_function == rl_redisplay)
{
if (rl_prompt && rl_already_prompted == 0)
{
nprompt = _rl_strip_prompt (rl_prompt);
fprintf (_rl_out_stream, "%s", nprompt);
fflush (_rl_out_stream);
free (nprompt);
}
}
else
{
if (rl_prompt && rl_already_prompted)
rl_on_new_line_with_prompt ();
else
rl_on_new_line ();
(*rl_redisplay_function) ();
}
#if defined (VI_MODE)
if (rl_editing_mode == vi_mode)
rl_vi_insertion_mode (1, 0);
#endif
if (rl_pre_input_hook)
(*rl_pre_input_hook) ();
}
STATIC_CALLBACK char *
readline_internal_teardown (eof)
int eof;
{
char *temp;
HIST_ENTRY *entry;
entry = current_history ();
if (entry && rl_undo_list)
{
temp = savestring (the_line);
rl_revert_line (1, 0);
entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL);
_rl_free_history_entry (entry);
strcpy (the_line, temp);
free (temp);
}
if (rl_undo_list)
rl_free_undo_list ();
_rl_set_insert_mode (RL_IM_INSERT, 0);
return (eof ? (char *)NULL : savestring (the_line));
}
STATIC_CALLBACK int
#if defined (READLINE_CALLBACKS)
readline_internal_char ()
#else
readline_internal_charloop ()
#endif
{
static int lastc, eof_found;
int c, code, lk;
lastc = -1;
eof_found = 0;
#if !defined (READLINE_CALLBACKS)
while (rl_done == 0)
{
#endif
lk = _rl_last_command_was_kill;
code = setjmp (readline_top_level);
if (code)
(*rl_redisplay_function) ();
if (rl_pending_input == 0)
{
_rl_init_argument ();
rl_key_sequence_length = 0;
}
RL_SETSTATE(RL_STATE_READCMD);
c = rl_read_key ();
RL_UNSETSTATE(RL_STATE_READCMD);
if (c == EOF && rl_end)
c = NEWLINE;
if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
{
#if defined (READLINE_CALLBACKS)
RL_SETSTATE(RL_STATE_DONE);
return (rl_done = 1);
#else
eof_found = 1;
break;
#endif
}
lastc = c;
_rl_dispatch ((unsigned char)c, _rl_keymap);
if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
_rl_last_command_was_kill = 0;
#if defined (VI_MODE)
if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
rl_vi_check ();
#endif
if (rl_num_chars_to_read && rl_end >= rl_num_chars_to_read)
{
(*rl_redisplay_function) ();
rl_newline (1, '\n');
}
if (rl_done == 0)
(*rl_redisplay_function) ();
if (rl_erase_empty_line && rl_done && rl_last_func == rl_newline &&
rl_point == 0 && rl_end == 0)
_rl_erase_entire_line ();
#if defined (READLINE_CALLBACKS)
return 0;
#else
}
return (eof_found);
#endif
}
#if defined (READLINE_CALLBACKS)
static int
readline_internal_charloop ()
{
int eof = 1;
while (rl_done == 0)
eof = readline_internal_char ();
return (eof);
}
#endif
static char *
readline_internal ()
{
int eof;
readline_internal_setup ();
eof = readline_internal_charloop ();
return (readline_internal_teardown (eof));
}
void
_rl_init_line_state ()
{
rl_point = rl_end = rl_mark = 0;
the_line = rl_line_buffer;
the_line[0] = 0;
}
void
_rl_set_the_line ()
{
the_line = rl_line_buffer;
}
int
_rl_dispatch (key, map)
register int key;
Keymap map;
{
return _rl_dispatch_subseq (key, map, 0);
}
int
_rl_dispatch_subseq (key, map, got_subseq)
register int key;
Keymap map;
int got_subseq;
{
int r, newkey;
char *macro;
rl_command_func_t *func;
if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
{
if (map[ESC].type == ISKMAP)
{
if (RL_ISSTATE (RL_STATE_MACRODEF))
_rl_add_macro_char (ESC);
map = FUNCTION_TO_KEYMAP (map, ESC);
key = UNMETA (key);
rl_key_sequence_length += 2;
return (_rl_dispatch (key, map));
}
else
rl_ding ();
return 0;
}
if (RL_ISSTATE (RL_STATE_MACRODEF))
_rl_add_macro_char (key);
r = 0;
switch (map[key].type)
{
case ISFUNC:
func = map[key].function;
if (func)
{
if (func == rl_do_lowercase_version)
return (_rl_dispatch (_rl_to_lower (key), map));
rl_executing_keymap = map;
#if 0
_rl_suppress_redisplay = (map[key].function == rl_insert) && _rl_input_available ();
#endif
rl_dispatching = 1;
RL_SETSTATE(RL_STATE_DISPATCHING);
r = (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
RL_UNSETSTATE(RL_STATE_DISPATCHING);
rl_dispatching = 0;
if (rl_pending_input == 0 && map[key].function != rl_digit_argument)
rl_last_func = map[key].function;
}
else if (map[ANYOTHERKEY].function)
{
_rl_unget_char (key);
return -2;
}
else if (got_subseq)
{
_rl_unget_char (key);
return -1;
}
else
{
_rl_abort_internal ();
return -1;
}
break;
case ISKMAP:
if (map[key].function != 0)
{
#if defined (VI_MODE)
if (rl_editing_mode == vi_mode && key == ESC && map == vi_insertion_keymap
&& _rl_input_queued (0) == 0)
return (_rl_dispatch (ANYOTHERKEY, FUNCTION_TO_KEYMAP (map, key)));
#endif
rl_key_sequence_length++;
if (key == ESC)
RL_SETSTATE(RL_STATE_METANEXT);
RL_SETSTATE(RL_STATE_MOREINPUT);
newkey = rl_read_key ();
RL_UNSETSTATE(RL_STATE_MOREINPUT);
if (key == ESC)
RL_UNSETSTATE(RL_STATE_METANEXT);
if (newkey < 0)
{
_rl_abort_internal ();
return -1;
}
r = _rl_dispatch_subseq (newkey, FUNCTION_TO_KEYMAP (map, key), got_subseq || map[ANYOTHERKEY].function);
if (r == -2)
r = _rl_dispatch (ANYOTHERKEY, FUNCTION_TO_KEYMAP (map, key));
else if (r && map[ANYOTHERKEY].function)
{
_rl_unget_char (key);
return -2;
}
else if (r && got_subseq)
{
_rl_unget_char (key);
return -1;
}
}
else
{
_rl_abort_internal ();
return -1;
}
break;
case ISMACR:
if (map[key].function != 0)
{
macro = savestring ((char *)map[key].function);
_rl_with_macro_input (macro);
return 0;
}
break;
}
#if defined (VI_MODE)
if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
key != ANYOTHERKEY &&
_rl_vi_textmod_command (key))
_rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
#endif
return (r);
}
int
rl_initialize ()
{
if (!rl_initialized)
{
RL_SETSTATE(RL_STATE_INITIALIZING);
readline_initialize_everything ();
RL_UNSETSTATE(RL_STATE_INITIALIZING);
rl_initialized++;
RL_SETSTATE(RL_STATE_INITIALIZED);
}
_rl_init_line_state ();
rl_done = 0;
RL_UNSETSTATE(RL_STATE_DONE);
_rl_start_using_history ();
rl_reset_line_state ();
rl_last_func = (rl_command_func_t *)NULL;
_rl_parsing_conditionalized_out = 0;
#if defined (VI_MODE)
if (rl_editing_mode == vi_mode)
_rl_vi_initialize_line ();
#endif
_rl_set_insert_mode (RL_IM_DEFAULT, 1);
return 0;
}
#if 0
#if defined (__EMX__)
static void
_emx_build_environ ()
{
TIB *tibp;
PIB *pibp;
char *t, **tp;
int c;
DosGetInfoBlocks (&tibp, &pibp);
t = pibp->pib_pchenv;
for (c = 1; *t; c++)
t += strlen (t) + 1;
tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *));
t = pibp->pib_pchenv;
while (*t)
{
*tp++ = t;
t += strlen (t) + 1;
}
*tp = 0;
}
#endif
#endif
static void
readline_initialize_everything ()
{
#if 0
#if defined (__EMX__)
if (environ == 0)
_emx_build_environ ();
#endif
#endif
#if 0
running_in_emacs = sh_get_env_value ("EMACS") != (char *)0;
#endif
if (!rl_instream)
rl_instream = stdin;
if (!rl_outstream)
rl_outstream = stdout;
_rl_in_stream = rl_instream;
_rl_out_stream = rl_outstream;
if (rl_line_buffer == 0)
rl_line_buffer = (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
if (rl_terminal_name == 0)
rl_terminal_name = sh_get_env_value ("TERM");
_rl_init_terminal_io (rl_terminal_name);
readline_default_bindings ();
rl_initialize_funmap ();
_rl_init_eightbit ();
rl_read_init_file ((char *)NULL);
if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
{
_rl_screenwidth--;
_rl_screenchars -= _rl_screenheight;
}
rl_set_keymap_from_edit_mode ();
bind_arrow_keys ();
if (_rl_enable_meta)
_rl_enable_meta_key ();
if (rl_completer_word_break_characters == (char *)NULL)
rl_completer_word_break_characters = rl_basic_word_break_characters;
}
static void
readline_default_bindings ()
{
rl_tty_set_default_bindings (_rl_keymap);
}
static void
bind_arrow_keys_internal (map)
Keymap map;
{
Keymap xkeymap;
xkeymap = _rl_keymap;
_rl_keymap = map;
#if defined (__MSDOS__)
_rl_bind_if_unbound ("\033[0A", rl_get_previous_history);
_rl_bind_if_unbound ("\033[0B", rl_backward_char);
_rl_bind_if_unbound ("\033[0C", rl_forward_char);
_rl_bind_if_unbound ("\033[0D", rl_get_next_history);
#endif
#ifdef __MINGW32__
rl_macro_bind ("\340H", "\033[A", map);
rl_macro_bind ("\340K", "\033[D", map);
rl_macro_bind ("\340M", "\033[C", map);
rl_macro_bind ("\340P", "\033[B", map);
#endif
_rl_bind_if_unbound ("\033[A", rl_get_previous_history);
_rl_bind_if_unbound ("\033[B", rl_get_next_history);
_rl_bind_if_unbound ("\033[C", rl_forward_char);
_rl_bind_if_unbound ("\033[D", rl_backward_char);
_rl_bind_if_unbound ("\033[H", rl_beg_of_line);
_rl_bind_if_unbound ("\033[F", rl_end_of_line);
_rl_bind_if_unbound ("\033OA", rl_get_previous_history);
_rl_bind_if_unbound ("\033OB", rl_get_next_history);
_rl_bind_if_unbound ("\033OC", rl_forward_char);
_rl_bind_if_unbound ("\033OD", rl_backward_char);
_rl_bind_if_unbound ("\033OH", rl_beg_of_line);
_rl_bind_if_unbound ("\033OF", rl_end_of_line);
_rl_keymap = xkeymap;
}
static void
bind_arrow_keys ()
{
bind_arrow_keys_internal (emacs_standard_keymap);
#if defined (VI_MODE)
bind_arrow_keys_internal (vi_movement_keymap);
bind_arrow_keys_internal (vi_insertion_keymap);
#endif
}
int
rl_save_state (sp)
struct readline_state *sp;
{
if (sp == 0)
return -1;
sp->point = rl_point;
sp->end = rl_end;
sp->mark = rl_mark;
sp->buffer = rl_line_buffer;
sp->buflen = rl_line_buffer_len;
sp->ul = rl_undo_list;
sp->prompt = rl_prompt;
sp->rlstate = rl_readline_state;
sp->done = rl_done;
sp->kmap = _rl_keymap;
sp->lastfunc = rl_last_func;
sp->insmode = rl_insert_mode;
sp->edmode = rl_editing_mode;
sp->kseqlen = rl_key_sequence_length;
sp->inf = rl_instream;
sp->outf = rl_outstream;
sp->pendingin = rl_pending_input;
sp->macro = rl_executing_macro;
sp->catchsigs = rl_catch_signals;
#if defined (SIGWINCH)
sp->catchsigwinch = rl_catch_sigwinch;
#endif
return (0);
}
int
rl_restore_state (sp)
struct readline_state *sp;
{
if (sp == 0)
return -1;
rl_point = sp->point;
rl_end = sp->end;
rl_mark = sp->mark;
the_line = rl_line_buffer = sp->buffer;
rl_line_buffer_len = sp->buflen;
rl_undo_list = sp->ul;
rl_prompt = sp->prompt;
rl_readline_state = sp->rlstate;
rl_done = sp->done;
_rl_keymap = sp->kmap;
rl_last_func = sp->lastfunc;
rl_insert_mode = sp->insmode;
rl_editing_mode = sp->edmode;
rl_key_sequence_length = sp->kseqlen;
rl_instream = sp->inf;
rl_outstream = sp->outf;
rl_pending_input = sp->pendingin;
rl_executing_macro = sp->macro;
rl_catch_signals = sp->catchsigs;
#if defined (SIGWINCH)
rl_catch_sigwinch = sp->catchsigwinch;
#endif
return (0);
}