-/* $OpenBSD: edit.c,v 1.37 2013/01/21 10:13:24 halex Exp $ */
+/* $OpenBSD: edit.c,v 1.38 2013/06/03 15:41:59 tedu Exp $ */
/* $OpenBSD: edit.h,v 1.9 2011/05/30 17:14:35 martynas Exp $ */
/* $OpenBSD: emacs.c,v 1.44 2011/09/05 04:50:33 marco Exp $ */
/* $OpenBSD: vi.c,v 1.26 2009/06/29 22:50:19 martynas Exp $ */
#ifndef MKSH_NO_CMDLINE_EDITING
-__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.265 2013/02/10 19:05:36 tg Exp $");
+__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.270 2013/08/14 20:26:17 tg Exp $");
/*
* in later versions we might use libtermcap for this, but since external
static char editmode;
static int xx_cols; /* for Emacs mode */
static int modified; /* buffer has been "modified" */
-static char holdbuf[LINE]; /* place to hold last edit buffer */
+static char *holdbufp; /* place to hold last edit buffer */
static int x_getc(void);
static void x_putcf(int);
static int x_basename(const char *, const char *);
static void x_free_words(int, char **);
static int x_escape(const char *, size_t, int (*)(const char *, size_t));
-static int x_emacs(char *, size_t);
+static int x_emacs(char *);
static void x_init_prompt(void);
#if !MKSH_S_NOVI
-static int x_vi(char *, size_t);
+static int x_vi(char *);
#endif
#define x_flush() shf_flush(shl_out)
* read an edited command line
*/
int
-x_read(char *buf, size_t len)
+x_read(char *buf)
{
int i;
x_mode(true);
modified = 1;
if (Flag(FEMACS) || Flag(FGMACS))
- i = x_emacs(buf, len);
+ i = x_emacs(buf);
#if !MKSH_S_NOVI
else if (Flag(FVI))
- i = x_vi(buf, len);
+ i = x_vi(buf);
#endif
else
/* internal error */
*/
static int x_adj_done; /* is incremented by x_adjust() */
-static int x_col;
static int x_displen;
static int x_arg; /* general purpose arg */
static bool x_arg_defaulted; /* x_arg not explicitly set; defaulted to 1 */
static char *macroptr; /* bind key macro active? */
#endif
#if !MKSH_S_NOVI
-static int cur_col; /* current column on line */
-static int pwidth; /* width of prompt */
-static int prompt_trunc; /* how much of prompt to truncate */
static int winwidth; /* width of window */
static char *wbuf[2]; /* window buffers */
static int wbuf_len; /* length of window buffers (x_cols - 3) */
static int lastref; /* argument to last refresh() */
static int holdlen; /* length of holdbuf */
#endif
-static bool prompt_redraw; /* false if newline forced after prompt */
+static int pwidth; /* width of prompt */
+static int prompt_trunc; /* how much of prompt to truncate or -1 */
+static int x_col; /* current column on line */
static int x_ins(const char *);
static void x_delete(size_t, bool);
static size_t x_bword(void);
static size_t x_fword(bool);
static void x_goto(char *);
+static char *x_bs0(char *, char *);
static void x_bs3(char **);
static int x_size_str(char *);
static int x_size2(char *, char **);
static void
x_init_prompt(void)
{
- x_col = promptlen(prompt);
- x_adj_ok = true;
- prompt_redraw = true;
- if (x_col >= xx_cols)
- x_col %= xx_cols;
- x_displen = xx_cols - 2 - x_col;
- x_adj_done = 0;
-
- pprompt(prompt, 0);
- if (x_displen < 1) {
- x_col = 0;
- x_displen = xx_cols - 2;
+ prompt_trunc = pprompt(prompt, 0);
+ pwidth = prompt_trunc % x_cols;
+ prompt_trunc -= pwidth;
+ if ((mksh_uari_t)pwidth > ((mksh_uari_t)x_cols - 3 - MIN_EDIT_SPACE)) {
+ /* force newline after prompt */
+ prompt_trunc = -1;
+ pwidth = 0;
x_e_putc2('\n');
- prompt_redraw = false;
}
}
static int
-x_emacs(char *buf, size_t len)
+x_emacs(char *buf)
{
int c, i;
unsigned char f;
- xbp = xbuf = buf; xend = buf + len;
+ xbp = xbuf = buf;
+ xend = buf + LINE;
xlp = xcp = xep = buf;
*xcp = 0;
xlp_valid = true;
x_histp = histptr + 1;
x_last_command = XFUNC_error;
- xx_cols = x_cols;
x_init_prompt();
+ x_displen = (xx_cols = x_cols) - 2 - (x_col = pwidth);
+ x_adj_done = 0;
+ x_adj_ok = true;
x_histncp = NULL;
if (x_nextcmd >= 0) {
static void
x_goto(char *cp)
{
- if (cp >= xep)
- cp = xep;
- else if (UTFMODE)
- while ((cp > xbuf) && ((*cp & 0xC0) == 0x80))
- --cp;
+ cp = cp >= xep ? xep : x_bs0(cp, xbuf);
if (cp < xbp || cp >= utf_skipcols(xbp, x_displen)) {
/* we are heading off screen */
xcp = cp;
}
}
+static char *
+x_bs0(char *cp, char *lower_bound)
+{
+ if (UTFMODE)
+ while ((!lower_bound || (cp > lower_bound)) &&
+ ((*(unsigned char *)cp & 0xC0) == 0x80))
+ --cp;
+ return (cp);
+}
+
static void
x_bs3(char **p)
{
int i;
- (*p)--;
- if (UTFMODE)
- while (((unsigned char)**p & 0xC0) == 0x80)
- (*p)--;
-
+ *p = x_bs0((*p) - 1, NULL);
i = x_size2(*p, NULL);
while (i--)
x_e_putc2('\b');
char *sp = NULL;
if (hp == histptr + 1) {
- sp = holdbuf;
+ sp = holdbufp;
modified = 0;
} else if (hp < history || hp > histptr) {
x_e_putc2(7);
x_histp = hp;
oldsize = x_size_str(xbuf);
if (modified)
- strlcpy(holdbuf, xbuf, sizeof(holdbuf));
+ strlcpy(holdbufp, xbuf, LINE);
strlcpy(xbuf, sp, xend - xbuf);
xbp = xbuf;
xep = xcp = xbuf + strlen(xbuf);
static void
x_redraw(int limit)
{
- int i, j, x_trunc = 0;
+ int i, j;
char *cp;
x_adj_ok = false;
x_e_putc2('\r');
x_flush();
if (xbp == xbuf) {
- x_col = promptlen(prompt);
- if (x_col >= xx_cols)
- x_trunc = (x_col / xx_cols) * xx_cols;
- if (prompt_redraw)
- pprompt(prompt, x_trunc);
- }
- if (x_col >= xx_cols)
- x_col %= xx_cols;
- x_displen = xx_cols - 2 - x_col;
- if (x_displen < 1) {
- x_col = 0;
- x_displen = xx_cols - 2;
+ if (prompt_trunc != -1)
+ pprompt(prompt, prompt_trunc);
+ x_col = pwidth;
}
+ x_displen = xx_cols - 2 - x_col;
xlp_valid = false;
x_zots(xbp);
if (xbp != xbuf || xep > xlp)
static void
x_adjust(void)
{
- /* flag the fact that we were called. */
+ int col_left, n;
+
+ /* flag the fact that we were called */
x_adj_done++;
+
/*
- * we had a problem if the prompt length > xx_cols / 2
+ * calculate the amount of columns we need to "go back"
+ * from xcp to set xbp to (but never < xbuf) to 2/3 of
+ * the display width; take care of pwidth though
*/
- if ((xbp = xcp - (x_displen / 2)) < xbuf)
- xbp = xbuf;
- if (UTFMODE)
- while ((xbp > xbuf) && ((*xbp & 0xC0) == 0x80))
- --xbp;
+ if ((col_left = xx_cols * 2 / 3) < MIN_EDIT_SPACE) {
+ /*
+ * cowardly refuse to do anything
+ * if the available space is too small;
+ * fall back to dumb pdksh code
+ */
+ if ((xbp = xcp - (x_displen / 2)) < xbuf)
+ xbp = xbuf;
+ /* elide UTF-8 fixup as penalty */
+ goto x_adjust_out;
+ }
+
+ /* fix up xbp to just past a character end first */
+ xbp = xcp >= xep ? xep : x_bs0(xcp, xbuf);
+ /* walk backwards */
+ while (xbp > xbuf && col_left > 0) {
+ xbp = x_bs0(xbp - 1, xbuf);
+ col_left -= (n = x_size2(xbp, NULL));
+ }
+ /* check if we hit the prompt */
+ if (xbp == xbuf && xcp != xbuf && col_left >= 0 && col_left < pwidth) {
+ /* so we did; force scrolling occurs */
+ xbp += utf_ptradj(xbp);
+ }
+
+ x_adjust_out:
xlp_valid = false;
x_redraw(xx_cols);
x_flush();
static int bracktype(int);
static void save_cbuf(void);
static void restore_cbuf(void);
-static int putbuf(const char *, ssize_t, int);
+static int putbuf(const char *, ssize_t, bool);
static void del_range(int, int);
-static int findch(int, int, int, int);
+static int findch(int, int, bool, bool);
static int forwword(int);
static int backword(int);
static int endword(int);
/* 8 @ A B C D E F G */
vC|vX, vC, vM, vC, vC, vM, vM|vX, vC|vU|vZ,
/* 9 H I J K L M N O */
- 0, vC, 0, 0, 0, 0, vC|vU, 0,
+ 0, vC, 0, 0, 0, 0, vC|vU, vU,
/* A P Q R S T U V W */
vC, 0, vC, vC, vM|vX, vC, 0, vM,
/* B X Y Z [ \ ] ^ _ */
- vC, vC|vU, 0, 0, vC|vZ, 0, vM, vC|vZ,
+ vC, vC|vU, 0, vU, vC|vZ, 0, vM, vC|vZ,
/* C ` a b c d e f g */
0, vC, vM, vE, vE, vM, vM|vX, vC|vZ,
/* D h i j k l m n o */
#define VLIT 8 /* ^V */
#define VSEARCH 9 /* /, ? */
#define VVERSION 10 /* <ESC> ^V */
-
-static char undocbuf[LINE];
+#define VPREFIX2 11 /* ^[[ and ^[O in insert mode */
static struct edstate *save_edstate(struct edstate *old);
static void restore_edstate(struct edstate *old, struct edstate *news);
static void free_edstate(struct edstate *old);
static struct edstate ebuf;
-static struct edstate undobuf = { undocbuf, 0, LINE, 0, 0 };
+static struct edstate undobuf;
-static struct edstate *es; /* current editor state */
+static struct edstate *es; /* current editor state */
static struct edstate *undo;
-static char ibuf[LINE]; /* input buffer */
-static int first_insert; /* set when starting in insert mode */
+static char *ibuf; /* input buffer */
+static bool first_insert; /* set when starting in insert mode */
static int saved_inslen; /* saved inslen for first insert */
static int inslen; /* length of input buffer */
static int srchlen; /* length of current search pattern */
-static char ybuf[LINE]; /* yank buffer */
+static char *ybuf; /* yank buffer */
static int yanklen; /* length of yank buffer */
static int fsavecmd = ' '; /* last find command */
static int fsavech; /* character to find */
static int lastac; /* argcnt for lastcmd */
static int lastsearch = ' '; /* last search command */
static char srchpat[SRCHLEN]; /* last search pattern */
-static int insert; /* non-zero in insert mode */
+static int insert; /* <>0 in insert mode */
static int hnum; /* position in history */
static int ohnum; /* history line copied (after mod) */
static int hlast; /* 1 past last position in history */
} expanded;
static int
-x_vi(char *buf, size_t len)
+x_vi(char *buf)
{
int c;
ohnum = hnum = hlast = histnum(-1) + 1;
insert = INSERT;
saved_inslen = inslen;
- first_insert = 1;
+ first_insert = true;
inslen = 0;
vi_macro_reset();
+ ebuf.cbuf = buf;
+ if (undobuf.cbuf == NULL) {
+ ibuf = alloc(LINE, AEDIT);
+ ybuf = alloc(LINE, AEDIT);
+ undobuf.cbuf = alloc(LINE, AEDIT);
+ }
+ undobuf.cbufsize = ebuf.cbufsize = LINE;
+ undobuf.linelen = ebuf.linelen = 0;
+ undobuf.cursor = ebuf.cursor = 0;
+ undobuf.winleft = ebuf.winleft = 0;
es = &ebuf;
- es->cbuf = buf;
undo = &undobuf;
- undo->cbufsize = es->cbufsize = len > LINE ? LINE : len;
-
- es->linelen = undo->linelen = 0;
- es->cursor = undo->cursor = 0;
- es->winleft = undo->winleft = 0;
- cur_col = promptlen(prompt);
- prompt_trunc = (cur_col / x_cols) * x_cols;
- cur_col -= prompt_trunc;
-
- pprompt(prompt, 0);
- if ((mksh_uari_t)cur_col > (mksh_uari_t)x_cols - 3 - MIN_EDIT_SPACE) {
- prompt_redraw = false;
- cur_col = 0;
- x_putc('\n');
- } else
- prompt_redraw = true;
- pwidth = cur_col;
+ x_init_prompt();
+ x_col = pwidth;
- if (!wbuf_len || wbuf_len != x_cols - 3) {
- wbuf_len = x_cols - 3;
- wbuf[0] = aresize(wbuf[0], wbuf_len, APERM);
- wbuf[1] = aresize(wbuf[1], wbuf_len, APERM);
+ if (wbuf_len != x_cols - 3 && ((wbuf_len = x_cols - 3))) {
+ wbuf[0] = aresize(wbuf[0], wbuf_len, AEDIT);
+ wbuf[1] = aresize(wbuf[1], wbuf_len, AEDIT);
}
if (wbuf_len) {
memset(wbuf[0], ' ', wbuf_len);
x_putc('\n');
x_flush();
- if (c == -1 || (ssize_t)len <= es->linelen)
+ if (c == -1 || (ssize_t)LINE <= es->linelen)
return (-1);
if (es->cbuf != buf)
- memmove(buf, es->cbuf, es->linelen);
+ memcpy(buf, es->cbuf, es->linelen);
buf[es->linelen++] = '\n';
save_cbuf();
es->cursor = 0;
es->linelen = 0;
- if (ch == '/') {
- if (putbuf("/", 1, 0) != 0)
- return (-1);
- } else if (putbuf("?", 1, 0) != 0)
+ if (putbuf(ch == '/' ? "/" : "?", 1,
+ false) != 0)
return (-1);
refresh(0);
}
es->cursor = 0;
es->linelen = 0;
putbuf(KSH_VERSION,
- strlen(KSH_VERSION), 0);
+ strlen(KSH_VERSION), false);
refresh(0);
}
}
return (0);
}
break;
+
+ case VPREFIX2:
+ state = VFAIL;
+ switch (ch) {
+ case 'A':
+ /* the cursor may not be at the BOL */
+ if (!es->cursor)
+ break;
+ /* nor further in the line than we can search for */
+ if ((size_t)es->cursor >= sizeof(srchpat) - 1)
+ es->cursor = sizeof(srchpat) - 2;
+ /* anchor the search pattern */
+ srchpat[0] = '^';
+ /* take the current line up to the cursor */
+ memmove(srchpat + 1, es->cbuf, es->cursor);
+ srchpat[es->cursor + 1] = '\0';
+ /* set a magic flag */
+ argc1 = 2 + (int)es->cursor;
+ /* and emulate a backwards history search */
+ lastsearch = '/';
+ *curcmd = 'n';
+ goto pseudo_VCMD;
+ }
+ break;
}
switch (state) {
case VCMD:
+ pseudo_VCMD:
state = VNORMAL;
switch (vi_cmd(argc1, curcmd)) {
case -1:
case Ctrl('['):
expanded = NONE;
if (first_insert) {
- first_insert = 0;
+ first_insert = false;
if (inslen == 0) {
inslen = saved_inslen;
return (redo_insert(0));
* at this point, it's fairly reasonable that
* nlen + olen + 2 doesn't overflow
*/
- nbuf = alloc(nlen + 1 + olen, APERM);
+ nbuf = alloc(nlen + 1 + olen, AEDIT);
memcpy(nbuf, ap->val.s, nlen);
nbuf[nlen++] = cmd[1];
if (macro.p) {
memcpy(nbuf + nlen, macro.p, olen);
- afree(macro.buf, APERM);
+ afree(macro.buf, AEDIT);
nlen += olen;
} else {
nbuf[nlen++] = '\0';
hnum = hlast;
if (es->linelen != 0)
es->cursor++;
- while (putbuf(ybuf, yanklen, 0) == 0 && --argcnt > 0)
+ while (putbuf(ybuf, yanklen, false) == 0 &&
+ --argcnt > 0)
;
if (es->cursor != 0)
es->cursor--;
modified = 1;
hnum = hlast;
any = 0;
- while (putbuf(ybuf, yanklen, 0) == 0 && --argcnt > 0)
+ while (putbuf(ybuf, yanklen, false) == 0 &&
+ --argcnt > 0)
any = 1;
if (any && es->cursor != 0)
es->cursor--;
hnum = c2;
ohnum = hnum;
}
+ if (argcnt >= 2) {
+ /* flag from cursor-up command */
+ es->cursor = argcnt - 2;
+ return (0);
+ }
break;
case '_':
{
argcnt++;
p++;
}
- if (putbuf(" ", 1, 0) != 0 ||
- putbuf(sp, argcnt, 0) != 0) {
+ if (putbuf(" ", 1, false) != 0 ||
+ putbuf(sp, argcnt, false) != 0) {
if (es->cursor != 0)
es->cursor--;
return (-1);
case Ctrl('x'):
expand_word(1);
break;
+
+
+ /* mksh: cursor movement */
+ case '[':
+ case 'O':
+ state = VPREFIX2;
+ if (es->linelen != 0)
+ es->cursor++;
+ insert = INSERT;
+ return (0);
}
if (insert == 0 && es->cursor != 0 && es->cursor >= es->linelen)
es->cursor--;
t = fsavecmd > 'a';
if (*cmd == ',')
t = !t;
- if ((ncursor = findch(fsavech, argcnt, t, i)) < 0)
+ if ((ncursor = findch(fsavech, argcnt, tobool(t),
+ tobool(i))) < 0)
return (-1);
if (sub && t)
ncursor++;
redo_insert(int count)
{
while (count-- > 0)
- if (putbuf(ibuf, inslen, insert == REPLACE) != 0)
+ if (putbuf(ibuf, inslen, tobool(insert == REPLACE)) != 0)
return (-1);
if (es->cursor > 0)
es->cursor--;
static void
save_cbuf(void)
{
- memmove(holdbuf, es->cbuf, es->linelen);
+ memmove(holdbufp, es->cbuf, es->linelen);
holdlen = es->linelen;
- holdbuf[holdlen] = '\0';
+ holdbufp[holdlen] = '\0';
}
static void
{
es->cursor = 0;
es->linelen = holdlen;
- memmove(es->cbuf, holdbuf, holdlen);
+ memmove(es->cbuf, holdbufp, holdlen);
}
/* return a new edstate */
{
struct edstate *news;
- news = alloc(sizeof(struct edstate), APERM);
- news->cbuf = alloc(old->cbufsize, APERM);
+ news = alloc(sizeof(struct edstate), AEDIT);
+ news->cbuf = alloc(old->cbufsize, AEDIT);
memcpy(news->cbuf, old->cbuf, old->linelen);
news->cbufsize = old->cbufsize;
news->linelen = old->linelen;
static void
free_edstate(struct edstate *old)
{
- afree(old->cbuf, APERM);
- afree(old, APERM);
+ afree(old->cbuf, AEDIT);
+ afree(old, AEDIT);
}
/*
static int
x_vi_putbuf(const char *s, size_t len)
{
- return (putbuf(s, len, 0));
+ return (putbuf(s, len, false));
}
static int
-putbuf(const char *buf, ssize_t len, int repl)
+putbuf(const char *buf, ssize_t len, bool repl)
{
if (len == 0)
return (0);
}
static int
-findch(int ch, int cnt, int forw, int incl)
+findch(int ch, int cnt, bool forw, bool incl)
{
int ncursor;
start--;
anchored = *pat == '^' ? (++pat, 1) : 0;
if ((hist = findhist(start, fwd, pat, anchored)) < 0) {
- /* (start != 0 && fwd && match(holdbuf, pat) >= 0) */
- if (start != 0 && fwd && strcmp(holdbuf, pat) >= 0) {
+ /* (start != 0 && fwd && match(holdbufp, pat) >= 0) */
+ if (start != 0 && fwd && strcmp(holdbufp, pat) >= 0) {
restore_cbuf();
return (0);
} else
x_putc('\r');
x_putc('\n');
}
- if (prompt_redraw)
+ if (prompt_trunc != -1)
pprompt(prompt, prompt_trunc);
- cur_col = pwidth;
+ x_col = pwidth;
morec = ' ';
}
twb2 = wb2;
while (cnt--) {
if (*twb1 != *twb2) {
- if (cur_col != col)
+ if (x_col != col)
ed_mov_opt(col, wb1);
x_putc(*twb1);
- cur_col++;
+ x_col++;
}
twb1++;
twb2++;
if (mc != morec) {
ed_mov_opt(pwidth + winwidth + 1, wb1);
x_putc(mc);
- cur_col++;
+ x_col++;
morec = mc;
}
- if (cur_col != ncol)
+ if (x_col != ncol)
ed_mov_opt(ncol, wb1);
}
static void
ed_mov_opt(int col, char *wb)
{
- if (col < cur_col) {
- if (col + 1 < cur_col - col) {
+ if (col < x_col) {
+ if (col + 1 < x_col - col) {
x_putc('\r');
- if (prompt_redraw)
+ if (prompt_trunc != -1)
pprompt(prompt, prompt_trunc);
- cur_col = pwidth;
- while (cur_col++ < col)
+ x_col = pwidth;
+ while (x_col++ < col)
x_putcf(*wb++);
} else {
- while (cur_col-- > col)
+ while (x_col-- > col)
x_putc('\b');
}
} else {
- wb = &wb[cur_col - pwidth];
- while (cur_col++ < col)
+ wb = &wb[x_col - pwidth];
+ while (x_col++ < col)
x_putcf(*wb++);
}
- cur_col = col;
+ x_col = col;
}
rval = -1;
break;
}
- if (++i < nwords && putbuf(" ", 1, 0) != 0) {
+ if (++i < nwords && putbuf(" ", 1, false) != 0) {
rval = -1;
break;
}
*/
if (match_len > 0 && match[match_len - 1] != '/' &&
!(flags & XCF_IS_NOSPACE))
- rval = putbuf(" ", 1, 0);
+ rval = putbuf(" ", 1, false);
}
x_free_words(nwords, words);
vi_macro_reset(void)
{
if (macro.p) {
- afree(macro.buf, APERM);
+ afree(macro.buf, AEDIT);
memset((char *)¯o, 0, sizeof(macro));
}
}
/* ^W */
edchars.werase = 027;
- /* initialise Emacs command line editing mode */
+ /* command line editing specific memory allocation */
ainit(AEDIT);
+ holdbufp = alloc(LINE, AEDIT);
+
+ /* initialise Emacs command line editing mode */
x_nextcmd = -1;
x_tab = alloc2(X_NTABS, sizeof(*x_tab), AEDIT);