3
* regexp.c - regular expression matching
7
* Underneath the reformatting and comment blocks which were added to
8
* make it consistent with the rest of the code, you will find a
9
* modified version of Henry Specer's regular expression library.
10
* Henry's functions were modified to provide the minimal regular
11
* expression matching, as required by P1003. Henry's code was
12
* copyrighted, and copy of the copyright message and restrictions
13
* are provided, verbatim, below:
15
* Copyright (c) 1986 by University of Toronto.
16
* Written by Henry Spencer. Not derived from licensed software.
18
* Permission is granted to anyone to use this software for any
19
* purpose on any computer system, and to redistribute it freely,
20
* subject to the following restrictions:
22
* 1. The author is not responsible for the consequences of use of
23
* this software, no matter how awful, even if they arise
26
* 2. The origin of this software must not be misrepresented, either
27
* by explicit claim or by omission.
29
* 3. Altered versions must be plainly marked as such, and must not
30
* be misrepresented as being the original software.
33
* This version modified by Ian Phillipps to return pointer to terminating
34
* NUL on substitution string. [ Temp mail address ex-igp@camcon.co.uk ]
36
* Altered by amylaar to support excompatible option and the
37
* operators \< and >\ . ( 7.Sep. 1991 )
39
* regsub altered by amylaar to take an additional parameter specifying
40
* maximum number of bytes that can be written to the memory region
43
* Also altered by Fredrik Hubinette to handle the + operator and
44
* eight-bit chars. Mars 22 1996
47
* Beware that some of this code is subtly aware of the way operator
48
* precedence is structured in regular expressions. Serious changes in
49
* regular-expression syntax might require a total rethink.
53
* Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
54
* Henry Spencer, University of Torronto (henry@utzoo.edu)
56
* Sponsored by The USENIX Association for public distribution.
61
#include "my_global.h"
72
* The "internal use only" fields in regexp.h are present to pass info from
73
* compile to execute that permits the execute phase to run lots faster on
74
* simple cases. They are:
76
* regstart char that must begin a match; '\0' if none obvious
77
* reganch is the match anchored (at beginning-of-line only)?
78
* regmust string (pointer into program) that match must include, or NULL
79
* regmlen length of regmust string
81
* Regstart and reganch permit very fast decisions on suitable starting points
82
* for a match, cutting down the work a lot. Regmust permits fast rejection
83
* of lines that cannot possibly match. The regmust tests are costly enough
84
* that regcomp() supplies a regmust only if the r.e. contains something
85
* potentially expensive (at present, the only such thing detected is * or +
86
* at the start of the r.e., which can involve a lot of backup). Regmlen is
87
* supplied because the test in regexec() needs it and regcomp() is computing
92
* Structure for regexp "program". This is essentially a linear encoding
93
* of a nondeterministic finite-state machine (aka syntax charts or
94
* "railroad normal form" in parsing technology). Each node is an opcode
95
* plus a "nxt" pointer, possibly plus an operand. "Nxt" pointers of
96
* all nodes except BRANCH implement concatenation; a "nxt" pointer with
97
* a BRANCH on both ends of it is connecting two alternatives. (Here we
98
* have one of the subtle syntax dependencies: an individual BRANCH (as
99
* opposed to a collection of them) is never concatenated with anything
100
* because of operator precedence.) The operand of some types of node is
101
* a literal string; for others, it is a node leading into a sub-FSM. In
102
* particular, the operand of a BRANCH node is the first node of the branch.
103
* (NB this is *not* a tree structure: the tail of the branch connects
104
* to the thing following the set of BRANCHes.) The opcodes are:
107
/* definition number opnd? meaning */
108
#define END 0 /* no End of program. */
109
#define BOL 1 /* no Match "" at beginning of line. */
110
#define EOL 2 /* no Match "" at end of line. */
111
#define ANY 3 /* no Match any one character. */
112
#define ANYOF 4 /* str Match any character in this string. */
113
#define ANYBUT 5 /* str Match any character not in this
115
#define BRANCH 6 /* node Match this alternative, or the
117
#define BACK 7 /* no Match "", "nxt" ptr points backward. */
118
#define EXACTLY 8 /* str Match this string. */
119
#define NOTHING 9 /* no Match empty string. */
120
#define STAR 10 /* node Match this (simple) thing 0 or more
122
#define WORDSTART 11 /* node matching a start of a word */
123
#define WORDEND 12 /* node matching an end of a word */
124
#define OPEN 20 /* no Mark this point in input as start of
126
/* OPEN+1 is number 1, etc. */
127
#define CLOSE 30 /* no Analogous to OPEN. */
132
* BRANCH The set of branches constituting a single choice are hooked
133
* together with their "nxt" pointers, since precedence prevents
134
* anything being concatenated to any individual branch. The
135
* "nxt" pointer of the last BRANCH in a choice points to the
136
* thing following the whole choice. This is also where the
137
* final "nxt" pointer of each individual branch points; each
138
* branch starts with the operand node of a BRANCH node.
140
* BACK Normal "nxt" pointers all implicitly point forward; BACK
141
* exists to make loop structures possible.
143
* STAR complex '*', are implemented as circular BRANCH structures
144
* using BACK. Simple cases (one character per match) are
145
* implemented with STAR for speed and to minimize recursive
148
* OPEN,CLOSE ...are numbered at compile time.
152
* A node is one char of opcode followed by two chars of "nxt" pointer.
153
* "Nxt" pointers are stored as two 8-bit pieces, high order first. The
154
* value is a positive offset from the opcode of the node containing it.
155
* An operand, if any, simply follows the node. (Note that much of the
156
* code generation knows about this implicit relationship.)
158
* Using two bytes for the "nxt" pointer is vast overkill for most things,
159
* but allows patterns to get big without disasters.
162
#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
163
#define OPERAND(p) ((p) + 3)
166
* The first byte of the regexp internal "program" is actually this magic
167
* number; the start node begins in the second byte.
172
* Utility definitions.
176
#define error(X,Y) fprintf(stderr, X, Y)
178
#define regerror(X) error("Regexp: %s\n",X);
179
#define SPECIAL 0x100
180
#define LBRAC ('('|SPECIAL)
181
#define RBRAC (')'|SPECIAL)
182
#define ASTERIX ('*'|SPECIAL)
183
#define PLUS ('+'|SPECIAL)
184
#define OR_OP ('|'|SPECIAL)
185
#define DOLLAR ('$'|SPECIAL)
186
#define DOT ('.'|SPECIAL)
187
#define CARET ('^'|SPECIAL)
188
#define LSQBRAC ('['|SPECIAL)
189
#define RSQBRAC (']'|SPECIAL)
190
#define LSHBRAC ('<'|SPECIAL)
191
#define RSHBRAC ('>'|SPECIAL)
192
#define FAIL(m) { regerror(m); return(NULL); }
193
#define ISMULT(c) ((c) == ASTERIX || (c)==PLUS)
194
#define META "^$.[()|*+\\"
196
#define CHARBITS 0xff
197
#define UCHARAT(p) ((int)*(unsigned char *)(p))
199
#define UCHARAT(p) ((int)*(p)&CHARBITS)
201
#define ISWORDPART(c) ( isalnum(c) || (c) == '_' )
204
* Flags to be passed up and down.
206
#define HASWIDTH 01 /* Known never to match null string. */
207
#define SIMPLE 02 /* Simple enough to be STAR operand. */
208
#define SPSTART 04 /* Starts with * */
209
#define WORST 0 /* Worst case. */
211
#define STRCHR(A,B) strchr(A,B)
215
* Global work variables for regcomp().
217
static short *regparse; /* Input-scan pointer. */
218
static int regnpar; /* () count. */
219
static char regdummy;
220
static char *regcode; /* Code-emit pointer; ®dummy = don't. */
221
static long regsize; /* Code size. */
224
* Forward declarations for regcomp()'s friends.
227
#define STATIC static
230
STATIC char *regbranch();
231
STATIC char *regpiece();
232
STATIC char *regatom();
233
STATIC char *regnode();
234
STATIC char *regnext();
236
STATIC void reginsert();
237
STATIC void regtail();
238
STATIC void regoptail();
241
- regcomp - compile a regular expression into internal code
243
* We can't allocate space until we know how big the compiled form will be,
244
* but we can't compile it (and thus know how big it is) until we've got a
245
* place to put the code. So we cheat: we compile it twice, once with code
246
* generation turned off and size counting turned on, and once "for real".
247
* This also means that we don't allocate space until we are sure that the
248
* thing really will compile successfully, and we never have to move the
249
* code and thus invalidate pointers into it. (Note that it has to be in
250
* one piece because free() must be able to free it all.)
252
* Beware that the optimization-preparation code in here knows about some
253
* of the structure of the compiled regexp.
255
regexp *regcomp(exp,excompat)
257
int excompat; /* \( \) operators like in unix ex */
261
register char *longest;
266
if (exp == (char *)NULL)
267
FAIL("NULL argument");
269
exp2=(short*)malloc( (strlen(exp)+1) * (sizeof(short[8])/sizeof(char[8])) );
270
for ( scan=exp,dest=exp2;( c= UCHARAT(scan++)); ) {
274
*dest++ = excompat ? c : c | SPECIAL;
284
*dest++ = c | SPECIAL;
287
switch ( c = *scan++ ) {
290
*dest++ = excompat ? c | SPECIAL : c;
294
*dest++ = c | SPECIAL;
298
FAIL("sorry, unimplemented operator");
299
case 'b': *dest++ = '\b'; break;
300
case 't': *dest++ = '\t'; break;
301
case 'r': *dest++ = '\r'; break;
311
/* First pass: determine size, legality. */
317
if (reg(0, &flags) == (char *)NULL)
318
return ((regexp *)NULL);
320
/* Small enough for pointer-storage convention? */
321
if (regsize >= 32767L) /* Probably could be 65535L. */
322
FAIL("regexp too big");
324
/* Allocate space. */
325
r = (regexp *) malloc(sizeof(regexp) + (unsigned) regsize);
326
if (r == (regexp *) NULL)
327
FAIL("out of space");
328
(void) bzero(r, sizeof(regexp) + (unsigned)regsize);
330
/* Second pass: emit code. */
333
regcode = r->program;
335
if (reg(0, &flags) == NULL)
336
return ((regexp *) NULL);
338
/* Dig out information for optimizations. */
339
r->regstart = '\0'; /* Worst-case defaults. */
343
scan = r->program + 1; /* First BRANCH. */
344
if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
345
scan = OPERAND(scan);
347
/* Starting-point info. */
348
if (OP(scan) == EXACTLY)
349
r->regstart = *OPERAND(scan);
350
else if (OP(scan) == BOL)
354
* If there's something expensive in the r.e., find the longest
355
* literal string that must appear and make it the regmust. Resolve
356
* ties in favor of later strings, since the regstart check works
357
* with the beginning of the r.e. and avoiding duplication
358
* strengthens checking. Not a strong reason, but sufficient in the
361
if (flags & SPSTART) {
364
for (; scan != NULL; scan = regnext(scan))
365
if (OP(scan) == EXACTLY &&
366
(int)strlen(OPERAND(scan)) >= len) {
367
longest = OPERAND(scan);
368
len = strlen(OPERAND(scan));
370
r->regmust = longest;
379
- reg - regular expression, i.e. main body or parenthesized thing
381
* Caller must absorb opening parenthesis.
383
* Combining parenthesis handling with the base level of regular expression
384
* is a trifle forced, but the need to tie the tails of the branches to what
385
* follows makes it hard to avoid.
387
static char *reg(paren, flagp)
388
int paren; /* Parenthesized? */
393
register char *ender;
394
register int parno=0; /* make gcc happy */
397
*flagp = HASWIDTH; /* Tentatively. */
399
/* Make an OPEN node, if parenthesized. */
401
if (regnpar >= NSUBEXP)
405
ret = regnode(OPEN + parno);
409
/* Pick up the branches, linking them together. */
410
br = regbranch(&flags);
411
if (br == (char *)NULL)
412
return ((char *)NULL);
413
if (ret != (char *)NULL)
414
regtail(ret, br); /* OPEN -> first. */
417
if (!(flags & HASWIDTH))
419
*flagp |= flags & SPSTART;
420
while (*regparse == OR_OP) {
422
br = regbranch(&flags);
423
if (br == (char *)NULL)
424
return ((char *)NULL);
425
regtail(ret, br); /* BRANCH -> BRANCH. */
426
if (!(flags & HASWIDTH))
428
*flagp |= flags & SPSTART;
431
/* Make a closing node, and hook it on the end. */
432
ender = regnode((paren) ? CLOSE + parno : END);
435
/* Hook the tails of the branches to the closing node. */
436
for (br = ret; br != (char *)NULL; br = regnext(br))
437
regoptail(br, ender);
439
/* Check for proper termination. */
440
if (paren && *regparse++ != RBRAC) {
441
FAIL("unmatched ()");
442
} else if (!paren && *regparse != '\0') {
443
if (*regparse == RBRAC) {
444
FAIL("unmatched ()");
446
FAIL("junk on end");/* "Can't happen". */
453
- regbranch - one alternative of an | operator
455
* Implements the concatenation operator.
457
static char *regbranch(flagp)
461
register char *chain;
462
register char *latest;
465
*flagp = WORST; /* Tentatively. */
467
ret = regnode(BRANCH);
468
chain = (char *)NULL;
469
while (*regparse != '\0' && *regparse != OR_OP && *regparse != RBRAC) {
470
latest = regpiece(&flags);
471
if (latest == (char *)NULL)
472
return ((char *)NULL);
473
*flagp |= flags & HASWIDTH;
474
if (chain == (char *)NULL) /* First piece. */
475
*flagp |= flags & SPSTART;
477
regtail(chain, latest);
480
if (chain == (char *)NULL) /* Loop ran zero times. */
487
- regpiece - something followed by possible [*]
489
* Note that the branching code sequence used for * is somewhat optimized:
490
* they use the same NOTHING node as both the endmarker for their branch
491
* list and the body of the last branch. It might seem that this node could
492
* be dispensed with entirely, but the endmarker role is not redundant.
494
static char *regpiece(flagp)
499
/* register char *nxt; */
502
ret = regatom(&flags);
503
if (ret == (char *)NULL)
504
return ((char *)NULL);
511
if (!(flags & HASWIDTH))
512
FAIL("* or + operand could be empty");
513
*flagp = (WORST | SPSTART);
519
reginsert(STAR, ret);
523
/* Emit x* as (x&|), where & means "self". */
524
reginsert(BRANCH, ret); /* Either x */
525
regoptail(ret, regnode(BACK)); /* and loop */
526
regoptail(ret, ret); /* back */
527
regtail(ret, regnode(BRANCH)); /* or */
528
regtail(ret, regnode(NOTHING)); /* null. */
533
/* Emit a+ as (a&) where & means "self" /Fredrik Hubinette */
536
reginsert(BRANCH, tmp);
539
regtail(ret, regnode(BRANCH));
540
regtail(ret, regnode(NOTHING));
544
if (ISMULT(*regparse))
545
FAIL("nested * or +");
552
- regatom - the lowest level
554
* Optimization: gobbles an entire sequence of ordinary characters so that
555
* it can turn them into a single node, which is smaller to store and
558
static char *regatom(flagp)
564
*flagp = WORST; /* Tentatively. */
566
switch (*regparse++) {
575
*flagp |= HASWIDTH | SIMPLE;
578
ret = regnode(WORDSTART);
581
ret = regnode(WORDEND);
585
register int classend;
587
if (*regparse == CARET) { /* Complement of range. */
588
ret = regnode(ANYBUT);
591
ret = regnode(ANYOF);
592
if (*regparse == RSQBRAC || *regparse == '-')
594
while (*regparse != '\0' && *regparse != RSQBRAC) {
595
if (*regparse == '-') {
597
if (*regparse == RSQBRAC || *regparse == '\0')
600
class = (CHARBITS & *(regparse - 2)) + 1;
601
classend = (CHARBITS & *(regparse));
602
if (class > classend + 1)
603
FAIL("invalid [] range");
604
for (; class <= classend; class++)
612
if (*regparse != RSQBRAC)
613
FAIL("unmatched []");
615
*flagp |= HASWIDTH | SIMPLE;
619
ret = reg(1, &flags);
620
if (ret == (char *)NULL)
621
return ((char *)NULL);
622
*flagp |= flags & (HASWIDTH | SPSTART);
627
FAIL("internal urp"); /* Supposed to be caught earlier. */
630
FAIL("* follows nothing\n");
634
register short ender;
637
for (len=0; regparse[len] &&
638
!(regparse[len]&SPECIAL) && regparse[len] != RSQBRAC; len++) ;
641
FAIL("internal disaster");
643
ender = *(regparse + len);
644
if (len > 1 && ISMULT(ender))
645
len--; /* Back off clear of * operand. */
649
ret = regnode(EXACTLY);
663
- regnode - emit a node
665
static char *regnode(op)
672
if (ret == ®dummy) {
678
*ptr++ = '\0'; /* Null "nxt" pointer. */
686
- regc - emit (if appropriate) a byte of code
691
if (regcode != ®dummy)
698
- reginsert - insert an operator in front of already-emitted operand
700
* Means relocating the operand.
702
static void reginsert(op, opnd)
708
register char *place;
710
if (regcode == ®dummy) {
720
place = opnd; /* Op node, where operand used to be. */
727
- regtail - set the next-pointer at the end of a node chain
729
static void regtail(p, val)
740
/* Find last node. */
743
temp = regnext(scan);
744
if (temp == (char *)NULL)
749
if (OP(scan) == BACK)
753
*(scan + 1) = (offset >> 8) & 0377;
754
*(scan + 2) = offset & 0377;
758
- regoptail - regtail on operand of first argument; nop if operandless
760
static void regoptail(p, val)
764
/* "Operandless" and "op != BRANCH" are synonymous in practice. */
765
if (p == (char *)NULL || p == ®dummy || OP(p) != BRANCH)
767
regtail(OPERAND(p), val);
771
* regexec and friends
775
* Global work variables for regexec().
777
static char *reginput; /* String-input pointer. */
778
static char *regbol; /* Beginning of input, for ^ check. */
779
static char **regstartp; /* Pointer to startp array. */
780
static char **regendp; /* Ditto for endp. */
786
STATIC int regmatch();
787
STATIC int regrepeat();
792
STATIC char *regprop();
796
- regexec - match a regexp against a string
798
int regexec(prog, string)
799
register regexp *prog;
800
register char *string;
805
if (prog == (regexp *)NULL || string == (char *)NULL) {
806
regerror("NULL parameter");
809
/* Check validity of program. */
810
if (UCHARAT(prog->program) != MAGIC) {
811
regerror("corrupted program");
814
/* If there is a "must appear" string, look for it. */
815
if (prog->regmust != (char *)NULL) {
817
while ((s = STRCHR(s, prog->regmust[0])) != (char *)NULL) {
818
if (strncmp(s, prog->regmust, prog->regmlen) == 0)
819
break; /* Found it. */
822
if (s == (char *)NULL) /* Not present. */
825
/* Mark beginning of line for ^ . */
828
/* Simplest case: anchored match need be tried only once. */
830
return (regtry(prog, string));
832
/* Messy cases: unanchored match. */
834
if (prog->regstart != '\0')
835
/* We know what char it must start with. */
836
while ((s = STRCHR(s, prog->regstart)) != (char *)NULL) {
842
/* We don't -- general case. */
846
} while (*s++ != '\0');
853
- regtry - try match at specific point
856
static int regtry(regexp *prog, char *string)
863
regstartp = prog->startp;
864
regendp = prog->endp;
868
for (i = NSUBEXP; i > 0; i--) {
869
*sp++ = (char *)NULL;
870
*ep++ = (char *)NULL;
872
if (regmatch(prog->program + 1)) {
873
prog->startp[0] = string;
874
prog->endp[0] = reginput;
881
- regmatch - main matching routine
883
* Conceptually the strategy is simple: check to see whether the current
884
* node matches, call self recursively to see whether the rest matches,
885
* and then act accordingly. In practice we make some effort to avoid
886
* recursion, in particular by going through "ordinary" nodes (that don't
887
* need to know whether the rest of the match failed) by a loop instead of
891
static int regmatch(char *prog)
893
register char *scan; /* Current node. */
894
char *nxt; /* nxt node. */
898
if (scan != (char *)NULL && regnarrate)
899
fprintf(stderr, "%s(\n", regprop(scan));
901
while (scan != (char *)NULL) {
904
fprintf(stderr, "%s...\n", regprop(scan));
910
if (reginput != regbol)
914
if (*reginput != '\0')
918
if (*reginput == '\0')
923
if (reginput == regbol)
925
if (*reginput == '\0' ||
926
ISWORDPART( *(reginput-1) ) || !ISWORDPART( *reginput ) )
930
if (*reginput == '\0')
932
if ( reginput == regbol ||
933
!ISWORDPART( *(reginput-1) ) || ISWORDPART( *reginput ) )
940
opnd = OPERAND(scan);
941
/* Inline the first character, for speed. */
942
if (*opnd != *reginput)
945
if (len > 1 && strncmp(opnd, reginput, len) != 0)
951
if (*reginput == '\0' ||
952
STRCHR(OPERAND(scan), *reginput) == (char *)NULL)
957
if (*reginput == '\0' ||
958
STRCHR(OPERAND(scan), *reginput) != (char *)NULL)
978
no = OP(scan) - OPEN;
983
* Don't set startp if some later invocation of the same
984
* parentheses already has.
986
if (regstartp[no] == (char *)NULL)
987
regstartp[no] = save;
1003
register char *save;
1005
no = OP(scan) - CLOSE;
1008
if (regmatch(nxt)) {
1010
* Don't set endp if some later invocation of the same
1011
* parentheses already has.
1013
if (regendp[no] == (char *)NULL)
1021
register char *save;
1023
if (OP(nxt) != BRANCH) /* No choice. */
1024
nxt = OPERAND(scan); /* Avoid recursion. */
1028
if (regmatch(OPERAND(scan)))
1031
scan = regnext(scan);
1032
} while (scan != (char *)NULL && OP(scan) == BRANCH);
1039
register char nextch;
1041
register char *save;
1042
register int minimum;
1045
* Lookahead to avoid useless match attempts when we know
1046
* what character comes next.
1049
if (OP(nxt) == EXACTLY)
1050
nextch = *OPERAND(nxt);
1051
minimum = (OP(scan) == STAR) ? 0 : 1;
1053
no = regrepeat(OPERAND(scan));
1054
while (no >= minimum) {
1055
/* If it could work, try it. */
1056
if (nextch == '\0' || *reginput == nextch)
1059
/* Couldn't or didn't -- back up. */
1061
reginput = save + no;
1067
return (1); /* Success! */
1070
regerror("memory corruption");
1079
* We get here only if there's trouble -- normally "case END" is the
1080
* terminating point.
1082
regerror("corrupted pointers");
1087
- regrepeat - repeatedly match something simple, report how many
1090
static int regrepeat(char *p)
1092
register int count = 0;
1093
register char *scan;
1094
register char *opnd;
1100
count = strlen(scan);
1104
while (*opnd == *scan) {
1110
while (*scan != '\0' && STRCHR(opnd, *scan) != (char *)NULL) {
1116
while (*scan != '\0' && STRCHR(opnd, *scan) == (char *)NULL) {
1121
default: /* Oh dear. Called inappropriately. */
1122
regerror("internal foulup");
1123
count = 0; /* Best compromise. */
1133
- regnext - dig the "nxt" pointer out of a node
1136
static char *regnext(register char *p)
1138
register int offset;
1141
return ((char *)NULL);
1145
return ((char *)NULL);
1148
return (p - offset);
1150
return (p + offset);
1155
STATIC char *regprop();
1158
- regdump - dump a regexp onto stdout in vaguely comprehensible form
1160
void regdump(regexp *r)
1163
register char op = EXACTLY; /* Arbitrary non-END op. */
1167
while (op != END) { /* While that wasn't END last time... */
1169
printf("%2ld%s", (long)(s - r->program), regprop(s)); /* Where, what. */
1171
if (nxt == (char *)NULL) /* nxt ptr. */
1174
printf("(%ld)", (long)( (s - r->program) + (nxt - s)));
1176
if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1177
/* Literal string, where present. */
1178
while (*s != '\0') {
1187
/* Header fields of interest. */
1188
if (r->regstart != '\0')
1189
printf("start `%c' ", r->regstart);
1191
printf("anchored ");
1192
if (r->regmust != (char *)NULL)
1193
printf("must have \"%s\"", r->regmust);
1198
- regprop - printable representation of opcode
1201
static char *regprop(char *op)
1204
static char buf[50];
1248
sprintf(buf + strlen(buf), "OPEN%d", OP(op) - OPEN);
1260
sprintf(buf + strlen(buf), "CLOSE%d", OP(op) - CLOSE);
1267
regerror("corrupted opcode");
1271
if (p != (char *)NULL)
1278
- regsub - perform substitutions after a regexp match
1281
char *regsub(regexp *prog, char *source, char *dest, int n)
1288
extern char *strncpy();
1290
if (prog == (regexp *)NULL ||
1291
source == (char *)NULL || dest == (char *)NULL) {
1292
regerror("NULL parm to regsub");
1295
if (UCHARAT(prog->program) != MAGIC) {
1296
regerror("damaged regexp fed to regsub");
1301
while ((c = *src++) != '\0') {
1304
else if (c == '\\' && '0' <= *src && *src <= '9')
1309
if (no < 0) { /* Ordinary character. */
1310
if (c == '\\' && (*src == '\\' || *src == '&'))
1312
if (--n < 0) { /* amylaar */
1313
regerror("line too long");
1317
} else if (prog->startp[no] != (char *)NULL &&
1318
prog->endp[no] != (char *)NULL) {
1319
len = prog->endp[no] - prog->startp[no];
1320
if ( (n-=len) < 0 ) { /* amylaar */
1321
regerror("line too long");
1324
strncpy(dst, prog->startp[no], len);
1326
if (len != 0 && *(dst - 1) == '\0') { /* strncpy hit NUL. */
1327
regerror("damaged match string");
1332
if (--n < 0) { /* amylaar */
1333
regerror("line too long");
1341
#if 0 /* Use the local regerror() in ed.c */
1343
void regerror(char *s)
1345
fprintf(stderr, "regexp(3): %s", s);