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
;
297 if (start_attnpos
== NULL_POSITION
)
299 old_start_attnpos
= start_attnpos
;
300 old_end_attnpos
= end_attnpos
;
301 start_attnpos
= end_attnpos
= NULL_POSITION
;
311 for (slinenum
= TOP
; slinenum
< TOP
+ sc_height
-1; slinenum
++)
313 pos
= position(slinenum
);
314 if (pos
== NULL_POSITION
)
316 epos
= position(slinenum
+1);
317 if (pos
< old_end_attnpos
&&
318 (epos
== NULL_POSITION
|| epos
> old_start_attnpos
))
320 (void) forw_line(pos
);
332 * Hide search string highlighting.
339 error("No previous regular expression", NULL_PARG
);
343 hide_hilite
= !hide_hilite
;
349 * Compile a search pattern, for future use by match_pattern.
352 compile_pattern(pattern
, search_type
)
356 if ((search_type
& SRCH_NO_REGEX
) == 0)
358 #if HAVE_POSIX_REGCOMP
359 regex_t
*s
= (regex_t
*) ecalloc(1, sizeof(regex_t
));
360 if (regcomp(s
, pattern
, REGCOMP_FLAG
))
363 error("Invalid pattern", NULL_PARG
);
366 if (regpattern
!= NULL
)
372 const char *errstring
;
375 comp
= pcre_compile(pattern
, 0,
376 &errstring
, &erroffset
, NULL
);
379 parg
.p_string
= (char *) errstring
;
387 if ((parg
.p_string
= re_comp(pattern
)) != NULL
)
396 if ((s
= regcmp(pattern
, 0)) == NULL
)
398 error("Invalid pattern", NULL_PARG
);
401 if (cpattern
!= NULL
)
407 if ((s
= regcomp(pattern
)) == NULL
)
410 * regcomp has already printed an error message
415 if (regpattern
!= NULL
)
421 if (last_pattern
!= NULL
)
423 last_pattern
= (char *) calloc(1, strlen(pattern
)+1);
424 if (last_pattern
!= NULL
)
425 strcpy(last_pattern
, pattern
);
427 last_search_type
= search_type
;
432 * Forget that we have a compiled pattern.
437 #if HAVE_POSIX_REGCOMP
438 if (regpattern
!= NULL
)
443 if (regpattern
!= NULL
)
444 pcre_free(regpattern
);
451 if (cpattern
!= NULL
)
456 if (regpattern
!= NULL
)
464 * Perform a pattern match with the previously compiled pattern.
465 * Set sp and ep to the start and end of the matched string.
468 match_pattern(line
, line_len
, sp
, ep
, notbol
)
477 if (last_search_type
& SRCH_NO_REGEX
)
478 return (match(last_pattern
, strlen(last_pattern
), line
, line_len
, sp
, ep
));
480 #if HAVE_POSIX_REGCOMP
483 int flags
= (notbol
) ? REG_NOTBOL
: 0;
484 matched
= !regexec(regpattern
, line
, 1, &rm
, flags
);
488 *sp
= line
+ rm
.rm_so
;
489 *ep
= line
+ rm
.rm_eo
;
498 int flags
= (notbol
) ? PCRE_NOTBOL
: 0;
500 matched
= pcre_exec(regpattern
, NULL
, line
, line_len
,
501 0, flags
, ovector
, 3) >= 0;
504 *sp
= line
+ ovector
[0];
505 *ep
= line
+ ovector
[1];
509 matched
= (re_exec(line
) == 1);
511 * re_exec doesn't seem to provide a way to get the matched string.
516 *ep
= regex(cpattern
, line
);
517 matched
= (*ep
!= NULL
);
524 matched
= regexec2(regpattern
, line
, notbol
);
526 matched
= regexec(regpattern
, line
);
530 *sp
= regpattern
->startp
[0];
531 *ep
= regpattern
->endp
[0];
534 matched
= match(last_pattern
, strlen(last_pattern
), line
, line_len
, sp
, ep
);
541 * Clear the hilite list.
547 struct hilite
*nexthl
;
549 for (hl
= hilite_anchor
.hl_first
; hl
!= NULL
; hl
= nexthl
)
551 nexthl
= hl
->hl_next
;
554 hilite_anchor
.hl_first
= NULL
;
555 prep_startpos
= prep_endpos
= NULL_POSITION
;
559 * Should any characters in a specified range be highlighted?
562 is_hilited_range(pos
, epos
)
569 * Look at each highlight and see if any part of it falls in the range.
571 for (hl
= hilite_anchor
.hl_first
; hl
!= NULL
; hl
= hl
->hl_next
)
573 if (hl
->hl_endpos
> pos
&&
574 (epos
== NULL_POSITION
|| epos
> hl
->hl_startpos
))
581 * Should any characters in a specified range be highlighted?
582 * If nohide is nonzero, don't consider hide_hilite.
585 is_hilited(pos
, epos
, nohide
, p_matches
)
593 if (p_matches
!= NULL
)
597 start_attnpos
!= NULL_POSITION
&&
599 (epos
== NULL_POSITION
|| epos
> start_attnpos
))
601 * The attn line overlaps this range.
605 match
= is_hilited_range(pos
, epos
);
609 if (p_matches
!= NULL
)
611 * Report matches, even if we're hiding highlights.
615 if (hilite_search
== 0)
617 * Not doing highlighting.
621 if (!nohide
&& hide_hilite
)
623 * Highlighting is hidden.
631 * Add a new hilite to a hilite list.
634 add_hilite(anchor
, hl
)
635 struct hilite
*anchor
;
641 * Hilites are sorted in the list; find where new one belongs.
642 * Insert new one after ihl.
644 for (ihl
= anchor
; ihl
->hl_next
!= NULL
; ihl
= ihl
->hl_next
)
646 if (ihl
->hl_next
->hl_startpos
> hl
->hl_startpos
)
651 * Truncate hilite so it doesn't overlap any existing ones
652 * above and below it.
655 hl
->hl_startpos
= MAXPOS(hl
->hl_startpos
, ihl
->hl_endpos
);
656 if (ihl
->hl_next
!= NULL
)
657 hl
->hl_endpos
= MINPOS(hl
->hl_endpos
, ihl
->hl_next
->hl_startpos
);
658 if (hl
->hl_startpos
>= hl
->hl_endpos
)
661 * Hilite was truncated out of existence.
666 hl
->hl_next
= ihl
->hl_next
;
671 adj_hilite_ansi(cvt_ops
, line
, line_len
, npos
)
677 char *line_end
= *line
+ line_len
;
679 if (cvt_ops
& CVT_ANSI
)
680 while (**line
== ESC
)
683 * Found an ESC. The file position moves
684 * forward past the entire ANSI escape sequence.
688 while (*line
< line_end
)
691 if (!is_ansi_middle(*(*line
)++))
698 * Adjust hl_startpos & hl_endpos to account for backspace processing.
701 adj_hilite(anchor
, linepos
, cvt_ops
)
702 struct hilite
*anchor
;
715 * The line was already scanned and hilites were added (in hilite_line).
716 * But it was assumed that each char position in the line
717 * correponds to one char position in the file.
718 * This may not be true if there are backspaces in the line.
719 * Get the raw line again. Look at each character.
721 (void) forw_raw_line(linepos
, &line
, &line_len
);
722 line_end
= line
+ line_len
;
723 opos
= npos
= linepos
;
724 hl
= anchor
->hl_first
;
729 * See if we need to adjust the current hl_startpos or
730 * hl_endpos. After adjusting startpos[i], move to endpos[i].
731 * After adjusting endpos[i], move to startpos[i+1].
732 * The hilite list must be sorted thus:
733 * startpos[0] < endpos[0] <= startpos[1] < endpos[1] <= etc.
735 if (checkstart
&& hl
->hl_startpos
== opos
)
737 hl
->hl_startpos
= npos
;
739 continue; /* {{ not really necessary }} */
740 } else if (!checkstart
&& hl
->hl_endpos
== opos
)
742 hl
->hl_endpos
= npos
;
745 continue; /* {{ necessary }} */
747 if (line
== line_end
)
749 adj_hilite_ansi(cvt_ops
, &line
, line_end
- line
, &npos
);
753 if (cvt_ops
& CVT_BS
)
755 while (*line
== '\b')
759 adj_hilite_ansi(cvt_ops
, &line
, line_end
- line
, &npos
);
760 if (line
== line_end
)
767 * Found a backspace. The file position moves
768 * forward by 2 relative to the processed line
769 * which was searched in hilite_line.
779 * Make a hilite for each string in a physical line which matches
780 * the current pattern.
781 * sp,ep delimit the first match already found.
784 hilite_line(linepos
, line
, line_len
, sp
, ep
, cvt_ops
)
793 char *line_end
= line
+ line_len
;
795 struct hilite hilites
;
797 if (sp
== NULL
|| ep
== NULL
)
800 * sp and ep delimit the first match in the line.
801 * Mark the corresponding file positions, then
802 * look for further matches and mark them.
803 * {{ This technique, of calling match_pattern on subsequent
804 * substrings of the line, may mark more than is correct
805 * if the pattern starts with "^". This bug is fixed
806 * for those regex functions that accept a notbol parameter
807 * (currently POSIX, PCRE and V8-with-regexec2). }}
811 * Put the hilites into a temporary list until they're adjusted.
813 hilites
.hl_first
= NULL
;
818 * Assume that each char position in the "line"
819 * buffer corresponds to one char position in the file.
820 * This is not quite true; we need to adjust later.
822 hl
= (struct hilite
*) ecalloc(1, sizeof(struct hilite
));
823 hl
->hl_startpos
= linepos
+ (sp
-line
);
824 hl
->hl_endpos
= linepos
+ (ep
-line
);
825 add_hilite(&hilites
, hl
);
828 * If we matched more than zero characters,
829 * move to the first char after the string we matched.
830 * If we matched zero, just move to the next char.
834 else if (searchp
!= line_end
)
836 else /* end of line */
838 } while (match_pattern(searchp
, line_end
- searchp
, &sp
, &ep
, 1));
841 * If there were backspaces in the original line, they
842 * were removed, and hl_startpos/hl_endpos are not correct.
843 * {{ This is very ugly. }}
845 adj_hilite(&hilites
, linepos
, cvt_ops
);
848 * Now put the hilites into the real list.
850 while ((hl
= hilites
.hl_next
) != NULL
)
852 hilites
.hl_next
= hl
->hl_next
;
853 add_hilite(&hilite_anchor
, hl
);
859 * Change the caseless-ness of searches.
860 * Updates the internal search state to reflect a change in the -i flag.
865 if (!is_ucase_pattern
)
867 * Pattern did not have uppercase.
868 * Just set the search caselessness to the global caselessness.
870 is_caseless
= caseless
;
873 * Pattern did have uppercase.
874 * Discard the pattern; we can't change search caselessness now.
881 * Find matching text which is currently on screen and highlight it.
886 struct scrpos scrpos
;
889 if (scrpos
.pos
== NULL_POSITION
)
891 prep_hilite(scrpos
.pos
, position(BOTTOM_PLUS_ONE
), -1);
896 * Change highlighting parameters.
902 * Erase any highlights currently on screen.
907 if (hilite_search
== OPT_ONPLUS
)
909 * Display highlights.
916 * Figure out where to start a search.
919 search_pos(search_type
)
928 * Start at the beginning (or end) of the file.
929 * The empty_screen() case is mainly for
930 * command line initiated searches;
931 * for example, "+/xyz" on the command line.
932 * Also for multi-file (SRCH_PAST_EOF) searches.
934 if (search_type
& SRCH_FORW
)
940 if (pos
== NULL_POSITION
)
942 (void) ch_end_seek();
951 * Search does not include current screen.
953 if (search_type
& SRCH_FORW
)
954 linenum
= BOTTOM_PLUS_ONE
;
957 pos
= position(linenum
);
961 * Search includes current screen.
962 * It starts at the jump target (if searching backwards),
963 * or at the jump target plus one (if forwards).
965 linenum
= adjsline(jump_sline
);
966 pos
= position(linenum
);
967 if (search_type
& SRCH_FORW
)
969 pos
= forw_raw_line(pos
, (char **)NULL
, (int *)NULL
);
970 while (pos
== NULL_POSITION
)
972 if (++linenum
>= sc_height
)
974 pos
= position(linenum
);
978 while (pos
== NULL_POSITION
)
982 pos
= position(linenum
);
990 * Search a subset of the file, specified by start/end position.
993 search_range(pos
, endpos
, search_type
, matches
, maxlines
, plinepos
, pendpos
)
1008 POSITION linepos
, oldpos
;
1010 linenum
= find_linenum(pos
);
1015 * Get lines until we find a matching one or until
1016 * we hit end-of-file (or beginning-of-file if we're
1017 * going backwards), or until we hit the end position.
1022 * A signal aborts the search.
1027 if ((endpos
!= NULL_POSITION
&& pos
>= endpos
) || maxlines
== 0)
1030 * Reached end position without a match.
1032 if (pendpos
!= NULL
)
1039 if (search_type
& SRCH_FORW
)
1042 * Read the next line, and save the
1043 * starting position of that line in linepos.
1046 pos
= forw_raw_line(pos
, &line
, &line_len
);
1052 * Read the previous line and save the
1053 * starting position of that line in linepos.
1055 pos
= back_raw_line(pos
, &line
, &line_len
);
1061 if (pos
== NULL_POSITION
)
1064 * Reached EOF/BOF without a match.
1066 if (pendpos
!= NULL
)
1072 * If we're using line numbers, we might as well
1073 * remember the information we have now (the position
1074 * and line number of the current line).
1075 * Don't do it for every line because it slows down
1076 * the search. Remember the line number only if
1077 * we're "far" from the last place we remembered it.
1079 if (linenums
&& abs((int)(pos
- oldpos
)) > 1024)
1080 add_lnum(linenum
, pos
);
1084 * If it's a caseless search, convert the line to lowercase.
1085 * If we're doing backspace processing, delete backspaces.
1087 cvt_ops
= get_cvt_ops();
1088 cvt_text(line
, line
, &line_len
, cvt_ops
);
1091 * Test the next line to see if we have a match.
1092 * We are successful if we either want a match and got one,
1093 * or if we want a non-match and got one.
1095 line_match
= match_pattern(line
, line_len
, &sp
, &ep
, 0);
1096 line_match
= (!(search_type
& SRCH_NO_MATCH
) && line_match
) ||
1097 ((search_type
& SRCH_NO_MATCH
) && !line_match
);
1103 if (search_type
& SRCH_FIND_ALL
)
1107 * We are supposed to find all matches in the range.
1108 * Just add the matches in this line to the
1109 * hilite list and keep searching.
1112 hilite_line(linepos
, line
, line_len
, sp
, ep
, cvt_ops
);
1114 } else if (--matches
<= 0)
1117 * Found the one match we're looking for.
1121 if (hilite_search
== OPT_ON
)
1124 * Clear the hilite list and add only
1125 * the matches in this one line.
1129 hilite_line(linepos
, line
, line_len
, sp
, ep
, cvt_ops
);
1132 if (plinepos
!= NULL
)
1133 *plinepos
= linepos
;
1140 * search for a pattern in history. If found, compile that pattern.
1143 hist_pattern(search_type
)
1149 set_mlist(ml_search
, 0);
1150 pattern
= cmd_lastpattern();
1151 if (pattern
== NULL
)
1154 if (caseless
== OPT_ONPLUS
)
1155 cvt_text(pattern
, pattern
, (int *)NULL
, CVT_TO_LC
);
1157 if (compile_pattern(pattern
, search_type
) < 0)
1160 is_ucase_pattern
= is_ucase(pattern
);
1161 if (is_ucase_pattern
&& caseless
!= OPT_ONPLUS
)
1164 is_caseless
= caseless
;
1167 if (hilite_search
== OPT_ONPLUS
&& !hide_hilite
)
1172 #else /* CMD_HISTORY */
1174 #endif /* CMD_HISTORY */
1178 * Search for the n-th occurrence of a specified pattern,
1179 * either forward or backward.
1180 * Return the number of matches not yet found in this file
1181 * (that is, n minus the number of matches found).
1182 * Return -1 if the search should be aborted.
1183 * Caller may continue the search in another file
1184 * if less than n matches are found in this file.
1187 search(search_type
, pattern
, n
)
1195 if (pattern
== NULL
|| *pattern
== '\0')
1198 * A null pattern means use the previously compiled pattern.
1200 if (!prev_pattern() && !hist_pattern(search_type
))
1202 error("No previous regular expression", NULL_PARG
);
1205 if ((search_type
& SRCH_NO_REGEX
) !=
1206 (last_search_type
& SRCH_NO_REGEX
))
1208 error("Please re-enter search pattern", NULL_PARG
);
1212 if (hilite_search
== OPT_ON
)
1215 * Erase the highlights currently on screen.
1216 * If the search fails, we'll redisplay them later.
1220 if (hilite_search
== OPT_ONPLUS
&& hide_hilite
)
1223 * Highlight any matches currently on screen,
1224 * before we actually start the search.
1234 * Compile the pattern.
1236 ucase
= is_ucase(pattern
);
1237 if (caseless
== OPT_ONPLUS
)
1238 cvt_text(pattern
, pattern
, (int *)NULL
, CVT_TO_LC
);
1239 if (compile_pattern(pattern
, search_type
) < 0)
1242 * Ignore case if -I is set OR
1243 * -i is set AND the pattern is all lowercase.
1245 is_ucase_pattern
= ucase
;
1246 if (is_ucase_pattern
&& caseless
!= OPT_ONPLUS
)
1249 is_caseless
= caseless
;
1254 * Erase the highlights currently on screen.
1255 * Also permanently delete them from the hilite list.
1261 if (hilite_search
== OPT_ONPLUS
)
1264 * Highlight any matches currently on screen,
1265 * before we actually start the search.
1273 * Figure out where to start the search.
1275 pos
= search_pos(search_type
);
1276 if (pos
== NULL_POSITION
)
1279 * Can't find anyplace to start searching from.
1281 if (search_type
& SRCH_PAST_EOF
)
1283 /* repaint(); -- why was this here? */
1284 error("Nothing to search", NULL_PARG
);
1288 n
= search_range(pos
, NULL_POSITION
, search_type
, n
, -1,
1289 &pos
, (POSITION
*)NULL
);
1293 * Search was unsuccessful.
1296 if (hilite_search
== OPT_ON
&& n
> 0)
1298 * Redisplay old hilites.
1305 if (!(search_type
& SRCH_NO_MOVE
))
1308 * Go to the matching line.
1310 jump_loc(pos
, jump_sline
);
1314 if (hilite_search
== OPT_ON
)
1316 * Display new hilites in the matching line.
1326 * Prepare hilites in a given range of the file.
1328 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1329 * of the file that has been "prepared"; that is, scanned for matches for
1330 * the current search pattern, and hilites have been created for such matches.
1331 * If prep_startpos == NULL_POSITION, the prep region is empty.
1332 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1333 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1336 prep_hilite(spos
, epos
, maxlines
)
1341 POSITION nprep_startpos
= prep_startpos
;
1342 POSITION nprep_endpos
= prep_endpos
;
1348 * Search beyond where we're asked to search, so the prep region covers
1349 * more than we need. Do one big search instead of a bunch of small ones.
1351 #define SEARCH_MORE (3*size_linebuf)
1353 if (!prev_pattern())
1357 * If we're limited to a max number of lines, figure out the
1358 * file position we should stop at.
1361 max_epos
= NULL_POSITION
;
1365 for (i
= 0; i
< maxlines
; i
++)
1366 max_epos
= forw_raw_line(max_epos
, (char **)NULL
, (int *)NULL
);
1371 * The range that we need to search (spos,epos); and the range that
1372 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1375 if (prep_startpos
== NULL_POSITION
||
1376 (epos
!= NULL_POSITION
&& epos
< prep_startpos
) ||
1380 * New range is not contiguous with old prep region.
1381 * Discard the old prep region and start a new one.
1384 if (epos
!= NULL_POSITION
)
1385 epos
+= SEARCH_MORE
;
1386 nprep_startpos
= spos
;
1390 * New range partially or completely overlaps old prep region.
1392 if (epos
== NULL_POSITION
)
1395 * New range goes to end of file.
1398 } else if (epos
> prep_endpos
)
1401 * New range ends after old prep region.
1402 * Extend prep region to end at end of new range.
1404 epos
+= SEARCH_MORE
;
1405 } else /* (epos <= prep_endpos) */
1408 * New range ends within old prep region.
1409 * Truncate search to end at start of old prep region.
1411 epos
= prep_startpos
;
1414 if (spos
< prep_startpos
)
1417 * New range starts before old prep region.
1418 * Extend old prep region backwards to start at
1419 * start of new range.
1421 if (spos
< SEARCH_MORE
)
1424 spos
-= SEARCH_MORE
;
1425 nprep_startpos
= spos
;
1426 } else /* (spos >= prep_startpos) */
1429 * New range starts within or after old prep region.
1430 * Trim search to start at end of old prep region.
1436 if (epos
!= NULL_POSITION
&& max_epos
!= NULL_POSITION
&&
1439 * Don't go past the max position we're allowed.
1443 if (epos
== NULL_POSITION
|| epos
> spos
)
1445 result
= search_range(spos
, epos
, SRCH_FORW
|SRCH_FIND_ALL
, 0,
1446 maxlines
, (POSITION
*)NULL
, &new_epos
);
1449 if (prep_endpos
== NULL_POSITION
|| new_epos
> prep_endpos
)
1450 nprep_endpos
= new_epos
;
1452 prep_startpos
= nprep_startpos
;
1453 prep_endpos
= nprep_endpos
;
1458 * Simple pattern matching function.
1459 * It supports no metacharacters like *, etc.
1462 match(pattern
, pattern_len
, buf
, buf_len
, pfound
, pend
)
1467 char **pfound
, **pend
;
1469 register char *pp
, *lp
;
1470 register char *pattern_end
= pattern
+ pattern_len
;
1471 register char *buf_end
= buf
+ buf_len
;
1473 for ( ; buf
< buf_end
; buf
++)
1475 for (pp
= pattern
, lp
= buf
; *pp
== *lp
; pp
++, lp
++)
1476 if (pp
== pattern_end
|| lp
== buf_end
)
1478 if (pp
== pattern_end
)
1492 * This function is called by the V8 regcomp to report
1493 * errors in regular expressions.