freebsd-skq/lib/libedit/search.c

632 lines
15 KiB
C
Raw Normal View History

1994-05-27 05:00:24 +00:00
/*-
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Christos Zoulas of Cornell University.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1994-05-27 05:00:24 +00:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
Merge changes from upstream libedit. Our libedit has been diverging from the mainstream version maintained in NetBSD. As a consequence it has been difficult to do an appropriate MFV and we have been bringing only partial updates. Here we update most of the files to at least match the version available in NetBSD's snapshot of 20091228. This version was chosen because it still doesn't include wide character support (UTF-8), which involves many changes and new files. From NetBSD's logs: Dec 15 22:13:33 2006 - editline.3 el.c el.h histedit.h add EL_GETFP, and EL_SETFP. Apr 5 15:53:28 2008 - editline.3 el.c histedit.h readline.c add EL_REFRESH for the benefit of readline Sep 10 15:45:37 2008 - common.c el.c read.c refresh.c sig.c term.c term.h tty.c Allow a single process to control multiple ttys (for pthreads using _REENTRANT) using multiple EditLine objects. Jan 18 12:17:24 2009 - el.c read.c readline.c fix -Wsign-compare issues Feb 6 14:40:32 2009 - history.c Plug memory leak, from MySQL. Feb 5 19:15:44 2009 - histedit.h read.c match documentation in el_push Feb 6 13:14:37 2009 - vi.c Portability fix. Feb 12 13:39:49 2009 - readline.c term.c More fixes for existing portability stuff. Feb 15 21:24:13 2009 - el.h read.c don't restart on EINTR, instead return NULL immediately. From Anon Ymous Feb 15 21:25:01 2009 - sig.c sig.h in order for read() to return EINTR we need to use sigaction, not signal, otherwise SA_RESTART is set. Feb 15 21:55:23 2009 - chared.c chared.h common.c emacs.c filecomplete.c filecomplete.h key.c key.h read.c readline.c refresh.c search.c term.c tokenizer.c tty.c vi.c pass lint on _LP64. Feb 17 21:34:26 2009 - el.c histedit.h prompt.c prompt.h allow for a prompt argument. Feb 18 15:04:40 2009 - sig.c SA_RESTART for all signals but SIGINT. From Anon Ymous. Feb 19 15:20:22 2009 - read.c sig.c sig.h reset and redraw on sigcont. From Anon Ymous. Feb 21 23:31:56 2009 - key.c key.h readline.c vi.c more size_t stuff. Mar 10 20:46:15 2009 - editline.3 read.c make el_gets set the count to -1 on error to distinguish between EOF and error. Mar 31 17:38:27 2009 - editline.3 el.c histedit.h prompt.c prompt.h refresh.c term.c term.h Implement literal prompt sequences. Now someone can implement RL_PROMPT_START_LITERAL/RL_PROMPT_END_LITERAL :-) Mar 31 21:33:17 2009 - term.c cast to size_t to avoid sign / unsigned comparison warning. Apr 23 02:03 2009 - term.c Apply patch (requested by msaitoh in ticket #2007): Coverity CID 1668: Plug memory leak when malloc() failed.:55 2009 May 11 18:33:30 2009 - editline.3 el.c histedit.h restore binary compatibility by providing new prompt functions that take an extra literal character. May 19 21:45:14 2009 - refresh.c always scroll when we advance past bottom. From Caleb Welton cwelton at greenplum dot com. Jul 17 12:27:57 2009 - term.c - off by one in the term.h case. - make code more similar to tcsh (if we want to handle wide chars, this is needed; for now it is a no-op) Jul 22 15:56:29 2009 - el.c Move filename to the scope it is being used. From Michael Cook mcook at bbn dot com Jul 22 15:57:00 2009 - read.c Always initialize nread since it is an out param. From Michael Cook mcook at bbn dot com Jul 22 18:25:26 2009 - el.c Only need path if we have issetugid... From Anon Ymous Jul 25 21:19:23 2009 - el.c Ignore comment lines in .editrc from Jess Thrysoee Sep 7 21:24:33 2009 histedit.h history.c readline.c apply apple patches from: http://opensource.apple.com/source/libedit/libedit-11/patches/ Dec 28 21:52:43 2009 - refresh.c Fix bug where tab completion on the second or > line that caused listing ended up corrupting the display by an extra space in the beginning. Reported by Mac Chan. Dec 28 22:15:36 2009 - refresh.c term.c reduce diff with tcsh Obtained from: NetBSD Tested by: bapt, jilles and current@ MFC after: 1 week
2012-06-22 18:01:22 +00:00
* $NetBSD: search.c,v 1.21 2009/02/15 21:55:23 christos Exp $
1994-05-27 05:00:24 +00:00
*/
#if !defined(lint) && !defined(SCCSID)
static char sccsid[] = "@(#)search.c 8.1 (Berkeley) 6/4/93";
#endif /* not lint && not SCCSID */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
1994-05-27 05:00:24 +00:00
/*
* search.c: History and character search functions
*/
#include "sys.h"
#include <stdlib.h>
#if defined(REGEX)
#include <regex.h>
#elif defined(REGEXP)
1994-05-27 05:00:24 +00:00
#include <regexp.h>
#endif
#include "el.h"
/*
* Adjust cursor in vi mode to include the character under it
*/
#define EL_CURSOR(el) \
1994-05-27 05:00:24 +00:00
((el)->el_line.cursor + (((el)->el_map.type == MAP_VI) && \
((el)->el_map.current == (el)->el_map.alt)))
/* search_init():
* Initialize the search stuff
*/
protected int
search_init(EditLine *el)
1994-05-27 05:00:24 +00:00
{
el->el_search.patbuf = (char *) el_malloc(EL_BUFSIZ);
if (el->el_search.patbuf == NULL)
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);
1994-05-27 05:00:24 +00:00
}
/* search_end():
* Initialize the search stuff
*/
protected void
search_end(EditLine *el)
1994-05-27 05:00:24 +00:00
{
el_free((ptr_t) el->el_search.patbuf);
el->el_search.patbuf = NULL;
1994-05-27 05:00:24 +00:00
}
1994-05-27 05:00:24 +00:00
#ifdef REGEXP
/* regerror():
* Handle regular expression errors
*/
1995-05-30 05:51:47 +00:00
public void
1994-05-27 05:00:24 +00:00
/*ARGSUSED*/
regerror(const char *msg)
1994-05-27 05:00:24 +00:00
{
}
#endif
1994-05-27 05:00:24 +00:00
/* el_match():
* Return if string matches pattern
*/
protected int
el_match(const char *str, const char *pat)
1994-05-27 05:00:24 +00:00
{
#if defined (REGEX)
regex_t re;
int rv;
#elif defined (REGEXP)
regexp *rp;
int rv;
#else
extern char *re_comp(const char *);
extern int re_exec(const char *);
1994-05-27 05:00:24 +00:00
#endif
if (strstr(str, pat) != NULL)
return (1);
#if defined(REGEX)
if (regcomp(&re, pat, 0) == 0) {
rv = regexec(&re, str, 0, NULL, 0) == 0;
regfree(&re);
} else {
rv = 0;
}
return (rv);
#elif defined(REGEXP)
if ((re = regcomp(pat)) != NULL) {
rv = regexec(re, str);
free((ptr_t) re);
} else {
rv = 0;
}
return (rv);
#else
if (re_comp(pat) != NULL)
return (0);
else
return (re_exec(str) == 1);
#endif
1994-05-27 05:00:24 +00:00
}
/* c_hmatch():
* return True if the pattern matches the prefix
*/
protected int
c_hmatch(EditLine *el, const char *str)
1994-05-27 05:00:24 +00:00
{
#ifdef SDEBUG
(void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
el->el_search.patbuf, str);
1994-05-27 05:00:24 +00:00
#endif /* SDEBUG */
1995-05-30 05:51:47 +00:00
return (el_match(str, el->el_search.patbuf));
1994-05-27 05:00:24 +00:00
}
1995-05-30 05:51:47 +00:00
/* c_setpat():
1994-05-27 05:00:24 +00:00
* Set the history seatch pattern
*/
protected void
c_setpat(EditLine *el)
1994-05-27 05:00:24 +00:00
{
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;
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,
el->el_search.patlen);
el->el_search.patbuf[el->el_search.patlen] = '\0';
} else
el->el_search.patlen = strlen(el->el_search.patbuf);
1994-05-27 05:00:24 +00:00
}
#ifdef SDEBUG
(void) fprintf(el->el_errfile, "\neventno = %d\n",
el->el_history.eventno);
(void) fprintf(el->el_errfile, "patlen = %d\n", el->el_search.patlen);
(void) fprintf(el->el_errfile, "patbuf = \"%s\"\n",
el->el_search.patbuf);
(void) fprintf(el->el_errfile, "cursor %d lastchar %d\n",
EL_CURSOR(el) - el->el_line.buffer,
el->el_line.lastchar - el->el_line.buffer);
1994-05-27 05:00:24 +00:00
#endif
}
/* ce_inc_search():
* Emacs incremental search
*/
protected el_action_t
ce_inc_search(EditLine *el, int dir)
1994-05-27 05:00:24 +00:00
{
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;
1994-05-27 05:00:24 +00:00
el_action_t ret = CC_NORM;
1994-05-27 05:00:24 +00:00
int ohisteventno = el->el_history.eventno;
Merge changes from upstream libedit. Our libedit has been diverging from the mainstream version maintained in NetBSD. As a consequence it has been difficult to do an appropriate MFV and we have been bringing only partial updates. Here we update most of the files to at least match the version available in NetBSD's snapshot of 20091228. This version was chosen because it still doesn't include wide character support (UTF-8), which involves many changes and new files. From NetBSD's logs: Dec 15 22:13:33 2006 - editline.3 el.c el.h histedit.h add EL_GETFP, and EL_SETFP. Apr 5 15:53:28 2008 - editline.3 el.c histedit.h readline.c add EL_REFRESH for the benefit of readline Sep 10 15:45:37 2008 - common.c el.c read.c refresh.c sig.c term.c term.h tty.c Allow a single process to control multiple ttys (for pthreads using _REENTRANT) using multiple EditLine objects. Jan 18 12:17:24 2009 - el.c read.c readline.c fix -Wsign-compare issues Feb 6 14:40:32 2009 - history.c Plug memory leak, from MySQL. Feb 5 19:15:44 2009 - histedit.h read.c match documentation in el_push Feb 6 13:14:37 2009 - vi.c Portability fix. Feb 12 13:39:49 2009 - readline.c term.c More fixes for existing portability stuff. Feb 15 21:24:13 2009 - el.h read.c don't restart on EINTR, instead return NULL immediately. From Anon Ymous Feb 15 21:25:01 2009 - sig.c sig.h in order for read() to return EINTR we need to use sigaction, not signal, otherwise SA_RESTART is set. Feb 15 21:55:23 2009 - chared.c chared.h common.c emacs.c filecomplete.c filecomplete.h key.c key.h read.c readline.c refresh.c search.c term.c tokenizer.c tty.c vi.c pass lint on _LP64. Feb 17 21:34:26 2009 - el.c histedit.h prompt.c prompt.h allow for a prompt argument. Feb 18 15:04:40 2009 - sig.c SA_RESTART for all signals but SIGINT. From Anon Ymous. Feb 19 15:20:22 2009 - read.c sig.c sig.h reset and redraw on sigcont. From Anon Ymous. Feb 21 23:31:56 2009 - key.c key.h readline.c vi.c more size_t stuff. Mar 10 20:46:15 2009 - editline.3 read.c make el_gets set the count to -1 on error to distinguish between EOF and error. Mar 31 17:38:27 2009 - editline.3 el.c histedit.h prompt.c prompt.h refresh.c term.c term.h Implement literal prompt sequences. Now someone can implement RL_PROMPT_START_LITERAL/RL_PROMPT_END_LITERAL :-) Mar 31 21:33:17 2009 - term.c cast to size_t to avoid sign / unsigned comparison warning. Apr 23 02:03 2009 - term.c Apply patch (requested by msaitoh in ticket #2007): Coverity CID 1668: Plug memory leak when malloc() failed.:55 2009 May 11 18:33:30 2009 - editline.3 el.c histedit.h restore binary compatibility by providing new prompt functions that take an extra literal character. May 19 21:45:14 2009 - refresh.c always scroll when we advance past bottom. From Caleb Welton cwelton at greenplum dot com. Jul 17 12:27:57 2009 - term.c - off by one in the term.h case. - make code more similar to tcsh (if we want to handle wide chars, this is needed; for now it is a no-op) Jul 22 15:56:29 2009 - el.c Move filename to the scope it is being used. From Michael Cook mcook at bbn dot com Jul 22 15:57:00 2009 - read.c Always initialize nread since it is an out param. From Michael Cook mcook at bbn dot com Jul 22 18:25:26 2009 - el.c Only need path if we have issetugid... From Anon Ymous Jul 25 21:19:23 2009 - el.c Ignore comment lines in .editrc from Jess Thrysoee Sep 7 21:24:33 2009 histedit.h history.c readline.c apply apple patches from: http://opensource.apple.com/source/libedit/libedit-11/patches/ Dec 28 21:52:43 2009 - refresh.c Fix bug where tab completion on the second or > line that caused listing ended up corrupting the display by an extra space in the beginning. Reported by Mac Chan. Dec 28 22:15:36 2009 - refresh.c term.c reduce diff with tcsh Obtained from: NetBSD Tested by: bapt, jilles and current@ MFC after: 1 week
2012-06-22 18:01:22 +00:00
size_t oldpatlen = el->el_search.patlen;
int newdir = dir;
int done, redo;
1994-05-27 05:00:24 +00:00
if (el->el_line.lastchar + sizeof(STRfwd) / sizeof(char) + 2 +
el->el_search.patlen >= el->el_line.limit)
return (CC_ERROR);
1994-05-27 05:00:24 +00:00
for (;;) {
1994-05-27 05:00:24 +00:00
if (el->el_search.patlen == 0) { /* first round */
pchar = ':';
1994-05-27 05:00:24 +00:00
#ifdef ANCHOR
#define LEN 2
el->el_search.patbuf[el->el_search.patlen++] = '.';
el->el_search.patbuf[el->el_search.patlen++] = '*';
#else
#define LEN 0
1994-05-27 05:00:24 +00:00
#endif
}
done = redo = 0;
*el->el_line.lastchar++ = '\n';
for (cp = (newdir == ED_SEARCH_PREV_HISTORY) ? STRbck : STRfwd;
*cp; *el->el_line.lastchar++ = *cp++)
continue;
*el->el_line.lastchar++ = pchar;
for (cp = &el->el_search.patbuf[LEN];
cp < &el->el_search.patbuf[el->el_search.patlen];
*el->el_line.lastchar++ = *cp++)
continue;
1994-05-27 05:00:24 +00:00
*el->el_line.lastchar = '\0';
re_refresh(el);
if (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)
1994-05-27 05:00:24 +00:00
term_beep(el);
else {
el->el_search.patbuf[el->el_search.patlen++] =
ch;
*el->el_line.lastchar++ = ch;
*el->el_line.lastchar = '\0';
re_refresh(el);
1994-05-27 05:00:24 +00:00
}
break;
case EM_INC_SEARCH_NEXT:
newdir = ED_SEARCH_NEXT_HISTORY;
redo++;
1994-05-27 05:00:24 +00:00
break;
case EM_INC_SEARCH_PREV:
newdir = ED_SEARCH_PREV_HISTORY;
redo++;
break;
1994-05-27 05:00:24 +00:00
case EM_DELETE_PREV_CHAR:
case ED_DELETE_PREV_CHAR:
if (el->el_search.patlen > LEN)
done++;
else
term_beep(el);
break;
1994-05-27 05:00:24 +00:00
default:
switch (ch) {
case 0007: /* ^G: Abort */
ret = CC_ERROR;
done++;
break;
1994-05-27 05:00:24 +00:00
case 0027: /* ^W: Append word */
/* No can do if globbing characters in pattern */
for (cp = &el->el_search.patbuf[LEN];; cp++)
if (cp >= &el->el_search.patbuf[
el->el_search.patlen]) {
el->el_line.cursor +=
el->el_search.patlen - LEN - 1;
cp = c__next_word(el->el_line.cursor,
el->el_line.lastchar, 1,
ce__isword);
while (el->el_line.cursor < cp &&
*el->el_line.cursor != '\n') {
if (el->el_search.patlen >=
EL_BUFSIZ - LEN) {
term_beep(el);
break;
}
el->el_search.patbuf[el->el_search.patlen++] =
*el->el_line.cursor;
*el->el_line.lastchar++ =
*el->el_line.cursor++;
}
el->el_line.cursor = ocursor;
*el->el_line.lastchar = '\0';
re_refresh(el);
break;
} else if (isglob(*cp)) {
term_beep(el);
break;
}
break;
default: /* Terminate and execute cmd */
endcmd[0] = ch;
el_push(el, endcmd);
/* FALLTHROUGH */
case 0033: /* ESC: Terminate */
ret = CC_REFRESH;
done++;
break;
}
break;
1994-05-27 05:00:24 +00:00
}
while (el->el_line.lastchar > el->el_line.buffer &&
*el->el_line.lastchar != '\n')
*el->el_line.lastchar-- = '\0';
*el->el_line.lastchar = '\0';
1994-05-27 05:00:24 +00:00
if (!done) {
/* Can't search if unmatched '[' */
for (cp = &el->el_search.patbuf[el->el_search.patlen-1],
ch = ']';
cp >= &el->el_search.patbuf[LEN];
cp--)
if (*cp == '[' || *cp == ']') {
ch = *cp;
break;
}
if (el->el_search.patlen > LEN && ch != '[') {
if (redo && newdir == dir) {
if (pchar == '?') { /* wrap around */
el->el_history.eventno =
newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
if (hist_get(el) == CC_ERROR)
/* el->el_history.event
* no was fixed by
* first call */
(void) hist_get(el);
el->el_line.cursor = newdir ==
ED_SEARCH_PREV_HISTORY ?
el->el_line.lastchar :
el->el_line.buffer;
} else
el->el_line.cursor +=
newdir ==
ED_SEARCH_PREV_HISTORY ?
-1 : 1;
}
#ifdef ANCHOR
el->el_search.patbuf[el->el_search.patlen++] =
'.';
el->el_search.patbuf[el->el_search.patlen++] =
'*';
#endif
el->el_search.patbuf[el->el_search.patlen] =
'\0';
if (el->el_line.cursor < el->el_line.buffer ||
el->el_line.cursor > el->el_line.lastchar ||
(ret = ce_search_line(el, newdir))
== CC_ERROR) {
/* avoid c_setpat */
el->el_state.lastcmd =
(el_action_t) newdir;
ret = newdir == ED_SEARCH_PREV_HISTORY ?
ed_search_prev_history(el, 0) :
ed_search_next_history(el, 0);
if (ret != CC_ERROR) {
el->el_line.cursor = newdir ==
ED_SEARCH_PREV_HISTORY ?
el->el_line.lastchar :
el->el_line.buffer;
(void) ce_search_line(el,
newdir);
}
}
el->el_search.patlen -= LEN;
el->el_search.patbuf[el->el_search.patlen] =
'\0';
if (ret == CC_ERROR) {
term_beep(el);
if (el->el_history.eventno !=
ohisteventno) {
el->el_history.eventno =
ohisteventno;
if (hist_get(el) == CC_ERROR)
return (CC_ERROR);
}
el->el_line.cursor = ocursor;
pchar = '?';
} else {
pchar = ':';
}
}
ret = ce_inc_search(el, newdir);
1994-05-27 05:00:24 +00:00
if (ret == CC_ERROR && pchar == '?' && oldpchar == ':')
/*
* break abort of failed search at last
* non-failed
*/
ret = CC_NORM;
1994-05-27 05:00:24 +00:00
}
if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
/* restore on normal return or error exit */
pchar = oldpchar;
el->el_search.patlen = oldpatlen;
if (el->el_history.eventno != ohisteventno) {
el->el_history.eventno = ohisteventno;
if (hist_get(el) == CC_ERROR)
return (CC_ERROR);
}
el->el_line.cursor = ocursor;
if (ret == CC_ERROR)
re_refresh(el);
}
if (done || ret != CC_NORM)
return (ret);
1994-05-27 05:00:24 +00:00
}
}
/* cv_search():
* Vi search.
*/
protected el_action_t
cv_search(EditLine *el, int dir)
1994-05-27 05:00:24 +00:00
{
char ch;
char tmpbuf[EL_BUFSIZ];
int tmplen;
1994-05-27 05:00:24 +00:00
#ifdef ANCHOR
tmpbuf[0] = '.';
tmpbuf[1] = '*';
1994-05-27 05:00:24 +00:00
#endif
tmplen = LEN;
1994-05-27 05:00:24 +00:00
el->el_search.patdir = dir;
1994-05-27 05:00:24 +00:00
tmplen = c_gets(el, &tmpbuf[LEN],
dir == ED_SEARCH_PREV_HISTORY ? "\n/" : "\n?" );
if (tmplen == -1)
return CC_REFRESH;
1994-05-27 05:00:24 +00:00
tmplen += LEN;
ch = tmpbuf[tmplen];
tmpbuf[tmplen] = '\0';
if (tmplen == LEN) {
/*
* Use the old pattern, but wild-card it.
*/
if (el->el_search.patlen == 0) {
re_refresh(el);
return (CC_ERROR);
}
1994-05-27 05:00:24 +00:00
#ifdef ANCHOR
if (el->el_search.patbuf[0] != '.' &&
el->el_search.patbuf[0] != '*') {
(void) strncpy(tmpbuf, el->el_search.patbuf,
sizeof(tmpbuf) - 1);
el->el_search.patbuf[0] = '.';
el->el_search.patbuf[1] = '*';
(void) strncpy(&el->el_search.patbuf[2], tmpbuf,
EL_BUFSIZ - 3);
el->el_search.patlen++;
el->el_search.patbuf[el->el_search.patlen++] = '.';
el->el_search.patbuf[el->el_search.patlen++] = '*';
el->el_search.patbuf[el->el_search.patlen] = '\0';
}
1994-05-27 05:00:24 +00:00
#endif
} else {
1994-05-27 05:00:24 +00:00
#ifdef ANCHOR
tmpbuf[tmplen++] = '.';
tmpbuf[tmplen++] = '*';
1994-05-27 05:00:24 +00:00
#endif
tmpbuf[tmplen] = '\0';
(void) strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
el->el_search.patlen = 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);
1994-05-27 05:00:24 +00:00
}
if (ch == 0033) {
re_refresh(el);
return ed_newline(el, 0);
}
return (CC_REFRESH);
1994-05-27 05:00:24 +00:00
}
/* ce_search_line():
* Look for a pattern inside a line
*/
protected el_action_t
ce_search_line(EditLine *el, int dir)
1994-05-27 05:00:24 +00:00
{
char *cp = el->el_line.cursor;
char *pattern = el->el_search.patbuf;
char oc, *ocp;
#ifdef ANCHOR
ocp = &pattern[1];
oc = *ocp;
*ocp = '^';
#else
ocp = pattern;
oc = *ocp;
#endif
if (dir == ED_SEARCH_PREV_HISTORY) {
for (; cp >= el->el_line.buffer; cp--) {
if (el_match(cp, ocp)) {
*ocp = oc;
el->el_line.cursor = cp;
return (CC_NORM);
}
}
*ocp = oc;
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);
}
}
*ocp = oc;
return (CC_ERROR);
}
1994-05-27 05:00:24 +00:00
}
/* cv_repeat_srch():
* Vi repeat search
*/
protected el_action_t
cv_repeat_srch(EditLine *el, int c)
1994-05-27 05:00:24 +00:00
{
1994-05-27 05:00:24 +00:00
#ifdef SDEBUG
(void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
c, el->el_search.patlen, el->el_search.patbuf);
1994-05-27 05:00:24 +00:00
#endif
el->el_state.lastcmd = (el_action_t) c; /* Hack to stop c_setpat */
el->el_line.lastchar = el->el_line.buffer;
switch (c) {
case ED_SEARCH_NEXT_HISTORY:
return (ed_search_next_history(el, 0));
case ED_SEARCH_PREV_HISTORY:
return (ed_search_prev_history(el, 0));
default:
return (CC_ERROR);
}
1994-05-27 05:00:24 +00:00
}
/* cv_csearch():
* Vi character search
1994-05-27 05:00:24 +00:00
*/
protected el_action_t
cv_csearch(EditLine *el, int direction, int ch, int count, int tflag)
1994-05-27 05:00:24 +00:00
{
char *cp;
if (ch == 0)
return CC_ERROR;
1994-05-27 05:00:24 +00:00
if (ch == -1) {
char c;
if (el_getc(el, &c) != 1)
return ed_end_of_file(el, 0);
ch = c;
}
1994-05-27 05:00:24 +00:00
/* Save for ';' and ',' commands */
el->el_search.chacha = ch;
el->el_search.chadir = direction;
el->el_search.chatflg = tflag;
cp = el->el_line.cursor;
while (count--) {
if (*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)
break;
}
}
if (tflag)
cp -= direction;
el->el_line.cursor = cp;
if (el->el_chared.c_vcmd.action != NOP) {
if (direction > 0)
el->el_line.cursor++;
cv_delfini(el);
return CC_REFRESH;
}
return CC_CURSOR;
1994-05-27 05:00:24 +00:00
}