2017-11-30 00:21:53 +08:00
|
|
|
/*
|
2017-11-30 00:20:21 +08:00
|
|
|
* Copyright (c) 1992, 1993 Simmule Turner and Rich Salz
|
|
|
|
* All rights reserved.
|
2010-07-18 07:41:18 +08:00
|
|
|
*
|
|
|
|
* This software is not subject to any license of the American Telephone
|
|
|
|
* and Telegraph Company or of the Regents of the University of California.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose on
|
|
|
|
* any computer system, and to alter it and redistribute it freely, subject
|
|
|
|
* to the following restrictions:
|
|
|
|
* 1. The authors are not responsible for the consequences of use of this
|
|
|
|
* software, no matter how awful, even if they arise from flaws in it.
|
|
|
|
* 2. The origin of this software must not be misrepresented, either by
|
|
|
|
* explicit claim or by omission. Since few users ever read sources,
|
|
|
|
* credits must appear in the documentation.
|
|
|
|
* 3. Altered versions must be plainly marked as such, and must not be
|
|
|
|
* misrepresented as being the original software. Since few users
|
|
|
|
* ever read sources, credits must appear in the documentation.
|
|
|
|
* 4. This notice may not be removed or altered.
|
|
|
|
*/
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
2013-07-08 22:27:55 +08:00
|
|
|
#include <signal.h>
|
2015-09-08 12:39:00 +08:00
|
|
|
#include <sys/ioctl.h>
|
2013-07-08 22:27:55 +08:00
|
|
|
|
|
|
|
#include "editline.h"
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Manifest constants.
|
|
|
|
*/
|
2010-07-19 00:57:07 +08:00
|
|
|
#define SCREEN_COLS 80
|
2008-12-03 04:58:55 +08:00
|
|
|
#define SCREEN_ROWS 24
|
2010-08-05 21:14:06 +08:00
|
|
|
#define EL_STDIN 0
|
|
|
|
#define EL_STDOUT 1
|
2008-12-03 04:58:55 +08:00
|
|
|
#define NO_ARG (-1)
|
|
|
|
#define DEL 127
|
|
|
|
#define SEPS "\"#$&'()*:;<=>?[\\]^`{|}~\n\t "
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** The type of case-changing to perform.
|
|
|
|
*/
|
2008-12-03 04:58:55 +08:00
|
|
|
typedef enum {
|
2010-07-26 07:44:18 +08:00
|
|
|
TOupper, TOlower, TOcapitalize
|
2008-12-03 04:58:55 +08:00
|
|
|
} el_case_t;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Key to command mapping.
|
|
|
|
*/
|
2008-12-03 04:58:55 +08:00
|
|
|
typedef struct {
|
|
|
|
int Key;
|
2010-07-18 08:41:38 +08:00
|
|
|
el_status_t (*Function)(void);
|
2008-12-03 04:58:55 +08:00
|
|
|
} el_keymap_t;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Command history structure.
|
|
|
|
*/
|
2008-12-03 04:58:55 +08:00
|
|
|
typedef struct {
|
|
|
|
int Size;
|
|
|
|
int Pos;
|
2010-08-04 08:12:19 +08:00
|
|
|
char **Lines;
|
2008-12-03 04:58:55 +08:00
|
|
|
} el_hist_t;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2017-12-01 01:20:29 +08:00
|
|
|
/* User definable callbacks. */
|
2010-08-05 07:56:09 +08:00
|
|
|
rl_getc_func_t *rl_getc_function = rl_getc;
|
|
|
|
rl_hook_func_t *rl_event_hook;
|
|
|
|
rl_vintfunc_t *rl_prep_term_function = rl_prep_terminal;
|
|
|
|
rl_voidfunc_t *rl_deprep_term_function = rl_deprep_terminal;
|
2010-08-05 07:08:30 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
/*
|
|
|
|
** Globals.
|
|
|
|
*/
|
2008-12-03 04:58:55 +08:00
|
|
|
int rl_eof;
|
|
|
|
int rl_erase;
|
|
|
|
int rl_intr;
|
|
|
|
int rl_kill;
|
|
|
|
int rl_quit;
|
2010-07-18 07:41:18 +08:00
|
|
|
#ifdef CONFIG_SIGSTOP
|
2008-12-03 04:58:55 +08:00
|
|
|
int rl_susp;
|
2010-07-18 07:41:18 +08:00
|
|
|
#endif
|
2008-12-03 04:58:55 +08:00
|
|
|
|
2010-08-04 08:12:19 +08:00
|
|
|
int el_hist_size = 15;
|
|
|
|
static el_hist_t H = {
|
|
|
|
.Size = 0,
|
|
|
|
.Pos = 0,
|
|
|
|
.Lines = NULL,
|
|
|
|
};
|
|
|
|
|
2010-07-26 04:14:14 +08:00
|
|
|
static char NILSTR[] = "";
|
|
|
|
static const char *el_input = NILSTR;
|
2008-12-03 04:58:55 +08:00
|
|
|
static char *Yanked;
|
|
|
|
static char *Screen;
|
|
|
|
static char NEWLINE[]= CRLF;
|
2018-12-24 03:49:25 +08:00
|
|
|
static char CLEAR[]= "\ec";
|
2010-08-05 21:14:06 +08:00
|
|
|
static const char *el_term = "dumb";
|
2008-12-03 04:58:55 +08:00
|
|
|
static int Repeat;
|
2010-07-26 04:14:14 +08:00
|
|
|
static int old_point;
|
|
|
|
static int el_push_back;
|
|
|
|
static int el_pushed;
|
2010-07-24 09:46:58 +08:00
|
|
|
static int el_intr_pending;
|
2010-08-05 21:14:06 +08:00
|
|
|
static int el_infd = EL_STDIN;
|
|
|
|
static int el_outfd = EL_STDOUT;
|
2010-03-10 04:18:03 +08:00
|
|
|
static el_keymap_t Map[];
|
|
|
|
static el_keymap_t MetaMap[];
|
2017-11-29 21:41:43 +08:00
|
|
|
static size_t Length = 0;
|
2010-07-24 09:12:45 +08:00
|
|
|
static size_t ScreenCount;
|
|
|
|
static size_t ScreenSize;
|
2010-08-05 21:14:06 +08:00
|
|
|
static char *backspace = "\b";
|
2017-11-29 22:32:59 +08:00
|
|
|
static char *old_search = NULL;
|
2010-08-05 21:14:06 +08:00
|
|
|
static int tty_cols = SCREEN_COLS;
|
|
|
|
static int tty_rows = SCREEN_ROWS;
|
2017-12-11 15:43:43 +08:00
|
|
|
static int Searching = 0;
|
|
|
|
static const char *(*search_move)(void);
|
|
|
|
static const char *old_prompt = NULL;
|
|
|
|
static rl_vcpfunc_t *line_handler = NULL;
|
2019-11-19 20:52:22 +08:00
|
|
|
static char *line_up = "\x1b[A";
|
|
|
|
static char *line_down = "\x1b[B";
|
|
|
|
int prompt_len = 0;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-24 09:38:25 +08:00
|
|
|
int el_no_echo = 0; /* e.g., under Emacs */
|
2014-11-05 05:47:50 +08:00
|
|
|
int el_no_hist = 0;
|
2010-07-20 06:18:20 +08:00
|
|
|
int rl_point;
|
|
|
|
int rl_mark;
|
|
|
|
int rl_end;
|
2010-07-26 04:01:04 +08:00
|
|
|
int rl_meta_chars = 0; /* Display 8-bit chars as the actual char(0) or as `M-x'(1)? */
|
2010-08-11 19:14:32 +08:00
|
|
|
int rl_inhibit_complete = 0;
|
2017-11-29 21:41:43 +08:00
|
|
|
char *rl_line_buffer = NULL;
|
|
|
|
const char *rl_prompt = NULL;
|
|
|
|
const char *rl_readline_name = NULL; /* Set by calling program, for conditional parsing of ~/.inputrc - Not supported yet! */
|
2010-08-05 19:20:35 +08:00
|
|
|
FILE *rl_instream = NULL; /* The stdio stream from which input is read. Defaults to stdin if NULL */
|
|
|
|
FILE *rl_outstream = NULL; /* The stdio stream to which output is flushed. Defaults to stdout if NULL */
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
/* Declarations. */
|
2017-12-11 15:43:43 +08:00
|
|
|
static char *editinput(int complete);
|
2010-07-19 00:57:07 +08:00
|
|
|
#ifdef CONFIG_USE_TERMCAP
|
|
|
|
extern char *tgetstr(const char *, char **);
|
|
|
|
extern int tgetent(char *, const char *);
|
|
|
|
extern int tgetnum(const char *);
|
2010-07-18 07:41:18 +08:00
|
|
|
#endif
|
2010-07-30 08:03:46 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Misc. local helper functions.
|
|
|
|
*/
|
|
|
|
static int is_alpha_num(unsigned char c)
|
|
|
|
{
|
|
|
|
if (isalnum(c))
|
2019-11-20 19:46:39 +08:00
|
|
|
return 1;
|
2010-07-30 08:03:46 +08:00
|
|
|
if (ISMETA(c))
|
2019-11-20 19:46:39 +08:00
|
|
|
return 1;
|
2010-07-30 08:03:46 +08:00
|
|
|
if (ISCTL(c))
|
2019-11-20 19:46:39 +08:00
|
|
|
return 1;
|
2010-07-30 08:03:46 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
/*
|
|
|
|
** TTY input/output functions.
|
|
|
|
*/
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static void tty_flush(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2009-02-09 05:09:02 +08:00
|
|
|
ssize_t res;
|
|
|
|
|
2013-07-08 22:34:32 +08:00
|
|
|
if (!ScreenCount)
|
2019-11-20 19:46:39 +08:00
|
|
|
return;
|
2013-07-08 22:34:32 +08:00
|
|
|
|
|
|
|
if (!el_no_echo) {
|
2019-11-20 19:46:39 +08:00
|
|
|
res = write(el_outfd, Screen, ScreenCount);
|
|
|
|
if (res > 0)
|
|
|
|
ScreenCount = 0;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static void tty_put(const char c)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2014-06-25 09:05:53 +08:00
|
|
|
if (el_no_echo)
|
2019-11-20 19:46:39 +08:00
|
|
|
return;
|
2014-06-25 09:05:53 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
Screen[ScreenCount] = c;
|
2018-07-12 07:01:48 +08:00
|
|
|
if (++ScreenCount >= ScreenSize) {
|
2008-12-03 04:58:55 +08:00
|
|
|
ScreenSize += SCREEN_INC;
|
2010-07-23 17:01:51 +08:00
|
|
|
Screen = realloc(Screen, sizeof(char) * ScreenSize);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static void tty_puts(const char *p)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
while (*p)
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_put(*p++);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2010-07-30 08:03:46 +08:00
|
|
|
static void tty_show(unsigned char c)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
if (c == DEL) {
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_put('^');
|
|
|
|
tty_put('?');
|
2010-07-30 08:03:46 +08:00
|
|
|
} else if (ISCTL(c)) {
|
|
|
|
tty_put('^');
|
|
|
|
tty_put(UNCTL(c));
|
2010-07-26 08:03:31 +08:00
|
|
|
} else if (rl_meta_chars && ISMETA(c)) {
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_put('M');
|
|
|
|
tty_put('-');
|
|
|
|
tty_put(UNMETA(c));
|
2010-07-26 08:09:15 +08:00
|
|
|
} else {
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_put(c);
|
2010-07-26 04:01:04 +08:00
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static void tty_string(char *p)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2019-11-19 20:52:22 +08:00
|
|
|
int i = rl_point + prompt_len + 1;
|
2019-11-28 13:58:37 +08:00
|
|
|
|
2019-11-19 20:52:22 +08:00
|
|
|
while (*p) {
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_show(*p++);
|
2019-11-20 19:46:39 +08:00
|
|
|
if ((i++) % tty_cols == 0)
|
|
|
|
tty_put('\n');
|
2019-11-19 20:52:22 +08:00
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2010-07-30 08:03:46 +08:00
|
|
|
static void tty_push(int c)
|
|
|
|
{
|
|
|
|
el_pushed = 1;
|
|
|
|
el_push_back = c;
|
|
|
|
}
|
|
|
|
|
2010-08-05 07:08:30 +08:00
|
|
|
int rl_getc(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
int r;
|
2010-08-05 07:08:30 +08:00
|
|
|
char c;
|
|
|
|
|
|
|
|
do {
|
2019-11-20 19:46:39 +08:00
|
|
|
r = read(el_infd, &c, 1);
|
2010-08-05 07:08:30 +08:00
|
|
|
} while (r == -1 && errno == EINTR);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-08-05 07:08:30 +08:00
|
|
|
return r == 1 ? c : EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tty_get(void)
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_flush();
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2010-07-26 04:14:14 +08:00
|
|
|
if (el_pushed) {
|
|
|
|
el_pushed = 0;
|
|
|
|
return el_push_back;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2010-07-26 04:14:14 +08:00
|
|
|
if (*el_input)
|
|
|
|
return *el_input++;
|
2010-07-23 15:46:49 +08:00
|
|
|
|
2010-08-05 07:08:30 +08:00
|
|
|
return rl_getc_function();
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2010-08-05 21:14:06 +08:00
|
|
|
#define tty_back() tty_puts(backspace)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static void tty_backn(int n)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
while (--n >= 0)
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_back();
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static void tty_info(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-08-05 21:14:06 +08:00
|
|
|
rl_reset_terminal(NULL);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2010-08-05 07:08:30 +08:00
|
|
|
/*
|
|
|
|
** Glue routines to rl_ttyset()
|
|
|
|
*/
|
|
|
|
void rl_prep_terminal(int meta_flag)
|
|
|
|
{
|
|
|
|
rl_meta_chars = !meta_flag;
|
|
|
|
rl_ttyset(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rl_deprep_terminal(void)
|
|
|
|
{
|
|
|
|
rl_ttyset(1);
|
|
|
|
}
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
/*
|
|
|
|
** Print an array of words in columns.
|
|
|
|
*/
|
2010-08-05 18:48:51 +08:00
|
|
|
void el_print_columns(int ac, char **av)
|
2008-12-03 04:58:55 +08:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int k;
|
|
|
|
int len;
|
|
|
|
int skip;
|
|
|
|
int longest;
|
|
|
|
int cols;
|
2019-04-27 01:12:26 +08:00
|
|
|
int colwidth;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
/* Find longest name, determine column count from that. */
|
2017-11-29 23:40:10 +08:00
|
|
|
for (longest = 0, i = 0; i < ac; i++) {
|
2008-12-03 04:58:55 +08:00
|
|
|
if ((j = strlen((char *)av[i])) > longest)
|
|
|
|
longest = j;
|
2017-11-29 23:40:10 +08:00
|
|
|
}
|
2019-11-28 13:58:37 +08:00
|
|
|
|
2019-04-27 01:12:26 +08:00
|
|
|
colwidth = longest + 3;
|
2019-11-20 19:46:39 +08:00
|
|
|
if (colwidth > tty_cols)
|
|
|
|
colwidth = tty_cols;
|
2019-04-27 01:12:26 +08:00
|
|
|
cols = tty_cols / colwidth;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_puts(NEWLINE);
|
2008-06-07 18:28:36 +08:00
|
|
|
for (skip = ac / cols + 1, i = 0; i < skip; i++) {
|
2008-12-03 04:58:55 +08:00
|
|
|
for (j = i; j < ac; j += skip) {
|
|
|
|
for (p = av[j], len = strlen((char *)p), k = len; --k >= 0; p++)
|
|
|
|
tty_put(*p);
|
2017-11-29 23:40:10 +08:00
|
|
|
|
|
|
|
if (j + skip < ac) {
|
2019-04-27 01:12:26 +08:00
|
|
|
while (++len < colwidth)
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_put(' ');
|
2019-11-19 20:52:22 +08:00
|
|
|
}
|
2008-12-03 04:58:55 +08:00
|
|
|
}
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_puts(NEWLINE);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 20:52:22 +08:00
|
|
|
static void reposition(int key)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2019-11-19 20:52:22 +08:00
|
|
|
int len_with_prompt = prompt_len + rl_end;
|
2019-11-28 13:58:37 +08:00
|
|
|
int n = len_with_prompt / tty_cols; /* determine the number of lines */
|
|
|
|
int i = 0;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_put('\r');
|
2019-11-19 20:52:22 +08:00
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
int line;
|
2019-11-28 13:58:37 +08:00
|
|
|
|
|
|
|
/* determine num of current line */
|
2019-11-19 20:52:22 +08:00
|
|
|
if (key == CTL('A') || key == CTL('E') || key == rl_kill)
|
2019-11-28 13:58:37 +08:00
|
|
|
line = (prompt_len + old_point) / tty_cols;
|
2019-11-19 20:52:22 +08:00
|
|
|
else
|
|
|
|
line = len_with_prompt / tty_cols;
|
|
|
|
|
2019-11-28 13:58:37 +08:00
|
|
|
/* move to end of line(s) */
|
|
|
|
if (key == CTL('E')) {
|
|
|
|
int k;
|
|
|
|
|
|
|
|
for (k = line; k < n; k++)
|
2019-11-19 20:52:22 +08:00
|
|
|
tty_puts(line_down);
|
2019-11-28 13:58:37 +08:00
|
|
|
|
|
|
|
/* determine reminder of last line and redraw only it */
|
|
|
|
i = rl_point - (len_with_prompt % tty_cols);
|
2019-11-19 20:52:22 +08:00
|
|
|
} else {
|
2019-11-28 13:58:37 +08:00
|
|
|
int k;
|
|
|
|
|
|
|
|
/* CTRL-A, CTRL-U, insert (end, middle), remove (end, middle) */
|
|
|
|
for (k = line; k > 0; k--)
|
|
|
|
tty_puts(line_up); /* redraw characters until changed data */
|
|
|
|
|
2019-11-19 20:52:22 +08:00
|
|
|
tty_puts(rl_prompt);
|
|
|
|
}
|
2019-11-28 13:58:37 +08:00
|
|
|
} else if (n == 0) {
|
2019-11-19 20:52:22 +08:00
|
|
|
tty_puts(rl_prompt);
|
|
|
|
}
|
|
|
|
|
2019-11-28 13:59:53 +08:00
|
|
|
for (; i < rl_point; i++) {
|
2010-07-30 08:03:46 +08:00
|
|
|
tty_show(rl_line_buffer[i]);
|
2019-11-28 13:58:37 +08:00
|
|
|
|
|
|
|
/* move to the next line */
|
|
|
|
if ((i + prompt_len + 1) % tty_cols == 0)
|
2019-11-19 20:52:22 +08:00
|
|
|
tty_put('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void itoa(int val, char* buf)
|
|
|
|
{
|
|
|
|
int digits = 0;
|
|
|
|
int temp = val;
|
2019-11-28 13:58:37 +08:00
|
|
|
|
2019-11-19 20:52:22 +08:00
|
|
|
while (temp != 0) {
|
|
|
|
temp = temp / 10;
|
|
|
|
++digits;
|
|
|
|
}
|
2019-11-28 13:58:37 +08:00
|
|
|
|
2019-11-19 20:52:22 +08:00
|
|
|
*(buf+digits) = 'C';
|
|
|
|
int i = 5; digits--;
|
|
|
|
for(; val && i ; --i, val /= 10) {
|
|
|
|
*(buf+digits) = "0123456789"[val % 10];
|
|
|
|
digits--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void move_cursor_forward(int columns)
|
|
|
|
{
|
|
|
|
const char* line_forward = "\x1b[";
|
2019-11-28 13:58:37 +08:00
|
|
|
char buf[12] = {0};
|
|
|
|
|
2019-11-19 20:52:22 +08:00
|
|
|
strcpy(buf, line_forward);
|
2019-11-28 13:58:37 +08:00
|
|
|
itoa(columns, buf + 2);
|
2019-11-19 20:52:22 +08:00
|
|
|
tty_puts(buf);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static void left(el_status_t Change)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-07-20 06:18:20 +08:00
|
|
|
if (rl_point) {
|
2019-11-19 20:52:22 +08:00
|
|
|
if ((rl_point + prompt_len) % tty_cols == 0) {
|
|
|
|
tty_puts(line_up);
|
|
|
|
move_cursor_forward(tty_cols);
|
|
|
|
} else {
|
|
|
|
tty_back();
|
|
|
|
}
|
2019-11-28 13:58:37 +08:00
|
|
|
|
2019-11-19 20:52:22 +08:00
|
|
|
if (ISMETA(rl_line_buffer[rl_point - 1])) {
|
|
|
|
if (rl_meta_chars) {
|
|
|
|
tty_back();
|
|
|
|
tty_back();
|
|
|
|
}
|
|
|
|
} else if (ISCTL(rl_line_buffer[rl_point - 1])) {
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_back();
|
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
if (Change == CSmove)
|
2010-07-20 06:18:20 +08:00
|
|
|
rl_point--;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static void right(el_status_t Change)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2019-11-19 20:52:22 +08:00
|
|
|
if ((rl_point + prompt_len + 1) % tty_cols == 0)
|
|
|
|
tty_put('\n');
|
|
|
|
else
|
|
|
|
tty_show(rl_line_buffer[rl_point]);
|
2010-07-30 08:03:46 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
if (Change == CSmove)
|
2010-07-20 06:18:20 +08:00
|
|
|
rl_point++;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2010-08-05 18:48:51 +08:00
|
|
|
el_status_t el_ring_bell(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_put('\07');
|
|
|
|
tty_flush();
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t do_macro(int c)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-07-18 07:41:18 +08:00
|
|
|
char name[4];
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
name[0] = '_';
|
|
|
|
name[1] = c;
|
|
|
|
name[2] = '_';
|
|
|
|
name[3] = '\0';
|
|
|
|
|
2010-07-26 04:14:14 +08:00
|
|
|
if ((el_input = (char *)getenv((char *)name)) == NULL) {
|
|
|
|
el_input = NILSTR;
|
2010-08-05 18:48:51 +08:00
|
|
|
return el_ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2010-07-26 07:44:18 +08:00
|
|
|
/* Skip forward to start of next word. If @move is set we also move the cursor. */
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t do_forward(el_status_t move)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
int i;
|
|
|
|
char *p;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
do {
|
2010-07-20 06:18:20 +08:00
|
|
|
p = &rl_line_buffer[rl_point];
|
2010-07-26 07:44:18 +08:00
|
|
|
|
2019-11-19 20:52:22 +08:00
|
|
|
/* Skip leading whitespace, like FSF Readline */
|
2018-09-16 15:06:36 +08:00
|
|
|
for ( ; rl_point < rl_end && (p[0] == ' ' || !is_alpha_num(p[0])); rl_point++, p++) {
|
|
|
|
if (move == CSmove)
|
|
|
|
right(CSstay);
|
2019-11-19 20:52:22 +08:00
|
|
|
}
|
2018-09-16 15:06:36 +08:00
|
|
|
|
2019-11-19 20:52:22 +08:00
|
|
|
/* Skip to end of word, if inside a word. */
|
2017-11-29 23:40:10 +08:00
|
|
|
for (; rl_point < rl_end && is_alpha_num(p[0]); rl_point++, p++) {
|
2008-12-03 04:58:55 +08:00
|
|
|
if (move == CSmove)
|
|
|
|
right(CSstay);
|
2019-11-19 20:52:22 +08:00
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2019-11-19 20:52:22 +08:00
|
|
|
/* Skip to next word, or skip leading white space if outside a word. */
|
2017-11-29 23:40:10 +08:00
|
|
|
for ( ; rl_point < rl_end && (p[0] == ' ' || !is_alpha_num(p[0])); rl_point++, p++) {
|
2008-12-03 04:58:55 +08:00
|
|
|
if (move == CSmove)
|
|
|
|
right(CSstay);
|
2019-11-19 20:52:22 +08:00
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
if (rl_point == rl_end)
|
2008-12-03 04:58:55 +08:00
|
|
|
break;
|
2008-06-07 18:28:36 +08:00
|
|
|
} while (++i < Repeat);
|
|
|
|
|
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t do_case(el_case_t type)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
int i;
|
|
|
|
int end;
|
|
|
|
int count;
|
|
|
|
char *p;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-18 07:41:18 +08:00
|
|
|
do_forward(CSstay);
|
2010-07-26 04:14:14 +08:00
|
|
|
if (old_point != rl_point) {
|
|
|
|
if ((count = rl_point - old_point) < 0)
|
2008-12-03 04:58:55 +08:00
|
|
|
count = -count;
|
2010-07-26 07:44:18 +08:00
|
|
|
|
2010-07-26 04:14:14 +08:00
|
|
|
rl_point = old_point;
|
2010-07-20 06:18:20 +08:00
|
|
|
if ((end = rl_point + count) > rl_end)
|
|
|
|
end = rl_end;
|
2010-07-26 07:44:18 +08:00
|
|
|
|
|
|
|
for (i = rl_point, p = &rl_line_buffer[i]; rl_point < end; p++) {
|
2019-11-19 20:52:22 +08:00
|
|
|
if ((type == TOupper) || (type == TOcapitalize && rl_point == i)) {
|
2008-12-03 04:58:55 +08:00
|
|
|
if (islower(*p))
|
|
|
|
*p = toupper(*p);
|
2010-07-26 07:44:18 +08:00
|
|
|
} else if (isupper(*p)) {
|
2008-12-03 04:58:55 +08:00
|
|
|
*p = tolower(*p);
|
2010-07-18 07:41:18 +08:00
|
|
|
}
|
2008-12-03 04:58:55 +08:00
|
|
|
right(CSmove);
|
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2010-07-26 07:44:18 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t case_down_word(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
return do_case(TOlower);
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t case_up_word(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
return do_case(TOupper);
|
|
|
|
}
|
|
|
|
|
2010-07-26 07:44:18 +08:00
|
|
|
static el_status_t case_cap_word(void)
|
|
|
|
{
|
|
|
|
return do_case(TOcapitalize);
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static void ceol(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2017-11-29 21:53:14 +08:00
|
|
|
int extras = 0;
|
2008-12-03 04:58:55 +08:00
|
|
|
int i;
|
|
|
|
char *p;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2017-11-29 21:53:14 +08:00
|
|
|
while (rl_point < 0) {
|
|
|
|
tty_put(' ');
|
2019-11-19 20:52:22 +08:00
|
|
|
rl_point++;
|
2017-11-29 21:53:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = rl_point, p = &rl_line_buffer[i]; i <= rl_end; i++, p++) {
|
2019-11-19 20:52:22 +08:00
|
|
|
if ((i + prompt_len + 1) % tty_cols == 0){
|
|
|
|
tty_put(' ');
|
|
|
|
tty_put('\n');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tty_put(' ');
|
2010-07-30 08:03:46 +08:00
|
|
|
if (ISMETA(*p)) {
|
2019-11-19 20:52:22 +08:00
|
|
|
if (rl_meta_chars) {
|
|
|
|
tty_put(' ');
|
|
|
|
tty_put(' ');
|
|
|
|
extras += 2;
|
|
|
|
}
|
|
|
|
} else if (ISCTL(*p)) {
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_put(' ');
|
|
|
|
extras++;
|
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2019-11-19 20:52:22 +08:00
|
|
|
for (i += extras; i > rl_point; i--) {
|
|
|
|
if ((i + prompt_len) % tty_cols == 0) {
|
|
|
|
tty_puts(line_up);
|
|
|
|
move_cursor_forward(tty_cols);
|
|
|
|
} else {
|
|
|
|
tty_back();
|
|
|
|
}
|
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static void clear_line(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2019-11-19 20:52:22 +08:00
|
|
|
int n = (rl_point + prompt_len) / tty_cols;
|
2010-07-26 04:14:14 +08:00
|
|
|
rl_point = -(int)strlen(rl_prompt);
|
2019-11-19 20:52:22 +08:00
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
for(int k = 0; k < n; k++)
|
|
|
|
tty_puts(line_up);
|
|
|
|
tty_put('\r');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tty_put('\r');
|
|
|
|
}
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
ceol();
|
2019-11-19 20:52:22 +08:00
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
rl_point = 0;
|
|
|
|
rl_end = 0;
|
|
|
|
rl_line_buffer[0] = '\0';
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t insert_string(const char *p)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-07-24 09:12:45 +08:00
|
|
|
size_t len;
|
2008-12-03 04:58:55 +08:00
|
|
|
int i;
|
2014-11-05 06:19:39 +08:00
|
|
|
char *line;
|
2008-12-03 04:58:55 +08:00
|
|
|
char *q;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-24 08:39:45 +08:00
|
|
|
len = strlen(p);
|
2010-07-20 06:18:20 +08:00
|
|
|
if (rl_end + len >= Length) {
|
2019-11-19 20:52:22 +08:00
|
|
|
line = malloc(sizeof(char) * (Length + len + MEM_INC));
|
2014-11-05 06:19:39 +08:00
|
|
|
if (!line)
|
2008-12-03 04:58:55 +08:00
|
|
|
return CSstay;
|
2014-11-05 06:19:39 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
if (Length) {
|
2014-11-05 06:19:39 +08:00
|
|
|
memcpy(line, rl_line_buffer, Length);
|
2010-07-23 17:01:51 +08:00
|
|
|
free(rl_line_buffer);
|
2008-12-03 04:58:55 +08:00
|
|
|
}
|
2014-11-05 06:19:39 +08:00
|
|
|
|
|
|
|
rl_line_buffer = line;
|
2008-12-03 04:58:55 +08:00
|
|
|
Length += len + MEM_INC;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
for (q = &rl_line_buffer[rl_point], i = rl_end - rl_point; --i >= 0; )
|
2008-12-03 04:58:55 +08:00
|
|
|
q[len + i] = q[i];
|
2014-11-05 06:19:39 +08:00
|
|
|
|
2010-07-23 17:01:51 +08:00
|
|
|
memcpy(&rl_line_buffer[rl_point], p, len);
|
2010-07-20 06:18:20 +08:00
|
|
|
rl_end += len;
|
|
|
|
rl_line_buffer[rl_end] = '\0';
|
|
|
|
tty_string(&rl_line_buffer[rl_point]);
|
|
|
|
rl_point += len;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
return rl_point == rl_end ? CSstay : CSmove;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2017-12-03 04:13:39 +08:00
|
|
|
int rl_insert_text(const char *text)
|
|
|
|
{
|
|
|
|
int mark = rl_point;
|
|
|
|
|
|
|
|
insert_string(text);
|
|
|
|
ceol();
|
|
|
|
|
|
|
|
return rl_point - mark;
|
|
|
|
}
|
|
|
|
|
2019-05-08 22:55:33 +08:00
|
|
|
static el_status_t redisplay(int cls)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2019-11-19 20:52:22 +08:00
|
|
|
if (cls)
|
|
|
|
tty_puts(CLEAR);
|
2019-11-28 13:58:37 +08:00
|
|
|
else
|
2019-11-19 20:52:22 +08:00
|
|
|
tty_puts("\r\e[K");
|
2019-05-06 06:54:57 +08:00
|
|
|
|
2010-07-26 04:14:14 +08:00
|
|
|
tty_puts(rl_prompt);
|
2019-11-19 20:52:22 +08:00
|
|
|
rl_point = 0;
|
2010-07-20 06:18:20 +08:00
|
|
|
tty_string(rl_line_buffer);
|
2019-11-19 20:52:22 +08:00
|
|
|
rl_point = rl_end;
|
2008-06-07 18:28:36 +08:00
|
|
|
return CSmove;
|
|
|
|
}
|
|
|
|
|
2019-05-08 22:55:33 +08:00
|
|
|
static el_status_t refresh(void)
|
|
|
|
{
|
|
|
|
return redisplay(1);
|
|
|
|
}
|
|
|
|
|
2017-12-03 04:14:14 +08:00
|
|
|
int rl_refresh_line(int ignore1 __attribute__((unused)), int ignore2 __attribute__((unused)))
|
|
|
|
{
|
2019-05-08 22:55:33 +08:00
|
|
|
redisplay(0);
|
2017-12-03 04:14:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t toggle_meta_mode(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
rl_meta_chars = ! rl_meta_chars;
|
2019-05-08 22:55:33 +08:00
|
|
|
return redisplay(0);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2018-04-01 22:10:10 +08:00
|
|
|
const char *el_next_hist(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
return H.Pos >= H.Size - 1 ? NULL : H.Lines[++H.Pos];
|
|
|
|
}
|
|
|
|
|
2018-04-01 22:10:10 +08:00
|
|
|
const char *el_prev_hist(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
return H.Pos == 0 ? NULL : H.Lines[--H.Pos];
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t do_insert_hist(const char *p)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
if (p == NULL)
|
2010-08-05 18:48:51 +08:00
|
|
|
return el_ring_bell();
|
2010-07-30 08:03:46 +08:00
|
|
|
|
2017-12-11 15:43:43 +08:00
|
|
|
clear_line();
|
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
rl_point = 0;
|
2019-11-19 20:52:22 +08:00
|
|
|
reposition(-1);
|
2010-07-20 06:18:20 +08:00
|
|
|
rl_end = 0;
|
2010-07-30 08:03:46 +08:00
|
|
|
|
2017-12-11 15:43:43 +08:00
|
|
|
return insert_string(p);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2010-07-18 07:41:18 +08:00
|
|
|
static el_status_t do_hist(const char *(*move)(void))
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
const char *p;
|
2017-11-29 23:40:10 +08:00
|
|
|
int i = 0;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
do {
|
2010-07-26 07:44:18 +08:00
|
|
|
if ((p = move()) == NULL)
|
2010-08-05 18:48:51 +08:00
|
|
|
return el_ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
} while (++i < Repeat);
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return do_insert_hist(p);
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t h_next(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2014-11-05 05:47:50 +08:00
|
|
|
if (el_no_hist)
|
2019-11-20 19:46:39 +08:00
|
|
|
return CSstay;
|
2014-11-05 05:47:50 +08:00
|
|
|
|
2018-04-01 22:10:10 +08:00
|
|
|
return do_hist(el_next_hist);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t h_prev(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2014-11-05 05:47:50 +08:00
|
|
|
if (el_no_hist)
|
2019-11-20 19:46:39 +08:00
|
|
|
return CSstay;
|
2014-11-05 05:47:50 +08:00
|
|
|
|
2018-04-01 22:10:10 +08:00
|
|
|
return do_hist(el_prev_hist);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t h_first(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
return do_insert_hist(H.Lines[H.Pos = 0]);
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t h_last(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
return do_insert_hist(H.Lines[H.Pos = H.Size - 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return zero if pat appears as a substring in text.
|
|
|
|
*/
|
2010-07-18 04:38:05 +08:00
|
|
|
static int substrcmp(const char *text, const char *pat, size_t len)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-07-23 17:01:51 +08:00
|
|
|
char c;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
if ((c = *pat) == '\0')
|
|
|
|
return *text == '\0';
|
2017-11-29 23:40:10 +08:00
|
|
|
|
|
|
|
for ( ; *text; text++) {
|
2008-06-07 18:28:36 +08:00
|
|
|
if (*text == c && strncmp(text, pat, len) == 0)
|
|
|
|
return 0;
|
2017-11-29 23:40:10 +08:00
|
|
|
}
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-07-18 07:41:18 +08:00
|
|
|
static const char *search_hist(const char *search, const char *(*move)(void))
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
int len;
|
|
|
|
int pos;
|
2010-07-18 04:38:05 +08:00
|
|
|
int (*match)(const char *s1, const char *s2, size_t n);
|
2010-07-24 08:39:45 +08:00
|
|
|
const char *pat;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
/* Save or get remembered search pattern. */
|
|
|
|
if (search && *search) {
|
2008-12-03 04:58:55 +08:00
|
|
|
if (old_search)
|
2010-07-23 17:01:51 +08:00
|
|
|
free(old_search);
|
2010-07-24 08:39:45 +08:00
|
|
|
old_search = strdup(search);
|
2010-07-26 08:03:31 +08:00
|
|
|
} else {
|
2008-12-03 04:58:55 +08:00
|
|
|
if (old_search == NULL || *old_search == '\0')
|
2008-06-07 18:28:36 +08:00
|
|
|
return NULL;
|
2008-12-03 04:58:55 +08:00
|
|
|
search = old_search;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up pattern-finder. */
|
|
|
|
if (*search == '^') {
|
2008-12-03 04:58:55 +08:00
|
|
|
match = strncmp;
|
2010-07-24 08:39:45 +08:00
|
|
|
pat = search + 1;
|
2010-07-26 08:03:31 +08:00
|
|
|
} else {
|
2008-12-03 04:58:55 +08:00
|
|
|
match = substrcmp;
|
2010-07-24 08:39:45 +08:00
|
|
|
pat = search;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
len = strlen(pat);
|
|
|
|
|
2010-07-26 08:03:31 +08:00
|
|
|
pos = H.Pos; /* Save H.Pos */
|
|
|
|
while (move()) {
|
2010-07-24 08:39:45 +08:00
|
|
|
if (match(H.Lines[H.Pos], pat, len) == 0)
|
2008-06-07 18:28:36 +08:00
|
|
|
return H.Lines[H.Pos];
|
2010-07-26 08:03:31 +08:00
|
|
|
}
|
|
|
|
H.Pos = pos; /* Restore H.Pos */
|
2010-07-23 17:01:51 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-11 15:43:43 +08:00
|
|
|
static el_status_t h_search_end(const char *p)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-07-26 04:14:14 +08:00
|
|
|
rl_prompt = old_prompt;
|
2008-06-07 18:28:36 +08:00
|
|
|
Searching = 0;
|
2017-11-29 22:01:22 +08:00
|
|
|
|
2019-05-08 19:47:47 +08:00
|
|
|
if (el_intr_pending > 0) {
|
2010-07-24 09:46:58 +08:00
|
|
|
el_intr_pending = 0;
|
2008-12-03 04:58:55 +08:00
|
|
|
clear_line();
|
2019-05-08 22:55:33 +08:00
|
|
|
return redisplay(0);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2017-11-29 22:01:22 +08:00
|
|
|
|
2017-12-11 15:43:43 +08:00
|
|
|
p = search_hist(p, search_move);
|
2008-06-07 18:28:36 +08:00
|
|
|
if (p == NULL) {
|
2010-08-05 18:48:51 +08:00
|
|
|
el_ring_bell();
|
2019-05-08 22:57:40 +08:00
|
|
|
clear_line();
|
2019-05-08 22:55:33 +08:00
|
|
|
return redisplay(0);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2017-11-29 22:01:22 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return do_insert_hist(p);
|
|
|
|
}
|
|
|
|
|
2017-12-11 15:43:43 +08:00
|
|
|
static el_status_t h_search(void)
|
|
|
|
{
|
|
|
|
if (Searching)
|
|
|
|
return el_ring_bell();
|
|
|
|
Searching = 1;
|
|
|
|
|
|
|
|
clear_line();
|
|
|
|
old_prompt = rl_prompt;
|
|
|
|
rl_prompt = "Search: ";
|
|
|
|
tty_puts(rl_prompt);
|
|
|
|
|
2018-04-01 22:10:10 +08:00
|
|
|
search_move = Repeat == NO_ARG ? el_prev_hist : el_next_hist;
|
2017-12-11 15:43:43 +08:00
|
|
|
if (line_handler) {
|
2019-11-20 19:46:39 +08:00
|
|
|
editinput(0);
|
|
|
|
return CSstay;
|
2017-12-11 15:43:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return h_search_end(editinput(1));
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t fd_char(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2017-11-29 23:40:10 +08:00
|
|
|
int i = 0;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
do {
|
2010-07-20 06:18:20 +08:00
|
|
|
if (rl_point >= rl_end)
|
2008-12-03 04:58:55 +08:00
|
|
|
break;
|
|
|
|
right(CSmove);
|
2008-06-07 18:28:36 +08:00
|
|
|
} while (++i < Repeat);
|
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static void save_yank(int begin, int i)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
if (Yanked) {
|
2010-07-23 17:01:51 +08:00
|
|
|
free(Yanked);
|
2008-12-03 04:58:55 +08:00
|
|
|
Yanked = NULL;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i < 1)
|
2008-12-03 04:58:55 +08:00
|
|
|
return;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-23 17:01:51 +08:00
|
|
|
Yanked = malloc(sizeof(char) * (i + 1));
|
|
|
|
if (Yanked) {
|
|
|
|
memcpy(Yanked, &rl_line_buffer[begin], i);
|
2008-12-03 04:58:55 +08:00
|
|
|
Yanked[i] = '\0';
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t delete_string(int count)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2017-11-29 23:40:10 +08:00
|
|
|
int i;
|
|
|
|
char *p;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
if (count <= 0 || rl_end == rl_point)
|
2010-08-05 18:48:51 +08:00
|
|
|
return el_ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
if (count == 1 && rl_point == rl_end - 1) {
|
2008-12-03 04:58:55 +08:00
|
|
|
/* Optimize common case of delete at end of line. */
|
2010-07-20 06:18:20 +08:00
|
|
|
rl_end--;
|
|
|
|
p = &rl_line_buffer[rl_point];
|
2008-12-03 04:58:55 +08:00
|
|
|
i = 1;
|
|
|
|
tty_put(' ');
|
|
|
|
if (ISCTL(*p)) {
|
|
|
|
i = 2;
|
|
|
|
tty_put(' ');
|
2010-07-26 08:03:31 +08:00
|
|
|
} else if (rl_meta_chars && ISMETA(*p)) {
|
2008-12-03 04:58:55 +08:00
|
|
|
i = 3;
|
|
|
|
tty_put(' ');
|
|
|
|
tty_put(' ');
|
|
|
|
}
|
|
|
|
tty_backn(i);
|
|
|
|
*p = '\0';
|
|
|
|
return CSmove;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
if (rl_point + count > rl_end && (count = rl_end - rl_point) <= 0)
|
2008-12-03 04:58:55 +08:00
|
|
|
return CSstay;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
if (count > 1)
|
2010-07-20 06:18:20 +08:00
|
|
|
save_yank(rl_point, count);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
for (p = &rl_line_buffer[rl_point], i = rl_end - (rl_point + count) + 1; --i >= 0; p++)
|
2008-12-03 04:58:55 +08:00
|
|
|
p[0] = p[count];
|
2008-06-07 18:28:36 +08:00
|
|
|
ceol();
|
2019-11-19 20:52:22 +08:00
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
rl_end -= count;
|
|
|
|
tty_string(&rl_line_buffer[rl_point]);
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return CSmove;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t bk_char(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2017-11-29 23:40:10 +08:00
|
|
|
int i = 0;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
do {
|
2010-07-20 06:18:20 +08:00
|
|
|
if (rl_point == 0)
|
2008-12-03 04:58:55 +08:00
|
|
|
break;
|
|
|
|
left(CSmove);
|
2008-06-07 18:28:36 +08:00
|
|
|
} while (++i < Repeat);
|
|
|
|
|
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t bk_del_char(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2017-11-29 23:40:10 +08:00
|
|
|
int i = 0;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
do {
|
2010-07-20 06:18:20 +08:00
|
|
|
if (rl_point == 0)
|
2008-12-03 04:58:55 +08:00
|
|
|
break;
|
|
|
|
left(CSmove);
|
2008-06-07 18:28:36 +08:00
|
|
|
} while (++i < Repeat);
|
|
|
|
|
|
|
|
return delete_string(i);
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t kill_line(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2017-11-29 23:40:10 +08:00
|
|
|
int i;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
if (Repeat != NO_ARG) {
|
2010-07-20 06:18:20 +08:00
|
|
|
if (Repeat < rl_point) {
|
|
|
|
i = rl_point;
|
|
|
|
rl_point = Repeat;
|
2019-11-19 20:52:22 +08:00
|
|
|
reposition(-1);
|
2010-07-20 06:18:20 +08:00
|
|
|
delete_string(i - rl_point);
|
2010-07-26 08:03:31 +08:00
|
|
|
} else if (Repeat > rl_point) {
|
2008-12-03 04:58:55 +08:00
|
|
|
right(CSmove);
|
2010-07-20 06:18:20 +08:00
|
|
|
delete_string(Repeat - rl_point - 1);
|
2008-12-03 04:58:55 +08:00
|
|
|
}
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
return CSmove;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
save_yank(rl_point, rl_end - rl_point);
|
|
|
|
rl_line_buffer[rl_point] = '\0';
|
2008-06-07 18:28:36 +08:00
|
|
|
ceol();
|
2010-07-20 06:18:20 +08:00
|
|
|
rl_end = rl_point;
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t insert_char(int c)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
el_status_t s;
|
|
|
|
char buff[2];
|
|
|
|
char *p;
|
|
|
|
char *q;
|
|
|
|
int i;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
if (Repeat == NO_ARG || Repeat < 2) {
|
2008-12-03 04:58:55 +08:00
|
|
|
buff[0] = c;
|
|
|
|
buff[1] = '\0';
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
return insert_string(buff);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2010-07-23 17:01:51 +08:00
|
|
|
p = malloc(sizeof(char) * (Repeat + 1));
|
|
|
|
if (!p)
|
2008-12-03 04:58:55 +08:00
|
|
|
return CSstay;
|
2010-07-23 17:01:51 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
for (i = Repeat, q = p; --i >= 0; )
|
2008-12-03 04:58:55 +08:00
|
|
|
*q++ = c;
|
2008-06-07 18:28:36 +08:00
|
|
|
*q = '\0';
|
|
|
|
Repeat = 0;
|
|
|
|
s = insert_string(p);
|
2010-07-23 17:01:51 +08:00
|
|
|
free(p);
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-02-09 05:09:02 +08:00
|
|
|
static el_status_t beg_line(void)
|
|
|
|
{
|
2010-07-20 06:18:20 +08:00
|
|
|
if (rl_point) {
|
|
|
|
rl_point = 0;
|
2009-02-09 05:09:02 +08:00
|
|
|
return CSmove;
|
|
|
|
}
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2009-02-09 05:09:02 +08:00
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
|
|
|
static el_status_t end_line(void)
|
|
|
|
{
|
2010-07-20 06:18:20 +08:00
|
|
|
if (rl_point != rl_end) {
|
|
|
|
rl_point = rl_end;
|
2009-02-09 05:09:02 +08:00
|
|
|
return CSmove;
|
|
|
|
}
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2009-02-09 05:09:02 +08:00
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
|
|
|
static el_status_t del_char(void)
|
|
|
|
{
|
2019-05-06 06:54:17 +08:00
|
|
|
return delete_string(Repeat == NO_ARG ? CSeof : Repeat);
|
2009-02-09 05:09:02 +08:00
|
|
|
}
|
|
|
|
|
2013-07-07 17:44:34 +08:00
|
|
|
el_status_t el_del_char(void)
|
|
|
|
{
|
|
|
|
return del_char();
|
|
|
|
}
|
|
|
|
|
2018-03-22 15:38:04 +08:00
|
|
|
static el_status_t fd_word(void)
|
|
|
|
{
|
|
|
|
return do_forward(CSmove);
|
|
|
|
}
|
|
|
|
|
|
|
|
static el_status_t bk_word(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
for (p = &rl_line_buffer[rl_point]; p > rl_line_buffer && !is_alpha_num(p[-1]); p--)
|
|
|
|
left(CSmove);
|
|
|
|
|
|
|
|
for (; p > rl_line_buffer && !isblank(p[-1]) && is_alpha_num(p[-1]); p--)
|
|
|
|
left(CSmove);
|
|
|
|
|
|
|
|
if (rl_point == 0)
|
|
|
|
break;
|
|
|
|
} while (++i < Repeat);
|
|
|
|
|
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t meta(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
int c;
|
2017-11-29 23:40:10 +08:00
|
|
|
el_keymap_t *kp;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
if ((c = tty_get()) == EOF)
|
|
|
|
return CSeof;
|
2010-07-24 08:52:51 +08:00
|
|
|
|
2010-07-18 07:41:18 +08:00
|
|
|
#ifdef CONFIG_ANSI_ARROWS
|
2008-06-07 18:28:36 +08:00
|
|
|
/* Also include VT-100 arrows. */
|
2009-02-09 05:09:02 +08:00
|
|
|
if (c == '[' || c == 'O') {
|
2010-07-30 08:03:46 +08:00
|
|
|
switch (tty_get()) {
|
2019-11-20 19:46:39 +08:00
|
|
|
case EOF: return CSeof;
|
|
|
|
case '1':
|
|
|
|
{
|
|
|
|
char seq[4] = { 0 };
|
|
|
|
|
|
|
|
for (c = 0; c < 3; c++)
|
|
|
|
seq[c] = tty_get();
|
|
|
|
|
|
|
|
if (!strncmp(seq, ";5C", 3))
|
|
|
|
return fd_word(); /* Ctrl+Right */
|
|
|
|
if (!strncmp(seq, ";5D", 3))
|
|
|
|
return bk_word(); /* Ctrl+Left */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '2': tty_get(); return CSstay; /* Insert */
|
|
|
|
case '3': tty_get(); return del_char(); /* Delete */
|
|
|
|
case '5': tty_get(); return CSstay; /* PgUp */
|
|
|
|
case '6': tty_get(); return CSstay; /* PgDn */
|
|
|
|
case 'A': return h_prev(); /* Up */
|
|
|
|
case 'B': return h_next(); /* Down */
|
|
|
|
case 'C': return fd_char(); /* Left */
|
|
|
|
case 'D': return bk_char(); /* Right */
|
|
|
|
case 'F': return end_line(); /* End */
|
|
|
|
case 'H': return beg_line(); /* Home */
|
|
|
|
default: /* Fall through */
|
|
|
|
break;
|
2008-12-03 04:58:55 +08:00
|
|
|
}
|
2010-07-24 08:52:51 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
return el_ring_bell();
|
2009-02-09 05:09:02 +08:00
|
|
|
}
|
2010-07-18 07:41:18 +08:00
|
|
|
#endif /* CONFIG_ANSI_ARROWS */
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
if (isdigit(c)) {
|
2008-12-03 04:58:55 +08:00
|
|
|
for (Repeat = c - '0'; (c = tty_get()) != EOF && isdigit(c); )
|
|
|
|
Repeat = Repeat * 10 + c - '0';
|
2019-11-20 19:46:39 +08:00
|
|
|
tty_push(c);
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
return CSstay;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isupper(c))
|
2008-12-03 04:58:55 +08:00
|
|
|
return do_macro(c);
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2010-07-30 08:03:46 +08:00
|
|
|
for (kp = MetaMap; kp->Function; kp++) {
|
2008-12-03 04:58:55 +08:00
|
|
|
if (kp->Key == c)
|
2010-07-26 07:44:18 +08:00
|
|
|
return kp->Function();
|
2010-07-30 08:03:46 +08:00
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-08-05 18:48:51 +08:00
|
|
|
return el_ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t emacs(int c)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-07-26 04:01:04 +08:00
|
|
|
el_status_t s;
|
|
|
|
el_keymap_t *kp;
|
|
|
|
|
|
|
|
/* Save point before interpreting input character 'c'. */
|
2010-07-26 04:14:14 +08:00
|
|
|
old_point = rl_point;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
if (rl_meta_chars && ISMETA(c)) {
|
2019-11-20 19:46:39 +08:00
|
|
|
tty_push(UNMETA(c));
|
2008-12-03 04:58:55 +08:00
|
|
|
return meta();
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2010-07-26 04:01:04 +08:00
|
|
|
|
|
|
|
for (kp = Map; kp->Function; kp++) {
|
2008-12-03 04:58:55 +08:00
|
|
|
if (kp->Key == c)
|
|
|
|
break;
|
2010-07-26 04:01:04 +08:00
|
|
|
}
|
2010-08-11 19:14:32 +08:00
|
|
|
|
|
|
|
if (kp->Function) {
|
2019-11-20 19:46:39 +08:00
|
|
|
s = kp->Function();
|
|
|
|
if (s == CSdispatch) /* If Function is inhibited. */
|
|
|
|
s = insert_char(c);
|
2010-08-11 19:14:32 +08:00
|
|
|
} else {
|
2019-11-20 19:46:39 +08:00
|
|
|
s = insert_char(c);
|
2010-08-11 19:14:32 +08:00
|
|
|
}
|
|
|
|
|
2010-07-26 04:14:14 +08:00
|
|
|
if (!el_pushed) {
|
2008-12-03 04:58:55 +08:00
|
|
|
/* No pushback means no repeat count; hacky, but true. */
|
|
|
|
Repeat = NO_ARG;
|
2010-07-26 04:01:04 +08:00
|
|
|
}
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t tty_special(int c)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2015-04-06 20:45:07 +08:00
|
|
|
#ifdef CONFIG_SIGINT
|
2013-07-07 17:40:23 +08:00
|
|
|
if (c == rl_intr) {
|
2019-05-08 23:10:21 +08:00
|
|
|
el_intr_pending = SIGINT;
|
2013-07-07 17:40:23 +08:00
|
|
|
return CSsignal;
|
|
|
|
}
|
2015-04-06 20:45:07 +08:00
|
|
|
#endif
|
2013-07-07 17:40:23 +08:00
|
|
|
if (c == rl_quit) {
|
|
|
|
el_intr_pending = SIGQUIT;
|
|
|
|
return CSeof;
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_SIGSTOP
|
|
|
|
if (c == rl_susp) {
|
|
|
|
el_intr_pending = SIGTSTP;
|
|
|
|
return CSsignal;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-11-29 23:40:10 +08:00
|
|
|
if (rl_meta_chars && ISMETA(c))
|
2008-12-03 04:58:55 +08:00
|
|
|
return CSdispatch;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
if (c == rl_erase || c == DEL)
|
2008-12-03 04:58:55 +08:00
|
|
|
return bk_del_char();
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
if (c == rl_kill) {
|
2010-07-20 06:18:20 +08:00
|
|
|
if (rl_point != 0) {
|
2019-11-19 20:52:22 +08:00
|
|
|
old_point = rl_point;
|
2010-07-20 06:18:20 +08:00
|
|
|
rl_point = 0;
|
2019-11-19 20:52:22 +08:00
|
|
|
reposition(c);
|
2008-12-03 04:58:55 +08:00
|
|
|
}
|
|
|
|
Repeat = NO_ARG;
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
return kill_line();
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2013-07-07 17:40:23 +08:00
|
|
|
|
2015-04-06 20:45:07 +08:00
|
|
|
#ifdef CONFIG_EOF
|
2010-07-20 06:18:20 +08:00
|
|
|
if (c == rl_eof && rl_point == 0 && rl_end == 0)
|
2008-12-03 04:58:55 +08:00
|
|
|
return CSeof;
|
2015-04-06 20:45:07 +08:00
|
|
|
#endif
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
return CSdispatch;
|
|
|
|
}
|
|
|
|
|
2017-12-11 15:43:43 +08:00
|
|
|
static char *editinput(int complete)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
int c;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2017-12-11 15:43:43 +08:00
|
|
|
do {
|
2019-11-20 19:46:39 +08:00
|
|
|
c = tty_get();
|
|
|
|
if (c == EOF)
|
|
|
|
break;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
switch (tty_special(c)) {
|
2019-11-20 19:46:39 +08:00
|
|
|
case CSdone:
|
|
|
|
return rl_line_buffer;
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
case CSeof:
|
|
|
|
return NULL;
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
case CSsignal:
|
|
|
|
return (char *)"";
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
case CSmove:
|
|
|
|
reposition(c);
|
|
|
|
break;
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
case CSdispatch:
|
|
|
|
switch (emacs(c)) {
|
|
|
|
case CSdone:
|
|
|
|
return rl_line_buffer;
|
2010-07-26 04:14:14 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
case CSeof:
|
|
|
|
return NULL;
|
2010-07-26 04:14:14 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
case CSsignal:
|
|
|
|
return (char *)"";
|
2010-07-26 04:14:14 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
case CSmove:
|
|
|
|
reposition(c);
|
|
|
|
break;
|
2010-07-26 04:14:14 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
case CSdispatch:
|
|
|
|
case CSstay:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
case CSstay:
|
|
|
|
break;
|
2008-12-03 04:58:55 +08:00
|
|
|
}
|
2017-12-11 15:43:43 +08:00
|
|
|
} while (complete);
|
2017-11-29 22:01:22 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-08-04 08:12:19 +08:00
|
|
|
static void hist_alloc(void)
|
|
|
|
{
|
|
|
|
if (!H.Lines)
|
2019-11-20 19:46:39 +08:00
|
|
|
H.Lines = calloc(el_hist_size, sizeof(char *));
|
2010-08-04 08:12:19 +08:00
|
|
|
}
|
|
|
|
|
2010-08-04 03:01:01 +08:00
|
|
|
static void hist_add(const char *p)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
int i;
|
2010-08-04 03:01:01 +08:00
|
|
|
char *s;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-08-04 03:01:01 +08:00
|
|
|
#ifdef CONFIG_UNIQUE_HISTORY
|
2019-05-03 22:54:25 +08:00
|
|
|
if (H.Size && strcmp(p, H.Lines[H.Size - 1]) == 0)
|
2010-08-04 03:01:01 +08:00
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2017-11-30 04:40:38 +08:00
|
|
|
s = strdup(p);
|
|
|
|
if (s == NULL)
|
2008-12-03 04:58:55 +08:00
|
|
|
return;
|
2017-11-29 22:01:22 +08:00
|
|
|
|
2010-08-04 08:12:19 +08:00
|
|
|
if (H.Size < el_hist_size) {
|
2010-08-04 03:01:01 +08:00
|
|
|
H.Lines[H.Size++] = s;
|
2010-07-26 08:03:31 +08:00
|
|
|
} else {
|
2010-07-23 17:01:51 +08:00
|
|
|
free(H.Lines[0]);
|
2010-08-04 08:12:19 +08:00
|
|
|
for (i = 0; i < el_hist_size - 1; i++)
|
2008-12-03 04:58:55 +08:00
|
|
|
H.Lines[i] = H.Lines[i + 1];
|
2010-08-04 03:01:01 +08:00
|
|
|
H.Lines[i] = s;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
H.Pos = H.Size - 1;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static char *read_redirected(void)
|
2008-06-10 03:37:01 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
int size = MEM_INC;
|
|
|
|
char *p;
|
|
|
|
char *line;
|
|
|
|
char *end;
|
2008-06-10 03:37:01 +08:00
|
|
|
|
2010-07-23 17:01:51 +08:00
|
|
|
p = line = malloc(sizeof(char) * size);
|
|
|
|
if (!p)
|
2019-11-20 19:46:39 +08:00
|
|
|
return NULL;
|
2010-07-23 17:01:51 +08:00
|
|
|
|
2008-06-10 04:55:13 +08:00
|
|
|
end = p + size;
|
|
|
|
while (1) {
|
2008-12-03 04:58:55 +08:00
|
|
|
if (p == end) {
|
2008-06-10 04:55:13 +08:00
|
|
|
int oldpos = end - line;
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
size += MEM_INC;
|
2010-07-23 17:01:51 +08:00
|
|
|
p = line = realloc(line, sizeof(char) * size);
|
2019-11-20 19:46:39 +08:00
|
|
|
if (!p)
|
|
|
|
return NULL;
|
2008-12-03 04:58:55 +08:00
|
|
|
end = p + size;
|
2008-06-10 04:55:13 +08:00
|
|
|
|
|
|
|
p += oldpos; /* Continue where we left off... */
|
2008-12-03 04:58:55 +08:00
|
|
|
}
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2010-08-05 19:20:35 +08:00
|
|
|
if (read(el_infd, p, 1) <= 0) {
|
2008-12-03 04:58:55 +08:00
|
|
|
/* Ignore "incomplete" lines at EOF, just like we do for a tty. */
|
|
|
|
free(line);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
if (*p == '\n')
|
2008-12-03 04:58:55 +08:00
|
|
|
break;
|
2008-06-10 04:55:13 +08:00
|
|
|
p++;
|
2008-06-10 03:37:01 +08:00
|
|
|
}
|
|
|
|
*p = '\0';
|
2010-07-23 17:01:51 +08:00
|
|
|
|
2008-06-10 03:37:01 +08:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2010-07-18 07:41:18 +08:00
|
|
|
/* For compatibility with FSF readline. */
|
2010-08-05 21:14:06 +08:00
|
|
|
void rl_reset_terminal(const char *terminal_name)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-08-05 21:14:06 +08:00
|
|
|
#ifdef CONFIG_USE_TERMCAP
|
2010-08-12 22:27:10 +08:00
|
|
|
char buf[1024];
|
2010-08-05 21:14:06 +08:00
|
|
|
char *bp;
|
|
|
|
#endif
|
|
|
|
#ifdef TIOCGWINSZ
|
|
|
|
struct winsize W;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (terminal_name) {
|
|
|
|
el_term = terminal_name;
|
2010-08-12 22:20:29 +08:00
|
|
|
} else if ((el_term = getenv("TERM")) == NULL) {
|
|
|
|
el_term = "dumb";
|
2010-08-05 21:14:06 +08:00
|
|
|
}
|
|
|
|
|
2010-08-12 22:20:29 +08:00
|
|
|
/* Initialize to faulty values to trigger fallback if nothing else works. */
|
|
|
|
tty_cols = tty_rows = -1;
|
2010-08-05 21:14:06 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_USE_TERMCAP
|
2010-08-12 22:27:10 +08:00
|
|
|
bp = buf;
|
|
|
|
if (-1 != tgetent(buf, el_term)) {
|
2019-11-20 19:46:39 +08:00
|
|
|
if ((backspace = tgetstr("le", &bp)) != NULL)
|
|
|
|
backspace = strdup(backspace);
|
|
|
|
tty_cols = tgetnum("co");
|
|
|
|
tty_rows = tgetnum("li");
|
2010-08-05 21:14:06 +08:00
|
|
|
}
|
|
|
|
/* Make sure to check width & rows and fallback to TIOCGWINSZ if available. */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (tty_cols <= 0 || tty_rows <= 0) {
|
|
|
|
#ifdef TIOCGWINSZ
|
2019-11-20 19:46:39 +08:00
|
|
|
if (ioctl(el_outfd, TIOCGWINSZ, &W) >= 0 && W.ws_col > 0 && W.ws_row > 0) {
|
|
|
|
tty_cols = (int)W.ws_col;
|
|
|
|
tty_rows = (int)W.ws_row;
|
|
|
|
return;
|
|
|
|
}
|
2010-08-05 21:14:06 +08:00
|
|
|
#endif
|
2019-11-20 19:46:39 +08:00
|
|
|
tty_cols = SCREEN_COLS;
|
|
|
|
tty_rows = SCREEN_ROWS;
|
2010-08-05 21:14:06 +08:00
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
void rl_initialize(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-07-26 04:14:14 +08:00
|
|
|
if (!rl_prompt)
|
2019-11-20 19:46:39 +08:00
|
|
|
rl_prompt = "? ";
|
2010-08-04 08:12:19 +08:00
|
|
|
|
|
|
|
hist_alloc();
|
2010-08-05 19:20:35 +08:00
|
|
|
|
|
|
|
/* Setup I/O descriptors */
|
2010-08-05 21:14:06 +08:00
|
|
|
if (!rl_instream) el_infd = EL_STDIN;
|
2010-08-05 19:20:35 +08:00
|
|
|
else el_infd = fileno(rl_instream);
|
2010-08-05 21:14:06 +08:00
|
|
|
if (el_infd < 0) el_infd = EL_STDIN;
|
|
|
|
if (!rl_outstream) el_outfd = EL_STDOUT;
|
2010-08-05 19:20:35 +08:00
|
|
|
else el_outfd = fileno(rl_outstream);
|
2010-08-05 21:14:06 +08:00
|
|
|
if (el_outfd < 0) el_outfd = EL_STDOUT;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-29 21:42:39 +08:00
|
|
|
void rl_uninitialize(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Uninitialize the history */
|
|
|
|
if (H.Lines) {
|
2019-11-20 19:46:39 +08:00
|
|
|
for (i = 0; i < el_hist_size; i++) {
|
|
|
|
if (H.Lines[i])
|
|
|
|
free(H.Lines[i]);
|
|
|
|
H.Lines[i] = NULL;
|
|
|
|
}
|
|
|
|
free(H.Lines);
|
|
|
|
H.Lines = NULL;
|
2017-11-29 21:42:39 +08:00
|
|
|
}
|
|
|
|
H.Size = 0;
|
|
|
|
H.Pos = 0;
|
|
|
|
|
2017-11-29 22:32:59 +08:00
|
|
|
if (old_search)
|
2019-11-20 19:46:39 +08:00
|
|
|
free(old_search);
|
2017-11-29 22:32:59 +08:00
|
|
|
old_search = NULL;
|
|
|
|
|
2017-11-29 21:42:39 +08:00
|
|
|
/* Uninitialize the line buffer */
|
|
|
|
if (rl_line_buffer)
|
2019-11-20 19:46:39 +08:00
|
|
|
free(rl_line_buffer);
|
2017-11-29 21:42:39 +08:00
|
|
|
rl_line_buffer = NULL;
|
|
|
|
Length = 0;
|
|
|
|
}
|
|
|
|
|
2014-09-17 11:48:56 +08:00
|
|
|
static const char *rl_saved_prompt = NULL;
|
|
|
|
void rl_save_prompt(void)
|
|
|
|
{
|
|
|
|
rl_saved_prompt = rl_prompt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rl_restore_prompt(void)
|
|
|
|
{
|
|
|
|
if (rl_saved_prompt)
|
2019-11-20 19:46:39 +08:00
|
|
|
rl_prompt = rl_saved_prompt;
|
2014-09-17 11:48:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void rl_set_prompt(const char *prompt)
|
|
|
|
{
|
|
|
|
rl_prompt = prompt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rl_clear_message(void)
|
|
|
|
{
|
|
|
|
/* Nothing to do atm. */
|
|
|
|
}
|
|
|
|
|
|
|
|
void rl_forced_update_display()
|
|
|
|
{
|
2019-05-08 22:55:33 +08:00
|
|
|
redisplay(0);
|
2016-01-29 01:16:17 +08:00
|
|
|
tty_flush();
|
2014-09-17 11:48:56 +08:00
|
|
|
}
|
|
|
|
|
2017-12-11 15:43:43 +08:00
|
|
|
static int el_prep(const char *prompt)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-03-10 04:18:03 +08:00
|
|
|
rl_initialize();
|
2008-10-02 15:09:09 +08:00
|
|
|
|
2010-07-23 17:01:51 +08:00
|
|
|
if (!rl_line_buffer) {
|
2008-12-03 04:58:55 +08:00
|
|
|
Length = MEM_INC;
|
2019-11-20 19:46:39 +08:00
|
|
|
rl_line_buffer = malloc(sizeof(char) * Length);
|
2010-07-23 17:01:51 +08:00
|
|
|
if (!rl_line_buffer)
|
2019-11-20 19:46:39 +08:00
|
|
|
return -1;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_info();
|
2010-08-05 07:08:30 +08:00
|
|
|
rl_prep_term_function(!rl_meta_chars);
|
2010-07-26 04:14:14 +08:00
|
|
|
hist_add(NILSTR);
|
2008-06-07 18:28:36 +08:00
|
|
|
ScreenSize = SCREEN_INC;
|
2010-07-23 17:01:51 +08:00
|
|
|
Screen = malloc(sizeof(char) * ScreenSize);
|
|
|
|
if (!Screen)
|
2019-11-20 19:46:39 +08:00
|
|
|
return -1;
|
2010-07-23 17:01:51 +08:00
|
|
|
|
2010-07-26 04:14:14 +08:00
|
|
|
rl_prompt = prompt ? prompt : NILSTR;
|
2019-11-19 20:52:22 +08:00
|
|
|
prompt_len = strlen(rl_prompt);
|
|
|
|
|
2010-07-24 09:38:25 +08:00
|
|
|
if (el_no_echo) {
|
2019-11-20 19:46:39 +08:00
|
|
|
int old = el_no_echo;
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
el_no_echo = 0;
|
|
|
|
tty_puts(rl_prompt);
|
|
|
|
tty_flush();
|
|
|
|
el_no_echo = old;
|
2010-07-24 09:38:25 +08:00
|
|
|
} else {
|
2019-11-20 19:46:39 +08:00
|
|
|
tty_puts(rl_prompt);
|
2010-07-24 09:38:25 +08:00
|
|
|
}
|
2010-07-26 04:14:14 +08:00
|
|
|
|
2017-12-11 15:43:43 +08:00
|
|
|
Repeat = NO_ARG;
|
|
|
|
old_point = rl_point = rl_mark = rl_end = 0;
|
|
|
|
rl_line_buffer[0] = '\0';
|
|
|
|
el_intr_pending = -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *el_deprep(char *line)
|
|
|
|
{
|
2010-07-24 09:38:25 +08:00
|
|
|
if (line) {
|
2008-12-03 04:58:55 +08:00
|
|
|
line = strdup(line);
|
|
|
|
tty_puts(NEWLINE);
|
|
|
|
tty_flush();
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2010-07-26 04:14:14 +08:00
|
|
|
|
2010-08-05 07:08:30 +08:00
|
|
|
rl_deprep_term_function();
|
2017-12-11 15:43:43 +08:00
|
|
|
if (Screen) {
|
2019-11-20 19:46:39 +08:00
|
|
|
free(Screen);
|
|
|
|
Screen = NULL;
|
2017-12-11 15:43:43 +08:00
|
|
|
}
|
|
|
|
|
2010-07-23 17:01:51 +08:00
|
|
|
free(H.Lines[--H.Size]);
|
2017-11-29 21:41:43 +08:00
|
|
|
H.Lines[H.Size] = NULL;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2014-11-05 05:47:50 +08:00
|
|
|
/* Add to history, unless no-echo or no-history mode ... */
|
|
|
|
if (!el_no_echo && !el_no_hist) {
|
2019-11-20 19:46:39 +08:00
|
|
|
if (line != NULL && *line != '\0')
|
|
|
|
hist_add(line);
|
2014-06-25 09:05:53 +08:00
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-24 09:46:58 +08:00
|
|
|
if (el_intr_pending > 0) {
|
2019-11-20 19:46:39 +08:00
|
|
|
int signo = el_intr_pending;
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
el_intr_pending = 0;
|
2017-11-29 23:40:10 +08:00
|
|
|
kill(getpid(), signo);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2010-07-23 17:01:51 +08:00
|
|
|
|
|
|
|
return line;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2017-12-11 15:43:43 +08:00
|
|
|
void rl_callback_handler_install(const char *prompt, rl_vcpfunc_t *lhandler)
|
|
|
|
{
|
|
|
|
if (!lhandler)
|
2019-11-20 19:46:39 +08:00
|
|
|
return;
|
2017-12-11 15:43:43 +08:00
|
|
|
line_handler = lhandler;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Any error from el_prep() is handled by the lhandler callbck as
|
|
|
|
* soon as the user calls rl_callback_read_char().
|
|
|
|
*/
|
|
|
|
el_prep(prompt);
|
|
|
|
tty_flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reads one character at a time, when a complete line has been received
|
|
|
|
* the lhandler from rl_callback_handler_install() is called with the
|
|
|
|
* line as argument.
|
|
|
|
*
|
|
|
|
* If the callback returns the terminal is prepped for reading a new
|
|
|
|
* line.
|
|
|
|
*
|
|
|
|
* If any error occurs, either in the _install() phase, or while reading
|
|
|
|
* one character, this function restores the terminal and calls lhandler
|
|
|
|
* with a NULL argument.
|
|
|
|
*/
|
|
|
|
void rl_callback_read_char(void)
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
|
|
|
|
if (!line_handler) {
|
2019-11-20 19:46:39 +08:00
|
|
|
errno = EINVAL;
|
|
|
|
return;
|
2017-12-11 15:43:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if rl_callback_handler_install() failed
|
|
|
|
* This is the only point where we can tell user
|
|
|
|
*/
|
|
|
|
if (!Screen || !rl_line_buffer) {
|
2019-11-20 19:46:39 +08:00
|
|
|
errno = ENOMEM;
|
|
|
|
line_handler(el_deprep(NULL));
|
|
|
|
return;
|
2017-12-11 15:43:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
line = editinput(0);
|
|
|
|
if (line) {
|
2019-11-20 19:46:39 +08:00
|
|
|
char *l;
|
2017-12-11 15:43:43 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
if (Searching) {
|
|
|
|
h_search_end(line);
|
|
|
|
tty_flush();
|
|
|
|
return;
|
|
|
|
}
|
2017-12-11 15:43:43 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
l = el_deprep(line);
|
|
|
|
line_handler(l);
|
2017-12-11 15:43:43 +08:00
|
|
|
|
2019-11-20 19:46:39 +08:00
|
|
|
if (el_prep(rl_prompt))
|
|
|
|
line_handler(NULL);
|
2017-12-11 15:43:43 +08:00
|
|
|
}
|
|
|
|
tty_flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void rl_callback_handler_remove(void)
|
|
|
|
{
|
|
|
|
if (!line_handler)
|
2019-11-20 19:46:39 +08:00
|
|
|
return;
|
2017-12-11 15:43:43 +08:00
|
|
|
|
|
|
|
el_deprep(NULL);
|
|
|
|
line_handler = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *readline(const char *prompt)
|
|
|
|
{
|
|
|
|
/* Unless called by the user already. */
|
|
|
|
rl_initialize();
|
|
|
|
|
|
|
|
if (!isatty(el_infd)) {
|
|
|
|
tty_flush();
|
|
|
|
|
|
|
|
return read_redirected();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (el_prep(prompt))
|
2019-11-20 19:46:39 +08:00
|
|
|
return NULL;
|
2017-12-11 15:43:43 +08:00
|
|
|
|
|
|
|
return el_deprep(editinput(1));
|
|
|
|
}
|
|
|
|
|
2017-11-29 22:01:22 +08:00
|
|
|
/*
|
|
|
|
* Even though readline() itself adds history automatically, the user
|
|
|
|
* can also add lines. This is for compatibility with GNU Readline.
|
|
|
|
*/
|
2010-08-04 03:01:01 +08:00
|
|
|
void add_history(const char *p)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
if (p == NULL || *p == '\0')
|
2008-12-03 04:58:55 +08:00
|
|
|
return;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-08-04 03:01:01 +08:00
|
|
|
hist_add(p);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2010-08-04 08:23:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
int read_history(const char *filename)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char buf[SCREEN_INC];
|
|
|
|
|
|
|
|
hist_alloc();
|
2020-01-05 14:14:58 +08:00
|
|
|
|
2010-08-04 08:23:05 +08:00
|
|
|
fp = fopen(filename, "r");
|
2020-01-05 14:14:58 +08:00
|
|
|
if (!fp)
|
|
|
|
return EOF;
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2020-01-05 14:14:58 +08:00
|
|
|
H.Size = 0;
|
|
|
|
while (H.Size < el_hist_size) {
|
|
|
|
if (!fgets(buf, SCREEN_INC, fp))
|
|
|
|
break;
|
2010-08-04 08:23:05 +08:00
|
|
|
|
2020-01-05 14:14:58 +08:00
|
|
|
buf[strlen(buf) - 1] = 0; /* Remove '\n' */
|
|
|
|
add_history(buf);
|
2010-08-04 08:23:05 +08:00
|
|
|
}
|
|
|
|
|
2020-01-05 14:14:58 +08:00
|
|
|
return fclose(fp);
|
2010-08-04 08:23:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int write_history(const char *filename)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2020-01-05 14:14:58 +08:00
|
|
|
int i = 0;
|
2010-08-04 08:23:05 +08:00
|
|
|
|
|
|
|
hist_alloc();
|
|
|
|
|
2020-01-05 14:14:58 +08:00
|
|
|
fp = fopen(filename, "w");
|
|
|
|
if (!fp)
|
|
|
|
return EOF;
|
2014-11-05 06:19:39 +08:00
|
|
|
|
2020-01-05 14:14:58 +08:00
|
|
|
while (i < H.Size)
|
|
|
|
fprintf(fp, "%s\n", H.Lines[i++]);
|
2010-08-04 08:23:05 +08:00
|
|
|
|
2020-01-05 14:14:58 +08:00
|
|
|
return fclose(fp);
|
2010-08-04 08:23:05 +08:00
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Move back to the beginning of the current word and return an
|
|
|
|
** allocated copy of it.
|
|
|
|
*/
|
2010-08-05 18:48:51 +08:00
|
|
|
char *el_find_word(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
char *p, *q;
|
2014-11-05 06:19:39 +08:00
|
|
|
char *word;
|
2010-07-24 09:12:45 +08:00
|
|
|
size_t len;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
p = &rl_line_buffer[rl_point];
|
|
|
|
while (p > rl_line_buffer) {
|
2008-12-03 04:58:55 +08:00
|
|
|
p--;
|
2010-07-20 06:18:20 +08:00
|
|
|
if (p > rl_line_buffer && p[-1] == '\\') {
|
2008-12-03 04:58:55 +08:00
|
|
|
p--;
|
|
|
|
} else {
|
|
|
|
if (strchr(SEPS, (char) *p) != NULL) {
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2010-07-23 17:01:51 +08:00
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
len = rl_point - (p - rl_line_buffer) + 1;
|
2014-11-05 06:19:39 +08:00
|
|
|
word = malloc(sizeof(char) * len);
|
|
|
|
if (!word)
|
2008-12-03 04:58:55 +08:00
|
|
|
return NULL;
|
2010-07-23 17:01:51 +08:00
|
|
|
|
2014-11-05 06:19:39 +08:00
|
|
|
q = word;
|
2010-07-20 06:18:20 +08:00
|
|
|
while (p < &rl_line_buffer[rl_point]) {
|
2008-12-03 04:58:55 +08:00
|
|
|
if (*p == '\\') {
|
2010-07-23 17:01:51 +08:00
|
|
|
if (++p == &rl_line_buffer[rl_point])
|
2019-11-20 19:46:39 +08:00
|
|
|
break;
|
2008-12-03 04:58:55 +08:00
|
|
|
}
|
|
|
|
*q++ = *p++;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
*q = '\0';
|
2010-07-23 17:01:51 +08:00
|
|
|
|
2014-11-05 06:19:39 +08:00
|
|
|
return word;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t c_possible(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
char **av;
|
|
|
|
char *word;
|
|
|
|
int ac;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-08-05 18:48:51 +08:00
|
|
|
word = el_find_word();
|
2010-07-24 08:21:28 +08:00
|
|
|
ac = rl_list_possib(word, &av);
|
2008-06-07 18:28:36 +08:00
|
|
|
if (word)
|
2010-07-23 17:01:51 +08:00
|
|
|
free(word);
|
2008-06-07 18:28:36 +08:00
|
|
|
if (ac) {
|
2010-08-05 18:48:51 +08:00
|
|
|
el_print_columns(ac, av);
|
2008-12-03 04:58:55 +08:00
|
|
|
while (--ac >= 0)
|
2010-07-23 17:01:51 +08:00
|
|
|
free(av[ac]);
|
|
|
|
free(av);
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
return CSmove;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2010-07-24 06:50:40 +08:00
|
|
|
|
2010-08-05 18:48:51 +08:00
|
|
|
return el_ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t c_complete(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
char *p, *q;
|
2014-11-05 06:19:39 +08:00
|
|
|
char *word, *string;
|
2010-07-24 09:12:45 +08:00
|
|
|
size_t len;
|
2008-12-03 04:58:55 +08:00
|
|
|
int unique;
|
2010-08-11 19:14:32 +08:00
|
|
|
el_status_t s = CSdone;
|
|
|
|
|
|
|
|
if (rl_inhibit_complete)
|
2019-11-20 19:46:39 +08:00
|
|
|
return CSdispatch;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-08-05 18:48:51 +08:00
|
|
|
word = el_find_word();
|
2010-08-12 03:19:41 +08:00
|
|
|
p = rl_complete(word, &unique);
|
2008-06-07 18:28:36 +08:00
|
|
|
if (word)
|
2010-07-23 17:01:51 +08:00
|
|
|
free(word);
|
2008-06-07 18:28:36 +08:00
|
|
|
if (p) {
|
2010-08-12 03:19:41 +08:00
|
|
|
len = strlen(p);
|
2008-12-03 04:58:55 +08:00
|
|
|
word = p;
|
2014-11-05 06:19:39 +08:00
|
|
|
|
|
|
|
string = q = malloc(sizeof(char) * (2 * len + 1));
|
2019-11-20 19:46:39 +08:00
|
|
|
if (!string) {
|
|
|
|
free(word);
|
|
|
|
return CSstay;
|
|
|
|
}
|
2014-11-05 06:19:39 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
while (*p) {
|
2010-08-12 03:19:41 +08:00
|
|
|
if ((*p < ' ' || strchr(SEPS, *p) != NULL)
|
2019-11-20 19:46:39 +08:00
|
|
|
&& (!unique || p[1] != 0)) {
|
2008-12-03 04:58:55 +08:00
|
|
|
*q++ = '\\';
|
|
|
|
}
|
|
|
|
*q++ = *p++;
|
|
|
|
}
|
|
|
|
*q = '\0';
|
2010-07-23 17:01:51 +08:00
|
|
|
free(word);
|
2014-11-05 06:19:39 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
if (len > 0) {
|
2014-11-05 06:19:39 +08:00
|
|
|
s = insert_string(string);
|
2015-04-06 21:22:35 +08:00
|
|
|
#ifdef CONFIG_TERMINAL_BELL
|
2008-12-03 04:58:55 +08:00
|
|
|
if (!unique)
|
2010-08-05 18:48:51 +08:00
|
|
|
el_ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
#endif
|
2008-12-03 04:58:55 +08:00
|
|
|
}
|
2014-11-05 06:19:39 +08:00
|
|
|
free(string);
|
|
|
|
|
2010-07-23 17:01:51 +08:00
|
|
|
if (len > 0)
|
2019-11-20 19:46:39 +08:00
|
|
|
return s;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2014-11-05 06:19:39 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return c_possible();
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t accept_line(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-07-20 06:18:20 +08:00
|
|
|
rl_line_buffer[rl_end] = '\0';
|
2008-06-07 18:28:36 +08:00
|
|
|
return CSdone;
|
|
|
|
}
|
|
|
|
|
2010-07-26 04:17:10 +08:00
|
|
|
#ifdef SYSTEM_IS_WIN32
|
|
|
|
static el_status_t end_of_input(void)
|
|
|
|
{
|
|
|
|
rl_line_buffer[rl_end] = '\0';
|
|
|
|
return CSeof;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t transpose(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
char c;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
if (rl_point) {
|
|
|
|
if (rl_point == rl_end)
|
2008-12-03 04:58:55 +08:00
|
|
|
left(CSmove);
|
2010-07-20 06:18:20 +08:00
|
|
|
c = rl_line_buffer[rl_point - 1];
|
2008-12-03 04:58:55 +08:00
|
|
|
left(CSstay);
|
2010-07-20 06:18:20 +08:00
|
|
|
rl_line_buffer[rl_point - 1] = rl_line_buffer[rl_point];
|
|
|
|
tty_show(rl_line_buffer[rl_point - 1]);
|
|
|
|
rl_line_buffer[rl_point++] = c;
|
2008-12-03 04:58:55 +08:00
|
|
|
tty_show(c);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t quote(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
int c;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
return (c = tty_get()) == EOF ? CSeof : insert_char((int)c);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t mk_set(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-07-20 06:18:20 +08:00
|
|
|
rl_mark = rl_point;
|
2008-06-07 18:28:36 +08:00
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t exchange(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
int c;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
if ((c = tty_get()) != CTL('X'))
|
2010-08-05 18:48:51 +08:00
|
|
|
return c == EOF ? CSeof : el_ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
if ((c = rl_mark) <= rl_end) {
|
|
|
|
rl_mark = rl_point;
|
|
|
|
rl_point = c;
|
2008-12-03 04:58:55 +08:00
|
|
|
return CSmove;
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t yank(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
|
|
|
if (Yanked && *Yanked)
|
2008-12-03 04:58:55 +08:00
|
|
|
return insert_string(Yanked);
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t copy_region(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-07-20 06:18:20 +08:00
|
|
|
if (rl_mark > rl_end)
|
2010-08-05 18:48:51 +08:00
|
|
|
return el_ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
if (rl_point > rl_mark)
|
|
|
|
save_yank(rl_mark, rl_point - rl_mark);
|
2008-06-07 18:28:36 +08:00
|
|
|
else
|
2010-07-20 06:18:20 +08:00
|
|
|
save_yank(rl_point, rl_mark - rl_point);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t move_to_char(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2017-11-29 23:40:10 +08:00
|
|
|
int i, c;
|
|
|
|
char *p;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
if ((c = tty_get()) == EOF)
|
|
|
|
return CSeof;
|
2017-11-29 23:40:10 +08:00
|
|
|
|
|
|
|
for (i = rl_point + 1, p = &rl_line_buffer[i]; i < rl_end; i++, p++) {
|
2008-12-03 04:58:55 +08:00
|
|
|
if (*p == c) {
|
2010-07-20 06:18:20 +08:00
|
|
|
rl_point = i;
|
2008-12-03 04:58:55 +08:00
|
|
|
return CSmove;
|
|
|
|
}
|
2017-11-29 23:40:10 +08:00
|
|
|
}
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t fd_kill_word(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2017-11-29 23:40:10 +08:00
|
|
|
int i;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-18 07:41:18 +08:00
|
|
|
do_forward(CSstay);
|
2010-07-26 04:14:14 +08:00
|
|
|
if (old_point != rl_point) {
|
2018-09-16 15:13:22 +08:00
|
|
|
i = rl_point - old_point - 1;
|
2010-07-26 04:14:14 +08:00
|
|
|
rl_point = old_point;
|
2008-12-03 04:58:55 +08:00
|
|
|
return delete_string(i);
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t bk_kill_word(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-07-18 07:41:18 +08:00
|
|
|
bk_word();
|
2010-07-26 04:14:14 +08:00
|
|
|
if (old_point != rl_point)
|
|
|
|
return delete_string(old_point - rl_point);
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return CSstay;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static int argify(char *line, char ***avp)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
char *c;
|
|
|
|
char **p;
|
2014-11-05 06:19:39 +08:00
|
|
|
char **arg;
|
2008-12-03 04:58:55 +08:00
|
|
|
int ac;
|
|
|
|
int i;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
i = MEM_INC;
|
2010-07-23 17:01:51 +08:00
|
|
|
*avp = p = malloc(sizeof(char *) * i);
|
|
|
|
if (!p)
|
2019-11-20 19:46:39 +08:00
|
|
|
return 0;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
for (c = line; isspace(*c); c++)
|
2008-12-03 04:58:55 +08:00
|
|
|
continue;
|
2017-11-29 23:40:10 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
if (*c == '\n' || *c == '\0')
|
2008-12-03 04:58:55 +08:00
|
|
|
return 0;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
for (ac = 0, p[ac++] = c; *c && *c != '\n'; ) {
|
2017-11-29 23:40:10 +08:00
|
|
|
if (!isspace(*c)) {
|
2019-11-20 19:46:39 +08:00
|
|
|
c++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*c++ = '\0';
|
|
|
|
if (*c && *c != '\n') {
|
|
|
|
if (ac + 1 == i) {
|
|
|
|
arg = malloc(sizeof(char *) * (i + MEM_INC));
|
|
|
|
if (!arg) {
|
|
|
|
p[ac] = NULL;
|
|
|
|
return ac;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(arg, p, i * sizeof(char *));
|
|
|
|
i += MEM_INC;
|
|
|
|
free(p);
|
|
|
|
*avp = p = arg;
|
|
|
|
}
|
|
|
|
p[ac++] = c;
|
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
}
|
2014-11-05 06:19:39 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
*c = '\0';
|
|
|
|
p[ac] = NULL;
|
2010-07-24 09:05:23 +08:00
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return ac;
|
|
|
|
}
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
static el_status_t last_argument(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
{
|
2010-07-24 09:05:23 +08:00
|
|
|
char **av = NULL;
|
2008-12-03 04:58:55 +08:00
|
|
|
char *p;
|
|
|
|
el_status_t s;
|
|
|
|
int ac;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
if (H.Size == 1 || (p = (char *)H.Lines[H.Size - 2]) == NULL)
|
2010-08-05 18:48:51 +08:00
|
|
|
return el_ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2014-11-05 06:19:39 +08:00
|
|
|
p = strdup(p);
|
|
|
|
if (!p)
|
2008-12-03 04:58:55 +08:00
|
|
|
return CSstay;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2014-11-05 06:19:39 +08:00
|
|
|
ac = argify(p, &av);
|
2008-06-07 18:28:36 +08:00
|
|
|
if (Repeat != NO_ARG)
|
2010-08-05 18:48:51 +08:00
|
|
|
s = Repeat < ac ? insert_string(av[Repeat]) : el_ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
else
|
2008-12-03 04:58:55 +08:00
|
|
|
s = ac ? insert_string(av[ac - 1]) : CSstay;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
2010-07-24 09:05:23 +08:00
|
|
|
if (av)
|
2010-07-23 17:01:51 +08:00
|
|
|
free(av);
|
|
|
|
free(p);
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2010-08-05 18:48:51 +08:00
|
|
|
static el_keymap_t Map[64] = {
|
2008-12-03 04:58:55 +08:00
|
|
|
{ CTL('@'), mk_set },
|
|
|
|
{ CTL('A'), beg_line },
|
|
|
|
{ CTL('B'), bk_char },
|
|
|
|
{ CTL('D'), del_char },
|
|
|
|
{ CTL('E'), end_line },
|
|
|
|
{ CTL('F'), fd_char },
|
2010-08-05 18:48:51 +08:00
|
|
|
{ CTL('G'), el_ring_bell },
|
2008-12-03 04:58:55 +08:00
|
|
|
{ CTL('H'), bk_del_char },
|
|
|
|
{ CTL('I'), c_complete },
|
|
|
|
{ CTL('J'), accept_line },
|
|
|
|
{ CTL('K'), kill_line },
|
2019-05-08 22:55:33 +08:00
|
|
|
{ CTL('L'), refresh },
|
2008-12-03 04:58:55 +08:00
|
|
|
{ CTL('M'), accept_line },
|
|
|
|
{ CTL('N'), h_next },
|
2010-08-05 18:48:51 +08:00
|
|
|
{ CTL('O'), el_ring_bell },
|
2008-12-03 04:58:55 +08:00
|
|
|
{ CTL('P'), h_prev },
|
2010-08-05 18:48:51 +08:00
|
|
|
{ CTL('Q'), el_ring_bell },
|
2008-12-03 04:58:55 +08:00
|
|
|
{ CTL('R'), h_search },
|
2010-08-05 18:48:51 +08:00
|
|
|
{ CTL('S'), el_ring_bell },
|
2008-12-03 04:58:55 +08:00
|
|
|
{ CTL('T'), transpose },
|
2010-08-05 18:48:51 +08:00
|
|
|
{ CTL('U'), el_ring_bell },
|
2008-12-03 04:58:55 +08:00
|
|
|
{ CTL('V'), quote },
|
|
|
|
{ CTL('W'), bk_kill_word },
|
|
|
|
{ CTL('X'), exchange },
|
|
|
|
{ CTL('Y'), yank },
|
2010-07-26 04:17:10 +08:00
|
|
|
#ifdef SYSTEM_IS_WIN32
|
|
|
|
{ CTL('Z'), end_of_input },
|
|
|
|
#else
|
2010-08-05 18:48:51 +08:00
|
|
|
{ CTL('Z'), el_ring_bell },
|
2010-07-26 04:17:10 +08:00
|
|
|
#endif
|
2008-12-03 04:58:55 +08:00
|
|
|
{ CTL('['), meta },
|
|
|
|
{ CTL(']'), move_to_char },
|
2010-08-05 18:48:51 +08:00
|
|
|
{ CTL('^'), el_ring_bell },
|
|
|
|
{ CTL('_'), el_ring_bell },
|
2008-12-03 04:58:55 +08:00
|
|
|
{ 0, NULL }
|
2008-06-07 18:28:36 +08:00
|
|
|
};
|
|
|
|
|
2010-07-26 04:17:10 +08:00
|
|
|
static el_keymap_t MetaMap[64]= {
|
2010-07-26 04:01:04 +08:00
|
|
|
{ CTL('H'), bk_kill_word },
|
|
|
|
{ DEL, bk_kill_word },
|
2008-12-03 04:58:55 +08:00
|
|
|
{ ' ', mk_set },
|
|
|
|
{ '.', last_argument },
|
|
|
|
{ '<', h_first },
|
|
|
|
{ '>', h_last },
|
|
|
|
{ '?', c_possible },
|
|
|
|
{ 'b', bk_word },
|
2010-07-26 07:44:18 +08:00
|
|
|
{ 'c', case_cap_word },
|
2008-12-03 04:58:55 +08:00
|
|
|
{ 'd', fd_kill_word },
|
|
|
|
{ 'f', fd_word },
|
|
|
|
{ 'l', case_down_word },
|
|
|
|
{ 'm', toggle_meta_mode },
|
|
|
|
{ 'u', case_up_word },
|
|
|
|
{ 'y', yank },
|
|
|
|
{ 'w', copy_region },
|
|
|
|
{ 0, NULL }
|
2008-06-07 18:28:36 +08:00
|
|
|
};
|
|
|
|
|
2013-07-08 22:19:17 +08:00
|
|
|
static size_t find_key_in_map(int key, el_keymap_t map[], size_t mapsz)
|
2010-07-26 04:17:10 +08:00
|
|
|
{
|
2010-08-05 18:48:51 +08:00
|
|
|
size_t i;
|
2010-07-26 04:17:10 +08:00
|
|
|
|
2015-02-01 22:03:19 +08:00
|
|
|
for (i = 0; map[i].Function && i < mapsz; i++) {
|
2019-11-20 19:46:39 +08:00
|
|
|
if (map[i].Key == key)
|
|
|
|
return i;
|
2010-07-26 04:17:10 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 22:19:17 +08:00
|
|
|
if (i < mapsz)
|
2019-11-20 19:46:39 +08:00
|
|
|
return i;
|
2013-07-08 22:19:17 +08:00
|
|
|
|
|
|
|
return mapsz;
|
|
|
|
}
|
|
|
|
|
2013-07-08 22:27:55 +08:00
|
|
|
static el_status_t el_bind_key_in_map(int key, el_keymap_func_t function, el_keymap_t map[], size_t mapsz)
|
2013-07-08 22:19:17 +08:00
|
|
|
{
|
|
|
|
size_t creat, pos = find_key_in_map(key, map, mapsz);
|
|
|
|
|
2014-11-05 06:50:45 +08:00
|
|
|
/* Must check that pos is not the next to last array position,
|
|
|
|
* otherwise we will write out-of-bounds to terminate the list. */
|
2015-09-10 19:06:39 +08:00
|
|
|
if (pos + 1 >= mapsz) {
|
2019-11-20 19:46:39 +08:00
|
|
|
errno = ENOMEM;
|
|
|
|
return CSeof;
|
2010-07-26 04:17:10 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 22:19:17 +08:00
|
|
|
/* Add at end, create new? */
|
|
|
|
creat = map[pos].Function == NULL;
|
|
|
|
|
|
|
|
/* A new key so have to add it to end */
|
2015-09-10 19:06:39 +08:00
|
|
|
map[pos].Key = key;
|
|
|
|
map[pos].Function = function;
|
2010-08-05 18:48:51 +08:00
|
|
|
|
2013-07-08 22:19:17 +08:00
|
|
|
/* Terminate list */
|
|
|
|
if (creat) {
|
2019-11-20 19:46:39 +08:00
|
|
|
map[pos + 1].Key = 0;
|
|
|
|
map[pos + 1].Function = NULL;
|
2013-07-08 22:19:17 +08:00
|
|
|
}
|
2013-07-08 22:27:55 +08:00
|
|
|
|
|
|
|
return CSdone;
|
2010-08-05 18:48:51 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 22:27:55 +08:00
|
|
|
el_status_t el_bind_key(int key, el_keymap_func_t function)
|
2010-08-05 18:48:51 +08:00
|
|
|
{
|
2018-09-21 13:12:24 +08:00
|
|
|
return el_bind_key_in_map(key, function, Map, NELEMS(Map));
|
2010-08-05 18:48:51 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 22:27:55 +08:00
|
|
|
el_status_t el_bind_key_in_metamap(int key, el_keymap_func_t function)
|
2010-08-05 18:48:51 +08:00
|
|
|
{
|
2018-09-21 13:12:24 +08:00
|
|
|
return el_bind_key_in_map(key, function, MetaMap, NELEMS(MetaMap));
|
2010-07-26 04:17:10 +08:00
|
|
|
}
|
|
|
|
|
2019-05-08 05:43:37 +08:00
|
|
|
rl_getc_func_t *rl_set_getc_func(rl_getc_func_t *func)
|
|
|
|
{
|
|
|
|
rl_getc_func_t *old = rl_getc_function;
|
|
|
|
rl_getc_function = func;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2010-07-19 00:57:07 +08:00
|
|
|
/**
|
|
|
|
* Local Variables:
|
2017-11-29 23:40:10 +08:00
|
|
|
* c-file-style: "k&r"
|
2010-07-19 00:57:07 +08:00
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
*/
|