Deleted Added
sdiff udiff text old ( 191930 ) new ( 195941 )
full compact
1/* $FreeBSD: head/contrib/less/search.c 195941 2009-07-29 09:20:32Z delphij $ */
2/*
3 * Copyright (C) 1984-2009 Mark Nudelman
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Less License, as specified in the README file.
7 *
8 * For more information about less, or for information on how to
9 * contact the author, see the README file.
10 */
11
12
13/*
14 * Routines to search a file for a pattern.
15 */
16
17#include "less.h"
18#include "pattern.h"
19#include "position.h"
20#include "charset.h"
21
22#define MINPOS(a,b) (((a) < (b)) ? (a) : (b))
23#define MAXPOS(a,b) (((a) > (b)) ? (a) : (b))
24
25extern int sigs;
26extern int how_search;
27extern int caseless;
28extern int linenums;
29extern int sc_height;
30extern int jump_sline;
31extern int bs_mode;
32extern int less_is_more;

--- 5 unchanged lines hidden (view full) ---

38extern int utf_mode;
39extern int screen_trashed;
40#if HILITE_SEARCH
41extern int hilite_search;
42extern int size_linebuf;
43extern int squished;
44extern int can_goto_line;
45static int hide_hilite;
46static POSITION prep_startpos;
47static POSITION prep_endpos;
48static int is_caseless;
49static int is_ucase_pattern;
50
51struct hilite
52{
53 struct hilite *hl_next;
54 POSITION hl_startpos;
55 POSITION hl_endpos;
56};
57static struct hilite hilite_anchor = { NULL, NULL_POSITION, NULL_POSITION };
58static struct hilite filter_anchor = { NULL, NULL_POSITION, NULL_POSITION };
59#define hl_first hl_next
60#endif
61
62/*
63 * These are the static variables that represent the "remembered"
64 * search pattern and filter pattern.
65 */
66struct pattern_info {
67 DEFINE_PATTERN(compiled);
68 char* text;
69 int search_type;
70};
71
72static struct pattern_info search_info;
73static struct pattern_info filter_info;
74
75/*
76 * Compile and save a search pattern.
77 */
78 static int
79set_pattern(info, pattern, search_type)
80 struct pattern_info *info;
81 char *pattern;
82 int search_type;
83{
84 if (pattern == NULL)
85 CLEAR_PATTERN(search_info.compiled);
86 else if (compile_pattern(pattern, search_type, &info->compiled) < 0)
87 return -1;
88 /* Pattern compiled successfully; save the text too. */
89 if (info->text != NULL)
90 free(info->text);
91 info->text = NULL;
92 if (pattern != NULL)
93 {
94 info->text = (char *) ecalloc(1, strlen(pattern)+1);
95 strcpy(info->text, pattern);
96 }
97 info->search_type = search_type;
98 return 0;
99}
100
101/*
102 * Discard a saved pattern.
103 */
104 static void
105clear_pattern(info)
106 struct pattern_info *info;
107{
108 if (info->text != NULL)
109 free(info->text);
110 info->text = NULL;
111 uncompile_pattern(&info->compiled);
112}
113
114/*
115 * Initialize saved pattern to nothing.
116 */
117 static void
118init_pattern(info)
119 struct pattern_info *info;
120{
121 CLEAR_PATTERN(info->compiled);
122 info->text = NULL;
123 info->search_type = 0;
124}
125
126/*
127 * Initialize search variables.
128 */
129 public void
130init_search()
131{
132 init_pattern(&search_info);
133 init_pattern(&filter_info);
134}
135
136/*
137 * Determine which text conversions to perform before pattern matching.
138 */
139 static int
140get_cvt_ops()
141{
142 int ops = 0;
143 if (is_caseless || bs_mode == BS_SPECIAL)
144 {
145 if (is_caseless)

--- 29 unchanged lines hidden (view full) ---

175 }
176 return (0);
177}
178
179/*
180 * Is there a previous (remembered) search pattern?
181 */
182 static int
183prev_pattern(info)
184 struct pattern_info *info;
185{
186 if (info->search_type & SRCH_NO_REGEX)
187 return (info->text != NULL);
188 return (!is_null_pattern(info->compiled));
189}
190
191#if HILITE_SEARCH
192/*
193 * Repaint the hilites currently displayed on the screen.
194 * Repaint each line which contains highlighted text.
195 * If on==0, force all hilites off.
196 */

