xref: /minix3/external/bsd/less/dist/regexp.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: regexp.c,v 1.3 2013/09/04 19:44:21 tron Exp $	*/
2f7cf2976SLionel Sambuc 
3f7cf2976SLionel Sambuc /*
4f7cf2976SLionel Sambuc  * regcomp and regexec -- regsub and regerror are elsewhere
5f7cf2976SLionel Sambuc  *
6f7cf2976SLionel Sambuc  *	Copyright (c) 1986 by University of Toronto.
7f7cf2976SLionel Sambuc  *	Written by Henry Spencer.  Not derived from licensed software.
8f7cf2976SLionel Sambuc  *
9f7cf2976SLionel Sambuc  *	Permission is granted to anyone to use this software for any
10f7cf2976SLionel Sambuc  *	purpose on any computer system, and to redistribute it freely,
11f7cf2976SLionel Sambuc  *	subject to the following restrictions:
12f7cf2976SLionel Sambuc  *
13f7cf2976SLionel Sambuc  *	1. The author is not responsible for the consequences of use of
14f7cf2976SLionel Sambuc  *		this software, no matter how awful, even if they arise
15f7cf2976SLionel Sambuc  *		from defects in it.
16f7cf2976SLionel Sambuc  *
17f7cf2976SLionel Sambuc  *	2. The origin of this software must not be misrepresented, either
18f7cf2976SLionel Sambuc  *		by explicit claim or by omission.
19f7cf2976SLionel Sambuc  *
20f7cf2976SLionel Sambuc  *	3. Altered versions must be plainly marked as such, and must not
21f7cf2976SLionel Sambuc  *		be misrepresented as being the original software.
22f7cf2976SLionel Sambuc  *
23f7cf2976SLionel Sambuc  * Beware that some of this code is subtly aware of the way operator
24f7cf2976SLionel Sambuc  * precedence is structured in regular expressions.  Serious changes in
25f7cf2976SLionel Sambuc  * regular-expression syntax might require a total rethink.
26f7cf2976SLionel Sambuc  *
27f7cf2976SLionel Sambuc  * *** NOTE: this code has been altered slightly for use in Tcl. ***
28f7cf2976SLionel Sambuc  * Slightly modified by David MacKenzie to undo most of the changes for TCL.
29f7cf2976SLionel Sambuc  * Added regexec2 with notbol parameter. -- 4/19/99 Mark Nudelman
30f7cf2976SLionel Sambuc  */
31f7cf2976SLionel Sambuc 
32f7cf2976SLionel Sambuc #include "less.h"
33f7cf2976SLionel Sambuc #if HAVE_STDIO_H
34f7cf2976SLionel Sambuc #include <stdio.h>
35f7cf2976SLionel Sambuc #endif
36f7cf2976SLionel Sambuc #if HAVE_STDLIB_H
37f7cf2976SLionel Sambuc #include <stdlib.h>
38f7cf2976SLionel Sambuc #endif
39f7cf2976SLionel Sambuc #if HAVE_STRING_H
40f7cf2976SLionel Sambuc #include <string.h>
41f7cf2976SLionel Sambuc #endif
42f7cf2976SLionel Sambuc #include "regexp.h"
43f7cf2976SLionel Sambuc 
44f7cf2976SLionel Sambuc /*
45f7cf2976SLionel Sambuc  * The "internal use only" fields in regexp.h are present to pass info from
46f7cf2976SLionel Sambuc  * compile to execute that permits the execute phase to run lots faster on
47f7cf2976SLionel Sambuc  * simple cases.  They are:
48f7cf2976SLionel Sambuc  *
49f7cf2976SLionel Sambuc  * regstart	char that must begin a match; '\0' if none obvious
50f7cf2976SLionel Sambuc  * reganch	is the match anchored (at beginning-of-line only)?
51f7cf2976SLionel Sambuc  * regmust	string (pointer into program) that match must include, or NULL
52f7cf2976SLionel Sambuc  * regmlen	length of regmust string
53f7cf2976SLionel Sambuc  *
54f7cf2976SLionel Sambuc  * Regstart and reganch permit very fast decisions on suitable starting points
55f7cf2976SLionel Sambuc  * for a match, cutting down the work a lot.  Regmust permits fast rejection
56f7cf2976SLionel Sambuc  * of lines that cannot possibly match.  The regmust tests are costly enough
57f7cf2976SLionel Sambuc  * that regcomp() supplies a regmust only if the r.e. contains something
58f7cf2976SLionel Sambuc  * potentially expensive (at present, the only such thing detected is * or +
59f7cf2976SLionel Sambuc  * at the start of the r.e., which can involve a lot of backup).  Regmlen is
60f7cf2976SLionel Sambuc  * supplied because the test in regexec() needs it and regcomp() is
61f7cf2976SLionel Sambuc  * computing it anyway.
62f7cf2976SLionel Sambuc  */
63f7cf2976SLionel Sambuc 
64f7cf2976SLionel Sambuc /*
65f7cf2976SLionel Sambuc  * Structure for regexp "program".  This is essentially a linear encoding
66f7cf2976SLionel Sambuc  * of a nondeterministic finite-state machine (aka syntax charts or
67f7cf2976SLionel Sambuc  * "railroad normal form" in parsing technology).  Each node is an opcode
68f7cf2976SLionel Sambuc  * plus a "next" pointer, possibly plus an operand.  "Next" pointers of
69f7cf2976SLionel Sambuc  * all nodes except BRANCH implement concatenation; a "next" pointer with
70f7cf2976SLionel Sambuc  * a BRANCH on both ends of it is connecting two alternatives.  (Here we
71f7cf2976SLionel Sambuc  * have one of the subtle syntax dependencies:  an individual BRANCH (as
72f7cf2976SLionel Sambuc  * opposed to a collection of them) is never concatenated with anything
73f7cf2976SLionel Sambuc  * because of operator precedence.)  The operand of some types of node is
74f7cf2976SLionel Sambuc  * a literal string; for others, it is a node leading into a sub-FSM.  In
75f7cf2976SLionel Sambuc  * particular, the operand of a BRANCH node is the first node of the branch.
76f7cf2976SLionel Sambuc  * (NB this is *not* a tree structure:  the tail of the branch connects
77f7cf2976SLionel Sambuc  * to the thing following the set of BRANCHes.)  The opcodes are:
78f7cf2976SLionel Sambuc  */
79f7cf2976SLionel Sambuc 
80f7cf2976SLionel Sambuc /* definition	number	opnd?	meaning */
81f7cf2976SLionel Sambuc #undef EOL
82f7cf2976SLionel Sambuc #define	END	0	/* no	End of program. */
83f7cf2976SLionel Sambuc #define	BOL	1	/* no	Match "" at beginning of line. */
84f7cf2976SLionel Sambuc #define	EOL	2	/* no	Match "" at end of line. */
85f7cf2976SLionel Sambuc #define	ANY	3	/* no	Match any one character. */
86f7cf2976SLionel Sambuc #define	ANYOF	4	/* str	Match any character in this string. */
87f7cf2976SLionel Sambuc #define	ANYBUT	5	/* str	Match any character not in this string. */
88f7cf2976SLionel Sambuc #define	BRANCH	6	/* node	Match this alternative, or the next... */
89f7cf2976SLionel Sambuc #define	BACK	7	/* no	Match "", "next" ptr points backward. */
90f7cf2976SLionel Sambuc #define	EXACTLY	8	/* str	Match this string. */
91f7cf2976SLionel Sambuc #define	NOTHING	9	/* no	Match empty string. */
92f7cf2976SLionel Sambuc #define	STAR	10	/* node	Match this (simple) thing 0 or more times. */
93f7cf2976SLionel Sambuc #define	PLUS	11	/* node	Match this (simple) thing 1 or more times. */
94f7cf2976SLionel Sambuc #define	OPEN	20	/* no	Mark this point in input as start of #n. */
95f7cf2976SLionel Sambuc 			/*	OPEN+1 is number 1, etc. */
96f7cf2976SLionel Sambuc #define	CLOSE	30	/* no	Analogous to OPEN. */
97f7cf2976SLionel Sambuc 
98f7cf2976SLionel Sambuc /*
99f7cf2976SLionel Sambuc  * Opcode notes:
100f7cf2976SLionel Sambuc  *
101f7cf2976SLionel Sambuc  * BRANCH	The set of branches constituting a single choice are hooked
102f7cf2976SLionel Sambuc  *		together with their "next" pointers, since precedence prevents
103f7cf2976SLionel Sambuc  *		anything being concatenated to any individual branch.  The
104f7cf2976SLionel Sambuc  *		"next" pointer of the last BRANCH in a choice points to the
105f7cf2976SLionel Sambuc  *		thing following the whole choice.  This is also where the
106f7cf2976SLionel Sambuc  *		final "next" pointer of each individual branch points; each
107f7cf2976SLionel Sambuc  *		branch starts with the operand node of a BRANCH node.
108f7cf2976SLionel Sambuc  *
109f7cf2976SLionel Sambuc  * BACK		Normal "next" pointers all implicitly point forward; BACK
110f7cf2976SLionel Sambuc  *		exists to make loop structures possible.
111f7cf2976SLionel Sambuc  *
112f7cf2976SLionel Sambuc  * STAR,PLUS	'?', and complex '*' and '+', are implemented as circular
113f7cf2976SLionel Sambuc  *		BRANCH structures using BACK.  Simple cases (one character
114f7cf2976SLionel Sambuc  *		per match) are implemented with STAR and PLUS for speed
115f7cf2976SLionel Sambuc  *		and to minimize recursive plunges.
116f7cf2976SLionel Sambuc  *
117f7cf2976SLionel Sambuc  * OPEN,CLOSE	...are numbered at compile time.
118f7cf2976SLionel Sambuc  */
119f7cf2976SLionel Sambuc 
120f7cf2976SLionel Sambuc /*
121f7cf2976SLionel Sambuc  * A node is one char of opcode followed by two chars of "next" pointer.
122f7cf2976SLionel Sambuc  * "Next" pointers are stored as two 8-bit pieces, high order first.  The
123f7cf2976SLionel Sambuc  * value is a positive offset from the opcode of the node containing it.
124f7cf2976SLionel Sambuc  * An operand, if any, simply follows the node.  (Note that much of the
125f7cf2976SLionel Sambuc  * code generation knows about this implicit relationship.)
126f7cf2976SLionel Sambuc  *
127f7cf2976SLionel Sambuc  * Using two bytes for the "next" pointer is vast overkill for most things,
128f7cf2976SLionel Sambuc  * but allows patterns to get big without disasters.
129f7cf2976SLionel Sambuc  */
130f7cf2976SLionel Sambuc #define	OP(p)	(*(p))
131f7cf2976SLionel Sambuc #define	NEXT(p)	(((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
132f7cf2976SLionel Sambuc #define	OPERAND(p)	((p) + 3)
133f7cf2976SLionel Sambuc 
134f7cf2976SLionel Sambuc /*
135f7cf2976SLionel Sambuc  * See regmagic.h for one further detail of program structure.
136f7cf2976SLionel Sambuc  */
137f7cf2976SLionel Sambuc 
138f7cf2976SLionel Sambuc 
139f7cf2976SLionel Sambuc /*
140f7cf2976SLionel Sambuc  * Utility definitions.
141f7cf2976SLionel Sambuc  */
142f7cf2976SLionel Sambuc #ifndef CHARBITS
143f7cf2976SLionel Sambuc #define	UCHARAT(p)	((int)*(unsigned char *)(p))
144f7cf2976SLionel Sambuc #else
145f7cf2976SLionel Sambuc #define	UCHARAT(p)	((int)*(p)&CHARBITS)
146f7cf2976SLionel Sambuc #endif
147f7cf2976SLionel Sambuc 
148f7cf2976SLionel Sambuc #define	FAIL(m)	{ regerror(m); return(NULL); }
149f7cf2976SLionel Sambuc #define	ISMULT(c)	((c) == '*' || (c) == '+' || (c) == '?')
150f7cf2976SLionel Sambuc #define	META	"^$.[()|?+*\\"
151f7cf2976SLionel Sambuc 
152f7cf2976SLionel Sambuc /*
153f7cf2976SLionel Sambuc  * Flags to be passed up and down.
154f7cf2976SLionel Sambuc  */
155f7cf2976SLionel Sambuc #define	HASWIDTH	01	/* Known never to match null string. */
156f7cf2976SLionel Sambuc #define	SIMPLE		02	/* Simple enough to be STAR/PLUS operand. */
157f7cf2976SLionel Sambuc #define	SPSTART		04	/* Starts with * or +. */
158f7cf2976SLionel Sambuc #define	WORST		0	/* Worst case. */
159f7cf2976SLionel Sambuc 
160f7cf2976SLionel Sambuc /*
161f7cf2976SLionel Sambuc  * Global work variables for regcomp().
162f7cf2976SLionel Sambuc  */
163f7cf2976SLionel Sambuc static char *regparse;		/* Input-scan pointer. */
164f7cf2976SLionel Sambuc static int regnpar;		/* () count. */
165f7cf2976SLionel Sambuc static char regdummy;
166f7cf2976SLionel Sambuc static char *regcode;		/* Code-emit pointer; &regdummy = don't. */
167f7cf2976SLionel Sambuc static long regsize;		/* Code size. */
168f7cf2976SLionel Sambuc 
169f7cf2976SLionel Sambuc /*
170f7cf2976SLionel Sambuc  * The first byte of the regexp internal "program" is actually this magic
171f7cf2976SLionel Sambuc  * number; the start node begins in the second byte.
172f7cf2976SLionel Sambuc  */
173f7cf2976SLionel Sambuc #define	MAGIC	0234
174f7cf2976SLionel Sambuc 
175f7cf2976SLionel Sambuc 
176f7cf2976SLionel Sambuc /*
177f7cf2976SLionel Sambuc  * Forward declarations for regcomp()'s friends.
178f7cf2976SLionel Sambuc  */
179f7cf2976SLionel Sambuc #ifndef STATIC
180f7cf2976SLionel Sambuc #define	STATIC	static
181f7cf2976SLionel Sambuc #endif
182f7cf2976SLionel Sambuc STATIC char *reg();
183f7cf2976SLionel Sambuc STATIC char *regbranch();
184f7cf2976SLionel Sambuc STATIC char *regpiece();
185f7cf2976SLionel Sambuc STATIC char *regatom();
186f7cf2976SLionel Sambuc STATIC char *regnode();
187f7cf2976SLionel Sambuc STATIC char *regnext();
188f7cf2976SLionel Sambuc STATIC void regc();
189f7cf2976SLionel Sambuc STATIC void reginsert();
190f7cf2976SLionel Sambuc STATIC void regtail();
191f7cf2976SLionel Sambuc STATIC void regoptail();
192f7cf2976SLionel Sambuc #ifdef STRCSPN
193f7cf2976SLionel Sambuc STATIC int strcspn();
194f7cf2976SLionel Sambuc #endif
195f7cf2976SLionel Sambuc 
196f7cf2976SLionel Sambuc /*
197f7cf2976SLionel Sambuc  - regcomp - compile a regular expression into internal code
198f7cf2976SLionel Sambuc  *
199f7cf2976SLionel Sambuc  * We can't allocate space until we know how big the compiled form will be,
200f7cf2976SLionel Sambuc  * but we can't compile it (and thus know how big it is) until we've got a
201f7cf2976SLionel Sambuc  * place to put the code.  So we cheat:  we compile it twice, once with code
202f7cf2976SLionel Sambuc  * generation turned off and size counting turned on, and once "for real".
203f7cf2976SLionel Sambuc  * This also means that we don't allocate space until we are sure that the
204f7cf2976SLionel Sambuc  * thing really will compile successfully, and we never have to move the
205f7cf2976SLionel Sambuc  * code and thus invalidate pointers into it.  (Note that it has to be in
206f7cf2976SLionel Sambuc  * one piece because free() must be able to free it all.)
207f7cf2976SLionel Sambuc  *
208f7cf2976SLionel Sambuc  * Beware that the optimization-preparation code in here knows about some
209f7cf2976SLionel Sambuc  * of the structure of the compiled regexp.
210f7cf2976SLionel Sambuc  */
211f7cf2976SLionel Sambuc regexp *
regcomp(exp)212f7cf2976SLionel Sambuc regcomp(exp)
213f7cf2976SLionel Sambuc char *exp;
214f7cf2976SLionel Sambuc {
215f7cf2976SLionel Sambuc 	register regexp *r;
216f7cf2976SLionel Sambuc 	register char *scan;
217f7cf2976SLionel Sambuc 	register char *longest;
218f7cf2976SLionel Sambuc 	register int len;
219f7cf2976SLionel Sambuc 	int flags;
220f7cf2976SLionel Sambuc 
221f7cf2976SLionel Sambuc 	if (exp == NULL)
222f7cf2976SLionel Sambuc 		FAIL("NULL argument");
223f7cf2976SLionel Sambuc 
224f7cf2976SLionel Sambuc 	/* First pass: determine size, legality. */
225f7cf2976SLionel Sambuc 	regparse = exp;
226f7cf2976SLionel Sambuc 	regnpar = 1;
227f7cf2976SLionel Sambuc 	regsize = 0L;
228f7cf2976SLionel Sambuc 	regcode = &regdummy;
229f7cf2976SLionel Sambuc 	regc(MAGIC);
230f7cf2976SLionel Sambuc 	if (reg(0, &flags) == NULL)
231f7cf2976SLionel Sambuc 		return(NULL);
232f7cf2976SLionel Sambuc 
233f7cf2976SLionel Sambuc 	/* Small enough for pointer-storage convention? */
234f7cf2976SLionel Sambuc 	if (regsize >= 32767L)		/* Probably could be 65535L. */
235f7cf2976SLionel Sambuc 		FAIL("regexp too big");
236f7cf2976SLionel Sambuc 
237f7cf2976SLionel Sambuc 	/* Allocate space. */
238f7cf2976SLionel Sambuc 	r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
239f7cf2976SLionel Sambuc 	if (r == NULL)
240f7cf2976SLionel Sambuc 		FAIL("out of space");
241f7cf2976SLionel Sambuc 
242f7cf2976SLionel Sambuc 	/* Second pass: emit code. */
243f7cf2976SLionel Sambuc 	regparse = exp;
244f7cf2976SLionel Sambuc 	regnpar = 1;
245f7cf2976SLionel Sambuc 	regcode = r->program;
246f7cf2976SLionel Sambuc 	regc(MAGIC);
247f7cf2976SLionel Sambuc 	if (reg(0, &flags) == NULL)
248f7cf2976SLionel Sambuc 		return(NULL);
249f7cf2976SLionel Sambuc 
250f7cf2976SLionel Sambuc 	/* Dig out information for optimizations. */
251f7cf2976SLionel Sambuc 	r->regstart = '\0';	/* Worst-case defaults. */
252f7cf2976SLionel Sambuc 	r->reganch = 0;
253f7cf2976SLionel Sambuc 	r->regmust = NULL;
254f7cf2976SLionel Sambuc 	r->regmlen = 0;
255f7cf2976SLionel Sambuc 	scan = r->program+1;			/* First BRANCH. */
256f7cf2976SLionel Sambuc 	if (OP(regnext(scan)) == END) {		/* Only one top-level choice. */
257f7cf2976SLionel Sambuc 		scan = OPERAND(scan);
258f7cf2976SLionel Sambuc 
259f7cf2976SLionel Sambuc 		/* Starting-point info. */
260f7cf2976SLionel Sambuc 		if (OP(scan) == EXACTLY)
261f7cf2976SLionel Sambuc 			r->regstart = *OPERAND(scan);
262f7cf2976SLionel Sambuc 		else if (OP(scan) == BOL)
263f7cf2976SLionel Sambuc 			r->reganch++;
264f7cf2976SLionel Sambuc 
265f7cf2976SLionel Sambuc 		/*
266f7cf2976SLionel Sambuc 		 * If there's something expensive in the r.e., find the
267f7cf2976SLionel Sambuc 		 * longest literal string that must appear and make it the
268f7cf2976SLionel Sambuc 		 * regmust.  Resolve ties in favor of later strings, since
269f7cf2976SLionel Sambuc 		 * the regstart check works with the beginning of the r.e.
270f7cf2976SLionel Sambuc 		 * and avoiding duplication strengthens checking.  Not a
271f7cf2976SLionel Sambuc 		 * strong reason, but sufficient in the absence of others.
272f7cf2976SLionel Sambuc 		 */
273f7cf2976SLionel Sambuc 		if (flags&SPSTART) {
274f7cf2976SLionel Sambuc 			longest = NULL;
275f7cf2976SLionel Sambuc 			len = 0;
276f7cf2976SLionel Sambuc 			for (; scan != NULL; scan = regnext(scan))
277f7cf2976SLionel Sambuc 				if (OP(scan) == EXACTLY && ((int) strlen(OPERAND(scan))) >= len) {
278f7cf2976SLionel Sambuc 					longest = OPERAND(scan);
279f7cf2976SLionel Sambuc 					len = strlen(OPERAND(scan));
280f7cf2976SLionel Sambuc 				}
281f7cf2976SLionel Sambuc 			r->regmust = longest;
282f7cf2976SLionel Sambuc 			r->regmlen = len;
283f7cf2976SLionel Sambuc 		}
284f7cf2976SLionel Sambuc 	}
285f7cf2976SLionel Sambuc 
286f7cf2976SLionel Sambuc 	return(r);
287f7cf2976SLionel Sambuc }
288f7cf2976SLionel Sambuc 
289f7cf2976SLionel Sambuc /*
290f7cf2976SLionel Sambuc  - reg - regular expression, i.e. main body or parenthesized thing
291f7cf2976SLionel Sambuc  *
292f7cf2976SLionel Sambuc  * Caller must absorb opening parenthesis.
293f7cf2976SLionel Sambuc  *
294f7cf2976SLionel Sambuc  * Combining parenthesis handling with the base level of regular expression
295f7cf2976SLionel Sambuc  * is a trifle forced, but the need to tie the tails of the branches to what
296f7cf2976SLionel Sambuc  * follows makes it hard to avoid.
297f7cf2976SLionel Sambuc  */
298f7cf2976SLionel Sambuc static char *
reg(paren,flagp)299f7cf2976SLionel Sambuc reg(paren, flagp)
300f7cf2976SLionel Sambuc int paren;			/* Parenthesized? */
301f7cf2976SLionel Sambuc int *flagp;
302f7cf2976SLionel Sambuc {
303f7cf2976SLionel Sambuc 	register char *ret;
304f7cf2976SLionel Sambuc 	register char *br;
305f7cf2976SLionel Sambuc 	register char *ender;
306f7cf2976SLionel Sambuc 	register int parno = 0;
307f7cf2976SLionel Sambuc 	int flags;
308f7cf2976SLionel Sambuc 
309f7cf2976SLionel Sambuc 	*flagp = HASWIDTH;	/* Tentatively. */
310f7cf2976SLionel Sambuc 
311f7cf2976SLionel Sambuc 	/* Make an OPEN node, if parenthesized. */
312f7cf2976SLionel Sambuc 	if (paren) {
313f7cf2976SLionel Sambuc 		if (regnpar >= NSUBEXP)
314f7cf2976SLionel Sambuc 			FAIL("too many ()");
315f7cf2976SLionel Sambuc 		parno = regnpar;
316f7cf2976SLionel Sambuc 		regnpar++;
317f7cf2976SLionel Sambuc 		ret = regnode(OPEN+parno);
318f7cf2976SLionel Sambuc 	} else
319f7cf2976SLionel Sambuc 		ret = NULL;
320f7cf2976SLionel Sambuc 
321f7cf2976SLionel Sambuc 	/* Pick up the branches, linking them together. */
322f7cf2976SLionel Sambuc 	br = regbranch(&flags);
323f7cf2976SLionel Sambuc 	if (br == NULL)
324f7cf2976SLionel Sambuc 		return(NULL);
325f7cf2976SLionel Sambuc 	if (ret != NULL)
326f7cf2976SLionel Sambuc 		regtail(ret, br);	/* OPEN -> first. */
327f7cf2976SLionel Sambuc 	else
328f7cf2976SLionel Sambuc 		ret = br;
329f7cf2976SLionel Sambuc 	if (!(flags&HASWIDTH))
330f7cf2976SLionel Sambuc 		*flagp &= ~HASWIDTH;
331f7cf2976SLionel Sambuc 	*flagp |= flags&SPSTART;
332f7cf2976SLionel Sambuc 	while (*regparse == '|') {
333f7cf2976SLionel Sambuc 		regparse++;
334f7cf2976SLionel Sambuc 		br = regbranch(&flags);
335f7cf2976SLionel Sambuc 		if (br == NULL)
336f7cf2976SLionel Sambuc 			return(NULL);
337f7cf2976SLionel Sambuc 		regtail(ret, br);	/* BRANCH -> BRANCH. */
338f7cf2976SLionel Sambuc 		if (!(flags&HASWIDTH))
339f7cf2976SLionel Sambuc 			*flagp &= ~HASWIDTH;
340f7cf2976SLionel Sambuc 		*flagp |= flags&SPSTART;
341f7cf2976SLionel Sambuc 	}
342f7cf2976SLionel Sambuc 
343f7cf2976SLionel Sambuc 	/* Make a closing node, and hook it on the end. */
344f7cf2976SLionel Sambuc 	ender = regnode((paren) ? CLOSE+parno : END);
345f7cf2976SLionel Sambuc 	regtail(ret, ender);
346f7cf2976SLionel Sambuc 
347f7cf2976SLionel Sambuc 	/* Hook the tails of the branches to the closing node. */
348f7cf2976SLionel Sambuc 	for (br = ret; br != NULL; br = regnext(br))
349f7cf2976SLionel Sambuc 		regoptail(br, ender);
350f7cf2976SLionel Sambuc 
351f7cf2976SLionel Sambuc 	/* Check for proper termination. */
352f7cf2976SLionel Sambuc 	if (paren && *regparse++ != ')') {
353f7cf2976SLionel Sambuc 		FAIL("unmatched ()");
354f7cf2976SLionel Sambuc 	} else if (!paren && *regparse != '\0') {
355f7cf2976SLionel Sambuc 		if (*regparse == ')') {
356f7cf2976SLionel Sambuc 			FAIL("unmatched ()");
357f7cf2976SLionel Sambuc 		} else
358f7cf2976SLionel Sambuc 			FAIL("junk on end");	/* "Can't happen". */
359f7cf2976SLionel Sambuc 		/* NOTREACHED */
360f7cf2976SLionel Sambuc 	}
361f7cf2976SLionel Sambuc 
362f7cf2976SLionel Sambuc 	return(ret);
363f7cf2976SLionel Sambuc }
364f7cf2976SLionel Sambuc 
365f7cf2976SLionel Sambuc /*
366f7cf2976SLionel Sambuc  - regbranch - one alternative of an | operator
367f7cf2976SLionel Sambuc  *
368f7cf2976SLionel Sambuc  * Implements the concatenation operator.
369f7cf2976SLionel Sambuc  */
370f7cf2976SLionel Sambuc static char *
regbranch(flagp)371f7cf2976SLionel Sambuc regbranch(flagp)
372f7cf2976SLionel Sambuc int *flagp;
373f7cf2976SLionel Sambuc {
374f7cf2976SLionel Sambuc 	register char *ret;
375f7cf2976SLionel Sambuc 	register char *chain;
376f7cf2976SLionel Sambuc 	register char *latest;
377f7cf2976SLionel Sambuc 	int flags;
378f7cf2976SLionel Sambuc 
379f7cf2976SLionel Sambuc 	*flagp = WORST;		/* Tentatively. */
380f7cf2976SLionel Sambuc 
381f7cf2976SLionel Sambuc 	ret = regnode(BRANCH);
382f7cf2976SLionel Sambuc 	chain = NULL;
383f7cf2976SLionel Sambuc 	while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
384f7cf2976SLionel Sambuc 		latest = regpiece(&flags);
385f7cf2976SLionel Sambuc 		if (latest == NULL)
386f7cf2976SLionel Sambuc 			return(NULL);
387f7cf2976SLionel Sambuc 		*flagp |= flags&HASWIDTH;
388f7cf2976SLionel Sambuc 		if (chain == NULL)	/* First piece. */
389f7cf2976SLionel Sambuc 			*flagp |= flags&SPSTART;
390f7cf2976SLionel Sambuc 		else
391f7cf2976SLionel Sambuc 			regtail(chain, latest);
392f7cf2976SLionel Sambuc 		chain = latest;
393f7cf2976SLionel Sambuc 	}
394f7cf2976SLionel Sambuc 	if (chain == NULL)	/* Loop ran zero times. */
395f7cf2976SLionel Sambuc 		(void) regnode(NOTHING);
396f7cf2976SLionel Sambuc 
397f7cf2976SLionel Sambuc 	return(ret);
398f7cf2976SLionel Sambuc }
399f7cf2976SLionel Sambuc 
400f7cf2976SLionel Sambuc /*
401f7cf2976SLionel Sambuc  - regpiece - something followed by possible [*+?]
402f7cf2976SLionel Sambuc  *
403f7cf2976SLionel Sambuc  * Note that the branching code sequences used for ? and the general cases
404f7cf2976SLionel Sambuc  * of * and + are somewhat optimized:  they use the same NOTHING node as
405f7cf2976SLionel Sambuc  * both the endmarker for their branch list and the body of the last branch.
406f7cf2976SLionel Sambuc  * It might seem that this node could be dispensed with entirely, but the
407f7cf2976SLionel Sambuc  * endmarker role is not redundant.
408f7cf2976SLionel Sambuc  */
409f7cf2976SLionel Sambuc static char *
regpiece(flagp)410f7cf2976SLionel Sambuc regpiece(flagp)
411f7cf2976SLionel Sambuc int *flagp;
412f7cf2976SLionel Sambuc {
413f7cf2976SLionel Sambuc 	register char *ret;
414f7cf2976SLionel Sambuc 	register char op;
415f7cf2976SLionel Sambuc 	register char *next;
416f7cf2976SLionel Sambuc 	int flags;
417f7cf2976SLionel Sambuc 
418f7cf2976SLionel Sambuc 	ret = regatom(&flags);
419f7cf2976SLionel Sambuc 	if (ret == NULL)
420f7cf2976SLionel Sambuc 		return(NULL);
421f7cf2976SLionel Sambuc 
422f7cf2976SLionel Sambuc 	op = *regparse;
423f7cf2976SLionel Sambuc 	if (!ISMULT(op)) {
424f7cf2976SLionel Sambuc 		*flagp = flags;
425f7cf2976SLionel Sambuc 		return(ret);
426f7cf2976SLionel Sambuc 	}
427f7cf2976SLionel Sambuc 
428f7cf2976SLionel Sambuc 	if (!(flags&HASWIDTH) && op != '?')
429f7cf2976SLionel Sambuc 		FAIL("*+ operand could be empty");
430f7cf2976SLionel Sambuc 	*flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
431f7cf2976SLionel Sambuc 
432f7cf2976SLionel Sambuc 	if (op == '*' && (flags&SIMPLE))
433f7cf2976SLionel Sambuc 		reginsert(STAR, ret);
434f7cf2976SLionel Sambuc 	else if (op == '*') {
435f7cf2976SLionel Sambuc 		/* Emit x* as (x&|), where & means "self". */
436f7cf2976SLionel Sambuc 		reginsert(BRANCH, ret);			/* Either x */
437f7cf2976SLionel Sambuc 		regoptail(ret, regnode(BACK));		/* and loop */
438f7cf2976SLionel Sambuc 		regoptail(ret, ret);			/* back */
439f7cf2976SLionel Sambuc 		regtail(ret, regnode(BRANCH));		/* or */
440f7cf2976SLionel Sambuc 		regtail(ret, regnode(NOTHING));		/* null. */
441f7cf2976SLionel Sambuc 	} else if (op == '+' && (flags&SIMPLE))
442f7cf2976SLionel Sambuc 		reginsert(PLUS, ret);
443f7cf2976SLionel Sambuc 	else if (op == '+') {
444f7cf2976SLionel Sambuc 		/* Emit x+ as x(&|), where & means "self". */
445f7cf2976SLionel Sambuc 		next = regnode(BRANCH);			/* Either */
446f7cf2976SLionel Sambuc 		regtail(ret, next);
447f7cf2976SLionel Sambuc 		regtail(regnode(BACK), ret);		/* loop back */
448f7cf2976SLionel Sambuc 		regtail(next, regnode(BRANCH));		/* or */
449f7cf2976SLionel Sambuc 		regtail(ret, regnode(NOTHING));		/* null. */
450f7cf2976SLionel Sambuc 	} else if (op == '?') {
451f7cf2976SLionel Sambuc 		/* Emit x? as (x|) */
452f7cf2976SLionel Sambuc 		reginsert(BRANCH, ret);			/* Either x */
453f7cf2976SLionel Sambuc 		regtail(ret, regnode(BRANCH));		/* or */
454f7cf2976SLionel Sambuc 		next = regnode(NOTHING);		/* null. */
455f7cf2976SLionel Sambuc 		regtail(ret, next);
456f7cf2976SLionel Sambuc 		regoptail(ret, next);
457f7cf2976SLionel Sambuc 	}
458f7cf2976SLionel Sambuc 	regparse++;
459f7cf2976SLionel Sambuc 	if (ISMULT(*regparse))
460f7cf2976SLionel Sambuc 		FAIL("nested *?+");
461f7cf2976SLionel Sambuc 
462f7cf2976SLionel Sambuc 	return(ret);
463f7cf2976SLionel Sambuc }
464f7cf2976SLionel Sambuc 
465f7cf2976SLionel Sambuc /*
466f7cf2976SLionel Sambuc  - regatom - the lowest level
467f7cf2976SLionel Sambuc  *
468f7cf2976SLionel Sambuc  * Optimization:  gobbles an entire sequence of ordinary characters so that
469f7cf2976SLionel Sambuc  * it can turn them into a single node, which is smaller to store and
470f7cf2976SLionel Sambuc  * faster to run.  Backslashed characters are exceptions, each becoming a
471f7cf2976SLionel Sambuc  * separate node; the code is simpler that way and it's not worth fixing.
472f7cf2976SLionel Sambuc  */
473f7cf2976SLionel Sambuc static char *
regatom(flagp)474f7cf2976SLionel Sambuc regatom(flagp)
475f7cf2976SLionel Sambuc int *flagp;
476f7cf2976SLionel Sambuc {
477f7cf2976SLionel Sambuc 	register char *ret;
478f7cf2976SLionel Sambuc 	int flags;
479f7cf2976SLionel Sambuc 
480f7cf2976SLionel Sambuc 	*flagp = WORST;		/* Tentatively. */
481f7cf2976SLionel Sambuc 
482f7cf2976SLionel Sambuc 	switch (*regparse++) {
483f7cf2976SLionel Sambuc 	case '^':
484f7cf2976SLionel Sambuc 		ret = regnode(BOL);
485f7cf2976SLionel Sambuc 		break;
486f7cf2976SLionel Sambuc 	case '$':
487f7cf2976SLionel Sambuc 		ret = regnode(EOL);
488f7cf2976SLionel Sambuc 		break;
489f7cf2976SLionel Sambuc 	case '.':
490f7cf2976SLionel Sambuc 		ret = regnode(ANY);
491f7cf2976SLionel Sambuc 		*flagp |= HASWIDTH|SIMPLE;
492f7cf2976SLionel Sambuc 		break;
493f7cf2976SLionel Sambuc 	case '[': {
494f7cf2976SLionel Sambuc 			register int clss;
495f7cf2976SLionel Sambuc 			register int classend;
496f7cf2976SLionel Sambuc 
497f7cf2976SLionel Sambuc 			if (*regparse == '^') {	/* Complement of range. */
498f7cf2976SLionel Sambuc 				ret = regnode(ANYBUT);
499f7cf2976SLionel Sambuc 				regparse++;
500f7cf2976SLionel Sambuc 			} else
501f7cf2976SLionel Sambuc 				ret = regnode(ANYOF);
502f7cf2976SLionel Sambuc 			if (*regparse == ']' || *regparse == '-')
503f7cf2976SLionel Sambuc 				regc(*regparse++);
504f7cf2976SLionel Sambuc 			while (*regparse != '\0' && *regparse != ']') {
505f7cf2976SLionel Sambuc 				if (*regparse == '-') {
506f7cf2976SLionel Sambuc 					regparse++;
507f7cf2976SLionel Sambuc 					if (*regparse == ']' || *regparse == '\0')
508f7cf2976SLionel Sambuc 						regc('-');
509f7cf2976SLionel Sambuc 					else {
510f7cf2976SLionel Sambuc 						clss = UCHARAT(regparse-2)+1;
511f7cf2976SLionel Sambuc 						classend = UCHARAT(regparse);
512f7cf2976SLionel Sambuc 						if (clss > classend+1)
513f7cf2976SLionel Sambuc 							FAIL("invalid [] range");
514f7cf2976SLionel Sambuc 						for (; clss <= classend; clss++)
515f7cf2976SLionel Sambuc 							regc(clss);
516f7cf2976SLionel Sambuc 						regparse++;
517f7cf2976SLionel Sambuc 					}
518f7cf2976SLionel Sambuc 				} else
519f7cf2976SLionel Sambuc 					regc(*regparse++);
520f7cf2976SLionel Sambuc 			}
521f7cf2976SLionel Sambuc 			regc('\0');
522f7cf2976SLionel Sambuc 			if (*regparse != ']')
523f7cf2976SLionel Sambuc 				FAIL("unmatched []");
524f7cf2976SLionel Sambuc 			regparse++;
525f7cf2976SLionel Sambuc 			*flagp |= HASWIDTH|SIMPLE;
526f7cf2976SLionel Sambuc 		}
527f7cf2976SLionel Sambuc 		break;
528f7cf2976SLionel Sambuc 	case '(':
529f7cf2976SLionel Sambuc 		ret = reg(1, &flags);
530f7cf2976SLionel Sambuc 		if (ret == NULL)
531f7cf2976SLionel Sambuc 			return(NULL);
532f7cf2976SLionel Sambuc 		*flagp |= flags&(HASWIDTH|SPSTART);
533f7cf2976SLionel Sambuc 		break;
534f7cf2976SLionel Sambuc 	case '\0':
535f7cf2976SLionel Sambuc 	case '|':
536f7cf2976SLionel Sambuc 	case ')':
537f7cf2976SLionel Sambuc 		FAIL("internal urp");	/* Supposed to be caught earlier. */
538f7cf2976SLionel Sambuc 		/* NOTREACHED */
539f7cf2976SLionel Sambuc 		break;
540f7cf2976SLionel Sambuc 	case '?':
541f7cf2976SLionel Sambuc 	case '+':
542f7cf2976SLionel Sambuc 	case '*':
543f7cf2976SLionel Sambuc 		FAIL("?+* follows nothing");
544f7cf2976SLionel Sambuc 		/* NOTREACHED */
545f7cf2976SLionel Sambuc 		break;
546f7cf2976SLionel Sambuc 	case '\\':
547f7cf2976SLionel Sambuc 		if (*regparse == '\0')
548f7cf2976SLionel Sambuc 			FAIL("trailing \\");
549f7cf2976SLionel Sambuc 		ret = regnode(EXACTLY);
550f7cf2976SLionel Sambuc 		regc(*regparse++);
551f7cf2976SLionel Sambuc 		regc('\0');
552f7cf2976SLionel Sambuc 		*flagp |= HASWIDTH|SIMPLE;
553f7cf2976SLionel Sambuc 		break;
554f7cf2976SLionel Sambuc 	default: {
555f7cf2976SLionel Sambuc 			register int len;
556f7cf2976SLionel Sambuc 			register char ender;
557f7cf2976SLionel Sambuc 
558f7cf2976SLionel Sambuc 			regparse--;
559f7cf2976SLionel Sambuc 			len = strcspn(regparse, META);
560f7cf2976SLionel Sambuc 			if (len <= 0)
561f7cf2976SLionel Sambuc 				FAIL("internal disaster");
562f7cf2976SLionel Sambuc 			ender = *(regparse+len);
563f7cf2976SLionel Sambuc 			if (len > 1 && ISMULT(ender))
564f7cf2976SLionel Sambuc 				len--;		/* Back off clear of ?+* operand. */
565f7cf2976SLionel Sambuc 			*flagp |= HASWIDTH;
566f7cf2976SLionel Sambuc 			if (len == 1)
567f7cf2976SLionel Sambuc 				*flagp |= SIMPLE;
568f7cf2976SLionel Sambuc 			ret = regnode(EXACTLY);
569f7cf2976SLionel Sambuc 			while (len > 0) {
570f7cf2976SLionel Sambuc 				regc(*regparse++);
571f7cf2976SLionel Sambuc 				len--;
572f7cf2976SLionel Sambuc 			}
573f7cf2976SLionel Sambuc 			regc('\0');
574f7cf2976SLionel Sambuc 		}
575f7cf2976SLionel Sambuc 		break;
576f7cf2976SLionel Sambuc 	}
577f7cf2976SLionel Sambuc 
578f7cf2976SLionel Sambuc 	return(ret);
579f7cf2976SLionel Sambuc }
580f7cf2976SLionel Sambuc 
581f7cf2976SLionel Sambuc /*
582f7cf2976SLionel Sambuc  - regnode - emit a node
583f7cf2976SLionel Sambuc  */
584f7cf2976SLionel Sambuc static char *			/* Location. */
regnode(op)585f7cf2976SLionel Sambuc regnode(op)
586f7cf2976SLionel Sambuc char op;
587f7cf2976SLionel Sambuc {
588f7cf2976SLionel Sambuc 	register char *ret;
589f7cf2976SLionel Sambuc 	register char *ptr;
590f7cf2976SLionel Sambuc 
591f7cf2976SLionel Sambuc 	ret = regcode;
592f7cf2976SLionel Sambuc 	if (ret == &regdummy) {
593f7cf2976SLionel Sambuc 		regsize += 3;
594f7cf2976SLionel Sambuc 		return(ret);
595f7cf2976SLionel Sambuc 	}
596f7cf2976SLionel Sambuc 
597f7cf2976SLionel Sambuc 	ptr = ret;
598f7cf2976SLionel Sambuc 	*ptr++ = op;
599f7cf2976SLionel Sambuc 	*ptr++ = '\0';		/* Null "next" pointer. */
600f7cf2976SLionel Sambuc 	*ptr++ = '\0';
601f7cf2976SLionel Sambuc 	regcode = ptr;
602f7cf2976SLionel Sambuc 
603f7cf2976SLionel Sambuc 	return(ret);
604f7cf2976SLionel Sambuc }
605f7cf2976SLionel Sambuc 
606f7cf2976SLionel Sambuc /*
607f7cf2976SLionel Sambuc  - regc - emit (if appropriate) a byte of code
608f7cf2976SLionel Sambuc  */
609f7cf2976SLionel Sambuc static void
regc(b)610f7cf2976SLionel Sambuc regc(b)
611f7cf2976SLionel Sambuc char b;
612f7cf2976SLionel Sambuc {
613f7cf2976SLionel Sambuc 	if (regcode != &regdummy)
614f7cf2976SLionel Sambuc 		*regcode++ = b;
615f7cf2976SLionel Sambuc 	else
616f7cf2976SLionel Sambuc 		regsize++;
617f7cf2976SLionel Sambuc }
618f7cf2976SLionel Sambuc 
619f7cf2976SLionel Sambuc /*
620f7cf2976SLionel Sambuc  - reginsert - insert an operator in front of already-emitted operand
621f7cf2976SLionel Sambuc  *
622f7cf2976SLionel Sambuc  * Means relocating the operand.
623f7cf2976SLionel Sambuc  */
624f7cf2976SLionel Sambuc static void
reginsert(op,opnd)625f7cf2976SLionel Sambuc reginsert(op, opnd)
626f7cf2976SLionel Sambuc char op;
627f7cf2976SLionel Sambuc char *opnd;
628f7cf2976SLionel Sambuc {
629f7cf2976SLionel Sambuc 	register char *src;
630f7cf2976SLionel Sambuc 	register char *dst;
631f7cf2976SLionel Sambuc 	register char *place;
632f7cf2976SLionel Sambuc 
633f7cf2976SLionel Sambuc 	if (regcode == &regdummy) {
634f7cf2976SLionel Sambuc 		regsize += 3;
635f7cf2976SLionel Sambuc 		return;
636f7cf2976SLionel Sambuc 	}
637f7cf2976SLionel Sambuc 
638f7cf2976SLionel Sambuc 	src = regcode;
639f7cf2976SLionel Sambuc 	regcode += 3;
640f7cf2976SLionel Sambuc 	dst = regcode;
641f7cf2976SLionel Sambuc 	while (src > opnd)
642f7cf2976SLionel Sambuc 		*--dst = *--src;
643f7cf2976SLionel Sambuc 
644f7cf2976SLionel Sambuc 	place = opnd;		/* Op node, where operand used to be. */
645f7cf2976SLionel Sambuc 	*place++ = op;
646f7cf2976SLionel Sambuc 	*place++ = '\0';
647f7cf2976SLionel Sambuc 	*place++ = '\0';
648f7cf2976SLionel Sambuc }
649f7cf2976SLionel Sambuc 
650f7cf2976SLionel Sambuc /*
651f7cf2976SLionel Sambuc  - regtail - set the next-pointer at the end of a node chain
652f7cf2976SLionel Sambuc  */
653f7cf2976SLionel Sambuc static void
regtail(p,val)654f7cf2976SLionel Sambuc regtail(p, val)
655f7cf2976SLionel Sambuc char *p;
656f7cf2976SLionel Sambuc char *val;
657f7cf2976SLionel Sambuc {
658f7cf2976SLionel Sambuc 	register char *scan;
659f7cf2976SLionel Sambuc 	register char *temp;
660f7cf2976SLionel Sambuc 	register int offset;
661f7cf2976SLionel Sambuc 
662f7cf2976SLionel Sambuc 	if (p == &regdummy)
663f7cf2976SLionel Sambuc 		return;
664f7cf2976SLionel Sambuc 
665f7cf2976SLionel Sambuc 	/* Find last node. */
666f7cf2976SLionel Sambuc 	scan = p;
667f7cf2976SLionel Sambuc 	for (;;) {
668f7cf2976SLionel Sambuc 		temp = regnext(scan);
669f7cf2976SLionel Sambuc 		if (temp == NULL)
670f7cf2976SLionel Sambuc 			break;
671f7cf2976SLionel Sambuc 		scan = temp;
672f7cf2976SLionel Sambuc 	}
673f7cf2976SLionel Sambuc 
674f7cf2976SLionel Sambuc 	if (OP(scan) == BACK)
675f7cf2976SLionel Sambuc 		offset = scan - val;
676f7cf2976SLionel Sambuc 	else
677f7cf2976SLionel Sambuc 		offset = val - scan;
678f7cf2976SLionel Sambuc 	*(scan+1) = (offset>>8)&0377;
679f7cf2976SLionel Sambuc 	*(scan+2) = offset&0377;
680f7cf2976SLionel Sambuc }
681f7cf2976SLionel Sambuc 
682f7cf2976SLionel Sambuc /*
683f7cf2976SLionel Sambuc  - regoptail - regtail on operand of first argument; nop if operandless
684f7cf2976SLionel Sambuc  */
685f7cf2976SLionel Sambuc static void
regoptail(p,val)686f7cf2976SLionel Sambuc regoptail(p, val)
687f7cf2976SLionel Sambuc char *p;
688f7cf2976SLionel Sambuc char *val;
689f7cf2976SLionel Sambuc {
690f7cf2976SLionel Sambuc 	/* "Operandless" and "op != BRANCH" are synonymous in practice. */
691f7cf2976SLionel Sambuc 	if (p == NULL || p == &regdummy || OP(p) != BRANCH)
692f7cf2976SLionel Sambuc 		return;
693f7cf2976SLionel Sambuc 	regtail(OPERAND(p), val);
694f7cf2976SLionel Sambuc }
695f7cf2976SLionel Sambuc 
696f7cf2976SLionel Sambuc /*
697f7cf2976SLionel Sambuc  * regexec and friends
698f7cf2976SLionel Sambuc  */
699f7cf2976SLionel Sambuc 
700f7cf2976SLionel Sambuc /*
701f7cf2976SLionel Sambuc  * Global work variables for regexec().
702f7cf2976SLionel Sambuc  */
703f7cf2976SLionel Sambuc static char *reginput;		/* String-input pointer. */
704f7cf2976SLionel Sambuc static char *regbol;		/* Beginning of input, for ^ check. */
705f7cf2976SLionel Sambuc static char **regstartp;	/* Pointer to startp array. */
706f7cf2976SLionel Sambuc static char **regendp;		/* Ditto for endp. */
707f7cf2976SLionel Sambuc 
708f7cf2976SLionel Sambuc /*
709f7cf2976SLionel Sambuc  * Forwards.
710f7cf2976SLionel Sambuc  */
711f7cf2976SLionel Sambuc STATIC int regtry();
712f7cf2976SLionel Sambuc STATIC int regmatch();
713f7cf2976SLionel Sambuc STATIC int regrepeat();
714f7cf2976SLionel Sambuc 
715f7cf2976SLionel Sambuc #ifdef DEBUG
716f7cf2976SLionel Sambuc int regnarrate = 0;
717f7cf2976SLionel Sambuc void regdump();
718f7cf2976SLionel Sambuc STATIC char *regprop();
719f7cf2976SLionel Sambuc #endif
720f7cf2976SLionel Sambuc 
721f7cf2976SLionel Sambuc /*
722f7cf2976SLionel Sambuc  - regexec - match a regexp against a string
723f7cf2976SLionel Sambuc  */
724f7cf2976SLionel Sambuc int
regexec2(prog,string,notbol)725f7cf2976SLionel Sambuc regexec2(prog, string, notbol)
726f7cf2976SLionel Sambuc register regexp *prog;
727f7cf2976SLionel Sambuc register char *string;
728f7cf2976SLionel Sambuc int notbol;
729f7cf2976SLionel Sambuc {
730f7cf2976SLionel Sambuc 	register char *s;
731f7cf2976SLionel Sambuc 
732f7cf2976SLionel Sambuc 	/* Be paranoid... */
733f7cf2976SLionel Sambuc 	if (prog == NULL || string == NULL) {
734f7cf2976SLionel Sambuc 		regerror("NULL parameter");
735f7cf2976SLionel Sambuc 		return(0);
736f7cf2976SLionel Sambuc 	}
737f7cf2976SLionel Sambuc 
738f7cf2976SLionel Sambuc 	/* Check validity of program. */
739f7cf2976SLionel Sambuc 	if (UCHARAT(prog->program) != MAGIC) {
740f7cf2976SLionel Sambuc 		regerror("corrupted program");
741f7cf2976SLionel Sambuc 		return(0);
742f7cf2976SLionel Sambuc 	}
743f7cf2976SLionel Sambuc 
744f7cf2976SLionel Sambuc 	/* If there is a "must appear" string, look for it. */
745f7cf2976SLionel Sambuc 	if (prog->regmust != NULL) {
746f7cf2976SLionel Sambuc 		s = string;
747f7cf2976SLionel Sambuc 		while ((s = strchr(s, prog->regmust[0])) != NULL) {
748f7cf2976SLionel Sambuc 			if (strncmp(s, prog->regmust, prog->regmlen) == 0)
749f7cf2976SLionel Sambuc 				break;	/* Found it. */
750f7cf2976SLionel Sambuc 			s++;
751f7cf2976SLionel Sambuc 		}
752f7cf2976SLionel Sambuc 		if (s == NULL)	/* Not present. */
753f7cf2976SLionel Sambuc 			return(0);
754f7cf2976SLionel Sambuc 	}
755f7cf2976SLionel Sambuc 
756f7cf2976SLionel Sambuc 	/* Mark beginning of line for ^ . */
757f7cf2976SLionel Sambuc 	if (notbol)
758f7cf2976SLionel Sambuc 		regbol = NULL;
759f7cf2976SLionel Sambuc 	else
760f7cf2976SLionel Sambuc 		regbol = string;
761f7cf2976SLionel Sambuc 
762f7cf2976SLionel Sambuc 	/* Simplest case:  anchored match need be tried only once. */
763f7cf2976SLionel Sambuc 	if (prog->reganch)
764f7cf2976SLionel Sambuc 		return(regtry(prog, string));
765f7cf2976SLionel Sambuc 
766f7cf2976SLionel Sambuc 	/* Messy cases:  unanchored match. */
767f7cf2976SLionel Sambuc 	s = string;
768f7cf2976SLionel Sambuc 	if (prog->regstart != '\0')
769f7cf2976SLionel Sambuc 		/* We know what char it must start with. */
770f7cf2976SLionel Sambuc 		while ((s = strchr(s, prog->regstart)) != NULL) {
771f7cf2976SLionel Sambuc 			if (regtry(prog, s))
772f7cf2976SLionel Sambuc 				return(1);
773f7cf2976SLionel Sambuc 			s++;
774f7cf2976SLionel Sambuc 		}
775f7cf2976SLionel Sambuc 	else
776f7cf2976SLionel Sambuc 		/* We don't -- general case. */
777f7cf2976SLionel Sambuc 		do {
778f7cf2976SLionel Sambuc 			if (regtry(prog, s))
779f7cf2976SLionel Sambuc 				return(1);
780f7cf2976SLionel Sambuc 		} while (*s++ != '\0');
781f7cf2976SLionel Sambuc 
782f7cf2976SLionel Sambuc 	/* Failure. */
783f7cf2976SLionel Sambuc 	return(0);
784f7cf2976SLionel Sambuc }
785f7cf2976SLionel Sambuc 
786f7cf2976SLionel Sambuc int
regexec(prog,string)787f7cf2976SLionel Sambuc regexec(prog, string)
788f7cf2976SLionel Sambuc register regexp *prog;
789f7cf2976SLionel Sambuc register char *string;
790f7cf2976SLionel Sambuc {
791f7cf2976SLionel Sambuc 	return regexec2(prog, string, 0);
792f7cf2976SLionel Sambuc }
793f7cf2976SLionel Sambuc 
794f7cf2976SLionel Sambuc /*
795f7cf2976SLionel Sambuc  - regtry - try match at specific point
796f7cf2976SLionel Sambuc  */
797f7cf2976SLionel Sambuc static int			/* 0 failure, 1 success */
regtry(prog,string)798f7cf2976SLionel Sambuc regtry(prog, string)
799f7cf2976SLionel Sambuc regexp *prog;
800f7cf2976SLionel Sambuc char *string;
801f7cf2976SLionel Sambuc {
802f7cf2976SLionel Sambuc 	register int i;
803f7cf2976SLionel Sambuc 	register char **sp;
804f7cf2976SLionel Sambuc 	register char **ep;
805f7cf2976SLionel Sambuc 
806f7cf2976SLionel Sambuc 	reginput = string;
807f7cf2976SLionel Sambuc 	regstartp = prog->startp;
808f7cf2976SLionel Sambuc 	regendp = prog->endp;
809f7cf2976SLionel Sambuc 
810f7cf2976SLionel Sambuc 	sp = prog->startp;
811f7cf2976SLionel Sambuc 	ep = prog->endp;
812f7cf2976SLionel Sambuc 	for (i = NSUBEXP; i > 0; i--) {
813f7cf2976SLionel Sambuc 		*sp++ = NULL;
814f7cf2976SLionel Sambuc 		*ep++ = NULL;
815f7cf2976SLionel Sambuc 	}
816f7cf2976SLionel Sambuc 	if (regmatch(prog->program + 1)) {
817f7cf2976SLionel Sambuc 		prog->startp[0] = string;
818f7cf2976SLionel Sambuc 		prog->endp[0] = reginput;
819f7cf2976SLionel Sambuc 		return(1);
820f7cf2976SLionel Sambuc 	} else
821f7cf2976SLionel Sambuc 		return(0);
822f7cf2976SLionel Sambuc }
823f7cf2976SLionel Sambuc 
824f7cf2976SLionel Sambuc /*
825f7cf2976SLionel Sambuc  - regmatch - main matching routine
826f7cf2976SLionel Sambuc  *
827f7cf2976SLionel Sambuc  * Conceptually the strategy is simple:  check to see whether the current
828f7cf2976SLionel Sambuc  * node matches, call self recursively to see whether the rest matches,
829f7cf2976SLionel Sambuc  * and then act accordingly.  In practice we make some effort to avoid
830f7cf2976SLionel Sambuc  * recursion, in particular by going through "ordinary" nodes (that don't
831f7cf2976SLionel Sambuc  * need to know whether the rest of the match failed) by a loop instead of
832f7cf2976SLionel Sambuc  * by recursion.
833f7cf2976SLionel Sambuc  */
834f7cf2976SLionel Sambuc static int			/* 0 failure, 1 success */
regmatch(prog)835f7cf2976SLionel Sambuc regmatch(prog)
836f7cf2976SLionel Sambuc char *prog;
837f7cf2976SLionel Sambuc {
838f7cf2976SLionel Sambuc 	register char *scan;	/* Current node. */
839f7cf2976SLionel Sambuc 	char *next;		/* Next node. */
840f7cf2976SLionel Sambuc 
841f7cf2976SLionel Sambuc 	scan = prog;
842f7cf2976SLionel Sambuc #ifdef DEBUG
843f7cf2976SLionel Sambuc 	if (scan != NULL && regnarrate)
844f7cf2976SLionel Sambuc 		fprintf(stderr, "%s(\n", regprop(scan));
845f7cf2976SLionel Sambuc #endif
846f7cf2976SLionel Sambuc 	while (scan != NULL) {
847f7cf2976SLionel Sambuc #ifdef DEBUG
848f7cf2976SLionel Sambuc 		if (regnarrate)
849f7cf2976SLionel Sambuc 			fprintf(stderr, "%s...\n", regprop(scan));
850f7cf2976SLionel Sambuc #endif
851f7cf2976SLionel Sambuc 		next = regnext(scan);
852f7cf2976SLionel Sambuc 
853f7cf2976SLionel Sambuc 		switch (OP(scan)) {
854f7cf2976SLionel Sambuc 		case BOL:
855f7cf2976SLionel Sambuc 			if (reginput != regbol)
856f7cf2976SLionel Sambuc 				return(0);
857f7cf2976SLionel Sambuc 			break;
858f7cf2976SLionel Sambuc 		case EOL:
859f7cf2976SLionel Sambuc 			if (*reginput != '\0')
860f7cf2976SLionel Sambuc 				return(0);
861f7cf2976SLionel Sambuc 			break;
862f7cf2976SLionel Sambuc 		case ANY:
863f7cf2976SLionel Sambuc 			if (*reginput == '\0')
864f7cf2976SLionel Sambuc 				return(0);
865f7cf2976SLionel Sambuc 			reginput++;
866f7cf2976SLionel Sambuc 			break;
867f7cf2976SLionel Sambuc 		case EXACTLY: {
868f7cf2976SLionel Sambuc 				register int len;
869f7cf2976SLionel Sambuc 				register char *opnd;
870f7cf2976SLionel Sambuc 
871f7cf2976SLionel Sambuc 				opnd = OPERAND(scan);
872f7cf2976SLionel Sambuc 				/* Inline the first character, for speed. */
873f7cf2976SLionel Sambuc 				if (*opnd != *reginput)
874f7cf2976SLionel Sambuc 					return(0);
875f7cf2976SLionel Sambuc 				len = strlen(opnd);
876f7cf2976SLionel Sambuc 				if (len > 1 && strncmp(opnd, reginput, len) != 0)
877f7cf2976SLionel Sambuc 					return(0);
878f7cf2976SLionel Sambuc 				reginput += len;
879f7cf2976SLionel Sambuc 			}
880f7cf2976SLionel Sambuc 			break;
881f7cf2976SLionel Sambuc 		case ANYOF:
882f7cf2976SLionel Sambuc  			if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
883f7cf2976SLionel Sambuc 				return(0);
884f7cf2976SLionel Sambuc 			reginput++;
885f7cf2976SLionel Sambuc 			break;
886f7cf2976SLionel Sambuc 		case ANYBUT:
887f7cf2976SLionel Sambuc  			if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
888f7cf2976SLionel Sambuc 				return(0);
889f7cf2976SLionel Sambuc 			reginput++;
890f7cf2976SLionel Sambuc 			break;
891f7cf2976SLionel Sambuc 		case NOTHING:
892f7cf2976SLionel Sambuc 			break;
893f7cf2976SLionel Sambuc 		case BACK:
894f7cf2976SLionel Sambuc 			break;
895f7cf2976SLionel Sambuc 		case OPEN+1:
896f7cf2976SLionel Sambuc 		case OPEN+2:
897f7cf2976SLionel Sambuc 		case OPEN+3:
898f7cf2976SLionel Sambuc 		case OPEN+4:
899f7cf2976SLionel Sambuc 		case OPEN+5:
900f7cf2976SLionel Sambuc 		case OPEN+6:
901f7cf2976SLionel Sambuc 		case OPEN+7:
902f7cf2976SLionel Sambuc 		case OPEN+8:
903f7cf2976SLionel Sambuc 		case OPEN+9: {
904f7cf2976SLionel Sambuc 				register int no;
905f7cf2976SLionel Sambuc 				register char *save;
906f7cf2976SLionel Sambuc 
907f7cf2976SLionel Sambuc 				no = OP(scan) - OPEN;
908f7cf2976SLionel Sambuc 				save = reginput;
909f7cf2976SLionel Sambuc 
910f7cf2976SLionel Sambuc 				if (regmatch(next)) {
911f7cf2976SLionel Sambuc 					/*
912f7cf2976SLionel Sambuc 					 * Don't set startp if some later
913f7cf2976SLionel Sambuc 					 * invocation of the same parentheses
914f7cf2976SLionel Sambuc 					 * already has.
915f7cf2976SLionel Sambuc 					 */
916f7cf2976SLionel Sambuc 					if (regstartp[no] == NULL)
917f7cf2976SLionel Sambuc 						regstartp[no] = save;
918f7cf2976SLionel Sambuc 					return(1);
919f7cf2976SLionel Sambuc 				} else
920f7cf2976SLionel Sambuc 					return(0);
921f7cf2976SLionel Sambuc 			}
922f7cf2976SLionel Sambuc 			/* NOTREACHED */
923f7cf2976SLionel Sambuc 			break;
924f7cf2976SLionel Sambuc 		case CLOSE+1:
925f7cf2976SLionel Sambuc 		case CLOSE+2:
926f7cf2976SLionel Sambuc 		case CLOSE+3:
927f7cf2976SLionel Sambuc 		case CLOSE+4:
928f7cf2976SLionel Sambuc 		case CLOSE+5:
929f7cf2976SLionel Sambuc 		case CLOSE+6:
930f7cf2976SLionel Sambuc 		case CLOSE+7:
931f7cf2976SLionel Sambuc 		case CLOSE+8:
932f7cf2976SLionel Sambuc 		case CLOSE+9: {
933f7cf2976SLionel Sambuc 				register int no;
934f7cf2976SLionel Sambuc 				register char *save;
935f7cf2976SLionel Sambuc 
936f7cf2976SLionel Sambuc 				no = OP(scan) - CLOSE;
937f7cf2976SLionel Sambuc 				save = reginput;
938f7cf2976SLionel Sambuc 
939f7cf2976SLionel Sambuc 				if (regmatch(next)) {
940f7cf2976SLionel Sambuc 					/*
941f7cf2976SLionel Sambuc 					 * Don't set endp if some later
942f7cf2976SLionel Sambuc 					 * invocation of the same parentheses
943f7cf2976SLionel Sambuc 					 * already has.
944f7cf2976SLionel Sambuc 					 */
945f7cf2976SLionel Sambuc 					if (regendp[no] == NULL)
946f7cf2976SLionel Sambuc 						regendp[no] = save;
947f7cf2976SLionel Sambuc 					return(1);
948f7cf2976SLionel Sambuc 				} else
949f7cf2976SLionel Sambuc 					return(0);
950f7cf2976SLionel Sambuc 			}
951f7cf2976SLionel Sambuc 			/* NOTREACHED */
952f7cf2976SLionel Sambuc 			break;
953f7cf2976SLionel Sambuc 		case BRANCH: {
954f7cf2976SLionel Sambuc 				register char *save;
955f7cf2976SLionel Sambuc 
956f7cf2976SLionel Sambuc 				if (OP(next) != BRANCH)		/* No choice. */
957f7cf2976SLionel Sambuc 					next = OPERAND(scan);	/* Avoid recursion. */
958f7cf2976SLionel Sambuc 				else {
959f7cf2976SLionel Sambuc 					do {
960f7cf2976SLionel Sambuc 						save = reginput;
961f7cf2976SLionel Sambuc 						if (regmatch(OPERAND(scan)))
962f7cf2976SLionel Sambuc 							return(1);
963f7cf2976SLionel Sambuc 						reginput = save;
964f7cf2976SLionel Sambuc 						scan = regnext(scan);
965f7cf2976SLionel Sambuc 					} while (scan != NULL && OP(scan) == BRANCH);
966f7cf2976SLionel Sambuc 					return(0);
967f7cf2976SLionel Sambuc 					/* NOTREACHED */
968f7cf2976SLionel Sambuc 				}
969f7cf2976SLionel Sambuc 			}
970f7cf2976SLionel Sambuc 			/* NOTREACHED */
971f7cf2976SLionel Sambuc 			break;
972f7cf2976SLionel Sambuc 		case STAR:
973f7cf2976SLionel Sambuc 		case PLUS: {
974f7cf2976SLionel Sambuc 				register char nextch;
975f7cf2976SLionel Sambuc 				register int no;
976f7cf2976SLionel Sambuc 				register char *save;
977f7cf2976SLionel Sambuc 				register int min;
978f7cf2976SLionel Sambuc 
979f7cf2976SLionel Sambuc 				/*
980f7cf2976SLionel Sambuc 				 * Lookahead to avoid useless match attempts
981f7cf2976SLionel Sambuc 				 * when we know what character comes next.
982f7cf2976SLionel Sambuc 				 */
983f7cf2976SLionel Sambuc 				nextch = '\0';
984f7cf2976SLionel Sambuc 				if (OP(next) == EXACTLY)
985f7cf2976SLionel Sambuc 					nextch = *OPERAND(next);
986f7cf2976SLionel Sambuc 				min = (OP(scan) == STAR) ? 0 : 1;
987f7cf2976SLionel Sambuc 				save = reginput;
988f7cf2976SLionel Sambuc 				no = regrepeat(OPERAND(scan));
989f7cf2976SLionel Sambuc 				while (no >= min) {
990f7cf2976SLionel Sambuc 					/* If it could work, try it. */
991f7cf2976SLionel Sambuc 					if (nextch == '\0' || *reginput == nextch)
992f7cf2976SLionel Sambuc 						if (regmatch(next))
993f7cf2976SLionel Sambuc 							return(1);
994f7cf2976SLionel Sambuc 					/* Couldn't or didn't -- back up. */
995f7cf2976SLionel Sambuc 					no--;
996f7cf2976SLionel Sambuc 					reginput = save + no;
997f7cf2976SLionel Sambuc 				}
998f7cf2976SLionel Sambuc 				return(0);
999f7cf2976SLionel Sambuc 			}
1000f7cf2976SLionel Sambuc 			/* NOTREACHED */
1001f7cf2976SLionel Sambuc 			break;
1002f7cf2976SLionel Sambuc 		case END:
1003f7cf2976SLionel Sambuc 			return(1);	/* Success! */
1004f7cf2976SLionel Sambuc 			/* NOTREACHED */
1005f7cf2976SLionel Sambuc 			break;
1006f7cf2976SLionel Sambuc 		default:
1007f7cf2976SLionel Sambuc 			regerror("memory corruption");
1008f7cf2976SLionel Sambuc 			return(0);
1009f7cf2976SLionel Sambuc 			/* NOTREACHED */
1010f7cf2976SLionel Sambuc 			break;
1011f7cf2976SLionel Sambuc 		}
1012f7cf2976SLionel Sambuc 
1013f7cf2976SLionel Sambuc 		scan = next;
1014f7cf2976SLionel Sambuc 	}
1015f7cf2976SLionel Sambuc 
1016f7cf2976SLionel Sambuc 	/*
1017f7cf2976SLionel Sambuc 	 * We get here only if there's trouble -- normally "case END" is
1018f7cf2976SLionel Sambuc 	 * the terminating point.
1019f7cf2976SLionel Sambuc 	 */
1020f7cf2976SLionel Sambuc 	regerror("corrupted pointers");
1021f7cf2976SLionel Sambuc 	return(0);
1022f7cf2976SLionel Sambuc }
1023f7cf2976SLionel Sambuc 
1024f7cf2976SLionel Sambuc /*
1025f7cf2976SLionel Sambuc  - regrepeat - repeatedly match something simple, report how many
1026f7cf2976SLionel Sambuc  */
1027f7cf2976SLionel Sambuc static int
regrepeat(p)1028f7cf2976SLionel Sambuc regrepeat(p)
1029f7cf2976SLionel Sambuc char *p;
1030f7cf2976SLionel Sambuc {
1031f7cf2976SLionel Sambuc 	register int count = 0;
1032f7cf2976SLionel Sambuc 	register char *scan;
1033f7cf2976SLionel Sambuc 	register char *opnd;
1034f7cf2976SLionel Sambuc 
1035f7cf2976SLionel Sambuc 	scan = reginput;
1036f7cf2976SLionel Sambuc 	opnd = OPERAND(p);
1037f7cf2976SLionel Sambuc 	switch (OP(p)) {
1038f7cf2976SLionel Sambuc 	case ANY:
1039f7cf2976SLionel Sambuc 		count = strlen(scan);
1040f7cf2976SLionel Sambuc 		scan += count;
1041f7cf2976SLionel Sambuc 		break;
1042f7cf2976SLionel Sambuc 	case EXACTLY:
1043f7cf2976SLionel Sambuc 		while (*opnd == *scan) {
1044f7cf2976SLionel Sambuc 			count++;
1045f7cf2976SLionel Sambuc 			scan++;
1046f7cf2976SLionel Sambuc 		}
1047f7cf2976SLionel Sambuc 		break;
1048f7cf2976SLionel Sambuc 	case ANYOF:
1049f7cf2976SLionel Sambuc 		while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1050f7cf2976SLionel Sambuc 			count++;
1051f7cf2976SLionel Sambuc 			scan++;
1052f7cf2976SLionel Sambuc 		}
1053f7cf2976SLionel Sambuc 		break;
1054f7cf2976SLionel Sambuc 	case ANYBUT:
1055f7cf2976SLionel Sambuc 		while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1056f7cf2976SLionel Sambuc 			count++;
1057f7cf2976SLionel Sambuc 			scan++;
1058f7cf2976SLionel Sambuc 		}
1059f7cf2976SLionel Sambuc 		break;
1060f7cf2976SLionel Sambuc 	default:		/* Oh dear.  Called inappropriately. */
1061f7cf2976SLionel Sambuc 		regerror("internal foulup");
1062f7cf2976SLionel Sambuc 		count = 0;	/* Best compromise. */
1063f7cf2976SLionel Sambuc 		break;
1064f7cf2976SLionel Sambuc 	}
1065f7cf2976SLionel Sambuc 	reginput = scan;
1066f7cf2976SLionel Sambuc 
1067f7cf2976SLionel Sambuc 	return(count);
1068f7cf2976SLionel Sambuc }
1069f7cf2976SLionel Sambuc 
1070f7cf2976SLionel Sambuc /*
1071f7cf2976SLionel Sambuc  - regnext - dig the "next" pointer out of a node
1072f7cf2976SLionel Sambuc  */
1073f7cf2976SLionel Sambuc static char *
regnext(p)1074f7cf2976SLionel Sambuc regnext(p)
1075f7cf2976SLionel Sambuc register char *p;
1076f7cf2976SLionel Sambuc {
1077f7cf2976SLionel Sambuc 	register int offset;
1078f7cf2976SLionel Sambuc 
1079f7cf2976SLionel Sambuc 	if (p == &regdummy)
1080f7cf2976SLionel Sambuc 		return(NULL);
1081f7cf2976SLionel Sambuc 
1082f7cf2976SLionel Sambuc 	offset = NEXT(p);
1083f7cf2976SLionel Sambuc 	if (offset == 0)
1084f7cf2976SLionel Sambuc 		return(NULL);
1085f7cf2976SLionel Sambuc 
1086f7cf2976SLionel Sambuc 	if (OP(p) == BACK)
1087f7cf2976SLionel Sambuc 		return(p-offset);
1088f7cf2976SLionel Sambuc 	else
1089f7cf2976SLionel Sambuc 		return(p+offset);
1090f7cf2976SLionel Sambuc }
1091f7cf2976SLionel Sambuc 
1092f7cf2976SLionel Sambuc #ifdef DEBUG
1093f7cf2976SLionel Sambuc 
1094f7cf2976SLionel Sambuc STATIC char *regprop();
1095f7cf2976SLionel Sambuc 
1096f7cf2976SLionel Sambuc /*
1097f7cf2976SLionel Sambuc  - regdump - dump a regexp onto stdout in vaguely comprehensible form
1098f7cf2976SLionel Sambuc  */
1099f7cf2976SLionel Sambuc void
regdump(r)1100f7cf2976SLionel Sambuc regdump(r)
1101f7cf2976SLionel Sambuc regexp *r;
1102f7cf2976SLionel Sambuc {
1103f7cf2976SLionel Sambuc 	register char *s;
1104f7cf2976SLionel Sambuc 	register char op = EXACTLY;	/* Arbitrary non-END op. */
1105f7cf2976SLionel Sambuc 	register char *next;
1106f7cf2976SLionel Sambuc 
1107f7cf2976SLionel Sambuc 
1108f7cf2976SLionel Sambuc 	s = r->program + 1;
1109f7cf2976SLionel Sambuc 	while (op != END) {	/* While that wasn't END last time... */
1110f7cf2976SLionel Sambuc 		op = OP(s);
1111f7cf2976SLionel Sambuc 		printf("%2d%s", s-r->program, regprop(s));	/* Where, what. */
1112f7cf2976SLionel Sambuc 		next = regnext(s);
1113f7cf2976SLionel Sambuc 		if (next == NULL)		/* Next ptr. */
1114f7cf2976SLionel Sambuc 			printf("(0)");
1115f7cf2976SLionel Sambuc 		else
1116f7cf2976SLionel Sambuc 			printf("(%d)", (s-r->program)+(next-s));
1117f7cf2976SLionel Sambuc 		s += 3;
1118f7cf2976SLionel Sambuc 		if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1119f7cf2976SLionel Sambuc 			/* Literal string, where present. */
1120f7cf2976SLionel Sambuc 			while (*s != '\0') {
1121f7cf2976SLionel Sambuc 				putchar(*s);
1122f7cf2976SLionel Sambuc 				s++;
1123f7cf2976SLionel Sambuc 			}
1124f7cf2976SLionel Sambuc 			s++;
1125f7cf2976SLionel Sambuc 		}
1126f7cf2976SLionel Sambuc 		putchar('\n');
1127f7cf2976SLionel Sambuc 	}
1128f7cf2976SLionel Sambuc 
1129f7cf2976SLionel Sambuc 	/* Header fields of interest. */
1130f7cf2976SLionel Sambuc 	if (r->regstart != '\0')
1131f7cf2976SLionel Sambuc 		printf("start `%c' ", r->regstart);
1132f7cf2976SLionel Sambuc 	if (r->reganch)
1133f7cf2976SLionel Sambuc 		printf("anchored ");
1134f7cf2976SLionel Sambuc 	if (r->regmust != NULL)
1135f7cf2976SLionel Sambuc 		printf("must have \"%s\"", r->regmust);
1136f7cf2976SLionel Sambuc 	printf("\n");
1137f7cf2976SLionel Sambuc }
1138f7cf2976SLionel Sambuc 
1139f7cf2976SLionel Sambuc /*
1140f7cf2976SLionel Sambuc  - regprop - printable representation of opcode
1141f7cf2976SLionel Sambuc  */
1142f7cf2976SLionel Sambuc static char *
regprop(op)1143f7cf2976SLionel Sambuc regprop(op)
1144f7cf2976SLionel Sambuc char *op;
1145f7cf2976SLionel Sambuc {
1146f7cf2976SLionel Sambuc 	register char *p;
1147f7cf2976SLionel Sambuc 	static char buf[50];
1148f7cf2976SLionel Sambuc 
1149f7cf2976SLionel Sambuc 	(void) strcpy(buf, ":");
1150f7cf2976SLionel Sambuc 
1151f7cf2976SLionel Sambuc 	switch (OP(op)) {
1152f7cf2976SLionel Sambuc 	case BOL:
1153f7cf2976SLionel Sambuc 		p = "BOL";
1154f7cf2976SLionel Sambuc 		break;
1155f7cf2976SLionel Sambuc 	case EOL:
1156f7cf2976SLionel Sambuc 		p = "EOL";
1157f7cf2976SLionel Sambuc 		break;
1158f7cf2976SLionel Sambuc 	case ANY:
1159f7cf2976SLionel Sambuc 		p = "ANY";
1160f7cf2976SLionel Sambuc 		break;
1161f7cf2976SLionel Sambuc 	case ANYOF:
1162f7cf2976SLionel Sambuc 		p = "ANYOF";
1163f7cf2976SLionel Sambuc 		break;
1164f7cf2976SLionel Sambuc 	case ANYBUT:
1165f7cf2976SLionel Sambuc 		p = "ANYBUT";
1166f7cf2976SLionel Sambuc 		break;
1167f7cf2976SLionel Sambuc 	case BRANCH:
1168f7cf2976SLionel Sambuc 		p = "BRANCH";
1169f7cf2976SLionel Sambuc 		break;
1170f7cf2976SLionel Sambuc 	case EXACTLY:
1171f7cf2976SLionel Sambuc 		p = "EXACTLY";
1172f7cf2976SLionel Sambuc 		break;
1173f7cf2976SLionel Sambuc 	case NOTHING:
1174f7cf2976SLionel Sambuc 		p = "NOTHING";
1175f7cf2976SLionel Sambuc 		break;
1176f7cf2976SLionel Sambuc 	case BACK:
1177f7cf2976SLionel Sambuc 		p = "BACK";
1178f7cf2976SLionel Sambuc 		break;
1179f7cf2976SLionel Sambuc 	case END:
1180f7cf2976SLionel Sambuc 		p = "END";
1181f7cf2976SLionel Sambuc 		break;
1182f7cf2976SLionel Sambuc 	case OPEN+1:
1183f7cf2976SLionel Sambuc 	case OPEN+2:
1184f7cf2976SLionel Sambuc 	case OPEN+3:
1185f7cf2976SLionel Sambuc 	case OPEN+4:
1186f7cf2976SLionel Sambuc 	case OPEN+5:
1187f7cf2976SLionel Sambuc 	case OPEN+6:
1188f7cf2976SLionel Sambuc 	case OPEN+7:
1189f7cf2976SLionel Sambuc 	case OPEN+8:
1190f7cf2976SLionel Sambuc 	case OPEN+9:
1191f7cf2976SLionel Sambuc 		sprintf(buf+strlen(buf), "OPEN%d", OP(op)-OPEN);
1192f7cf2976SLionel Sambuc 		p = NULL;
1193f7cf2976SLionel Sambuc 		break;
1194f7cf2976SLionel Sambuc 	case CLOSE+1:
1195f7cf2976SLionel Sambuc 	case CLOSE+2:
1196f7cf2976SLionel Sambuc 	case CLOSE+3:
1197f7cf2976SLionel Sambuc 	case CLOSE+4:
1198f7cf2976SLionel Sambuc 	case CLOSE+5:
1199f7cf2976SLionel Sambuc 	case CLOSE+6:
1200f7cf2976SLionel Sambuc 	case CLOSE+7:
1201f7cf2976SLionel Sambuc 	case CLOSE+8:
1202f7cf2976SLionel Sambuc 	case CLOSE+9:
1203f7cf2976SLionel Sambuc 		sprintf(buf+strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1204f7cf2976SLionel Sambuc 		p = NULL;
1205f7cf2976SLionel Sambuc 		break;
1206f7cf2976SLionel Sambuc 	case STAR:
1207f7cf2976SLionel Sambuc 		p = "STAR";
1208f7cf2976SLionel Sambuc 		break;
1209f7cf2976SLionel Sambuc 	case PLUS:
1210f7cf2976SLionel Sambuc 		p = "PLUS";
1211f7cf2976SLionel Sambuc 		break;
1212f7cf2976SLionel Sambuc 	default:
1213f7cf2976SLionel Sambuc 		regerror("corrupted opcode");
1214f7cf2976SLionel Sambuc 		break;
1215f7cf2976SLionel Sambuc 	}
1216f7cf2976SLionel Sambuc 	if (p != NULL)
1217f7cf2976SLionel Sambuc 		(void) strcat(buf, p);
1218f7cf2976SLionel Sambuc 	return(buf);
1219f7cf2976SLionel Sambuc }
1220f7cf2976SLionel Sambuc #endif
1221f7cf2976SLionel Sambuc 
1222f7cf2976SLionel Sambuc /*
1223f7cf2976SLionel Sambuc  * The following is provided for those people who do not have strcspn() in
1224f7cf2976SLionel Sambuc  * their C libraries.  They should get off their butts and do something
1225f7cf2976SLionel Sambuc  * about it; at least one public-domain implementation of those (highly
1226f7cf2976SLionel Sambuc  * useful) string routines has been published on Usenet.
1227f7cf2976SLionel Sambuc  */
1228f7cf2976SLionel Sambuc #ifdef STRCSPN
1229f7cf2976SLionel Sambuc /*
1230f7cf2976SLionel Sambuc  * strcspn - find length of initial segment of s1 consisting entirely
1231f7cf2976SLionel Sambuc  * of characters not from s2
1232f7cf2976SLionel Sambuc  */
1233f7cf2976SLionel Sambuc 
1234f7cf2976SLionel Sambuc static int
strcspn(s1,s2)1235f7cf2976SLionel Sambuc strcspn(s1, s2)
1236f7cf2976SLionel Sambuc char *s1;
1237f7cf2976SLionel Sambuc char *s2;
1238f7cf2976SLionel Sambuc {
1239f7cf2976SLionel Sambuc 	register char *scan1;
1240f7cf2976SLionel Sambuc 	register char *scan2;
1241f7cf2976SLionel Sambuc 	register int count;
1242f7cf2976SLionel Sambuc 
1243f7cf2976SLionel Sambuc 	count = 0;
1244f7cf2976SLionel Sambuc 	for (scan1 = s1; *scan1 != '\0'; scan1++) {
1245f7cf2976SLionel Sambuc 		for (scan2 = s2; *scan2 != '\0';)	/* ++ moved down. */
1246f7cf2976SLionel Sambuc 			if (*scan1 == *scan2++)
1247f7cf2976SLionel Sambuc 				return(count);
1248f7cf2976SLionel Sambuc 		count++;
1249f7cf2976SLionel Sambuc 	}
1250f7cf2976SLionel Sambuc 	return(count);
1251f7cf2976SLionel Sambuc }
1252f7cf2976SLionel Sambuc #endif
1253