summaryrefslogtreecommitdiff
path: root/cmd-line-utils/libedit/search.c
diff options
context:
space:
mode:
Diffstat (limited to 'cmd-line-utils/libedit/search.c')
-rw-r--r--cmd-line-utils/libedit/search.c160
1 files changed, 84 insertions, 76 deletions
diff --git a/cmd-line-utils/libedit/search.c b/cmd-line-utils/libedit/search.c
index df50c7e7370..2324cc94d76 100644
--- a/cmd-line-utils/libedit/search.c
+++ b/cmd-line-utils/libedit/search.c
@@ -1,4 +1,4 @@
-/* $NetBSD: search.c,v 1.20 2004/11/04 01:16:03 christos Exp $ */
+/* $NetBSD: search.c,v 1.30 2011/10/04 15:27:04 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -65,15 +65,16 @@ protected int
search_init(EditLine *el)
{
- el->el_search.patbuf = (char *) el_malloc(EL_BUFSIZ);
+ el->el_search.patbuf = el_malloc(EL_BUFSIZ *
+ sizeof(*el->el_search.patbuf));
if (el->el_search.patbuf == NULL)
- return (-1);
+ return -1;
el->el_search.patlen = 0;
el->el_search.patdir = -1;
el->el_search.chacha = '\0';
el->el_search.chadir = CHAR_FWD;
el->el_search.chatflg = 0;
- return (0);
+ return 0;
}
@@ -84,7 +85,7 @@ protected void
search_end(EditLine *el)
{
- el_free((ptr_t) el->el_search.patbuf);
+ el_free(el->el_search.patbuf);
el->el_search.patbuf = NULL;
}
@@ -105,8 +106,11 @@ regerror(const char *msg)
* Return if string matches pattern
*/
protected int
-el_match(const char *str, const char *pat)
+el_match(const Char *str, const Char *pat)
{
+#ifdef WIDECHAR
+ static ct_buffer_t conv;
+#endif
#if defined (REGEX)
regex_t re;
int rv;
@@ -118,30 +122,31 @@ el_match(const char *str, const char *pat)
extern int re_exec(const char *);
#endif
- if (strstr(str, pat) != NULL)
- return (1);
+ if (Strstr(str, pat) != 0)
+ return 1;
#if defined(REGEX)
- if (regcomp(&re, pat, 0) == 0) {
- rv = regexec(&re, str, 0, NULL, 0) == 0;
+ if (regcomp(&re, ct_encode_string(pat, &conv), 0) == 0) {
+ rv = regexec(&re, ct_encode_string(str, &conv), (size_t)0, NULL,
+ 0) == 0;
regfree(&re);
} else {
rv = 0;
}
- return (rv);
+ return rv;
#elif defined(REGEXP)
- if ((re = regcomp(pat)) != NULL) {
- rv = regexec(re, str);
- free((ptr_t) re);
+ if ((re = regcomp(ct_encode_string(pat, &conv))) != NULL) {
+ rv = regexec(re, ct_encode_string(str, &conv));
+ el_free(re);
} else {
rv = 0;
}
- return (rv);
+ return rv;
#else
- if (re_comp(pat) != NULL)
- return (0);
+ if (re_comp(ct_encode_string(pat, &conv)) != NULL)
+ return 0;
else
- return (re_exec(str) == 1);
+ return re_exec(ct_encode_string(str, &conv) == 1);
#endif
}
@@ -150,14 +155,14 @@ el_match(const char *str, const char *pat)
* return True if the pattern matches the prefix
*/
protected int
-c_hmatch(EditLine *el, const char *str)
+c_hmatch(EditLine *el, const Char *str)
{
#ifdef SDEBUG
(void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
el->el_search.patbuf, str);
#endif /* SDEBUG */
- return (el_match(str, el->el_search.patbuf));
+ return el_match(str, el->el_search.patbuf);
}
@@ -169,15 +174,16 @@ c_setpat(EditLine *el)
{
if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
- el->el_search.patlen = EL_CURSOR(el) - el->el_line.buffer;
+ el->el_search.patlen =
+ (size_t)(EL_CURSOR(el) - el->el_line.buffer);
if (el->el_search.patlen >= EL_BUFSIZ)
el->el_search.patlen = EL_BUFSIZ - 1;
if (el->el_search.patlen != 0) {
- (void) strncpy(el->el_search.patbuf, el->el_line.buffer,
+ (void) Strncpy(el->el_search.patbuf, el->el_line.buffer,
el->el_search.patlen);
el->el_search.patbuf[el->el_search.patlen] = '\0';
} else
- el->el_search.patlen = strlen(el->el_search.patbuf);
+ el->el_search.patlen = Strlen(el->el_search.patbuf);
}
#ifdef SDEBUG
(void) fprintf(el->el_errfile, "\neventno = %d\n",
@@ -198,23 +204,24 @@ c_setpat(EditLine *el)
protected el_action_t
ce_inc_search(EditLine *el, int dir)
{
- static const char STRfwd[] = {'f', 'w', 'd', '\0'},
+ static const Char STRfwd[] = {'f', 'w', 'd', '\0'},
STRbck[] = {'b', 'c', 'k', '\0'};
- static char pchar = ':';/* ':' = normal, '?' = failed */
- static char endcmd[2] = {'\0', '\0'};
- char ch, *ocursor = el->el_line.cursor, oldpchar = pchar;
- const char *cp;
+ static Char pchar = ':';/* ':' = normal, '?' = failed */
+ static Char endcmd[2] = {'\0', '\0'};
+ Char ch, *ocursor = el->el_line.cursor, oldpchar = pchar;
+ const Char *cp;
el_action_t ret = CC_NORM;
int ohisteventno = el->el_history.eventno;
- int oldpatlen = el->el_search.patlen;
+ size_t oldpatlen = el->el_search.patlen;
int newdir = dir;
int done, redo;
- if (el->el_line.lastchar + sizeof(STRfwd) / sizeof(char) + 2 +
+ if (el->el_line.lastchar + sizeof(STRfwd) /
+ sizeof(*el->el_line.lastchar) + 2 +
el->el_search.patlen >= el->el_line.limit)
- return (CC_ERROR);
+ return CC_ERROR;
for (;;) {
@@ -241,14 +248,14 @@ ce_inc_search(EditLine *el, int dir)
*el->el_line.lastchar = '\0';
re_refresh(el);
- if (el_getc(el, &ch) != 1)
- return (ed_end_of_file(el, 0));
+ if (FUN(el,getc)(el, &ch) != 1)
+ return ed_end_of_file(el, 0);
switch (el->el_map.current[(unsigned char) ch]) {
case ED_INSERT:
case ED_DIGIT:
if (el->el_search.patlen >= EL_BUFSIZ - LEN)
- term_beep(el);
+ terminal_beep(el);
else {
el->el_search.patbuf[el->el_search.patlen++] =
ch;
@@ -273,7 +280,7 @@ ce_inc_search(EditLine *el, int dir)
if (el->el_search.patlen > LEN)
done++;
else
- term_beep(el);
+ terminal_beep(el);
break;
default:
@@ -297,7 +304,7 @@ ce_inc_search(EditLine *el, int dir)
*el->el_line.cursor != '\n') {
if (el->el_search.patlen >=
EL_BUFSIZ - LEN) {
- term_beep(el);
+ terminal_beep(el);
break;
}
el->el_search.patbuf[el->el_search.patlen++] =
@@ -310,14 +317,14 @@ ce_inc_search(EditLine *el, int dir)
re_refresh(el);
break;
} else if (isglob(*cp)) {
- term_beep(el);
+ terminal_beep(el);
break;
}
break;
default: /* Terminate and execute cmd */
endcmd[0] = ch;
- el_push(el, endcmd);
+ FUN(el,push)(el, endcmd);
/* FALLTHROUGH */
case 0033: /* ESC: Terminate */
@@ -379,9 +386,10 @@ ce_inc_search(EditLine *el, int dir)
/* avoid c_setpat */
el->el_state.lastcmd =
(el_action_t) newdir;
- ret = newdir == ED_SEARCH_PREV_HISTORY ?
+ ret = (el_action_t)
+ (newdir == ED_SEARCH_PREV_HISTORY ?
ed_search_prev_history(el, 0) :
- ed_search_next_history(el, 0);
+ ed_search_next_history(el, 0));
if (ret != CC_ERROR) {
el->el_line.cursor = newdir ==
ED_SEARCH_PREV_HISTORY ?
@@ -395,13 +403,13 @@ ce_inc_search(EditLine *el, int dir)
el->el_search.patbuf[el->el_search.patlen] =
'\0';
if (ret == CC_ERROR) {
- term_beep(el);
+ terminal_beep(el);
if (el->el_history.eventno !=
ohisteventno) {
el->el_history.eventno =
ohisteventno;
if (hist_get(el) == CC_ERROR)
- return (CC_ERROR);
+ return CC_ERROR;
}
el->el_line.cursor = ocursor;
pchar = '?';
@@ -426,14 +434,14 @@ ce_inc_search(EditLine *el, int dir)
if (el->el_history.eventno != ohisteventno) {
el->el_history.eventno = ohisteventno;
if (hist_get(el) == CC_ERROR)
- return (CC_ERROR);
+ return CC_ERROR;
}
el->el_line.cursor = ocursor;
if (ret == CC_ERROR)
re_refresh(el);
}
if (done || ret != CC_NORM)
- return (ret);
+ return ret;
}
}
@@ -444,9 +452,9 @@ ce_inc_search(EditLine *el, int dir)
protected el_action_t
cv_search(EditLine *el, int dir)
{
- char ch;
- char tmpbuf[EL_BUFSIZ];
- int tmplen;
+ Char ch;
+ Char tmpbuf[EL_BUFSIZ];
+ ssize_t tmplen;
#ifdef ANCHOR
tmpbuf[0] = '.';
@@ -457,7 +465,7 @@ cv_search(EditLine *el, int dir)
el->el_search.patdir = dir;
tmplen = c_gets(el, &tmpbuf[LEN],
- dir == ED_SEARCH_PREV_HISTORY ? "\n/" : "\n?" );
+ dir == ED_SEARCH_PREV_HISTORY ? STR("\n/") : STR("\n?") );
if (tmplen == -1)
return CC_REFRESH;
@@ -471,16 +479,16 @@ cv_search(EditLine *el, int dir)
*/
if (el->el_search.patlen == 0) {
re_refresh(el);
- return (CC_ERROR);
+ return CC_ERROR;
}
#ifdef ANCHOR
if (el->el_search.patbuf[0] != '.' &&
el->el_search.patbuf[0] != '*') {
- (void) strncpy(tmpbuf, el->el_search.patbuf,
- sizeof(tmpbuf) - 1);
+ (void) Strncpy(tmpbuf, el->el_search.patbuf,
+ sizeof(tmpbuf) / sizeof(*tmpbuf) - 1);
el->el_search.patbuf[0] = '.';
el->el_search.patbuf[1] = '*';
- (void) strncpy(&el->el_search.patbuf[2], tmpbuf,
+ (void) Strncpy(&el->el_search.patbuf[2], tmpbuf,
EL_BUFSIZ - 3);
el->el_search.patlen++;
el->el_search.patbuf[el->el_search.patlen++] = '.';
@@ -494,21 +502,21 @@ cv_search(EditLine *el, int dir)
tmpbuf[tmplen++] = '*';
#endif
tmpbuf[tmplen] = '\0';
- (void) strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
- el->el_search.patlen = tmplen;
+ (void) Strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
+ el->el_search.patlen = (size_t)tmplen;
}
el->el_state.lastcmd = (el_action_t) dir; /* avoid c_setpat */
el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
ed_search_next_history(el, 0)) == CC_ERROR) {
re_refresh(el);
- return (CC_ERROR);
+ return CC_ERROR;
}
if (ch == 0033) {
re_refresh(el);
return ed_newline(el, 0);
}
- return (CC_REFRESH);
+ return CC_REFRESH;
}
@@ -518,9 +526,9 @@ cv_search(EditLine *el, int dir)
protected el_action_t
ce_search_line(EditLine *el, int dir)
{
- char *cp = el->el_line.cursor;
- char *pattern = el->el_search.patbuf;
- char oc, *ocp;
+ Char *cp = el->el_line.cursor;
+ Char *pattern = el->el_search.patbuf;
+ Char oc, *ocp;
#ifdef ANCHOR
ocp = &pattern[1];
oc = *ocp;
@@ -535,21 +543,21 @@ ce_search_line(EditLine *el, int dir)
if (el_match(cp, ocp)) {
*ocp = oc;
el->el_line.cursor = cp;
- return (CC_NORM);
+ return CC_NORM;
}
}
*ocp = oc;
- return (CC_ERROR);
+ return CC_ERROR;
} else {
for (; *cp != '\0' && cp < el->el_line.limit; cp++) {
if (el_match(cp, ocp)) {
*ocp = oc;
el->el_line.cursor = cp;
- return (CC_NORM);
+ return CC_NORM;
}
}
*ocp = oc;
- return (CC_ERROR);
+ return CC_ERROR;
}
}
@@ -558,12 +566,12 @@ ce_search_line(EditLine *el, int dir)
* Vi repeat search
*/
protected el_action_t
-cv_repeat_srch(EditLine *el, int c)
+cv_repeat_srch(EditLine *el, Int c)
{
#ifdef SDEBUG
(void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
- c, el->el_search.patlen, el->el_search.patbuf);
+ c, el->el_search.patlen, ct_encode_string(el->el_search.patbuf));
#endif
el->el_state.lastcmd = (el_action_t) c; /* Hack to stop c_setpat */
@@ -571,11 +579,11 @@ cv_repeat_srch(EditLine *el, int c)
switch (c) {
case ED_SEARCH_NEXT_HISTORY:
- return (ed_search_next_history(el, 0));
+ return ed_search_next_history(el, 0);
case ED_SEARCH_PREV_HISTORY:
- return (ed_search_prev_history(el, 0));
+ return ed_search_prev_history(el, 0);
default:
- return (CC_ERROR);
+ return CC_ERROR;
}
}
@@ -584,16 +592,16 @@ cv_repeat_srch(EditLine *el, int c)
* Vi character search
*/
protected el_action_t
-cv_csearch(EditLine *el, int direction, int ch, int count, int tflag)
+cv_csearch(EditLine *el, int direction, Int ch, int count, int tflag)
{
- char *cp;
+ Char *cp;
if (ch == 0)
return CC_ERROR;
- if (ch == -1) {
- char c;
- if (el_getc(el, &c) != 1)
+ if (ch == (Int)-1) {
+ Char c;
+ if (FUN(el,getc)(el, &c) != 1)
return ed_end_of_file(el, 0);
ch = c;
}
@@ -601,18 +609,18 @@ cv_csearch(EditLine *el, int direction, int ch, int count, int tflag)
/* Save for ';' and ',' commands */
el->el_search.chacha = ch;
el->el_search.chadir = direction;
- el->el_search.chatflg = tflag;
+ el->el_search.chatflg = (char)tflag;
cp = el->el_line.cursor;
while (count--) {
- if (*cp == ch)
+ if ((Int)*cp == ch)
cp += direction;
for (;;cp += direction) {
if (cp >= el->el_line.lastchar)
return CC_ERROR;
if (cp < el->el_line.buffer)
return CC_ERROR;
- if (*cp == ch)
+ if ((Int)*cp == ch)
break;
}
}