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)
|
2010-07-18 11:19:03 +08:00
|
|
|
|
#ifndef HIST_SIZE /* Default to one line history, i.e. disabled. */
|
|
|
|
|
#define HIST_SIZE 1
|
|
|
|
|
#endif
|
2008-12-03 04:58:55 +08:00
|
|
|
|
#define SEPS "\"#$&'()*:;<=>?[\\]^`{|}~\n\t "
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Command status codes.
|
|
|
|
|
*/
|
2008-12-03 04:58:55 +08:00
|
|
|
|
typedef enum {
|
2008-06-07 18:28:36 +08:00
|
|
|
|
CSdone, CSeof, CSmove, CSdispatch, CSstay, CSsignal
|
2008-12-03 04:58:55 +08:00
|
|
|
|
} el_status_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 {
|
2008-06-07 18:28:36 +08:00
|
|
|
|
TOupper, TOlower
|
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;
|
|
|
|
|
const char *Lines[HIST_SIZE];
|
|
|
|
|
} el_hist_t;
|
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
|
|
|
|
|
|
|
|
|
static const char NIL[] = "";
|
|
|
|
|
static const char *Input = NIL;
|
|
|
|
|
static char *Line;
|
|
|
|
|
static const char *Prompt;
|
|
|
|
|
static char *Yanked;
|
|
|
|
|
static char *Screen;
|
|
|
|
|
static char NEWLINE[]= CRLF;
|
|
|
|
|
static el_hist_t H;
|
|
|
|
|
static int Repeat;
|
|
|
|
|
static int End;
|
|
|
|
|
static int Mark;
|
|
|
|
|
static int OldPoint;
|
|
|
|
|
static int Point;
|
|
|
|
|
static int PushBack;
|
|
|
|
|
static int Pushed;
|
|
|
|
|
static int Signal;
|
2010-03-10 04:18:03 +08:00
|
|
|
|
static el_keymap_t Map[];
|
|
|
|
|
static el_keymap_t MetaMap[];
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static SIZE_T Length;
|
|
|
|
|
static SIZE_T ScreenCount;
|
|
|
|
|
static SIZE_T ScreenSize;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
/* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
|
2008-12-03 04:58:55 +08:00
|
|
|
|
int rl_meta_chars = 1;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2008-10-02 15:09:09 +08:00
|
|
|
|
/* User definable callbacks. */
|
|
|
|
|
char *(*rl_complete)(char *token, int *match);
|
|
|
|
|
int (*rl_list_possib)(char *token, char ***av);
|
|
|
|
|
|
|
|
|
|
|
2008-06-07 18:28:36 +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
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** 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) {
|
2009-02-09 05:09:02 +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;
|
|
|
|
|
RENEW(Screen, 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
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static void tty_show(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('?');
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
else if (ISCTL(c)) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_put('^');
|
|
|
|
|
tty_put(UNCTL(c));
|
2008-06-07 18:28:36 +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));
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_put(c);
|
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
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static int tty_get(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
|
|
|
|
int r;
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_flush();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (Pushed) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
Pushed = 0;
|
|
|
|
|
return PushBack;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
if (*Input)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return *Input++;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
do
|
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
r= read(0, &c, (SIZE_T)1);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
} while (r == -1 && errno == EINTR);
|
|
|
|
|
return r == 1 ? c : EOF;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2008-12-03 04:58:55 +08:00
|
|
|
|
char *term;
|
|
|
|
|
char buff[2048];
|
|
|
|
|
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)) {
|
|
|
|
|
if ((backspace = tgetstr("le", &bp)) != NULL)
|
|
|
|
|
backspace = strdup(backspace);
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** 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
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
int i;
|
|
|
|
|
char *p;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_put('\r');
|
|
|
|
|
tty_puts(Prompt);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
for (i = Point, p = Line; --i >= 0; p++)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_show(*p);
|
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
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_back();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (Point) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (ISCTL(Line[Point - 1]))
|
|
|
|
|
tty_back();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
else if (rl_meta_chars && ISMETA(Line[Point - 1])) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_back();
|
|
|
|
|
tty_back();
|
|
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
if (Change == CSmove)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
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
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_show(Line[Point]);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (Change == CSmove)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
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';
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if ((Input = (char *)getenv((char *)name)) == NULL) {
|
|
|
|
|
Input = NIL;
|
|
|
|
|
return ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
return CSstay;
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
p = &Line[Point];
|
|
|
|
|
for ( ; Point < End && (*p == ' ' || !isalnum(*p)); Point++, p++)
|
|
|
|
|
if (move == CSmove)
|
|
|
|
|
right(CSstay);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
for (; Point < End && isalnum(*p); Point++, p++)
|
|
|
|
|
if (move == CSmove)
|
|
|
|
|
right(CSstay);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (Point == End)
|
|
|
|
|
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);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (OldPoint != Point) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if ((count = Point - OldPoint) < 0)
|
|
|
|
|
count = -count;
|
|
|
|
|
Point = OldPoint;
|
|
|
|
|
if ((end = Point + count) > End)
|
|
|
|
|
end = End;
|
|
|
|
|
for (i = Point, p = &Line[i]; i < end; i++, p++) {
|
|
|
|
|
if (type == TOupper) {
|
|
|
|
|
if (islower(*p))
|
|
|
|
|
*p = toupper(*p);
|
|
|
|
|
}
|
2010-07-18 07:41: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
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
for (extras = 0, i = Point, p = &Line[i]; i <= End; i++, p++) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_put(' ');
|
|
|
|
|
if (ISCTL(*p)) {
|
|
|
|
|
tty_put(' ');
|
|
|
|
|
extras++;
|
|
|
|
|
}
|
|
|
|
|
else if (rl_meta_chars && ISMETA(*p)) {
|
|
|
|
|
tty_put(' ');
|
|
|
|
|
tty_put(' ');
|
|
|
|
|
extras += 2;
|
|
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i += extras; i > 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
|
|
|
|
{
|
|
|
|
|
Point = -strlen(Prompt);
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_put('\r');
|
2008-06-07 18:28:36 +08:00
|
|
|
|
ceol();
|
|
|
|
|
Point = 0;
|
|
|
|
|
End = 0;
|
|
|
|
|
Line[0] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
SIZE_T len;
|
|
|
|
|
int i;
|
|
|
|
|
char *new;
|
|
|
|
|
char *q;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
len = strlen((char *)p);
|
|
|
|
|
if (End + len >= Length) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if ((new = NEW(char, Length + len + MEM_INC)) == NULL)
|
|
|
|
|
return CSstay;
|
|
|
|
|
if (Length) {
|
|
|
|
|
COPYFROMTO(new, Line, Length);
|
|
|
|
|
DISPOSE(Line);
|
|
|
|
|
}
|
|
|
|
|
Line = new;
|
|
|
|
|
Length += len + MEM_INC;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (q = &Line[Point], i = End - Point; --i >= 0; )
|
2008-12-03 04:58:55 +08:00
|
|
|
|
q[len + i] = q[i];
|
2008-06-07 18:28:36 +08:00
|
|
|
|
COPYFROMTO(&Line[Point], p, len);
|
|
|
|
|
End += len;
|
|
|
|
|
Line[End] = '\0';
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_string(&Line[Point]);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
Point += len;
|
|
|
|
|
|
|
|
|
|
return Point == End ? CSstay : CSmove;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static el_status_t redisplay(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_puts(NEWLINE);
|
|
|
|
|
tty_puts(Prompt);
|
|
|
|
|
tty_string(Line);
|
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();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
Point = 0;
|
|
|
|
|
reposition();
|
|
|
|
|
ceol();
|
|
|
|
|
End = 0;
|
|
|
|
|
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 {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if ((p = (*move)()) == NULL)
|
|
|
|
|
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
|
|
|
|
{
|
2008-12-03 04:58:55 +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);
|
2008-12-03 04:58:55 +08:00
|
|
|
|
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)
|
|
|
|
|
DISPOSE(old_search);
|
|
|
|
|
old_search = (char *)strdup((char *)search);
|
2008-06-07 18:28:36 +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;
|
|
|
|
|
pat = (char *)(search + 1);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
match = substrcmp;
|
|
|
|
|
pat = (char *)search;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
len = strlen(pat);
|
|
|
|
|
|
|
|
|
|
for (pos = H.Pos; (*move)() != NULL; )
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if ((*match)((char *)H.Lines[H.Pos], pat, len) == 0)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
return H.Lines[H.Pos];
|
|
|
|
|
H.Pos = pos;
|
|
|
|
|
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();
|
|
|
|
|
old_prompt = Prompt;
|
|
|
|
|
Prompt = "Search: ";
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_puts(Prompt);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
move = Repeat == NO_ARG ? prev_hist : next_hist;
|
|
|
|
|
p = editinput();
|
|
|
|
|
Prompt = old_prompt;
|
|
|
|
|
Searching = 0;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_puts(Prompt);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (p == NULL && Signal > 0) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
Signal = 0;
|
|
|
|
|
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 {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (Point >= End)
|
|
|
|
|
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) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
DISPOSE(Yanked);
|
|
|
|
|
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
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if ((Yanked = NEW(char, (SIZE_T)i + 1)) != NULL) {
|
|
|
|
|
COPYFROMTO(Yanked, &Line[begin], i);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
if (count <= 0 || End == Point)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
if (count == 1 && Point == End - 1) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
/* Optimize common case of delete at end of line. */
|
|
|
|
|
End--;
|
|
|
|
|
p = &Line[Point];
|
|
|
|
|
i = 1;
|
|
|
|
|
tty_put(' ');
|
|
|
|
|
if (ISCTL(*p)) {
|
|
|
|
|
i = 2;
|
|
|
|
|
tty_put(' ');
|
|
|
|
|
}
|
|
|
|
|
else if (rl_meta_chars && ISMETA(*p)) {
|
|
|
|
|
i = 3;
|
|
|
|
|
tty_put(' ');
|
|
|
|
|
tty_put(' ');
|
|
|
|
|
}
|
|
|
|
|
tty_backn(i);
|
|
|
|
|
*p = '\0';
|
|
|
|
|
return CSmove;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
if (Point + count > End && (count = End - Point) <= 0)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return CSstay;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
if (count > 1)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
save_yank(Point, count);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
for (p = &Line[Point], i = End - (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();
|
|
|
|
|
End -= count;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_string(&Line[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 {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (Point == 0)
|
|
|
|
|
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 {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (Point == 0)
|
|
|
|
|
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) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (Repeat < Point) {
|
|
|
|
|
i = Point;
|
|
|
|
|
Point = Repeat;
|
|
|
|
|
reposition();
|
2010-07-18 07:41:18 +08:00
|
|
|
|
delete_string(i - Point);
|
2008-12-03 04:58:55 +08:00
|
|
|
|
}
|
|
|
|
|
else if (Repeat > Point) {
|
|
|
|
|
right(CSmove);
|
2010-07-18 07:41:18 +08:00
|
|
|
|
delete_string(Repeat - Point - 1);
|
2008-12-03 04:58:55 +08:00
|
|
|
|
}
|
|
|
|
|
return CSmove;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
save_yank(Point, End - Point);
|
|
|
|
|
Line[Point] = '\0';
|
|
|
|
|
ceol();
|
|
|
|
|
End = Point;
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if ((p = NEW(char, Repeat + 1)) == NULL)
|
|
|
|
|
return CSstay;
|
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);
|
|
|
|
|
DISPOSE(p);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-09 05:09:02 +08:00
|
|
|
|
static el_status_t beg_line(void)
|
|
|
|
|
{
|
|
|
|
|
if (Point) {
|
|
|
|
|
Point = 0;
|
|
|
|
|
return CSmove;
|
|
|
|
|
}
|
|
|
|
|
return CSstay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static el_status_t end_line(void)
|
|
|
|
|
{
|
|
|
|
|
if (Point != End) {
|
|
|
|
|
Point = End;
|
|
|
|
|
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-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') {
|
|
|
|
|
c = tty_get();
|
|
|
|
|
// printf ("E[%c\n", c);
|
|
|
|
|
switch (c) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
default: return ring_bell();
|
|
|
|
|
case EOF: return CSeof;
|
2009-02-09 05:09:02 +08:00
|
|
|
|
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 */
|
2008-12-03 04:58:55 +08:00
|
|
|
|
}
|
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';
|
|
|
|
|
Pushed = 1;
|
|
|
|
|
PushBack = c;
|
|
|
|
|
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);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
for (kp = MetaMap; kp->Function; kp++)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (kp->Key == c)
|
|
|
|
|
return (*kp->Function)();
|
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
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
el_status_t s;
|
|
|
|
|
el_keymap_t *kp;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2008-06-10 03:37:01 +08:00
|
|
|
|
#if 0 /* Debian patch removes this to be able to handle 8-bit input */
|
|
|
|
|
/* This test makes it impossible to enter eight-bit characters when
|
|
|
|
|
* meta-char mode is enabled. */
|
2008-06-07 18:28:36 +08:00
|
|
|
|
OldPoint = Point;
|
|
|
|
|
if (rl_meta_chars && ISMETA(c)) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
Pushed = 1;
|
|
|
|
|
PushBack = UNMETA(c);
|
|
|
|
|
return meta();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
2008-06-10 03:37:01 +08:00
|
|
|
|
#endif /* Debian patch removal. */
|
2008-06-07 18:28:36 +08:00
|
|
|
|
for (kp = Map; kp->Function; kp++)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (kp->Key == c)
|
|
|
|
|
break;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
s = kp->Function ? (*kp->Function)() : insert_char((int)c);
|
|
|
|
|
if (!Pushed)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
/* No pushback means no repeat count; hacky, but true. */
|
|
|
|
|
Repeat = NO_ARG;
|
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
|
|
|
|
{
|
2008-06-10 03:37:01 +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) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (Point != 0) {
|
|
|
|
|
Point = 0;
|
|
|
|
|
reposition();
|
|
|
|
|
}
|
|
|
|
|
Repeat = NO_ARG;
|
|
|
|
|
return kill_line();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
if (c == rl_eof && Point == 0 && End == 0)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return CSeof;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (c == rl_intr) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
Signal = SIGINT;
|
|
|
|
|
return CSsignal;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
if (c == rl_quit) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
Signal = SIGQUIT;
|
|
|
|
|
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) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
Signal = SIGTSTP;
|
|
|
|
|
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;
|
|
|
|
|
OldPoint = Point = Mark = End = 0;
|
|
|
|
|
Line[0] = '\0';
|
|
|
|
|
|
|
|
|
|
Signal = -1;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
while ((c = tty_get()) != EOF)
|
|
|
|
|
switch (tty_special(c)) {
|
|
|
|
|
case CSdone:
|
|
|
|
|
return Line;
|
|
|
|
|
case CSeof:
|
|
|
|
|
return NULL;
|
|
|
|
|
case CSsignal:
|
|
|
|
|
return (char *)"";
|
|
|
|
|
case CSmove:
|
|
|
|
|
reposition();
|
|
|
|
|
break;
|
|
|
|
|
case CSdispatch:
|
|
|
|
|
switch (emacs(c)) {
|
|
|
|
|
case CSdone:
|
|
|
|
|
return Line;
|
|
|
|
|
case CSeof:
|
|
|
|
|
return NULL;
|
|
|
|
|
case CSsignal:
|
|
|
|
|
return (char *)"";
|
|
|
|
|
case CSmove:
|
|
|
|
|
reposition();
|
|
|
|
|
break;
|
|
|
|
|
case CSdispatch:
|
|
|
|
|
case CSstay:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case CSstay:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +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;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if ((p = (char *)strdup((char *)p)) == NULL)
|
|
|
|
|
return;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (H.Size < HIST_SIZE)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
H.Lines[H.Size++] = p;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
else {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
DISPOSE(H.Lines[0]);
|
|
|
|
|
for (i = 0; i < HIST_SIZE - 1; i++)
|
|
|
|
|
H.Lines[i] = H.Lines[i + 1];
|
|
|
|
|
H.Lines[i] = p;
|
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
|
|
|
|
|
2008-06-10 04:55:13 +08:00
|
|
|
|
p = line = NEW(char, size);
|
|
|
|
|
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;
|
|
|
|
|
p = RENEW(line, char, size);
|
|
|
|
|
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';
|
|
|
|
|
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-18 07:41:18 +08:00
|
|
|
|
#ifdef CONFIG_DEFAULT_COMPLETE
|
2008-10-02 15:09:09 +08:00
|
|
|
|
int done = 0;
|
|
|
|
|
|
|
|
|
|
if (!done)
|
|
|
|
|
{
|
|
|
|
|
if (!rl_complete)
|
|
|
|
|
rl_complete = &default_rl_complete;
|
|
|
|
|
|
|
|
|
|
if (!rl_list_possib)
|
|
|
|
|
rl_list_possib = &default_rl_list_possib;
|
|
|
|
|
|
|
|
|
|
done = 1;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
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
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
char *line;
|
|
|
|
|
int s;
|
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
|
|
|
|
}
|
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (Line == NULL) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
Length = MEM_INC;
|
|
|
|
|
if ((Line = NEW(char, Length)) == NULL)
|
|
|
|
|
return NULL;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
tty_info();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
rl_ttyset(0);
|
|
|
|
|
hist_add(NIL);
|
|
|
|
|
ScreenSize = SCREEN_INC;
|
|
|
|
|
Screen = NEW(char, ScreenSize);
|
2008-12-03 04:58:55 +08:00
|
|
|
|
Prompt = prompt ? prompt : NIL;
|
|
|
|
|
tty_puts(Prompt);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if ((line = editinput()) != NULL) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
line = strdup(line);
|
|
|
|
|
tty_puts(NEWLINE);
|
|
|
|
|
tty_flush();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
rl_ttyset(1);
|
|
|
|
|
DISPOSE(Screen);
|
|
|
|
|
DISPOSE(H.Lines[--H.Size]);
|
|
|
|
|
|
|
|
|
|
if (line != NULL && *line != '\0'
|
2010-07-19 10:13:11 +08:00
|
|
|
|
#ifdef CONFIG_UNIQUE_HISTORY
|
2008-12-03 04:58:55 +08:00
|
|
|
|
&& !(H.Pos && strcmp((char *) line, (char *) H.Lines[H.Pos - 1]) == 0)
|
2010-07-19 10:13:11 +08:00
|
|
|
|
#endif
|
2008-12-03 04:58:55 +08:00
|
|
|
|
&& !(H.Size && strcmp((char *) line, (char *) H.Lines[H.Size - 1]) == 0)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
hist_add(line);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Signal > 0) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
s = Signal;
|
|
|
|
|
Signal = 0;
|
2010-07-18 07:41:18 +08:00
|
|
|
|
kill(getpid(), s);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
return (char *)line;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
void add_history(char *p __attribute__ ((unused)))
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
#ifdef obsolete /* Made part of readline(). -- kjb */
|
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-07-19 10:13:11 +08:00
|
|
|
|
#ifdef CONFIG_UNIQUE_HISTORY
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (H.Pos && strcmp(p, (char *) H.Lines[H.Pos - 1]) == 0)
|
|
|
|
|
return;
|
2010-07-19 10:13:11 +08:00
|
|
|
|
#endif
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (H.Size && strcmp(p, (char *) H.Lines[H.Size - 1]) == 0)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return;
|
|
|
|
|
hist_add((char *)p);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** 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;
|
|
|
|
|
SIZE_T len;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
p = &Line[Point];
|
|
|
|
|
while (p > Line) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
p--;
|
|
|
|
|
if (p > Line && p[-1] == '\\') {
|
|
|
|
|
p--;
|
|
|
|
|
} else {
|
|
|
|
|
if (strchr(SEPS, (char) *p) != NULL) {
|
|
|
|
|
p++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
len = Point - (p - Line) + 1;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if ((new = NEW(char, len)) == NULL)
|
|
|
|
|
return NULL;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
q = new;
|
|
|
|
|
while (p < &Line[Point]) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (*p == '\\') {
|
|
|
|
|
if (++p == &Line[Point]) break;
|
|
|
|
|
}
|
|
|
|
|
*q++ = *p++;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
*q = '\0';
|
|
|
|
|
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
|
|
|
|
|
2008-10-02 15:09:09 +08:00
|
|
|
|
if (!rl_list_possib) {
|
|
|
|
|
return ring_bell();
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-07 18:28:36 +08:00
|
|
|
|
word = find_word();
|
|
|
|
|
ac = rl_list_possib((char *)word, (char ***)&av);
|
|
|
|
|
if (word)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
DISPOSE(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)
|
|
|
|
|
DISPOSE(av[ac]);
|
|
|
|
|
DISPOSE(av);
|
|
|
|
|
return CSmove;
|
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;
|
|
|
|
|
SIZE_T len;
|
|
|
|
|
int unique;
|
2009-02-09 03:19:58 +08:00
|
|
|
|
el_status_t s = 0;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2008-10-02 15:09:09 +08:00
|
|
|
|
if (!rl_complete) {
|
|
|
|
|
return ring_bell();
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
DISPOSE(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;
|
|
|
|
|
new = q = NEW(char, 2 * len + 1);
|
|
|
|
|
while (*p) {
|
|
|
|
|
if ((*p < ' ' || strchr(SEPS, (char) *p) != NULL)
|
|
|
|
|
&& (!unique || p[1] != 0)) {
|
|
|
|
|
*q++ = '\\';
|
|
|
|
|
}
|
|
|
|
|
*q++ = *p++;
|
|
|
|
|
}
|
|
|
|
|
*q = '\0';
|
|
|
|
|
DISPOSE(word);
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
DISPOSE(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
|
|
|
|
{
|
|
|
|
|
Line[End] = '\0';
|
|
|
|
|
return CSdone;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
if (Point) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (Point == End)
|
|
|
|
|
left(CSmove);
|
|
|
|
|
c = Line[Point - 1];
|
|
|
|
|
left(CSstay);
|
|
|
|
|
Line[Point - 1] = Line[Point];
|
|
|
|
|
tty_show(Line[Point - 1]);
|
|
|
|
|
Line[Point++] = c;
|
|
|
|
|
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 wipe(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 (Mark > End)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
if (Point > Mark) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
i = Point;
|
|
|
|
|
Point = Mark;
|
|
|
|
|
Mark = i;
|
|
|
|
|
reposition();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return delete_string(Mark - Point);
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
static el_status_t mk_set(void)
|
2008-06-07 18:28:36 +08:00
|
|
|
|
{
|
|
|
|
|
Mark = Point;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
if ((c = Mark) <= End) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
Mark = Point;
|
|
|
|
|
Point = c;
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
if (Mark > End)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return ring_bell();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
|
|
|
|
if (Point > Mark)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
save_yank(Mark, Point - Mark);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
else
|
2008-12-03 04:58:55 +08:00
|
|
|
|
save_yank(Point, Mark - 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;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
for (i = Point + 1, p = &Line[i]; i < End; i++, p++)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (*p == c) {
|
|
|
|
|
Point = i;
|
|
|
|
|
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);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (OldPoint != Point) {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
i = Point - OldPoint;
|
|
|
|
|
Point = OldPoint;
|
|
|
|
|
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 {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
for (p = &Line[Point]; p > Line && !isalnum(p[-1]); p--)
|
|
|
|
|
left(CSmove);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
for (; p > Line && p[-1] != ' ' && isalnum(p[-1]); p--)
|
|
|
|
|
left(CSmove);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if (Point == 0)
|
|
|
|
|
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();
|
2008-06-07 18:28:36 +08:00
|
|
|
|
if (OldPoint != Point)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
return delete_string(OldPoint - 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;
|
2008-12-03 04:58:55 +08:00
|
|
|
|
if ((*avp = p = NEW(char *, i))== NULL)
|
|
|
|
|
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) {
|
|
|
|
|
new = NEW(char *, i + MEM_INC);
|
|
|
|
|
if (new == NULL) {
|
|
|
|
|
p[ac] = NULL;
|
|
|
|
|
return ac;
|
|
|
|
|
}
|
|
|
|
|
COPYFROMTO(new, p, i * sizeof (char **));
|
|
|
|
|
i += MEM_INC;
|
|
|
|
|
DISPOSE(p);
|
|
|
|
|
*avp = p = new;
|
|
|
|
|
}
|
|
|
|
|
p[ac++] = c;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
c++;
|
2008-06-07 18:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
*c = '\0';
|
|
|
|
|
p[ac] = NULL;
|
|
|
|
|
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
|
|
|
|
{
|
2008-12-03 04:58:55 +08:00
|
|
|
|
char **av;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
if (ac)
|
2008-12-03 04:58:55 +08:00
|
|
|
|
DISPOSE(av);
|
2008-06-07 18:28:36 +08:00
|
|
|
|
DISPOSE(p);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-10 04:18:03 +08:00
|
|
|
|
static el_keymap_t Map[] = {
|
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 },
|
|
|
|
|
{ CTL('Z'), end_line },
|
|
|
|
|
{ CTL('['), meta },
|
|
|
|
|
{ CTL(']'), move_to_char },
|
|
|
|
|
{ CTL('^'), ring_bell },
|
|
|
|
|
{ CTL('_'), ring_bell },
|
|
|
|
|
{ 0, NULL }
|
2008-06-07 18:28:36 +08:00
|
|
|
|
};
|
|
|
|
|
|
2010-03-10 04:18:03 +08:00
|
|
|
|
static el_keymap_t MetaMap[]= {
|
2008-12-03 04:58:55 +08:00
|
|
|
|
{ CTL('H'), wipe },
|
|
|
|
|
{ DEL, wipe },
|
|
|
|
|
{ ' ', mk_set },
|
|
|
|
|
{ '.', last_argument },
|
|
|
|
|
{ '<', h_first },
|
|
|
|
|
{ '>', h_last },
|
|
|
|
|
{ '?', c_possible },
|
|
|
|
|
{ 'b', bk_word },
|
|
|
|
|
{ '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-19 00:57:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* Local Variables:
|
|
|
|
|
* version-control: t
|
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
|
* c-file-style: "ellemtel"
|
|
|
|
|
* c-basic-offset: 4
|
|
|
|
|
* End:
|
|
|
|
|
*/
|