2015-09-05 21:19:12 +02:00
|
|
|
/* $OpenBSD: edit.c,v 1.41 2015/09/01 13:12:31 tedu Exp $ */
|
2011-06-04 18:44:30 +02:00
|
|
|
/* $OpenBSD: edit.h,v 1.9 2011/05/30 17:14:35 martynas Exp $ */
|
2015-10-09 18:11:19 +02:00
|
|
|
/* $OpenBSD: emacs.c,v 1.52 2015/09/10 22:48:58 nicm Exp $ */
|
|
|
|
/* $OpenBSD: vi.c,v 1.30 2015/09/10 22:48:58 nicm Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
2012-03-24 00:25:30 +01:00
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
|
2017-03-11 23:49:56 +01:00
|
|
|
* 2011, 2012, 2013, 2014, 2015, 2016, 2017
|
2015-10-09 18:11:19 +02:00
|
|
|
* mirabilos <m@mirbsd.org>
|
2009-05-16 18:59:42 +02:00
|
|
|
*
|
|
|
|
* Provided that these terms and disclaimer and all copyright notices
|
|
|
|
* are retained or reproduced in an accompanying document, permission
|
|
|
|
* is granted to deal in this work without restriction, including un-
|
|
|
|
* limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
* merge, give away, or sublicence.
|
|
|
|
*
|
|
|
|
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|
|
|
* the utmost extent permitted by applicable law, neither express nor
|
|
|
|
* implied; without malicious intent or gross negligence. In no event
|
|
|
|
* may a licensor, author or contributor be held liable for indirect,
|
|
|
|
* direct, other damage, loss, or other issues arising in any way out
|
|
|
|
* of dealing in the work, even if advised of the possibility of such
|
|
|
|
* damage or existence of a defect, except proven that it results out
|
|
|
|
* of said person's immediate fault when using the work as intended.
|
|
|
|
*/
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
#include "sh.h"
|
|
|
|
|
2012-05-04 22:49:08 +02:00
|
|
|
#ifndef MKSH_NO_CMDLINE_EDITING
|
|
|
|
|
2017-04-02 17:43:24 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.316 2017/04/02 15:43:24 tg Exp $");
|
2010-07-04 19:45:17 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* in later versions we might use libtermcap for this, but since external
|
|
|
|
* dependencies are problematic, this has not yet been decided on; another
|
|
|
|
* good string is "\033c" except on hardware terminals like the DEC VT420
|
|
|
|
* which do a full power cycle then...
|
|
|
|
*/
|
|
|
|
#ifndef MKSH_CLS_STRING
|
|
|
|
#define MKSH_CLS_STRING "\033[;H\033[J"
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* tty driver characters we are interested in */
|
2016-07-28 23:39:04 +02:00
|
|
|
#define EDCHAR_DISABLED 0xFFFFU
|
|
|
|
#define EDCHAR_INITIAL 0xFFFEU
|
|
|
|
static struct {
|
|
|
|
unsigned short erase;
|
|
|
|
unsigned short kill;
|
|
|
|
unsigned short werase;
|
|
|
|
unsigned short intr;
|
|
|
|
unsigned short quit;
|
|
|
|
unsigned short eof;
|
|
|
|
} edchars;
|
|
|
|
|
|
|
|
#define isched(x,e) ((unsigned short)(unsigned char)(x) == (e))
|
|
|
|
#define isedchar(x) (!((x) & ~0xFF))
|
|
|
|
#ifndef _POSIX_VDISABLE
|
|
|
|
#define toedchar(x) ((unsigned short)(unsigned char)(x))
|
|
|
|
#else
|
|
|
|
#define toedchar(x) (((_POSIX_VDISABLE != -1) && ((x) == _POSIX_VDISABLE)) ? \
|
|
|
|
((unsigned short)EDCHAR_DISABLED) : \
|
|
|
|
((unsigned short)(unsigned char)(x)))
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-06-04 18:44:30 +02:00
|
|
|
/* x_cf_glob() flags */
|
2005-05-23 05:06:10 +02:00
|
|
|
#define XCF_COMMAND BIT(0) /* Do command completion */
|
|
|
|
#define XCF_FILE BIT(1) /* Do file completion */
|
|
|
|
#define XCF_FULLPATH BIT(2) /* command completion: store full path */
|
2011-06-04 17:06:50 +02:00
|
|
|
#define XCF_COMMAND_FILE (XCF_COMMAND | XCF_FILE)
|
2011-02-03 16:57:52 +01:00
|
|
|
#define XCF_IS_COMMAND BIT(3) /* return flag: is command */
|
2012-08-24 22:57:46 +02:00
|
|
|
#define XCF_IS_NOSPACE BIT(4) /* return flag: do not append a space */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2010-07-04 19:45:17 +02:00
|
|
|
static char editmode;
|
2010-07-25 13:35:43 +02:00
|
|
|
static int xx_cols; /* for Emacs mode */
|
2008-09-20 16:10:23 +02:00
|
|
|
static int modified; /* buffer has been "modified" */
|
2013-08-10 15:44:33 +02:00
|
|
|
static char *holdbufp; /* place to hold last edit buffer */
|
2008-09-20 16:10:23 +02:00
|
|
|
|
2016-07-25 22:43:54 +02:00
|
|
|
/* 0=dumb 1=tmux (for now) */
|
|
|
|
static bool x_term_mode;
|
|
|
|
|
|
|
|
static void x_adjust(void);
|
2007-06-07 01:28:17 +02:00
|
|
|
static int x_getc(void);
|
2007-10-25 17:19:16 +02:00
|
|
|
static void x_putcf(int);
|
2012-07-01 17:36:18 +02:00
|
|
|
static void x_modified(void);
|
2011-05-29 04:18:57 +02:00
|
|
|
static void x_mode(bool);
|
2011-08-27 20:06:52 +02:00
|
|
|
static int x_do_comment(char *, ssize_t, ssize_t *);
|
2012-12-28 03:28:39 +01:00
|
|
|
static void x_print_expansions(int, char * const *, bool);
|
2011-02-03 16:57:52 +01:00
|
|
|
static int x_cf_glob(int *, const char *, int, int, int *, int *, char ***);
|
2012-12-28 03:28:39 +01:00
|
|
|
static size_t x_longest_prefix(int, char * const *);
|
2012-07-20 20:39:53 +02:00
|
|
|
static void x_glob_hlp_add_qchar(char *);
|
2012-10-21 20:33:46 +02:00
|
|
|
static char *x_glob_hlp_tilde_and_rem_qchar(char *, bool);
|
2016-07-28 23:39:04 +02:00
|
|
|
static size_t x_basename(const char *, const char *);
|
2007-06-07 01:28:17 +02:00
|
|
|
static void x_free_words(int, char **);
|
|
|
|
static int x_escape(const char *, size_t, int (*)(const char *, size_t));
|
2013-08-10 15:44:33 +02:00
|
|
|
static int x_emacs(char *);
|
2013-08-16 12:59:03 +02:00
|
|
|
static void x_init_prompt(bool);
|
2009-09-24 19:15:33 +02:00
|
|
|
#if !MKSH_S_NOVI
|
2013-08-10 15:44:33 +02:00
|
|
|
static int x_vi(char *);
|
2007-06-16 00:00:00 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-10-25 17:19:16 +02:00
|
|
|
#define x_flush() shf_flush(shl_out)
|
2012-07-01 17:55:00 +02:00
|
|
|
#if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
|
2010-07-04 19:45:17 +02:00
|
|
|
#define x_putc(c) x_putcf(c)
|
|
|
|
#else
|
2007-10-25 17:19:16 +02:00
|
|
|
#define x_putc(c) shf_putc((c), shl_out)
|
2010-07-04 19:45:17 +02:00
|
|
|
#endif
|
2007-10-25 17:19:16 +02:00
|
|
|
|
2012-08-24 22:05:13 +02:00
|
|
|
static int path_order_cmp(const void *, const void *);
|
2006-08-01 14:44:17 +02:00
|
|
|
static void glob_table(const char *, XPtrV *, struct table *);
|
2012-08-24 22:05:13 +02:00
|
|
|
static void glob_path(int, const char *, XPtrV *, const char *);
|
|
|
|
static int x_file_glob(int *, char *, char ***);
|
2011-02-03 16:57:52 +01:00
|
|
|
static int x_command_glob(int, char *, char ***);
|
2008-04-19 19:21:55 +02:00
|
|
|
static int x_locate_word(const char *, int, int, int *, bool *);
|
|
|
|
|
|
|
|
static int x_e_getmbc(char *);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* +++ generic editing functions +++ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* read an edited command line
|
|
|
|
*/
|
|
|
|
int
|
2013-08-10 15:44:33 +02:00
|
|
|
x_read(char *buf)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
int i;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
x_mode(true);
|
2008-09-20 16:10:23 +02:00
|
|
|
modified = 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (Flag(FEMACS) || Flag(FGMACS))
|
2013-08-10 15:44:33 +02:00
|
|
|
i = x_emacs(buf);
|
2009-09-24 19:15:33 +02:00
|
|
|
#if !MKSH_S_NOVI
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (Flag(FVI))
|
2013-08-10 15:44:33 +02:00
|
|
|
i = x_vi(buf);
|
2007-06-16 00:00:00 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
2011-02-03 16:57:52 +01:00
|
|
|
/* internal error */
|
|
|
|
i = -1;
|
2010-07-04 19:45:17 +02:00
|
|
|
editmode = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
x_mode(false);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (i);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* tty I/O */
|
|
|
|
|
2007-06-07 01:28:17 +02:00
|
|
|
static int
|
2005-05-23 05:06:10 +02:00
|
|
|
x_getc(void)
|
|
|
|
{
|
2015-05-18 06:51:43 +02:00
|
|
|
#ifdef __OS2__
|
2017-03-26 00:18:21 +01:00
|
|
|
return (_read_kbd(0, 1, 0));
|
2015-05-18 06:51:43 +02:00
|
|
|
#else
|
2005-05-23 05:06:10 +02:00
|
|
|
char c;
|
2010-08-28 18:47:11 +02:00
|
|
|
ssize_t n;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
while ((n = blocking_read(STDIN_FILENO, &c, 1)) < 0 && errno == EINTR)
|
|
|
|
if (trap) {
|
|
|
|
x_mode(false);
|
|
|
|
runtraps(0);
|
2010-07-25 13:35:43 +02:00
|
|
|
#ifdef SIGWINCH
|
|
|
|
if (got_winch) {
|
|
|
|
change_winsz();
|
|
|
|
if (x_cols != xx_cols && editmode == 1) {
|
|
|
|
/* redraw line in Emacs mode */
|
|
|
|
xx_cols = x_cols;
|
2013-08-16 12:59:03 +02:00
|
|
|
x_init_prompt(false);
|
2016-07-25 22:43:54 +02:00
|
|
|
x_adjust();
|
2010-07-25 13:35:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
x_mode(true);
|
|
|
|
}
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return ((n == 1) ? (int)(unsigned char)c : -1);
|
2015-05-18 06:51:43 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2007-06-07 01:28:17 +02:00
|
|
|
static void
|
2007-10-25 17:19:16 +02:00
|
|
|
x_putcf(int c)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
shf_putc(c, shl_out);
|
|
|
|
}
|
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
/*********************************
|
|
|
|
* Misc common code for vi/emacs *
|
|
|
|
*********************************/
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*-
|
|
|
|
* Handle the commenting/uncommenting of a line.
|
2005-05-23 05:06:10 +02:00
|
|
|
* Returns:
|
|
|
|
* 1 if a carriage return is indicated (comment added)
|
|
|
|
* 0 if no return (comment removed)
|
|
|
|
* -1 if there is an error (not enough room for comment chars)
|
2009-06-10 20:12:51 +02:00
|
|
|
* If successful, *lenp contains the new length. Note: cursor should be
|
2005-05-23 05:06:10 +02:00
|
|
|
* moved to the start of the line after (un)commenting.
|
|
|
|
*/
|
2007-06-07 01:28:17 +02:00
|
|
|
static int
|
2011-08-27 20:06:52 +02:00
|
|
|
x_do_comment(char *buf, ssize_t bsize, ssize_t *lenp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-08-27 20:06:52 +02:00
|
|
|
ssize_t i, j, len = *lenp;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (len == 0)
|
2011-02-03 16:57:52 +01:00
|
|
|
/* somewhat arbitrary - it's what AT&T ksh does */
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* Already commented? */
|
|
|
|
if (buf[0] == '#') {
|
2008-04-19 19:21:55 +02:00
|
|
|
bool saw_nl = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
for (j = 0, i = 1; i < len; i++) {
|
|
|
|
if (!saw_nl || buf[i] != '#')
|
|
|
|
buf[j++] = buf[i];
|
|
|
|
saw_nl = buf[i] == '\n';
|
|
|
|
}
|
|
|
|
*lenp = j;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
int n = 1;
|
|
|
|
|
|
|
|
/* See if there's room for the #s - 1 per \n */
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
if (buf[i] == '\n')
|
|
|
|
n++;
|
|
|
|
if (len + n >= bsize)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Now add them... */
|
|
|
|
for (i = len, j = len + n; --i >= 0; ) {
|
|
|
|
if (buf[i] == '\n')
|
|
|
|
buf[--j] = '#';
|
|
|
|
buf[--j] = buf[i];
|
|
|
|
}
|
|
|
|
buf[0] = '#';
|
|
|
|
*lenp += n;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
/****************************************************
|
|
|
|
* Common file/command completion code for vi/emacs *
|
|
|
|
****************************************************/
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-06-07 01:28:17 +02:00
|
|
|
static void
|
2008-04-19 19:21:55 +02:00
|
|
|
x_print_expansions(int nwords, char * const *words, bool is_command)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2008-04-19 19:21:55 +02:00
|
|
|
bool use_copy = false;
|
2016-07-28 23:39:04 +02:00
|
|
|
size_t prefix_len;
|
2012-10-03 17:50:32 +02:00
|
|
|
XPtrV l = { NULL, 0, 0 };
|
2016-11-11 21:14:18 +01:00
|
|
|
struct columnise_opts co;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* Check if all matches are in the same directory (in this
|
2005-05-23 05:06:10 +02:00
|
|
|
* case, we want to omit the directory name)
|
|
|
|
*/
|
|
|
|
if (!is_command &&
|
|
|
|
(prefix_len = x_longest_prefix(nwords, words)) > 0) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Special case for 1 match (prefix is whole word) */
|
|
|
|
if (nwords == 1)
|
|
|
|
prefix_len = x_basename(words[0], NULL);
|
|
|
|
/* Any (non-trailing) slashes in non-common word suffixes? */
|
|
|
|
for (i = 0; i < nwords; i++)
|
|
|
|
if (x_basename(words[i] + prefix_len, NULL) >
|
|
|
|
prefix_len)
|
|
|
|
break;
|
|
|
|
/* All in same directory? */
|
|
|
|
if (i == nwords) {
|
2016-11-12 00:31:39 +01:00
|
|
|
while (prefix_len > 0 &&
|
|
|
|
!mksh_cdirsep(words[0][prefix_len - 1]))
|
2005-05-23 05:06:10 +02:00
|
|
|
prefix_len--;
|
2008-04-19 19:21:55 +02:00
|
|
|
use_copy = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
XPinit(l, nwords + 1);
|
|
|
|
for (i = 0; i < nwords; i++)
|
|
|
|
XPput(l, words[i] + prefix_len);
|
|
|
|
XPput(l, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Enumerate expansions
|
|
|
|
*/
|
|
|
|
x_putc('\r');
|
|
|
|
x_putc('\n');
|
2016-11-11 21:14:18 +01:00
|
|
|
co.shf = shl_out;
|
2016-11-11 22:13:25 +01:00
|
|
|
co.linesep = '\n';
|
|
|
|
co.do_last = true;
|
2016-11-11 21:14:18 +01:00
|
|
|
co.prefcol = false;
|
|
|
|
pr_list(&co, use_copy ? (char **)XPptrv(l) : words);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (use_copy)
|
2011-02-03 16:57:52 +01:00
|
|
|
/* not x_free_words() */
|
|
|
|
XPfree(l);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2012-07-20 20:39:53 +02:00
|
|
|
/*
|
|
|
|
* Convert backslash-escaped string to QCHAR-escaped
|
|
|
|
* string useful for globbing; loses QCHAR unless it
|
|
|
|
* can squeeze in, eg. by previous loss of backslash
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2012-07-20 20:39:53 +02:00
|
|
|
static void
|
|
|
|
x_glob_hlp_add_qchar(char *cp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2012-07-20 20:39:53 +02:00
|
|
|
char ch, *dp = cp;
|
|
|
|
bool escaping = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2012-07-20 20:39:53 +02:00
|
|
|
while ((ch = *cp++)) {
|
2012-03-23 22:15:34 +01:00
|
|
|
if (ch == '\\' && !escaping) {
|
2009-10-30 01:57:39 +01:00
|
|
|
escaping = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
2012-07-20 20:39:53 +02:00
|
|
|
if (escaping || (ch == QCHAR && (cp - dp) > 1)) {
|
2012-03-23 22:15:34 +01:00
|
|
|
/*
|
|
|
|
* empirically made list of chars to escape
|
2012-07-20 20:39:53 +02:00
|
|
|
* for globbing as well as QCHAR itself
|
2012-03-23 22:15:34 +01:00
|
|
|
*/
|
|
|
|
switch (ch) {
|
2012-07-20 20:39:53 +02:00
|
|
|
case QCHAR:
|
2012-03-23 22:15:34 +01:00
|
|
|
case '$':
|
|
|
|
case '*':
|
|
|
|
case '?':
|
|
|
|
case '[':
|
|
|
|
case '\\':
|
|
|
|
case '`':
|
2012-07-20 20:39:53 +02:00
|
|
|
*dp++ = QCHAR;
|
2012-03-23 22:15:34 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-10-30 01:57:39 +01:00
|
|
|
escaping = false;
|
2012-03-23 22:15:34 +01:00
|
|
|
}
|
2012-07-20 20:39:53 +02:00
|
|
|
*dp++ = ch;
|
|
|
|
}
|
|
|
|
*dp = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-10-21 20:33:46 +02:00
|
|
|
* Run tilde expansion on argument string, return the result
|
|
|
|
* after unescaping; if the flag is set, the original string
|
|
|
|
* is freed if changed and assumed backslash-escaped, if not
|
|
|
|
* it is assumed QCHAR-escaped
|
2012-07-20 20:39:53 +02:00
|
|
|
*/
|
2012-10-21 20:33:46 +02:00
|
|
|
static char *
|
|
|
|
x_glob_hlp_tilde_and_rem_qchar(char *s, bool magic_flag)
|
2012-07-20 20:39:53 +02:00
|
|
|
{
|
2012-10-21 20:33:46 +02:00
|
|
|
char ch, *cp, *dp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On the string, check whether we have a tilde expansion,
|
|
|
|
* and if so, discern "~foo/bar" and "~/baz" from "~blah";
|
|
|
|
* if we have a directory part (the former), try to expand
|
|
|
|
*/
|
2017-03-11 23:49:56 +01:00
|
|
|
if (*s == '~' && (cp = /* not sdirsep */ strchr(s, '/')) != NULL) {
|
2012-10-21 20:33:46 +02:00
|
|
|
/* ok, so split into "~foo"/"bar" or "~"/"baz" */
|
|
|
|
*cp++ = 0;
|
|
|
|
/* try to expand the tilde */
|
2014-11-19 22:49:12 +01:00
|
|
|
if (!(dp = do_tilde(s + 1))) {
|
2012-10-21 20:33:46 +02:00
|
|
|
/* nope, revert damage */
|
|
|
|
*--cp = '/';
|
|
|
|
} else {
|
|
|
|
/* ok, expand and replace */
|
2016-07-25 02:04:48 +02:00
|
|
|
cp = shf_smprintf(Tf_sSs, dp, cp);
|
2012-10-21 20:33:46 +02:00
|
|
|
if (magic_flag)
|
|
|
|
afree(s, ATEMP);
|
|
|
|
s = cp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ... convert it from backslash-escaped via QCHAR-escaped... */
|
|
|
|
if (magic_flag)
|
|
|
|
x_glob_hlp_add_qchar(s);
|
|
|
|
/* ... to unescaped, for comparison with the matches */
|
|
|
|
cp = dp = s;
|
2012-07-20 20:39:53 +02:00
|
|
|
|
|
|
|
while ((ch = *cp++)) {
|
|
|
|
if (ch == QCHAR && !(ch = *cp++))
|
|
|
|
break;
|
|
|
|
*dp++ = ch;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2012-07-20 20:39:53 +02:00
|
|
|
*dp = '\0';
|
2012-10-21 20:33:46 +02:00
|
|
|
|
|
|
|
return (s);
|
2012-07-20 20:39:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do file globbing:
|
|
|
|
* - does expansion, checks for no match, etc.
|
|
|
|
* - sets *wordsp to array of matching strings
|
|
|
|
* - returns number of matching strings
|
|
|
|
*/
|
|
|
|
static int
|
2012-08-24 22:05:13 +02:00
|
|
|
x_file_glob(int *flagsp, char *toglob, char ***wordsp)
|
2012-07-20 20:39:53 +02:00
|
|
|
{
|
2012-08-24 22:05:13 +02:00
|
|
|
char **words, *cp;
|
2012-07-20 20:39:53 +02:00
|
|
|
int nwords;
|
|
|
|
XPtrV w;
|
|
|
|
struct source *s, *sold;
|
|
|
|
|
|
|
|
/* remove all escaping backward slashes */
|
|
|
|
x_glob_hlp_add_qchar(toglob);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert "foo*" (toglob) to an array of strings (words)
|
|
|
|
*/
|
|
|
|
sold = source;
|
|
|
|
s = pushs(SWSTR, ATEMP);
|
|
|
|
s->start = s->str = toglob;
|
|
|
|
source = s;
|
2005-06-09 00:22:24 +02:00
|
|
|
if (yylex(ONEWORD | LQCHAR) != LWORD) {
|
2005-05-23 05:06:10 +02:00
|
|
|
source = sold;
|
2016-01-21 19:24:45 +01:00
|
|
|
internal_warningf(Tfg_badsubst);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
source = sold;
|
2012-08-24 21:14:56 +02:00
|
|
|
afree(s, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
XPinit(w, 32);
|
2012-08-24 22:05:13 +02:00
|
|
|
cp = yylval.cp;
|
|
|
|
while (*cp == CHAR || *cp == QCHAR)
|
|
|
|
cp += 2;
|
|
|
|
nwords = DOGLOB | DOTILDE | DOMARKDIRS;
|
|
|
|
if (*cp != EOS) {
|
|
|
|
/* probably a $FOO expansion */
|
2012-08-24 22:57:46 +02:00
|
|
|
*flagsp |= XCF_IS_NOSPACE;
|
|
|
|
/* this always results in at most one match */
|
2012-08-24 23:15:42 +02:00
|
|
|
nwords = 0;
|
2012-08-24 22:05:13 +02:00
|
|
|
}
|
|
|
|
expand(yylval.cp, &w, nwords);
|
2005-05-23 05:06:10 +02:00
|
|
|
XPput(w, NULL);
|
2005-06-09 00:22:24 +02:00
|
|
|
words = (char **)XPclose(w);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
for (nwords = 0; words[nwords]; nwords++)
|
|
|
|
;
|
|
|
|
if (nwords == 1) {
|
|
|
|
struct stat statb;
|
|
|
|
|
2012-10-21 20:33:46 +02:00
|
|
|
/* Expand any tilde and drop all QCHAR for comparison */
|
|
|
|
toglob = x_glob_hlp_tilde_and_rem_qchar(toglob, false);
|
2012-03-23 22:15:34 +01:00
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* Check if globbing failed (returned glob pattern),
|
|
|
|
* but be careful (e.g. toglob == "ab*" when the file
|
2005-05-23 05:06:10 +02:00
|
|
|
* "ab*" exists is not an error).
|
|
|
|
* Also, check for empty result - happens if we tried
|
|
|
|
* to glob something which evaluated to an empty
|
|
|
|
* string (e.g., "$FOO" when there is no FOO, etc).
|
|
|
|
*/
|
|
|
|
if ((strcmp(words[0], toglob) == 0 &&
|
|
|
|
stat(words[0], &statb) < 0) ||
|
|
|
|
words[0][0] == '\0') {
|
|
|
|
x_free_words(nwords, words);
|
2007-09-09 20:06:42 +02:00
|
|
|
words = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
nwords = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-09 20:06:42 +02:00
|
|
|
if ((*wordsp = nwords ? words : NULL) == NULL && words != NULL)
|
|
|
|
x_free_words(nwords, words);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (nwords);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Data structure used in x_command_glob() */
|
|
|
|
struct path_order_info {
|
|
|
|
char *word;
|
2016-07-28 23:39:04 +02:00
|
|
|
size_t base;
|
|
|
|
size_t path_order;
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Compare routine used in x_command_glob() */
|
|
|
|
static int
|
|
|
|
path_order_cmp(const void *aa, const void *bb)
|
|
|
|
{
|
2006-11-12 15:58:16 +01:00
|
|
|
const struct path_order_info *a = (const struct path_order_info *)aa;
|
|
|
|
const struct path_order_info *b = (const struct path_order_info *)bb;
|
2005-05-23 05:06:10 +02:00
|
|
|
int t;
|
|
|
|
|
2016-07-28 23:39:04 +02:00
|
|
|
if ((t = strcmp(a->word + a->base, b->word + b->base)))
|
|
|
|
return (t);
|
|
|
|
if (a->path_order > b->path_order)
|
|
|
|
return (1);
|
|
|
|
if (a->path_order < b->path_order)
|
|
|
|
return (-1);
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-02-03 16:57:52 +01:00
|
|
|
x_command_glob(int flags, char *toglob, char ***wordsp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-02-03 16:57:52 +01:00
|
|
|
char *pat, *fpath;
|
2012-10-03 17:50:32 +02:00
|
|
|
size_t nwords;
|
2005-05-23 05:06:10 +02:00
|
|
|
XPtrV w;
|
|
|
|
struct block *l;
|
|
|
|
|
|
|
|
/* Convert "foo*" (toglob) to a pattern for future use */
|
2005-06-09 00:22:24 +02:00
|
|
|
pat = evalstr(toglob, DOPAT | DOTILDE);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
XPinit(w, 32);
|
|
|
|
|
|
|
|
glob_table(pat, &w, &keywords);
|
|
|
|
glob_table(pat, &w, &aliases);
|
|
|
|
glob_table(pat, &w, &builtins);
|
|
|
|
for (l = e->loc; l; l = l->next)
|
|
|
|
glob_table(pat, &w, &l->funs);
|
|
|
|
|
|
|
|
glob_path(flags, pat, &w, path);
|
2016-07-25 02:04:48 +02:00
|
|
|
if ((fpath = str_val(global(TFPATH))) != null)
|
2005-05-23 05:06:10 +02:00
|
|
|
glob_path(flags, pat, &w, fpath);
|
|
|
|
|
|
|
|
nwords = XPsize(w);
|
|
|
|
|
|
|
|
if (!nwords) {
|
|
|
|
*wordsp = NULL;
|
|
|
|
XPfree(w);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
/* Sort entries */
|
|
|
|
if (flags & XCF_FULLPATH) {
|
|
|
|
/* Sort by basename, then path order */
|
2008-04-19 19:21:55 +02:00
|
|
|
struct path_order_info *info, *last_info = NULL;
|
2005-06-09 00:22:24 +02:00
|
|
|
char **words = (char **)XPptrv(w);
|
2012-10-03 17:50:32 +02:00
|
|
|
size_t i, path_order = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
info = (struct path_order_info *)
|
2010-09-14 23:26:19 +02:00
|
|
|
alloc2(nwords, sizeof(struct path_order_info), ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (i = 0; i < nwords; i++) {
|
|
|
|
info[i].word = words[i];
|
|
|
|
info[i].base = x_basename(words[i], NULL);
|
|
|
|
if (!last_info || info[i].base != last_info->base ||
|
|
|
|
strncmp(words[i], last_info->word, info[i].base) != 0) {
|
|
|
|
last_info = &info[i];
|
|
|
|
path_order++;
|
|
|
|
}
|
|
|
|
info[i].path_order = path_order;
|
|
|
|
}
|
|
|
|
qsort(info, nwords, sizeof(struct path_order_info),
|
2005-06-09 00:22:24 +02:00
|
|
|
path_order_cmp);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (i = 0; i < nwords; i++)
|
|
|
|
words[i] = info[i].word;
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(info, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
/* Sort and remove duplicate entries */
|
2005-06-09 00:22:24 +02:00
|
|
|
char **words = (char **)XPptrv(w);
|
2012-10-03 17:50:32 +02:00
|
|
|
size_t i, j;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
qsort(words, nwords, sizeof(void *), xstrcmp);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (i = j = 0; i < nwords - 1; i++) {
|
|
|
|
if (strcmp(words[i], words[i + 1]))
|
|
|
|
words[j++] = words[i];
|
|
|
|
else
|
|
|
|
afree(words[i], ATEMP);
|
|
|
|
}
|
|
|
|
words[j++] = words[i];
|
2012-10-03 17:50:32 +02:00
|
|
|
w.len = nwords = j;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
XPput(w, NULL);
|
2005-06-09 00:22:24 +02:00
|
|
|
*wordsp = (char **)XPclose(w);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (nwords);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2007-10-25 17:19:16 +02:00
|
|
|
#define IS_WORDC(c) (!ctype(c, C_LEX1) && (c) != '\'' && (c) != '"' && \
|
|
|
|
(c) != '`' && (c) != '=' && (c) != ':')
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
x_locate_word(const char *buf, int buflen, int pos, int *startp,
|
2008-04-19 19:21:55 +02:00
|
|
|
bool *is_commandp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
int start, end;
|
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
/* Bad call? Probably should report error */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (pos < 0 || pos > buflen) {
|
|
|
|
*startp = pos;
|
2008-04-19 19:21:55 +02:00
|
|
|
*is_commandp = false;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
/* The case where pos == buflen happens to take care of itself... */
|
|
|
|
|
|
|
|
start = pos;
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* Keep going backwards to start of word (has effect of allowing
|
2005-05-23 05:06:10 +02:00
|
|
|
* one blank after the end of a word)
|
|
|
|
*/
|
|
|
|
for (; (start > 0 && IS_WORDC(buf[start - 1])) ||
|
2005-06-09 00:22:24 +02:00
|
|
|
(start > 1 && buf[start - 2] == '\\'); start--)
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
|
|
|
/* Go forwards to end of word */
|
|
|
|
for (end = start; end < buflen && IS_WORDC(buf[end]); end++) {
|
2005-06-09 00:22:24 +02:00
|
|
|
if (buf[end] == '\\' && (end + 1) < buflen)
|
2005-05-23 05:06:10 +02:00
|
|
|
end++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_commandp) {
|
2008-04-19 19:21:55 +02:00
|
|
|
bool iscmd;
|
|
|
|
int p = start - 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* Figure out if this is a command */
|
2008-04-19 19:21:55 +02:00
|
|
|
while (p >= 0 && ksh_isspace(buf[p]))
|
|
|
|
p--;
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
iscmd = p < 0 || vstrchr(";|&()`", buf[p]);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (iscmd) {
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* If command has a /, path, etc. is not searched;
|
2006-01-30 13:37:24 +01:00
|
|
|
* only current directory is searched which is just
|
2005-05-23 05:06:10 +02:00
|
|
|
* like file globbing.
|
|
|
|
*/
|
|
|
|
for (p = start; p < end; p++)
|
2016-11-12 00:31:39 +01:00
|
|
|
if (mksh_cdirsep(buf[p]))
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
iscmd = p == end;
|
|
|
|
}
|
|
|
|
*is_commandp = iscmd;
|
|
|
|
}
|
|
|
|
*startp = start;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (end - start);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2007-06-07 01:28:17 +02:00
|
|
|
static int
|
2011-02-03 16:57:52 +01:00
|
|
|
x_cf_glob(int *flagsp, const char *buf, int buflen, int pos, int *startp,
|
|
|
|
int *endp, char ***wordsp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-02-03 16:57:52 +01:00
|
|
|
int len, nwords = 0;
|
2007-09-09 22:03:32 +02:00
|
|
|
char **words = NULL;
|
2008-04-19 19:21:55 +02:00
|
|
|
bool is_command;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
len = x_locate_word(buf, buflen, pos, startp, &is_command);
|
2011-06-04 17:06:50 +02:00
|
|
|
if (!((*flagsp) & XCF_COMMAND))
|
2008-04-19 19:21:55 +02:00
|
|
|
is_command = false;
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* Don't do command globing on zero length strings - it takes too
|
2009-06-10 20:12:51 +02:00
|
|
|
* long and isn't very useful. File globs are more likely to be
|
2005-05-23 05:06:10 +02:00
|
|
|
* useful, so allow these.
|
|
|
|
*/
|
|
|
|
if (len == 0 && is_command)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
if (len >= 0) {
|
|
|
|
char *toglob, *s;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a string, copy it and possibly add a '*' to the end.
|
|
|
|
*/
|
|
|
|
|
|
|
|
strndupx(toglob, buf + *startp, len + /* the '*' */ 1, ATEMP);
|
|
|
|
toglob[len] = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the pathname contains a wildcard (an unquoted '*',
|
2012-08-24 22:05:13 +02:00
|
|
|
* '?', or '[') or an extglob, then it is globbed based
|
2012-08-24 22:57:46 +02:00
|
|
|
* on that value (i.e., without the appended '*'). Same
|
|
|
|
* for parameter substitutions (as in “cat $HOME/.ss↹”)
|
|
|
|
* without appending a trailing space (LP: #710539), as
|
|
|
|
* well as for “~foo” (but not “~foo/”).
|
2011-02-03 16:57:52 +01:00
|
|
|
*/
|
|
|
|
for (s = toglob; *s; s++) {
|
|
|
|
if (*s == '\\' && s[1])
|
|
|
|
s++;
|
2012-08-24 22:05:13 +02:00
|
|
|
else if (*s == '?' || *s == '*' || *s == '[' ||
|
|
|
|
*s == '$' ||
|
2011-02-03 16:57:52 +01:00
|
|
|
/* ?() *() +() @() !() but two already checked */
|
|
|
|
(s[1] == '(' /*)*/ &&
|
|
|
|
(*s == '+' || *s == '@' || *s == '!'))) {
|
2012-08-24 22:57:46 +02:00
|
|
|
/*
|
|
|
|
* just expand based on the extglob
|
|
|
|
* or parameter
|
|
|
|
*/
|
2012-08-24 22:05:13 +02:00
|
|
|
goto dont_add_glob;
|
2011-06-30 16:09:04 +02:00
|
|
|
}
|
2011-02-03 16:57:52 +01:00
|
|
|
}
|
2012-08-24 22:05:13 +02:00
|
|
|
|
2017-03-11 23:49:56 +01:00
|
|
|
if (*toglob == '~' && /* not vdirsep */ !vstrchr(toglob, '/')) {
|
2012-08-24 22:05:13 +02:00
|
|
|
/* neither for '~foo' (but '~foo/bar') */
|
|
|
|
*flagsp |= XCF_IS_NOSPACE;
|
|
|
|
goto dont_add_glob;
|
2011-02-03 16:57:52 +01:00
|
|
|
}
|
|
|
|
|
2012-08-24 22:05:13 +02:00
|
|
|
/* append a glob */
|
|
|
|
toglob[len] = '*';
|
|
|
|
toglob[len + 1] = '\0';
|
|
|
|
dont_add_glob:
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* Expand (glob) it now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nwords = is_command ?
|
|
|
|
x_command_glob(*flagsp, toglob, &words) :
|
2012-08-24 22:05:13 +02:00
|
|
|
x_file_glob(flagsp, toglob, &words);
|
2011-02-03 16:57:52 +01:00
|
|
|
afree(toglob, ATEMP);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
if (nwords == 0) {
|
|
|
|
*wordsp = NULL;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-06-04 17:11:29 +02:00
|
|
|
if (is_command)
|
|
|
|
*flagsp |= XCF_IS_COMMAND;
|
2005-05-23 05:06:10 +02:00
|
|
|
*wordsp = words;
|
|
|
|
*endp = *startp + len;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (nwords);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find longest common prefix
|
|
|
|
*/
|
2011-08-27 20:06:52 +02:00
|
|
|
static size_t
|
2005-06-09 00:22:24 +02:00
|
|
|
x_longest_prefix(int nwords, char * const * words)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-08-27 20:06:52 +02:00
|
|
|
int i;
|
|
|
|
size_t j, prefix_len;
|
2005-05-23 05:06:10 +02:00
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (nwords <= 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
prefix_len = strlen(words[0]);
|
|
|
|
for (i = 1; i < nwords; i++)
|
|
|
|
for (j = 0, p = words[i]; j < prefix_len; j++)
|
|
|
|
if (p[j] != words[0][j]) {
|
|
|
|
prefix_len = j;
|
|
|
|
break;
|
|
|
|
}
|
2011-12-30 00:36:10 +01:00
|
|
|
/* false for nwords==1 as 0 = words[0][prefix_len] then */
|
|
|
|
if (UTFMODE && prefix_len && (words[0][prefix_len] & 0xC0) == 0x80)
|
|
|
|
while (prefix_len && (words[0][prefix_len] & 0xC0) != 0xC0)
|
|
|
|
--prefix_len;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (prefix_len);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2007-06-07 01:28:17 +02:00
|
|
|
static void
|
2005-05-23 05:06:10 +02:00
|
|
|
x_free_words(int nwords, char **words)
|
|
|
|
{
|
2008-04-19 19:21:55 +02:00
|
|
|
while (nwords)
|
2008-05-17 20:47:03 +02:00
|
|
|
afree(words[--nwords], ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
afree(words, ATEMP);
|
|
|
|
}
|
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*-
|
|
|
|
* Return the offset of the basename of string s (which ends at se - need not
|
2009-06-10 20:12:51 +02:00
|
|
|
* be null terminated). Trailing slashes are ignored. If s is just a slash,
|
2005-05-23 05:06:10 +02:00
|
|
|
* then the offset is 0 (actually, length - 1).
|
|
|
|
* s Return
|
|
|
|
* /etc 1
|
|
|
|
* /etc/ 1
|
|
|
|
* /etc// 1
|
|
|
|
* /etc/fo 5
|
|
|
|
* foo 0
|
|
|
|
* /// 2
|
|
|
|
* 0
|
|
|
|
*/
|
2016-07-28 23:39:04 +02:00
|
|
|
static size_t
|
2005-05-23 05:06:10 +02:00
|
|
|
x_basename(const char *s, const char *se)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (se == NULL)
|
|
|
|
se = s + strlen(s);
|
|
|
|
if (s == se)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2016-11-12 00:31:39 +01:00
|
|
|
/* skip trailing directory separators */
|
|
|
|
p = se - 1;
|
|
|
|
while (p > s && mksh_cdirsep(*p))
|
|
|
|
--p;
|
|
|
|
/* drop last component */
|
|
|
|
while (p > s && !mksh_cdirsep(*p))
|
|
|
|
--p;
|
|
|
|
if (mksh_cdirsep(*p) && p + 1 < se)
|
|
|
|
++p;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (p - s);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-06-10 20:12:51 +02:00
|
|
|
* Apply pattern matching to a table: all table entries that match a pattern
|
2005-05-23 05:06:10 +02:00
|
|
|
* are added to wp.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
glob_table(const char *pat, XPtrV *wp, struct table *tp)
|
|
|
|
{
|
|
|
|
struct tstate ts;
|
|
|
|
struct tbl *te;
|
|
|
|
|
2008-10-28 15:32:43 +01:00
|
|
|
ktwalk(&ts, tp);
|
|
|
|
while ((te = ktnext(&ts)))
|
|
|
|
if (gmatchx(te->name, pat, false)) {
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
strdupx(cp, te->name, ATEMP);
|
|
|
|
XPput(*wp, cp);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
glob_path(int flags, const char *pat, XPtrV *wp, const char *lpath)
|
|
|
|
{
|
2010-09-14 23:26:19 +02:00
|
|
|
const char *sp = lpath, *p;
|
2008-04-19 19:21:55 +02:00
|
|
|
char *xp, **words;
|
2010-09-14 23:26:19 +02:00
|
|
|
size_t pathlen, patlen, oldsize, newsize, i, j;
|
2005-05-23 05:06:10 +02:00
|
|
|
XString xs;
|
|
|
|
|
2010-09-14 23:26:19 +02:00
|
|
|
patlen = strlen(pat);
|
|
|
|
checkoktoadd(patlen, 129 + X_EXTRA);
|
|
|
|
++patlen;
|
2005-05-23 05:06:10 +02:00
|
|
|
Xinit(xs, xp, patlen + 128, ATEMP);
|
|
|
|
while (sp) {
|
|
|
|
xp = Xstring(xs, xp);
|
2015-07-10 21:36:38 +02:00
|
|
|
if (!(p = cstrchr(sp, MKSH_PATHSEPC)))
|
2005-05-23 05:06:10 +02:00
|
|
|
p = sp + strlen(sp);
|
|
|
|
pathlen = p - sp;
|
|
|
|
if (pathlen) {
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* Copy sp into xp, stuffing any MAGIC characters
|
2005-05-23 05:06:10 +02:00
|
|
|
* on the way
|
|
|
|
*/
|
|
|
|
const char *s = sp;
|
|
|
|
|
|
|
|
XcheckN(xs, xp, pathlen * 2);
|
|
|
|
while (s < p) {
|
|
|
|
if (ISMAGIC(*s))
|
|
|
|
*xp++ = MAGIC;
|
|
|
|
*xp++ = *s++;
|
|
|
|
}
|
|
|
|
*xp++ = '/';
|
|
|
|
pathlen++;
|
|
|
|
}
|
|
|
|
sp = p;
|
|
|
|
XcheckN(xs, xp, patlen);
|
|
|
|
memcpy(xp, pat, patlen);
|
|
|
|
|
|
|
|
oldsize = XPsize(*wp);
|
2011-02-03 16:57:52 +01:00
|
|
|
/* mark dirs */
|
2012-08-24 21:02:57 +02:00
|
|
|
glob_str(Xstring(xs, xp), wp, true);
|
2005-05-23 05:06:10 +02:00
|
|
|
newsize = XPsize(*wp);
|
|
|
|
|
|
|
|
/* Check that each match is executable... */
|
2005-06-09 00:22:24 +02:00
|
|
|
words = (char **)XPptrv(*wp);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (i = j = oldsize; i < newsize; i++) {
|
2011-09-07 17:24:22 +02:00
|
|
|
if (ksh_access(words[i], X_OK) == 0) {
|
2005-05-23 05:06:10 +02:00
|
|
|
words[j] = words[i];
|
|
|
|
if (!(flags & XCF_FULLPATH))
|
|
|
|
memmove(words[j], words[j] + pathlen,
|
|
|
|
strlen(words[j] + pathlen) + 1);
|
|
|
|
j++;
|
|
|
|
} else
|
|
|
|
afree(words[i], ATEMP);
|
|
|
|
}
|
2012-10-03 17:50:32 +02:00
|
|
|
wp->len = j;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (!*sp++)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Xfree(xs, xp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if argument string contains any special characters, they will
|
|
|
|
* be escaped and the result will be put into edit buffer by
|
|
|
|
* keybinding-specific function
|
|
|
|
*/
|
2007-06-07 01:28:17 +02:00
|
|
|
static int
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
x_escape(const char *s, size_t len, int (*putbuf_func)(const char *, size_t))
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2007-06-21 18:04:46 +02:00
|
|
|
size_t add = 0, wlen = len;
|
2005-06-09 00:22:24 +02:00
|
|
|
int rval = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2012-08-24 23:15:42 +02:00
|
|
|
while (wlen - add > 0)
|
|
|
|
if (vstrchr("\"#$&'()*:;<=>?[\\`{|}", s[add]) ||
|
2016-08-01 20:42:42 +02:00
|
|
|
ctype(s[add], C_IFS)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (putbuf_func(s, add) != 0) {
|
|
|
|
rval = -1;
|
|
|
|
break;
|
|
|
|
}
|
2012-08-24 23:15:42 +02:00
|
|
|
putbuf_func(s[add] == '\n' ? "'" : "\\", 1);
|
2005-05-23 05:06:10 +02:00
|
|
|
putbuf_func(&s[add], 1);
|
2012-08-24 23:15:42 +02:00
|
|
|
if (s[add] == '\n')
|
2007-02-16 18:46:42 +01:00
|
|
|
putbuf_func("'", 1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
add++;
|
|
|
|
wlen -= add;
|
|
|
|
s += add;
|
2007-06-21 18:04:46 +02:00
|
|
|
add = 0;
|
|
|
|
} else
|
|
|
|
++add;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (wlen > 0 && rval == 0)
|
|
|
|
rval = putbuf_func(s, wlen);
|
|
|
|
|
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
2006-11-05 16:31:37 +01:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/* +++ emacs editing mode +++ */
|
|
|
|
|
2008-12-13 18:02:18 +01:00
|
|
|
static Area aedit;
|
|
|
|
#define AEDIT &aedit /* area for kill ring and macro defns */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* values returned by keyboard functions */
|
|
|
|
#define KSTD 0
|
|
|
|
#define KEOL 1 /* ^M, ^J */
|
|
|
|
#define KINTR 2 /* ^G, ^C */
|
|
|
|
|
2007-05-21 14:24:44 +02:00
|
|
|
struct x_ftab {
|
|
|
|
int (*xf_func)(int c);
|
|
|
|
const char *xf_name;
|
|
|
|
short xf_flags;
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct x_defbindings {
|
2007-05-21 14:24:44 +02:00
|
|
|
unsigned char xdb_func; /* XFUNC_* */
|
|
|
|
unsigned char xdb_tab;
|
|
|
|
unsigned char xdb_char;
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#define XF_ARG 1 /* command takes number prefix */
|
|
|
|
#define XF_NOBIND 2 /* not allowed to bind to function */
|
|
|
|
#define XF_PREFIX 4 /* function sets prefix */
|
|
|
|
|
|
|
|
/* Separator for completion */
|
2006-07-03 14:16:31 +02:00
|
|
|
#define is_cfs(c) ((c) == ' ' || (c) == '\t' || (c) == '"' || (c) == '\'')
|
2010-03-27 16:26:19 +01:00
|
|
|
/* Separator for motion */
|
|
|
|
#define is_mfs(c) (!(ksh_isalnux(c) || (c) == '$' || ((c) & 0x80)))
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2015-07-10 20:41:07 +02:00
|
|
|
#define X_NTABS 4 /* normal, meta1, meta2, pc */
|
2007-10-14 15:43:41 +02:00
|
|
|
#define X_TABSZ 256 /* size of keydef tables etc */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*-
|
|
|
|
* Arguments for do_complete()
|
2009-06-10 20:12:51 +02:00
|
|
|
* 0 = enumerate M-= complete as much as possible and then list
|
|
|
|
* 1 = complete M-Esc
|
|
|
|
* 2 = list M-?
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
CT_LIST, /* list the possible completions */
|
|
|
|
CT_COMPLETE, /* complete to longest prefix */
|
|
|
|
CT_COMPLIST /* complete and then list (if non-exact) */
|
|
|
|
} Comp_type;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following are used for my horizontal scrolling stuff
|
|
|
|
*/
|
2006-11-09 16:02:31 +01:00
|
|
|
static char *xbuf; /* beg input buffer */
|
|
|
|
static char *xend; /* end input buffer */
|
|
|
|
static char *xcp; /* current position */
|
|
|
|
static char *xep; /* current end */
|
|
|
|
static char *xbp; /* start of visible portion of input buffer */
|
|
|
|
static char *xlp; /* last char visible on screen */
|
2012-03-31 21:20:12 +02:00
|
|
|
static bool x_adj_ok;
|
2005-05-23 05:06:10 +02:00
|
|
|
/*
|
|
|
|
* we use x_adj_done so that functions can tell
|
|
|
|
* whether x_adjust() has been called while they are active.
|
|
|
|
*/
|
2012-11-26 23:39:14 +01:00
|
|
|
static int x_adj_done; /* is incremented by x_adjust() */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2008-10-26 21:59:40 +01:00
|
|
|
static int x_displen;
|
|
|
|
static int x_arg; /* general purpose arg */
|
2011-02-09 14:08:18 +01:00
|
|
|
static bool x_arg_defaulted; /* x_arg not explicitly set; defaulted to 1 */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2012-11-26 23:39:14 +01:00
|
|
|
static bool xlp_valid; /* lastvis pointer was recalculated */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2008-10-26 21:59:40 +01:00
|
|
|
static char **x_histp; /* history position */
|
|
|
|
static int x_nextcmd; /* for newline-and-next */
|
2011-12-11 19:01:03 +01:00
|
|
|
static char **x_histncp; /* saved x_histp for " */
|
2008-10-26 21:59:40 +01:00
|
|
|
static char *xmp; /* mark pointer */
|
2007-10-25 17:23:10 +02:00
|
|
|
static unsigned char x_last_command;
|
|
|
|
static unsigned char (*x_tab)[X_TABSZ]; /* key definition */
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2008-10-26 21:59:40 +01:00
|
|
|
static char *(*x_atab)[X_TABSZ]; /* macro definitions */
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2008-10-26 21:59:40 +01:00
|
|
|
static unsigned char x_bound[(X_TABSZ * X_NTABS + 7) / 8];
|
|
|
|
#define KILLSIZE 20
|
|
|
|
static char *killstack[KILLSIZE];
|
|
|
|
static int killsp, killtp;
|
|
|
|
static int x_curprefix;
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2011-04-22 14:10:14 +02:00
|
|
|
static char *macroptr; /* bind key macro active? */
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2009-09-24 19:15:33 +02:00
|
|
|
#if !MKSH_S_NOVI
|
2008-10-26 21:59:40 +01:00
|
|
|
static int winwidth; /* width of window */
|
|
|
|
static char *wbuf[2]; /* window buffers */
|
|
|
|
static int wbuf_len; /* length of window buffers (x_cols - 3) */
|
|
|
|
static int win; /* window buffer in use */
|
|
|
|
static char morec; /* more character at right of window */
|
|
|
|
static int lastref; /* argument to last refresh() */
|
|
|
|
static int holdlen; /* length of holdbuf */
|
2007-06-16 00:00:00 +02:00
|
|
|
#endif
|
2013-07-26 22:33:39 +02:00
|
|
|
static int pwidth; /* width of prompt */
|
|
|
|
static int prompt_trunc; /* how much of prompt to truncate or -1 */
|
|
|
|
static int x_col; /* current column on line */
|
2008-10-26 21:59:40 +01:00
|
|
|
|
|
|
|
static int x_ins(const char *);
|
2012-07-20 22:50:07 +02:00
|
|
|
static void x_delete(size_t, bool);
|
|
|
|
static size_t x_bword(void);
|
|
|
|
static size_t x_fword(bool);
|
2008-10-26 21:59:40 +01:00
|
|
|
static void x_goto(char *);
|
2014-01-05 22:57:29 +01:00
|
|
|
static char *x_bs0(char *, char *) MKSH_A_PURE;
|
2008-10-26 21:59:40 +01:00
|
|
|
static void x_bs3(char **);
|
|
|
|
static int x_size2(char *, char **);
|
|
|
|
static void x_zots(char *);
|
|
|
|
static void x_zotc3(char **);
|
|
|
|
static void x_load_hist(char **);
|
|
|
|
static int x_search(char *, int, int);
|
2009-09-20 19:00:53 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2009-05-16 16:19:23 +02:00
|
|
|
static int x_search_dir(int);
|
2009-09-20 19:00:53 +02:00
|
|
|
#endif
|
2008-10-26 21:59:40 +01:00
|
|
|
static int x_match(char *, char *);
|
|
|
|
static void x_redraw(int);
|
2016-07-26 22:43:14 +02:00
|
|
|
static void x_push(size_t);
|
2012-12-01 02:36:30 +01:00
|
|
|
static char *x_mapin(const char *, Area *);
|
2008-10-26 21:59:40 +01:00
|
|
|
static char *x_mapout(int);
|
|
|
|
static void x_mapout2(int, char **);
|
|
|
|
static void x_print(int, int);
|
|
|
|
static void x_e_ungetc(int);
|
|
|
|
static int x_e_getc(void);
|
|
|
|
static void x_e_putc2(int);
|
|
|
|
static void x_e_putc3(const char **);
|
|
|
|
static void x_e_puts(const char *);
|
2009-09-20 19:00:53 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2008-10-26 21:59:40 +01:00
|
|
|
static int x_fold_case(int);
|
2009-09-20 19:00:53 +02:00
|
|
|
#endif
|
2008-10-26 21:59:40 +01:00
|
|
|
static char *x_lastcp(void);
|
2016-07-26 23:37:26 +02:00
|
|
|
static void x_lastpos(void);
|
2008-10-26 21:59:40 +01:00
|
|
|
static void do_complete(int, Comp_type);
|
2014-01-05 22:57:29 +01:00
|
|
|
static size_t x_nb2nc(size_t) MKSH_A_PURE;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2006-08-01 14:44:17 +02:00
|
|
|
static int unget_char = -1;
|
|
|
|
|
2007-05-10 20:46:38 +02:00
|
|
|
static int x_do_ins(const char *, size_t);
|
2006-08-01 14:44:17 +02:00
|
|
|
static void bind_if_not_bound(int, int, int);
|
|
|
|
|
2009-09-20 17:38:07 +02:00
|
|
|
enum emacs_funcs {
|
|
|
|
#define EMACSFN_ENUMS
|
|
|
|
#include "emacsfn.h"
|
|
|
|
XFUNC_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
#define EMACSFN_DEFNS
|
|
|
|
#include "emacsfn.h"
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
static const struct x_ftab x_ftab[] = {
|
2009-09-20 17:38:07 +02:00
|
|
|
#define EMACSFN_ITEMS
|
|
|
|
#include "emacsfn.h"
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct x_defbindings const x_defbindings[] = {
|
2009-06-10 20:12:51 +02:00
|
|
|
{ XFUNC_del_back, 0, CTRL('?') },
|
|
|
|
{ XFUNC_del_bword, 1, CTRL('?') },
|
|
|
|
{ XFUNC_eot_del, 0, CTRL('D') },
|
|
|
|
{ XFUNC_del_back, 0, CTRL('H') },
|
|
|
|
{ XFUNC_del_bword, 1, CTRL('H') },
|
|
|
|
{ XFUNC_del_bword, 1, 'h' },
|
|
|
|
{ XFUNC_mv_bword, 1, 'b' },
|
|
|
|
{ XFUNC_mv_fword, 1, 'f' },
|
|
|
|
{ XFUNC_del_fword, 1, 'd' },
|
|
|
|
{ XFUNC_mv_back, 0, CTRL('B') },
|
|
|
|
{ XFUNC_mv_forw, 0, CTRL('F') },
|
|
|
|
{ XFUNC_search_char_forw, 0, CTRL(']') },
|
|
|
|
{ XFUNC_search_char_back, 1, CTRL(']') },
|
|
|
|
{ XFUNC_newline, 0, CTRL('M') },
|
|
|
|
{ XFUNC_newline, 0, CTRL('J') },
|
|
|
|
{ XFUNC_end_of_text, 0, CTRL('_') },
|
|
|
|
{ XFUNC_abort, 0, CTRL('G') },
|
|
|
|
{ XFUNC_prev_com, 0, CTRL('P') },
|
|
|
|
{ XFUNC_next_com, 0, CTRL('N') },
|
|
|
|
{ XFUNC_nl_next_com, 0, CTRL('O') },
|
|
|
|
{ XFUNC_search_hist, 0, CTRL('R') },
|
|
|
|
{ XFUNC_beg_hist, 1, '<' },
|
|
|
|
{ XFUNC_end_hist, 1, '>' },
|
|
|
|
{ XFUNC_goto_hist, 1, 'g' },
|
|
|
|
{ XFUNC_mv_end, 0, CTRL('E') },
|
2016-09-01 14:59:12 +02:00
|
|
|
{ XFUNC_mv_beg, 0, CTRL('A') },
|
2009-06-10 20:12:51 +02:00
|
|
|
{ XFUNC_draw_line, 0, CTRL('L') },
|
|
|
|
{ XFUNC_cls, 1, CTRL('L') },
|
|
|
|
{ XFUNC_meta1, 0, CTRL('[') },
|
|
|
|
{ XFUNC_meta2, 0, CTRL('X') },
|
|
|
|
{ XFUNC_kill, 0, CTRL('K') },
|
|
|
|
{ XFUNC_yank, 0, CTRL('Y') },
|
|
|
|
{ XFUNC_meta_yank, 1, 'y' },
|
|
|
|
{ XFUNC_literal, 0, CTRL('^') },
|
|
|
|
{ XFUNC_comment, 1, '#' },
|
|
|
|
{ XFUNC_transpose, 0, CTRL('T') },
|
|
|
|
{ XFUNC_complete, 1, CTRL('[') },
|
|
|
|
{ XFUNC_comp_list, 0, CTRL('I') },
|
|
|
|
{ XFUNC_comp_list, 1, '=' },
|
|
|
|
{ XFUNC_enumerate, 1, '?' },
|
|
|
|
{ XFUNC_expand, 1, '*' },
|
|
|
|
{ XFUNC_comp_file, 1, CTRL('X') },
|
|
|
|
{ XFUNC_comp_comm, 2, CTRL('[') },
|
|
|
|
{ XFUNC_list_comm, 2, '?' },
|
|
|
|
{ XFUNC_list_file, 2, CTRL('Y') },
|
|
|
|
{ XFUNC_set_mark, 1, ' ' },
|
|
|
|
{ XFUNC_kill_region, 0, CTRL('W') },
|
|
|
|
{ XFUNC_xchg_point_mark, 2, CTRL('X') },
|
|
|
|
{ XFUNC_literal, 0, CTRL('V') },
|
|
|
|
{ XFUNC_version, 1, CTRL('V') },
|
|
|
|
{ XFUNC_prev_histword, 1, '.' },
|
|
|
|
{ XFUNC_prev_histword, 1, '_' },
|
|
|
|
{ XFUNC_set_arg, 1, '0' },
|
|
|
|
{ XFUNC_set_arg, 1, '1' },
|
|
|
|
{ XFUNC_set_arg, 1, '2' },
|
|
|
|
{ XFUNC_set_arg, 1, '3' },
|
|
|
|
{ XFUNC_set_arg, 1, '4' },
|
|
|
|
{ XFUNC_set_arg, 1, '5' },
|
|
|
|
{ XFUNC_set_arg, 1, '6' },
|
|
|
|
{ XFUNC_set_arg, 1, '7' },
|
|
|
|
{ XFUNC_set_arg, 1, '8' },
|
|
|
|
{ XFUNC_set_arg, 1, '9' },
|
2009-09-20 19:00:53 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2009-06-10 20:12:51 +02:00
|
|
|
{ XFUNC_fold_upper, 1, 'U' },
|
|
|
|
{ XFUNC_fold_upper, 1, 'u' },
|
|
|
|
{ XFUNC_fold_lower, 1, 'L' },
|
|
|
|
{ XFUNC_fold_lower, 1, 'l' },
|
|
|
|
{ XFUNC_fold_capitalise, 1, 'C' },
|
|
|
|
{ XFUNC_fold_capitalise, 1, 'c' },
|
2009-09-20 19:00:53 +02:00
|
|
|
#endif
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* These for ANSI arrow keys: arguablely shouldn't be here by
|
2005-05-23 05:06:10 +02:00
|
|
|
* default, but its simpler/faster/smaller than using termcap
|
|
|
|
* entries.
|
|
|
|
*/
|
2009-06-10 20:12:51 +02:00
|
|
|
{ XFUNC_meta2, 1, '[' },
|
|
|
|
{ XFUNC_meta2, 1, 'O' },
|
|
|
|
{ XFUNC_prev_com, 2, 'A' },
|
|
|
|
{ XFUNC_next_com, 2, 'B' },
|
|
|
|
{ XFUNC_mv_forw, 2, 'C' },
|
|
|
|
{ XFUNC_mv_back, 2, 'D' },
|
2009-09-20 19:00:53 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2009-09-20 19:23:52 +02:00
|
|
|
{ XFUNC_vt_hack, 2, '1' },
|
2016-09-01 14:59:12 +02:00
|
|
|
{ XFUNC_mv_beg | 0x80, 2, '7' },
|
|
|
|
{ XFUNC_mv_beg, 2, 'H' },
|
2009-06-10 20:12:51 +02:00
|
|
|
{ XFUNC_mv_end | 0x80, 2, '4' },
|
|
|
|
{ XFUNC_mv_end | 0x80, 2, '8' },
|
|
|
|
{ XFUNC_mv_end, 2, 'F' },
|
|
|
|
{ XFUNC_del_char | 0x80, 2, '3' },
|
2015-07-10 20:41:07 +02:00
|
|
|
{ XFUNC_del_char, 2, 'P' },
|
2009-06-10 20:12:51 +02:00
|
|
|
{ XFUNC_search_hist_up | 0x80, 2, '5' },
|
|
|
|
{ XFUNC_search_hist_dn | 0x80, 2, '6' },
|
2015-07-10 20:41:07 +02:00
|
|
|
#endif
|
|
|
|
/* PC scancodes */
|
|
|
|
#if !defined(MKSH_SMALL) || defined(__OS2__)
|
|
|
|
{ XFUNC_meta3, 0, 0 },
|
2016-09-01 14:59:12 +02:00
|
|
|
{ XFUNC_mv_beg, 3, 71 },
|
2015-07-10 20:41:07 +02:00
|
|
|
{ XFUNC_prev_com, 3, 72 },
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
{ XFUNC_search_hist_up, 3, 73 },
|
|
|
|
#endif
|
|
|
|
{ XFUNC_mv_back, 3, 75 },
|
|
|
|
{ XFUNC_mv_forw, 3, 77 },
|
|
|
|
{ XFUNC_mv_end, 3, 79 },
|
|
|
|
{ XFUNC_next_com, 3, 80 },
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
{ XFUNC_search_hist_dn, 3, 81 },
|
|
|
|
#endif
|
|
|
|
{ XFUNC_del_char, 3, 83 },
|
|
|
|
#endif
|
|
|
|
#ifndef MKSH_SMALL
|
2008-09-30 19:23:00 +02:00
|
|
|
/* more non-standard ones */
|
2016-07-26 23:37:26 +02:00
|
|
|
{ XFUNC_eval_region, 1, CTRL('E') },
|
2009-06-10 20:12:51 +02:00
|
|
|
{ XFUNC_edit_line, 2, 'e' }
|
2009-09-20 19:00:53 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
2012-07-20 22:50:07 +02:00
|
|
|
static size_t
|
|
|
|
x_nb2nc(size_t nb)
|
2012-07-20 22:33:15 +02:00
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
size_t nc = 0;
|
|
|
|
|
|
|
|
for (cp = xcp; cp < (xcp + nb); ++nc)
|
|
|
|
cp += utf_ptradj(cp);
|
|
|
|
return (nc);
|
|
|
|
}
|
|
|
|
|
2008-10-05 18:06:43 +02:00
|
|
|
static void
|
|
|
|
x_modified(void)
|
|
|
|
{
|
|
|
|
if (!modified) {
|
|
|
|
x_histp = histptr + 1;
|
|
|
|
modified = 1;
|
|
|
|
}
|
|
|
|
}
|
2012-07-01 17:36:18 +02:00
|
|
|
|
|
|
|
#ifdef MKSH_SMALL
|
2009-09-20 19:18:53 +02:00
|
|
|
#define XFUNC_VALUE(f) (f)
|
2008-10-05 18:06:43 +02:00
|
|
|
#else
|
2009-09-20 19:18:53 +02:00
|
|
|
#define XFUNC_VALUE(f) (f & 0x7F)
|
2008-10-05 18:06:43 +02:00
|
|
|
#endif
|
|
|
|
|
2006-11-05 18:54:46 +01:00
|
|
|
static int
|
|
|
|
x_e_getmbc(char *sbuf)
|
|
|
|
{
|
|
|
|
int c, pos = 0;
|
|
|
|
unsigned char *buf = (unsigned char *)sbuf;
|
|
|
|
|
|
|
|
memset(buf, 0, 4);
|
|
|
|
buf[pos++] = c = x_e_getc();
|
|
|
|
if (c == -1)
|
|
|
|
return (-1);
|
2008-12-04 19:11:08 +01:00
|
|
|
if (UTFMODE) {
|
2006-11-05 18:54:46 +01:00
|
|
|
if ((buf[0] >= 0xC2) && (buf[0] < 0xF0)) {
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
c = x_e_getc();
|
2006-11-05 18:54:46 +01:00
|
|
|
if (c == -1)
|
|
|
|
return (-1);
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
if ((c & 0xC0) != 0x80) {
|
|
|
|
x_e_ungetc(c);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
buf[pos++] = c;
|
2006-11-05 18:54:46 +01:00
|
|
|
}
|
|
|
|
if ((buf[0] >= 0xE0) && (buf[0] < 0xF0)) {
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
/* XXX x_e_ungetc is one-octet only */
|
2006-11-05 18:54:46 +01:00
|
|
|
buf[pos++] = c = x_e_getc();
|
|
|
|
if (c == -1)
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (pos);
|
|
|
|
}
|
|
|
|
|
2016-07-13 01:07:10 +02:00
|
|
|
/*
|
|
|
|
* minimum required space to work with on a line - if the prompt
|
|
|
|
* leaves less space than this on a line, the prompt is truncated
|
|
|
|
*/
|
|
|
|
#define MIN_EDIT_SPACE 7
|
|
|
|
|
2007-07-31 17:29:40 +02:00
|
|
|
static void
|
2013-08-16 12:59:03 +02:00
|
|
|
x_init_prompt(bool doprint)
|
2007-07-31 17:29:40 +02:00
|
|
|
{
|
2013-08-16 12:59:03 +02:00
|
|
|
prompt_trunc = pprompt(prompt, doprint ? 0 : -1);
|
2013-07-26 22:33:39 +02:00
|
|
|
pwidth = prompt_trunc % x_cols;
|
|
|
|
prompt_trunc -= pwidth;
|
|
|
|
if ((mksh_uari_t)pwidth > ((mksh_uari_t)x_cols - 3 - MIN_EDIT_SPACE)) {
|
|
|
|
/* force newline after prompt */
|
|
|
|
prompt_trunc = -1;
|
|
|
|
pwidth = 0;
|
2013-08-16 12:59:03 +02:00
|
|
|
if (doprint)
|
|
|
|
x_e_putc2('\n');
|
2007-07-31 17:29:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-07 01:28:17 +02:00
|
|
|
static int
|
2013-08-10 15:44:33 +02:00
|
|
|
x_emacs(char *buf)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-08-02 13:33:37 +02:00
|
|
|
int c, i;
|
2007-10-25 17:23:10 +02:00
|
|
|
unsigned char f;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2013-08-10 15:44:33 +02:00
|
|
|
xbp = xbuf = buf;
|
|
|
|
xend = buf + LINE;
|
2005-05-23 05:06:10 +02:00
|
|
|
xlp = xcp = xep = buf;
|
|
|
|
*xcp = 0;
|
|
|
|
xlp_valid = true;
|
|
|
|
xmp = NULL;
|
|
|
|
x_curprefix = 0;
|
|
|
|
x_histp = histptr + 1;
|
|
|
|
x_last_command = XFUNC_error;
|
|
|
|
|
2013-08-16 12:59:03 +02:00
|
|
|
x_init_prompt(true);
|
2013-07-26 22:33:39 +02:00
|
|
|
x_displen = (xx_cols = x_cols) - 2 - (x_col = pwidth);
|
|
|
|
x_adj_done = 0;
|
|
|
|
x_adj_ok = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-12-11 19:01:03 +01:00
|
|
|
x_histncp = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (x_nextcmd >= 0) {
|
|
|
|
int off = source->line - x_nextcmd;
|
2011-12-11 19:01:03 +01:00
|
|
|
if (histptr - history >= off) {
|
2005-05-23 05:06:10 +02:00
|
|
|
x_load_hist(histptr - off);
|
2011-12-11 19:01:03 +01:00
|
|
|
x_histncp = x_histp;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
x_nextcmd = -1;
|
|
|
|
}
|
2010-07-04 19:45:17 +02:00
|
|
|
editmode = 1;
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
x_flush();
|
|
|
|
if ((c = x_e_getc()) < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
f = x_curprefix == -1 ? XFUNC_insert :
|
2007-10-14 15:43:41 +02:00
|
|
|
x_tab[x_curprefix][c];
|
2009-09-20 19:00:53 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2006-08-02 15:32:18 +02:00
|
|
|
if (f & 0x80) {
|
|
|
|
f &= 0x7F;
|
|
|
|
if ((i = x_e_getc()) != '~')
|
|
|
|
x_e_ungetc(i);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-04-05 14:28:55 +02:00
|
|
|
/* avoid bind key macro recursion */
|
|
|
|
if (macroptr && f == XFUNC_ins_string)
|
|
|
|
f = XFUNC_insert;
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2009-04-05 14:28:55 +02:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!(x_ftab[f].xf_flags & XF_PREFIX) &&
|
|
|
|
x_last_command != XFUNC_set_arg) {
|
|
|
|
x_arg = 1;
|
2011-02-09 14:08:18 +01:00
|
|
|
x_arg_defaulted = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
i = c | (x_curprefix << 8);
|
|
|
|
x_curprefix = 0;
|
2008-10-14 01:06:04 +02:00
|
|
|
switch ((*x_ftab[f].xf_func)(i)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
case KSTD:
|
|
|
|
if (!(x_ftab[f].xf_flags & XF_PREFIX))
|
|
|
|
x_last_command = f;
|
|
|
|
break;
|
|
|
|
case KEOL:
|
|
|
|
i = xep - xbuf;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (i);
|
2011-02-03 16:57:52 +01:00
|
|
|
case KINTR:
|
|
|
|
/* special case for interrupt */
|
2005-05-23 05:06:10 +02:00
|
|
|
trapsig(SIGINT);
|
|
|
|
x_mode(false);
|
|
|
|
unwind(LSHELL);
|
|
|
|
}
|
2007-05-21 14:24:44 +02:00
|
|
|
/* ad-hoc hack for fixing the cursor position */
|
|
|
|
x_goto(xcp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
x_insert(int c)
|
|
|
|
{
|
2011-04-22 14:15:42 +02:00
|
|
|
static int left, pos, save_arg;
|
2006-11-05 17:10:10 +01:00
|
|
|
static char str[4];
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/*
|
2009-06-10 20:12:51 +02:00
|
|
|
* Should allow tab and control chars.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
if (c == 0) {
|
2007-03-10 01:36:44 +01:00
|
|
|
invmbs:
|
2006-11-05 17:10:10 +01:00
|
|
|
left = 0;
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2008-12-04 19:11:08 +01:00
|
|
|
if (UTFMODE) {
|
2006-11-05 17:10:10 +01:00
|
|
|
if (((c & 0xC0) == 0x80) && left) {
|
|
|
|
str[pos++] = c;
|
2007-03-10 19:16:28 +01:00
|
|
|
if (!--left) {
|
2006-11-05 17:10:10 +01:00
|
|
|
str[pos] = '\0';
|
|
|
|
x_arg = save_arg;
|
2007-03-10 19:16:28 +01:00
|
|
|
while (x_arg--)
|
|
|
|
x_ins(str);
|
2006-11-05 17:10:10 +01:00
|
|
|
}
|
|
|
|
return (KSTD);
|
|
|
|
}
|
|
|
|
if (left) {
|
2007-03-10 01:36:44 +01:00
|
|
|
if (x_curprefix == -1) {
|
|
|
|
/* flush invalid multibyte */
|
|
|
|
str[pos] = '\0';
|
|
|
|
while (save_arg--)
|
|
|
|
x_ins(str);
|
|
|
|
}
|
2006-11-05 17:10:10 +01:00
|
|
|
}
|
|
|
|
if ((c >= 0xC2) && (c < 0xE0))
|
|
|
|
left = 1;
|
|
|
|
else if ((c >= 0xE0) && (c < 0xF0))
|
|
|
|
left = 2;
|
2007-03-10 19:16:28 +01:00
|
|
|
else if (c > 0x7F)
|
|
|
|
goto invmbs;
|
|
|
|
else
|
|
|
|
left = 0;
|
2006-11-05 17:10:10 +01:00
|
|
|
if (left) {
|
|
|
|
save_arg = x_arg;
|
|
|
|
pos = 1;
|
|
|
|
str[0] = c;
|
|
|
|
return (KSTD);
|
2007-03-10 19:16:28 +01:00
|
|
|
}
|
2006-11-05 20:56:20 +01:00
|
|
|
}
|
2006-11-05 17:10:10 +01:00
|
|
|
left = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
str[0] = c;
|
|
|
|
str[1] = '\0';
|
|
|
|
while (x_arg--)
|
|
|
|
x_ins(str);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2005-05-23 05:06:10 +02:00
|
|
|
static int
|
|
|
|
x_ins_string(int c)
|
|
|
|
{
|
2007-10-14 15:43:41 +02:00
|
|
|
macroptr = x_atab[c >> 8][c & 255];
|
2009-04-05 14:35:21 +02:00
|
|
|
/*
|
|
|
|
* we no longer need to bother checking if macroptr is
|
|
|
|
* not NULL but first char is NUL; x_e_getc() does it
|
|
|
|
*/
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
static int
|
2007-05-10 20:46:38 +02:00
|
|
|
x_do_ins(const char *cp, size_t len)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
if (xep + len >= xend) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2005-06-09 00:22:24 +02:00
|
|
|
memmove(xcp + len, xcp, xep - xcp + 1);
|
2005-05-23 05:06:10 +02:00
|
|
|
memmove(xcp, cp, len);
|
|
|
|
xcp += len;
|
|
|
|
xep += len;
|
2008-09-20 16:10:23 +02:00
|
|
|
x_modified();
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-05-10 20:58:31 +02:00
|
|
|
x_ins(const char *s)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-05 16:31:37 +01:00
|
|
|
char *cp = xcp;
|
2012-11-26 23:39:14 +01:00
|
|
|
int adj = x_adj_done;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (x_do_ins(s, strlen(s)) < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
/*
|
|
|
|
* x_zots() may result in a call to x_adjust()
|
|
|
|
* we want xcp to reflect the new position.
|
|
|
|
*/
|
|
|
|
xlp_valid = false;
|
|
|
|
x_lastcp();
|
2012-03-31 21:20:12 +02:00
|
|
|
x_adj_ok = tobool(xcp >= xlp);
|
2005-05-23 05:06:10 +02:00
|
|
|
x_zots(cp);
|
2016-07-27 00:55:35 +02:00
|
|
|
if (adj == x_adj_done)
|
2016-07-26 23:37:26 +02:00
|
|
|
/* x_adjust() has not been called */
|
|
|
|
x_lastpos();
|
2012-03-31 21:20:12 +02:00
|
|
|
x_adj_ok = true;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_del_back(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2012-07-20 22:50:07 +02:00
|
|
|
ssize_t i = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2006-11-05 18:34:39 +01:00
|
|
|
if (xcp == xbuf) {
|
|
|
|
x_e_putc2(7);
|
|
|
|
return (KSTD);
|
|
|
|
}
|
2006-11-06 20:57:19 +01:00
|
|
|
do {
|
2006-11-05 17:43:57 +01:00
|
|
|
x_goto(xcp - 1);
|
2006-11-06 20:57:19 +01:00
|
|
|
} while ((++i < x_arg) && (xcp != xbuf));
|
2006-11-05 18:34:39 +01:00
|
|
|
x_delete(i, false);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_del_char(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-05 17:53:34 +01:00
|
|
|
char *cp, *cp2;
|
2012-10-03 19:24:23 +02:00
|
|
|
size_t i = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2006-11-05 17:53:34 +01:00
|
|
|
cp = xcp;
|
2012-10-03 19:24:23 +02:00
|
|
|
while (i < (size_t)x_arg) {
|
2008-10-28 15:32:43 +01:00
|
|
|
utf_ptradjx(cp, cp2);
|
2006-11-05 17:53:34 +01:00
|
|
|
if (cp2 > xep)
|
|
|
|
break;
|
|
|
|
cp = cp2;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!i) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2006-11-05 17:53:34 +01:00
|
|
|
x_delete(i, false);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete nc chars to the right of the cursor (including cursor position) */
|
|
|
|
static void
|
2012-07-20 22:50:07 +02:00
|
|
|
x_delete(size_t nc, bool push)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2012-07-20 22:50:07 +02:00
|
|
|
size_t i, nb, nw;
|
2006-11-05 16:31:37 +01:00
|
|
|
char *cp;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (nc == 0)
|
|
|
|
return;
|
2006-11-05 17:41:02 +01:00
|
|
|
|
|
|
|
nw = 0;
|
|
|
|
cp = xcp;
|
2006-11-05 18:11:25 +01:00
|
|
|
for (i = 0; i < nc; ++i) {
|
|
|
|
char *cp2;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
j = x_size2(cp, &cp2);
|
|
|
|
if (cp2 > xep)
|
|
|
|
break;
|
|
|
|
cp = cp2;
|
|
|
|
nw += j;
|
|
|
|
}
|
2006-11-05 17:41:02 +01:00
|
|
|
nb = cp - xcp;
|
2007-07-01 23:10:29 +02:00
|
|
|
/* nc = i; */
|
2006-11-05 17:41:02 +01:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if (xmp != NULL && xmp > xcp) {
|
2006-11-05 17:41:02 +01:00
|
|
|
if (xcp + nb > xmp)
|
2005-05-23 05:06:10 +02:00
|
|
|
xmp = xcp;
|
|
|
|
else
|
2006-11-05 17:41:02 +01:00
|
|
|
xmp -= nb;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* This lets us yank a word we have deleted.
|
|
|
|
*/
|
|
|
|
if (push)
|
2006-11-05 17:41:02 +01:00
|
|
|
x_push(nb);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2006-11-05 17:41:02 +01:00
|
|
|
xep -= nb;
|
2011-02-03 16:57:52 +01:00
|
|
|
/* Copies the NUL */
|
|
|
|
memmove(xcp, xcp + nb, xep - xcp + 1);
|
|
|
|
/* don't redraw */
|
2012-03-31 21:20:12 +02:00
|
|
|
x_adj_ok = false;
|
2006-11-05 22:00:08 +01:00
|
|
|
xlp_valid = false;
|
2006-11-05 16:31:37 +01:00
|
|
|
x_zots(xcp);
|
2005-05-23 05:06:10 +02:00
|
|
|
/*
|
|
|
|
* if we are already filling the line,
|
2011-02-03 16:57:52 +01:00
|
|
|
* there is no need to ' ', '\b'.
|
2005-05-23 05:06:10 +02:00
|
|
|
* But if we must, make sure we do the minimum.
|
|
|
|
*/
|
2007-05-21 21:25:32 +02:00
|
|
|
if ((i = xx_cols - 2 - x_col) > 0 || xep - xlp == 0) {
|
2007-05-24 21:15:46 +02:00
|
|
|
nw = i = (nw < i) ? nw : i;
|
2005-05-23 05:06:10 +02:00
|
|
|
while (i--)
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(' ');
|
2007-05-24 21:15:46 +02:00
|
|
|
if (x_col == xx_cols - 2) {
|
|
|
|
x_e_putc2((xep > xlp) ? '>' : (xbp > xbuf) ? '<' : ' ');
|
|
|
|
++nw;
|
|
|
|
}
|
2007-05-22 23:13:56 +02:00
|
|
|
while (nw--)
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2('\b');
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
/*x_goto(xcp);*/
|
2012-03-31 21:20:12 +02:00
|
|
|
x_adj_ok = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
xlp_valid = false;
|
2016-07-26 23:37:26 +02:00
|
|
|
x_lastpos();
|
2008-09-20 16:10:23 +02:00
|
|
|
x_modified();
|
2005-05-23 05:06:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_del_bword(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
x_delete(x_bword(), true);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_mv_bword(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2007-05-20 19:53:13 +02:00
|
|
|
x_bword();
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_mv_fword(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2012-07-20 22:33:15 +02:00
|
|
|
x_fword(true);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_del_fword(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2012-07-20 22:33:15 +02:00
|
|
|
x_delete(x_fword(false), true);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2012-07-20 22:50:07 +02:00
|
|
|
static size_t
|
2005-05-23 05:06:10 +02:00
|
|
|
x_bword(void)
|
|
|
|
{
|
2012-07-20 22:50:07 +02:00
|
|
|
size_t nb = 0;
|
2005-06-09 00:22:24 +02:00
|
|
|
char *cp = xcp;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (cp == xbuf) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
while (x_arg--) {
|
|
|
|
while (cp != xbuf && is_mfs(cp[-1])) {
|
|
|
|
cp--;
|
2006-11-05 18:24:00 +01:00
|
|
|
nb++;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
while (cp != xbuf && !is_mfs(cp[-1])) {
|
|
|
|
cp--;
|
2006-11-05 18:24:00 +01:00
|
|
|
nb++;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
x_goto(cp);
|
2012-07-20 22:33:15 +02:00
|
|
|
return (x_nb2nc(nb));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2012-07-20 22:50:07 +02:00
|
|
|
static size_t
|
2012-07-20 22:33:15 +02:00
|
|
|
x_fword(bool move)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2012-07-20 22:50:07 +02:00
|
|
|
size_t nc;
|
2012-07-20 22:33:15 +02:00
|
|
|
char *cp = xcp;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (cp == xep) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
while (x_arg--) {
|
2007-07-01 23:10:29 +02:00
|
|
|
while (cp != xep && is_mfs(*cp))
|
2005-05-23 05:06:10 +02:00
|
|
|
cp++;
|
2007-07-01 23:10:29 +02:00
|
|
|
while (cp != xep && !is_mfs(*cp))
|
2005-05-23 05:06:10 +02:00
|
|
|
cp++;
|
|
|
|
}
|
2012-07-20 22:33:15 +02:00
|
|
|
nc = x_nb2nc(cp - xcp);
|
2006-11-05 18:24:00 +01:00
|
|
|
if (move)
|
|
|
|
x_goto(cp);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (nc);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
x_goto(char *cp)
|
|
|
|
{
|
2013-07-26 22:33:39 +02:00
|
|
|
cp = cp >= xep ? xep : x_bs0(cp, xbuf);
|
2016-05-06 00:56:15 +02:00
|
|
|
if (cp < xbp || cp >= utf_skipcols(xbp, x_displen, NULL)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
/* we are heading off screen */
|
|
|
|
xcp = cp;
|
|
|
|
x_adjust();
|
2011-02-03 16:57:52 +01:00
|
|
|
} else if (cp < xcp) {
|
|
|
|
/* move back */
|
2006-11-05 20:56:20 +01:00
|
|
|
while (cp < xcp)
|
2008-10-26 21:59:40 +01:00
|
|
|
x_bs3(&xcp);
|
2011-02-03 16:57:52 +01:00
|
|
|
} else if (cp > xcp) {
|
|
|
|
/* move forward */
|
2006-11-05 20:56:20 +01:00
|
|
|
while (cp > xcp)
|
2006-11-05 16:31:37 +01:00
|
|
|
x_zotc3(&xcp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 22:33:39 +02:00
|
|
|
static char *
|
|
|
|
x_bs0(char *cp, char *lower_bound)
|
|
|
|
{
|
|
|
|
if (UTFMODE)
|
|
|
|
while ((!lower_bound || (cp > lower_bound)) &&
|
|
|
|
((*(unsigned char *)cp & 0xC0) == 0x80))
|
|
|
|
--cp;
|
|
|
|
return (cp);
|
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
static void
|
2008-10-26 21:59:40 +01:00
|
|
|
x_bs3(char **p)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2013-07-26 22:33:39 +02:00
|
|
|
*p = x_bs0((*p) - 1, NULL);
|
2008-10-26 21:59:40 +01:00
|
|
|
i = x_size2(*p, NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
while (i--)
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2('\b');
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-11-05 16:31:37 +01:00
|
|
|
x_size2(char *cp, char **dcp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
uint8_t c = *(unsigned char *)cp;
|
2006-11-05 16:31:37 +01:00
|
|
|
|
2008-12-04 19:11:08 +01:00
|
|
|
if (UTFMODE && (c > 0x7F))
|
2006-11-05 16:31:37 +01:00
|
|
|
return (utf_widthadj(cp, (const char **)dcp));
|
|
|
|
if (dcp)
|
|
|
|
*dcp = cp + 1;
|
2005-06-09 00:22:24 +02:00
|
|
|
if (c == '\t')
|
2011-02-03 16:57:52 +01:00
|
|
|
/* Kludge, tabs are always four spaces. */
|
|
|
|
return (4);
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
if (ISCTRL(c) && /* but not C1 */ c < 0x80)
|
2011-02-03 16:57:52 +01:00
|
|
|
/* control unsigned char */
|
|
|
|
return (2);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-11-05 16:31:37 +01:00
|
|
|
x_zots(char *str)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2012-11-26 23:39:14 +01:00
|
|
|
int adj = x_adj_done;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
x_lastcp();
|
2014-07-13 13:34:29 +02:00
|
|
|
while (*str && str < xlp && x_col < xx_cols && adj == x_adj_done)
|
2006-11-05 16:31:37 +01:00
|
|
|
x_zotc3(&str);
|
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
static void
|
2006-11-05 16:31:37 +01:00
|
|
|
x_zotc3(char **cp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
unsigned char c = **(unsigned char **)cp;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c == '\t') {
|
2009-03-15 19:48:43 +01:00
|
|
|
/* Kludge, tabs are always four spaces. */
|
2016-07-25 02:04:48 +02:00
|
|
|
x_e_puts(T4spaces);
|
2006-11-05 16:31:37 +01:00
|
|
|
(*cp)++;
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
} else if (ISCTRL(c) && /* but not C1 */ c < 0x80) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2('^');
|
|
|
|
x_e_putc2(UNCTRL(c));
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
(*cp)++;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc3((const char **)cp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_mv_back(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-05 18:34:39 +01:00
|
|
|
if (xcp == xbuf) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2006-11-05 18:34:39 +01:00
|
|
|
while (x_arg--) {
|
|
|
|
x_goto(xcp - 1);
|
|
|
|
if (xcp == xbuf)
|
|
|
|
break;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_mv_forw(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-05 18:54:46 +01:00
|
|
|
char *cp = xcp, *cp2;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2006-11-05 18:54:46 +01:00
|
|
|
if (xcp == xep) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2006-11-05 18:54:46 +01:00
|
|
|
while (x_arg--) {
|
2008-10-28 15:32:43 +01:00
|
|
|
utf_ptradjx(cp, cp2);
|
2006-11-05 18:54:46 +01:00
|
|
|
if (cp2 > xep)
|
|
|
|
break;
|
|
|
|
cp = cp2;
|
|
|
|
}
|
|
|
|
x_goto(cp);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_search_char_forw(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
char *cp = xcp;
|
2006-11-05 18:54:46 +01:00
|
|
|
char tmp[4];
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
*xep = '\0';
|
2006-11-05 18:54:46 +01:00
|
|
|
if (x_e_getmbc(tmp) < 0) {
|
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2006-11-05 18:54:46 +01:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
while (x_arg--) {
|
2006-11-05 18:54:46 +01:00
|
|
|
if ((cp = (cp == xep) ? NULL : strstr(cp + 1, tmp)) == NULL &&
|
|
|
|
(cp = strstr(xbuf, tmp)) == NULL) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
x_goto(cp);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_search_char_back(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2008-04-19 19:21:55 +02:00
|
|
|
char *cp = xcp, *p, tmp[4];
|
|
|
|
bool b;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2006-11-05 18:54:46 +01:00
|
|
|
if (x_e_getmbc(tmp) < 0) {
|
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2006-11-05 18:54:46 +01:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
for (; x_arg--; cp = p)
|
|
|
|
for (p = cp; ; ) {
|
|
|
|
if (p-- == xbuf)
|
|
|
|
p = xep;
|
2006-11-05 18:54:46 +01:00
|
|
|
if (p == cp) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2006-11-05 18:54:46 +01:00
|
|
|
if ((tmp[1] && ((p+1) > xep)) ||
|
|
|
|
(tmp[2] && ((p+2) > xep)))
|
|
|
|
continue;
|
2008-04-19 19:21:55 +02:00
|
|
|
b = true;
|
2006-11-05 18:54:46 +01:00
|
|
|
if (*p != tmp[0])
|
2008-04-19 19:21:55 +02:00
|
|
|
b = false;
|
2006-11-05 18:54:46 +01:00
|
|
|
if (b && tmp[1] && p[1] != tmp[1])
|
2008-04-19 19:21:55 +02:00
|
|
|
b = false;
|
2006-11-05 18:54:46 +01:00
|
|
|
if (b && tmp[2] && p[2] != tmp[2])
|
2008-04-19 19:21:55 +02:00
|
|
|
b = false;
|
2006-11-05 18:54:46 +01:00
|
|
|
if (b)
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
x_goto(cp);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_newline(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2('\r');
|
|
|
|
x_e_putc2('\n');
|
2005-05-23 05:06:10 +02:00
|
|
|
x_flush();
|
|
|
|
*xep++ = '\n';
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KEOL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_end_of_text(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2016-07-28 23:39:04 +02:00
|
|
|
unsigned char tmp;
|
2016-08-01 16:25:39 +02:00
|
|
|
char *cp = (void *)&tmp;
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
|
2016-07-28 23:39:04 +02:00
|
|
|
tmp = isedchar(edchars.eof) ? (unsigned char)edchars.eof :
|
|
|
|
(unsigned char)CTRL('D');
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
x_zotc3(&cp);
|
2005-05-23 05:06:10 +02:00
|
|
|
x_putc('\r');
|
|
|
|
x_putc('\n');
|
|
|
|
x_flush();
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KEOL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2005-06-08 23:51:22 +02:00
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_beg_hist(int c MKSH_A_UNUSED)
|
2005-06-08 23:51:22 +02:00
|
|
|
{
|
|
|
|
x_load_hist(history);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2005-06-08 23:51:22 +02:00
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_end_hist(int c MKSH_A_UNUSED)
|
2005-06-08 23:51:22 +02:00
|
|
|
{
|
|
|
|
x_load_hist(histptr);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2005-06-08 23:51:22 +02:00
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_prev_com(int c MKSH_A_UNUSED)
|
2005-06-08 23:51:22 +02:00
|
|
|
{
|
|
|
|
x_load_hist(x_histp - x_arg);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2005-06-08 23:51:22 +02:00
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_next_com(int c MKSH_A_UNUSED)
|
2005-06-08 23:51:22 +02:00
|
|
|
{
|
|
|
|
x_load_hist(x_histp + x_arg);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* Goto a particular history number obtained from argument.
|
2005-05-23 05:06:10 +02:00
|
|
|
* If no argument is given history 1 is probably not what you
|
|
|
|
* want so we'll simply go to the oldest one.
|
|
|
|
*/
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_goto_hist(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
if (x_arg_defaulted)
|
|
|
|
x_load_hist(history);
|
|
|
|
else
|
|
|
|
x_load_hist(histptr + x_arg - source->line);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
x_load_hist(char **hp)
|
|
|
|
{
|
2008-09-20 16:10:23 +02:00
|
|
|
char *sp = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2008-09-20 16:10:23 +02:00
|
|
|
if (hp == histptr + 1) {
|
2013-08-10 15:44:33 +02:00
|
|
|
sp = holdbufp;
|
2008-09-20 16:10:23 +02:00
|
|
|
modified = 0;
|
|
|
|
} else if (hp < history || hp > histptr) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2005-05-23 05:06:10 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-09-20 16:10:23 +02:00
|
|
|
if (sp == NULL)
|
|
|
|
sp = *hp;
|
2005-05-23 05:06:10 +02:00
|
|
|
x_histp = hp;
|
2008-09-20 16:10:23 +02:00
|
|
|
if (modified)
|
2013-08-10 15:44:33 +02:00
|
|
|
strlcpy(holdbufp, xbuf, LINE);
|
2008-09-20 16:10:23 +02:00
|
|
|
strlcpy(xbuf, sp, xend - xbuf);
|
2005-05-23 05:06:10 +02:00
|
|
|
xbp = xbuf;
|
|
|
|
xep = xcp = xbuf + strlen(xbuf);
|
2016-07-13 01:07:10 +02:00
|
|
|
x_adjust();
|
2008-09-20 16:10:23 +02:00
|
|
|
modified = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_nl_next_com(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-12-11 19:01:03 +01:00
|
|
|
if (!x_histncp || (x_histp != x_histncp && x_histp != histptr + 1))
|
|
|
|
/* fresh start of ^O */
|
|
|
|
x_histncp = x_histp;
|
|
|
|
x_nextcmd = source->line - (histptr - x_histncp) + 1;
|
2008-09-30 19:23:00 +02:00
|
|
|
return (x_newline('\n'));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
x_eot_del(int c)
|
|
|
|
{
|
|
|
|
if (xep == xbuf && x_arg_defaulted)
|
|
|
|
return (x_end_of_text(c));
|
|
|
|
else
|
|
|
|
return (x_del_char(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reverse incremental history search */
|
|
|
|
static int
|
|
|
|
x_search_hist(int c)
|
|
|
|
{
|
|
|
|
int offset = -1; /* offset of match in xbuf, else -1 */
|
2012-01-29 02:41:15 +01:00
|
|
|
char pat[80 + 1]; /* pattern buffer */
|
2005-05-23 05:06:10 +02:00
|
|
|
char *p = pat;
|
2007-10-25 17:23:10 +02:00
|
|
|
unsigned char f;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
*p = '\0';
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (offset < 0) {
|
|
|
|
x_e_puts("\nI-search: ");
|
|
|
|
x_e_puts(pat);
|
|
|
|
}
|
|
|
|
x_flush();
|
|
|
|
if ((c = x_e_getc()) < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2007-10-14 15:43:41 +02:00
|
|
|
f = x_tab[0][c];
|
2009-06-10 20:12:51 +02:00
|
|
|
if (c == CTRL('[')) {
|
2008-09-20 17:59:23 +02:00
|
|
|
if ((f & 0x7F) == XFUNC_meta1) {
|
|
|
|
if ((c = x_e_getc()) < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2008-09-20 17:59:23 +02:00
|
|
|
f = x_tab[1][c] & 0x7F;
|
|
|
|
if (f == XFUNC_meta1 || f == XFUNC_meta2)
|
2009-06-10 20:12:51 +02:00
|
|
|
x_meta1(CTRL('['));
|
2008-09-20 17:59:23 +02:00
|
|
|
x_e_ungetc(c);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-09-20 19:00:53 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2006-08-02 15:32:18 +02:00
|
|
|
if (f & 0x80) {
|
|
|
|
f &= 0x7F;
|
|
|
|
if ((c = x_e_getc()) != '~')
|
|
|
|
x_e_ungetc(c);
|
|
|
|
}
|
2009-09-20 19:00:53 +02:00
|
|
|
#endif
|
2008-09-20 17:59:23 +02:00
|
|
|
if (f == XFUNC_search_hist)
|
2005-05-23 05:06:10 +02:00
|
|
|
offset = x_search(pat, 0, offset);
|
|
|
|
else if (f == XFUNC_del_back) {
|
|
|
|
if (p == pat) {
|
|
|
|
offset = -1;
|
|
|
|
break;
|
|
|
|
}
|
2017-04-02 17:43:24 +02:00
|
|
|
if (p > pat) {
|
|
|
|
p = x_bs0(p - 1, pat);
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
if (p == pat)
|
|
|
|
offset = -1;
|
|
|
|
else
|
|
|
|
offset = x_search(pat, 1, offset);
|
|
|
|
continue;
|
|
|
|
} else if (f == XFUNC_insert) {
|
|
|
|
/* add char to pattern */
|
|
|
|
/* overflow check... */
|
2012-10-03 19:24:23 +02:00
|
|
|
if ((size_t)(p - pat) >= sizeof(pat) - 1) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*p++ = c, *p = '\0';
|
|
|
|
if (offset >= 0) {
|
|
|
|
/* already have partial match */
|
|
|
|
offset = x_match(xbuf, pat);
|
|
|
|
if (offset >= 0) {
|
|
|
|
x_goto(xbuf + offset + (p - pat) -
|
|
|
|
(*pat == '^'));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
offset = x_search(pat, 0, offset);
|
2008-10-19 22:15:45 +02:00
|
|
|
} else if (f == XFUNC_abort) {
|
|
|
|
if (offset >= 0)
|
|
|
|
x_load_hist(histptr + 1);
|
|
|
|
break;
|
2011-02-03 16:57:52 +01:00
|
|
|
} else {
|
|
|
|
/* other command */
|
2005-05-23 05:06:10 +02:00
|
|
|
x_e_ungetc(c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (offset < 0)
|
2016-07-26 22:43:14 +02:00
|
|
|
x_redraw('\n');
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* search backward from current line */
|
|
|
|
static int
|
|
|
|
x_search(char *pat, int sameline, int offset)
|
|
|
|
{
|
|
|
|
char **hp;
|
|
|
|
int i;
|
|
|
|
|
2005-06-09 00:22:24 +02:00
|
|
|
for (hp = x_histp - (sameline ? 0 : 1); hp >= history; --hp) {
|
2005-05-23 05:06:10 +02:00
|
|
|
i = x_match(*hp, pat);
|
|
|
|
if (i >= 0) {
|
|
|
|
if (offset < 0)
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2('\n');
|
2005-05-23 05:06:10 +02:00
|
|
|
x_load_hist(hp);
|
|
|
|
x_goto(xbuf + i + strlen(pat) - (*pat == '^'));
|
2009-06-08 22:06:50 +02:00
|
|
|
return (i);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2005-05-23 05:06:10 +02:00
|
|
|
x_histp = histptr;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2009-09-20 19:00:53 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2009-05-16 16:19:23 +02:00
|
|
|
/* anchored search up from current line */
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_search_hist_up(int c MKSH_A_UNUSED)
|
2009-05-16 16:19:23 +02:00
|
|
|
{
|
|
|
|
return (x_search_dir(-1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* anchored search down from current line */
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_search_hist_dn(int c MKSH_A_UNUSED)
|
2009-05-16 16:19:23 +02:00
|
|
|
{
|
|
|
|
return (x_search_dir(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* anchored search in the indicated direction */
|
|
|
|
static int
|
|
|
|
x_search_dir(int search_dir /* should've been bool */)
|
|
|
|
{
|
|
|
|
char **hp = x_histp + search_dir;
|
|
|
|
size_t curs = xcp - xbuf;
|
|
|
|
|
|
|
|
while (histptr >= hp && hp >= history) {
|
|
|
|
if (strncmp(xbuf, *hp, curs) == 0) {
|
|
|
|
x_load_hist(hp);
|
|
|
|
x_goto(xbuf + curs);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
hp += search_dir;
|
|
|
|
}
|
|
|
|
return (KSTD);
|
|
|
|
}
|
2009-09-20 19:00:53 +02:00
|
|
|
#endif
|
2009-05-16 16:19:23 +02:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/* return position of first match of pattern in string, else -1 */
|
|
|
|
static int
|
|
|
|
x_match(char *str, char *pat)
|
|
|
|
{
|
|
|
|
if (*pat == '^') {
|
2009-06-08 22:06:50 +02:00
|
|
|
return ((strncmp(str, pat + 1, strlen(pat + 1)) == 0) ? 0 : -1);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
char *q = strstr(str, pat);
|
2009-06-08 22:06:50 +02:00
|
|
|
return ((q == NULL) ? -1 : q - str);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_del_line(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
*xep = 0;
|
2016-07-26 22:43:14 +02:00
|
|
|
x_push(xep - (xcp = xbuf));
|
2005-05-23 05:06:10 +02:00
|
|
|
xlp = xbp = xep = xbuf;
|
|
|
|
xlp_valid = true;
|
|
|
|
*xcp = 0;
|
|
|
|
xmp = NULL;
|
2016-07-26 22:43:14 +02:00
|
|
|
x_redraw('\r');
|
2008-09-20 16:10:23 +02:00
|
|
|
x_modified();
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_mv_end(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
x_goto(xep);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-09-01 14:59:12 +02:00
|
|
|
x_mv_beg(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
x_goto(xbuf);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_draw_line(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2016-07-26 22:43:14 +02:00
|
|
|
x_redraw('\n');
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2007-07-31 17:29:40 +02:00
|
|
|
static int
|
2016-07-25 22:43:54 +02:00
|
|
|
x_cls(int c MKSH_A_UNUSED)
|
2007-07-31 17:29:40 +02:00
|
|
|
{
|
2016-07-25 22:43:54 +02:00
|
|
|
shf_puts(MKSH_CLS_STRING, shl_out);
|
2016-07-26 22:43:14 +02:00
|
|
|
x_redraw(0);
|
2007-07-31 17:29:40 +02:00
|
|
|
return (KSTD);
|
|
|
|
}
|
|
|
|
|
2016-07-26 23:37:26 +02:00
|
|
|
/*
|
|
|
|
* clear line from x_col (current cursor position) to xx_cols - 2,
|
|
|
|
* then output lastch, then go back to x_col; if lastch is space,
|
|
|
|
* clear with termcap instead of spaces, or not if line_was_cleared;
|
|
|
|
* lastch MUST be an ASCII character with wcwidth(lastch) == 1
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
x_clrtoeol(int lastch, bool line_was_cleared)
|
|
|
|
{
|
|
|
|
int col;
|
|
|
|
|
|
|
|
if (lastch == ' ' && !line_was_cleared && x_term_mode == 1) {
|
|
|
|
shf_puts("\033[K", shl_out);
|
|
|
|
line_was_cleared = true;
|
|
|
|
}
|
|
|
|
if (lastch == ' ' && line_was_cleared)
|
|
|
|
return;
|
|
|
|
|
|
|
|
col = x_col;
|
|
|
|
while (col < (xx_cols - 2)) {
|
|
|
|
x_putc(' ');
|
|
|
|
++col;
|
|
|
|
}
|
|
|
|
x_putc(lastch);
|
|
|
|
++col;
|
|
|
|
while (col > x_col) {
|
|
|
|
x_putc('\b');
|
|
|
|
--col;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output the prompt, assuming a line has just been started */
|
|
|
|
static void
|
|
|
|
x_pprompt(void)
|
|
|
|
{
|
|
|
|
if (prompt_trunc != -1)
|
|
|
|
pprompt(prompt, prompt_trunc);
|
|
|
|
x_col = pwidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output CR, then redraw the line, clearing to EOL if needed (cr ≠ 0, LF) */
|
2005-05-23 05:06:10 +02:00
|
|
|
static void
|
2016-07-26 22:43:14 +02:00
|
|
|
x_redraw(int cr)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2016-07-26 23:37:26 +02:00
|
|
|
int lch;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2012-03-31 21:20:12 +02:00
|
|
|
x_adj_ok = false;
|
2016-07-26 23:37:26 +02:00
|
|
|
/* clear the line */
|
2016-07-26 22:43:14 +02:00
|
|
|
x_e_putc2(cr ? cr : '\r');
|
2005-05-23 05:06:10 +02:00
|
|
|
x_flush();
|
2016-07-26 23:37:26 +02:00
|
|
|
/* display the prompt */
|
|
|
|
if (xbp == xbuf)
|
|
|
|
x_pprompt();
|
2005-05-23 05:06:10 +02:00
|
|
|
x_displen = xx_cols - 2 - x_col;
|
2016-07-26 23:37:26 +02:00
|
|
|
/* display the line content */
|
2005-05-23 05:06:10 +02:00
|
|
|
xlp_valid = false;
|
2006-11-05 16:31:37 +01:00
|
|
|
x_zots(xbp);
|
2016-07-26 23:37:26 +02:00
|
|
|
/* check whether there is more off-screen */
|
|
|
|
lch = xep > xlp ? (xbp > xbuf ? '*' : '>') : (xbp > xbuf) ? '<' : ' ';
|
|
|
|
/* clear the rest of the line */
|
|
|
|
x_clrtoeol(lch, !cr || cr == '\n');
|
|
|
|
/* go back to actual cursor position */
|
|
|
|
x_lastpos();
|
2012-03-31 21:20:12 +02:00
|
|
|
x_adj_ok = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_transpose(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2008-04-19 19:21:55 +02:00
|
|
|
unsigned int tmpa, tmpb;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*-
|
|
|
|
* What transpose is meant to do seems to be up for debate. This
|
2005-05-23 05:06:10 +02:00
|
|
|
* is a general summary of the options; the text is abcd with the
|
|
|
|
* upper case character or underscore indicating the cursor position:
|
2009-06-10 20:12:51 +02:00
|
|
|
* Who Before After Before After
|
2009-06-11 14:42:21 +02:00
|
|
|
* AT&T ksh in emacs mode: abCd abdC abcd_ (bell)
|
|
|
|
* AT&T ksh in gmacs mode: abCd baCd abcd_ abdc_
|
2009-06-10 20:12:51 +02:00
|
|
|
* gnu emacs: abCd acbD abcd_ abdc_
|
2005-05-23 05:06:10 +02:00
|
|
|
* Pdksh currently goes with GNU behavior since I believe this is the
|
|
|
|
* most common version of emacs, unless in gmacs mode, in which case
|
2009-06-11 14:42:21 +02:00
|
|
|
* it does the AT&T ksh gmacs mode.
|
2005-05-23 05:06:10 +02:00
|
|
|
* This should really be broken up into 3 functions so users can bind
|
|
|
|
* to the one they want.
|
|
|
|
*/
|
|
|
|
if (xcp == xbuf) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (xcp == xep || Flag(FGMACS)) {
|
|
|
|
if (xcp - xbuf == 1) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* Gosling/Unipress emacs style: Swap two characters before
|
|
|
|
* the cursor, do not change cursor position
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2008-10-26 21:59:40 +01:00
|
|
|
x_bs3(&xcp);
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
if (utf_mbtowc(&tmpa, xcp) == (size_t)-1) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2006-11-05 16:31:37 +01:00
|
|
|
}
|
2008-10-26 21:59:40 +01:00
|
|
|
x_bs3(&xcp);
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
if (utf_mbtowc(&tmpb, xcp) == (size_t)-1) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2006-11-05 16:31:37 +01:00
|
|
|
}
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
utf_wctomb(xcp, tmpa);
|
2006-11-05 16:31:37 +01:00
|
|
|
x_zotc3(&xcp);
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
utf_wctomb(xcp, tmpb);
|
2006-11-05 16:31:37 +01:00
|
|
|
x_zotc3(&xcp);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* GNU emacs style: Swap the characters before and under the
|
2005-05-23 05:06:10 +02:00
|
|
|
* cursor, move cursor position along one.
|
|
|
|
*/
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
if (utf_mbtowc(&tmpa, xcp) == (size_t)-1) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2006-11-05 16:31:37 +01:00
|
|
|
}
|
2008-10-26 21:59:40 +01:00
|
|
|
x_bs3(&xcp);
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
if (utf_mbtowc(&tmpb, xcp) == (size_t)-1) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2006-11-05 16:31:37 +01:00
|
|
|
}
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
utf_wctomb(xcp, tmpa);
|
2006-11-05 16:31:37 +01:00
|
|
|
x_zotc3(&xcp);
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
utf_wctomb(xcp, tmpb);
|
2006-11-05 16:31:37 +01:00
|
|
|
x_zotc3(&xcp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2008-09-20 16:10:23 +02:00
|
|
|
x_modified();
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_literal(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
x_curprefix = -1;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_meta1(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
x_curprefix = 1;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_meta2(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
x_curprefix = 2;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 20:41:07 +02:00
|
|
|
static int
|
|
|
|
x_meta3(int c MKSH_A_UNUSED)
|
|
|
|
{
|
|
|
|
x_curprefix = 3;
|
|
|
|
return (KSTD);
|
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_kill(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2012-07-20 22:50:07 +02:00
|
|
|
size_t col = xcp - xbuf;
|
|
|
|
size_t lastcol = xep - xbuf;
|
|
|
|
size_t ndel, narg;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2012-07-20 22:50:07 +02:00
|
|
|
if (x_arg_defaulted || (narg = x_arg) > lastcol)
|
|
|
|
narg = lastcol;
|
|
|
|
if (narg < col) {
|
|
|
|
x_goto(xbuf + narg);
|
|
|
|
ndel = col - narg;
|
|
|
|
} else
|
|
|
|
ndel = narg - col;
|
|
|
|
x_delete(x_nb2nc(ndel), true);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-07-26 22:43:14 +02:00
|
|
|
x_push(size_t nchars)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2015-09-05 21:19:12 +02:00
|
|
|
afree(killstack[killsp], AEDIT);
|
|
|
|
strndupx(killstack[killsp], xcp, nchars, AEDIT);
|
2005-05-23 05:06:10 +02:00
|
|
|
killsp = (killsp + 1) % KILLSIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_yank(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
if (killsp == 0)
|
|
|
|
killtp = KILLSIZE;
|
|
|
|
else
|
|
|
|
killtp = killsp;
|
2005-06-09 00:22:24 +02:00
|
|
|
killtp--;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (killstack[killtp] == 0) {
|
|
|
|
x_e_puts("\nnothing to yank");
|
2016-07-26 22:43:14 +02:00
|
|
|
x_redraw('\n');
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
xmp = xcp;
|
|
|
|
x_ins(killstack[killtp]);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_meta_yank(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-08-27 20:06:52 +02:00
|
|
|
size_t len;
|
2008-04-19 19:21:55 +02:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((x_last_command != XFUNC_yank && x_last_command != XFUNC_meta_yank) ||
|
|
|
|
killstack[killtp] == 0) {
|
|
|
|
killtp = killsp;
|
|
|
|
x_e_puts("\nyank something first");
|
2016-07-26 22:43:14 +02:00
|
|
|
x_redraw('\n');
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
len = strlen(killstack[killtp]);
|
|
|
|
x_goto(xcp - len);
|
2012-07-20 22:50:07 +02:00
|
|
|
x_delete(x_nb2nc(len), false);
|
2005-05-23 05:06:10 +02:00
|
|
|
do {
|
|
|
|
if (killtp == 0)
|
|
|
|
killtp = KILLSIZE - 1;
|
|
|
|
else
|
|
|
|
killtp--;
|
|
|
|
} while (killstack[killtp] == 0);
|
|
|
|
x_ins(killstack[killtp]);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_abort(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
/* x_zotc(c); */
|
|
|
|
xlp = xep = xcp = xbp = xbuf;
|
|
|
|
xlp_valid = true;
|
|
|
|
*xcp = 0;
|
2008-09-20 16:10:23 +02:00
|
|
|
x_modified();
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KINTR);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_error(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2009-09-20 19:23:52 +02:00
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
/* special VT100 style key sequence hack */
|
|
|
|
static int
|
|
|
|
x_vt_hack(int c)
|
|
|
|
{
|
|
|
|
/* we only support PF2-'1' for now */
|
|
|
|
if (c != (2 << 8 | '1'))
|
|
|
|
return (x_error(c));
|
|
|
|
|
|
|
|
/* what's the next character? */
|
|
|
|
switch ((c = x_e_getc())) {
|
|
|
|
case '~':
|
|
|
|
x_arg = 1;
|
2011-02-09 14:08:18 +01:00
|
|
|
x_arg_defaulted = true;
|
2016-09-01 14:59:12 +02:00
|
|
|
return (x_mv_beg(0));
|
2009-09-20 19:23:52 +02:00
|
|
|
case ';':
|
|
|
|
/* "interesting" sequence detected */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto unwind_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX x_e_ungetc is one-octet only */
|
2010-01-08 23:21:06 +01:00
|
|
|
if ((c = x_e_getc()) != '5' && c != '3')
|
2009-09-20 19:23:52 +02:00
|
|
|
goto unwind_err;
|
|
|
|
|
2010-01-08 23:21:06 +01:00
|
|
|
/*-
|
|
|
|
* At this point, we have read the following octets so far:
|
2010-08-28 22:22:24 +02:00
|
|
|
* - ESC+[ or ESC+O or Ctrl-X (Prefix 2)
|
2010-01-08 23:21:06 +01:00
|
|
|
* - 1 (vt_hack)
|
|
|
|
* - ;
|
|
|
|
* - 5 (Ctrl key combiner) or 3 (Alt key combiner)
|
|
|
|
* We can now accept one more octet designating the key.
|
|
|
|
*/
|
|
|
|
|
2009-09-20 19:23:52 +02:00
|
|
|
switch ((c = x_e_getc())) {
|
|
|
|
case 'C':
|
|
|
|
return (x_mv_fword(c));
|
|
|
|
case 'D':
|
|
|
|
return (x_mv_bword(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
unwind_err:
|
|
|
|
x_e_ungetc(c);
|
|
|
|
return (x_error(c));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
static char *
|
2008-12-13 18:02:18 +01:00
|
|
|
x_mapin(const char *cp, Area *ap)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2010-07-18 00:09:40 +02:00
|
|
|
char *news, *op;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2010-07-18 00:09:40 +02:00
|
|
|
strdupx(news, cp, ap);
|
|
|
|
op = news;
|
2005-05-23 05:06:10 +02:00
|
|
|
while (*cp) {
|
|
|
|
/* XXX -- should handle \^ escape? */
|
|
|
|
if (*cp == '^') {
|
|
|
|
cp++;
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
/*XXX or ^^ escape? this is ugly. */
|
2011-02-03 16:57:52 +01:00
|
|
|
if (*cp >= '?')
|
|
|
|
/* includes '?'; ASCII */
|
2009-06-10 20:12:51 +02:00
|
|
|
*op++ = CTRL(*cp);
|
2005-05-23 05:06:10 +02:00
|
|
|
else {
|
|
|
|
*op++ = '^';
|
|
|
|
cp--;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
*op++ = *cp;
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
*op = '\0';
|
|
|
|
|
2010-07-18 00:09:40 +02:00
|
|
|
return (news);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2006-11-09 16:02:31 +01:00
|
|
|
static void
|
|
|
|
x_mapout2(int c, char **buf)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-09 16:02:31 +01:00
|
|
|
char *p = *buf;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
if (ISCTRL(c)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
*p++ = '^';
|
|
|
|
*p++ = UNCTRL(c);
|
|
|
|
} else
|
|
|
|
*p++ = c;
|
|
|
|
*p = 0;
|
2006-11-09 16:02:31 +01:00
|
|
|
*buf = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
x_mapout(int c)
|
|
|
|
{
|
|
|
|
static char buf[8];
|
|
|
|
char *bp = buf;
|
|
|
|
|
|
|
|
x_mapout2(c, &bp);
|
|
|
|
return (buf);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
x_print(int prefix, int key)
|
|
|
|
{
|
2006-08-02 15:32:18 +02:00
|
|
|
int f = x_tab[prefix][key];
|
|
|
|
|
2007-02-07 03:01:43 +01:00
|
|
|
if (prefix)
|
|
|
|
/* prefix == 1 || prefix == 2 */
|
2015-07-10 20:41:07 +02:00
|
|
|
shf_puts(x_mapout(prefix == 1 ? CTRL('[') :
|
|
|
|
prefix == 2 ? CTRL('X') : 0), shl_stdout);
|
2009-09-20 19:00:53 +02:00
|
|
|
#ifdef MKSH_SMALL
|
|
|
|
shprintf("%s = ", x_mapout(key));
|
|
|
|
#else
|
2007-02-07 03:01:43 +01:00
|
|
|
shprintf("%s%s = ", x_mapout(key), (f & 0x80) ? "~" : "");
|
2009-09-20 19:00:53 +02:00
|
|
|
if (XFUNC_VALUE(f) != XFUNC_ins_string)
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2016-07-25 02:04:48 +02:00
|
|
|
shprintf(Tf_sN, x_ftab[XFUNC_VALUE(f)].xf_name);
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
|
|
|
shprintf("'%s'\n", x_atab[prefix][key]);
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-06-09 00:22:24 +02:00
|
|
|
x_bind(const char *a1, const char *a2,
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2011-02-03 16:57:52 +01:00
|
|
|
/* bind -m */
|
|
|
|
bool macro,
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2011-02-03 16:57:52 +01:00
|
|
|
/* bind -l */
|
|
|
|
bool list)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2007-10-25 17:23:10 +02:00
|
|
|
unsigned char f;
|
2005-05-23 05:06:10 +02:00
|
|
|
int prefix, key;
|
2009-09-23 20:04:58 +02:00
|
|
|
char *m1, *m2;
|
2009-09-20 19:00:53 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2009-09-23 20:04:58 +02:00
|
|
|
char *sp = NULL;
|
2007-09-09 20:06:42 +02:00
|
|
|
bool hastilde;
|
2009-09-20 19:00:53 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (x_tab == NULL) {
|
2010-08-28 22:22:24 +02:00
|
|
|
bi_errorf("can't bind, not a tty");
|
2006-08-02 14:49:04 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
/* List function names */
|
|
|
|
if (list) {
|
|
|
|
for (f = 0; f < NELEM(x_ftab); f++)
|
2014-11-14 21:00:40 +01:00
|
|
|
if (!(x_ftab[f].xf_flags & XF_NOBIND))
|
2016-07-25 02:04:48 +02:00
|
|
|
shprintf(Tf_sN, x_ftab[f].xf_name);
|
2006-08-02 14:49:04 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (a1 == NULL) {
|
|
|
|
for (prefix = 0; prefix < X_NTABS; prefix++)
|
|
|
|
for (key = 0; key < X_TABSZ; key++) {
|
2009-09-20 19:00:53 +02:00
|
|
|
f = XFUNC_VALUE(x_tab[prefix][key]);
|
2009-09-23 20:04:58 +02:00
|
|
|
if (f == XFUNC_insert || f == XFUNC_error
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
|| (macro && f != XFUNC_ins_string)
|
|
|
|
#endif
|
|
|
|
)
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
|
|
|
x_print(prefix, key);
|
|
|
|
}
|
2006-08-02 14:49:04 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2007-09-09 20:06:42 +02:00
|
|
|
m2 = m1 = x_mapin(a1, ATEMP);
|
2009-12-05 18:43:50 +01:00
|
|
|
prefix = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
for (;; m1++) {
|
2007-10-25 17:23:10 +02:00
|
|
|
key = (unsigned char)*m1;
|
2009-09-20 19:00:53 +02:00
|
|
|
f = XFUNC_VALUE(x_tab[prefix][key]);
|
2006-08-02 15:32:18 +02:00
|
|
|
if (f == XFUNC_meta1)
|
2005-05-23 05:06:10 +02:00
|
|
|
prefix = 1;
|
2006-08-02 15:32:18 +02:00
|
|
|
else if (f == XFUNC_meta2)
|
2005-05-23 05:06:10 +02:00
|
|
|
prefix = 2;
|
2015-07-10 20:41:07 +02:00
|
|
|
else if (f == XFUNC_meta3)
|
|
|
|
prefix = 3;
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2009-09-20 19:00:53 +02:00
|
|
|
if (*++m1
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
&& ((*m1 != '~') || *(m1 + 1))
|
|
|
|
#endif
|
|
|
|
) {
|
2010-08-28 22:22:24 +02:00
|
|
|
char msg[256];
|
2006-08-02 14:49:04 +02:00
|
|
|
const char *c = a1;
|
2010-08-28 22:22:24 +02:00
|
|
|
m1 = msg;
|
2012-10-03 19:24:23 +02:00
|
|
|
while (*c && (size_t)(m1 - msg) < sizeof(msg) - 3)
|
2006-11-09 16:02:31 +01:00
|
|
|
x_mapout2(*c++, &m1);
|
2016-01-21 19:24:45 +01:00
|
|
|
bi_errorf("too long key sequence: %s", msg);
|
2006-08-02 14:49:04 +02:00
|
|
|
return (1);
|
|
|
|
}
|
2009-09-20 19:00:53 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2011-03-07 21:30:41 +01:00
|
|
|
hastilde = tobool(*m1);
|
2009-09-20 19:00:53 +02:00
|
|
|
#endif
|
2007-09-09 20:06:42 +02:00
|
|
|
afree(m2, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (a2 == NULL) {
|
|
|
|
x_print(prefix, key);
|
2006-08-02 14:49:04 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-09-23 20:04:58 +02:00
|
|
|
if (*a2 == 0) {
|
2005-05-23 05:06:10 +02:00
|
|
|
f = XFUNC_insert;
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
} else if (macro) {
|
|
|
|
f = XFUNC_ins_string;
|
|
|
|
sp = x_mapin(a2, AEDIT);
|
|
|
|
#endif
|
|
|
|
} else {
|
2005-05-23 05:06:10 +02:00
|
|
|
for (f = 0; f < NELEM(x_ftab); f++)
|
2014-11-14 21:00:40 +01:00
|
|
|
if (!strcmp(x_ftab[f].xf_name, a2))
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
if (f == NELEM(x_ftab) || x_ftab[f].xf_flags & XF_NOBIND) {
|
2016-01-21 19:24:45 +01:00
|
|
|
bi_errorf("%s: no such function", a2);
|
2006-08-02 14:49:04 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2009-09-20 19:00:53 +02:00
|
|
|
if (XFUNC_VALUE(x_tab[prefix][key]) == XFUNC_ins_string &&
|
2006-08-02 15:32:18 +02:00
|
|
|
x_atab[prefix][key])
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(x_atab[prefix][key], AEDIT);
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2009-09-20 19:00:53 +02:00
|
|
|
x_tab[prefix][key] = f
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
| (hastilde ? 0x80 : 0)
|
|
|
|
#endif
|
|
|
|
;
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2005-05-23 05:06:10 +02:00
|
|
|
x_atab[prefix][key] = sp;
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2006-11-10 05:31:04 +01:00
|
|
|
/* Track what the user has bound so x_mode(true) won't toast things */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (f == XFUNC_insert)
|
|
|
|
x_bound[(prefix * X_TABSZ + key) / 8] &=
|
|
|
|
~(1 << ((prefix * X_TABSZ + key) % 8));
|
|
|
|
else
|
|
|
|
x_bound[(prefix * X_TABSZ + key) / 8] |=
|
|
|
|
(1 << ((prefix * X_TABSZ + key) % 8));
|
|
|
|
|
2006-08-02 14:49:04 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bind_if_not_bound(int p, int k, int func)
|
|
|
|
{
|
2012-10-03 19:24:23 +02:00
|
|
|
int t;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Has user already bound this key?
|
|
|
|
* If so, do not override it.
|
|
|
|
*/
|
|
|
|
t = p * X_TABSZ + k;
|
|
|
|
if (x_bound[t >> 3] & (1 << (t & 7)))
|
2005-05-23 05:06:10 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
x_tab[p][k] = func;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_set_mark(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
xmp = xcp;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_kill_region(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2012-07-20 22:50:07 +02:00
|
|
|
size_t rsize;
|
2005-06-09 00:22:24 +02:00
|
|
|
char *xr;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (xmp == NULL) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (xmp > xcp) {
|
|
|
|
rsize = xmp - xcp;
|
|
|
|
xr = xcp;
|
|
|
|
} else {
|
|
|
|
rsize = xcp - xmp;
|
|
|
|
xr = xmp;
|
|
|
|
}
|
|
|
|
x_goto(xr);
|
2012-07-20 22:50:07 +02:00
|
|
|
x_delete(x_nb2nc(rsize), true);
|
2005-05-23 05:06:10 +02:00
|
|
|
xmp = xr;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_xchg_point_mark(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
char *tmp;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (xmp == NULL) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
tmp = xmp;
|
|
|
|
xmp = xcp;
|
2005-06-09 00:22:24 +02:00
|
|
|
x_goto(tmp);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_noop(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* File/command name completion routines
|
|
|
|
*/
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_comp_comm(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
do_complete(XCF_COMMAND, CT_COMPLETE);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_list_comm(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
do_complete(XCF_COMMAND, CT_LIST);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_complete(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
do_complete(XCF_COMMAND_FILE, CT_COMPLETE);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_enumerate(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
do_complete(XCF_COMMAND_FILE, CT_LIST);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_comp_file(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
do_complete(XCF_FILE, CT_COMPLETE);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_list_file(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
do_complete(XCF_FILE, CT_LIST);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_comp_list(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
do_complete(XCF_COMMAND_FILE, CT_COMPLIST);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_expand(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
char **words;
|
2008-04-19 19:21:55 +02:00
|
|
|
int start, end, nwords, i;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
i = XCF_FILE;
|
|
|
|
nwords = x_cf_glob(&i, xbuf, xep - xbuf, xcp - xbuf,
|
|
|
|
&start, &end, &words);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (nwords == 0) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
x_goto(xbuf + start);
|
2012-07-20 22:50:07 +02:00
|
|
|
x_delete(x_nb2nc(end - start), false);
|
2011-02-03 16:57:52 +01:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (i < nwords) {
|
2007-05-10 20:46:38 +02:00
|
|
|
if (x_escape(words[i], strlen(words[i]), x_do_ins) < 0 ||
|
2016-07-25 02:04:48 +02:00
|
|
|
(++i < nwords && x_ins(T1space) < 0)) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
x_adjust();
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-02-03 16:57:52 +01:00
|
|
|
do_complete(
|
|
|
|
/* XCF_{COMMAND,FILE,COMMAND_FILE} */
|
|
|
|
int flags,
|
|
|
|
/* 0 for list, 1 for complete and 2 for complete-list */
|
2005-05-23 05:06:10 +02:00
|
|
|
Comp_type type)
|
|
|
|
{
|
|
|
|
char **words;
|
2008-04-19 19:21:55 +02:00
|
|
|
int start, end, nlen, olen, nwords;
|
2012-07-20 22:16:26 +02:00
|
|
|
bool completed;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
nwords = x_cf_glob(&flags, xbuf, xep - xbuf, xcp - xbuf,
|
|
|
|
&start, &end, &words);
|
2005-05-23 05:06:10 +02:00
|
|
|
/* no match */
|
|
|
|
if (nwords == 0) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2005-05-23 05:06:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (type == CT_LIST) {
|
2011-06-04 17:06:50 +02:00
|
|
|
x_print_expansions(nwords, words,
|
|
|
|
tobool(flags & XCF_IS_COMMAND));
|
2005-05-23 05:06:10 +02:00
|
|
|
x_redraw(0);
|
|
|
|
x_free_words(nwords, words);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
olen = end - start;
|
|
|
|
nlen = x_longest_prefix(nwords, words);
|
2012-08-24 22:57:46 +02:00
|
|
|
if (nwords == 1) {
|
2012-07-20 22:16:26 +02:00
|
|
|
/*
|
2012-08-24 22:57:46 +02:00
|
|
|
* always complete single matches;
|
|
|
|
* any expansion of parameter substitution
|
|
|
|
* is always at most one result, too
|
2012-07-20 22:16:26 +02:00
|
|
|
*/
|
2008-04-19 19:21:55 +02:00
|
|
|
completed = true;
|
2012-07-20 22:16:26 +02:00
|
|
|
} else {
|
|
|
|
char *unescaped;
|
|
|
|
|
2012-08-24 22:57:46 +02:00
|
|
|
/* make a copy of the original string part */
|
2012-07-20 22:16:26 +02:00
|
|
|
strndupx(unescaped, xbuf + start, olen, ATEMP);
|
2012-08-24 22:57:46 +02:00
|
|
|
|
2012-10-21 20:33:46 +02:00
|
|
|
/* expand any tilde and unescape the string for comparison */
|
|
|
|
unescaped = x_glob_hlp_tilde_and_rem_qchar(unescaped, true);
|
2012-08-24 22:57:46 +02:00
|
|
|
|
2012-07-20 22:16:26 +02:00
|
|
|
/*
|
|
|
|
* match iff entire original string is part of the
|
|
|
|
* longest prefix, implying the latter is at least
|
|
|
|
* the same size (after unescaping)
|
|
|
|
*/
|
|
|
|
completed = !strncmp(words[0], unescaped, strlen(unescaped));
|
|
|
|
|
|
|
|
afree(unescaped, ATEMP);
|
|
|
|
}
|
|
|
|
if (type == CT_COMPLIST && nwords > 1) {
|
|
|
|
/*
|
|
|
|
* print expansions, since we didn't get back
|
|
|
|
* just a single match
|
|
|
|
*/
|
|
|
|
x_print_expansions(nwords, words,
|
|
|
|
tobool(flags & XCF_IS_COMMAND));
|
|
|
|
}
|
|
|
|
if (completed) {
|
|
|
|
/* expand on the command line */
|
|
|
|
xmp = NULL;
|
|
|
|
xcp = xbuf + start;
|
|
|
|
xep -= olen;
|
|
|
|
memmove(xcp, xcp + olen, xep - xcp + 1);
|
2012-08-24 22:57:46 +02:00
|
|
|
x_escape(words[0], nlen, x_do_ins);
|
2012-07-20 22:16:26 +02:00
|
|
|
}
|
|
|
|
x_adjust();
|
2011-06-30 15:48:13 +02:00
|
|
|
/*
|
|
|
|
* append a space if this is a single non-directory match
|
|
|
|
* and not a parameter or homedir substitution
|
|
|
|
*/
|
2016-11-12 00:31:39 +01:00
|
|
|
if (nwords == 1 && !mksh_cdirsep(words[0][nlen - 1]) &&
|
2012-08-24 22:05:13 +02:00
|
|
|
!(flags & XCF_IS_NOSPACE)) {
|
2016-07-25 02:04:48 +02:00
|
|
|
x_ins(T1space);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
x_free_words(nwords, words);
|
|
|
|
}
|
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*-
|
|
|
|
* NAME:
|
2008-10-26 21:59:40 +01:00
|
|
|
* x_adjust - redraw the line adjusting starting point etc.
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* DESCRIPTION:
|
2008-10-26 21:59:40 +01:00
|
|
|
* This function is called when we have exceeded the bounds
|
2009-06-10 20:12:51 +02:00
|
|
|
* of the edit window. It increments x_adj_done so that
|
2008-10-26 21:59:40 +01:00
|
|
|
* functions like x_ins and x_delete know that we have been
|
|
|
|
* called and can skip the x_bs() stuff which has already
|
|
|
|
* been done by x_redraw.
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* RETURN VALUE:
|
2008-10-26 21:59:40 +01:00
|
|
|
* None
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
x_adjust(void)
|
|
|
|
{
|
2013-07-26 22:33:39 +02:00
|
|
|
int col_left, n;
|
|
|
|
|
|
|
|
/* flag the fact that we were called */
|
2012-11-26 23:39:14 +01:00
|
|
|
x_adj_done++;
|
2013-07-26 22:33:39 +02:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/*
|
2013-07-26 22:33:39 +02:00
|
|
|
* calculate the amount of columns we need to "go back"
|
|
|
|
* from xcp to set xbp to (but never < xbuf) to 2/3 of
|
|
|
|
* the display width; take care of pwidth though
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2013-07-26 22:33:39 +02:00
|
|
|
if ((col_left = xx_cols * 2 / 3) < MIN_EDIT_SPACE) {
|
|
|
|
/*
|
|
|
|
* cowardly refuse to do anything
|
|
|
|
* if the available space is too small;
|
|
|
|
* fall back to dumb pdksh code
|
|
|
|
*/
|
|
|
|
if ((xbp = xcp - (x_displen / 2)) < xbuf)
|
|
|
|
xbp = xbuf;
|
|
|
|
/* elide UTF-8 fixup as penalty */
|
|
|
|
goto x_adjust_out;
|
|
|
|
}
|
|
|
|
|
2013-08-14 22:26:19 +02:00
|
|
|
/* fix up xbp to just past a character end first */
|
|
|
|
xbp = xcp >= xep ? xep : x_bs0(xcp, xbuf);
|
2013-07-26 22:33:39 +02:00
|
|
|
/* walk backwards */
|
|
|
|
while (xbp > xbuf && col_left > 0) {
|
|
|
|
xbp = x_bs0(xbp - 1, xbuf);
|
|
|
|
col_left -= (n = x_size2(xbp, NULL));
|
|
|
|
}
|
|
|
|
/* check if we hit the prompt */
|
2013-08-14 22:26:19 +02:00
|
|
|
if (xbp == xbuf && xcp != xbuf && col_left >= 0 && col_left < pwidth) {
|
2013-07-26 22:33:39 +02:00
|
|
|
/* so we did; force scrolling occurs */
|
|
|
|
xbp += utf_ptradj(xbp);
|
|
|
|
}
|
|
|
|
|
|
|
|
x_adjust_out:
|
2005-05-23 05:06:10 +02:00
|
|
|
xlp_valid = false;
|
2016-07-26 22:43:14 +02:00
|
|
|
x_redraw('\r');
|
2005-05-23 05:06:10 +02:00
|
|
|
x_flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
x_e_ungetc(int c)
|
|
|
|
{
|
2007-10-14 15:43:41 +02:00
|
|
|
unget_char = c < 0 ? -1 : (c & 255);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2007-10-09 16:50:50 +02:00
|
|
|
static int
|
2005-05-23 05:06:10 +02:00
|
|
|
x_e_getc(void)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (unget_char >= 0) {
|
|
|
|
c = unget_char;
|
|
|
|
unget_char = -1;
|
2009-04-05 14:28:55 +02:00
|
|
|
return (c);
|
|
|
|
}
|
|
|
|
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2009-04-05 14:28:55 +02:00
|
|
|
if (macroptr) {
|
|
|
|
if ((c = (unsigned char)*macroptr++))
|
|
|
|
return (c);
|
|
|
|
macroptr = NULL;
|
|
|
|
}
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-04-05 14:28:55 +02:00
|
|
|
return (x_getc());
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(int c)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-05 16:31:37 +01:00
|
|
|
int width = 1;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c == '\r' || c == '\n')
|
|
|
|
x_col = 0;
|
2006-11-05 20:56:20 +01:00
|
|
|
if (x_col < xx_cols) {
|
2008-12-04 19:11:08 +01:00
|
|
|
if (UTFMODE && (c > 0x7F)) {
|
2006-11-05 16:31:37 +01:00
|
|
|
char utf_tmp[3];
|
|
|
|
size_t x;
|
|
|
|
|
|
|
|
if (c < 0xA0)
|
|
|
|
c = 0xFFFD;
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
x = utf_wctomb(utf_tmp, c);
|
2006-11-05 16:31:37 +01:00
|
|
|
x_putc(utf_tmp[0]);
|
|
|
|
if (x > 1)
|
|
|
|
x_putc(utf_tmp[1]);
|
|
|
|
if (x > 2)
|
|
|
|
x_putc(utf_tmp[2]);
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
width = utf_wcwidth(c);
|
2006-11-05 16:31:37 +01:00
|
|
|
} else
|
|
|
|
x_putc(c);
|
2005-05-23 05:06:10 +02:00
|
|
|
switch (c) {
|
2005-08-21 15:02:17 +02:00
|
|
|
case 7:
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
x_col--;
|
|
|
|
break;
|
|
|
|
default:
|
2006-11-05 16:31:37 +01:00
|
|
|
x_col += width;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (x_adj_ok && (x_col < 0 || x_col >= (xx_cols - 2)))
|
|
|
|
x_adjust();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
x_e_putc3(const char **cp)
|
|
|
|
{
|
|
|
|
int width = 1, c = **(const unsigned char **)cp;
|
|
|
|
|
|
|
|
if (c == '\r' || c == '\n')
|
|
|
|
x_col = 0;
|
2006-11-05 20:56:20 +01:00
|
|
|
if (x_col < xx_cols) {
|
2008-12-04 19:11:08 +01:00
|
|
|
if (UTFMODE && (c > 0x7F)) {
|
2006-11-05 16:31:37 +01:00
|
|
|
char *cp2;
|
|
|
|
|
|
|
|
width = utf_widthadj(*cp, (const char **)&cp2);
|
2017-04-02 17:42:59 +02:00
|
|
|
if (cp2 == *cp + 1) {
|
|
|
|
(*cp)++;
|
|
|
|
shf_puts("\xEF\xBF\xBD", shl_out);
|
|
|
|
} else
|
|
|
|
while (*cp < cp2)
|
|
|
|
x_putcf(*(*cp)++);
|
2006-11-05 16:31:37 +01:00
|
|
|
} else {
|
|
|
|
(*cp)++;
|
|
|
|
x_putc(c);
|
|
|
|
}
|
|
|
|
switch (c) {
|
|
|
|
case 7:
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
x_col--;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
x_col += width;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (x_adj_ok && (x_col < 0 || x_col >= (xx_cols - 2)))
|
|
|
|
x_adjust();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
x_e_puts(const char *s)
|
|
|
|
{
|
2012-11-26 23:39:14 +01:00
|
|
|
int adj = x_adj_done;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
while (*s && adj == x_adj_done)
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc3(&s);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*-
|
|
|
|
* NAME:
|
2008-10-26 21:59:40 +01:00
|
|
|
* x_set_arg - set an arg value for next function
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* DESCRIPTION:
|
2008-10-26 21:59:40 +01:00
|
|
|
* This is a simple implementation of M-[0-9].
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* RETURN VALUE:
|
2008-10-26 21:59:40 +01:00
|
|
|
* KSTD
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
x_set_arg(int c)
|
|
|
|
{
|
2011-10-07 21:45:08 +02:00
|
|
|
unsigned int n = 0;
|
2011-02-09 14:08:18 +01:00
|
|
|
bool first = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/* strip command prefix */
|
|
|
|
c &= 255;
|
2011-02-09 14:08:18 +01:00
|
|
|
while (c >= 0 && ksh_isdigit(c)) {
|
2015-04-29 22:07:35 +02:00
|
|
|
n = n * 10 + ksh_numdig(c);
|
2011-10-07 21:45:08 +02:00
|
|
|
if (n > LINE)
|
|
|
|
/* upper bound for repeat */
|
|
|
|
goto x_set_arg_too_big;
|
2011-02-09 14:08:18 +01:00
|
|
|
c = x_e_getc();
|
|
|
|
first = false;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c < 0 || first) {
|
2011-10-07 21:45:08 +02:00
|
|
|
x_set_arg_too_big:
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2005-05-23 05:06:10 +02:00
|
|
|
x_arg = 1;
|
2011-02-09 14:08:18 +01:00
|
|
|
x_arg_defaulted = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
x_e_ungetc(c);
|
|
|
|
x_arg = n;
|
2011-02-09 14:08:18 +01:00
|
|
|
x_arg_defaulted = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Comment or uncomment the current line. */
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_comment(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-08-27 20:06:52 +02:00
|
|
|
ssize_t len = xep - xbuf;
|
2005-05-23 05:06:10 +02:00
|
|
|
int ret = x_do_comment(xbuf, xend - xbuf, &len);
|
|
|
|
|
|
|
|
if (ret < 0)
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2005-05-23 05:06:10 +02:00
|
|
|
else {
|
2008-09-20 16:10:23 +02:00
|
|
|
x_modified();
|
2005-05-23 05:06:10 +02:00
|
|
|
xep = xbuf + len;
|
|
|
|
*xep = '\0';
|
|
|
|
xcp = xbp = xbuf;
|
2016-07-26 22:43:14 +02:00
|
|
|
x_redraw('\r');
|
2005-05-23 05:06:10 +02:00
|
|
|
if (ret > 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (x_newline('\n'));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2006-08-01 14:44:17 +02:00
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_version(int c MKSH_A_UNUSED)
|
2006-08-01 14:44:17 +02:00
|
|
|
{
|
|
|
|
char *o_xbuf = xbuf, *o_xend = xend;
|
|
|
|
char *o_xbp = xbp, *o_xep = xep, *o_xcp = xcp;
|
2008-10-28 15:32:43 +01:00
|
|
|
char *v;
|
|
|
|
|
|
|
|
strdupx(v, KSH_VERSION, ATEMP);
|
2006-08-01 14:44:17 +02:00
|
|
|
|
|
|
|
xbuf = xbp = xcp = v;
|
2016-07-26 22:43:14 +02:00
|
|
|
xend = xep = v + strlen(v);
|
|
|
|
x_redraw('\r');
|
2006-08-01 14:44:17 +02:00
|
|
|
x_flush();
|
|
|
|
|
|
|
|
c = x_e_getc();
|
|
|
|
xbuf = o_xbuf;
|
|
|
|
xend = o_xend;
|
|
|
|
xbp = o_xbp;
|
|
|
|
xep = o_xep;
|
|
|
|
xcp = o_xcp;
|
2016-07-26 22:43:14 +02:00
|
|
|
x_redraw('\r');
|
2006-08-01 14:44:17 +02:00
|
|
|
|
|
|
|
if (c < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2009-06-11 14:42:21 +02:00
|
|
|
/* This is what AT&T ksh seems to do... Very bizarre */
|
2006-08-01 14:44:17 +02:00
|
|
|
if (c != ' ')
|
|
|
|
x_e_ungetc(c);
|
|
|
|
|
2006-11-10 07:40:05 +01:00
|
|
|
afree(v, ATEMP);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2006-08-01 14:44:17 +02:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-09-20 19:00:53 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2008-09-30 19:23:00 +02:00
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_edit_line(int c MKSH_A_UNUSED)
|
2008-09-30 19:23:00 +02:00
|
|
|
{
|
|
|
|
if (x_arg_defaulted) {
|
|
|
|
if (xep == xbuf) {
|
|
|
|
x_e_putc2(7);
|
|
|
|
return (KSTD);
|
|
|
|
}
|
|
|
|
if (modified) {
|
|
|
|
*xep = '\0';
|
2015-07-05 21:37:18 +02:00
|
|
|
histsave(&source->line, xbuf, HIST_STORE, true);
|
2008-09-30 19:23:00 +02:00
|
|
|
x_arg = 0;
|
|
|
|
} else
|
|
|
|
x_arg = source->line - (histptr - x_histp);
|
|
|
|
}
|
|
|
|
if (x_arg)
|
2016-07-25 02:04:48 +02:00
|
|
|
shf_snprintf(xbuf, xend - xbuf, Tf_sd,
|
2008-09-30 19:23:00 +02:00
|
|
|
"fc -e ${VISUAL:-${EDITOR:-vi}} --", x_arg);
|
|
|
|
else
|
|
|
|
strlcpy(xbuf, "fc -e ${VISUAL:-${EDITOR:-vi}} --", xend - xbuf);
|
|
|
|
xep = xbuf + strlen(xbuf);
|
|
|
|
return (x_newline('\n'));
|
|
|
|
}
|
2009-09-20 19:00:53 +02:00
|
|
|
#endif
|
2008-09-30 19:23:00 +02:00
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*-
|
|
|
|
* NAME:
|
2008-10-26 21:59:40 +01:00
|
|
|
* x_prev_histword - recover word from prev command
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* DESCRIPTION:
|
2008-10-26 21:59:40 +01:00
|
|
|
* This function recovers the last word from the previous
|
2009-06-10 20:12:51 +02:00
|
|
|
* command and inserts it into the current edit line. If a
|
2008-10-26 21:59:40 +01:00
|
|
|
* numeric arg is supplied then the n'th word from the
|
|
|
|
* start of the previous command is used.
|
|
|
|
* As a side effect, trashes the mark in order to achieve
|
|
|
|
* being called in a repeatable fashion.
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
2008-10-26 21:59:40 +01:00
|
|
|
* Bound to M-.
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* RETURN VALUE:
|
2008-10-26 21:59:40 +01:00
|
|
|
* KSTD
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_prev_histword(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2008-04-19 19:21:55 +02:00
|
|
|
char *rcp, *cp;
|
2008-10-05 01:08:04 +02:00
|
|
|
char **xhp;
|
2011-01-21 23:43:17 +01:00
|
|
|
int m = 1;
|
2011-02-09 14:08:18 +01:00
|
|
|
/* -1 = defaulted; 0+ = argument */
|
|
|
|
static int last_arg = -1;
|
2008-10-05 01:08:04 +02:00
|
|
|
|
2011-01-21 23:43:17 +01:00
|
|
|
if (x_last_command == XFUNC_prev_histword) {
|
|
|
|
if (xmp && modified > 1)
|
|
|
|
x_kill_region(0);
|
|
|
|
if (modified)
|
|
|
|
m = modified;
|
2011-02-09 14:08:18 +01:00
|
|
|
} else
|
|
|
|
last_arg = x_arg_defaulted ? -1 : x_arg;
|
2008-10-05 01:08:04 +02:00
|
|
|
xhp = histptr - (m - 1);
|
|
|
|
if ((xhp < history) || !(cp = *xhp)) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2008-10-05 01:08:04 +02:00
|
|
|
x_modified();
|
|
|
|
return (KSTD);
|
|
|
|
}
|
|
|
|
x_set_mark(0);
|
2011-02-09 14:08:18 +01:00
|
|
|
if ((x_arg = last_arg) == -1) {
|
|
|
|
/* x_arg_defaulted */
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
rcp = &cp[strlen(cp) - 1];
|
|
|
|
/*
|
|
|
|
* ignore white-space after the last word
|
|
|
|
*/
|
|
|
|
while (rcp > cp && is_cfs(*rcp))
|
|
|
|
rcp--;
|
|
|
|
while (rcp > cp && !is_cfs(*rcp))
|
|
|
|
rcp--;
|
|
|
|
if (is_cfs(*rcp))
|
|
|
|
rcp++;
|
|
|
|
x_ins(rcp);
|
|
|
|
} else {
|
2011-02-09 14:08:18 +01:00
|
|
|
/* not x_arg_defaulted */
|
2007-06-07 00:26:26 +02:00
|
|
|
char ch;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
rcp = cp;
|
|
|
|
/*
|
|
|
|
* ignore white-space at start of line
|
|
|
|
*/
|
|
|
|
while (*rcp && is_cfs(*rcp))
|
|
|
|
rcp++;
|
2011-02-09 14:08:18 +01:00
|
|
|
while (x_arg-- > 0) {
|
2005-05-23 05:06:10 +02:00
|
|
|
while (*rcp && !is_cfs(*rcp))
|
|
|
|
rcp++;
|
|
|
|
while (*rcp && is_cfs(*rcp))
|
|
|
|
rcp++;
|
|
|
|
}
|
|
|
|
cp = rcp;
|
|
|
|
while (*rcp && !is_cfs(*rcp))
|
|
|
|
rcp++;
|
|
|
|
ch = *rcp;
|
|
|
|
*rcp = '\0';
|
|
|
|
x_ins(cp);
|
|
|
|
*rcp = ch;
|
|
|
|
}
|
2008-10-05 01:08:04 +02:00
|
|
|
modified = m + 1;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2009-09-20 19:00:53 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Uppercase N(1) words */
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_fold_upper(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2009-06-08 22:06:50 +02:00
|
|
|
return (x_fold_case('U'));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Lowercase N(1) words */
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_fold_lower(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2009-06-08 22:06:50 +02:00
|
|
|
return (x_fold_case('L'));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2012-04-06 17:06:42 +02:00
|
|
|
/* Titlecase N(1) words */
|
2005-05-23 05:06:10 +02:00
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
x_fold_capitalise(int c MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2009-06-08 22:06:50 +02:00
|
|
|
return (x_fold_case('C'));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*-
|
|
|
|
* NAME:
|
2008-10-26 21:59:40 +01:00
|
|
|
* x_fold_case - convert word to UPPER/lower/Capital case
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* DESCRIPTION:
|
2012-04-06 17:06:42 +02:00
|
|
|
* This function is used to implement M-U/M-u, M-L/M-l, M-C/M-c
|
|
|
|
* to UPPER CASE, lower case or Capitalise Words.
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* RETURN VALUE:
|
2008-10-26 21:59:40 +01:00
|
|
|
* None
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
x_fold_case(int c)
|
|
|
|
{
|
|
|
|
char *cp = xcp;
|
|
|
|
|
|
|
|
if (cp == xep) {
|
2006-11-05 16:31:37 +01:00
|
|
|
x_e_putc2(7);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
while (x_arg--) {
|
|
|
|
/*
|
|
|
|
* first skip over any white-space
|
|
|
|
*/
|
|
|
|
while (cp != xep && is_mfs(*cp))
|
|
|
|
cp++;
|
|
|
|
/*
|
|
|
|
* do the first char on its own since it may be
|
|
|
|
* a different action than for the rest.
|
|
|
|
*/
|
|
|
|
if (cp != xep) {
|
2011-02-03 16:57:52 +01:00
|
|
|
if (c == 'L')
|
|
|
|
/* lowercase */
|
2006-11-10 08:18:58 +01:00
|
|
|
*cp = ksh_tolower(*cp);
|
2011-02-03 16:57:52 +01:00
|
|
|
else
|
|
|
|
/* uppercase, capitalise */
|
2006-11-10 08:18:58 +01:00
|
|
|
*cp = ksh_toupper(*cp);
|
2005-05-23 05:06:10 +02:00
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* now for the rest of the word
|
|
|
|
*/
|
|
|
|
while (cp != xep && !is_mfs(*cp)) {
|
2011-02-03 16:57:52 +01:00
|
|
|
if (c == 'U')
|
|
|
|
/* uppercase */
|
2006-11-10 08:18:58 +01:00
|
|
|
*cp = ksh_toupper(*cp);
|
2011-02-03 16:57:52 +01:00
|
|
|
else
|
|
|
|
/* lowercase, capitalise */
|
2006-11-10 08:18:58 +01:00
|
|
|
*cp = ksh_tolower(*cp);
|
2005-05-23 05:06:10 +02:00
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x_goto(cp);
|
2008-09-20 16:10:23 +02:00
|
|
|
x_modified();
|
2009-06-08 22:06:50 +02:00
|
|
|
return (KSTD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-09-20 19:00:53 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*-
|
|
|
|
* NAME:
|
2008-10-26 21:59:40 +01:00
|
|
|
* x_lastcp - last visible char
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* DESCRIPTION:
|
2009-06-10 20:12:51 +02:00
|
|
|
* This function returns a pointer to that char in the
|
2008-10-26 21:59:40 +01:00
|
|
|
* edit buffer that will be the last displayed on the
|
2016-07-26 23:37:26 +02:00
|
|
|
* screen.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
x_lastcp(void)
|
|
|
|
{
|
|
|
|
if (!xlp_valid) {
|
2006-11-05 16:31:37 +01:00
|
|
|
int i = 0, j;
|
|
|
|
char *xlp2;
|
|
|
|
|
|
|
|
xlp = xbp;
|
|
|
|
while (xlp < xep) {
|
|
|
|
j = x_size2(xlp, &xlp2);
|
2006-11-05 22:11:17 +01:00
|
|
|
if ((i + j) > x_displen)
|
2006-11-05 16:31:37 +01:00
|
|
|
break;
|
|
|
|
i += j;
|
|
|
|
xlp = xlp2;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
xlp_valid = true;
|
|
|
|
return (xlp);
|
|
|
|
}
|
|
|
|
|
2016-07-26 23:37:26 +02:00
|
|
|
/* correctly position the cursor on the screen from end of visible area */
|
|
|
|
static void
|
|
|
|
x_lastpos(void)
|
|
|
|
{
|
|
|
|
char *cp = x_lastcp();
|
|
|
|
|
|
|
|
while (cp > xcp)
|
|
|
|
x_bs3(&cp);
|
|
|
|
}
|
|
|
|
|
2011-05-29 04:18:57 +02:00
|
|
|
static void
|
2006-11-10 05:31:04 +01:00
|
|
|
x_mode(bool onoff)
|
|
|
|
{
|
|
|
|
static bool x_cur_mode;
|
|
|
|
|
|
|
|
if (x_cur_mode == onoff)
|
2011-05-29 04:18:57 +02:00
|
|
|
return;
|
2006-11-10 05:31:04 +01:00
|
|
|
x_cur_mode = onoff;
|
|
|
|
|
|
|
|
if (onoff) {
|
2011-05-29 04:18:57 +02:00
|
|
|
x_mkraw(tty_fd, NULL, false);
|
2006-11-10 05:31:04 +01:00
|
|
|
|
2016-07-28 23:39:04 +02:00
|
|
|
edchars.erase = toedchar(tty_state.c_cc[VERASE]);
|
|
|
|
edchars.kill = toedchar(tty_state.c_cc[VKILL]);
|
|
|
|
edchars.intr = toedchar(tty_state.c_cc[VINTR]);
|
|
|
|
edchars.quit = toedchar(tty_state.c_cc[VQUIT]);
|
|
|
|
edchars.eof = toedchar(tty_state.c_cc[VEOF]);
|
2006-11-10 05:31:04 +01:00
|
|
|
#ifdef VWERASE
|
2016-07-28 23:39:04 +02:00
|
|
|
edchars.werase = toedchar(tty_state.c_cc[VWERASE]);
|
2015-07-10 19:31:09 +02:00
|
|
|
#else
|
|
|
|
edchars.werase = 0;
|
2006-11-10 05:31:04 +01:00
|
|
|
#endif
|
|
|
|
|
2015-07-10 19:31:09 +02:00
|
|
|
if (!edchars.erase)
|
|
|
|
edchars.erase = CTRL('H');
|
|
|
|
if (!edchars.kill)
|
|
|
|
edchars.kill = CTRL('U');
|
|
|
|
if (!edchars.intr)
|
|
|
|
edchars.intr = CTRL('C');
|
|
|
|
if (!edchars.quit)
|
|
|
|
edchars.quit = CTRL('\\');
|
|
|
|
if (!edchars.eof)
|
|
|
|
edchars.eof = CTRL('D');
|
|
|
|
if (!edchars.werase)
|
|
|
|
edchars.werase = CTRL('W');
|
|
|
|
|
2016-07-28 23:39:04 +02:00
|
|
|
if (isedchar(edchars.erase)) {
|
2006-11-10 05:31:04 +01:00
|
|
|
bind_if_not_bound(0, edchars.erase, XFUNC_del_back);
|
|
|
|
bind_if_not_bound(1, edchars.erase, XFUNC_del_bword);
|
|
|
|
}
|
2016-07-28 23:39:04 +02:00
|
|
|
if (isedchar(edchars.kill))
|
2006-11-10 05:31:04 +01:00
|
|
|
bind_if_not_bound(0, edchars.kill, XFUNC_del_line);
|
2016-07-28 23:39:04 +02:00
|
|
|
if (isedchar(edchars.werase))
|
2006-11-10 05:31:04 +01:00
|
|
|
bind_if_not_bound(0, edchars.werase, XFUNC_del_bword);
|
2016-07-28 23:39:04 +02:00
|
|
|
if (isedchar(edchars.intr))
|
2006-11-10 05:31:04 +01:00
|
|
|
bind_if_not_bound(0, edchars.intr, XFUNC_abort);
|
2016-07-28 23:39:04 +02:00
|
|
|
if (isedchar(edchars.quit))
|
2006-11-10 05:31:04 +01:00
|
|
|
bind_if_not_bound(0, edchars.quit, XFUNC_noop);
|
|
|
|
} else
|
2012-05-05 00:18:27 +02:00
|
|
|
mksh_tcset(tty_fd, &tty_state);
|
2006-11-10 05:31:04 +01:00
|
|
|
}
|
|
|
|
|
2009-09-24 19:15:33 +02:00
|
|
|
#if !MKSH_S_NOVI
|
2005-05-23 05:06:10 +02:00
|
|
|
/* +++ vi editing mode +++ */
|
|
|
|
|
|
|
|
struct edstate {
|
2009-06-10 21:33:16 +02:00
|
|
|
char *cbuf;
|
2011-08-27 20:06:52 +02:00
|
|
|
ssize_t winleft;
|
|
|
|
ssize_t cbufsize;
|
|
|
|
ssize_t linelen;
|
|
|
|
ssize_t cursor;
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
2009-06-10 21:33:16 +02:00
|
|
|
static int vi_hook(int);
|
|
|
|
static int nextstate(int);
|
|
|
|
static int vi_insert(int);
|
|
|
|
static int vi_cmd(int, const char *);
|
|
|
|
static int domove(int, const char *, int);
|
|
|
|
static int redo_insert(int);
|
|
|
|
static void yank_range(int, int);
|
|
|
|
static int bracktype(int);
|
|
|
|
static void save_cbuf(void);
|
|
|
|
static void restore_cbuf(void);
|
2013-08-10 15:44:33 +02:00
|
|
|
static int putbuf(const char *, ssize_t, bool);
|
2009-06-10 21:33:16 +02:00
|
|
|
static void del_range(int, int);
|
2014-01-05 22:57:29 +01:00
|
|
|
static int findch(int, int, bool, bool) MKSH_A_PURE;
|
2009-06-10 21:33:16 +02:00
|
|
|
static int forwword(int);
|
|
|
|
static int backword(int);
|
|
|
|
static int endword(int);
|
|
|
|
static int Forwword(int);
|
|
|
|
static int Backword(int);
|
|
|
|
static int Endword(int);
|
|
|
|
static int grabhist(int, int);
|
2011-12-05 00:22:59 +01:00
|
|
|
static int grabsearch(int, int, int, const char *);
|
2011-07-16 20:15:45 +02:00
|
|
|
static void redraw_line(bool);
|
2009-06-10 21:33:16 +02:00
|
|
|
static void refresh(int);
|
|
|
|
static int outofwin(void);
|
|
|
|
static void rewindow(void);
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
static int newcol(unsigned char, int);
|
2009-06-10 21:33:16 +02:00
|
|
|
static void display(char *, char *, int);
|
|
|
|
static void ed_mov_opt(int, char *);
|
|
|
|
static int expand_word(int);
|
|
|
|
static int complete_word(int, int);
|
|
|
|
static int print_expansions(struct edstate *, int);
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
#define char_len(c) ((ISCTRL((unsigned char)c) && \
|
|
|
|
/* but not C1 */ (unsigned char)c < 0x80) ? 2 : 1)
|
2009-06-10 21:33:16 +02:00
|
|
|
static void x_vi_zotc(int);
|
|
|
|
static void vi_error(void);
|
|
|
|
static void vi_macro_reset(void);
|
|
|
|
static int x_vi_putbuf(const char *, size_t);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-09-07 17:24:22 +02:00
|
|
|
#define vC 0x01 /* a valid command that isn't a vM, vE, vU */
|
|
|
|
#define vM 0x02 /* movement command (h, l, etc.) */
|
|
|
|
#define vE 0x04 /* extended command (c, d, y) */
|
|
|
|
#define vX 0x08 /* long command (@, f, F, t, T, etc.) */
|
|
|
|
#define vU 0x10 /* an UN-undoable command (that isn't a vM) */
|
|
|
|
#define vB 0x20 /* bad command (^@) */
|
|
|
|
#define vZ 0x40 /* repeat count defaults to 0 (not 1) */
|
|
|
|
#define vS 0x80 /* search (/, ?) */
|
|
|
|
|
|
|
|
#define is_bad(c) (classify[(c)&0x7f]&vB)
|
|
|
|
#define is_cmd(c) (classify[(c)&0x7f]&(vM|vE|vC|vU))
|
|
|
|
#define is_move(c) (classify[(c)&0x7f]&vM)
|
|
|
|
#define is_extend(c) (classify[(c)&0x7f]&vE)
|
|
|
|
#define is_long(c) (classify[(c)&0x7f]&vX)
|
|
|
|
#define is_undoable(c) (!(classify[(c)&0x7f]&vU))
|
|
|
|
#define is_srch(c) (classify[(c)&0x7f]&vS)
|
|
|
|
#define is_zerocount(c) (classify[(c)&0x7f]&vZ)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-03-15 17:13:40 +01:00
|
|
|
static const unsigned char classify[128] = {
|
2009-06-10 20:12:51 +02:00
|
|
|
/* 0 1 2 3 4 5 6 7 */
|
|
|
|
/* 0 ^@ ^A ^B ^C ^D ^E ^F ^G */
|
2011-09-07 17:24:22 +02:00
|
|
|
vB, 0, 0, 0, 0, vC|vU, vC|vZ, 0,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* 1 ^H ^I ^J ^K ^L ^M ^N ^O */
|
2011-09-07 17:24:22 +02:00
|
|
|
vM, vC|vZ, 0, 0, vC|vU, 0, vC, 0,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* 2 ^P ^Q ^R ^S ^T ^U ^V ^W */
|
2011-09-07 17:24:22 +02:00
|
|
|
vC, 0, vC|vU, 0, 0, 0, vC, 0,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* 3 ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
|
2011-09-07 17:24:22 +02:00
|
|
|
vC, 0, 0, vC|vZ, 0, 0, 0, 0,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* 4 <space> ! " # $ % & ' */
|
2011-09-07 17:24:22 +02:00
|
|
|
vM, 0, 0, vC, vM, vM, 0, 0,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* 5 ( ) * + , - . / */
|
2011-09-07 17:24:22 +02:00
|
|
|
0, 0, vC, vC, vM, vC, 0, vC|vS,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* 6 0 1 2 3 4 5 6 7 */
|
2011-09-07 17:24:22 +02:00
|
|
|
vM, 0, 0, 0, 0, 0, 0, 0,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* 7 8 9 : ; < = > ? */
|
2011-09-07 17:24:22 +02:00
|
|
|
0, 0, 0, vM, 0, vC, 0, vC|vS,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* 8 @ A B C D E F G */
|
2011-09-07 17:24:22 +02:00
|
|
|
vC|vX, vC, vM, vC, vC, vM, vM|vX, vC|vU|vZ,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* 9 H I J K L M N O */
|
2013-05-02 17:33:30 +02:00
|
|
|
0, vC, 0, 0, 0, 0, vC|vU, vU,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* A P Q R S T U V W */
|
2011-09-07 17:24:22 +02:00
|
|
|
vC, 0, vC, vC, vM|vX, vC, 0, vM,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* B X Y Z [ \ ] ^ _ */
|
2013-05-02 17:33:30 +02:00
|
|
|
vC, vC|vU, 0, vU, vC|vZ, 0, vM, vC|vZ,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* C ` a b c d e f g */
|
2011-09-07 17:24:22 +02:00
|
|
|
0, vC, vM, vE, vE, vM, vM|vX, vC|vZ,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* D h i j k l m n o */
|
2011-09-07 17:24:22 +02:00
|
|
|
vM, vC, vC|vU, vC|vU, vM, 0, vC|vU, 0,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* E p q r s t u v w */
|
2011-09-07 17:24:22 +02:00
|
|
|
vC, 0, vX, vC, vM|vX, vC|vU, vC|vU|vZ, vM,
|
2009-06-10 20:12:51 +02:00
|
|
|
/* F x y z { | } ~ ^? */
|
2011-09-07 17:24:22 +02:00
|
|
|
vC, vE|vU, 0, 0, vM|vZ, 0, vC, 0
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#define MAXVICMD 3
|
|
|
|
#define SRCHLEN 40
|
|
|
|
|
|
|
|
#define INSERT 1
|
|
|
|
#define REPLACE 2
|
|
|
|
|
|
|
|
#define VNORMAL 0 /* command, insert or replace mode */
|
|
|
|
#define VARG1 1 /* digit prefix (first, eg, 5l) */
|
|
|
|
#define VEXTCMD 2 /* cmd + movement (eg, cl) */
|
|
|
|
#define VARG2 3 /* digit prefix (second, eg, 2c3l) */
|
|
|
|
#define VXCH 4 /* f, F, t, T, @ */
|
|
|
|
#define VFAIL 5 /* bad command */
|
|
|
|
#define VCMD 6 /* single char command (eg, X) */
|
|
|
|
#define VREDO 7 /* . */
|
|
|
|
#define VLIT 8 /* ^V */
|
|
|
|
#define VSEARCH 9 /* /, ? */
|
2006-08-01 14:44:17 +02:00
|
|
|
#define VVERSION 10 /* <ESC> ^V */
|
2013-05-02 17:33:30 +02:00
|
|
|
#define VPREFIX2 11 /* ^[[ and ^[O in insert mode */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
static struct edstate *save_edstate(struct edstate *old);
|
2010-07-18 00:09:40 +02:00
|
|
|
static void restore_edstate(struct edstate *old, struct edstate *news);
|
2005-05-23 05:06:10 +02:00
|
|
|
static void free_edstate(struct edstate *old);
|
|
|
|
|
|
|
|
static struct edstate ebuf;
|
2013-08-10 15:44:33 +02:00
|
|
|
static struct edstate undobuf;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2013-08-10 15:44:33 +02:00
|
|
|
static struct edstate *es; /* current editor state */
|
2005-05-23 05:06:10 +02:00
|
|
|
static struct edstate *undo;
|
|
|
|
|
2013-08-10 15:44:33 +02:00
|
|
|
static char *ibuf; /* input buffer */
|
|
|
|
static bool first_insert; /* set when starting in insert mode */
|
2009-06-10 21:33:16 +02:00
|
|
|
static int saved_inslen; /* saved inslen for first insert */
|
|
|
|
static int inslen; /* length of input buffer */
|
|
|
|
static int srchlen; /* length of current search pattern */
|
2013-08-10 15:44:33 +02:00
|
|
|
static char *ybuf; /* yank buffer */
|
2009-06-10 21:33:16 +02:00
|
|
|
static int yanklen; /* length of yank buffer */
|
|
|
|
static int fsavecmd = ' '; /* last find command */
|
|
|
|
static int fsavech; /* character to find */
|
|
|
|
static char lastcmd[MAXVICMD]; /* last non-move command */
|
|
|
|
static int lastac; /* argcnt for lastcmd */
|
|
|
|
static int lastsearch = ' '; /* last search command */
|
|
|
|
static char srchpat[SRCHLEN]; /* last search pattern */
|
2013-08-10 15:44:33 +02:00
|
|
|
static int insert; /* <>0 in insert mode */
|
2009-06-10 21:33:16 +02:00
|
|
|
static int hnum; /* position in history */
|
|
|
|
static int ohnum; /* history line copied (after mod) */
|
|
|
|
static int hlast; /* 1 past last position in history */
|
|
|
|
static int state;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* Information for keeping track of macros that are being expanded.
|
2007-07-22 15:38:26 +02:00
|
|
|
* The format of buf is the alias contents followed by a NUL byte followed
|
2009-06-10 20:12:51 +02:00
|
|
|
* by the name (letter) of the alias. The end of the buffer is marked by
|
|
|
|
* a double NUL. The name of the alias is stored so recursive macros can
|
2005-05-23 05:06:10 +02:00
|
|
|
* be detected.
|
|
|
|
*/
|
|
|
|
struct macro_state {
|
2005-06-09 00:22:24 +02:00
|
|
|
unsigned char *p; /* current position in buf */
|
|
|
|
unsigned char *buf; /* pointer to macro(s) being expanded */
|
2011-08-27 20:06:52 +02:00
|
|
|
size_t len; /* how much data in buffer */
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
static struct macro_state macro;
|
|
|
|
|
2011-04-22 14:16:38 +02:00
|
|
|
/* last input was expanded */
|
|
|
|
static enum expand_mode {
|
|
|
|
NONE = 0, EXPAND, COMPLETE, PRINT
|
|
|
|
} expanded;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-06-07 01:28:17 +02:00
|
|
|
static int
|
2013-08-10 15:44:33 +02:00
|
|
|
x_vi(char *buf)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
int c;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2008-09-14 22:24:59 +02:00
|
|
|
state = VNORMAL;
|
|
|
|
ohnum = hnum = hlast = histnum(-1) + 1;
|
|
|
|
insert = INSERT;
|
|
|
|
saved_inslen = inslen;
|
2013-08-10 15:44:33 +02:00
|
|
|
first_insert = true;
|
2008-09-14 22:24:59 +02:00
|
|
|
inslen = 0;
|
|
|
|
vi_macro_reset();
|
|
|
|
|
2013-08-10 15:44:33 +02:00
|
|
|
ebuf.cbuf = buf;
|
|
|
|
if (undobuf.cbuf == NULL) {
|
|
|
|
ibuf = alloc(LINE, AEDIT);
|
|
|
|
ybuf = alloc(LINE, AEDIT);
|
|
|
|
undobuf.cbuf = alloc(LINE, AEDIT);
|
|
|
|
}
|
|
|
|
undobuf.cbufsize = ebuf.cbufsize = LINE;
|
|
|
|
undobuf.linelen = ebuf.linelen = 0;
|
|
|
|
undobuf.cursor = ebuf.cursor = 0;
|
|
|
|
undobuf.winleft = ebuf.winleft = 0;
|
2008-09-14 22:24:59 +02:00
|
|
|
es = &ebuf;
|
|
|
|
undo = &undobuf;
|
|
|
|
|
2013-08-16 12:59:03 +02:00
|
|
|
x_init_prompt(true);
|
2013-07-26 22:33:39 +02:00
|
|
|
x_col = pwidth;
|
2008-09-14 22:24:59 +02:00
|
|
|
|
2013-08-10 15:44:33 +02:00
|
|
|
if (wbuf_len != x_cols - 3 && ((wbuf_len = x_cols - 3))) {
|
|
|
|
wbuf[0] = aresize(wbuf[0], wbuf_len, AEDIT);
|
|
|
|
wbuf[1] = aresize(wbuf[1], wbuf_len, AEDIT);
|
2008-09-14 22:24:59 +02:00
|
|
|
}
|
2011-07-16 20:15:45 +02:00
|
|
|
if (wbuf_len) {
|
|
|
|
memset(wbuf[0], ' ', wbuf_len);
|
|
|
|
memset(wbuf[1], ' ', wbuf_len);
|
|
|
|
}
|
2008-09-14 22:24:59 +02:00
|
|
|
winwidth = x_cols - pwidth - 3;
|
|
|
|
win = 0;
|
|
|
|
morec = ' ';
|
|
|
|
lastref = 1;
|
|
|
|
holdlen = 0;
|
|
|
|
|
2010-07-04 19:45:17 +02:00
|
|
|
editmode = 2;
|
2005-05-23 05:06:10 +02:00
|
|
|
x_flush();
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (macro.p) {
|
2014-01-05 20:11:46 +01:00
|
|
|
c = (unsigned char)*macro.p++;
|
2005-05-23 05:06:10 +02:00
|
|
|
/* end of current macro? */
|
|
|
|
if (!c) {
|
|
|
|
/* more macros left to finish? */
|
|
|
|
if (*macro.p++)
|
|
|
|
continue;
|
|
|
|
/* must be the end of all the macros */
|
|
|
|
vi_macro_reset();
|
|
|
|
c = x_getc();
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
c = x_getc();
|
|
|
|
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
if (state != VLIT) {
|
2016-07-28 23:39:04 +02:00
|
|
|
if (isched(c, edchars.intr) ||
|
|
|
|
isched(c, edchars.quit)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
/* pretend we got an interrupt */
|
|
|
|
x_vi_zotc(c);
|
|
|
|
x_flush();
|
2016-07-28 23:39:04 +02:00
|
|
|
trapsig(isched(c, edchars.intr) ?
|
|
|
|
SIGINT : SIGQUIT);
|
2005-05-23 05:06:10 +02:00
|
|
|
x_mode(false);
|
|
|
|
unwind(LSHELL);
|
2016-07-28 23:39:04 +02:00
|
|
|
} else if (isched(c, edchars.eof) &&
|
|
|
|
state != VVERSION) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (es->linelen == 0) {
|
2016-07-28 23:39:04 +02:00
|
|
|
x_vi_zotc(c);
|
2005-05-23 05:06:10 +02:00
|
|
|
c = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (vi_hook(c))
|
|
|
|
break;
|
|
|
|
x_flush();
|
|
|
|
}
|
|
|
|
|
2005-06-09 00:22:24 +02:00
|
|
|
x_putc('\r');
|
|
|
|
x_putc('\n');
|
|
|
|
x_flush();
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2013-08-10 15:44:33 +02:00
|
|
|
if (c == -1 || (ssize_t)LINE <= es->linelen)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (es->cbuf != buf)
|
2013-08-10 15:44:33 +02:00
|
|
|
memcpy(buf, es->cbuf, es->linelen);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
buf[es->linelen++] = '\n';
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (es->linelen);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vi_hook(int ch)
|
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
static char curcmd[MAXVICMD], locpat[SRCHLEN];
|
|
|
|
static int cmdlen, argc1, argc2;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
|
|
|
|
case VNORMAL:
|
2015-07-10 20:41:07 +02:00
|
|
|
/* PC scancodes */
|
|
|
|
if (!ch) switch (cmdlen = 0, (ch = x_getc())) {
|
|
|
|
case 71: ch = '0'; goto pseudo_vi_command;
|
|
|
|
case 72: ch = 'k'; goto pseudo_vi_command;
|
|
|
|
case 73: ch = 'A'; goto vi_xfunc_search_up;
|
|
|
|
case 75: ch = 'h'; goto pseudo_vi_command;
|
|
|
|
case 77: ch = 'l'; goto pseudo_vi_command;
|
|
|
|
case 79: ch = '$'; goto pseudo_vi_command;
|
|
|
|
case 80: ch = 'j'; goto pseudo_vi_command;
|
|
|
|
case 83: ch = 'x'; goto pseudo_vi_command;
|
|
|
|
default: ch = 0; goto vi_insert_failed;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
if (insert != 0) {
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
if (ch == CTRL('v')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
state = VLIT;
|
|
|
|
ch = '^';
|
|
|
|
}
|
|
|
|
switch (vi_insert(ch)) {
|
|
|
|
case -1:
|
2015-07-10 20:41:07 +02:00
|
|
|
vi_insert_failed:
|
2005-05-23 05:06:10 +02:00
|
|
|
vi_error();
|
|
|
|
state = VNORMAL;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
if (state == VLIT) {
|
|
|
|
es->cursor--;
|
|
|
|
refresh(0);
|
|
|
|
} else
|
|
|
|
refresh(insert != 0);
|
|
|
|
break;
|
|
|
|
case 1:
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ch == '\r' || ch == '\n')
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
cmdlen = 0;
|
|
|
|
argc1 = 0;
|
2015-04-29 22:07:35 +02:00
|
|
|
if (ch >= ord('1') && ch <= ord('9')) {
|
|
|
|
argc1 = ksh_numdig(ch);
|
2005-05-23 05:06:10 +02:00
|
|
|
state = VARG1;
|
|
|
|
} else {
|
2015-07-10 20:41:07 +02:00
|
|
|
pseudo_vi_command:
|
2005-05-23 05:06:10 +02:00
|
|
|
curcmd[cmdlen++] = ch;
|
|
|
|
state = nextstate(ch);
|
|
|
|
if (state == VSEARCH) {
|
|
|
|
save_cbuf();
|
|
|
|
es->cursor = 0;
|
|
|
|
es->linelen = 0;
|
2013-08-10 15:44:33 +02:00
|
|
|
if (putbuf(ch == '/' ? "/" : "?", 1,
|
|
|
|
false) != 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
refresh(0);
|
|
|
|
}
|
2006-08-01 14:44:17 +02:00
|
|
|
if (state == VVERSION) {
|
|
|
|
save_cbuf();
|
|
|
|
es->cursor = 0;
|
|
|
|
es->linelen = 0;
|
2006-08-23 00:49:38 +02:00
|
|
|
putbuf(KSH_VERSION,
|
2013-08-10 15:44:33 +02:00
|
|
|
strlen(KSH_VERSION), false);
|
2006-08-01 14:44:17 +02:00
|
|
|
refresh(0);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VLIT:
|
|
|
|
if (is_bad(ch)) {
|
|
|
|
del_range(es->cursor, es->cursor + 1);
|
|
|
|
vi_error();
|
|
|
|
} else
|
|
|
|
es->cbuf[es->cursor++] = ch;
|
|
|
|
refresh(1);
|
|
|
|
state = VNORMAL;
|
|
|
|
break;
|
|
|
|
|
2006-08-01 14:44:17 +02:00
|
|
|
case VVERSION:
|
|
|
|
restore_cbuf();
|
|
|
|
state = VNORMAL;
|
|
|
|
refresh(0);
|
|
|
|
break;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
case VARG1:
|
2006-11-10 07:53:27 +01:00
|
|
|
if (ksh_isdigit(ch))
|
2015-04-29 22:07:35 +02:00
|
|
|
argc1 = argc1 * 10 + ksh_numdig(ch);
|
2005-05-23 05:06:10 +02:00
|
|
|
else {
|
|
|
|
curcmd[cmdlen++] = ch;
|
|
|
|
state = nextstate(ch);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VEXTCMD:
|
|
|
|
argc2 = 0;
|
2015-04-29 22:07:35 +02:00
|
|
|
if (ch >= ord('1') && ch <= ord('9')) {
|
|
|
|
argc2 = ksh_numdig(ch);
|
2005-05-23 05:06:10 +02:00
|
|
|
state = VARG2;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
curcmd[cmdlen++] = ch;
|
|
|
|
if (ch == curcmd[0])
|
|
|
|
state = VCMD;
|
|
|
|
else if (is_move(ch))
|
|
|
|
state = nextstate(ch);
|
|
|
|
else
|
|
|
|
state = VFAIL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VARG2:
|
2006-11-10 07:53:27 +01:00
|
|
|
if (ksh_isdigit(ch))
|
2015-04-29 22:07:35 +02:00
|
|
|
argc2 = argc2 * 10 + ksh_numdig(ch);
|
2005-05-23 05:06:10 +02:00
|
|
|
else {
|
|
|
|
if (argc1 == 0)
|
|
|
|
argc1 = argc2;
|
|
|
|
else
|
|
|
|
argc1 *= argc2;
|
|
|
|
curcmd[cmdlen++] = ch;
|
|
|
|
if (ch == curcmd[0])
|
|
|
|
state = VCMD;
|
|
|
|
else if (is_move(ch))
|
|
|
|
state = nextstate(ch);
|
|
|
|
else
|
|
|
|
state = VFAIL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VXCH:
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
if (ch == CTRL('['))
|
2005-05-23 05:06:10 +02:00
|
|
|
state = VNORMAL;
|
|
|
|
else {
|
|
|
|
curcmd[cmdlen++] = ch;
|
|
|
|
state = VCMD;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VSEARCH:
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
if (ch == '\r' || ch == '\n' /*|| ch == CTRL('[')*/ ) {
|
2005-05-23 05:06:10 +02:00
|
|
|
restore_cbuf();
|
|
|
|
/* Repeat last search? */
|
|
|
|
if (srchlen == 0) {
|
|
|
|
if (!srchpat[0]) {
|
|
|
|
vi_error();
|
|
|
|
state = VNORMAL;
|
|
|
|
refresh(0);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
locpat[srchlen] = '\0';
|
2009-08-01 22:32:45 +02:00
|
|
|
memcpy(srchpat, locpat, srchlen + 1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
state = VCMD;
|
2016-07-28 23:39:04 +02:00
|
|
|
} else if (isched(ch, edchars.erase) || ch == CTRL('h')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (srchlen != 0) {
|
|
|
|
srchlen--;
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
es->linelen -= char_len(locpat[srchlen]);
|
2005-05-23 05:06:10 +02:00
|
|
|
es->cursor = es->linelen;
|
|
|
|
refresh(0);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
restore_cbuf();
|
|
|
|
state = VNORMAL;
|
|
|
|
refresh(0);
|
2016-07-28 23:39:04 +02:00
|
|
|
} else if (isched(ch, edchars.kill)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
srchlen = 0;
|
|
|
|
es->linelen = 1;
|
|
|
|
es->cursor = 1;
|
|
|
|
refresh(0);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2016-07-28 23:39:04 +02:00
|
|
|
} else if (isched(ch, edchars.werase)) {
|
2014-01-05 22:57:29 +01:00
|
|
|
unsigned int i, n;
|
2009-07-05 15:56:48 +02:00
|
|
|
struct edstate new_es, *save_es;
|
|
|
|
|
2014-01-05 22:57:29 +01:00
|
|
|
new_es.cursor = srchlen;
|
2009-07-05 15:56:48 +02:00
|
|
|
new_es.cbuf = locpat;
|
|
|
|
|
|
|
|
save_es = es;
|
|
|
|
es = &new_es;
|
|
|
|
n = backword(1);
|
|
|
|
es = save_es;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2014-01-05 22:57:29 +01:00
|
|
|
i = (unsigned)srchlen;
|
|
|
|
while (--i >= n)
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
es->linelen -= char_len(locpat[i]);
|
2014-01-05 22:57:29 +01:00
|
|
|
srchlen = (int)n;
|
2005-05-23 05:06:10 +02:00
|
|
|
es->cursor = es->linelen;
|
|
|
|
refresh(0);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
if (srchlen == SRCHLEN - 1)
|
|
|
|
vi_error();
|
|
|
|
else {
|
|
|
|
locpat[srchlen++] = ch;
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
if (ISCTRL(ch) && /* but not C1 */ ch < 0x80) {
|
2012-10-03 19:24:23 +02:00
|
|
|
if ((size_t)es->linelen + 2 >
|
|
|
|
(size_t)es->cbufsize)
|
2005-05-23 05:06:10 +02:00
|
|
|
vi_error();
|
|
|
|
es->cbuf[es->linelen++] = '^';
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
es->cbuf[es->linelen++] = UNCTRL(ch);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
if (es->linelen >= es->cbufsize)
|
|
|
|
vi_error();
|
|
|
|
es->cbuf[es->linelen++] = ch;
|
|
|
|
}
|
|
|
|
es->cursor = es->linelen;
|
|
|
|
refresh(0);
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
break;
|
2013-05-02 17:33:30 +02:00
|
|
|
|
|
|
|
case VPREFIX2:
|
2015-07-10 20:41:07 +02:00
|
|
|
vi_xfunc_search_up:
|
2013-05-02 17:33:30 +02:00
|
|
|
state = VFAIL;
|
|
|
|
switch (ch) {
|
|
|
|
case 'A':
|
|
|
|
/* the cursor may not be at the BOL */
|
|
|
|
if (!es->cursor)
|
|
|
|
break;
|
|
|
|
/* nor further in the line than we can search for */
|
|
|
|
if ((size_t)es->cursor >= sizeof(srchpat) - 1)
|
|
|
|
es->cursor = sizeof(srchpat) - 2;
|
|
|
|
/* anchor the search pattern */
|
|
|
|
srchpat[0] = '^';
|
|
|
|
/* take the current line up to the cursor */
|
|
|
|
memmove(srchpat + 1, es->cbuf, es->cursor);
|
|
|
|
srchpat[es->cursor + 1] = '\0';
|
|
|
|
/* set a magic flag */
|
|
|
|
argc1 = 2 + (int)es->cursor;
|
|
|
|
/* and emulate a backwards history search */
|
|
|
|
lastsearch = '/';
|
|
|
|
*curcmd = 'n';
|
|
|
|
goto pseudo_VCMD;
|
|
|
|
}
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case VCMD:
|
2013-05-02 17:33:30 +02:00
|
|
|
pseudo_VCMD:
|
2005-05-23 05:06:10 +02:00
|
|
|
state = VNORMAL;
|
|
|
|
switch (vi_cmd(argc1, curcmd)) {
|
|
|
|
case -1:
|
|
|
|
vi_error();
|
|
|
|
refresh(0);
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
if (insert != 0)
|
|
|
|
inslen = 0;
|
|
|
|
refresh(insert != 0);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
refresh(0);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
case 2:
|
|
|
|
/* back from a 'v' command - don't redraw the screen */
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VREDO:
|
|
|
|
state = VNORMAL;
|
|
|
|
if (argc1 != 0)
|
|
|
|
lastac = argc1;
|
|
|
|
switch (vi_cmd(lastac, lastcmd)) {
|
|
|
|
case -1:
|
|
|
|
vi_error();
|
|
|
|
refresh(0);
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
if (insert != 0) {
|
|
|
|
if (lastcmd[0] == 's' || lastcmd[0] == 'c' ||
|
|
|
|
lastcmd[0] == 'C') {
|
|
|
|
if (redo_insert(1) != 0)
|
|
|
|
vi_error();
|
|
|
|
} else {
|
|
|
|
if (redo_insert(lastac) != 0)
|
|
|
|
vi_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
refresh(0);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
refresh(0);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
case 2:
|
|
|
|
/* back from a 'v' command - can't happen */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VFAIL:
|
|
|
|
state = VNORMAL;
|
|
|
|
vi_error();
|
|
|
|
break;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nextstate(int ch)
|
|
|
|
{
|
|
|
|
if (is_extend(ch))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (VEXTCMD);
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (is_srch(ch))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (VSEARCH);
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (is_long(ch))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (VXCH);
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (ch == '.')
|
2009-06-08 22:06:50 +02:00
|
|
|
return (VREDO);
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
else if (ch == CTRL('v'))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (VVERSION);
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (is_cmd(ch))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (VCMD);
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
2009-06-08 22:06:50 +02:00
|
|
|
return (VFAIL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vi_insert(int ch)
|
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
int tcursor;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2016-07-28 23:39:04 +02:00
|
|
|
if (isched(ch, edchars.erase) || ch == CTRL('h')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (insert == REPLACE) {
|
|
|
|
if (es->cursor == undo->cursor) {
|
|
|
|
vi_error();
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (inslen > 0)
|
|
|
|
inslen--;
|
|
|
|
es->cursor--;
|
|
|
|
if (es->cursor >= undo->linelen)
|
|
|
|
es->linelen--;
|
|
|
|
else
|
|
|
|
es->cbuf[es->cursor] = undo->cbuf[es->cursor];
|
|
|
|
} else {
|
2005-08-21 15:02:17 +02:00
|
|
|
if (es->cursor == 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (inslen > 0)
|
|
|
|
inslen--;
|
|
|
|
es->cursor--;
|
|
|
|
es->linelen--;
|
2005-06-09 00:22:24 +02:00
|
|
|
memmove(&es->cbuf[es->cursor], &es->cbuf[es->cursor + 1],
|
2005-05-23 05:06:10 +02:00
|
|
|
es->linelen - es->cursor + 1);
|
|
|
|
}
|
|
|
|
expanded = NONE;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2016-07-28 23:39:04 +02:00
|
|
|
if (isched(ch, edchars.kill)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (es->cursor != 0) {
|
|
|
|
inslen = 0;
|
|
|
|
memmove(es->cbuf, &es->cbuf[es->cursor],
|
|
|
|
es->linelen - es->cursor);
|
|
|
|
es->linelen -= es->cursor;
|
|
|
|
es->cursor = 0;
|
|
|
|
}
|
|
|
|
expanded = NONE;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2016-07-28 23:39:04 +02:00
|
|
|
if (isched(ch, edchars.werase)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (es->cursor != 0) {
|
2009-06-10 21:33:16 +02:00
|
|
|
tcursor = backword(1);
|
2005-05-23 05:06:10 +02:00
|
|
|
memmove(&es->cbuf[tcursor], &es->cbuf[es->cursor],
|
|
|
|
es->linelen - es->cursor);
|
|
|
|
es->linelen -= es->cursor - tcursor;
|
|
|
|
if (inslen < es->cursor - tcursor)
|
|
|
|
inslen = 0;
|
|
|
|
else
|
|
|
|
inslen -= es->cursor - tcursor;
|
|
|
|
es->cursor = tcursor;
|
|
|
|
}
|
|
|
|
expanded = NONE;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* If any chars are entered before escape, trash the saved insert
|
2005-05-23 05:06:10 +02:00
|
|
|
* buffer (if user inserts & deletes char, ibuf gets trashed and
|
|
|
|
* we don't want to use it)
|
|
|
|
*/
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
if (first_insert && ch != CTRL('['))
|
2005-05-23 05:06:10 +02:00
|
|
|
saved_inslen = 0;
|
|
|
|
switch (ch) {
|
|
|
|
case '\0':
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
case CTRL('['):
|
2005-05-23 05:06:10 +02:00
|
|
|
expanded = NONE;
|
|
|
|
if (first_insert) {
|
2013-08-10 15:44:33 +02:00
|
|
|
first_insert = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (inslen == 0) {
|
|
|
|
inslen = saved_inslen;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (redo_insert(0));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
lastcmd[0] = 'a';
|
|
|
|
lastac = 1;
|
|
|
|
}
|
|
|
|
if (lastcmd[0] == 's' || lastcmd[0] == 'c' ||
|
|
|
|
lastcmd[0] == 'C')
|
2009-06-08 22:06:50 +02:00
|
|
|
return (redo_insert(0));
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
2009-06-08 22:06:50 +02:00
|
|
|
return (redo_insert(lastac - 1));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2016-09-01 14:59:12 +02:00
|
|
|
/* { start nonstandard vi commands */
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
case CTRL('x'):
|
2005-05-23 05:06:10 +02:00
|
|
|
expand_word(0);
|
|
|
|
break;
|
|
|
|
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
case CTRL('f'):
|
2005-05-23 05:06:10 +02:00
|
|
|
complete_word(0, 0);
|
|
|
|
break;
|
|
|
|
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
case CTRL('e'):
|
2005-05-23 05:06:10 +02:00
|
|
|
print_expansions(es, 0);
|
|
|
|
break;
|
|
|
|
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
case CTRL('i'):
|
2005-05-23 05:06:10 +02:00
|
|
|
if (Flag(FVITABCOMPLETE)) {
|
|
|
|
complete_word(0, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2016-09-01 14:59:12 +02:00
|
|
|
/* end nonstandard vi commands } */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
if (es->linelen >= es->cbufsize - 1)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
ibuf[inslen++] = ch;
|
|
|
|
if (insert == INSERT) {
|
2005-06-09 00:22:24 +02:00
|
|
|
memmove(&es->cbuf[es->cursor + 1], &es->cbuf[es->cursor],
|
2005-05-23 05:06:10 +02:00
|
|
|
es->linelen - es->cursor);
|
|
|
|
es->linelen++;
|
|
|
|
}
|
|
|
|
es->cbuf[es->cursor++] = ch;
|
|
|
|
if (insert == REPLACE && es->cursor > es->linelen)
|
|
|
|
es->linelen++;
|
|
|
|
expanded = NONE;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vi_cmd(int argcnt, const char *cmd)
|
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
int ncursor;
|
|
|
|
int cur, c1, c2, c3 = 0;
|
|
|
|
int any;
|
|
|
|
struct edstate *t;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (argcnt == 0 && !is_zerocount(*cmd))
|
|
|
|
argcnt = 1;
|
|
|
|
|
|
|
|
if (is_move(*cmd)) {
|
|
|
|
if ((cur = domove(argcnt, cmd, 0)) >= 0) {
|
|
|
|
if (cur == es->linelen && cur != 0)
|
|
|
|
cur--;
|
|
|
|
es->cursor = cur;
|
|
|
|
} else
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
/* Don't save state in middle of macro.. */
|
|
|
|
if (is_undoable(*cmd) && !macro.p) {
|
|
|
|
undo->winleft = es->winleft;
|
|
|
|
memmove(undo->cbuf, es->cbuf, es->linelen);
|
|
|
|
undo->linelen = es->linelen;
|
|
|
|
undo->cursor = es->cursor;
|
|
|
|
lastac = argcnt;
|
|
|
|
memmove(lastcmd, cmd, MAXVICMD);
|
|
|
|
}
|
|
|
|
switch (*cmd) {
|
|
|
|
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
case CTRL('l'):
|
|
|
|
case CTRL('r'):
|
2011-07-16 20:15:45 +02:00
|
|
|
redraw_line(true);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '@':
|
|
|
|
{
|
|
|
|
static char alias[] = "_\0";
|
|
|
|
struct tbl *ap;
|
2010-09-14 23:26:19 +02:00
|
|
|
size_t olen, nlen;
|
2005-06-09 00:22:24 +02:00
|
|
|
char *p, *nbuf;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* lookup letter in alias list... */
|
|
|
|
alias[1] = cmd[1];
|
2009-08-28 22:30:59 +02:00
|
|
|
ap = ktsearch(&aliases, alias, hash(alias));
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!cmd[1] || !ap || !(ap->flag & ISSET))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
/* check if this is a recursive call... */
|
2005-06-09 00:22:24 +02:00
|
|
|
if ((p = (char *)macro.p))
|
2009-12-05 23:19:42 +01:00
|
|
|
while ((p = strnul(p)) && p[1])
|
2005-05-23 05:06:10 +02:00
|
|
|
if (*++p == cmd[1])
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
/* insert alias into macro buffer */
|
|
|
|
nlen = strlen(ap->val.s) + 1;
|
|
|
|
olen = !macro.p ? 2 :
|
|
|
|
macro.len - (macro.p - macro.buf);
|
2010-09-14 23:26:19 +02:00
|
|
|
/*
|
|
|
|
* at this point, it's fairly reasonable that
|
|
|
|
* nlen + olen + 2 doesn't overflow
|
|
|
|
*/
|
2013-08-10 15:44:33 +02:00
|
|
|
nbuf = alloc(nlen + 1 + olen, AEDIT);
|
2005-05-23 05:06:10 +02:00
|
|
|
memcpy(nbuf, ap->val.s, nlen);
|
|
|
|
nbuf[nlen++] = cmd[1];
|
|
|
|
if (macro.p) {
|
|
|
|
memcpy(nbuf + nlen, macro.p, olen);
|
2013-08-10 15:44:33 +02:00
|
|
|
afree(macro.buf, AEDIT);
|
2005-05-23 05:06:10 +02:00
|
|
|
nlen += olen;
|
|
|
|
} else {
|
|
|
|
nbuf[nlen++] = '\0';
|
|
|
|
nbuf[nlen++] = '\0';
|
|
|
|
}
|
2005-06-09 00:22:24 +02:00
|
|
|
macro.p = macro.buf = (unsigned char *)nbuf;
|
2005-05-23 05:06:10 +02:00
|
|
|
macro.len = nlen;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a':
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (es->linelen != 0)
|
|
|
|
es->cursor++;
|
|
|
|
insert = INSERT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'A':
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
del_range(0, 0);
|
|
|
|
es->cursor = es->linelen;
|
|
|
|
insert = INSERT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S':
|
|
|
|
es->cursor = domove(1, "^", 1);
|
|
|
|
del_range(es->cursor, es->linelen);
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
insert = INSERT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Y':
|
|
|
|
cmd = "y$";
|
|
|
|
/* ahhhhhh... */
|
2016-11-12 00:48:30 +01:00
|
|
|
|
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
case 'c':
|
|
|
|
case 'd':
|
|
|
|
case 'y':
|
|
|
|
if (*cmd == cmd[1]) {
|
|
|
|
c1 = *cmd == 'c' ? domove(1, "^", 1) : 0;
|
|
|
|
c2 = es->linelen;
|
|
|
|
} else if (!is_move(cmd[1]))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
else {
|
|
|
|
if ((ncursor = domove(argcnt, &cmd[1], 1)) < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (*cmd == 'c' &&
|
2005-06-09 00:22:24 +02:00
|
|
|
(cmd[1] == 'w' || cmd[1] == 'W') &&
|
2006-11-10 08:18:58 +01:00
|
|
|
!ksh_isspace(es->cbuf[es->cursor])) {
|
2008-10-28 15:32:43 +01:00
|
|
|
do {
|
|
|
|
--ncursor;
|
|
|
|
} while (ksh_isspace(es->cbuf[ncursor]));
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor++;
|
|
|
|
}
|
|
|
|
if (ncursor > es->cursor) {
|
|
|
|
c1 = es->cursor;
|
|
|
|
c2 = ncursor;
|
|
|
|
} else {
|
|
|
|
c1 = ncursor;
|
|
|
|
c2 = es->cursor;
|
|
|
|
if (cmd[1] == '%')
|
|
|
|
c2++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*cmd != 'c' && c1 != c2)
|
|
|
|
yank_range(c1, c2);
|
|
|
|
if (*cmd != 'y') {
|
|
|
|
del_range(c1, c2);
|
|
|
|
es->cursor = c1;
|
|
|
|
}
|
|
|
|
if (*cmd == 'c') {
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
insert = INSERT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (es->linelen != 0)
|
|
|
|
es->cursor++;
|
2013-08-10 15:44:33 +02:00
|
|
|
while (putbuf(ybuf, yanklen, false) == 0 &&
|
|
|
|
--argcnt > 0)
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
|
|
|
if (es->cursor != 0)
|
|
|
|
es->cursor--;
|
|
|
|
if (argcnt != 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'P':
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
any = 0;
|
2013-08-10 15:44:33 +02:00
|
|
|
while (putbuf(ybuf, yanklen, false) == 0 &&
|
|
|
|
--argcnt > 0)
|
2005-05-23 05:06:10 +02:00
|
|
|
any = 1;
|
|
|
|
if (any && es->cursor != 0)
|
|
|
|
es->cursor--;
|
|
|
|
if (argcnt != 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
del_range(es->cursor, es->linelen);
|
|
|
|
insert = INSERT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
|
|
|
yank_range(es->cursor, es->linelen);
|
|
|
|
del_range(es->cursor, es->linelen);
|
|
|
|
if (es->cursor != 0)
|
|
|
|
es->cursor--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'g':
|
|
|
|
if (!argcnt)
|
|
|
|
argcnt = hlast;
|
2010-01-29 10:34:31 +01:00
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
case 'G':
|
|
|
|
if (!argcnt)
|
|
|
|
argcnt = 1;
|
|
|
|
else
|
|
|
|
argcnt = hlast - (source->line - argcnt);
|
|
|
|
if (grabhist(modified, argcnt - 1) < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
else {
|
|
|
|
modified = 0;
|
|
|
|
hnum = argcnt - 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
insert = INSERT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'I':
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
es->cursor = domove(1, "^", 1);
|
|
|
|
insert = INSERT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'j':
|
|
|
|
case '+':
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
case CTRL('n'):
|
2005-05-23 05:06:10 +02:00
|
|
|
if (grabhist(modified, hnum + argcnt) < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
else {
|
|
|
|
modified = 0;
|
|
|
|
hnum += argcnt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'k':
|
|
|
|
case '-':
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
case CTRL('p'):
|
2005-05-23 05:06:10 +02:00
|
|
|
if (grabhist(modified, hnum - argcnt) < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
else {
|
|
|
|
modified = 0;
|
|
|
|
hnum -= argcnt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
if (es->linelen == 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (cmd[1] == 0)
|
|
|
|
vi_error();
|
|
|
|
else {
|
2005-06-09 00:22:24 +02:00
|
|
|
int n;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (es->cursor + argcnt > es->linelen)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (n = 0; n < argcnt; ++n)
|
|
|
|
es->cbuf[es->cursor + n] = cmd[1];
|
|
|
|
es->cursor += n - 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'R':
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
insert = REPLACE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
if (es->linelen == 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (es->cursor + argcnt > es->linelen)
|
|
|
|
argcnt = es->linelen - es->cursor;
|
|
|
|
del_range(es->cursor, es->cursor + argcnt);
|
|
|
|
insert = INSERT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
if (!argcnt) {
|
2008-09-30 21:28:12 +02:00
|
|
|
if (es->linelen == 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (modified) {
|
|
|
|
es->cbuf[es->linelen] = '\0';
|
2015-07-05 21:37:18 +02:00
|
|
|
histsave(&source->line, es->cbuf,
|
|
|
|
HIST_STORE, true);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else
|
2009-06-10 20:12:51 +02:00
|
|
|
argcnt = source->line + 1 -
|
|
|
|
(hlast - hnum);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2007-07-22 15:34:52 +02:00
|
|
|
if (argcnt)
|
2016-07-25 02:04:48 +02:00
|
|
|
shf_snprintf(es->cbuf, es->cbufsize, Tf_sd,
|
2007-07-22 15:34:52 +02:00
|
|
|
"fc -e ${VISUAL:-${EDITOR:-vi}} --",
|
|
|
|
argcnt);
|
|
|
|
else
|
|
|
|
strlcpy(es->cbuf,
|
|
|
|
"fc -e ${VISUAL:-${EDITOR:-vi}} --",
|
|
|
|
es->cbufsize);
|
2005-05-23 05:06:10 +02:00
|
|
|
es->linelen = strlen(es->cbuf);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (2);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case 'x':
|
|
|
|
if (es->linelen == 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (es->cursor + argcnt > es->linelen)
|
|
|
|
argcnt = es->linelen - es->cursor;
|
|
|
|
yank_range(es->cursor, es->cursor + argcnt);
|
|
|
|
del_range(es->cursor, es->cursor + argcnt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'X':
|
|
|
|
if (es->cursor > 0) {
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (es->cursor < argcnt)
|
|
|
|
argcnt = es->cursor;
|
|
|
|
yank_range(es->cursor - argcnt, es->cursor);
|
|
|
|
del_range(es->cursor - argcnt, es->cursor);
|
|
|
|
es->cursor -= argcnt;
|
|
|
|
} else
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
t = es;
|
|
|
|
es = undo;
|
|
|
|
undo = t;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'U':
|
|
|
|
if (!modified)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (grabhist(modified, ohnum) < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
modified = 0;
|
|
|
|
hnum = ohnum;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
if (hnum == hlast)
|
|
|
|
hnum = -1;
|
|
|
|
/* ahhh */
|
2016-11-12 00:48:30 +01:00
|
|
|
|
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
case '/':
|
|
|
|
c3 = 1;
|
|
|
|
srchlen = 0;
|
|
|
|
lastsearch = *cmd;
|
2010-01-29 10:34:31 +01:00
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
case 'n':
|
|
|
|
case 'N':
|
|
|
|
if (lastsearch == ' ')
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (lastsearch == '?')
|
|
|
|
c1 = 1;
|
|
|
|
else
|
|
|
|
c1 = 0;
|
|
|
|
if (*cmd == 'N')
|
|
|
|
c1 = !c1;
|
|
|
|
if ((c2 = grabsearch(modified, hnum,
|
|
|
|
c1, srchpat)) < 0) {
|
|
|
|
if (c3) {
|
|
|
|
restore_cbuf();
|
|
|
|
refresh(0);
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
modified = 0;
|
|
|
|
hnum = c2;
|
|
|
|
ohnum = hnum;
|
|
|
|
}
|
2013-05-02 17:33:30 +02:00
|
|
|
if (argcnt >= 2) {
|
|
|
|
/* flag from cursor-up command */
|
|
|
|
es->cursor = argcnt - 2;
|
|
|
|
return (0);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2005-05-26 01:39:10 +02:00
|
|
|
case '_':
|
|
|
|
{
|
2012-10-03 19:24:23 +02:00
|
|
|
bool inspace;
|
2005-06-09 00:22:24 +02:00
|
|
|
char *p, *sp;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2005-05-26 01:39:10 +02:00
|
|
|
if (histnum(-1) < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-26 01:39:10 +02:00
|
|
|
p = *histpos();
|
2006-11-10 08:18:58 +01:00
|
|
|
#define issp(c) (ksh_isspace(c) || (c) == '\n')
|
2005-05-26 01:39:10 +02:00
|
|
|
if (argcnt) {
|
2005-05-23 05:06:10 +02:00
|
|
|
while (*p && issp(*p))
|
|
|
|
p++;
|
2005-05-26 01:39:10 +02:00
|
|
|
while (*p && --argcnt) {
|
|
|
|
while (*p && !issp(*p))
|
|
|
|
p++;
|
|
|
|
while (*p && issp(*p))
|
|
|
|
p++;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2005-05-26 01:39:10 +02:00
|
|
|
if (!*p)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-26 01:39:10 +02:00
|
|
|
sp = p;
|
|
|
|
} else {
|
|
|
|
sp = p;
|
2012-10-03 19:24:23 +02:00
|
|
|
inspace = false;
|
2005-05-26 01:39:10 +02:00
|
|
|
while (*p) {
|
|
|
|
if (issp(*p))
|
2012-10-03 19:24:23 +02:00
|
|
|
inspace = true;
|
2005-05-26 01:39:10 +02:00
|
|
|
else if (inspace) {
|
2012-10-03 19:24:23 +02:00
|
|
|
inspace = false;
|
2005-05-26 01:39:10 +02:00
|
|
|
sp = p;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
p = sp;
|
|
|
|
}
|
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
|
|
|
if (es->cursor != es->linelen)
|
|
|
|
es->cursor++;
|
|
|
|
while (*p && !issp(*p)) {
|
|
|
|
argcnt++;
|
2005-05-23 05:06:10 +02:00
|
|
|
p++;
|
|
|
|
}
|
2016-07-25 02:04:48 +02:00
|
|
|
if (putbuf(T1space, 1, false) != 0 ||
|
2013-08-10 15:44:33 +02:00
|
|
|
putbuf(sp, argcnt, false) != 0) {
|
2005-05-26 01:39:10 +02:00
|
|
|
if (es->cursor != 0)
|
|
|
|
es->cursor--;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-26 01:39:10 +02:00
|
|
|
}
|
|
|
|
insert = INSERT;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-05-26 01:39:10 +02:00
|
|
|
case '~':
|
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
char *p;
|
|
|
|
int i;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2005-05-26 01:39:10 +02:00
|
|
|
if (es->linelen == 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-26 01:39:10 +02:00
|
|
|
for (i = 0; i < argcnt; i++) {
|
|
|
|
p = &es->cbuf[es->cursor];
|
2006-11-10 08:18:58 +01:00
|
|
|
if (ksh_islower(*p)) {
|
2005-05-26 01:39:10 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2006-11-10 08:18:58 +01:00
|
|
|
*p = ksh_toupper(*p);
|
|
|
|
} else if (ksh_isupper(*p)) {
|
2005-05-26 01:39:10 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2006-11-10 08:18:58 +01:00
|
|
|
*p = ksh_tolower(*p);
|
2005-05-26 01:39:10 +02:00
|
|
|
}
|
|
|
|
if (es->cursor < es->linelen - 1)
|
|
|
|
es->cursor++;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2005-05-26 01:39:10 +02:00
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case '#':
|
2005-05-26 01:39:10 +02:00
|
|
|
{
|
|
|
|
int ret = x_do_comment(es->cbuf, es->cbufsize,
|
|
|
|
&es->linelen);
|
|
|
|
if (ret >= 0)
|
|
|
|
es->cursor = 0;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ret);
|
2005-05-26 01:39:10 +02:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/* AT&T ksh */
|
|
|
|
case '=':
|
|
|
|
/* Nonstandard vi/ksh */
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
case CTRL('e'):
|
2005-05-23 05:06:10 +02:00
|
|
|
print_expansions(es, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/* Nonstandard vi/ksh */
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
case CTRL('i'):
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!Flag(FVITABCOMPLETE))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
complete_word(1, argcnt);
|
|
|
|
break;
|
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/* some annoying AT&T kshs */
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
case CTRL('['):
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!Flag(FVIESCCOMPLETE))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2016-11-12 00:48:30 +01:00
|
|
|
/* FALLTHROUGH */
|
2011-02-03 16:57:52 +01:00
|
|
|
/* AT&T ksh */
|
|
|
|
case '\\':
|
|
|
|
/* Nonstandard vi/ksh */
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
case CTRL('f'):
|
2005-05-23 05:06:10 +02:00
|
|
|
complete_word(1, argcnt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/* AT&T ksh */
|
|
|
|
case '*':
|
|
|
|
/* Nonstandard vi/ksh */
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
case CTRL('x'):
|
2005-05-23 05:06:10 +02:00
|
|
|
expand_word(1);
|
|
|
|
break;
|
2013-05-02 17:33:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* mksh: cursor movement */
|
|
|
|
case '[':
|
|
|
|
case 'O':
|
|
|
|
state = VPREFIX2;
|
|
|
|
if (es->linelen != 0)
|
|
|
|
es->cursor++;
|
|
|
|
insert = INSERT;
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (insert == 0 && es->cursor != 0 && es->cursor >= es->linelen)
|
|
|
|
es->cursor--;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
domove(int argcnt, const char *cmd, int sub)
|
|
|
|
{
|
2016-03-04 15:26:16 +01:00
|
|
|
int ncursor = 0, i = 0, t;
|
|
|
|
unsigned int bcount;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
switch (*cmd) {
|
|
|
|
case 'b':
|
|
|
|
if (!sub && es->cursor == 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor = backword(argcnt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B':
|
|
|
|
if (!sub && es->cursor == 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor = Backword(argcnt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e':
|
|
|
|
if (!sub && es->cursor + 1 >= es->linelen)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor = endword(argcnt);
|
|
|
|
if (sub && ncursor < es->linelen)
|
|
|
|
ncursor++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'E':
|
|
|
|
if (!sub && es->cursor + 1 >= es->linelen)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor = Endword(argcnt);
|
|
|
|
if (sub && ncursor < es->linelen)
|
|
|
|
ncursor++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
|
|
|
case 't':
|
|
|
|
case 'T':
|
|
|
|
fsavecmd = *cmd;
|
|
|
|
fsavech = cmd[1];
|
2016-11-12 00:48:30 +01:00
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
case ',':
|
|
|
|
case ';':
|
|
|
|
if (fsavecmd == ' ')
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
i = fsavecmd == 'f' || fsavecmd == 'F';
|
|
|
|
t = fsavecmd > 'a';
|
|
|
|
if (*cmd == ',')
|
|
|
|
t = !t;
|
2013-08-10 15:44:33 +02:00
|
|
|
if ((ncursor = findch(fsavech, argcnt, tobool(t),
|
|
|
|
tobool(i))) < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (sub && t)
|
|
|
|
ncursor++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
case CTRL('h'):
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!sub && es->cursor == 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor = es->cursor - argcnt;
|
|
|
|
if (ncursor < 0)
|
|
|
|
ncursor = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ' ':
|
|
|
|
case 'l':
|
|
|
|
if (!sub && es->cursor + 1 >= es->linelen)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (es->linelen != 0) {
|
|
|
|
ncursor = es->cursor + argcnt;
|
|
|
|
if (ncursor > es->linelen)
|
|
|
|
ncursor = es->linelen;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w':
|
|
|
|
if (!sub && es->cursor + 1 >= es->linelen)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor = forwword(argcnt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'W':
|
|
|
|
if (!sub && es->cursor + 1 >= es->linelen)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor = Forwword(argcnt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '0':
|
|
|
|
ncursor = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '^':
|
|
|
|
ncursor = 0;
|
2006-07-03 14:16:31 +02:00
|
|
|
while (ncursor < es->linelen - 1 &&
|
2006-11-10 08:18:58 +01:00
|
|
|
ksh_isspace(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '|':
|
|
|
|
ncursor = argcnt;
|
|
|
|
if (ncursor > es->linelen)
|
|
|
|
ncursor = es->linelen;
|
|
|
|
if (ncursor)
|
|
|
|
ncursor--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '$':
|
|
|
|
if (es->linelen != 0)
|
|
|
|
ncursor = es->linelen;
|
|
|
|
else
|
|
|
|
ncursor = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '%':
|
|
|
|
ncursor = es->cursor;
|
|
|
|
while (ncursor < es->linelen &&
|
|
|
|
(i = bracktype(es->cbuf[ncursor])) == 0)
|
|
|
|
ncursor++;
|
|
|
|
if (ncursor == es->linelen)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
bcount = 1;
|
|
|
|
do {
|
|
|
|
if (i > 0) {
|
|
|
|
if (++ncursor >= es->linelen)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
if (--ncursor < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
t = bracktype(es->cbuf[ncursor]);
|
|
|
|
if (t == i)
|
|
|
|
bcount++;
|
|
|
|
else if (t == -i)
|
|
|
|
bcount--;
|
|
|
|
} while (bcount != 0);
|
|
|
|
if (sub && i > 0)
|
|
|
|
ncursor++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ncursor);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
redo_insert(int count)
|
|
|
|
{
|
|
|
|
while (count-- > 0)
|
2013-08-10 15:44:33 +02:00
|
|
|
if (putbuf(ibuf, inslen, tobool(insert == REPLACE)) != 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (es->cursor > 0)
|
|
|
|
es->cursor--;
|
|
|
|
insert = 0;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
yank_range(int a, int b)
|
|
|
|
{
|
|
|
|
yanklen = b - a;
|
|
|
|
if (yanklen != 0)
|
|
|
|
memmove(ybuf, &es->cbuf[a], yanklen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
bracktype(int ch)
|
|
|
|
{
|
|
|
|
switch (ch) {
|
|
|
|
|
|
|
|
case '(':
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case '[':
|
2009-06-08 22:06:50 +02:00
|
|
|
return (2);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case '{':
|
2009-06-08 22:06:50 +02:00
|
|
|
return (3);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case ')':
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case ']':
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-2);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case '}':
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-3);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
default:
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Non user interface editor routines below here
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
save_cbuf(void)
|
|
|
|
{
|
2013-08-10 15:44:33 +02:00
|
|
|
memmove(holdbufp, es->cbuf, es->linelen);
|
2005-05-23 05:06:10 +02:00
|
|
|
holdlen = es->linelen;
|
2013-08-10 15:44:33 +02:00
|
|
|
holdbufp[holdlen] = '\0';
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
restore_cbuf(void)
|
|
|
|
{
|
|
|
|
es->cursor = 0;
|
|
|
|
es->linelen = holdlen;
|
2013-08-10 15:44:33 +02:00
|
|
|
memmove(es->cbuf, holdbufp, holdlen);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return a new edstate */
|
|
|
|
static struct edstate *
|
|
|
|
save_edstate(struct edstate *old)
|
|
|
|
{
|
2010-07-18 00:09:40 +02:00
|
|
|
struct edstate *news;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2013-08-10 15:44:33 +02:00
|
|
|
news = alloc(sizeof(struct edstate), AEDIT);
|
|
|
|
news->cbuf = alloc(old->cbufsize, AEDIT);
|
2010-07-18 00:09:40 +02:00
|
|
|
memcpy(news->cbuf, old->cbuf, old->linelen);
|
|
|
|
news->cbufsize = old->cbufsize;
|
|
|
|
news->linelen = old->linelen;
|
|
|
|
news->cursor = old->cursor;
|
|
|
|
news->winleft = old->winleft;
|
|
|
|
return (news);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-07-18 00:09:40 +02:00
|
|
|
restore_edstate(struct edstate *news, struct edstate *old)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2010-07-18 00:09:40 +02:00
|
|
|
memcpy(news->cbuf, old->cbuf, old->linelen);
|
|
|
|
news->linelen = old->linelen;
|
|
|
|
news->cursor = old->cursor;
|
|
|
|
news->winleft = old->winleft;
|
2005-05-23 05:06:10 +02:00
|
|
|
free_edstate(old);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_edstate(struct edstate *old)
|
|
|
|
{
|
2013-08-10 15:44:33 +02:00
|
|
|
afree(old->cbuf, AEDIT);
|
|
|
|
afree(old, AEDIT);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is used for calling x_escape() in complete_word()
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
x_vi_putbuf(const char *s, size_t len)
|
|
|
|
{
|
2013-08-10 15:44:33 +02:00
|
|
|
return (putbuf(s, len, false));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-08-10 15:44:33 +02:00
|
|
|
putbuf(const char *buf, ssize_t len, bool repl)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
if (len == 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (repl) {
|
|
|
|
if (es->cursor + len >= es->cbufsize)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (es->cursor + len > es->linelen)
|
|
|
|
es->linelen = es->cursor + len;
|
|
|
|
} else {
|
|
|
|
if (es->linelen + len >= es->cbufsize)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
memmove(&es->cbuf[es->cursor + len], &es->cbuf[es->cursor],
|
|
|
|
es->linelen - es->cursor);
|
|
|
|
es->linelen += len;
|
|
|
|
}
|
|
|
|
memmove(&es->cbuf[es->cursor], buf, len);
|
|
|
|
es->cursor += len;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
del_range(int a, int b)
|
|
|
|
{
|
|
|
|
if (es->linelen != b)
|
|
|
|
memmove(&es->cbuf[a], &es->cbuf[b], es->linelen - b);
|
|
|
|
es->linelen -= b - a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-08-10 15:44:33 +02:00
|
|
|
findch(int ch, int cnt, bool forw, bool incl)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
int ncursor;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (es->linelen == 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor = es->cursor;
|
|
|
|
while (cnt--) {
|
|
|
|
do {
|
|
|
|
if (forw) {
|
|
|
|
if (++ncursor == es->linelen)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
if (--ncursor < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
} while (es->cbuf[ncursor] != ch);
|
|
|
|
}
|
|
|
|
if (!incl) {
|
|
|
|
if (forw)
|
|
|
|
ncursor--;
|
|
|
|
else
|
|
|
|
ncursor++;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ncursor);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
forwword(int argcnt)
|
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
int ncursor;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
ncursor = es->cursor;
|
|
|
|
while (ncursor < es->linelen && argcnt--) {
|
2006-11-10 08:52:04 +01:00
|
|
|
if (ksh_isalnux(es->cbuf[ncursor]))
|
2016-04-14 13:51:26 +02:00
|
|
|
while (ncursor < es->linelen &&
|
|
|
|
ksh_isalnux(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor++;
|
2006-11-10 08:18:58 +01:00
|
|
|
else if (!ksh_isspace(es->cbuf[ncursor]))
|
2016-04-14 13:51:26 +02:00
|
|
|
while (ncursor < es->linelen &&
|
|
|
|
!ksh_isalnux(es->cbuf[ncursor]) &&
|
|
|
|
!ksh_isspace(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor++;
|
2016-04-14 13:51:26 +02:00
|
|
|
while (ncursor < es->linelen &&
|
|
|
|
ksh_isspace(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor++;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ncursor);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
backword(int argcnt)
|
|
|
|
{
|
2009-06-10 21:33:16 +02:00
|
|
|
int ncursor;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
ncursor = es->cursor;
|
|
|
|
while (ncursor > 0 && argcnt--) {
|
2006-11-10 08:18:58 +01:00
|
|
|
while (--ncursor > 0 && ksh_isspace(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
|
|
|
if (ncursor > 0) {
|
2006-11-10 08:52:04 +01:00
|
|
|
if (ksh_isalnux(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
while (--ncursor >= 0 &&
|
2006-11-10 08:52:04 +01:00
|
|
|
ksh_isalnux(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
|
|
|
else
|
|
|
|
while (--ncursor >= 0 &&
|
2006-11-10 08:52:04 +01:00
|
|
|
!ksh_isalnux(es->cbuf[ncursor]) &&
|
2006-11-10 08:18:58 +01:00
|
|
|
!ksh_isspace(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
|
|
|
ncursor++;
|
|
|
|
}
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ncursor);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
endword(int argcnt)
|
|
|
|
{
|
2009-06-10 21:33:16 +02:00
|
|
|
int ncursor;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
ncursor = es->cursor;
|
|
|
|
while (ncursor < es->linelen && argcnt--) {
|
|
|
|
while (++ncursor < es->linelen - 1 &&
|
2006-11-10 08:18:58 +01:00
|
|
|
ksh_isspace(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
|
|
|
if (ncursor < es->linelen - 1) {
|
2006-11-10 08:52:04 +01:00
|
|
|
if (ksh_isalnux(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
while (++ncursor < es->linelen &&
|
2006-11-10 08:52:04 +01:00
|
|
|
ksh_isalnux(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
|
|
|
else
|
|
|
|
while (++ncursor < es->linelen &&
|
2006-11-10 08:52:04 +01:00
|
|
|
!ksh_isalnux(es->cbuf[ncursor]) &&
|
2006-11-10 08:18:58 +01:00
|
|
|
!ksh_isspace(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
|
|
|
ncursor--;
|
|
|
|
}
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ncursor);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
Forwword(int argcnt)
|
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
int ncursor;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
ncursor = es->cursor;
|
|
|
|
while (ncursor < es->linelen && argcnt--) {
|
2016-04-14 13:51:26 +02:00
|
|
|
while (ncursor < es->linelen &&
|
|
|
|
!ksh_isspace(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor++;
|
2016-04-14 13:51:26 +02:00
|
|
|
while (ncursor < es->linelen &&
|
|
|
|
ksh_isspace(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor++;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ncursor);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
Backword(int argcnt)
|
|
|
|
{
|
2009-06-10 21:33:16 +02:00
|
|
|
int ncursor;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
ncursor = es->cursor;
|
|
|
|
while (ncursor > 0 && argcnt--) {
|
2009-06-10 21:33:16 +02:00
|
|
|
while (--ncursor >= 0 && ksh_isspace(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
2009-06-10 21:33:16 +02:00
|
|
|
while (ncursor >= 0 && !ksh_isspace(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
ncursor--;
|
|
|
|
ncursor++;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ncursor);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
Endword(int argcnt)
|
|
|
|
{
|
2009-06-10 21:33:16 +02:00
|
|
|
int ncursor;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
ncursor = es->cursor;
|
|
|
|
while (ncursor < es->linelen - 1 && argcnt--) {
|
|
|
|
while (++ncursor < es->linelen - 1 &&
|
2006-11-10 08:18:58 +01:00
|
|
|
ksh_isspace(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
|
|
|
if (ncursor < es->linelen - 1) {
|
|
|
|
while (++ncursor < es->linelen &&
|
2006-11-10 08:18:58 +01:00
|
|
|
!ksh_isspace(es->cbuf[ncursor]))
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
|
|
|
ncursor--;
|
|
|
|
}
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ncursor);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
grabhist(int save, int n)
|
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
char *hptr;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (n < 0 || n > hlast)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (n == hlast) {
|
|
|
|
restore_cbuf();
|
|
|
|
ohnum = n;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2005-06-09 00:22:24 +02:00
|
|
|
(void)histnum(n);
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((hptr = *histpos()) == NULL) {
|
2016-01-21 19:24:45 +01:00
|
|
|
internal_warningf("grabhist: bad history array");
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (save)
|
|
|
|
save_cbuf();
|
|
|
|
if ((es->linelen = strlen(hptr)) >= es->cbufsize)
|
|
|
|
es->linelen = es->cbufsize - 1;
|
|
|
|
memmove(es->cbuf, hptr, es->linelen);
|
|
|
|
es->cursor = 0;
|
|
|
|
ohnum = n;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-12-05 00:22:59 +01:00
|
|
|
grabsearch(int save, int start, int fwd, const char *pat)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
char *hptr;
|
|
|
|
int hist;
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
bool anchored;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2005-06-09 00:22:24 +02:00
|
|
|
if ((start == 0 && fwd == 0) || (start >= hlast - 1 && fwd == 1))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (fwd)
|
|
|
|
start++;
|
|
|
|
else
|
|
|
|
start--;
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
anchored = *pat == '^' ? (++pat, true) : false;
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((hist = findhist(start, fwd, pat, anchored)) < 0) {
|
2013-08-10 15:44:33 +02:00
|
|
|
/* (start != 0 && fwd && match(holdbufp, pat) >= 0) */
|
|
|
|
if (start != 0 && fwd && strcmp(holdbufp, pat) >= 0) {
|
2005-05-23 05:06:10 +02:00
|
|
|
restore_cbuf();
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (save)
|
|
|
|
save_cbuf();
|
|
|
|
histnum(hist);
|
|
|
|
hptr = *histpos();
|
|
|
|
if ((es->linelen = strlen(hptr)) >= es->cbufsize)
|
|
|
|
es->linelen = es->cbufsize - 1;
|
|
|
|
memmove(es->cbuf, hptr, es->linelen);
|
|
|
|
es->cursor = 0;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (hist);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-07-16 20:15:45 +02:00
|
|
|
redraw_line(bool newl)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-07-16 20:15:45 +02:00
|
|
|
if (wbuf_len)
|
|
|
|
memset(wbuf[win], ' ', wbuf_len);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (newl) {
|
|
|
|
x_putc('\r');
|
|
|
|
x_putc('\n');
|
|
|
|
}
|
2016-07-26 23:37:26 +02:00
|
|
|
x_pprompt();
|
2005-05-23 05:06:10 +02:00
|
|
|
morec = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
refresh(int leftside)
|
|
|
|
{
|
|
|
|
if (leftside < 0)
|
|
|
|
leftside = lastref;
|
|
|
|
else
|
|
|
|
lastref = leftside;
|
|
|
|
if (outofwin())
|
|
|
|
rewindow();
|
|
|
|
display(wbuf[1 - win], wbuf[win], leftside);
|
|
|
|
win = 1 - win;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
outofwin(void)
|
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
int cur, col;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (es->cursor < es->winleft)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
col = 0;
|
|
|
|
cur = es->winleft;
|
|
|
|
while (cur < es->cursor)
|
2005-06-09 00:22:24 +02:00
|
|
|
col = newcol((unsigned char)es->cbuf[cur++], col);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (col >= winwidth)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rewindow(void)
|
|
|
|
{
|
2005-06-09 00:22:24 +02:00
|
|
|
int tcur, tcol;
|
|
|
|
int holdcur1, holdcol1;
|
|
|
|
int holdcur2, holdcol2;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
holdcur1 = holdcur2 = tcur = 0;
|
|
|
|
holdcol1 = holdcol2 = tcol = 0;
|
|
|
|
while (tcur < es->cursor) {
|
|
|
|
if (tcol - holdcol2 > winwidth / 2) {
|
|
|
|
holdcur1 = holdcur2;
|
|
|
|
holdcol1 = holdcol2;
|
|
|
|
holdcur2 = tcur;
|
|
|
|
holdcol2 = tcol;
|
|
|
|
}
|
2005-06-09 00:22:24 +02:00
|
|
|
tcol = newcol((unsigned char)es->cbuf[tcur++], tcol);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
while (tcol - holdcol1 > winwidth / 2)
|
2005-06-09 00:22:24 +02:00
|
|
|
holdcol1 = newcol((unsigned char)es->cbuf[holdcur1++],
|
2005-05-23 05:06:10 +02:00
|
|
|
holdcol1);
|
|
|
|
es->winleft = holdcur1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
newcol(unsigned char ch, int col)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
if (ch == '\t')
|
2009-06-08 22:06:50 +02:00
|
|
|
return ((col | 7) + 1);
|
|
|
|
return (col + char_len(ch));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
display(char *wb1, char *wb2, int leftside)
|
|
|
|
{
|
|
|
|
unsigned char ch;
|
2005-06-09 00:22:24 +02:00
|
|
|
char *twb1, *twb2, mc;
|
|
|
|
int cur, col, cnt;
|
|
|
|
int ncol = 0;
|
|
|
|
int moreright;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
col = 0;
|
|
|
|
cur = es->winleft;
|
|
|
|
moreright = 0;
|
|
|
|
twb1 = wb1;
|
|
|
|
while (col < winwidth && cur < es->linelen) {
|
|
|
|
if (cur == es->cursor && leftside)
|
|
|
|
ncol = col + pwidth;
|
2006-11-19 21:43:14 +01:00
|
|
|
if ((ch = es->cbuf[cur]) == '\t')
|
2005-05-23 05:06:10 +02:00
|
|
|
do {
|
|
|
|
*twb1++ = ' ';
|
|
|
|
} while (++col < winwidth && (col & 7) != 0);
|
2006-11-19 21:43:14 +01:00
|
|
|
else if (col < winwidth) {
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
if (ISCTRL(ch) && /* but not C1 */ ch < 0x80) {
|
2006-11-19 21:43:14 +01:00
|
|
|
*twb1++ = '^';
|
2005-05-23 05:06:10 +02:00
|
|
|
if (++col < winwidth) {
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
*twb1++ = UNCTRL(ch);
|
2005-05-23 05:06:10 +02:00
|
|
|
col++;
|
|
|
|
}
|
2006-11-19 21:43:14 +01:00
|
|
|
} else {
|
|
|
|
*twb1++ = ch;
|
|
|
|
col++;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cur == es->cursor && !leftside)
|
|
|
|
ncol = col + pwidth - 1;
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
if (cur == es->cursor)
|
|
|
|
ncol = col + pwidth;
|
|
|
|
if (col < winwidth) {
|
|
|
|
while (col < winwidth) {
|
|
|
|
*twb1++ = ' ';
|
|
|
|
col++;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
moreright++;
|
|
|
|
*twb1 = ' ';
|
|
|
|
|
|
|
|
col = pwidth;
|
|
|
|
cnt = winwidth;
|
|
|
|
twb1 = wb1;
|
|
|
|
twb2 = wb2;
|
|
|
|
while (cnt--) {
|
|
|
|
if (*twb1 != *twb2) {
|
2013-07-26 22:33:39 +02:00
|
|
|
if (x_col != col)
|
2005-05-23 05:06:10 +02:00
|
|
|
ed_mov_opt(col, wb1);
|
|
|
|
x_putc(*twb1);
|
2013-07-26 22:33:39 +02:00
|
|
|
x_col++;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
twb1++;
|
|
|
|
twb2++;
|
|
|
|
col++;
|
|
|
|
}
|
|
|
|
if (es->winleft > 0 && moreright)
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* POSIX says to use * for this but that is a globbing
|
2005-05-23 05:06:10 +02:00
|
|
|
* character and may confuse people; + is more innocuous
|
|
|
|
*/
|
|
|
|
mc = '+';
|
|
|
|
else if (es->winleft > 0)
|
|
|
|
mc = '<';
|
|
|
|
else if (moreright)
|
|
|
|
mc = '>';
|
|
|
|
else
|
|
|
|
mc = ' ';
|
|
|
|
if (mc != morec) {
|
|
|
|
ed_mov_opt(pwidth + winwidth + 1, wb1);
|
|
|
|
x_putc(mc);
|
2013-07-26 22:33:39 +02:00
|
|
|
x_col++;
|
2005-05-23 05:06:10 +02:00
|
|
|
morec = mc;
|
|
|
|
}
|
2013-07-26 22:33:39 +02:00
|
|
|
if (x_col != ncol)
|
2005-05-23 05:06:10 +02:00
|
|
|
ed_mov_opt(ncol, wb1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ed_mov_opt(int col, char *wb)
|
|
|
|
{
|
2013-07-26 22:33:39 +02:00
|
|
|
if (col < x_col) {
|
|
|
|
if (col + 1 < x_col - col) {
|
2005-05-23 05:06:10 +02:00
|
|
|
x_putc('\r');
|
2016-07-26 23:37:26 +02:00
|
|
|
x_pprompt();
|
2013-07-26 22:33:39 +02:00
|
|
|
while (x_col++ < col)
|
2007-10-25 17:19:16 +02:00
|
|
|
x_putcf(*wb++);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
2013-07-26 22:33:39 +02:00
|
|
|
while (x_col-- > col)
|
2005-05-23 05:06:10 +02:00
|
|
|
x_putc('\b');
|
|
|
|
}
|
|
|
|
} else {
|
2013-07-26 22:33:39 +02:00
|
|
|
wb = &wb[x_col - pwidth];
|
|
|
|
while (x_col++ < col)
|
2007-10-25 17:19:16 +02:00
|
|
|
x_putcf(*wb++);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2013-07-26 22:33:39 +02:00
|
|
|
x_col = col;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* replace word with all expansions (ie, expand word*) */
|
|
|
|
static int
|
|
|
|
expand_word(int cmd)
|
|
|
|
{
|
|
|
|
static struct edstate *buf;
|
2011-02-03 16:57:52 +01:00
|
|
|
int rval = 0, nwords, start, end, i;
|
2005-05-23 05:06:10 +02:00
|
|
|
char **words;
|
|
|
|
|
|
|
|
/* Undo previous expansion */
|
|
|
|
if (cmd == 0 && expanded == EXPAND && buf) {
|
|
|
|
restore_edstate(es, buf);
|
|
|
|
buf = 0;
|
|
|
|
expanded = NONE;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (buf) {
|
|
|
|
free_edstate(buf);
|
|
|
|
buf = 0;
|
|
|
|
}
|
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
i = XCF_COMMAND_FILE | XCF_FULLPATH;
|
|
|
|
nwords = x_cf_glob(&i, es->cbuf, es->linelen, es->cursor,
|
|
|
|
&start, &end, &words);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (nwords == 0) {
|
|
|
|
vi_error();
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buf = save_edstate(es);
|
|
|
|
expanded = EXPAND;
|
|
|
|
del_range(start, end);
|
|
|
|
es->cursor = start;
|
2011-02-03 16:57:52 +01:00
|
|
|
i = 0;
|
|
|
|
while (i < nwords) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (x_escape(words[i], strlen(words[i]), x_vi_putbuf) != 0) {
|
|
|
|
rval = -1;
|
|
|
|
break;
|
|
|
|
}
|
2016-07-25 02:04:48 +02:00
|
|
|
if (++i < nwords && putbuf(T1space, 1, false) != 0) {
|
2005-05-23 05:06:10 +02:00
|
|
|
rval = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = buf->cursor - end;
|
|
|
|
if (rval == 0 && i > 0)
|
|
|
|
es->cursor += i;
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
insert = INSERT;
|
|
|
|
lastac = 0;
|
|
|
|
refresh(0);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (rval);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
complete_word(int cmd, int count)
|
|
|
|
{
|
|
|
|
static struct edstate *buf;
|
2011-08-27 20:06:52 +02:00
|
|
|
int rval, nwords, start, end, flags;
|
|
|
|
size_t match_len;
|
2005-05-23 05:06:10 +02:00
|
|
|
char **words;
|
|
|
|
char *match;
|
2011-02-03 16:57:52 +01:00
|
|
|
bool is_unique;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* Undo previous completion */
|
|
|
|
if (cmd == 0 && expanded == COMPLETE && buf) {
|
|
|
|
print_expansions(buf, 0);
|
|
|
|
expanded = PRINT;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (cmd == 0 && expanded == PRINT && buf) {
|
|
|
|
restore_edstate(es, buf);
|
|
|
|
buf = 0;
|
|
|
|
expanded = NONE;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (buf) {
|
|
|
|
free_edstate(buf);
|
|
|
|
buf = 0;
|
|
|
|
}
|
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* XCF_FULLPATH for count 'cause the menu printed by
|
|
|
|
* print_expansions() was done this way.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2011-02-03 16:57:52 +01:00
|
|
|
flags = XCF_COMMAND_FILE;
|
|
|
|
if (count)
|
|
|
|
flags |= XCF_FULLPATH;
|
|
|
|
nwords = x_cf_glob(&flags, es->cbuf, es->linelen, es->cursor,
|
|
|
|
&start, &end, &words);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (nwords == 0) {
|
|
|
|
vi_error();
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (count) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
count--;
|
|
|
|
if (count >= nwords) {
|
|
|
|
vi_error();
|
2011-02-03 16:57:52 +01:00
|
|
|
x_print_expansions(nwords, words,
|
2011-06-04 17:06:50 +02:00
|
|
|
tobool(flags & XCF_IS_COMMAND));
|
2005-05-23 05:06:10 +02:00
|
|
|
x_free_words(nwords, words);
|
2011-07-16 20:15:45 +02:00
|
|
|
redraw_line(false);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Expand the count'th word to its basename
|
|
|
|
*/
|
2011-02-03 16:57:52 +01:00
|
|
|
if (flags & XCF_IS_COMMAND) {
|
2005-05-23 05:06:10 +02:00
|
|
|
match = words[count] +
|
|
|
|
x_basename(words[count], NULL);
|
|
|
|
/* If more than one possible match, use full path */
|
|
|
|
for (i = 0; i < nwords; i++)
|
|
|
|
if (i != count &&
|
|
|
|
strcmp(words[i] + x_basename(words[i],
|
|
|
|
NULL), match) == 0) {
|
|
|
|
match = words[count];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
match = words[count];
|
|
|
|
match_len = strlen(match);
|
2008-04-19 19:21:55 +02:00
|
|
|
is_unique = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
/* expanded = PRINT; next call undo */
|
|
|
|
} else {
|
|
|
|
match = words[0];
|
|
|
|
match_len = x_longest_prefix(nwords, words);
|
2011-02-03 16:57:52 +01:00
|
|
|
/* next call will list completions */
|
|
|
|
expanded = COMPLETE;
|
2005-05-23 05:06:10 +02:00
|
|
|
is_unique = nwords == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = save_edstate(es);
|
|
|
|
del_range(start, end);
|
|
|
|
es->cursor = start;
|
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* escape all shell-sensitive characters and put the result into
|
|
|
|
* command buffer
|
|
|
|
*/
|
2005-05-23 05:06:10 +02:00
|
|
|
rval = x_escape(match, match_len, x_vi_putbuf);
|
|
|
|
|
|
|
|
if (rval == 0 && is_unique) {
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
|
|
|
* If exact match, don't undo. Allows directory completions
|
2005-05-23 05:06:10 +02:00
|
|
|
* to be used (ie, complete the next portion of the path).
|
|
|
|
*/
|
|
|
|
expanded = NONE;
|
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
/*
|
2011-06-30 15:48:13 +02:00
|
|
|
* append a space if this is a non-directory match
|
|
|
|
* and not a parameter or homedir substitution
|
2011-02-03 16:57:52 +01:00
|
|
|
*/
|
2016-11-12 00:31:39 +01:00
|
|
|
if (match_len > 0 && !mksh_cdirsep(match[match_len - 1]) &&
|
2012-08-24 22:05:13 +02:00
|
|
|
!(flags & XCF_IS_NOSPACE))
|
2016-07-25 02:04:48 +02:00
|
|
|
rval = putbuf(T1space, 1, false);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
x_free_words(nwords, words);
|
|
|
|
|
2005-06-09 00:22:24 +02:00
|
|
|
modified = 1;
|
|
|
|
hnum = hlast;
|
2005-05-23 05:06:10 +02:00
|
|
|
insert = INSERT;
|
2011-02-03 16:57:52 +01:00
|
|
|
/* prevent this from being redone... */
|
|
|
|
lastac = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
refresh(0);
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (rval);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-12 23:27:10 +01:00
|
|
|
print_expansions(struct edstate *est, int cmd MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-02-03 16:57:52 +01:00
|
|
|
int start, end, nwords, i;
|
2005-05-23 05:06:10 +02:00
|
|
|
char **words;
|
|
|
|
|
2011-02-03 16:57:52 +01:00
|
|
|
i = XCF_COMMAND_FILE | XCF_FULLPATH;
|
|
|
|
nwords = x_cf_glob(&i, est->cbuf, est->linelen, est->cursor,
|
|
|
|
&start, &end, &words);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (nwords == 0) {
|
|
|
|
vi_error();
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-06-04 17:06:50 +02:00
|
|
|
x_print_expansions(nwords, words, tobool(i & XCF_IS_COMMAND));
|
2005-05-23 05:06:10 +02:00
|
|
|
x_free_words(nwords, words);
|
2011-07-16 20:15:45 +02:00
|
|
|
redraw_line(false);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Similar to x_zotc(emacs.c), but no tab weirdness */
|
|
|
|
static void
|
|
|
|
x_vi_zotc(int c)
|
|
|
|
{
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
if (ISCTRL(c)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
x_putc('^');
|
slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
c = UNCTRL(c);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
x_putc(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vi_error(void)
|
|
|
|
{
|
|
|
|
/* Beem out of any macros as soon as an error occurs */
|
|
|
|
vi_macro_reset();
|
2005-08-21 15:02:17 +02:00
|
|
|
x_putc(7);
|
2005-05-23 05:06:10 +02:00
|
|
|
x_flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vi_macro_reset(void)
|
|
|
|
{
|
|
|
|
if (macro.p) {
|
2013-08-10 15:44:33 +02:00
|
|
|
afree(macro.buf, AEDIT);
|
2005-06-09 00:22:24 +02:00
|
|
|
memset((char *)¯o, 0, sizeof(macro));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
2009-09-24 19:15:33 +02:00
|
|
|
#endif /* !MKSH_S_NOVI */
|
2013-02-10 18:38:15 +01:00
|
|
|
|
|
|
|
/* called from main.c */
|
|
|
|
void
|
|
|
|
x_init(void)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
/*
|
2016-07-28 23:39:04 +02:00
|
|
|
* set edchars to force initial binding, except we need
|
|
|
|
* default values for ^W for some deficient systems…
|
2013-02-10 18:38:15 +01:00
|
|
|
*/
|
|
|
|
edchars.erase = edchars.kill = edchars.intr = edchars.quit =
|
2016-07-28 23:39:04 +02:00
|
|
|
edchars.eof = EDCHAR_INITIAL;
|
2013-02-10 18:38:15 +01:00
|
|
|
edchars.werase = 027;
|
|
|
|
|
2013-08-10 15:44:33 +02:00
|
|
|
/* command line editing specific memory allocation */
|
2013-02-10 18:38:15 +01:00
|
|
|
ainit(AEDIT);
|
2013-08-10 15:44:33 +02:00
|
|
|
holdbufp = alloc(LINE, AEDIT);
|
|
|
|
|
|
|
|
/* initialise Emacs command line editing mode */
|
2013-02-10 18:38:15 +01:00
|
|
|
x_nextcmd = -1;
|
|
|
|
|
|
|
|
x_tab = alloc2(X_NTABS, sizeof(*x_tab), AEDIT);
|
|
|
|
for (j = 0; j < X_TABSZ; j++)
|
|
|
|
x_tab[0][j] = XFUNC_insert;
|
|
|
|
for (i = 1; i < X_NTABS; i++)
|
|
|
|
for (j = 0; j < X_TABSZ; j++)
|
|
|
|
x_tab[i][j] = XFUNC_error;
|
|
|
|
for (i = 0; i < (int)NELEM(x_defbindings); i++)
|
|
|
|
x_tab[x_defbindings[i].xdb_tab][x_defbindings[i].xdb_char]
|
|
|
|
= x_defbindings[i].xdb_func;
|
|
|
|
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
x_atab = alloc2(X_NTABS, sizeof(*x_atab), AEDIT);
|
|
|
|
for (i = 1; i < X_NTABS; i++)
|
|
|
|
for (j = 0; j < X_TABSZ; j++)
|
|
|
|
x_atab[i][j] = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-02-10 18:41:06 +01:00
|
|
|
#ifdef DEBUG_LEAKS
|
2013-02-10 18:38:15 +01:00
|
|
|
void
|
|
|
|
x_done(void)
|
|
|
|
{
|
2013-02-10 20:05:37 +01:00
|
|
|
if (x_tab != NULL)
|
|
|
|
afreeall(AEDIT);
|
2013-02-10 18:38:15 +01:00
|
|
|
}
|
2013-02-10 18:39:38 +01:00
|
|
|
#endif
|
2016-07-25 22:43:54 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
x_initterm(const char *termtype)
|
|
|
|
{
|
|
|
|
/* default must be 0 (bss) */
|
|
|
|
x_term_mode = 0;
|
|
|
|
/* this is what tmux uses, don't ask me about it */
|
|
|
|
if (!strcmp(termtype, "screen") || !strncmp(termtype, "screen-", 7))
|
|
|
|
x_term_mode = 1;
|
|
|
|
}
|
2016-07-26 23:37:26 +02:00
|
|
|
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
static char *
|
|
|
|
x_eval_region_helper(const char *cmd, size_t len)
|
|
|
|
{
|
|
|
|
char * volatile cp;
|
|
|
|
newenv(E_ERRH);
|
|
|
|
|
|
|
|
if (!kshsetjmp(e->jbuf)) {
|
|
|
|
char *wds = alloc(len + 3, ATEMP);
|
|
|
|
|
|
|
|
wds[0] = FUNSUB;
|
|
|
|
memcpy(wds + 1, cmd, len);
|
|
|
|
wds[len + 1] = '\0';
|
|
|
|
wds[len + 2] = EOS;
|
|
|
|
|
|
|
|
cp = evalstr(wds, DOSCALAR);
|
|
|
|
strdupx(cp, cp, AEDIT);
|
|
|
|
} else
|
|
|
|
cp = NULL;
|
|
|
|
quitenv(NULL);
|
|
|
|
return (cp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
x_eval_region(int c MKSH_A_UNUSED)
|
|
|
|
{
|
|
|
|
char *evbeg, *evend, *cp;
|
|
|
|
size_t newlen;
|
|
|
|
/* only for LINE overflow checking */
|
|
|
|
size_t restlen;
|
|
|
|
|
|
|
|
if (xmp == NULL) {
|
|
|
|
evbeg = xbuf;
|
|
|
|
evend = xep;
|
|
|
|
} else if (xmp < xcp) {
|
|
|
|
evbeg = xmp;
|
|
|
|
evend = xcp;
|
|
|
|
} else {
|
|
|
|
evbeg = xcp;
|
|
|
|
evend = xmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
x_e_putc2('\r');
|
|
|
|
x_clrtoeol(' ', false);
|
|
|
|
x_flush();
|
|
|
|
x_mode(false);
|
|
|
|
cp = x_eval_region_helper(evbeg, evend - evbeg);
|
|
|
|
x_mode(true);
|
|
|
|
|
|
|
|
if (cp == NULL) {
|
|
|
|
/* command cannot be parsed */
|
|
|
|
x_eval_region_err:
|
|
|
|
x_e_putc2(7);
|
|
|
|
x_redraw('\r');
|
|
|
|
return (KSTD);
|
|
|
|
}
|
|
|
|
|
|
|
|
newlen = strlen(cp);
|
|
|
|
restlen = xep - evend;
|
|
|
|
/* check for LINE overflow, until this is dynamically allocated */
|
|
|
|
if (evbeg + newlen + restlen >= xend)
|
|
|
|
goto x_eval_region_err;
|
|
|
|
|
|
|
|
xmp = evbeg;
|
|
|
|
xcp = evbeg + newlen;
|
|
|
|
xep = xcp + restlen;
|
|
|
|
memmove(xcp, evend, restlen + /* NUL */ 1);
|
|
|
|
memcpy(xmp, cp, newlen);
|
|
|
|
afree(cp, AEDIT);
|
|
|
|
x_adjust();
|
|
|
|
x_modified();
|
|
|
|
return (KSTD);
|
|
|
|
}
|
|
|
|
#endif /* !MKSH_SMALL */
|
2012-05-04 22:49:08 +02:00
|
|
|
#endif /* !MKSH_NO_CMDLINE_EDITING */
|