xref: /netbsd-src/external/bsd/less/dist/search.c (revision 838f5788460f0f133b15d706e644d692a9d4d6ec)
1*838f5788Ssimonb /*	$NetBSD: search.c,v 1.5 2023/10/06 05:49:49 simonb Exp $	*/
220006a0bStron 
320006a0bStron /*
4*838f5788Ssimonb  * Copyright (C) 1984-2023  Mark Nudelman
520006a0bStron  *
620006a0bStron  * You may distribute under the terms of either the GNU General Public
720006a0bStron  * License or the Less License, as specified in the README file.
820006a0bStron  *
9ec18bca0Stron  * For more information, see the README file.
1020006a0bStron  */
1120006a0bStron 
1220006a0bStron 
1320006a0bStron /*
1420006a0bStron  * Routines to search a file for a pattern.
1520006a0bStron  */
1620006a0bStron 
1720006a0bStron #include "less.h"
1820006a0bStron #include "position.h"
1920006a0bStron #include "charset.h"
2020006a0bStron 
2120006a0bStron #define MINPOS(a,b)     (((a) < (b)) ? (a) : (b))
2220006a0bStron #define MAXPOS(a,b)     (((a) > (b)) ? (a) : (b))
2320006a0bStron 
2420006a0bStron extern int sigs;
2520006a0bStron extern int how_search;
2620006a0bStron extern int caseless;
2720006a0bStron extern int linenums;
2820006a0bStron extern int sc_height;
2920006a0bStron extern int jump_sline;
3020006a0bStron extern int bs_mode;
31*838f5788Ssimonb extern int proc_backspace;
32*838f5788Ssimonb extern int proc_return;
3320006a0bStron extern int ctldisp;
3420006a0bStron extern int status_col;
35*838f5788Ssimonb extern void *ml_search;
3620006a0bStron extern POSITION start_attnpos;
3720006a0bStron extern POSITION end_attnpos;
3820006a0bStron extern int utf_mode;
3920006a0bStron extern int screen_trashed;
40*838f5788Ssimonb extern int sc_width;
41*838f5788Ssimonb extern int sc_height;
42*838f5788Ssimonb extern int hshift;
43*838f5788Ssimonb extern int nosearch_headers;
44*838f5788Ssimonb extern int header_lines;
45*838f5788Ssimonb extern int header_cols;
4620006a0bStron #if HILITE_SEARCH
4720006a0bStron extern int hilite_search;
4820006a0bStron extern int size_linebuf;
4920006a0bStron extern int squished;
5020006a0bStron extern int can_goto_line;
5120006a0bStron static int hide_hilite;
5220006a0bStron static POSITION prep_startpos;
5320006a0bStron static POSITION prep_endpos;
54*838f5788Ssimonb extern POSITION xxpos;
5520006a0bStron 
56*838f5788Ssimonb /*
57*838f5788Ssimonb  * Structures for maintaining a set of ranges for hilites and filtered-out
58*838f5788Ssimonb  * lines. Each range is stored as a node within a red-black tree, and we
59*838f5788Ssimonb  * try to extend existing ranges (without creating overlaps) rather than
60*838f5788Ssimonb  * create new nodes if possible. We remember the last node found by a
61*838f5788Ssimonb  * search for constant-time lookup if the next search is near enough to
62*838f5788Ssimonb  * the previous. To aid that, we overlay a secondary doubly-linked list
63*838f5788Ssimonb  * on top of the red-black tree so we can find the preceding/succeeding
64*838f5788Ssimonb  * nodes also in constant time.
65*838f5788Ssimonb  *
66*838f5788Ssimonb  * Each node is allocated from a series of pools, each pool double the size
67*838f5788Ssimonb  * of the previous (for amortised constant time allocation). Since our only
68*838f5788Ssimonb  * tree operations are clear and node insertion, not node removal, we don't
69*838f5788Ssimonb  * need to maintain a usage bitmap or freelist and can just return nodes
70*838f5788Ssimonb  * from the pool in-order until capacity is reached.
71*838f5788Ssimonb  */
7220006a0bStron struct hilite
7320006a0bStron {
7420006a0bStron 	POSITION hl_startpos;
7520006a0bStron 	POSITION hl_endpos;
76*838f5788Ssimonb 	int hl_attr;
7720006a0bStron };
78*838f5788Ssimonb struct hilite_node
79*838f5788Ssimonb {
80*838f5788Ssimonb 	struct hilite_node *parent;
81*838f5788Ssimonb 	struct hilite_node *left;
82*838f5788Ssimonb 	struct hilite_node *right;
83*838f5788Ssimonb 	struct hilite_node *prev;
84*838f5788Ssimonb 	struct hilite_node *next;
85*838f5788Ssimonb 	int red;
86*838f5788Ssimonb 	struct hilite r;
87*838f5788Ssimonb };
88*838f5788Ssimonb struct hilite_storage
89*838f5788Ssimonb {
90*838f5788Ssimonb 	int capacity;
91*838f5788Ssimonb 	int used;
92*838f5788Ssimonb 	struct hilite_storage *next;
93*838f5788Ssimonb 	struct hilite_node *nodes;
94*838f5788Ssimonb };
95*838f5788Ssimonb struct hilite_tree
96*838f5788Ssimonb {
97*838f5788Ssimonb 	struct hilite_storage *first;
98*838f5788Ssimonb 	struct hilite_storage *current;
99*838f5788Ssimonb 	struct hilite_node *root;
100*838f5788Ssimonb 	struct hilite_node *lookaside;
101*838f5788Ssimonb };
102*838f5788Ssimonb #define HILITE_INITIALIZER() { NULL, NULL, NULL, NULL }
103*838f5788Ssimonb #define HILITE_LOOKASIDE_STEPS 2
104*838f5788Ssimonb 
105*838f5788Ssimonb static struct hilite_tree hilite_anchor = HILITE_INITIALIZER();
106*838f5788Ssimonb static struct hilite_tree filter_anchor = HILITE_INITIALIZER();
107*838f5788Ssimonb static struct pattern_info *filter_infos = NULL;
108*838f5788Ssimonb 
10920006a0bStron #endif
11020006a0bStron 
11120006a0bStron /*
11220006a0bStron  * These are the static variables that represent the "remembered"
11320006a0bStron  * search pattern and filter pattern.
11420006a0bStron  */
11520006a0bStron struct pattern_info {
116*838f5788Ssimonb 	PATTERN_TYPE compiled;
11720006a0bStron 	char* text;
11820006a0bStron 	int search_type;
119*838f5788Ssimonb 	int is_ucase_pattern;
120*838f5788Ssimonb 	struct pattern_info *next;
12120006a0bStron };
12220006a0bStron 
123ec18bca0Stron #if NO_REGEX
124ec18bca0Stron #define info_compiled(info) ((void*)0)
125ec18bca0Stron #else
126ec18bca0Stron #define info_compiled(info) ((info)->compiled)
127ec18bca0Stron #endif
128ec18bca0Stron 
12920006a0bStron static struct pattern_info search_info;
130*838f5788Ssimonb public int is_caseless;
13120006a0bStron 
13220006a0bStron /*
13320006a0bStron  * Are there any uppercase letters in this string?
13420006a0bStron  */
is_ucase(char * str)135*838f5788Ssimonb static int is_ucase(char *str)
13620006a0bStron {
13720006a0bStron 	char *str_end = str + strlen(str);
13820006a0bStron 	LWCHAR ch;
13920006a0bStron 
14020006a0bStron 	while (str < str_end)
14120006a0bStron 	{
14220006a0bStron 		ch = step_char(&str, +1, str_end);
14320006a0bStron 		if (IS_UPPER(ch))
14420006a0bStron 			return (1);
14520006a0bStron 	}
14620006a0bStron 	return (0);
14720006a0bStron }
14820006a0bStron 
14920006a0bStron /*
150*838f5788Ssimonb  * Discard a saved pattern.
151*838f5788Ssimonb  */
clear_pattern(struct pattern_info * info)152*838f5788Ssimonb static void clear_pattern(struct pattern_info *info)
153*838f5788Ssimonb {
154*838f5788Ssimonb 	if (info->text != NULL)
155*838f5788Ssimonb 		free(info->text);
156*838f5788Ssimonb 	info->text = NULL;
157*838f5788Ssimonb #if !NO_REGEX
158*838f5788Ssimonb 	uncompile_pattern(&info->compiled);
159*838f5788Ssimonb #endif
160*838f5788Ssimonb }
161*838f5788Ssimonb 
162*838f5788Ssimonb /*
16320006a0bStron  * Compile and save a search pattern.
16420006a0bStron  */
set_pattern(struct pattern_info * info,char * pattern,int search_type,int show_error)165*838f5788Ssimonb static int set_pattern(struct pattern_info *info, char *pattern, int search_type, int show_error)
16620006a0bStron {
167*838f5788Ssimonb 	/*
168*838f5788Ssimonb 	 * Ignore case if -I is set OR
169*838f5788Ssimonb 	 * -i is set AND the pattern is all lowercase.
170*838f5788Ssimonb 	 */
171*838f5788Ssimonb 	info->is_ucase_pattern = (pattern == NULL) ? FALSE : is_ucase(pattern);
172*838f5788Ssimonb 	is_caseless = (info->is_ucase_pattern && caseless != OPT_ONPLUS) ? 0 : caseless;
173ec18bca0Stron #if !NO_REGEX
17420006a0bStron 	if (pattern == NULL)
175*838f5788Ssimonb 		SET_NULL_PATTERN(info->compiled);
176*838f5788Ssimonb 	else if (compile_pattern(pattern, search_type, show_error, &info->compiled) < 0)
17720006a0bStron 		return -1;
178ec18bca0Stron #endif
17920006a0bStron 	/* Pattern compiled successfully; save the text too. */
18020006a0bStron 	if (info->text != NULL)
18120006a0bStron 		free(info->text);
18220006a0bStron 	info->text = NULL;
18320006a0bStron 	if (pattern != NULL)
18420006a0bStron 	{
18520006a0bStron 		info->text = (char *) ecalloc(1, strlen(pattern)+1);
18620006a0bStron 		strcpy(info->text, pattern);
18720006a0bStron 	}
18820006a0bStron 	info->search_type = search_type;
18920006a0bStron 	return 0;
19020006a0bStron }
19120006a0bStron 
19220006a0bStron /*
19320006a0bStron  * Initialize saved pattern to nothing.
19420006a0bStron  */
init_pattern(struct pattern_info * info)195*838f5788Ssimonb static void init_pattern(struct pattern_info *info)
19620006a0bStron {
197*838f5788Ssimonb 	SET_NULL_PATTERN(info->compiled);
19820006a0bStron 	info->text = NULL;
19920006a0bStron 	info->search_type = 0;
200*838f5788Ssimonb 	info->next = NULL;
20120006a0bStron }
20220006a0bStron 
20320006a0bStron /*
20420006a0bStron  * Initialize search variables.
20520006a0bStron  */
init_search(void)206*838f5788Ssimonb public void init_search(void)
20720006a0bStron {
20820006a0bStron 	init_pattern(&search_info);
20920006a0bStron }
21020006a0bStron 
21120006a0bStron /*
21220006a0bStron  * Determine which text conversions to perform before pattern matching.
21320006a0bStron  */
get_cvt_ops(int search_type)214*838f5788Ssimonb static int get_cvt_ops(int search_type)
21520006a0bStron {
21620006a0bStron 	int ops = 0;
217*838f5788Ssimonb 
218*838f5788Ssimonb 	if (is_caseless && (!re_handles_caseless || (search_type & SRCH_NO_REGEX)))
21920006a0bStron 		ops |= CVT_TO_LC;
220*838f5788Ssimonb 	if (proc_backspace == OPT_ON || (bs_mode == BS_SPECIAL && proc_backspace == OPT_OFF))
22120006a0bStron 		ops |= CVT_BS;
222*838f5788Ssimonb 	if (proc_return == OPT_ON || (bs_mode != BS_CONTROL && proc_backspace == OPT_OFF))
22320006a0bStron 		ops |= CVT_CRLF;
22420006a0bStron 	if (ctldisp == OPT_ONPLUS)
22520006a0bStron 		ops |= CVT_ANSI;
22620006a0bStron 	return (ops);
22720006a0bStron }
22820006a0bStron 
22920006a0bStron /*
23020006a0bStron  * Is there a previous (remembered) search pattern?
23120006a0bStron  */
prev_pattern(struct pattern_info * info)232*838f5788Ssimonb static int prev_pattern(struct pattern_info *info)
23320006a0bStron {
234ec18bca0Stron #if !NO_REGEX
235ec18bca0Stron 	if ((info->search_type & SRCH_NO_REGEX) == 0)
23620006a0bStron 		return (!is_null_pattern(info->compiled));
237ec18bca0Stron #endif
238ec18bca0Stron 	return (info->text != NULL);
23920006a0bStron }
24020006a0bStron 
24120006a0bStron #if HILITE_SEARCH
24220006a0bStron /*
24320006a0bStron  * Repaint the hilites currently displayed on the screen.
24420006a0bStron  * Repaint each line which contains highlighted text.
24520006a0bStron  * If on==0, force all hilites off.
24620006a0bStron  */
repaint_hilite(int on)247*838f5788Ssimonb public void repaint_hilite(int on)
24820006a0bStron {
249*838f5788Ssimonb 	int sindex;
25020006a0bStron 	POSITION pos;
25120006a0bStron 	int save_hide_hilite;
25220006a0bStron 
25320006a0bStron 	if (squished)
25420006a0bStron 		repaint();
25520006a0bStron 
25620006a0bStron 	save_hide_hilite = hide_hilite;
25720006a0bStron 	if (!on)
25820006a0bStron 	{
25920006a0bStron 		if (hide_hilite)
26020006a0bStron 			return;
26120006a0bStron 		hide_hilite = 1;
26220006a0bStron 	}
26320006a0bStron 
26420006a0bStron 	if (!can_goto_line)
26520006a0bStron 	{
26620006a0bStron 		repaint();
26720006a0bStron 		hide_hilite = save_hide_hilite;
26820006a0bStron 		return;
26920006a0bStron 	}
27020006a0bStron 
271*838f5788Ssimonb 	for (sindex = TOP;  sindex < TOP + sc_height-1;  sindex++)
27220006a0bStron 	{
273*838f5788Ssimonb 		pos = position(sindex);
27420006a0bStron 		if (pos == NULL_POSITION)
27520006a0bStron 			continue;
27620006a0bStron 		(void) forw_line(pos);
277*838f5788Ssimonb 		goto_line(sindex);
278*838f5788Ssimonb 		clear_eol();
27920006a0bStron 		put_line();
28020006a0bStron 	}
281*838f5788Ssimonb 	overlay_header();
28220006a0bStron 	lower_left();
28320006a0bStron 	hide_hilite = save_hide_hilite;
28420006a0bStron }
285*838f5788Ssimonb #endif
28620006a0bStron 
28720006a0bStron /*
28820006a0bStron  * Clear the attn hilite.
28920006a0bStron  */
clear_attn(void)290*838f5788Ssimonb public void clear_attn(void)
29120006a0bStron {
292*838f5788Ssimonb #if HILITE_SEARCH
293*838f5788Ssimonb 	int sindex;
29420006a0bStron 	POSITION old_start_attnpos;
29520006a0bStron 	POSITION old_end_attnpos;
29620006a0bStron 	POSITION pos;
29720006a0bStron 	POSITION epos;
29820006a0bStron 	int moved = 0;
29920006a0bStron 
30020006a0bStron 	if (start_attnpos == NULL_POSITION)
30120006a0bStron 		return;
30220006a0bStron 	old_start_attnpos = start_attnpos;
30320006a0bStron 	old_end_attnpos = end_attnpos;
30420006a0bStron 	start_attnpos = end_attnpos = NULL_POSITION;
30520006a0bStron 
30620006a0bStron 	if (!can_goto_line)
30720006a0bStron 	{
30820006a0bStron 		repaint();
30920006a0bStron 		return;
31020006a0bStron 	}
31120006a0bStron 	if (squished)
31220006a0bStron 		repaint();
31320006a0bStron 
314*838f5788Ssimonb 	for (sindex = TOP;  sindex < TOP + sc_height-1;  sindex++)
31520006a0bStron 	{
316*838f5788Ssimonb 		pos = position(sindex);
31720006a0bStron 		if (pos == NULL_POSITION)
31820006a0bStron 			continue;
319*838f5788Ssimonb 		epos = position(sindex+1);
320*838f5788Ssimonb 		if (pos <= old_end_attnpos &&
32120006a0bStron 		     (epos == NULL_POSITION || epos > old_start_attnpos))
32220006a0bStron 		{
32320006a0bStron 			(void) forw_line(pos);
324*838f5788Ssimonb 			goto_line(sindex);
325*838f5788Ssimonb 			clear_eol();
32620006a0bStron 			put_line();
32720006a0bStron 			moved = 1;
32820006a0bStron 		}
32920006a0bStron 	}
330*838f5788Ssimonb 	if (overlay_header())
331*838f5788Ssimonb 		moved = 1;
33220006a0bStron 	if (moved)
33320006a0bStron 		lower_left();
33420006a0bStron #endif
335*838f5788Ssimonb }
33620006a0bStron 
33720006a0bStron /*
338*838f5788Ssimonb  * Toggle or clear search string highlighting.
33920006a0bStron  */
undo_search(int clear)340*838f5788Ssimonb public void undo_search(int clear)
34120006a0bStron {
342*838f5788Ssimonb 	clear_pattern(&search_info);
343*838f5788Ssimonb #if HILITE_SEARCH
344*838f5788Ssimonb 	if (clear)
345*838f5788Ssimonb 	{
346*838f5788Ssimonb 		clr_hilite();
347*838f5788Ssimonb 	} else
348*838f5788Ssimonb 	{
349*838f5788Ssimonb 		if (hilite_anchor.first == NULL)
35020006a0bStron 		{
35120006a0bStron 			error("No previous regular expression", NULL_PARG);
35220006a0bStron 			return;
35320006a0bStron 		}
35420006a0bStron 		hide_hilite = !hide_hilite;
355*838f5788Ssimonb 	}
35620006a0bStron 	repaint_hilite(1);
35720006a0bStron #endif
35820006a0bStron }
35920006a0bStron 
36020006a0bStron #if HILITE_SEARCH
36120006a0bStron /*
36220006a0bStron  * Clear the hilite list.
36320006a0bStron  */
clr_hlist(struct hilite_tree * anchor)364*838f5788Ssimonb public void clr_hlist(struct hilite_tree *anchor)
36520006a0bStron {
366*838f5788Ssimonb 	struct hilite_storage *hls;
367*838f5788Ssimonb 	struct hilite_storage *nexthls;
36820006a0bStron 
369*838f5788Ssimonb 	for (hls = anchor->first;  hls != NULL;  hls = nexthls)
37020006a0bStron 	{
371*838f5788Ssimonb 		nexthls = hls->next;
372*838f5788Ssimonb 		free((void*)hls->nodes);
373*838f5788Ssimonb 		free((void*)hls);
37420006a0bStron 	}
375*838f5788Ssimonb 	anchor->first = NULL;
376*838f5788Ssimonb 	anchor->current = NULL;
377*838f5788Ssimonb 	anchor->root = NULL;
378*838f5788Ssimonb 
379*838f5788Ssimonb 	anchor->lookaside = NULL;
380*838f5788Ssimonb 
38120006a0bStron 	prep_startpos = prep_endpos = NULL_POSITION;
38220006a0bStron }
38320006a0bStron 
clr_hilite(void)384*838f5788Ssimonb public void clr_hilite(void)
38520006a0bStron {
38620006a0bStron 	clr_hlist(&hilite_anchor);
38720006a0bStron }
38820006a0bStron 
clr_filter(void)389*838f5788Ssimonb public void clr_filter(void)
39020006a0bStron {
39120006a0bStron 	clr_hlist(&filter_anchor);
39220006a0bStron }
39320006a0bStron 
39420006a0bStron /*
395*838f5788Ssimonb  * Find the node covering pos, or the node after it if no node covers it,
396*838f5788Ssimonb  * or return NULL if pos is after the last range. Remember the found node,
397*838f5788Ssimonb  * to speed up subsequent searches for the same or similar positions (if
398*838f5788Ssimonb  * we return NULL, remember the last node.)
39920006a0bStron  */
hlist_find(struct hilite_tree * anchor,POSITION pos)400*838f5788Ssimonb static struct hilite_node* hlist_find(struct hilite_tree *anchor, POSITION pos)
40120006a0bStron {
402*838f5788Ssimonb 	struct hilite_node *n, *m;
403*838f5788Ssimonb 
404*838f5788Ssimonb 	if (anchor->lookaside)
405*838f5788Ssimonb 	{
406*838f5788Ssimonb 		int steps = 0;
407*838f5788Ssimonb 		int hit = 0;
408*838f5788Ssimonb 
409*838f5788Ssimonb 		n = anchor->lookaside;
410*838f5788Ssimonb 
411*838f5788Ssimonb 		for (;;)
412*838f5788Ssimonb 		{
413*838f5788Ssimonb 			if (pos < n->r.hl_endpos)
414*838f5788Ssimonb 			{
415*838f5788Ssimonb 				if (n->prev == NULL || pos >= n->prev->r.hl_endpos)
416*838f5788Ssimonb 				{
417*838f5788Ssimonb 					hit = 1;
418*838f5788Ssimonb 					break;
419*838f5788Ssimonb 				}
420*838f5788Ssimonb 			} else if (n->next == NULL)
421*838f5788Ssimonb 			{
422*838f5788Ssimonb 				n = NULL;
423*838f5788Ssimonb 				hit = 1;
424*838f5788Ssimonb 				break;
425*838f5788Ssimonb 			}
42620006a0bStron 
42720006a0bStron 			/*
428*838f5788Ssimonb 			 * If we don't find the right node within a small
429*838f5788Ssimonb 			 * distance, don't keep doing a linear search!
43020006a0bStron 			 */
431*838f5788Ssimonb 			if (steps >= HILITE_LOOKASIDE_STEPS)
432*838f5788Ssimonb 				break;
433*838f5788Ssimonb 			steps++;
434*838f5788Ssimonb 
435*838f5788Ssimonb 			if (pos < n->r.hl_endpos)
436*838f5788Ssimonb 				anchor->lookaside = n = n->prev;
437*838f5788Ssimonb 			else
438*838f5788Ssimonb 				anchor->lookaside = n = n->next;
43920006a0bStron 		}
440*838f5788Ssimonb 
441*838f5788Ssimonb 		if (hit)
442*838f5788Ssimonb 			return n;
443*838f5788Ssimonb 	}
444*838f5788Ssimonb 
445*838f5788Ssimonb 	n = anchor->root;
446*838f5788Ssimonb 	m = NULL;
447*838f5788Ssimonb 
448*838f5788Ssimonb 	while (n != NULL)
449*838f5788Ssimonb 	{
450*838f5788Ssimonb 		if (pos < n->r.hl_startpos)
451*838f5788Ssimonb 		{
452*838f5788Ssimonb 			if (n->left != NULL)
453*838f5788Ssimonb 			{
454*838f5788Ssimonb 				m = n;
455*838f5788Ssimonb 				n = n->left;
456*838f5788Ssimonb 				continue;
457*838f5788Ssimonb 			}
458*838f5788Ssimonb 			break;
459*838f5788Ssimonb 		}
460*838f5788Ssimonb 		if (pos >= n->r.hl_endpos)
461*838f5788Ssimonb 		{
462*838f5788Ssimonb 			if (n->right != NULL)
463*838f5788Ssimonb 			{
464*838f5788Ssimonb 				n = n->right;
465*838f5788Ssimonb 				continue;
466*838f5788Ssimonb 			}
467*838f5788Ssimonb 			if (m != NULL)
468*838f5788Ssimonb 			{
469*838f5788Ssimonb 				n = m;
470*838f5788Ssimonb 			} else
471*838f5788Ssimonb 			{
472*838f5788Ssimonb 				m = n;
473*838f5788Ssimonb 				n = NULL;
474*838f5788Ssimonb 			}
475*838f5788Ssimonb 		}
476*838f5788Ssimonb 		break;
477*838f5788Ssimonb 	}
478*838f5788Ssimonb 
479*838f5788Ssimonb 	if (n != NULL)
480*838f5788Ssimonb 		anchor->lookaside = n;
481*838f5788Ssimonb 	else if (m != NULL)
482*838f5788Ssimonb 		anchor->lookaside = m;
483*838f5788Ssimonb 
484*838f5788Ssimonb 	return n;
485*838f5788Ssimonb }
486*838f5788Ssimonb 
487*838f5788Ssimonb /*
488*838f5788Ssimonb  * Should any characters in a specified range be highlighted?
489*838f5788Ssimonb  */
hilited_range_attr(POSITION pos,POSITION epos)490*838f5788Ssimonb static int hilited_range_attr(POSITION pos, POSITION epos)
491*838f5788Ssimonb {
492*838f5788Ssimonb 	struct hilite_node *n = hlist_find(&hilite_anchor, pos);
493*838f5788Ssimonb 	if (n == NULL)
494*838f5788Ssimonb 		return 0;
495*838f5788Ssimonb 	if (epos != NULL_POSITION && epos <= n->r.hl_startpos)
496*838f5788Ssimonb 		return 0;
497*838f5788Ssimonb 	return n->r.hl_attr;
49820006a0bStron }
49920006a0bStron 
50020006a0bStron /*
50120006a0bStron  * Is a line "filtered" -- that is, should it be hidden?
50220006a0bStron  */
is_filtered(POSITION pos)503*838f5788Ssimonb public int is_filtered(POSITION pos)
50420006a0bStron {
505*838f5788Ssimonb 	struct hilite_node *n;
50620006a0bStron 
50720006a0bStron 	if (ch_getflags() & CH_HELPFILE)
50820006a0bStron 		return (0);
50920006a0bStron 
510*838f5788Ssimonb 	n = hlist_find(&filter_anchor, pos);
511*838f5788Ssimonb 	return (n != NULL && pos >= n->r.hl_startpos);
512*838f5788Ssimonb }
513*838f5788Ssimonb 
51420006a0bStron /*
515*838f5788Ssimonb  * If pos is hidden, return the next position which isn't, otherwise
516*838f5788Ssimonb  * just return pos.
51720006a0bStron  */
next_unfiltered(POSITION pos)518*838f5788Ssimonb public POSITION next_unfiltered(POSITION pos)
51920006a0bStron {
520*838f5788Ssimonb 	struct hilite_node *n;
521*838f5788Ssimonb 
522*838f5788Ssimonb 	if (ch_getflags() & CH_HELPFILE)
523*838f5788Ssimonb 		return (pos);
524*838f5788Ssimonb 
525*838f5788Ssimonb 	n = hlist_find(&filter_anchor, pos);
526*838f5788Ssimonb 	while (n != NULL && pos >= n->r.hl_startpos)
527*838f5788Ssimonb 	{
528*838f5788Ssimonb 		pos = n->r.hl_endpos;
529*838f5788Ssimonb 		n = n->next;
53020006a0bStron 	}
531*838f5788Ssimonb 	return (pos);
53220006a0bStron }
53320006a0bStron 
53420006a0bStron /*
535*838f5788Ssimonb  * If pos is hidden, return the previous position which isn't or 0 if
536*838f5788Ssimonb  * we're filtered right to the beginning, otherwise just return pos.
537*838f5788Ssimonb  */
prev_unfiltered(POSITION pos)538*838f5788Ssimonb public POSITION prev_unfiltered(POSITION pos)
539*838f5788Ssimonb {
540*838f5788Ssimonb 	struct hilite_node *n;
541*838f5788Ssimonb 
542*838f5788Ssimonb 	if (ch_getflags() & CH_HELPFILE)
543*838f5788Ssimonb 		return (pos);
544*838f5788Ssimonb 
545*838f5788Ssimonb 	n = hlist_find(&filter_anchor, pos);
546*838f5788Ssimonb 	while (n != NULL && pos >= n->r.hl_startpos)
547*838f5788Ssimonb 	{
548*838f5788Ssimonb 		pos = n->r.hl_startpos;
549*838f5788Ssimonb 		if (pos == 0)
550*838f5788Ssimonb 			break;
551*838f5788Ssimonb 		pos--;
552*838f5788Ssimonb 		n = n->prev;
553*838f5788Ssimonb 	}
554*838f5788Ssimonb 	return (pos);
555*838f5788Ssimonb }
556*838f5788Ssimonb 
557*838f5788Ssimonb 
558*838f5788Ssimonb /*
55920006a0bStron  * Should any characters in a specified range be highlighted?
56020006a0bStron  * If nohide is nonzero, don't consider hide_hilite.
56120006a0bStron  */
is_hilited_attr(POSITION pos,POSITION epos,int nohide,int * p_matches)562*838f5788Ssimonb public int is_hilited_attr(POSITION pos, POSITION epos, int nohide, int *p_matches)
56320006a0bStron {
564*838f5788Ssimonb 	int attr;
56520006a0bStron 
56620006a0bStron 	if (p_matches != NULL)
56720006a0bStron 		*p_matches = 0;
56820006a0bStron 
56920006a0bStron 	if (!status_col &&
57020006a0bStron 	    start_attnpos != NULL_POSITION &&
571*838f5788Ssimonb 	    pos <= end_attnpos &&
572*838f5788Ssimonb 	     (epos == NULL_POSITION || epos >= start_attnpos))
57320006a0bStron 		/*
57420006a0bStron 		 * The attn line overlaps this range.
57520006a0bStron 		 */
576*838f5788Ssimonb 		return (AT_HILITE|AT_COLOR_ATTN);
57720006a0bStron 
578*838f5788Ssimonb 	attr = hilited_range_attr(pos, epos);
579*838f5788Ssimonb 	if (attr == 0)
58020006a0bStron 		return (0);
58120006a0bStron 
582*838f5788Ssimonb 	if (p_matches == NULL)
583*838f5788Ssimonb 		/*
584*838f5788Ssimonb 		 * Kinda kludgy way to recognize that caller is checking for
585*838f5788Ssimonb 		 * hilite in status column. In this case we want to return
586*838f5788Ssimonb 		 * hilite status even if hiliting is disabled or hidden.
587*838f5788Ssimonb 		 */
588*838f5788Ssimonb 		return (attr);
589*838f5788Ssimonb 
59020006a0bStron 	/*
59120006a0bStron 	 * Report matches, even if we're hiding highlights.
59220006a0bStron 	 */
59320006a0bStron 	*p_matches = 1;
59420006a0bStron 
59520006a0bStron 	if (hilite_search == 0)
59620006a0bStron 		/*
59720006a0bStron 		 * Not doing highlighting.
59820006a0bStron 		 */
59920006a0bStron 		return (0);
60020006a0bStron 
60120006a0bStron 	if (!nohide && hide_hilite)
60220006a0bStron 		/*
60320006a0bStron 		 * Highlighting is hidden.
60420006a0bStron 		 */
60520006a0bStron 		return (0);
60620006a0bStron 
607*838f5788Ssimonb 	return (attr);
60820006a0bStron }
60920006a0bStron 
61020006a0bStron /*
611*838f5788Ssimonb  * Tree node storage: get the current block of nodes if it has spare
612*838f5788Ssimonb  * capacity, or create a new one if not.
613*838f5788Ssimonb  */
hlist_getstorage(struct hilite_tree * anchor)614*838f5788Ssimonb static struct hilite_storage * hlist_getstorage(struct hilite_tree *anchor)
615*838f5788Ssimonb {
616*838f5788Ssimonb 	int capacity = 1;
617*838f5788Ssimonb 	struct hilite_storage *s;
618*838f5788Ssimonb 
619*838f5788Ssimonb 	if (anchor->current)
620*838f5788Ssimonb 	{
621*838f5788Ssimonb 		if (anchor->current->used < anchor->current->capacity)
622*838f5788Ssimonb 			return anchor->current;
623*838f5788Ssimonb 		capacity = anchor->current->capacity * 2;
624*838f5788Ssimonb 	}
625*838f5788Ssimonb 
626*838f5788Ssimonb 	s = (struct hilite_storage *) ecalloc(1, sizeof(struct hilite_storage));
627*838f5788Ssimonb 	s->nodes = (struct hilite_node *) ecalloc(capacity, sizeof(struct hilite_node));
628*838f5788Ssimonb 	s->capacity = capacity;
629*838f5788Ssimonb 	s->used = 0;
630*838f5788Ssimonb 	s->next = NULL;
631*838f5788Ssimonb 	if (anchor->current)
632*838f5788Ssimonb 		anchor->current->next = s;
633*838f5788Ssimonb 	else
634*838f5788Ssimonb 		anchor->first = s;
635*838f5788Ssimonb 	anchor->current = s;
636*838f5788Ssimonb 	return s;
637*838f5788Ssimonb }
638*838f5788Ssimonb 
639*838f5788Ssimonb /*
640*838f5788Ssimonb  * Tree node storage: retrieve a new empty node to be inserted into the
641*838f5788Ssimonb  * tree.
642*838f5788Ssimonb  */
hlist_getnode(struct hilite_tree * anchor)643*838f5788Ssimonb static struct hilite_node * hlist_getnode(struct hilite_tree *anchor)
644*838f5788Ssimonb {
645*838f5788Ssimonb 	struct hilite_storage *s = hlist_getstorage(anchor);
646*838f5788Ssimonb 	return &s->nodes[s->used++];
647*838f5788Ssimonb }
648*838f5788Ssimonb 
649*838f5788Ssimonb /*
650*838f5788Ssimonb  * Rotate the tree left around a pivot node.
651*838f5788Ssimonb  */
hlist_rotate_left(struct hilite_tree * anchor,struct hilite_node * n)652*838f5788Ssimonb static void hlist_rotate_left(struct hilite_tree *anchor, struct hilite_node *n)
653*838f5788Ssimonb {
654*838f5788Ssimonb 	struct hilite_node *np = n->parent;
655*838f5788Ssimonb 	struct hilite_node *nr = n->right;
656*838f5788Ssimonb 	struct hilite_node *nrl = n->right->left;
657*838f5788Ssimonb 
658*838f5788Ssimonb 	if (np != NULL)
659*838f5788Ssimonb 	{
660*838f5788Ssimonb 		if (n == np->left)
661*838f5788Ssimonb 			np->left = nr;
662*838f5788Ssimonb 		else
663*838f5788Ssimonb 			np->right = nr;
664*838f5788Ssimonb 	} else
665*838f5788Ssimonb 	{
666*838f5788Ssimonb 		anchor->root = nr;
667*838f5788Ssimonb 	}
668*838f5788Ssimonb 	nr->left = n;
669*838f5788Ssimonb 	n->right = nrl;
670*838f5788Ssimonb 
671*838f5788Ssimonb 	nr->parent = np;
672*838f5788Ssimonb 	n->parent = nr;
673*838f5788Ssimonb 	if (nrl != NULL)
674*838f5788Ssimonb 		nrl->parent = n;
675*838f5788Ssimonb }
676*838f5788Ssimonb 
677*838f5788Ssimonb /*
678*838f5788Ssimonb  * Rotate the tree right around a pivot node.
679*838f5788Ssimonb  */
hlist_rotate_right(struct hilite_tree * anchor,struct hilite_node * n)680*838f5788Ssimonb static void hlist_rotate_right(struct hilite_tree *anchor, struct hilite_node *n)
681*838f5788Ssimonb {
682*838f5788Ssimonb 	struct hilite_node *np = n->parent;
683*838f5788Ssimonb 	struct hilite_node *nl = n->left;
684*838f5788Ssimonb 	struct hilite_node *nlr = n->left->right;
685*838f5788Ssimonb 
686*838f5788Ssimonb 	if (np != NULL)
687*838f5788Ssimonb 	{
688*838f5788Ssimonb 		if (n == np->right)
689*838f5788Ssimonb 			np->right = nl;
690*838f5788Ssimonb 		else
691*838f5788Ssimonb 			np->left = nl;
692*838f5788Ssimonb 	} else
693*838f5788Ssimonb 	{
694*838f5788Ssimonb 		anchor->root = nl;
695*838f5788Ssimonb 	}
696*838f5788Ssimonb 	nl->right = n;
697*838f5788Ssimonb 	n->left = nlr;
698*838f5788Ssimonb 
699*838f5788Ssimonb 	nl->parent = np;
700*838f5788Ssimonb 	n->parent = nl;
701*838f5788Ssimonb 	if (nlr != NULL)
702*838f5788Ssimonb 		nlr->parent = n;
703*838f5788Ssimonb }
704*838f5788Ssimonb 
705*838f5788Ssimonb 
706*838f5788Ssimonb /*
70720006a0bStron  * Add a new hilite to a hilite list.
70820006a0bStron  */
add_hilite(struct hilite_tree * anchor,struct hilite * hl)709*838f5788Ssimonb static void add_hilite(struct hilite_tree *anchor, struct hilite *hl)
71020006a0bStron {
711*838f5788Ssimonb 	struct hilite_node *p, *n, *u;
712*838f5788Ssimonb 
713*838f5788Ssimonb 	/* Ignore empty ranges. */
714*838f5788Ssimonb 	if (hl->hl_startpos >= hl->hl_endpos)
715*838f5788Ssimonb 		return;
716*838f5788Ssimonb 
717*838f5788Ssimonb 	p = anchor->root;
718*838f5788Ssimonb 
719*838f5788Ssimonb 	/* Inserting the very first node is trivial. */
720*838f5788Ssimonb 	if (p == NULL)
721*838f5788Ssimonb 	{
722*838f5788Ssimonb 		n = hlist_getnode(anchor);
723*838f5788Ssimonb 		n->r = *hl;
724*838f5788Ssimonb 		anchor->root = n;
725*838f5788Ssimonb 		anchor->lookaside = n;
726*838f5788Ssimonb 		return;
727*838f5788Ssimonb 	}
72820006a0bStron 
72920006a0bStron 	/*
730*838f5788Ssimonb 	 * Find our insertion point. If we come across any overlapping
731*838f5788Ssimonb 	 * or adjoining existing ranges, shrink our range and discard
732*838f5788Ssimonb 	 * if it become empty.
73320006a0bStron 	 */
734*838f5788Ssimonb 	for (;;)
73520006a0bStron 	{
736*838f5788Ssimonb 		if (hl->hl_startpos < p->r.hl_startpos)
737*838f5788Ssimonb 		{
738*838f5788Ssimonb 			if (hl->hl_endpos > p->r.hl_startpos && hl->hl_attr == p->r.hl_attr)
739*838f5788Ssimonb 				hl->hl_endpos = p->r.hl_startpos;
740*838f5788Ssimonb 			if (p->left != NULL)
741*838f5788Ssimonb 			{
742*838f5788Ssimonb 				p = p->left;
743*838f5788Ssimonb 				continue;
744*838f5788Ssimonb 			}
745*838f5788Ssimonb 			break;
746*838f5788Ssimonb 		}
747*838f5788Ssimonb 		if (hl->hl_startpos < p->r.hl_endpos && hl->hl_attr == p->r.hl_attr) {
748*838f5788Ssimonb 			hl->hl_startpos = p->r.hl_endpos;
749*838f5788Ssimonb 			if (hl->hl_startpos >= hl->hl_endpos)
750*838f5788Ssimonb 				return;
751*838f5788Ssimonb 		}
752*838f5788Ssimonb 		if (p->right != NULL)
753*838f5788Ssimonb 		{
754*838f5788Ssimonb 			p = p->right;
755*838f5788Ssimonb 			continue;
756*838f5788Ssimonb 		}
75720006a0bStron 		break;
75820006a0bStron 	}
75920006a0bStron 
76020006a0bStron 	/*
761*838f5788Ssimonb 	 * Now we're at the right leaf, again check for contiguous ranges
762*838f5788Ssimonb 	 * and extend the existing node if possible to avoid the
763*838f5788Ssimonb 	 * insertion. Otherwise insert a new node at the leaf.
76420006a0bStron 	 */
765*838f5788Ssimonb 	if (hl->hl_startpos < p->r.hl_startpos) {
766*838f5788Ssimonb 		if (hl->hl_attr == p->r.hl_attr)
76720006a0bStron 		{
768*838f5788Ssimonb 			if (hl->hl_endpos == p->r.hl_startpos)
769*838f5788Ssimonb 			{
770*838f5788Ssimonb 				p->r.hl_startpos = hl->hl_startpos;
77120006a0bStron 				return;
77220006a0bStron 			}
773*838f5788Ssimonb 			if (p->prev != NULL && p->prev->r.hl_endpos == hl->hl_startpos)
774*838f5788Ssimonb 			{
775*838f5788Ssimonb 				p->prev->r.hl_endpos = hl->hl_endpos;
776*838f5788Ssimonb 				return;
777*838f5788Ssimonb 			}
778*838f5788Ssimonb 		}
779*838f5788Ssimonb 		p->left = n = hlist_getnode(anchor);
780*838f5788Ssimonb 		n->next = p;
781*838f5788Ssimonb 		if (p->prev != NULL)
782*838f5788Ssimonb 		{
783*838f5788Ssimonb 			n->prev = p->prev;
784*838f5788Ssimonb 			p->prev->next = n;
785*838f5788Ssimonb 		}
786*838f5788Ssimonb 		p->prev = n;
787*838f5788Ssimonb 	} else {
788*838f5788Ssimonb 		if (hl->hl_attr == p->r.hl_attr)
789*838f5788Ssimonb 		{
790*838f5788Ssimonb 			if (p->r.hl_endpos == hl->hl_startpos)
791*838f5788Ssimonb 			{
792*838f5788Ssimonb 				p->r.hl_endpos = hl->hl_endpos;
793*838f5788Ssimonb 				return;
794*838f5788Ssimonb 			}
795*838f5788Ssimonb 			if (p->next != NULL && hl->hl_endpos == p->next->r.hl_startpos) {
796*838f5788Ssimonb 				p->next->r.hl_startpos = hl->hl_startpos;
797*838f5788Ssimonb 				return;
798*838f5788Ssimonb 			}
799*838f5788Ssimonb 		}
800*838f5788Ssimonb 		p->right = n = hlist_getnode(anchor);
801*838f5788Ssimonb 		n->prev = p;
802*838f5788Ssimonb 		if (p->next != NULL)
803*838f5788Ssimonb 		{
804*838f5788Ssimonb 			n->next = p->next;
805*838f5788Ssimonb 			p->next->prev = n;
806*838f5788Ssimonb 		}
807*838f5788Ssimonb 		p->next = n;
808*838f5788Ssimonb 	}
809*838f5788Ssimonb 	n->parent = p;
810*838f5788Ssimonb 	n->red = 1;
811*838f5788Ssimonb 	n->r = *hl;
812*838f5788Ssimonb 
813*838f5788Ssimonb 	/*
814*838f5788Ssimonb 	 * The tree is in the correct order and covers the right ranges
815*838f5788Ssimonb 	 * now, but may have become unbalanced. Rebalance it using the
816*838f5788Ssimonb 	 * standard red-black tree constraints and operations.
817*838f5788Ssimonb 	 */
818*838f5788Ssimonb 	for (;;)
819*838f5788Ssimonb 	{
820*838f5788Ssimonb 		/* case 1 - current is root, root is always black */
821*838f5788Ssimonb 		if (n->parent == NULL)
822*838f5788Ssimonb 		{
823*838f5788Ssimonb 			n->red = 0;
824*838f5788Ssimonb 			break;
825*838f5788Ssimonb 		}
826*838f5788Ssimonb 
827*838f5788Ssimonb 		/* case 2 - parent is black, we can always be red */
828*838f5788Ssimonb 		if (!n->parent->red)
829*838f5788Ssimonb 			break;
830*838f5788Ssimonb 
831*838f5788Ssimonb 		/*
832*838f5788Ssimonb 		 * constraint: because the root must be black, if our
833*838f5788Ssimonb 		 * parent is red it cannot be the root therefore we must
834*838f5788Ssimonb 		 * have a grandparent
835*838f5788Ssimonb 		 */
836*838f5788Ssimonb 
837*838f5788Ssimonb 		/*
838*838f5788Ssimonb 		 * case 3 - parent and uncle are red, repaint them black,
839*838f5788Ssimonb 		 * the grandparent red, and start again at the grandparent.
840*838f5788Ssimonb 		 */
841*838f5788Ssimonb 		u = n->parent->parent->left;
842*838f5788Ssimonb 		if (n->parent == u)
843*838f5788Ssimonb 			u = n->parent->parent->right;
844*838f5788Ssimonb 		if (u != NULL && u->red)
845*838f5788Ssimonb 		{
846*838f5788Ssimonb 			n->parent->red = 0;
847*838f5788Ssimonb 			u->red = 0;
848*838f5788Ssimonb 			n = n->parent->parent;
849*838f5788Ssimonb 			n->red = 1;
850*838f5788Ssimonb 			continue;
85120006a0bStron 		}
85220006a0bStron 
85320006a0bStron 		/*
854*838f5788Ssimonb 		 * case 4 - parent is red but uncle is black, parent and
855*838f5788Ssimonb 		 * grandparent on opposite sides. We need to start
856*838f5788Ssimonb 		 * changing the structure now. This and case 5 will shorten
857*838f5788Ssimonb 		 * our branch and lengthen the sibling, between them
858*838f5788Ssimonb 		 * restoring balance.
859ec18bca0Stron 		 */
860*838f5788Ssimonb 		if (n == n->parent->right &&
861*838f5788Ssimonb 		    n->parent == n->parent->parent->left)
862ec18bca0Stron 		{
863*838f5788Ssimonb 			hlist_rotate_left(anchor, n->parent);
864*838f5788Ssimonb 			n = n->left;
865*838f5788Ssimonb 		} else if (n == n->parent->left &&
866*838f5788Ssimonb 			   n->parent == n->parent->parent->right)
867*838f5788Ssimonb 		{
868*838f5788Ssimonb 			hlist_rotate_right(anchor, n->parent);
869*838f5788Ssimonb 			n = n->right;
870*838f5788Ssimonb 		}
871*838f5788Ssimonb 
872*838f5788Ssimonb 		/*
873*838f5788Ssimonb 		 * case 5 - parent is red but uncle is black, parent and
874*838f5788Ssimonb 		 * grandparent on same side
875*838f5788Ssimonb 		 */
876*838f5788Ssimonb 		n->parent->red = 0;
877*838f5788Ssimonb 		n->parent->parent->red = 1;
878*838f5788Ssimonb 		if (n == n->parent->left)
879*838f5788Ssimonb 			hlist_rotate_right(anchor, n->parent->parent);
880*838f5788Ssimonb 		else
881*838f5788Ssimonb 			hlist_rotate_left(anchor, n->parent->parent);
882*838f5788Ssimonb 		break;
883*838f5788Ssimonb 	}
884*838f5788Ssimonb }
885*838f5788Ssimonb 
886*838f5788Ssimonb /*
887*838f5788Ssimonb  * Highlight every character in a range of displayed characters.
888*838f5788Ssimonb  */
create_hilites(POSITION linepos,char * line,char * sp,char * ep,int attr,int * chpos)889*838f5788Ssimonb static void create_hilites(POSITION linepos, char *line, char *sp, char *ep, int attr, int *chpos)
890*838f5788Ssimonb {
891*838f5788Ssimonb 	int start_index = sp - line;
892*838f5788Ssimonb 	int end_index = ep - line;
893*838f5788Ssimonb 	struct hilite hl;
894ec18bca0Stron 	int i;
895ec18bca0Stron 
896ec18bca0Stron 	/* Start the first hilite. */
897*838f5788Ssimonb 	hl.hl_startpos = linepos + chpos[start_index];
898*838f5788Ssimonb 	hl.hl_attr = attr;
899ec18bca0Stron 
900ec18bca0Stron 	/*
901ec18bca0Stron 	 * Step through the displayed chars.
902ec18bca0Stron 	 * If the source position (before cvt) of the char is one more
903ec18bca0Stron 	 * than the source pos of the previous char (the usual case),
904ec18bca0Stron 	 * just increase the size of the current hilite by one.
905ec18bca0Stron 	 * Otherwise (there are backspaces or something involved),
906ec18bca0Stron 	 * finish the current hilite and start a new one.
907ec18bca0Stron 	 */
908ec18bca0Stron 	for (i = start_index+1;  i <= end_index;  i++)
909ec18bca0Stron 	{
910ec18bca0Stron 		if (chpos[i] != chpos[i-1] + 1 || i == end_index)
911ec18bca0Stron 		{
912*838f5788Ssimonb 			hl.hl_endpos = linepos + chpos[i-1] + 1;
913*838f5788Ssimonb 			add_hilite(&hilite_anchor, &hl);
914ec18bca0Stron 			/* Start new hilite unless this is the last char. */
915ec18bca0Stron 			if (i < end_index)
916ec18bca0Stron 			{
917*838f5788Ssimonb 				hl.hl_startpos = linepos + chpos[i];
918ec18bca0Stron 			}
919ec18bca0Stron 		}
920ec18bca0Stron 	}
921ec18bca0Stron }
922ec18bca0Stron 
923ec18bca0Stron /*
92420006a0bStron  * Make a hilite for each string in a physical line which matches
92520006a0bStron  * the current pattern.
92620006a0bStron  * sp,ep delimit the first match already found.
92720006a0bStron  */
hilite_line(POSITION linepos,char * line,int line_len,int * chpos,char ** sp,char ** ep,int nsp,int cvt_ops)928*838f5788Ssimonb static void hilite_line(POSITION linepos, char *line, int line_len, int *chpos, char **sp, char **ep, int nsp, int cvt_ops)
92920006a0bStron {
93020006a0bStron 	char *searchp;
93120006a0bStron 	char *line_end = line + line_len;
93220006a0bStron 
93320006a0bStron 	/*
934*838f5788Ssimonb 	 * sp[0] and ep[0] delimit the first match in the line.
93520006a0bStron 	 * Mark the corresponding file positions, then
93620006a0bStron 	 * look for further matches and mark them.
93720006a0bStron 	 * {{ This technique, of calling match_pattern on subsequent
93820006a0bStron 	 *    substrings of the line, may mark more than is correct
93920006a0bStron 	 *    if the pattern starts with "^".  This bug is fixed
94020006a0bStron 	 *    for those regex functions that accept a notbol parameter
94120006a0bStron 	 *    (currently POSIX, PCRE and V8-with-regexec2). }}
942*838f5788Ssimonb 	 * sp[i] and ep[i] for i>0 delimit subpattern matches.
943*838f5788Ssimonb 	 * Color each of them with its unique color.
94420006a0bStron 	 */
94520006a0bStron 	searchp = line;
94620006a0bStron 	do {
947*838f5788Ssimonb 		char *lep = sp[0];
948*838f5788Ssimonb 		int i;
949*838f5788Ssimonb 		if (sp[0] == NULL || ep[0] == NULL)
950*838f5788Ssimonb 			break;
951*838f5788Ssimonb 		for (i = 1;  i < nsp;  i++)
952*838f5788Ssimonb 		{
953*838f5788Ssimonb 			if (sp[i] == NULL || ep[i] == NULL)
954*838f5788Ssimonb 				break;
955*838f5788Ssimonb 			if (ep[i] > sp[i])
956*838f5788Ssimonb 			{
957*838f5788Ssimonb 				create_hilites(linepos, line, lep, sp[i],
958*838f5788Ssimonb 					AT_HILITE | AT_COLOR_SEARCH, chpos);
959*838f5788Ssimonb 				create_hilites(linepos, line, sp[i], ep[i],
960*838f5788Ssimonb 					AT_HILITE | AT_COLOR_SUBSEARCH(i), chpos);
961*838f5788Ssimonb 				lep = ep[i];
962*838f5788Ssimonb 			}
963*838f5788Ssimonb 		}
964*838f5788Ssimonb 		create_hilites(linepos, line, lep, ep[0],
965*838f5788Ssimonb 			AT_HILITE | AT_COLOR_SEARCH, chpos);
966*838f5788Ssimonb 
96720006a0bStron 		/*
96820006a0bStron 		 * If we matched more than zero characters,
96920006a0bStron 		 * move to the first char after the string we matched.
97020006a0bStron 		 * If we matched zero, just move to the next char.
97120006a0bStron 		 */
972*838f5788Ssimonb 		if (ep[0] > searchp)
973*838f5788Ssimonb 			searchp = ep[0];
97420006a0bStron 		else if (searchp != line_end)
97520006a0bStron 			searchp++;
97620006a0bStron 		else /* end of line */
97720006a0bStron 			break;
978ec18bca0Stron 	} while (match_pattern(info_compiled(&search_info), search_info.text,
979*838f5788Ssimonb 			searchp, line_end - searchp, sp, ep, nsp, 1, search_info.search_type));
98020006a0bStron }
98120006a0bStron #endif
98220006a0bStron 
98320006a0bStron #if HILITE_SEARCH
98420006a0bStron /*
98520006a0bStron  * Find matching text which is currently on screen and highlight it.
98620006a0bStron  */
hilite_screen(void)987*838f5788Ssimonb static void hilite_screen(void)
98820006a0bStron {
98920006a0bStron 	struct scrpos scrpos;
99020006a0bStron 
991*838f5788Ssimonb 	get_scrpos(&scrpos, TOP);
99220006a0bStron 	if (scrpos.pos == NULL_POSITION)
99320006a0bStron 		return;
99420006a0bStron 	prep_hilite(scrpos.pos, position(BOTTOM_PLUS_ONE), -1);
99520006a0bStron 	repaint_hilite(1);
99620006a0bStron }
99720006a0bStron 
99820006a0bStron /*
99920006a0bStron  * Change highlighting parameters.
100020006a0bStron  */
chg_hilite(void)1001*838f5788Ssimonb public void chg_hilite(void)
100220006a0bStron {
100320006a0bStron 	/*
100420006a0bStron 	 * Erase any highlights currently on screen.
100520006a0bStron 	 */
100620006a0bStron 	clr_hilite();
100720006a0bStron 	hide_hilite = 0;
100820006a0bStron 
100920006a0bStron 	if (hilite_search == OPT_ONPLUS)
101020006a0bStron 		/*
101120006a0bStron 		 * Display highlights.
101220006a0bStron 		 */
101320006a0bStron 		hilite_screen();
101420006a0bStron }
101520006a0bStron #endif
101620006a0bStron 
101720006a0bStron /*
101820006a0bStron  * Figure out where to start a search.
101920006a0bStron  */
search_pos(int search_type)1020*838f5788Ssimonb static POSITION search_pos(int search_type)
102120006a0bStron {
102220006a0bStron 	POSITION pos;
1023*838f5788Ssimonb 	int sindex;
102420006a0bStron 
102520006a0bStron 	if (empty_screen())
102620006a0bStron 	{
102720006a0bStron 		/*
102820006a0bStron 		 * Start at the beginning (or end) of the file.
102920006a0bStron 		 * The empty_screen() case is mainly for
103020006a0bStron 		 * command line initiated searches;
103120006a0bStron 		 * for example, "+/xyz" on the command line.
103220006a0bStron 		 * Also for multi-file (SRCH_PAST_EOF) searches.
103320006a0bStron 		 */
103420006a0bStron 		if (search_type & SRCH_FORW)
103520006a0bStron 		{
103620006a0bStron 			pos = ch_zero();
103720006a0bStron 		} else
103820006a0bStron 		{
103920006a0bStron 			pos = ch_length();
104020006a0bStron 			if (pos == NULL_POSITION)
104120006a0bStron 			{
104220006a0bStron 				(void) ch_end_seek();
104320006a0bStron 				pos = ch_length();
104420006a0bStron 			}
104520006a0bStron 		}
1046*838f5788Ssimonb 		sindex = 0;
104720006a0bStron 	} else
104820006a0bStron 	{
104920006a0bStron 		int add_one = 0;
105020006a0bStron 
105120006a0bStron 		if (how_search == OPT_ON)
105220006a0bStron 		{
105320006a0bStron 			/*
105420006a0bStron 			 * Search does not include current screen.
105520006a0bStron 			 */
105620006a0bStron 			if (search_type & SRCH_FORW)
1057*838f5788Ssimonb 				sindex = sc_height-1; /* BOTTOM_PLUS_ONE */
105820006a0bStron 			else
1059*838f5788Ssimonb 				sindex = 0; /* TOP */
106020006a0bStron 		} else if (how_search == OPT_ONPLUS && !(search_type & SRCH_AFTER_TARGET))
106120006a0bStron 		{
106220006a0bStron 			/*
106320006a0bStron 			 * Search includes all of displayed screen.
106420006a0bStron 			 */
106520006a0bStron 			if (search_type & SRCH_FORW)
1066*838f5788Ssimonb 				sindex = 0; /* TOP */
106720006a0bStron 			else
1068*838f5788Ssimonb 				sindex = sc_height-1; /* BOTTOM_PLUS_ONE */
106920006a0bStron 		} else
107020006a0bStron 		{
107120006a0bStron 			/*
107220006a0bStron 			 * Search includes the part of current screen beyond the jump target.
107320006a0bStron 			 * It starts at the jump target (if searching backwards),
107420006a0bStron 			 * or at the jump target plus one (if forwards).
107520006a0bStron 			 */
1076*838f5788Ssimonb 			sindex = sindex_from_sline(jump_sline);
107720006a0bStron 			if (search_type & SRCH_FORW)
107820006a0bStron 				add_one = 1;
107920006a0bStron 		}
1080*838f5788Ssimonb 		pos = position(sindex);
108120006a0bStron 		if (add_one)
108220006a0bStron 			pos = forw_raw_line(pos, (char **)NULL, (int *)NULL);
108320006a0bStron 	}
108420006a0bStron 
108520006a0bStron 	/*
108620006a0bStron 	 * If the line is empty, look around for a plausible starting place.
108720006a0bStron 	 */
108820006a0bStron 	if (search_type & SRCH_FORW)
108920006a0bStron 	{
109020006a0bStron 		while (pos == NULL_POSITION)
109120006a0bStron 		{
1092*838f5788Ssimonb 			if (++sindex >= sc_height)
109320006a0bStron 				break;
1094*838f5788Ssimonb 			pos = position(sindex);
109520006a0bStron 		}
109620006a0bStron 	} else
109720006a0bStron 	{
109820006a0bStron 		while (pos == NULL_POSITION)
109920006a0bStron 		{
1100*838f5788Ssimonb 			if (--sindex < 0)
110120006a0bStron 				break;
1102*838f5788Ssimonb 			pos = position(sindex);
110320006a0bStron 		}
110420006a0bStron 	}
110520006a0bStron 	return (pos);
110620006a0bStron }
110720006a0bStron 
110820006a0bStron /*
1109*838f5788Ssimonb  * Check to see if the line matches the filter pattern.
1110*838f5788Ssimonb  * If so, add an entry to the filter list.
1111*838f5788Ssimonb  */
1112*838f5788Ssimonb #if HILITE_SEARCH
matches_filters(POSITION pos,char * cline,int line_len,int * chpos,POSITION linepos,char ** sp,char ** ep,int nsp)1113*838f5788Ssimonb static int matches_filters(POSITION pos, char *cline, int line_len, int *chpos, POSITION linepos, char **sp, char **ep, int nsp)
1114*838f5788Ssimonb {
1115*838f5788Ssimonb 	struct pattern_info *filter;
1116*838f5788Ssimonb 
1117*838f5788Ssimonb 	for (filter = filter_infos; filter != NULL; filter = filter->next)
1118*838f5788Ssimonb 	{
1119*838f5788Ssimonb 		int line_filter = match_pattern(info_compiled(filter), filter->text,
1120*838f5788Ssimonb 			cline, line_len, sp, ep, nsp, 0, filter->search_type);
1121*838f5788Ssimonb 		if (line_filter)
1122*838f5788Ssimonb 		{
1123*838f5788Ssimonb 			struct hilite hl;
1124*838f5788Ssimonb 			hl.hl_startpos = linepos;
1125*838f5788Ssimonb 			hl.hl_endpos = pos;
1126*838f5788Ssimonb 			add_hilite(&filter_anchor, &hl);
1127*838f5788Ssimonb 			free(cline);
1128*838f5788Ssimonb 			free(chpos);
1129*838f5788Ssimonb 			return (1);
1130*838f5788Ssimonb 		}
1131*838f5788Ssimonb 	}
1132*838f5788Ssimonb 	return (0);
1133*838f5788Ssimonb }
1134*838f5788Ssimonb #endif
1135*838f5788Ssimonb 
1136*838f5788Ssimonb /*
1137*838f5788Ssimonb  * Get the position of the first char in the screen line which
1138*838f5788Ssimonb  * puts tpos on screen.
1139*838f5788Ssimonb  */
get_lastlinepos(POSITION pos,POSITION tpos,int sheight)1140*838f5788Ssimonb static POSITION get_lastlinepos(POSITION pos, POSITION tpos, int sheight)
1141*838f5788Ssimonb {
1142*838f5788Ssimonb 	int nlines;
1143*838f5788Ssimonb 
1144*838f5788Ssimonb 	for (nlines = 0;;  nlines++)
1145*838f5788Ssimonb 	{
1146*838f5788Ssimonb 		POSITION npos = forw_line(pos);
1147*838f5788Ssimonb 		if (npos > tpos)
1148*838f5788Ssimonb 		{
1149*838f5788Ssimonb 			if (nlines < sheight)
1150*838f5788Ssimonb 				return NULL_POSITION;
1151*838f5788Ssimonb 			return pos;
1152*838f5788Ssimonb 		}
1153*838f5788Ssimonb 		pos = npos;
1154*838f5788Ssimonb 	}
1155*838f5788Ssimonb }
1156*838f5788Ssimonb 
1157*838f5788Ssimonb /*
1158*838f5788Ssimonb  * Get the segment index of tpos in the line starting at pos.
1159*838f5788Ssimonb  * A segment is a string of printable chars that fills the screen width.
1160*838f5788Ssimonb  */
get_seg(POSITION pos,POSITION tpos)1161*838f5788Ssimonb static int get_seg(POSITION pos, POSITION tpos)
1162*838f5788Ssimonb {
1163*838f5788Ssimonb 	int seg;
1164*838f5788Ssimonb 
1165*838f5788Ssimonb 	for (seg = 0;;  seg++)
1166*838f5788Ssimonb 	{
1167*838f5788Ssimonb 		POSITION npos = forw_line_seg(pos, FALSE, FALSE, TRUE);
1168*838f5788Ssimonb 		if (npos > tpos)
1169*838f5788Ssimonb 			return seg;
1170*838f5788Ssimonb 		pos = npos;
1171*838f5788Ssimonb 	}
1172*838f5788Ssimonb }
1173*838f5788Ssimonb 
1174*838f5788Ssimonb /*
117520006a0bStron  * Search a subset of the file, specified by start/end position.
117620006a0bStron  */
search_range(POSITION pos,POSITION endpos,int search_type,int matches,int maxlines,POSITION * plinepos,POSITION * pendpos,POSITION * plastlinepos)1177*838f5788Ssimonb static int search_range(POSITION pos, POSITION endpos, int search_type, int matches, int maxlines, POSITION *plinepos, POSITION *pendpos, POSITION *plastlinepos)
117820006a0bStron {
117920006a0bStron 	char *line;
118020006a0bStron 	char *cline;
118120006a0bStron 	int line_len;
118220006a0bStron 	LINENUM linenum;
1183*838f5788Ssimonb 	#define NSP (NUM_SEARCH_COLORS+2)
1184*838f5788Ssimonb 	char *sp[NSP];
1185*838f5788Ssimonb 	char *ep[NSP];
118620006a0bStron 	int line_match;
118720006a0bStron 	int cvt_ops;
118820006a0bStron 	int cvt_len;
118920006a0bStron 	int *chpos;
119020006a0bStron 	POSITION linepos, oldpos;
1191*838f5788Ssimonb 	int skip_bytes = 0;
1192*838f5788Ssimonb 	int swidth = sc_width - line_pfx_width();
1193*838f5788Ssimonb 	int sheight = sc_height - sindex_from_sline(jump_sline);
119420006a0bStron 
119520006a0bStron 	linenum = find_linenum(pos);
1196*838f5788Ssimonb 	if (nosearch_headers && linenum <= header_lines)
1197*838f5788Ssimonb 	{
1198*838f5788Ssimonb 		linenum = header_lines + 1;
1199*838f5788Ssimonb 		pos = find_pos(linenum);
1200*838f5788Ssimonb 	}
1201*838f5788Ssimonb 	if (pos == NULL_POSITION)
1202*838f5788Ssimonb 		return (-1);
120320006a0bStron 	oldpos = pos;
1204*838f5788Ssimonb 	/* When the search wraps around, end at starting position. */
1205*838f5788Ssimonb 	if ((search_type & SRCH_WRAP) && endpos == NULL_POSITION)
1206*838f5788Ssimonb 		endpos = pos;
120720006a0bStron 	for (;;)
120820006a0bStron 	{
120920006a0bStron 		/*
121020006a0bStron 		 * Get lines until we find a matching one or until
121120006a0bStron 		 * we hit end-of-file (or beginning-of-file if we're
121220006a0bStron 		 * going backwards), or until we hit the end position.
121320006a0bStron 		 */
121420006a0bStron 		if (ABORT_SIGS())
121520006a0bStron 		{
121620006a0bStron 			/*
121720006a0bStron 			 * A signal aborts the search.
121820006a0bStron 			 */
121920006a0bStron 			return (-1);
122020006a0bStron 		}
122120006a0bStron 
1222*838f5788Ssimonb 		if ((endpos != NULL_POSITION && !(search_type & SRCH_WRAP) &&
1223*838f5788Ssimonb 			(((search_type & SRCH_FORW) && pos >= endpos) ||
1224*838f5788Ssimonb 			 ((search_type & SRCH_BACK) && pos <= endpos))) || maxlines == 0)
122520006a0bStron 		{
122620006a0bStron 			/*
122720006a0bStron 			 * Reached end position without a match.
122820006a0bStron 			 */
122920006a0bStron 			if (pendpos != NULL)
123020006a0bStron 				*pendpos = pos;
123120006a0bStron 			return (matches);
123220006a0bStron 		}
123320006a0bStron 		if (maxlines > 0)
123420006a0bStron 			maxlines--;
123520006a0bStron 
123620006a0bStron 		if (search_type & SRCH_FORW)
123720006a0bStron 		{
123820006a0bStron 			/*
123920006a0bStron 			 * Read the next line, and save the
124020006a0bStron 			 * starting position of that line in linepos.
124120006a0bStron 			 */
124220006a0bStron 			linepos = pos;
124320006a0bStron 			pos = forw_raw_line(pos, &line, &line_len);
124420006a0bStron 			if (linenum != 0)
124520006a0bStron 				linenum++;
124620006a0bStron 		} else
124720006a0bStron 		{
124820006a0bStron 			/*
124920006a0bStron 			 * Read the previous line and save the
125020006a0bStron 			 * starting position of that line in linepos.
125120006a0bStron 			 */
125220006a0bStron 			pos = back_raw_line(pos, &line, &line_len);
125320006a0bStron 			linepos = pos;
125420006a0bStron 			if (linenum != 0)
125520006a0bStron 				linenum--;
125620006a0bStron 		}
125720006a0bStron 
125820006a0bStron 		if (pos == NULL_POSITION)
125920006a0bStron 		{
126020006a0bStron 			/*
126120006a0bStron 			 * Reached EOF/BOF without a match.
126220006a0bStron 			 */
1263*838f5788Ssimonb 			if (search_type & SRCH_WRAP)
1264*838f5788Ssimonb 			{
1265*838f5788Ssimonb 				/*
1266*838f5788Ssimonb 				 * The search wraps around the current file, so
1267*838f5788Ssimonb 				 * try to continue at BOF/EOF.
1268*838f5788Ssimonb 				 */
1269*838f5788Ssimonb 				if (search_type & SRCH_FORW)
1270*838f5788Ssimonb 				{
1271*838f5788Ssimonb 					pos = ch_zero();
1272*838f5788Ssimonb 				} else
1273*838f5788Ssimonb 				{
1274*838f5788Ssimonb 					pos = ch_length();
1275*838f5788Ssimonb 					if (pos == NULL_POSITION)
1276*838f5788Ssimonb 					{
1277*838f5788Ssimonb 						(void) ch_end_seek();
1278*838f5788Ssimonb 						pos = ch_length();
1279*838f5788Ssimonb 					}
1280*838f5788Ssimonb 				}
1281*838f5788Ssimonb 				if (pos != NULL_POSITION) {
1282*838f5788Ssimonb 					/*
1283*838f5788Ssimonb 					 * Wrap-around was successful. Clear
1284*838f5788Ssimonb 					 * the flag so we don't wrap again, and
1285*838f5788Ssimonb 					 * continue the search at new pos.
1286*838f5788Ssimonb 					 */
1287*838f5788Ssimonb 					search_type &= ~SRCH_WRAP;
1288*838f5788Ssimonb 					linenum = find_linenum(pos);
1289*838f5788Ssimonb 					continue;
1290*838f5788Ssimonb 				}
1291*838f5788Ssimonb 			}
129220006a0bStron 			if (pendpos != NULL)
129320006a0bStron 				*pendpos = oldpos;
129420006a0bStron 			return (matches);
129520006a0bStron 		}
129620006a0bStron 
129720006a0bStron 		/*
129820006a0bStron 		 * If we're using line numbers, we might as well
129920006a0bStron 		 * remember the information we have now (the position
130020006a0bStron 		 * and line number of the current line).
130120006a0bStron 		 * Don't do it for every line because it slows down
130220006a0bStron 		 * the search.  Remember the line number only if
130320006a0bStron 		 * we're "far" from the last place we remembered it.
130420006a0bStron 		 */
130520006a0bStron 		if (linenums && abs((int)(pos - oldpos)) > 2048)
130620006a0bStron 			add_lnum(linenum, pos);
130720006a0bStron 		oldpos = pos;
130820006a0bStron 
1309*838f5788Ssimonb #if HILITE_SEARCH
131020006a0bStron 		if (is_filtered(linepos))
131120006a0bStron 			continue;
1312*838f5788Ssimonb #endif
1313*838f5788Ssimonb 		if (nosearch_headers)
1314*838f5788Ssimonb 			skip_bytes = skip_columns(header_cols, &line, &line_len);
131520006a0bStron 
131620006a0bStron 		/*
131720006a0bStron 		 * If it's a caseless search, convert the line to lowercase.
131820006a0bStron 		 * If we're doing backspace processing, delete backspaces.
131920006a0bStron 		 */
1320*838f5788Ssimonb 		cvt_ops = get_cvt_ops(search_type);
132120006a0bStron 		cvt_len = cvt_length(line_len, cvt_ops);
132220006a0bStron 		cline = (char *) ecalloc(1, cvt_len);
132320006a0bStron 		chpos = cvt_alloc_chpos(cvt_len);
132420006a0bStron 		cvt_text(cline, line, chpos, &line_len, cvt_ops);
132520006a0bStron 
132620006a0bStron #if HILITE_SEARCH
132720006a0bStron 		/*
1328*838f5788Ssimonb 		 * If any filters are in effect, ignore non-matching lines.
132920006a0bStron 		 */
1330*838f5788Ssimonb 		if (filter_infos != NULL &&
1331*838f5788Ssimonb 		   ((search_type & SRCH_FIND_ALL) ||
1332*838f5788Ssimonb 		     prep_startpos == NULL_POSITION ||
1333*838f5788Ssimonb 		     linepos < prep_startpos || linepos >= prep_endpos)) {
1334*838f5788Ssimonb 			if (matches_filters(pos, cline, line_len, chpos, linepos, sp, ep, NSP))
1335*838f5788Ssimonb 				continue;
133620006a0bStron 		}
133720006a0bStron #endif
133820006a0bStron 
133920006a0bStron 		/*
134020006a0bStron 		 * Test the next line to see if we have a match.
134120006a0bStron 		 * We are successful if we either want a match and got one,
134220006a0bStron 		 * or if we want a non-match and got one.
134320006a0bStron 		 */
134420006a0bStron 		if (prev_pattern(&search_info))
134520006a0bStron 		{
1346ec18bca0Stron 			line_match = match_pattern(info_compiled(&search_info), search_info.text,
1347*838f5788Ssimonb 				cline, line_len, sp, ep, NSP, 0, search_type);
134820006a0bStron 			if (line_match)
134920006a0bStron 			{
135020006a0bStron 				/*
135120006a0bStron 				 * Got a match.
135220006a0bStron 				 */
135320006a0bStron 				if (search_type & SRCH_FIND_ALL)
135420006a0bStron 				{
135520006a0bStron #if HILITE_SEARCH
135620006a0bStron 					/*
135720006a0bStron 					 * We are supposed to find all matches in the range.
135820006a0bStron 					 * Just add the matches in this line to the
135920006a0bStron 					 * hilite list and keep searching.
136020006a0bStron 					 */
1361*838f5788Ssimonb 					hilite_line(linepos + skip_bytes, cline, line_len, chpos, sp, ep, NSP, cvt_ops);
136220006a0bStron #endif
136320006a0bStron 				} else if (--matches <= 0)
136420006a0bStron 				{
136520006a0bStron 					/*
136620006a0bStron 					 * Found the one match we're looking for.
136720006a0bStron 					 * Return it.
136820006a0bStron 					 */
136920006a0bStron #if HILITE_SEARCH
137020006a0bStron 					if (hilite_search == OPT_ON)
137120006a0bStron 					{
137220006a0bStron 						/*
137320006a0bStron 						 * Clear the hilite list and add only
137420006a0bStron 						 * the matches in this one line.
137520006a0bStron 						 */
137620006a0bStron 						clr_hilite();
1377*838f5788Ssimonb 						hilite_line(linepos + skip_bytes, cline, line_len, chpos, sp, ep, NSP, cvt_ops);
137820006a0bStron 					}
137920006a0bStron #endif
1380*838f5788Ssimonb 					if (chop_line())
1381*838f5788Ssimonb 					{
1382*838f5788Ssimonb 						/*
1383*838f5788Ssimonb 						 * If necessary, shift horizontally to make sure
1384*838f5788Ssimonb 						 * search match is fully visible.
1385*838f5788Ssimonb 						 */
1386*838f5788Ssimonb 						if (sp[0] != NULL && ep[0] != NULL)
1387*838f5788Ssimonb 						{
1388*838f5788Ssimonb 							int start_off = sp[0] - cline;
1389*838f5788Ssimonb 							int end_off = ep[0] - cline;
1390*838f5788Ssimonb 							int save_hshift = hshift;
1391*838f5788Ssimonb 							int sshift;
1392*838f5788Ssimonb 							int eshift;
1393*838f5788Ssimonb 							hshift = 0; /* make get_seg count screen lines */
1394*838f5788Ssimonb 							sshift = swidth * get_seg(linepos, linepos + chpos[start_off]);
1395*838f5788Ssimonb 							eshift = swidth * get_seg(linepos, linepos + chpos[end_off]);
1396*838f5788Ssimonb 							if (sshift >= save_hshift && eshift <= save_hshift)
1397*838f5788Ssimonb 							{
1398*838f5788Ssimonb 								hshift = save_hshift;
1399*838f5788Ssimonb 							} else
1400*838f5788Ssimonb 							{
1401*838f5788Ssimonb 								hshift = sshift;
1402*838f5788Ssimonb 								screen_trashed = 1;
1403*838f5788Ssimonb 							}
1404*838f5788Ssimonb 						}
1405*838f5788Ssimonb 					} else if (plastlinepos != NULL)
1406*838f5788Ssimonb 					{
1407*838f5788Ssimonb 						/*
1408*838f5788Ssimonb 						 * If the line is so long that the highlighted match
1409*838f5788Ssimonb 						 * won't be seen when the line is displayed normally
1410*838f5788Ssimonb 						 * (starting at the first char) because it fills the whole
1411*838f5788Ssimonb 						 * screen and more, scroll forward until the last char
1412*838f5788Ssimonb 						 * of the match appears in the last line on the screen.
1413*838f5788Ssimonb 						 * lastlinepos is the position of the first char of that last line.
1414*838f5788Ssimonb 						 */
1415*838f5788Ssimonb 						if (ep[0] != NULL)
1416*838f5788Ssimonb 						{
1417*838f5788Ssimonb 							int end_off = ep[0] - cline;
1418*838f5788Ssimonb 							if (end_off >= swidth * sheight / 4) /* heuristic */
1419*838f5788Ssimonb 								*plastlinepos = get_lastlinepos(linepos, linepos + chpos[end_off], sheight);
1420*838f5788Ssimonb 						}
1421*838f5788Ssimonb 					}
142220006a0bStron 					free(cline);
142320006a0bStron 					free(chpos);
142420006a0bStron 					if (plinepos != NULL)
142520006a0bStron 						*plinepos = linepos;
142620006a0bStron 					return (0);
142720006a0bStron 				}
142820006a0bStron 			}
142920006a0bStron 		}
143020006a0bStron 		free(cline);
143120006a0bStron 		free(chpos);
143220006a0bStron 	}
143320006a0bStron }
143420006a0bStron 
143520006a0bStron /*
143620006a0bStron  * search for a pattern in history. If found, compile that pattern.
143720006a0bStron  */
hist_pattern(int search_type)1438*838f5788Ssimonb static int hist_pattern(int search_type)
143920006a0bStron {
144020006a0bStron #if CMD_HISTORY
144120006a0bStron 	char *pattern;
144220006a0bStron 
144320006a0bStron 	set_mlist(ml_search, 0);
144420006a0bStron 	pattern = cmd_lastpattern();
144520006a0bStron 	if (pattern == NULL)
144620006a0bStron 		return (0);
144720006a0bStron 
1448*838f5788Ssimonb 	if (set_pattern(&search_info, pattern, search_type, 1) < 0)
1449*838f5788Ssimonb 		return (-1);
145020006a0bStron 
145120006a0bStron #if HILITE_SEARCH
145220006a0bStron 	if (hilite_search == OPT_ONPLUS && !hide_hilite)
145320006a0bStron 		hilite_screen();
145420006a0bStron #endif
145520006a0bStron 
145620006a0bStron 	return (1);
145720006a0bStron #else /* CMD_HISTORY */
145820006a0bStron 	return (0);
145920006a0bStron #endif /* CMD_HISTORY */
146020006a0bStron }
146120006a0bStron 
146220006a0bStron /*
1463*838f5788Ssimonb  * Change the caseless-ness of searches.
1464*838f5788Ssimonb  * Updates the internal search state to reflect a change in the -i flag.
1465*838f5788Ssimonb  */
chg_caseless(void)1466*838f5788Ssimonb public void chg_caseless(void)
1467*838f5788Ssimonb {
1468*838f5788Ssimonb 	if (!search_info.is_ucase_pattern)
1469*838f5788Ssimonb 	{
1470*838f5788Ssimonb 		/*
1471*838f5788Ssimonb 		 * Pattern did not have uppercase.
1472*838f5788Ssimonb 		 * Set the search caselessness to the global caselessness.
1473*838f5788Ssimonb 		 */
1474*838f5788Ssimonb 		is_caseless = caseless;
1475*838f5788Ssimonb 		/*
1476*838f5788Ssimonb 		 * If regex handles caseless, we need to discard
1477*838f5788Ssimonb 		 * the pattern which was compiled with the old caseless.
1478*838f5788Ssimonb 		 */
1479*838f5788Ssimonb 		if (!re_handles_caseless)
1480*838f5788Ssimonb 			/* We handle caseless, so the pattern doesn't change. */
1481*838f5788Ssimonb 			return;
1482*838f5788Ssimonb 	}
1483*838f5788Ssimonb 	/*
1484*838f5788Ssimonb 	 * Regenerate the pattern using the new state.
1485*838f5788Ssimonb 	 */
1486*838f5788Ssimonb 	clear_pattern(&search_info);
1487*838f5788Ssimonb 	(void) hist_pattern(search_info.search_type);
1488*838f5788Ssimonb }
1489*838f5788Ssimonb 
1490*838f5788Ssimonb /*
149120006a0bStron  * Search for the n-th occurrence of a specified pattern,
149220006a0bStron  * either forward or backward.
149320006a0bStron  * Return the number of matches not yet found in this file
149420006a0bStron  * (that is, n minus the number of matches found).
149520006a0bStron  * Return -1 if the search should be aborted.
149620006a0bStron  * Caller may continue the search in another file
149720006a0bStron  * if less than n matches are found in this file.
149820006a0bStron  */
search(int search_type,char * pattern,int n)1499*838f5788Ssimonb public int search(int search_type, char *pattern, int n)
150020006a0bStron {
150120006a0bStron 	POSITION pos;
1502*838f5788Ssimonb 	POSITION opos;
1503*838f5788Ssimonb 	POSITION lastlinepos = NULL_POSITION;
150420006a0bStron 
150520006a0bStron 	if (pattern == NULL || *pattern == '\0')
150620006a0bStron 	{
150720006a0bStron 		/*
150820006a0bStron 		 * A null pattern means use the previously compiled pattern.
150920006a0bStron 		 */
151020006a0bStron 		search_type |= SRCH_AFTER_TARGET;
1511*838f5788Ssimonb 		if (!prev_pattern(&search_info))
151220006a0bStron 		{
1513*838f5788Ssimonb 			int r = hist_pattern(search_type);
1514*838f5788Ssimonb 			if (r == 0)
151520006a0bStron 				error("No previous regular expression", NULL_PARG);
1516*838f5788Ssimonb 			if (r <= 0)
151720006a0bStron 				return (-1);
151820006a0bStron 		}
151920006a0bStron 		if ((search_type & SRCH_NO_REGEX) !=
152020006a0bStron 		      (search_info.search_type & SRCH_NO_REGEX))
152120006a0bStron 		{
152220006a0bStron 			error("Please re-enter search pattern", NULL_PARG);
152320006a0bStron 			return -1;
152420006a0bStron 		}
152520006a0bStron #if HILITE_SEARCH
1526*838f5788Ssimonb 		if (hilite_search == OPT_ON || status_col)
152720006a0bStron 		{
152820006a0bStron 			/*
152920006a0bStron 			 * Erase the highlights currently on screen.
153020006a0bStron 			 * If the search fails, we'll redisplay them later.
153120006a0bStron 			 */
153220006a0bStron 			repaint_hilite(0);
153320006a0bStron 		}
153420006a0bStron 		if (hilite_search == OPT_ONPLUS && hide_hilite)
153520006a0bStron 		{
153620006a0bStron 			/*
153720006a0bStron 			 * Highlight any matches currently on screen,
153820006a0bStron 			 * before we actually start the search.
153920006a0bStron 			 */
154020006a0bStron 			hide_hilite = 0;
154120006a0bStron 			hilite_screen();
154220006a0bStron 		}
154320006a0bStron 		hide_hilite = 0;
154420006a0bStron #endif
154520006a0bStron 	} else
154620006a0bStron 	{
154720006a0bStron 		/*
154820006a0bStron 		 * Compile the pattern.
154920006a0bStron 		 */
1550*838f5788Ssimonb 		int show_error = !(search_type & SRCH_INCR);
1551*838f5788Ssimonb 		if (set_pattern(&search_info, pattern, search_type, show_error) < 0)
155220006a0bStron 			return (-1);
155320006a0bStron #if HILITE_SEARCH
1554*838f5788Ssimonb 		if (hilite_search || status_col)
155520006a0bStron 		{
155620006a0bStron 			/*
155720006a0bStron 			 * Erase the highlights currently on screen.
155820006a0bStron 			 * Also permanently delete them from the hilite list.
155920006a0bStron 			 */
156020006a0bStron 			repaint_hilite(0);
156120006a0bStron 			hide_hilite = 0;
156220006a0bStron 			clr_hilite();
156320006a0bStron 		}
1564*838f5788Ssimonb 		if (hilite_search == OPT_ONPLUS || status_col)
156520006a0bStron 		{
156620006a0bStron 			/*
156720006a0bStron 			 * Highlight any matches currently on screen,
156820006a0bStron 			 * before we actually start the search.
156920006a0bStron 			 */
157020006a0bStron 			hilite_screen();
157120006a0bStron 		}
157220006a0bStron #endif
157320006a0bStron 	}
157420006a0bStron 
157520006a0bStron 	/*
157620006a0bStron 	 * Figure out where to start the search.
157720006a0bStron 	 */
157820006a0bStron 	pos = search_pos(search_type);
1579*838f5788Ssimonb 	opos = position(sindex_from_sline(jump_sline));
158020006a0bStron 	if (pos == NULL_POSITION)
158120006a0bStron 	{
158220006a0bStron 		/*
158320006a0bStron 		 * Can't find anyplace to start searching from.
158420006a0bStron 		 */
158520006a0bStron 		if (search_type & SRCH_PAST_EOF)
158620006a0bStron 			return (n);
1587*838f5788Ssimonb #if HILITE_SEARCH
1588*838f5788Ssimonb 		if (hilite_search == OPT_ON || status_col)
1589*838f5788Ssimonb 			repaint_hilite(1);
1590*838f5788Ssimonb #endif
159120006a0bStron 		error("Nothing to search", NULL_PARG);
159220006a0bStron 		return (-1);
159320006a0bStron 	}
159420006a0bStron 
159520006a0bStron 	n = search_range(pos, NULL_POSITION, search_type, n, -1,
1596*838f5788Ssimonb 			&pos, (POSITION*)NULL, &lastlinepos);
159720006a0bStron 	if (n != 0)
159820006a0bStron 	{
159920006a0bStron 		/*
160020006a0bStron 		 * Search was unsuccessful.
160120006a0bStron 		 */
160220006a0bStron #if HILITE_SEARCH
1603*838f5788Ssimonb 		if ((hilite_search == OPT_ON || status_col) && n > 0)
160420006a0bStron 			/*
160520006a0bStron 			 * Redisplay old hilites.
160620006a0bStron 			 */
160720006a0bStron 			repaint_hilite(1);
160820006a0bStron #endif
160920006a0bStron 		return (n);
161020006a0bStron 	}
161120006a0bStron 
161220006a0bStron 	if (!(search_type & SRCH_NO_MOVE))
161320006a0bStron 	{
161420006a0bStron 		/*
161520006a0bStron 		 * Go to the matching line.
161620006a0bStron 		 */
1617*838f5788Ssimonb 		if (lastlinepos != NULL_POSITION)
1618*838f5788Ssimonb 			jump_loc(lastlinepos, BOTTOM);
1619*838f5788Ssimonb 		else if (pos != opos)
162020006a0bStron 			jump_loc(pos, jump_sline);
162120006a0bStron 	}
162220006a0bStron 
162320006a0bStron #if HILITE_SEARCH
1624*838f5788Ssimonb 	if (hilite_search == OPT_ON || status_col)
162520006a0bStron 		/*
162620006a0bStron 		 * Display new hilites in the matching line.
162720006a0bStron 		 */
162820006a0bStron 		repaint_hilite(1);
162920006a0bStron #endif
163020006a0bStron 	return (0);
163120006a0bStron }
163220006a0bStron 
163320006a0bStron #if HILITE_SEARCH
163420006a0bStron /*
163520006a0bStron  * Prepare hilites in a given range of the file.
163620006a0bStron  *
163720006a0bStron  * The pair (prep_startpos,prep_endpos) delimits a contiguous region
163820006a0bStron  * of the file that has been "prepared"; that is, scanned for matches for
163920006a0bStron  * the current search pattern, and hilites have been created for such matches.
164020006a0bStron  * If prep_startpos == NULL_POSITION, the prep region is empty.
164120006a0bStron  * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
164220006a0bStron  * prep_hilite asks that the range (spos,epos) be covered by the prep region.
164320006a0bStron  */
prep_hilite(POSITION spos,POSITION epos,int maxlines)1644*838f5788Ssimonb public void prep_hilite(POSITION spos, POSITION epos, int maxlines)
164520006a0bStron {
164620006a0bStron 	POSITION nprep_startpos = prep_startpos;
164720006a0bStron 	POSITION nprep_endpos = prep_endpos;
164820006a0bStron 	POSITION new_epos;
164920006a0bStron 	POSITION max_epos;
165020006a0bStron 	int result;
165120006a0bStron 	int i;
165220006a0bStron 
165320006a0bStron /*
165420006a0bStron  * Search beyond where we're asked to search, so the prep region covers
165520006a0bStron  * more than we need.  Do one big search instead of a bunch of small ones.
165620006a0bStron  */
165720006a0bStron #define SEARCH_MORE (3*size_linebuf)
165820006a0bStron 
165920006a0bStron 	if (!prev_pattern(&search_info) && !is_filtering())
166020006a0bStron 		return;
166120006a0bStron 
166220006a0bStron 	/*
1663*838f5788Ssimonb 	 * Make sure our prep region always starts at the beginning of
1664*838f5788Ssimonb 	 * a line. (search_range takes care of the end boundary below.)
1665*838f5788Ssimonb 	 */
1666*838f5788Ssimonb 	spos = back_raw_line(spos+1, (char **)NULL, (int *)NULL);
1667*838f5788Ssimonb 
1668*838f5788Ssimonb 	/*
166920006a0bStron 	 * If we're limited to a max number of lines, figure out the
167020006a0bStron 	 * file position we should stop at.
167120006a0bStron 	 */
167220006a0bStron 	if (maxlines < 0)
167320006a0bStron 		max_epos = NULL_POSITION;
167420006a0bStron 	else
167520006a0bStron 	{
167620006a0bStron 		max_epos = spos;
167720006a0bStron 		for (i = 0;  i < maxlines;  i++)
167820006a0bStron 			max_epos = forw_raw_line(max_epos, (char **)NULL, (int *)NULL);
167920006a0bStron 	}
168020006a0bStron 
168120006a0bStron 	/*
168220006a0bStron 	 * Find two ranges:
168320006a0bStron 	 * The range that we need to search (spos,epos); and the range that
168420006a0bStron 	 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
168520006a0bStron 	 */
168620006a0bStron 
168720006a0bStron 	if (prep_startpos == NULL_POSITION ||
168820006a0bStron 	    (epos != NULL_POSITION && epos < prep_startpos) ||
168920006a0bStron 	    spos > prep_endpos)
169020006a0bStron 	{
169120006a0bStron 		/*
169220006a0bStron 		 * New range is not contiguous with old prep region.
169320006a0bStron 		 * Discard the old prep region and start a new one.
169420006a0bStron 		 */
169520006a0bStron 		clr_hilite();
169620006a0bStron 		clr_filter();
169720006a0bStron 		if (epos != NULL_POSITION)
169820006a0bStron 			epos += SEARCH_MORE;
169920006a0bStron 		nprep_startpos = spos;
170020006a0bStron 	} else
170120006a0bStron 	{
170220006a0bStron 		/*
170320006a0bStron 		 * New range partially or completely overlaps old prep region.
170420006a0bStron 		 */
170520006a0bStron 		if (epos == NULL_POSITION)
170620006a0bStron 		{
170720006a0bStron 			/*
170820006a0bStron 			 * New range goes to end of file.
170920006a0bStron 			 */
171020006a0bStron 			;
171120006a0bStron 		} else if (epos > prep_endpos)
171220006a0bStron 		{
171320006a0bStron 			/*
171420006a0bStron 			 * New range ends after old prep region.
171520006a0bStron 			 * Extend prep region to end at end of new range.
171620006a0bStron 			 */
171720006a0bStron 			epos += SEARCH_MORE;
171820006a0bStron 		} else /* (epos <= prep_endpos) */
171920006a0bStron 		{
172020006a0bStron 			/*
172120006a0bStron 			 * New range ends within old prep region.
172220006a0bStron 			 * Truncate search to end at start of old prep region.
172320006a0bStron 			 */
172420006a0bStron 			epos = prep_startpos;
172520006a0bStron 		}
172620006a0bStron 
172720006a0bStron 		if (spos < prep_startpos)
172820006a0bStron 		{
172920006a0bStron 			/*
173020006a0bStron 			 * New range starts before old prep region.
173120006a0bStron 			 * Extend old prep region backwards to start at
173220006a0bStron 			 * start of new range.
173320006a0bStron 			 */
173420006a0bStron 			if (spos < SEARCH_MORE)
173520006a0bStron 				spos = 0;
173620006a0bStron 			else
173720006a0bStron 				spos -= SEARCH_MORE;
173820006a0bStron 			nprep_startpos = spos;
173920006a0bStron 		} else /* (spos >= prep_startpos) */
174020006a0bStron 		{
174120006a0bStron 			/*
174220006a0bStron 			 * New range starts within or after old prep region.
174320006a0bStron 			 * Trim search to start at end of old prep region.
174420006a0bStron 			 */
174520006a0bStron 			spos = prep_endpos;
174620006a0bStron 		}
174720006a0bStron 	}
174820006a0bStron 
174920006a0bStron 	if (epos != NULL_POSITION && max_epos != NULL_POSITION &&
175020006a0bStron 	    epos > max_epos)
175120006a0bStron 		/*
175220006a0bStron 		 * Don't go past the max position we're allowed.
175320006a0bStron 		 */
175420006a0bStron 		epos = max_epos;
175520006a0bStron 
175620006a0bStron 	if (epos == NULL_POSITION || epos > spos)
175720006a0bStron 	{
175820006a0bStron 		int search_type = SRCH_FORW | SRCH_FIND_ALL;
1759*838f5788Ssimonb 		search_type |= (search_info.search_type & (SRCH_NO_REGEX|SRCH_SUBSEARCH_ALL));
1760*838f5788Ssimonb 		for (;;)
1761*838f5788Ssimonb 		{
1762*838f5788Ssimonb 			result = search_range(spos, epos, search_type, 0, maxlines, (POSITION*)NULL, &new_epos, (POSITION*)NULL);
176320006a0bStron 			if (result < 0)
176420006a0bStron 				return;
176520006a0bStron 			if (prep_endpos == NULL_POSITION || new_epos > prep_endpos)
176620006a0bStron 				nprep_endpos = new_epos;
1767*838f5788Ssimonb 
1768*838f5788Ssimonb 			/*
1769*838f5788Ssimonb 			 * Check both ends of the resulting prep region to
1770*838f5788Ssimonb 			 * make sure they're not filtered. If they are,
1771*838f5788Ssimonb 			 * keep going at least one more line until we find
1772*838f5788Ssimonb 			 * something that isn't filtered, or hit the end.
1773*838f5788Ssimonb 			 */
1774*838f5788Ssimonb 			if (prep_endpos == NULL_POSITION || nprep_endpos > prep_endpos)
1775*838f5788Ssimonb 			{
1776*838f5788Ssimonb 				if (new_epos >= nprep_endpos && is_filtered(new_epos-1))
1777*838f5788Ssimonb 				{
1778*838f5788Ssimonb 					spos = nprep_endpos;
1779*838f5788Ssimonb 					epos = forw_raw_line(nprep_endpos, (char **)NULL, (int *)NULL);
1780*838f5788Ssimonb 					if (epos == NULL_POSITION)
1781*838f5788Ssimonb 						break;
1782*838f5788Ssimonb 					maxlines = 1;
1783*838f5788Ssimonb 					continue;
1784*838f5788Ssimonb 				}
1785*838f5788Ssimonb 			}
1786*838f5788Ssimonb 
1787*838f5788Ssimonb 			if (prep_startpos == NULL_POSITION || nprep_startpos < prep_startpos)
1788*838f5788Ssimonb 			{
1789*838f5788Ssimonb 				if (nprep_startpos > 0 && is_filtered(nprep_startpos))
1790*838f5788Ssimonb 				{
1791*838f5788Ssimonb 					epos = nprep_startpos;
1792*838f5788Ssimonb 					spos = back_raw_line(nprep_startpos, (char **)NULL, (int *)NULL);
1793*838f5788Ssimonb 					if (spos == NULL_POSITION)
1794*838f5788Ssimonb 						break;
1795*838f5788Ssimonb 					nprep_startpos = spos;
1796*838f5788Ssimonb 					maxlines = 1;
1797*838f5788Ssimonb 					continue;
1798*838f5788Ssimonb 				}
1799*838f5788Ssimonb 			}
1800*838f5788Ssimonb 			break;
1801*838f5788Ssimonb 		}
180220006a0bStron 	}
180320006a0bStron 	prep_startpos = nprep_startpos;
180420006a0bStron 	prep_endpos = nprep_endpos;
180520006a0bStron }
180620006a0bStron 
180720006a0bStron /*
180820006a0bStron  * Set the pattern to be used for line filtering.
180920006a0bStron  */
set_filter_pattern(char * pattern,int search_type)1810*838f5788Ssimonb public void set_filter_pattern(char *pattern, int search_type)
181120006a0bStron {
1812*838f5788Ssimonb 	struct pattern_info *filter;
1813*838f5788Ssimonb 
181420006a0bStron 	clr_filter();
181520006a0bStron 	if (pattern == NULL || *pattern == '\0')
1816*838f5788Ssimonb 	{
1817*838f5788Ssimonb 		/* Clear and free all filters. */
1818*838f5788Ssimonb 		for (filter = filter_infos; filter != NULL; )
1819*838f5788Ssimonb 		{
1820*838f5788Ssimonb 			struct pattern_info *next_filter = filter->next;
1821*838f5788Ssimonb 			clear_pattern(filter);
1822*838f5788Ssimonb 			free(filter);
1823*838f5788Ssimonb 			filter = next_filter;
1824*838f5788Ssimonb 		}
1825*838f5788Ssimonb 		filter_infos = NULL;
1826*838f5788Ssimonb 	} else
1827*838f5788Ssimonb 	{
1828*838f5788Ssimonb 		/* Create a new filter and add it to the filter_infos list. */
1829*838f5788Ssimonb 		filter = ecalloc(1, sizeof(struct pattern_info));
1830*838f5788Ssimonb 		init_pattern(filter);
1831*838f5788Ssimonb 		if (set_pattern(filter, pattern, search_type, 1) < 0)
1832*838f5788Ssimonb 		{
1833*838f5788Ssimonb 			free(filter);
1834*838f5788Ssimonb 			return;
1835*838f5788Ssimonb 		}
1836*838f5788Ssimonb 		filter->next = filter_infos;
1837*838f5788Ssimonb 		filter_infos = filter;
1838*838f5788Ssimonb 	}
183920006a0bStron 	screen_trashed = 1;
184020006a0bStron }
184120006a0bStron 
184220006a0bStron /*
184320006a0bStron  * Is there a line filter in effect?
184420006a0bStron  */
is_filtering(void)1845*838f5788Ssimonb public int is_filtering(void)
184620006a0bStron {
184720006a0bStron 	if (ch_getflags() & CH_HELPFILE)
184820006a0bStron 		return (0);
1849*838f5788Ssimonb 	return (filter_infos != NULL);
185020006a0bStron }
185120006a0bStron #endif
185220006a0bStron 
185320006a0bStron #if HAVE_V8_REGCOMP
185420006a0bStron /*
185520006a0bStron  * This function is called by the V8 regcomp to report
185620006a0bStron  * errors in regular expressions.
185720006a0bStron  */
1858*838f5788Ssimonb public int reg_show_error = 1;
1859*838f5788Ssimonb 
regerror(char * s)1860*838f5788Ssimonb void regerror(char *s)
186120006a0bStron {
186220006a0bStron 	PARG parg;
186320006a0bStron 
1864*838f5788Ssimonb 	if (!reg_show_error)
1865*838f5788Ssimonb 		return;
186620006a0bStron 	parg.p_string = s;
186720006a0bStron 	error("%s", &parg);
186820006a0bStron }
186920006a0bStron #endif
187020006a0bStron 
1871