-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.c
1870 lines (1644 loc) · 42.3 KB
/
search.c
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
/*
* The functions in this file implement commands that search in the forward
* and backward directions. There are no special characters in the search
* strings. Probably should have a regular expression search, or something
* like that.
*
* Aug. 1986 John M. Gamble:
* Made forward and reverse search use the same scan routine.
*
* Added a limited number of regular expressions - 'any',
* 'character class', 'closure', 'beginning of line', and
* 'end of line'.
*
* Replacement metacharacters will have to wait for a re-write of
* the replaces function, and a new variation of ldelete().
*
* For those curious as to my references, i made use of
* Kernighan & Plauger's "Software Tools."
* I deliberately did not look at any published grep or editor
* source (aside from this one) for inspiration. I did make use of
* Allen Hollub's bitmap routines as published in Doctor Dobb's Journal,
* June, 1985 and modified them for the limited needs of character class
* matching. Any inefficiences, bugs, stupid coding examples, etc.,
* are therefore my own responsibility.
*
* April 1987: John M. Gamble
* Deleted the "if (n == 0) n = 1;" statements in front of the
* search/hunt routines. Since we now use a do loop, these
* checks are unnecessary. Consolidated common code into the
* function delins(). Renamed global mclen matchlen,
* and added the globals matchline, matchoff, patmatch, and
* mlenold.
* This gave us the ability to unreplace regular expression searches,
* and to put the matched string into an evironment variable.
* SOON TO COME: Meta-replacement characters!
*
* 25-apr-87 DML
* - cleaned up an unneccessary if/else in forwsearch() and
* backsearch()
* - savematch() failed to malloc room for the terminating byte
* of the match string (stomp...stomp...). It does now. Also
* it now returns gracefully if malloc fails
*
* July 1987: John M. Gamble
* Set the variables matchlen and matchoff in the 'unreplace'
* section of replaces(). It would confuse the function
* savematch() if you replaced, unreplaced, then replaced
* again (serves you right for being so wishy-washy...)
*/
#include <stdio.h>
#include "estruct.h"
#include "edef.h"
#if MAGIC
#define MCNIL 0 /* Like the '\0' for strings.*/
#define LITCHAR 1
#define ANY 2
#define CCL 3
#define NCCL 4
#define BOL 5
#define EOL 6
#define DITTO 7
#define CLOSURE 0x0100 /* An or-able value for a closure modifier.*/
#define CLOSURE_1 0x0200 /* An or-able value for a closure modifier.*/
#define ZEROONE 0x0400 /* An or-able value for a closure modifier.*/
#define ALLCLOS (CLOSURE | CLOSURE_1 | ZEROONE)
#define MASKCLO 0x00ff
#define MC_ANY '.' /* 'Any' character (except newline).*/
#define MC_CCL '[' /* Character class.*/
#define MC_NCCL '^' /* Negate character class.*/
#define MC_RCCL '-' /* Range in character class.*/
#define MC_ECCL ']' /* End of character class.*/
#define MC_BOL '^' /* Beginning of line.*/
#define MC_EOL '$' /* End of line.*/
#define MC_CLOSURE '*' /* Closure - does not extend past newline.*/
#define MC_CLOSURE_1 '+' /* Closure - one to many characters match.*/
#define MC_ZEROONE '?' /* Closure - zero to one characters match.*/
#define MC_DITTO '&' /* Use matched string in replacement */
#define MC_ESC '\\' /* Escape - suppress meta-meaning.*/
#define BIT(n) (1 << (n)) /* An integer with one bit set.*/
#endif
#if LATTICE | C86 | DECUSC
#define void int
#endif
#if C86
#define STATIC /* */
#else
#if DECUSC
#define STATIC extern
#else
#define STATIC static
#endif
#endif
/* UCAST must map a possibly signed char to an unsigned value */
/* In standard C, (unsigned int) will sign extend first like (int) */
/* Not all compilers understand (unsigned char) */
#if LATTICE | C86 | DECUSC | BSD29 | V7 | CPM | WMCS
#define UCAST (unsigned int)
#else
#define UCAST (unsigned char)
#endif
STATIC int amatch PP((register MC *mcptr, int direct, LINE **pcwline, int *pcwoff));
STATIC int readpattern PP((CONSTA char *prompt, char *apat, int srch));
STATIC int replaces PP((int kind, int f, int n));
STATIC int nextch PP((LINE **pcurline, int *pcuroff, int dir));
STATIC int mcstr PP((void));
STATIC int mceq PP((int bc, MC *mt));
STATIC int cclmake PP((char **ppatptr, MC *mcptr));
STATIC int biteq PP((int bc, BITMAP cclmap));
STATIC int boundry PP((LINE *curline, int curoff, int dir));
STATIC VOID savematch PP((void));
STATIC int fbound PP((int jump, LINE **pcurline, int *pcuroff, int dir));
STATIC int delins PP((int dlength, char *instr, int usemeta));
#if MAGIC
STATIC void setbit PP((int bc, BITMAP cclmap));
STATIC BITMAP clearbits PP((void));
STATIC int mcscanner PP((MC *mcpatrn, int direct, int beg_or_end));
STATIC VOID rmcclear PP((void));
STATIC int rmcstr PP((void));
#endif
#if DECUSC
#define STATIC /* */
#endif
static int FAR deltaf[HICHAR];
static int FAR deltab[HICHAR];
static int lastchfjump, lastchbjump;
static int patlenadd;
/*
* forwsearch -- Search forward. Get a search string from the user, and
* search for the string. If found, reset the "." to be just after
* the match string, and (perhaps) repaint the display.
*/
int forwsearch(f, n)
int f, n; /* default flag / numeric argument */
{
register int status;
status = TRUE;
/* If n is negative, search backwards.
* Otherwise proceed by asking for the search string.
*/
if (n < 0)
return(backsearch(f, -n));
/* Ask the user for the text of a pattern. If the
* response is TRUE (responses other than FALSE are
* possible), search for the pattern for as long as
* n is positive (n == 0 will go through once, which
* is just fine).
*/
if ((status = readpattern("Search", &pat[0], TRUE)) == TRUE) {
do {
#if MAGIC
if (magical && (curwp->w_bufp->b_mode & MDMAGIC) != 0)
status = mcscanner(&mcpat[0], FORWARD, PTEND);
else
#endif
status = scanner(&pat[0], FORWARD, PTEND);
} while ((--n > 0) && status);
/* Save away the match, or complain
* if not there.
*/
if (status != TRUE)
mlwrite("Not found");
}
return(status);
}
/*
* forwhunt -- Search forward for a previously acquired search string.
* If found, reset the "." to be just after the match string,
* and (perhaps) repaint the display.
*/
int forwhunt(f, n)
int f, n; /* default flag / numeric argument */
{
register int status;
status = TRUE;
if (n < 0) /* search backwards */
return(backhunt(f, -n));
/* Make sure a pattern exists, or that we didn't switch
* into MAGIC mode until after we entered the pattern.
*/
if (pat[0] == '\0')
{
mlwrite("No pattern set");
return (FALSE);
}
#if MAGIC
if ((curwp->w_bufp->b_mode & MDMAGIC) != 0 &&
mcpat[0].mc_type == MCNIL)
{
if (!mcstr())
return (FALSE);
}
#endif
/* Search for the pattern for as long as
* n is positive (n == 0 will go through once, which
* is just fine).
*/
do
{
#if MAGIC
if (magical && (curwp->w_bufp->b_mode & MDMAGIC) != 0)
status = mcscanner(&mcpat[0], FORWARD, PTEND);
else
#endif
status = scanner(&pat[0], FORWARD, PTEND);
} while ((--n > 0) && status);
/* Save away the match, or complain
* if not there.
*/
if (status != TRUE)
mlwrite("Not found");
return(status);
}
/*
* backsearch -- Reverse search. Get a search string from the user, and
* search, starting at "." and proceeding toward the front of the buffer.
* If found "." is left pointing at the first character of the pattern
* (the last character that was matched).
*/
int backsearch(f, n)
int f, n; /* default flag / numeric argument */
{
register int status;
status = TRUE;
/* If n is negative, search forwards.
* Otherwise proceed by asking for the search string.
*/
if (n < 0)
return(forwsearch(f, -n));
/* Ask the user for the text of a pattern. If the
* response is TRUE (responses other than FALSE are
* possible), search for the pattern for as long as
* n is positive (n == 0 will go through once, which
* is just fine).
*/
if ((status = readpattern("Reverse search", &pat[0], TRUE)) == TRUE) {
do {
#if MAGIC
if (magical && (curwp->w_bufp->b_mode & MDMAGIC) != 0)
status = mcscanner(&tapcm[0], REVERSE, PTBEG);
else
#endif
status = scanner(&tap[0], REVERSE, PTBEG);
} while ((--n > 0) && status);
/* Save away the match, or complain
* if not there.
*/
if (status != TRUE)
mlwrite("Not found");
}
return(status);
}
/*
* backhunt -- Reverse search for a previously acquired search string,
* starting at "." and proceeding toward the front of the buffer.
* If found "." is left pointing at the first character of the pattern
* (the last character that was matched).
*/
int backhunt(f, n)
int f, n; /* default flag / numeric argument */
{
register int status;
status = TRUE;
if (n < 0)
return(forwhunt(f, -n));
/* Make sure a pattern exists, or that we didn't switch
* into MAGIC mode until after we entered the pattern.
*/
if (tap[0] == '\0')
{
mlwrite("No pattern set");
return (FALSE);
}
#if MAGIC
if ((curwp->w_bufp->b_mode & MDMAGIC) != 0 &&
tapcm[0].mc_type == MCNIL)
{
if (!mcstr())
return (FALSE);
}
#endif
/* Go search for it for as long as
* n is positive (n == 0 will go through once, which
* is just fine).
*/
do
{
#if MAGIC
if (magical && (curwp->w_bufp->b_mode & MDMAGIC) != 0)
status = mcscanner(&tapcm[0], REVERSE, PTBEG);
else
#endif
status = scanner(&tap[0], REVERSE, PTBEG);
} while ((--n > 0) && status);
/* Save away the match, or complain
* if not there.
*/
if (status != TRUE)
mlwrite("Not found");
return(status);
}
#if MAGIC
/*
* mcscanner -- Search for a meta-pattern in either direction. If found,
* reset the "." to be at the start or just after the match string,
* and (perhaps) repaint the display.
*/
STATIC int mcscanner(mcpatrn, direct, beg_or_end)
MC *mcpatrn; /* pointer into pattern */
int direct; /* which way to go.*/
int beg_or_end; /* put point at beginning or end of pattern.*/
{
LINE *curline; /* current line during scan */
int curoff; /* position within current line */
/* If we are going in reverse, then the 'end' is actually
* the beginning of the pattern. Toggle it.
*/
beg_or_end ^= direct;
/*
* Save the old matchlen length, in case it is
* horribly different (closure) from the old length.
* This is terribly important for query-replace undo
* command.
*/
mlenold = mtchlen;
/* Setup local scan pointers to global ".".
*/
curline = curwp->w_dotp;
curoff = curwp->w_doto;
/* Scan each character until we hit the head link record.
*/
while (!boundry(curline, curoff, direct))
{
/* Save the current position in case we need to
* restore it on a match, and initialize matchlen to
* zero in case we are doing a search for replacement.
*/
matchline = curline;
matchoff = curoff;
mtchlen = 0;
if (amatch(mcpatrn, direct, &curline, &curoff))
{
/* A SUCCESSFULL MATCH!!!
* reset the global "." pointers.
*/
if (beg_or_end == PTEND) /* at end of string */
{
curwp->w_dotp = curline;
curwp->w_doto = curoff;
}
else /* at beginning of string */
{
curwp->w_dotp = matchline;
curwp->w_doto = matchoff;
}
curwp->w_flag |= WFMOVE; /* flag that we have moved */
savematch();
return (TRUE);
}
/* Advance the cursor.
*/
nextch(&curline, &curoff, direct);
}
return (FALSE); /* We could not find a match.*/
}
/*
* amatch -- Search for a meta-pattern in either direction. Based on the
* recursive routine amatch() (for "anchored match") in
* Kernighan & Plauger's "Software Tools".
*/
STATIC int amatch(mcptr, direct, pcwline, pcwoff)
register MC *mcptr; /* string to scan for */
int direct; /* which way to go.*/
LINE **pcwline; /* current line during scan */
int *pcwoff; /* position within current line */
{
register int c; /* character at current position */
LINE *curline; /* current line during scan */
int curoff; /* position within current line */
int nchars; /* number of chars matched in closure.*/
int cl_min; /* minimum number of chars matched in closure.*/
int cl_type; /* Which closure type?.*/
/* Set up local scan pointers to ".", and get
* the current character. Then loop around
* the pattern pointer until success or failure.
*/
curline = *pcwline;
curoff = *pcwoff;
/* The beginning-of-line and end-of-line metacharacters
* do not compare against characters, they compare
* against positions.
* BOL is guaranteed to be at the start of the pattern
* for forward searches, and at the end of the pattern
* for reverse searches. The reverse is true for EOL.
* So, for a start, we check for them on entry.
*/
if (mcptr->mc_type == BOL)
{
if (curoff != 0)
return (FALSE);
mcptr++;
}
if (mcptr->mc_type == EOL)
{
if (curoff != llength(curline))
return (FALSE);
mcptr++;
}
while (mcptr->mc_type != MCNIL)
{
c = nextch(&curline, &curoff, direct);
/* Is the current meta-character modified
* by a closure?
*/
if ((cl_type = (mcptr->mc_type & ALLCLOS)) != 0)
{
/* Try to match as many characters as possible
* against the current meta-character. A
* newline never matches a closure.
*/
nchars = 0;
cl_min = 0;
if (cl_type == ZEROONE) {
if (mceq(c, mcptr)) {
nextch(&curline, &curoff, direct);
nchars++;
}
} else {
/* Match as many characters as possible
* against the current meta-character.
*/
while (mceq(c, mcptr)) {
c = nextch(&curline, &curoff, direct);
nchars++;
}
/* Minimum number of characters that may
* match is 0 or 1.
*/
if (cl_type == CLOSURE_1) cl_min = 1;
}
/* We are now at the character that made us
* fail. Try to match the rest of the pattern.
* Shrink the closure by one for each failure.
*/
mcptr++;
for (;;) {
if (nchars < cl_min)
return FALSE;
nextch(&curline, &curoff, direct ^ REVERSE);
if (amatch(mcptr, direct, &curline, &curoff)) {
mtchlen += nchars;
goto success;
}
nchars--;
}
}
else /* Not closure.*/
{
/* The only way we'd get a BOL metacharacter
* at this point is at the end of the reversed pattern.
* The only way we'd get an EOL metacharacter
* here is at the end of a regular pattern.
* So if we match one or the other, and are at
* the appropriate position, we are guaranteed success
* (since the next pattern character has to be MCNIL).
* Before we report success, however, we back up by
* one character, so as to leave the cursor in the
* correct position. For example, a search for ")$"
* will leave the cursor at the end of the line, while
* a search for ")<NL>" will leave the cursor at the
* beginning of the next line. This follows the
* notion that the meta-character '$' (and likewise
* '^') match positions, not characters.
*/
if (mcptr->mc_type == BOL) {
if (curoff == llength(curline))
{
nextch(&curline, &curoff,
direct ^ REVERSE);
goto success;
}
else
return (FALSE);
}
if (mcptr->mc_type == EOL) {
if (curoff == 0)
{
nextch(&curline, &curoff,
direct ^ REVERSE);
goto success;
}
else
return (FALSE);
}
/* Neither BOL nor EOL, so go through
* the meta-character equal function.
*/
if (!mceq(c, mcptr))
return (FALSE);
}
/* Increment the length counter and
* advance the pattern pointer.
*/
mtchlen++;
mcptr++;
} /* End of mcptr loop.*/
/* A SUCCESSFULL MATCH!!!
* Reset the "." pointers.
*/
success:
*pcwline = curline;
*pcwoff = curoff;
return (TRUE);
}
#endif
/*
* scanner -- Search for a pattern in either direction. If found,
* reset the "." to be at the start or just after the match string,
* and (perhaps) repaint the display.
* Fast version using simplified version of Boyer and Moore
* Software-Practice and Experience, vol 10, 501-506 (1980)
*/
int scanner(patrn, direct, beg_or_end)
char *patrn; /* string to scan for */
int direct; /* which way to go.*/
int beg_or_end; /* put point at beginning or end of pattern.*/
{
register int c; /* character at current position */
register char *patptr; /* pointer into pattern */
LINE *curline; /* current line during scan */
int curoff; /* position within current line */
LINE *scanline; /* current line during scanning */
int scanoff; /* position in scanned line */
int jump; /* next offset */
int bc;
/* If we are going in reverse, then the 'end' is actually
* the beginning of the pattern. Toggle it.
*/
beg_or_end ^= direct;
/* Set up local pointers to global ".".
*/
curline = curwp->w_dotp;
curoff = curwp->w_doto;
if (direct == FORWARD && curline == curbp->b_linep) return(FALSE);
/* Scan each character until we hit the head link record.
* Get the character resolving newlines, offset
* by the pattern length, i.e. the last character of the
* potential match.
*/
jump = patlenadd;
while (!fbound(jump, &curline, &curoff, direct))
{
/* Save the current position in case we match
* the search string at this point.
*/
matchline = curline;
matchoff = curoff;
/* Setup scanning pointers.
*/
scanline = curline;
scanoff = curoff;
patptr = &patrn[0];
/* Scan through the pattern for a match.
*/
while ((c = UCAST *patptr++) != '\0') {
/* if(!me_eq((int)c,nextch(&scanline,&scanoff,direct))) */
bc = UCAST nextch(&scanline,&scanoff,direct);
if ((curwp->w_bufp->b_mode & MDEXACT) == 0)
{
if (islower(c)) c ^= DIFCASE;
if (islower(bc)) bc ^= DIFCASE;
}
if (c != bc)
{
jump = (direct == FORWARD)
? lastchfjump
: lastchbjump;
goto fail;
}
}
/* A SUCCESSFULL MATCH!!!
* reset the global "." pointers
*/
if (beg_or_end == PTEND) /* at end of string */
{
curwp->w_dotp = scanline;
curwp->w_doto = scanoff;
}
else /* at beginning of string */
{
curwp->w_dotp = matchline;
curwp->w_doto = matchoff;
}
curwp->w_flag |= WFMOVE; /* Flag that we have moved.*/
savematch();
return TRUE;
fail:; /* continue to search */
}
return FALSE; /* We could not find a match */
}
/*
* fbound -- Return information depending on whether we have hit a boundry
* (and may therefore search no further) or if a trailing character
* of the search string has been found. See boundry() for search
* restrictions.
*/
static int fbound(jump, pcurline, pcuroff, dir)
LINE **pcurline;
int *pcuroff, dir, jump;
{
register int spare, curoff;
register LINE *curline;
curline = *pcurline;
curoff = *pcuroff;
if (dir == FORWARD)
{
while (jump != 0)
{
curoff += jump;
spare = curoff - llength(curline);
while (spare > 0)
{
curline = lforw(curline);/* skip to next line */
curoff = spare - 1;
spare = curoff - llength(curline);
if (curline == curbp->b_linep)
return TRUE; /* hit end of buffer */
}
if (spare == 0)
jump = deltaf[UCAST '\n'];
else
jump = deltaf[UCAST lgetc(curline, curoff)];
}
/* The last character matches, so back up to start
* of possible match.
*/
curoff -= patlenadd;
while (curoff < 0)
{
curline = lback(curline);/* skip back a line */
curoff += llength(curline) + 1;
}
}
else /* Reverse.*/
{
jump++; /* allow for offset in reverse */
while (jump != 0)
{
curoff -= jump;
while (curoff < 0)
{
curline = lback(curline); /* skip back a line */
curoff += llength(curline) + 1;
if (curline == curbp->b_linep)
return TRUE; /* hit end of buffer */
}
if (curoff == llength(curline))
jump = deltab[UCAST '\n'];
else
jump = deltab[UCAST lgetc(curline, curoff)];
}
/* The last character matches, so back up to start
* of possible match.
*/
curoff += mtchlen;
spare = curoff - llength(curline);
while (spare > 0)
{
curline = lforw(curline);/* skip back a line */
curoff = spare - 1;
spare = curoff - llength(curline);
}
}
*pcurline = curline;
*pcuroff = curoff;
return FALSE;
}
/*
* setjtable -- Settting up search delta forward and delta backward
* tables. The reverse search string and string lengths are
* set here, for table initialization and for substitution
* purposes. The default for any character to jump is the
* pattern length.
*/
VOID setjtable()
{
char *apat;
int i;
apat = pat;
rvstrcpy(tap, apat);
patlenadd = (mlenold = mtchlen = (int) strlen(apat)) - 1;
for (i = 0; i < HICHAR; i++)
{
deltaf[i] = mtchlen;
deltab[i] = mtchlen;
}
/* Now put in the characters contained
* in the pattern, duplicating the CASE
*/
for (i = 0; i < patlenadd; i++)
{
#if 0
/*
* Debugging & tracing information.
*/
mlwrite("Considering %d with jump %d",
UCAST apat[i], patlenadd - i);
tgetc();
if (isletter(apat[i]))
{
mlwrite("Its other case is %d",
CHCASE[UCAST apat[i]));
tgetc();
}
#endif
deltaf[UCAST apat[i]] = patlenadd - i;
if (isletter(apat[i]))
deltaf[UCAST CHCASE(UCAST apat[i])]
= patlenadd - i;
deltab[UCAST tap[i]] = patlenadd - i;
if (isletter(tap[i]))
deltab[UCAST CHCASE(UCAST tap[i])]
= patlenadd - i;
}
/* The last character will have the pattern length
* unless there are duplicates of it. Get the number to
* jump from the arrays delta, and overwrite with zeroes in delta
* duplicating the CASE.
*/
lastchfjump = patlenadd + deltaf[UCAST apat[patlenadd]];
lastchbjump = patlenadd + deltab[UCAST apat[0]];
deltaf[UCAST apat[patlenadd]] = 0;
if (isletter(apat[patlenadd]))
deltaf[UCAST CHCASE(apat[patlenadd])] = 0;
deltab[UCAST apat[0]] = 0;
if (isletter(apat[0]))
deltab[UCAST CHCASE(apat[0])] = 0;
}
/*
* eq -- Compare two characters. The "bc" comes from the buffer, "pc"
* from the pattern. If we are not in EXACT mode, fold out the case.
*/
int me_eq(bc, pc)
register int bc;
register int pc;
{
if ((curwp->w_bufp->b_mode & MDEXACT) == 0)
{
if (islower(bc))
bc ^= DIFCASE;
if (islower(pc))
pc ^= DIFCASE;
}
return (bc == pc);
}
/*
* readpattern -- Read a pattern. Stash it in apat. If it is the
* search string, create the reverse pattern and the magic
* pattern, assuming we are in MAGIC mode (and defined that way).
* Apat is not updated if the user types in an empty line. If
* the user typed an empty line, and there is no old pattern, it is
* an error. Display the old pattern, in the style of Jeff Lomicka.
* There is some do-it-yourself control expansion. Change to using
* <META> to delimit the end-of-pattern to allow <NL>s in the search
* string.
*/
STATIC int readpattern(prompt, apat, srch)
CONSTA char *prompt;
char apat[];
int srch;
{
int status;
char tpat[NPAT+20];
strcpy(tpat, prompt); /* copy prompt to output string */
strcat(tpat, " ["); /* build new prompt string */
expandp(&apat[0], &tpat[strlen(tpat)], NPAT/2); /* add old pattern */
strcat(tpat, "]<META>: ");
/* Read a pattern. Either we get one,
* or we just get the META charater, and use the previous pattern.
* Then, if it's the search string, make a reversed pattern.
* *Then*, make the meta-pattern, if we are defined that way.
*/
if ((status = mltreply(tpat, tpat, NPAT, metac)) == TRUE)
{
strcpy(apat, tpat);
if (srch) /* If we are doing the search string.*/
{
/* If we are doing the search string set the
* delta tables.
*/
setjtable();
}
#if MAGIC
/* Only make the meta-pattern if in magic mode,
* since the pattern in question might have an
* invalid meta combination.
*/
if ((curwp->w_bufp->b_mode & MDMAGIC) == 0) {
mcclear();
rmcclear();
}
else
status = (srch? mcstr(): rmcstr());
#endif
}
else if (status == FALSE && apat[0] != 0) /* Old one */
status = TRUE;
return(status);
}
/*
* savematch -- We found the pattern? Let's save it away.
*/
static VOID savematch()
{
register char *ptr; /* ptr into malloced last match string */
register unsigned int j; /* index */
LINE *curline; /* line of last match */
int curoff; /* offset " " */
/* free any existing match string */
if (patmatch != NULL)
free(patmatch);
/* attempt to allocate a new one */
ptr = patmatch = malloc(mtchlen + 1);
if (ptr == NULL)
return;
/* save the match! */
curoff = matchoff;
curline = matchline;
for (j = 0; j < mtchlen; j++)
*ptr++ = (char) nextch(&curline, &curoff, FORWARD);
/* null terminate the match string */
*ptr = '\0';
}
/*
* rvstrcpy -- Reverse string copy.
*/
VOID rvstrcpy(rvstr, str)
register char *rvstr, *str;
{
register int i;
str += (i = (int) strlen(str));
while (i-- > 0)
*rvstr++ = *--str;
*rvstr = '\0';
}
/*
* sreplace -- Search and replace.
*/
int sreplace(f, n)
int f; /* default flag */
int n; /* # of repetitions wanted */
{
return(replaces(FALSE, f, n));
}
/*
* qreplace -- search and replace with query.
*/
int qreplace(f, n)
int f; /* default flag */
int n; /* # of repetitions wanted */
{
return(replaces(TRUE, f, n));
}
/*
* replaces -- Search for a string and replace it with another
* string. Query might be enabled (according to kind).
*/
STATIC int replaces(kind, f, n)
int kind; /* Query enabled flag */
int f; /* default flag */
int n; /* # of repetitions wanted */
{
register int status; /* success flag on pattern inputs */
register int rlength; /* length of replacement string */
register int numsub; /* number of substitutions */
register int nummatch; /* number of found matches */
int nlflag; /* last char of search string a <NL>? */
int nlrepl; /* was a replace done on the last line? */
char c; /* input char for query */