2 * Copyright (C) 1984-2007 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
13 * Routines to search a file for a pattern.
19 #define MINPOS(a,b) (((a) < (b)) ? (a) : (b))
20 #define MAXPOS(a,b) (((a) > (b)) ? (a) : (b))
22 #if HAVE_POSIX_REGCOMP
25 #define REGCOMP_FLAG REG_EXTENDED
27 #define REGCOMP_FLAG 0
49 extern int how_search
;
53 extern int jump_sline
;
56 extern int status_col
;
57 extern void * constant ml_search
;
58 extern POSITION start_attnpos
;
59 extern POSITION end_attnpos
;
61 extern int hilite_search
;
62 extern int screen_trashed
;
63 extern int size_linebuf
;
65 extern int can_goto_line
;
66 static int hide_hilite
;
68 static POSITION prep_startpos
;
69 static POSITION prep_endpos
;
73 struct hilite
*hl_next
;
77 static struct hilite hilite_anchor
= { NULL
, NULL_POSITION
, NULL_POSITION
};
78 #define hl_first hl_next
82 * These are the static variables that represent the "remembered"
85 #if HAVE_POSIX_REGCOMP
86 static regex_t
*regpattern
= NULL
;
89 pcre
*regpattern
= NULL
;
95 static char *cpattern
= NULL
;
98 static struct regexp
*regpattern
= NULL
;
101 static int is_caseless
;
102 static int is_ucase_pattern
;
103 static int last_search_type
;
104 static char *last_pattern
= NULL
;
107 * Convert text. Perform one or more of these transformations:
109 #define CVT_TO_LC 01 /* Convert upper-case to lower-case */
110 #define CVT_BS 02 /* Do backspace processing */
111 #define CVT_CRLF 04 /* Remove CR after LF */
112 #define CVT_ANSI 010 /* Remove ANSI escape sequences */
115 cvt_text(odst
, osrc
, lenp
, ops
)
123 register char *src_end
;
126 src_end
= osrc
+ *lenp
;
128 src_end
= osrc
+ strlen(osrc
);
130 for (src
= osrc
, dst
= odst
; src
< src_end
; src
++)
132 if ((ops
& CVT_TO_LC
) && IS_UPPER(*src
))
133 /* Convert uppercase to lowercase. */
134 *dst
++ = TO_LOWER(*src
);
135 else if ((ops
& CVT_BS
) && *src
== '\b' && dst
> odst
)
136 /* Delete BS and preceding char. */
138 else if ((ops
& CVT_ANSI
) && *src
== ESC
)
140 /* Skip to end of ANSI escape sequence. */
141 while (src
+ 1 != src_end
)
142 if (!is_ansi_middle(*++src
))
148 if ((ops
& CVT_CRLF
) && dst
> odst
&& dst
[-1] == '\r')
156 * Determine which conversions to perform.
162 if (is_caseless
|| bs_mode
== BS_SPECIAL
)
166 if (bs_mode
== BS_SPECIAL
)
168 if (bs_mode
!= BS_CONTROL
)
170 } else if (bs_mode
!= BS_CONTROL
)
174 if (ctldisp
== OPT_ONPLUS
)
180 * Are there any uppercase letters in this string?
188 for (p
= s
; *p
!= '\0'; p
++)
195 * Is there a previous (remembered) search pattern?
200 if (last_search_type
& SRCH_NO_REGEX
)
201 return (last_pattern
!= NULL
);
202 #if HAVE_POSIX_REGCOMP
203 return (regpattern
!= NULL
);
206 return (regpattern
!= NULL
);
209 return (re_pattern
!= 0);
212 return (cpattern
!= NULL
);
215 return (regpattern
!= NULL
);
218 return (last_pattern
!= NULL
);
224 * Repaint the hilites currently displayed on the screen.
225 * Repaint each line which contains highlighted text.
226 * If on==0, force all hilites off.
235 int save_hide_hilite
;
240 save_hide_hilite
= hide_hilite
;
251 hide_hilite
= save_hide_hilite
;
255 for (slinenum
= TOP
; slinenum
< TOP
+ sc_height
-1; slinenum
++)
257 pos
= position(slinenum
);
258 if (pos
== NULL_POSITION
)
260 epos
= position(slinenum
+1);
263 * If any character in the line is highlighted,
266 * {{ This doesn't work -- if line is drawn with highlights
267 * which should be erased (e.g. toggle -i with status column),
268 * we must redraw the line even if it has no highlights.
269 * For now, just repaint every line. }}
271 if (is_hilited(pos
, epos
, 1, NULL
))
274 (void) forw_line(pos
);
281 hide_hilite
= save_hide_hilite
;
285 * Clear the attn hilite.
291 POSITION old_start_attnpos
;
292 POSITION old_end_attnpos
;
296 if (start_attnpos
== NULL_POSITION
)
298 old_start_attnpos
= start_attnpos
;
299 old_end_attnpos
= end_attnpos
;
300 start_attnpos
= end_attnpos
= NULL_POSITION
;
310 for (slinenum
= TOP
; slinenum
< TOP
+ sc_height
-1; slinenum
++)
312 pos
= position(slinenum
);
313 if (pos
== NULL_POSITION
)
315 epos
= position(slinenum
+1);
316 if (pos
< old_end_attnpos
&&
317 (epos
== NULL_POSITION
|| epos
> old_start_attnpos
))
319 (void) forw_line(pos
);
328 * Hide search string highlighting.
335 error("No previous regular expression", NULL_PARG
);
339 hide_hilite
= !hide_hilite
;
345 * Compile a search pattern, for future use by match_pattern.
348 compile_pattern(pattern
, search_type
)
352 if ((search_type
& SRCH_NO_REGEX
) == 0)
354 #if HAVE_POSIX_REGCOMP
355 regex_t
*s
= (regex_t
*) ecalloc(1, sizeof(regex_t
));
356 if (regcomp(s
, pattern
, REGCOMP_FLAG
))
359 error("Invalid pattern", NULL_PARG
);
362 if (regpattern
!= NULL
)
368 const char *errstring
;
371 comp
= pcre_compile(pattern
, 0,
372 &errstring
, &erroffset
, NULL
);
375 parg
.p_string
= (char *) errstring
;
383 if ((parg
.p_string
= re_comp(pattern
)) != NULL
)
392 if ((s
= regcmp(pattern
, 0)) == NULL
)
394 error("Invalid pattern", NULL_PARG
);
397 if (cpattern
!= NULL
)
403 if ((s
= regcomp(pattern
)) == NULL
)
406 * regcomp has already printed an error message
411 if (regpattern
!= NULL
)
417 if (last_pattern
!= NULL
)
419 last_pattern
= (char *) calloc(1, strlen(pattern
)+1);
420 if (last_pattern
!= NULL
)
421 strcpy(last_pattern
, pattern
);
423 last_search_type
= search_type
;
428 * Forget that we have a compiled pattern.
433 #if HAVE_POSIX_REGCOMP
434 if (regpattern
!= NULL
)
439 if (regpattern
!= NULL
)
440 pcre_free(regpattern
);
447 if (cpattern
!= NULL
)
452 if (regpattern
!= NULL
)
460 * Perform a pattern match with the previously compiled pattern.
461 * Set sp and ep to the start and end of the matched string.
464 match_pattern(line
, line_len
, sp
, ep
, notbol
)
473 if (last_search_type
& SRCH_NO_REGEX
)
474 return (match(last_pattern
, strlen(last_pattern
), line
, line_len
, sp
, ep
));
476 #if HAVE_POSIX_REGCOMP
479 int flags
= (notbol
) ? REG_NOTBOL
: 0;
480 matched
= !regexec(regpattern
, line
, 1, &rm
, flags
);
484 *sp
= line
+ rm
.rm_so
;
485 *ep
= line
+ rm
.rm_eo
;
494 int flags
= (notbol
) ? PCRE_NOTBOL
: 0;
496 matched
= pcre_exec(regpattern
, NULL
, line
, line_len
,
497 0, flags
, ovector
, 3) >= 0;
500 *sp
= line
+ ovector
[0];
501 *ep
= line
+ ovector
[1];
505 matched
= (re_exec(line
) == 1);
507 * re_exec doesn't seem to provide a way to get the matched string.
512 *ep
= regex(cpattern
, line
);
513 matched
= (*ep
!= NULL
);
520 matched
= regexec2(regpattern
, line
, notbol
);
522 matched
= regexec(regpattern
, line
);
526 *sp
= regpattern
->startp
[0];
527 *ep
= regpattern
->endp
[0];
530 matched
= match(last_pattern
, strlen(last_pattern
), line
, line_len
, sp
, ep
);
537 * Clear the hilite list.
543 struct hilite
*nexthl
;
545 for (hl
= hilite_anchor
.hl_first
; hl
!= NULL
; hl
= nexthl
)
547 nexthl
= hl
->hl_next
;
550 hilite_anchor
.hl_first
= NULL
;
551 prep_startpos
= prep_endpos
= NULL_POSITION
;
555 * Should any characters in a specified range be highlighted?
558 is_hilited_range(pos
, epos
)
565 * Look at each highlight and see if any part of it falls in the range.
567 for (hl
= hilite_anchor
.hl_first
; hl
!= NULL
; hl
= hl
->hl_next
)
569 if (hl
->hl_endpos
> pos
&&
570 (epos
== NULL_POSITION
|| epos
> hl
->hl_startpos
))
577 * Should any characters in a specified range be highlighted?
578 * If nohide is nonzero, don't consider hide_hilite.
581 is_hilited(pos
, epos
, nohide
, p_matches
)
589 if (p_matches
!= NULL
)
593 start_attnpos
!= NULL_POSITION
&&
595 (epos
== NULL_POSITION
|| epos
> start_attnpos
))
597 * The attn line overlaps this range.
601 match
= is_hilited_range(pos
, epos
);
605 if (p_matches
!= NULL
)
607 * Report matches, even if we're hiding highlights.
611 if (hilite_search
== 0)
613 * Not doing highlighting.
617 if (!nohide
&& hide_hilite
)
619 * Highlighting is hidden.
627 * Add a new hilite to a hilite list.
630 add_hilite(anchor
, hl
)
631 struct hilite
*anchor
;
637 * Hilites are sorted in the list; find where new one belongs.
638 * Insert new one after ihl.
640 for (ihl
= anchor
; ihl
->hl_next
!= NULL
; ihl
= ihl
->hl_next
)
642 if (ihl
->hl_next
->hl_startpos
> hl
->hl_startpos
)
647 * Truncate hilite so it doesn't overlap any existing ones
648 * above and below it.
651 hl
->hl_startpos
= MAXPOS(hl
->hl_startpos
, ihl
->hl_endpos
);
652 if (ihl
->hl_next
!= NULL
)
653 hl
->hl_endpos
= MINPOS(hl
->hl_endpos
, ihl
->hl_next
->hl_startpos
);
654 if (hl
->hl_startpos
>= hl
->hl_endpos
)
657 * Hilite was truncated out of existence.
662 hl
->hl_next
= ihl
->hl_next
;
667 adj_hilite_ansi(cvt_ops
, line
, line_len
, npos
)
673 char *line_end
= *line
+ line_len
;
675 if (cvt_ops
& CVT_ANSI
)
676 while (**line
== ESC
)
679 * Found an ESC. The file position moves
680 * forward past the entire ANSI escape sequence.
684 while (*line
< line_end
)
687 if (!is_ansi_middle(*(*line
)++))
694 * Adjust hl_startpos & hl_endpos to account for backspace processing.
697 adj_hilite(anchor
, linepos
, cvt_ops
)
698 struct hilite
*anchor
;
711 * The line was already scanned and hilites were added (in hilite_line).
712 * But it was assumed that each char position in the line
713 * correponds to one char position in the file.
714 * This may not be true if there are backspaces in the line.
715 * Get the raw line again. Look at each character.
717 (void) forw_raw_line(linepos
, &line
, &line_len
);
718 line_end
= line
+ line_len
;
719 opos
= npos
= linepos
;
720 hl
= anchor
->hl_first
;
725 * See if we need to adjust the current hl_startpos or
726 * hl_endpos. After adjusting startpos[i], move to endpos[i].
727 * After adjusting endpos[i], move to startpos[i+1].
728 * The hilite list must be sorted thus:
729 * startpos[0] < endpos[0] <= startpos[1] < endpos[1] <= etc.
731 if (checkstart
&& hl
->hl_startpos
== opos
)
733 hl
->hl_startpos
= npos
;
735 continue; /* {{ not really necessary }} */
736 } else if (!checkstart
&& hl
->hl_endpos
== opos
)
738 hl
->hl_endpos
= npos
;
741 continue; /* {{ necessary }} */
743 if (line
== line_end
)
745 adj_hilite_ansi(cvt_ops
, &line
, line_end
- line
, &npos
);
749 if (cvt_ops
& CVT_BS
)
751 while (*line
== '\b')
755 adj_hilite_ansi(cvt_ops
, &line
, line_end
- line
, &npos
);
756 if (line
== line_end
)
763 * Found a backspace. The file position moves
764 * forward by 2 relative to the processed line
765 * which was searched in hilite_line.
775 * Make a hilite for each string in a physical line which matches
776 * the current pattern.
777 * sp,ep delimit the first match already found.
780 hilite_line(linepos
, line
, line_len
, sp
, ep
, cvt_ops
)
789 char *line_end
= line
+ line_len
;
791 struct hilite hilites
;
793 if (sp
== NULL
|| ep
== NULL
)
796 * sp and ep delimit the first match in the line.
797 * Mark the corresponding file positions, then
798 * look for further matches and mark them.
799 * {{ This technique, of calling match_pattern on subsequent
800 * substrings of the line, may mark more than is correct
801 * if the pattern starts with "^". This bug is fixed
802 * for those regex functions that accept a notbol parameter
803 * (currently POSIX, PCRE and V8-with-regexec2). }}
807 * Put the hilites into a temporary list until they're adjusted.
809 hilites
.hl_first
= NULL
;
814 * Assume that each char position in the "line"
815 * buffer corresponds to one char position in the file.
816 * This is not quite true; we need to adjust later.
818 hl
= (struct hilite
*) ecalloc(1, sizeof(struct hilite
));
819 hl
->hl_startpos
= linepos
+ (sp
-line
);
820 hl
->hl_endpos
= linepos
+ (ep
-line
);
821 add_hilite(&hilites
, hl
);
824 * If we matched more than zero characters,
825 * move to the first char after the string we matched.
826 * If we matched zero, just move to the next char.
830 else if (searchp
!= line_end
)
832 else /* end of line */
834 } while (match_pattern(searchp
, line_end
- searchp
, &sp
, &ep
, 1));
837 * If there were backspaces in the original line, they
838 * were removed, and hl_startpos/hl_endpos are not correct.
839 * {{ This is very ugly. }}
841 adj_hilite(&hilites
, linepos
, cvt_ops
);
844 * Now put the hilites into the real list.
846 while ((hl
= hilites
.hl_next
) != NULL
)
848 hilites
.hl_next
= hl
->hl_next
;
849 add_hilite(&hilite_anchor
, hl
);
855 * Change the caseless-ness of searches.
856 * Updates the internal search state to reflect a change in the -i flag.
861 if (!is_ucase_pattern
)
863 * Pattern did not have uppercase.
864 * Just set the search caselessness to the global caselessness.
866 is_caseless
= caseless
;
869 * Pattern did have uppercase.
870 * Discard the pattern; we can't change search caselessness now.
877 * Find matching text which is currently on screen and highlight it.
882 struct scrpos scrpos
;
885 if (scrpos
.pos
== NULL_POSITION
)
887 prep_hilite(scrpos
.pos
, position(BOTTOM_PLUS_ONE
), -1);
892 * Change highlighting parameters.
898 * Erase any highlights currently on screen.
903 if (hilite_search
== OPT_ONPLUS
)
905 * Display highlights.
912 * Figure out where to start a search.
915 search_pos(search_type
)
924 * Start at the beginning (or end) of the file.
925 * The empty_screen() case is mainly for
926 * command line initiated searches;
927 * for example, "+/xyz" on the command line.
928 * Also for multi-file (SRCH_PAST_EOF) searches.
930 if (search_type
& SRCH_FORW
)
936 if (pos
== NULL_POSITION
)
938 (void) ch_end_seek();
947 * Search does not include current screen.
949 if (search_type
& SRCH_FORW
)
950 linenum
= BOTTOM_PLUS_ONE
;
953 pos
= position(linenum
);
957 * Search includes current screen.
958 * It starts at the jump target (if searching backwards),
959 * or at the jump target plus one (if forwards).
961 linenum
= adjsline(jump_sline
);
962 pos
= position(linenum
);
963 if (search_type
& SRCH_FORW
)
965 pos
= forw_raw_line(pos
, (char **)NULL
, (int *)NULL
);
966 while (pos
== NULL_POSITION
)
968 if (++linenum
>= sc_height
)
970 pos
= position(linenum
);
974 while (pos
== NULL_POSITION
)
978 pos
= position(linenum
);
986 * Search a subset of the file, specified by start/end position.
989 search_range(pos
, endpos
, search_type
, matches
, maxlines
, plinepos
, pendpos
)
1004 POSITION linepos
, oldpos
;
1006 linenum
= find_linenum(pos
);
1011 * Get lines until we find a matching one or until
1012 * we hit end-of-file (or beginning-of-file if we're
1013 * going backwards), or until we hit the end position.
1018 * A signal aborts the search.
1023 if ((endpos
!= NULL_POSITION
&& pos
>= endpos
) || maxlines
== 0)
1026 * Reached end position without a match.
1028 if (pendpos
!= NULL
)
1035 if (search_type
& SRCH_FORW
)
1038 * Read the next line, and save the
1039 * starting position of that line in linepos.
1042 pos
= forw_raw_line(pos
, &line
, &line_len
);
1048 * Read the previous line and save the
1049 * starting position of that line in linepos.
1051 pos
= back_raw_line(pos
, &line
, &line_len
);
1057 if (pos
== NULL_POSITION
)
1060 * Reached EOF/BOF without a match.
1062 if (pendpos
!= NULL
)
1068 * If we're using line numbers, we might as well
1069 * remember the information we have now (the position
1070 * and line number of the current line).
1071 * Don't do it for every line because it slows down
1072 * the search. Remember the line number only if
1073 * we're "far" from the last place we remembered it.
1075 if (linenums
&& abs((int)(pos
- oldpos
)) > 1024)
1076 add_lnum(linenum
, pos
);
1080 * If it's a caseless search, convert the line to lowercase.
1081 * If we're doing backspace processing, delete backspaces.
1083 cvt_ops
= get_cvt_ops();
1084 cvt_text(line
, line
, &line_len
, cvt_ops
);
1087 * Test the next line to see if we have a match.
1088 * We are successful if we either want a match and got one,
1089 * or if we want a non-match and got one.
1091 line_match
= match_pattern(line
, line_len
, &sp
, &ep
, 0);
1092 line_match
= (!(search_type
& SRCH_NO_MATCH
) && line_match
) ||
1093 ((search_type
& SRCH_NO_MATCH
) && !line_match
);
1099 if (search_type
& SRCH_FIND_ALL
)
1103 * We are supposed to find all matches in the range.
1104 * Just add the matches in this line to the
1105 * hilite list and keep searching.
1108 hilite_line(linepos
, line
, line_len
, sp
, ep
, cvt_ops
);
1110 } else if (--matches
<= 0)
1113 * Found the one match we're looking for.
1117 if (hilite_search
== OPT_ON
)
1120 * Clear the hilite list and add only
1121 * the matches in this one line.
1125 hilite_line(linepos
, line
, line_len
, sp
, ep
, cvt_ops
);
1128 if (plinepos
!= NULL
)
1129 *plinepos
= linepos
;
1136 * search for a pattern in history. If found, compile that pattern.
1139 hist_pattern(search_type
)
1145 set_mlist(ml_search
, 0);
1146 pattern
= cmd_lastpattern();
1147 if (pattern
== NULL
)
1150 if (caseless
== OPT_ONPLUS
)
1151 cvt_text(pattern
, pattern
, (int *)NULL
, CVT_TO_LC
);
1153 if (compile_pattern(pattern
, search_type
) < 0)
1156 is_ucase_pattern
= is_ucase(pattern
);
1157 if (is_ucase_pattern
&& caseless
!= OPT_ONPLUS
)
1160 is_caseless
= caseless
;
1163 if (hilite_search
== OPT_ONPLUS
&& !hide_hilite
)
1168 #else /* CMD_HISTORY */
1170 #endif /* CMD_HISTORY */
1174 * Search for the n-th occurrence of a specified pattern,
1175 * either forward or backward.
1176 * Return the number of matches not yet found in this file
1177 * (that is, n minus the number of matches found).
1178 * Return -1 if the search should be aborted.
1179 * Caller may continue the search in another file
1180 * if less than n matches are found in this file.
1183 search(search_type
, pattern
, n
)
1191 if (pattern
== NULL
|| *pattern
== '\0')
1194 * A null pattern means use the previously compiled pattern.
1196 if (!prev_pattern() && !hist_pattern(search_type
))
1198 error("No previous regular expression", NULL_PARG
);
1201 if ((search_type
& SRCH_NO_REGEX
) !=
1202 (last_search_type
& SRCH_NO_REGEX
))
1204 error("Please re-enter search pattern", NULL_PARG
);
1208 if (hilite_search
== OPT_ON
)
1211 * Erase the highlights currently on screen.
1212 * If the search fails, we'll redisplay them later.
1216 if (hilite_search
== OPT_ONPLUS
&& hide_hilite
)
1219 * Highlight any matches currently on screen,
1220 * before we actually start the search.
1230 * Compile the pattern.
1232 ucase
= is_ucase(pattern
);
1233 if (caseless
== OPT_ONPLUS
)
1234 cvt_text(pattern
, pattern
, (int *)NULL
, CVT_TO_LC
);
1235 if (compile_pattern(pattern
, search_type
) < 0)
1238 * Ignore case if -I is set OR
1239 * -i is set AND the pattern is all lowercase.
1241 is_ucase_pattern
= ucase
;
1242 if (is_ucase_pattern
&& caseless
!= OPT_ONPLUS
)
1245 is_caseless
= caseless
;
1250 * Erase the highlights currently on screen.
1251 * Also permanently delete them from the hilite list.
1257 if (hilite_search
== OPT_ONPLUS
)
1260 * Highlight any matches currently on screen,
1261 * before we actually start the search.
1269 * Figure out where to start the search.
1271 pos
= search_pos(search_type
);
1272 if (pos
== NULL_POSITION
)
1275 * Can't find anyplace to start searching from.
1277 if (search_type
& SRCH_PAST_EOF
)
1279 /* repaint(); -- why was this here? */
1280 error("Nothing to search", NULL_PARG
);
1284 n
= search_range(pos
, NULL_POSITION
, search_type
, n
, -1,
1285 &pos
, (POSITION
*)NULL
);
1289 * Search was unsuccessful.
1292 if (hilite_search
== OPT_ON
&& n
> 0)
1294 * Redisplay old hilites.
1301 if (!(search_type
& SRCH_NO_MOVE
))
1304 * Go to the matching line.
1306 jump_loc(pos
, jump_sline
);
1310 if (hilite_search
== OPT_ON
)
1312 * Display new hilites in the matching line.
1322 * Prepare hilites in a given range of the file.
1324 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1325 * of the file that has been "prepared"; that is, scanned for matches for
1326 * the current search pattern, and hilites have been created for such matches.
1327 * If prep_startpos == NULL_POSITION, the prep region is empty.
1328 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1329 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1332 prep_hilite(spos
, epos
, maxlines
)
1337 POSITION nprep_startpos
= prep_startpos
;
1338 POSITION nprep_endpos
= prep_endpos
;
1344 * Search beyond where we're asked to search, so the prep region covers
1345 * more than we need. Do one big search instead of a bunch of small ones.
1347 #define SEARCH_MORE (3*size_linebuf)
1349 if (!prev_pattern())
1353 * If we're limited to a max number of lines, figure out the
1354 * file position we should stop at.
1357 max_epos
= NULL_POSITION
;
1361 for (i
= 0; i
< maxlines
; i
++)
1362 max_epos
= forw_raw_line(max_epos
, (char **)NULL
, (int *)NULL
);
1367 * The range that we need to search (spos,epos); and the range that
1368 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1371 if (prep_startpos
== NULL_POSITION
||
1372 (epos
!= NULL_POSITION
&& epos
< prep_startpos
) ||
1376 * New range is not contiguous with old prep region.
1377 * Discard the old prep region and start a new one.
1380 if (epos
!= NULL_POSITION
)
1381 epos
+= SEARCH_MORE
;
1382 nprep_startpos
= spos
;
1386 * New range partially or completely overlaps old prep region.
1388 if (epos
== NULL_POSITION
)
1391 * New range goes to end of file.
1394 } else if (epos
> prep_endpos
)
1397 * New range ends after old prep region.
1398 * Extend prep region to end at end of new range.
1400 epos
+= SEARCH_MORE
;
1401 } else /* (epos <= prep_endpos) */
1404 * New range ends within old prep region.
1405 * Truncate search to end at start of old prep region.
1407 epos
= prep_startpos
;
1410 if (spos
< prep_startpos
)
1413 * New range starts before old prep region.
1414 * Extend old prep region backwards to start at
1415 * start of new range.
1417 if (spos
< SEARCH_MORE
)
1420 spos
-= SEARCH_MORE
;
1421 nprep_startpos
= spos
;
1422 } else /* (spos >= prep_startpos) */
1425 * New range starts within or after old prep region.
1426 * Trim search to start at end of old prep region.
1432 if (epos
!= NULL_POSITION
&& max_epos
!= NULL_POSITION
&&
1435 * Don't go past the max position we're allowed.
1439 if (epos
== NULL_POSITION
|| epos
> spos
)
1441 result
= search_range(spos
, epos
, SRCH_FORW
|SRCH_FIND_ALL
, 0,
1442 maxlines
, (POSITION
*)NULL
, &new_epos
);
1445 if (prep_endpos
== NULL_POSITION
|| new_epos
> prep_endpos
)
1446 nprep_endpos
= new_epos
;
1448 prep_startpos
= nprep_startpos
;
1449 prep_endpos
= nprep_endpos
;
1454 * Simple pattern matching function.
1455 * It supports no metacharacters like *, etc.
1458 match(pattern
, pattern_len
, buf
, buf_len
, pfound
, pend
)
1463 char **pfound
, **pend
;
1465 register char *pp
, *lp
;
1466 register char *pattern_end
= pattern
+ pattern_len
;
1467 register char *buf_end
= buf
+ buf_len
;
1469 for ( ; buf
< buf_end
; buf
++)
1471 for (pp
= pattern
, lp
= buf
; *pp
== *lp
; pp
++, lp
++)
1472 if (pp
== pattern_end
|| lp
== buf_end
)
1474 if (pp
== pattern_end
)
1488 * This function is called by the V8 regcomp to report
1489 * errors in regular expressions.