2010-07-18 07:41:18 +08:00
|
|
|
|
/* Main editing routines for editline library.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 1992, 1993 Simmule Turner and Rich Salz. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* 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 "editline.h"
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** 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
|
|
|
|
|
#define NO_ARG (-1)
|
|
|
|
|
#define DEL 127
|
|
|
|
|
#define CTL(x) ((x) & 0x1F)
|
|
|
|
|
#define ISCTL(x) ((x) && (x) < ' ')
|
|
|
|
|
#define UNCTL(x) ((x) + 64)
|
|
|
|
|
#define META(x) ((x) | 0x80)
|
|
|
|
|
#define ISMETA(x) ((x) & 0x80)
|
|
|
|
|
#define UNMETA(x) ((x) & 0x7F)
|
|
|
|
|
#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
|
|
|
|
|
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;
|
|
|
|
|
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-03-10 04:18:03 +08:00
|
|
|
|
static el_keymap_t Map[];
|
|
|
|
|
static el_keymap_t MetaMap[];
|
2010-07-24 09:12:45 +08:00
|
|
|
|
static size_t Length;
|
|
|
|
|
static size_t ScreenCount;
|
|
|
|
|
static size_t ScreenSize;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static char *backspace;
|
2010-07-19 00:57:07 +08:00
|
|
|
|
static int tty_cols;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static int tty_rows;
|
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 */
|
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-07-20 06:18:20 +08:00
|
|
|
|
char *rl_line_buffer;
|
2010-07-26 04:14:14 +08:00
|
|
|
|
const char *rl_prompt;
|
2010-07-20 06:18:20 +08:00
|
|
|
|
const char *rl_readline_name;/* Set by calling program, for conditional parsing of ~/.inputrc - Not supported yet! */
|
2010-08-05 07:08:30 +08:00
|
|
|
|
FILE *rl_instream;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2008-10-02 15:09:09 +08:00
|
|
|
|
/* User definable callbacks. */
|
2010-07-20 06:18:20 +08:00
|
|
|
|
char **(*rl_attempted_completion_function)(const char *token, int start, int end);
|
2008-10-02 15:09:09 +08:00
|
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
|
/* Declarations. */
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static char *editinput(void);
|
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
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2010-07-30 08:03:46 +08:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Misc. local helper functions.
|
|
|
|
|
*/
|
|
|
|
|
static int is_alpha_num(unsigned char c)
|
|
|
|
|
{
|
|
|
|
|
if (isalnum(c))
|
|
|
|
|
return 1;
|
|
|
|
|
if (ISMETA(c))
|
|
|
|
|
return 1;
|
|
|
|
|
if (ISCTL(c))
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (ScreenCount) {
|
2010-07-24 09:38:25 +08:00
|
|
|
|
if (!el_no_echo)
|
2010-07-30 08:03:46 +08:00
|
|
|
|
res = write(1, Screen, ScreenCount);
|
2008-12-03 04:58:55 +08:00
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
Screen[ScreenCount] = c;
|
|
|
|
|
if (++ScreenCount >= ScreenSize - 1) {
|
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
|
|
|
|
{
|
|
|
|
|
while (*p)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_show(*p++);
|
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 {
|
|
|
|
|
r = read(0, &c, 1);
|
|
|
|
|
} 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();
|
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
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
#define tty_back() (backspace ? tty_puts(backspace) : tty_put('\b'))
|
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
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static int init;
|
2010-07-19 00:57:07 +08:00
|
|
|
|
#ifdef CONFIG_USE_TERMCAP
|
2010-07-24 08:21:28 +08:00
|
|
|
|
char *term;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
char buff[2048];
|
2010-07-24 08:21:28 +08:00
|
|
|
|
char *bp;
|
2010-07-18 07:41:18 +08:00
|
|
|
|
#endif
|
|
|
|
|
#ifdef TIOCGWINSZ
|
2008-12-03 04:58:55 +08:00
|
|
|
|
struct winsize W;
|
2010-07-18 07:41:18 +08:00
|
|
|
|
#endif
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
if (init) {
|
2010-07-18 07:41:18 +08:00
|
|
|
|
#ifdef TIOCGWINSZ
|
2008-12-03 04:58:55 +08:00
|
|
|
|
/* Perhaps we got resized. */
|
|
|
|
|
if (ioctl(0, TIOCGWINSZ, &W) >= 0
|
|
|
|
|
&& W.ws_col > 0 && W.ws_row > 0) {
|
2010-07-19 00:57:07 +08:00
|
|
|
|
tty_cols = (int)W.ws_col;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_rows = (int)W.ws_row;
|
|
|
|
|
}
|
2010-07-18 07:41:18 +08:00
|
|
|
|
#endif
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
init++;
|
|
|
|
|
|
2010-07-19 00:57:07 +08:00
|
|
|
|
/* Initialize to faulty values to trigger fallback if nothing else works. */
|
|
|
|
|
tty_cols = tty_rows = -1;
|
|
|
|
|
#ifdef CONFIG_USE_TERMCAP
|
2010-07-18 11:19:03 +08:00
|
|
|
|
bp = buff;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if ((term = getenv("TERM")) == NULL)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
term = "dumb";
|
2010-07-19 00:57:07 +08:00
|
|
|
|
if (-1 != tgetent(buff, term)) {
|
2010-07-24 08:02:14 +08:00
|
|
|
|
if ((backspace = tgetstr("le", &bp)) != NULL)
|
|
|
|
|
backspace = strdup(backspace);
|
|
|
|
|
else
|
|
|
|
|
backspace = "\b";
|
|
|
|
|
tty_cols = tgetnum("co");
|
|
|
|
|
tty_rows = tgetnum("li");
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
2010-07-19 00:57:07 +08:00
|
|
|
|
/* Make sure to check width & rows and fallback to TIOCGWINSZ if available. */
|
2010-07-18 07:41:18 +08:00
|
|
|
|
#endif
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2010-07-19 00:57:07 +08:00
|
|
|
|
if (tty_cols <= 0 || tty_rows <= 0) {
|
2010-07-18 07:41:18 +08:00
|
|
|
|
#ifdef TIOCGWINSZ
|
2010-07-19 00:57:07 +08:00
|
|
|
|
if (-1 != ioctl(0, TIOCGWINSZ, &W)) {
|
|
|
|
|
tty_cols = (int)W.ws_col;
|
|
|
|
|
tty_rows = (int)W.ws_row;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-07-18 07:41:18 +08:00
|
|
|
|
#endif
|
2010-07-19 00:57:07 +08:00
|
|
|
|
tty_cols = SCREEN_COLS;
|
|
|
|
|
tty_rows = SCREEN_ROWS;
|
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.
|
|
|
|
|
*/
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static void columns(int ac, char **av)
|
|
|
|
|
{
|
|
|
|
|
char *p;
|
|
|
|
|
int i;
|
|
|
|
|
int j;
|
|
|
|
|
int k;
|
|
|
|
|
int len;
|
|
|
|
|
int skip;
|
|
|
|
|
int longest;
|
|
|
|
|
int cols;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
/* Find longest name, determine column count from that. */
|
|
|
|
|
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;
|
2010-07-19 00:57:07 +08:00
|
|
|
|
cols = tty_cols / (longest + 3);
|
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);
|
|
|
|
|
if (j + skip < ac)
|
|
|
|
|
while (++len < longest + 3)
|
|
|
|
|
tty_put(' ');
|
|
|
|
|
}
|
|
|
|
|
tty_puts(NEWLINE);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static void reposition(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
2010-07-30 08:03:46 +08:00
|
|
|
|
int i;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_put('\r');
|
2010-07-26 04:14:14 +08:00
|
|
|
|
tty_puts(rl_prompt);
|
2010-07-30 08:03:46 +08:00
|
|
|
|
for (i = 0; i < rl_point; i++)
|
|
|
|
|
tty_show(rl_line_buffer[i]);
|
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) {
|
2010-07-30 08:03:46 +08:00
|
|
|
|
tty_back();
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2010-07-20 06:18:20 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static el_status_t ring_bell(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_put('\07');
|
|
|
|
|
tty_flush();
|
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;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return ring_bell();
|
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
|
|
|
|
|
|
|
|
|
/* Skip to end of word, if inside a word. */
|
2010-07-30 08:03:46 +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);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2010-07-26 07:44:18 +08:00
|
|
|
|
/* Skip to next word, or skip leading white space if outside a word. */
|
2010-07-30 08:03:46 +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);
|
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++) {
|
|
|
|
|
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
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
int extras;
|
|
|
|
|
int i;
|
|
|
|
|
char *p;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
|
for (extras = 0, i = rl_point, p = &rl_line_buffer[i]; i <= rl_end; i++, p++) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_put(' ');
|
2010-07-30 08:03:46 +08:00
|
|
|
|
if (ISMETA(*p)) {
|
|
|
|
|
if (rl_meta_chars) {
|
|
|
|
|
tty_put(' ');
|
|
|
|
|
tty_put(' ');
|
|
|
|
|
extras += 2;
|
|
|
|
|
}
|
|
|
|
|
} if (ISCTL(*p)) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_put(' ');
|
|
|
|
|
extras++;
|
|
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
|
for (i += extras; i > rl_point; i--)
|
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 clear_line(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
2010-07-26 04:14:14 +08:00
|
|
|
|
rl_point = -(int)strlen(rl_prompt);
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_put('\r');
|
2008-06-07 18:28:36 +08:00
|
|
|
|
ceol();
|
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;
|
|
|
|
|
char *new;
|
|
|
|
|
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) {
|
2010-07-23 17:01:51 +08:00
|
|
|
|
new = malloc(sizeof(char) * (Length + len + MEM_INC));
|
|
|
|
|
if (!new)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return CSstay;
|
|
|
|
|
if (Length) {
|
2010-07-23 17:01:51 +08:00
|
|
|
|
memcpy(new, rl_line_buffer, Length);
|
|
|
|
|
free(rl_line_buffer);
|
2008-12-03 04:58:55 +08:00
|
|
|
|
}
|
2010-07-20 06:18:20 +08:00
|
|
|
|
rl_line_buffer = new;
|
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];
|
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
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static el_status_t redisplay(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
2010-07-26 04:14:14 +08:00
|
|
|
|
tty_puts(NEWLINE); /* XXX: Use "\r\e[K" to get really neat effect on ANSI capable terminals. */
|
|
|
|
|
tty_puts(rl_prompt);
|
2010-07-20 06:18:20 +08:00
|
|
|
|
tty_string(rl_line_buffer);
|
2010-07-30 08:03:46 +08:00
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
|
return CSmove;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
return redisplay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static const char *next_hist(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
|
|
|
|
return H.Pos >= H.Size - 1 ? NULL : H.Lines[++H.Pos];
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static const char *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)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return ring_bell();
|
2010-07-30 08:03:46 +08:00
|
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
|
rl_point = 0;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
reposition();
|
|
|
|
|
ceol();
|
2010-07-20 06:18:20 +08:00
|
|
|
|
rl_end = 0;
|
2010-07-30 08:03:46 +08:00
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
|
return insert_string(p);
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
int i;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
do {
|
2010-07-26 07:44:18 +08:00
|
|
|
|
if ((p = move()) == NULL)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
} while (++i < Repeat);
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
return do_hist(next_hist);
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static el_status_t h_prev(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
|
|
|
|
return do_hist(prev_hist);
|
|
|
|
|
}
|
|
|
|
|
|
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';
|
|
|
|
|
for ( ; *text; text++)
|
|
|
|
|
if (*text == c && strncmp(text, pat, len) == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
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
|
|
|
|
static char *old_search;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static el_status_t h_search(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static int Searching;
|
|
|
|
|
const char *old_prompt;
|
2010-07-18 08:41:38 +08:00
|
|
|
|
const char *(*move)(void);
|
2008-12-03 04:58:55 +08:00
|
|
|
|
const char *p;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
if (Searching)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
Searching = 1;
|
|
|
|
|
|
|
|
|
|
clear_line();
|
2010-07-26 04:14:14 +08:00
|
|
|
|
old_prompt = rl_prompt;
|
|
|
|
|
rl_prompt = "Search: ";
|
|
|
|
|
tty_puts(rl_prompt);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
move = Repeat == NO_ARG ? prev_hist : next_hist;
|
|
|
|
|
p = editinput();
|
2010-07-26 04:14:14 +08:00
|
|
|
|
rl_prompt = old_prompt;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
Searching = 0;
|
2010-07-26 04:14:14 +08:00
|
|
|
|
tty_puts(rl_prompt);
|
2010-07-24 09:46:58 +08:00
|
|
|
|
if (p == NULL && el_intr_pending > 0) {
|
|
|
|
|
el_intr_pending = 0;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
clear_line();
|
|
|
|
|
return redisplay();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
p = search_hist(p, move);
|
|
|
|
|
clear_line();
|
|
|
|
|
if (p == NULL) {
|
2010-07-18 07:41:18 +08:00
|
|
|
|
ring_bell();
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return redisplay();
|
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 fd_char(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
int i;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
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
|
|
|
|
{
|
2008-12-03 04:58:55 +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)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return 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
|
|
|
|
}
|
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();
|
2010-07-20 06:18:20 +08:00
|
|
|
|
rl_end -= count;
|
|
|
|
|
tty_string(&rl_line_buffer[rl_point]);
|
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
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
int i;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
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
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
int i;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
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
|
|
|
|
{
|
2008-12-03 04:58:55 +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;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
reposition();
|
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
|
|
|
|
}
|
|
|
|
|
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;
|
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';
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
return CSstay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static el_status_t del_char(void)
|
|
|
|
|
{
|
|
|
|
|
return delete_string(Repeat == NO_ARG ? 1 : Repeat);
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
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()) {
|
2010-07-24 08:52:51 +08:00
|
|
|
|
case EOF: return CSeof;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
return 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';
|
2010-07-30 08:03:46 +08:00
|
|
|
|
tty_push(c);
|
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);
|
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
|
|
|
|
|
|
|
|
|
return ring_bell();
|
|
|
|
|
}
|
|
|
|
|
|
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)) {
|
2010-07-30 08:03:46 +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-07-26 07:44:18 +08:00
|
|
|
|
s = kp->Function ? kp->Function() : insert_char(c);
|
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
|
|
|
|
{
|
2010-07-30 08:03:46 +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();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (c == rl_kill) {
|
2010-07-20 06:18:20 +08:00
|
|
|
|
if (rl_point != 0) {
|
|
|
|
|
rl_point = 0;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
reposition();
|
|
|
|
|
}
|
|
|
|
|
Repeat = NO_ARG;
|
|
|
|
|
return kill_line();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
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;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (c == rl_intr) {
|
2010-07-24 09:46:58 +08:00
|
|
|
|
el_intr_pending = SIGINT;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return CSsignal;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
if (c == rl_quit) {
|
2010-07-24 09:46:58 +08:00
|
|
|
|
el_intr_pending = SIGQUIT;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return CSeof;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
2010-07-18 07:41:18 +08:00
|
|
|
|
#ifdef CONFIG_SIGSTOP
|
2008-06-10 03:37:01 +08:00
|
|
|
|
if (c == rl_susp) {
|
2010-07-24 09:46:58 +08:00
|
|
|
|
el_intr_pending = SIGTSTP;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return CSsignal;
|
2008-06-10 03:37:01 +08:00
|
|
|
|
}
|
2010-07-18 07:41:18 +08:00
|
|
|
|
#endif
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
return CSdispatch;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static char *editinput(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
|
|
|
|
|
|
|
|
|
Repeat = NO_ARG;
|
2010-07-26 04:14:14 +08:00
|
|
|
|
old_point = rl_point = rl_mark = rl_end = 0;
|
2010-07-20 06:18:20 +08:00
|
|
|
|
rl_line_buffer[0] = '\0';
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2010-07-24 09:46:58 +08:00
|
|
|
|
el_intr_pending = -1;
|
2010-07-26 04:14:14 +08:00
|
|
|
|
while ((c = tty_get()) != EOF) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
switch (tty_special(c)) {
|
2010-07-26 04:14:14 +08:00
|
|
|
|
case CSdone:
|
|
|
|
|
return rl_line_buffer;
|
|
|
|
|
|
|
|
|
|
case CSeof:
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
case CSsignal:
|
|
|
|
|
return (char *)"";
|
|
|
|
|
|
|
|
|
|
case CSmove:
|
|
|
|
|
reposition();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CSdispatch:
|
|
|
|
|
switch (emacs(c)) {
|
|
|
|
|
case CSdone:
|
|
|
|
|
return rl_line_buffer;
|
|
|
|
|
|
|
|
|
|
case CSeof:
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
case CSsignal:
|
|
|
|
|
return (char *)"";
|
|
|
|
|
|
|
|
|
|
case CSmove:
|
|
|
|
|
reposition();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CSdispatch:
|
|
|
|
|
case CSstay:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CSstay:
|
|
|
|
|
break;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
}
|
2010-07-26 04:14:14 +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)
|
|
|
|
|
H.Lines = calloc(el_hist_size, sizeof(char *));
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
if (H.Pos && strcmp(p, H.Lines[H.Pos - 1]) == 0)
|
|
|
|
|
return;
|
|
|
|
|
#endif
|
|
|
|
|
if (H.Size && strcmp(p, H.Lines[H.Size - 1]) == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if ((s = strdup(p)) == NULL)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return;
|
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)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
if (read(0, p, 1) <= 0) {
|
|
|
|
|
/* Ignore "incomplete" lines at EOF, just like we do for a tty. */
|
|
|
|
|
free(line);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (*p == '\n')
|
|
|
|
|
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. */
|
2008-12-03 04:58:55 +08:00
|
|
|
|
void rl_reset_terminal(char *p __attribute__((__unused__)))
|
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)
|
|
|
|
|
rl_prompt = "? ";
|
2010-08-04 08:12:19 +08:00
|
|
|
|
|
|
|
|
|
hist_alloc();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
char *readline(const char *prompt)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
2010-08-04 03:01:01 +08:00
|
|
|
|
char *line;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2008-10-02 15:09:09 +08:00
|
|
|
|
/* Unless called by the user already. */
|
2010-03-10 04:18:03 +08:00
|
|
|
|
rl_initialize();
|
2008-10-02 15:09:09 +08:00
|
|
|
|
|
2008-06-10 03:37:01 +08:00
|
|
|
|
if (!isatty(0)) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_flush();
|
|
|
|
|
return read_redirected();
|
2008-06-10 03:37:01 +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;
|
2010-07-23 17:01:51 +08:00
|
|
|
|
rl_line_buffer = malloc(sizeof(char) * Length);
|
|
|
|
|
if (!rl_line_buffer)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return NULL;
|
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)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2010-07-26 04:14:14 +08:00
|
|
|
|
rl_prompt = prompt ? prompt : NILSTR;
|
2010-07-24 09:38:25 +08:00
|
|
|
|
if (el_no_echo) {
|
|
|
|
|
int old = el_no_echo;
|
|
|
|
|
el_no_echo = 0;
|
2010-07-26 04:14:14 +08:00
|
|
|
|
tty_puts(rl_prompt);
|
2010-07-24 09:38:25 +08:00
|
|
|
|
tty_flush();
|
|
|
|
|
el_no_echo = old;
|
|
|
|
|
} else {
|
2010-07-26 04:14:14 +08:00
|
|
|
|
tty_puts(rl_prompt);
|
2010-07-24 09:38:25 +08:00
|
|
|
|
}
|
2010-07-26 04:14:14 +08:00
|
|
|
|
|
2010-07-24 09:38:25 +08:00
|
|
|
|
line = editinput();
|
|
|
|
|
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();
|
2010-07-23 17:01:51 +08:00
|
|
|
|
free(Screen);
|
|
|
|
|
free(H.Lines[--H.Size]);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2010-08-04 03:01:01 +08:00
|
|
|
|
/* Always add history, if it's a sane line. */
|
|
|
|
|
if (line != NULL && *line != '\0')
|
2008-12-03 04:58:55 +08:00
|
|
|
|
hist_add(line);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2010-07-24 09:46:58 +08:00
|
|
|
|
if (el_intr_pending > 0) {
|
|
|
|
|
int s = el_intr_pending;
|
|
|
|
|
el_intr_pending = 0;
|
2010-07-18 07:41:18 +08:00
|
|
|
|
kill(getpid(), s);
|
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
|
|
|
|
}
|
|
|
|
|
|
2010-08-04 03:01:01 +08:00
|
|
|
|
/* Even though readline() itself adds history automatically, the user can also add
|
|
|
|
|
* lines. This is for compat with GNU Readline. */
|
|
|
|
|
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();
|
|
|
|
|
fp = fopen(filename, "r");
|
|
|
|
|
if (fp) {
|
|
|
|
|
H.Size = 0;
|
|
|
|
|
while (H.Size < el_hist_size) {
|
|
|
|
|
if (!fgets(buf, SCREEN_INC, fp))
|
|
|
|
|
break;
|
|
|
|
|
buf[strlen(buf) - 1] = 0; /* Remove '\n' */
|
|
|
|
|
add_history(buf);
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return errno;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int write_history(const char *filename)
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
|
|
hist_alloc();
|
|
|
|
|
fp = fopen(filename, "w");
|
|
|
|
|
if (fp) {
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
while (i < H.Size) {
|
|
|
|
|
fprintf(fp, "%s\n", H.Lines[i++]);
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return errno;
|
|
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Move back to the beginning of the current word and return an
|
|
|
|
|
** allocated copy of it.
|
|
|
|
|
*/
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static char *find_word(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
char *p, *q;
|
|
|
|
|
char *new;
|
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;
|
2010-07-23 17:01:51 +08:00
|
|
|
|
new = malloc(sizeof(char) * len);
|
|
|
|
|
if (!new)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return NULL;
|
2010-07-23 17:01:51 +08:00
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
|
q = new;
|
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])
|
|
|
|
|
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
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
|
return new;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
word = 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) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
columns(ac, av);
|
|
|
|
|
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
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
|
return ring_bell();
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
char *word, *new;
|
2010-07-24 09:12:45 +08:00
|
|
|
|
size_t len;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
int unique;
|
2009-02-09 03:19:58 +08:00
|
|
|
|
el_status_t s = 0;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
word = find_word();
|
2008-12-03 04:58:55 +08:00
|
|
|
|
p = (char *)rl_complete((char *)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) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
len = strlen((char *)p);
|
|
|
|
|
word = p;
|
2010-07-23 17:01:51 +08:00
|
|
|
|
new = q = malloc(sizeof(char) * (2 * len + 1));
|
|
|
|
|
if (!new)
|
|
|
|
|
return CSstay;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
while (*p) {
|
|
|
|
|
if ((*p < ' ' || strchr(SEPS, (char) *p) != NULL)
|
|
|
|
|
&& (!unique || p[1] != 0)) {
|
|
|
|
|
*q++ = '\\';
|
|
|
|
|
}
|
|
|
|
|
*q++ = *p++;
|
|
|
|
|
}
|
|
|
|
|
*q = '\0';
|
2010-07-23 17:01:51 +08:00
|
|
|
|
free(word);
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (len > 0) {
|
|
|
|
|
s = insert_string(new);
|
2010-07-18 07:41:18 +08:00
|
|
|
|
#ifdef CONFIG_ANNOYING_NOISE
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (!unique)
|
2010-07-18 07:41:18 +08:00
|
|
|
|
ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
#endif
|
2008-12-03 04:58:55 +08:00
|
|
|
|
}
|
2010-07-23 17:01:51 +08:00
|
|
|
|
free(new);
|
|
|
|
|
if (len > 0)
|
|
|
|
|
return s;
|
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
|
|
|
|
}
|
|
|
|
|
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'))
|
|
|
|
|
return c == EOF ? CSeof : 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
|
|
|
|
}
|
|
|
|
|
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);
|
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)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return 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
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
int c;
|
|
|
|
|
int i;
|
|
|
|
|
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;
|
2010-07-20 06:18:20 +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;
|
|
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
|
return CSstay;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static el_status_t fd_word(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
|
|
|
|
return do_forward(CSmove);
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static el_status_t fd_kill_word(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
2008-12-03 04:58:55 +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) {
|
|
|
|
|
i = rl_point - old_point;
|
|
|
|
|
rl_point = old_point;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return delete_string(i);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
return CSstay;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static el_status_t bk_word(void)
|
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-30 08:03:46 +08:00
|
|
|
|
for (p = &rl_line_buffer[rl_point]; p > rl_line_buffer && !is_alpha_num(p[-1]); p--)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
left(CSmove);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2010-07-30 08:03:46 +08:00
|
|
|
|
for (; p > rl_line_buffer && !isblank(p[-1]) && is_alpha_num(p[-1]); p--)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
left(CSmove);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2010-07-20 06:18:20 +08:00
|
|
|
|
if (rl_point == 0)
|
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 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;
|
|
|
|
|
char **new;
|
|
|
|
|
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)
|
2008-12-03 04:58:55 +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;
|
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'; ) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (isspace(*c)) {
|
|
|
|
|
*c++ = '\0';
|
|
|
|
|
if (*c && *c != '\n') {
|
|
|
|
|
if (ac + 1 == i) {
|
2010-07-23 17:01:51 +08:00
|
|
|
|
new = malloc(sizeof(char *) * (i + MEM_INC));
|
|
|
|
|
if (!new) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
p[ac] = NULL;
|
|
|
|
|
return ac;
|
|
|
|
|
}
|
2010-07-23 17:01:51 +08:00
|
|
|
|
memcpy(new, p, i * sizeof(char **));
|
2008-12-03 04:58:55 +08:00
|
|
|
|
i += MEM_INC;
|
2010-07-23 17:01:51 +08:00
|
|
|
|
free(p);
|
2008-12-03 04:58:55 +08:00
|
|
|
|
*avp = p = new;
|
|
|
|
|
}
|
|
|
|
|
p[ac++] = c;
|
|
|
|
|
}
|
2010-07-26 08:03:31 +08:00
|
|
|
|
} else {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
c++;
|
2010-07-26 08:03:31 +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)
|
|
|
|
|
return ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if ((p = strdup(p)) == NULL)
|
|
|
|
|
return CSstay;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
ac = argify(p, &av);
|
|
|
|
|
|
|
|
|
|
if (Repeat != NO_ARG)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
s = Repeat < ac ? insert_string(av[Repeat]) : 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-07-26 04:17:10 +08:00
|
|
|
|
static el_keymap_t Map[33] = {
|
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 },
|
|
|
|
|
{ CTL('G'), ring_bell },
|
|
|
|
|
{ CTL('H'), bk_del_char },
|
|
|
|
|
{ CTL('I'), c_complete },
|
|
|
|
|
{ CTL('J'), accept_line },
|
|
|
|
|
{ CTL('K'), kill_line },
|
|
|
|
|
{ CTL('L'), redisplay },
|
|
|
|
|
{ CTL('M'), accept_line },
|
|
|
|
|
{ CTL('N'), h_next },
|
|
|
|
|
{ CTL('O'), ring_bell },
|
|
|
|
|
{ CTL('P'), h_prev },
|
|
|
|
|
{ CTL('Q'), ring_bell },
|
|
|
|
|
{ CTL('R'), h_search },
|
|
|
|
|
{ CTL('S'), ring_bell },
|
|
|
|
|
{ CTL('T'), transpose },
|
|
|
|
|
{ CTL('U'), ring_bell },
|
|
|
|
|
{ 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
|
|
|
|
|
{ CTL('Z'), ring_bell },
|
|
|
|
|
#endif
|
2008-12-03 04:58:55 +08:00
|
|
|
|
{ CTL('['), meta },
|
|
|
|
|
{ CTL(']'), move_to_char },
|
|
|
|
|
{ CTL('^'), ring_bell },
|
|
|
|
|
{ CTL('_'), ring_bell },
|
|
|
|
|
{ 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
|
|
|
|
};
|
|
|
|
|
|
2010-07-26 04:17:10 +08:00
|
|
|
|
void el_bind_key_in_metamap(char c, el_keymap_func_t func)
|
|
|
|
|
{
|
|
|
|
|
/* Add given function to key map for META keys */
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; MetaMap[i].Key != 0; i++)
|
|
|
|
|
{
|
|
|
|
|
if (MetaMap[i].Key == c)
|
|
|
|
|
{
|
|
|
|
|
MetaMap[i].Function = func;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* A new key so have to add it to end */
|
|
|
|
|
if (i == 63)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr,"editline: MetaMap table full, requires increase\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MetaMap[i].Function = func;
|
|
|
|
|
MetaMap[i].Key = c;
|
|
|
|
|
MetaMap[i + 1].Function = 0; /* Zero the last location */
|
|
|
|
|
MetaMap[i + 1].Key = 0; /* Zero the last location */
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-19 00:57:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* Local Variables:
|
|
|
|
|
* version-control: t
|
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
|
* c-file-style: "ellemtel"
|
|
|
|
|
* c-basic-offset: 4
|
|
|
|
|
* End:
|
|
|
|
|
*/
|