1 /* $OpenBSD: edit.c,v 1.39 2013/12/17 16:37:05 deraadt Exp $ */
2 /* $OpenBSD: edit.h,v 1.9 2011/05/30 17:14:35 martynas Exp $ */
3 /* $OpenBSD: emacs.c,v 1.48 2013/12/17 16:37:05 deraadt Exp $ */
4 /* $OpenBSD: vi.c,v 1.28 2013/12/18 16:45:46 deraadt Exp $ */
7 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
8 * 2011, 2012, 2013, 2014
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.275 2014/01/05 21:57:24 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(bool);
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_init_prompt(false);
152 x_e_rebuildline(MKSH_CLRTOEOL_STRING);
158 return ((n == 1) ? (int)(unsigned char)c : -1);
164 shf_putc(c, shl_out);
167 /*********************************
168 * Misc common code for vi/emacs *
169 *********************************/
172 * Handle the commenting/uncommenting of a line.
174 * 1 if a carriage return is indicated (comment added)
175 * 0 if no return (comment removed)
176 * -1 if there is an error (not enough room for comment chars)
177 * If successful, *lenp contains the new length. Note: cursor should be
178 * moved to the start of the line after (un)commenting.
181 x_do_comment(char *buf, ssize_t bsize, ssize_t *lenp)
183 ssize_t i, j, len = *lenp;
186 /* somewhat arbitrary - it's what AT&T ksh does */
189 /* Already commented? */
193 for (j = 0, i = 1; i < len; i++) {
194 if (!saw_nl || buf[i] != '#')
196 saw_nl = buf[i] == '\n';
203 /* See if there's room for the #s - 1 per \n */
204 for (i = 0; i < len; i++)
207 if (len + n >= bsize)
209 /* Now add them... */
210 for (i = len, j = len + n; --i >= 0; ) {
221 /****************************************************
222 * Common file/command completion code for vi/emacs *
223 ****************************************************/
226 x_print_expansions(int nwords, char * const *words, bool is_command)
228 bool use_copy = false;
230 XPtrV l = { NULL, 0, 0 };
233 * Check if all matches are in the same directory (in this
234 * case, we want to omit the directory name)
237 (prefix_len = x_longest_prefix(nwords, words)) > 0) {
240 /* Special case for 1 match (prefix is whole word) */
242 prefix_len = x_basename(words[0], NULL);
243 /* Any (non-trailing) slashes in non-common word suffixes? */
244 for (i = 0; i < nwords; i++)
245 if (x_basename(words[i] + prefix_len, NULL) >
248 /* All in same directory? */
250 while (prefix_len > 0 && words[0][prefix_len - 1] != '/')
253 XPinit(l, nwords + 1);
254 for (i = 0; i < nwords; i++)
255 XPput(l, words[i] + prefix_len);
260 * Enumerate expansions
264 pr_list(use_copy ? (char **)XPptrv(l) : words);
267 /* not x_free_words() */
272 * Convert backslash-escaped string to QCHAR-escaped
273 * string useful for globbing; loses QCHAR unless it
274 * can squeeze in, eg. by previous loss of backslash
277 x_glob_hlp_add_qchar(char *cp)
280 bool escaping = false;
282 while ((ch = *cp++)) {
283 if (ch == '\\' && !escaping) {
287 if (escaping || (ch == QCHAR && (cp - dp) > 1)) {
289 * empirically made list of chars to escape
290 * for globbing as well as QCHAR itself
311 * Run tilde expansion on argument string, return the result
312 * after unescaping; if the flag is set, the original string
313 * is freed if changed and assumed backslash-escaped, if not
314 * it is assumed QCHAR-escaped
317 x_glob_hlp_tilde_and_rem_qchar(char *s, bool magic_flag)
322 * On the string, check whether we have a tilde expansion,
323 * and if so, discern "~foo/bar" and "~/baz" from "~blah";
324 * if we have a directory part (the former), try to expand
326 if (*s == '~' && (cp = strchr(s, '/')) != NULL) {
327 /* ok, so split into "~foo"/"bar" or "~"/"baz" */
329 /* try to expand the tilde */
330 if (!(dp = tilde(s + 1))) {
331 /* nope, revert damage */
334 /* ok, expand and replace */
335 cp = shf_smprintf("%s/%s", dp, cp);
342 /* ... convert it from backslash-escaped via QCHAR-escaped... */
344 x_glob_hlp_add_qchar(s);
345 /* ... to unescaped, for comparison with the matches */
348 while ((ch = *cp++)) {
349 if (ch == QCHAR && !(ch = *cp++))
360 * - does expansion, checks for no match, etc.
361 * - sets *wordsp to array of matching strings
362 * - returns number of matching strings
365 x_file_glob(int *flagsp, char *toglob, char ***wordsp)
370 struct source *s, *sold;
372 /* remove all escaping backward slashes */
373 x_glob_hlp_add_qchar(toglob);
376 * Convert "foo*" (toglob) to an array of strings (words)
379 s = pushs(SWSTR, ATEMP);
380 s->start = s->str = toglob;
382 if (yylex(ONEWORD | LQCHAR) != LWORD) {
384 internal_warningf("%s: %s", "fileglob", "bad substitution");
391 while (*cp == CHAR || *cp == QCHAR)
393 nwords = DOGLOB | DOTILDE | DOMARKDIRS;
395 /* probably a $FOO expansion */
396 *flagsp |= XCF_IS_NOSPACE;
397 /* this always results in at most one match */
400 expand(yylval.cp, &w, nwords);
402 words = (char **)XPclose(w);
404 for (nwords = 0; words[nwords]; nwords++)
409 /* Expand any tilde and drop all QCHAR for comparison */
410 toglob = x_glob_hlp_tilde_and_rem_qchar(toglob, false);
413 * Check if globbing failed (returned glob pattern),
414 * but be careful (e.g. toglob == "ab*" when the file
415 * "ab*" exists is not an error).
416 * Also, check for empty result - happens if we tried
417 * to glob something which evaluated to an empty
418 * string (e.g., "$FOO" when there is no FOO, etc).
420 if ((strcmp(words[0], toglob) == 0 &&
421 stat(words[0], &statb) < 0) ||
422 words[0][0] == '\0') {
423 x_free_words(nwords, words);
429 if ((*wordsp = nwords ? words : NULL) == NULL && words != NULL)
430 x_free_words(nwords, words);
435 /* Data structure used in x_command_glob() */
436 struct path_order_info {
442 /* Compare routine used in x_command_glob() */
444 path_order_cmp(const void *aa, const void *bb)
446 const struct path_order_info *a = (const struct path_order_info *)aa;
447 const struct path_order_info *b = (const struct path_order_info *)bb;
450 t = strcmp(a->word + a->base, b->word + b->base);
451 return (t ? t : a->path_order - b->path_order);
455 x_command_glob(int flags, char *toglob, char ***wordsp)
462 /* Convert "foo*" (toglob) to a pattern for future use */
463 pat = evalstr(toglob, DOPAT | DOTILDE);
467 glob_table(pat, &w, &keywords);
468 glob_table(pat, &w, &aliases);
469 glob_table(pat, &w, &builtins);
470 for (l = e->loc; l; l = l->next)
471 glob_table(pat, &w, &l->funs);
473 glob_path(flags, pat, &w, path);
474 if ((fpath = str_val(global("FPATH"))) != null)
475 glob_path(flags, pat, &w, fpath);
485 if (flags & XCF_FULLPATH) {
486 /* Sort by basename, then path order */
487 struct path_order_info *info, *last_info = NULL;
488 char **words = (char **)XPptrv(w);
489 size_t i, path_order = 0;
491 info = (struct path_order_info *)
492 alloc2(nwords, sizeof(struct path_order_info), ATEMP);
493 for (i = 0; i < nwords; i++) {
494 info[i].word = words[i];
495 info[i].base = x_basename(words[i], NULL);
496 if (!last_info || info[i].base != last_info->base ||
497 strncmp(words[i], last_info->word, info[i].base) != 0) {
498 last_info = &info[i];
501 info[i].path_order = path_order;
503 qsort(info, nwords, sizeof(struct path_order_info),
505 for (i = 0; i < nwords; i++)
506 words[i] = info[i].word;
509 /* Sort and remove duplicate entries */
510 char **words = (char **)XPptrv(w);
513 qsort(words, nwords, sizeof(void *), xstrcmp);
514 for (i = j = 0; i < nwords - 1; i++) {
515 if (strcmp(words[i], words[i + 1]))
516 words[j++] = words[i];
518 afree(words[i], ATEMP);
520 words[j++] = words[i];
525 *wordsp = (char **)XPclose(w);
530 #define IS_WORDC(c) (!ctype(c, C_LEX1) && (c) != '\'' && (c) != '"' && \
531 (c) != '`' && (c) != '=' && (c) != ':')
534 x_locate_word(const char *buf, int buflen, int pos, int *startp,
539 /* Bad call? Probably should report error */
540 if (pos < 0 || pos > buflen) {
542 *is_commandp = false;
545 /* The case where pos == buflen happens to take care of itself... */
549 * Keep going backwards to start of word (has effect of allowing
550 * one blank after the end of a word)
552 for (; (start > 0 && IS_WORDC(buf[start - 1])) ||
553 (start > 1 && buf[start - 2] == '\\'); start--)
555 /* Go forwards to end of word */
556 for (end = start; end < buflen && IS_WORDC(buf[end]); end++) {
557 if (buf[end] == '\\' && (end + 1) < buflen)
565 /* Figure out if this is a command */
566 while (p >= 0 && ksh_isspace(buf[p]))
568 iscmd = p < 0 || vstrchr(";|&()`", buf[p]);
571 * If command has a /, path, etc. is not searched;
572 * only current directory is searched which is just
573 * like file globbing.
575 for (p = start; p < end; p++)
580 *is_commandp = iscmd;
584 return (end - start);
588 x_cf_glob(int *flagsp, const char *buf, int buflen, int pos, int *startp,
589 int *endp, char ***wordsp)
595 mkssert(buf != NULL);
597 len = x_locate_word(buf, buflen, pos, startp, &is_command);
598 if (!((*flagsp) & XCF_COMMAND))
601 * Don't do command globing on zero length strings - it takes too
602 * long and isn't very useful. File globs are more likely to be
603 * useful, so allow these.
605 if (len == 0 && is_command)
612 * Given a string, copy it and possibly add a '*' to the end.
615 strndupx(toglob, buf + *startp, len + /* the '*' */ 1, ATEMP);
619 * If the pathname contains a wildcard (an unquoted '*',
620 * '?', or '[') or an extglob, then it is globbed based
621 * on that value (i.e., without the appended '*'). Same
622 * for parameter substitutions (as in “cat $HOME/.ss↹”)
623 * without appending a trailing space (LP: #710539), as
624 * well as for “~foo” (but not “~foo/”).
626 for (s = toglob; *s; s++) {
627 if (*s == '\\' && s[1])
629 else if (*s == '?' || *s == '*' || *s == '[' ||
631 /* ?() *() +() @() !() but two already checked */
632 (s[1] == '(' /*)*/ &&
633 (*s == '+' || *s == '@' || *s == '!'))) {
635 * just expand based on the extglob
642 if (*toglob == '~' && !vstrchr(toglob, '/')) {
643 /* neither for '~foo' (but '~foo/bar') */
644 *flagsp |= XCF_IS_NOSPACE;
650 toglob[len + 1] = '\0';
653 * Expand (glob) it now.
656 nwords = is_command ?
657 x_command_glob(*flagsp, toglob, &words) :
658 x_file_glob(flagsp, toglob, &words);
659 afree(toglob, ATEMP);
666 *flagsp |= XCF_IS_COMMAND;
668 *endp = *startp + len;
674 * Find longest common prefix
677 x_longest_prefix(int nwords, char * const * words)
680 size_t j, prefix_len;
686 prefix_len = strlen(words[0]);
687 for (i = 1; i < nwords; i++)
688 for (j = 0, p = words[i]; j < prefix_len; j++)
689 if (p[j] != words[0][j]) {
693 /* false for nwords==1 as 0 = words[0][prefix_len] then */
694 if (UTFMODE && prefix_len && (words[0][prefix_len] & 0xC0) == 0x80)
695 while (prefix_len && (words[0][prefix_len] & 0xC0) != 0xC0)
701 x_free_words(int nwords, char **words)
704 afree(words[--nwords], ATEMP);
709 * Return the offset of the basename of string s (which ends at se - need not
710 * be null terminated). Trailing slashes are ignored. If s is just a slash,
711 * then the offset is 0 (actually, length - 1).
722 x_basename(const char *s, const char *se)
731 /* Skip trailing slashes */
732 for (p = se - 1; p > s && *p == '/'; p--)
734 for (; p > s && *p != '/'; p--)
736 if (*p == '/' && p + 1 < se)
743 * Apply pattern matching to a table: all table entries that match a pattern
747 glob_table(const char *pat, XPtrV *wp, struct table *tp)
753 while ((te = ktnext(&ts)))
754 if (gmatchx(te->name, pat, false)) {
757 strdupx(cp, te->name, ATEMP);
763 glob_path(int flags, const char *pat, XPtrV *wp, const char *lpath)
765 const char *sp = lpath, *p;
767 size_t pathlen, patlen, oldsize, newsize, i, j;
770 patlen = strlen(pat);
771 checkoktoadd(patlen, 129 + X_EXTRA);
773 Xinit(xs, xp, patlen + 128, ATEMP);
775 xp = Xstring(xs, xp);
776 if (!(p = cstrchr(sp, ':')))
781 * Copy sp into xp, stuffing any MAGIC characters
786 XcheckN(xs, xp, pathlen * 2);
796 XcheckN(xs, xp, patlen);
797 memcpy(xp, pat, patlen);
799 oldsize = XPsize(*wp);
801 glob_str(Xstring(xs, xp), wp, true);
802 newsize = XPsize(*wp);
804 /* Check that each match is executable... */
805 words = (char **)XPptrv(*wp);
806 for (i = j = oldsize; i < newsize; i++) {
807 if (ksh_access(words[i], X_OK) == 0) {
809 if (!(flags & XCF_FULLPATH))
810 memmove(words[j], words[j] + pathlen,
811 strlen(words[j] + pathlen) + 1);
814 afree(words[i], ATEMP);
825 * if argument string contains any special characters, they will
826 * be escaped and the result will be put into edit buffer by
827 * keybinding-specific function
830 x_escape(const char *s, size_t len, int (*putbuf_func)(const char *, size_t))
832 size_t add = 0, wlen = len;
833 const char *ifs = str_val(local("IFS", 0));
836 while (wlen - add > 0)
837 if (vstrchr("\"#$&'()*:;<=>?[\\`{|}", s[add]) ||
838 vstrchr(ifs, s[add])) {
839 if (putbuf_func(s, add) != 0) {
843 putbuf_func(s[add] == '\n' ? "'" : "\\", 1);
844 putbuf_func(&s[add], 1);
854 if (wlen > 0 && rval == 0)
855 rval = putbuf_func(s, wlen);
861 /* +++ emacs editing mode +++ */
864 #define AEDIT &aedit /* area for kill ring and macro defns */
866 /* values returned by keyboard functions */
868 #define KEOL 1 /* ^M, ^J */
869 #define KINTR 2 /* ^G, ^C */
872 int (*xf_func)(int c);
877 struct x_defbindings {
878 unsigned char xdb_func; /* XFUNC_* */
879 unsigned char xdb_tab;
880 unsigned char xdb_char;
883 #define XF_ARG 1 /* command takes number prefix */
884 #define XF_NOBIND 2 /* not allowed to bind to function */
885 #define XF_PREFIX 4 /* function sets prefix */
887 /* Separator for completion */
888 #define is_cfs(c) ((c) == ' ' || (c) == '\t' || (c) == '"' || (c) == '\'')
889 /* Separator for motion */
890 #define is_mfs(c) (!(ksh_isalnux(c) || (c) == '$' || ((c) & 0x80)))
892 #define X_NTABS 3 /* normal, meta1, meta2 */
893 #define X_TABSZ 256 /* size of keydef tables etc */
896 * Arguments for do_complete()
897 * 0 = enumerate M-= complete as much as possible and then list
902 CT_LIST, /* list the possible completions */
903 CT_COMPLETE, /* complete to longest prefix */
904 CT_COMPLIST /* complete and then list (if non-exact) */
908 * The following are used for my horizontal scrolling stuff
910 static char *xbuf; /* beg input buffer */
911 static char *xend; /* end input buffer */
912 static char *xcp; /* current position */
913 static char *xep; /* current end */
914 static char *xbp; /* start of visible portion of input buffer */
915 static char *xlp; /* last char visible on screen */
916 static bool x_adj_ok;
918 * we use x_adj_done so that functions can tell
919 * whether x_adjust() has been called while they are active.
921 static int x_adj_done; /* is incremented by x_adjust() */
923 static int x_displen;
924 static int x_arg; /* general purpose arg */
925 static bool x_arg_defaulted; /* x_arg not explicitly set; defaulted to 1 */
927 static bool xlp_valid; /* lastvis pointer was recalculated */
929 static char **x_histp; /* history position */
930 static int x_nextcmd; /* for newline-and-next */
931 static char **x_histncp; /* saved x_histp for " */
932 static char *xmp; /* mark pointer */
933 static unsigned char x_last_command;
934 static unsigned char (*x_tab)[X_TABSZ]; /* key definition */
936 static char *(*x_atab)[X_TABSZ]; /* macro definitions */
938 static unsigned char x_bound[(X_TABSZ * X_NTABS + 7) / 8];
940 static char *killstack[KILLSIZE];
941 static int killsp, killtp;
942 static int x_curprefix;
944 static char *macroptr; /* bind key macro active? */
947 static int winwidth; /* width of window */
948 static char *wbuf[2]; /* window buffers */
949 static int wbuf_len; /* length of window buffers (x_cols - 3) */
950 static int win; /* window buffer in use */
951 static char morec; /* more character at right of window */
952 static int lastref; /* argument to last refresh() */
953 static int holdlen; /* length of holdbuf */
955 static int pwidth; /* width of prompt */
956 static int prompt_trunc; /* how much of prompt to truncate or -1 */
957 static int x_col; /* current column on line */
959 static int x_ins(const char *);
960 static void x_delete(size_t, bool);
961 static size_t x_bword(void);
962 static size_t x_fword(bool);
963 static void x_goto(char *);
964 static char *x_bs0(char *, char *) MKSH_A_PURE;
965 static void x_bs3(char **);
966 static int x_size_str(char *);
967 static int x_size2(char *, char **);
968 static void x_zots(char *);
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) MKSH_A_PURE;
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();
1170 x_init_prompt(bool doprint)
1172 prompt_trunc = pprompt(prompt, doprint ? 0 : -1);
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 */
1192 xlp = xcp = xep = buf;
1197 x_histp = histptr + 1;
1198 x_last_command = XFUNC_error;
1200 x_init_prompt(true);
1201 x_displen = (xx_cols = x_cols) - 2 - (x_col = pwidth);
1206 if (x_nextcmd >= 0) {
1207 int off = source->line - x_nextcmd;
1208 if (histptr - history >= off) {
1209 x_load_hist(histptr - off);
1210 x_histncp = x_histp;
1215 while (/* CONSTCOND */ 1) {
1217 if ((c = x_e_getc()) < 0)
1220 f = x_curprefix == -1 ? XFUNC_insert :
1221 x_tab[x_curprefix][c];
1225 if ((i = x_e_getc()) != '~')
1229 /* avoid bind key macro recursion */
1230 if (macroptr && f == XFUNC_ins_string)
1234 if (!(x_ftab[f].xf_flags & XF_PREFIX) &&
1235 x_last_command != XFUNC_set_arg) {
1237 x_arg_defaulted = true;
1239 i = c | (x_curprefix << 8);
1241 switch ((*x_ftab[f].xf_func)(i)) {
1243 if (!(x_ftab[f].xf_flags & XF_PREFIX))
1250 /* special case for interrupt */
1255 /* ad-hoc hack for fixing the cursor position */
1263 static int left, pos, save_arg;
1267 * Should allow tab and control chars.
1276 if (((c & 0xC0) == 0x80) && left) {
1287 if (x_curprefix == -1) {
1288 /* flush invalid multibyte */
1294 if ((c >= 0xC2) && (c < 0xE0))
1296 else if ((c >= 0xE0) && (c < 0xF0))
1321 macroptr = x_atab[c >> 8][c & 255];
1323 * we no longer need to bother checking if macroptr is
1324 * not NULL but first char is NUL; x_e_getc() does it
1331 x_do_ins(const char *cp, size_t len)
1333 if (xep + len >= xend) {
1337 memmove(xcp + len, xcp, xep - xcp + 1);
1338 memmove(xcp, cp, len);
1346 x_ins(const char *s)
1349 int adj = x_adj_done;
1351 if (x_do_ins(s, strlen(s)) < 0)
1354 * x_zots() may result in a call to x_adjust()
1355 * we want xcp to reflect the new position.
1359 x_adj_ok = tobool(xcp >= xlp);
1361 /* has x_adjust() been called? */
1362 if (adj == x_adj_done) {
1375 x_del_back(int c MKSH_A_UNUSED)
1385 } while ((++i < x_arg) && (xcp != xbuf));
1391 x_del_char(int c MKSH_A_UNUSED)
1397 while (i < (size_t)x_arg) {
1398 utf_ptradjx(cp, cp2);
1413 /* Delete nc chars to the right of the cursor (including cursor position) */
1415 x_delete(size_t nc, bool push)
1425 for (i = 0; i < nc; ++i) {
1429 j = x_size2(cp, &cp2);
1438 if (xmp != NULL && xmp > xcp) {
1445 * This lets us yank a word we have deleted.
1451 /* Copies the NUL */
1452 memmove(xcp, xcp + nb, xep - xcp + 1);
1458 * if we are already filling the line,
1459 * there is no need to ' ', '\b'.
1460 * But if we must, make sure we do the minimum.
1462 if ((i = xx_cols - 2 - x_col) > 0 || xep - xlp == 0) {
1463 nw = i = (nw < i) ? nw : i;
1466 if (x_col == xx_cols - 2) {
1467 x_e_putc2((xep > xlp) ? '>' : (xbp > xbuf) ? '<' : ' ');
1485 x_del_bword(int c MKSH_A_UNUSED)
1487 x_delete(x_bword(), true);
1492 x_mv_bword(int c MKSH_A_UNUSED)
1499 x_mv_fword(int c MKSH_A_UNUSED)
1506 x_del_fword(int c MKSH_A_UNUSED)
1508 x_delete(x_fword(false), true);
1523 while (cp != xbuf && is_mfs(cp[-1])) {
1527 while (cp != xbuf && !is_mfs(cp[-1])) {
1533 return (x_nb2nc(nb));
1547 while (cp != xep && is_mfs(*cp))
1549 while (cp != xep && !is_mfs(*cp))
1552 nc = x_nb2nc(cp - xcp);
1561 cp = cp >= xep ? xep : x_bs0(cp, xbuf);
1562 if (cp < xbp || cp >= utf_skipcols(xbp, x_displen)) {
1563 /* we are heading off screen */
1566 } else if (cp < xcp) {
1570 } else if (cp > xcp) {
1578 x_bs0(char *cp, char *lower_bound)
1581 while ((!lower_bound || (cp > lower_bound)) &&
1582 ((*(unsigned char *)cp & 0xC0) == 0x80))
1592 *p = x_bs0((*p) - 1, NULL);
1593 i = x_size2(*p, NULL);
1599 x_size_str(char *cp)
1603 size += x_size2(cp, &cp);
1608 x_size2(char *cp, char **dcp)
1610 uint8_t c = *(unsigned char *)cp;
1612 if (UTFMODE && (c > 0x7F))
1613 return (utf_widthadj(cp, (const char **)dcp));
1617 /* Kludge, tabs are always four spaces. */
1619 if (ISCTRL(c) && /* but not C1 */ c < 0x80)
1620 /* control unsigned char */
1628 int adj = x_adj_done;
1631 while (*str && str < xlp && adj == x_adj_done)
1638 unsigned char c = **(unsigned char **)cp;
1641 /* Kludge, tabs are always four spaces. */
1644 } else if (ISCTRL(c) && /* but not C1 */ c < 0x80) {
1646 x_e_putc2(UNCTRL(c));
1649 x_e_putc3((const char **)cp);
1653 x_mv_back(int c MKSH_A_UNUSED)
1668 x_mv_forw(int c MKSH_A_UNUSED)
1670 char *cp = xcp, *cp2;
1677 utf_ptradjx(cp, cp2);
1687 x_search_char_forw(int c MKSH_A_UNUSED)
1693 if (x_e_getmbc(tmp) < 0) {
1698 if ((cp = (cp == xep) ? NULL : strstr(cp + 1, tmp)) == NULL &&
1699 (cp = strstr(xbuf, tmp)) == NULL) {
1709 x_search_char_back(int c MKSH_A_UNUSED)
1711 char *cp = xcp, *p, tmp[4];
1714 if (x_e_getmbc(tmp) < 0) {
1718 for (; x_arg--; cp = p)
1726 if ((tmp[1] && ((p+1) > xep)) ||
1727 (tmp[2] && ((p+2) > xep)))
1732 if (b && tmp[1] && p[1] != tmp[1])
1734 if (b && tmp[2] && p[2] != tmp[2])
1744 x_newline(int c MKSH_A_UNUSED)
1754 x_end_of_text(int c MKSH_A_UNUSED)
1756 char tmp = edchars.eof;
1767 x_beg_hist(int c MKSH_A_UNUSED)
1769 x_load_hist(history);
1774 x_end_hist(int c MKSH_A_UNUSED)
1776 x_load_hist(histptr);
1781 x_prev_com(int c MKSH_A_UNUSED)
1783 x_load_hist(x_histp - x_arg);
1788 x_next_com(int c MKSH_A_UNUSED)
1790 x_load_hist(x_histp + x_arg);
1795 * Goto a particular history number obtained from argument.
1796 * If no argument is given history 1 is probably not what you
1797 * want so we'll simply go to the oldest one.
1800 x_goto_hist(int c MKSH_A_UNUSED)
1802 if (x_arg_defaulted)
1803 x_load_hist(history);
1805 x_load_hist(histptr + x_arg - source->line);
1810 x_load_hist(char **hp)
1815 if (hp == histptr + 1) {
1818 } else if (hp < history || hp > histptr) {
1825 oldsize = x_size_str(xbuf);
1827 strlcpy(holdbufp, xbuf, LINE);
1828 strlcpy(xbuf, sp, xend - xbuf);
1830 xep = xcp = xbuf + strlen(xbuf);
1832 if (xep <= x_lastcp()) {
1840 x_nl_next_com(int c MKSH_A_UNUSED)
1842 if (!x_histncp || (x_histp != x_histncp && x_histp != histptr + 1))
1843 /* fresh start of ^O */
1844 x_histncp = x_histp;
1845 x_nextcmd = source->line - (histptr - x_histncp) + 1;
1846 return (x_newline('\n'));
1852 if (xep == xbuf && x_arg_defaulted)
1853 return (x_end_of_text(c));
1855 return (x_del_char(c));
1858 /* reverse incremental history search */
1860 x_search_hist(int c)
1862 int offset = -1; /* offset of match in xbuf, else -1 */
1863 char pat[80 + 1]; /* pattern buffer */
1868 while (/* CONSTCOND */ 1) {
1870 x_e_puts("\nI-search: ");
1874 if ((c = x_e_getc()) < 0)
1877 if (c == CTRL('[')) {
1878 if ((f & 0x7F) == XFUNC_meta1) {
1879 if ((c = x_e_getc()) < 0)
1881 f = x_tab[1][c] & 0x7F;
1882 if (f == XFUNC_meta1 || f == XFUNC_meta2)
1891 if ((c = x_e_getc()) != '~')
1895 if (f == XFUNC_search_hist)
1896 offset = x_search(pat, 0, offset);
1897 else if (f == XFUNC_del_back) {
1907 offset = x_search(pat, 1, offset);
1909 } else if (f == XFUNC_insert) {
1910 /* add char to pattern */
1911 /* overflow check... */
1912 if ((size_t)(p - pat) >= sizeof(pat) - 1) {
1916 *p++ = c, *p = '\0';
1918 /* already have partial match */
1919 offset = x_match(xbuf, pat);
1921 x_goto(xbuf + offset + (p - pat) -
1926 offset = x_search(pat, 0, offset);
1927 } else if (f == XFUNC_abort) {
1929 x_load_hist(histptr + 1);
1942 /* search backward from current line */
1944 x_search(char *pat, int sameline, int offset)
1949 for (hp = x_histp - (sameline ? 0 : 1); hp >= history; --hp) {
1950 i = x_match(*hp, pat);
1955 x_goto(xbuf + i + strlen(pat) - (*pat == '^'));
1965 /* anchored search up from current line */
1967 x_search_hist_up(int c MKSH_A_UNUSED)
1969 return (x_search_dir(-1));
1972 /* anchored search down from current line */
1974 x_search_hist_dn(int c MKSH_A_UNUSED)
1976 return (x_search_dir(1));
1979 /* anchored search in the indicated direction */
1981 x_search_dir(int search_dir /* should've been bool */)
1983 char **hp = x_histp + search_dir;
1984 size_t curs = xcp - xbuf;
1986 while (histptr >= hp && hp >= history) {
1987 if (strncmp(xbuf, *hp, curs) == 0) {
1989 x_goto(xbuf + curs);
1998 /* return position of first match of pattern in string, else -1 */
2000 x_match(char *str, char *pat)
2003 return ((strncmp(str, pat + 1, strlen(pat + 1)) == 0) ? 0 : -1);
2005 char *q = strstr(str, pat);
2006 return ((q == NULL) ? -1 : q - str);
2011 x_del_line(int c MKSH_A_UNUSED)
2017 j = x_size_str(xbuf);
2020 xlp = xbp = xep = xbuf;
2030 x_mv_end(int c MKSH_A_UNUSED)
2037 x_mv_begin(int c MKSH_A_UNUSED)
2044 x_draw_line(int c MKSH_A_UNUSED)
2051 x_e_rebuildline(const char *clrstr)
2053 shf_puts(clrstr, shl_out);
2059 x_cls(int c MKSH_A_UNUSED)
2061 return (x_e_rebuildline(MKSH_CLS_STRING));
2065 * Redraw (part of) the line. If limit is < 0, the everything is redrawn
2066 * on a NEW line, otherwise limit is the screen column up to which needs
2082 if (prompt_trunc != -1)
2083 pprompt(prompt, prompt_trunc);
2086 x_displen = xx_cols - 2 - x_col;
2089 if (xbp != xbuf || xep > xlp)
2093 /* we fill the line */
2100 i -= x_size2(cpl, &cpl);
2104 while ((j < i) || (x_col < (xx_cols - 2))) {
2105 if (!(x_col < (xx_cols - 2)))
2112 /* more off screen */
2117 } else if (xbp > xbuf)
2132 x_transpose(int c MKSH_A_UNUSED)
2134 unsigned int tmpa, tmpb;
2137 * What transpose is meant to do seems to be up for debate. This
2138 * is a general summary of the options; the text is abcd with the
2139 * upper case character or underscore indicating the cursor position:
2140 * Who Before After Before After
2141 * AT&T ksh in emacs mode: abCd abdC abcd_ (bell)
2142 * AT&T ksh in gmacs mode: abCd baCd abcd_ abdc_
2143 * gnu emacs: abCd acbD abcd_ abdc_
2144 * Pdksh currently goes with GNU behavior since I believe this is the
2145 * most common version of emacs, unless in gmacs mode, in which case
2146 * it does the AT&T ksh gmacs mode.
2147 * This should really be broken up into 3 functions so users can bind
2148 * to the one they want.
2153 } else if (xcp == xep || Flag(FGMACS)) {
2154 if (xcp - xbuf == 1) {
2159 * Gosling/Unipress emacs style: Swap two characters before
2160 * the cursor, do not change cursor position
2163 if (utf_mbtowc(&tmpa, xcp) == (size_t)-1) {
2168 if (utf_mbtowc(&tmpb, xcp) == (size_t)-1) {
2172 utf_wctomb(xcp, tmpa);
2174 utf_wctomb(xcp, tmpb);
2178 * GNU emacs style: Swap the characters before and under the
2179 * cursor, move cursor position along one.
2181 if (utf_mbtowc(&tmpa, xcp) == (size_t)-1) {
2186 if (utf_mbtowc(&tmpb, xcp) == (size_t)-1) {
2190 utf_wctomb(xcp, tmpa);
2192 utf_wctomb(xcp, tmpb);
2200 x_literal(int c MKSH_A_UNUSED)
2207 x_meta1(int c MKSH_A_UNUSED)
2214 x_meta2(int c MKSH_A_UNUSED)
2221 x_kill(int c MKSH_A_UNUSED)
2223 size_t col = xcp - xbuf;
2224 size_t lastcol = xep - xbuf;
2227 if (x_arg_defaulted || (narg = x_arg) > lastcol)
2230 x_goto(xbuf + narg);
2234 x_delete(x_nb2nc(ndel), true);
2243 mkssert(xcp != NULL);
2244 strndupx(cp, xcp, nchars, AEDIT);
2245 if (killstack[killsp])
2246 afree(killstack[killsp], AEDIT);
2247 killstack[killsp] = cp;
2248 killsp = (killsp + 1) % KILLSIZE;
2252 x_yank(int c MKSH_A_UNUSED)
2259 if (killstack[killtp] == 0) {
2260 x_e_puts("\nnothing to yank");
2265 x_ins(killstack[killtp]);
2270 x_meta_yank(int c MKSH_A_UNUSED)
2274 if ((x_last_command != XFUNC_yank && x_last_command != XFUNC_meta_yank) ||
2275 killstack[killtp] == 0) {
2277 x_e_puts("\nyank something first");
2281 len = strlen(killstack[killtp]);
2283 x_delete(x_nb2nc(len), false);
2286 killtp = KILLSIZE - 1;
2289 } while (killstack[killtp] == 0);
2290 x_ins(killstack[killtp]);
2295 x_abort(int c MKSH_A_UNUSED)
2298 xlp = xep = xcp = xbp = xbuf;
2306 x_error(int c MKSH_A_UNUSED)
2313 /* special VT100 style key sequence hack */
2317 /* we only support PF2-'1' for now */
2318 if (c != (2 << 8 | '1'))
2319 return (x_error(c));
2321 /* what's the next character? */
2322 switch ((c = x_e_getc())) {
2325 x_arg_defaulted = true;
2326 return (x_mv_begin(0));
2328 /* "interesting" sequence detected */
2334 /* XXX x_e_ungetc is one-octet only */
2335 if ((c = x_e_getc()) != '5' && c != '3')
2339 * At this point, we have read the following octets so far:
2340 * - ESC+[ or ESC+O or Ctrl-X (Prefix 2)
2343 * - 5 (Ctrl key combiner) or 3 (Alt key combiner)
2344 * We can now accept one more octet designating the key.
2347 switch ((c = x_e_getc())) {
2349 return (x_mv_fword(c));
2351 return (x_mv_bword(c));
2356 return (x_error(c));
2361 x_mapin(const char *cp, Area *ap)
2365 strdupx(news, cp, ap);
2368 /* XXX -- should handle \^ escape? */
2371 /*XXX or ^^ escape? this is ugly. */
2373 /* includes '?'; ASCII */
2389 x_mapout2(int c, char **buf)
2413 x_print(int prefix, int key)
2415 int f = x_tab[prefix][key];
2418 /* prefix == 1 || prefix == 2 */
2419 shf_puts(x_mapout(prefix == 1 ?
2420 CTRL('[') : CTRL('X')), shl_stdout);
2422 shprintf("%s = ", x_mapout(key));
2424 shprintf("%s%s = ", x_mapout(key), (f & 0x80) ? "~" : "");
2425 if (XFUNC_VALUE(f) != XFUNC_ins_string)
2427 shprintf("%s\n", x_ftab[XFUNC_VALUE(f)].xf_name);
2430 shprintf("'%s'\n", x_atab[prefix][key]);
2435 x_bind(const char *a1, const char *a2,
2451 if (x_tab == NULL) {
2452 bi_errorf("can't bind, not a tty");
2455 /* List function names */
2457 for (f = 0; f < NELEM(x_ftab); f++)
2458 if (x_ftab[f].xf_name &&
2459 !(x_ftab[f].xf_flags & XF_NOBIND))
2460 shprintf("%s\n", x_ftab[f].xf_name);
2464 for (prefix = 0; prefix < X_NTABS; prefix++)
2465 for (key = 0; key < X_TABSZ; key++) {
2466 f = XFUNC_VALUE(x_tab[prefix][key]);
2467 if (f == XFUNC_insert || f == XFUNC_error
2469 || (macro && f != XFUNC_ins_string)
2473 x_print(prefix, key);
2477 m2 = m1 = x_mapin(a1, ATEMP);
2480 key = (unsigned char)*m1;
2481 f = XFUNC_VALUE(x_tab[prefix][key]);
2482 if (f == XFUNC_meta1)
2484 else if (f == XFUNC_meta2)
2491 && ((*m1 != '~') || *(m1 + 1))
2497 while (*c && (size_t)(m1 - msg) < sizeof(msg) - 3)
2498 x_mapout2(*c++, &m1);
2499 bi_errorf("%s: %s", "too long key sequence", msg);
2503 hastilde = tobool(*m1);
2508 x_print(prefix, key);
2515 f = XFUNC_ins_string;
2516 sp = x_mapin(a2, AEDIT);
2519 for (f = 0; f < NELEM(x_ftab); f++)
2520 if (x_ftab[f].xf_name &&
2521 strcmp(x_ftab[f].xf_name, a2) == 0)
2523 if (f == NELEM(x_ftab) || x_ftab[f].xf_flags & XF_NOBIND) {
2524 bi_errorf("%s: %s %s", a2, "no such", Tfunction);
2530 if (XFUNC_VALUE(x_tab[prefix][key]) == XFUNC_ins_string &&
2531 x_atab[prefix][key])
2532 afree(x_atab[prefix][key], AEDIT);
2534 x_tab[prefix][key] = f
2536 | (hastilde ? 0x80 : 0)
2540 x_atab[prefix][key] = sp;
2543 /* Track what the user has bound so x_mode(true) won't toast things */
2544 if (f == XFUNC_insert)
2545 x_bound[(prefix * X_TABSZ + key) / 8] &=
2546 ~(1 << ((prefix * X_TABSZ + key) % 8));
2548 x_bound[(prefix * X_TABSZ + key) / 8] |=
2549 (1 << ((prefix * X_TABSZ + key) % 8));
2555 bind_if_not_bound(int p, int k, int func)
2560 * Has user already bound this key?
2561 * If so, do not override it.
2563 t = p * X_TABSZ + k;
2564 if (x_bound[t >> 3] & (1 << (t & 7)))
2571 x_set_mark(int c MKSH_A_UNUSED)
2578 x_kill_region(int c MKSH_A_UNUSED)
2595 x_delete(x_nb2nc(rsize), true);
2601 x_xchg_point_mark(int c MKSH_A_UNUSED)
2616 x_noop(int c MKSH_A_UNUSED)
2622 * File/command name completion routines
2625 x_comp_comm(int c MKSH_A_UNUSED)
2627 do_complete(XCF_COMMAND, CT_COMPLETE);
2632 x_list_comm(int c MKSH_A_UNUSED)
2634 do_complete(XCF_COMMAND, CT_LIST);
2639 x_complete(int c MKSH_A_UNUSED)
2641 do_complete(XCF_COMMAND_FILE, CT_COMPLETE);
2646 x_enumerate(int c MKSH_A_UNUSED)
2648 do_complete(XCF_COMMAND_FILE, CT_LIST);
2653 x_comp_file(int c MKSH_A_UNUSED)
2655 do_complete(XCF_FILE, CT_COMPLETE);
2660 x_list_file(int c MKSH_A_UNUSED)
2662 do_complete(XCF_FILE, CT_LIST);
2667 x_comp_list(int c MKSH_A_UNUSED)
2669 do_complete(XCF_COMMAND_FILE, CT_COMPLIST);
2674 x_expand(int c MKSH_A_UNUSED)
2677 int start, end, nwords, i;
2680 nwords = x_cf_glob(&i, xbuf, xep - xbuf, xcp - xbuf,
2681 &start, &end, &words);
2687 x_goto(xbuf + start);
2688 x_delete(x_nb2nc(end - start), false);
2691 while (i < nwords) {
2692 if (x_escape(words[i], strlen(words[i]), x_do_ins) < 0 ||
2693 (++i < nwords && x_ins(" ") < 0)) {
2705 /* XCF_{COMMAND,FILE,COMMAND_FILE} */
2707 /* 0 for list, 1 for complete and 2 for complete-list */
2711 int start, end, nlen, olen, nwords;
2714 nwords = x_cf_glob(&flags, xbuf, xep - xbuf, xcp - xbuf,
2715 &start, &end, &words);
2721 if (type == CT_LIST) {
2722 x_print_expansions(nwords, words,
2723 tobool(flags & XCF_IS_COMMAND));
2725 x_free_words(nwords, words);
2729 nlen = x_longest_prefix(nwords, words);
2732 * always complete single matches;
2733 * any expansion of parameter substitution
2734 * is always at most one result, too
2740 /* make a copy of the original string part */
2741 strndupx(unescaped, xbuf + start, olen, ATEMP);
2743 /* expand any tilde and unescape the string for comparison */
2744 unescaped = x_glob_hlp_tilde_and_rem_qchar(unescaped, true);
2747 * match iff entire original string is part of the
2748 * longest prefix, implying the latter is at least
2749 * the same size (after unescaping)
2751 completed = !strncmp(words[0], unescaped, strlen(unescaped));
2753 afree(unescaped, ATEMP);
2755 if (type == CT_COMPLIST && nwords > 1) {
2757 * print expansions, since we didn't get back
2758 * just a single match
2760 x_print_expansions(nwords, words,
2761 tobool(flags & XCF_IS_COMMAND));
2764 /* expand on the command line */
2768 memmove(xcp, xcp + olen, xep - xcp + 1);
2769 x_escape(words[0], nlen, x_do_ins);
2773 * append a space if this is a single non-directory match
2774 * and not a parameter or homedir substitution
2776 if (nwords == 1 && words[0][nlen - 1] != '/' &&
2777 !(flags & XCF_IS_NOSPACE)) {
2781 x_free_words(nwords, words);
2786 * x_adjust - redraw the line adjusting starting point etc.
2789 * This function is called when we have exceeded the bounds
2790 * of the edit window. It increments x_adj_done so that
2791 * functions like x_ins and x_delete know that we have been
2792 * called and can skip the x_bs() stuff which has already
2793 * been done by x_redraw.
2803 /* flag the fact that we were called */
2807 * calculate the amount of columns we need to "go back"
2808 * from xcp to set xbp to (but never < xbuf) to 2/3 of
2809 * the display width; take care of pwidth though
2811 if ((col_left = xx_cols * 2 / 3) < MIN_EDIT_SPACE) {
2813 * cowardly refuse to do anything
2814 * if the available space is too small;
2815 * fall back to dumb pdksh code
2817 if ((xbp = xcp - (x_displen / 2)) < xbuf)
2819 /* elide UTF-8 fixup as penalty */
2823 /* fix up xbp to just past a character end first */
2824 xbp = xcp >= xep ? xep : x_bs0(xcp, xbuf);
2825 /* walk backwards */
2826 while (xbp > xbuf && col_left > 0) {
2827 xbp = x_bs0(xbp - 1, xbuf);
2828 col_left -= (n = x_size2(xbp, NULL));
2830 /* check if we hit the prompt */
2831 if (xbp == xbuf && xcp != xbuf && col_left >= 0 && col_left < pwidth) {
2832 /* so we did; force scrolling occurs */
2833 xbp += utf_ptradj(xbp);
2845 unget_char = c < 0 ? -1 : (c & 255);
2853 if (unget_char >= 0) {
2861 if ((c = (unsigned char)*macroptr++))
2875 if (c == '\r' || c == '\n')
2877 if (x_col < xx_cols) {
2878 if (UTFMODE && (c > 0x7F)) {
2884 x = utf_wctomb(utf_tmp, c);
2890 width = utf_wcwidth(c);
2907 if (x_adj_ok && (x_col < 0 || x_col >= (xx_cols - 2)))
2912 x_e_putc3(const char **cp)
2914 int width = 1, c = **(const unsigned char **)cp;
2916 if (c == '\r' || c == '\n')
2918 if (x_col < xx_cols) {
2919 if (UTFMODE && (c > 0x7F)) {
2922 width = utf_widthadj(*cp, (const char **)&cp2);
2943 if (x_adj_ok && (x_col < 0 || x_col >= (xx_cols - 2)))
2948 x_e_puts(const char *s)
2950 int adj = x_adj_done;
2952 while (*s && adj == x_adj_done)
2958 * x_set_arg - set an arg value for next function
2961 * This is a simple implementation of M-[0-9].
2972 /* strip command prefix */
2974 while (c >= 0 && ksh_isdigit(c)) {
2975 n = n * 10 + (c - '0');
2977 /* upper bound for repeat */
2978 goto x_set_arg_too_big;
2982 if (c < 0 || first) {
2986 x_arg_defaulted = true;
2990 x_arg_defaulted = false;
2995 /* Comment or uncomment the current line. */
2997 x_comment(int c MKSH_A_UNUSED)
2999 int oldsize = x_size_str(xbuf);
3000 ssize_t len = xep - xbuf;
3001 int ret = x_do_comment(xbuf, xend - xbuf, &len);
3012 return (x_newline('\n'));
3018 x_version(int c MKSH_A_UNUSED)
3020 char *o_xbuf = xbuf, *o_xend = xend;
3021 char *o_xbp = xbp, *o_xep = xep, *o_xcp = xcp;
3022 int lim = x_lastcp() - xbp;
3026 strdupx(v, KSH_VERSION, ATEMP);
3028 xbuf = xbp = xcp = v;
3029 xend = xep = v + (vlen = strlen(v));
3039 x_redraw((int)vlen);
3043 /* This is what AT&T ksh seems to do... Very bizarre */
3053 x_edit_line(int c MKSH_A_UNUSED)
3055 if (x_arg_defaulted) {
3062 histsave(&source->line, xbuf, true, true);
3065 x_arg = source->line - (histptr - x_histp);
3068 shf_snprintf(xbuf, xend - xbuf, "%s %d",
3069 "fc -e ${VISUAL:-${EDITOR:-vi}} --", x_arg);
3071 strlcpy(xbuf, "fc -e ${VISUAL:-${EDITOR:-vi}} --", xend - xbuf);
3072 xep = xbuf + strlen(xbuf);
3073 return (x_newline('\n'));
3079 * x_prev_histword - recover word from prev command
3082 * This function recovers the last word from the previous
3083 * command and inserts it into the current edit line. If a
3084 * numeric arg is supplied then the n'th word from the
3085 * start of the previous command is used.
3086 * As a side effect, trashes the mark in order to achieve
3087 * being called in a repeatable fashion.
3095 x_prev_histword(int c MKSH_A_UNUSED)
3100 /* -1 = defaulted; 0+ = argument */
3101 static int last_arg = -1;
3103 if (x_last_command == XFUNC_prev_histword) {
3104 if (xmp && modified > 1)
3109 last_arg = x_arg_defaulted ? -1 : x_arg;
3110 xhp = histptr - (m - 1);
3111 if ((xhp < history) || !(cp = *xhp)) {
3117 if ((x_arg = last_arg) == -1) {
3118 /* x_arg_defaulted */
3120 rcp = &cp[strlen(cp) - 1];
3122 * ignore white-space after the last word
3124 while (rcp > cp && is_cfs(*rcp))
3126 while (rcp > cp && !is_cfs(*rcp))
3132 /* not x_arg_defaulted */
3137 * ignore white-space at start of line
3139 while (*rcp && is_cfs(*rcp))
3141 while (x_arg-- > 0) {
3142 while (*rcp && !is_cfs(*rcp))
3144 while (*rcp && is_cfs(*rcp))
3148 while (*rcp && !is_cfs(*rcp))
3160 /* Uppercase N(1) words */
3162 x_fold_upper(int c MKSH_A_UNUSED)
3164 return (x_fold_case('U'));
3167 /* Lowercase N(1) words */
3169 x_fold_lower(int c MKSH_A_UNUSED)
3171 return (x_fold_case('L'));
3174 /* Titlecase N(1) words */
3176 x_fold_capitalise(int c MKSH_A_UNUSED)
3178 return (x_fold_case('C'));
3183 * x_fold_case - convert word to UPPER/lower/Capital case
3186 * This function is used to implement M-U/M-u, M-L/M-l, M-C/M-c
3187 * to UPPER CASE, lower case or Capitalise Words.
3203 * first skip over any white-space
3205 while (cp != xep && is_mfs(*cp))
3208 * do the first char on its own since it may be
3209 * a different action than for the rest.
3214 *cp = ksh_tolower(*cp);
3216 /* uppercase, capitalise */
3217 *cp = ksh_toupper(*cp);
3221 * now for the rest of the word
3223 while (cp != xep && !is_mfs(*cp)) {
3226 *cp = ksh_toupper(*cp);
3228 /* lowercase, capitalise */
3229 *cp = ksh_tolower(*cp);
3241 * x_lastcp - last visible char
3247 * This function returns a pointer to that char in the
3248 * edit buffer that will be the last displayed on the
3249 * screen. The sequence:
3255 * Will position the cursor correctly on the screen.
3269 j = x_size2(xlp, &xlp2);
3270 if ((i + j) > x_displen)
3283 static bool x_cur_mode;
3285 if (x_cur_mode == onoff)
3290 x_mkraw(tty_fd, NULL, false);
3292 edchars.erase = tty_state.c_cc[VERASE];
3293 edchars.kill = tty_state.c_cc[VKILL];
3294 edchars.intr = tty_state.c_cc[VINTR];
3295 edchars.quit = tty_state.c_cc[VQUIT];
3296 edchars.eof = tty_state.c_cc[VEOF];
3298 edchars.werase = tty_state.c_cc[VWERASE];
3301 #ifdef _POSIX_VDISABLE
3302 /* Convert unset values to internal 'unset' value */
3303 if (edchars.erase == _POSIX_VDISABLE)
3305 if (edchars.kill == _POSIX_VDISABLE)
3307 if (edchars.intr == _POSIX_VDISABLE)
3309 if (edchars.quit == _POSIX_VDISABLE)
3311 if (edchars.eof == _POSIX_VDISABLE)
3313 if (edchars.werase == _POSIX_VDISABLE)
3314 edchars.werase = -1;
3317 if (edchars.erase >= 0) {
3318 bind_if_not_bound(0, edchars.erase, XFUNC_del_back);
3319 bind_if_not_bound(1, edchars.erase, XFUNC_del_bword);
3321 if (edchars.kill >= 0)
3322 bind_if_not_bound(0, edchars.kill, XFUNC_del_line);
3323 if (edchars.werase >= 0)
3324 bind_if_not_bound(0, edchars.werase, XFUNC_del_bword);
3325 if (edchars.intr >= 0)
3326 bind_if_not_bound(0, edchars.intr, XFUNC_abort);
3327 if (edchars.quit >= 0)
3328 bind_if_not_bound(0, edchars.quit, XFUNC_noop);
3330 mksh_tcset(tty_fd, &tty_state);
3334 /* +++ vi editing mode +++ */
3344 static int vi_hook(int);
3345 static int nextstate(int);
3346 static int vi_insert(int);
3347 static int vi_cmd(int, const char *);
3348 static int domove(int, const char *, int);
3349 static int redo_insert(int);
3350 static void yank_range(int, int);
3351 static int bracktype(int);
3352 static void save_cbuf(void);
3353 static void restore_cbuf(void);
3354 static int putbuf(const char *, ssize_t, bool);
3355 static void del_range(int, int);
3356 static int findch(int, int, bool, bool) MKSH_A_PURE;
3357 static int forwword(int);
3358 static int backword(int);
3359 static int endword(int);
3360 static int Forwword(int);
3361 static int Backword(int);
3362 static int Endword(int);
3363 static int grabhist(int, int);
3364 static int grabsearch(int, int, int, const char *);
3365 static void redraw_line(bool);
3366 static void refresh(int);
3367 static int outofwin(void);
3368 static void rewindow(void);
3369 static int newcol(unsigned char, int);
3370 static void display(char *, char *, int);
3371 static void ed_mov_opt(int, char *);
3372 static int expand_word(int);
3373 static int complete_word(int, int);
3374 static int print_expansions(struct edstate *, int);
3375 #define char_len(c) ((ISCTRL((unsigned char)c) && \
3376 /* but not C1 */ (unsigned char)c < 0x80) ? 2 : 1)
3377 static void x_vi_zotc(int);
3378 static void vi_error(void);
3379 static void vi_macro_reset(void);
3380 static int x_vi_putbuf(const char *, size_t);
3382 #define vC 0x01 /* a valid command that isn't a vM, vE, vU */
3383 #define vM 0x02 /* movement command (h, l, etc.) */
3384 #define vE 0x04 /* extended command (c, d, y) */
3385 #define vX 0x08 /* long command (@, f, F, t, T, etc.) */
3386 #define vU 0x10 /* an UN-undoable command (that isn't a vM) */
3387 #define vB 0x20 /* bad command (^@) */
3388 #define vZ 0x40 /* repeat count defaults to 0 (not 1) */
3389 #define vS 0x80 /* search (/, ?) */
3391 #define is_bad(c) (classify[(c)&0x7f]&vB)
3392 #define is_cmd(c) (classify[(c)&0x7f]&(vM|vE|vC|vU))
3393 #define is_move(c) (classify[(c)&0x7f]&vM)
3394 #define is_extend(c) (classify[(c)&0x7f]&vE)
3395 #define is_long(c) (classify[(c)&0x7f]&vX)
3396 #define is_undoable(c) (!(classify[(c)&0x7f]&vU))
3397 #define is_srch(c) (classify[(c)&0x7f]&vS)
3398 #define is_zerocount(c) (classify[(c)&0x7f]&vZ)
3400 static const unsigned char classify[128] = {
3401 /* 0 1 2 3 4 5 6 7 */
3402 /* 0 ^@ ^A ^B ^C ^D ^E ^F ^G */
3403 vB, 0, 0, 0, 0, vC|vU, vC|vZ, 0,
3404 /* 1 ^H ^I ^J ^K ^L ^M ^N ^O */
3405 vM, vC|vZ, 0, 0, vC|vU, 0, vC, 0,
3406 /* 2 ^P ^Q ^R ^S ^T ^U ^V ^W */
3407 vC, 0, vC|vU, 0, 0, 0, vC, 0,
3408 /* 3 ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
3409 vC, 0, 0, vC|vZ, 0, 0, 0, 0,
3410 /* 4 <space> ! " # $ % & ' */
3411 vM, 0, 0, vC, vM, vM, 0, 0,
3412 /* 5 ( ) * + , - . / */
3413 0, 0, vC, vC, vM, vC, 0, vC|vS,
3414 /* 6 0 1 2 3 4 5 6 7 */
3415 vM, 0, 0, 0, 0, 0, 0, 0,
3416 /* 7 8 9 : ; < = > ? */
3417 0, 0, 0, vM, 0, vC, 0, vC|vS,
3418 /* 8 @ A B C D E F G */
3419 vC|vX, vC, vM, vC, vC, vM, vM|vX, vC|vU|vZ,
3420 /* 9 H I J K L M N O */
3421 0, vC, 0, 0, 0, 0, vC|vU, vU,
3422 /* A P Q R S T U V W */
3423 vC, 0, vC, vC, vM|vX, vC, 0, vM,
3424 /* B X Y Z [ \ ] ^ _ */
3425 vC, vC|vU, 0, vU, vC|vZ, 0, vM, vC|vZ,
3426 /* C ` a b c d e f g */
3427 0, vC, vM, vE, vE, vM, vM|vX, vC|vZ,
3428 /* D h i j k l m n o */
3429 vM, vC, vC|vU, vC|vU, vM, 0, vC|vU, 0,
3430 /* E p q r s t u v w */
3431 vC, 0, vX, vC, vM|vX, vC|vU, vC|vU|vZ, vM,
3432 /* F x y z { | } ~ ^? */
3433 vC, vE|vU, 0, 0, vM|vZ, 0, vC, 0
3442 #define VNORMAL 0 /* command, insert or replace mode */
3443 #define VARG1 1 /* digit prefix (first, eg, 5l) */
3444 #define VEXTCMD 2 /* cmd + movement (eg, cl) */
3445 #define VARG2 3 /* digit prefix (second, eg, 2c3l) */
3446 #define VXCH 4 /* f, F, t, T, @ */
3447 #define VFAIL 5 /* bad command */
3448 #define VCMD 6 /* single char command (eg, X) */
3449 #define VREDO 7 /* . */
3450 #define VLIT 8 /* ^V */
3451 #define VSEARCH 9 /* /, ? */
3452 #define VVERSION 10 /* <ESC> ^V */
3453 #define VPREFIX2 11 /* ^[[ and ^[O in insert mode */
3455 static struct edstate *save_edstate(struct edstate *old);
3456 static void restore_edstate(struct edstate *old, struct edstate *news);
3457 static void free_edstate(struct edstate *old);
3459 static struct edstate ebuf;
3460 static struct edstate undobuf;
3462 static struct edstate *es; /* current editor state */
3463 static struct edstate *undo;
3465 static char *ibuf; /* input buffer */
3466 static bool first_insert; /* set when starting in insert mode */
3467 static int saved_inslen; /* saved inslen for first insert */
3468 static int inslen; /* length of input buffer */
3469 static int srchlen; /* length of current search pattern */
3470 static char *ybuf; /* yank buffer */
3471 static int yanklen; /* length of yank buffer */
3472 static int fsavecmd = ' '; /* last find command */
3473 static int fsavech; /* character to find */
3474 static char lastcmd[MAXVICMD]; /* last non-move command */
3475 static int lastac; /* argcnt for lastcmd */
3476 static int lastsearch = ' '; /* last search command */
3477 static char srchpat[SRCHLEN]; /* last search pattern */
3478 static int insert; /* <>0 in insert mode */
3479 static int hnum; /* position in history */
3480 static int ohnum; /* history line copied (after mod) */
3481 static int hlast; /* 1 past last position in history */
3485 * Information for keeping track of macros that are being expanded.
3486 * The format of buf is the alias contents followed by a NUL byte followed
3487 * by the name (letter) of the alias. The end of the buffer is marked by
3488 * a double NUL. The name of the alias is stored so recursive macros can
3491 struct macro_state {
3492 unsigned char *p; /* current position in buf */
3493 unsigned char *buf; /* pointer to macro(s) being expanded */
3494 size_t len; /* how much data in buffer */
3496 static struct macro_state macro;
3498 /* last input was expanded */
3499 static enum expand_mode {
3500 NONE = 0, EXPAND, COMPLETE, PRINT
3509 ohnum = hnum = hlast = histnum(-1) + 1;
3511 saved_inslen = inslen;
3512 first_insert = true;
3517 if (undobuf.cbuf == NULL) {
3518 ibuf = alloc(LINE, AEDIT);
3519 ybuf = alloc(LINE, AEDIT);
3520 undobuf.cbuf = alloc(LINE, AEDIT);
3522 undobuf.cbufsize = ebuf.cbufsize = LINE;
3523 undobuf.linelen = ebuf.linelen = 0;
3524 undobuf.cursor = ebuf.cursor = 0;
3525 undobuf.winleft = ebuf.winleft = 0;
3529 x_init_prompt(true);
3532 if (wbuf_len != x_cols - 3 && ((wbuf_len = x_cols - 3))) {
3533 wbuf[0] = aresize(wbuf[0], wbuf_len, AEDIT);
3534 wbuf[1] = aresize(wbuf[1], wbuf_len, AEDIT);
3537 memset(wbuf[0], ' ', wbuf_len);
3538 memset(wbuf[1], ' ', wbuf_len);
3540 winwidth = x_cols - pwidth - 3;
3548 while (/* CONSTCOND */ 1) {
3550 c = (unsigned char)*macro.p++;
3551 /* end of current macro? */
3553 /* more macros left to finish? */
3556 /* must be the end of all the macros */
3565 if (state != VLIT) {
3566 if (c == edchars.intr || c == edchars.quit) {
3567 /* pretend we got an interrupt */
3570 trapsig(c == edchars.intr ? SIGINT : SIGQUIT);
3573 } else if (c == edchars.eof && state != VVERSION) {
3574 if (es->linelen == 0) {
3575 x_vi_zotc(edchars.eof);
3591 if (c == -1 || (ssize_t)LINE <= es->linelen)
3594 if (es->cbuf != buf)
3595 memcpy(buf, es->cbuf, es->linelen);
3597 buf[es->linelen++] = '\n';
3599 return (es->linelen);
3605 static char curcmd[MAXVICMD], locpat[SRCHLEN];
3606 static int cmdlen, argc1, argc2;
3612 if (ch == CTRL('v')) {
3616 switch (vi_insert(ch)) {
3622 if (state == VLIT) {
3626 refresh(insert != 0);
3632 if (ch == '\r' || ch == '\n')
3636 if (ch >= '1' && ch <= '9') {
3640 curcmd[cmdlen++] = ch;
3641 state = nextstate(ch);
3642 if (state == VSEARCH) {
3646 if (putbuf(ch == '/' ? "/" : "?", 1,
3651 if (state == VVERSION) {
3656 strlen(KSH_VERSION), false);
3665 del_range(es->cursor, es->cursor + 1);
3668 es->cbuf[es->cursor++] = ch;
3680 if (ksh_isdigit(ch))
3681 argc1 = argc1 * 10 + ch - '0';
3683 curcmd[cmdlen++] = ch;
3684 state = nextstate(ch);
3690 if (ch >= '1' && ch <= '9') {
3695 curcmd[cmdlen++] = ch;
3696 if (ch == curcmd[0])
3698 else if (is_move(ch))
3699 state = nextstate(ch);
3706 if (ksh_isdigit(ch))
3707 argc2 = argc2 * 10 + ch - '0';
3713 curcmd[cmdlen++] = ch;
3714 if (ch == curcmd[0])
3716 else if (is_move(ch))
3717 state = nextstate(ch);
3724 if (ch == CTRL('['))
3727 curcmd[cmdlen++] = ch;
3733 if (ch == '\r' || ch == '\n' /*|| ch == CTRL('[')*/ ) {
3735 /* Repeat last search? */
3744 locpat[srchlen] = '\0';
3745 memcpy(srchpat, locpat, srchlen + 1);
3748 } else if (ch == edchars.erase || ch == CTRL('h')) {
3751 es->linelen -= char_len(locpat[srchlen]);
3752 es->cursor = es->linelen;
3759 } else if (ch == edchars.kill) {
3765 } else if (ch == edchars.werase) {
3767 struct edstate new_es, *save_es;
3769 new_es.cursor = srchlen;
3770 new_es.cbuf = locpat;
3777 i = (unsigned)srchlen;
3779 es->linelen -= char_len(locpat[i]);
3781 es->cursor = es->linelen;
3785 if (srchlen == SRCHLEN - 1)
3788 locpat[srchlen++] = ch;
3789 if (ISCTRL(ch) && /* but not C1 */ ch < 0x80) {
3790 if ((size_t)es->linelen + 2 >
3791 (size_t)es->cbufsize)
3793 es->cbuf[es->linelen++] = '^';
3794 es->cbuf[es->linelen++] = UNCTRL(ch);
3796 if (es->linelen >= es->cbufsize)
3798 es->cbuf[es->linelen++] = ch;
3800 es->cursor = es->linelen;
3811 /* the cursor may not be at the BOL */
3814 /* nor further in the line than we can search for */
3815 if ((size_t)es->cursor >= sizeof(srchpat) - 1)
3816 es->cursor = sizeof(srchpat) - 2;
3817 /* anchor the search pattern */
3819 /* take the current line up to the cursor */
3820 memmove(srchpat + 1, es->cbuf, es->cursor);
3821 srchpat[es->cursor + 1] = '\0';
3822 /* set a magic flag */
3823 argc1 = 2 + (int)es->cursor;
3824 /* and emulate a backwards history search */
3836 switch (vi_cmd(argc1, curcmd)) {
3844 refresh(insert != 0);
3850 /* back from a 'v' command - don't redraw the screen */
3859 switch (vi_cmd(lastac, lastcmd)) {
3866 if (lastcmd[0] == 's' || lastcmd[0] == 'c' ||
3867 lastcmd[0] == 'C') {
3868 if (redo_insert(1) != 0)
3871 if (redo_insert(lastac) != 0)
3881 /* back from a 'v' command - can't happen */
3899 else if (is_srch(ch))
3901 else if (is_long(ch))
3905 else if (ch == CTRL('v'))
3907 else if (is_cmd(ch))
3918 if (ch == edchars.erase || ch == CTRL('h')) {
3919 if (insert == REPLACE) {
3920 if (es->cursor == undo->cursor) {
3927 if (es->cursor >= undo->linelen)
3930 es->cbuf[es->cursor] = undo->cbuf[es->cursor];
3932 if (es->cursor == 0)
3938 memmove(&es->cbuf[es->cursor], &es->cbuf[es->cursor + 1],
3939 es->linelen - es->cursor + 1);
3944 if (ch == edchars.kill) {
3945 if (es->cursor != 0) {
3947 memmove(es->cbuf, &es->cbuf[es->cursor],
3948 es->linelen - es->cursor);
3949 es->linelen -= es->cursor;
3955 if (ch == edchars.werase) {
3956 if (es->cursor != 0) {
3957 tcursor = backword(1);
3958 memmove(&es->cbuf[tcursor], &es->cbuf[es->cursor],
3959 es->linelen - es->cursor);
3960 es->linelen -= es->cursor - tcursor;
3961 if (inslen < es->cursor - tcursor)
3964 inslen -= es->cursor - tcursor;
3965 es->cursor = tcursor;
3971 * If any chars are entered before escape, trash the saved insert
3972 * buffer (if user inserts & deletes char, ibuf gets trashed and
3973 * we don't want to use it)
3975 if (first_insert && ch != CTRL('['))
3988 first_insert = false;
3990 inslen = saved_inslen;
3991 return (redo_insert(0));
3996 if (lastcmd[0] == 's' || lastcmd[0] == 'c' ||
3998 return (redo_insert(0));
4000 return (redo_insert(lastac - 1));
4002 /* { Begin nonstandard vi commands */
4008 complete_word(0, 0);
4012 print_expansions(es, 0);
4016 if (Flag(FVITABCOMPLETE)) {
4017 complete_word(0, 0);
4021 /* End nonstandard vi commands } */
4024 if (es->linelen >= es->cbufsize - 1)
4026 ibuf[inslen++] = ch;
4027 if (insert == INSERT) {
4028 memmove(&es->cbuf[es->cursor + 1], &es->cbuf[es->cursor],
4029 es->linelen - es->cursor);
4032 es->cbuf[es->cursor++] = ch;
4033 if (insert == REPLACE && es->cursor > es->linelen)
4041 vi_cmd(int argcnt, const char *cmd)
4044 int cur, c1, c2, c3 = 0;
4048 if (argcnt == 0 && !is_zerocount(*cmd))
4051 if (is_move(*cmd)) {
4052 if ((cur = domove(argcnt, cmd, 0)) >= 0) {
4053 if (cur == es->linelen && cur != 0)
4059 /* Don't save state in middle of macro.. */
4060 if (is_undoable(*cmd) && !macro.p) {
4061 undo->winleft = es->winleft;
4062 memmove(undo->cbuf, es->cbuf, es->linelen);
4063 undo->linelen = es->linelen;
4064 undo->cursor = es->cursor;
4066 memmove(lastcmd, cmd, MAXVICMD);
4077 static char alias[] = "_\0";
4082 /* lookup letter in alias list... */
4084 ap = ktsearch(&aliases, alias, hash(alias));
4085 if (!cmd[1] || !ap || !(ap->flag & ISSET))
4087 /* check if this is a recursive call... */
4088 if ((p = (char *)macro.p))
4089 while ((p = strnul(p)) && p[1])
4092 /* insert alias into macro buffer */
4093 nlen = strlen(ap->val.s) + 1;
4094 olen = !macro.p ? 2 :
4095 macro.len - (macro.p - macro.buf);
4097 * at this point, it's fairly reasonable that
4098 * nlen + olen + 2 doesn't overflow
4100 nbuf = alloc(nlen + 1 + olen, AEDIT);
4101 memcpy(nbuf, ap->val.s, nlen);
4102 nbuf[nlen++] = cmd[1];
4104 memcpy(nbuf + nlen, macro.p, olen);
4105 afree(macro.buf, AEDIT);
4108 nbuf[nlen++] = '\0';
4109 nbuf[nlen++] = '\0';
4111 macro.p = macro.buf = (unsigned char *)nbuf;
4119 if (es->linelen != 0)
4128 es->cursor = es->linelen;
4133 es->cursor = domove(1, "^", 1);
4134 del_range(es->cursor, es->linelen);
4146 if (*cmd == cmd[1]) {
4147 c1 = *cmd == 'c' ? domove(1, "^", 1) : 0;
4149 } else if (!is_move(cmd[1]))
4152 if ((ncursor = domove(argcnt, &cmd[1], 1)) < 0)
4155 (cmd[1] == 'w' || cmd[1] == 'W') &&
4156 !ksh_isspace(es->cbuf[es->cursor])) {
4159 } while (ksh_isspace(es->cbuf[ncursor]));
4162 if (ncursor > es->cursor) {
4172 if (*cmd != 'c' && c1 != c2)
4188 if (es->linelen != 0)
4190 while (putbuf(ybuf, yanklen, false) == 0 &&
4193 if (es->cursor != 0)
4203 while (putbuf(ybuf, yanklen, false) == 0 &&
4206 if (any && es->cursor != 0)
4215 del_range(es->cursor, es->linelen);
4220 yank_range(es->cursor, es->linelen);
4221 del_range(es->cursor, es->linelen);
4222 if (es->cursor != 0)
4234 argcnt = hlast - (source->line - argcnt);
4235 if (grabhist(modified, argcnt - 1) < 0)
4252 es->cursor = domove(1, "^", 1);
4259 if (grabhist(modified, hnum + argcnt) < 0)
4270 if (grabhist(modified, hnum - argcnt) < 0)
4279 if (es->linelen == 0)
4288 if (es->cursor + argcnt > es->linelen)
4290 for (n = 0; n < argcnt; ++n)
4291 es->cbuf[es->cursor + n] = cmd[1];
4292 es->cursor += n - 1;
4303 if (es->linelen == 0)
4307 if (es->cursor + argcnt > es->linelen)
4308 argcnt = es->linelen - es->cursor;
4309 del_range(es->cursor, es->cursor + argcnt);
4315 if (es->linelen == 0)
4318 es->cbuf[es->linelen] = '\0';
4319 histsave(&source->line, es->cbuf, true,
4322 argcnt = source->line + 1 -
4326 shf_snprintf(es->cbuf, es->cbufsize, "%s %d",
4327 "fc -e ${VISUAL:-${EDITOR:-vi}} --",
4331 "fc -e ${VISUAL:-${EDITOR:-vi}} --",
4333 es->linelen = strlen(es->cbuf);
4337 if (es->linelen == 0)
4341 if (es->cursor + argcnt > es->linelen)
4342 argcnt = es->linelen - es->cursor;
4343 yank_range(es->cursor, es->cursor + argcnt);
4344 del_range(es->cursor, es->cursor + argcnt);
4348 if (es->cursor > 0) {
4351 if (es->cursor < argcnt)
4352 argcnt = es->cursor;
4353 yank_range(es->cursor - argcnt, es->cursor);
4354 del_range(es->cursor - argcnt, es->cursor);
4355 es->cursor -= argcnt;
4369 if (grabhist(modified, ohnum) < 0)
4386 if (lastsearch == ' ')
4388 if (lastsearch == '?')
4394 if ((c2 = grabsearch(modified, hnum,
4395 c1, srchpat)) < 0) {
4407 /* flag from cursor-up command */
4408 es->cursor = argcnt - 2;
4417 if (histnum(-1) < 0)
4420 #define issp(c) (ksh_isspace(c) || (c) == '\n')
4422 while (*p && issp(*p))
4424 while (*p && --argcnt) {
4425 while (*p && !issp(*p))
4427 while (*p && issp(*p))
4449 if (es->cursor != es->linelen)
4451 while (*p && !issp(*p)) {
4455 if (putbuf(" ", 1, false) != 0 ||
4456 putbuf(sp, argcnt, false) != 0) {
4457 if (es->cursor != 0)
4470 if (es->linelen == 0)
4472 for (i = 0; i < argcnt; i++) {
4473 p = &es->cbuf[es->cursor];
4474 if (ksh_islower(*p)) {
4477 *p = ksh_toupper(*p);
4478 } else if (ksh_isupper(*p)) {
4481 *p = ksh_tolower(*p);
4483 if (es->cursor < es->linelen - 1)
4491 int ret = x_do_comment(es->cbuf, es->cbufsize,
4500 /* Nonstandard vi/ksh */
4502 print_expansions(es, 1);
4506 /* Nonstandard vi/ksh */
4508 if (!Flag(FVITABCOMPLETE))
4510 complete_word(1, argcnt);
4513 /* some annoying AT&T kshs */
4515 if (!Flag(FVIESCCOMPLETE))
4519 /* Nonstandard vi/ksh */
4521 complete_word(1, argcnt);
4527 /* Nonstandard vi/ksh */
4533 /* mksh: cursor movement */
4537 if (es->linelen != 0)
4542 if (insert == 0 && es->cursor != 0 && es->cursor >= es->linelen)
4549 domove(int argcnt, const char *cmd, int sub)
4551 int bcount, i = 0, t;
4556 if (!sub && es->cursor == 0)
4558 ncursor = backword(argcnt);
4562 if (!sub && es->cursor == 0)
4564 ncursor = Backword(argcnt);
4568 if (!sub && es->cursor + 1 >= es->linelen)
4570 ncursor = endword(argcnt);
4571 if (sub && ncursor < es->linelen)
4576 if (!sub && es->cursor + 1 >= es->linelen)
4578 ncursor = Endword(argcnt);
4579 if (sub && ncursor < es->linelen)
4593 if (fsavecmd == ' ')
4595 i = fsavecmd == 'f' || fsavecmd == 'F';
4599 if ((ncursor = findch(fsavech, argcnt, tobool(t),
4608 if (!sub && es->cursor == 0)
4610 ncursor = es->cursor - argcnt;
4617 if (!sub && es->cursor + 1 >= es->linelen)
4619 if (es->linelen != 0) {
4620 ncursor = es->cursor + argcnt;
4621 if (ncursor > es->linelen)
4622 ncursor = es->linelen;
4627 if (!sub && es->cursor + 1 >= es->linelen)
4629 ncursor = forwword(argcnt);
4633 if (!sub && es->cursor + 1 >= es->linelen)
4635 ncursor = Forwword(argcnt);
4644 while (ncursor < es->linelen - 1 &&
4645 ksh_isspace(es->cbuf[ncursor]))
4651 if (ncursor > es->linelen)
4652 ncursor = es->linelen;
4658 if (es->linelen != 0)
4659 ncursor = es->linelen;
4665 ncursor = es->cursor;
4666 while (ncursor < es->linelen &&
4667 (i = bracktype(es->cbuf[ncursor])) == 0)
4669 if (ncursor == es->linelen)
4674 if (++ncursor >= es->linelen)
4680 t = bracktype(es->cbuf[ncursor]);
4685 } while (bcount != 0);
4697 redo_insert(int count)
4700 if (putbuf(ibuf, inslen, tobool(insert == REPLACE)) != 0)
4709 yank_range(int a, int b)
4713 memmove(ybuf, &es->cbuf[a], yanklen);
4745 * Non user interface editor routines below here
4751 memmove(holdbufp, es->cbuf, es->linelen);
4752 holdlen = es->linelen;
4753 holdbufp[holdlen] = '\0';
4760 es->linelen = holdlen;
4761 memmove(es->cbuf, holdbufp, holdlen);
4764 /* return a new edstate */
4765 static struct edstate *
4766 save_edstate(struct edstate *old)
4768 struct edstate *news;
4770 news = alloc(sizeof(struct edstate), AEDIT);
4771 news->cbuf = alloc(old->cbufsize, AEDIT);
4772 memcpy(news->cbuf, old->cbuf, old->linelen);
4773 news->cbufsize = old->cbufsize;
4774 news->linelen = old->linelen;
4775 news->cursor = old->cursor;
4776 news->winleft = old->winleft;
4781 restore_edstate(struct edstate *news, struct edstate *old)
4783 memcpy(news->cbuf, old->cbuf, old->linelen);
4784 news->linelen = old->linelen;
4785 news->cursor = old->cursor;
4786 news->winleft = old->winleft;
4791 free_edstate(struct edstate *old)
4793 afree(old->cbuf, AEDIT);
4798 * this is used for calling x_escape() in complete_word()
4801 x_vi_putbuf(const char *s, size_t len)
4803 return (putbuf(s, len, false));
4807 putbuf(const char *buf, ssize_t len, bool repl)
4812 if (es->cursor + len >= es->cbufsize)
4814 if (es->cursor + len > es->linelen)
4815 es->linelen = es->cursor + len;
4817 if (es->linelen + len >= es->cbufsize)
4819 memmove(&es->cbuf[es->cursor + len], &es->cbuf[es->cursor],
4820 es->linelen - es->cursor);
4823 memmove(&es->cbuf[es->cursor], buf, len);
4829 del_range(int a, int b)
4831 if (es->linelen != b)
4832 memmove(&es->cbuf[a], &es->cbuf[b], es->linelen - b);
4833 es->linelen -= b - a;
4837 findch(int ch, int cnt, bool forw, bool incl)
4841 if (es->linelen == 0)
4843 ncursor = es->cursor;
4847 if (++ncursor == es->linelen)
4853 } while (es->cbuf[ncursor] != ch);
4865 forwword(int argcnt)
4869 ncursor = es->cursor;
4870 while (ncursor < es->linelen && argcnt--) {
4871 if (ksh_isalnux(es->cbuf[ncursor]))
4872 while (ksh_isalnux(es->cbuf[ncursor]) &&
4873 ncursor < es->linelen)
4875 else if (!ksh_isspace(es->cbuf[ncursor]))
4876 while (!ksh_isalnux(es->cbuf[ncursor]) &&
4877 !ksh_isspace(es->cbuf[ncursor]) &&
4878 ncursor < es->linelen)
4880 while (ksh_isspace(es->cbuf[ncursor]) &&
4881 ncursor < es->linelen)
4888 backword(int argcnt)
4892 ncursor = es->cursor;
4893 while (ncursor > 0 && argcnt--) {
4894 while (--ncursor > 0 && ksh_isspace(es->cbuf[ncursor]))
4897 if (ksh_isalnux(es->cbuf[ncursor]))
4898 while (--ncursor >= 0 &&
4899 ksh_isalnux(es->cbuf[ncursor]))
4902 while (--ncursor >= 0 &&
4903 !ksh_isalnux(es->cbuf[ncursor]) &&
4904 !ksh_isspace(es->cbuf[ncursor]))
4917 ncursor = es->cursor;
4918 while (ncursor < es->linelen && argcnt--) {
4919 while (++ncursor < es->linelen - 1 &&
4920 ksh_isspace(es->cbuf[ncursor]))
4922 if (ncursor < es->linelen - 1) {
4923 if (ksh_isalnux(es->cbuf[ncursor]))
4924 while (++ncursor < es->linelen &&
4925 ksh_isalnux(es->cbuf[ncursor]))
4928 while (++ncursor < es->linelen &&
4929 !ksh_isalnux(es->cbuf[ncursor]) &&
4930 !ksh_isspace(es->cbuf[ncursor]))
4939 Forwword(int argcnt)
4943 ncursor = es->cursor;
4944 while (ncursor < es->linelen && argcnt--) {
4945 while (!ksh_isspace(es->cbuf[ncursor]) &&
4946 ncursor < es->linelen)
4948 while (ksh_isspace(es->cbuf[ncursor]) &&
4949 ncursor < es->linelen)
4956 Backword(int argcnt)
4960 ncursor = es->cursor;
4961 while (ncursor > 0 && argcnt--) {
4962 while (--ncursor >= 0 && ksh_isspace(es->cbuf[ncursor]))
4964 while (ncursor >= 0 && !ksh_isspace(es->cbuf[ncursor]))
4976 ncursor = es->cursor;
4977 while (ncursor < es->linelen - 1 && argcnt--) {
4978 while (++ncursor < es->linelen - 1 &&
4979 ksh_isspace(es->cbuf[ncursor]))
4981 if (ncursor < es->linelen - 1) {
4982 while (++ncursor < es->linelen &&
4983 !ksh_isspace(es->cbuf[ncursor]))
4992 grabhist(int save, int n)
4996 if (n < 0 || n > hlast)
5004 if ((hptr = *histpos()) == NULL) {
5005 internal_warningf("%s: %s", "grabhist", "bad history array");
5010 if ((es->linelen = strlen(hptr)) >= es->cbufsize)
5011 es->linelen = es->cbufsize - 1;
5012 memmove(es->cbuf, hptr, es->linelen);
5019 grabsearch(int save, int start, int fwd, const char *pat)
5025 if ((start == 0 && fwd == 0) || (start >= hlast - 1 && fwd == 1))
5031 anchored = *pat == '^' ? (++pat, true) : false;
5032 if ((hist = findhist(start, fwd, pat, anchored)) < 0) {
5033 /* (start != 0 && fwd && match(holdbufp, pat) >= 0) */
5034 if (start != 0 && fwd && strcmp(holdbufp, pat) >= 0) {
5044 if ((es->linelen = strlen(hptr)) >= es->cbufsize)
5045 es->linelen = es->cbufsize - 1;
5046 memmove(es->cbuf, hptr, es->linelen);
5052 redraw_line(bool newl)
5055 memset(wbuf[win], ' ', wbuf_len);
5060 if (prompt_trunc != -1)
5061 pprompt(prompt, prompt_trunc);
5067 refresh(int leftside)
5075 display(wbuf[1 - win], wbuf[win], leftside);
5084 if (es->cursor < es->winleft)
5088 while (cur < es->cursor)
5089 col = newcol((unsigned char)es->cbuf[cur++], col);
5090 if (col >= winwidth)
5099 int holdcur1, holdcol1;
5100 int holdcur2, holdcol2;
5102 holdcur1 = holdcur2 = tcur = 0;
5103 holdcol1 = holdcol2 = tcol = 0;
5104 while (tcur < es->cursor) {
5105 if (tcol - holdcol2 > winwidth / 2) {
5106 holdcur1 = holdcur2;
5107 holdcol1 = holdcol2;
5111 tcol = newcol((unsigned char)es->cbuf[tcur++], tcol);
5113 while (tcol - holdcol1 > winwidth / 2)
5114 holdcol1 = newcol((unsigned char)es->cbuf[holdcur1++],
5116 es->winleft = holdcur1;
5120 newcol(unsigned char ch, int col)
5123 return ((col | 7) + 1);
5124 return (col + char_len(ch));
5128 display(char *wb1, char *wb2, int leftside)
5131 char *twb1, *twb2, mc;
5140 while (col < winwidth && cur < es->linelen) {
5141 if (cur == es->cursor && leftside)
5142 ncol = col + pwidth;
5143 if ((ch = es->cbuf[cur]) == '\t')
5146 } while (++col < winwidth && (col & 7) != 0);
5147 else if (col < winwidth) {
5148 if (ISCTRL(ch) && /* but not C1 */ ch < 0x80) {
5150 if (++col < winwidth) {
5151 *twb1++ = UNCTRL(ch);
5159 if (cur == es->cursor && !leftside)
5160 ncol = col + pwidth - 1;
5163 if (cur == es->cursor)
5164 ncol = col + pwidth;
5165 if (col < winwidth) {
5166 while (col < winwidth) {
5179 if (*twb1 != *twb2) {
5181 ed_mov_opt(col, wb1);
5189 if (es->winleft > 0 && moreright)
5191 * POSIX says to use * for this but that is a globbing
5192 * character and may confuse people; + is more innocuous
5195 else if (es->winleft > 0)
5202 ed_mov_opt(pwidth + winwidth + 1, wb1);
5208 ed_mov_opt(ncol, wb1);
5212 ed_mov_opt(int col, char *wb)
5215 if (col + 1 < x_col - col) {
5217 if (prompt_trunc != -1)
5218 pprompt(prompt, prompt_trunc);
5220 while (x_col++ < col)
5223 while (x_col-- > col)
5227 wb = &wb[x_col - pwidth];
5228 while (x_col++ < col)
5235 /* replace word with all expansions (ie, expand word*) */
5237 expand_word(int cmd)
5239 static struct edstate *buf;
5240 int rval = 0, nwords, start, end, i;
5243 /* Undo previous expansion */
5244 if (cmd == 0 && expanded == EXPAND && buf) {
5245 restore_edstate(es, buf);
5255 i = XCF_COMMAND_FILE | XCF_FULLPATH;
5256 nwords = x_cf_glob(&i, es->cbuf, es->linelen, es->cursor,
5257 &start, &end, &words);
5263 buf = save_edstate(es);
5265 del_range(start, end);
5268 while (i < nwords) {
5269 if (x_escape(words[i], strlen(words[i]), x_vi_putbuf) != 0) {
5273 if (++i < nwords && putbuf(" ", 1, false) != 0) {
5278 i = buf->cursor - end;
5279 if (rval == 0 && i > 0)
5290 complete_word(int cmd, int count)
5292 static struct edstate *buf;
5293 int rval, nwords, start, end, flags;
5299 /* Undo previous completion */
5300 if (cmd == 0 && expanded == COMPLETE && buf) {
5301 print_expansions(buf, 0);
5305 if (cmd == 0 && expanded == PRINT && buf) {
5306 restore_edstate(es, buf);
5317 * XCF_FULLPATH for count 'cause the menu printed by
5318 * print_expansions() was done this way.
5320 flags = XCF_COMMAND_FILE;
5322 flags |= XCF_FULLPATH;
5323 nwords = x_cf_glob(&flags, es->cbuf, es->linelen, es->cursor,
5324 &start, &end, &words);
5333 if (count >= nwords) {
5335 x_print_expansions(nwords, words,
5336 tobool(flags & XCF_IS_COMMAND));
5337 x_free_words(nwords, words);
5342 * Expand the count'th word to its basename
5344 if (flags & XCF_IS_COMMAND) {
5345 match = words[count] +
5346 x_basename(words[count], NULL);
5347 /* If more than one possible match, use full path */
5348 for (i = 0; i < nwords; i++)
5350 strcmp(words[i] + x_basename(words[i],
5351 NULL), match) == 0) {
5352 match = words[count];
5356 match = words[count];
5357 match_len = strlen(match);
5359 /* expanded = PRINT; next call undo */
5362 match_len = x_longest_prefix(nwords, words);
5363 /* next call will list completions */
5364 expanded = COMPLETE;
5365 is_unique = nwords == 1;
5368 buf = save_edstate(es);
5369 del_range(start, end);
5373 * escape all shell-sensitive characters and put the result into
5376 rval = x_escape(match, match_len, x_vi_putbuf);
5378 if (rval == 0 && is_unique) {
5380 * If exact match, don't undo. Allows directory completions
5381 * to be used (ie, complete the next portion of the path).
5386 * append a space if this is a non-directory match
5387 * and not a parameter or homedir substitution
5389 if (match_len > 0 && match[match_len - 1] != '/' &&
5390 !(flags & XCF_IS_NOSPACE))
5391 rval = putbuf(" ", 1, false);
5393 x_free_words(nwords, words);
5398 /* prevent this from being redone... */
5406 print_expansions(struct edstate *est, int cmd MKSH_A_UNUSED)
5408 int start, end, nwords, i;
5411 i = XCF_COMMAND_FILE | XCF_FULLPATH;
5412 nwords = x_cf_glob(&i, est->cbuf, est->linelen, est->cursor,
5413 &start, &end, &words);
5418 x_print_expansions(nwords, words, tobool(i & XCF_IS_COMMAND));
5419 x_free_words(nwords, words);
5424 /* Similar to x_zotc(emacs.c), but no tab weirdness */
5438 /* Beem out of any macros as soon as an error occurs */
5445 vi_macro_reset(void)
5448 afree(macro.buf, AEDIT);
5449 memset((char *)¯o, 0, sizeof(macro));
5452 #endif /* !MKSH_S_NOVI */
5454 /* called from main.c */
5461 * Set edchars to -2 to force initial binding, except
5462 * we need default values for some deficient systems…
5464 edchars.erase = edchars.kill = edchars.intr = edchars.quit =
5467 edchars.werase = 027;
5469 /* command line editing specific memory allocation */
5471 holdbufp = alloc(LINE, AEDIT);
5473 /* initialise Emacs command line editing mode */
5476 x_tab = alloc2(X_NTABS, sizeof(*x_tab), AEDIT);
5477 for (j = 0; j < X_TABSZ; j++)
5478 x_tab[0][j] = XFUNC_insert;
5479 for (i = 1; i < X_NTABS; i++)
5480 for (j = 0; j < X_TABSZ; j++)
5481 x_tab[i][j] = XFUNC_error;
5482 for (i = 0; i < (int)NELEM(x_defbindings); i++)
5483 x_tab[x_defbindings[i].xdb_tab][x_defbindings[i].xdb_char]
5484 = x_defbindings[i].xdb_func;
5487 x_atab = alloc2(X_NTABS, sizeof(*x_atab), AEDIT);
5488 for (i = 1; i < X_NTABS; i++)
5489 for (j = 0; j < X_TABSZ; j++)
5490 x_atab[i][j] = NULL;
5502 #endif /* !MKSH_NO_CMDLINE_EDITING */