blob: 0e517804c2558a62bbfdeeff5792cd81ae44ca4f [file] [log] [blame]
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001/* $OpenBSD: edit.c,v 1.41 2015/09/01 13:12:31 tedu Exp $ */
Geremy Condra03ebf062011-10-12 18:17:24 -07002/* $OpenBSD: edit.h,v 1.9 2011/05/30 17:14:35 martynas Exp $ */
Elliott Hughesfc0307d2016-02-02 15:26:47 -08003/* $OpenBSD: emacs.c,v 1.52 2015/09/10 22:48:58 nicm Exp $ */
4/* $OpenBSD: vi.c,v 1.30 2015/09/10 22:48:58 nicm Exp $ */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005
6/*-
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00007 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
Elliott Hughesdd4abe02018-02-05 15:55:19 -08008 * 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
Elliott Hughesfc0307d2016-02-02 15:26:47 -08009 * mirabilos <m@mirbsd.org>
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070010 *
11 * Provided that these terms and disclaimer and all copyright notices
12 * are retained or reproduced in an accompanying document, permission
13 * is granted to deal in this work without restriction, including un-
14 * limited rights to use, publicly perform, distribute, sell, modify,
15 * merge, give away, or sublicence.
16 *
17 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
18 * the utmost extent permitted by applicable law, neither express nor
19 * implied; without malicious intent or gross negligence. In no event
20 * may a licensor, author or contributor be held liable for indirect,
21 * direct, other damage, loss, or other issues arising in any way out
22 * of dealing in the work, even if advised of the possibility of such
23 * damage or existence of a defect, except proven that it results out
24 * of said person's immediate fault when using the work as intended.
25 */
26
27#include "sh.h"
28
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000029#ifndef MKSH_NO_CMDLINE_EDITING
30
Elliott Hughesdd4abe02018-02-05 15:55:19 -080031__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.342 2018/01/14 00:03:00 tg Exp $");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070032
33/*
34 * in later versions we might use libtermcap for this, but since external
35 * dependencies are problematic, this has not yet been decided on; another
Elliott Hughes23925bb2017-09-22 16:04:20 -070036 * good string is KSH_ESC_STRING "c" except on hardware terminals like the
37 * DEC VT420 which do a full power cycle then...
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070038 */
39#ifndef MKSH_CLS_STRING
Elliott Hughes23925bb2017-09-22 16:04:20 -070040#define MKSH_CLS_STRING KSH_ESC_STRING "[;H" KSH_ESC_STRING "[J"
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070041#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070042
43/* tty driver characters we are interested in */
Elliott Hughes77740fc2016-08-12 15:06:53 -070044#define EDCHAR_DISABLED 0xFFFFU
45#define EDCHAR_INITIAL 0xFFFEU
46static struct {
47 unsigned short erase;
48 unsigned short kill;
49 unsigned short werase;
50 unsigned short intr;
51 unsigned short quit;
52 unsigned short eof;
53} edchars;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070054
Elliott Hughes77740fc2016-08-12 15:06:53 -070055#define isched(x,e) ((unsigned short)(unsigned char)(x) == (e))
56#define isedchar(x) (!((x) & ~0xFF))
57#ifndef _POSIX_VDISABLE
58#define toedchar(x) ((unsigned short)(unsigned char)(x))
59#else
60#define toedchar(x) (((_POSIX_VDISABLE != -1) && ((x) == _POSIX_VDISABLE)) ? \
61 ((unsigned short)EDCHAR_DISABLED) : \
62 ((unsigned short)(unsigned char)(x)))
63#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070064
Geremy Condra03ebf062011-10-12 18:17:24 -070065/* x_cf_glob() flags */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070066#define XCF_COMMAND BIT(0) /* Do command completion */
67#define XCF_FILE BIT(1) /* Do file completion */
68#define XCF_FULLPATH BIT(2) /* command completion: store full path */
Geremy Condra03ebf062011-10-12 18:17:24 -070069#define XCF_COMMAND_FILE (XCF_COMMAND | XCF_FILE)
70#define XCF_IS_COMMAND BIT(3) /* return flag: is command */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000071#define XCF_IS_NOSPACE BIT(4) /* return flag: do not append a space */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070072
73static char editmode;
74static int xx_cols; /* for Emacs mode */
75static int modified; /* buffer has been "modified" */
Thorsten Glaser811a5752013-07-25 14:24:45 +000076static char *holdbufp; /* place to hold last edit buffer */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070077
Elliott Hughes77740fc2016-08-12 15:06:53 -070078/* 0=dumb 1=tmux (for now) */
Elliott Hughes23925bb2017-09-22 16:04:20 -070079static uint8_t x_term_mode;
Elliott Hughes77740fc2016-08-12 15:06:53 -070080
81static void x_adjust(void);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070082static int x_getc(void);
83static void x_putcf(int);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000084static void x_modified(void);
Geremy Condra03ebf062011-10-12 18:17:24 -070085static void x_mode(bool);
86static int x_do_comment(char *, ssize_t, ssize_t *);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000087static void x_print_expansions(int, char * const *, bool);
Geremy Condra03ebf062011-10-12 18:17:24 -070088static int x_cf_glob(int *, const char *, int, int, int *, int *, char ***);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000089static size_t x_longest_prefix(int, char * const *);
90static void x_glob_hlp_add_qchar(char *);
91static char *x_glob_hlp_tilde_and_rem_qchar(char *, bool);
Elliott Hughes77740fc2016-08-12 15:06:53 -070092static size_t x_basename(const char *, const char *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070093static void x_free_words(int, char **);
94static int x_escape(const char *, size_t, int (*)(const char *, size_t));
Thorsten Glaser811a5752013-07-25 14:24:45 +000095static int x_emacs(char *);
Elliott Hughes737fdce2014-08-07 12:59:26 -070096static void x_init_prompt(bool);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070097#if !MKSH_S_NOVI
Thorsten Glaser811a5752013-07-25 14:24:45 +000098static int x_vi(char *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070099#endif
Elliott Hughes23925bb2017-09-22 16:04:20 -0700100static void x_intr(int, int) MKSH_A_NORETURN;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700101
102#define x_flush() shf_flush(shl_out)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000103#if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700104#define x_putc(c) x_putcf(c)
105#else
106#define x_putc(c) shf_putc((c), shl_out)
107#endif
108
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000109static int path_order_cmp(const void *, const void *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700110static void glob_table(const char *, XPtrV *, struct table *);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000111static void glob_path(int, const char *, XPtrV *, const char *);
112static int x_file_glob(int *, char *, char ***);
Geremy Condra03ebf062011-10-12 18:17:24 -0700113static int x_command_glob(int, char *, char ***);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700114static int x_locate_word(const char *, int, int, int *, bool *);
115
116static int x_e_getmbc(char *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700117
118/* +++ generic editing functions +++ */
119
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700120/*
121 * read an edited command line
122 */
123int
Thorsten Glaser811a5752013-07-25 14:24:45 +0000124x_read(char *buf)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700125{
126 int i;
127
128 x_mode(true);
129 modified = 1;
130 if (Flag(FEMACS) || Flag(FGMACS))
Thorsten Glaser811a5752013-07-25 14:24:45 +0000131 i = x_emacs(buf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700132#if !MKSH_S_NOVI
133 else if (Flag(FVI))
Thorsten Glaser811a5752013-07-25 14:24:45 +0000134 i = x_vi(buf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700135#endif
136 else
Geremy Condra03ebf062011-10-12 18:17:24 -0700137 /* internal error */
138 i = -1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700139 editmode = 0;
140 x_mode(false);
141 return (i);
142}
143
144/* tty I/O */
145
146static int
147x_getc(void)
148{
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700149#ifdef __OS2__
150 return (_read_kbd(0, 1, 0));
151#else
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700152 char c;
Geremy Condra03ebf062011-10-12 18:17:24 -0700153 ssize_t n;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700154
155 while ((n = blocking_read(STDIN_FILENO, &c, 1)) < 0 && errno == EINTR)
156 if (trap) {
157 x_mode(false);
158 runtraps(0);
159#ifdef SIGWINCH
160 if (got_winch) {
161 change_winsz();
162 if (x_cols != xx_cols && editmode == 1) {
163 /* redraw line in Emacs mode */
164 xx_cols = x_cols;
Elliott Hughes737fdce2014-08-07 12:59:26 -0700165 x_init_prompt(false);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700166 x_adjust();
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700167 }
168 }
169#endif
170 x_mode(true);
171 }
172 return ((n == 1) ? (int)(unsigned char)c : -1);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700173#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700174}
175
176static void
177x_putcf(int c)
178{
179 shf_putc(c, shl_out);
180}
181
182/*********************************
183 * Misc common code for vi/emacs *
184 *********************************/
185
Geremy Condra03ebf062011-10-12 18:17:24 -0700186/*-
187 * Handle the commenting/uncommenting of a line.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700188 * Returns:
189 * 1 if a carriage return is indicated (comment added)
190 * 0 if no return (comment removed)
191 * -1 if there is an error (not enough room for comment chars)
192 * If successful, *lenp contains the new length. Note: cursor should be
193 * moved to the start of the line after (un)commenting.
194 */
195static int
Geremy Condra03ebf062011-10-12 18:17:24 -0700196x_do_comment(char *buf, ssize_t bsize, ssize_t *lenp)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700197{
Geremy Condra03ebf062011-10-12 18:17:24 -0700198 ssize_t i, j, len = *lenp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700199
200 if (len == 0)
Geremy Condra03ebf062011-10-12 18:17:24 -0700201 /* somewhat arbitrary - it's what AT&T ksh does */
202 return (1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700203
204 /* Already commented? */
205 if (buf[0] == '#') {
206 bool saw_nl = false;
207
208 for (j = 0, i = 1; i < len; i++) {
209 if (!saw_nl || buf[i] != '#')
210 buf[j++] = buf[i];
211 saw_nl = buf[i] == '\n';
212 }
213 *lenp = j;
214 return (0);
215 } else {
216 int n = 1;
217
218 /* See if there's room for the #s - 1 per \n */
219 for (i = 0; i < len; i++)
220 if (buf[i] == '\n')
221 n++;
222 if (len + n >= bsize)
223 return (-1);
224 /* Now add them... */
225 for (i = len, j = len + n; --i >= 0; ) {
226 if (buf[i] == '\n')
227 buf[--j] = '#';
228 buf[--j] = buf[i];
229 }
230 buf[0] = '#';
231 *lenp += n;
232 return (1);
233 }
234}
235
236/****************************************************
237 * Common file/command completion code for vi/emacs *
238 ****************************************************/
239
240static void
241x_print_expansions(int nwords, char * const *words, bool is_command)
242{
243 bool use_copy = false;
Elliott Hughes77740fc2016-08-12 15:06:53 -0700244 size_t prefix_len;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000245 XPtrV l = { NULL, 0, 0 };
Elliott Hughes966dd552016-12-08 15:56:04 -0800246 struct columnise_opts co;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700247
Geremy Condra03ebf062011-10-12 18:17:24 -0700248 /*
249 * Check if all matches are in the same directory (in this
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700250 * case, we want to omit the directory name)
251 */
252 if (!is_command &&
253 (prefix_len = x_longest_prefix(nwords, words)) > 0) {
254 int i;
255
256 /* Special case for 1 match (prefix is whole word) */
257 if (nwords == 1)
258 prefix_len = x_basename(words[0], NULL);
259 /* Any (non-trailing) slashes in non-common word suffixes? */
260 for (i = 0; i < nwords; i++)
261 if (x_basename(words[i] + prefix_len, NULL) >
262 prefix_len)
263 break;
264 /* All in same directory? */
265 if (i == nwords) {
Elliott Hughes966dd552016-12-08 15:56:04 -0800266 while (prefix_len > 0 &&
267 !mksh_cdirsep(words[0][prefix_len - 1]))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700268 prefix_len--;
269 use_copy = true;
270 XPinit(l, nwords + 1);
271 for (i = 0; i < nwords; i++)
272 XPput(l, words[i] + prefix_len);
273 XPput(l, NULL);
274 }
275 }
276 /*
277 * Enumerate expansions
278 */
279 x_putc('\r');
280 x_putc('\n');
Elliott Hughes966dd552016-12-08 15:56:04 -0800281 co.shf = shl_out;
282 co.linesep = '\n';
283 co.do_last = true;
284 co.prefcol = false;
285 pr_list(&co, use_copy ? (char **)XPptrv(l) : words);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700286
287 if (use_copy)
Geremy Condra03ebf062011-10-12 18:17:24 -0700288 /* not x_free_words() */
289 XPfree(l);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700290}
291
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000292/*
293 * Convert backslash-escaped string to QCHAR-escaped
294 * string useful for globbing; loses QCHAR unless it
295 * can squeeze in, eg. by previous loss of backslash
296 */
297static void
298x_glob_hlp_add_qchar(char *cp)
299{
300 char ch, *dp = cp;
301 bool escaping = false;
302
303 while ((ch = *cp++)) {
304 if (ch == '\\' && !escaping) {
305 escaping = true;
306 continue;
307 }
308 if (escaping || (ch == QCHAR && (cp - dp) > 1)) {
309 /*
310 * empirically made list of chars to escape
311 * for globbing as well as QCHAR itself
312 */
Elliott Hughes23925bb2017-09-22 16:04:20 -0700313 switch (ord(ch)) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000314 case QCHAR:
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800315 case ORD('$'):
316 case ORD('*'):
317 case ORD('?'):
318 case ORD('['):
319 case ORD('\\'):
320 case ORD('`'):
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000321 *dp++ = QCHAR;
322 break;
323 }
324 escaping = false;
325 }
326 *dp++ = ch;
327 }
328 *dp = '\0';
329}
330
331/*
332 * Run tilde expansion on argument string, return the result
333 * after unescaping; if the flag is set, the original string
334 * is freed if changed and assumed backslash-escaped, if not
335 * it is assumed QCHAR-escaped
336 */
337static char *
338x_glob_hlp_tilde_and_rem_qchar(char *s, bool magic_flag)
339{
340 char ch, *cp, *dp;
341
342 /*
343 * On the string, check whether we have a tilde expansion,
344 * and if so, discern "~foo/bar" and "~/baz" from "~blah";
345 * if we have a directory part (the former), try to expand
346 */
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700347 if (*s == '~' && (cp = /* not sdirsep */ strchr(s, '/')) != NULL) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000348 /* ok, so split into "~foo"/"bar" or "~"/"baz" */
349 *cp++ = 0;
350 /* try to expand the tilde */
Elliott Hughes50012062015-03-10 22:22:24 -0700351 if (!(dp = do_tilde(s + 1))) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000352 /* nope, revert damage */
353 *--cp = '/';
354 } else {
355 /* ok, expand and replace */
Elliott Hughes77740fc2016-08-12 15:06:53 -0700356 cp = shf_smprintf(Tf_sSs, dp, cp);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000357 if (magic_flag)
358 afree(s, ATEMP);
359 s = cp;
360 }
361 }
362
363 /* ... convert it from backslash-escaped via QCHAR-escaped... */
364 if (magic_flag)
365 x_glob_hlp_add_qchar(s);
366 /* ... to unescaped, for comparison with the matches */
367 cp = dp = s;
368
369 while ((ch = *cp++)) {
370 if (ch == QCHAR && !(ch = *cp++))
371 break;
372 *dp++ = ch;
373 }
374 *dp = '\0';
375
376 return (s);
377}
378
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700379/**
380 * Do file globbing:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700381 * - does expansion, checks for no match, etc.
382 * - sets *wordsp to array of matching strings
383 * - returns number of matching strings
384 */
385static int
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000386x_file_glob(int *flagsp, char *toglob, char ***wordsp)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700387{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000388 char **words, *cp;
389 int nwords;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700390 XPtrV w;
391 struct source *s, *sold;
392
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700393 /* remove all escaping backward slashes */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000394 x_glob_hlp_add_qchar(toglob);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700395
396 /*
397 * Convert "foo*" (toglob) to an array of strings (words)
398 */
399 sold = source;
400 s = pushs(SWSTR, ATEMP);
401 s->start = s->str = toglob;
402 source = s;
403 if (yylex(ONEWORD | LQCHAR) != LWORD) {
404 source = sold;
Elliott Hughes77740fc2016-08-12 15:06:53 -0700405 internal_warningf(Tfg_badsubst);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700406 return (0);
407 }
408 source = sold;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000409 afree(s, ATEMP);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700410 XPinit(w, 32);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000411 cp = yylval.cp;
412 while (*cp == CHAR || *cp == QCHAR)
413 cp += 2;
414 nwords = DOGLOB | DOTILDE | DOMARKDIRS;
415 if (*cp != EOS) {
416 /* probably a $FOO expansion */
417 *flagsp |= XCF_IS_NOSPACE;
418 /* this always results in at most one match */
419 nwords = 0;
420 }
421 expand(yylval.cp, &w, nwords);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700422 XPput(w, NULL);
423 words = (char **)XPclose(w);
424
425 for (nwords = 0; words[nwords]; nwords++)
426 ;
427 if (nwords == 1) {
428 struct stat statb;
429
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000430 /* Expand any tilde and drop all QCHAR for comparison */
431 toglob = x_glob_hlp_tilde_and_rem_qchar(toglob, false);
432
Geremy Condra03ebf062011-10-12 18:17:24 -0700433 /*
434 * Check if globbing failed (returned glob pattern),
435 * but be careful (e.g. toglob == "ab*" when the file
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700436 * "ab*" exists is not an error).
437 * Also, check for empty result - happens if we tried
438 * to glob something which evaluated to an empty
439 * string (e.g., "$FOO" when there is no FOO, etc).
440 */
441 if ((strcmp(words[0], toglob) == 0 &&
442 stat(words[0], &statb) < 0) ||
443 words[0][0] == '\0') {
444 x_free_words(nwords, words);
445 words = NULL;
446 nwords = 0;
447 }
448 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700449
450 if ((*wordsp = nwords ? words : NULL) == NULL && words != NULL)
451 x_free_words(nwords, words);
452
453 return (nwords);
454}
455
456/* Data structure used in x_command_glob() */
457struct path_order_info {
458 char *word;
Elliott Hughes77740fc2016-08-12 15:06:53 -0700459 size_t base;
460 size_t path_order;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700461};
462
463/* Compare routine used in x_command_glob() */
464static int
465path_order_cmp(const void *aa, const void *bb)
466{
467 const struct path_order_info *a = (const struct path_order_info *)aa;
468 const struct path_order_info *b = (const struct path_order_info *)bb;
469 int t;
470
Elliott Hughes23925bb2017-09-22 16:04:20 -0700471 if ((t = ascstrcmp(a->word + a->base, b->word + b->base)))
Elliott Hughes77740fc2016-08-12 15:06:53 -0700472 return (t);
473 if (a->path_order > b->path_order)
474 return (1);
475 if (a->path_order < b->path_order)
476 return (-1);
477 return (0);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700478}
479
480static int
Geremy Condra03ebf062011-10-12 18:17:24 -0700481x_command_glob(int flags, char *toglob, char ***wordsp)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700482{
Geremy Condra03ebf062011-10-12 18:17:24 -0700483 char *pat, *fpath;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000484 size_t nwords;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700485 XPtrV w;
486 struct block *l;
487
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700488 /* Convert "foo*" (toglob) to a pattern for future use */
489 pat = evalstr(toglob, DOPAT | DOTILDE);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700490
491 XPinit(w, 32);
492
493 glob_table(pat, &w, &keywords);
494 glob_table(pat, &w, &aliases);
495 glob_table(pat, &w, &builtins);
496 for (l = e->loc; l; l = l->next)
497 glob_table(pat, &w, &l->funs);
498
499 glob_path(flags, pat, &w, path);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700500 if ((fpath = str_val(global(TFPATH))) != null)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700501 glob_path(flags, pat, &w, fpath);
502
503 nwords = XPsize(w);
504
505 if (!nwords) {
506 *wordsp = NULL;
507 XPfree(w);
508 return (0);
509 }
510 /* Sort entries */
511 if (flags & XCF_FULLPATH) {
512 /* Sort by basename, then path order */
513 struct path_order_info *info, *last_info = NULL;
514 char **words = (char **)XPptrv(w);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000515 size_t i, path_order = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700516
517 info = (struct path_order_info *)
Geremy Condra03ebf062011-10-12 18:17:24 -0700518 alloc2(nwords, sizeof(struct path_order_info), ATEMP);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700519 for (i = 0; i < nwords; i++) {
520 info[i].word = words[i];
521 info[i].base = x_basename(words[i], NULL);
522 if (!last_info || info[i].base != last_info->base ||
523 strncmp(words[i], last_info->word, info[i].base) != 0) {
524 last_info = &info[i];
525 path_order++;
526 }
527 info[i].path_order = path_order;
528 }
529 qsort(info, nwords, sizeof(struct path_order_info),
530 path_order_cmp);
531 for (i = 0; i < nwords; i++)
532 words[i] = info[i].word;
533 afree(info, ATEMP);
534 } else {
535 /* Sort and remove duplicate entries */
536 char **words = (char **)XPptrv(w);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000537 size_t i, j;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700538
Elliott Hughes23925bb2017-09-22 16:04:20 -0700539 qsort(words, nwords, sizeof(void *), ascpstrcmp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700540 for (i = j = 0; i < nwords - 1; i++) {
541 if (strcmp(words[i], words[i + 1]))
542 words[j++] = words[i];
543 else
544 afree(words[i], ATEMP);
545 }
546 words[j++] = words[i];
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000547 w.len = nwords = j;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700548 }
549
550 XPput(w, NULL);
551 *wordsp = (char **)XPclose(w);
552
553 return (nwords);
554}
555
Elliott Hughes23925bb2017-09-22 16:04:20 -0700556#define IS_WORDC(c) (!ctype(c, C_EDNWC))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700557
558static int
559x_locate_word(const char *buf, int buflen, int pos, int *startp,
560 bool *is_commandp)
561{
562 int start, end;
563
564 /* Bad call? Probably should report error */
565 if (pos < 0 || pos > buflen) {
566 *startp = pos;
567 *is_commandp = false;
568 return (0);
569 }
570 /* The case where pos == buflen happens to take care of itself... */
571
572 start = pos;
Geremy Condra03ebf062011-10-12 18:17:24 -0700573 /*
574 * Keep going backwards to start of word (has effect of allowing
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700575 * one blank after the end of a word)
576 */
577 for (; (start > 0 && IS_WORDC(buf[start - 1])) ||
578 (start > 1 && buf[start - 2] == '\\'); start--)
579 ;
580 /* Go forwards to end of word */
581 for (end = start; end < buflen && IS_WORDC(buf[end]); end++) {
582 if (buf[end] == '\\' && (end + 1) < buflen)
583 end++;
584 }
585
586 if (is_commandp) {
587 bool iscmd;
588 int p = start - 1;
589
590 /* Figure out if this is a command */
Elliott Hughes23925bb2017-09-22 16:04:20 -0700591 while (p >= 0 && ctype(buf[p], C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700592 p--;
Elliott Hughes23925bb2017-09-22 16:04:20 -0700593 iscmd = p < 0 || ctype(buf[p], C_EDCMD);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700594 if (iscmd) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700595 /*
596 * If command has a /, path, etc. is not searched;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700597 * only current directory is searched which is just
598 * like file globbing.
599 */
600 for (p = start; p < end; p++)
Elliott Hughes966dd552016-12-08 15:56:04 -0800601 if (mksh_cdirsep(buf[p]))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700602 break;
603 iscmd = p == end;
604 }
605 *is_commandp = iscmd;
606 }
607 *startp = start;
608
609 return (end - start);
610}
611
612static int
Geremy Condra03ebf062011-10-12 18:17:24 -0700613x_cf_glob(int *flagsp, const char *buf, int buflen, int pos, int *startp,
614 int *endp, char ***wordsp)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700615{
Geremy Condra03ebf062011-10-12 18:17:24 -0700616 int len, nwords = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700617 char **words = NULL;
618 bool is_command;
619
620 len = x_locate_word(buf, buflen, pos, startp, &is_command);
Geremy Condra03ebf062011-10-12 18:17:24 -0700621 if (!((*flagsp) & XCF_COMMAND))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700622 is_command = false;
Geremy Condra03ebf062011-10-12 18:17:24 -0700623 /*
624 * Don't do command globing on zero length strings - it takes too
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700625 * long and isn't very useful. File globs are more likely to be
626 * useful, so allow these.
627 */
628 if (len == 0 && is_command)
629 return (0);
630
Geremy Condra03ebf062011-10-12 18:17:24 -0700631 if (len >= 0) {
632 char *toglob, *s;
Geremy Condra03ebf062011-10-12 18:17:24 -0700633
634 /*
635 * Given a string, copy it and possibly add a '*' to the end.
636 */
637
638 strndupx(toglob, buf + *startp, len + /* the '*' */ 1, ATEMP);
639 toglob[len] = '\0';
640
641 /*
642 * If the pathname contains a wildcard (an unquoted '*',
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000643 * '?', or '[') or an extglob, then it is globbed based
644 * on that value (i.e., without the appended '*'). Same
645 * for parameter substitutions (as in “cat $HOME/.ss↹”)
646 * without appending a trailing space (LP: #710539), as
647 * well as for “~foo” (but not “~foo/”).
Geremy Condra03ebf062011-10-12 18:17:24 -0700648 */
649 for (s = toglob; *s; s++) {
650 if (*s == '\\' && s[1])
651 s++;
Elliott Hughes23925bb2017-09-22 16:04:20 -0700652 else if (ctype(*s, C_QUEST | C_DOLAR) ||
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800653 ord(*s) == ORD('*') || ord(*s) == ORD('[') ||
Geremy Condra03ebf062011-10-12 18:17:24 -0700654 /* ?() *() +() @() !() but two already checked */
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800655 (ord(s[1]) == ORD('(' /*)*/) &&
656 (ord(*s) == ORD('+') || ord(*s) == ORD('@') ||
657 ord(*s) == ORD('!')))) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000658 /*
659 * just expand based on the extglob
660 * or parameter
661 */
662 goto dont_add_glob;
Geremy Condra03ebf062011-10-12 18:17:24 -0700663 }
664 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000665
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700666 if (*toglob == '~' && /* not vdirsep */ !vstrchr(toglob, '/')) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000667 /* neither for '~foo' (but '~foo/bar') */
668 *flagsp |= XCF_IS_NOSPACE;
669 goto dont_add_glob;
Geremy Condra03ebf062011-10-12 18:17:24 -0700670 }
671
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000672 /* append a glob */
673 toglob[len] = '*';
674 toglob[len + 1] = '\0';
675 dont_add_glob:
Geremy Condra03ebf062011-10-12 18:17:24 -0700676 /*
677 * Expand (glob) it now.
678 */
679
680 nwords = is_command ?
681 x_command_glob(*flagsp, toglob, &words) :
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000682 x_file_glob(flagsp, toglob, &words);
Geremy Condra03ebf062011-10-12 18:17:24 -0700683 afree(toglob, ATEMP);
684 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700685 if (nwords == 0) {
686 *wordsp = NULL;
687 return (0);
688 }
Geremy Condra03ebf062011-10-12 18:17:24 -0700689 if (is_command)
690 *flagsp |= XCF_IS_COMMAND;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700691 *wordsp = words;
692 *endp = *startp + len;
693
694 return (nwords);
695}
696
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700697/*
698 * Find longest common prefix
699 */
Geremy Condra03ebf062011-10-12 18:17:24 -0700700static size_t
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700701x_longest_prefix(int nwords, char * const * words)
702{
Geremy Condra03ebf062011-10-12 18:17:24 -0700703 int i;
704 size_t j, prefix_len;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700705 char *p;
706
707 if (nwords <= 0)
708 return (0);
709
710 prefix_len = strlen(words[0]);
711 for (i = 1; i < nwords; i++)
712 for (j = 0, p = words[i]; j < prefix_len; j++)
713 if (p[j] != words[0][j]) {
714 prefix_len = j;
715 break;
716 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000717 /* false for nwords==1 as 0 = words[0][prefix_len] then */
Elliott Hughes23925bb2017-09-22 16:04:20 -0700718 if (UTFMODE && prefix_len && (rtt2asc(words[0][prefix_len]) & 0xC0) == 0x80)
719 while (prefix_len && (rtt2asc(words[0][prefix_len]) & 0xC0) != 0xC0)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000720 --prefix_len;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700721 return (prefix_len);
722}
723
724static void
725x_free_words(int nwords, char **words)
726{
727 while (nwords)
728 afree(words[--nwords], ATEMP);
729 afree(words, ATEMP);
730}
731
Geremy Condra03ebf062011-10-12 18:17:24 -0700732/*-
733 * Return the offset of the basename of string s (which ends at se - need not
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700734 * be null terminated). Trailing slashes are ignored. If s is just a slash,
735 * then the offset is 0 (actually, length - 1).
736 * s Return
737 * /etc 1
738 * /etc/ 1
739 * /etc// 1
740 * /etc/fo 5
741 * foo 0
742 * /// 2
743 * 0
744 */
Elliott Hughes77740fc2016-08-12 15:06:53 -0700745static size_t
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700746x_basename(const char *s, const char *se)
747{
748 const char *p;
749
750 if (se == NULL)
Elliott Hughes23925bb2017-09-22 16:04:20 -0700751 se = strnul(s);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700752 if (s == se)
753 return (0);
754
Elliott Hughes966dd552016-12-08 15:56:04 -0800755 /* skip trailing directory separators */
756 p = se - 1;
757 while (p > s && mksh_cdirsep(*p))
758 --p;
759 /* drop last component */
760 while (p > s && !mksh_cdirsep(*p))
761 --p;
762 if (mksh_cdirsep(*p) && p + 1 < se)
763 ++p;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700764
765 return (p - s);
766}
767
768/*
769 * Apply pattern matching to a table: all table entries that match a pattern
770 * are added to wp.
771 */
772static void
773glob_table(const char *pat, XPtrV *wp, struct table *tp)
774{
775 struct tstate ts;
776 struct tbl *te;
777
778 ktwalk(&ts, tp);
779 while ((te = ktnext(&ts)))
780 if (gmatchx(te->name, pat, false)) {
781 char *cp;
782
783 strdupx(cp, te->name, ATEMP);
784 XPput(*wp, cp);
785 }
786}
787
788static void
789glob_path(int flags, const char *pat, XPtrV *wp, const char *lpath)
790{
Geremy Condra03ebf062011-10-12 18:17:24 -0700791 const char *sp = lpath, *p;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700792 char *xp, **words;
Geremy Condra03ebf062011-10-12 18:17:24 -0700793 size_t pathlen, patlen, oldsize, newsize, i, j;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700794 XString xs;
795
Geremy Condra03ebf062011-10-12 18:17:24 -0700796 patlen = strlen(pat);
797 checkoktoadd(patlen, 129 + X_EXTRA);
798 ++patlen;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700799 Xinit(xs, xp, patlen + 128, ATEMP);
800 while (sp) {
801 xp = Xstring(xs, xp);
Elliott Hughes96b43632015-07-17 11:39:41 -0700802 if (!(p = cstrchr(sp, MKSH_PATHSEPC)))
Elliott Hughes23925bb2017-09-22 16:04:20 -0700803 p = strnul(sp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700804 pathlen = p - sp;
805 if (pathlen) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700806 /*
807 * Copy sp into xp, stuffing any MAGIC characters
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700808 * on the way
809 */
810 const char *s = sp;
811
812 XcheckN(xs, xp, pathlen * 2);
813 while (s < p) {
814 if (ISMAGIC(*s))
815 *xp++ = MAGIC;
816 *xp++ = *s++;
817 }
818 *xp++ = '/';
819 pathlen++;
820 }
821 sp = p;
822 XcheckN(xs, xp, patlen);
823 memcpy(xp, pat, patlen);
824
825 oldsize = XPsize(*wp);
Geremy Condra03ebf062011-10-12 18:17:24 -0700826 /* mark dirs */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000827 glob_str(Xstring(xs, xp), wp, true);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700828 newsize = XPsize(*wp);
829
830 /* Check that each match is executable... */
831 words = (char **)XPptrv(*wp);
832 for (i = j = oldsize; i < newsize; i++) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700833 if (ksh_access(words[i], X_OK) == 0) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700834 words[j] = words[i];
835 if (!(flags & XCF_FULLPATH))
836 memmove(words[j], words[j] + pathlen,
837 strlen(words[j] + pathlen) + 1);
838 j++;
839 } else
840 afree(words[i], ATEMP);
841 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000842 wp->len = j;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700843
844 if (!*sp++)
845 break;
846 }
847 Xfree(xs, xp);
848}
849
850/*
851 * if argument string contains any special characters, they will
852 * be escaped and the result will be put into edit buffer by
853 * keybinding-specific function
854 */
855static int
856x_escape(const char *s, size_t len, int (*putbuf_func)(const char *, size_t))
857{
858 size_t add = 0, wlen = len;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700859 int rval = 0;
860
861 while (wlen - add > 0)
Elliott Hughes23925bb2017-09-22 16:04:20 -0700862 if (ctype(s[add], C_IFS | C_EDQ)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700863 if (putbuf_func(s, add) != 0) {
864 rval = -1;
865 break;
866 }
867 putbuf_func(s[add] == '\n' ? "'" : "\\", 1);
868 putbuf_func(&s[add], 1);
869 if (s[add] == '\n')
870 putbuf_func("'", 1);
871
872 add++;
873 wlen -= add;
874 s += add;
875 add = 0;
876 } else
877 ++add;
878 if (wlen > 0 && rval == 0)
879 rval = putbuf_func(s, wlen);
880
881 return (rval);
882}
883
884
885/* +++ emacs editing mode +++ */
886
887static Area aedit;
888#define AEDIT &aedit /* area for kill ring and macro defns */
889
890/* values returned by keyboard functions */
891#define KSTD 0
892#define KEOL 1 /* ^M, ^J */
893#define KINTR 2 /* ^G, ^C */
894
895struct x_ftab {
896 int (*xf_func)(int c);
897 const char *xf_name;
898 short xf_flags;
899};
900
901struct x_defbindings {
902 unsigned char xdb_func; /* XFUNC_* */
903 unsigned char xdb_tab;
904 unsigned char xdb_char;
905};
906
907#define XF_ARG 1 /* command takes number prefix */
908#define XF_NOBIND 2 /* not allowed to bind to function */
909#define XF_PREFIX 4 /* function sets prefix */
910
Elliott Hughes96b43632015-07-17 11:39:41 -0700911#define X_NTABS 4 /* normal, meta1, meta2, pc */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700912#define X_TABSZ 256 /* size of keydef tables etc */
913
Geremy Condra03ebf062011-10-12 18:17:24 -0700914/*-
915 * Arguments for do_complete()
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700916 * 0 = enumerate M-= complete as much as possible and then list
917 * 1 = complete M-Esc
918 * 2 = list M-?
919 */
920typedef enum {
921 CT_LIST, /* list the possible completions */
922 CT_COMPLETE, /* complete to longest prefix */
923 CT_COMPLIST /* complete and then list (if non-exact) */
924} Comp_type;
925
926/*
927 * The following are used for my horizontal scrolling stuff
928 */
929static char *xbuf; /* beg input buffer */
930static char *xend; /* end input buffer */
931static char *xcp; /* current position */
932static char *xep; /* current end */
933static char *xbp; /* start of visible portion of input buffer */
934static char *xlp; /* last char visible on screen */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000935static bool x_adj_ok;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700936/*
937 * we use x_adj_done so that functions can tell
938 * whether x_adjust() has been called while they are active.
939 */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000940static int x_adj_done; /* is incremented by x_adjust() */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700941
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700942static int x_displen;
943static int x_arg; /* general purpose arg */
Geremy Condra03ebf062011-10-12 18:17:24 -0700944static bool x_arg_defaulted; /* x_arg not explicitly set; defaulted to 1 */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700945
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000946static bool xlp_valid; /* lastvis pointer was recalculated */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700947
948static char **x_histp; /* history position */
949static int x_nextcmd; /* for newline-and-next */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000950static char **x_histncp; /* saved x_histp for " */
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700951static char **x_histmcp; /* saved x_histp for " */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700952static char *xmp; /* mark pointer */
953static unsigned char x_last_command;
954static unsigned char (*x_tab)[X_TABSZ]; /* key definition */
955#ifndef MKSH_SMALL
956static char *(*x_atab)[X_TABSZ]; /* macro definitions */
957#endif
958static unsigned char x_bound[(X_TABSZ * X_NTABS + 7) / 8];
959#define KILLSIZE 20
960static char *killstack[KILLSIZE];
961static int killsp, killtp;
962static int x_curprefix;
963#ifndef MKSH_SMALL
Geremy Condra03ebf062011-10-12 18:17:24 -0700964static char *macroptr; /* bind key macro active? */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700965#endif
966#if !MKSH_S_NOVI
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700967static int winwidth; /* width of window */
968static char *wbuf[2]; /* window buffers */
969static int wbuf_len; /* length of window buffers (x_cols - 3) */
970static int win; /* window buffer in use */
971static char morec; /* more character at right of window */
972static int lastref; /* argument to last refresh() */
973static int holdlen; /* length of holdbuf */
974#endif
Thorsten Glaser811a5752013-07-25 14:24:45 +0000975static int pwidth; /* width of prompt */
976static int prompt_trunc; /* how much of prompt to truncate or -1 */
977static int x_col; /* current column on line */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700978
979static int x_ins(const char *);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000980static void x_delete(size_t, bool);
981static size_t x_bword(void);
982static size_t x_fword(bool);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700983static void x_goto(char *);
Elliott Hughes737fdce2014-08-07 12:59:26 -0700984static char *x_bs0(char *, char *) MKSH_A_PURE;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700985static void x_bs3(char **);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700986static int x_size2(char *, char **);
987static void x_zots(char *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700988static void x_zotc3(char **);
Elliott Hughes23925bb2017-09-22 16:04:20 -0700989static void x_vi_zotc(int);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700990static void x_load_hist(char **);
991static int x_search(char *, int, int);
992#ifndef MKSH_SMALL
993static int x_search_dir(int);
994#endif
995static int x_match(char *, char *);
996static void x_redraw(int);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700997static void x_push(size_t);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000998static char *x_mapin(const char *, Area *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700999static char *x_mapout(int);
1000static void x_mapout2(int, char **);
1001static void x_print(int, int);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001002static void x_e_ungetc(int);
1003static int x_e_getc(void);
1004static void x_e_putc2(int);
1005static void x_e_putc3(const char **);
1006static void x_e_puts(const char *);
1007#ifndef MKSH_SMALL
1008static int x_fold_case(int);
1009#endif
1010static char *x_lastcp(void);
Elliott Hughes77740fc2016-08-12 15:06:53 -07001011static void x_lastpos(void);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001012static void do_complete(int, Comp_type);
Elliott Hughes737fdce2014-08-07 12:59:26 -07001013static size_t x_nb2nc(size_t) MKSH_A_PURE;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001014
1015static int unget_char = -1;
1016
1017static int x_do_ins(const char *, size_t);
1018static void bind_if_not_bound(int, int, int);
1019
1020enum emacs_funcs {
1021#define EMACSFN_ENUMS
1022#include "emacsfn.h"
1023 XFUNC_MAX
1024};
1025
1026#define EMACSFN_DEFNS
1027#include "emacsfn.h"
1028
1029static const struct x_ftab x_ftab[] = {
1030#define EMACSFN_ITEMS
1031#include "emacsfn.h"
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001032};
1033
1034static struct x_defbindings const x_defbindings[] = {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001035 { XFUNC_del_back, 0, CTRL_QM },
1036 { XFUNC_del_bword, 1, CTRL_QM },
1037 { XFUNC_eot_del, 0, CTRL_D },
1038 { XFUNC_del_back, 0, CTRL_H },
1039 { XFUNC_del_bword, 1, CTRL_H },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001040 { XFUNC_del_bword, 1, 'h' },
1041 { XFUNC_mv_bword, 1, 'b' },
1042 { XFUNC_mv_fword, 1, 'f' },
1043 { XFUNC_del_fword, 1, 'd' },
Elliott Hughes23925bb2017-09-22 16:04:20 -07001044 { XFUNC_mv_back, 0, CTRL_B },
1045 { XFUNC_mv_forw, 0, CTRL_F },
1046 { XFUNC_search_char_forw, 0, CTRL_BC },
1047 { XFUNC_search_char_back, 1, CTRL_BC },
1048 { XFUNC_newline, 0, CTRL_M },
1049 { XFUNC_newline, 0, CTRL_J },
1050 { XFUNC_end_of_text, 0, CTRL_US },
1051 { XFUNC_abort, 0, CTRL_G },
1052 { XFUNC_prev_com, 0, CTRL_P },
1053 { XFUNC_next_com, 0, CTRL_N },
1054 { XFUNC_nl_next_com, 0, CTRL_O },
1055 { XFUNC_search_hist, 0, CTRL_R },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001056 { XFUNC_beg_hist, 1, '<' },
1057 { XFUNC_end_hist, 1, '>' },
1058 { XFUNC_goto_hist, 1, 'g' },
Elliott Hughes23925bb2017-09-22 16:04:20 -07001059 { XFUNC_mv_end, 0, CTRL_E },
1060 { XFUNC_mv_beg, 0, CTRL_A },
1061 { XFUNC_draw_line, 0, CTRL_L },
1062 { XFUNC_cls, 1, CTRL_L },
1063 { XFUNC_meta1, 0, CTRL_BO },
1064 { XFUNC_meta2, 0, CTRL_X },
1065 { XFUNC_kill, 0, CTRL_K },
1066 { XFUNC_yank, 0, CTRL_Y },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001067 { XFUNC_meta_yank, 1, 'y' },
Elliott Hughes23925bb2017-09-22 16:04:20 -07001068 { XFUNC_literal, 0, CTRL_CA },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001069 { XFUNC_comment, 1, '#' },
Elliott Hughes23925bb2017-09-22 16:04:20 -07001070 { XFUNC_transpose, 0, CTRL_T },
1071 { XFUNC_complete, 1, CTRL_BO },
1072 { XFUNC_comp_list, 0, CTRL_I },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001073 { XFUNC_comp_list, 1, '=' },
1074 { XFUNC_enumerate, 1, '?' },
1075 { XFUNC_expand, 1, '*' },
Elliott Hughes23925bb2017-09-22 16:04:20 -07001076 { XFUNC_comp_file, 1, CTRL_X },
1077 { XFUNC_comp_comm, 2, CTRL_BO },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001078 { XFUNC_list_comm, 2, '?' },
Elliott Hughes23925bb2017-09-22 16:04:20 -07001079 { XFUNC_list_file, 2, CTRL_Y },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001080 { XFUNC_set_mark, 1, ' ' },
Elliott Hughes23925bb2017-09-22 16:04:20 -07001081 { XFUNC_kill_region, 0, CTRL_W },
1082 { XFUNC_xchg_point_mark, 2, CTRL_X },
1083 { XFUNC_literal, 0, CTRL_V },
1084 { XFUNC_version, 1, CTRL_V },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001085 { XFUNC_prev_histword, 1, '.' },
1086 { XFUNC_prev_histword, 1, '_' },
1087 { XFUNC_set_arg, 1, '0' },
1088 { XFUNC_set_arg, 1, '1' },
1089 { XFUNC_set_arg, 1, '2' },
1090 { XFUNC_set_arg, 1, '3' },
1091 { XFUNC_set_arg, 1, '4' },
1092 { XFUNC_set_arg, 1, '5' },
1093 { XFUNC_set_arg, 1, '6' },
1094 { XFUNC_set_arg, 1, '7' },
1095 { XFUNC_set_arg, 1, '8' },
1096 { XFUNC_set_arg, 1, '9' },
1097#ifndef MKSH_SMALL
1098 { XFUNC_fold_upper, 1, 'U' },
1099 { XFUNC_fold_upper, 1, 'u' },
1100 { XFUNC_fold_lower, 1, 'L' },
1101 { XFUNC_fold_lower, 1, 'l' },
1102 { XFUNC_fold_capitalise, 1, 'C' },
1103 { XFUNC_fold_capitalise, 1, 'c' },
1104#endif
Geremy Condra03ebf062011-10-12 18:17:24 -07001105 /*
1106 * These for ANSI arrow keys: arguablely shouldn't be here by
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001107 * default, but its simpler/faster/smaller than using termcap
1108 * entries.
1109 */
1110 { XFUNC_meta2, 1, '[' },
1111 { XFUNC_meta2, 1, 'O' },
1112 { XFUNC_prev_com, 2, 'A' },
1113 { XFUNC_next_com, 2, 'B' },
1114 { XFUNC_mv_forw, 2, 'C' },
1115 { XFUNC_mv_back, 2, 'D' },
1116#ifndef MKSH_SMALL
1117 { XFUNC_vt_hack, 2, '1' },
Elliott Hughes966dd552016-12-08 15:56:04 -08001118 { XFUNC_mv_beg | 0x80, 2, '7' },
1119 { XFUNC_mv_beg, 2, 'H' },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001120 { XFUNC_mv_end | 0x80, 2, '4' },
1121 { XFUNC_mv_end | 0x80, 2, '8' },
1122 { XFUNC_mv_end, 2, 'F' },
1123 { XFUNC_del_char | 0x80, 2, '3' },
Elliott Hughes96b43632015-07-17 11:39:41 -07001124 { XFUNC_del_char, 2, 'P' },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001125 { XFUNC_search_hist_up | 0x80, 2, '5' },
1126 { XFUNC_search_hist_dn | 0x80, 2, '6' },
Elliott Hughes96b43632015-07-17 11:39:41 -07001127#endif
1128 /* PC scancodes */
1129#if !defined(MKSH_SMALL) || defined(__OS2__)
1130 { XFUNC_meta3, 0, 0 },
Elliott Hughes966dd552016-12-08 15:56:04 -08001131 { XFUNC_mv_beg, 3, 71 },
Elliott Hughes96b43632015-07-17 11:39:41 -07001132 { XFUNC_prev_com, 3, 72 },
1133#ifndef MKSH_SMALL
1134 { XFUNC_search_hist_up, 3, 73 },
1135#endif
1136 { XFUNC_mv_back, 3, 75 },
1137 { XFUNC_mv_forw, 3, 77 },
1138 { XFUNC_mv_end, 3, 79 },
1139 { XFUNC_next_com, 3, 80 },
1140#ifndef MKSH_SMALL
1141 { XFUNC_search_hist_dn, 3, 81 },
1142#endif
1143 { XFUNC_del_char, 3, 83 },
1144#endif
1145#ifndef MKSH_SMALL
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001146 /* more non-standard ones */
Elliott Hughes23925bb2017-09-22 16:04:20 -07001147 { XFUNC_eval_region, 1, CTRL_E },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001148 { XFUNC_edit_line, 2, 'e' }
1149#endif
1150};
1151
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001152static size_t
1153x_nb2nc(size_t nb)
1154{
1155 char *cp;
1156 size_t nc = 0;
1157
1158 for (cp = xcp; cp < (xcp + nb); ++nc)
1159 cp += utf_ptradj(cp);
1160 return (nc);
1161}
1162
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001163static void
1164x_modified(void)
1165{
1166 if (!modified) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001167 x_histmcp = x_histp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001168 x_histp = histptr + 1;
1169 modified = 1;
1170 }
1171}
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001172
1173#ifdef MKSH_SMALL
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001174#define XFUNC_VALUE(f) (f)
1175#else
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001176#define XFUNC_VALUE(f) (f & 0x7F)
1177#endif
1178
1179static int
1180x_e_getmbc(char *sbuf)
1181{
1182 int c, pos = 0;
1183 unsigned char *buf = (unsigned char *)sbuf;
1184
1185 memset(buf, 0, 4);
1186 buf[pos++] = c = x_e_getc();
1187 if (c == -1)
1188 return (-1);
1189 if (UTFMODE) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001190 if ((rtt2asc(buf[0]) >= (unsigned char)0xC2) &&
1191 (rtt2asc(buf[0]) < (unsigned char)0xF0)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001192 c = x_e_getc();
1193 if (c == -1)
1194 return (-1);
Elliott Hughes23925bb2017-09-22 16:04:20 -07001195 if ((rtt2asc(c) & 0xC0) != 0x80) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001196 x_e_ungetc(c);
1197 return (1);
1198 }
1199 buf[pos++] = c;
1200 }
Elliott Hughes23925bb2017-09-22 16:04:20 -07001201 if ((rtt2asc(buf[0]) >= (unsigned char)0xE0) &&
1202 (rtt2asc(buf[0]) < (unsigned char)0xF0)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001203 /* XXX x_e_ungetc is one-octet only */
1204 buf[pos++] = c = x_e_getc();
1205 if (c == -1)
1206 return (-1);
1207 }
1208 }
1209 return (pos);
1210}
1211
Elliott Hughes77740fc2016-08-12 15:06:53 -07001212/*
1213 * minimum required space to work with on a line - if the prompt
1214 * leaves less space than this on a line, the prompt is truncated
1215 */
1216#define MIN_EDIT_SPACE 7
1217
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001218static void
Elliott Hughes737fdce2014-08-07 12:59:26 -07001219x_init_prompt(bool doprint)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001220{
Elliott Hughes737fdce2014-08-07 12:59:26 -07001221 prompt_trunc = pprompt(prompt, doprint ? 0 : -1);
Thorsten Glaser811a5752013-07-25 14:24:45 +00001222 pwidth = prompt_trunc % x_cols;
1223 prompt_trunc -= pwidth;
1224 if ((mksh_uari_t)pwidth > ((mksh_uari_t)x_cols - 3 - MIN_EDIT_SPACE)) {
1225 /* force newline after prompt */
1226 prompt_trunc = -1;
1227 pwidth = 0;
Elliott Hughes737fdce2014-08-07 12:59:26 -07001228 if (doprint)
1229 x_e_putc2('\n');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001230 }
1231}
1232
1233static int
Thorsten Glaser811a5752013-07-25 14:24:45 +00001234x_emacs(char *buf)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001235{
1236 int c, i;
1237 unsigned char f;
1238
Thorsten Glaser811a5752013-07-25 14:24:45 +00001239 xbp = xbuf = buf;
1240 xend = buf + LINE;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001241 xlp = xcp = xep = buf;
1242 *xcp = 0;
1243 xlp_valid = true;
1244 xmp = NULL;
1245 x_curprefix = 0;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001246 x_histmcp = x_histp = histptr + 1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001247 x_last_command = XFUNC_error;
1248
Elliott Hughes737fdce2014-08-07 12:59:26 -07001249 x_init_prompt(true);
Thorsten Glaser811a5752013-07-25 14:24:45 +00001250 x_displen = (xx_cols = x_cols) - 2 - (x_col = pwidth);
1251 x_adj_done = 0;
1252 x_adj_ok = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001253
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001254 x_histncp = NULL;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001255 if (x_nextcmd >= 0) {
1256 int off = source->line - x_nextcmd;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001257 if (histptr - history >= off) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001258 x_load_hist(histptr - off);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001259 x_histncp = x_histp;
1260 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001261 x_nextcmd = -1;
1262 }
1263 editmode = 1;
Geremy Condra03ebf062011-10-12 18:17:24 -07001264 while (/* CONSTCOND */ 1) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001265 x_flush();
1266 if ((c = x_e_getc()) < 0)
1267 return (0);
1268
1269 f = x_curprefix == -1 ? XFUNC_insert :
1270 x_tab[x_curprefix][c];
1271#ifndef MKSH_SMALL
1272 if (f & 0x80) {
1273 f &= 0x7F;
1274 if ((i = x_e_getc()) != '~')
1275 x_e_ungetc(i);
1276 }
1277
1278 /* avoid bind key macro recursion */
1279 if (macroptr && f == XFUNC_ins_string)
1280 f = XFUNC_insert;
1281#endif
1282
1283 if (!(x_ftab[f].xf_flags & XF_PREFIX) &&
1284 x_last_command != XFUNC_set_arg) {
1285 x_arg = 1;
Geremy Condra03ebf062011-10-12 18:17:24 -07001286 x_arg_defaulted = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001287 }
1288 i = c | (x_curprefix << 8);
1289 x_curprefix = 0;
1290 switch ((*x_ftab[f].xf_func)(i)) {
1291 case KSTD:
1292 if (!(x_ftab[f].xf_flags & XF_PREFIX))
1293 x_last_command = f;
1294 break;
1295 case KEOL:
1296 i = xep - xbuf;
1297 return (i);
Geremy Condra03ebf062011-10-12 18:17:24 -07001298 case KINTR:
1299 /* special case for interrupt */
Elliott Hughes23925bb2017-09-22 16:04:20 -07001300 x_intr(SIGINT, c);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001301 }
1302 /* ad-hoc hack for fixing the cursor position */
1303 x_goto(xcp);
1304 }
1305}
1306
1307static int
1308x_insert(int c)
1309{
Geremy Condra03ebf062011-10-12 18:17:24 -07001310 static int left, pos, save_arg;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001311 static char str[4];
1312
1313 /*
1314 * Should allow tab and control chars.
1315 */
1316 if (c == 0) {
1317 invmbs:
1318 left = 0;
Elliott Hughes23925bb2017-09-22 16:04:20 -07001319 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001320 return (KSTD);
1321 }
1322 if (UTFMODE) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001323 if (((rtt2asc(c) & 0xC0) == 0x80) && left) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001324 str[pos++] = c;
1325 if (!--left) {
1326 str[pos] = '\0';
1327 x_arg = save_arg;
1328 while (x_arg--)
1329 x_ins(str);
1330 }
1331 return (KSTD);
1332 }
1333 if (left) {
1334 if (x_curprefix == -1) {
1335 /* flush invalid multibyte */
1336 str[pos] = '\0';
1337 while (save_arg--)
1338 x_ins(str);
1339 }
1340 }
1341 if ((c >= 0xC2) && (c < 0xE0))
1342 left = 1;
1343 else if ((c >= 0xE0) && (c < 0xF0))
1344 left = 2;
1345 else if (c > 0x7F)
1346 goto invmbs;
1347 else
1348 left = 0;
1349 if (left) {
1350 save_arg = x_arg;
1351 pos = 1;
1352 str[0] = c;
1353 return (KSTD);
1354 }
1355 }
1356 left = 0;
1357 str[0] = c;
1358 str[1] = '\0';
1359 while (x_arg--)
1360 x_ins(str);
1361 return (KSTD);
1362}
1363
1364#ifndef MKSH_SMALL
1365static int
1366x_ins_string(int c)
1367{
1368 macroptr = x_atab[c >> 8][c & 255];
1369 /*
1370 * we no longer need to bother checking if macroptr is
1371 * not NULL but first char is NUL; x_e_getc() does it
1372 */
1373 return (KSTD);
1374}
1375#endif
1376
1377static int
1378x_do_ins(const char *cp, size_t len)
1379{
1380 if (xep + len >= xend) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001381 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001382 return (-1);
1383 }
1384 memmove(xcp + len, xcp, xep - xcp + 1);
1385 memmove(xcp, cp, len);
1386 xcp += len;
1387 xep += len;
1388 x_modified();
1389 return (0);
1390}
1391
1392static int
1393x_ins(const char *s)
1394{
1395 char *cp = xcp;
1396 int adj = x_adj_done;
1397
1398 if (x_do_ins(s, strlen(s)) < 0)
1399 return (-1);
1400 /*
1401 * x_zots() may result in a call to x_adjust()
1402 * we want xcp to reflect the new position.
1403 */
1404 xlp_valid = false;
1405 x_lastcp();
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001406 x_adj_ok = tobool(xcp >= xlp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001407 x_zots(cp);
Elliott Hughes77740fc2016-08-12 15:06:53 -07001408 if (adj == x_adj_done)
1409 /* x_adjust() has not been called */
1410 x_lastpos();
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001411 x_adj_ok = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001412 return (0);
1413}
1414
1415static int
1416x_del_back(int c MKSH_A_UNUSED)
1417{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001418 ssize_t i = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001419
1420 if (xcp == xbuf) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001421 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001422 return (KSTD);
1423 }
1424 do {
1425 x_goto(xcp - 1);
1426 } while ((++i < x_arg) && (xcp != xbuf));
1427 x_delete(i, false);
1428 return (KSTD);
1429}
1430
1431static int
1432x_del_char(int c MKSH_A_UNUSED)
1433{
1434 char *cp, *cp2;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001435 size_t i = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001436
1437 cp = xcp;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001438 while (i < (size_t)x_arg) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001439 utf_ptradjx(cp, cp2);
1440 if (cp2 > xep)
1441 break;
1442 cp = cp2;
1443 i++;
1444 }
1445
1446 if (!i) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001447 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001448 return (KSTD);
1449 }
1450 x_delete(i, false);
1451 return (KSTD);
1452}
1453
1454/* Delete nc chars to the right of the cursor (including cursor position) */
1455static void
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001456x_delete(size_t nc, bool push)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001457{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001458 size_t i, nb, nw;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001459 char *cp;
1460
1461 if (nc == 0)
1462 return;
1463
1464 nw = 0;
1465 cp = xcp;
1466 for (i = 0; i < nc; ++i) {
1467 char *cp2;
1468 int j;
1469
1470 j = x_size2(cp, &cp2);
1471 if (cp2 > xep)
1472 break;
1473 cp = cp2;
1474 nw += j;
1475 }
1476 nb = cp - xcp;
1477 /* nc = i; */
1478
1479 if (xmp != NULL && xmp > xcp) {
1480 if (xcp + nb > xmp)
1481 xmp = xcp;
1482 else
1483 xmp -= nb;
1484 }
1485 /*
1486 * This lets us yank a word we have deleted.
1487 */
1488 if (push)
1489 x_push(nb);
1490
1491 xep -= nb;
Geremy Condra03ebf062011-10-12 18:17:24 -07001492 /* Copies the NUL */
1493 memmove(xcp, xcp + nb, xep - xcp + 1);
1494 /* don't redraw */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001495 x_adj_ok = false;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001496 xlp_valid = false;
1497 x_zots(xcp);
1498 /*
1499 * if we are already filling the line,
Geremy Condra03ebf062011-10-12 18:17:24 -07001500 * there is no need to ' ', '\b'.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001501 * But if we must, make sure we do the minimum.
1502 */
1503 if ((i = xx_cols - 2 - x_col) > 0 || xep - xlp == 0) {
1504 nw = i = (nw < i) ? nw : i;
1505 while (i--)
1506 x_e_putc2(' ');
1507 if (x_col == xx_cols - 2) {
1508 x_e_putc2((xep > xlp) ? '>' : (xbp > xbuf) ? '<' : ' ');
1509 ++nw;
1510 }
1511 while (nw--)
1512 x_e_putc2('\b');
1513 }
1514 /*x_goto(xcp);*/
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001515 x_adj_ok = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001516 xlp_valid = false;
Elliott Hughes77740fc2016-08-12 15:06:53 -07001517 x_lastpos();
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001518 x_modified();
1519 return;
1520}
1521
1522static int
1523x_del_bword(int c MKSH_A_UNUSED)
1524{
1525 x_delete(x_bword(), true);
1526 return (KSTD);
1527}
1528
1529static int
1530x_mv_bword(int c MKSH_A_UNUSED)
1531{
1532 x_bword();
1533 return (KSTD);
1534}
1535
1536static int
1537x_mv_fword(int c MKSH_A_UNUSED)
1538{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001539 x_fword(true);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001540 return (KSTD);
1541}
1542
1543static int
1544x_del_fword(int c MKSH_A_UNUSED)
1545{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001546 x_delete(x_fword(false), true);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001547 return (KSTD);
1548}
1549
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001550static size_t
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001551x_bword(void)
1552{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001553 size_t nb = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001554 char *cp = xcp;
1555
1556 if (cp == xbuf) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001557 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001558 return (0);
1559 }
1560 while (x_arg--) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001561 while (cp != xbuf && ctype(cp[-1], C_MFS)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001562 cp--;
1563 nb++;
1564 }
Elliott Hughes23925bb2017-09-22 16:04:20 -07001565 while (cp != xbuf && !ctype(cp[-1], C_MFS)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001566 cp--;
1567 nb++;
1568 }
1569 }
1570 x_goto(cp);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001571 return (x_nb2nc(nb));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001572}
1573
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001574static size_t
1575x_fword(bool move)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001576{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001577 size_t nc;
1578 char *cp = xcp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001579
1580 if (cp == xep) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001581 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001582 return (0);
1583 }
1584 while (x_arg--) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001585 while (cp != xep && ctype(*cp, C_MFS))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001586 cp++;
Elliott Hughes23925bb2017-09-22 16:04:20 -07001587 while (cp != xep && !ctype(*cp, C_MFS))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001588 cp++;
1589 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001590 nc = x_nb2nc(cp - xcp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001591 if (move)
1592 x_goto(cp);
1593 return (nc);
1594}
1595
1596static void
1597x_goto(char *cp)
1598{
Thorsten Glaser811a5752013-07-25 14:24:45 +00001599 cp = cp >= xep ? xep : x_bs0(cp, xbuf);
Elliott Hughes77740fc2016-08-12 15:06:53 -07001600 if (cp < xbp || cp >= utf_skipcols(xbp, x_displen, NULL)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001601 /* we are heading off screen */
1602 xcp = cp;
1603 x_adjust();
Geremy Condra03ebf062011-10-12 18:17:24 -07001604 } else if (cp < xcp) {
1605 /* move back */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001606 while (cp < xcp)
1607 x_bs3(&xcp);
Geremy Condra03ebf062011-10-12 18:17:24 -07001608 } else if (cp > xcp) {
1609 /* move forward */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001610 while (cp > xcp)
1611 x_zotc3(&xcp);
1612 }
1613}
1614
Thorsten Glaser811a5752013-07-25 14:24:45 +00001615static char *
1616x_bs0(char *cp, char *lower_bound)
1617{
1618 if (UTFMODE)
1619 while ((!lower_bound || (cp > lower_bound)) &&
Elliott Hughes23925bb2017-09-22 16:04:20 -07001620 ((rtt2asc(*cp) & 0xC0) == 0x80))
Thorsten Glaser811a5752013-07-25 14:24:45 +00001621 --cp;
1622 return (cp);
1623}
1624
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001625static void
1626x_bs3(char **p)
1627{
1628 int i;
1629
Thorsten Glaser811a5752013-07-25 14:24:45 +00001630 *p = x_bs0((*p) - 1, NULL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001631 i = x_size2(*p, NULL);
1632 while (i--)
1633 x_e_putc2('\b');
1634}
1635
1636static int
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001637x_size2(char *cp, char **dcp)
1638{
Elliott Hughes737fdce2014-08-07 12:59:26 -07001639 uint8_t c = *(unsigned char *)cp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001640
Elliott Hughes23925bb2017-09-22 16:04:20 -07001641 if (UTFMODE && (rtt2asc(c) > 0x7F))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001642 return (utf_widthadj(cp, (const char **)dcp));
1643 if (dcp)
1644 *dcp = cp + 1;
1645 if (c == '\t')
Geremy Condra03ebf062011-10-12 18:17:24 -07001646 /* Kludge, tabs are always four spaces. */
1647 return (4);
Elliott Hughes23925bb2017-09-22 16:04:20 -07001648 if (ksh_isctrl(c))
Geremy Condra03ebf062011-10-12 18:17:24 -07001649 /* control unsigned char */
1650 return (2);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001651 return (1);
1652}
1653
1654static void
1655x_zots(char *str)
1656{
1657 int adj = x_adj_done;
1658
1659 x_lastcp();
Elliott Hughes56b517d2014-10-06 11:30:44 -07001660 while (*str && str < xlp && x_col < xx_cols && adj == x_adj_done)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001661 x_zotc3(&str);
1662}
1663
1664static void
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001665x_zotc3(char **cp)
1666{
1667 unsigned char c = **(unsigned char **)cp;
1668
1669 if (c == '\t') {
1670 /* Kludge, tabs are always four spaces. */
Elliott Hughes77740fc2016-08-12 15:06:53 -07001671 x_e_puts(T4spaces);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001672 (*cp)++;
Elliott Hughes23925bb2017-09-22 16:04:20 -07001673 } else if (ksh_isctrl(c)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001674 x_e_putc2('^');
Elliott Hughes23925bb2017-09-22 16:04:20 -07001675 x_e_putc2(ksh_unctrl(c));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001676 (*cp)++;
1677 } else
1678 x_e_putc3((const char **)cp);
1679}
1680
1681static int
1682x_mv_back(int c MKSH_A_UNUSED)
1683{
1684 if (xcp == xbuf) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001685 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001686 return (KSTD);
1687 }
1688 while (x_arg--) {
1689 x_goto(xcp - 1);
1690 if (xcp == xbuf)
1691 break;
1692 }
1693 return (KSTD);
1694}
1695
1696static int
1697x_mv_forw(int c MKSH_A_UNUSED)
1698{
1699 char *cp = xcp, *cp2;
1700
1701 if (xcp == xep) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001702 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001703 return (KSTD);
1704 }
1705 while (x_arg--) {
1706 utf_ptradjx(cp, cp2);
1707 if (cp2 > xep)
1708 break;
1709 cp = cp2;
1710 }
1711 x_goto(cp);
1712 return (KSTD);
1713}
1714
1715static int
1716x_search_char_forw(int c MKSH_A_UNUSED)
1717{
1718 char *cp = xcp;
1719 char tmp[4];
1720
1721 *xep = '\0';
1722 if (x_e_getmbc(tmp) < 0) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001723 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001724 return (KSTD);
1725 }
1726 while (x_arg--) {
1727 if ((cp = (cp == xep) ? NULL : strstr(cp + 1, tmp)) == NULL &&
1728 (cp = strstr(xbuf, tmp)) == NULL) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001729 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001730 return (KSTD);
1731 }
1732 }
1733 x_goto(cp);
1734 return (KSTD);
1735}
1736
1737static int
1738x_search_char_back(int c MKSH_A_UNUSED)
1739{
1740 char *cp = xcp, *p, tmp[4];
1741 bool b;
1742
1743 if (x_e_getmbc(tmp) < 0) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001744 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001745 return (KSTD);
1746 }
1747 for (; x_arg--; cp = p)
1748 for (p = cp; ; ) {
1749 if (p-- == xbuf)
1750 p = xep;
1751 if (p == cp) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001752 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001753 return (KSTD);
1754 }
1755 if ((tmp[1] && ((p+1) > xep)) ||
1756 (tmp[2] && ((p+2) > xep)))
1757 continue;
1758 b = true;
1759 if (*p != tmp[0])
1760 b = false;
1761 if (b && tmp[1] && p[1] != tmp[1])
1762 b = false;
1763 if (b && tmp[2] && p[2] != tmp[2])
1764 b = false;
1765 if (b)
1766 break;
1767 }
1768 x_goto(cp);
1769 return (KSTD);
1770}
1771
1772static int
1773x_newline(int c MKSH_A_UNUSED)
1774{
1775 x_e_putc2('\r');
1776 x_e_putc2('\n');
1777 x_flush();
1778 *xep++ = '\n';
1779 return (KEOL);
1780}
1781
1782static int
1783x_end_of_text(int c MKSH_A_UNUSED)
1784{
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001785 unsigned char tmp[1], *cp = tmp;
Elliott Hughes737fdce2014-08-07 12:59:26 -07001786
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001787 *tmp = isedchar(edchars.eof) ? (unsigned char)edchars.eof :
Elliott Hughes23925bb2017-09-22 16:04:20 -07001788 (unsigned char)CTRL_D;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001789 x_zotc3((char **)&cp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001790 x_putc('\r');
1791 x_putc('\n');
1792 x_flush();
1793 return (KEOL);
1794}
1795
1796static int
1797x_beg_hist(int c MKSH_A_UNUSED)
1798{
1799 x_load_hist(history);
1800 return (KSTD);
1801}
1802
1803static int
1804x_end_hist(int c MKSH_A_UNUSED)
1805{
1806 x_load_hist(histptr);
1807 return (KSTD);
1808}
1809
1810static int
1811x_prev_com(int c MKSH_A_UNUSED)
1812{
1813 x_load_hist(x_histp - x_arg);
1814 return (KSTD);
1815}
1816
1817static int
1818x_next_com(int c MKSH_A_UNUSED)
1819{
1820 x_load_hist(x_histp + x_arg);
1821 return (KSTD);
1822}
1823
Geremy Condra03ebf062011-10-12 18:17:24 -07001824/*
1825 * Goto a particular history number obtained from argument.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001826 * If no argument is given history 1 is probably not what you
1827 * want so we'll simply go to the oldest one.
1828 */
1829static int
1830x_goto_hist(int c MKSH_A_UNUSED)
1831{
1832 if (x_arg_defaulted)
1833 x_load_hist(history);
1834 else
1835 x_load_hist(histptr + x_arg - source->line);
1836 return (KSTD);
1837}
1838
1839static void
1840x_load_hist(char **hp)
1841{
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001842 char *sp = NULL;
1843
1844 if (hp == histptr + 1) {
Thorsten Glaser811a5752013-07-25 14:24:45 +00001845 sp = holdbufp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001846 modified = 0;
1847 } else if (hp < history || hp > histptr) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001848 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001849 return;
1850 }
1851 if (sp == NULL)
1852 sp = *hp;
1853 x_histp = hp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001854 if (modified)
Thorsten Glaser811a5752013-07-25 14:24:45 +00001855 strlcpy(holdbufp, xbuf, LINE);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001856 strlcpy(xbuf, sp, xend - xbuf);
1857 xbp = xbuf;
Elliott Hughes23925bb2017-09-22 16:04:20 -07001858 xep = xcp = strnul(xbuf);
Elliott Hughes77740fc2016-08-12 15:06:53 -07001859 x_adjust();
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001860 modified = 0;
1861}
1862
1863static int
1864x_nl_next_com(int c MKSH_A_UNUSED)
1865{
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001866 if (!modified)
1867 x_histmcp = x_histp;
1868 if (!x_histncp || (x_histmcp != x_histncp && x_histmcp != histptr + 1))
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001869 /* fresh start of ^O */
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001870 x_histncp = x_histmcp;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001871 x_nextcmd = source->line - (histptr - x_histncp) + 1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001872 return (x_newline('\n'));
1873}
1874
1875static int
1876x_eot_del(int c)
1877{
1878 if (xep == xbuf && x_arg_defaulted)
1879 return (x_end_of_text(c));
1880 else
1881 return (x_del_char(c));
1882}
1883
1884/* reverse incremental history search */
1885static int
1886x_search_hist(int c)
1887{
1888 int offset = -1; /* offset of match in xbuf, else -1 */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001889 char pat[80 + 1]; /* pattern buffer */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001890 char *p = pat;
1891 unsigned char f;
1892
1893 *p = '\0';
Geremy Condra03ebf062011-10-12 18:17:24 -07001894 while (/* CONSTCOND */ 1) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001895 if (offset < 0) {
1896 x_e_puts("\nI-search: ");
1897 x_e_puts(pat);
1898 }
1899 x_flush();
1900 if ((c = x_e_getc()) < 0)
1901 return (KSTD);
1902 f = x_tab[0][c];
Elliott Hughes23925bb2017-09-22 16:04:20 -07001903 if (c == CTRL_BO) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001904 if ((f & 0x7F) == XFUNC_meta1) {
1905 if ((c = x_e_getc()) < 0)
1906 return (KSTD);
1907 f = x_tab[1][c] & 0x7F;
1908 if (f == XFUNC_meta1 || f == XFUNC_meta2)
Elliott Hughes23925bb2017-09-22 16:04:20 -07001909 x_meta1(CTRL_BO);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001910 x_e_ungetc(c);
1911 }
1912 break;
1913 }
1914#ifndef MKSH_SMALL
1915 if (f & 0x80) {
1916 f &= 0x7F;
1917 if ((c = x_e_getc()) != '~')
1918 x_e_ungetc(c);
1919 }
1920#endif
1921 if (f == XFUNC_search_hist)
1922 offset = x_search(pat, 0, offset);
1923 else if (f == XFUNC_del_back) {
1924 if (p == pat) {
1925 offset = -1;
1926 break;
1927 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001928 if (p > pat) {
1929 p = x_bs0(p - 1, pat);
1930 *p = '\0';
1931 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001932 if (p == pat)
1933 offset = -1;
1934 else
1935 offset = x_search(pat, 1, offset);
1936 continue;
1937 } else if (f == XFUNC_insert) {
1938 /* add char to pattern */
1939 /* overflow check... */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001940 if ((size_t)(p - pat) >= sizeof(pat) - 1) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07001941 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001942 continue;
1943 }
1944 *p++ = c, *p = '\0';
1945 if (offset >= 0) {
1946 /* already have partial match */
1947 offset = x_match(xbuf, pat);
1948 if (offset >= 0) {
1949 x_goto(xbuf + offset + (p - pat) -
1950 (*pat == '^'));
1951 continue;
1952 }
1953 }
1954 offset = x_search(pat, 0, offset);
1955 } else if (f == XFUNC_abort) {
1956 if (offset >= 0)
1957 x_load_hist(histptr + 1);
1958 break;
Geremy Condra03ebf062011-10-12 18:17:24 -07001959 } else {
1960 /* other command */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001961 x_e_ungetc(c);
1962 break;
1963 }
1964 }
1965 if (offset < 0)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001966 x_redraw('\n');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001967 return (KSTD);
1968}
1969
1970/* search backward from current line */
1971static int
1972x_search(char *pat, int sameline, int offset)
1973{
1974 char **hp;
1975 int i;
1976
1977 for (hp = x_histp - (sameline ? 0 : 1); hp >= history; --hp) {
1978 i = x_match(*hp, pat);
1979 if (i >= 0) {
1980 if (offset < 0)
1981 x_e_putc2('\n');
1982 x_load_hist(hp);
1983 x_goto(xbuf + i + strlen(pat) - (*pat == '^'));
1984 return (i);
1985 }
1986 }
Elliott Hughes23925bb2017-09-22 16:04:20 -07001987 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001988 x_histp = histptr;
1989 return (-1);
1990}
1991
1992#ifndef MKSH_SMALL
1993/* anchored search up from current line */
1994static int
1995x_search_hist_up(int c MKSH_A_UNUSED)
1996{
1997 return (x_search_dir(-1));
1998}
1999
2000/* anchored search down from current line */
2001static int
2002x_search_hist_dn(int c MKSH_A_UNUSED)
2003{
2004 return (x_search_dir(1));
2005}
2006
2007/* anchored search in the indicated direction */
2008static int
2009x_search_dir(int search_dir /* should've been bool */)
2010{
2011 char **hp = x_histp + search_dir;
2012 size_t curs = xcp - xbuf;
2013
2014 while (histptr >= hp && hp >= history) {
2015 if (strncmp(xbuf, *hp, curs) == 0) {
2016 x_load_hist(hp);
2017 x_goto(xbuf + curs);
2018 break;
2019 }
2020 hp += search_dir;
2021 }
2022 return (KSTD);
2023}
2024#endif
2025
2026/* return position of first match of pattern in string, else -1 */
2027static int
2028x_match(char *str, char *pat)
2029{
2030 if (*pat == '^') {
2031 return ((strncmp(str, pat + 1, strlen(pat + 1)) == 0) ? 0 : -1);
2032 } else {
2033 char *q = strstr(str, pat);
2034 return ((q == NULL) ? -1 : q - str);
2035 }
2036}
2037
2038static int
2039x_del_line(int c MKSH_A_UNUSED)
2040{
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002041 *xep = 0;
Elliott Hughes77740fc2016-08-12 15:06:53 -07002042 x_push(xep - (xcp = xbuf));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002043 xlp = xbp = xep = xbuf;
2044 xlp_valid = true;
2045 *xcp = 0;
2046 xmp = NULL;
Elliott Hughes77740fc2016-08-12 15:06:53 -07002047 x_redraw('\r');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002048 x_modified();
2049 return (KSTD);
2050}
2051
2052static int
2053x_mv_end(int c MKSH_A_UNUSED)
2054{
2055 x_goto(xep);
2056 return (KSTD);
2057}
2058
2059static int
Elliott Hughes966dd552016-12-08 15:56:04 -08002060x_mv_beg(int c MKSH_A_UNUSED)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002061{
2062 x_goto(xbuf);
2063 return (KSTD);
2064}
2065
2066static int
2067x_draw_line(int c MKSH_A_UNUSED)
2068{
Elliott Hughes77740fc2016-08-12 15:06:53 -07002069 x_redraw('\n');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002070 return (KSTD);
2071}
2072
2073static int
2074x_cls(int c MKSH_A_UNUSED)
2075{
Elliott Hughes77740fc2016-08-12 15:06:53 -07002076 shf_puts(MKSH_CLS_STRING, shl_out);
2077 x_redraw(0);
2078 return (KSTD);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002079}
2080
Geremy Condra03ebf062011-10-12 18:17:24 -07002081/*
Elliott Hughes77740fc2016-08-12 15:06:53 -07002082 * clear line from x_col (current cursor position) to xx_cols - 2,
2083 * then output lastch, then go back to x_col; if lastch is space,
2084 * clear with termcap instead of spaces, or not if line_was_cleared;
2085 * lastch MUST be an ASCII character with wcwidth(lastch) == 1
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002086 */
2087static void
Elliott Hughes77740fc2016-08-12 15:06:53 -07002088x_clrtoeol(int lastch, bool line_was_cleared)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002089{
Elliott Hughes77740fc2016-08-12 15:06:53 -07002090 int col;
2091
2092 if (lastch == ' ' && !line_was_cleared && x_term_mode == 1) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002093 shf_puts(KSH_ESC_STRING "[K", shl_out);
Elliott Hughes77740fc2016-08-12 15:06:53 -07002094 line_was_cleared = true;
2095 }
2096 if (lastch == ' ' && line_was_cleared)
2097 return;
2098
2099 col = x_col;
2100 while (col < (xx_cols - 2)) {
2101 x_putc(' ');
2102 ++col;
2103 }
2104 x_putc(lastch);
2105 ++col;
2106 while (col > x_col) {
2107 x_putc('\b');
2108 --col;
2109 }
2110}
2111
2112/* output the prompt, assuming a line has just been started */
2113static void
2114x_pprompt(void)
2115{
2116 if (prompt_trunc != -1)
2117 pprompt(prompt, prompt_trunc);
2118 x_col = pwidth;
2119}
2120
2121/* output CR, then redraw the line, clearing to EOL if needed (cr ≠ 0, LF) */
2122static void
2123x_redraw(int cr)
2124{
2125 int lch;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002126
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002127 x_adj_ok = false;
Elliott Hughes77740fc2016-08-12 15:06:53 -07002128 /* clear the line */
2129 x_e_putc2(cr ? cr : '\r');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002130 x_flush();
Elliott Hughes77740fc2016-08-12 15:06:53 -07002131 /* display the prompt */
2132 if (xbp == xbuf)
2133 x_pprompt();
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002134 x_displen = xx_cols - 2 - x_col;
Elliott Hughes77740fc2016-08-12 15:06:53 -07002135 /* display the line content */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002136 xlp_valid = false;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002137 x_zots(xbp);
Elliott Hughes77740fc2016-08-12 15:06:53 -07002138 /* check whether there is more off-screen */
2139 lch = xep > xlp ? (xbp > xbuf ? '*' : '>') : (xbp > xbuf) ? '<' : ' ';
2140 /* clear the rest of the line */
2141 x_clrtoeol(lch, !cr || cr == '\n');
2142 /* go back to actual cursor position */
2143 x_lastpos();
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002144 x_adj_ok = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002145}
2146
2147static int
2148x_transpose(int c MKSH_A_UNUSED)
2149{
2150 unsigned int tmpa, tmpb;
2151
Geremy Condra03ebf062011-10-12 18:17:24 -07002152 /*-
2153 * What transpose is meant to do seems to be up for debate. This
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002154 * is a general summary of the options; the text is abcd with the
2155 * upper case character or underscore indicating the cursor position:
2156 * Who Before After Before After
2157 * AT&T ksh in emacs mode: abCd abdC abcd_ (bell)
2158 * AT&T ksh in gmacs mode: abCd baCd abcd_ abdc_
2159 * gnu emacs: abCd acbD abcd_ abdc_
2160 * Pdksh currently goes with GNU behavior since I believe this is the
2161 * most common version of emacs, unless in gmacs mode, in which case
2162 * it does the AT&T ksh gmacs mode.
2163 * This should really be broken up into 3 functions so users can bind
2164 * to the one they want.
2165 */
2166 if (xcp == xbuf) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002167 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002168 return (KSTD);
2169 } else if (xcp == xep || Flag(FGMACS)) {
2170 if (xcp - xbuf == 1) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002171 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002172 return (KSTD);
2173 }
Geremy Condra03ebf062011-10-12 18:17:24 -07002174 /*
2175 * Gosling/Unipress emacs style: Swap two characters before
2176 * the cursor, do not change cursor position
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002177 */
2178 x_bs3(&xcp);
2179 if (utf_mbtowc(&tmpa, xcp) == (size_t)-1) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002180 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002181 return (KSTD);
2182 }
2183 x_bs3(&xcp);
2184 if (utf_mbtowc(&tmpb, xcp) == (size_t)-1) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002185 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002186 return (KSTD);
2187 }
2188 utf_wctomb(xcp, tmpa);
2189 x_zotc3(&xcp);
2190 utf_wctomb(xcp, tmpb);
2191 x_zotc3(&xcp);
2192 } else {
Geremy Condra03ebf062011-10-12 18:17:24 -07002193 /*
2194 * GNU emacs style: Swap the characters before and under the
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002195 * cursor, move cursor position along one.
2196 */
2197 if (utf_mbtowc(&tmpa, xcp) == (size_t)-1) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002198 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002199 return (KSTD);
2200 }
2201 x_bs3(&xcp);
2202 if (utf_mbtowc(&tmpb, xcp) == (size_t)-1) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002203 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002204 return (KSTD);
2205 }
2206 utf_wctomb(xcp, tmpa);
2207 x_zotc3(&xcp);
2208 utf_wctomb(xcp, tmpb);
2209 x_zotc3(&xcp);
2210 }
2211 x_modified();
2212 return (KSTD);
2213}
2214
2215static int
2216x_literal(int c MKSH_A_UNUSED)
2217{
2218 x_curprefix = -1;
2219 return (KSTD);
2220}
2221
2222static int
2223x_meta1(int c MKSH_A_UNUSED)
2224{
2225 x_curprefix = 1;
2226 return (KSTD);
2227}
2228
2229static int
2230x_meta2(int c MKSH_A_UNUSED)
2231{
2232 x_curprefix = 2;
2233 return (KSTD);
2234}
2235
2236static int
Elliott Hughes96b43632015-07-17 11:39:41 -07002237x_meta3(int c MKSH_A_UNUSED)
2238{
2239 x_curprefix = 3;
2240 return (KSTD);
2241}
2242
2243static int
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002244x_kill(int c MKSH_A_UNUSED)
2245{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002246 size_t col = xcp - xbuf;
2247 size_t lastcol = xep - xbuf;
2248 size_t ndel, narg;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002249
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002250 if (x_arg_defaulted || (narg = x_arg) > lastcol)
2251 narg = lastcol;
2252 if (narg < col) {
2253 x_goto(xbuf + narg);
2254 ndel = col - narg;
2255 } else
2256 ndel = narg - col;
2257 x_delete(x_nb2nc(ndel), true);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002258 return (KSTD);
2259}
2260
2261static void
Elliott Hughes77740fc2016-08-12 15:06:53 -07002262x_push(size_t nchars)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002263{
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002264 afree(killstack[killsp], AEDIT);
2265 strndupx(killstack[killsp], xcp, nchars, AEDIT);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002266 killsp = (killsp + 1) % KILLSIZE;
2267}
2268
2269static int
2270x_yank(int c MKSH_A_UNUSED)
2271{
2272 if (killsp == 0)
2273 killtp = KILLSIZE;
2274 else
2275 killtp = killsp;
2276 killtp--;
2277 if (killstack[killtp] == 0) {
2278 x_e_puts("\nnothing to yank");
Elliott Hughes77740fc2016-08-12 15:06:53 -07002279 x_redraw('\n');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002280 return (KSTD);
2281 }
2282 xmp = xcp;
2283 x_ins(killstack[killtp]);
2284 return (KSTD);
2285}
2286
2287static int
2288x_meta_yank(int c MKSH_A_UNUSED)
2289{
Geremy Condra03ebf062011-10-12 18:17:24 -07002290 size_t len;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002291
2292 if ((x_last_command != XFUNC_yank && x_last_command != XFUNC_meta_yank) ||
2293 killstack[killtp] == 0) {
2294 killtp = killsp;
2295 x_e_puts("\nyank something first");
Elliott Hughes77740fc2016-08-12 15:06:53 -07002296 x_redraw('\n');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002297 return (KSTD);
2298 }
2299 len = strlen(killstack[killtp]);
2300 x_goto(xcp - len);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002301 x_delete(x_nb2nc(len), false);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002302 do {
2303 if (killtp == 0)
2304 killtp = KILLSIZE - 1;
2305 else
2306 killtp--;
2307 } while (killstack[killtp] == 0);
2308 x_ins(killstack[killtp]);
2309 return (KSTD);
2310}
2311
Elliott Hughes23925bb2017-09-22 16:04:20 -07002312/* fake receiving an interrupt */
2313static void
2314x_intr(int signo, int c)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002315{
Elliott Hughes23925bb2017-09-22 16:04:20 -07002316 x_vi_zotc(c);
2317 *xep = '\0';
2318 strip_nuls(xbuf, xep - xbuf);
2319 if (*xbuf)
2320 histsave(&source->line, xbuf, HIST_STORE, true);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002321 xlp = xep = xcp = xbp = xbuf;
2322 xlp_valid = true;
2323 *xcp = 0;
2324 x_modified();
Elliott Hughes23925bb2017-09-22 16:04:20 -07002325 x_flush();
2326 trapsig(signo);
2327 x_mode(false);
2328 unwind(LSHELL);
2329}
2330
2331static int
2332x_abort(int c MKSH_A_UNUSED)
2333{
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002334 return (KINTR);
2335}
2336
2337static int
2338x_error(int c MKSH_A_UNUSED)
2339{
Elliott Hughes23925bb2017-09-22 16:04:20 -07002340 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002341 return (KSTD);
2342}
2343
2344#ifndef MKSH_SMALL
2345/* special VT100 style key sequence hack */
2346static int
2347x_vt_hack(int c)
2348{
2349 /* we only support PF2-'1' for now */
2350 if (c != (2 << 8 | '1'))
2351 return (x_error(c));
2352
2353 /* what's the next character? */
2354 switch ((c = x_e_getc())) {
2355 case '~':
2356 x_arg = 1;
Geremy Condra03ebf062011-10-12 18:17:24 -07002357 x_arg_defaulted = true;
Elliott Hughes966dd552016-12-08 15:56:04 -08002358 return (x_mv_beg(0));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002359 case ';':
2360 /* "interesting" sequence detected */
2361 break;
2362 default:
2363 goto unwind_err;
2364 }
2365
2366 /* XXX x_e_ungetc is one-octet only */
2367 if ((c = x_e_getc()) != '5' && c != '3')
2368 goto unwind_err;
2369
2370 /*-
2371 * At this point, we have read the following octets so far:
Geremy Condra03ebf062011-10-12 18:17:24 -07002372 * - ESC+[ or ESC+O or Ctrl-X (Prefix 2)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002373 * - 1 (vt_hack)
2374 * - ;
2375 * - 5 (Ctrl key combiner) or 3 (Alt key combiner)
2376 * We can now accept one more octet designating the key.
2377 */
2378
2379 switch ((c = x_e_getc())) {
2380 case 'C':
2381 return (x_mv_fword(c));
2382 case 'D':
2383 return (x_mv_bword(c));
2384 }
2385
2386 unwind_err:
2387 x_e_ungetc(c);
2388 return (x_error(c));
2389}
2390#endif
2391
2392static char *
2393x_mapin(const char *cp, Area *ap)
2394{
2395 char *news, *op;
2396
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002397 strdupx(news, cp, ap);
2398 op = news;
2399 while (*cp) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002400 switch (*cp) {
2401 case '^':
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002402 cp++;
Elliott Hughes23925bb2017-09-22 16:04:20 -07002403 *op++ = ksh_toctrl(*cp);
2404 break;
2405 case '\\':
2406 if (cp[1] == '\\' || cp[1] == '^')
2407 ++cp;
2408 /* FALLTHROUGH */
2409 default:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002410 *op++ = *cp;
Elliott Hughes23925bb2017-09-22 16:04:20 -07002411 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002412 cp++;
2413 }
2414 *op = '\0';
2415
2416 return (news);
2417}
2418
2419static void
2420x_mapout2(int c, char **buf)
2421{
2422 char *p = *buf;
2423
Elliott Hughes23925bb2017-09-22 16:04:20 -07002424 if (ksh_isctrl(c)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002425 *p++ = '^';
Elliott Hughes23925bb2017-09-22 16:04:20 -07002426 *p++ = ksh_unctrl(c);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002427 } else
2428 *p++ = c;
2429 *p = 0;
2430 *buf = p;
2431}
2432
2433static char *
2434x_mapout(int c)
2435{
2436 static char buf[8];
2437 char *bp = buf;
2438
2439 x_mapout2(c, &bp);
2440 return (buf);
2441}
2442
2443static void
2444x_print(int prefix, int key)
2445{
2446 int f = x_tab[prefix][key];
2447
2448 if (prefix)
Elliott Hughes23925bb2017-09-22 16:04:20 -07002449 /* prefix == 1 || prefix == 2 || prefix == 3 */
2450 shf_puts(x_mapout(prefix == 1 ? CTRL_BO :
2451 prefix == 2 ? CTRL_X : 0), shl_stdout);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002452#ifdef MKSH_SMALL
2453 shprintf("%s = ", x_mapout(key));
2454#else
2455 shprintf("%s%s = ", x_mapout(key), (f & 0x80) ? "~" : "");
2456 if (XFUNC_VALUE(f) != XFUNC_ins_string)
2457#endif
Elliott Hughes77740fc2016-08-12 15:06:53 -07002458 shprintf(Tf_sN, x_ftab[XFUNC_VALUE(f)].xf_name);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002459#ifndef MKSH_SMALL
2460 else
2461 shprintf("'%s'\n", x_atab[prefix][key]);
2462#endif
2463}
2464
2465int
2466x_bind(const char *a1, const char *a2,
2467#ifndef MKSH_SMALL
Geremy Condra03ebf062011-10-12 18:17:24 -07002468 /* bind -m */
2469 bool macro,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002470#endif
Geremy Condra03ebf062011-10-12 18:17:24 -07002471 /* bind -l */
2472 bool list)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002473{
2474 unsigned char f;
2475 int prefix, key;
2476 char *m1, *m2;
2477#ifndef MKSH_SMALL
2478 char *sp = NULL;
2479 bool hastilde;
2480#endif
2481
2482 if (x_tab == NULL) {
Geremy Condra03ebf062011-10-12 18:17:24 -07002483 bi_errorf("can't bind, not a tty");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002484 return (1);
2485 }
2486 /* List function names */
2487 if (list) {
2488 for (f = 0; f < NELEM(x_ftab); f++)
Elliott Hughes50012062015-03-10 22:22:24 -07002489 if (!(x_ftab[f].xf_flags & XF_NOBIND))
Elliott Hughes77740fc2016-08-12 15:06:53 -07002490 shprintf(Tf_sN, x_ftab[f].xf_name);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002491 return (0);
2492 }
2493 if (a1 == NULL) {
2494 for (prefix = 0; prefix < X_NTABS; prefix++)
2495 for (key = 0; key < X_TABSZ; key++) {
2496 f = XFUNC_VALUE(x_tab[prefix][key]);
2497 if (f == XFUNC_insert || f == XFUNC_error
2498#ifndef MKSH_SMALL
2499 || (macro && f != XFUNC_ins_string)
2500#endif
2501 )
2502 continue;
2503 x_print(prefix, key);
2504 }
2505 return (0);
2506 }
2507 m2 = m1 = x_mapin(a1, ATEMP);
2508 prefix = 0;
2509 for (;; m1++) {
2510 key = (unsigned char)*m1;
2511 f = XFUNC_VALUE(x_tab[prefix][key]);
2512 if (f == XFUNC_meta1)
2513 prefix = 1;
2514 else if (f == XFUNC_meta2)
2515 prefix = 2;
Elliott Hughes96b43632015-07-17 11:39:41 -07002516 else if (f == XFUNC_meta3)
2517 prefix = 3;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002518 else
2519 break;
2520 }
2521 if (*++m1
2522#ifndef MKSH_SMALL
2523 && ((*m1 != '~') || *(m1 + 1))
2524#endif
2525 ) {
Geremy Condra03ebf062011-10-12 18:17:24 -07002526 char msg[256];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002527 const char *c = a1;
Geremy Condra03ebf062011-10-12 18:17:24 -07002528 m1 = msg;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002529 while (*c && (size_t)(m1 - msg) < sizeof(msg) - 3)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002530 x_mapout2(*c++, &m1);
Elliott Hughes77740fc2016-08-12 15:06:53 -07002531 bi_errorf("too long key sequence: %s", msg);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002532 return (1);
2533 }
2534#ifndef MKSH_SMALL
Geremy Condra03ebf062011-10-12 18:17:24 -07002535 hastilde = tobool(*m1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002536#endif
2537 afree(m2, ATEMP);
2538
2539 if (a2 == NULL) {
2540 x_print(prefix, key);
2541 return (0);
2542 }
2543 if (*a2 == 0) {
2544 f = XFUNC_insert;
2545#ifndef MKSH_SMALL
2546 } else if (macro) {
2547 f = XFUNC_ins_string;
2548 sp = x_mapin(a2, AEDIT);
2549#endif
2550 } else {
2551 for (f = 0; f < NELEM(x_ftab); f++)
Elliott Hughes50012062015-03-10 22:22:24 -07002552 if (!strcmp(x_ftab[f].xf_name, a2))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002553 break;
2554 if (f == NELEM(x_ftab) || x_ftab[f].xf_flags & XF_NOBIND) {
Elliott Hughes77740fc2016-08-12 15:06:53 -07002555 bi_errorf("%s: no such function", a2);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002556 return (1);
2557 }
2558 }
2559
2560#ifndef MKSH_SMALL
2561 if (XFUNC_VALUE(x_tab[prefix][key]) == XFUNC_ins_string &&
2562 x_atab[prefix][key])
2563 afree(x_atab[prefix][key], AEDIT);
2564#endif
2565 x_tab[prefix][key] = f
2566#ifndef MKSH_SMALL
2567 | (hastilde ? 0x80 : 0)
2568#endif
2569 ;
2570#ifndef MKSH_SMALL
2571 x_atab[prefix][key] = sp;
2572#endif
2573
2574 /* Track what the user has bound so x_mode(true) won't toast things */
2575 if (f == XFUNC_insert)
2576 x_bound[(prefix * X_TABSZ + key) / 8] &=
2577 ~(1 << ((prefix * X_TABSZ + key) % 8));
2578 else
2579 x_bound[(prefix * X_TABSZ + key) / 8] |=
2580 (1 << ((prefix * X_TABSZ + key) % 8));
2581
2582 return (0);
2583}
2584
2585static void
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002586bind_if_not_bound(int p, int k, int func)
2587{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002588 int t;
2589
2590 /*
2591 * Has user already bound this key?
2592 * If so, do not override it.
2593 */
2594 t = p * X_TABSZ + k;
2595 if (x_bound[t >> 3] & (1 << (t & 7)))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002596 return;
2597
2598 x_tab[p][k] = func;
2599}
2600
2601static int
2602x_set_mark(int c MKSH_A_UNUSED)
2603{
2604 xmp = xcp;
2605 return (KSTD);
2606}
2607
2608static int
2609x_kill_region(int c MKSH_A_UNUSED)
2610{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002611 size_t rsize;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002612 char *xr;
2613
2614 if (xmp == NULL) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002615 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002616 return (KSTD);
2617 }
2618 if (xmp > xcp) {
2619 rsize = xmp - xcp;
2620 xr = xcp;
2621 } else {
2622 rsize = xcp - xmp;
2623 xr = xmp;
2624 }
2625 x_goto(xr);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002626 x_delete(x_nb2nc(rsize), true);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002627 xmp = xr;
2628 return (KSTD);
2629}
2630
2631static int
2632x_xchg_point_mark(int c MKSH_A_UNUSED)
2633{
2634 char *tmp;
2635
2636 if (xmp == NULL) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002637 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002638 return (KSTD);
2639 }
2640 tmp = xmp;
2641 xmp = xcp;
2642 x_goto(tmp);
2643 return (KSTD);
2644}
2645
2646static int
2647x_noop(int c MKSH_A_UNUSED)
2648{
2649 return (KSTD);
2650}
2651
2652/*
2653 * File/command name completion routines
2654 */
2655static int
2656x_comp_comm(int c MKSH_A_UNUSED)
2657{
2658 do_complete(XCF_COMMAND, CT_COMPLETE);
2659 return (KSTD);
2660}
2661
2662static int
2663x_list_comm(int c MKSH_A_UNUSED)
2664{
2665 do_complete(XCF_COMMAND, CT_LIST);
2666 return (KSTD);
2667}
2668
2669static int
2670x_complete(int c MKSH_A_UNUSED)
2671{
2672 do_complete(XCF_COMMAND_FILE, CT_COMPLETE);
2673 return (KSTD);
2674}
2675
2676static int
2677x_enumerate(int c MKSH_A_UNUSED)
2678{
2679 do_complete(XCF_COMMAND_FILE, CT_LIST);
2680 return (KSTD);
2681}
2682
2683static int
2684x_comp_file(int c MKSH_A_UNUSED)
2685{
2686 do_complete(XCF_FILE, CT_COMPLETE);
2687 return (KSTD);
2688}
2689
2690static int
2691x_list_file(int c MKSH_A_UNUSED)
2692{
2693 do_complete(XCF_FILE, CT_LIST);
2694 return (KSTD);
2695}
2696
2697static int
2698x_comp_list(int c MKSH_A_UNUSED)
2699{
2700 do_complete(XCF_COMMAND_FILE, CT_COMPLIST);
2701 return (KSTD);
2702}
2703
2704static int
2705x_expand(int c MKSH_A_UNUSED)
2706{
2707 char **words;
2708 int start, end, nwords, i;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002709
Geremy Condra03ebf062011-10-12 18:17:24 -07002710 i = XCF_FILE;
2711 nwords = x_cf_glob(&i, xbuf, xep - xbuf, xcp - xbuf,
2712 &start, &end, &words);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002713
2714 if (nwords == 0) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002715 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002716 return (KSTD);
2717 }
2718 x_goto(xbuf + start);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002719 x_delete(x_nb2nc(end - start), false);
Geremy Condra03ebf062011-10-12 18:17:24 -07002720
2721 i = 0;
2722 while (i < nwords) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002723 if (x_escape(words[i], strlen(words[i]), x_do_ins) < 0 ||
Elliott Hughes77740fc2016-08-12 15:06:53 -07002724 (++i < nwords && x_ins(T1space) < 0)) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002725 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002726 return (KSTD);
2727 }
2728 }
2729 x_adjust();
2730
2731 return (KSTD);
2732}
2733
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002734static void
Geremy Condra03ebf062011-10-12 18:17:24 -07002735do_complete(
2736 /* XCF_{COMMAND,FILE,COMMAND_FILE} */
2737 int flags,
2738 /* 0 for list, 1 for complete and 2 for complete-list */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002739 Comp_type type)
2740{
2741 char **words;
2742 int start, end, nlen, olen, nwords;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002743 bool completed;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002744
Geremy Condra03ebf062011-10-12 18:17:24 -07002745 nwords = x_cf_glob(&flags, xbuf, xep - xbuf, xcp - xbuf,
2746 &start, &end, &words);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002747 /* no match */
2748 if (nwords == 0) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002749 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002750 return;
2751 }
2752 if (type == CT_LIST) {
Geremy Condra03ebf062011-10-12 18:17:24 -07002753 x_print_expansions(nwords, words,
2754 tobool(flags & XCF_IS_COMMAND));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002755 x_redraw(0);
2756 x_free_words(nwords, words);
2757 return;
2758 }
2759 olen = end - start;
2760 nlen = x_longest_prefix(nwords, words);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002761 if (nwords == 1) {
2762 /*
2763 * always complete single matches;
2764 * any expansion of parameter substitution
2765 * is always at most one result, too
2766 */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002767 completed = true;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002768 } else {
2769 char *unescaped;
2770
2771 /* make a copy of the original string part */
2772 strndupx(unescaped, xbuf + start, olen, ATEMP);
2773
2774 /* expand any tilde and unescape the string for comparison */
2775 unescaped = x_glob_hlp_tilde_and_rem_qchar(unescaped, true);
2776
2777 /*
2778 * match iff entire original string is part of the
2779 * longest prefix, implying the latter is at least
2780 * the same size (after unescaping)
2781 */
2782 completed = !strncmp(words[0], unescaped, strlen(unescaped));
2783
2784 afree(unescaped, ATEMP);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002785 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002786 if (type == CT_COMPLIST && nwords > 1) {
2787 /*
2788 * print expansions, since we didn't get back
2789 * just a single match
2790 */
2791 x_print_expansions(nwords, words,
2792 tobool(flags & XCF_IS_COMMAND));
2793 }
2794 if (completed) {
2795 /* expand on the command line */
2796 xmp = NULL;
2797 xcp = xbuf + start;
2798 xep -= olen;
2799 memmove(xcp, xcp + olen, xep - xcp + 1);
2800 x_escape(words[0], nlen, x_do_ins);
2801 }
2802 x_adjust();
Geremy Condra03ebf062011-10-12 18:17:24 -07002803 /*
2804 * append a space if this is a single non-directory match
2805 * and not a parameter or homedir substitution
2806 */
Elliott Hughes966dd552016-12-08 15:56:04 -08002807 if (nwords == 1 && !mksh_cdirsep(words[0][nlen - 1]) &&
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002808 !(flags & XCF_IS_NOSPACE)) {
Elliott Hughes77740fc2016-08-12 15:06:53 -07002809 x_ins(T1space);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002810 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002811
2812 x_free_words(nwords, words);
2813}
2814
Geremy Condra03ebf062011-10-12 18:17:24 -07002815/*-
2816 * NAME:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002817 * x_adjust - redraw the line adjusting starting point etc.
2818 *
2819 * DESCRIPTION:
2820 * This function is called when we have exceeded the bounds
2821 * of the edit window. It increments x_adj_done so that
2822 * functions like x_ins and x_delete know that we have been
2823 * called and can skip the x_bs() stuff which has already
2824 * been done by x_redraw.
2825 *
2826 * RETURN VALUE:
2827 * None
2828 */
2829static void
2830x_adjust(void)
2831{
Thorsten Glaser811a5752013-07-25 14:24:45 +00002832 int col_left, n;
2833
2834 /* flag the fact that we were called */
Geremy Condra03ebf062011-10-12 18:17:24 -07002835 x_adj_done++;
Thorsten Glaser811a5752013-07-25 14:24:45 +00002836
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002837 /*
Thorsten Glaser811a5752013-07-25 14:24:45 +00002838 * calculate the amount of columns we need to "go back"
2839 * from xcp to set xbp to (but never < xbuf) to 2/3 of
2840 * the display width; take care of pwidth though
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002841 */
Thorsten Glaser811a5752013-07-25 14:24:45 +00002842 if ((col_left = xx_cols * 2 / 3) < MIN_EDIT_SPACE) {
2843 /*
2844 * cowardly refuse to do anything
2845 * if the available space is too small;
2846 * fall back to dumb pdksh code
2847 */
2848 if ((xbp = xcp - (x_displen / 2)) < xbuf)
2849 xbp = xbuf;
2850 /* elide UTF-8 fixup as penalty */
2851 goto x_adjust_out;
2852 }
2853
2854 /* fix up xbp to just past a character end first */
2855 xbp = xcp >= xep ? xep : x_bs0(xcp, xbuf);
2856 /* walk backwards */
2857 while (xbp > xbuf && col_left > 0) {
2858 xbp = x_bs0(xbp - 1, xbuf);
2859 col_left -= (n = x_size2(xbp, NULL));
2860 }
2861 /* check if we hit the prompt */
2862 if (xbp == xbuf && xcp != xbuf && col_left >= 0 && col_left < pwidth) {
2863 /* so we did; force scrolling occurs */
2864 xbp += utf_ptradj(xbp);
2865 }
2866
2867 x_adjust_out:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002868 xlp_valid = false;
Elliott Hughes77740fc2016-08-12 15:06:53 -07002869 x_redraw('\r');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002870 x_flush();
2871}
2872
2873static void
2874x_e_ungetc(int c)
2875{
2876 unget_char = c < 0 ? -1 : (c & 255);
2877}
2878
2879static int
2880x_e_getc(void)
2881{
2882 int c;
2883
2884 if (unget_char >= 0) {
2885 c = unget_char;
2886 unget_char = -1;
2887 return (c);
2888 }
2889
2890#ifndef MKSH_SMALL
2891 if (macroptr) {
2892 if ((c = (unsigned char)*macroptr++))
2893 return (c);
2894 macroptr = NULL;
2895 }
2896#endif
2897
2898 return (x_getc());
2899}
2900
2901static void
2902x_e_putc2(int c)
2903{
2904 int width = 1;
2905
Elliott Hughes23925bb2017-09-22 16:04:20 -07002906 if (ctype(c, C_CR | C_LF))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002907 x_col = 0;
2908 if (x_col < xx_cols) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002909#ifndef MKSH_EBCDIC
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002910 if (UTFMODE && (c > 0x7F)) {
2911 char utf_tmp[3];
2912 size_t x;
2913
2914 if (c < 0xA0)
2915 c = 0xFFFD;
2916 x = utf_wctomb(utf_tmp, c);
2917 x_putc(utf_tmp[0]);
2918 if (x > 1)
2919 x_putc(utf_tmp[1]);
2920 if (x > 2)
2921 x_putc(utf_tmp[2]);
2922 width = utf_wcwidth(c);
2923 } else
Elliott Hughes23925bb2017-09-22 16:04:20 -07002924#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002925 x_putc(c);
2926 switch (c) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002927 case KSH_BEL:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002928 break;
2929 case '\r':
2930 case '\n':
2931 break;
2932 case '\b':
2933 x_col--;
2934 break;
2935 default:
2936 x_col += width;
2937 break;
2938 }
2939 }
2940 if (x_adj_ok && (x_col < 0 || x_col >= (xx_cols - 2)))
2941 x_adjust();
2942}
2943
2944static void
2945x_e_putc3(const char **cp)
2946{
2947 int width = 1, c = **(const unsigned char **)cp;
2948
Elliott Hughes23925bb2017-09-22 16:04:20 -07002949 if (ctype(c, C_CR | C_LF))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002950 x_col = 0;
2951 if (x_col < xx_cols) {
2952 if (UTFMODE && (c > 0x7F)) {
2953 char *cp2;
2954
2955 width = utf_widthadj(*cp, (const char **)&cp2);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002956 if (cp2 == *cp + 1) {
2957 (*cp)++;
Elliott Hughes23925bb2017-09-22 16:04:20 -07002958#ifdef MKSH_EBCDIC
2959 x_putc(asc2rtt(0xEF));
2960 x_putc(asc2rtt(0xBF));
2961 x_putc(asc2rtt(0xBD));
2962#else
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002963 shf_puts("\xEF\xBF\xBD", shl_out);
Elliott Hughes23925bb2017-09-22 16:04:20 -07002964#endif
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002965 } else
2966 while (*cp < cp2)
2967 x_putcf(*(*cp)++);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002968 } else {
2969 (*cp)++;
2970 x_putc(c);
2971 }
2972 switch (c) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07002973 case KSH_BEL:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002974 break;
2975 case '\r':
2976 case '\n':
2977 break;
2978 case '\b':
2979 x_col--;
2980 break;
2981 default:
2982 x_col += width;
2983 break;
2984 }
2985 }
2986 if (x_adj_ok && (x_col < 0 || x_col >= (xx_cols - 2)))
2987 x_adjust();
2988}
2989
2990static void
2991x_e_puts(const char *s)
2992{
2993 int adj = x_adj_done;
2994
2995 while (*s && adj == x_adj_done)
2996 x_e_putc3(&s);
2997}
2998
Geremy Condra03ebf062011-10-12 18:17:24 -07002999/*-
3000 * NAME:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003001 * x_set_arg - set an arg value for next function
3002 *
3003 * DESCRIPTION:
3004 * This is a simple implementation of M-[0-9].
3005 *
3006 * RETURN VALUE:
3007 * KSTD
3008 */
3009static int
3010x_set_arg(int c)
3011{
Geremy Condra03ebf062011-10-12 18:17:24 -07003012 unsigned int n = 0;
3013 bool first = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003014
Geremy Condra03ebf062011-10-12 18:17:24 -07003015 /* strip command prefix */
3016 c &= 255;
Elliott Hughes23925bb2017-09-22 16:04:20 -07003017 while (c >= 0 && ctype(c, C_DIGIT)) {
Elliott Hughes96b43632015-07-17 11:39:41 -07003018 n = n * 10 + ksh_numdig(c);
Geremy Condra03ebf062011-10-12 18:17:24 -07003019 if (n > LINE)
3020 /* upper bound for repeat */
3021 goto x_set_arg_too_big;
3022 c = x_e_getc();
3023 first = false;
3024 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003025 if (c < 0 || first) {
Geremy Condra03ebf062011-10-12 18:17:24 -07003026 x_set_arg_too_big:
Elliott Hughes23925bb2017-09-22 16:04:20 -07003027 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003028 x_arg = 1;
Geremy Condra03ebf062011-10-12 18:17:24 -07003029 x_arg_defaulted = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003030 } else {
3031 x_e_ungetc(c);
3032 x_arg = n;
Geremy Condra03ebf062011-10-12 18:17:24 -07003033 x_arg_defaulted = false;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003034 }
3035 return (KSTD);
3036}
3037
3038/* Comment or uncomment the current line. */
3039static int
3040x_comment(int c MKSH_A_UNUSED)
3041{
Geremy Condra03ebf062011-10-12 18:17:24 -07003042 ssize_t len = xep - xbuf;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003043 int ret = x_do_comment(xbuf, xend - xbuf, &len);
3044
3045 if (ret < 0)
Elliott Hughes23925bb2017-09-22 16:04:20 -07003046 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003047 else {
3048 x_modified();
3049 xep = xbuf + len;
3050 *xep = '\0';
3051 xcp = xbp = xbuf;
Elliott Hughes77740fc2016-08-12 15:06:53 -07003052 x_redraw('\r');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003053 if (ret > 0)
3054 return (x_newline('\n'));
3055 }
3056 return (KSTD);
3057}
3058
3059static int
3060x_version(int c MKSH_A_UNUSED)
3061{
3062 char *o_xbuf = xbuf, *o_xend = xend;
3063 char *o_xbp = xbp, *o_xep = xep, *o_xcp = xcp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003064 char *v;
3065
3066 strdupx(v, KSH_VERSION, ATEMP);
3067
3068 xbuf = xbp = xcp = v;
Elliott Hughes23925bb2017-09-22 16:04:20 -07003069 xend = xep = strnul(v);
Elliott Hughes77740fc2016-08-12 15:06:53 -07003070 x_redraw('\r');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003071 x_flush();
3072
3073 c = x_e_getc();
3074 xbuf = o_xbuf;
3075 xend = o_xend;
3076 xbp = o_xbp;
3077 xep = o_xep;
3078 xcp = o_xcp;
Elliott Hughes77740fc2016-08-12 15:06:53 -07003079 x_redraw('\r');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003080
3081 if (c < 0)
3082 return (KSTD);
3083 /* This is what AT&T ksh seems to do... Very bizarre */
3084 if (c != ' ')
3085 x_e_ungetc(c);
3086
3087 afree(v, ATEMP);
3088 return (KSTD);
3089}
3090
3091#ifndef MKSH_SMALL
3092static int
3093x_edit_line(int c MKSH_A_UNUSED)
3094{
3095 if (x_arg_defaulted) {
3096 if (xep == xbuf) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07003097 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003098 return (KSTD);
3099 }
3100 if (modified) {
3101 *xep = '\0';
Elliott Hughes96b43632015-07-17 11:39:41 -07003102 histsave(&source->line, xbuf, HIST_STORE, true);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003103 x_arg = 0;
3104 } else
3105 x_arg = source->line - (histptr - x_histp);
3106 }
3107 if (x_arg)
Elliott Hughes77740fc2016-08-12 15:06:53 -07003108 shf_snprintf(xbuf, xend - xbuf, Tf_sd,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003109 "fc -e ${VISUAL:-${EDITOR:-vi}} --", x_arg);
3110 else
3111 strlcpy(xbuf, "fc -e ${VISUAL:-${EDITOR:-vi}} --", xend - xbuf);
Elliott Hughes23925bb2017-09-22 16:04:20 -07003112 xep = strnul(xbuf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003113 return (x_newline('\n'));
3114}
3115#endif
3116
Geremy Condra03ebf062011-10-12 18:17:24 -07003117/*-
3118 * NAME:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003119 * x_prev_histword - recover word from prev command
3120 *
3121 * DESCRIPTION:
3122 * This function recovers the last word from the previous
3123 * command and inserts it into the current edit line. If a
3124 * numeric arg is supplied then the n'th word from the
3125 * start of the previous command is used.
3126 * As a side effect, trashes the mark in order to achieve
3127 * being called in a repeatable fashion.
3128 *
3129 * Bound to M-.
3130 *
3131 * RETURN VALUE:
3132 * KSTD
3133 */
3134static int
3135x_prev_histword(int c MKSH_A_UNUSED)
3136{
3137 char *rcp, *cp;
3138 char **xhp;
Geremy Condra03ebf062011-10-12 18:17:24 -07003139 int m = 1;
3140 /* -1 = defaulted; 0+ = argument */
3141 static int last_arg = -1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003142
Geremy Condra03ebf062011-10-12 18:17:24 -07003143 if (x_last_command == XFUNC_prev_histword) {
3144 if (xmp && modified > 1)
3145 x_kill_region(0);
3146 if (modified)
3147 m = modified;
3148 } else
3149 last_arg = x_arg_defaulted ? -1 : x_arg;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003150 xhp = histptr - (m - 1);
3151 if ((xhp < history) || !(cp = *xhp)) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07003152 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003153 x_modified();
3154 return (KSTD);
3155 }
3156 x_set_mark(0);
Geremy Condra03ebf062011-10-12 18:17:24 -07003157 if ((x_arg = last_arg) == -1) {
3158 /* x_arg_defaulted */
3159
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003160 rcp = &cp[strlen(cp) - 1];
3161 /*
3162 * ignore white-space after the last word
3163 */
Elliott Hughes23925bb2017-09-22 16:04:20 -07003164 while (rcp > cp && ctype(*rcp, C_CFS))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003165 rcp--;
Elliott Hughes23925bb2017-09-22 16:04:20 -07003166 while (rcp > cp && !ctype(*rcp, C_CFS))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003167 rcp--;
Elliott Hughes23925bb2017-09-22 16:04:20 -07003168 if (ctype(*rcp, C_CFS))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003169 rcp++;
3170 x_ins(rcp);
3171 } else {
Geremy Condra03ebf062011-10-12 18:17:24 -07003172 /* not x_arg_defaulted */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003173 char ch;
3174
3175 rcp = cp;
3176 /*
3177 * ignore white-space at start of line
3178 */
Elliott Hughes23925bb2017-09-22 16:04:20 -07003179 while (*rcp && ctype(*rcp, C_CFS))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003180 rcp++;
Geremy Condra03ebf062011-10-12 18:17:24 -07003181 while (x_arg-- > 0) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07003182 while (*rcp && !ctype(*rcp, C_CFS))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003183 rcp++;
Elliott Hughes23925bb2017-09-22 16:04:20 -07003184 while (*rcp && ctype(*rcp, C_CFS))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003185 rcp++;
3186 }
3187 cp = rcp;
Elliott Hughes23925bb2017-09-22 16:04:20 -07003188 while (*rcp && !ctype(*rcp, C_CFS))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003189 rcp++;
3190 ch = *rcp;
3191 *rcp = '\0';
3192 x_ins(cp);
3193 *rcp = ch;
3194 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003195 if (!modified)
3196 x_histmcp = x_histp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003197 modified = m + 1;
3198 return (KSTD);
3199}
3200
3201#ifndef MKSH_SMALL
3202/* Uppercase N(1) words */
3203static int
3204x_fold_upper(int c MKSH_A_UNUSED)
3205{
3206 return (x_fold_case('U'));
3207}
3208
3209/* Lowercase N(1) words */
3210static int
3211x_fold_lower(int c MKSH_A_UNUSED)
3212{
3213 return (x_fold_case('L'));
3214}
3215
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00003216/* Titlecase N(1) words */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003217static int
3218x_fold_capitalise(int c MKSH_A_UNUSED)
3219{
3220 return (x_fold_case('C'));
3221}
3222
Geremy Condra03ebf062011-10-12 18:17:24 -07003223/*-
3224 * NAME:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003225 * x_fold_case - convert word to UPPER/lower/Capital case
3226 *
3227 * DESCRIPTION:
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00003228 * This function is used to implement M-U/M-u, M-L/M-l, M-C/M-c
3229 * to UPPER CASE, lower case or Capitalise Words.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003230 *
3231 * RETURN VALUE:
3232 * None
3233 */
3234static int
3235x_fold_case(int c)
3236{
3237 char *cp = xcp;
3238
3239 if (cp == xep) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07003240 x_e_putc2(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003241 return (KSTD);
3242 }
3243 while (x_arg--) {
3244 /*
3245 * first skip over any white-space
3246 */
Elliott Hughes23925bb2017-09-22 16:04:20 -07003247 while (cp != xep && ctype(*cp, C_MFS))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003248 cp++;
3249 /*
3250 * do the first char on its own since it may be
3251 * a different action than for the rest.
3252 */
3253 if (cp != xep) {
Geremy Condra03ebf062011-10-12 18:17:24 -07003254 if (c == 'L')
3255 /* lowercase */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003256 *cp = ksh_tolower(*cp);
Geremy Condra03ebf062011-10-12 18:17:24 -07003257 else
3258 /* uppercase, capitalise */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003259 *cp = ksh_toupper(*cp);
3260 cp++;
3261 }
3262 /*
3263 * now for the rest of the word
3264 */
Elliott Hughes23925bb2017-09-22 16:04:20 -07003265 while (cp != xep && !ctype(*cp, C_MFS)) {
Geremy Condra03ebf062011-10-12 18:17:24 -07003266 if (c == 'U')
3267 /* uppercase */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003268 *cp = ksh_toupper(*cp);
Geremy Condra03ebf062011-10-12 18:17:24 -07003269 else
3270 /* lowercase, capitalise */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003271 *cp = ksh_tolower(*cp);
3272 cp++;
3273 }
3274 }
3275 x_goto(cp);
3276 x_modified();
3277 return (KSTD);
3278}
3279#endif
3280
Geremy Condra03ebf062011-10-12 18:17:24 -07003281/*-
3282 * NAME:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003283 * x_lastcp - last visible char
3284 *
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003285 * DESCRIPTION:
3286 * This function returns a pointer to that char in the
3287 * edit buffer that will be the last displayed on the
Elliott Hughes77740fc2016-08-12 15:06:53 -07003288 * screen.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003289 */
3290static char *
3291x_lastcp(void)
3292{
3293 if (!xlp_valid) {
3294 int i = 0, j;
3295 char *xlp2;
3296
3297 xlp = xbp;
3298 while (xlp < xep) {
3299 j = x_size2(xlp, &xlp2);
3300 if ((i + j) > x_displen)
3301 break;
3302 i += j;
3303 xlp = xlp2;
3304 }
3305 }
3306 xlp_valid = true;
3307 return (xlp);
3308}
3309
Elliott Hughes77740fc2016-08-12 15:06:53 -07003310/* correctly position the cursor on the screen from end of visible area */
3311static void
3312x_lastpos(void)
3313{
3314 char *cp = x_lastcp();
3315
3316 while (cp > xcp)
3317 x_bs3(&cp);
3318}
3319
Geremy Condra03ebf062011-10-12 18:17:24 -07003320static void
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003321x_mode(bool onoff)
3322{
3323 static bool x_cur_mode;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003324
3325 if (x_cur_mode == onoff)
Geremy Condra03ebf062011-10-12 18:17:24 -07003326 return;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003327 x_cur_mode = onoff;
3328
3329 if (onoff) {
Geremy Condra03ebf062011-10-12 18:17:24 -07003330 x_mkraw(tty_fd, NULL, false);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003331
Elliott Hughes77740fc2016-08-12 15:06:53 -07003332 edchars.erase = toedchar(tty_state.c_cc[VERASE]);
3333 edchars.kill = toedchar(tty_state.c_cc[VKILL]);
3334 edchars.intr = toedchar(tty_state.c_cc[VINTR]);
3335 edchars.quit = toedchar(tty_state.c_cc[VQUIT]);
3336 edchars.eof = toedchar(tty_state.c_cc[VEOF]);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003337#ifdef VWERASE
Elliott Hughes77740fc2016-08-12 15:06:53 -07003338 edchars.werase = toedchar(tty_state.c_cc[VWERASE]);
Elliott Hughes96b43632015-07-17 11:39:41 -07003339#else
3340 edchars.werase = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003341#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003342
Elliott Hughes96b43632015-07-17 11:39:41 -07003343 if (!edchars.erase)
Elliott Hughes23925bb2017-09-22 16:04:20 -07003344 edchars.erase = CTRL_H;
Elliott Hughes96b43632015-07-17 11:39:41 -07003345 if (!edchars.kill)
Elliott Hughes23925bb2017-09-22 16:04:20 -07003346 edchars.kill = CTRL_U;
Elliott Hughes96b43632015-07-17 11:39:41 -07003347 if (!edchars.intr)
Elliott Hughes23925bb2017-09-22 16:04:20 -07003348 edchars.intr = CTRL_C;
Elliott Hughes96b43632015-07-17 11:39:41 -07003349 if (!edchars.quit)
Elliott Hughes23925bb2017-09-22 16:04:20 -07003350 edchars.quit = CTRL_BK;
Elliott Hughes96b43632015-07-17 11:39:41 -07003351 if (!edchars.eof)
Elliott Hughes23925bb2017-09-22 16:04:20 -07003352 edchars.eof = CTRL_D;
Elliott Hughes96b43632015-07-17 11:39:41 -07003353 if (!edchars.werase)
Elliott Hughes23925bb2017-09-22 16:04:20 -07003354 edchars.werase = CTRL_W;
Elliott Hughes96b43632015-07-17 11:39:41 -07003355
Elliott Hughes77740fc2016-08-12 15:06:53 -07003356 if (isedchar(edchars.erase)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003357 bind_if_not_bound(0, edchars.erase, XFUNC_del_back);
3358 bind_if_not_bound(1, edchars.erase, XFUNC_del_bword);
3359 }
Elliott Hughes77740fc2016-08-12 15:06:53 -07003360 if (isedchar(edchars.kill))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003361 bind_if_not_bound(0, edchars.kill, XFUNC_del_line);
Elliott Hughes77740fc2016-08-12 15:06:53 -07003362 if (isedchar(edchars.werase))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003363 bind_if_not_bound(0, edchars.werase, XFUNC_del_bword);
Elliott Hughes77740fc2016-08-12 15:06:53 -07003364 if (isedchar(edchars.intr))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003365 bind_if_not_bound(0, edchars.intr, XFUNC_abort);
Elliott Hughes77740fc2016-08-12 15:06:53 -07003366 if (isedchar(edchars.quit))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003367 bind_if_not_bound(0, edchars.quit, XFUNC_noop);
3368 } else
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00003369 mksh_tcset(tty_fd, &tty_state);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003370}
3371
3372#if !MKSH_S_NOVI
3373/* +++ vi editing mode +++ */
3374
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003375struct edstate {
3376 char *cbuf;
Geremy Condra03ebf062011-10-12 18:17:24 -07003377 ssize_t winleft;
3378 ssize_t cbufsize;
3379 ssize_t linelen;
3380 ssize_t cursor;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003381};
3382
3383static int vi_hook(int);
3384static int nextstate(int);
3385static int vi_insert(int);
3386static int vi_cmd(int, const char *);
3387static int domove(int, const char *, int);
Elliott Hughes23925bb2017-09-22 16:04:20 -07003388static int domovebeg(void);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003389static int redo_insert(int);
3390static void yank_range(int, int);
3391static int bracktype(int);
3392static void save_cbuf(void);
3393static void restore_cbuf(void);
Thorsten Glaser811a5752013-07-25 14:24:45 +00003394static int putbuf(const char *, ssize_t, bool);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003395static void del_range(int, int);
Elliott Hughes737fdce2014-08-07 12:59:26 -07003396static int findch(int, int, bool, bool) MKSH_A_PURE;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003397static int forwword(int);
3398static int backword(int);
3399static int endword(int);
3400static int Forwword(int);
3401static int Backword(int);
3402static int Endword(int);
3403static int grabhist(int, int);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00003404static int grabsearch(int, int, int, const char *);
Geremy Condra03ebf062011-10-12 18:17:24 -07003405static void redraw_line(bool);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003406static void refresh(int);
3407static int outofwin(void);
3408static void rewindow(void);
Elliott Hughes737fdce2014-08-07 12:59:26 -07003409static int newcol(unsigned char, int);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003410static void display(char *, char *, int);
3411static void ed_mov_opt(int, char *);
3412static int expand_word(int);
3413static int complete_word(int, int);
3414static int print_expansions(struct edstate *, int);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003415static void vi_error(void);
3416static void vi_macro_reset(void);
3417static int x_vi_putbuf(const char *, size_t);
Elliott Hughes23925bb2017-09-22 16:04:20 -07003418#define char_len(c) (ksh_isctrl(c) ? 2 : 1)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003419
Geremy Condra03ebf062011-10-12 18:17:24 -07003420#define vC 0x01 /* a valid command that isn't a vM, vE, vU */
3421#define vM 0x02 /* movement command (h, l, etc.) */
3422#define vE 0x04 /* extended command (c, d, y) */
3423#define vX 0x08 /* long command (@, f, F, t, T, etc.) */
3424#define vU 0x10 /* an UN-undoable command (that isn't a vM) */
3425#define vB 0x20 /* bad command (^@) */
3426#define vZ 0x40 /* repeat count defaults to 0 (not 1) */
3427#define vS 0x80 /* search (/, ?) */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003428
Elliott Hughes23925bb2017-09-22 16:04:20 -07003429#define is_bad(c) (classify[rtt2asc(c) & 0x7F] & vB)
3430#define is_cmd(c) (classify[rtt2asc(c) & 0x7F] & (vM | vE | vC | vU))
3431#define is_move(c) (classify[rtt2asc(c) & 0x7F] & vM)
3432#define is_extend(c) (classify[rtt2asc(c) & 0x7F] & vE)
3433#define is_long(c) (classify[rtt2asc(c) & 0x7F] & vX)
3434#define is_undoable(c) (!(classify[rtt2asc(c) & 0x7F] & vU))
3435#define is_srch(c) (classify[rtt2asc(c) & 0x7F] & vS)
3436#define is_zerocount(c) (classify[rtt2asc(c) & 0x7F] & vZ)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003437
3438static const unsigned char classify[128] = {
3439/* 0 1 2 3 4 5 6 7 */
3440/* 0 ^@ ^A ^B ^C ^D ^E ^F ^G */
Geremy Condra03ebf062011-10-12 18:17:24 -07003441 vB, 0, 0, 0, 0, vC|vU, vC|vZ, 0,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003442/* 1 ^H ^I ^J ^K ^L ^M ^N ^O */
Geremy Condra03ebf062011-10-12 18:17:24 -07003443 vM, vC|vZ, 0, 0, vC|vU, 0, vC, 0,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003444/* 2 ^P ^Q ^R ^S ^T ^U ^V ^W */
Geremy Condra03ebf062011-10-12 18:17:24 -07003445 vC, 0, vC|vU, 0, 0, 0, vC, 0,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003446/* 3 ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
Geremy Condra03ebf062011-10-12 18:17:24 -07003447 vC, 0, 0, vC|vZ, 0, 0, 0, 0,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003448/* 4 <space> ! " # $ % & ' */
Geremy Condra03ebf062011-10-12 18:17:24 -07003449 vM, 0, 0, vC, vM, vM, 0, 0,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003450/* 5 ( ) * + , - . / */
Geremy Condra03ebf062011-10-12 18:17:24 -07003451 0, 0, vC, vC, vM, vC, 0, vC|vS,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003452/* 6 0 1 2 3 4 5 6 7 */
Geremy Condra03ebf062011-10-12 18:17:24 -07003453 vM, 0, 0, 0, 0, 0, 0, 0,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003454/* 7 8 9 : ; < = > ? */
Geremy Condra03ebf062011-10-12 18:17:24 -07003455 0, 0, 0, vM, 0, vC, 0, vC|vS,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003456/* 8 @ A B C D E F G */
Geremy Condra03ebf062011-10-12 18:17:24 -07003457 vC|vX, vC, vM, vC, vC, vM, vM|vX, vC|vU|vZ,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003458/* 9 H I J K L M N O */
Thorsten Glaser811a5752013-07-25 14:24:45 +00003459 0, vC, 0, 0, 0, 0, vC|vU, vU,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003460/* A P Q R S T U V W */
Geremy Condra03ebf062011-10-12 18:17:24 -07003461 vC, 0, vC, vC, vM|vX, vC, 0, vM,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003462/* B X Y Z [ \ ] ^ _ */
Thorsten Glaser811a5752013-07-25 14:24:45 +00003463 vC, vC|vU, 0, vU, vC|vZ, 0, vM, vC|vZ,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003464/* C ` a b c d e f g */
Geremy Condra03ebf062011-10-12 18:17:24 -07003465 0, vC, vM, vE, vE, vM, vM|vX, vC|vZ,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003466/* D h i j k l m n o */
Geremy Condra03ebf062011-10-12 18:17:24 -07003467 vM, vC, vC|vU, vC|vU, vM, 0, vC|vU, 0,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003468/* E p q r s t u v w */
Geremy Condra03ebf062011-10-12 18:17:24 -07003469 vC, 0, vX, vC, vM|vX, vC|vU, vC|vU|vZ, vM,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003470/* F x y z { | } ~ ^? */
Geremy Condra03ebf062011-10-12 18:17:24 -07003471 vC, vE|vU, 0, 0, vM|vZ, 0, vC, 0
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003472};
3473
3474#define MAXVICMD 3
3475#define SRCHLEN 40
3476
3477#define INSERT 1
3478#define REPLACE 2
3479
3480#define VNORMAL 0 /* command, insert or replace mode */
3481#define VARG1 1 /* digit prefix (first, eg, 5l) */
3482#define VEXTCMD 2 /* cmd + movement (eg, cl) */
3483#define VARG2 3 /* digit prefix (second, eg, 2c3l) */
3484#define VXCH 4 /* f, F, t, T, @ */
3485#define VFAIL 5 /* bad command */
3486#define VCMD 6 /* single char command (eg, X) */
3487#define VREDO 7 /* . */
3488#define VLIT 8 /* ^V */
3489#define VSEARCH 9 /* /, ? */
3490#define VVERSION 10 /* <ESC> ^V */
Thorsten Glaser811a5752013-07-25 14:24:45 +00003491#define VPREFIX2 11 /* ^[[ and ^[O in insert mode */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003492
3493static struct edstate *save_edstate(struct edstate *old);
3494static void restore_edstate(struct edstate *old, struct edstate *news);
3495static void free_edstate(struct edstate *old);
3496
3497static struct edstate ebuf;
Thorsten Glaser811a5752013-07-25 14:24:45 +00003498static struct edstate undobuf;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003499
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003500static struct edstate *vs; /* current Vi editing mode state */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003501static struct edstate *undo;
3502
Thorsten Glaser811a5752013-07-25 14:24:45 +00003503static char *ibuf; /* input buffer */
3504static bool first_insert; /* set when starting in insert mode */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003505static int saved_inslen; /* saved inslen for first insert */
3506static int inslen; /* length of input buffer */
3507static int srchlen; /* length of current search pattern */
Thorsten Glaser811a5752013-07-25 14:24:45 +00003508static char *ybuf; /* yank buffer */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003509static int yanklen; /* length of yank buffer */
3510static int fsavecmd = ' '; /* last find command */
3511static int fsavech; /* character to find */
3512static char lastcmd[MAXVICMD]; /* last non-move command */
3513static int lastac; /* argcnt for lastcmd */
3514static int lastsearch = ' '; /* last search command */
3515static char srchpat[SRCHLEN]; /* last search pattern */
Thorsten Glaser811a5752013-07-25 14:24:45 +00003516static int insert; /* <>0 in insert mode */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003517static int hnum; /* position in history */
3518static int ohnum; /* history line copied (after mod) */
3519static int hlast; /* 1 past last position in history */
3520static int state;
3521
Geremy Condra03ebf062011-10-12 18:17:24 -07003522/*
3523 * Information for keeping track of macros that are being expanded.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003524 * The format of buf is the alias contents followed by a NUL byte followed
3525 * by the name (letter) of the alias. The end of the buffer is marked by
3526 * a double NUL. The name of the alias is stored so recursive macros can
3527 * be detected.
3528 */
3529struct macro_state {
3530 unsigned char *p; /* current position in buf */
3531 unsigned char *buf; /* pointer to macro(s) being expanded */
Geremy Condra03ebf062011-10-12 18:17:24 -07003532 size_t len; /* how much data in buffer */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003533};
3534static struct macro_state macro;
3535
Geremy Condra03ebf062011-10-12 18:17:24 -07003536/* last input was expanded */
3537static enum expand_mode {
3538 NONE = 0, EXPAND, COMPLETE, PRINT
3539} expanded;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003540
3541static int
Thorsten Glaser811a5752013-07-25 14:24:45 +00003542x_vi(char *buf)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003543{
3544 int c;
3545
3546 state = VNORMAL;
3547 ohnum = hnum = hlast = histnum(-1) + 1;
3548 insert = INSERT;
3549 saved_inslen = inslen;
Thorsten Glaser811a5752013-07-25 14:24:45 +00003550 first_insert = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003551 inslen = 0;
3552 vi_macro_reset();
3553
Thorsten Glaser811a5752013-07-25 14:24:45 +00003554 ebuf.cbuf = buf;
3555 if (undobuf.cbuf == NULL) {
3556 ibuf = alloc(LINE, AEDIT);
3557 ybuf = alloc(LINE, AEDIT);
3558 undobuf.cbuf = alloc(LINE, AEDIT);
3559 }
3560 undobuf.cbufsize = ebuf.cbufsize = LINE;
3561 undobuf.linelen = ebuf.linelen = 0;
3562 undobuf.cursor = ebuf.cursor = 0;
3563 undobuf.winleft = ebuf.winleft = 0;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003564 vs = &ebuf;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003565 undo = &undobuf;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003566
Elliott Hughes737fdce2014-08-07 12:59:26 -07003567 x_init_prompt(true);
Thorsten Glaser811a5752013-07-25 14:24:45 +00003568 x_col = pwidth;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003569
Thorsten Glaser811a5752013-07-25 14:24:45 +00003570 if (wbuf_len != x_cols - 3 && ((wbuf_len = x_cols - 3))) {
3571 wbuf[0] = aresize(wbuf[0], wbuf_len, AEDIT);
3572 wbuf[1] = aresize(wbuf[1], wbuf_len, AEDIT);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003573 }
Geremy Condra03ebf062011-10-12 18:17:24 -07003574 if (wbuf_len) {
3575 memset(wbuf[0], ' ', wbuf_len);
3576 memset(wbuf[1], ' ', wbuf_len);
3577 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003578 winwidth = x_cols - pwidth - 3;
3579 win = 0;
3580 morec = ' ';
3581 lastref = 1;
3582 holdlen = 0;
3583
3584 editmode = 2;
3585 x_flush();
Geremy Condra03ebf062011-10-12 18:17:24 -07003586 while (/* CONSTCOND */ 1) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003587 if (macro.p) {
Elliott Hughes737fdce2014-08-07 12:59:26 -07003588 c = (unsigned char)*macro.p++;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003589 /* end of current macro? */
3590 if (!c) {
3591 /* more macros left to finish? */
3592 if (*macro.p++)
3593 continue;
3594 /* must be the end of all the macros */
3595 vi_macro_reset();
3596 c = x_getc();
3597 }
3598 } else
3599 c = x_getc();
3600
3601 if (c == -1)
3602 break;
3603 if (state != VLIT) {
Elliott Hughes77740fc2016-08-12 15:06:53 -07003604 if (isched(c, edchars.intr) ||
3605 isched(c, edchars.quit)) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07003606 /* shove input buffer away */
3607 xbuf = ebuf.cbuf;
3608 xep = xbuf;
3609 if (ebuf.linelen > 0)
3610 xep += ebuf.linelen;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003611 /* pretend we got an interrupt */
Elliott Hughes23925bb2017-09-22 16:04:20 -07003612 x_intr(isched(c, edchars.intr) ?
3613 SIGINT : SIGQUIT, c);
Elliott Hughes77740fc2016-08-12 15:06:53 -07003614 } else if (isched(c, edchars.eof) &&
3615 state != VVERSION) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003616 if (vs->linelen == 0) {
Elliott Hughes77740fc2016-08-12 15:06:53 -07003617 x_vi_zotc(c);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003618 c = -1;
3619 break;
3620 }
3621 continue;
3622 }
3623 }
3624 if (vi_hook(c))
3625 break;
3626 x_flush();
3627 }
3628
3629 x_putc('\r');
3630 x_putc('\n');
3631 x_flush();
3632
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003633 if (c == -1 || (ssize_t)LINE <= vs->linelen)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003634 return (-1);
3635
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003636 if (vs->cbuf != buf)
3637 memcpy(buf, vs->cbuf, vs->linelen);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003638
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003639 buf[vs->linelen++] = '\n';
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003640
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003641 return (vs->linelen);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003642}
3643
3644static int
3645vi_hook(int ch)
3646{
3647 static char curcmd[MAXVICMD], locpat[SRCHLEN];
3648 static int cmdlen, argc1, argc2;
3649
3650 switch (state) {
3651
3652 case VNORMAL:
Elliott Hughes96b43632015-07-17 11:39:41 -07003653 /* PC scancodes */
3654 if (!ch) switch (cmdlen = 0, (ch = x_getc())) {
3655 case 71: ch = '0'; goto pseudo_vi_command;
3656 case 72: ch = 'k'; goto pseudo_vi_command;
3657 case 73: ch = 'A'; goto vi_xfunc_search_up;
3658 case 75: ch = 'h'; goto pseudo_vi_command;
3659 case 77: ch = 'l'; goto pseudo_vi_command;
3660 case 79: ch = '$'; goto pseudo_vi_command;
3661 case 80: ch = 'j'; goto pseudo_vi_command;
3662 case 83: ch = 'x'; goto pseudo_vi_command;
3663 default: ch = 0; goto vi_insert_failed;
3664 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003665 if (insert != 0) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07003666 if (ch == CTRL_V) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003667 state = VLIT;
3668 ch = '^';
3669 }
3670 switch (vi_insert(ch)) {
3671 case -1:
Elliott Hughes96b43632015-07-17 11:39:41 -07003672 vi_insert_failed:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003673 vi_error();
3674 state = VNORMAL;
3675 break;
3676 case 0:
3677 if (state == VLIT) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003678 vs->cursor--;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003679 refresh(0);
3680 } else
3681 refresh(insert != 0);
3682 break;
3683 case 1:
3684 return (1);
3685 }
3686 } else {
Elliott Hughes23925bb2017-09-22 16:04:20 -07003687 if (ctype(ch, C_CR | C_LF))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003688 return (1);
3689 cmdlen = 0;
3690 argc1 = 0;
Elliott Hughesdd4abe02018-02-05 15:55:19 -08003691 if (ctype(ch, C_DIGIT) && ord(ch) != ORD('0')) {
Elliott Hughes96b43632015-07-17 11:39:41 -07003692 argc1 = ksh_numdig(ch);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003693 state = VARG1;
3694 } else {
Elliott Hughes96b43632015-07-17 11:39:41 -07003695 pseudo_vi_command:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003696 curcmd[cmdlen++] = ch;
3697 state = nextstate(ch);
3698 if (state == VSEARCH) {
3699 save_cbuf();
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003700 vs->cursor = 0;
3701 vs->linelen = 0;
Thorsten Glaser811a5752013-07-25 14:24:45 +00003702 if (putbuf(ch == '/' ? "/" : "?", 1,
3703 false) != 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003704 return (-1);
3705 refresh(0);
3706 }
3707 if (state == VVERSION) {
3708 save_cbuf();
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003709 vs->cursor = 0;
3710 vs->linelen = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003711 putbuf(KSH_VERSION,
Thorsten Glaser811a5752013-07-25 14:24:45 +00003712 strlen(KSH_VERSION), false);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003713 refresh(0);
3714 }
3715 }
3716 }
3717 break;
3718
3719 case VLIT:
3720 if (is_bad(ch)) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003721 del_range(vs->cursor, vs->cursor + 1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003722 vi_error();
3723 } else
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003724 vs->cbuf[vs->cursor++] = ch;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003725 refresh(1);
3726 state = VNORMAL;
3727 break;
3728
3729 case VVERSION:
3730 restore_cbuf();
3731 state = VNORMAL;
3732 refresh(0);
3733 break;
3734
3735 case VARG1:
Elliott Hughes23925bb2017-09-22 16:04:20 -07003736 if (ctype(ch, C_DIGIT))
Elliott Hughes96b43632015-07-17 11:39:41 -07003737 argc1 = argc1 * 10 + ksh_numdig(ch);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003738 else {
3739 curcmd[cmdlen++] = ch;
3740 state = nextstate(ch);
3741 }
3742 break;
3743
3744 case VEXTCMD:
3745 argc2 = 0;
Elliott Hughesdd4abe02018-02-05 15:55:19 -08003746 if (ctype(ch, C_DIGIT) && ord(ch) != ORD('0')) {
Elliott Hughes96b43632015-07-17 11:39:41 -07003747 argc2 = ksh_numdig(ch);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003748 state = VARG2;
3749 return (0);
3750 } else {
3751 curcmd[cmdlen++] = ch;
3752 if (ch == curcmd[0])
3753 state = VCMD;
3754 else if (is_move(ch))
3755 state = nextstate(ch);
3756 else
3757 state = VFAIL;
3758 }
3759 break;
3760
3761 case VARG2:
Elliott Hughes23925bb2017-09-22 16:04:20 -07003762 if (ctype(ch, C_DIGIT))
Elliott Hughes96b43632015-07-17 11:39:41 -07003763 argc2 = argc2 * 10 + ksh_numdig(ch);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003764 else {
3765 if (argc1 == 0)
3766 argc1 = argc2;
3767 else
3768 argc1 *= argc2;
3769 curcmd[cmdlen++] = ch;
3770 if (ch == curcmd[0])
3771 state = VCMD;
3772 else if (is_move(ch))
3773 state = nextstate(ch);
3774 else
3775 state = VFAIL;
3776 }
3777 break;
3778
3779 case VXCH:
Elliott Hughes23925bb2017-09-22 16:04:20 -07003780 if (ch == CTRL_BO)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003781 state = VNORMAL;
3782 else {
3783 curcmd[cmdlen++] = ch;
3784 state = VCMD;
3785 }
3786 break;
3787
3788 case VSEARCH:
Elliott Hughes23925bb2017-09-22 16:04:20 -07003789 if (ctype(ch, C_CR | C_LF) /* || ch == CTRL_BO */ ) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003790 restore_cbuf();
3791 /* Repeat last search? */
3792 if (srchlen == 0) {
3793 if (!srchpat[0]) {
3794 vi_error();
3795 state = VNORMAL;
3796 refresh(0);
3797 return (0);
3798 }
3799 } else {
3800 locpat[srchlen] = '\0';
3801 memcpy(srchpat, locpat, srchlen + 1);
3802 }
3803 state = VCMD;
Elliott Hughes23925bb2017-09-22 16:04:20 -07003804 } else if (isched(ch, edchars.erase) || ch == CTRL_H) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003805 if (srchlen != 0) {
3806 srchlen--;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003807 vs->linelen -= char_len(locpat[srchlen]);
3808 vs->cursor = vs->linelen;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003809 refresh(0);
3810 return (0);
3811 }
3812 restore_cbuf();
3813 state = VNORMAL;
3814 refresh(0);
Elliott Hughes77740fc2016-08-12 15:06:53 -07003815 } else if (isched(ch, edchars.kill)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003816 srchlen = 0;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003817 vs->linelen = 1;
3818 vs->cursor = 1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003819 refresh(0);
3820 return (0);
Elliott Hughes77740fc2016-08-12 15:06:53 -07003821 } else if (isched(ch, edchars.werase)) {
Elliott Hughes737fdce2014-08-07 12:59:26 -07003822 unsigned int i, n;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003823 struct edstate new_es, *save_es;
3824
Elliott Hughes737fdce2014-08-07 12:59:26 -07003825 new_es.cursor = srchlen;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003826 new_es.cbuf = locpat;
3827
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003828 save_es = vs;
3829 vs = &new_es;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003830 n = backword(1);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003831 vs = save_es;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003832
Elliott Hughes737fdce2014-08-07 12:59:26 -07003833 i = (unsigned)srchlen;
3834 while (--i >= n)
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003835 vs->linelen -= char_len(locpat[i]);
Elliott Hughes737fdce2014-08-07 12:59:26 -07003836 srchlen = (int)n;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003837 vs->cursor = vs->linelen;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003838 refresh(0);
3839 return (0);
3840 } else {
3841 if (srchlen == SRCHLEN - 1)
3842 vi_error();
3843 else {
3844 locpat[srchlen++] = ch;
Elliott Hughes23925bb2017-09-22 16:04:20 -07003845 if (ksh_isctrl(ch)) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003846 if ((size_t)vs->linelen + 2 >
3847 (size_t)vs->cbufsize)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003848 vi_error();
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003849 vs->cbuf[vs->linelen++] = '^';
Elliott Hughes23925bb2017-09-22 16:04:20 -07003850 vs->cbuf[vs->linelen++] = ksh_unctrl(ch);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003851 } else {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003852 if (vs->linelen >= vs->cbufsize)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003853 vi_error();
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003854 vs->cbuf[vs->linelen++] = ch;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003855 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003856 vs->cursor = vs->linelen;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003857 refresh(0);
3858 }
3859 return (0);
3860 }
3861 break;
Thorsten Glaser811a5752013-07-25 14:24:45 +00003862
3863 case VPREFIX2:
Elliott Hughes96b43632015-07-17 11:39:41 -07003864 vi_xfunc_search_up:
Thorsten Glaser811a5752013-07-25 14:24:45 +00003865 state = VFAIL;
3866 switch (ch) {
3867 case 'A':
3868 /* the cursor may not be at the BOL */
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003869 if (!vs->cursor)
Thorsten Glaser811a5752013-07-25 14:24:45 +00003870 break;
3871 /* nor further in the line than we can search for */
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003872 if ((size_t)vs->cursor >= sizeof(srchpat) - 1)
3873 vs->cursor = sizeof(srchpat) - 2;
Thorsten Glaser811a5752013-07-25 14:24:45 +00003874 /* anchor the search pattern */
3875 srchpat[0] = '^';
3876 /* take the current line up to the cursor */
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003877 memmove(srchpat + 1, vs->cbuf, vs->cursor);
3878 srchpat[vs->cursor + 1] = '\0';
Thorsten Glaser811a5752013-07-25 14:24:45 +00003879 /* set a magic flag */
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003880 argc1 = 2 + (int)vs->cursor;
Thorsten Glaser811a5752013-07-25 14:24:45 +00003881 /* and emulate a backwards history search */
3882 lastsearch = '/';
3883 *curcmd = 'n';
3884 goto pseudo_VCMD;
3885 }
3886 break;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003887 }
3888
3889 switch (state) {
3890 case VCMD:
Thorsten Glaser811a5752013-07-25 14:24:45 +00003891 pseudo_VCMD:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003892 state = VNORMAL;
3893 switch (vi_cmd(argc1, curcmd)) {
3894 case -1:
3895 vi_error();
3896 refresh(0);
3897 break;
3898 case 0:
3899 if (insert != 0)
3900 inslen = 0;
3901 refresh(insert != 0);
3902 break;
3903 case 1:
3904 refresh(0);
3905 return (1);
3906 case 2:
3907 /* back from a 'v' command - don't redraw the screen */
3908 return (1);
3909 }
3910 break;
3911
3912 case VREDO:
3913 state = VNORMAL;
3914 if (argc1 != 0)
3915 lastac = argc1;
3916 switch (vi_cmd(lastac, lastcmd)) {
3917 case -1:
3918 vi_error();
3919 refresh(0);
3920 break;
3921 case 0:
3922 if (insert != 0) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07003923 if (lastcmd[0] == 's' ||
3924 ksh_eq(lastcmd[0], 'C', 'c')) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003925 if (redo_insert(1) != 0)
3926 vi_error();
3927 } else {
3928 if (redo_insert(lastac) != 0)
3929 vi_error();
3930 }
3931 }
3932 refresh(0);
3933 break;
3934 case 1:
3935 refresh(0);
3936 return (1);
3937 case 2:
3938 /* back from a 'v' command - can't happen */
3939 break;
3940 }
3941 break;
3942
3943 case VFAIL:
3944 state = VNORMAL;
3945 vi_error();
3946 break;
3947 }
3948 return (0);
3949}
3950
3951static int
3952nextstate(int ch)
3953{
3954 if (is_extend(ch))
3955 return (VEXTCMD);
3956 else if (is_srch(ch))
3957 return (VSEARCH);
3958 else if (is_long(ch))
3959 return (VXCH);
3960 else if (ch == '.')
3961 return (VREDO);
Elliott Hughes23925bb2017-09-22 16:04:20 -07003962 else if (ch == CTRL_V)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003963 return (VVERSION);
3964 else if (is_cmd(ch))
3965 return (VCMD);
3966 else
3967 return (VFAIL);
3968}
3969
3970static int
3971vi_insert(int ch)
3972{
3973 int tcursor;
3974
Elliott Hughes23925bb2017-09-22 16:04:20 -07003975 if (isched(ch, edchars.erase) || ch == CTRL_H) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003976 if (insert == REPLACE) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003977 if (vs->cursor == undo->cursor) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003978 vi_error();
3979 return (0);
3980 }
3981 if (inslen > 0)
3982 inslen--;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003983 vs->cursor--;
3984 if (vs->cursor >= undo->linelen)
3985 vs->linelen--;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003986 else
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003987 vs->cbuf[vs->cursor] = undo->cbuf[vs->cursor];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003988 } else {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003989 if (vs->cursor == 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003990 return (0);
3991 if (inslen > 0)
3992 inslen--;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003993 vs->cursor--;
3994 vs->linelen--;
3995 memmove(&vs->cbuf[vs->cursor], &vs->cbuf[vs->cursor + 1],
3996 vs->linelen - vs->cursor + 1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07003997 }
3998 expanded = NONE;
3999 return (0);
4000 }
Elliott Hughes77740fc2016-08-12 15:06:53 -07004001 if (isched(ch, edchars.kill)) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004002 if (vs->cursor != 0) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004003 inslen = 0;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004004 memmove(vs->cbuf, &vs->cbuf[vs->cursor],
4005 vs->linelen - vs->cursor);
4006 vs->linelen -= vs->cursor;
4007 vs->cursor = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004008 }
4009 expanded = NONE;
4010 return (0);
4011 }
Elliott Hughes77740fc2016-08-12 15:06:53 -07004012 if (isched(ch, edchars.werase)) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004013 if (vs->cursor != 0) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004014 tcursor = backword(1);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004015 memmove(&vs->cbuf[tcursor], &vs->cbuf[vs->cursor],
4016 vs->linelen - vs->cursor);
4017 vs->linelen -= vs->cursor - tcursor;
4018 if (inslen < vs->cursor - tcursor)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004019 inslen = 0;
4020 else
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004021 inslen -= vs->cursor - tcursor;
4022 vs->cursor = tcursor;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004023 }
4024 expanded = NONE;
4025 return (0);
4026 }
Geremy Condra03ebf062011-10-12 18:17:24 -07004027 /*
4028 * If any chars are entered before escape, trash the saved insert
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004029 * buffer (if user inserts & deletes char, ibuf gets trashed and
4030 * we don't want to use it)
4031 */
Elliott Hughes23925bb2017-09-22 16:04:20 -07004032 if (first_insert && ch != CTRL_BO)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004033 saved_inslen = 0;
4034 switch (ch) {
4035 case '\0':
4036 return (-1);
4037
4038 case '\r':
4039 case '\n':
4040 return (1);
4041
Elliott Hughes23925bb2017-09-22 16:04:20 -07004042 case CTRL_BO:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004043 expanded = NONE;
4044 if (first_insert) {
Thorsten Glaser811a5752013-07-25 14:24:45 +00004045 first_insert = false;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004046 if (inslen == 0) {
4047 inslen = saved_inslen;
4048 return (redo_insert(0));
4049 }
4050 lastcmd[0] = 'a';
4051 lastac = 1;
4052 }
Elliott Hughes23925bb2017-09-22 16:04:20 -07004053 if (lastcmd[0] == 's' || ksh_eq(lastcmd[0], 'C', 'c'))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004054 return (redo_insert(0));
4055 else
4056 return (redo_insert(lastac - 1));
4057
Elliott Hughes966dd552016-12-08 15:56:04 -08004058 /* { start nonstandard vi commands */
Elliott Hughes23925bb2017-09-22 16:04:20 -07004059 case CTRL_X:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004060 expand_word(0);
4061 break;
4062
Elliott Hughes23925bb2017-09-22 16:04:20 -07004063 case CTRL_F:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004064 complete_word(0, 0);
4065 break;
4066
Elliott Hughes23925bb2017-09-22 16:04:20 -07004067 case CTRL_E:
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004068 print_expansions(vs, 0);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004069 break;
4070
Elliott Hughes23925bb2017-09-22 16:04:20 -07004071 case CTRL_I:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004072 if (Flag(FVITABCOMPLETE)) {
4073 complete_word(0, 0);
4074 break;
4075 }
4076 /* FALLTHROUGH */
Elliott Hughes966dd552016-12-08 15:56:04 -08004077 /* end nonstandard vi commands } */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004078
4079 default:
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004080 if (vs->linelen >= vs->cbufsize - 1)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004081 return (-1);
4082 ibuf[inslen++] = ch;
4083 if (insert == INSERT) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004084 memmove(&vs->cbuf[vs->cursor + 1], &vs->cbuf[vs->cursor],
4085 vs->linelen - vs->cursor);
4086 vs->linelen++;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004087 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004088 vs->cbuf[vs->cursor++] = ch;
4089 if (insert == REPLACE && vs->cursor > vs->linelen)
4090 vs->linelen++;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004091 expanded = NONE;
4092 }
4093 return (0);
4094}
4095
4096static int
4097vi_cmd(int argcnt, const char *cmd)
4098{
4099 int ncursor;
4100 int cur, c1, c2, c3 = 0;
4101 int any;
4102 struct edstate *t;
4103
4104 if (argcnt == 0 && !is_zerocount(*cmd))
4105 argcnt = 1;
4106
4107 if (is_move(*cmd)) {
4108 if ((cur = domove(argcnt, cmd, 0)) >= 0) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004109 if (cur == vs->linelen && cur != 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004110 cur--;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004111 vs->cursor = cur;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004112 } else
4113 return (-1);
4114 } else {
4115 /* Don't save state in middle of macro.. */
4116 if (is_undoable(*cmd) && !macro.p) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004117 undo->winleft = vs->winleft;
4118 memmove(undo->cbuf, vs->cbuf, vs->linelen);
4119 undo->linelen = vs->linelen;
4120 undo->cursor = vs->cursor;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004121 lastac = argcnt;
4122 memmove(lastcmd, cmd, MAXVICMD);
4123 }
Elliott Hughes23925bb2017-09-22 16:04:20 -07004124 switch (ord(*cmd)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004125
Elliott Hughes23925bb2017-09-22 16:04:20 -07004126 case CTRL_L:
4127 case CTRL_R:
Geremy Condra03ebf062011-10-12 18:17:24 -07004128 redraw_line(true);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004129 break;
4130
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004131 case ORD('@'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004132 {
4133 static char alias[] = "_\0";
4134 struct tbl *ap;
Geremy Condra03ebf062011-10-12 18:17:24 -07004135 size_t olen, nlen;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004136 char *p, *nbuf;
4137
4138 /* lookup letter in alias list... */
4139 alias[1] = cmd[1];
4140 ap = ktsearch(&aliases, alias, hash(alias));
4141 if (!cmd[1] || !ap || !(ap->flag & ISSET))
4142 return (-1);
4143 /* check if this is a recursive call... */
4144 if ((p = (char *)macro.p))
4145 while ((p = strnul(p)) && p[1])
4146 if (*++p == cmd[1])
4147 return (-1);
4148 /* insert alias into macro buffer */
4149 nlen = strlen(ap->val.s) + 1;
4150 olen = !macro.p ? 2 :
4151 macro.len - (macro.p - macro.buf);
Geremy Condra03ebf062011-10-12 18:17:24 -07004152 /*
4153 * at this point, it's fairly reasonable that
4154 * nlen + olen + 2 doesn't overflow
4155 */
Thorsten Glaser811a5752013-07-25 14:24:45 +00004156 nbuf = alloc(nlen + 1 + olen, AEDIT);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004157 memcpy(nbuf, ap->val.s, nlen);
4158 nbuf[nlen++] = cmd[1];
4159 if (macro.p) {
4160 memcpy(nbuf + nlen, macro.p, olen);
Thorsten Glaser811a5752013-07-25 14:24:45 +00004161 afree(macro.buf, AEDIT);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004162 nlen += olen;
4163 } else {
4164 nbuf[nlen++] = '\0';
4165 nbuf[nlen++] = '\0';
4166 }
4167 macro.p = macro.buf = (unsigned char *)nbuf;
4168 macro.len = nlen;
4169 }
4170 break;
4171
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004172 case ORD('a'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004173 modified = 1;
4174 hnum = hlast;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004175 if (vs->linelen != 0)
4176 vs->cursor++;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004177 insert = INSERT;
4178 break;
4179
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004180 case ORD('A'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004181 modified = 1;
4182 hnum = hlast;
4183 del_range(0, 0);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004184 vs->cursor = vs->linelen;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004185 insert = INSERT;
4186 break;
4187
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004188 case ORD('S'):
Elliott Hughes23925bb2017-09-22 16:04:20 -07004189 vs->cursor = domovebeg();
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004190 del_range(vs->cursor, vs->linelen);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004191 modified = 1;
4192 hnum = hlast;
4193 insert = INSERT;
4194 break;
4195
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004196 case ORD('Y'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004197 cmd = "y$";
4198 /* ahhhhhh... */
Elliott Hughes966dd552016-12-08 15:56:04 -08004199
4200 /* FALLTHROUGH */
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004201 case ORD('c'):
4202 case ORD('d'):
4203 case ORD('y'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004204 if (*cmd == cmd[1]) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07004205 c1 = *cmd == 'c' ? domovebeg() : 0;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004206 c2 = vs->linelen;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004207 } else if (!is_move(cmd[1]))
4208 return (-1);
4209 else {
4210 if ((ncursor = domove(argcnt, &cmd[1], 1)) < 0)
4211 return (-1);
Elliott Hughes23925bb2017-09-22 16:04:20 -07004212 if (*cmd == 'c' && ksh_eq(cmd[1], 'W', 'w') &&
4213 !ctype(vs->cbuf[vs->cursor], C_SPACE)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004214 do {
4215 --ncursor;
Elliott Hughes23925bb2017-09-22 16:04:20 -07004216 } while (ctype(vs->cbuf[ncursor], C_SPACE));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004217 ncursor++;
4218 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004219 if (ncursor > vs->cursor) {
4220 c1 = vs->cursor;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004221 c2 = ncursor;
4222 } else {
4223 c1 = ncursor;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004224 c2 = vs->cursor;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004225 if (cmd[1] == '%')
4226 c2++;
4227 }
4228 }
4229 if (*cmd != 'c' && c1 != c2)
4230 yank_range(c1, c2);
4231 if (*cmd != 'y') {
4232 del_range(c1, c2);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004233 vs->cursor = c1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004234 }
4235 if (*cmd == 'c') {
4236 modified = 1;
4237 hnum = hlast;
4238 insert = INSERT;
4239 }
4240 break;
4241
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004242 case ORD('p'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004243 modified = 1;
4244 hnum = hlast;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004245 if (vs->linelen != 0)
4246 vs->cursor++;
Thorsten Glaser811a5752013-07-25 14:24:45 +00004247 while (putbuf(ybuf, yanklen, false) == 0 &&
4248 --argcnt > 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004249 ;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004250 if (vs->cursor != 0)
4251 vs->cursor--;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004252 if (argcnt != 0)
4253 return (-1);
4254 break;
4255
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004256 case ORD('P'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004257 modified = 1;
4258 hnum = hlast;
4259 any = 0;
Thorsten Glaser811a5752013-07-25 14:24:45 +00004260 while (putbuf(ybuf, yanklen, false) == 0 &&
4261 --argcnt > 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004262 any = 1;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004263 if (any && vs->cursor != 0)
4264 vs->cursor--;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004265 if (argcnt != 0)
4266 return (-1);
4267 break;
4268
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004269 case ORD('C'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004270 modified = 1;
4271 hnum = hlast;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004272 del_range(vs->cursor, vs->linelen);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004273 insert = INSERT;
4274 break;
4275
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004276 case ORD('D'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004277 yank_range(vs->cursor, vs->linelen);
4278 del_range(vs->cursor, vs->linelen);
4279 if (vs->cursor != 0)
4280 vs->cursor--;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004281 break;
4282
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004283 case ORD('g'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004284 if (!argcnt)
4285 argcnt = hlast;
4286 /* FALLTHROUGH */
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004287 case ORD('G'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004288 if (!argcnt)
4289 argcnt = 1;
4290 else
4291 argcnt = hlast - (source->line - argcnt);
4292 if (grabhist(modified, argcnt - 1) < 0)
4293 return (-1);
4294 else {
4295 modified = 0;
4296 hnum = argcnt - 1;
4297 }
4298 break;
4299
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004300 case ORD('i'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004301 modified = 1;
4302 hnum = hlast;
4303 insert = INSERT;
4304 break;
4305
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004306 case ORD('I'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004307 modified = 1;
4308 hnum = hlast;
Elliott Hughes23925bb2017-09-22 16:04:20 -07004309 vs->cursor = domovebeg();
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004310 insert = INSERT;
4311 break;
4312
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004313 case ORD('j'):
4314 case ORD('+'):
Elliott Hughes23925bb2017-09-22 16:04:20 -07004315 case CTRL_N:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004316 if (grabhist(modified, hnum + argcnt) < 0)
4317 return (-1);
4318 else {
4319 modified = 0;
4320 hnum += argcnt;
4321 }
4322 break;
4323
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004324 case ORD('k'):
4325 case ORD('-'):
Elliott Hughes23925bb2017-09-22 16:04:20 -07004326 case CTRL_P:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004327 if (grabhist(modified, hnum - argcnt) < 0)
4328 return (-1);
4329 else {
4330 modified = 0;
4331 hnum -= argcnt;
4332 }
4333 break;
4334
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004335 case ORD('r'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004336 if (vs->linelen == 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004337 return (-1);
4338 modified = 1;
4339 hnum = hlast;
4340 if (cmd[1] == 0)
4341 vi_error();
4342 else {
4343 int n;
4344
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004345 if (vs->cursor + argcnt > vs->linelen)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004346 return (-1);
4347 for (n = 0; n < argcnt; ++n)
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004348 vs->cbuf[vs->cursor + n] = cmd[1];
4349 vs->cursor += n - 1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004350 }
4351 break;
4352
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004353 case ORD('R'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004354 modified = 1;
4355 hnum = hlast;
4356 insert = REPLACE;
4357 break;
4358
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004359 case ORD('s'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004360 if (vs->linelen == 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004361 return (-1);
4362 modified = 1;
4363 hnum = hlast;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004364 if (vs->cursor + argcnt > vs->linelen)
4365 argcnt = vs->linelen - vs->cursor;
4366 del_range(vs->cursor, vs->cursor + argcnt);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004367 insert = INSERT;
4368 break;
4369
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004370 case ORD('v'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004371 if (!argcnt) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004372 if (vs->linelen == 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004373 return (-1);
4374 if (modified) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004375 vs->cbuf[vs->linelen] = '\0';
4376 histsave(&source->line, vs->cbuf,
Elliott Hughes96b43632015-07-17 11:39:41 -07004377 HIST_STORE, true);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004378 } else
4379 argcnt = source->line + 1 -
4380 (hlast - hnum);
4381 }
4382 if (argcnt)
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004383 shf_snprintf(vs->cbuf, vs->cbufsize, Tf_sd,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004384 "fc -e ${VISUAL:-${EDITOR:-vi}} --",
4385 argcnt);
4386 else
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004387 strlcpy(vs->cbuf,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004388 "fc -e ${VISUAL:-${EDITOR:-vi}} --",
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004389 vs->cbufsize);
4390 vs->linelen = strlen(vs->cbuf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004391 return (2);
4392
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004393 case ORD('x'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004394 if (vs->linelen == 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004395 return (-1);
4396 modified = 1;
4397 hnum = hlast;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004398 if (vs->cursor + argcnt > vs->linelen)
4399 argcnt = vs->linelen - vs->cursor;
4400 yank_range(vs->cursor, vs->cursor + argcnt);
4401 del_range(vs->cursor, vs->cursor + argcnt);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004402 break;
4403
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004404 case ORD('X'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004405 if (vs->cursor > 0) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004406 modified = 1;
4407 hnum = hlast;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004408 if (vs->cursor < argcnt)
4409 argcnt = vs->cursor;
4410 yank_range(vs->cursor - argcnt, vs->cursor);
4411 del_range(vs->cursor - argcnt, vs->cursor);
4412 vs->cursor -= argcnt;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004413 } else
4414 return (-1);
4415 break;
4416
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004417 case ORD('u'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004418 t = vs;
4419 vs = undo;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004420 undo = t;
4421 break;
4422
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004423 case ORD('U'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004424 if (!modified)
4425 return (-1);
4426 if (grabhist(modified, ohnum) < 0)
4427 return (-1);
4428 modified = 0;
4429 hnum = ohnum;
4430 break;
4431
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004432 case ORD('?'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004433 if (hnum == hlast)
4434 hnum = -1;
4435 /* ahhh */
Elliott Hughes966dd552016-12-08 15:56:04 -08004436
4437 /* FALLTHROUGH */
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004438 case ORD('/'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004439 c3 = 1;
4440 srchlen = 0;
4441 lastsearch = *cmd;
4442 /* FALLTHROUGH */
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004443 case ORD('n'):
4444 case ORD('N'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004445 if (lastsearch == ' ')
4446 return (-1);
4447 if (lastsearch == '?')
4448 c1 = 1;
4449 else
4450 c1 = 0;
4451 if (*cmd == 'N')
4452 c1 = !c1;
4453 if ((c2 = grabsearch(modified, hnum,
4454 c1, srchpat)) < 0) {
4455 if (c3) {
4456 restore_cbuf();
4457 refresh(0);
4458 }
4459 return (-1);
4460 } else {
4461 modified = 0;
4462 hnum = c2;
4463 ohnum = hnum;
4464 }
Thorsten Glaser811a5752013-07-25 14:24:45 +00004465 if (argcnt >= 2) {
4466 /* flag from cursor-up command */
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004467 vs->cursor = argcnt - 2;
Thorsten Glaser811a5752013-07-25 14:24:45 +00004468 return (0);
4469 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004470 break;
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004471 case ORD('_'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004472 {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00004473 bool inspace;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004474 char *p, *sp;
4475
4476 if (histnum(-1) < 0)
4477 return (-1);
4478 p = *histpos();
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004479 if (argcnt) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07004480 while (ctype(*p, C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004481 p++;
4482 while (*p && --argcnt) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07004483 while (*p && !ctype(*p, C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004484 p++;
Elliott Hughes23925bb2017-09-22 16:04:20 -07004485 while (ctype(*p, C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004486 p++;
4487 }
4488 if (!*p)
4489 return (-1);
4490 sp = p;
4491 } else {
4492 sp = p;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00004493 inspace = false;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004494 while (*p) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07004495 if (ctype(*p, C_SPACE))
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00004496 inspace = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004497 else if (inspace) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00004498 inspace = false;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004499 sp = p;
4500 }
4501 p++;
4502 }
4503 p = sp;
4504 }
4505 modified = 1;
4506 hnum = hlast;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004507 if (vs->cursor != vs->linelen)
4508 vs->cursor++;
Elliott Hughes23925bb2017-09-22 16:04:20 -07004509 while (*p && !ctype(*p, C_SPACE)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004510 argcnt++;
4511 p++;
4512 }
Elliott Hughes77740fc2016-08-12 15:06:53 -07004513 if (putbuf(T1space, 1, false) != 0 ||
Thorsten Glaser811a5752013-07-25 14:24:45 +00004514 putbuf(sp, argcnt, false) != 0) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004515 if (vs->cursor != 0)
4516 vs->cursor--;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004517 return (-1);
4518 }
4519 insert = INSERT;
4520 }
4521 break;
4522
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004523 case ORD('~'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004524 {
4525 char *p;
4526 int i;
4527
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004528 if (vs->linelen == 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004529 return (-1);
4530 for (i = 0; i < argcnt; i++) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004531 p = &vs->cbuf[vs->cursor];
Elliott Hughes23925bb2017-09-22 16:04:20 -07004532 if (ctype(*p, C_LOWER)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004533 modified = 1;
4534 hnum = hlast;
4535 *p = ksh_toupper(*p);
Elliott Hughes23925bb2017-09-22 16:04:20 -07004536 } else if (ctype(*p, C_UPPER)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004537 modified = 1;
4538 hnum = hlast;
4539 *p = ksh_tolower(*p);
4540 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004541 if (vs->cursor < vs->linelen - 1)
4542 vs->cursor++;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004543 }
4544 break;
4545 }
4546
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004547 case ORD('#'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004548 {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004549 int ret = x_do_comment(vs->cbuf, vs->cbufsize,
4550 &vs->linelen);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004551 if (ret >= 0)
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004552 vs->cursor = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004553 return (ret);
4554 }
4555
Geremy Condra03ebf062011-10-12 18:17:24 -07004556 /* AT&T ksh */
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004557 case ORD('='):
Geremy Condra03ebf062011-10-12 18:17:24 -07004558 /* Nonstandard vi/ksh */
Elliott Hughes23925bb2017-09-22 16:04:20 -07004559 case CTRL_E:
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004560 print_expansions(vs, 1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004561 break;
4562
4563
Geremy Condra03ebf062011-10-12 18:17:24 -07004564 /* Nonstandard vi/ksh */
Elliott Hughes23925bb2017-09-22 16:04:20 -07004565 case CTRL_I:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004566 if (!Flag(FVITABCOMPLETE))
4567 return (-1);
4568 complete_word(1, argcnt);
4569 break;
4570
Geremy Condra03ebf062011-10-12 18:17:24 -07004571 /* some annoying AT&T kshs */
Elliott Hughes23925bb2017-09-22 16:04:20 -07004572 case CTRL_BO:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004573 if (!Flag(FVIESCCOMPLETE))
4574 return (-1);
Elliott Hughes966dd552016-12-08 15:56:04 -08004575 /* FALLTHROUGH */
Geremy Condra03ebf062011-10-12 18:17:24 -07004576 /* AT&T ksh */
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004577 case ORD('\\'):
Geremy Condra03ebf062011-10-12 18:17:24 -07004578 /* Nonstandard vi/ksh */
Elliott Hughes23925bb2017-09-22 16:04:20 -07004579 case CTRL_F:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004580 complete_word(1, argcnt);
4581 break;
4582
4583
Geremy Condra03ebf062011-10-12 18:17:24 -07004584 /* AT&T ksh */
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004585 case ORD('*'):
Geremy Condra03ebf062011-10-12 18:17:24 -07004586 /* Nonstandard vi/ksh */
Elliott Hughes23925bb2017-09-22 16:04:20 -07004587 case CTRL_X:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004588 expand_word(1);
4589 break;
Thorsten Glaser811a5752013-07-25 14:24:45 +00004590
4591
4592 /* mksh: cursor movement */
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004593 case ORD('['):
4594 case ORD('O'):
Thorsten Glaser811a5752013-07-25 14:24:45 +00004595 state = VPREFIX2;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004596 if (vs->linelen != 0)
4597 vs->cursor++;
Thorsten Glaser811a5752013-07-25 14:24:45 +00004598 insert = INSERT;
4599 return (0);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004600 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004601 if (insert == 0 && vs->cursor != 0 && vs->cursor >= vs->linelen)
4602 vs->cursor--;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004603 }
4604 return (0);
4605}
4606
4607static int
4608domove(int argcnt, const char *cmd, int sub)
4609{
Elliott Hughes77740fc2016-08-12 15:06:53 -07004610 int ncursor = 0, i = 0, t;
4611 unsigned int bcount;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004612
Elliott Hughes23925bb2017-09-22 16:04:20 -07004613 switch (ord(*cmd)) {
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004614 case ORD('b'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004615 if (!sub && vs->cursor == 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004616 return (-1);
4617 ncursor = backword(argcnt);
4618 break;
4619
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004620 case ORD('B'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004621 if (!sub && vs->cursor == 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004622 return (-1);
4623 ncursor = Backword(argcnt);
4624 break;
4625
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004626 case ORD('e'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004627 if (!sub && vs->cursor + 1 >= vs->linelen)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004628 return (-1);
4629 ncursor = endword(argcnt);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004630 if (sub && ncursor < vs->linelen)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004631 ncursor++;
4632 break;
4633
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004634 case ORD('E'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004635 if (!sub && vs->cursor + 1 >= vs->linelen)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004636 return (-1);
4637 ncursor = Endword(argcnt);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004638 if (sub && ncursor < vs->linelen)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004639 ncursor++;
4640 break;
4641
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004642 case ORD('f'):
4643 case ORD('F'):
4644 case ORD('t'):
4645 case ORD('T'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004646 fsavecmd = *cmd;
4647 fsavech = cmd[1];
Elliott Hughes966dd552016-12-08 15:56:04 -08004648 /* FALLTHROUGH */
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004649 case ORD(','):
4650 case ORD(';'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004651 if (fsavecmd == ' ')
4652 return (-1);
Elliott Hughes23925bb2017-09-22 16:04:20 -07004653 i = ksh_eq(fsavecmd, 'F', 'f');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004654 t = fsavecmd > 'a';
4655 if (*cmd == ',')
4656 t = !t;
Thorsten Glaser811a5752013-07-25 14:24:45 +00004657 if ((ncursor = findch(fsavech, argcnt, tobool(t),
4658 tobool(i))) < 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004659 return (-1);
4660 if (sub && t)
4661 ncursor++;
4662 break;
4663
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004664 case ORD('h'):
Elliott Hughes23925bb2017-09-22 16:04:20 -07004665 case CTRL_H:
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004666 if (!sub && vs->cursor == 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004667 return (-1);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004668 ncursor = vs->cursor - argcnt;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004669 if (ncursor < 0)
4670 ncursor = 0;
4671 break;
4672
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004673 case ORD(' '):
4674 case ORD('l'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004675 if (!sub && vs->cursor + 1 >= vs->linelen)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004676 return (-1);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004677 if (vs->linelen != 0) {
4678 ncursor = vs->cursor + argcnt;
4679 if (ncursor > vs->linelen)
4680 ncursor = vs->linelen;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004681 }
4682 break;
4683
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004684 case ORD('w'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004685 if (!sub && vs->cursor + 1 >= vs->linelen)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004686 return (-1);
4687 ncursor = forwword(argcnt);
4688 break;
4689
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004690 case ORD('W'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004691 if (!sub && vs->cursor + 1 >= vs->linelen)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004692 return (-1);
4693 ncursor = Forwword(argcnt);
4694 break;
4695
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004696 case ORD('0'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004697 ncursor = 0;
4698 break;
4699
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004700 case ORD('^'):
Elliott Hughes23925bb2017-09-22 16:04:20 -07004701 ncursor = domovebeg();
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004702 break;
4703
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004704 case ORD('|'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004705 ncursor = argcnt;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004706 if (ncursor > vs->linelen)
4707 ncursor = vs->linelen;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004708 if (ncursor)
4709 ncursor--;
4710 break;
4711
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004712 case ORD('$'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004713 if (vs->linelen != 0)
4714 ncursor = vs->linelen;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004715 else
4716 ncursor = 0;
4717 break;
4718
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004719 case ORD('%'):
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004720 ncursor = vs->cursor;
4721 while (ncursor < vs->linelen &&
4722 (i = bracktype(vs->cbuf[ncursor])) == 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004723 ncursor++;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004724 if (ncursor == vs->linelen)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004725 return (-1);
4726 bcount = 1;
4727 do {
4728 if (i > 0) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004729 if (++ncursor >= vs->linelen)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004730 return (-1);
4731 } else {
4732 if (--ncursor < 0)
4733 return (-1);
4734 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004735 t = bracktype(vs->cbuf[ncursor]);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004736 if (t == i)
4737 bcount++;
4738 else if (t == -i)
4739 bcount--;
4740 } while (bcount != 0);
4741 if (sub && i > 0)
4742 ncursor++;
4743 break;
4744
4745 default:
4746 return (-1);
4747 }
4748 return (ncursor);
4749}
4750
4751static int
Elliott Hughes23925bb2017-09-22 16:04:20 -07004752domovebeg(void)
4753{
4754 int ncursor = 0;
4755
4756 while (ncursor < vs->linelen - 1 &&
4757 ctype(vs->cbuf[ncursor], C_SPACE))
4758 ncursor++;
4759 return (ncursor);
4760}
4761
4762static int
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004763redo_insert(int count)
4764{
4765 while (count-- > 0)
Thorsten Glaser811a5752013-07-25 14:24:45 +00004766 if (putbuf(ibuf, inslen, tobool(insert == REPLACE)) != 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004767 return (-1);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004768 if (vs->cursor > 0)
4769 vs->cursor--;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004770 insert = 0;
4771 return (0);
4772}
4773
4774static void
4775yank_range(int a, int b)
4776{
4777 yanklen = b - a;
4778 if (yanklen != 0)
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004779 memmove(ybuf, &vs->cbuf[a], yanklen);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004780}
4781
4782static int
4783bracktype(int ch)
4784{
Elliott Hughes23925bb2017-09-22 16:04:20 -07004785 switch (ord(ch)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004786
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004787 case ORD('('):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004788 return (1);
4789
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004790 case ORD('['):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004791 return (2);
4792
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004793 case ORD('{'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004794 return (3);
4795
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004796 case ORD(')'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004797 return (-1);
4798
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004799 case ORD(']'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004800 return (-2);
4801
Elliott Hughesdd4abe02018-02-05 15:55:19 -08004802 case ORD('}'):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004803 return (-3);
4804
4805 default:
4806 return (0);
4807 }
4808}
4809
4810/*
4811 * Non user interface editor routines below here
4812 */
4813
4814static void
4815save_cbuf(void)
4816{
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004817 memmove(holdbufp, vs->cbuf, vs->linelen);
4818 holdlen = vs->linelen;
Thorsten Glaser811a5752013-07-25 14:24:45 +00004819 holdbufp[holdlen] = '\0';
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004820}
4821
4822static void
4823restore_cbuf(void)
4824{
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004825 vs->cursor = 0;
4826 vs->linelen = holdlen;
4827 memmove(vs->cbuf, holdbufp, holdlen);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004828}
4829
4830/* return a new edstate */
4831static struct edstate *
4832save_edstate(struct edstate *old)
4833{
4834 struct edstate *news;
4835
Thorsten Glaser811a5752013-07-25 14:24:45 +00004836 news = alloc(sizeof(struct edstate), AEDIT);
4837 news->cbuf = alloc(old->cbufsize, AEDIT);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004838 memcpy(news->cbuf, old->cbuf, old->linelen);
4839 news->cbufsize = old->cbufsize;
4840 news->linelen = old->linelen;
4841 news->cursor = old->cursor;
4842 news->winleft = old->winleft;
4843 return (news);
4844}
4845
4846static void
4847restore_edstate(struct edstate *news, struct edstate *old)
4848{
4849 memcpy(news->cbuf, old->cbuf, old->linelen);
4850 news->linelen = old->linelen;
4851 news->cursor = old->cursor;
4852 news->winleft = old->winleft;
4853 free_edstate(old);
4854}
4855
4856static void
4857free_edstate(struct edstate *old)
4858{
Thorsten Glaser811a5752013-07-25 14:24:45 +00004859 afree(old->cbuf, AEDIT);
4860 afree(old, AEDIT);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004861}
4862
4863/*
4864 * this is used for calling x_escape() in complete_word()
4865 */
4866static int
4867x_vi_putbuf(const char *s, size_t len)
4868{
Thorsten Glaser811a5752013-07-25 14:24:45 +00004869 return (putbuf(s, len, false));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004870}
4871
4872static int
Thorsten Glaser811a5752013-07-25 14:24:45 +00004873putbuf(const char *buf, ssize_t len, bool repl)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004874{
4875 if (len == 0)
4876 return (0);
4877 if (repl) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004878 if (vs->cursor + len >= vs->cbufsize)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004879 return (-1);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004880 if (vs->cursor + len > vs->linelen)
4881 vs->linelen = vs->cursor + len;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004882 } else {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004883 if (vs->linelen + len >= vs->cbufsize)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004884 return (-1);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004885 memmove(&vs->cbuf[vs->cursor + len], &vs->cbuf[vs->cursor],
4886 vs->linelen - vs->cursor);
4887 vs->linelen += len;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004888 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004889 memmove(&vs->cbuf[vs->cursor], buf, len);
4890 vs->cursor += len;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004891 return (0);
4892}
4893
4894static void
4895del_range(int a, int b)
4896{
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004897 if (vs->linelen != b)
4898 memmove(&vs->cbuf[a], &vs->cbuf[b], vs->linelen - b);
4899 vs->linelen -= b - a;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004900}
4901
4902static int
Thorsten Glaser811a5752013-07-25 14:24:45 +00004903findch(int ch, int cnt, bool forw, bool incl)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004904{
4905 int ncursor;
4906
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004907 if (vs->linelen == 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004908 return (-1);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004909 ncursor = vs->cursor;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004910 while (cnt--) {
4911 do {
4912 if (forw) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004913 if (++ncursor == vs->linelen)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004914 return (-1);
4915 } else {
4916 if (--ncursor < 0)
4917 return (-1);
4918 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004919 } while (vs->cbuf[ncursor] != ch);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004920 }
4921 if (!incl) {
4922 if (forw)
4923 ncursor--;
4924 else
4925 ncursor++;
4926 }
4927 return (ncursor);
4928}
4929
4930static int
4931forwword(int argcnt)
4932{
4933 int ncursor;
4934
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004935 ncursor = vs->cursor;
4936 while (ncursor < vs->linelen && argcnt--) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07004937 if (ctype(vs->cbuf[ncursor], C_ALNUX))
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004938 while (ncursor < vs->linelen &&
Elliott Hughes23925bb2017-09-22 16:04:20 -07004939 ctype(vs->cbuf[ncursor], C_ALNUX))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004940 ncursor++;
Elliott Hughes23925bb2017-09-22 16:04:20 -07004941 else if (!ctype(vs->cbuf[ncursor], C_SPACE))
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004942 while (ncursor < vs->linelen &&
Elliott Hughes23925bb2017-09-22 16:04:20 -07004943 !ctype(vs->cbuf[ncursor], C_ALNUX | C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004944 ncursor++;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004945 while (ncursor < vs->linelen &&
Elliott Hughes23925bb2017-09-22 16:04:20 -07004946 ctype(vs->cbuf[ncursor], C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004947 ncursor++;
4948 }
4949 return (ncursor);
4950}
4951
4952static int
4953backword(int argcnt)
4954{
4955 int ncursor;
4956
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004957 ncursor = vs->cursor;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004958 while (ncursor > 0 && argcnt--) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07004959 while (--ncursor > 0 && ctype(vs->cbuf[ncursor], C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004960 ;
4961 if (ncursor > 0) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07004962 if (ctype(vs->cbuf[ncursor], C_ALNUX))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004963 while (--ncursor >= 0 &&
Elliott Hughes23925bb2017-09-22 16:04:20 -07004964 ctype(vs->cbuf[ncursor], C_ALNUX))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004965 ;
4966 else
4967 while (--ncursor >= 0 &&
Elliott Hughes23925bb2017-09-22 16:04:20 -07004968 !ctype(vs->cbuf[ncursor], C_ALNUX | C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004969 ;
4970 ncursor++;
4971 }
4972 }
4973 return (ncursor);
4974}
4975
4976static int
4977endword(int argcnt)
4978{
4979 int ncursor;
4980
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004981 ncursor = vs->cursor;
4982 while (ncursor < vs->linelen && argcnt--) {
4983 while (++ncursor < vs->linelen - 1 &&
Elliott Hughes23925bb2017-09-22 16:04:20 -07004984 ctype(vs->cbuf[ncursor], C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004985 ;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004986 if (ncursor < vs->linelen - 1) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07004987 if (ctype(vs->cbuf[ncursor], C_ALNUX))
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004988 while (++ncursor < vs->linelen &&
Elliott Hughes23925bb2017-09-22 16:04:20 -07004989 ctype(vs->cbuf[ncursor], C_ALNUX))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004990 ;
4991 else
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004992 while (++ncursor < vs->linelen &&
Elliott Hughes23925bb2017-09-22 16:04:20 -07004993 !ctype(vs->cbuf[ncursor], C_ALNUX | C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004994 ;
4995 ncursor--;
4996 }
4997 }
4998 return (ncursor);
4999}
5000
5001static int
5002Forwword(int argcnt)
5003{
5004 int ncursor;
5005
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005006 ncursor = vs->cursor;
5007 while (ncursor < vs->linelen && argcnt--) {
5008 while (ncursor < vs->linelen &&
Elliott Hughes23925bb2017-09-22 16:04:20 -07005009 !ctype(vs->cbuf[ncursor], C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005010 ncursor++;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005011 while (ncursor < vs->linelen &&
Elliott Hughes23925bb2017-09-22 16:04:20 -07005012 ctype(vs->cbuf[ncursor], C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005013 ncursor++;
5014 }
5015 return (ncursor);
5016}
5017
5018static int
5019Backword(int argcnt)
5020{
5021 int ncursor;
5022
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005023 ncursor = vs->cursor;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005024 while (ncursor > 0 && argcnt--) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07005025 while (--ncursor >= 0 && ctype(vs->cbuf[ncursor], C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005026 ;
Elliott Hughes23925bb2017-09-22 16:04:20 -07005027 while (ncursor >= 0 && !ctype(vs->cbuf[ncursor], C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005028 ncursor--;
5029 ncursor++;
5030 }
5031 return (ncursor);
5032}
5033
5034static int
5035Endword(int argcnt)
5036{
5037 int ncursor;
5038
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005039 ncursor = vs->cursor;
5040 while (ncursor < vs->linelen - 1 && argcnt--) {
5041 while (++ncursor < vs->linelen - 1 &&
Elliott Hughes23925bb2017-09-22 16:04:20 -07005042 ctype(vs->cbuf[ncursor], C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005043 ;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005044 if (ncursor < vs->linelen - 1) {
5045 while (++ncursor < vs->linelen &&
Elliott Hughes23925bb2017-09-22 16:04:20 -07005046 !ctype(vs->cbuf[ncursor], C_SPACE))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005047 ;
5048 ncursor--;
5049 }
5050 }
5051 return (ncursor);
5052}
5053
5054static int
5055grabhist(int save, int n)
5056{
5057 char *hptr;
5058
5059 if (n < 0 || n > hlast)
5060 return (-1);
5061 if (n == hlast) {
5062 restore_cbuf();
5063 ohnum = n;
5064 return (0);
5065 }
5066 (void)histnum(n);
5067 if ((hptr = *histpos()) == NULL) {
Elliott Hughes77740fc2016-08-12 15:06:53 -07005068 internal_warningf("grabhist: bad history array");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005069 return (-1);
5070 }
5071 if (save)
5072 save_cbuf();
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005073 if ((vs->linelen = strlen(hptr)) >= vs->cbufsize)
5074 vs->linelen = vs->cbufsize - 1;
5075 memmove(vs->cbuf, hptr, vs->linelen);
5076 vs->cursor = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005077 ohnum = n;
5078 return (0);
5079}
5080
5081static int
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00005082grabsearch(int save, int start, int fwd, const char *pat)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005083{
5084 char *hptr;
5085 int hist;
Elliott Hughes737fdce2014-08-07 12:59:26 -07005086 bool anchored;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005087
5088 if ((start == 0 && fwd == 0) || (start >= hlast - 1 && fwd == 1))
5089 return (-1);
5090 if (fwd)
5091 start++;
5092 else
5093 start--;
Elliott Hughes737fdce2014-08-07 12:59:26 -07005094 anchored = *pat == '^' ? (++pat, true) : false;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005095 if ((hist = findhist(start, fwd, pat, anchored)) < 0) {
Thorsten Glaser811a5752013-07-25 14:24:45 +00005096 /* (start != 0 && fwd && match(holdbufp, pat) >= 0) */
5097 if (start != 0 && fwd && strcmp(holdbufp, pat) >= 0) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005098 restore_cbuf();
5099 return (0);
5100 } else
5101 return (-1);
5102 }
5103 if (save)
5104 save_cbuf();
5105 histnum(hist);
5106 hptr = *histpos();
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005107 if ((vs->linelen = strlen(hptr)) >= vs->cbufsize)
5108 vs->linelen = vs->cbufsize - 1;
5109 memmove(vs->cbuf, hptr, vs->linelen);
5110 vs->cursor = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005111 return (hist);
5112}
5113
5114static void
Geremy Condra03ebf062011-10-12 18:17:24 -07005115redraw_line(bool newl)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005116{
Geremy Condra03ebf062011-10-12 18:17:24 -07005117 if (wbuf_len)
5118 memset(wbuf[win], ' ', wbuf_len);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005119 if (newl) {
5120 x_putc('\r');
5121 x_putc('\n');
5122 }
Elliott Hughes77740fc2016-08-12 15:06:53 -07005123 x_pprompt();
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005124 morec = ' ';
5125}
5126
5127static void
5128refresh(int leftside)
5129{
5130 if (leftside < 0)
5131 leftside = lastref;
5132 else
5133 lastref = leftside;
5134 if (outofwin())
5135 rewindow();
5136 display(wbuf[1 - win], wbuf[win], leftside);
5137 win = 1 - win;
5138}
5139
5140static int
5141outofwin(void)
5142{
5143 int cur, col;
5144
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005145 if (vs->cursor < vs->winleft)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005146 return (1);
5147 col = 0;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005148 cur = vs->winleft;
5149 while (cur < vs->cursor)
5150 col = newcol((unsigned char)vs->cbuf[cur++], col);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005151 if (col >= winwidth)
5152 return (1);
5153 return (0);
5154}
5155
5156static void
5157rewindow(void)
5158{
5159 int tcur, tcol;
5160 int holdcur1, holdcol1;
5161 int holdcur2, holdcol2;
5162
5163 holdcur1 = holdcur2 = tcur = 0;
5164 holdcol1 = holdcol2 = tcol = 0;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005165 while (tcur < vs->cursor) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005166 if (tcol - holdcol2 > winwidth / 2) {
5167 holdcur1 = holdcur2;
5168 holdcol1 = holdcol2;
5169 holdcur2 = tcur;
5170 holdcol2 = tcol;
5171 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005172 tcol = newcol((unsigned char)vs->cbuf[tcur++], tcol);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005173 }
5174 while (tcol - holdcol1 > winwidth / 2)
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005175 holdcol1 = newcol((unsigned char)vs->cbuf[holdcur1++],
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005176 holdcol1);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005177 vs->winleft = holdcur1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005178}
5179
5180static int
Elliott Hughes737fdce2014-08-07 12:59:26 -07005181newcol(unsigned char ch, int col)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005182{
5183 if (ch == '\t')
5184 return ((col | 7) + 1);
5185 return (col + char_len(ch));
5186}
5187
5188static void
5189display(char *wb1, char *wb2, int leftside)
5190{
5191 unsigned char ch;
5192 char *twb1, *twb2, mc;
5193 int cur, col, cnt;
5194 int ncol = 0;
5195 int moreright;
5196
5197 col = 0;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005198 cur = vs->winleft;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005199 moreright = 0;
5200 twb1 = wb1;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005201 while (col < winwidth && cur < vs->linelen) {
5202 if (cur == vs->cursor && leftside)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005203 ncol = col + pwidth;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005204 if ((ch = vs->cbuf[cur]) == '\t')
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005205 do {
5206 *twb1++ = ' ';
5207 } while (++col < winwidth && (col & 7) != 0);
5208 else if (col < winwidth) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07005209 if (ksh_isctrl(ch)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005210 *twb1++ = '^';
5211 if (++col < winwidth) {
Elliott Hughes23925bb2017-09-22 16:04:20 -07005212 *twb1++ = ksh_unctrl(ch);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005213 col++;
5214 }
5215 } else {
5216 *twb1++ = ch;
5217 col++;
5218 }
5219 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005220 if (cur == vs->cursor && !leftside)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005221 ncol = col + pwidth - 1;
5222 cur++;
5223 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005224 if (cur == vs->cursor)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005225 ncol = col + pwidth;
5226 if (col < winwidth) {
5227 while (col < winwidth) {
5228 *twb1++ = ' ';
5229 col++;
5230 }
5231 } else
5232 moreright++;
5233 *twb1 = ' ';
5234
5235 col = pwidth;
5236 cnt = winwidth;
5237 twb1 = wb1;
5238 twb2 = wb2;
5239 while (cnt--) {
5240 if (*twb1 != *twb2) {
Thorsten Glaser811a5752013-07-25 14:24:45 +00005241 if (x_col != col)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005242 ed_mov_opt(col, wb1);
5243 x_putc(*twb1);
Thorsten Glaser811a5752013-07-25 14:24:45 +00005244 x_col++;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005245 }
5246 twb1++;
5247 twb2++;
5248 col++;
5249 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005250 if (vs->winleft > 0 && moreright)
Geremy Condra03ebf062011-10-12 18:17:24 -07005251 /*
5252 * POSIX says to use * for this but that is a globbing
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005253 * character and may confuse people; + is more innocuous
5254 */
5255 mc = '+';
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005256 else if (vs->winleft > 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005257 mc = '<';
5258 else if (moreright)
5259 mc = '>';
5260 else
5261 mc = ' ';
5262 if (mc != morec) {
5263 ed_mov_opt(pwidth + winwidth + 1, wb1);
5264 x_putc(mc);
Thorsten Glaser811a5752013-07-25 14:24:45 +00005265 x_col++;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005266 morec = mc;
5267 }
Thorsten Glaser811a5752013-07-25 14:24:45 +00005268 if (x_col != ncol)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005269 ed_mov_opt(ncol, wb1);
5270}
5271
5272static void
5273ed_mov_opt(int col, char *wb)
5274{
Thorsten Glaser811a5752013-07-25 14:24:45 +00005275 if (col < x_col) {
5276 if (col + 1 < x_col - col) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005277 x_putc('\r');
Elliott Hughes77740fc2016-08-12 15:06:53 -07005278 x_pprompt();
Thorsten Glaser811a5752013-07-25 14:24:45 +00005279 while (x_col++ < col)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005280 x_putcf(*wb++);
5281 } else {
Thorsten Glaser811a5752013-07-25 14:24:45 +00005282 while (x_col-- > col)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005283 x_putc('\b');
5284 }
5285 } else {
Thorsten Glaser811a5752013-07-25 14:24:45 +00005286 wb = &wb[x_col - pwidth];
5287 while (x_col++ < col)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005288 x_putcf(*wb++);
5289 }
Thorsten Glaser811a5752013-07-25 14:24:45 +00005290 x_col = col;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005291}
5292
5293
5294/* replace word with all expansions (ie, expand word*) */
5295static int
5296expand_word(int cmd)
5297{
5298 static struct edstate *buf;
Geremy Condra03ebf062011-10-12 18:17:24 -07005299 int rval = 0, nwords, start, end, i;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005300 char **words;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005301
5302 /* Undo previous expansion */
5303 if (cmd == 0 && expanded == EXPAND && buf) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005304 restore_edstate(vs, buf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005305 buf = 0;
5306 expanded = NONE;
5307 return (0);
5308 }
5309 if (buf) {
5310 free_edstate(buf);
5311 buf = 0;
5312 }
5313
Geremy Condra03ebf062011-10-12 18:17:24 -07005314 i = XCF_COMMAND_FILE | XCF_FULLPATH;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005315 nwords = x_cf_glob(&i, vs->cbuf, vs->linelen, vs->cursor,
Geremy Condra03ebf062011-10-12 18:17:24 -07005316 &start, &end, &words);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005317 if (nwords == 0) {
5318 vi_error();
5319 return (-1);
5320 }
5321
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005322 buf = save_edstate(vs);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005323 expanded = EXPAND;
5324 del_range(start, end);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005325 vs->cursor = start;
Geremy Condra03ebf062011-10-12 18:17:24 -07005326 i = 0;
5327 while (i < nwords) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005328 if (x_escape(words[i], strlen(words[i]), x_vi_putbuf) != 0) {
5329 rval = -1;
5330 break;
5331 }
Elliott Hughes77740fc2016-08-12 15:06:53 -07005332 if (++i < nwords && putbuf(T1space, 1, false) != 0) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005333 rval = -1;
5334 break;
5335 }
5336 }
5337 i = buf->cursor - end;
5338 if (rval == 0 && i > 0)
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005339 vs->cursor += i;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005340 modified = 1;
5341 hnum = hlast;
5342 insert = INSERT;
5343 lastac = 0;
5344 refresh(0);
5345 return (rval);
5346}
5347
5348static int
5349complete_word(int cmd, int count)
5350{
5351 static struct edstate *buf;
Geremy Condra03ebf062011-10-12 18:17:24 -07005352 int rval, nwords, start, end, flags;
5353 size_t match_len;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005354 char **words;
5355 char *match;
Geremy Condra03ebf062011-10-12 18:17:24 -07005356 bool is_unique;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005357
5358 /* Undo previous completion */
5359 if (cmd == 0 && expanded == COMPLETE && buf) {
5360 print_expansions(buf, 0);
5361 expanded = PRINT;
5362 return (0);
5363 }
5364 if (cmd == 0 && expanded == PRINT && buf) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005365 restore_edstate(vs, buf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005366 buf = 0;
5367 expanded = NONE;
5368 return (0);
5369 }
5370 if (buf) {
5371 free_edstate(buf);
5372 buf = 0;
5373 }
5374
Geremy Condra03ebf062011-10-12 18:17:24 -07005375 /*
5376 * XCF_FULLPATH for count 'cause the menu printed by
5377 * print_expansions() was done this way.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005378 */
Geremy Condra03ebf062011-10-12 18:17:24 -07005379 flags = XCF_COMMAND_FILE;
5380 if (count)
5381 flags |= XCF_FULLPATH;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005382 nwords = x_cf_glob(&flags, vs->cbuf, vs->linelen, vs->cursor,
Geremy Condra03ebf062011-10-12 18:17:24 -07005383 &start, &end, &words);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005384 if (nwords == 0) {
5385 vi_error();
5386 return (-1);
5387 }
5388 if (count) {
5389 int i;
5390
5391 count--;
5392 if (count >= nwords) {
5393 vi_error();
Geremy Condra03ebf062011-10-12 18:17:24 -07005394 x_print_expansions(nwords, words,
5395 tobool(flags & XCF_IS_COMMAND));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005396 x_free_words(nwords, words);
Geremy Condra03ebf062011-10-12 18:17:24 -07005397 redraw_line(false);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005398 return (-1);
5399 }
5400 /*
5401 * Expand the count'th word to its basename
5402 */
Geremy Condra03ebf062011-10-12 18:17:24 -07005403 if (flags & XCF_IS_COMMAND) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005404 match = words[count] +
5405 x_basename(words[count], NULL);
5406 /* If more than one possible match, use full path */
5407 for (i = 0; i < nwords; i++)
5408 if (i != count &&
5409 strcmp(words[i] + x_basename(words[i],
5410 NULL), match) == 0) {
5411 match = words[count];
5412 break;
5413 }
5414 } else
5415 match = words[count];
5416 match_len = strlen(match);
5417 is_unique = true;
5418 /* expanded = PRINT; next call undo */
5419 } else {
5420 match = words[0];
5421 match_len = x_longest_prefix(nwords, words);
Geremy Condra03ebf062011-10-12 18:17:24 -07005422 /* next call will list completions */
5423 expanded = COMPLETE;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005424 is_unique = nwords == 1;
5425 }
5426
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005427 buf = save_edstate(vs);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005428 del_range(start, end);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005429 vs->cursor = start;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005430
Geremy Condra03ebf062011-10-12 18:17:24 -07005431 /*
5432 * escape all shell-sensitive characters and put the result into
5433 * command buffer
5434 */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005435 rval = x_escape(match, match_len, x_vi_putbuf);
5436
5437 if (rval == 0 && is_unique) {
Geremy Condra03ebf062011-10-12 18:17:24 -07005438 /*
5439 * If exact match, don't undo. Allows directory completions
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005440 * to be used (ie, complete the next portion of the path).
5441 */
5442 expanded = NONE;
5443
Geremy Condra03ebf062011-10-12 18:17:24 -07005444 /*
5445 * append a space if this is a non-directory match
5446 * and not a parameter or homedir substitution
5447 */
Elliott Hughes966dd552016-12-08 15:56:04 -08005448 if (match_len > 0 && !mksh_cdirsep(match[match_len - 1]) &&
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00005449 !(flags & XCF_IS_NOSPACE))
Elliott Hughes77740fc2016-08-12 15:06:53 -07005450 rval = putbuf(T1space, 1, false);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005451 }
5452 x_free_words(nwords, words);
5453
5454 modified = 1;
5455 hnum = hlast;
5456 insert = INSERT;
Geremy Condra03ebf062011-10-12 18:17:24 -07005457 /* prevent this from being redone... */
5458 lastac = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005459 refresh(0);
5460
5461 return (rval);
5462}
5463
5464static int
5465print_expansions(struct edstate *est, int cmd MKSH_A_UNUSED)
5466{
Geremy Condra03ebf062011-10-12 18:17:24 -07005467 int start, end, nwords, i;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005468 char **words;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005469
Geremy Condra03ebf062011-10-12 18:17:24 -07005470 i = XCF_COMMAND_FILE | XCF_FULLPATH;
5471 nwords = x_cf_glob(&i, est->cbuf, est->linelen, est->cursor,
5472 &start, &end, &words);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005473 if (nwords == 0) {
5474 vi_error();
5475 return (-1);
5476 }
Geremy Condra03ebf062011-10-12 18:17:24 -07005477 x_print_expansions(nwords, words, tobool(i & XCF_IS_COMMAND));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005478 x_free_words(nwords, words);
Geremy Condra03ebf062011-10-12 18:17:24 -07005479 redraw_line(false);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005480 return (0);
5481}
Elliott Hughes23925bb2017-09-22 16:04:20 -07005482#endif /* !MKSH_S_NOVI */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005483
5484/* Similar to x_zotc(emacs.c), but no tab weirdness */
5485static void
5486x_vi_zotc(int c)
5487{
Elliott Hughes23925bb2017-09-22 16:04:20 -07005488 if (ksh_isctrl(c)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005489 x_putc('^');
Elliott Hughes23925bb2017-09-22 16:04:20 -07005490 c = ksh_unctrl(c);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005491 }
5492 x_putc(c);
5493}
5494
Elliott Hughes23925bb2017-09-22 16:04:20 -07005495#if !MKSH_S_NOVI
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005496static void
5497vi_error(void)
5498{
5499 /* Beem out of any macros as soon as an error occurs */
5500 vi_macro_reset();
Elliott Hughes23925bb2017-09-22 16:04:20 -07005501 x_putc(KSH_BEL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005502 x_flush();
5503}
5504
5505static void
5506vi_macro_reset(void)
5507{
5508 if (macro.p) {
Thorsten Glaser811a5752013-07-25 14:24:45 +00005509 afree(macro.buf, AEDIT);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005510 memset((char *)&macro, 0, sizeof(macro));
5511 }
5512}
5513#endif /* !MKSH_S_NOVI */
Geremy Condra03ebf062011-10-12 18:17:24 -07005514
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00005515/* called from main.c */
Geremy Condra03ebf062011-10-12 18:17:24 -07005516void
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00005517x_init(void)
Geremy Condra03ebf062011-10-12 18:17:24 -07005518{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00005519 int i, j;
Geremy Condra03ebf062011-10-12 18:17:24 -07005520
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00005521 /*
Elliott Hughes77740fc2016-08-12 15:06:53 -07005522 * set edchars to force initial binding, except we need
5523 * default values for ^W for some deficient systems…
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00005524 */
5525 edchars.erase = edchars.kill = edchars.intr = edchars.quit =
Elliott Hughes77740fc2016-08-12 15:06:53 -07005526 edchars.eof = EDCHAR_INITIAL;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00005527 edchars.werase = 027;
Geremy Condra03ebf062011-10-12 18:17:24 -07005528
Thorsten Glaser811a5752013-07-25 14:24:45 +00005529 /* command line editing specific memory allocation */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00005530 ainit(AEDIT);
Thorsten Glaser811a5752013-07-25 14:24:45 +00005531 holdbufp = alloc(LINE, AEDIT);
5532
5533 /* initialise Emacs command line editing mode */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00005534 x_nextcmd = -1;
5535
5536 x_tab = alloc2(X_NTABS, sizeof(*x_tab), AEDIT);
5537 for (j = 0; j < X_TABSZ; j++)
5538 x_tab[0][j] = XFUNC_insert;
5539 for (i = 1; i < X_NTABS; i++)
5540 for (j = 0; j < X_TABSZ; j++)
5541 x_tab[i][j] = XFUNC_error;
5542 for (i = 0; i < (int)NELEM(x_defbindings); i++)
5543 x_tab[x_defbindings[i].xdb_tab][x_defbindings[i].xdb_char]
5544 = x_defbindings[i].xdb_func;
5545
5546#ifndef MKSH_SMALL
5547 x_atab = alloc2(X_NTABS, sizeof(*x_atab), AEDIT);
5548 for (i = 1; i < X_NTABS; i++)
5549 for (j = 0; j < X_TABSZ; j++)
5550 x_atab[i][j] = NULL;
Geremy Condra03ebf062011-10-12 18:17:24 -07005551#endif
Geremy Condra03ebf062011-10-12 18:17:24 -07005552}
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00005553
5554#ifdef DEBUG_LEAKS
5555void
5556x_done(void)
5557{
5558 if (x_tab != NULL)
5559 afreeall(AEDIT);
5560}
5561#endif
Elliott Hughes77740fc2016-08-12 15:06:53 -07005562
5563void
5564x_initterm(const char *termtype)
5565{
5566 /* default must be 0 (bss) */
5567 x_term_mode = 0;
5568 /* this is what tmux uses, don't ask me about it */
5569 if (!strcmp(termtype, "screen") || !strncmp(termtype, "screen-", 7))
5570 x_term_mode = 1;
5571}
5572
5573#ifndef MKSH_SMALL
5574static char *
5575x_eval_region_helper(const char *cmd, size_t len)
5576{
5577 char * volatile cp;
5578 newenv(E_ERRH);
5579
5580 if (!kshsetjmp(e->jbuf)) {
5581 char *wds = alloc(len + 3, ATEMP);
5582
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005583 wds[0] = FUNASUB;
Elliott Hughes77740fc2016-08-12 15:06:53 -07005584 memcpy(wds + 1, cmd, len);
5585 wds[len + 1] = '\0';
5586 wds[len + 2] = EOS;
5587
5588 cp = evalstr(wds, DOSCALAR);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005589 afree(wds, ATEMP);
Elliott Hughes77740fc2016-08-12 15:06:53 -07005590 strdupx(cp, cp, AEDIT);
5591 } else
5592 cp = NULL;
5593 quitenv(NULL);
5594 return (cp);
5595}
5596
5597static int
5598x_eval_region(int c MKSH_A_UNUSED)
5599{
5600 char *evbeg, *evend, *cp;
5601 size_t newlen;
5602 /* only for LINE overflow checking */
5603 size_t restlen;
5604
5605 if (xmp == NULL) {
5606 evbeg = xbuf;
5607 evend = xep;
5608 } else if (xmp < xcp) {
5609 evbeg = xmp;
5610 evend = xcp;
5611 } else {
5612 evbeg = xcp;
5613 evend = xmp;
5614 }
5615
5616 x_e_putc2('\r');
5617 x_clrtoeol(' ', false);
5618 x_flush();
5619 x_mode(false);
5620 cp = x_eval_region_helper(evbeg, evend - evbeg);
5621 x_mode(true);
5622
5623 if (cp == NULL) {
5624 /* command cannot be parsed */
5625 x_eval_region_err:
Elliott Hughes23925bb2017-09-22 16:04:20 -07005626 x_e_putc2(KSH_BEL);
Elliott Hughes77740fc2016-08-12 15:06:53 -07005627 x_redraw('\r');
5628 return (KSTD);
5629 }
5630
5631 newlen = strlen(cp);
5632 restlen = xep - evend;
5633 /* check for LINE overflow, until this is dynamically allocated */
5634 if (evbeg + newlen + restlen >= xend)
5635 goto x_eval_region_err;
5636
5637 xmp = evbeg;
5638 xcp = evbeg + newlen;
5639 xep = xcp + restlen;
5640 memmove(xcp, evend, restlen + /* NUL */ 1);
5641 memcpy(xmp, cp, newlen);
5642 afree(cp, AEDIT);
5643 x_adjust();
5644 x_modified();
5645 return (KSTD);
5646}
5647#endif /* !MKSH_SMALL */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00005648#endif /* !MKSH_NO_CMDLINE_EDITING */