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