Pay attention to the timeout value passed down by the upper layer. This
[dragonfly.git] / contrib / less-4 / search.c
blob3a153808429cfc83f22025874c7184a8dbe1a6d7
1 /*
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.
9 */
13 * Routines to search a file for a pattern.
16 #include "less.h"
17 #include "position.h"
18 #include "charset.h"
20 #define MINPOS(a,b) (((a) < (b)) ? (a) : (b))
21 #define MAXPOS(a,b) (((a) > (b)) ? (a) : (b))
23 #if HAVE_POSIX_REGCOMP
24 #include <regex.h>
25 #ifdef REG_EXTENDED
26 #define REGCOMP_FLAG REG_EXTENDED
27 #else
28 #define REGCOMP_FLAG 0
29 #endif
30 #endif
31 #if HAVE_PCRE
32 #include <pcre.h>
33 #endif
34 #if HAVE_RE_COMP
35 char *re_comp();
36 int re_exec();
37 #endif
38 #if HAVE_REGCMP
39 char *regcmp();
40 char *regex();
41 extern char *__loc1;
42 #endif
43 #if HAVE_V8_REGCOMP
44 #include "regexp.h"
45 #endif
47 static int match();
49 extern int sigs;
50 extern int how_search;
51 extern int caseless;
52 extern int linenums;
53 extern int sc_height;
54 extern int jump_sline;
55 extern int bs_mode;
56 extern int ctldisp;
57 extern int status_col;
58 extern void * constant ml_search;
59 extern POSITION start_attnpos;
60 extern POSITION end_attnpos;
61 #if HILITE_SEARCH
62 extern int hilite_search;
63 extern int screen_trashed;
64 extern int size_linebuf;
65 extern int squished;
66 extern int can_goto_line;
67 extern int utf_mode;
68 static int hide_hilite;
69 static int oldbot;
70 static POSITION prep_startpos;
71 static POSITION prep_endpos;
73 struct hilite
75 struct hilite *hl_next;
76 POSITION hl_startpos;
77 POSITION hl_endpos;
79 static struct hilite hilite_anchor = { NULL, NULL_POSITION, NULL_POSITION };
80 #define hl_first hl_next
81 #endif
84 * These are the static variables that represent the "remembered"
85 * search pattern.
87 #if HAVE_POSIX_REGCOMP
88 static regex_t *regpattern = NULL;
89 #endif
90 #if HAVE_PCRE
91 pcre *regpattern = NULL;
92 #endif
93 #if HAVE_RE_COMP
94 int re_pattern = 0;
95 #endif
96 #if HAVE_REGCMP
97 static char *cpattern = NULL;
98 #endif
99 #if HAVE_V8_REGCOMP
100 static struct regexp *regpattern = NULL;
101 #endif
103 static int is_caseless;
104 static int is_ucase_pattern;
105 static int last_search_type;
106 static char *last_pattern = NULL;
108 #define CVT_TO_LC 01 /* Convert upper-case to lower-case */
109 #define CVT_BS 02 /* Do backspace processing */
110 #define CVT_CRLF 04 /* Remove CR after LF */
111 #define CVT_ANSI 010 /* Remove ANSI escape sequences */
114 * Get the length of a buffer needed to convert a string.
116 static int
117 cvt_length(len, ops)
118 int len;
119 int ops;
121 if (utf_mode)
123 * Just copying a string in UTF-8 mode can cause it to grow
124 * in length.
125 * Six output bytes for one input byte is the worst case
126 * (and unfortunately is far more than is needed in any
127 * non-pathological situation, so this is very wasteful).
129 len *= 6;
130 return len + 1;
134 * Convert text. Perform one or more of these transformations:
136 static void
137 cvt_text(odst, osrc, lenp, ops)
138 char *odst;
139 char *osrc;
140 int *lenp;
141 int ops;
143 char *dst;
144 char *src;
145 register char *src_end;
146 LWCHAR ch;
148 if (lenp != NULL)
149 src_end = osrc + *lenp;
150 else
151 src_end = osrc + strlen(osrc);
153 for (src = osrc, dst = odst; src < src_end; )
155 ch = step_char(&src, +1, src_end);
156 if ((ops & CVT_TO_LC) && IS_UPPER(ch))
158 /* Convert uppercase to lowercase. */
159 put_wchar(&dst, TO_LOWER(ch));
160 } else if ((ops & CVT_BS) && ch == '\b' && dst > odst)
162 /* Delete backspace and preceding char. */
163 do {
164 dst--;
165 } while (dst > odst &&
166 !IS_ASCII_OCTET(*dst) && !IS_UTF8_LEAD(*dst));
167 } else if ((ops & CVT_ANSI) && IS_CSI_START(ch))
169 /* Skip to end of ANSI escape sequence. */
170 while (src + 1 != src_end)
171 if (!is_ansi_middle(*++src))
172 break;
173 } else
174 /* Just copy. */
175 put_wchar(&dst, ch);
177 if ((ops & CVT_CRLF) && dst > odst && dst[-1] == '\r')
178 dst--;
179 *dst = '\0';
180 if (lenp != NULL)
181 *lenp = dst - odst;
185 * Determine which conversions to perform.
187 static int
188 get_cvt_ops()
190 int ops = 0;
191 if (is_caseless || bs_mode == BS_SPECIAL)
193 if (is_caseless)
194 ops |= CVT_TO_LC;
195 if (bs_mode == BS_SPECIAL)
196 ops |= CVT_BS;
197 if (bs_mode != BS_CONTROL)
198 ops |= CVT_CRLF;
199 } else if (bs_mode != BS_CONTROL)
201 ops |= CVT_CRLF;
203 if (ctldisp == OPT_ONPLUS)
204 ops |= CVT_ANSI;
205 return (ops);
209 * Are there any uppercase letters in this string?
211 static int
212 is_ucase(str)
213 char *str;
215 char *str_end = str + strlen(str);
216 LWCHAR ch;
218 while (str < str_end)
220 ch = step_char(&str, +1, str_end);
221 if (IS_UPPER(ch))
222 return (1);
224 return (0);
228 * Is there a previous (remembered) search pattern?
230 static int
231 prev_pattern()
233 if (last_search_type & SRCH_NO_REGEX)
234 return (last_pattern != NULL);
235 #if HAVE_POSIX_REGCOMP
236 return (regpattern != NULL);
237 #endif
238 #if HAVE_PCRE
239 return (regpattern != NULL);
240 #endif
241 #if HAVE_RE_COMP
242 return (re_pattern != 0);
243 #endif
244 #if HAVE_REGCMP
245 return (cpattern != NULL);
246 #endif
247 #if HAVE_V8_REGCOMP
248 return (regpattern != NULL);
249 #endif
250 #if NO_REGEX
251 return (last_pattern != NULL);
252 #endif
255 #if HILITE_SEARCH
257 * Repaint the hilites currently displayed on the screen.
258 * Repaint each line which contains highlighted text.
259 * If on==0, force all hilites off.
261 public void
262 repaint_hilite(on)
263 int on;
265 int slinenum;
266 POSITION pos;
267 POSITION epos;
268 int save_hide_hilite;
270 if (squished)
271 repaint();
273 save_hide_hilite = hide_hilite;
274 if (!on)
276 if (hide_hilite)
277 return;
278 hide_hilite = 1;
281 if (!can_goto_line)
283 repaint();
284 hide_hilite = save_hide_hilite;
285 return;
288 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
290 pos = position(slinenum);
291 if (pos == NULL_POSITION)
292 continue;
293 epos = position(slinenum+1);
294 #if 0
296 * If any character in the line is highlighted,
297 * repaint the line.
299 * {{ This doesn't work -- if line is drawn with highlights
300 * which should be erased (e.g. toggle -i with status column),
301 * we must redraw the line even if it has no highlights.
302 * For now, just repaint every line. }}
304 if (is_hilited(pos, epos, 1, NULL))
305 #endif
307 (void) forw_line(pos);
308 goto_line(slinenum);
309 put_line();
312 if (!oldbot)
313 lower_left();
314 hide_hilite = save_hide_hilite;
318 * Clear the attn hilite.
320 public void
321 clear_attn()
323 int slinenum;
324 POSITION old_start_attnpos;
325 POSITION old_end_attnpos;
326 POSITION pos;
327 POSITION epos;
328 int moved = 0;
330 if (start_attnpos == NULL_POSITION)
331 return;
332 old_start_attnpos = start_attnpos;
333 old_end_attnpos = end_attnpos;
334 start_attnpos = end_attnpos = NULL_POSITION;
336 if (!can_goto_line)
338 repaint();
339 return;
341 if (squished)
342 repaint();
344 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
346 pos = position(slinenum);
347 if (pos == NULL_POSITION)
348 continue;
349 epos = position(slinenum+1);
350 if (pos < old_end_attnpos &&
351 (epos == NULL_POSITION || epos > old_start_attnpos))
353 (void) forw_line(pos);
354 goto_line(slinenum);
355 put_line();
356 moved = 1;
359 if (moved)
360 lower_left();
362 #endif
365 * Hide search string highlighting.
367 public void
368 undo_search()
370 if (!prev_pattern())
372 error("No previous regular expression", NULL_PARG);
373 return;
375 #if HILITE_SEARCH
376 hide_hilite = !hide_hilite;
377 repaint_hilite(1);
378 #endif
382 * Compile a search pattern, for future use by match_pattern.
384 static int
385 compile_pattern2(pattern, search_type)
386 char *pattern;
387 int search_type;
389 if ((search_type & SRCH_NO_REGEX) == 0)
391 #if HAVE_POSIX_REGCOMP
392 regex_t *s = (regex_t *) ecalloc(1, sizeof(regex_t));
393 if (regcomp(s, pattern, REGCOMP_FLAG))
395 free(s);
396 error("Invalid pattern", NULL_PARG);
397 return (-1);
399 if (regpattern != NULL)
400 regfree(regpattern);
401 regpattern = s;
402 #endif
403 #if HAVE_PCRE
404 pcre *comp;
405 const char *errstring;
406 int erroffset;
407 PARG parg;
408 comp = pcre_compile(pattern, 0,
409 &errstring, &erroffset, NULL);
410 if (comp == NULL)
412 parg.p_string = (char *) errstring;
413 error("%s", &parg);
414 return (-1);
416 regpattern = comp;
417 #endif
418 #if HAVE_RE_COMP
419 PARG parg;
420 if ((parg.p_string = re_comp(pattern)) != NULL)
422 error("%s", &parg);
423 return (-1);
425 re_pattern = 1;
426 #endif
427 #if HAVE_REGCMP
428 char *s;
429 if ((s = regcmp(pattern, 0)) == NULL)
431 error("Invalid pattern", NULL_PARG);
432 return (-1);
434 if (cpattern != NULL)
435 free(cpattern);
436 cpattern = s;
437 #endif
438 #if HAVE_V8_REGCOMP
439 struct regexp *s;
440 if ((s = regcomp(pattern)) == NULL)
443 * regcomp has already printed an error message
444 * via regerror().
446 return (-1);
448 if (regpattern != NULL)
449 free(regpattern);
450 regpattern = s;
451 #endif
454 if (last_pattern != NULL)
455 free(last_pattern);
456 last_pattern = (char *) calloc(1, strlen(pattern)+1);
457 if (last_pattern != NULL)
458 strcpy(last_pattern, pattern);
460 last_search_type = search_type;
461 return (0);
465 * Like compile_pattern, but convert the pattern to lowercase if necessary.
467 static int
468 compile_pattern(pattern, search_type)
469 char *pattern;
470 int search_type;
472 char *cvt_pattern;
473 int result;
475 if (caseless != OPT_ONPLUS)
476 cvt_pattern = pattern;
477 else
479 cvt_pattern = (char*) ecalloc(1, cvt_length(strlen(pattern), CVT_TO_LC));
480 cvt_text(cvt_pattern, pattern, (int *)NULL, CVT_TO_LC);
482 result = compile_pattern2(cvt_pattern, search_type);
483 if (cvt_pattern != pattern)
484 free(cvt_pattern);
485 return (result);
489 * Forget that we have a compiled pattern.
491 static void
492 uncompile_pattern()
494 #if HAVE_POSIX_REGCOMP
495 if (regpattern != NULL)
496 regfree(regpattern);
497 regpattern = NULL;
498 #endif
499 #if HAVE_PCRE
500 if (regpattern != NULL)
501 pcre_free(regpattern);
502 regpattern = NULL;
503 #endif
504 #if HAVE_RE_COMP
505 re_pattern = 0;
506 #endif
507 #if HAVE_REGCMP
508 if (cpattern != NULL)
509 free(cpattern);
510 cpattern = NULL;
511 #endif
512 #if HAVE_V8_REGCOMP
513 if (regpattern != NULL)
514 free(regpattern);
515 regpattern = NULL;
516 #endif
517 last_pattern = NULL;
521 * Perform a pattern match with the previously compiled pattern.
522 * Set sp and ep to the start and end of the matched string.
524 static int
525 match_pattern(line, line_len, sp, ep, notbol)
526 char *line;
527 int line_len;
528 char **sp;
529 char **ep;
530 int notbol;
532 int matched;
534 if (last_search_type & SRCH_NO_REGEX)
535 return (match(last_pattern, strlen(last_pattern), line, line_len, sp, ep));
537 #if HAVE_POSIX_REGCOMP
539 regmatch_t rm;
540 int flags = (notbol) ? REG_NOTBOL : 0;
541 matched = !regexec(regpattern, line, 1, &rm, flags);
542 if (!matched)
543 return (0);
544 #ifndef __WATCOMC__
545 *sp = line + rm.rm_so;
546 *ep = line + rm.rm_eo;
547 #else
548 *sp = rm.rm_sp;
549 *ep = rm.rm_ep;
550 #endif
552 #endif
553 #if HAVE_PCRE
555 int flags = (notbol) ? PCRE_NOTBOL : 0;
556 int ovector[3];
557 matched = pcre_exec(regpattern, NULL, line, line_len,
558 0, flags, ovector, 3) >= 0;
559 if (!matched)
560 return (0);
561 *sp = line + ovector[0];
562 *ep = line + ovector[1];
564 #endif
565 #if HAVE_RE_COMP
566 matched = (re_exec(line) == 1);
568 * re_exec doesn't seem to provide a way to get the matched string.
570 *sp = *ep = NULL;
571 #endif
572 #if HAVE_REGCMP
573 *ep = regex(cpattern, line);
574 matched = (*ep != NULL);
575 if (!matched)
576 return (0);
577 *sp = __loc1;
578 #endif
579 #if HAVE_V8_REGCOMP
580 #if HAVE_REGEXEC2
581 matched = regexec2(regpattern, line, notbol);
582 #else
583 matched = regexec(regpattern, line);
584 #endif
585 if (!matched)
586 return (0);
587 *sp = regpattern->startp[0];
588 *ep = regpattern->endp[0];
589 #endif
590 #if NO_REGEX
591 matched = match(last_pattern, strlen(last_pattern), line, line_len, sp, ep);
592 #endif
593 return (matched);
596 #if HILITE_SEARCH
598 * Clear the hilite list.
600 public void
601 clr_hilite()
603 struct hilite *hl;
604 struct hilite *nexthl;
606 for (hl = hilite_anchor.hl_first; hl != NULL; hl = nexthl)
608 nexthl = hl->hl_next;
609 free((void*)hl);
611 hilite_anchor.hl_first = NULL;
612 prep_startpos = prep_endpos = NULL_POSITION;
616 * Should any characters in a specified range be highlighted?
618 static int
619 is_hilited_range(pos, epos)
620 POSITION pos;
621 POSITION epos;
623 struct hilite *hl;
626 * Look at each highlight and see if any part of it falls in the range.
628 for (hl = hilite_anchor.hl_first; hl != NULL; hl = hl->hl_next)
630 if (hl->hl_endpos > pos &&
631 (epos == NULL_POSITION || epos > hl->hl_startpos))
632 return (1);
634 return (0);
638 * Should any characters in a specified range be highlighted?
639 * If nohide is nonzero, don't consider hide_hilite.
641 public int
642 is_hilited(pos, epos, nohide, p_matches)
643 POSITION pos;
644 POSITION epos;
645 int nohide;
646 int *p_matches;
648 int match;
650 if (p_matches != NULL)
651 *p_matches = 0;
653 if (!status_col &&
654 start_attnpos != NULL_POSITION &&
655 pos < end_attnpos &&
656 (epos == NULL_POSITION || epos > start_attnpos))
658 * The attn line overlaps this range.
660 return (1);
662 match = is_hilited_range(pos, epos);
663 if (!match)
664 return (0);
666 if (p_matches != NULL)
668 * Report matches, even if we're hiding highlights.
670 *p_matches = 1;
672 if (hilite_search == 0)
674 * Not doing highlighting.
676 return (0);
678 if (!nohide && hide_hilite)
680 * Highlighting is hidden.
682 return (0);
684 return (1);
688 * Add a new hilite to a hilite list.
690 static void
691 add_hilite(anchor, hl)
692 struct hilite *anchor;
693 struct hilite *hl;
695 struct hilite *ihl;
698 * Hilites are sorted in the list; find where new one belongs.
699 * Insert new one after ihl.
701 for (ihl = anchor; ihl->hl_next != NULL; ihl = ihl->hl_next)
703 if (ihl->hl_next->hl_startpos > hl->hl_startpos)
704 break;
708 * Truncate hilite so it doesn't overlap any existing ones
709 * above and below it.
711 if (ihl != anchor)
712 hl->hl_startpos = MAXPOS(hl->hl_startpos, ihl->hl_endpos);
713 if (ihl->hl_next != NULL)
714 hl->hl_endpos = MINPOS(hl->hl_endpos, ihl->hl_next->hl_startpos);
715 if (hl->hl_startpos >= hl->hl_endpos)
718 * Hilite was truncated out of existence.
720 free(hl);
721 return;
723 hl->hl_next = ihl->hl_next;
724 ihl->hl_next = hl;
728 * Adjust hl_startpos & hl_endpos to account for processing by cvt_text.
730 static void
731 adj_hilite(anchor, linepos, cvt_ops)
732 struct hilite *anchor;
733 POSITION linepos;
734 int cvt_ops;
736 char *line;
737 char *oline;
738 int line_len;
739 char *line_end;
740 struct hilite *hl;
741 int checkstart;
742 POSITION opos;
743 POSITION npos;
744 LWCHAR ch;
745 int ncwidth;
748 * The line was already scanned and hilites were added (in hilite_line).
749 * But it was assumed that each char position in the line
750 * correponds to one char position in the file.
751 * This may not be true if cvt_text modified the line.
752 * Get the raw line again. Look at each character.
754 (void) forw_raw_line(linepos, &line, &line_len);
755 line_end = line + line_len;
756 opos = npos = linepos;
757 hl = anchor->hl_first;
758 checkstart = TRUE;
759 while (hl != NULL)
762 * See if we need to adjust the current hl_startpos or
763 * hl_endpos. After adjusting startpos[i], move to endpos[i].
764 * After adjusting endpos[i], move to startpos[i+1].
765 * The hilite list must be sorted thus:
766 * startpos[0] < endpos[0] <= startpos[1] < endpos[1] <= etc.
768 if (checkstart && hl->hl_startpos == opos)
770 hl->hl_startpos = npos;
771 checkstart = FALSE;
772 continue; /* {{ not really necessary }} */
773 } else if (!checkstart && hl->hl_endpos == opos)
775 hl->hl_endpos = npos;
776 checkstart = TRUE;
777 hl = hl->hl_next;
778 continue; /* {{ necessary }} */
780 if (line == line_end)
781 break;
783 /* Get the next char from the line. */
784 oline = line;
785 ch = step_char(&line, +1, line_end);
786 ncwidth = line - oline;
787 npos += ncwidth;
789 /* Figure out how this char was processed by cvt_text. */
790 if ((cvt_ops & CVT_BS) && ch == '\b')
792 /* Skip the backspace and the following char. */
793 oline = line;
794 ch = step_char(&line, +1, line_end);
795 ncwidth = line - oline;
796 npos += ncwidth;
797 } else if ((cvt_ops & CVT_TO_LC) && IS_UPPER(ch))
799 /* Converted uppercase to lower.
800 * Note that this may have changed the number of bytes
801 * that the character occupies. */
802 char dbuf[6];
803 char *dst = dbuf;
804 put_wchar(&dst, TO_LOWER(ch));
805 opos += dst - dbuf;
806 } else if ((cvt_ops & CVT_ANSI) && IS_CSI_START(ch))
808 /* Skip to end of ANSI escape sequence. */
809 while (line < line_end)
811 npos++;
812 if (!is_ansi_middle(*++line))
813 break;
815 } else
817 /* Ordinary unprocessed character. */
818 opos += ncwidth;
824 * Make a hilite for each string in a physical line which matches
825 * the current pattern.
826 * sp,ep delimit the first match already found.
828 static void
829 hilite_line(linepos, line, line_len, sp, ep, cvt_ops)
830 POSITION linepos;
831 char *line;
832 int line_len;
833 char *sp;
834 char *ep;
835 int cvt_ops;
837 char *searchp;
838 char *line_end = line + line_len;
839 struct hilite *hl;
840 struct hilite hilites;
842 if (sp == NULL || ep == NULL)
843 return;
845 * sp and ep delimit the first match in the line.
846 * Mark the corresponding file positions, then
847 * look for further matches and mark them.
848 * {{ This technique, of calling match_pattern on subsequent
849 * substrings of the line, may mark more than is correct
850 * if the pattern starts with "^". This bug is fixed
851 * for those regex functions that accept a notbol parameter
852 * (currently POSIX, PCRE and V8-with-regexec2). }}
854 searchp = line;
856 * Put the hilites into a temporary list until they're adjusted.
858 hilites.hl_first = NULL;
859 do {
860 if (ep > sp)
863 * Assume that each char position in the "line"
864 * buffer corresponds to one char position in the file.
865 * This is not quite true; we need to adjust later.
867 hl = (struct hilite *) ecalloc(1, sizeof(struct hilite));
868 hl->hl_startpos = linepos + (sp-line);
869 hl->hl_endpos = linepos + (ep-line);
870 add_hilite(&hilites, hl);
873 * If we matched more than zero characters,
874 * move to the first char after the string we matched.
875 * If we matched zero, just move to the next char.
877 if (ep > searchp)
878 searchp = ep;
879 else if (searchp != line_end)
880 searchp++;
881 else /* end of line */
882 break;
883 } while (match_pattern(searchp, line_end - searchp, &sp, &ep, 1));
886 * If there were backspaces in the original line, they
887 * were removed, and hl_startpos/hl_endpos are not correct.
888 * {{ This is very ugly. }}
890 adj_hilite(&hilites, linepos, cvt_ops);
893 * Now put the hilites into the real list.
895 while ((hl = hilites.hl_next) != NULL)
897 hilites.hl_next = hl->hl_next;
898 add_hilite(&hilite_anchor, hl);
901 #endif
904 * Change the caseless-ness of searches.
905 * Updates the internal search state to reflect a change in the -i flag.
907 public void
908 chg_caseless()
910 if (!is_ucase_pattern)
912 * Pattern did not have uppercase.
913 * Just set the search caselessness to the global caselessness.
915 is_caseless = caseless;
916 else
918 * Pattern did have uppercase.
919 * Discard the pattern; we can't change search caselessness now.
921 uncompile_pattern();
924 #if HILITE_SEARCH
926 * Find matching text which is currently on screen and highlight it.
928 static void
929 hilite_screen()
931 struct scrpos scrpos;
933 get_scrpos(&scrpos);
934 if (scrpos.pos == NULL_POSITION)
935 return;
936 prep_hilite(scrpos.pos, position(BOTTOM_PLUS_ONE), -1);
937 repaint_hilite(1);
941 * Change highlighting parameters.
943 public void
944 chg_hilite()
947 * Erase any highlights currently on screen.
949 clr_hilite();
950 hide_hilite = 0;
952 if (hilite_search == OPT_ONPLUS)
954 * Display highlights.
956 hilite_screen();
958 #endif
961 * Figure out where to start a search.
963 static POSITION
964 search_pos(search_type)
965 int search_type;
967 POSITION pos;
968 int linenum;
970 if (empty_screen())
973 * Start at the beginning (or end) of the file.
974 * The empty_screen() case is mainly for
975 * command line initiated searches;
976 * for example, "+/xyz" on the command line.
977 * Also for multi-file (SRCH_PAST_EOF) searches.
979 if (search_type & SRCH_FORW)
981 return (ch_zero());
982 } else
984 pos = ch_length();
985 if (pos == NULL_POSITION)
987 (void) ch_end_seek();
988 pos = ch_length();
990 return (pos);
993 if (how_search)
996 * Search does not include current screen.
998 if (search_type & SRCH_FORW)
999 linenum = BOTTOM_PLUS_ONE;
1000 else
1001 linenum = TOP;
1002 pos = position(linenum);
1003 } else
1006 * Search includes current screen.
1007 * It starts at the jump target (if searching backwards),
1008 * or at the jump target plus one (if forwards).
1010 linenum = adjsline(jump_sline);
1011 pos = position(linenum);
1012 if (search_type & SRCH_FORW)
1014 pos = forw_raw_line(pos, (char **)NULL, (int *)NULL);
1015 while (pos == NULL_POSITION)
1017 if (++linenum >= sc_height)
1018 break;
1019 pos = position(linenum);
1021 } else
1023 while (pos == NULL_POSITION)
1025 if (--linenum < 0)
1026 break;
1027 pos = position(linenum);
1031 return (pos);
1035 * Search a subset of the file, specified by start/end position.
1037 static int
1038 search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos)
1039 POSITION pos;
1040 POSITION endpos;
1041 int search_type;
1042 int matches;
1043 int maxlines;
1044 POSITION *plinepos;
1045 POSITION *pendpos;
1047 char *line;
1048 char *cline;
1049 int line_len;
1050 LINENUM linenum;
1051 char *sp, *ep;
1052 int line_match;
1053 int cvt_ops;
1054 POSITION linepos, oldpos;
1056 linenum = find_linenum(pos);
1057 oldpos = pos;
1058 for (;;)
1061 * Get lines until we find a matching one or until
1062 * we hit end-of-file (or beginning-of-file if we're
1063 * going backwards), or until we hit the end position.
1065 if (ABORT_SIGS())
1068 * A signal aborts the search.
1070 return (-1);
1073 if ((endpos != NULL_POSITION && pos >= endpos) || maxlines == 0)
1076 * Reached end position without a match.
1078 if (pendpos != NULL)
1079 *pendpos = pos;
1080 return (matches);
1082 if (maxlines > 0)
1083 maxlines--;
1085 if (search_type & SRCH_FORW)
1088 * Read the next line, and save the
1089 * starting position of that line in linepos.
1091 linepos = pos;
1092 pos = forw_raw_line(pos, &line, &line_len);
1093 if (linenum != 0)
1094 linenum++;
1095 } else
1098 * Read the previous line and save the
1099 * starting position of that line in linepos.
1101 pos = back_raw_line(pos, &line, &line_len);
1102 linepos = pos;
1103 if (linenum != 0)
1104 linenum--;
1107 if (pos == NULL_POSITION)
1110 * Reached EOF/BOF without a match.
1112 if (pendpos != NULL)
1113 *pendpos = oldpos;
1114 return (matches);
1118 * If we're using line numbers, we might as well
1119 * remember the information we have now (the position
1120 * and line number of the current line).
1121 * Don't do it for every line because it slows down
1122 * the search. Remember the line number only if
1123 * we're "far" from the last place we remembered it.
1125 if (linenums && abs((int)(pos - oldpos)) > 1024)
1126 add_lnum(linenum, pos);
1127 oldpos = pos;
1130 * If it's a caseless search, convert the line to lowercase.
1131 * If we're doing backspace processing, delete backspaces.
1133 cvt_ops = get_cvt_ops();
1134 cline = calloc(1, cvt_length(line_len, cvt_ops));
1135 cvt_text(cline, line, &line_len, cvt_ops);
1138 * Test the next line to see if we have a match.
1139 * We are successful if we either want a match and got one,
1140 * or if we want a non-match and got one.
1142 line_match = match_pattern(cline, line_len, &sp, &ep, 0);
1143 line_match = (!(search_type & SRCH_NO_MATCH) && line_match) ||
1144 ((search_type & SRCH_NO_MATCH) && !line_match);
1145 if (!line_match)
1147 free(cline);
1148 continue;
1151 * Got a match.
1153 if (search_type & SRCH_FIND_ALL)
1155 #if HILITE_SEARCH
1157 * We are supposed to find all matches in the range.
1158 * Just add the matches in this line to the
1159 * hilite list and keep searching.
1161 if (line_match)
1162 hilite_line(linepos, cline, line_len, sp, ep, cvt_ops);
1163 #endif
1164 free(cline);
1165 } else if (--matches <= 0)
1168 * Found the one match we're looking for.
1169 * Return it.
1171 #if HILITE_SEARCH
1172 if (hilite_search == OPT_ON)
1175 * Clear the hilite list and add only
1176 * the matches in this one line.
1178 clr_hilite();
1179 if (line_match)
1180 hilite_line(linepos, cline, line_len, sp, ep, cvt_ops);
1182 #endif
1183 free(cline);
1184 if (plinepos != NULL)
1185 *plinepos = linepos;
1186 return (0);
1192 * search for a pattern in history. If found, compile that pattern.
1194 static int
1195 hist_pattern(search_type)
1196 int search_type;
1198 #if CMD_HISTORY
1199 char *pattern;
1201 set_mlist(ml_search, 0);
1202 pattern = cmd_lastpattern();
1203 if (pattern == NULL)
1204 return (0);
1206 if (compile_pattern(pattern, search_type) < 0)
1207 return (0);
1209 is_ucase_pattern = is_ucase(pattern);
1210 if (is_ucase_pattern && caseless != OPT_ONPLUS)
1211 is_caseless = 0;
1212 else
1213 is_caseless = caseless;
1215 #if HILITE_SEARCH
1216 if (hilite_search == OPT_ONPLUS && !hide_hilite)
1217 hilite_screen();
1218 #endif
1220 return (1);
1221 #else /* CMD_HISTORY */
1222 return (0);
1223 #endif /* CMD_HISTORY */
1227 * Search for the n-th occurrence of a specified pattern,
1228 * either forward or backward.
1229 * Return the number of matches not yet found in this file
1230 * (that is, n minus the number of matches found).
1231 * Return -1 if the search should be aborted.
1232 * Caller may continue the search in another file
1233 * if less than n matches are found in this file.
1235 public int
1236 search(search_type, pattern, n)
1237 int search_type;
1238 char *pattern;
1239 int n;
1241 POSITION pos;
1242 int result;
1244 if (pattern == NULL || *pattern == '\0')
1247 * A null pattern means use the previously compiled pattern.
1249 if (!prev_pattern() && !hist_pattern(search_type))
1251 error("No previous regular expression", NULL_PARG);
1252 return (-1);
1254 if ((search_type & SRCH_NO_REGEX) !=
1255 (last_search_type & SRCH_NO_REGEX))
1257 error("Please re-enter search pattern", NULL_PARG);
1258 return -1;
1260 #if HILITE_SEARCH
1261 if (hilite_search == OPT_ON)
1264 * Erase the highlights currently on screen.
1265 * If the search fails, we'll redisplay them later.
1267 repaint_hilite(0);
1269 if (hilite_search == OPT_ONPLUS && hide_hilite)
1272 * Highlight any matches currently on screen,
1273 * before we actually start the search.
1275 hide_hilite = 0;
1276 hilite_screen();
1278 hide_hilite = 0;
1279 #endif
1280 } else
1283 * Compile the pattern.
1285 if (compile_pattern(pattern, search_type) < 0)
1286 return (-1);
1288 * Ignore case if -I is set OR
1289 * -i is set AND the pattern is all lowercase.
1291 is_ucase_pattern = is_ucase(pattern);
1292 if (is_ucase_pattern && caseless != OPT_ONPLUS)
1293 is_caseless = 0;
1294 else
1295 is_caseless = caseless;
1296 #if HILITE_SEARCH
1297 if (hilite_search)
1300 * Erase the highlights currently on screen.
1301 * Also permanently delete them from the hilite list.
1303 repaint_hilite(0);
1304 hide_hilite = 0;
1305 clr_hilite();
1307 if (hilite_search == OPT_ONPLUS)
1310 * Highlight any matches currently on screen,
1311 * before we actually start the search.
1313 hilite_screen();
1315 #endif
1319 * Figure out where to start the search.
1321 pos = search_pos(search_type);
1322 if (pos == NULL_POSITION)
1325 * Can't find anyplace to start searching from.
1327 if (search_type & SRCH_PAST_EOF)
1328 return (n);
1329 /* repaint(); -- why was this here? */
1330 error("Nothing to search", NULL_PARG);
1331 return (-1);
1334 n = search_range(pos, NULL_POSITION, search_type, n, -1,
1335 &pos, (POSITION*)NULL);
1336 if (n != 0)
1339 * Search was unsuccessful.
1341 #if HILITE_SEARCH
1342 if (hilite_search == OPT_ON && n > 0)
1344 * Redisplay old hilites.
1346 repaint_hilite(1);
1347 #endif
1348 return (n);
1351 if (!(search_type & SRCH_NO_MOVE))
1354 * Go to the matching line.
1356 jump_loc(pos, jump_sline);
1359 #if HILITE_SEARCH
1360 if (hilite_search == OPT_ON)
1362 * Display new hilites in the matching line.
1364 repaint_hilite(1);
1365 #endif
1366 return (0);
1370 #if HILITE_SEARCH
1372 * Prepare hilites in a given range of the file.
1374 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1375 * of the file that has been "prepared"; that is, scanned for matches for
1376 * the current search pattern, and hilites have been created for such matches.
1377 * If prep_startpos == NULL_POSITION, the prep region is empty.
1378 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1379 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1381 public void
1382 prep_hilite(spos, epos, maxlines)
1383 POSITION spos;
1384 POSITION epos;
1385 int maxlines;
1387 POSITION nprep_startpos = prep_startpos;
1388 POSITION nprep_endpos = prep_endpos;
1389 POSITION new_epos;
1390 POSITION max_epos;
1391 int result;
1392 int i;
1394 * Search beyond where we're asked to search, so the prep region covers
1395 * more than we need. Do one big search instead of a bunch of small ones.
1397 #define SEARCH_MORE (3*size_linebuf)
1399 if (!prev_pattern())
1400 return;
1403 * If we're limited to a max number of lines, figure out the
1404 * file position we should stop at.
1406 if (maxlines < 0)
1407 max_epos = NULL_POSITION;
1408 else
1410 max_epos = spos;
1411 for (i = 0; i < maxlines; i++)
1412 max_epos = forw_raw_line(max_epos, (char **)NULL, (int *)NULL);
1416 * Find two ranges:
1417 * The range that we need to search (spos,epos); and the range that
1418 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1421 if (prep_startpos == NULL_POSITION ||
1422 (epos != NULL_POSITION && epos < prep_startpos) ||
1423 spos > prep_endpos)
1426 * New range is not contiguous with old prep region.
1427 * Discard the old prep region and start a new one.
1429 clr_hilite();
1430 if (epos != NULL_POSITION)
1431 epos += SEARCH_MORE;
1432 nprep_startpos = spos;
1433 } else
1436 * New range partially or completely overlaps old prep region.
1438 if (epos == NULL_POSITION)
1441 * New range goes to end of file.
1444 } else if (epos > prep_endpos)
1447 * New range ends after old prep region.
1448 * Extend prep region to end at end of new range.
1450 epos += SEARCH_MORE;
1451 } else /* (epos <= prep_endpos) */
1454 * New range ends within old prep region.
1455 * Truncate search to end at start of old prep region.
1457 epos = prep_startpos;
1460 if (spos < prep_startpos)
1463 * New range starts before old prep region.
1464 * Extend old prep region backwards to start at
1465 * start of new range.
1467 if (spos < SEARCH_MORE)
1468 spos = 0;
1469 else
1470 spos -= SEARCH_MORE;
1471 nprep_startpos = spos;
1472 } else /* (spos >= prep_startpos) */
1475 * New range starts within or after old prep region.
1476 * Trim search to start at end of old prep region.
1478 spos = prep_endpos;
1482 if (epos != NULL_POSITION && max_epos != NULL_POSITION &&
1483 epos > max_epos)
1485 * Don't go past the max position we're allowed.
1487 epos = max_epos;
1489 if (epos == NULL_POSITION || epos > spos)
1491 result = search_range(spos, epos, SRCH_FORW|SRCH_FIND_ALL, 0,
1492 maxlines, (POSITION*)NULL, &new_epos);
1493 if (result < 0)
1494 return;
1495 if (prep_endpos == NULL_POSITION || new_epos > prep_endpos)
1496 nprep_endpos = new_epos;
1498 prep_startpos = nprep_startpos;
1499 prep_endpos = nprep_endpos;
1501 #endif
1504 * Simple pattern matching function.
1505 * It supports no metacharacters like *, etc.
1507 static int
1508 match(pattern, pattern_len, buf, buf_len, pfound, pend)
1509 char *pattern;
1510 int pattern_len;
1511 char *buf;
1512 int buf_len;
1513 char **pfound, **pend;
1515 register char *pp, *lp;
1516 register char *pattern_end = pattern + pattern_len;
1517 register char *buf_end = buf + buf_len;
1519 for ( ; buf < buf_end; buf++)
1521 for (pp = pattern, lp = buf; *pp == *lp; pp++, lp++)
1522 if (pp == pattern_end || lp == buf_end)
1523 break;
1524 if (pp == pattern_end)
1526 if (pfound != NULL)
1527 *pfound = buf;
1528 if (pend != NULL)
1529 *pend = lp;
1530 return (1);
1533 return (0);
1536 #if HAVE_V8_REGCOMP
1538 * This function is called by the V8 regcomp to report
1539 * errors in regular expressions.
1541 void
1542 regerror(s)
1543 char *s;
1545 PARG parg;
1547 parg.p_string = s;
1548 error("%s", &parg);
1550 #endif