#include "charset.h"
#include "less.h"
#include "pattern.h"
#include "position.h"
#define MINPOS(a, b) (((a) < (b)) ? (a) : (b))
#define MAXPOS(a, b) (((a) > (b)) ? (a) : (b))
extern int how_search;
extern int caseless;
extern int linenums;
extern int sc_height;
extern int jump_sline;
extern int bs_mode;
extern int ctldisp;
extern int status_col;
extern void *const ml_search;
extern off_t start_attnpos;
extern off_t end_attnpos;
extern int utf_mode;
extern int screen_trashed;
extern int hilite_search;
extern int size_linebuf;
extern int squished;
extern int can_goto_line;
static int hide_hilite;
static off_t prep_startpos;
static off_t prep_endpos;
static int is_caseless;
static int is_ucase_pattern;
struct hilite {
struct hilite *hl_next;
off_t hl_startpos;
off_t hl_endpos;
};
static struct hilite hilite_anchor = { NULL, -1, -1 };
static struct hilite filter_anchor = { NULL, -1, -1 };
#define hl_first hl_next
struct pattern_info {
regex_t *compiled;
char *text;
int search_type;
};
#define info_compiled(info) ((info)->compiled)
static struct pattern_info search_info;
static struct pattern_info filter_info;
static int
is_ucase(char *str)
{
wchar_t ch;
int len;
for (; *str != '\0'; str += len) {
if ((len = mbtowc(&ch, str, MB_CUR_MAX)) == -1) {
mbtowc(NULL, NULL, MB_CUR_MAX);
len = 1;
} else if (iswupper(ch))
return (1);
}
return (0);
}
static int
set_pattern(struct pattern_info *info, char *pattern, int search_type)
{
if (pattern == NULL)
info->compiled = NULL;
else if (compile_pattern(pattern, search_type, &info->compiled) < 0)
return (-1);
free(info->text);
info->text = NULL;
if (pattern != NULL)
info->text = estrdup(pattern);
info->search_type = search_type;
is_ucase_pattern = is_ucase(pattern);
if (is_ucase_pattern && caseless != OPT_ONPLUS)
is_caseless = 0;
else
is_caseless = caseless;
return (0);
}
static void
clear_pattern(struct pattern_info *info)
{
free(info->text);
info->text = NULL;
uncompile_pattern(&info->compiled);
}
static void
init_pattern(struct pattern_info *info)
{
info->compiled = NULL;
info->text = NULL;
info->search_type = 0;
}
void
init_search(void)
{
init_pattern(&search_info);
init_pattern(&filter_info);
}
static int
get_cvt_ops(void)
{
int ops = 0;
if (is_caseless || bs_mode == BS_SPECIAL) {
if (is_caseless)
ops |= CVT_TO_LC;
if (bs_mode == BS_SPECIAL)
ops |= CVT_BS;
if (bs_mode != BS_CONTROL)
ops |= CVT_CRLF;
} else if (bs_mode != BS_CONTROL) {
ops |= CVT_CRLF;
}
if (ctldisp == OPT_ONPLUS)
ops |= CVT_ANSI;
return (ops);
}
static int
prev_pattern(struct pattern_info *info)
{
if ((info->search_type & SRCH_NO_REGEX) == 0)
return (info->compiled != NULL);
return (info->text != NULL);
}
void
repaint_hilite(int on)
{
int slinenum;
off_t pos;
int save_hide_hilite;
if (squished)
repaint();
save_hide_hilite = hide_hilite;
if (!on) {
if (hide_hilite)
return;
hide_hilite = 1;
}
if (!can_goto_line) {
repaint();
hide_hilite = save_hide_hilite;
return;
}
for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++) {
pos = position(slinenum);
if (pos == -1)
continue;
(void) forw_line(pos);
goto_line(slinenum);
put_line();
}
lower_left();
hide_hilite = save_hide_hilite;
}
void
clear_attn(void)
{
int slinenum;
off_t old_start_attnpos;
off_t old_end_attnpos;
off_t pos;
off_t epos;
int moved = 0;
if (start_attnpos == -1)
return;
old_start_attnpos = start_attnpos;
old_end_attnpos = end_attnpos;
start_attnpos = end_attnpos = -1;
if (!can_goto_line) {
repaint();
return;
}
if (squished)
repaint();
for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++) {
pos = position(slinenum);
if (pos == -1)
continue;
epos = position(slinenum+1);
if (pos < old_end_attnpos &&
(epos == -1 || epos > old_start_attnpos)) {
(void) forw_line(pos);
goto_line(slinenum);
put_line();
moved = 1;
}
}
if (moved)
lower_left();
}
void
undo_search(void)
{
if (!prev_pattern(&search_info)) {
error("No previous regular expression", NULL);
return;
}
hide_hilite = !hide_hilite;
repaint_hilite(1);
}
static void
clr_hlist(struct hilite *anchor)
{
struct hilite *hl;
struct hilite *nexthl;
for (hl = anchor->hl_first; hl != NULL; hl = nexthl) {
nexthl = hl->hl_next;
free(hl);
}
anchor->hl_first = NULL;
prep_startpos = prep_endpos = -1;
}
void
clr_hilite(void)
{
clr_hlist(&hilite_anchor);
}
static void
clr_filter(void)
{
clr_hlist(&filter_anchor);
}
static int
is_hilited_range(off_t pos, off_t epos)
{
struct hilite *hl;
for (hl = hilite_anchor.hl_first; hl != NULL; hl = hl->hl_next) {
if (hl->hl_endpos > pos &&
(epos == -1 || epos > hl->hl_startpos))
return (1);
}
return (0);
}
int
is_filtered(off_t pos)
{
struct hilite *hl;
if (ch_getflags() & CH_HELPFILE)
return (0);
for (hl = filter_anchor.hl_first; hl != NULL; hl = hl->hl_next) {
if (hl->hl_startpos == pos)
return (1);
}
return (0);
}
int
is_hilited(off_t pos, off_t epos, int nohide, int *p_matches)
{
int match;
if (p_matches != NULL)
*p_matches = 0;
if (!status_col &&
start_attnpos != -1 &&
pos < end_attnpos &&
(epos == -1 || epos > start_attnpos))
return (1);
match = is_hilited_range(pos, epos);
if (!match)
return (0);
if (p_matches != NULL)
*p_matches = 1;
if (hilite_search == 0)
return (0);
if (!nohide && hide_hilite)
return (0);
return (1);
}
static void
add_hilite(struct hilite *anchor, struct hilite *hl)
{
struct hilite *ihl;
for (ihl = anchor; ihl->hl_next != NULL; ihl = ihl->hl_next)
{
if (ihl->hl_next->hl_startpos > hl->hl_startpos)
break;
}
if (ihl != anchor)
hl->hl_startpos = MAXPOS(hl->hl_startpos, ihl->hl_endpos);
if (ihl->hl_next != NULL)
hl->hl_endpos = MINPOS(hl->hl_endpos,
ihl->hl_next->hl_startpos);
if (hl->hl_startpos >= hl->hl_endpos) {
free(hl);
return;
}
hl->hl_next = ihl->hl_next;
ihl->hl_next = hl;
}
static void
create_hilites(off_t linepos, int start_index, int end_index, int *chpos)
{
struct hilite *hl;
int i;
hl = ecalloc(1, sizeof (struct hilite));
hl->hl_startpos = linepos + chpos[start_index];
for (i = start_index+1; i <= end_index; i++) {
if (chpos[i] != chpos[i-1] + 1 || i == end_index) {
hl->hl_endpos = linepos + chpos[i-1] + 1;
add_hilite(&hilite_anchor, hl);
if (i < end_index) {
hl = ecalloc(1, sizeof (struct hilite));
hl->hl_startpos = linepos + chpos[i];
}
}
}
}
static void
hilite_line(off_t linepos, char *line, int line_len, int *chpos,
char *sp, char *ep)
{
char *searchp;
char *line_end = line + line_len;
searchp = line;
do {
if (sp == NULL || ep == NULL)
return;
create_hilites(linepos, (intptr_t)sp - (intptr_t)line,
(intptr_t)ep - (intptr_t)line, chpos);
if (ep > searchp)
searchp = ep;
else if (searchp != line_end)
searchp++;
else
break;
} while (match_pattern(info_compiled(&search_info), search_info.text,
searchp, (intptr_t)line_end - (intptr_t)searchp, &sp, &ep, 1,
search_info.search_type));
}
void
chg_caseless(void)
{
if (!is_ucase_pattern)
is_caseless = caseless;
else
clear_pattern(&search_info);
}
static void
hilite_screen(void)
{
struct scrpos scrpos;
get_scrpos(&scrpos);
if (scrpos.pos == -1)
return;
prep_hilite(scrpos.pos, position(BOTTOM_PLUS_ONE), -1);
repaint_hilite(1);
}
void
chg_hilite(void)
{
clr_hilite();
hide_hilite = 0;
if (hilite_search == OPT_ONPLUS)
hilite_screen();
}
static off_t
search_pos(int search_type)
{
off_t pos;
int linenum;
if (empty_screen()) {
if (search_type & SRCH_FORW) {
pos = ch_zero();
} else {
pos = ch_length();
if (pos == -1) {
(void) ch_end_seek();
pos = ch_length();
}
}
linenum = 0;
} else {
int add_one = 0;
if (how_search == OPT_ON) {
if (search_type & SRCH_FORW)
linenum = BOTTOM_PLUS_ONE;
else
linenum = TOP;
} else if (how_search == OPT_ONPLUS &&
!(search_type & SRCH_AFTER_TARGET)) {
if (search_type & SRCH_FORW)
linenum = TOP;
else
linenum = BOTTOM_PLUS_ONE;
} else {
linenum = jump_sline;
if (search_type & SRCH_FORW)
add_one = 1;
}
linenum = adjsline(linenum);
pos = position(linenum);
if (add_one)
pos = forw_raw_line(pos, NULL, NULL);
}
if (search_type & SRCH_FORW) {
while (pos == -1) {
if (++linenum >= sc_height)
break;
pos = position(linenum);
}
} else {
while (pos == -1) {
if (--linenum < 0)
break;
pos = position(linenum);
}
}
return (pos);
}
static int
search_range(off_t pos, off_t endpos, int search_type, int matches,
int maxlines, off_t *plinepos, off_t *pendpos)
{
char *line;
char *cline;
int line_len;
off_t linenum;
char *sp, *ep;
int line_match;
int cvt_ops;
int cvt_len;
int *chpos;
off_t linepos, oldpos;
linenum = find_linenum(pos);
oldpos = pos;
for (;;) {
if (abort_sigs()) {
return (-1);
}
if ((endpos != -1 && pos >= endpos) ||
maxlines == 0) {
if (pendpos != NULL)
*pendpos = pos;
return (matches);
}
if (maxlines > 0)
maxlines--;
if (search_type & SRCH_FORW) {
linepos = pos;
pos = forw_raw_line(pos, &line, &line_len);
if (linenum != 0)
linenum++;
} else {
pos = back_raw_line(pos, &line, &line_len);
linepos = pos;
if (linenum != 0)
linenum--;
}
if (pos == -1) {
if (pendpos != NULL)
*pendpos = oldpos;
return (matches);
}
if (linenums && abs((int)(pos - oldpos)) > 2048)
add_lnum(linenum, pos);
oldpos = pos;
if (is_filtered(linepos))
continue;
cvt_ops = get_cvt_ops();
cvt_len = cvt_length(line_len);
cline = ecalloc(1, cvt_len);
chpos = cvt_alloc_chpos(cvt_len);
cvt_text(cline, line, chpos, &line_len, cvt_ops);
if ((search_type & SRCH_FIND_ALL) &&
prev_pattern(&filter_info)) {
int line_filter =
match_pattern(info_compiled(&filter_info),
filter_info.text, cline, line_len, &sp, &ep, 0,
filter_info.search_type);
if (line_filter) {
struct hilite *hl =
ecalloc(1, sizeof (struct hilite));
hl->hl_startpos = linepos;
hl->hl_endpos = pos;
add_hilite(&filter_anchor, hl);
}
}
if (prev_pattern(&search_info)) {
line_match = match_pattern(info_compiled(&search_info),
search_info.text, cline, line_len, &sp, &ep, 0,
search_type);
if (line_match) {
if (search_type & SRCH_FIND_ALL) {
hilite_line(linepos, cline, line_len,
chpos, sp, ep);
} else if (--matches <= 0) {
if (hilite_search == OPT_ON) {
clr_hilite();
hilite_line(linepos, cline,
line_len, chpos, sp, ep);
}
free(cline);
free(chpos);
if (plinepos != NULL)
*plinepos = linepos;
return (0);
}
}
}
free(cline);
free(chpos);
}
}
static int
hist_pattern(int search_type)
{
char *pattern;
set_mlist(ml_search, 0);
pattern = cmd_lastpattern();
if (pattern == NULL)
return (0);
if (set_pattern(&search_info, pattern, search_type) < 0)
return (0);
if (hilite_search == OPT_ONPLUS && !hide_hilite)
hilite_screen();
return (1);
}
int
search(int search_type, char *pattern, int n)
{
off_t pos;
if (pattern == NULL || *pattern == '\0') {
search_type |= SRCH_AFTER_TARGET;
if (!prev_pattern(&search_info) && !hist_pattern(search_type)) {
error("No previous regular expression", NULL);
return (-1);
}
if ((search_type & SRCH_NO_REGEX) !=
(search_info.search_type & SRCH_NO_REGEX)) {
error("Please re-enter search pattern", NULL);
return (-1);
}
if (hilite_search == OPT_ON) {
repaint_hilite(0);
}
if (hilite_search == OPT_ONPLUS && hide_hilite) {
hide_hilite = 0;
hilite_screen();
}
hide_hilite = 0;
} else {
if (set_pattern(&search_info, pattern, search_type) < 0)
return (-1);
if (hilite_search) {
repaint_hilite(0);
hide_hilite = 0;
clr_hilite();
}
if (hilite_search == OPT_ONPLUS) {
hilite_screen();
}
}
pos = search_pos(search_type);
if (pos == -1) {
if (search_type & SRCH_PAST_EOF)
return (n);
error("Nothing to search", NULL);
return (-1);
}
n = search_range(pos, -1, search_type, n, -1, &pos, NULL);
if (n != 0) {
if (hilite_search == OPT_ON && n > 0)
repaint_hilite(1);
return (n);
}
if (!(search_type & SRCH_NO_MOVE)) {
jump_loc(pos, jump_sline);
}
if (hilite_search == OPT_ON)
repaint_hilite(1);
return (0);
}
void
prep_hilite(off_t spos, off_t epos, int maxlines)
{
off_t nprep_startpos = prep_startpos;
off_t nprep_endpos = prep_endpos;
off_t new_epos;
off_t max_epos;
int result;
int i;
#define SEARCH_MORE (3*size_linebuf)
if (!prev_pattern(&search_info) && !is_filtering())
return;
if (maxlines < 0) {
max_epos = -1;
} else {
max_epos = spos;
for (i = 0; i < maxlines; i++)
max_epos = forw_raw_line(max_epos, NULL, NULL);
}
if (prep_startpos == -1 ||
(epos != -1 && epos < prep_startpos) ||
spos > prep_endpos) {
clr_hilite();
clr_filter();
if (epos != -1)
epos += SEARCH_MORE;
nprep_startpos = spos;
} else {
if (epos != -1) {
if (epos > prep_endpos) {
epos += SEARCH_MORE;
} else {
epos = prep_startpos;
}
}
if (spos < prep_startpos) {
if (spos < SEARCH_MORE)
spos = 0;
else
spos -= SEARCH_MORE;
nprep_startpos = spos;
} else {
spos = prep_endpos;
}
}
if (epos != -1 && max_epos != -1 &&
epos > max_epos)
epos = max_epos;
if (epos == -1 || epos > spos) {
int search_type = SRCH_FORW | SRCH_FIND_ALL;
search_type |= (search_info.search_type & SRCH_NO_REGEX);
result = search_range(spos, epos, search_type, 0,
maxlines, NULL, &new_epos);
if (result < 0)
return;
if (prep_endpos == -1 || new_epos > prep_endpos)
nprep_endpos = new_epos;
}
prep_startpos = nprep_startpos;
prep_endpos = nprep_endpos;
}
void
set_filter_pattern(char *pattern, int search_type)
{
clr_filter();
if (pattern == NULL || *pattern == '\0')
clear_pattern(&filter_info);
else
(void) set_pattern(&filter_info, pattern, search_type);
screen_trashed = 1;
}
int
is_filtering(void)
{
if (ch_getflags() & CH_HELPFILE)
return (0);
return (prev_pattern(&filter_info));
}