--- 25 unchanged lines hidden (view full) ---

222 }
223
224 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
225 {
226 pos = position(slinenum);
227 if (pos == NULL_POSITION)
228 continue;
229 epos = position(slinenum+1);
230 (void) forw_line(pos);
231 goto_line(slinenum);
232 put_line();
233 }
234 lower_left(); // if !oldbot
235 hide_hilite = save_hide_hilite;
236}
237
238/*
239 * Clear the attn hilite.
240 */
241 public void
242clear_attn()

--- 40 unchanged lines hidden (view full) ---

283#endif
284
285/*
286 * Hide search string highlighting.
287 */
288 public void
289undo_search()
290{
291 if (!prev_pattern(&search_info))
292 {
293 error("No previous regular expression", NULL_PARG);
294 return;
295 }
296#if HILITE_SEARCH
297 hide_hilite = !hide_hilite;
298 repaint_hilite(1);
299#endif
300}
301
302#if HILITE_SEARCH
303/*
304 * Clear the hilite list.
305 */
306 public void
307clr_hlist(anchor)
308 struct hilite *anchor;
309{

--- 153 unchanged lines hidden (view full) ---

463 free(hl);
464 return;
465 }
466 hl->hl_next = ihl->hl_next;
467 ihl->hl_next = hl;
468}
469
470/*
471 * Make a hilite for each string in a physical line which matches
472 * the current pattern.
473 * sp,ep delimit the first match already found.
474 */
475 static void
476hilite_line(linepos, line, line_len, chpos, sp, ep, cvt_ops)
477 POSITION linepos;
478 char *line;
479 int line_len;
480 int *chpos;
481 char *sp;
482 char *ep;
483 int cvt_ops;
484{
485 char *searchp;
486 char *line_end = line + line_len;
487 struct hilite *hl;
488
489 if (sp == NULL || ep == NULL)
490 return;
491 /*
492 * sp and ep delimit the first match in the line.
493 * Mark the corresponding file positions, then
494 * look for further matches and mark them.
495 * {{ This technique, of calling match_pattern on subsequent
496 * substrings of the line, may mark more than is correct
497 * if the pattern starts with "^". This bug is fixed
498 * for those regex functions that accept a notbol parameter
499 * (currently POSIX, PCRE and V8-with-regexec2). }}
500 */
501 searchp = line;
502 do {
503 if (ep > sp)
504 {
505 hl = (struct hilite *) ecalloc(1, sizeof(struct hilite));
506 hl->hl_startpos = linepos + chpos[sp-line];
507 hl->hl_endpos = linepos + chpos[ep-line];
508 add_hilite(&hilite_anchor, hl);
509 }
510 /*
511 * If we matched more than zero characters,
512 * move to the first char after the string we matched.
513 * If we matched zero, just move to the next char.
514 */
515 if (ep > searchp)
516 searchp = ep;
517 else if (searchp != line_end)
518 searchp++;
519 else /* end of line */
520 break;
521 } while (match_pattern(search_info.compiled, search_info.text,
522 searchp, line_end - searchp, &sp, &ep, 1, search_info.search_type));
523}
524#endif
525
526/*
527 * Change the caseless-ness of searches.
528 * Updates the internal search state to reflect a change in the -i flag.
529 */
530 public void

--- 5 unchanged lines hidden (view full) ---

536 * Just set the search caselessness to the global caselessness.
537 */
538 is_caseless = caseless;
539 else
540 /*
541 * Pattern did have uppercase.
542 * Discard the pattern; we can't change search caselessness now.
543 */
544 clear_pattern(&search_info);
545}
546
547#if HILITE_SEARCH
548/*
549 * Find matching text which is currently on screen and highlight it.
550 */
551 static void
552hilite_screen()

--- 116 unchanged lines hidden (view full) ---

