1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
|
/* $OpenBSD: search.c,v 1.2 2001/01/29 01:58:04 niklas Exp $ */
/*
* Copyright (c) 1984,1985,1989,1994,1995 Mark Nudelman
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice in the documentation and/or other materials provided with
* the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Routines to search a file for a pattern.
*/
#include "less.h"
#include "position.h"
#define MINPOS(a,b) (((a) < (b)) ? (a) : (b))
#define MAXPOS(a,b) (((a) > (b)) ? (a) : (b))
#if HAVE_POSIX_REGCOMP
#include <regex.h>
#endif
#if HAVE_RE_COMP
char *re_comp();
int re_exec();
#endif
#if HAVE_REGCMP
char *regcmp();
char *regex();
extern char *__loc1;
#endif
#if HAVE_V8_REGCOMP
#include "regexp.h"
#endif
#if NO_REGEX
static int match();
#endif
extern int sigs;
extern int how_search;
extern int caseless;
extern int linenums;
extern int sc_height;
extern int jump_sline;
extern int bs_mode;
#if HILITE_SEARCH
extern int hilite_search;
extern int screen_trashed;
extern int size_linebuf;
static int hide_hilite;
static POSITION prep_startpos;
static POSITION prep_endpos;
struct hilite
{
struct hilite *hl_next;
POSITION hl_startpos;
POSITION hl_endpos;
};
static struct hilite hilite_anchor = { NULL };
#define hl_first hl_next
#endif
/*
* These are the static variables that represent the "remembered"
* search pattern.
*/
#if HAVE_POSIX_REGCOMP
static regex_t *regpattern = NULL;
#endif
#if HAVE_RE_COMP
int re_pattern = 0;
#endif
#if HAVE_REGCMP
static char *cpattern = NULL;
#endif
#if HAVE_V8_REGCOMP
static struct regexp *regpattern = NULL;
#endif
#if NO_REGEX
static char *last_pattern = NULL;
#endif
static int is_caseless;
static int is_ucase_pattern;
/*
* Convert text. Perform one or more of these transformations:
*/
#define CVT_TO_LC 01 /* Convert upper-case to lower-case */
#define CVT_BS 02 /* Do backspace processing */
static void
cvt_text(odst, osrc, ops)
char *odst;
char *osrc;
int ops;
{
register char *dst;
register char *src;
for (src = osrc, dst = odst; *src != '\0'; src++, dst++)
{
if ((ops & CVT_TO_LC) && isupper(*src))
/* Convert uppercase to lowercase. */
*dst = tolower(*src);
else if ((ops & CVT_BS) && *src == '\b' && dst > odst)
/* Delete BS and preceding char. */
dst -= 2;
else
/* Just copy. */
*dst = *src;
}
*dst = '\0';
}
/*
* Are there any uppercase letters in this string?
*/
static int
is_ucase(s)
char *s;
{
register char *p;
for (p = s; *p != '\0'; p++)
if (isupper(*p))
return (1);
return (0);
}
/*
* Is there a previous (remembered) search pattern?
*/
static int
prev_pattern()
{
#if HAVE_POSIX_REGCOMP
return (regpattern != NULL);
#endif
#if HAVE_RE_COMP
return (re_pattern != 0);
#endif
#if HAVE_REGCMP
return (cpattern != NULL);
#endif
#if HAVE_V8_REGCOMP
return (regpattern != NULL);
#endif
#if NO_REGEX
return (last_pattern != NULL);
#endif
}
#if HILITE_SEARCH
/*
* Repaint the hilites currently displayed on the screen.
* Repaint each line which contains highlighted text.
* If on==0, force all hilites off.
*/
public void
repaint_hilite(on)
int on;
{
int slinenum;
POSITION pos;
POSITION epos;
int save_hide_hilite;
extern int can_goto_line;
save_hide_hilite = hide_hilite;
if (!on)
{
if (hide_hilite)
return;
hide_hilite = 1;
}
if (!can_goto_line)
{
repaint();
hide_hilite = save_hide_hilite;
return;
}
for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
{
pos = position(slinenum);
if (pos == NULL_POSITION)
continue;
epos = position(slinenum+1);
/*
* If any character in the line is highlighted,
* repaint the line.
*/
if (is_hilited(pos, epos, 1))
{
(void) forw_line(pos);
goto_line(slinenum);
put_line();
}
}
hide_hilite = save_hide_hilite;
}
#endif
/*
* Hide search string highlighting.
*/
public void
undo_search()
{
if (!prev_pattern())
{
error("No previous regular expression", NULL_PARG);
return;
}
#if HILITE_SEARCH
hide_hilite = !hide_hilite;
repaint_hilite(1);
#endif
}
/*
* Compile a search pattern, for future use by match_pattern.
*/
static int
compile_pattern(pattern)
char *pattern;
{
#if HAVE_POSIX_REGCOMP
regex_t *s = (regex_t *) ecalloc(1, sizeof(regex_t));
if (regcomp(s, pattern, 0))
{
free(s);
error("Invalid pattern", NULL_PARG);
return (-1);
}
if (regpattern != NULL)
regfree(regpattern);
regpattern = s;
#endif
#if HAVE_RE_COMP
PARG parg;
if ((parg.p_string = re_comp(pattern)) != NULL)
{
error("%s", &parg);
return (-1);
}
re_pattern = 1;
#endif
#if HAVE_REGCMP
char *s;
if ((s = regcmp(pattern, 0)) == NULL)
{
error("Invalid pattern", NULL_PARG);
return (-1);
}
if (cpattern != NULL)
free(cpattern);
cpattern = s;
#endif
#if HAVE_V8_REGCOMP
struct regexp *s;
if ((s = regcomp(pattern)) == NULL)
{
/*
* regcomp has already printed error message via regerror().
*/
return (-1);
}
if (regpattern != NULL)
free(regpattern);
regpattern = s;
#endif
#if NO_REGEX
static char lpbuf[100];
strcpy(lpbuf, pattern);
last_pattern = lpbuf;
#endif
return (0);
}
/*
* Forget that we have a compiled pattern.
*/
static void
uncompile_pattern()
{
#if HAVE_POSIX_REGCOMP
if (regpattern != NULL)
regfree(regpattern);
regpattern = NULL;
#endif
#if HAVE_RE_COMP
re_pattern = 0;
#endif
#if HAVE_REGCMP
if (cpattern != NULL)
free(cpattern);
cpattern = NULL;
#endif
#if HAVE_V8_REGCOMP
if (regpattern != NULL)
free(regpattern);
regpattern = NULL;
#endif
#if NO_REGEX
last_pattern = NULL;
#endif
}
/*
* Perform a pattern match with the previously compiled pattern.
* Set sp and ep to the start and end of the matched string.
*/
static int
match_pattern(line, sp, ep)
char *line;
char **sp;
char **ep;
{
int matched;
#if HAVE_POSIX_REGCOMP
regmatch_t rm;
matched = !regexec(regpattern, line, 1, &rm, 0);
if (!matched)
return (0);
*sp = line + rm.rm_so;
*ep = line + rm.rm_eo;
#endif
#if HAVE_RE_COMP
matched = (re_exec(line) == 1);
/*
* re_exec doesn't seem to provide a way to get the matched string.
*/
*sp = *ep = NULL;
#endif
#if HAVE_REGCMP
*ep = regex(cpattern, line);
matched = (*ep != NULL);
if (!matched)
return (0);
*sp = __loc1;
#endif
#if HAVE_V8_REGCOMP
matched = regexec(regpattern, line);
if (!matched)
return (0);
*sp = regpattern->startp[0];
*ep = regpattern->endp[0];
#endif
#if NO_REGEX
matched = match(last_pattern, line, sp, ep);
#endif
return (matched);
}
#if HILITE_SEARCH
/*
* Clear the hilite list.
*/
public void
clr_hilite()
{
struct hilite *hl;
struct hilite *nexthl;
for (hl = hilite_anchor.hl_first; hl != NULL; hl = nexthl)
{
nexthl = hl->hl_next;
free((void*)hl);
}
hilite_anchor.hl_first = NULL;
prep_startpos = prep_endpos = NULL_POSITION;
}
/*
* Should any characters in a specified range be highlighted?
* If nohide is nonzero, don't consider hide_hilite.
*/
public int
is_hilited(pos, epos, nohide)
POSITION pos;
POSITION epos;
int nohide;
{
struct hilite *hl;
if (hilite_search == 0)
/*
* Not doing highlighting.
*/
return (0);
if (!nohide && hide_hilite)
/*
* Highlighting is hidden.
*/
return (0);
/*
* Look at each highlight and see if any part of it falls in the range.
*/
for (hl = hilite_anchor.hl_first; hl != NULL; hl = hl->hl_next)
{
if (hl->hl_endpos > pos &&
(epos == NULL_POSITION || epos > hl->hl_startpos))
return (1);
}
return (0);
}
/*
* Add a new hilite to a hilite list.
*/
static void
add_hilite(anchor, hl)
struct hilite *anchor;
struct hilite *hl;
{
struct hilite *ihl;
/*
* Hilites are sorted in the list; find where new one belongs.
* Insert new one after ihl.
*/
for (ihl = anchor; ihl->hl_next != NULL; ihl = ihl->hl_next)
{
if (ihl->hl_next->hl_startpos > hl->hl_startpos)
break;
}
/*
* Truncate hilite so it doesn't overlap any existing ones
* above and below it.
*/
if (ihl != anchor)
hl->hl_startpos = MAXPOS(hl->hl_startpos, ihl->hl_endpos);
if (ihl->hl_next != NULL)
hl->hl_endpos = MINPOS(hl->hl_endpos, ihl->hl_next->hl_startpos);
if (hl->hl_startpos >= hl->hl_endpos)
{
/*
* Hilite was truncated out of existence.
*/
free(hl);
return;
}
hl->hl_next = ihl->hl_next;
ihl->hl_next = hl;
}
/*
* Adjust hl_startpos & hl_endpos to account for backspace processing.
*/
static void
adj_hilite(anchor, linepos)
struct hilite *anchor;
POSITION linepos;
{
char *line;
struct hilite *hl;
int checkstart;
POSITION opos;
POSITION npos;
/*
* The line was already scanned and hilites were added (in hilite_line).
* But it was assumed that each char position in the line
* correponds to one char position in the file.
* This may not be true if there are backspaces in the line.
* Get the raw line again. Look at each character.
*/
(void) forw_raw_line(linepos, &line);
opos = npos = linepos;
hl = anchor->hl_first;
checkstart = TRUE;
while (hl != NULL)
{
/*
* See if we need to adjust the current hl_startpos or
* hl_endpos. After adjusting startpos[i], move to endpos[i].
* After adjusting endpos[i], move to startpos[i+1].
* The hilite list must be sorted thus:
* startpos[0] < endpos[0] <= startpos[1] < endpos[1] <= etc.
*/
if (checkstart && hl->hl_startpos == opos)
{
hl->hl_startpos = npos;
checkstart = FALSE;
continue; /* {{ not really necessary }} */
} else if (!checkstart && hl->hl_endpos == opos)
{
hl->hl_endpos = npos;
checkstart = TRUE;
hl = hl->hl_next;
continue; /* {{ necessary }} */
}
if (*line == '\0')
break;
opos++;
npos++;
line++;
while (line[0] == '\b' && line[1] != '\0')
{
/*
* Found a backspace. The file position moves
* forward by 2 relative to the processed line
* which was searched in hilite_line.
*/
npos += 2;
line += 2;
}
}
}
/*
* Make a hilite for each string in a physical line which matches
* the current pattern.
* sp,ep delimit the first match already found.
*/
static void
hilite_line(linepos, line, sp, ep)
POSITION linepos;
char *line;
char *sp;
char *ep;
{
char *searchp;
struct hilite *hl;
struct hilite hilites;
if (sp == NULL || ep == NULL)
return;
/*
* sp and ep delimit the first match in the line.
* Mark the corresponding file positions, then
* look for further matches and mark them.
* {{ This technique, of calling match_pattern on subsequent
* substrings of the line, may mark more than is correct
* if, for example, the pattern starts with "^". }}
*/
searchp = line;
/*
* Put the hilites into a temporary list until they're adjusted.
*/
hilites.hl_first = NULL;
do {
if (ep > sp)
{
/*
* Assume that each char position in the "line"
* buffer corresponds to one char position in the file.
* This is not quite true; we need to adjust later.
*/
hl = (struct hilite *) ecalloc(1, sizeof(struct hilite));
hl->hl_startpos = linepos + (sp-line);
hl->hl_endpos = linepos + (ep-line);
add_hilite(&hilites, hl);
}
/*
* If we matched more than zero characters,
* move to the first char after the string we matched.
* If we matched zero, just move to the next char.
*/
if (ep > searchp)
searchp = ep;
else if (*searchp != '\0')
searchp++;
else /* end of line */
break;
} while (match_pattern(searchp, &sp, &ep));
if (bs_mode == BS_SPECIAL)
{
/*
* If there were backspaces in the original line, they
* were removed, and hl_startpos/hl_endpos are not correct.
* {{ This is very ugly. }}
*/
adj_hilite(&hilites, linepos);
}
/*
* Now put the hilites into the real list.
*/
while ((hl = hilites.hl_next) != NULL)
{
hilites.hl_next = hl->hl_next;
add_hilite(&hilite_anchor, hl);
}
}
#endif
/*
* Change the caseless-ness of searches.
* Updates the internal search state to reflect a change in the -i flag.
*/
public void
chg_caseless()
{
if (!is_ucase_pattern)
/*
* Pattern did not have uppercase.
* Just set the search caselessness to the global caselessness.
*/
is_caseless = caseless;
else
/*
* Pattern did have uppercase.
* Discard the pattern; we can't change search caselessness now.
*/
uncompile_pattern();
}
#if HILITE_SEARCH
/*
* Find matching text which is currently on screen and highlight it.
*/
static void
hilite_screen()
{
struct scrpos scrpos;
get_scrpos(&scrpos);
if (scrpos.pos == NULL_POSITION)
return;
prep_hilite(scrpos.pos, position(BOTTOM_PLUS_ONE));
repaint_hilite(1);
}
/*
* Change highlighting parameters.
*/
public void
chg_hilite()
{
/*
* Erase any highlights currently on screen.
*/
clr_hilite();
hide_hilite = 0;
if (hilite_search == OPT_ONPLUS)
/*
* Display highlights.
*/
hilite_screen();
}
#endif
/*
* Figure out where to start a search.
*/
static POSITION
search_pos(search_type)
int search_type;
{
POSITION pos;
int linenum;
if (empty_screen())
{
/*
* Start at the beginning (or end) of the file.
* The empty_screen() case is mainly for
* command line initiated searches;
* for example, "+/xyz" on the command line.
* Also for multi-file (SRCH_PAST_EOF) searches.
*/
if (search_type & SRCH_FORW)
{
return (ch_zero());
} else
{
pos = ch_length();
if (pos == NULL_POSITION)
{
(void) ch_end_seek();
pos = ch_length();
}
return (pos);
}
}
if (how_search)
{
/*
* Search does not include current screen.
*/
if (search_type & SRCH_FORW)
linenum = BOTTOM_PLUS_ONE;
else
linenum = TOP;
pos = position(linenum);
} else
{
/*
* Search includes current screen.
* It starts at the jump target (if searching backwards),
* or at the jump target plus one (if forwards).
*/
linenum = adjsline(jump_sline);
pos = position(linenum);
if (search_type & SRCH_FORW)
pos = forw_raw_line(pos, (char **)NULL);
}
return (pos);
}
/*
* Search a subset of the file, specified by start/end position.
*/
static int
search_range(pos, endpos, search_type, n, plinepos, pendpos)
POSITION pos;
POSITION endpos;
int search_type;
int n;
POSITION *plinepos;
POSITION *pendpos;
{
char *line;
int linenum;
char *sp, *ep;
int line_match;
POSITION linepos, oldpos;
linenum = find_linenum(pos);
oldpos = pos;
for (;;)
{
/*
* Get lines until we find a matching one or until
* we hit end-of-file (or beginning-of-file if we're
* going backwards), or until we hit the end position.
*/
if (ABORT_SIGS())
{
/*
* A signal aborts the search.
*/
return (-1);
}
if (endpos != NULL_POSITION && pos >= endpos)
{
/*
* Reached end position without a match.
*/
if (pendpos != NULL)
*pendpos = pos;
return (n);
}
if (search_type & SRCH_FORW)
{
/*
* Read the next line, and save the
* starting position of that line in linepos.
*/
linepos = pos;
pos = forw_raw_line(pos, &line);
if (linenum != 0)
linenum++;
} else
{
/*
* Read the previous line and save the
* starting position of that line in linepos.
*/
pos = back_raw_line(pos, &line);
linepos = pos;
if (linenum != 0)
linenum--;
}
if (pos == NULL_POSITION)
{
/*
* Reached EOF/BOF without a match.
*/
if (pendpos != NULL)
*pendpos = NULL_POSITION;
return (n);
}
/*
* If we're using line numbers, we might as well
* remember the information we have now (the position
* and line number of the current line).
* Don't do it for every line because it slows down
* the search. Remember the line number only if
* we're "far" from the last place we remembered it.
*/
if (linenums && abs((int)(pos - oldpos)) > 1024)
{
add_lnum(linenum, pos);
oldpos = pos;
}
/*
* If it's a caseless search, convert the line to lowercase.
* If we're doing backspace processing, delete backspaces.
*/
if (is_caseless || bs_mode == BS_SPECIAL)
{
int ops = 0;
if (is_caseless)
ops |= CVT_TO_LC;
if (bs_mode == BS_SPECIAL)
ops |= CVT_BS;
cvt_text(line, line, ops);
}
/*
* Test the next line to see if we have a match.
* We are successful if we either want a match and got one,
* or if we want a non-match and got one.
*/
line_match = match_pattern(line, &sp, &ep);
line_match = (!(search_type & SRCH_NOMATCH) && line_match) ||
((search_type & SRCH_NOMATCH) && !line_match);
if (!line_match)
continue;
/*
* Got a match.
*/
if (search_type & SRCH_FIND_ALL)
{
#if HILITE_SEARCH
/*
* We are supposed to find all matches in the range.
* Just add the matches in this line to the
* hilite list and keep searching.
*/
if (line_match)
hilite_line(linepos, line, sp, ep);
#endif
} else if (--n <= 0)
{
/*
* Found the one match we're looking for.
* Return it.
*/
#if HILITE_SEARCH
if (hilite_search == 1)
{
/*
* Clear the hilite list and add only
* the matches in this one line.
*/
clr_hilite();
if (line_match)
hilite_line(linepos, line, sp, ep);
}
#endif
if (plinepos != NULL)
*plinepos = linepos;
return (0);
}
}
}
/*
* Search for the n-th occurrence of a specified pattern,
* either forward or backward.
* Return the number of matches not yet found in this file
* (that is, n minus the number of matches found).
* Return -1 if the search should be aborted.
* Caller may continue the search in another file
* if less than n matches are found in this file.
*/
public int
search(search_type, pattern, n)
int search_type;
char *pattern;
int n;
{
POSITION pos;
int ucase;
if (pattern == NULL || *pattern == '\0')
{
/*
* A null pattern means use the previously compiled pattern.
*/
if (!prev_pattern())
{
error("No previous regular expression", NULL_PARG);
return (-1);
}
#if HILITE_SEARCH
if (hilite_search == OPT_ON)
{
/*
* Erase the highlights currently on screen.
* If the search fails, we'll redisplay them later.
*/
repaint_hilite(0);
}
if (hilite_search == OPT_ONPLUS && hide_hilite)
{
/*
* Highlight any matches currently on screen,
* before we actually start the search.
*/
hide_hilite = 0;
hilite_screen();
}
hide_hilite = 0;
#endif
} else
{
/*
* Compile the pattern.
*/
ucase = is_ucase(pattern);
if (caseless == OPT_ONPLUS)
cvt_text(pattern, pattern, CVT_TO_LC);
if (compile_pattern(pattern) < 0)
return (-1);
/*
* Ignore case if -I is set OR
* -i is set AND the pattern is all lowercase.
*/
is_ucase_pattern = ucase;
if (is_ucase_pattern && caseless != OPT_ONPLUS)
is_caseless = 0;
else
is_caseless = caseless;
#if HILITE_SEARCH
if (hilite_search)
{
/*
* Erase the highlights currently on screen.
* Also permanently delete them from the hilite list.
*/
repaint_hilite(0);
hide_hilite = 0;
clr_hilite();
}
if (hilite_search == OPT_ONPLUS)
{
/*
* Highlight any matches currently on screen,
* before we actually start the search.
*/
hilite_screen();
}
#endif
}
/*
* Figure out where to start the search.
*/
pos = search_pos(search_type);
if (pos == NULL_POSITION)
{
/*
* Can't find anyplace to start searching from.
*/
if (search_type & SRCH_PAST_EOF)
return (n);
error("Nothing to search", NULL_PARG);
return (-1);
}
n = search_range(pos, NULL_POSITION, search_type, n,
&pos, (POSITION*)NULL);
if (n != 0)
{
/*
* Search was unsuccessful.
*/
#if HILITE_SEARCH
if (hilite_search == OPT_ON && n > 0)
/*
* Redisplay old hilites.
*/
repaint_hilite(1);
#endif
return (n);
}
/*
* Go to the matching line.
*/
jump_loc(pos, jump_sline);
#if HILITE_SEARCH
if (hilite_search == OPT_ON)
/*
* Display new hilites in the matching line.
*/
repaint_hilite(1);
#endif
return (0);
}
#if HILITE_SEARCH
/*
* Prepare hilites in a given range of the file.
*
* The pair (prep_startpos,prep_endpos) delimits a contiguous region
* of the file that has been "prepared"; that is, scanned for matches for
* the current search pattern, and hilites have been created for such matches.
* If prep_startpos == NULL_POSITION, the prep region is empty.
* If prep_endpos == NULL_POSITION, the prep region extends to EOF.
* prep_hilite asks that the range (spos,epos) be covered by the prep region.
*/
public void
prep_hilite(spos, epos)
POSITION spos;
POSITION epos;
{
POSITION nprep_startpos = prep_startpos;
POSITION nprep_endpos = prep_endpos;
/*
* Search beyond where we're asked to search, so the prep region covers
* more than we need. Do one big search instead of a bunch of small ones.
*/
#define SEARCH_MORE (3*size_linebuf)
if (!prev_pattern())
return;
/*
* Find two ranges:
* The range that we need to search (spos,epos); and the range that
* the "prep" region will then cover (nprep_startpos,nprep_endpos).
*/
if (prep_startpos == NULL_POSITION ||
(epos != NULL_POSITION && epos < prep_startpos) ||
(prep_endpos != NULL_POSITION && spos > prep_endpos))
{
/*
* New range is not contiguous with old prep region.
* Discard the old prep region and start a new one.
*/
clr_hilite();
if (epos != NULL_POSITION)
epos += SEARCH_MORE;
nprep_startpos = spos;
nprep_endpos = epos;
} else
{
/*
* New range partially or completely overlaps old prep region.
*/
if (epos == NULL_POSITION)
{
/*
* New range goes to end of file.
*/
nprep_endpos = NULL_POSITION;
} else if (epos > prep_endpos)
{
/*
* New range ends after old prep region.
* Extend prep region to end at end of new range.
*/
epos += SEARCH_MORE;
nprep_endpos = epos;
} else /* (epos <= prep_endpos) */
{
/*
* New range ends within old prep region.
* Truncate search to end at start of old prep region.
*/
epos = prep_startpos;
}
if (spos < prep_startpos)
{
/*
* New range starts before old prep region.
* Extend old prep region backwards to start at
* start of new range.
*/
if (spos < SEARCH_MORE)
spos = 0;
else
spos -= SEARCH_MORE;
nprep_startpos = spos;
} else /* (spos >= prep_startpos) */
{
/*
* New range starts within or after old prep region.
* Trim search to start near end of old prep region
* (actually, one linebuf before end of old range).
*/
if (prep_endpos == NULL_POSITION)
return;
else if (prep_endpos < size_linebuf)
spos = 0;
else
spos = prep_endpos - size_linebuf;
}
}
if (epos == NULL_POSITION || epos > spos)
{
if (search_range(spos, epos, SRCH_FORW|SRCH_FIND_ALL, 0,
(POSITION*)NULL, &epos) >= 0)
{
if (epos == NULL_POSITION || epos > nprep_endpos)
nprep_endpos = epos;
}
}
prep_startpos = nprep_startpos;
prep_endpos = nprep_endpos;
}
#endif
#if NO_REGEX
/*
* We have no pattern matching function from the library.
* We use this function to do simple pattern matching.
* It supports no metacharacters like *, etc.
*/
static int
match(pattern, buf, pfound, pend)
char *pattern, *buf;
char **pfound, **pend;
{
register char *pp, *lp;
for ( ; *buf != '\0'; buf++)
{
for (pp = pattern, lp = buf; *pp == *lp; pp++, lp++)
if (*pp == '\0' || *lp == '\0')
break;
if (*pp == '\0')
{
if (pfound != NULL)
*pfound = buf;
if (pend != NULL)
*pend = lp;
return (1);
}
}
return (0);
}
#endif
#if HAVE_V8_REGCOMP
/*
* This function is called by the V8 regcomp to report
* errors in regular expressions.
*/
void
regerror(s)
char *s;
{
PARG parg;
parg.p_string = s;
error("%s", &parg);
}
#endif
#if !HAVE_STRCHR
/*
* strchr is used by regexp.c.
*/
char *
strchr(s, c)
char *s;
int c;
{
for ( ; *s != '\0'; s++)
if (*s == c)
return (s);
if (c == '\0')
return (s);
return (NULL);
}
#endif
|