14887Schin /***********************************************************************
24887Schin * *
34887Schin * This software is part of the ast package *
4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1985-2010 AT&T Intellectual Property *
54887Schin * and is licensed under the *
64887Schin * Common Public License, Version 1.0 *
78462SApril.Chin@Sun.COM * by AT&T Intellectual Property *
84887Schin * *
94887Schin * A copy of the License is available at *
104887Schin * http://www.opensource.org/licenses/cpl1.0.txt *
114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
124887Schin * *
134887Schin * Information and Software Systems Research *
144887Schin * AT&T Research *
154887Schin * Florham Park NJ *
164887Schin * *
174887Schin * Glenn Fowler <gsf@research.att.com> *
184887Schin * David Korn <dgk@research.att.com> *
194887Schin * Phong Vo <kpv@research.att.com> *
204887Schin * *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin * re_comp implementation
254887Schin */
264887Schin
274887Schin #include <ast.h>
284887Schin #include <re_comp.h>
294887Schin #include <regex.h>
304887Schin
314887Schin #undef error
324887Schin #undef valid
334887Schin
344887Schin static struct
354887Schin {
364887Schin char error[64];
374887Schin regex_t re;
384887Schin int valid;
394887Schin } state;
404887Schin
414887Schin char*
re_comp(const char * pattern)424887Schin re_comp(const char* pattern)
434887Schin {
444887Schin register int r;
454887Schin
464887Schin if (!pattern || !*pattern)
474887Schin {
484887Schin if (state.valid)
494887Schin return 0;
504887Schin r = REG_BADPAT;
514887Schin }
524887Schin else
534887Schin {
544887Schin if (state.valid)
554887Schin {
564887Schin state.valid = 0;
574887Schin regfree(&state.re);
584887Schin }
594887Schin if (!(r = regcomp(&state.re, pattern, REG_LENIENT|REG_NOSUB|REG_NULL)))
604887Schin {
614887Schin state.valid = 1;
624887Schin return 0;
634887Schin }
644887Schin }
654887Schin regerror(r, &state.re, state.error, sizeof(state.error));
664887Schin return state.error;
674887Schin }
684887Schin
694887Schin int
re_exec(const char * subject)704887Schin re_exec(const char* subject)
714887Schin {
724887Schin if (state.valid && subject)
734887Schin switch (regexec(&state.re, subject, 0, NiL, 0))
744887Schin {
754887Schin case 0:
764887Schin return 1;
774887Schin case REG_NOMATCH:
784887Schin return 0;
794887Schin }
804887Schin return -1;
814887Schin }
82