669{
670 char *line;
671 char *cline;
672 int line_len;
673 LINENUM linenum;
674 char *sp, *ep;
675 int line_match;
676 int cvt_ops;
677 int cvt_len;
678 int *chpos;
679 POSITION linepos, oldpos;
680
681 linenum = find_linenum(pos);
682 oldpos = pos;
683 for (;;)
684 {
685 /*
686 * Get lines until we find a matching one or until

--- 55 unchanged lines hidden (view full) ---

742 /*
743 * If we're using line numbers, we might as well
744 * remember the information we have now (the position
745 * and line number of the current line).
746 * Don't do it for every line because it slows down
747 * the search. Remember the line number only if
748 * we're "far" from the last place we remembered it.
749 */
750 if (linenums && abs((int)(pos - oldpos)) > 2048)
751 add_lnum(linenum, pos);
752 oldpos = pos;
753
754 if (is_filtered(linepos))
755 continue;
756
757 /*
758 * If it's a caseless search, convert the line to lowercase.
759 * If we're doing backspace processing, delete backspaces.
760 */
761 cvt_ops = get_cvt_ops();
762 cvt_len = cvt_length(line_len, cvt_ops);
763 cline = (char *) ecalloc(1, cvt_len);
764 chpos = cvt_alloc_chpos(cvt_len);
765 cvt_text(cline, line, chpos, &line_len, cvt_ops);
766
767#if HILITE_SEARCH
768 /*
769 * Check to see if the line matches the filter pattern.
770 * If so, add an entry to the filter list.
771 */
772 if ((search_type & SRCH_FIND_ALL) && prev_pattern(&filter_info)) {
773 int line_filter = match_pattern(filter_info.compiled, filter_info.text,
774 cline, line_len, &sp, &ep, 0, filter_info.search_type);
775 if (line_filter)
776 {
777 struct hilite *hl = (struct hilite *)
778 ecalloc(1, sizeof(struct hilite));
779 hl->hl_startpos = linepos;
780 hl->hl_endpos = pos;
781 add_hilite(&filter_anchor, hl);
782 }
783 }
784#endif
785
786 /*
787 * Test the next line to see if we have a match.
788 * We are successful if we either want a match and got one,
789 * or if we want a non-match and got one.
790 */
791 if (prev_pattern(&search_info))
792 {
793 line_match = match_pattern(search_info.compiled, search_info.text,
794 cline, line_len, &sp, &ep, 0, search_type); //FIXME search_info.search_type
795 if (line_match)
796 {
797 /*
798 * Got a match.
799 */
800 if (search_type & SRCH_FIND_ALL)
801 {
802#if HILITE_SEARCH
803 /*
804 * We are supposed to find all matches in the range.
805 * Just add the matches in this line to the
806 * hilite list and keep searching.
807 */
808 hilite_line(linepos, cline, line_len, chpos, sp, ep, cvt_ops);
809#endif
810 } else if (--matches <= 0)
811 {
812 /*
813 * Found the one match we're looking for.
814 * Return it.
815 */
816#if HILITE_SEARCH
817 if (hilite_search == OPT_ON)
818 {
819 /*
820 * Clear the hilite list and add only
821 * the matches in this one line.
822 */
823 clr_hilite();
824 hilite_line(linepos, cline, line_len, chpos, sp, ep, cvt_ops);
825 }
826#endif
827 free(cline);
828 free(chpos);
829 if (plinepos != NULL)
830 *plinepos = linepos;
831 return (0);
832 }
833 }
834 }
835 free(cline);
836 free(chpos);
837 }
838}
839
840/*
841 * search for a pattern in history. If found, compile that pattern.
842 */
843 static int
844hist_pattern(search_type)
845 int search_type;
846{
847#if CMD_HISTORY
848 char *pattern;
849
850 set_mlist(ml_search, 0);
851 pattern = cmd_lastpattern();
852 if (pattern == NULL)
853 return (0);
854
855 if (set_pattern(&search_info, pattern, search_type) < 0)
856 return (0);
857
858 is_ucase_pattern = is_ucase(pattern);
859 if (is_ucase_pattern && caseless != OPT_ONPLUS)
860 is_caseless = 0;
861 else
862 is_caseless = caseless;
863

--- 25 unchanged lines hidden (view full) ---

889{
890 POSITION pos;
891
892 if (pattern == NULL || *pattern == '\0')
893 {
894 /*
895 * A null pattern means use the previously compiled pattern.
896 */
897 if (!prev_pattern(&search_info) && !hist_pattern(search_type))
898 {
899 error("No previous regular expression", NULL_PARG);
900 return (-1);
901 }
902 if ((search_type & SRCH_NO_REGEX) !=
903 (search_info.search_type & SRCH_NO_REGEX))
904 {
905 error("Please re-enter search pattern", NULL_PARG);
906 return -1;
907 }
908#if HILITE_SEARCH
909 if (hilite_search == OPT_ON)
910 {
911 /*

--- 13 unchanged lines hidden (view full) ---

925 }
926 hide_hilite = 0;
927#endif
928 } else
929 {
930 /*
931 * Compile the pattern.
932 */
933 if (set_pattern(&search_info, pattern, search_type) < 0)
934 return (-1);
935 /*
936 * Ignore case if -I is set OR
937 * -i is set AND the pattern is all lowercase.
938 */
939 is_ucase_pattern = is_ucase(pattern);
940 if (is_ucase_pattern && caseless != OPT_ONPLUS)
941 is_caseless = 0;

--- 91 unchanged lines hidden (view full) ---

1033 int maxlines;
1034{
1035 POSITION nprep_startpos = prep_startpos;
1036 POSITION nprep_endpos = prep_endpos;
1037 POSITION new_epos;
1038 POSITION max_epos;
1039 int result;
1040 int i;
1041
1042/*
1043 * Search beyond where we're asked to search, so the prep region covers
1044 * more than we need. Do one big search instead of a bunch of small ones.
1045 */
1046#define SEARCH_MORE (3*size_linebuf)
1047
1048 if (!prev_pattern(&search_info) && !is_filtering())
1049 return;
1050
1051 /*
1052 * If we're limited to a max number of lines, figure out the
1053 * file position we should stop at.
1054 */
1055 if (maxlines < 0)
1056 max_epos = NULL_POSITION;

--- 76 unchanged lines hidden (view full) ---

1133 epos > max_epos)
1134 /*
1135 * Don't go past the max position we're allowed.
1136 */
1137 epos = max_epos;
1138
1139 if (epos == NULL_POSITION || epos > spos)
1140 {
1141 int search_type = SRCH_FORW | SRCH_FIND_ALL;
1142 search_type |= (search_info.search_type & SRCH_NO_REGEX);
1143 result = search_range(spos, epos, search_type, 0,
1144 maxlines, (POSITION*)NULL, &new_epos);
1145 if (result < 0)
1146 return;
1147 if (prep_endpos == NULL_POSITION || new_epos > prep_endpos)
1148 nprep_endpos = new_epos;
1149 }
1150 prep_startpos = nprep_startpos;
1151 prep_endpos = nprep_endpos;

--- 4 unchanged lines hidden (view full) ---

1156 */
1157 public void
1158set_filter_pattern(pattern, search_type)
1159 char *pattern;
1160 int search_type;
1161{
1162 clr_filter();
1163 if (pattern == NULL || *pattern == '\0')
1164 clear_pattern(&filter_info);
1165 else
1166 set_pattern(&filter_info, pattern, search_type);
1167 screen_trashed = 1;
1168}
1169
1170/*
1171 * Is there a line filter in effect?
1172 */
1173 public int
1174is_filtering()
1175{
1176 if (ch_getflags() & CH_HELPFILE)
1177 return (0);
1178 return prev_pattern(&filter_info);
1179}
1180#endif
1181
1182#if HAVE_V8_REGCOMP
1183/*
1184 * This function is called by the V8 regcomp to report
1185 * errors in regular expressions.
1186 */
1187 void
1188regerror(s)
1189 char *s;
1190{
1191 PARG parg;
1192
1193 parg.p_string = s;
1194 error("%s", &parg);
1195}
1196#endif
1197