summaryrefslogtreecommitdiff
path: root/lib/libedit/chared.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2010-06-30 00:05:36 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2010-06-30 00:05:36 +0000
commit4e317cd7d599f8bfe9b7906ba2f75a82e29a5f7a (patch)
tree7381b8c061e5ffffa2117ee119ec36681dde870f /lib/libedit/chared.c
parenta6429617a1d028ff60e599bcc583a0b56c844d85 (diff)
Update libedit to bring it into sync with the latest version from NetBSD.
ok deraadt
Diffstat (limited to 'lib/libedit/chared.c')
-rw-r--r--lib/libedit/chared.c232
1 files changed, 141 insertions, 91 deletions
diff --git a/lib/libedit/chared.c b/lib/libedit/chared.c
index 5a2ffa6b701..90c480d5b7e 100644
--- a/lib/libedit/chared.c
+++ b/lib/libedit/chared.c
@@ -1,5 +1,5 @@
-/* $OpenBSD: chared.c,v 1.9 2009/10/27 23:59:28 deraadt Exp $ */
-/* $NetBSD: chared.c,v 1.21 2003/11/02 20:08:41 christos Exp $ */
+/* $OpenBSD: chared.c,v 1.10 2010/06/30 00:05:35 nicm Exp $ */
+/* $NetBSD: chared.c,v 1.28 2009/12/30 22:37:40 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -41,6 +41,8 @@
#include <stdlib.h>
#include "el.h"
+private void ch__clearmacro (EditLine *);
+
/* value to leave unused in line buffer */
#define EL_LEAVE 2
@@ -52,13 +54,13 @@ cv_undo(EditLine *el)
{
c_undo_t *vu = &el->el_chared.c_undo;
c_redo_t *r = &el->el_chared.c_redo;
- uint size;
+ size_t size;
/* Save entire line for undo */
size = el->el_line.lastchar - el->el_line.buffer;
vu->len = size;
- vu->cursor = el->el_line.cursor - el->el_line.buffer;
- memcpy(vu->buf, el->el_line.buffer, size);
+ vu->cursor = (int)(el->el_line.cursor - el->el_line.buffer);
+ (void)memcpy(vu->buf, el->el_line.buffer, size * sizeof(*vu->buf));
/* save command info for redo */
r->count = el->el_state.doingarg ? el->el_state.argument : 0;
@@ -72,11 +74,11 @@ cv_undo(EditLine *el)
* Save yank/delete data for paste
*/
protected void
-cv_yank(EditLine *el, const char *ptr, int size)
+cv_yank(EditLine *el, const Char *ptr, int size)
{
c_kill_t *k = &el->el_chared.c_kill;
- memcpy(k->buf, ptr, size +0u);
+ (void)memcpy(k->buf, ptr, size * sizeof(*k->buf));
k->last = k->buf + size;
}
@@ -87,10 +89,10 @@ cv_yank(EditLine *el, const char *ptr, int size)
protected void
c_insert(EditLine *el, int num)
{
- char *cp;
+ Char *cp;
if (el->el_line.lastchar + num >= el->el_line.limit) {
- if (!ch_enlargebufs(el, num +0u))
+ if (!ch_enlargebufs(el, (size_t)num))
return; /* can't go past end of buffer */
}
@@ -111,7 +113,7 @@ c_delafter(EditLine *el, int num)
{
if (el->el_line.cursor + num > el->el_line.lastchar)
- num = el->el_line.lastchar - el->el_line.cursor;
+ num = (int)(el->el_line.lastchar - el->el_line.cursor);
if (el->el_map.current != el->el_map.emacs) {
cv_undo(el);
@@ -119,7 +121,7 @@ c_delafter(EditLine *el, int num)
}
if (num > 0) {
- char *cp;
+ Char *cp;
for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
*cp = cp[num];
@@ -129,6 +131,21 @@ c_delafter(EditLine *el, int num)
}
+/* c_delafter1():
+ * Delete the character after the cursor, do not yank
+ */
+protected void
+c_delafter1(EditLine *el)
+{
+ Char *cp;
+
+ for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
+ *cp = cp[1];
+
+ el->el_line.lastchar--;
+}
+
+
/* c_delbefore():
* Delete num characters before the cursor
*/
@@ -137,7 +154,7 @@ c_delbefore(EditLine *el, int num)
{
if (el->el_line.cursor - num < el->el_line.buffer)
- num = el->el_line.cursor - el->el_line.buffer;
+ num = (int)(el->el_line.cursor - el->el_line.buffer);
if (el->el_map.current != el->el_map.emacs) {
cv_undo(el);
@@ -145,7 +162,7 @@ c_delbefore(EditLine *el, int num)
}
if (num > 0) {
- char *cp;
+ Char *cp;
for (cp = el->el_line.cursor - num;
cp <= el->el_line.lastchar;
@@ -157,13 +174,28 @@ c_delbefore(EditLine *el, int num)
}
+/* c_delbefore1():
+ * Delete the character before the cursor, do not yank
+ */
+protected void
+c_delbefore1(EditLine *el)
+{
+ Char *cp;
+
+ for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
+ *cp = cp[1];
+
+ el->el_line.lastchar--;
+}
+
+
/* ce__isword():
* Return if p is part of a word according to emacs
*/
protected int
-ce__isword(int p)
+ce__isword(Int p)
{
- return (isalnum(p) || strchr("*?_-.[]~=", p) != NULL);
+ return (Isalnum(p) || Strchr(STR("*?_-.[]~="), p) != NULL);
}
@@ -171,11 +203,11 @@ ce__isword(int p)
* Return if p is part of a word according to vi
*/
protected int
-cv__isword(int p)
+cv__isword(Int p)
{
- if (isalnum(p) || p == '_')
+ if (Isalnum(p) || p == '_')
return 1;
- if (isgraph(p))
+ if (Isgraph(p))
return 2;
return 0;
}
@@ -185,24 +217,24 @@ cv__isword(int p)
* Return if p is part of a big word according to vi
*/
protected int
-cv__isWord(int p)
+cv__isWord(Int p)
{
- return (!isspace(p));
+ return (!Isspace(p));
}
/* c__prev_word():
* Find the previous word
*/
-protected char *
-c__prev_word(char *p, char *low, int n, int (*wtest)(int))
+protected Char *
+c__prev_word(Char *p, Char *low, int n, int (*wtest)(Int))
{
p--;
while (n--) {
- while ((p >= low) && !(*wtest)((unsigned char) *p))
+ while ((p >= low) && !(*wtest)(*p))
p--;
- while ((p >= low) && (*wtest)((unsigned char) *p))
+ while ((p >= low) && (*wtest)(*p))
p--;
}
@@ -218,13 +250,13 @@ c__prev_word(char *p, char *low, int n, int (*wtest)(int))
/* c__next_word():
* Find the next word
*/
-protected char *
-c__next_word(char *p, char *high, int n, int (*wtest)(int))
+protected Char *
+c__next_word(Char *p, Char *high, int n, int (*wtest)(Int))
{
while (n--) {
- while ((p < high) && !(*wtest)((unsigned char) *p))
+ while ((p < high) && !(*wtest)(*p))
p++;
- while ((p < high) && (*wtest)((unsigned char) *p))
+ while ((p < high) && (*wtest)(*p))
p++;
}
if (p > high)
@@ -236,21 +268,21 @@ c__next_word(char *p, char *high, int n, int (*wtest)(int))
/* cv_next_word():
* Find the next word vi style
*/
-protected char *
-cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
+protected Char *
+cv_next_word(EditLine *el, Char *p, Char *high, int n, int (*wtest)(Int))
{
int test;
while (n--) {
- test = (*wtest)((unsigned char) *p);
- while ((p < high) && (*wtest)((unsigned char) *p) == test)
+ test = (*wtest)(*p);
+ while ((p < high) && (*wtest)(*p) == test)
p++;
/*
* vi historically deletes with cw only the word preserving the
* trailing whitespace! This is not what 'w' does..
*/
if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
- while ((p < high) && isspace((unsigned char) *p))
+ while ((p < high) && Isspace(*p))
p++;
}
@@ -265,17 +297,17 @@ cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
/* cv_prev_word():
* Find the previous word vi style
*/
-protected char *
-cv_prev_word(char *p, char *low, int n, int (*wtest)(int))
+protected Char *
+cv_prev_word(Char *p, Char *low, int n, int (*wtest)(Int))
{
int test;
p--;
while (n--) {
- while ((p > low) && isspace((unsigned char) *p))
+ while ((p > low) && Isspace(*p))
p--;
- test = (*wtest)((unsigned char) *p);
- while ((p >= low) && (*wtest)((unsigned char) *p) == test)
+ test = (*wtest)(*p);
+ while ((p >= low) && (*wtest)(*p) == test)
p--;
}
p++;
@@ -294,9 +326,9 @@ cv_prev_word(char *p, char *low, int n, int (*wtest)(int))
* A '$' by itself means a big number; "$-" is for negative; '^' means 1.
* Return p pointing to last char used.
*/
-protected char *
+protected Char *
c__number(
- char *p, /* character position */
+ Char *p, /* character position */
int *num, /* Return value */
int dval) /* dval is the number to subtract from like $-3 */
{
@@ -315,7 +347,8 @@ c__number(
sign = -1; /* Handle $- */
++p;
}
- for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
+ /* XXX: this assumes ASCII compatible digits */
+ for (i = 0; Isdigit(*p); i = 10 * i + *p++ - '0')
continue;
*num = (sign < 0 ? dval - i : i);
return (--p);
@@ -338,7 +371,7 @@ cv_delfini(EditLine *el)
/* sanity */
return;
- size = el->el_line.cursor - el->el_chared.c_vcmd.pos;
+ size = (int)(el->el_line.cursor - el->el_chared.c_vcmd.pos);
if (size == 0)
size = 1;
el->el_line.cursor = el->el_chared.c_vcmd.pos;
@@ -364,15 +397,15 @@ cv_delfini(EditLine *el)
/* ce__endword():
* Go to the end of this word according to emacs
*/
-protected char *
-ce__endword(char *p, char *high, int n)
+protected Char *
+ce__endword(Char *p, Char *high, int n)
{
p++;
while (n--) {
- while ((p < high) && isspace((unsigned char) *p))
+ while ((p < high) && Isspace(*p))
p++;
- while ((p < high) && !isspace((unsigned char) *p))
+ while ((p < high) && !Isspace(*p))
p++;
}
@@ -385,19 +418,19 @@ ce__endword(char *p, char *high, int n)
/* cv__endword():
* Go to the end of this word according to vi
*/
-protected char *
-cv__endword(char *p, char *high, int n, int (*wtest)(int))
+protected Char *
+cv__endword(Char *p, Char *high, int n, int (*wtest)(Int))
{
int test;
p++;
while (n--) {
- while ((p < high) && isspace((unsigned char) *p))
+ while ((p < high) && Isspace(*p))
p++;
- test = (*wtest)((unsigned char) *p);
- while ((p < high) && (*wtest)((unsigned char) *p) == test)
+ test = (*wtest)(*p);
+ while ((p < high) && (*wtest)(*p) == test)
p++;
}
p--;
@@ -410,22 +443,29 @@ cv__endword(char *p, char *high, int n, int (*wtest)(int))
protected int
ch_init(EditLine *el)
{
- el->el_line.buffer = (char *) el_malloc(EL_BUFSIZ);
+ c_macro_t *ma = &el->el_chared.c_macro;
+
+ el->el_line.buffer = el_malloc(EL_BUFSIZ *
+ sizeof(*el->el_line.buffer));
if (el->el_line.buffer == NULL)
return (-1);
- (void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
+ (void) memset(el->el_line.buffer, 0, EL_BUFSIZ *
+ sizeof(*el->el_line.buffer));
el->el_line.cursor = el->el_line.buffer;
el->el_line.lastchar = el->el_line.buffer;
el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
- el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ);
+ el->el_chared.c_undo.buf = el_malloc(EL_BUFSIZ *
+ sizeof(*el->el_chared.c_undo.buf));
if (el->el_chared.c_undo.buf == NULL)
return (-1);
- (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
+ (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ *
+ sizeof(*el->el_chared.c_undo.buf));
el->el_chared.c_undo.len = -1;
el->el_chared.c_undo.cursor = 0;
- el->el_chared.c_redo.buf = (char *) el_malloc(EL_BUFSIZ);
+ el->el_chared.c_redo.buf = el_malloc(EL_BUFSIZ *
+ sizeof(*el->el_chared.c_redo.buf));
if (el->el_chared.c_redo.buf == NULL)
return (-1);
el->el_chared.c_redo.pos = el->el_chared.c_redo.buf;
@@ -435,10 +475,12 @@ ch_init(EditLine *el)
el->el_chared.c_vcmd.action = NOP;
el->el_chared.c_vcmd.pos = el->el_line.buffer;
- el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ);
+ el->el_chared.c_kill.buf = el_malloc(EL_BUFSIZ *
+ sizeof(*el->el_chared.c_kill.buf));
if (el->el_chared.c_kill.buf == NULL)
return (-1);
- (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
+ (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ *
+ sizeof(*el->el_chared.c_kill.buf));
el->el_chared.c_kill.mark = el->el_line.buffer;
el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
@@ -450,11 +492,10 @@ ch_init(EditLine *el)
el->el_state.argument = 1;
el->el_state.lastcmd = ED_UNASSIGNED;
- el->el_chared.c_macro.level = -1;
- el->el_chared.c_macro.offset = 0;
- el->el_chared.c_macro.macro = (char **) el_malloc(EL_MAXMACRO *
- sizeof(char *));
- if (el->el_chared.c_macro.macro == NULL)
+ ma->level = -1;
+ ma->offset = 0;
+ ma->macro = el_malloc(EL_MAXMACRO * sizeof(*ma->macro));
+ if (ma->macro == NULL)
return (-1);
return (0);
}
@@ -463,7 +504,7 @@ ch_init(EditLine *el)
* Reset the character editor
*/
protected void
-ch_reset(EditLine *el)
+ch_reset(EditLine *el, int mclear)
{
el->el_line.cursor = el->el_line.buffer;
el->el_line.lastchar = el->el_line.buffer;
@@ -484,9 +525,18 @@ ch_reset(EditLine *el)
el->el_state.argument = 1;
el->el_state.lastcmd = ED_UNASSIGNED;
- el->el_chared.c_macro.level = -1;
-
el->el_history.eventno = 0;
+
+ if (mclear)
+ ch__clearmacro(el);
+}
+
+private void
+ch__clearmacro(EditLine *el)
+{
+ c_macro_t *ma = &el->el_chared.c_macro;
+ while (ma->level >= 0)
+ el_free((ptr_t)ma->macro[ma->level--]);
}
/* ch_enlargebufs():
@@ -494,12 +544,10 @@ ch_reset(EditLine *el)
* Returns 1 if successful, 0 if not.
*/
protected int
-ch_enlargebufs(el, addlen)
- EditLine *el;
- size_t addlen;
+ch_enlargebufs(EditLine *el, size_t addlen)
{
size_t sz, newsz;
- char *newbuffer, *oldbuf, *oldkbuf;
+ Char *newbuffer, *oldbuf, *oldkbuf;
sz = el->el_line.limit - el->el_line.buffer + EL_LEAVE;
newsz = sz * 2;
@@ -515,12 +563,12 @@ ch_enlargebufs(el, addlen)
/*
* Reallocate line buffer.
*/
- newbuffer = el_realloc(el->el_line.buffer, newsz);
+ newbuffer = el_realloc(el->el_line.buffer, newsz * sizeof(*newbuffer));
if (!newbuffer)
return 0;
/* zero the newly added memory, leave old data in */
- (void) memset(&newbuffer[sz], 0, newsz - sz);
+ (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
oldbuf = el->el_line.buffer;
@@ -533,12 +581,12 @@ ch_enlargebufs(el, addlen)
/*
* Reallocate kill buffer.
*/
- newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz);
+ newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz * sizeof(*newbuffer));
if (!newbuffer)
return 0;
/* zero the newly added memory, leave old data in */
- (void) memset(&newbuffer[sz], 0, newsz - sz);
+ (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
oldkbuf = el->el_chared.c_kill.buf;
@@ -551,15 +599,17 @@ ch_enlargebufs(el, addlen)
/*
* Reallocate undo buffer.
*/
- newbuffer = el_realloc(el->el_chared.c_undo.buf, newsz);
+ newbuffer = el_realloc(el->el_chared.c_undo.buf,
+ newsz * sizeof(*newbuffer));
if (!newbuffer)
return 0;
/* zero the newly added memory, leave old data in */
- (void) memset(&newbuffer[sz], 0, newsz - sz);
+ (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
el->el_chared.c_undo.buf = newbuffer;
- newbuffer = el_realloc(el->el_chared.c_redo.buf, newsz);
+ newbuffer = el_realloc(el->el_chared.c_redo.buf,
+ newsz * sizeof(*newbuffer));
if (!newbuffer)
return 0;
el->el_chared.c_redo.pos = newbuffer +
@@ -594,9 +644,9 @@ ch_end(EditLine *el)
el->el_chared.c_redo.cmd = ED_UNASSIGNED;
el_free((ptr_t) el->el_chared.c_kill.buf);
el->el_chared.c_kill.buf = NULL;
+ ch_reset(el, 1);
el_free((ptr_t) el->el_chared.c_macro.macro);
el->el_chared.c_macro.macro = NULL;
- ch_reset(el);
}
@@ -604,11 +654,11 @@ ch_end(EditLine *el)
* Insert string at cursorI
*/
public int
-el_insertstr(EditLine *el, const char *s)
+FUN(el,insertstr)(EditLine *el, const Char *s)
{
size_t len;
- if ((len = strlen(s)) == 0)
+ if ((len = Strlen(s)) == 0)
return (-1);
if (el->el_line.lastchar + len >= el->el_line.limit) {
if (!ch_enlargebufs(el, len))
@@ -644,15 +694,15 @@ el_deletestr(EditLine *el, int n)
* Get a string
*/
protected int
-c_gets(EditLine *el, char *buf, const char *prompt)
+c_gets(EditLine *el, Char *buf, const Char *prompt)
{
- char ch;
- int len;
- char *cp = el->el_line.buffer;
+ Char ch;
+ ssize_t len;
+ Char *cp = el->el_line.buffer;
if (prompt) {
- len = strlen(prompt);
- memcpy(cp, prompt, len + 0u);
+ len = Strlen(prompt);
+ (void)memcpy(cp, prompt, len * sizeof(*cp));
cp += len;
}
len = 0;
@@ -663,7 +713,7 @@ c_gets(EditLine *el, char *buf, const char *prompt)
el->el_line.lastchar = cp + 1;
re_refresh(el);
- if (el_getc(el, &ch) != 1) {
+ if (FUN(el,getc)(el, &ch) != 1) {
ed_end_of_file(el, 0);
len = -1;
break;
@@ -673,7 +723,7 @@ c_gets(EditLine *el, char *buf, const char *prompt)
case 0010: /* Delete and backspace */
case 0177:
- if (len <= 0) {
+ if (len == 0) {
len = -1;
break;
}
@@ -701,7 +751,7 @@ c_gets(EditLine *el, char *buf, const char *prompt)
el->el_line.buffer[0] = '\0';
el->el_line.lastchar = el->el_line.buffer;
el->el_line.cursor = el->el_line.buffer;
- return len;
+ return (int)len;
}
@@ -711,7 +761,7 @@ c_gets(EditLine *el, char *buf, const char *prompt)
protected int
c_hpos(EditLine *el)
{
- char *ptr;
+ Char *ptr;
/*
* Find how many characters till the beginning of this line.
@@ -723,6 +773,6 @@ c_hpos(EditLine *el)
ptr >= el->el_line.buffer && *ptr != '\n';
ptr--)
continue;
- return (el->el_line.cursor - ptr - 1);
+ return (int)(el->el_line.cursor - ptr - 1);
}
}