xref: /onnv-gate/usr/src/cmd/vgrind/regexp.c (revision 283:f7518827bcbb)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
30Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
40Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
50Sstevel@tonic-gate  */
60Sstevel@tonic-gate 
70Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
80Sstevel@tonic-gate 
90Sstevel@tonic-gate #include <ctype.h>
100Sstevel@tonic-gate 
110Sstevel@tonic-gate typedef int	boolean;
120Sstevel@tonic-gate #define TRUE	1
130Sstevel@tonic-gate #define FALSE	0
140Sstevel@tonic-gate #define NIL	0
150Sstevel@tonic-gate 
160Sstevel@tonic-gate extern boolean	l_onecase;	/* true if upper and lower equivalent */
170Sstevel@tonic-gate extern char	*l_idchars;	/* set of characters legal in identifiers
180Sstevel@tonic-gate 				   in addition to letters and digits */
190Sstevel@tonic-gate 
200Sstevel@tonic-gate extern char	*strchr();
21*283Scraigm static void	expconv(void);
220Sstevel@tonic-gate 
230Sstevel@tonic-gate #define isidchr(c)	\
240Sstevel@tonic-gate 		(isalnum(c) || ((c) != NIL && strchr(l_idchars, (c)) != NIL))
250Sstevel@tonic-gate #define makelower(c)	(isupper((c)) ? tolower((c)) : (c))
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*  STRNCMP -	like strncmp except that we convert the
280Sstevel@tonic-gate  *	 	first string to lower case before comparing
290Sstevel@tonic-gate  *		if l_onecase is set.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
32*283Scraigm int
STRNCMP(char * s1,char * s2,int len)33*283Scraigm STRNCMP(char *s1, char *s2, int len)
340Sstevel@tonic-gate {
350Sstevel@tonic-gate 	if (l_onecase) {
360Sstevel@tonic-gate 	    do
370Sstevel@tonic-gate 		if (*s2 - makelower(*s1))
380Sstevel@tonic-gate 			return (*s2 - makelower(*s1));
390Sstevel@tonic-gate 		else {
400Sstevel@tonic-gate 			s2++;
410Sstevel@tonic-gate 			s1++;
420Sstevel@tonic-gate 		}
430Sstevel@tonic-gate 	    while (--len);
440Sstevel@tonic-gate 	} else {
450Sstevel@tonic-gate 	    do
460Sstevel@tonic-gate 		if (*s2 - *s1)
470Sstevel@tonic-gate 			return (*s2 - *s1);
480Sstevel@tonic-gate 		else {
490Sstevel@tonic-gate 			s2++;
500Sstevel@tonic-gate 			s1++;
510Sstevel@tonic-gate 		}
520Sstevel@tonic-gate 	    while (--len);
530Sstevel@tonic-gate 	}
540Sstevel@tonic-gate 	return(0);
550Sstevel@tonic-gate }
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*	The following routine converts an irregular expression to
580Sstevel@tonic-gate  *	internal format.
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  *	Either meta symbols (\a \d or \p) or character strings or
610Sstevel@tonic-gate  *	operations ( alternation or parenthesizing ) can be
620Sstevel@tonic-gate  *	specified.  Each starts with a descriptor byte.  The descriptor
630Sstevel@tonic-gate  *	byte has STR set for strings, META set for meta symbols
640Sstevel@tonic-gate  *	and OPER set for operations.
650Sstevel@tonic-gate  *	The descriptor byte can also have the OPT bit set if the object
660Sstevel@tonic-gate  *	defined is optional.  Also ALT can be set to indicate an alternation.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  *	For metasymbols the byte following the descriptor byte identities
690Sstevel@tonic-gate  *	the meta symbol (containing an ascii 'a', 'd', 'p', '|', or '(').  For
700Sstevel@tonic-gate  *	strings the byte after the descriptor is a character count for
710Sstevel@tonic-gate  *	the string:
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  *		meta symbols := descriptor
740Sstevel@tonic-gate  *				symbol
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  *		strings :=	descriptor
770Sstevel@tonic-gate  *				character count
780Sstevel@tonic-gate  *				the string
790Sstevel@tonic-gate  *
800Sstevel@tonic-gate  *		operations :=	descriptor
810Sstevel@tonic-gate  *				symbol
820Sstevel@tonic-gate  *				character count
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  *  handy macros for accessing parts of match blocks
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate #define MSYM(A) (*(A+1))	/* symbol in a meta symbol block */
890Sstevel@tonic-gate #define MNEXT(A) (A+2)		/* character following a metasymbol block */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #define OSYM(A) (*(A+1))	/* symbol in an operation block */
920Sstevel@tonic-gate #define OCNT(A) (*(A+2))	/* character count */
930Sstevel@tonic-gate #define ONEXT(A) (A+3)		/* next character after the operation */
940Sstevel@tonic-gate #define OPTR(A) (A+*(A+2))	/* place pointed to by the operator */
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #define SCNT(A) (*(A+1))	/* byte count of a string */
970Sstevel@tonic-gate #define SSTR(A) (A+2)		/* address of the string */
980Sstevel@tonic-gate #define SNEXT(A) (A+2+*(A+1))	/* character following the string */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  *  bit flags in the descriptor
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate #define OPT 1
1040Sstevel@tonic-gate #define STR 2
1050Sstevel@tonic-gate #define META 4
1060Sstevel@tonic-gate #define ALT 8
1070Sstevel@tonic-gate #define OPER 16
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate char *ure;		/* pointer current position in unconverted exp */
1100Sstevel@tonic-gate char *ccre;		/* pointer to current position in converted exp*/
1110Sstevel@tonic-gate char *malloc();
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate char *
convexp(char * re)114*283Scraigm convexp(char *re)
115*283Scraigm 	/* re - unconverted irregular expression */
1160Sstevel@tonic-gate {
117*283Scraigm     char *cre;		/* pointer to converted regular expression */
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate     /* allocate room for the converted expression */
1200Sstevel@tonic-gate     if (re == NIL)
1210Sstevel@tonic-gate 	return (NIL);
1220Sstevel@tonic-gate     if (*re == '\0')
1230Sstevel@tonic-gate 	return (NIL);
1240Sstevel@tonic-gate     cre = malloc (4 * strlen(re) + 3);
1250Sstevel@tonic-gate     ccre = cre;
1260Sstevel@tonic-gate     ure = re;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate     /* start the conversion with a \a */
1290Sstevel@tonic-gate     *cre = META | OPT;
1300Sstevel@tonic-gate     MSYM(cre) = 'a';
1310Sstevel@tonic-gate     ccre = MNEXT(cre);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate     /* start the conversion (its recursive) */
1340Sstevel@tonic-gate     expconv ();
1350Sstevel@tonic-gate     *ccre = 0;
1360Sstevel@tonic-gate     return (cre);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
139*283Scraigm static void
expconv(void)140*283Scraigm expconv(void)
1410Sstevel@tonic-gate {
142*283Scraigm     char *cs;		/* pointer to current symbol in converted exp */
143*283Scraigm     char c;		/* character being processed */
144*283Scraigm     char *acs;		/* pinter to last alternate */
145*283Scraigm     int temp;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate     /* let the conversion begin */
1480Sstevel@tonic-gate     acs = NIL;
1490Sstevel@tonic-gate     cs = NIL;
1500Sstevel@tonic-gate     while (*ure != NIL) {
1510Sstevel@tonic-gate 	switch (c = *ure++) {
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	case '\\':
1540Sstevel@tonic-gate 	    switch (c = *ure++) {
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	    /* escaped characters are just characters */
1570Sstevel@tonic-gate 	    default:
1580Sstevel@tonic-gate 		if (cs == NIL || (*cs & STR) == 0) {
1590Sstevel@tonic-gate 		    cs = ccre;
1600Sstevel@tonic-gate 		    *cs = STR;
1610Sstevel@tonic-gate 		    SCNT(cs) = 1;
1620Sstevel@tonic-gate 		    ccre += 2;
1630Sstevel@tonic-gate 		} else
1640Sstevel@tonic-gate 		    SCNT(cs)++;
1650Sstevel@tonic-gate 		*ccre++ = c;
1660Sstevel@tonic-gate 		break;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	    /* normal(?) metacharacters */
1690Sstevel@tonic-gate 	    case 'a':
1700Sstevel@tonic-gate 	    case 'd':
1710Sstevel@tonic-gate 	    case 'e':
1720Sstevel@tonic-gate 	    case 'p':
1730Sstevel@tonic-gate 		if (acs != NIL && acs != cs) {
1740Sstevel@tonic-gate 		    do {
1750Sstevel@tonic-gate 			temp = OCNT(acs);
1760Sstevel@tonic-gate 			OCNT(acs) = ccre - acs;
1770Sstevel@tonic-gate 			acs -= temp;
1780Sstevel@tonic-gate 		    } while (temp != 0);
1790Sstevel@tonic-gate 		    acs = NIL;
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 		cs = ccre;
1820Sstevel@tonic-gate 		*cs = META;
1830Sstevel@tonic-gate 		MSYM(cs) = c;
1840Sstevel@tonic-gate 		ccre = MNEXT(cs);
1850Sstevel@tonic-gate 		break;
1860Sstevel@tonic-gate 	    }
1870Sstevel@tonic-gate 	    break;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/* just put the symbol in */
1900Sstevel@tonic-gate 	case '^':
1910Sstevel@tonic-gate 	case '$':
1920Sstevel@tonic-gate 	    if (acs != NIL && acs != cs) {
1930Sstevel@tonic-gate 		do {
1940Sstevel@tonic-gate 		    temp = OCNT(acs);
1950Sstevel@tonic-gate 		    OCNT(acs) = ccre - acs;
1960Sstevel@tonic-gate 		    acs -= temp;
1970Sstevel@tonic-gate 		} while (temp != 0);
1980Sstevel@tonic-gate 		acs = NIL;
1990Sstevel@tonic-gate 	    }
2000Sstevel@tonic-gate 	    cs = ccre;
2010Sstevel@tonic-gate 	    *cs = META;
2020Sstevel@tonic-gate 	    MSYM(cs) = c;
2030Sstevel@tonic-gate 	    ccre = MNEXT(cs);
2040Sstevel@tonic-gate 	    break;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	/* mark the last match sequence as optional */
2070Sstevel@tonic-gate 	case '?':
2080Sstevel@tonic-gate 	    if (cs)
2090Sstevel@tonic-gate 	    	*cs = *cs | OPT;
2100Sstevel@tonic-gate 	    break;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/* recurse and define a subexpression */
2130Sstevel@tonic-gate 	case '(':
2140Sstevel@tonic-gate 	    if (acs != NIL && acs != cs) {
2150Sstevel@tonic-gate 		do {
2160Sstevel@tonic-gate 		    temp = OCNT(acs);
2170Sstevel@tonic-gate 		    OCNT(acs) = ccre - acs;
2180Sstevel@tonic-gate 		    acs -= temp;
2190Sstevel@tonic-gate 		} while (temp != 0);
2200Sstevel@tonic-gate 		acs = NIL;
2210Sstevel@tonic-gate 	    }
2220Sstevel@tonic-gate 	    cs = ccre;
2230Sstevel@tonic-gate 	    *cs = OPER;
2240Sstevel@tonic-gate 	    OSYM(cs) = '(';
2250Sstevel@tonic-gate 	    ccre = ONEXT(cs);
2260Sstevel@tonic-gate 	    expconv ();
2270Sstevel@tonic-gate 	    OCNT(cs) = ccre - cs;		/* offset to next symbol */
2280Sstevel@tonic-gate 	    break;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	/* return from a recursion */
2310Sstevel@tonic-gate 	case ')':
2320Sstevel@tonic-gate 	    if (acs != NIL) {
2330Sstevel@tonic-gate 		do {
2340Sstevel@tonic-gate 		    temp = OCNT(acs);
2350Sstevel@tonic-gate 		    OCNT(acs) = ccre - acs;
2360Sstevel@tonic-gate 		    acs -= temp;
2370Sstevel@tonic-gate 		} while (temp != 0);
2380Sstevel@tonic-gate 		acs = NIL;
2390Sstevel@tonic-gate 	    }
2400Sstevel@tonic-gate 	    cs = ccre;
2410Sstevel@tonic-gate 	    *cs = META;
2420Sstevel@tonic-gate 	    MSYM(cs) = c;
2430Sstevel@tonic-gate 	    ccre = MNEXT(cs);
2440Sstevel@tonic-gate 	    return;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	/* mark the last match sequence as having an alternate */
2470Sstevel@tonic-gate 	/* the third byte will contain an offset to jump over the */
2480Sstevel@tonic-gate 	/* alternate match in case the first did not fail */
2490Sstevel@tonic-gate 	case '|':
2500Sstevel@tonic-gate 	    if (acs != NIL && acs != cs)
2510Sstevel@tonic-gate 		OCNT(ccre) = ccre - acs;	/* make a back pointer */
2520Sstevel@tonic-gate 	    else
2530Sstevel@tonic-gate 		OCNT(ccre) = 0;
2540Sstevel@tonic-gate 	    *cs |= ALT;
2550Sstevel@tonic-gate 	    cs = ccre;
2560Sstevel@tonic-gate 	    *cs = OPER;
2570Sstevel@tonic-gate 	    OSYM(cs) = '|';
2580Sstevel@tonic-gate 	    ccre = ONEXT(cs);
2590Sstevel@tonic-gate 	    acs = cs;	/* remember that the pointer is to be filles */
2600Sstevel@tonic-gate 	    break;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/* if its not a metasymbol just build a scharacter string */
2630Sstevel@tonic-gate 	default:
2640Sstevel@tonic-gate 	    if (cs == NIL || (*cs & STR) == 0) {
2650Sstevel@tonic-gate 		cs = ccre;
2660Sstevel@tonic-gate 		*cs = STR;
2670Sstevel@tonic-gate 		SCNT(cs) = 1;
2680Sstevel@tonic-gate 		ccre = SSTR(cs);
2690Sstevel@tonic-gate 	    } else
2700Sstevel@tonic-gate 		SCNT(cs)++;
2710Sstevel@tonic-gate 	    *ccre++ = c;
2720Sstevel@tonic-gate 	    break;
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate     }
2750Sstevel@tonic-gate     if (acs != NIL) {
2760Sstevel@tonic-gate 	do {
2770Sstevel@tonic-gate 	    temp = OCNT(acs);
2780Sstevel@tonic-gate 	    OCNT(acs) = ccre - acs;
2790Sstevel@tonic-gate 	    acs -= temp;
2800Sstevel@tonic-gate 	} while (temp != 0);
2810Sstevel@tonic-gate 	acs = NIL;
2820Sstevel@tonic-gate     }
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate /* end of convertre */
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate  *	The following routine recognises an irregular expresion
2890Sstevel@tonic-gate  *	with the following special characters:
2900Sstevel@tonic-gate  *
2910Sstevel@tonic-gate  *		\?	-	means last match was optional
2920Sstevel@tonic-gate  *		\a	-	matches any number of characters
2930Sstevel@tonic-gate  *		\d	-	matches any number of spaces and tabs
2940Sstevel@tonic-gate  *		\p	-	matches any number of alphanumeric
2950Sstevel@tonic-gate  *				characters. The
2960Sstevel@tonic-gate  *				characters matched will be copied into
2970Sstevel@tonic-gate  *				the area pointed to by 'name'.
2980Sstevel@tonic-gate  *		\|	-	alternation
2990Sstevel@tonic-gate  *		\( \)	-	grouping used mostly for alternation and
3000Sstevel@tonic-gate  *				optionality
3010Sstevel@tonic-gate  *
3020Sstevel@tonic-gate  *	The irregular expression must be translated to internal form
3030Sstevel@tonic-gate  *	prior to calling this routine
3040Sstevel@tonic-gate  *
3050Sstevel@tonic-gate  *	The value returned is the pointer to the first non \a
3060Sstevel@tonic-gate  *	character matched.
3070Sstevel@tonic-gate  */
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate boolean _escaped;		/* true if we are currently _escaped */
3100Sstevel@tonic-gate char *Start;			/* start of string */
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate char *
expmatch(char * s,char * re,char * mstring)313*283Scraigm expmatch(char *s, char *re, char *mstring)
314*283Scraigm 	/* s - string to check for a match in */
315*283Scraigm 	/* re - a converted irregular expression */
316*283Scraigm 	/* mstring - where to put whatever matches a \p */
3170Sstevel@tonic-gate {
318*283Scraigm     char *cs;		/* the current symbol */
319*283Scraigm     char *ptr, *s1;	/* temporary pointer */
320*283Scraigm     boolean matched;	/* a temporary boolean */
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate     /* initial conditions */
3230Sstevel@tonic-gate     if (re == NIL)
3240Sstevel@tonic-gate 	return (NIL);
3250Sstevel@tonic-gate     cs = re;
3260Sstevel@tonic-gate     matched = FALSE;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate     /* loop till expression string is exhausted (or at least pretty tired) */
3290Sstevel@tonic-gate     while (*cs) {
3300Sstevel@tonic-gate 	switch (*cs & (OPER | STR | META)) {
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/* try to match a string */
3330Sstevel@tonic-gate 	case STR:
3340Sstevel@tonic-gate 	    matched = !STRNCMP (s, SSTR(cs), SCNT(cs));
3350Sstevel@tonic-gate 	    if (matched) {
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 		/* hoorah it matches */
3380Sstevel@tonic-gate 		s += SCNT(cs);
3390Sstevel@tonic-gate 		cs = SNEXT(cs);
3400Sstevel@tonic-gate 	    } else if (*cs & ALT) {
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 		/* alternation, skip to next expression */
3430Sstevel@tonic-gate 		cs = SNEXT(cs);
3440Sstevel@tonic-gate 	    } else if (*cs & OPT) {
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		/* the match is optional */
3470Sstevel@tonic-gate 		cs = SNEXT(cs);
3480Sstevel@tonic-gate 		matched = 1;		/* indicate a successful match */
3490Sstevel@tonic-gate 	    } else {
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		/* no match, error return */
3520Sstevel@tonic-gate 		return (NIL);
3530Sstevel@tonic-gate 	    }
3540Sstevel@tonic-gate 	    break;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	/* an operator, do something fancy */
3570Sstevel@tonic-gate 	case OPER:
3580Sstevel@tonic-gate 	    switch (OSYM(cs)) {
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	    /* this is an alternation */
3610Sstevel@tonic-gate 	    case '|':
3620Sstevel@tonic-gate 		if (matched)
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 		    /* last thing in the alternation was a match, skip ahead */
3650Sstevel@tonic-gate 		    cs = OPTR(cs);
3660Sstevel@tonic-gate 		else
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 		    /* no match, keep trying */
3690Sstevel@tonic-gate 		    cs = ONEXT(cs);
3700Sstevel@tonic-gate 		break;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	    /* this is a grouping, recurse */
3730Sstevel@tonic-gate 	    case '(':
3740Sstevel@tonic-gate 		ptr = expmatch (s, ONEXT(cs), mstring);
3750Sstevel@tonic-gate 		if (ptr != NIL) {
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		    /* the subexpression matched */
3780Sstevel@tonic-gate 		    matched = 1;
3790Sstevel@tonic-gate 		    s = ptr;
3800Sstevel@tonic-gate 		} else if (*cs & ALT) {
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 		    /* alternation, skip to next expression */
3830Sstevel@tonic-gate 		    matched = 0;
3840Sstevel@tonic-gate 		} else if (*cs & OPT) {
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 		    /* the match is optional */
3870Sstevel@tonic-gate 		    matched = 1;	/* indicate a successful match */
3880Sstevel@tonic-gate 		} else {
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 		    /* no match, error return */
3910Sstevel@tonic-gate 		    return (NIL);
3920Sstevel@tonic-gate 		}
3930Sstevel@tonic-gate 		cs = OPTR(cs);
3940Sstevel@tonic-gate 		break;
3950Sstevel@tonic-gate 	    }
3960Sstevel@tonic-gate 	    break;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	/* try to match a metasymbol */
3990Sstevel@tonic-gate 	case META:
4000Sstevel@tonic-gate 	    switch (MSYM(cs)) {
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	    /* try to match anything and remember what was matched */
4030Sstevel@tonic-gate 	    case 'p':
4040Sstevel@tonic-gate 		/*
4050Sstevel@tonic-gate 		 *  This is really the same as trying the match the
4060Sstevel@tonic-gate 		 *  remaining parts of the expression to any subset
4070Sstevel@tonic-gate 		 *  of the string.
4080Sstevel@tonic-gate 		 */
4090Sstevel@tonic-gate 		s1 = s;
4100Sstevel@tonic-gate 		do {
4110Sstevel@tonic-gate 		    ptr = expmatch (s1, MNEXT(cs), mstring);
4120Sstevel@tonic-gate 		    if (ptr != NIL && s1 != s) {
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 			/* we have a match, remember the match */
4150Sstevel@tonic-gate 			strncpy (mstring, s, s1 - s);
4160Sstevel@tonic-gate 			mstring[s1 - s] = '\0';
4170Sstevel@tonic-gate 			return (ptr);
4180Sstevel@tonic-gate 		    } else if (ptr != NIL && (*cs & OPT)) {
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 			/* it was aoptional so no match is ok */
4210Sstevel@tonic-gate 			return (ptr);
4220Sstevel@tonic-gate 		    } else if (ptr != NIL) {
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 			/* not optional and we still matched */
4250Sstevel@tonic-gate 			return (NIL);
4260Sstevel@tonic-gate 		    }
4270Sstevel@tonic-gate 		    if (!isidchr(*s1))
4280Sstevel@tonic-gate 			return (NIL);
4290Sstevel@tonic-gate 		    if (*s1 == '\\')
4300Sstevel@tonic-gate 			_escaped = _escaped ? FALSE : TRUE;
4310Sstevel@tonic-gate 		    else
4320Sstevel@tonic-gate 			_escaped = FALSE;
4330Sstevel@tonic-gate 		} while (*s1++);
4340Sstevel@tonic-gate 		return (NIL);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	    /* try to match anything */
4370Sstevel@tonic-gate 	    case 'a':
4380Sstevel@tonic-gate 		/*
4390Sstevel@tonic-gate 		 *  This is really the same as trying the match the
4400Sstevel@tonic-gate 		 *  remaining parts of the expression to any subset
4410Sstevel@tonic-gate 		 *  of the string.
4420Sstevel@tonic-gate 		 */
4430Sstevel@tonic-gate 		s1 = s;
4440Sstevel@tonic-gate 		do {
4450Sstevel@tonic-gate 		    ptr = expmatch (s1, MNEXT(cs), mstring);
4460Sstevel@tonic-gate 		    if (ptr != NIL && s1 != s) {
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 			/* we have a match */
4490Sstevel@tonic-gate 			return (ptr);
4500Sstevel@tonic-gate 		    } else if (ptr != NIL && (*cs & OPT)) {
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 			/* it was aoptional so no match is ok */
4530Sstevel@tonic-gate 			return (ptr);
4540Sstevel@tonic-gate 		    } else if (ptr != NIL) {
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 			/* not optional and we still matched */
4570Sstevel@tonic-gate 			return (NIL);
4580Sstevel@tonic-gate 		    }
4590Sstevel@tonic-gate 		    if (*s1 == '\\')
4600Sstevel@tonic-gate 			_escaped = _escaped ? FALSE : TRUE;
4610Sstevel@tonic-gate 		    else
4620Sstevel@tonic-gate 			_escaped = FALSE;
4630Sstevel@tonic-gate 		} while (*s1++);
4640Sstevel@tonic-gate 		return (NIL);
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	    /* fail if we are currently _escaped */
4670Sstevel@tonic-gate 	    case 'e':
4680Sstevel@tonic-gate 		if (_escaped)
4690Sstevel@tonic-gate 		    return(NIL);
4700Sstevel@tonic-gate 		cs = MNEXT(cs);
4710Sstevel@tonic-gate 		break;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	    /* match any number of tabs and spaces */
4740Sstevel@tonic-gate 	    case 'd':
4750Sstevel@tonic-gate 		ptr = s;
4760Sstevel@tonic-gate 		while (*s == ' ' || *s == '\t')
4770Sstevel@tonic-gate 		    s++;
4780Sstevel@tonic-gate 		if (s != ptr || s == Start) {
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		    /* match, be happy */
4810Sstevel@tonic-gate 		    matched = 1;
4820Sstevel@tonic-gate 		    cs = MNEXT(cs);
4830Sstevel@tonic-gate 		} else if (*s == '\n' || *s == '\0') {
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 		    /* match, be happy */
4860Sstevel@tonic-gate 		    matched = 1;
4870Sstevel@tonic-gate 		    cs = MNEXT(cs);
4880Sstevel@tonic-gate 		} else if (*cs & ALT) {
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 		    /* try the next part */
4910Sstevel@tonic-gate 		    matched = 0;
4920Sstevel@tonic-gate 		    cs = MNEXT(cs);
4930Sstevel@tonic-gate 		} else if (*cs & OPT) {
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 		    /* doesn't matter */
4960Sstevel@tonic-gate 		    matched = 1;
4970Sstevel@tonic-gate 		    cs = MNEXT(cs);
4980Sstevel@tonic-gate 		} else
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 		    /* no match, error return */
5010Sstevel@tonic-gate 		    return (NIL);
5020Sstevel@tonic-gate 		break;
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	    /* check for end of line */
5050Sstevel@tonic-gate 	    case '$':
5060Sstevel@tonic-gate 		if (*s == '\0' || *s == '\n') {
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 		    /* match, be happy */
5090Sstevel@tonic-gate 		    s++;
5100Sstevel@tonic-gate 		    matched = 1;
5110Sstevel@tonic-gate 		    cs = MNEXT(cs);
5120Sstevel@tonic-gate 		} else if (*cs & ALT) {
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 		    /* try the next part */
5150Sstevel@tonic-gate 		    matched = 0;
5160Sstevel@tonic-gate 		    cs = MNEXT(cs);
5170Sstevel@tonic-gate 		} else if (*cs & OPT) {
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 		    /* doesn't matter */
5200Sstevel@tonic-gate 		    matched = 1;
5210Sstevel@tonic-gate 		    cs = MNEXT(cs);
5220Sstevel@tonic-gate 		} else
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 		    /* no match, error return */
5250Sstevel@tonic-gate 		    return (NIL);
5260Sstevel@tonic-gate 		break;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	    /* check for start of line */
5290Sstevel@tonic-gate 	    case '^':
5300Sstevel@tonic-gate 		if (s == Start) {
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 		    /* match, be happy */
5330Sstevel@tonic-gate 		    matched = 1;
5340Sstevel@tonic-gate 		    cs = MNEXT(cs);
5350Sstevel@tonic-gate 		} else if (*cs & ALT) {
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 		    /* try the next part */
5380Sstevel@tonic-gate 		    matched = 0;
5390Sstevel@tonic-gate 		    cs = MNEXT(cs);
5400Sstevel@tonic-gate 		} else if (*cs & OPT) {
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 		    /* doesn't matter */
5430Sstevel@tonic-gate 		    matched = 1;
5440Sstevel@tonic-gate 		    cs = MNEXT(cs);
5450Sstevel@tonic-gate 		} else
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 		    /* no match, error return */
5480Sstevel@tonic-gate 		    return (NIL);
5490Sstevel@tonic-gate 		break;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	    /* end of a subexpression, return success */
5520Sstevel@tonic-gate 	    case ')':
5530Sstevel@tonic-gate 		return (s);
5540Sstevel@tonic-gate 	    }
5550Sstevel@tonic-gate 	    break;
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate     }
5580Sstevel@tonic-gate     return (s);
5590Sstevel@tonic-gate }
560