17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
37c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
47c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
57c478bd9Sstevel@tonic-gate */
67c478bd9Sstevel@tonic-gate
77c478bd9Sstevel@tonic-gate #include <ctype.h>
87c478bd9Sstevel@tonic-gate
97c478bd9Sstevel@tonic-gate typedef int boolean;
107c478bd9Sstevel@tonic-gate #define TRUE 1
117c478bd9Sstevel@tonic-gate #define FALSE 0
127c478bd9Sstevel@tonic-gate #define NIL 0
137c478bd9Sstevel@tonic-gate
147c478bd9Sstevel@tonic-gate extern boolean l_onecase; /* true if upper and lower equivalent */
157c478bd9Sstevel@tonic-gate extern char *l_idchars; /* set of characters legal in identifiers
167c478bd9Sstevel@tonic-gate in addition to letters and digits */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate extern char *strchr();
19e5af7cceScraigm static void expconv(void);
207c478bd9Sstevel@tonic-gate
217c478bd9Sstevel@tonic-gate #define isidchr(c) \
227c478bd9Sstevel@tonic-gate (isalnum(c) || ((c) != NIL && strchr(l_idchars, (c)) != NIL))
237c478bd9Sstevel@tonic-gate #define makelower(c) (isupper((c)) ? tolower((c)) : (c))
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate /* STRNCMP - like strncmp except that we convert the
267c478bd9Sstevel@tonic-gate * first string to lower case before comparing
277c478bd9Sstevel@tonic-gate * if l_onecase is set.
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
30e5af7cceScraigm int
STRNCMP(char * s1,char * s2,int len)31e5af7cceScraigm STRNCMP(char *s1, char *s2, int len)
327c478bd9Sstevel@tonic-gate {
337c478bd9Sstevel@tonic-gate if (l_onecase) {
347c478bd9Sstevel@tonic-gate do
357c478bd9Sstevel@tonic-gate if (*s2 - makelower(*s1))
367c478bd9Sstevel@tonic-gate return (*s2 - makelower(*s1));
377c478bd9Sstevel@tonic-gate else {
387c478bd9Sstevel@tonic-gate s2++;
397c478bd9Sstevel@tonic-gate s1++;
407c478bd9Sstevel@tonic-gate }
417c478bd9Sstevel@tonic-gate while (--len);
427c478bd9Sstevel@tonic-gate } else {
437c478bd9Sstevel@tonic-gate do
447c478bd9Sstevel@tonic-gate if (*s2 - *s1)
457c478bd9Sstevel@tonic-gate return (*s2 - *s1);
467c478bd9Sstevel@tonic-gate else {
477c478bd9Sstevel@tonic-gate s2++;
487c478bd9Sstevel@tonic-gate s1++;
497c478bd9Sstevel@tonic-gate }
507c478bd9Sstevel@tonic-gate while (--len);
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate return(0);
537c478bd9Sstevel@tonic-gate }
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate /* The following routine converts an irregular expression to
567c478bd9Sstevel@tonic-gate * internal format.
577c478bd9Sstevel@tonic-gate *
587c478bd9Sstevel@tonic-gate * Either meta symbols (\a \d or \p) or character strings or
597c478bd9Sstevel@tonic-gate * operations ( alternation or parenthesizing ) can be
607c478bd9Sstevel@tonic-gate * specified. Each starts with a descriptor byte. The descriptor
617c478bd9Sstevel@tonic-gate * byte has STR set for strings, META set for meta symbols
627c478bd9Sstevel@tonic-gate * and OPER set for operations.
637c478bd9Sstevel@tonic-gate * The descriptor byte can also have the OPT bit set if the object
647c478bd9Sstevel@tonic-gate * defined is optional. Also ALT can be set to indicate an alternation.
657c478bd9Sstevel@tonic-gate *
667c478bd9Sstevel@tonic-gate * For metasymbols the byte following the descriptor byte identities
677c478bd9Sstevel@tonic-gate * the meta symbol (containing an ascii 'a', 'd', 'p', '|', or '('). For
687c478bd9Sstevel@tonic-gate * strings the byte after the descriptor is a character count for
697c478bd9Sstevel@tonic-gate * the string:
707c478bd9Sstevel@tonic-gate *
717c478bd9Sstevel@tonic-gate * meta symbols := descriptor
727c478bd9Sstevel@tonic-gate * symbol
737c478bd9Sstevel@tonic-gate *
747c478bd9Sstevel@tonic-gate * strings := descriptor
757c478bd9Sstevel@tonic-gate * character count
767c478bd9Sstevel@tonic-gate * the string
777c478bd9Sstevel@tonic-gate *
787c478bd9Sstevel@tonic-gate * operations := descriptor
797c478bd9Sstevel@tonic-gate * symbol
807c478bd9Sstevel@tonic-gate * character count
817c478bd9Sstevel@tonic-gate */
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate * handy macros for accessing parts of match blocks
857c478bd9Sstevel@tonic-gate */
867c478bd9Sstevel@tonic-gate #define MSYM(A) (*(A+1)) /* symbol in a meta symbol block */
877c478bd9Sstevel@tonic-gate #define MNEXT(A) (A+2) /* character following a metasymbol block */
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate #define OSYM(A) (*(A+1)) /* symbol in an operation block */
907c478bd9Sstevel@tonic-gate #define OCNT(A) (*(A+2)) /* character count */
917c478bd9Sstevel@tonic-gate #define ONEXT(A) (A+3) /* next character after the operation */
927c478bd9Sstevel@tonic-gate #define OPTR(A) (A+*(A+2)) /* place pointed to by the operator */
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate #define SCNT(A) (*(A+1)) /* byte count of a string */
957c478bd9Sstevel@tonic-gate #define SSTR(A) (A+2) /* address of the string */
967c478bd9Sstevel@tonic-gate #define SNEXT(A) (A+2+*(A+1)) /* character following the string */
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * bit flags in the descriptor
1007c478bd9Sstevel@tonic-gate */
1017c478bd9Sstevel@tonic-gate #define OPT 1
1027c478bd9Sstevel@tonic-gate #define STR 2
1037c478bd9Sstevel@tonic-gate #define META 4
1047c478bd9Sstevel@tonic-gate #define ALT 8
1057c478bd9Sstevel@tonic-gate #define OPER 16
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate char *ure; /* pointer current position in unconverted exp */
1087c478bd9Sstevel@tonic-gate char *ccre; /* pointer to current position in converted exp*/
1097c478bd9Sstevel@tonic-gate char *malloc();
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate char *
convexp(char * re)112e5af7cceScraigm convexp(char *re)
113e5af7cceScraigm /* re - unconverted irregular expression */
1147c478bd9Sstevel@tonic-gate {
115e5af7cceScraigm char *cre; /* pointer to converted regular expression */
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate /* allocate room for the converted expression */
1187c478bd9Sstevel@tonic-gate if (re == NIL)
1197c478bd9Sstevel@tonic-gate return (NIL);
1207c478bd9Sstevel@tonic-gate if (*re == '\0')
1217c478bd9Sstevel@tonic-gate return (NIL);
1227c478bd9Sstevel@tonic-gate cre = malloc (4 * strlen(re) + 3);
1237c478bd9Sstevel@tonic-gate ccre = cre;
1247c478bd9Sstevel@tonic-gate ure = re;
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate /* start the conversion with a \a */
1277c478bd9Sstevel@tonic-gate *cre = META | OPT;
1287c478bd9Sstevel@tonic-gate MSYM(cre) = 'a';
1297c478bd9Sstevel@tonic-gate ccre = MNEXT(cre);
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /* start the conversion (its recursive) */
1327c478bd9Sstevel@tonic-gate expconv ();
1337c478bd9Sstevel@tonic-gate *ccre = 0;
1347c478bd9Sstevel@tonic-gate return (cre);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate
137e5af7cceScraigm static void
expconv(void)138e5af7cceScraigm expconv(void)
1397c478bd9Sstevel@tonic-gate {
140e5af7cceScraigm char *cs; /* pointer to current symbol in converted exp */
141e5af7cceScraigm char c; /* character being processed */
142e5af7cceScraigm char *acs; /* pinter to last alternate */
143e5af7cceScraigm int temp;
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate /* let the conversion begin */
1467c478bd9Sstevel@tonic-gate acs = NIL;
1477c478bd9Sstevel@tonic-gate cs = NIL;
1487c478bd9Sstevel@tonic-gate while (*ure != NIL) {
1497c478bd9Sstevel@tonic-gate switch (c = *ure++) {
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate case '\\':
1527c478bd9Sstevel@tonic-gate switch (c = *ure++) {
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate /* escaped characters are just characters */
1557c478bd9Sstevel@tonic-gate default:
1567c478bd9Sstevel@tonic-gate if (cs == NIL || (*cs & STR) == 0) {
1577c478bd9Sstevel@tonic-gate cs = ccre;
1587c478bd9Sstevel@tonic-gate *cs = STR;
1597c478bd9Sstevel@tonic-gate SCNT(cs) = 1;
1607c478bd9Sstevel@tonic-gate ccre += 2;
161*a6dda843SToomas Soome } else {
1627c478bd9Sstevel@tonic-gate SCNT(cs)++;
163*a6dda843SToomas Soome }
1647c478bd9Sstevel@tonic-gate *ccre++ = c;
1657c478bd9Sstevel@tonic-gate break;
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate /* normal(?) metacharacters */
1687c478bd9Sstevel@tonic-gate case 'a':
1697c478bd9Sstevel@tonic-gate case 'd':
1707c478bd9Sstevel@tonic-gate case 'e':
1717c478bd9Sstevel@tonic-gate case 'p':
1727c478bd9Sstevel@tonic-gate if (acs != NIL && acs != cs) {
1737c478bd9Sstevel@tonic-gate do {
1747c478bd9Sstevel@tonic-gate temp = OCNT(acs);
1757c478bd9Sstevel@tonic-gate OCNT(acs) = ccre - acs;
1767c478bd9Sstevel@tonic-gate acs -= temp;
1777c478bd9Sstevel@tonic-gate } while (temp != 0);
1787c478bd9Sstevel@tonic-gate acs = NIL;
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate cs = ccre;
1817c478bd9Sstevel@tonic-gate *cs = META;
1827c478bd9Sstevel@tonic-gate MSYM(cs) = c;
1837c478bd9Sstevel@tonic-gate ccre = MNEXT(cs);
1847c478bd9Sstevel@tonic-gate break;
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate break;
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate /* just put the symbol in */
1897c478bd9Sstevel@tonic-gate case '^':
1907c478bd9Sstevel@tonic-gate case '$':
1917c478bd9Sstevel@tonic-gate if (acs != NIL && acs != cs) {
1927c478bd9Sstevel@tonic-gate do {
1937c478bd9Sstevel@tonic-gate temp = OCNT(acs);
1947c478bd9Sstevel@tonic-gate OCNT(acs) = ccre - acs;
1957c478bd9Sstevel@tonic-gate acs -= temp;
1967c478bd9Sstevel@tonic-gate } while (temp != 0);
1977c478bd9Sstevel@tonic-gate acs = NIL;
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate cs = ccre;
2007c478bd9Sstevel@tonic-gate *cs = META;
2017c478bd9Sstevel@tonic-gate MSYM(cs) = c;
2027c478bd9Sstevel@tonic-gate ccre = MNEXT(cs);
2037c478bd9Sstevel@tonic-gate break;
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate /* mark the last match sequence as optional */
2067c478bd9Sstevel@tonic-gate case '?':
2077c478bd9Sstevel@tonic-gate if (cs)
2087c478bd9Sstevel@tonic-gate *cs = *cs | OPT;
2097c478bd9Sstevel@tonic-gate break;
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate /* recurse and define a subexpression */
2127c478bd9Sstevel@tonic-gate case '(':
2137c478bd9Sstevel@tonic-gate if (acs != NIL && acs != cs) {
2147c478bd9Sstevel@tonic-gate do {
2157c478bd9Sstevel@tonic-gate temp = OCNT(acs);
2167c478bd9Sstevel@tonic-gate OCNT(acs) = ccre - acs;
2177c478bd9Sstevel@tonic-gate acs -= temp;
2187c478bd9Sstevel@tonic-gate } while (temp != 0);
2197c478bd9Sstevel@tonic-gate acs = NIL;
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate cs = ccre;
2227c478bd9Sstevel@tonic-gate *cs = OPER;
2237c478bd9Sstevel@tonic-gate OSYM(cs) = '(';
2247c478bd9Sstevel@tonic-gate ccre = ONEXT(cs);
2257c478bd9Sstevel@tonic-gate expconv ();
2267c478bd9Sstevel@tonic-gate OCNT(cs) = ccre - cs; /* offset to next symbol */
2277c478bd9Sstevel@tonic-gate break;
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate /* return from a recursion */
2307c478bd9Sstevel@tonic-gate case ')':
2317c478bd9Sstevel@tonic-gate if (acs != NIL) {
2327c478bd9Sstevel@tonic-gate do {
2337c478bd9Sstevel@tonic-gate temp = OCNT(acs);
2347c478bd9Sstevel@tonic-gate OCNT(acs) = ccre - acs;
2357c478bd9Sstevel@tonic-gate acs -= temp;
2367c478bd9Sstevel@tonic-gate } while (temp != 0);
2377c478bd9Sstevel@tonic-gate acs = NIL;
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate cs = ccre;
2407c478bd9Sstevel@tonic-gate *cs = META;
2417c478bd9Sstevel@tonic-gate MSYM(cs) = c;
2427c478bd9Sstevel@tonic-gate ccre = MNEXT(cs);
2437c478bd9Sstevel@tonic-gate return;
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate /* mark the last match sequence as having an alternate */
2467c478bd9Sstevel@tonic-gate /* the third byte will contain an offset to jump over the */
2477c478bd9Sstevel@tonic-gate /* alternate match in case the first did not fail */
2487c478bd9Sstevel@tonic-gate case '|':
2497c478bd9Sstevel@tonic-gate if (acs != NIL && acs != cs)
2507c478bd9Sstevel@tonic-gate OCNT(ccre) = ccre - acs; /* make a back pointer */
2517c478bd9Sstevel@tonic-gate else
2527c478bd9Sstevel@tonic-gate OCNT(ccre) = 0;
2537c478bd9Sstevel@tonic-gate *cs |= ALT;
2547c478bd9Sstevel@tonic-gate cs = ccre;
2557c478bd9Sstevel@tonic-gate *cs = OPER;
2567c478bd9Sstevel@tonic-gate OSYM(cs) = '|';
2577c478bd9Sstevel@tonic-gate ccre = ONEXT(cs);
2587c478bd9Sstevel@tonic-gate acs = cs; /* remember that the pointer is to be filles */
2597c478bd9Sstevel@tonic-gate break;
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate /* if its not a metasymbol just build a scharacter string */
2627c478bd9Sstevel@tonic-gate default:
2637c478bd9Sstevel@tonic-gate if (cs == NIL || (*cs & STR) == 0) {
2647c478bd9Sstevel@tonic-gate cs = ccre;
2657c478bd9Sstevel@tonic-gate *cs = STR;
2667c478bd9Sstevel@tonic-gate SCNT(cs) = 1;
2677c478bd9Sstevel@tonic-gate ccre = SSTR(cs);
2687c478bd9Sstevel@tonic-gate } else
2697c478bd9Sstevel@tonic-gate SCNT(cs)++;
2707c478bd9Sstevel@tonic-gate *ccre++ = c;
2717c478bd9Sstevel@tonic-gate break;
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate if (acs != NIL) {
2757c478bd9Sstevel@tonic-gate do {
2767c478bd9Sstevel@tonic-gate temp = OCNT(acs);
2777c478bd9Sstevel@tonic-gate OCNT(acs) = ccre - acs;
2787c478bd9Sstevel@tonic-gate acs -= temp;
2797c478bd9Sstevel@tonic-gate } while (temp != 0);
2807c478bd9Sstevel@tonic-gate acs = NIL;
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate /* end of convertre */
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate * The following routine recognises an irregular expresion
2887c478bd9Sstevel@tonic-gate * with the following special characters:
2897c478bd9Sstevel@tonic-gate *
2907c478bd9Sstevel@tonic-gate * \? - means last match was optional
2917c478bd9Sstevel@tonic-gate * \a - matches any number of characters
2927c478bd9Sstevel@tonic-gate * \d - matches any number of spaces and tabs
2937c478bd9Sstevel@tonic-gate * \p - matches any number of alphanumeric
2947c478bd9Sstevel@tonic-gate * characters. The
2957c478bd9Sstevel@tonic-gate * characters matched will be copied into
2967c478bd9Sstevel@tonic-gate * the area pointed to by 'name'.
2977c478bd9Sstevel@tonic-gate * \| - alternation
2987c478bd9Sstevel@tonic-gate * \( \) - grouping used mostly for alternation and
2997c478bd9Sstevel@tonic-gate * optionality
3007c478bd9Sstevel@tonic-gate *
3017c478bd9Sstevel@tonic-gate * The irregular expression must be translated to internal form
3027c478bd9Sstevel@tonic-gate * prior to calling this routine
3037c478bd9Sstevel@tonic-gate *
3047c478bd9Sstevel@tonic-gate * The value returned is the pointer to the first non \a
3057c478bd9Sstevel@tonic-gate * character matched.
3067c478bd9Sstevel@tonic-gate */
3077c478bd9Sstevel@tonic-gate
308*a6dda843SToomas Soome extern boolean _escaped; /* true if we are currently _escaped */
309*a6dda843SToomas Soome extern char *Start; /* start of string */
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate char *
expmatch(char * s,char * re,char * mstring)312e5af7cceScraigm expmatch(char *s, char *re, char *mstring)
313e5af7cceScraigm /* s - string to check for a match in */
314e5af7cceScraigm /* re - a converted irregular expression */
315e5af7cceScraigm /* mstring - where to put whatever matches a \p */
3167c478bd9Sstevel@tonic-gate {
317e5af7cceScraigm char *cs; /* the current symbol */
318e5af7cceScraigm char *ptr, *s1; /* temporary pointer */
3197c478bd9Sstevel@tonic-gate boolean matched; /* a temporary boolean */
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate /* initial conditions */
3227c478bd9Sstevel@tonic-gate if (re == NIL)
3237c478bd9Sstevel@tonic-gate return (NIL);
3247c478bd9Sstevel@tonic-gate cs = re;
3257c478bd9Sstevel@tonic-gate matched = FALSE;
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate /* loop till expression string is exhausted (or at least pretty tired) */
3287c478bd9Sstevel@tonic-gate while (*cs) {
3297c478bd9Sstevel@tonic-gate switch (*cs & (OPER | STR | META)) {
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate /* try to match a string */
3327c478bd9Sstevel@tonic-gate case STR:
3337c478bd9Sstevel@tonic-gate matched = !STRNCMP (s, SSTR(cs), SCNT(cs));
3347c478bd9Sstevel@tonic-gate if (matched) {
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate /* hoorah it matches */
3377c478bd9Sstevel@tonic-gate s += SCNT(cs);
3387c478bd9Sstevel@tonic-gate cs = SNEXT(cs);
3397c478bd9Sstevel@tonic-gate } else if (*cs & ALT) {
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate /* alternation, skip to next expression */
3427c478bd9Sstevel@tonic-gate cs = SNEXT(cs);
3437c478bd9Sstevel@tonic-gate } else if (*cs & OPT) {
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate /* the match is optional */
3467c478bd9Sstevel@tonic-gate cs = SNEXT(cs);
3477c478bd9Sstevel@tonic-gate matched = 1; /* indicate a successful match */
3487c478bd9Sstevel@tonic-gate } else {
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate /* no match, error return */
3517c478bd9Sstevel@tonic-gate return (NIL);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate break;
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate /* an operator, do something fancy */
3567c478bd9Sstevel@tonic-gate case OPER:
3577c478bd9Sstevel@tonic-gate switch (OSYM(cs)) {
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate /* this is an alternation */
3607c478bd9Sstevel@tonic-gate case '|':
3617c478bd9Sstevel@tonic-gate if (matched)
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate /* last thing in the alternation was a match, skip ahead */
3647c478bd9Sstevel@tonic-gate cs = OPTR(cs);
3657c478bd9Sstevel@tonic-gate else
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate /* no match, keep trying */
3687c478bd9Sstevel@tonic-gate cs = ONEXT(cs);
3697c478bd9Sstevel@tonic-gate break;
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate /* this is a grouping, recurse */
3727c478bd9Sstevel@tonic-gate case '(':
3737c478bd9Sstevel@tonic-gate ptr = expmatch (s, ONEXT(cs), mstring);
3747c478bd9Sstevel@tonic-gate if (ptr != NIL) {
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate /* the subexpression matched */
3777c478bd9Sstevel@tonic-gate matched = 1;
3787c478bd9Sstevel@tonic-gate s = ptr;
3797c478bd9Sstevel@tonic-gate } else if (*cs & ALT) {
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate /* alternation, skip to next expression */
3827c478bd9Sstevel@tonic-gate matched = 0;
3837c478bd9Sstevel@tonic-gate } else if (*cs & OPT) {
3847c478bd9Sstevel@tonic-gate
3857c478bd9Sstevel@tonic-gate /* the match is optional */
3867c478bd9Sstevel@tonic-gate matched = 1; /* indicate a successful match */
3877c478bd9Sstevel@tonic-gate } else {
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate /* no match, error return */
3907c478bd9Sstevel@tonic-gate return (NIL);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate cs = OPTR(cs);
3937c478bd9Sstevel@tonic-gate break;
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate break;
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate /* try to match a metasymbol */
3987c478bd9Sstevel@tonic-gate case META:
3997c478bd9Sstevel@tonic-gate switch (MSYM(cs)) {
4007c478bd9Sstevel@tonic-gate
4017c478bd9Sstevel@tonic-gate /* try to match anything and remember what was matched */
4027c478bd9Sstevel@tonic-gate case 'p':
4037c478bd9Sstevel@tonic-gate /*
4047c478bd9Sstevel@tonic-gate * This is really the same as trying the match the
4057c478bd9Sstevel@tonic-gate * remaining parts of the expression to any subset
4067c478bd9Sstevel@tonic-gate * of the string.
4077c478bd9Sstevel@tonic-gate */
4087c478bd9Sstevel@tonic-gate s1 = s;
4097c478bd9Sstevel@tonic-gate do {
4107c478bd9Sstevel@tonic-gate ptr = expmatch (s1, MNEXT(cs), mstring);
4117c478bd9Sstevel@tonic-gate if (ptr != NIL && s1 != s) {
4127c478bd9Sstevel@tonic-gate
4137c478bd9Sstevel@tonic-gate /* we have a match, remember the match */
4147c478bd9Sstevel@tonic-gate strncpy (mstring, s, s1 - s);
4157c478bd9Sstevel@tonic-gate mstring[s1 - s] = '\0';
4167c478bd9Sstevel@tonic-gate return (ptr);
4177c478bd9Sstevel@tonic-gate } else if (ptr != NIL && (*cs & OPT)) {
4187c478bd9Sstevel@tonic-gate
4197c478bd9Sstevel@tonic-gate /* it was aoptional so no match is ok */
4207c478bd9Sstevel@tonic-gate return (ptr);
4217c478bd9Sstevel@tonic-gate } else if (ptr != NIL) {
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate /* not optional and we still matched */
4247c478bd9Sstevel@tonic-gate return (NIL);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate if (!isidchr(*s1))
4277c478bd9Sstevel@tonic-gate return (NIL);
4287c478bd9Sstevel@tonic-gate if (*s1 == '\\')
4297c478bd9Sstevel@tonic-gate _escaped = _escaped ? FALSE : TRUE;
4307c478bd9Sstevel@tonic-gate else
4317c478bd9Sstevel@tonic-gate _escaped = FALSE;
4327c478bd9Sstevel@tonic-gate } while (*s1++);
4337c478bd9Sstevel@tonic-gate return (NIL);
4347c478bd9Sstevel@tonic-gate
4357c478bd9Sstevel@tonic-gate /* try to match anything */
4367c478bd9Sstevel@tonic-gate case 'a':
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate * This is really the same as trying the match the
4397c478bd9Sstevel@tonic-gate * remaining parts of the expression to any subset
4407c478bd9Sstevel@tonic-gate * of the string.
4417c478bd9Sstevel@tonic-gate */
4427c478bd9Sstevel@tonic-gate s1 = s;
4437c478bd9Sstevel@tonic-gate do {
4447c478bd9Sstevel@tonic-gate ptr = expmatch (s1, MNEXT(cs), mstring);
4457c478bd9Sstevel@tonic-gate if (ptr != NIL && s1 != s) {
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate /* we have a match */
4487c478bd9Sstevel@tonic-gate return (ptr);
4497c478bd9Sstevel@tonic-gate } else if (ptr != NIL && (*cs & OPT)) {
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate /* it was aoptional so no match is ok */
4527c478bd9Sstevel@tonic-gate return (ptr);
4537c478bd9Sstevel@tonic-gate } else if (ptr != NIL) {
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate /* not optional and we still matched */
4567c478bd9Sstevel@tonic-gate return (NIL);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate if (*s1 == '\\')
4597c478bd9Sstevel@tonic-gate _escaped = _escaped ? FALSE : TRUE;
4607c478bd9Sstevel@tonic-gate else
4617c478bd9Sstevel@tonic-gate _escaped = FALSE;
4627c478bd9Sstevel@tonic-gate } while (*s1++);
4637c478bd9Sstevel@tonic-gate return (NIL);
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate /* fail if we are currently _escaped */
4667c478bd9Sstevel@tonic-gate case 'e':
4677c478bd9Sstevel@tonic-gate if (_escaped)
4687c478bd9Sstevel@tonic-gate return(NIL);
4697c478bd9Sstevel@tonic-gate cs = MNEXT(cs);
4707c478bd9Sstevel@tonic-gate break;
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate /* match any number of tabs and spaces */
4737c478bd9Sstevel@tonic-gate case 'd':
4747c478bd9Sstevel@tonic-gate ptr = s;
4757c478bd9Sstevel@tonic-gate while (*s == ' ' || *s == '\t')
4767c478bd9Sstevel@tonic-gate s++;
4777c478bd9Sstevel@tonic-gate if (s != ptr || s == Start) {
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate /* match, be happy */
4807c478bd9Sstevel@tonic-gate matched = 1;
4817c478bd9Sstevel@tonic-gate cs = MNEXT(cs);
4827c478bd9Sstevel@tonic-gate } else if (*s == '\n' || *s == '\0') {
4837c478bd9Sstevel@tonic-gate
4847c478bd9Sstevel@tonic-gate /* match, be happy */
4857c478bd9Sstevel@tonic-gate matched = 1;
4867c478bd9Sstevel@tonic-gate cs = MNEXT(cs);
4877c478bd9Sstevel@tonic-gate } else if (*cs & ALT) {
4887c478bd9Sstevel@tonic-gate
4897c478bd9Sstevel@tonic-gate /* try the next part */
4907c478bd9Sstevel@tonic-gate matched = 0;
4917c478bd9Sstevel@tonic-gate cs = MNEXT(cs);
4927c478bd9Sstevel@tonic-gate } else if (*cs & OPT) {
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate /* doesn't matter */
4957c478bd9Sstevel@tonic-gate matched = 1;
4967c478bd9Sstevel@tonic-gate cs = MNEXT(cs);
4977c478bd9Sstevel@tonic-gate } else
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate /* no match, error return */
5007c478bd9Sstevel@tonic-gate return (NIL);
5017c478bd9Sstevel@tonic-gate break;
5027c478bd9Sstevel@tonic-gate
5037c478bd9Sstevel@tonic-gate /* check for end of line */
5047c478bd9Sstevel@tonic-gate case '$':
5057c478bd9Sstevel@tonic-gate if (*s == '\0' || *s == '\n') {
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate /* match, be happy */
5087c478bd9Sstevel@tonic-gate s++;
5097c478bd9Sstevel@tonic-gate matched = 1;
5107c478bd9Sstevel@tonic-gate cs = MNEXT(cs);
5117c478bd9Sstevel@tonic-gate } else if (*cs & ALT) {
5127c478bd9Sstevel@tonic-gate
5137c478bd9Sstevel@tonic-gate /* try the next part */
5147c478bd9Sstevel@tonic-gate matched = 0;
5157c478bd9Sstevel@tonic-gate cs = MNEXT(cs);
5167c478bd9Sstevel@tonic-gate } else if (*cs & OPT) {
5177c478bd9Sstevel@tonic-gate
5187c478bd9Sstevel@tonic-gate /* doesn't matter */
5197c478bd9Sstevel@tonic-gate matched = 1;
5207c478bd9Sstevel@tonic-gate cs = MNEXT(cs);
5217c478bd9Sstevel@tonic-gate } else
5227c478bd9Sstevel@tonic-gate
5237c478bd9Sstevel@tonic-gate /* no match, error return */
5247c478bd9Sstevel@tonic-gate return (NIL);
5257c478bd9Sstevel@tonic-gate break;
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate /* check for start of line */
5287c478bd9Sstevel@tonic-gate case '^':
5297c478bd9Sstevel@tonic-gate if (s == Start) {
5307c478bd9Sstevel@tonic-gate
5317c478bd9Sstevel@tonic-gate /* match, be happy */
5327c478bd9Sstevel@tonic-gate matched = 1;
5337c478bd9Sstevel@tonic-gate cs = MNEXT(cs);
5347c478bd9Sstevel@tonic-gate } else if (*cs & ALT) {
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate /* try the next part */
5377c478bd9Sstevel@tonic-gate matched = 0;
5387c478bd9Sstevel@tonic-gate cs = MNEXT(cs);
5397c478bd9Sstevel@tonic-gate } else if (*cs & OPT) {
5407c478bd9Sstevel@tonic-gate
5417c478bd9Sstevel@tonic-gate /* doesn't matter */
5427c478bd9Sstevel@tonic-gate matched = 1;
5437c478bd9Sstevel@tonic-gate cs = MNEXT(cs);
5447c478bd9Sstevel@tonic-gate } else
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate /* no match, error return */
5477c478bd9Sstevel@tonic-gate return (NIL);
5487c478bd9Sstevel@tonic-gate break;
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate /* end of a subexpression, return success */
5517c478bd9Sstevel@tonic-gate case ')':
5527c478bd9Sstevel@tonic-gate return (s);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate break;
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate return (s);
5587c478bd9Sstevel@tonic-gate }
559