Updated libedit to NetBSD upstream

Note: This unconditionally assumes wchar_t support.  May need revision
if some platforms prove problematic.
This commit is contained in:
Viktor Dukhovni
2016-11-13 15:49:16 +11:00
parent e1c1cdb1b6
commit 77ff7185d7
55 changed files with 6113 additions and 4536 deletions

View File

@@ -1,4 +1,4 @@
/* $NetBSD: chared.c,v 1.29 2010/08/28 15:44:59 christos Exp $ */
/* $NetBSD: chared.c,v 1.56 2016/05/22 19:44:26 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -37,17 +37,20 @@
#if 0
static char sccsid[] = "@(#)chared.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: chared.c,v 1.29 2010/08/28 15:44:59 christos Exp $");
__RCSID("$NetBSD: chared.c,v 1.56 2016/05/22 19:44:26 christos Exp $");
#endif
#endif /* not lint && not SCCSID */
/*
* chared.c: Character editor utilities
*/
#include <ctype.h>
#include <stdlib.h>
#include "el.h"
#include <string.h>
private void ch__clearmacro (EditLine *);
#include "el.h"
#include "common.h"
#include "fcns.h"
/* value to leave unused in line buffer */
#define EL_LEAVE 2
@@ -55,7 +58,7 @@ private void ch__clearmacro (EditLine *);
/* cv_undo():
* Handle state for the vi undo command
*/
protected void
libedit_private void
cv_undo(EditLine *el)
{
c_undo_t *vu = &el->el_chared.c_undo;
@@ -63,8 +66,8 @@ cv_undo(EditLine *el)
size_t size;
/* Save entire line for undo */
size = el->el_line.lastchar - el->el_line.buffer;
vu->len = size;
size = (size_t)(el->el_line.lastchar - el->el_line.buffer);
vu->len = (ssize_t)size;
vu->cursor = (int)(el->el_line.cursor - el->el_line.buffer);
(void)memcpy(vu->buf, el->el_line.buffer, size * sizeof(*vu->buf));
@@ -79,12 +82,12 @@ cv_undo(EditLine *el)
/* cv_yank():
* Save yank/delete data for paste
*/
protected void
cv_yank(EditLine *el, const Char *ptr, int size)
libedit_private void
cv_yank(EditLine *el, const wchar_t *ptr, int size)
{
c_kill_t *k = &el->el_chared.c_kill;
(void)memcpy(k->buf, ptr, size * sizeof(*k->buf));
(void)memcpy(k->buf, ptr, (size_t)size * sizeof(*k->buf));
k->last = k->buf + size;
}
@@ -92,10 +95,10 @@ cv_yank(EditLine *el, const Char *ptr, int size)
/* c_insert():
* Insert num characters
*/
protected void
libedit_private void
c_insert(EditLine *el, int num)
{
Char *cp;
wchar_t *cp;
if (el->el_line.lastchar + num >= el->el_line.limit) {
if (!ch_enlargebufs(el, (size_t)num))
@@ -114,7 +117,7 @@ c_insert(EditLine *el, int num)
/* c_delafter():
* Delete num characters after the cursor
*/
protected void
libedit_private void
c_delafter(EditLine *el, int num)
{
@@ -127,7 +130,7 @@ c_delafter(EditLine *el, int num)
}
if (num > 0) {
Char *cp;
wchar_t *cp;
for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
*cp = cp[num];
@@ -140,10 +143,10 @@ c_delafter(EditLine *el, int num)
/* c_delafter1():
* Delete the character after the cursor, do not yank
*/
protected void
libedit_private void
c_delafter1(EditLine *el)
{
Char *cp;
wchar_t *cp;
for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
*cp = cp[1];
@@ -155,7 +158,7 @@ c_delafter1(EditLine *el)
/* c_delbefore():
* Delete num characters before the cursor
*/
protected void
libedit_private void
c_delbefore(EditLine *el, int num)
{
@@ -168,7 +171,7 @@ c_delbefore(EditLine *el, int num)
}
if (num > 0) {
Char *cp;
wchar_t *cp;
for (cp = el->el_line.cursor - num;
cp <= el->el_line.lastchar;
@@ -183,10 +186,10 @@ c_delbefore(EditLine *el, int num)
/* c_delbefore1():
* Delete the character before the cursor, do not yank
*/
protected void
libedit_private void
c_delbefore1(EditLine *el)
{
Char *cp;
wchar_t *cp;
for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
*cp = cp[1];
@@ -198,22 +201,22 @@ c_delbefore1(EditLine *el)
/* ce__isword():
* Return if p is part of a word according to emacs
*/
protected int
ce__isword(Int p)
libedit_private int
ce__isword(wint_t p)
{
return (Isalnum(p) || Strchr(STR("*?_-.[]~="), p) != NULL);
return iswalnum(p) || wcschr(L"*?_-.[]~=", p) != NULL;
}
/* cv__isword():
* Return if p is part of a word according to vi
*/
protected int
cv__isword(Int p)
libedit_private int
cv__isword(wint_t p)
{
if (Isalnum(p) || p == '_')
if (iswalnum(p) || p == L'_')
return 1;
if (Isgraph(p))
if (iswgraph(p))
return 2;
return 0;
}
@@ -222,18 +225,18 @@ cv__isword(Int p)
/* cv__isWord():
* Return if p is part of a big word according to vi
*/
protected int
cv__isWord(Int p)
libedit_private int
cv__isWord(wint_t p)
{
return (!Isspace(p));
return !iswspace(p);
}
/* c__prev_word():
* Find the previous word
*/
protected Char *
c__prev_word(Char *p, Char *low, int n, int (*wtest)(Int))
libedit_private wchar_t *
c__prev_word(wchar_t *p, wchar_t *low, int n, int (*wtest)(wint_t))
{
p--;
@@ -249,15 +252,15 @@ c__prev_word(Char *p, Char *low, int n, int (*wtest)(Int))
if (p < low)
p = low;
/* cp now points where we want it */
return (p);
return p;
}
/* c__next_word():
* Find the next word
*/
protected Char *
c__next_word(Char *p, Char *high, int n, int (*wtest)(Int))
libedit_private wchar_t *
c__next_word(wchar_t *p, wchar_t *high, int n, int (*wtest)(wint_t))
{
while (n--) {
while ((p < high) && !(*wtest)(*p))
@@ -268,14 +271,15 @@ c__next_word(Char *p, Char *high, int n, int (*wtest)(Int))
if (p > high)
p = high;
/* p now points where we want it */
return (p);
return p;
}
/* 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))
libedit_private wchar_t *
cv_next_word(EditLine *el, wchar_t *p, wchar_t *high, int n,
int (*wtest)(wint_t))
{
int test;
@@ -288,29 +292,29 @@ cv_next_word(EditLine *el, Char *p, Char *high, int n, int (*wtest)(Int))
* trailing whitespace! This is not what 'w' does..
*/
if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
while ((p < high) && Isspace(*p))
while ((p < high) && iswspace(*p))
p++;
}
/* p now points where we want it */
if (p > high)
return (high);
return high;
else
return (p);
return p;
}
/* cv_prev_word():
* Find the previous word vi style
*/
protected Char *
cv_prev_word(Char *p, Char *low, int n, int (*wtest)(Int))
libedit_private wchar_t *
cv_prev_word(wchar_t *p, wchar_t *low, int n, int (*wtest)(wint_t))
{
int test;
p--;
while (n--) {
while ((p > low) && Isspace(*p))
while ((p > low) && iswspace(*p))
p--;
test = (*wtest)(*p);
while ((p >= low) && (*wtest)(*p) == test)
@@ -320,51 +324,16 @@ cv_prev_word(Char *p, Char *low, int n, int (*wtest)(Int))
/* p now points where we want it */
if (p < low)
return (low);
return low;
else
return (p);
return p;
}
#ifdef notdef
/* c__number():
* Ignore character p points to, return number appearing after that.
* A '$' by itself means a big number; "$-" is for negative; '^' means 1.
* Return p pointing to last char used.
*/
protected Char *
c__number(
Char *p, /* character position */
int *num, /* Return value */
int dval) /* dval is the number to subtract from like $-3 */
{
int i;
int sign = 1;
if (*++p == '^') {
*num = 1;
return (p);
}
if (*p == '$') {
if (*++p != '-') {
*num = 0x7fffffff; /* Handle $ */
return (--p);
}
sign = -1; /* Handle $- */
++p;
}
/* 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);
}
#endif
/* cv_delfini():
* Finish vi delete action
*/
protected void
libedit_private void
cv_delfini(EditLine *el)
{
int size;
@@ -399,40 +368,18 @@ cv_delfini(EditLine *el)
}
#ifdef notdef
/* ce__endword():
* Go to the end of this word according to emacs
*/
protected Char *
ce__endword(Char *p, Char *high, int n)
{
p++;
while (n--) {
while ((p < high) && Isspace(*p))
p++;
while ((p < high) && !Isspace(*p))
p++;
}
p--;
return (p);
}
#endif
/* 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))
libedit_private wchar_t *
cv__endword(wchar_t *p, wchar_t *high, int n, int (*wtest)(wint_t))
{
int test;
p++;
while (n--) {
while ((p < high) && Isspace(*p))
while ((p < high) && iswspace(*p))
p++;
test = (*wtest)(*p);
@@ -440,21 +387,19 @@ cv__endword(Char *p, Char *high, int n, int (*wtest)(Int))
p++;
}
p--;
return (p);
return p;
}
/* ch_init():
* Initialize the character editor
*/
protected int
libedit_private int
ch_init(EditLine *el)
{
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);
return -1;
(void) memset(el->el_line.buffer, 0, EL_BUFSIZ *
sizeof(*el->el_line.buffer));
@@ -465,7 +410,7 @@ ch_init(EditLine *el)
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);
return -1;
(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;
@@ -473,7 +418,7 @@ ch_init(EditLine *el)
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);
return -1;
el->el_chared.c_redo.pos = el->el_chared.c_redo.buf;
el->el_chared.c_redo.lim = el->el_chared.c_redo.buf + EL_BUFSIZ;
el->el_chared.c_redo.cmd = ED_UNASSIGNED;
@@ -484,13 +429,15 @@ ch_init(EditLine *el)
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);
return -1;
(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;
el->el_chared.c_resizefun = NULL;
el->el_chared.c_resizearg = NULL;
el->el_chared.c_aliasfun = NULL;
el->el_chared.c_aliasarg = NULL;
el->el_map.current = el->el_map.key;
@@ -500,19 +447,14 @@ ch_init(EditLine *el)
el->el_state.argument = 1;
el->el_state.lastcmd = ED_UNASSIGNED;
ma->level = -1;
ma->offset = 0;
ma->macro = el_malloc(EL_MAXMACRO * sizeof(*ma->macro));
if (ma->macro == NULL)
return (-1);
return (0);
return 0;
}
/* ch_reset():
* Reset the character editor
*/
protected void
ch_reset(EditLine *el, int mclear)
libedit_private void
ch_reset(EditLine *el)
{
el->el_line.cursor = el->el_line.buffer;
el->el_line.lastchar = el->el_line.buffer;
@@ -534,30 +476,19 @@ ch_reset(EditLine *el, int mclear)
el->el_state.lastcmd = ED_UNASSIGNED;
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():
* Enlarge line buffer to be able to hold twice as much characters.
* Returns 1 if successful, 0 if not.
*/
protected int
libedit_private int
ch_enlargebufs(EditLine *el, size_t addlen)
{
size_t sz, newsz;
Char *newbuffer, *oldbuf, *oldkbuf;
wchar_t *newbuffer, *oldbuf, *oldkbuf;
sz = el->el_line.limit - el->el_line.buffer + EL_LEAVE;
sz = (size_t)(el->el_line.limit - el->el_line.buffer + EL_LEAVE);
newsz = sz * 2;
/*
* If newly required length is longer than current buffer, we need
@@ -589,7 +520,8 @@ ch_enlargebufs(EditLine *el, size_t addlen)
/*
* Reallocate kill buffer.
*/
newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz * sizeof(*newbuffer));
newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz *
sizeof(*newbuffer));
if (!newbuffer)
return 0;
@@ -639,53 +571,51 @@ ch_enlargebufs(EditLine *el, size_t addlen)
/* ch_end():
* Free the data structures used by the editor
*/
protected void
libedit_private void
ch_end(EditLine *el)
{
el_free((ptr_t) el->el_line.buffer);
el_free(el->el_line.buffer);
el->el_line.buffer = NULL;
el->el_line.limit = NULL;
el_free((ptr_t) el->el_chared.c_undo.buf);
el_free(el->el_chared.c_undo.buf);
el->el_chared.c_undo.buf = NULL;
el_free((ptr_t) el->el_chared.c_redo.buf);
el_free(el->el_chared.c_redo.buf);
el->el_chared.c_redo.buf = NULL;
el->el_chared.c_redo.pos = NULL;
el->el_chared.c_redo.lim = NULL;
el->el_chared.c_redo.cmd = ED_UNASSIGNED;
el_free((ptr_t) el->el_chared.c_kill.buf);
el_free(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);
}
/* el_insertstr():
* Insert string at cursorI
*/
public int
FUN(el,insertstr)(EditLine *el, const Char *s)
int
el_winsertstr(EditLine *el, const wchar_t *s)
{
size_t len;
if ((len = Strlen(s)) == 0)
return (-1);
if (s == NULL || (len = wcslen(s)) == 0)
return -1;
if (el->el_line.lastchar + len >= el->el_line.limit) {
if (!ch_enlargebufs(el, len))
return (-1);
return -1;
}
c_insert(el, (int)len);
while (*s)
*el->el_line.cursor++ = *s++;
return (0);
return 0;
}
/* el_deletestr():
* Delete num characters before the cursor
*/
public void
void
el_deletestr(EditLine *el, int n)
{
if (n <= 0)
@@ -700,19 +630,37 @@ el_deletestr(EditLine *el, int n)
el->el_line.cursor = el->el_line.buffer;
}
/* el_cursor():
* Move the cursor to the left or the right of the current position
*/
int
el_cursor(EditLine *el, int n)
{
if (n == 0)
goto out;
el->el_line.cursor += n;
if (el->el_line.cursor < el->el_line.buffer)
el->el_line.cursor = el->el_line.buffer;
if (el->el_line.cursor > el->el_line.lastchar)
el->el_line.cursor = el->el_line.lastchar;
out:
return (int)(el->el_line.cursor - el->el_line.buffer);
}
/* c_gets():
* Get a string
*/
protected int
c_gets(EditLine *el, Char *buf, const Char *prompt)
libedit_private int
c_gets(EditLine *el, wchar_t *buf, const wchar_t *prompt)
{
Char ch;
ssize_t len;
Char *cp = el->el_line.buffer;
wchar_t *cp = el->el_line.buffer, ch;
if (prompt) {
len = Strlen(prompt);
(void)memcpy(cp, prompt, len * sizeof(*cp));
len = (ssize_t)wcslen(prompt);
(void)memcpy(cp, prompt, (size_t)len * sizeof(*cp));
cp += len;
}
len = 0;
@@ -723,7 +671,7 @@ c_gets(EditLine *el, Char *buf, const Char *prompt)
el->el_line.lastchar = cp + 1;
re_refresh(el);
if (FUN(el,getc)(el, &ch) != 1) {
if (el_wgetc(el, &ch) != 1) {
ed_end_of_file(el, 0);
len = -1;
break;
@@ -731,24 +679,25 @@ c_gets(EditLine *el, Char *buf, const Char *prompt)
switch (ch) {
case 0010: /* Delete and backspace */
case L'\b': /* Delete and backspace */
case 0177:
if (len == 0) {
len = -1;
break;
}
len--;
cp--;
continue;
case 0033: /* ESC */
case '\r': /* Newline */
case '\n':
case L'\r': /* Newline */
case L'\n':
buf[len] = ch;
break;
default:
if (len >= EL_BUFSIZ - 16)
term_beep(el);
if (len >= (ssize_t)(EL_BUFSIZ - 16))
terminal_beep(el);
else {
buf[len++] = ch;
*cp++ = ch;
@@ -768,16 +717,16 @@ c_gets(EditLine *el, Char *buf, const Char *prompt)
/* c_hpos():
* Return the current horizontal position of the cursor
*/
protected int
libedit_private int
c_hpos(EditLine *el)
{
Char *ptr;
wchar_t *ptr;
/*
* Find how many characters till the beginning of this line.
*/
if (el->el_line.cursor == el->el_line.buffer)
return (0);
return 0;
else {
for (ptr = el->el_line.cursor - 1;
ptr >= el->el_line.buffer && *ptr != '\n';
@@ -787,10 +736,18 @@ c_hpos(EditLine *el)
}
}
protected int
libedit_private int
ch_resizefun(EditLine *el, el_zfunc_t f, void *a)
{
el->el_chared.c_resizefun = f;
el->el_chared.c_resizearg = a;
return 0;
}
libedit_private int
ch_aliasfun(EditLine *el, el_afunc_t f, void *a)
{
el->el_chared.c_aliasfun = f;
el->el_chared.c_aliasarg = a;
return 0;
}