Debian Codegraph @

re.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 /* search package */
9
10 #include "jove.h"
11 #include "re.h"
12 #include "jctype.h"
13 #include "ask.h"
14 #include "disp.h"
15 #include "fmt.h"
16 #include "marks.h"
17
18 private jbool
19 do_comp(struct RE_block *,int);
20
21 char
22 rep_search[128], /* replace search string */
23 rep_str[128]; /* contains replacement string */
24
25 jbool
26 CaseIgnore = NO, /* VAR: ignore case in search */
27 WrapScan = NO; /* VAR: make searches wrap */
28
29 private ZXchar REpeekc;
30 private const char *REptr;
31
32 private ZXchar
33 REgetc(void)
34 {
35 ZXchar c;
36
37 if ((c = REpeekc) != EOF)
38 REpeekc = EOF;
39 else if (*REptr != '\0')
40 c = ZXC(*REptr++);
41 else
42 c = EOF;
43
44 return c;
45 }
46
47 #define STAR 01 /* Match any number of last RE (ORed into other ops). */
48
49 #define AT_BOL 2 /* ^ */
50 #define AT_EOL 4 /* $ */
51 #define AT_BOW 6 /* \< */
52 #define AT_EOW 8 /* \> */
53 #define OPENP 10 /* \( {chunk number} */
54 #define CLOSEP 12 /* \) {chunk number} */
55 #define CURLYB 14 /* \{ {number of alt, alts } */
56
57 #define NOSTR 14 /* Codes <= NOSTR can't be *'d. */
58
59 #define ANYC (NOSTR+2) /* . */
60 #define NORMC (ANYC+2) /* normal chars {len, char...} */
61 #define CINDC (NORMC+2) /* case independent chars {len, char...} */
62 #define ONE_OF (CINDC+2) /* [xxx] {bitmask} */
63 #define NONE_OF (ONE_OF+2) /* [^xxx] {bitmask} */
64 #define BACKREF (NONE_OF+2) /* \# {chunk number} */
65 #define EOP (BACKREF+2) /* end of pattern */
66
67 #define CHAR_MASK ((1 << CHAR_BIT) - 1) /* byte mask, really */
68 #define ALT_LEN_LEN 2 /* an alt starts with a two-byte length */
69 #define ALT_LEN(p) (((p)[0] & CHAR_MASK) + (((p)[1] & CHAR_MASK) << CHAR_BIT))
70
71 /* ONE_OF/NONE_OF is represented as a bit vector.
72 * These symbols parameterize the representation.
73 */
74
75 #define SETSIZE (NCHARS / CHAR_BIT)
76 #define SETBYTE(c) ((c) / CHAR_BIT)
77 #define SETBIT(c) (1 << ((c) % CHAR_BIT))
78
79 #define NPAR 10 /* [0-9] - 0th is the entire matched string, i.e. & */
80 private char *comp_ptr,
81 **alt_p,
82 **alt_endp;
83
84 void
85 REcompile(const char *pattern, jbool re, struct RE_block *re_blk)
86 {
87 REptr = pattern;
88 REpeekc = EOF;
89 comp_ptr = re_blk->r_compbuf;
90 alt_p = re_blk->r_alternates;
91 alt_endp = alt_p + NALTS - 1;
92 *alt_p++ = comp_ptr;
93 re_blk->r_nparens = 0;
94 (void) do_comp(re_blk, re ? OKAY_RE : NORM);
95 *alt_p = NULL;
96
97 re_blk->r_anchored = NO;
98 re_blk->r_firstc = EOF;
99 /* do a little post processing */
100 if (re_blk->r_alternates[1] == NULL) {
101 char *p;
102
103 p = re_blk->r_alternates[0];
104 for (;;) {
105 switch (*p) {
106 case OPENP:
107 case CLOSEP:
108 p += 2;
109 continue;
110
111 case AT_BOW:
112 case AT_EOW:
113 p += 1;
114 continue;
115
116 case AT_BOL:
117 re_blk->r_anchored = YES;
118 /* don't set firstc -- won't work */
119 break;
120
121 case NORMC:
122 case CINDC:
123 re_blk->r_firstc = CharUpcase(p[2]);
124 break;
125
126 default:
127 break;
128 }
129 break;
130 }
131 }
132 }
133
134 /* compile the pattern into an internal code */
135
136 private jbool
137 do_comp(struct RE_block *re_blk, int kind)
138 {
139 char *this_verb,
140 *prev_verb,
141 *start_p,
142 *comp_endp;
143 int parens[NPAR],
144 *parenp,
145 outer_max_paren = -1;
146 ZXchar c;
147 jbool done_cb = NO;
148
149 parenp = parens;
150 this_verb = NULL;
151 comp_endp = &re_blk->r_compbuf[COMPSIZE - 6];
152
153 /* wrap the whole expression around (implied) parens */
154 if (kind != IN_CB) {
155 if (re_blk->r_nparens >= NPAR) {
156 complain("Too many ('s; max is %d.", NPAR);
157 /* NOTREACHED */
158 }
159 *comp_ptr++ = OPENP;
160 *parenp++ = *comp_ptr++ = re_blk->r_nparens++;
161 }
162
163 start_p = comp_ptr;
164
165 while ((c = REgetc()) != EOF) {
166 if (comp_ptr > comp_endp) {
167 toolong:
168 complain("Search string too long/complex.");
169 /* NOTREACHED */
170 }
171 prev_verb = this_verb;
172 this_verb = comp_ptr;
173
174 /* The following test ought to be
175 * kind == NORM && c != '\\'
176 * but Jon likes to put ^, $, and \ in i-searches.
177 * Don't tell him, but $ only sort of works. -- DHR
178 */
179 if (kind == NORM && strchr("^$\\", c) == NULL)
180 goto defchar;
181 switch (c) {
182 case '\\':
183 switch (c = REgetc()) {
184 case EOF:
185 complain("[Premature end of pattern]");
186 /*NOTREACHED*/
187
188 case '{':
189 {
190 char *altcntp; /* alternate count */
191 int
192 init_paren = re_blk->r_nparens,
193 max_paren = -1;
194
195 *comp_ptr++ = CURLYB;
196 altcntp = comp_ptr;
197 *comp_ptr++ = 0; /* initialize alt-count */
198 for (;;) {
199 char *comp_len = comp_ptr;
200 jbool done;
201 long len;
202
203 comp_ptr += ALT_LEN_LEN;
204 re_blk->r_nparens = init_paren;
205 done = do_comp(re_blk, IN_CB);
206
207 /* We demand that each alternate has the same number
208 * of parens because we currently have no mechanism to
209 * set the matching strings to a meaningful default.
210 */
211 if (max_paren == -1)
212 max_paren = re_blk->r_nparens;
213 if (max_paren != re_blk->r_nparens) {
214 complain("[each alternate must have the same number of \\( \\)]");
215 /* NOTREACHED */
216 }
217 len = comp_ptr - comp_len;
218 comp_len[0] = (char) len; /* truncate */
219 comp_len[1] = (char) (len >> CHAR_BIT); /* truncate */
220 (*altcntp) += 1;
221 if (done)
222 break;
223 }
224 break;
225 }
226
227 case '}':
228 if (kind != IN_CB) {
229 complain("Unexpected \\}.");
230 /* NOTREACHED */
231 }
232 done_cb = YES;
233 goto outahere;
234
235 case '(':
236 if (re_blk->r_nparens >= NPAR) {
237 complain("Too many ('s; max is %d.", NPAR);
238 /* NOTREACHED */
239 }
240 *comp_ptr++ = OPENP;
241 *parenp++ = *comp_ptr++ = re_blk->r_nparens++;
242 break;
243
244 case ')':
245 if (parenp == parens) {
246 complain("Too many )'s.");
247 /* NOTREACHED */
248 }
249 *comp_ptr++ = CLOSEP;
250 *comp_ptr++ = *--parenp;
251 break;
252
253 case '|':
254 if (kind == IN_CB)
255 goto outahere;
256 if (alt_p >= alt_endp) {
257 complain("Too many alternates; max %d.", NALTS);
258 /* NOTREACHED */
259 }
260 /* close off previous alternate */
261 *comp_ptr++ = CLOSEP;
262 *comp_ptr++ = *--parenp;
263 if (parenp != parens) {
264 complain("Unmatched \\(.");
265 /* NOTREACHED */
266 }
267 *comp_ptr++ = EOP;
268
269 /* We demand that each alternate has the same number
270 * of parens because we currently have no mechanism to
271 * set the matching strings to a meaningful default.
272 */
273 if (outer_max_paren == -1)
274 outer_max_paren = re_blk->r_nparens;
275 if (outer_max_paren != re_blk->r_nparens) {
276 complain("[each alternate must have the same number of \\( \\)]");
277 /* NOTREACHED */
278 }
279 /* start a new alt */
280 *alt_p++ = comp_ptr;
281 re_blk->r_nparens = 0;
282 *comp_ptr++ = OPENP;
283 *parenp++ = *comp_ptr++ = re_blk->r_nparens++;
284 start_p = comp_ptr;
285 break;
286
287 case '1':
288 case '2':
289 case '3':
290 case '4':
291 case '5':
292 case '6':
293 case '7':
294 case '8':
295 case '9':
296 *comp_ptr++ = BACKREF;
297 *comp_ptr++ = c - '0';
298 break;
299
300 case '<':
301 *comp_ptr++ = AT_BOW;
302 break;
303
304 case '>':
305 *comp_ptr++ = AT_EOW;
306 break;
307
308 default:
309 goto defchar;
310 }
311 break;
312
313 case '.':
314 *comp_ptr++ = ANYC;
315 break;
316
317 case '^':
318 if (comp_ptr == start_p) {
319 *comp_ptr++ = AT_BOL;
320 break;
321 }
322 goto defchar;
323
324 case '$':
325 if ((REpeekc = REgetc()) != EOF && REpeekc != '\\')
326 goto defchar;
327 *comp_ptr++ = AT_EOL;
328 break;
329
330 case '[':
331 *comp_ptr++ = ONE_OF;
332 if (comp_ptr + SETSIZE >= comp_endp)
333 goto toolong;
334 byte_zero(comp_ptr, (size_t) SETSIZE);
335 c = REgetc();
336 if (c == '^') {
337 *this_verb = NONE_OF;
338 c = REgetc();
339 }
340 do {
341 if (c == EOF)
342 break;
343
344 if (c == '\\') {
345 c = REgetc();
346 if (c == EOF)
347 break;
348 }
349 if ((REpeekc = REgetc()) == '-') {
350 /* possibly a range */
351 ZXchar i = c;
352
353 REpeekc = EOF; /* discard '-' */
354 c = REgetc();
355 if (c == EOF)
356 break;
357
358 if (c == ']') {
359 /* not a range after all */
360 REpeekc = c; /* push back ']' */
361 c = '-'; /* recycle '-' */
362 comp_ptr[SETBYTE(i)] |= SETBIT(i); /* handle initial char */
363 } else {
364 /* really a range: add members up to c */
365 if (c == '\\') {
366 c = REgetc();
367 if (c == EOF)
368 break;
369 }
370 while (i < c) {
371 comp_ptr[SETBYTE(i)] |= SETBIT(i);
372 i += 1;
373 }
374 }
375 }
376 comp_ptr[SETBYTE(c)] |= SETBIT(c);
377 c = REgetc();
378 } while (c != ']');
379 if (c == EOF) {
380 complain("Missing ].");
381 /* NOTREACHED */
382 }
383 comp_ptr += SETSIZE;
384 break;
385
386 case '*':
387 if (prev_verb == NULL || *prev_verb <= NOSTR || (*prev_verb&STAR)!=0)
388 goto defchar;
389
390 if (*prev_verb == NORMC || *prev_verb == CINDC) {
391 char lastc = comp_ptr[-1];
392
393 /* The * operator applies only to the
394 * previous character. Since we were
395 * building a string-matching command
396 * (NORMC or CINDC), we must split it
397 * up and work with the last character.
398 *
399 * Note that the STARed versions of these
400 * commands do not operate on strings, and
401 * so do not need or have character counts.
402 */
403
404 if (prev_verb[1] == 1) {
405 /* Only one char in string:
406 * delete old command.
407 */
408 this_verb = prev_verb;
409 } else {
410 /* Several chars in string:
411 * strip off the last.
412 * New verb is derived from old.
413 */
414 prev_verb[1] -= 1;
415 this_verb -= 1;
416 *this_verb = *prev_verb;
417 }
418 comp_ptr = this_verb + 1;
419 *comp_ptr++ = lastc;
420 } else {
421 /* This command is just the previous one,
422 * whose verb we will modify.
423 */
424 this_verb = prev_verb;
425 }
426 *this_verb |= STAR;
427 break;
428 default:
429 defchar:
430 if (prev_verb == NULL
431 || !(*prev_verb == NORMC || *prev_verb == CINDC))
432 {
433 /* create new string command */
434 *comp_ptr++ = (CaseIgnore) ? CINDC : NORMC;
435 *comp_ptr++ = 0;
436 } else {
437 /* merge this into previous string command */
438 this_verb = prev_verb;
439 }
440 this_verb[1] += 1;
441 *comp_ptr++ = c;
442 break;
443 }
444 }
445 outahere:
446
447 /* End of pattern, let's do some error checking. */
448 if (kind != IN_CB) {
449 *comp_ptr++ = CLOSEP;
450 *comp_ptr++ = *--parenp;
451 }
452 if (parenp != parens) {
453 complain("Unmatched \\(.");
454 /* NOTREACHED */
455 }
456 if (kind == IN_CB && c == EOF) { /* end of pattern with missing \}. */
457 complain("Missing \\}.");
458 /* NOTREACHED */
459 }
460 *comp_ptr++ = EOP;
461
462 /* We demand that each alternate has the same number
463 * of parens because we currently have no mechanism to
464 * set the matching strings to a meaningful default.
465 */
466 if (outer_max_paren != -1 && outer_max_paren != re_blk->r_nparens) {
467 complain("[each alternate must have the same number of \\( \\)]");
468 /* NOTREACHED */
469 }
470 return done_cb;
471 }
472
473 private char *pstrtlst[NPAR], /* index into re_blk->r_lbuf */
474 *pendlst[NPAR],
475 *REbolp, /* begining-of-line pointer */
476 *locrater, /* roof of last substitution */
477 *loc1, /* start of matched text */
478 *loc2; /* roof of matched text */
479
480 int REbom, /* beginning and end columns of match */
481 REeom,
482 REdelta; /* increase in line length due to last re_dosub */
483
484 private jbool
485 backref(int n, char *linep)
486 {
487 char *backsp = pstrtlst[n],
488 *backep = pendlst[n];
489
490 while (*backsp++ == *linep++)
491 if (backsp >= backep)
492 return YES;
493 return NO;
494 }
495
496 private jbool
497 member(char *comp_ptr, ZXchar c, jbool af)
498 {
499 return c != '\0' && ((comp_ptr[SETBYTE(c)] & SETBIT(c))? af : !af);
500 }
501
502 private jbool
503 REmatch(char *linep, char *comp_ptr)
504 {
505 char *first_p;
506 int n;
507
508 for (;;) switch (*comp_ptr++) {
509 case NORMC:
510 n = *comp_ptr++;
511 while (--n >= 0)
512 if (*linep++ != *comp_ptr++)
513 return NO;
514 continue;
515
516 case CINDC: /* case independent comparison */
517 n = *comp_ptr++;
518 while (--n >= 0)
519 if (!cind_eq(*linep++, *comp_ptr++))
520 return NO;
521 continue;
522
523 case EOP:
524 loc2 = linep;
525 REeom = (loc2 - REbolp);
526 return YES; /* Success! */
527
528 case AT_BOL:
529 if (linep == REbolp && linep != locrater)
530 continue;
531
532 return NO;
533
534 case AT_EOL:
535 if (*linep == '\0')
536 continue;
537
538 return NO;
539
540 case ANYC:
541 if (*linep++ != '\0')
542 continue;
543
544 return NO;
545
546 case AT_BOW:
547 if (linep != locrater && jisident(*linep)
548 && (linep == REbolp || !jisident(linep[-1])))
549 continue;
550
551 return NO;
552
553 case AT_EOW:
554 if (linep != locrater && (*linep == '\0' || !jisident(*linep))
555 && (linep != REbolp && jisident(linep[-1])))
556 continue;
557
558 return NO;
559
560 case ONE_OF:
561 case NONE_OF:
562 if (member(comp_ptr, ZXC(*linep++), comp_ptr[-1] == ONE_OF)) {
563 comp_ptr += SETSIZE;
564 continue;
565 }
566 return NO;
567
568 case OPENP:
569 pstrtlst[(int) *comp_ptr++] = linep;
570 continue;
571
572 case CLOSEP:
573 pendlst[(int) *comp_ptr++] = linep;
574 continue;
575
576 case BACKREF:
577 if (pstrtlst[n = *comp_ptr++] == NULL) {
578 s_mess("\\%d was not specified.", n + 1);
579 } else if (backref(n, linep)) {
580 linep += pendlst[n] - pstrtlst[n];
581 continue;
582 }
583 return NO;
584
585 case CURLYB:
586 {
587 int altcnt = *comp_ptr++;
588 jbool any = NO;
589
590 while (--altcnt >= 0) {
591 if (!any)
592 any = REmatch(linep, comp_ptr + ALT_LEN_LEN);
593 comp_ptr += ALT_LEN(comp_ptr);
594 }
595 if (!any)
596 return NO;
597
598 linep = loc2;
599 continue;
600 }
601
602 case ANYC | STAR:
603 first_p = linep;
604 do {} while (*linep++ != '\0');
605 goto star;
606
607 case NORMC | STAR:
608 first_p = linep;
609 do {} while (*comp_ptr == *linep++);
610 comp_ptr += 1;
611 goto star;
612
613 case CINDC | STAR:
614 first_p = linep;
615 do {} while (cind_eq(*comp_ptr, *linep++));
616 comp_ptr += 1;
617 goto star;
618
619 case ONE_OF | STAR:
620 case NONE_OF | STAR:
621 first_p = linep;
622 do {} while (member(comp_ptr, ZXC(*linep++), comp_ptr[-1] == (ONE_OF | STAR)));
623 comp_ptr += SETSIZE;
624 /* fall through */
625 star:
626 /* linep points *after* first unmatched char.
627 * first_p points at where starred element started matching.
628 */
629 while (--linep > first_p) {
630 if ((*comp_ptr != NORMC || *linep == comp_ptr[2])
631 && REmatch(linep, comp_ptr))
632 return YES;
633 }
634 continue;
635
636 case BACKREF | STAR:
637 first_p = linep;
638 n = *comp_ptr++;
639 while (backref(n, linep))
640 linep += pendlst[n] - pstrtlst[n];
641 while (linep > first_p) {
642 if (REmatch(linep, comp_ptr))
643 return YES;
644
645 linep -= pendlst[n] - pstrtlst[n];
646 }
647 continue;
648
649 default:
650 complain("RE error match (%d).", comp_ptr[-1]);
651 /* NOTREACHED */
652 }
653 /* NOTREACHED */
654 }
655
656 private void
657 REreset(void)
658 {
659 int i;
660
661 for (i = 0; i < NPAR; i++)
662 pstrtlst[i] = pendlst[i] = NULL;
663 }
664
665 /* Index LINE at OFFSET. If lbuf_okay is YES it's okay to use linebuf
666 * if LINE is the current line. This should save lots of time in things
667 * like paren matching in LISP mode. Saves all that copying from linebuf
668 * to a local buffer. substitute() is the guy who calls re_lindex with
669 * lbuf_okay as NO, since the substitution gets placed in linebuf ...
670 * doesn't work too well when the source and destination strings are the
671 * same. I hate all these arguments!
672 *
673 * This code is cumbersome, repetetive for reasons of efficiency. Fast
674 * search is a must as far as I am concerned.
675 */
676
677 jbool
678 re_lindex(LinePtr line, int offset, int dir,
679 struct RE_block *re_blk, jbool lbuf_okay,
680 int crater) /* offset of previous substitute (or -1). */
681 {
682 char *p;
683 ZXchar firstc = re_blk->r_firstc;
684 jbool anchored = re_blk->r_anchored;
685 char **alts = re_blk->r_alternates;
686
687 REreset();
688 if (lbuf_okay) {
689 REbolp = lbptr(line);
690 if (offset == -1)
691 offset = strlen(REbolp); /* arg! */
692 } else {
693 REbolp = ltobuf(line, re_blk->r_lbuf);
694 if (offset == -1) { /* Reverse search, find end of line. */
695 offset = Jr_Len; /* Just Read Len. */
696 }
697 }
698
699 if (anchored) {
700 if (dir == FORWARD) {
701 if (offset != 0 || crater != -1)
702 return NO;
703 } else {
704 offset = 0;
705 }
706 }
707
708 p = REbolp + offset;
709 locrater = REbolp + crater;
710
711 if (firstc != EOF) {
712 char *first_alt = *alts;
713
714 if (dir == FORWARD) {
715 while (CharUpcase(*p) != firstc || !REmatch(p, first_alt))
716 if (*p++ == '\0')
717 return NO;
718 } else {
719 while (CharUpcase(*p) != firstc || !REmatch(p, first_alt))
720 if (--p < REbolp)
721 return NO;
722 }
723 } else {
724 for (;;) {
725 char **altp = alts;
726
727 while (*altp != NULL)
728 if (REmatch(p, *altp++))
729 goto success;
730 if (anchored
731 || (dir == FORWARD ? *p++ == '\0' : --p < REbolp))
732 return NO;
733 }
734 success:;
735 }
736 loc1 = p;
737 REbom = loc1 - REbolp;
738
739 return YES;
740 }
741
742 jbool okay_wrap = NO; /* Do a wrap search ... not when we're
743 parsing errors ... */
744
745 Bufpos *
746 dosearch(const char *pattern, int dir, jbool re)
747 {
748 Bufpos *pos;
749 struct RE_block re_blk; /* global re-compiled buffer */
750
751 if (bobp() && eobp()) /* Can't match! There's no buffer. */
752 return NULL;
753
754 REcompile(pattern, re, &re_blk);
755
756 pos = docompiled(dir, &re_blk);
757 return pos;
758 }
759
760 Bufpos *
761 docompiled(int dir, struct RE_block *re_blk)
762 {
763 static Bufpos
764 ret;
765 LinePtr lp;
766 int offset;
767 jbool we_wrapped = NO;
768
769 lsave();
770 /* Search now lsave()'s so it doesn't make any assumptions on
771 * whether the the contents of curline/curchar are in linebuf.
772 * Nowhere does search write all over linebuf. However, we have to
773 * be careful about what calls we make here, because many of them
774 * assume (and rightly so) that curline is in linebuf.
775 */
776 lp = curline;
777 offset = curchar;
778 if (dir == BACKWARD) {
779 if (bobp()) {
780 if (okay_wrap && WrapScan)
781 goto doit;
782
783 return NULL;
784 }
785 /* here we simulate BackChar() */
786 if (bolp()) {
787 lp = lp->l_prev;
788 offset = length(lp);
789 } else {
790 offset -= 1;
791 }
792 } else if (dir==FORWARD && lbptr(lp)[offset]=='\0' && !lastp(lp)) {
793 lp = lp->l_next;
794 offset = 0;
795 }
796
797 do {
798 if (re_lindex(lp, offset, dir, re_blk, YES, -1))
799 break;
800 doit:
801 lp = (dir == FORWARD) ? lp->l_next : lp->l_prev;
802 if (lp == NULL) {
803 if (okay_wrap && WrapScan) {
804 lp = (dir == FORWARD)
805 ? curbuf->b_first : curbuf->b_last;
806 we_wrapped = YES;
807 } else
808 break;
809 }
810 if (dir == FORWARD)
811 offset = 0;
812 else
813 offset = -1; /* signals re_lindex ... */
814 } while (lp != curline);
815
816 if (lp == curline && we_wrapped)
817 lp = NULL;
818 if (lp == NULL)
819 return NULL;
820
821 ret.p_line = lp;
822 ret.p_char = (dir == FORWARD) ? REeom : REbom;
823 return &ret;
824 }
825
826 private char *
827 insert(char *off, char *endp, int which)
828 {
829 char *pp = pstrtlst[which];
830 int n;
831
832 if (pp == NULL) {
833 complain("\\%d not defined", which);
834 /* NOTREACHED */
835 }
836 n = pendlst[which] - pp;
837
838 /* note: ensure space will be left for a NUL */
839 if (off + n >= endp) {
840 len_error(JMP_ERROR);
841 /* NOTREACHED */
842 }
843 while (--n >= 0)
844 *off++ = *pp++;
845 return off;
846 }
847
848 /* Perform the substitution. If DELP is YES the matched string is
849 * deleted, i.e., the substitution string is not inserted.
850 */
851 void
852 re_dosub(struct RE_block *re_blk, char *tobuf, jbool delp)
853 {
854 char *tp,
855 *rp,
856 *endp;
857
858 tp = tobuf;
859 endp = tp + LBSIZE;
860 rp = re_blk->r_lbuf;
861
862 while (rp < loc1)
863 *tp++ = *rp++;
864
865 if (!delp) {
866 char c;
867
868 rp = rep_str;
869 while ((c = *rp++) != '\0') {
870 if (c == '\\') {
871 c = *rp++;
872 if (c >= '0' && c < re_blk->r_nparens + '0') {
873 tp = insert(tp, endp, c - '0');
874 continue;
875 }
876 if (c == '\0') {
877 /* treat \ at the end as if it were \\ */
878 c = '\\';
879 rp--; /* be sure to hit again */
880 }
881 }
882 *tp++ = c;
883 if (tp >= endp) {
884 len_error(JMP_ERROR);
885 /* NOTREACHED */
886 }
887 }
888 }
889 rp = loc2;
890 REdelta = -REeom;
891 REeom = tp - tobuf;
892 REdelta += REeom;
893 if (loc1==rp && *rp!='\0') {
894 /* Skip an extra character if the matched text was a null
895 * string, but don't skip over the end of line. This is to
896 * prevent an infinite number of replacements in the same
897 * position, e.g., replace "^" with "".
898 */
899 REeom += 1;
900 }
901 loc2 = re_blk->r_lbuf + REeom;
902 while ((*tp++ = *rp++) != '\0') {
903 if (tp >= endp) {
904 len_error(JMP_ERROR);
905 /* NOTREACHED */
906 }
907 }
908 }
909
910 void
911 putmatch(int which, char *buf, size_t size)
912 {
913 *(insert(buf, buf + size, which)) = '\0';
914 }
915
916 void
917 RErecur(void)
918 {
919 char repbuf[sizeof rep_str];
920 Mark *m = MakeMark(curline, REbom);
921
922 message("Type ^X ^C to continue with query replace.");
923
924 byte_copy(rep_str, repbuf, sizeof rep_str);
925 Recur();
926 byte_copy(repbuf, rep_str, sizeof rep_str);
927 if (!is_an_arg())
928 ToMark(m);
929 DelMark(m);
930 }
931
932 /* Do we match PATTERN at OFFSET in BUF? */
933
934 jbool
935 LookingAt(const char *pattern, char *buf, int offset)
936 {
937 struct RE_block re_blk;
938 char **alt = re_blk.r_alternates;
939
940 REcompile(pattern, YES, &re_blk);
941 REreset();
942 locrater = NULL;
943 REbolp = buf;
944
945 while (*alt)
946 if (REmatch(buf + offset, *alt++))
947 return YES;
948 return NO;
949 }
950
951 jbool
952 look_at(char *expr)
953 {
954 struct RE_block re_blk;
955
956 REcompile(expr, NO, &re_blk);
957 REreset();
958 locrater = NULL;
959 REbolp = linebuf;
960 return REmatch(linebuf + curchar, re_blk.r_alternates[0]);
961 }