1 /* $OpenBSD: edit.c,v 1.38 2013/06/03 15:41:59 tedu Exp $ */
2 /* $OpenBSD: edit.h,v 1.9 2011/05/30 17:14:35 martynas Exp $ */
3 /* $OpenBSD: emacs.c,v 1.44 2011/09/05 04:50:33 marco Exp $ */
4 /* $OpenBSD: vi.c,v 1.26 2009/06/29 22:50:19 martynas Exp $ */
7 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
9 * Thorsten Glaser <tg@mirbsd.org>
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.
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.
29 #ifndef MKSH_NO_CMDLINE_EDITING
31 __RCSID("$MirOS: src/bin/mksh/edit.c,v 1.270 2013/08/14 20:26:17 tg Exp $");
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
36 * good string is "\033c" except on hardware terminals like the DEC VT420
37 * which do a full power cycle then...
39 #ifndef MKSH_CLS_STRING
40 #define MKSH_CLS_STRING "\033[;H\033[J"
42 #ifndef MKSH_CLRTOEOL_STRING
43 #define MKSH_CLRTOEOL_STRING "\033[K"
46 /* tty driver characters we are interested in */
56 static X_chars edchars;
58 /* x_cf_glob() flags */
59 #define XCF_COMMAND BIT(0) /* Do command completion */
60 #define XCF_FILE BIT(1) /* Do file completion */
61 #define XCF_FULLPATH BIT(2) /* command completion: store full path */
62 #define XCF_COMMAND_FILE (XCF_COMMAND | XCF_FILE)
63 #define XCF_IS_COMMAND BIT(3) /* return flag: is command */
64 #define XCF_IS_NOSPACE BIT(4) /* return flag: do not append a space */
67 static int xx_cols; /* for Emacs mode */
68 static int modified; /* buffer has been "modified" */
69 static char *holdbufp; /* place to hold last edit buffer */
71 static int x_getc(void);
72 static void x_putcf(int);
73 static void x_modified(void);
74 static void x_mode(bool);
75 static int x_do_comment(char *, ssize_t, ssize_t *);
76 static void x_print_expansions(int, char * const *, bool);
77 static int x_cf_glob(int *, const char *, int, int, int *, int *, char ***);
78 static size_t x_longest_prefix(int, char * const *);
79 static void x_glob_hlp_add_qchar(char *);
80 static char *x_glob_hlp_tilde_and_rem_qchar(char *, bool);
81 static int x_basename(const char *, const char *);
82 static void x_free_words(int, char **);
83 static int x_escape(const char *, size_t, int (*)(const char *, size_t));
84 static int x_emacs(char *);
85 static void x_init_prompt(void);
87 static int x_vi(char *);
90 #define x_flush() shf_flush(shl_out)
91 #if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
92 #define x_putc(c) x_putcf(c)
94 #define x_putc(c) shf_putc((c), shl_out)
97 static int path_order_cmp(const void *, const void *);
98 static void glob_table(const char *, XPtrV *, struct table *);
99 static void glob_path(int, const char *, XPtrV *, const char *);
100 static int x_file_glob(int *, char *, char ***);
101 static int x_command_glob(int, char *, char ***);
102 static int x_locate_word(const char *, int, int, int *, bool *);
104 static int x_e_getmbc(char *);
105 static int x_e_rebuildline(const char *);
107 /* +++ generic editing functions +++ */
110 * read an edited command line
119 if (Flag(FEMACS) || Flag(FGMACS))
141 while ((n = blocking_read(STDIN_FILENO, &c, 1)) < 0 && errno == EINTR)
148 if (x_cols != xx_cols && editmode == 1) {
149 /* redraw line in Emacs mode */
151 x_e_rebuildline(MKSH_CLRTOEOL_STRING);
157 return ((n == 1) ? (int)(unsigned char)c : -1);
163 shf_putc(c, shl_out);
166 /*********************************
167 * Misc common code for vi/emacs *
168 *********************************/
171 * Handle the commenting/uncommenting of a line.
173 * 1 if a carriage return is indicated (comment added)
174 * 0 if no return (comment removed)
175 * -1 if there is an error (not enough room for comment chars)
176 * If successful, *lenp contains the new length. Note: cursor should be
177 * moved to the start of the line after (un)commenting.
180 x_do_comment(char *buf, ssize_t bsize, ssize_t *lenp)
182 ssize_t i, j, len = *lenp;
185 /* somewhat arbitrary - it's what AT&T ksh does */
188 /* Already commented? */
192 for (j = 0, i = 1; i < len; i++) {
193 if (!saw_nl || buf[i] != '#')
195 saw_nl = buf[i] == '\n';
202 /* See if there's room for the #s - 1 per \n */
203 for (i = 0; i < len; i++)
206 if (len + n >= bsize)
208 /* Now add them... */
209 for (i = len, j = len + n; --i >= 0; ) {
220 /****************************************************
221 * Common file/command completion code for vi/emacs *
222 ****************************************************/
225 x_print_expansions(int nwords, char * const *words, bool is_command)
227 bool use_copy = false;
229 XPtrV l = { NULL, 0, 0 };
232 * Check if all matches are in the same directory (in this
233 * case, we want to omit the directory name)
236 (prefix_len = x_longest_prefix(nwords, words)) > 0) {
239 /* Special case for 1 match (prefix is whole word) */
241 prefix_len = x_basename(words[0], NULL);
242 /* Any (non-trailing) slashes in non-common word suffixes? */
243 for (i = 0; i < nwords; i++)
244 if (x_basename(words[i] + prefix_len, NULL) >
247 /* All in same directory? */
249 while (prefix_len > 0 && words[0][prefix_len - 1] != '/')
252 XPinit(l, nwords + 1);
253 for (i = 0; i < nwords; i++)
254 XPput(l, words[i] + prefix_len);
259 * Enumerate expansions
263 pr_list(use_copy ? (char **)XPptrv(l) : words);
266 /* not x_free_words() */
271 * Convert backslash-escaped string to QCHAR-escaped
272 * string useful for globbing; loses QCHAR unless it
273 * can squeeze in, eg. by previous loss of backslash
276 x_glob_hlp_add_qchar(char *cp)
279 bool escaping = false;
281 while ((ch = *cp++)) {
282 if (ch == '\\' && !escaping) {
286 if (escaping || (ch == QCHAR && (cp - dp) > 1)) {
288 * empirically made list of chars to escape
289 * for globbing as well as QCHAR itself
310 * Run tilde expansion on argument string, return the result
311 * after unescaping; if the flag is set, the original string
312 * is freed if changed and assumed backslash-escaped, if not
313 * it is assumed QCHAR-escaped
316 x_glob_hlp_tilde_and_rem_qchar(char *s, bool magic_flag)
321 * On the string, check whether we have a tilde expansion,
322 * and if so, discern "~foo/bar" and "~/baz" from "~blah";
323 * if we have a directory part (the former), try to expand
325 if (*s == '~' && (cp = strchr(s, '/')) != NULL) {
326 /* ok, so split into "~foo"/"bar" or "~"/"baz" */
328 /* try to expand the tilde */
329 if (!(dp = tilde(s + 1))) {
330 /* nope, revert damage */
333 /* ok, expand and replace */
334 cp = shf_smprintf("%s/%s", dp, cp);
341 /* ... convert it from backslash-escaped via QCHAR-escaped... */
343 x_glob_hlp_add_qchar(s);
344 /* ... to unescaped, for comparison with the matches */
347 while ((ch = *cp++)) {
348 if (ch == QCHAR && !(ch = *cp++))
359 * - does expansion, checks for no match, etc.
360 * - sets *wordsp to array of matching strings
361 * - returns number of matching strings
364 x_file_glob(int *flagsp, char *toglob, char ***wordsp)
369 struct source *s, *sold;
371 /* remove all escaping backward slashes */
372 x_glob_hlp_add_qchar(toglob);
375 * Convert "foo*" (toglob) to an array of strings (words)
378 s = pushs(SWSTR, ATEMP);
379 s->start = s->str = toglob;
381 if (yylex(ONEWORD | LQCHAR) != LWORD) {
383 internal_warningf("%s: %s", "fileglob", "bad substitution");
390 while (*cp == CHAR || *cp == QCHAR)
392 nwords = DOGLOB | DOTILDE | DOMARKDIRS;
394 /* probably a $FOO expansion */
395 *flagsp |= XCF_IS_NOSPACE;
396 /* this always results in at most one match */
399 expand(yylval.cp, &w, nwords);
401 words = (char **)XPclose(w);
403 for (nwords = 0; words[nwords]; nwords++)
408 /* Expand any tilde and drop all QCHAR for comparison */
409 toglob = x_glob_hlp_tilde_and_rem_qchar(toglob, false);
412 * Check if globbing failed (returned glob pattern),
413 * but be careful (e.g. toglob == "ab*" when the file
414 * "ab*" exists is not an error).
415 * Also, check for empty result - happens if we tried
416 * to glob something which evaluated to an empty
417 * string (e.g., "$FOO" when there is no FOO, etc).
419 if ((strcmp(words[0], toglob) == 0 &&
420 stat(words[0], &statb) < 0) ||
421 words[0][0] == '\0') {
422 x_free_words(nwords, words);
428 if ((*wordsp = nwords ? words : NULL) == NULL && words != NULL)
429 x_free_words(nwords, words);
434 /* Data structure used in x_command_glob() */
435 struct path_order_info {
441 /* Compare routine used in x_command_glob() */
443 path_order_cmp(const void *aa, const void *bb)
445 const struct path_order_info *a = (const struct path_order_info *)aa;
446 const struct path_order_info *b = (const struct path_order_info *)bb;
449 t = strcmp(a->word + a->base, b->word + b->base);
450 return (t ? t : a->path_order - b->path_order);
454 x_command_glob(int flags, char *toglob, char ***wordsp)
461 /* Convert "foo*" (toglob) to a pattern for future use */
462 pat = evalstr(toglob, DOPAT | DOTILDE);
466 glob_table(pat, &w, &keywords);
467 glob_table(pat, &w, &aliases);
468 glob_table(pat, &w, &builtins);
469 for (l = e->loc; l; l = l->next)
470 glob_table(pat, &w, &l->funs);
472 glob_path(flags, pat, &w, path);
473 if ((fpath = str_val(global("FPATH"))) != null)
474 glob_path(flags, pat, &w, fpath);
484 if (flags & XCF_FULLPATH) {
485 /* Sort by basename, then path order */
486 struct path_order_info *info, *last_info = NULL;
487 char **words = (char **)XPptrv(w);
488 size_t i, path_order = 0;
490 info = (struct path_order_info *)
491 alloc2(nwords, sizeof(struct path_order_info), ATEMP);
492 for (i = 0; i < nwords; i++) {
493 info[i].word = words[i];
494 info[i].base = x_basename(words[i], NULL);
495 if (!last_info || info[i].base != last_info->base ||
496 strncmp(words[i], last_info->word, info[i].base) != 0) {
497 last_info = &info[i];
500 info[i].path_order = path_order;
502 qsort(info, nwords, sizeof(struct path_order_info),
504 for (i = 0; i < nwords; i++)
505 words[i] = info[i].word;
508 /* Sort and remove duplicate entries */
509 char **words = (char **)XPptrv(w);
512 qsort(words, nwords, sizeof(void *), xstrcmp);
513 for (i = j = 0; i < nwords - 1; i++) {
514 if (strcmp(words[i], words[i + 1]))
515 words[j++] = words[i];
517 afree(words[i], ATEMP);
519 words[j++] = words[i];
524 *wordsp = (char **)XPclose(w);
529 #define IS_WORDC(c) (!ctype(c, C_LEX1) && (c) != '\'' && (c) != '"' && \
530 (c) != '`' && (c) != '=' && (c) != ':')
533 x_locate_word(const char *buf, int buflen, int pos, int *startp,
538 /* Bad call? Probably should report error */
539 if (pos < 0 || pos > buflen) {
541 *is_commandp = false;
544 /* The case where pos == buflen happens to take care of itself... */
548 * Keep going backwards to start of word (has effect of allowing
549 * one blank after the end of a word)
551 for (; (start > 0 && IS_WORDC(buf[start - 1])) ||
552 (start > 1 && buf[start - 2] == '\\'); start--)
554 /* Go forwards to end of word */
555 for (end = start; end < buflen && IS_WORDC(buf[end]); end++) {
556 if (buf[end] == '\\' && (end + 1) < buflen)
564 /* Figure out if this is a command */
565 while (p >= 0 && ksh_isspace(buf[p]))
567 iscmd = p < 0 || vstrchr(";|&()`", buf[p]);
570 * If command has a /, path, etc. is not searched;
571 * only current directory is searched which is just
572 * like file globbing.
574 for (p = start; p < end; p++)
579 *is_commandp = iscmd;
583 return (end - start);
587 x_cf_glob(int *flagsp, const char *buf, int buflen, int pos, int *startp,
588 int *endp, char ***wordsp)
594 mkssert(buf != NULL);
596 len = x_locate_word(buf, buflen, pos, startp, &is_command);
597 if (!((*flagsp) & XCF_COMMAND))
600 * Don't do command globing on zero length strings - it takes too
601 * long and isn't very useful. File globs are more likely to be
602 * useful, so allow these.
604 if (len == 0 && is_command)
611 * Given a string, copy it and possibly add a '*' to the end.
614 strndupx(toglob, buf + *startp, len + /* the '*' */ 1, ATEMP);
618 * If the pathname contains a wildcard (an unquoted '*',
619 * '?', or '[') or an extglob, then it is globbed based
620 * on that value (i.e., without the appended '*'). Same
621 * for parameter substitutions (as in “cat $HOME/.ss↹”)
622 * without appending a trailing space (LP: #710539), as
623 * well as for “~foo” (but not “~foo/”).
625 for (s = toglob; *s; s++) {
626 if (*s == '\\' && s[1])
628 else if (*s == '?' || *s == '*' || *s == '[' ||
630 /* ?() *() +() @() !() but two already checked */
631 (s[1] == '(' /*)*/ &&
632 (*s == '+' || *s == '@' || *s == '!'))) {
634 * just expand based on the extglob
641 if (*toglob == '~' && !vstrchr(toglob, '/')) {
642 /* neither for '~foo' (but '~foo/bar') */
643 *flagsp |= XCF_IS_NOSPACE;
649 toglob[len + 1] = '\0';
652 * Expand (glob) it now.
655 nwords = is_command ?
656 x_command_glob(*flagsp, toglob, &words) :
657 x_file_glob(flagsp, toglob, &words);
658 afree(toglob, ATEMP);
665 *flagsp |= XCF_IS_COMMAND;
667 *endp = *startp + len;
673 * Find longest common prefix
676 x_longest_prefix(int nwords, char * const * words)
679 size_t j, prefix_len;
685 prefix_len = strlen(words[0]);
686 for (i = 1; i < nwords; i++)
687 for (j = 0, p = words[i]; j < prefix_len; j++)
688 if (p[j] != words[0][j]) {
692 /* false for nwords==1 as 0 = words[0][prefix_len] then */
693 if (UTFMODE && prefix_len && (words[0][prefix_len] & 0xC0) == 0x80)
694 while (prefix_len && (words[0][prefix_len] & 0xC0) != 0xC0)
700 x_free_words(int nwords, char **words)
703 afree(words[--nwords], ATEMP);
708 * Return the offset of the basename of string s (which ends at se - need not
709 * be null terminated). Trailing slashes are ignored. If s is just a slash,
710 * then the offset is 0 (actually, length - 1).
721 x_basename(const char *s, const char *se)
730 /* Skip trailing slashes */
731 for (p = se - 1; p > s && *p == '/'; p--)
733 for (; p > s && *p != '/'; p--)
735 if (*p == '/' && p + 1 < se)
742 * Apply pattern matching to a table: all table entries that match a pattern
746 glob_table(const char *pat, XPtrV *wp, struct table *tp)
752 while ((te = ktnext(&ts)))
753 if (gmatchx(te->name, pat, false)) {
756 strdupx(cp, te->name, ATEMP);
762 glob_path(int flags, const char *pat, XPtrV *wp, const char *lpath)
764 const char *sp = lpath, *p;
766 size_t pathlen, patlen, oldsize, newsize, i, j;
769 patlen = strlen(pat);
770 checkoktoadd(patlen, 129 + X_EXTRA);
772 Xinit(xs, xp, patlen + 128, ATEMP);
774 xp = Xstring(xs, xp);
775 if (!(p = cstrchr(sp, ':')))
780 * Copy sp into xp, stuffing any MAGIC characters
785 XcheckN(xs, xp, pathlen * 2);
795 XcheckN(xs, xp, patlen);
796 memcpy(xp, pat, patlen);
798 oldsize = XPsize(*wp);
800 glob_str(Xstring(xs, xp), wp, true);
801 newsize = XPsize(*wp);
803 /* Check that each match is executable... */
804 words = (char **)XPptrv(*wp);
805 for (i = j = oldsize; i < newsize; i++) {
806 if (ksh_access(words[i], X_OK) == 0) {
808 if (!(flags & XCF_FULLPATH))
809 memmove(words[j], words[j] + pathlen,
810 strlen(words[j] + pathlen) + 1);
813 afree(words[i], ATEMP);
824 * if argument string contains any special characters, they will
825 * be escaped and the result will be put into edit buffer by
826 * keybinding-specific function
829 x_escape(const char *s, size_t len, int (*putbuf_func)(const char *, size_t))
831 size_t add = 0, wlen = len;
832 const char *ifs = str_val(local("IFS", 0));
835 while (wlen - add > 0)
836 if (vstrchr("\"#$&'()*:;<=>?[\\`{|}", s[add]) ||
837 vstrchr(ifs, s[add])) {
838 if (putbuf_func(s, add) != 0) {
842 putbuf_func(s[add] == '\n' ? "'" : "\\", 1);
843 putbuf_func(&s[add], 1);
853 if (wlen > 0 && rval == 0)
854 rval = putbuf_func(s, wlen);
860 /* +++ emacs editing mode +++ */
863 #define AEDIT &aedit /* area for kill ring and macro defns */
865 /* values returned by keyboard functions */
867 #define KEOL 1 /* ^M, ^J */
868 #define KINTR 2 /* ^G, ^C */
871 int (*xf_func)(int c);
876 struct x_defbindings {
877 unsigned char xdb_func; /* XFUNC_* */
878 unsigned char xdb_tab;
879 unsigned char xdb_char;
882 #define XF_ARG 1 /* command takes number prefix */
883 #define XF_NOBIND 2 /* not allowed to bind to function */
884 #define XF_PREFIX 4 /* function sets prefix */
886 /* Separator for completion */
887 #define is_cfs(c) ((c) == ' ' || (c) == '\t' || (c) == '"' || (c) == '\'')
888 /* Separator for motion */
889 #define is_mfs(c) (!(ksh_isalnux(c) || (c) == '$' || ((c) & 0x80)))
891 #define X_NTABS 3 /* normal, meta1, meta2 */
892 #define X_TABSZ 256 /* size of keydef tables etc */
895 * Arguments for do_complete()
896 * 0 = enumerate M-= complete as much as possible and then list
901 CT_LIST, /* list the possible completions */
902 CT_COMPLETE, /* complete to longest prefix */
903 CT_COMPLIST /* complete and then list (if non-exact) */
907 * The following are used for my horizontal scrolling stuff
909 static char *xbuf; /* beg input buffer */
910 static char *xend; /* end input buffer */
911 static char *xcp; /* current position */
912 static char *xep; /* current end */
913 static char *xbp; /* start of visible portion of input buffer */
914 static char *xlp; /* last char visible on screen */
915 static bool x_adj_ok;
917 * we use x_adj_done so that functions can tell
918 * whether x_adjust() has been called while they are active.
920 static int x_adj_done; /* is incremented by x_adjust() */
922 static int x_displen;
923 static int x_arg; /* general purpose arg */
924 static bool x_arg_defaulted; /* x_arg not explicitly set; defaulted to 1 */
926 static bool xlp_valid; /* lastvis pointer was recalculated */
928 static char **x_histp; /* history position */
929 static int x_nextcmd; /* for newline-and-next */
930 static char **x_histncp; /* saved x_histp for " */
931 static char *xmp; /* mark pointer */
932 static unsigned char x_last_command;
933 static unsigned char (*x_tab)[X_TABSZ]; /* key definition */
935 static char *(*x_atab)[X_TABSZ]; /* macro definitions */
937 static unsigned char x_bound[(X_TABSZ * X_NTABS + 7) / 8];
939 static char *killstack[KILLSIZE];
940 static int killsp, killtp;
941 static int x_curprefix;
943 static char *macroptr; /* bind key macro active? */
946 static int winwidth; /* width of window */
947 static char *wbuf[2]; /* window buffers */
948 static int wbuf_len; /* length of window buffers (x_cols - 3) */
949 static int win; /* window buffer in use */
950 static char morec; /* more character at right of window */
951 static int lastref; /* argument to last refresh() */
952 static int holdlen; /* length of holdbuf */
954 static int pwidth; /* width of prompt */
955 static int prompt_trunc; /* how much of prompt to truncate or -1 */
956 static int x_col; /* current column on line */
958 static int x_ins(const char *);
959 static void x_delete(size_t, bool);
960 static size_t x_bword(void);
961 static size_t x_fword(bool);
962 static void x_goto(char *);
963 static char *x_bs0(char *, char *);
964 static void x_bs3(char **);
965 static int x_size_str(char *);
966 static int x_size2(char *, char **);
967 static void x_zots(char *);
968 static void x_zotc2(int);
969 static void x_zotc3(char **);
970 static void x_load_hist(char **);
971 static int x_search(char *, int, int);
973 static int x_search_dir(int);
975 static int x_match(char *, char *);
976 static void x_redraw(int);
977 static void x_push(int);
978 static char *x_mapin(const char *, Area *);
979 static char *x_mapout(int);
980 static void x_mapout2(int, char **);
981 static void x_print(int, int);
982 static void x_adjust(void);
983 static void x_e_ungetc(int);
984 static int x_e_getc(void);
985 static void x_e_putc2(int);
986 static void x_e_putc3(const char **);
987 static void x_e_puts(const char *);
989 static int x_fold_case(int);
991 static char *x_lastcp(void);
992 static void do_complete(int, Comp_type);
993 static size_t x_nb2nc(size_t);
995 static int unget_char = -1;
997 static int x_do_ins(const char *, size_t);
998 static void bind_if_not_bound(int, int, int);
1001 #define EMACSFN_ENUMS
1002 #include "emacsfn.h"
1006 #define EMACSFN_DEFNS
1007 #include "emacsfn.h"
1009 static const struct x_ftab x_ftab[] = {
1010 #define EMACSFN_ITEMS
1011 #include "emacsfn.h"
1015 static struct x_defbindings const x_defbindings[] = {
1016 { XFUNC_del_back, 0, CTRL('?') },
1017 { XFUNC_del_bword, 1, CTRL('?') },
1018 { XFUNC_eot_del, 0, CTRL('D') },
1019 { XFUNC_del_back, 0, CTRL('H') },
1020 { XFUNC_del_bword, 1, CTRL('H') },
1021 { XFUNC_del_bword, 1, 'h' },
1022 { XFUNC_mv_bword, 1, 'b' },
1023 { XFUNC_mv_fword, 1, 'f' },
1024 { XFUNC_del_fword, 1, 'd' },
1025 { XFUNC_mv_back, 0, CTRL('B') },
1026 { XFUNC_mv_forw, 0, CTRL('F') },
1027 { XFUNC_search_char_forw, 0, CTRL(']') },
1028 { XFUNC_search_char_back, 1, CTRL(']') },
1029 { XFUNC_newline, 0, CTRL('M') },
1030 { XFUNC_newline, 0, CTRL('J') },
1031 { XFUNC_end_of_text, 0, CTRL('_') },
1032 { XFUNC_abort, 0, CTRL('G') },
1033 { XFUNC_prev_com, 0, CTRL('P') },
1034 { XFUNC_next_com, 0, CTRL('N') },
1035 { XFUNC_nl_next_com, 0, CTRL('O') },
1036 { XFUNC_search_hist, 0, CTRL('R') },
1037 { XFUNC_beg_hist, 1, '<' },
1038 { XFUNC_end_hist, 1, '>' },
1039 { XFUNC_goto_hist, 1, 'g' },
1040 { XFUNC_mv_end, 0, CTRL('E') },
1041 { XFUNC_mv_begin, 0, CTRL('A') },
1042 { XFUNC_draw_line, 0, CTRL('L') },
1043 { XFUNC_cls, 1, CTRL('L') },
1044 { XFUNC_meta1, 0, CTRL('[') },
1045 { XFUNC_meta2, 0, CTRL('X') },
1046 { XFUNC_kill, 0, CTRL('K') },
1047 { XFUNC_yank, 0, CTRL('Y') },
1048 { XFUNC_meta_yank, 1, 'y' },
1049 { XFUNC_literal, 0, CTRL('^') },
1050 { XFUNC_comment, 1, '#' },
1051 { XFUNC_transpose, 0, CTRL('T') },
1052 { XFUNC_complete, 1, CTRL('[') },
1053 { XFUNC_comp_list, 0, CTRL('I') },
1054 { XFUNC_comp_list, 1, '=' },
1055 { XFUNC_enumerate, 1, '?' },
1056 { XFUNC_expand, 1, '*' },
1057 { XFUNC_comp_file, 1, CTRL('X') },
1058 { XFUNC_comp_comm, 2, CTRL('[') },
1059 { XFUNC_list_comm, 2, '?' },
1060 { XFUNC_list_file, 2, CTRL('Y') },
1061 { XFUNC_set_mark, 1, ' ' },
1062 { XFUNC_kill_region, 0, CTRL('W') },
1063 { XFUNC_xchg_point_mark, 2, CTRL('X') },
1064 { XFUNC_literal, 0, CTRL('V') },
1065 { XFUNC_version, 1, CTRL('V') },
1066 { XFUNC_prev_histword, 1, '.' },
1067 { XFUNC_prev_histword, 1, '_' },
1068 { XFUNC_set_arg, 1, '0' },
1069 { XFUNC_set_arg, 1, '1' },
1070 { XFUNC_set_arg, 1, '2' },
1071 { XFUNC_set_arg, 1, '3' },
1072 { XFUNC_set_arg, 1, '4' },
1073 { XFUNC_set_arg, 1, '5' },
1074 { XFUNC_set_arg, 1, '6' },
1075 { XFUNC_set_arg, 1, '7' },
1076 { XFUNC_set_arg, 1, '8' },
1077 { XFUNC_set_arg, 1, '9' },
1079 { XFUNC_fold_upper, 1, 'U' },
1080 { XFUNC_fold_upper, 1, 'u' },
1081 { XFUNC_fold_lower, 1, 'L' },
1082 { XFUNC_fold_lower, 1, 'l' },
1083 { XFUNC_fold_capitalise, 1, 'C' },
1084 { XFUNC_fold_capitalise, 1, 'c' },
1087 * These for ANSI arrow keys: arguablely shouldn't be here by
1088 * default, but its simpler/faster/smaller than using termcap
1091 { XFUNC_meta2, 1, '[' },
1092 { XFUNC_meta2, 1, 'O' },
1093 { XFUNC_prev_com, 2, 'A' },
1094 { XFUNC_next_com, 2, 'B' },
1095 { XFUNC_mv_forw, 2, 'C' },
1096 { XFUNC_mv_back, 2, 'D' },
1098 { XFUNC_vt_hack, 2, '1' },
1099 { XFUNC_mv_begin | 0x80, 2, '7' },
1100 { XFUNC_mv_begin, 2, 'H' },
1101 { XFUNC_mv_end | 0x80, 2, '4' },
1102 { XFUNC_mv_end | 0x80, 2, '8' },
1103 { XFUNC_mv_end, 2, 'F' },
1104 { XFUNC_del_char | 0x80, 2, '3' },
1105 { XFUNC_search_hist_up | 0x80, 2, '5' },
1106 { XFUNC_search_hist_dn | 0x80, 2, '6' },
1107 /* more non-standard ones */
1108 { XFUNC_edit_line, 2, 'e' }
1118 for (cp = xcp; cp < (xcp + nb); ++nc)
1119 cp += utf_ptradj(cp);
1127 x_histp = histptr + 1;
1133 #define XFUNC_VALUE(f) (f)
1135 #define XFUNC_VALUE(f) (f & 0x7F)
1139 x_e_getmbc(char *sbuf)
1142 unsigned char *buf = (unsigned char *)sbuf;
1145 buf[pos++] = c = x_e_getc();
1149 if ((buf[0] >= 0xC2) && (buf[0] < 0xF0)) {
1153 if ((c & 0xC0) != 0x80) {
1159 if ((buf[0] >= 0xE0) && (buf[0] < 0xF0)) {
1160 /* XXX x_e_ungetc is one-octet only */
1161 buf[pos++] = c = x_e_getc();
1172 prompt_trunc = pprompt(prompt, 0);
1173 pwidth = prompt_trunc % x_cols;
1174 prompt_trunc -= pwidth;
1175 if ((mksh_uari_t)pwidth > ((mksh_uari_t)x_cols - 3 - MIN_EDIT_SPACE)) {
1176 /* force newline after prompt */
1191 xlp = xcp = xep = buf;
1196 x_histp = histptr + 1;
1197 x_last_command = XFUNC_error;
1200 x_displen = (xx_cols = x_cols) - 2 - (x_col = pwidth);
1205 if (x_nextcmd >= 0) {
1206 int off = source->line - x_nextcmd;
1207 if (histptr - history >= off) {
1208 x_load_hist(histptr - off);
1209 x_histncp = x_histp;
1214 while (/* CONSTCOND */ 1) {
1216 if ((c = x_e_getc()) < 0)
1219 f = x_curprefix == -1 ? XFUNC_insert :
1220 x_tab[x_curprefix][c];
1224 if ((i = x_e_getc()) != '~')
1228 /* avoid bind key macro recursion */
1229 if (macroptr && f == XFUNC_ins_string)
1233 if (!(x_ftab[f].xf_flags & XF_PREFIX) &&
1234 x_last_command != XFUNC_set_arg) {
1236 x_arg_defaulted = true;
1238 i = c | (x_curprefix << 8);
1240 switch ((*x_ftab[f].xf_func)(i)) {
1242 if (!(x_ftab[f].xf_flags & XF_PREFIX))
1249 /* special case for interrupt */
1254 /* ad-hoc hack for fixing the cursor position */
1262 static int left, pos, save_arg;
1266 * Should allow tab and control chars.
1275 if (((c & 0xC0) == 0x80) && left) {
1286 if (x_curprefix == -1) {
1287 /* flush invalid multibyte */
1293 if ((c >= 0xC2) && (c < 0xE0))
1295 else if ((c >= 0xE0) && (c < 0xF0))
1320 macroptr = x_atab[c >> 8][c & 255];
1322 * we no longer need to bother checking if macroptr is
1323 * not NULL but first char is NUL; x_e_getc() does it
1330 x_do_ins(const char *cp, size_t len)
1332 if (xep + len >= xend) {
1336 memmove(xcp + len, xcp, xep - xcp + 1);
1337 memmove(xcp, cp, len);
1345 x_ins(const char *s)
1348 int adj = x_adj_done;
1350 if (x_do_ins(s, strlen(s)) < 0)
1353 * x_zots() may result in a call to x_adjust()
1354 * we want xcp to reflect the new position.
1358 x_adj_ok = tobool(xcp >= xlp);
1360 /* has x_adjust() been called? */
1361 if (adj == x_adj_done) {
1374 x_del_back(int c MKSH_A_UNUSED)
1384 } while ((++i < x_arg) && (xcp != xbuf));
1390 x_del_char(int c MKSH_A_UNUSED)
1396 while (i < (size_t)x_arg) {
1397 utf_ptradjx(cp, cp2);
1412 /* Delete nc chars to the right of the cursor (including cursor position) */
1414 x_delete(size_t nc, bool push)
1424 for (i = 0; i < nc; ++i) {
1428 j = x_size2(cp, &cp2);
1437 if (xmp != NULL && xmp > xcp) {
1444 * This lets us yank a word we have deleted.
1450 /* Copies the NUL */
1451 memmove(xcp, xcp + nb, xep - xcp + 1);
1457 * if we are already filling the line,
1458 * there is no need to ' ', '\b'.
1459 * But if we must, make sure we do the minimum.
1461 if ((i = xx_cols - 2 - x_col) > 0 || xep - xlp == 0) {
1462 nw = i = (nw < i) ? nw : i;
1465 if (x_col == xx_cols - 2) {
1466 x_e_putc2((xep > xlp) ? '>' : (xbp > xbuf) ? '<' : ' ');
1484 x_del_bword(int c MKSH_A_UNUSED)
1486 x_delete(x_bword(), true);
1491 x_mv_bword(int c MKSH_A_UNUSED)
1498 x_mv_fword(int c MKSH_A_UNUSED)
1505 x_del_fword(int c MKSH_A_UNUSED)
1507 x_delete(x_fword(false), true);
1522 while (cp != xbuf && is_mfs(cp[-1])) {
1526 while (cp != xbuf && !is_mfs(cp[-1])) {
1532 return (x_nb2nc(nb));
1546 while (cp != xep && is_mfs(*cp))
1548 while (cp != xep && !is_mfs(*cp))
1551 nc = x_nb2nc(cp - xcp);
1560 cp = cp >= xep ? xep : x_bs0(cp, xbuf);
1561 if (cp < xbp || cp >= utf_skipcols(xbp, x_displen)) {
1562 /* we are heading off screen */
1565 } else if (cp < xcp) {
1569 } else if (cp > xcp) {
1577 x_bs0(char *cp, char *lower_bound)
1580 while ((!lower_bound || (cp > lower_bound)) &&
1581 ((*(unsigned char *)cp & 0xC0) == 0x80))
1591 *p = x_bs0((*p) - 1, NULL);
1592 i = x_size2(*p, NULL);
1598 x_size_str(char *cp)
1602 size += x_size2(cp, &cp);
1607 x_size2(char *cp, char **dcp)
1609 int c = *(unsigned char *)cp;
1611 if (UTFMODE && (c > 0x7F))
1612 return (utf_widthadj(cp, (const char **)dcp));
1616 /* Kludge, tabs are always four spaces. */
1618 if (c < ' ' || c == 0x7f)
1619 /* control unsigned char */
1627 int adj = x_adj_done;
1630 while (*str && str < xlp && adj == x_adj_done)
1638 /* Kludge, tabs are always four spaces. */
1640 } else if (c < ' ' || c == 0x7f) {
1642 x_e_putc2(UNCTRL(c));
1650 unsigned char c = **(unsigned char **)cp;
1653 /* Kludge, tabs are always four spaces. */
1656 } else if (c < ' ' || c == 0x7f) {
1658 x_e_putc2(UNCTRL(c));
1661 x_e_putc3((const char **)cp);
1665 x_mv_back(int c MKSH_A_UNUSED)
1680 x_mv_forw(int c MKSH_A_UNUSED)
1682 char *cp = xcp, *cp2;
1689 utf_ptradjx(cp, cp2);
1699 x_search_char_forw(int c MKSH_A_UNUSED)
1705 if (x_e_getmbc(tmp) < 0) {
1710 if ((cp = (cp == xep) ? NULL : strstr(cp + 1, tmp)) == NULL &&
1711 (cp = strstr(xbuf, tmp)) == NULL) {
1721 x_search_char_back(int c MKSH_A_UNUSED)
1723 char *cp = xcp, *p, tmp[4];
1726 if (x_e_getmbc(tmp) < 0) {
1730 for (; x_arg--; cp = p)
1738 if ((tmp[1] && ((p+1) > xep)) ||
1739 (tmp[2] && ((p+2) > xep)))
1744 if (b && tmp[1] && p[1] != tmp[1])
1746 if (b && tmp[2] && p[2] != tmp[2])
1756 x_newline(int c MKSH_A_UNUSED)
1766 x_end_of_text(int c MKSH_A_UNUSED)
1768 x_zotc2(edchars.eof);
1776 x_beg_hist(int c MKSH_A_UNUSED)
1778 x_load_hist(history);
1783 x_end_hist(int c MKSH_A_UNUSED)
1785 x_load_hist(histptr);
1790 x_prev_com(int c MKSH_A_UNUSED)
1792 x_load_hist(x_histp - x_arg);
1797 x_next_com(int c MKSH_A_UNUSED)
1799 x_load_hist(x_histp + x_arg);
1804 * Goto a particular history number obtained from argument.
1805 * If no argument is given history 1 is probably not what you
1806 * want so we'll simply go to the oldest one.
1809 x_goto_hist(int c MKSH_A_UNUSED)
1811 if (x_arg_defaulted)
1812 x_load_hist(history);
1814 x_load_hist(histptr + x_arg - source->line);
1819 x_load_hist(char **hp)
1824 if (hp == histptr + 1) {
1827 } else if (hp < history || hp > histptr) {
1834 oldsize = x_size_str(xbuf);
1836 strlcpy(holdbufp, xbuf, LINE);
1837 strlcpy(xbuf, sp, xend - xbuf);
1839 xep = xcp = xbuf + strlen(xbuf);
1841 if (xep <= x_lastcp()) {
1849 x_nl_next_com(int c MKSH_A_UNUSED)
1851 if (!x_histncp || (x_histp != x_histncp && x_histp != histptr + 1))
1852 /* fresh start of ^O */
1853 x_histncp = x_histp;
1854 x_nextcmd = source->line - (histptr - x_histncp) + 1;
1855 return (x_newline('\n'));
1861 if (xep == xbuf && x_arg_defaulted)
1862 return (x_end_of_text(c));
1864 return (x_del_char(c));
1867 /* reverse incremental history search */
1869 x_search_hist(int c)
1871 int offset = -1; /* offset of match in xbuf, else -1 */
1872 char pat[80 + 1]; /* pattern buffer */
1877 while (/* CONSTCOND */ 1) {
1879 x_e_puts("\nI-search: ");
1883 if ((c = x_e_getc()) < 0)
1886 if (c == CTRL('[')) {
1887 if ((f & 0x7F) == XFUNC_meta1) {
1888 if ((c = x_e_getc()) < 0)
1890 f = x_tab[1][c] & 0x7F;
1891 if (f == XFUNC_meta1 || f == XFUNC_meta2)
1900 if ((c = x_e_getc()) != '~')
1904 if (f == XFUNC_search_hist)
1905 offset = x_search(pat, 0, offset);
1906 else if (f == XFUNC_del_back) {
1916 offset = x_search(pat, 1, offset);
1918 } else if (f == XFUNC_insert) {
1919 /* add char to pattern */
1920 /* overflow check... */
1921 if ((size_t)(p - pat) >= sizeof(pat) - 1) {
1925 *p++ = c, *p = '\0';
1927 /* already have partial match */
1928 offset = x_match(xbuf, pat);
1930 x_goto(xbuf + offset + (p - pat) -
1935 offset = x_search(pat, 0, offset);
1936 } else if (f == XFUNC_abort) {
1938 x_load_hist(histptr + 1);
1951 /* search backward from current line */
1953 x_search(char *pat, int sameline, int offset)
1958 for (hp = x_histp - (sameline ? 0 : 1); hp >= history; --hp) {
1959 i = x_match(*hp, pat);
1964 x_goto(xbuf + i + strlen(pat) - (*pat == '^'));
1974 /* anchored search up from current line */
1976 x_search_hist_up(int c MKSH_A_UNUSED)
1978 return (x_search_dir(-1));
1981 /* anchored search down from current line */
1983 x_search_hist_dn(int c MKSH_A_UNUSED)
1985 return (x_search_dir(1));
1988 /* anchored search in the indicated direction */
1990 x_search_dir(int search_dir /* should've been bool */)
1992 char **hp = x_histp + search_dir;
1993 size_t curs = xcp - xbuf;
1995 while (histptr >= hp && hp >= history) {
1996 if (strncmp(xbuf, *hp, curs) == 0) {
1998 x_goto(xbuf + curs);
2007 /* return position of first match of pattern in string, else -1 */
2009 x_match(char *str, char *pat)
2012 return ((strncmp(str, pat + 1, strlen(pat + 1)) == 0) ? 0 : -1);
2014 char *q = strstr(str, pat);
2015 return ((q == NULL) ? -1 : q - str);
2020 x_del_line(int c MKSH_A_UNUSED)
2026 j = x_size_str(xbuf);
2029 xlp = xbp = xep = xbuf;
2039 x_mv_end(int c MKSH_A_UNUSED)
2046 x_mv_begin(int c MKSH_A_UNUSED)
2053 x_draw_line(int c MKSH_A_UNUSED)
2060 x_e_rebuildline(const char *clrstr)
2062 shf_puts(clrstr, shl_out);
2068 x_cls(int c MKSH_A_UNUSED)
2070 return (x_e_rebuildline(MKSH_CLS_STRING));
2074 * Redraw (part of) the line. If limit is < 0, the everything is redrawn
2075 * on a NEW line, otherwise limit is the screen column up to which needs
2091 if (prompt_trunc != -1)
2092 pprompt(prompt, prompt_trunc);
2095 x_displen = xx_cols - 2 - x_col;
2098 if (xbp != xbuf || xep > xlp)
2102 /* we fill the line */
2109 i -= x_size2(cpl, &cpl);
2113 while ((j < i) || (x_col < (xx_cols - 2))) {
2114 if (!(x_col < (xx_cols - 2)))
2121 /* more off screen */
2126 } else if (xbp > xbuf)
2141 x_transpose(int c MKSH_A_UNUSED)
2143 unsigned int tmpa, tmpb;
2146 * What transpose is meant to do seems to be up for debate. This
2147 * is a general summary of the options; the text is abcd with the
2148 * upper case character or underscore indicating the cursor position:
2149 * Who Before After Before After
2150 * AT&T ksh in emacs mode: abCd abdC abcd_ (bell)
2151 * AT&T ksh in gmacs mode: abCd baCd abcd_ abdc_
2152 * gnu emacs: abCd acbD abcd_ abdc_
2153 * Pdksh currently goes with GNU behavior since I believe this is the
2154 * most common version of emacs, unless in gmacs mode, in which case
2155 * it does the AT&T ksh gmacs mode.
2156 * This should really be broken up into 3 functions so users can bind
2157 * to the one they want.
2162 } else if (xcp == xep || Flag(FGMACS)) {
2163 if (xcp - xbuf == 1) {
2168 * Gosling/Unipress emacs style: Swap two characters before
2169 * the cursor, do not change cursor position
2172 if (utf_mbtowc(&tmpa, xcp) == (size_t)-1) {
2177 if (utf_mbtowc(&tmpb, xcp) == (size_t)-1) {
2181 utf_wctomb(xcp, tmpa);
2183 utf_wctomb(xcp, tmpb);
2187 * GNU emacs style: Swap the characters before and under the
2188 * cursor, move cursor position along one.
2190 if (utf_mbtowc(&tmpa, xcp) == (size_t)-1) {
2195 if (utf_mbtowc(&tmpb, xcp) == (size_t)-1) {
2199 utf_wctomb(xcp, tmpa);
2201 utf_wctomb(xcp, tmpb);
2209 x_literal(int c MKSH_A_UNUSED)
2216 x_meta1(int c MKSH_A_UNUSED)
2223 x_meta2(int c MKSH_A_UNUSED)
2230 x_kill(int c MKSH_A_UNUSED)
2232 size_t col = xcp - xbuf;
2233 size_t lastcol = xep - xbuf;
2236 if (x_arg_defaulted || (narg = x_arg) > lastcol)
2239 x_goto(xbuf + narg);
2243 x_delete(x_nb2nc(ndel), true);
2252 mkssert(xcp != NULL);
2253 strndupx(cp, xcp, nchars, AEDIT);
2254 if (killstack[killsp])
2255 afree(killstack[killsp], AEDIT);
2256 killstack[killsp] = cp;
2257 killsp = (killsp + 1) % KILLSIZE;
2261 x_yank(int c MKSH_A_UNUSED)
2268 if (killstack[killtp] == 0) {
2269 x_e_puts("\nnothing to yank");
2274 x_ins(killstack[killtp]);
2279 x_meta_yank(int c MKSH_A_UNUSED)
2283 if ((x_last_command != XFUNC_yank && x_last_command != XFUNC_meta_yank) ||
2284 killstack[killtp] == 0) {
2286 x_e_puts("\nyank something first");
2290 len = strlen(killstack[killtp]);
2292 x_delete(x_nb2nc(len), false);
2295 killtp = KILLSIZE - 1;
2298 } while (killstack[killtp] == 0);
2299 x_ins(killstack[killtp]);
2304 x_abort(int c MKSH_A_UNUSED)
2307 xlp = xep = xcp = xbp = xbuf;
2315 x_error(int c MKSH_A_UNUSED)
2322 /* special VT100 style key sequence hack */
2326 /* we only support PF2-'1' for now */
2327 if (c != (2 << 8 | '1'))
2328 return (x_error(c));
2330 /* what's the next character? */
2331 switch ((c = x_e_getc())) {
2334 x_arg_defaulted = true;
2335 return (x_mv_begin(0));
2337 /* "interesting" sequence detected */
2343 /* XXX x_e_ungetc is one-octet only */
2344 if ((c = x_e_getc()) != '5' && c != '3')
2348 * At this point, we have read the following octets so far:
2349 * - ESC+[ or ESC+O or Ctrl-X (Prefix 2)
2352 * - 5 (Ctrl key combiner) or 3 (Alt key combiner)
2353 * We can now accept one more octet designating the key.
2356 switch ((c = x_e_getc())) {
2358 return (x_mv_fword(c));
2360 return (x_mv_bword(c));
2365 return (x_error(c));
2370 x_mapin(const char *cp, Area *ap)
2374 strdupx(news, cp, ap);
2377 /* XXX -- should handle \^ escape? */
2381 /* includes '?'; ASCII */
2397 x_mapout2(int c, char **buf)
2401 if (c < ' ' || c == 0x7f) {
2421 x_print(int prefix, int key)
2423 int f = x_tab[prefix][key];
2426 /* prefix == 1 || prefix == 2 */
2427 shf_puts(x_mapout(prefix == 1 ?
2428 CTRL('[') : CTRL('X')), shl_stdout);
2430 shprintf("%s = ", x_mapout(key));
2432 shprintf("%s%s = ", x_mapout(key), (f & 0x80) ? "~" : "");
2433 if (XFUNC_VALUE(f) != XFUNC_ins_string)
2435 shprintf("%s\n", x_ftab[XFUNC_VALUE(f)].xf_name);
2438 shprintf("'%s'\n", x_atab[prefix][key]);
2443 x_bind(const char *a1, const char *a2,
2459 if (x_tab == NULL) {
2460 bi_errorf("can't bind, not a tty");
2463 /* List function names */
2465 for (f = 0; f < NELEM(x_ftab); f++)
2466 if (x_ftab[f].xf_name &&
2467 !(x_ftab[f].xf_flags & XF_NOBIND))
2468 shprintf("%s\n", x_ftab[f].xf_name);
2472 for (prefix = 0; prefix < X_NTABS; prefix++)
2473 for (key = 0; key < X_TABSZ; key++) {
2474 f = XFUNC_VALUE(x_tab[prefix][key]);
2475 if (f == XFUNC_insert || f == XFUNC_error
2477 || (macro && f != XFUNC_ins_string)
2481 x_print(prefix, key);
2485 m2 = m1 = x_mapin(a1, ATEMP);
2488 key = (unsigned char)*m1;
2489 f = XFUNC_VALUE(x_tab[prefix][key]);
2490 if (f == XFUNC_meta1)
2492 else if (f == XFUNC_meta2)
2499 && ((*m1 != '~') || *(m1 + 1))
2505 while (*c && (size_t)(m1 - msg) < sizeof(msg) - 3)
2506 x_mapout2(*c++, &m1);
2507 bi_errorf("%s: %s", "too long key sequence", msg);
2511 hastilde = tobool(*m1);
2516 x_print(prefix, key);
2523 f = XFUNC_ins_string;
2524 sp = x_mapin(a2, AEDIT);
2527 for (f = 0; f < NELEM(x_ftab); f++)
2528 if (x_ftab[f].xf_name &&
2529 strcmp(x_ftab[f].xf_name, a2) == 0)
2531 if (f == NELEM(x_ftab) || x_ftab[f].xf_flags & XF_NOBIND) {
2532 bi_errorf("%s: %s %s", a2, "no such", Tfunction);
2538 if (XFUNC_VALUE(x_tab[prefix][key]) == XFUNC_ins_string &&
2539 x_atab[prefix][key])
2540 afree(x_atab[prefix][key], AEDIT);
2542 x_tab[prefix][key] = f
2544 | (hastilde ? 0x80 : 0)
2548 x_atab[prefix][key] = sp;
2551 /* Track what the user has bound so x_mode(true) won't toast things */
2552 if (f == XFUNC_insert)
2553 x_bound[(prefix * X_TABSZ + key) / 8] &=
2554 ~(1 << ((prefix * X_TABSZ + key) % 8));
2556 x_bound[(prefix * X_TABSZ + key) / 8] |=
2557 (1 << ((prefix * X_TABSZ + key) % 8));
2563 bind_if_not_bound(int p, int k, int func)
2568 * Has user already bound this key?
2569 * If so, do not override it.
2571 t = p * X_TABSZ + k;
2572 if (x_bound[t >> 3] & (1 << (t & 7)))
2579 x_set_mark(int c MKSH_A_UNUSED)
2586 x_kill_region(int c MKSH_A_UNUSED)
2603 x_delete(x_nb2nc(rsize), true);
2609 x_xchg_point_mark(int c MKSH_A_UNUSED)
2624 x_noop(int c MKSH_A_UNUSED)
2630 * File/command name completion routines
2633 x_comp_comm(int c MKSH_A_UNUSED)
2635 do_complete(XCF_COMMAND, CT_COMPLETE);
2640 x_list_comm(int c MKSH_A_UNUSED)
2642 do_complete(XCF_COMMAND, CT_LIST);
2647 x_complete(int c MKSH_A_UNUSED)
2649 do_complete(XCF_COMMAND_FILE, CT_COMPLETE);
2654 x_enumerate(int c MKSH_A_UNUSED)
2656 do_complete(XCF_COMMAND_FILE, CT_LIST);
2661 x_comp_file(int c MKSH_A_UNUSED)
2663 do_complete(XCF_FILE, CT_COMPLETE);
2668 x_list_file(int c MKSH_A_UNUSED)
2670 do_complete(XCF_FILE, CT_LIST);
2675 x_comp_list(int c MKSH_A_UNUSED)
2677 do_complete(XCF_COMMAND_FILE, CT_COMPLIST);
2682 x_expand(int c MKSH_A_UNUSED)
2685 int start, end, nwords, i;
2688 nwords = x_cf_glob(&i, xbuf, xep - xbuf, xcp - xbuf,
2689 &start, &end, &words);
2695 x_goto(xbuf + start);
2696 x_delete(x_nb2nc(end - start), false);
2699 while (i < nwords) {
2700 if (x_escape(words[i], strlen(words[i]), x_do_ins) < 0 ||
2701 (++i < nwords && x_ins(" ") < 0)) {
2713 /* XCF_{COMMAND,FILE,COMMAND_FILE} */
2715 /* 0 for list, 1 for complete and 2 for complete-list */
2719 int start, end, nlen, olen, nwords;
2722 nwords = x_cf_glob(&flags, xbuf, xep - xbuf, xcp - xbuf,
2723 &start, &end, &words);
2729 if (type == CT_LIST) {
2730 x_print_expansions(nwords, words,
2731 tobool(flags & XCF_IS_COMMAND));
2733 x_free_words(nwords, words);
2737 nlen = x_longest_prefix(nwords, words);
2740 * always complete single matches;
2741 * any expansion of parameter substitution
2742 * is always at most one result, too
2748 /* make a copy of the original string part */
2749 strndupx(unescaped, xbuf + start, olen, ATEMP);
2751 /* expand any tilde and unescape the string for comparison */
2752 unescaped = x_glob_hlp_tilde_and_rem_qchar(unescaped, true);
2755 * match iff entire original string is part of the
2756 * longest prefix, implying the latter is at least
2757 * the same size (after unescaping)
2759 completed = !strncmp(words[0], unescaped, strlen(unescaped));
2761 afree(unescaped, ATEMP);
2763 if (type == CT_COMPLIST && nwords > 1) {
2765 * print expansions, since we didn't get back
2766 * just a single match
2768 x_print_expansions(nwords, words,
2769 tobool(flags & XCF_IS_COMMAND));
2772 /* expand on the command line */
2776 memmove(xcp, xcp + olen, xep - xcp + 1);
2777 x_escape(words[0], nlen, x_do_ins);
2781 * append a space if this is a single non-directory match
2782 * and not a parameter or homedir substitution
2784 if (nwords == 1 && words[0][nlen - 1] != '/' &&
2785 !(flags & XCF_IS_NOSPACE)) {
2789 x_free_words(nwords, words);
2794 * x_adjust - redraw the line adjusting starting point etc.
2797 * This function is called when we have exceeded the bounds
2798 * of the edit window. It increments x_adj_done so that
2799 * functions like x_ins and x_delete know that we have been
2800 * called and can skip the x_bs() stuff which has already
2801 * been done by x_redraw.
2811 /* flag the fact that we were called */
2815 * calculate the amount of columns we need to "go back"
2816 * from xcp to set xbp to (but never < xbuf) to 2/3 of
2817 * the display width; take care of pwidth though
2819 if ((col_left = xx_cols * 2 / 3) < MIN_EDIT_SPACE) {
2821 * cowardly refuse to do anything
2822 * if the available space is too small;
2823 * fall back to dumb pdksh code
2825 if ((xbp = xcp - (x_displen / 2)) < xbuf)
2827 /* elide UTF-8 fixup as penalty */
2831 /* fix up xbp to just past a character end first */
2832 xbp = xcp >= xep ? xep : x_bs0(xcp, xbuf);
2833 /* walk backwards */
2834 while (xbp > xbuf && col_left > 0) {
2835 xbp = x_bs0(xbp - 1, xbuf);
2836 col_left -= (n = x_size2(xbp, NULL));
2838 /* check if we hit the prompt */
2839 if (xbp == xbuf && xcp != xbuf && col_left >= 0 && col_left < pwidth) {
2840 /* so we did; force scrolling occurs */
2841 xbp += utf_ptradj(xbp);
2853 unget_char = c < 0 ? -1 : (c & 255);
2861 if (unget_char >= 0) {
2869 if ((c = (unsigned char)*macroptr++))
2883 if (c == '\r' || c == '\n')
2885 if (x_col < xx_cols) {
2886 if (UTFMODE && (c > 0x7F)) {
2892 x = utf_wctomb(utf_tmp, c);
2898 width = utf_wcwidth(c);
2915 if (x_adj_ok && (x_col < 0 || x_col >= (xx_cols - 2)))
2920 x_e_putc3(const char **cp)
2922 int width = 1, c = **(const unsigned char **)cp;
2924 if (c == '\r' || c == '\n')
2926 if (x_col < xx_cols) {
2927 if (UTFMODE && (c > 0x7F)) {
2930 width = utf_widthadj(*cp, (const char **)&cp2);
2951 if (x_adj_ok && (x_col < 0 || x_col >= (xx_cols - 2)))
2956 x_e_puts(const char *s)
2958 int adj = x_adj_done;
2960 while (*s && adj == x_adj_done)
2966 * x_set_arg - set an arg value for next function
2969 * This is a simple implementation of M-[0-9].
2980 /* strip command prefix */
2982 while (c >= 0 && ksh_isdigit(c)) {
2983 n = n * 10 + (c - '0');
2985 /* upper bound for repeat */
2986 goto x_set_arg_too_big;
2990 if (c < 0 || first) {
2994 x_arg_defaulted = true;
2998 x_arg_defaulted = false;
3003 /* Comment or uncomment the current line. */
3005 x_comment(int c MKSH_A_UNUSED)
3007 int oldsize = x_size_str(xbuf);
3008 ssize_t len = xep - xbuf;
3009 int ret = x_do_comment(xbuf, xend - xbuf, &len);
3020 return (x_newline('\n'));
3026 x_version(int c MKSH_A_UNUSED)
3028 char *o_xbuf = xbuf, *o_xend = xend;
3029 char *o_xbp = xbp, *o_xep = xep, *o_xcp = xcp;
3030 int lim = x_lastcp() - xbp;
3034 strdupx(v, KSH_VERSION, ATEMP);
3036 xbuf = xbp = xcp = v;
3037 xend = xep = v + (vlen = strlen(v));
3047 x_redraw((int)vlen);
3051 /* This is what AT&T ksh seems to do... Very bizarre */
3061 x_edit_line(int c MKSH_A_UNUSED)
3063 if (x_arg_defaulted) {
3070 histsave(&source->line, xbuf, true, true);
3073 x_arg = source->line - (histptr - x_histp);
3076 shf_snprintf(xbuf, xend - xbuf, "%s %d",
3077 "fc -e ${VISUAL:-${EDITOR:-vi}} --", x_arg);
3079 strlcpy(xbuf, "fc -e ${VISUAL:-${EDITOR:-vi}} --", xend - xbuf);
3080 xep = xbuf + strlen(xbuf);
3081 return (x_newline('\n'));
3087 * x_prev_histword - recover word from prev command
3090 * This function recovers the last word from the previous
3091 * command and inserts it into the current edit line. If a
3092 * numeric arg is supplied then the n'th word from the
3093 * start of the previous command is used.
3094 * As a side effect, trashes the mark in order to achieve
3095 * being called in a repeatable fashion.
3103 x_prev_histword(int c MKSH_A_UNUSED)
3108 /* -1 = defaulted; 0+ = argument */
3109 static int last_arg = -1;
3111 if (x_last_command == XFUNC_prev_histword) {
3112 if (xmp && modified > 1)
3117 last_arg = x_arg_defaulted ? -1 : x_arg;
3118 xhp = histptr - (m - 1);
3119 if ((xhp < history) || !(cp = *xhp)) {
3125 if ((x_arg = last_arg) == -1) {
3126 /* x_arg_defaulted */
3128 rcp = &cp[strlen(cp) - 1];
3130 * ignore white-space after the last word
3132 while (rcp > cp && is_cfs(*rcp))
3134 while (rcp > cp && !is_cfs(*rcp))
3140 /* not x_arg_defaulted */
3145 * ignore white-space at start of line
3147 while (*rcp && is_cfs(*rcp))
3149 while (x_arg-- > 0) {
3150 while (*rcp && !is_cfs(*rcp))
3152 while (*rcp && is_cfs(*rcp))
3156 while (*rcp && !is_cfs(*rcp))
3168 /* Uppercase N(1) words */
3170 x_fold_upper(int c MKSH_A_UNUSED)
3172 return (x_fold_case('U'));
3175 /* Lowercase N(1) words */
3177 x_fold_lower(int c MKSH_A_UNUSED)
3179 return (x_fold_case('L'));
3182 /* Titlecase N(1) words */
3184 x_fold_capitalise(int c MKSH_A_UNUSED)
3186 return (x_fold_case('C'));
3191 * x_fold_case - convert word to UPPER/lower/Capital case
3194 * This function is used to implement M-U/M-u, M-L/M-l, M-C/M-c
3195 * to UPPER CASE, lower case or Capitalise Words.
3211 * first skip over any white-space
3213 while (cp != xep && is_mfs(*cp))
3216 * do the first char on its own since it may be
3217 * a different action than for the rest.
3222 *cp = ksh_tolower(*cp);
3224 /* uppercase, capitalise */
3225 *cp = ksh_toupper(*cp);
3229 * now for the rest of the word
3231 while (cp != xep && !is_mfs(*cp)) {
3234 *cp = ksh_toupper(*cp);
3236 /* lowercase, capitalise */
3237 *cp = ksh_tolower(*cp);
3249 * x_lastcp - last visible char
3255 * This function returns a pointer to that char in the
3256 * edit buffer that will be the last displayed on the
3257 * screen. The sequence:
3263 * Will position the cursor correctly on the screen.
3277 j = x_size2(xlp, &xlp2);
3278 if ((i + j) > x_displen)
3291 static bool x_cur_mode;
3293 if (x_cur_mode == onoff)
3298 x_mkraw(tty_fd, NULL, false);
3300 edchars.erase = tty_state.c_cc[VERASE];
3301 edchars.kill = tty_state.c_cc[VKILL];
3302 edchars.intr = tty_state.c_cc[VINTR];
3303 edchars.quit = tty_state.c_cc[VQUIT];
3304 edchars.eof = tty_state.c_cc[VEOF];
3306 edchars.werase = tty_state.c_cc[VWERASE];
3309 #ifdef _POSIX_VDISABLE
3310 /* Convert unset values to internal 'unset' value */
3311 if (edchars.erase == _POSIX_VDISABLE)
3313 if (edchars.kill == _POSIX_VDISABLE)
3315 if (edchars.intr == _POSIX_VDISABLE)
3317 if (edchars.quit == _POSIX_VDISABLE)
3319 if (edchars.eof == _POSIX_VDISABLE)
3321 if (edchars.werase == _POSIX_VDISABLE)
3322 edchars.werase = -1;
3325 if (edchars.erase >= 0) {
3326 bind_if_not_bound(0, edchars.erase, XFUNC_del_back);
3327 bind_if_not_bound(1, edchars.erase, XFUNC_del_bword);
3329 if (edchars.kill >= 0)
3330 bind_if_not_bound(0, edchars.kill, XFUNC_del_line);
3331 if (edchars.werase >= 0)
3332 bind_if_not_bound(0, edchars.werase, XFUNC_del_bword);
3333 if (edchars.intr >= 0)
3334 bind_if_not_bound(0, edchars.intr, XFUNC_abort);
3335 if (edchars.quit >= 0)
3336 bind_if_not_bound(0, edchars.quit, XFUNC_noop);
3338 mksh_tcset(tty_fd, &tty_state);
3342 /* +++ vi editing mode +++ */
3344 #define Ctrl(c) (c&0x1f)
3354 static int vi_hook(int);
3355 static int nextstate(int);
3356 static int vi_insert(int);
3357 static int vi_cmd(int, const char *);
3358 static int domove(int, const char *, int);
3359 static int redo_insert(int);
3360 static void yank_range(int, int);
3361 static int bracktype(int);
3362 static void save_cbuf(void);
3363 static void restore_cbuf(void);
3364 static int putbuf(const char *, ssize_t, bool);
3365 static void del_range(int, int);
3366 static int findch(int, int, bool, bool);
3367 static int forwword(int);
3368 static int backword(int);
3369 static int endword(int);
3370 static int Forwword(int);
3371 static int Backword(int);
3372 static int Endword(int);
3373 static int grabhist(int, int);
3374 static int grabsearch(int, int, int, const char *);
3375 static void redraw_line(bool);
3376 static void refresh(int);
3377 static int outofwin(void);
3378 static void rewindow(void);
3379 static int newcol(int, int);
3380 static void display(char *, char *, int);
3381 static void ed_mov_opt(int, char *);
3382 static int expand_word(int);
3383 static int complete_word(int, int);
3384 static int print_expansions(struct edstate *, int);
3385 #define char_len(c) ((c) < ' ' || (c) == 0x7F ? 2 : 1)
3386 static void x_vi_zotc(int);
3387 static void vi_error(void);
3388 static void vi_macro_reset(void);
3389 static int x_vi_putbuf(const char *, size_t);
3391 #define vC 0x01 /* a valid command that isn't a vM, vE, vU */
3392 #define vM 0x02 /* movement command (h, l, etc.) */
3393 #define vE 0x04 /* extended command (c, d, y) */
3394 #define vX 0x08 /* long command (@, f, F, t, T, etc.) */
3395 #define vU 0x10 /* an UN-undoable command (that isn't a vM) */
3396 #define vB 0x20 /* bad command (^@) */
3397 #define vZ 0x40 /* repeat count defaults to 0 (not 1) */
3398 #define vS 0x80 /* search (/, ?) */
3400 #define is_bad(c) (classify[(c)&0x7f]&vB)
3401 #define is_cmd(c) (classify[(c)&0x7f]&(vM|vE|vC|vU))
3402 #define is_move(c) (classify[(c)&0x7f]&vM)
3403 #define is_extend(c) (classify[(c)&0x7f]&vE)
3404 #define is_long(c) (classify[(c)&0x7f]&vX)
3405 #define is_undoable(c) (!(classify[(c)&0x7f]&vU))
3406 #define is_srch(c) (classify[(c)&0x7f]&vS)
3407 #define is_zerocount(c) (classify[(c)&0x7f]&vZ)
3409 static const unsigned char classify[128] = {
3410 /* 0 1 2 3 4 5 6 7 */
3411 /* 0 ^@ ^A ^B ^C ^D ^E ^F ^G */
3412 vB, 0, 0, 0, 0, vC|vU, vC|vZ, 0,
3413 /* 1 ^H ^I ^J ^K ^L ^M ^N ^O */
3414 vM, vC|vZ, 0, 0, vC|vU, 0, vC, 0,
3415 /* 2 ^P ^Q ^R ^S ^T ^U ^V ^W */
3416 vC, 0, vC|vU, 0, 0, 0, vC, 0,
3417 /* 3 ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
3418 vC, 0, 0, vC|vZ, 0, 0, 0, 0,
3419 /* 4 <space> ! " # $ % & ' */
3420 vM, 0, 0, vC, vM, vM, 0, 0,
3421 /* 5 ( ) * + , - . / */
3422 0, 0, vC, vC, vM, vC, 0, vC|vS,
3423 /* 6 0 1 2 3 4 5 6 7 */
3424 vM, 0, 0, 0, 0, 0, 0, 0,
3425 /* 7 8 9 : ; < = > ? */
3426 0, 0, 0, vM, 0, vC, 0, vC|vS,
3427 /* 8 @ A B C D E F G */
3428 vC|vX, vC, vM, vC, vC, vM, vM|vX, vC|vU|vZ,
3429 /* 9 H I J K L M N O */
3430 0, vC, 0, 0, 0, 0, vC|vU, vU,
3431 /* A P Q R S T U V W */
3432 vC, 0, vC, vC, vM|vX, vC, 0, vM,
3433 /* B X Y Z [ \ ] ^ _ */
3434 vC, vC|vU, 0, vU, vC|vZ, 0, vM, vC|vZ,
3435 /* C ` a b c d e f g */
3436 0, vC, vM, vE, vE, vM, vM|vX, vC|vZ,
3437 /* D h i j k l m n o */
3438 vM, vC, vC|vU, vC|vU, vM, 0, vC|vU, 0,
3439 /* E p q r s t u v w */
3440 vC, 0, vX, vC, vM|vX, vC|vU, vC|vU|vZ, vM,
3441 /* F x y z { | } ~ ^? */
3442 vC, vE|vU, 0, 0, vM|vZ, 0, vC, 0
3451 #define VNORMAL 0 /* command, insert or replace mode */
3452 #define VARG1 1 /* digit prefix (first, eg, 5l) */
3453 #define VEXTCMD 2 /* cmd + movement (eg, cl) */
3454 #define VARG2 3 /* digit prefix (second, eg, 2c3l) */
3455 #define VXCH 4 /* f, F, t, T, @ */
3456 #define VFAIL 5 /* bad command */
3457 #define VCMD 6 /* single char command (eg, X) */
3458 #define VREDO 7 /* . */
3459 #define VLIT 8 /* ^V */
3460 #define VSEARCH 9 /* /, ? */
3461 #define VVERSION 10 /* <ESC> ^V */
3462 #define VPREFIX2 11 /* ^[[ and ^[O in insert mode */
3464 static struct edstate *save_edstate(struct edstate *old);
3465 static void restore_edstate(struct edstate *old, struct edstate *news);
3466 static void free_edstate(struct edstate *old);
3468 static struct edstate ebuf;
3469 static struct edstate undobuf;
3471 static struct edstate *es; /* current editor state */
3472 static struct edstate *undo;
3474 static char *ibuf; /* input buffer */
3475 static bool first_insert; /* set when starting in insert mode */
3476 static int saved_inslen; /* saved inslen for first insert */
3477 static int inslen; /* length of input buffer */
3478 static int srchlen; /* length of current search pattern */
3479 static char *ybuf; /* yank buffer */
3480 static int yanklen; /* length of yank buffer */
3481 static int fsavecmd = ' '; /* last find command */
3482 static int fsavech; /* character to find */
3483 static char lastcmd[MAXVICMD]; /* last non-move command */
3484 static int lastac; /* argcnt for lastcmd */
3485 static int lastsearch = ' '; /* last search command */
3486 static char srchpat[SRCHLEN]; /* last search pattern */
3487 static int insert; /* <>0 in insert mode */
3488 static int hnum; /* position in history */
3489 static int ohnum; /* history line copied (after mod) */
3490 static int hlast; /* 1 past last position in history */
3494 * Information for keeping track of macros that are being expanded.
3495 * The format of buf is the alias contents followed by a NUL byte followed
3496 * by the name (letter) of the alias. The end of the buffer is marked by
3497 * a double NUL. The name of the alias is stored so recursive macros can
3500 struct macro_state {
3501 unsigned char *p; /* current position in buf */
3502 unsigned char *buf; /* pointer to macro(s) being expanded */
3503 size_t len; /* how much data in buffer */
3505 static struct macro_state macro;
3507 /* last input was expanded */
3508 static enum expand_mode {
3509 NONE = 0, EXPAND, COMPLETE, PRINT
3518 ohnum = hnum = hlast = histnum(-1) + 1;
3520 saved_inslen = inslen;
3521 first_insert = true;
3526 if (undobuf.cbuf == NULL) {
3527 ibuf = alloc(LINE, AEDIT);
3528 ybuf = alloc(LINE, AEDIT);
3529 undobuf.cbuf = alloc(LINE, AEDIT);
3531 undobuf.cbufsize = ebuf.cbufsize = LINE;
3532 undobuf.linelen = ebuf.linelen = 0;
3533 undobuf.cursor = ebuf.cursor = 0;
3534 undobuf.winleft = ebuf.winleft = 0;
3541 if (wbuf_len != x_cols - 3 && ((wbuf_len = x_cols - 3))) {
3542 wbuf[0] = aresize(wbuf[0], wbuf_len, AEDIT);
3543 wbuf[1] = aresize(wbuf[1], wbuf_len, AEDIT);
3546 memset(wbuf[0], ' ', wbuf_len);
3547 memset(wbuf[1], ' ', wbuf_len);
3549 winwidth = x_cols - pwidth - 3;
3557 while (/* CONSTCOND */ 1) {
3560 /* end of current macro? */
3562 /* more macros left to finish? */
3565 /* must be the end of all the macros */
3574 if (state != VLIT) {
3575 if (c == edchars.intr || c == edchars.quit) {
3576 /* pretend we got an interrupt */
3579 trapsig(c == edchars.intr ? SIGINT : SIGQUIT);
3582 } else if (c == edchars.eof && state != VVERSION) {
3583 if (es->linelen == 0) {
3584 x_vi_zotc(edchars.eof);
3600 if (c == -1 || (ssize_t)LINE <= es->linelen)
3603 if (es->cbuf != buf)
3604 memcpy(buf, es->cbuf, es->linelen);
3606 buf[es->linelen++] = '\n';
3608 return (es->linelen);
3614 static char curcmd[MAXVICMD], locpat[SRCHLEN];
3615 static int cmdlen, argc1, argc2;
3621 if (ch == Ctrl('v')) {
3625 switch (vi_insert(ch)) {
3631 if (state == VLIT) {
3635 refresh(insert != 0);
3641 if (ch == '\r' || ch == '\n')
3645 if (ch >= '1' && ch <= '9') {
3649 curcmd[cmdlen++] = ch;
3650 state = nextstate(ch);
3651 if (state == VSEARCH) {
3655 if (putbuf(ch == '/' ? "/" : "?", 1,
3660 if (state == VVERSION) {
3665 strlen(KSH_VERSION), false);
3674 del_range(es->cursor, es->cursor + 1);
3677 es->cbuf[es->cursor++] = ch;
3689 if (ksh_isdigit(ch))
3690 argc1 = argc1 * 10 + ch - '0';
3692 curcmd[cmdlen++] = ch;
3693 state = nextstate(ch);
3699 if (ch >= '1' && ch <= '9') {
3704 curcmd[cmdlen++] = ch;
3705 if (ch == curcmd[0])
3707 else if (is_move(ch))
3708 state = nextstate(ch);
3715 if (ksh_isdigit(ch))
3716 argc2 = argc2 * 10 + ch - '0';
3722 curcmd[cmdlen++] = ch;
3723 if (ch == curcmd[0])
3725 else if (is_move(ch))
3726 state = nextstate(ch);
3733 if (ch == Ctrl('['))
3736 curcmd[cmdlen++] = ch;
3742 if (ch == '\r' || ch == '\n' /*|| ch == Ctrl('[')*/ ) {
3744 /* Repeat last search? */
3753 locpat[srchlen] = '\0';
3754 memcpy(srchpat, locpat, srchlen + 1);
3757 } else if (ch == edchars.erase || ch == Ctrl('h')) {
3760 es->linelen -= char_len((unsigned char)locpat[srchlen]);
3761 es->cursor = es->linelen;
3768 } else if (ch == edchars.kill) {
3774 } else if (ch == edchars.werase) {
3776 struct edstate new_es, *save_es;
3779 new_es.cbuf = locpat;
3786 for (i = srchlen; --i >= n; )
3787 es->linelen -= char_len((unsigned char)locpat[i]);
3789 es->cursor = es->linelen;
3793 if (srchlen == SRCHLEN - 1)
3796 locpat[srchlen++] = ch;
3797 if (ch < ' ' || ch == 0x7f) {
3798 if ((size_t)es->linelen + 2 >
3799 (size_t)es->cbufsize)
3801 es->cbuf[es->linelen++] = '^';
3802 es->cbuf[es->linelen++] = ch ^ '@';
3804 if (es->linelen >= es->cbufsize)
3806 es->cbuf[es->linelen++] = ch;
3808 es->cursor = es->linelen;
3819 /* the cursor may not be at the BOL */
3822 /* nor further in the line than we can search for */
3823 if ((size_t)es->cursor >= sizeof(srchpat) - 1)
3824 es->cursor = sizeof(srchpat) - 2;
3825 /* anchor the search pattern */
3827 /* take the current line up to the cursor */
3828 memmove(srchpat + 1, es->cbuf, es->cursor);
3829 srchpat[es->cursor + 1] = '\0';
3830 /* set a magic flag */
3831 argc1 = 2 + (int)es->cursor;
3832 /* and emulate a backwards history search */
3844 switch (vi_cmd(argc1, curcmd)) {
3852 refresh(insert != 0);
3858 /* back from a 'v' command - don't redraw the screen */
3867 switch (vi_cmd(lastac, lastcmd)) {
3874 if (lastcmd[0] == 's' || lastcmd[0] == 'c' ||
3875 lastcmd[0] == 'C') {
3876 if (redo_insert(1) != 0)
3879 if (redo_insert(lastac) != 0)
3889 /* back from a 'v' command - can't happen */
3907 else if (is_srch(ch))
3909 else if (is_long(ch))
3913 else if (ch == Ctrl('v'))
3915 else if (is_cmd(ch))
3926 if (ch == edchars.erase || ch == Ctrl('h')) {
3927 if (insert == REPLACE) {
3928 if (es->cursor == undo->cursor) {
3935 if (es->cursor >= undo->linelen)
3938 es->cbuf[es->cursor] = undo->cbuf[es->cursor];
3940 if (es->cursor == 0)
3946 memmove(&es->cbuf[es->cursor], &es->cbuf[es->cursor + 1],
3947 es->linelen - es->cursor + 1);
3952 if (ch == edchars.kill) {
3953 if (es->cursor != 0) {
3955 memmove(es->cbuf, &es->cbuf[es->cursor],
3956 es->linelen - es->cursor);
3957 es->linelen -= es->cursor;
3963 if (ch == edchars.werase) {
3964 if (es->cursor != 0) {
3965 tcursor = backword(1);
3966 memmove(&es->cbuf[tcursor], &es->cbuf[es->cursor],
3967 es->linelen - es->cursor);
3968 es->linelen -= es->cursor - tcursor;
3969 if (inslen < es->cursor - tcursor)
3972 inslen -= es->cursor - tcursor;
3973 es->cursor = tcursor;
3979 * If any chars are entered before escape, trash the saved insert
3980 * buffer (if user inserts & deletes char, ibuf gets trashed and
3981 * we don't want to use it)
3983 if (first_insert && ch != Ctrl('['))
3996 first_insert = false;
3998 inslen = saved_inslen;
3999 return (redo_insert(0));
4004 if (lastcmd[0] == 's' || lastcmd[0] == 'c' ||
4006 return (redo_insert(0));
4008 return (redo_insert(lastac - 1));
4010 /* { Begin nonstandard vi commands */
4016 complete_word(0, 0);
4020 print_expansions(es, 0);
4024 if (Flag(FVITABCOMPLETE)) {
4025 complete_word(0, 0);
4029 /* End nonstandard vi commands } */
4032 if (es->linelen >= es->cbufsize - 1)
4034 ibuf[inslen++] = ch;
4035 if (insert == INSERT) {
4036 memmove(&es->cbuf[es->cursor + 1], &es->cbuf[es->cursor],
4037 es->linelen - es->cursor);
4040 es->cbuf[es->cursor++] = ch;
4041 if (insert == REPLACE && es->cursor > es->linelen)
4049 vi_cmd(int argcnt, const char *cmd)
4052 int cur, c1, c2, c3 = 0;
4056 if (argcnt == 0 && !is_zerocount(*cmd))
4059 if (is_move(*cmd)) {
4060 if ((cur = domove(argcnt, cmd, 0)) >= 0) {
4061 if (cur == es->linelen && cur != 0)
4067 /* Don't save state in middle of macro.. */
4068 if (is_undoable(*cmd) && !macro.p) {
4069 undo->winleft = es->winleft;
4070 memmove(undo->cbuf, es->cbuf, es->linelen);
4071 undo->linelen = es->linelen;
4072 undo->cursor = es->cursor;
4074 memmove(lastcmd, cmd, MAXVICMD);
4085 static char alias[] = "_\0";
4090 /* lookup letter in alias list... */
4092 ap = ktsearch(&aliases, alias, hash(alias));
4093 if (!cmd[1] || !ap || !(ap->flag & ISSET))
4095 /* check if this is a recursive call... */
4096 if ((p = (char *)macro.p))
4097 while ((p = strnul(p)) && p[1])
4100 /* insert alias into macro buffer */
4101 nlen = strlen(ap->val.s) + 1;
4102 olen = !macro.p ? 2 :
4103 macro.len - (macro.p - macro.buf);
4105 * at this point, it's fairly reasonable that
4106 * nlen + olen + 2 doesn't overflow
4108 nbuf = alloc(nlen + 1 + olen, AEDIT);
4109 memcpy(nbuf, ap->val.s, nlen);
4110 nbuf[nlen++] = cmd[1];
4112 memcpy(nbuf + nlen, macro.p, olen);
4113 afree(macro.buf, AEDIT);
4116 nbuf[nlen++] = '\0';
4117 nbuf[nlen++] = '\0';
4119 macro.p = macro.buf = (unsigned char *)nbuf;
4127 if (es->linelen != 0)
4136 es->cursor = es->linelen;
4141 es->cursor = domove(1, "^", 1);
4142 del_range(es->cursor, es->linelen);
4154 if (*cmd == cmd[1]) {
4155 c1 = *cmd == 'c' ? domove(1, "^", 1) : 0;
4157 } else if (!is_move(cmd[1]))
4160 if ((ncursor = domove(argcnt, &cmd[1], 1)) < 0)
4163 (cmd[1] == 'w' || cmd[1] == 'W') &&
4164 !ksh_isspace(es->cbuf[es->cursor])) {
4167 } while (ksh_isspace(es->cbuf[ncursor]));
4170 if (ncursor > es->cursor) {
4180 if (*cmd != 'c' && c1 != c2)
4196 if (es->linelen != 0)
4198 while (putbuf(ybuf, yanklen, false) == 0 &&
4201 if (es->cursor != 0)
4211 while (putbuf(ybuf, yanklen, false) == 0 &&
4214 if (any && es->cursor != 0)
4223 del_range(es->cursor, es->linelen);
4228 yank_range(es->cursor, es->linelen);
4229 del_range(es->cursor, es->linelen);
4230 if (es->cursor != 0)
4242 argcnt = hlast - (source->line - argcnt);
4243 if (grabhist(modified, argcnt - 1) < 0)
4260 es->cursor = domove(1, "^", 1);
4267 if (grabhist(modified, hnum + argcnt) < 0)
4278 if (grabhist(modified, hnum - argcnt) < 0)
4287 if (es->linelen == 0)
4296 if (es->cursor + argcnt > es->linelen)
4298 for (n = 0; n < argcnt; ++n)
4299 es->cbuf[es->cursor + n] = cmd[1];
4300 es->cursor += n - 1;
4311 if (es->linelen == 0)
4315 if (es->cursor + argcnt > es->linelen)
4316 argcnt = es->linelen - es->cursor;
4317 del_range(es->cursor, es->cursor + argcnt);
4323 if (es->linelen == 0)
4326 es->cbuf[es->linelen] = '\0';
4327 histsave(&source->line, es->cbuf, true,
4330 argcnt = source->line + 1 -
4334 shf_snprintf(es->cbuf, es->cbufsize, "%s %d",
4335 "fc -e ${VISUAL:-${EDITOR:-vi}} --",
4339 "fc -e ${VISUAL:-${EDITOR:-vi}} --",
4341 es->linelen = strlen(es->cbuf);
4345 if (es->linelen == 0)
4349 if (es->cursor + argcnt > es->linelen)
4350 argcnt = es->linelen - es->cursor;
4351 yank_range(es->cursor, es->cursor + argcnt);
4352 del_range(es->cursor, es->cursor + argcnt);
4356 if (es->cursor > 0) {
4359 if (es->cursor < argcnt)
4360 argcnt = es->cursor;
4361 yank_range(es->cursor - argcnt, es->cursor);
4362 del_range(es->cursor - argcnt, es->cursor);
4363 es->cursor -= argcnt;
4377 if (grabhist(modified, ohnum) < 0)
4394 if (lastsearch == ' ')
4396 if (lastsearch == '?')
4402 if ((c2 = grabsearch(modified, hnum,
4403 c1, srchpat)) < 0) {
4415 /* flag from cursor-up command */
4416 es->cursor = argcnt - 2;
4425 if (histnum(-1) < 0)
4428 #define issp(c) (ksh_isspace(c) || (c) == '\n')
4430 while (*p && issp(*p))
4432 while (*p && --argcnt) {
4433 while (*p && !issp(*p))
4435 while (*p && issp(*p))
4457 if (es->cursor != es->linelen)
4459 while (*p && !issp(*p)) {
4463 if (putbuf(" ", 1, false) != 0 ||
4464 putbuf(sp, argcnt, false) != 0) {
4465 if (es->cursor != 0)
4478 if (es->linelen == 0)
4480 for (i = 0; i < argcnt; i++) {
4481 p = &es->cbuf[es->cursor];
4482 if (ksh_islower(*p)) {
4485 *p = ksh_toupper(*p);
4486 } else if (ksh_isupper(*p)) {
4489 *p = ksh_tolower(*p);
4491 if (es->cursor < es->linelen - 1)
4499 int ret = x_do_comment(es->cbuf, es->cbufsize,
4508 /* Nonstandard vi/ksh */
4510 print_expansions(es, 1);
4514 /* Nonstandard vi/ksh */
4516 if (!Flag(FVITABCOMPLETE))
4518 complete_word(1, argcnt);
4521 /* some annoying AT&T kshs */
4523 if (!Flag(FVIESCCOMPLETE))
4527 /* Nonstandard vi/ksh */
4529 complete_word(1, argcnt);
4535 /* Nonstandard vi/ksh */
4541 /* mksh: cursor movement */
4545 if (es->linelen != 0)
4550 if (insert == 0 && es->cursor != 0 && es->cursor >= es->linelen)
4557 domove(int argcnt, const char *cmd, int sub)
4559 int bcount, i = 0, t;
4564 if (!sub && es->cursor == 0)
4566 ncursor = backword(argcnt);
4570 if (!sub && es->cursor == 0)
4572 ncursor = Backword(argcnt);
4576 if (!sub && es->cursor + 1 >= es->linelen)
4578 ncursor = endword(argcnt);
4579 if (sub && ncursor < es->linelen)
4584 if (!sub && es->cursor + 1 >= es->linelen)
4586 ncursor = Endword(argcnt);
4587 if (sub && ncursor < es->linelen)
4601 if (fsavecmd == ' ')
4603 i = fsavecmd == 'f' || fsavecmd == 'F';
4607 if ((ncursor = findch(fsavech, argcnt, tobool(t),
4616 if (!sub && es->cursor == 0)
4618 ncursor = es->cursor - argcnt;
4625 if (!sub && es->cursor + 1 >= es->linelen)
4627 if (es->linelen != 0) {
4628 ncursor = es->cursor + argcnt;
4629 if (ncursor > es->linelen)
4630 ncursor = es->linelen;
4635 if (!sub && es->cursor + 1 >= es->linelen)
4637 ncursor = forwword(argcnt);
4641 if (!sub && es->cursor + 1 >= es->linelen)
4643 ncursor = Forwword(argcnt);
4652 while (ncursor < es->linelen - 1 &&
4653 ksh_isspace(es->cbuf[ncursor]))
4659 if (ncursor > es->linelen)
4660 ncursor = es->linelen;
4666 if (es->linelen != 0)
4667 ncursor = es->linelen;
4673 ncursor = es->cursor;
4674 while (ncursor < es->linelen &&
4675 (i = bracktype(es->cbuf[ncursor])) == 0)
4677 if (ncursor == es->linelen)
4682 if (++ncursor >= es->linelen)
4688 t = bracktype(es->cbuf[ncursor]);
4693 } while (bcount != 0);
4705 redo_insert(int count)
4708 if (putbuf(ibuf, inslen, tobool(insert == REPLACE)) != 0)
4717 yank_range(int a, int b)
4721 memmove(ybuf, &es->cbuf[a], yanklen);
4753 * Non user interface editor routines below here
4759 memmove(holdbufp, es->cbuf, es->linelen);
4760 holdlen = es->linelen;
4761 holdbufp[holdlen] = '\0';
4768 es->linelen = holdlen;
4769 memmove(es->cbuf, holdbufp, holdlen);
4772 /* return a new edstate */
4773 static struct edstate *
4774 save_edstate(struct edstate *old)
4776 struct edstate *news;
4778 news = alloc(sizeof(struct edstate), AEDIT);
4779 news->cbuf = alloc(old->cbufsize, AEDIT);
4780 memcpy(news->cbuf, old->cbuf, old->linelen);
4781 news->cbufsize = old->cbufsize;
4782 news->linelen = old->linelen;
4783 news->cursor = old->cursor;
4784 news->winleft = old->winleft;
4789 restore_edstate(struct edstate *news, struct edstate *old)
4791 memcpy(news->cbuf, old->cbuf, old->linelen);
4792 news->linelen = old->linelen;
4793 news->cursor = old->cursor;
4794 news->winleft = old->winleft;
4799 free_edstate(struct edstate *old)
4801 afree(old->cbuf, AEDIT);
4806 * this is used for calling x_escape() in complete_word()
4809 x_vi_putbuf(const char *s, size_t len)
4811 return (putbuf(s, len, false));
4815 putbuf(const char *buf, ssize_t len, bool repl)
4820 if (es->cursor + len >= es->cbufsize)
4822 if (es->cursor + len > es->linelen)
4823 es->linelen = es->cursor + len;
4825 if (es->linelen + len >= es->cbufsize)
4827 memmove(&es->cbuf[es->cursor + len], &es->cbuf[es->cursor],
4828 es->linelen - es->cursor);
4831 memmove(&es->cbuf[es->cursor], buf, len);
4837 del_range(int a, int b)
4839 if (es->linelen != b)
4840 memmove(&es->cbuf[a], &es->cbuf[b], es->linelen - b);
4841 es->linelen -= b - a;
4845 findch(int ch, int cnt, bool forw, bool incl)
4849 if (es->linelen == 0)
4851 ncursor = es->cursor;
4855 if (++ncursor == es->linelen)
4861 } while (es->cbuf[ncursor] != ch);
4873 forwword(int argcnt)
4877 ncursor = es->cursor;
4878 while (ncursor < es->linelen && argcnt--) {
4879 if (ksh_isalnux(es->cbuf[ncursor]))
4880 while (ksh_isalnux(es->cbuf[ncursor]) &&
4881 ncursor < es->linelen)
4883 else if (!ksh_isspace(es->cbuf[ncursor]))
4884 while (!ksh_isalnux(es->cbuf[ncursor]) &&
4885 !ksh_isspace(es->cbuf[ncursor]) &&
4886 ncursor < es->linelen)
4888 while (ksh_isspace(es->cbuf[ncursor]) &&
4889 ncursor < es->linelen)
4896 backword(int argcnt)
4900 ncursor = es->cursor;
4901 while (ncursor > 0 && argcnt--) {
4902 while (--ncursor > 0 && ksh_isspace(es->cbuf[ncursor]))
4905 if (ksh_isalnux(es->cbuf[ncursor]))
4906 while (--ncursor >= 0 &&
4907 ksh_isalnux(es->cbuf[ncursor]))
4910 while (--ncursor >= 0 &&
4911 !ksh_isalnux(es->cbuf[ncursor]) &&
4912 !ksh_isspace(es->cbuf[ncursor]))
4925 ncursor = es->cursor;
4926 while (ncursor < es->linelen && argcnt--) {
4927 while (++ncursor < es->linelen - 1 &&
4928 ksh_isspace(es->cbuf[ncursor]))
4930 if (ncursor < es->linelen - 1) {
4931 if (ksh_isalnux(es->cbuf[ncursor]))
4932 while (++ncursor < es->linelen &&
4933 ksh_isalnux(es->cbuf[ncursor]))
4936 while (++ncursor < es->linelen &&
4937 !ksh_isalnux(es->cbuf[ncursor]) &&
4938 !ksh_isspace(es->cbuf[ncursor]))
4947 Forwword(int argcnt)
4951 ncursor = es->cursor;
4952 while (ncursor < es->linelen && argcnt--) {
4953 while (!ksh_isspace(es->cbuf[ncursor]) &&
4954 ncursor < es->linelen)
4956 while (ksh_isspace(es->cbuf[ncursor]) &&
4957 ncursor < es->linelen)
4964 Backword(int argcnt)
4968 ncursor = es->cursor;
4969 while (ncursor > 0 && argcnt--) {
4970 while (--ncursor >= 0 && ksh_isspace(es->cbuf[ncursor]))
4972 while (ncursor >= 0 && !ksh_isspace(es->cbuf[ncursor]))
4984 ncursor = es->cursor;
4985 while (ncursor < es->linelen - 1 && argcnt--) {
4986 while (++ncursor < es->linelen - 1 &&
4987 ksh_isspace(es->cbuf[ncursor]))
4989 if (ncursor < es->linelen - 1) {
4990 while (++ncursor < es->linelen &&
4991 !ksh_isspace(es->cbuf[ncursor]))
5000 grabhist(int save, int n)
5004 if (n < 0 || n > hlast)
5012 if ((hptr = *histpos()) == NULL) {
5013 internal_warningf("%s: %s", "grabhist", "bad history array");
5018 if ((es->linelen = strlen(hptr)) >= es->cbufsize)
5019 es->linelen = es->cbufsize - 1;
5020 memmove(es->cbuf, hptr, es->linelen);
5027 grabsearch(int save, int start, int fwd, const char *pat)
5033 if ((start == 0 && fwd == 0) || (start >= hlast - 1 && fwd == 1))
5039 anchored = *pat == '^' ? (++pat, 1) : 0;
5040 if ((hist = findhist(start, fwd, pat, anchored)) < 0) {
5041 /* (start != 0 && fwd && match(holdbufp, pat) >= 0) */
5042 if (start != 0 && fwd && strcmp(holdbufp, pat) >= 0) {
5052 if ((es->linelen = strlen(hptr)) >= es->cbufsize)
5053 es->linelen = es->cbufsize - 1;
5054 memmove(es->cbuf, hptr, es->linelen);
5060 redraw_line(bool newl)
5063 memset(wbuf[win], ' ', wbuf_len);
5068 if (prompt_trunc != -1)
5069 pprompt(prompt, prompt_trunc);
5075 refresh(int leftside)
5083 display(wbuf[1 - win], wbuf[win], leftside);
5092 if (es->cursor < es->winleft)
5096 while (cur < es->cursor)
5097 col = newcol((unsigned char)es->cbuf[cur++], col);
5098 if (col >= winwidth)
5107 int holdcur1, holdcol1;
5108 int holdcur2, holdcol2;
5110 holdcur1 = holdcur2 = tcur = 0;
5111 holdcol1 = holdcol2 = tcol = 0;
5112 while (tcur < es->cursor) {
5113 if (tcol - holdcol2 > winwidth / 2) {
5114 holdcur1 = holdcur2;
5115 holdcol1 = holdcol2;
5119 tcol = newcol((unsigned char)es->cbuf[tcur++], tcol);
5121 while (tcol - holdcol1 > winwidth / 2)
5122 holdcol1 = newcol((unsigned char)es->cbuf[holdcur1++],
5124 es->winleft = holdcur1;
5128 newcol(int ch, int col)
5131 return ((col | 7) + 1);
5132 return (col + char_len(ch));
5136 display(char *wb1, char *wb2, int leftside)
5139 char *twb1, *twb2, mc;
5148 while (col < winwidth && cur < es->linelen) {
5149 if (cur == es->cursor && leftside)
5150 ncol = col + pwidth;
5151 if ((ch = es->cbuf[cur]) == '\t')
5154 } while (++col < winwidth && (col & 7) != 0);
5155 else if (col < winwidth) {
5156 if (ch < ' ' || ch == 0x7f) {
5158 if (++col < winwidth) {
5167 if (cur == es->cursor && !leftside)
5168 ncol = col + pwidth - 1;
5171 if (cur == es->cursor)
5172 ncol = col + pwidth;
5173 if (col < winwidth) {
5174 while (col < winwidth) {
5187 if (*twb1 != *twb2) {
5189 ed_mov_opt(col, wb1);
5197 if (es->winleft > 0 && moreright)
5199 * POSIX says to use * for this but that is a globbing
5200 * character and may confuse people; + is more innocuous
5203 else if (es->winleft > 0)
5210 ed_mov_opt(pwidth + winwidth + 1, wb1);
5216 ed_mov_opt(ncol, wb1);
5220 ed_mov_opt(int col, char *wb)
5223 if (col + 1 < x_col - col) {
5225 if (prompt_trunc != -1)
5226 pprompt(prompt, prompt_trunc);
5228 while (x_col++ < col)
5231 while (x_col-- > col)
5235 wb = &wb[x_col - pwidth];
5236 while (x_col++ < col)
5243 /* replace word with all expansions (ie, expand word*) */
5245 expand_word(int cmd)
5247 static struct edstate *buf;
5248 int rval = 0, nwords, start, end, i;
5251 /* Undo previous expansion */
5252 if (cmd == 0 && expanded == EXPAND && buf) {
5253 restore_edstate(es, buf);
5263 i = XCF_COMMAND_FILE | XCF_FULLPATH;
5264 nwords = x_cf_glob(&i, es->cbuf, es->linelen, es->cursor,
5265 &start, &end, &words);
5271 buf = save_edstate(es);
5273 del_range(start, end);
5276 while (i < nwords) {
5277 if (x_escape(words[i], strlen(words[i]), x_vi_putbuf) != 0) {
5281 if (++i < nwords && putbuf(" ", 1, false) != 0) {
5286 i = buf->cursor - end;
5287 if (rval == 0 && i > 0)
5298 complete_word(int cmd, int count)
5300 static struct edstate *buf;
5301 int rval, nwords, start, end, flags;
5307 /* Undo previous completion */
5308 if (cmd == 0 && expanded == COMPLETE && buf) {
5309 print_expansions(buf, 0);
5313 if (cmd == 0 && expanded == PRINT && buf) {
5314 restore_edstate(es, buf);
5325 * XCF_FULLPATH for count 'cause the menu printed by
5326 * print_expansions() was done this way.
5328 flags = XCF_COMMAND_FILE;
5330 flags |= XCF_FULLPATH;
5331 nwords = x_cf_glob(&flags, es->cbuf, es->linelen, es->cursor,
5332 &start, &end, &words);
5341 if (count >= nwords) {
5343 x_print_expansions(nwords, words,
5344 tobool(flags & XCF_IS_COMMAND));
5345 x_free_words(nwords, words);
5350 * Expand the count'th word to its basename
5352 if (flags & XCF_IS_COMMAND) {
5353 match = words[count] +
5354 x_basename(words[count], NULL);
5355 /* If more than one possible match, use full path */
5356 for (i = 0; i < nwords; i++)
5358 strcmp(words[i] + x_basename(words[i],
5359 NULL), match) == 0) {
5360 match = words[count];
5364 match = words[count];
5365 match_len = strlen(match);
5367 /* expanded = PRINT; next call undo */
5370 match_len = x_longest_prefix(nwords, words);
5371 /* next call will list completions */
5372 expanded = COMPLETE;
5373 is_unique = nwords == 1;
5376 buf = save_edstate(es);
5377 del_range(start, end);
5381 * escape all shell-sensitive characters and put the result into
5384 rval = x_escape(match, match_len, x_vi_putbuf);
5386 if (rval == 0 && is_unique) {
5388 * If exact match, don't undo. Allows directory completions
5389 * to be used (ie, complete the next portion of the path).
5394 * append a space if this is a non-directory match
5395 * and not a parameter or homedir substitution
5397 if (match_len > 0 && match[match_len - 1] != '/' &&
5398 !(flags & XCF_IS_NOSPACE))
5399 rval = putbuf(" ", 1, false);
5401 x_free_words(nwords, words);
5406 /* prevent this from being redone... */
5414 print_expansions(struct edstate *est, int cmd MKSH_A_UNUSED)
5416 int start, end, nwords, i;
5419 i = XCF_COMMAND_FILE | XCF_FULLPATH;
5420 nwords = x_cf_glob(&i, est->cbuf, est->linelen, est->cursor,
5421 &start, &end, &words);
5426 x_print_expansions(nwords, words, tobool(i & XCF_IS_COMMAND));
5427 x_free_words(nwords, words);
5432 /* Similar to x_zotc(emacs.c), but no tab weirdness */
5436 if (c < ' ' || c == 0x7f) {
5446 /* Beem out of any macros as soon as an error occurs */
5453 vi_macro_reset(void)
5456 afree(macro.buf, AEDIT);
5457 memset((char *)¯o, 0, sizeof(macro));
5460 #endif /* !MKSH_S_NOVI */
5462 /* called from main.c */
5469 * Set edchars to -2 to force initial binding, except
5470 * we need default values for some deficient systems…
5472 edchars.erase = edchars.kill = edchars.intr = edchars.quit =
5475 edchars.werase = 027;
5477 /* command line editing specific memory allocation */
5479 holdbufp = alloc(LINE, AEDIT);
5481 /* initialise Emacs command line editing mode */
5484 x_tab = alloc2(X_NTABS, sizeof(*x_tab), AEDIT);
5485 for (j = 0; j < X_TABSZ; j++)
5486 x_tab[0][j] = XFUNC_insert;
5487 for (i = 1; i < X_NTABS; i++)
5488 for (j = 0; j < X_TABSZ; j++)
5489 x_tab[i][j] = XFUNC_error;
5490 for (i = 0; i < (int)NELEM(x_defbindings); i++)
5491 x_tab[x_defbindings[i].xdb_tab][x_defbindings[i].xdb_char]
5492 = x_defbindings[i].xdb_func;
5495 x_atab = alloc2(X_NTABS, sizeof(*x_atab), AEDIT);
5496 for (i = 1; i < X_NTABS; i++)
5497 for (j = 0; j < X_TABSZ; j++)
5498 x_atab[i][j] = NULL;
5510 #endif /* !MKSH_NO_CMDLINE_EDITING */