xref: /onnv-gate/usr/src/lib/libc/port/regex/regcmp.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * IMPORTANT NOTE:
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * regcmp() WORKS **ONLY** WITH THE ASCII AND THE Solaris EUC CHARACTER SETS.
360Sstevel@tonic-gate  * IT IS **NOT** CHARACTER SET INDEPENDENT.
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
40*6812Sraf #pragma weak _regcmp = regcmp
410Sstevel@tonic-gate 
42*6812Sraf #include "lint.h"
430Sstevel@tonic-gate #include "mtlib.h"
440Sstevel@tonic-gate #include <limits.h>
450Sstevel@tonic-gate #include <stdarg.h>
460Sstevel@tonic-gate #include <stdlib.h>
470Sstevel@tonic-gate #include <thread.h>
480Sstevel@tonic-gate #include <wctype.h>
490Sstevel@tonic-gate #include <widec.h>
500Sstevel@tonic-gate #include <string.h>
510Sstevel@tonic-gate #include "tsd.h"
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /* CONSTANTS SHARED WITH regex() */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include "regex.h"
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /* PRIVATE CONSTANTS */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #define	BACKSLASH		'\\'
610Sstevel@tonic-gate #define	CIRCUMFLEX		'^'
620Sstevel@tonic-gate #define	COMMA			','
630Sstevel@tonic-gate #define	DASH			'-'
640Sstevel@tonic-gate #define	DOLLAR_SIGN		'$'
650Sstevel@tonic-gate #define	DOT			'.'
660Sstevel@tonic-gate #define	LEFT_CURLY_BRACE	'{'
670Sstevel@tonic-gate #define	LEFT_PAREN		'('
680Sstevel@tonic-gate #define	LEFT_SQUARE_BRACKET	'['
690Sstevel@tonic-gate #define	PLUS			'+'
700Sstevel@tonic-gate #define	RIGHT_CURLY_BRACE	'}'
710Sstevel@tonic-gate #define	RIGHT_PAREN		')'
720Sstevel@tonic-gate #define	RIGHT_SQUARE_BRACKET	']'
730Sstevel@tonic-gate #define	SINGLE_BYTE_MASK	0xff
740Sstevel@tonic-gate #define	STRINGP_STACK_SIZE	50
750Sstevel@tonic-gate #define	STAR			'*'
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /* PRIVATE GLOBAL VARIABLES */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static char	*compilep_stack[STRINGP_STACK_SIZE];
800Sstevel@tonic-gate static char	**compilep_stackp;
810Sstevel@tonic-gate static mutex_t  regcmp_lock = DEFAULTMUTEX;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /* DECLARATIONS OF PRIVATE FUNCTIONS */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static int add_char(char *compilep, wchar_t wchar);
860Sstevel@tonic-gate static int add_single_char_expr(char *compilep, wchar_t wchar);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #define	ERROR_EXIT(mutex_lockp, arg_listp, compile_startp) \
890Sstevel@tonic-gate \
900Sstevel@tonic-gate 	va_end(arg_listp); \
910Sstevel@tonic-gate 	lmutex_unlock(mutex_lockp); \
920Sstevel@tonic-gate 	if ((compile_startp) != (char *)0) \
930Sstevel@tonic-gate 		free((void *)compile_startp); \
940Sstevel@tonic-gate 	return ((char *)0)
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static int get_count(int *countp, const char *regexp);
970Sstevel@tonic-gate static int get_digit(const char *regexp);
980Sstevel@tonic-gate static int get_wchar(wchar_t *wchar, const char *regexp);
990Sstevel@tonic-gate static char *pop_compilep(void);
1000Sstevel@tonic-gate static char *push_compilep(char *compilep);
1010Sstevel@tonic-gate static boolean_t valid_range(wchar_t lower_char, wchar_t upper_char);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /* DEFINITIONS OF PUBLIC VARIABLES */
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate int __i_size;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * define thread-specific storage for __i_size
1100Sstevel@tonic-gate  *
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate int *
___i_size(void)1130Sstevel@tonic-gate ___i_size(void)
1140Sstevel@tonic-gate {
115*6812Sraf 	if (thr_main())
1160Sstevel@tonic-gate 		return (&__i_size);
1170Sstevel@tonic-gate 	return ((int *)tsdalloc(_T_REGCMP_ISIZE, sizeof (int), NULL));
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate #define		__i_size (*(___i_size()))
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /* DEFINITION OF regcmp() */
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate extern char *
regcmp(const char * regexp,...)1250Sstevel@tonic-gate regcmp(const char *regexp, ...)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	va_list		arg_listp;
1280Sstevel@tonic-gate 	size_t		arg_strlen;
1290Sstevel@tonic-gate 	boolean_t	can_repeat;
1300Sstevel@tonic-gate 	int		char_size;
1310Sstevel@tonic-gate 	unsigned int	class_length;
1320Sstevel@tonic-gate 	char		*compilep;
1330Sstevel@tonic-gate 	char		*compile_startp = (char *)0;
1340Sstevel@tonic-gate 	int		count_length;
1350Sstevel@tonic-gate 	wchar_t		current_char;
1360Sstevel@tonic-gate 	int		expr_length;
1370Sstevel@tonic-gate 	int		groupn;
1380Sstevel@tonic-gate 	unsigned int	group_length;
1390Sstevel@tonic-gate 	unsigned int	high_bits;
1400Sstevel@tonic-gate 	boolean_t	dash_indicates_range;
1410Sstevel@tonic-gate 	unsigned int	low_bits;
1420Sstevel@tonic-gate 	int		max_count;
1430Sstevel@tonic-gate 	int		min_count;
1440Sstevel@tonic-gate 	const char	*next_argp;
1450Sstevel@tonic-gate 	wchar_t		first_char_in_range;
1460Sstevel@tonic-gate 	char		*regex_typep;
1470Sstevel@tonic-gate 	int		return_arg_number;
1480Sstevel@tonic-gate 	int		substringn;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if (___i_size() == (int *)0)
1510Sstevel@tonic-gate 		return ((char *)0);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/*
1540Sstevel@tonic-gate 	 * When compiling a regular expression, regcmp() generates at most
1550Sstevel@tonic-gate 	 * two extra single-byte characters for each character in the
1560Sstevel@tonic-gate 	 * expression, so allocating three times the number of bytes in all
1570Sstevel@tonic-gate 	 * the strings that comprise the regular expression will ensure that
1580Sstevel@tonic-gate 	 * regcmp() won't overwrite the end of the allocated block when
1590Sstevel@tonic-gate 	 * compiling the expression.
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	va_start(arg_listp, regexp);
1630Sstevel@tonic-gate 	next_argp = regexp;
1640Sstevel@tonic-gate 	arg_strlen = 0;
1650Sstevel@tonic-gate 	while (next_argp != (char *)0) {
1660Sstevel@tonic-gate 		arg_strlen += strlen(next_argp);
1670Sstevel@tonic-gate 		next_argp = va_arg(arg_listp, /* const */ char *);
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 	va_end(arg_listp);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (arg_strlen == 0)
1720Sstevel@tonic-gate 		return ((char *)0);
1730Sstevel@tonic-gate 	compile_startp = (char *)malloc(3 * arg_strlen);
1740Sstevel@tonic-gate 	if (compile_startp == (char *)0)
1750Sstevel@tonic-gate 		return ((char *)0);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	lmutex_lock(&regcmp_lock);
1780Sstevel@tonic-gate 	__i_size = 0;
1790Sstevel@tonic-gate 	compilep = compile_startp;
1800Sstevel@tonic-gate 	compilep_stackp = &compilep_stack[STRINGP_STACK_SIZE];
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/* GET THE FIRST CHARACTER IN THE REGULAR EXPRESSION */
1830Sstevel@tonic-gate 	va_start(arg_listp, regexp);
1840Sstevel@tonic-gate 	next_argp = va_arg(arg_listp, /* const */ char *);
1850Sstevel@tonic-gate 	char_size = get_wchar(&current_char, regexp);
1860Sstevel@tonic-gate 	if (char_size < 0) {
1870Sstevel@tonic-gate 		ERROR_EXIT(&regcmp_lock, arg_listp, compile_startp);
1880Sstevel@tonic-gate 	} else if (char_size > 0) {
1890Sstevel@tonic-gate 		regexp += char_size;
1900Sstevel@tonic-gate 	} else /* (char_size == 0 ) */ {
1910Sstevel@tonic-gate 		regexp = next_argp;
1920Sstevel@tonic-gate 		next_argp = va_arg(arg_listp, /* const */ char *);
1930Sstevel@tonic-gate 		char_size = get_wchar(&current_char, regexp);
1940Sstevel@tonic-gate 		if (char_size <= 0) {
1950Sstevel@tonic-gate 			ERROR_EXIT(&regcmp_lock, arg_listp, compile_startp);
1960Sstevel@tonic-gate 		} else {
1970Sstevel@tonic-gate 			regexp += char_size;
1980Sstevel@tonic-gate 		}
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	/* FIND OUT IF THE EXPRESSION MUST START AT THE START OF A STRING */
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (current_char == CIRCUMFLEX) {
2040Sstevel@tonic-gate 		char_size = get_wchar(&current_char, regexp);
2050Sstevel@tonic-gate 		if (char_size < 0) {
2060Sstevel@tonic-gate 			ERROR_EXIT(&regcmp_lock, arg_listp, compile_startp);
2070Sstevel@tonic-gate 		} else if (char_size > 0) {
2080Sstevel@tonic-gate 			regexp += char_size;
2090Sstevel@tonic-gate 			*compilep = (unsigned char)START_OF_STRING_MARK;
2100Sstevel@tonic-gate 			compilep++;
2110Sstevel@tonic-gate 		} else if /* (char_size == 0) && */ (next_argp != (char *)0) {
2120Sstevel@tonic-gate 			regexp = next_argp;
2130Sstevel@tonic-gate 			next_argp = va_arg(arg_listp, /* const */ char *);
2140Sstevel@tonic-gate 			char_size = get_wchar(&current_char, regexp);
2150Sstevel@tonic-gate 			if (char_size <= 0) {
2160Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
2170Sstevel@tonic-gate 				    compile_startp);
2180Sstevel@tonic-gate 			} else {
2190Sstevel@tonic-gate 				regexp += char_size;
2200Sstevel@tonic-gate 			}
2210Sstevel@tonic-gate 			*compilep = (unsigned char)START_OF_STRING_MARK;
2220Sstevel@tonic-gate 			compilep++;
2230Sstevel@tonic-gate 		} else {
2240Sstevel@tonic-gate 			/* ((char_size==0) && (next_argp==(char *)0)) */
2250Sstevel@tonic-gate 			/*
2260Sstevel@tonic-gate 			 * the regular expression is "^"
2270Sstevel@tonic-gate 			 */
2280Sstevel@tonic-gate 			*compilep = (unsigned char)START_OF_STRING_MARK;
2290Sstevel@tonic-gate 			compilep++;
2300Sstevel@tonic-gate 			*compilep = (unsigned char)END_REGEX;
2310Sstevel@tonic-gate 			compilep++;
2320Sstevel@tonic-gate 			*compilep = '\0';
2330Sstevel@tonic-gate 			compilep++;
2340Sstevel@tonic-gate 			__i_size = (int)(compilep - compile_startp);
2350Sstevel@tonic-gate 			va_end(arg_listp);
2360Sstevel@tonic-gate 			lmutex_unlock(&regcmp_lock);
2370Sstevel@tonic-gate 			return (compile_startp);
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	/* COMPILE THE REGULAR EXPRESSION */
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	groupn = 0;
2440Sstevel@tonic-gate 	substringn = 0;
2450Sstevel@tonic-gate 	can_repeat = B_FALSE;
2460Sstevel@tonic-gate 	for (;;) {
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 		/*
2490Sstevel@tonic-gate 		 * At the end of each iteration get the next character
2500Sstevel@tonic-gate 		 * from the regular expression and increment regexp to
2510Sstevel@tonic-gate 		 * point to the following character.  Exit when all
2520Sstevel@tonic-gate 		 * the characters in all the strings in the argument
2530Sstevel@tonic-gate 		 * list have been read.
2540Sstevel@tonic-gate 		 */
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 		switch (current_char) {
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 			/*
2590Sstevel@tonic-gate 			 * No fall-through.  Each case ends with either
2600Sstevel@tonic-gate 			 * a break or an error exit.  Each case starts
2610Sstevel@tonic-gate 			 * with compilep addressing the next location to
2620Sstevel@tonic-gate 			 * be written in the compiled regular expression,
2630Sstevel@tonic-gate 			 * and with regexp addressing the next character
2640Sstevel@tonic-gate 			 * to be read from the regular expression being
2650Sstevel@tonic-gate 			 * compiled.  Each case that doesn't return
2660Sstevel@tonic-gate 			 * increments regexp to address the next character
2670Sstevel@tonic-gate 			 * to be read from the regular expression and
2680Sstevel@tonic-gate 			 * increments compilep to address the next
2690Sstevel@tonic-gate 			 * location to be written in the compiled
2700Sstevel@tonic-gate 			 * regular expression.
2710Sstevel@tonic-gate 			 *
2720Sstevel@tonic-gate 			 * NOTE: The comments for each case give the meaning
2730Sstevel@tonic-gate 			 * of the regular expression compiled by the case
2740Sstevel@tonic-gate 			 * and the character string written to the compiled
2750Sstevel@tonic-gate 			 * regular expression by the case.  Each single
2760Sstevel@tonic-gate 			 * character
2770Sstevel@tonic-gate 			 * written to the compiled regular expression is
2780Sstevel@tonic-gate 			 * shown enclosed in angle brackets (<>).  Each
2790Sstevel@tonic-gate 			 * compiled regular expression begins with a marker
2800Sstevel@tonic-gate 			 * character which is shown as a named constant
2810Sstevel@tonic-gate 			 * (e.g. <ASCII_CHAR>). Character constants are
2820Sstevel@tonic-gate 			 * shown enclosed in single quotes (e.g. <'$'>).
2830Sstevel@tonic-gate 			 * All other single characters written to the
2840Sstevel@tonic-gate 			 * compiled regular expression are shown as lower
2850Sstevel@tonic-gate 			 * case variable names (e.g. <ascii_char> or
2860Sstevel@tonic-gate 			 * <multibyte_char>). Multicharacter
2870Sstevel@tonic-gate 			 * strings written to the compiled regular expression
2880Sstevel@tonic-gate 			 * are shown as variable names followed by elipses
2890Sstevel@tonic-gate 			 * (e.g. <regex...>).
2900Sstevel@tonic-gate 			 */
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		case DOLLAR_SIGN:
2930Sstevel@tonic-gate 			/* end of string marker or simple dollar sign */
2940Sstevel@tonic-gate 			/* compiles to <END_OF_STRING_MARK> or */
2950Sstevel@tonic-gate 			/* <ASCII_CHAR><'$'> */
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 			char_size = get_wchar(&current_char, regexp);
2980Sstevel@tonic-gate 			if ((char_size == 0) && (next_argp == (char *)0)) {
2990Sstevel@tonic-gate 				can_repeat = B_FALSE;
3000Sstevel@tonic-gate 				*compilep = (unsigned char)END_OF_STRING_MARK;
3010Sstevel@tonic-gate 				compilep++;
3020Sstevel@tonic-gate 			} else {
3030Sstevel@tonic-gate 				can_repeat = B_TRUE;
3040Sstevel@tonic-gate 				*compilep = (unsigned char)ASCII_CHAR;
3050Sstevel@tonic-gate 				regex_typep = compilep;
3060Sstevel@tonic-gate 				compilep++;
3070Sstevel@tonic-gate 				*compilep = DOLLAR_SIGN;
3080Sstevel@tonic-gate 				compilep++;
3090Sstevel@tonic-gate 			}
3100Sstevel@tonic-gate 			break; /* end case DOLLAR_SIGN */
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		case DOT: /* any character */
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 			/* compiles to <ANY_CHAR> */
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 			can_repeat = B_TRUE;
3170Sstevel@tonic-gate 			*compilep = (unsigned char)ANY_CHAR;
3180Sstevel@tonic-gate 			regex_typep = compilep;
3190Sstevel@tonic-gate 			compilep++;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 			break; /* end case DOT */
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 		case BACKSLASH: /* escaped character */
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 			/*
3260Sstevel@tonic-gate 			 * compiles to <ASCII_CHAR><ascii_char> or
3270Sstevel@tonic-gate 			 * <MULTIBYTE_CHAR><multibyte_char>
3280Sstevel@tonic-gate 			 */
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 			char_size = get_wchar(&current_char, regexp);
3310Sstevel@tonic-gate 			if (char_size <= 0) {
3320Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
3330Sstevel@tonic-gate 				    compile_startp);
3340Sstevel@tonic-gate 			} else {
3350Sstevel@tonic-gate 				regexp += char_size;
3360Sstevel@tonic-gate 				can_repeat = B_TRUE;
3370Sstevel@tonic-gate 				expr_length = add_single_char_expr(
3380Sstevel@tonic-gate 				    compilep, current_char);
3390Sstevel@tonic-gate 				regex_typep = compilep;
3400Sstevel@tonic-gate 				compilep += expr_length;
3410Sstevel@tonic-gate 			}
3420Sstevel@tonic-gate 			break; /* end case '\\' */
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 		case LEFT_SQUARE_BRACKET:
3450Sstevel@tonic-gate 			/* start of a character class expression */
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 			/*
3480Sstevel@tonic-gate 			 * [^...c...] compiles to
3490Sstevel@tonic-gate 			 * <NOT_IN_CLASS><class_length><...c...>
3500Sstevel@tonic-gate 			 * [^...a-z...] compiles to
3510Sstevel@tonic-gate 			 * <NOT_IN_CLASS><class_length><...a<THRU>z...>
3520Sstevel@tonic-gate 			 * [...c...] compiles to
3530Sstevel@tonic-gate 			 * <IN_CLASS><class_length><...c...>
3540Sstevel@tonic-gate 			 * [...a-z...] compiles to
3550Sstevel@tonic-gate 			 * <IN_CLASS><class_length><...a<THRU>z...>
3560Sstevel@tonic-gate 			 *
3570Sstevel@tonic-gate 			 * NOTE: <class_length> includes the
3580Sstevel@tonic-gate 			 * <class_length> byte
3590Sstevel@tonic-gate 			 */
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 			can_repeat = B_TRUE;
3620Sstevel@tonic-gate 			regex_typep = compilep;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 			/* DETERMINE THE CLASS TYPE */
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 			/*
3670Sstevel@tonic-gate 			 * NOTE: This algorithm checks the value of the
3680Sstevel@tonic-gate 			 * "multibyte"
3690Sstevel@tonic-gate 			 * macro in <euc.h> (included in <widec.h> )
3700Sstevel@tonic-gate 			 * to find out if regcmp()
3710Sstevel@tonic-gate 			 * is compiling the regular expression in a
3720Sstevel@tonic-gate 			 * multibyte locale.
3730Sstevel@tonic-gate 			 */
3740Sstevel@tonic-gate 			char_size = get_wchar(&current_char, regexp);
3750Sstevel@tonic-gate 			if (char_size <= 0) {
3760Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
3770Sstevel@tonic-gate 				    compile_startp);
3780Sstevel@tonic-gate 			} else if (current_char == CIRCUMFLEX) {
3790Sstevel@tonic-gate 				regexp++;
3800Sstevel@tonic-gate 				char_size = get_wchar(&current_char, regexp);
3810Sstevel@tonic-gate 				if (char_size <= 0) {
3820Sstevel@tonic-gate 					ERROR_EXIT(&regcmp_lock,
3830Sstevel@tonic-gate 					    arg_listp, compile_startp);
3840Sstevel@tonic-gate 				} else {
3850Sstevel@tonic-gate 					regexp += char_size;
3860Sstevel@tonic-gate 					if (!multibyte) {
3870Sstevel@tonic-gate 						*compilep = (unsigned char)
3880Sstevel@tonic-gate 						    NOT_IN_ASCII_CHAR_CLASS;
3890Sstevel@tonic-gate 					} else {
3900Sstevel@tonic-gate 						*compilep = (unsigned char)
3910Sstevel@tonic-gate 						    NOT_IN_MULTIBYTE_CHAR_CLASS;
3920Sstevel@tonic-gate 					}
3930Sstevel@tonic-gate 					/* leave space for <class_length> */
3940Sstevel@tonic-gate 					compilep += 2;
3950Sstevel@tonic-gate 				}
3960Sstevel@tonic-gate 			} else {
3970Sstevel@tonic-gate 				regexp += char_size;
3980Sstevel@tonic-gate 				if (!multibyte) {
3990Sstevel@tonic-gate 					*compilep = (unsigned char)
4000Sstevel@tonic-gate 					    IN_ASCII_CHAR_CLASS;
4010Sstevel@tonic-gate 				} else {
4020Sstevel@tonic-gate 					*compilep = (unsigned char)
4030Sstevel@tonic-gate 					    IN_MULTIBYTE_CHAR_CLASS;
4040Sstevel@tonic-gate 				}
4050Sstevel@tonic-gate 				/* leave space for <class_length> */
4060Sstevel@tonic-gate 				compilep += 2;
4070Sstevel@tonic-gate 			}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 			/* COMPILE THE CLASS */
4100Sstevel@tonic-gate 			/*
4110Sstevel@tonic-gate 			 * check for a leading right square bracket,
4120Sstevel@tonic-gate 			 * which is allowed
4130Sstevel@tonic-gate 			 */
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 			if (current_char == RIGHT_SQUARE_BRACKET) {
4160Sstevel@tonic-gate 				/*
4170Sstevel@tonic-gate 				 * the leading RIGHT_SQUARE_BRACKET may
4180Sstevel@tonic-gate 				 * be part of a character range
4190Sstevel@tonic-gate 				 * expression like "[]-\]"
4200Sstevel@tonic-gate 				 */
4210Sstevel@tonic-gate 				dash_indicates_range = B_TRUE;
4220Sstevel@tonic-gate 				first_char_in_range = current_char;
4230Sstevel@tonic-gate 				char_size = get_wchar(&current_char, regexp);
4240Sstevel@tonic-gate 				if (char_size <= 0) {
4250Sstevel@tonic-gate 					ERROR_EXIT(&regcmp_lock,
4260Sstevel@tonic-gate 					    arg_listp, compile_startp);
4270Sstevel@tonic-gate 				} else {
4280Sstevel@tonic-gate 					regexp += char_size;
4290Sstevel@tonic-gate 					*compilep = RIGHT_SQUARE_BRACKET;
4300Sstevel@tonic-gate 					compilep++;
4310Sstevel@tonic-gate 				}
4320Sstevel@tonic-gate 			} else {
4330Sstevel@tonic-gate 				/*
4340Sstevel@tonic-gate 				 * decode the character in the following
4350Sstevel@tonic-gate 				 * while loop and decide then if it can
4360Sstevel@tonic-gate 				 * be the first character
4370Sstevel@tonic-gate 				 * in a character range expression
4380Sstevel@tonic-gate 				 */
4390Sstevel@tonic-gate 				dash_indicates_range = B_FALSE;
4400Sstevel@tonic-gate 			}
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 			while (current_char != RIGHT_SQUARE_BRACKET) {
4430Sstevel@tonic-gate 				if (current_char != DASH) {
4440Sstevel@tonic-gate 					/*
4450Sstevel@tonic-gate 					 * if a DASH follows current_char,
4460Sstevel@tonic-gate 					 *  current_char, the DASH and the
4470Sstevel@tonic-gate 					 * character that follows the DASH
4480Sstevel@tonic-gate 					 * may form a character range
4490Sstevel@tonic-gate 					 * expression
4500Sstevel@tonic-gate 					 */
4510Sstevel@tonic-gate 					dash_indicates_range = B_TRUE;
4520Sstevel@tonic-gate 					first_char_in_range = current_char;
4530Sstevel@tonic-gate 					expr_length = add_char(
4540Sstevel@tonic-gate 					    compilep, current_char);
4550Sstevel@tonic-gate 					compilep += expr_length;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 				} else if /* (current_char == DASH) && */
4580Sstevel@tonic-gate 					(dash_indicates_range == B_FALSE) {
4590Sstevel@tonic-gate 					/*
4600Sstevel@tonic-gate 					 * current_char is a DASH, but
4610Sstevel@tonic-gate 					 * either begins the entire
4620Sstevel@tonic-gate 					 * character class or follows a
4630Sstevel@tonic-gate 					 * character that's already
4640Sstevel@tonic-gate 					 * part of a character range
4650Sstevel@tonic-gate 					 * expression, so it simply
4660Sstevel@tonic-gate 					 * represents the DASH character
4670Sstevel@tonic-gate 					 * itself
4680Sstevel@tonic-gate 					 */
4690Sstevel@tonic-gate 					*compilep = DASH;
4700Sstevel@tonic-gate 					compilep ++;
4710Sstevel@tonic-gate 					/*
4720Sstevel@tonic-gate 					 * if another DASH follows this
4730Sstevel@tonic-gate 					 * one, this DASH is part
4740Sstevel@tonic-gate 					 * of a character range expression
4750Sstevel@tonic-gate 					 * like "[--\]"
4760Sstevel@tonic-gate 					 */
4770Sstevel@tonic-gate 					dash_indicates_range = B_TRUE;
4780Sstevel@tonic-gate 					first_char_in_range = current_char;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 				} else /* ((current_char == DASH && */
4810Sstevel@tonic-gate 				/* (dash_indicates_range == B_TRUE)) */ {
4820Sstevel@tonic-gate 					/*
4830Sstevel@tonic-gate 					 * the DASH appears after a single
4840Sstevel@tonic-gate 					 * character that isn't
4850Sstevel@tonic-gate 					 * already part of a character
4860Sstevel@tonic-gate 					 * range expression, so it
4870Sstevel@tonic-gate 					 * and the characters preceding
4880Sstevel@tonic-gate 					 * and following it can form a
4890Sstevel@tonic-gate 					 * character range expression
4900Sstevel@tonic-gate 					 * like "[a-z]"
4910Sstevel@tonic-gate 					 */
4920Sstevel@tonic-gate 					char_size = get_wchar(
4930Sstevel@tonic-gate 					    &current_char, regexp);
4940Sstevel@tonic-gate 					if (char_size <= 0) {
4950Sstevel@tonic-gate 						ERROR_EXIT(&regcmp_lock,
4960Sstevel@tonic-gate 						    arg_listp, compile_startp);
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 					} else if (current_char ==
4990Sstevel@tonic-gate 						RIGHT_SQUARE_BRACKET) {
5000Sstevel@tonic-gate 						/*
5010Sstevel@tonic-gate 						 * the preceding DASH is
5020Sstevel@tonic-gate 						 * the last character in the
5030Sstevel@tonic-gate 						 * class and represents the
5040Sstevel@tonic-gate 						 * DASH character itself
5050Sstevel@tonic-gate 						 */
5060Sstevel@tonic-gate 						*compilep = DASH;
5070Sstevel@tonic-gate 						compilep++;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 					} else if (valid_range(
5100Sstevel@tonic-gate 					    first_char_in_range,
5110Sstevel@tonic-gate 					    current_char) == B_FALSE) {
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 						ERROR_EXIT(&regcmp_lock,
5140Sstevel@tonic-gate 						arg_listp, compile_startp);
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 					} else {
5170Sstevel@tonic-gate 						/*
5180Sstevel@tonic-gate 						 * the DASH is part of a
5190Sstevel@tonic-gate 						 * character range
5200Sstevel@tonic-gate 						 * expression; encode the
5210Sstevel@tonic-gate 						 * rest of the expression
5220Sstevel@tonic-gate 						 */
5230Sstevel@tonic-gate 						regexp += char_size;
5240Sstevel@tonic-gate 						*compilep = (unsigned char)
5250Sstevel@tonic-gate 						    THRU;
5260Sstevel@tonic-gate 						compilep++;
5270Sstevel@tonic-gate 						expr_length = add_char(
5280Sstevel@tonic-gate 						    compilep, current_char);
5290Sstevel@tonic-gate 						compilep += expr_length;
5300Sstevel@tonic-gate 						/*
5310Sstevel@tonic-gate 						 * if a DASH follows this
5320Sstevel@tonic-gate 						 * character range
5330Sstevel@tonic-gate 						 * expression,
5340Sstevel@tonic-gate 						 * it represents the DASH
5350Sstevel@tonic-gate 						 * character itself
5360Sstevel@tonic-gate 						 */
5370Sstevel@tonic-gate 						dash_indicates_range =
5380Sstevel@tonic-gate 						    B_FALSE;
5390Sstevel@tonic-gate 					}
5400Sstevel@tonic-gate 				}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 				/* GET THE NEXT CHARACTER */
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 				char_size = get_wchar(&current_char, regexp);
5450Sstevel@tonic-gate 				if (char_size <= 0) {
5460Sstevel@tonic-gate 					ERROR_EXIT(&regcmp_lock,
5470Sstevel@tonic-gate 					    arg_listp, compile_startp);
5480Sstevel@tonic-gate 				} else {
5490Sstevel@tonic-gate 					regexp += char_size;
5500Sstevel@tonic-gate 				}
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 			}
5530Sstevel@tonic-gate 			/* end while (current_char != RIGHT_SQUARE_BRACKET) */
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 			/* INSERT THE LENGTH OF THE CLASS INTO THE */
5560Sstevel@tonic-gate 			/* COMPILED EXPRESSION */
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 			class_length = (unsigned int)
5590Sstevel@tonic-gate 			    (compilep - regex_typep - 1);
5600Sstevel@tonic-gate 			if ((class_length < 2) ||
5610Sstevel@tonic-gate 			    (class_length > MAX_SINGLE_BYTE_INT)) {
5620Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
5630Sstevel@tonic-gate 				    compile_startp);
5640Sstevel@tonic-gate 			} else {
5650Sstevel@tonic-gate 				*(regex_typep + 1) = (unsigned char)
5660Sstevel@tonic-gate 				    class_length;
5670Sstevel@tonic-gate 			}
5680Sstevel@tonic-gate 			break; /* end case LEFT_SQUARE_BRACKET */
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 		case LEFT_PAREN:
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 			/*
5730Sstevel@tonic-gate 			 * start of a parenthesized group of regular
5740Sstevel@tonic-gate 			 * expressions compiles to <'\0'><'\0'>, leaving
5750Sstevel@tonic-gate 			 * space in the compiled regular expression for
5760Sstevel@tonic-gate 			 * <group_type|ADDED_LENGTH_BITS><group_length>
5770Sstevel@tonic-gate 			 */
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 			if (push_compilep(compilep) == (char *)0) {
5800Sstevel@tonic-gate 				/*
5810Sstevel@tonic-gate 				 * groups can contain groups, so group
5820Sstevel@tonic-gate 				 * start pointers
5830Sstevel@tonic-gate 				 * must be saved and restored in sequence
5840Sstevel@tonic-gate 				 */
5850Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
5860Sstevel@tonic-gate 				    compile_startp);
5870Sstevel@tonic-gate 			} else {
5880Sstevel@tonic-gate 				can_repeat = B_FALSE;
5890Sstevel@tonic-gate 				*compilep = '\0';	/* for debugging */
5900Sstevel@tonic-gate 				compilep++;
5910Sstevel@tonic-gate 				*compilep = '\0';	/* for debugging */
5920Sstevel@tonic-gate 				compilep++;
5930Sstevel@tonic-gate 			}
5940Sstevel@tonic-gate 			break; /* end case LEFT_PAREN */
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 		case RIGHT_PAREN:
5970Sstevel@tonic-gate 			/* end of a marked group of regular expressions */
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 			/*
6000Sstevel@tonic-gate 			 * (<regex>)$0-9 compiles to
6010Sstevel@tonic-gate 			 * <SAVED_GROUP><substringn><compiled_regex...>\
6020Sstevel@tonic-gate 			 * <END_SAVED_GROUP><substringn><return_arg_number>
6030Sstevel@tonic-gate 			 * (<regex>)* compiles to
6040Sstevel@tonic-gate 			 * <ZERO_OR_MORE_GROUP|ADDED_LENGTH_BITS>
6050Sstevel@tonic-gate 			 * <group_length> <compiled_regex...>
6060Sstevel@tonic-gate 			 * <END_GROUP|ZERO_OR_MORE><groupn>
6070Sstevel@tonic-gate 			 * (<regex>)+ compiles to
6080Sstevel@tonic-gate 			 * <ONE_OR_MORE_GROUP|ADDED_LENGTH_BITS>
6090Sstevel@tonic-gate 			 * <group_length>\
6100Sstevel@tonic-gate 			 * <compiled_regex...><END_GROUP|ONE_OR_MORE>
6110Sstevel@tonic-gate 			 * <groupn>
6120Sstevel@tonic-gate 			 * (<regex>){...} compiles to
6130Sstevel@tonic-gate 			 * <COUNTED_GROUP|ADDED_LENGTH_BITS><group_length>\
6140Sstevel@tonic-gate 			 * <compiled_regex...><END_GROUP|COUNT><groupn>\
6150Sstevel@tonic-gate 			 * <minimum_repeat_count><maximum_repeat_count>
6160Sstevel@tonic-gate 			 * otherwise (<regex>) compiles to
6170Sstevel@tonic-gate 			 * <SIMPLE_GROUP><blank><compiled_regex...>
6180Sstevel@tonic-gate 			 * <END_GROUP><groupn>
6190Sstevel@tonic-gate 			 *
6200Sstevel@tonic-gate 			 * NOTE:
6210Sstevel@tonic-gate 			 *
6220Sstevel@tonic-gate 			 * group_length + (256 * ADDED_LENGTH_BITS) ==
6230Sstevel@tonic-gate 			 * length_of(<compiled_regex...><END_GROUP|...>
6240Sstevel@tonic-gate 			 * <groupn>)
6250Sstevel@tonic-gate 			 * which also ==
6260Sstevel@tonic-gate 			 * length_of(<group_type|ADDED_LENGTH_BITS>
6270Sstevel@tonic-gate 			 * <group_length>\ <compiled_regex...>)
6280Sstevel@tonic-gate 			 * groupn no longer seems to be used, but the code
6290Sstevel@tonic-gate 			 * still computes it to preserve backward
6300Sstevel@tonic-gate 			 * compatibility
6310Sstevel@tonic-gate 			 * with earlier versions of regex().
6320Sstevel@tonic-gate 			 */
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 			/* RETRIEVE THE ADDRESS OF THE START OF THE GROUP */
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 			regex_typep = pop_compilep();
6370Sstevel@tonic-gate 			if (regex_typep == (char *)0) {
6380Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
6390Sstevel@tonic-gate 				    compile_startp);
6400Sstevel@tonic-gate 			}
6410Sstevel@tonic-gate 			char_size = get_wchar(&current_char, regexp);
6420Sstevel@tonic-gate 			if (char_size < 0) {
6430Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
6440Sstevel@tonic-gate 				    compile_startp);
6450Sstevel@tonic-gate 			} else if (char_size == 0) {
6460Sstevel@tonic-gate 				*regex_typep = SIMPLE_GROUP;
6470Sstevel@tonic-gate 				can_repeat = B_TRUE;
6480Sstevel@tonic-gate 				*compilep = (unsigned char)END_GROUP;
6490Sstevel@tonic-gate 				regex_typep = compilep;
6500Sstevel@tonic-gate 				compilep++;
6510Sstevel@tonic-gate 				*compilep = (unsigned char)groupn;
6520Sstevel@tonic-gate 				groupn++;
6530Sstevel@tonic-gate 				compilep++;
6540Sstevel@tonic-gate 			} else if (current_char == DOLLAR_SIGN) {
6550Sstevel@tonic-gate 				*regex_typep = SAVED_GROUP;
6560Sstevel@tonic-gate 				regex_typep++;
6570Sstevel@tonic-gate 				*regex_typep = (char)substringn;
6580Sstevel@tonic-gate 				can_repeat = B_FALSE;
6590Sstevel@tonic-gate 				regexp ++;
6600Sstevel@tonic-gate 				return_arg_number = get_digit(regexp);
6610Sstevel@tonic-gate 				if ((return_arg_number < 0) ||
6620Sstevel@tonic-gate 				    (substringn >= NSUBSTRINGS)) {
6630Sstevel@tonic-gate 					ERROR_EXIT(&regcmp_lock, arg_listp,
6640Sstevel@tonic-gate 					    compile_startp);
6650Sstevel@tonic-gate 				}
6660Sstevel@tonic-gate 				regexp++;
6670Sstevel@tonic-gate 				*compilep = (unsigned char)END_SAVED_GROUP;
6680Sstevel@tonic-gate 				compilep++;
6690Sstevel@tonic-gate 				*compilep = (unsigned char)substringn;
6700Sstevel@tonic-gate 				substringn++;
6710Sstevel@tonic-gate 				compilep++;
6720Sstevel@tonic-gate 				*compilep = (unsigned char)return_arg_number;
6730Sstevel@tonic-gate 				compilep++;
6740Sstevel@tonic-gate 			} else {
6750Sstevel@tonic-gate 				switch (current_char) {
6760Sstevel@tonic-gate 				case STAR:
6770Sstevel@tonic-gate 					*regex_typep = ZERO_OR_MORE_GROUP;
6780Sstevel@tonic-gate 					break;
6790Sstevel@tonic-gate 				case PLUS:
6800Sstevel@tonic-gate 					*regex_typep = ONE_OR_MORE_GROUP;
6810Sstevel@tonic-gate 					break;
6820Sstevel@tonic-gate 				case LEFT_CURLY_BRACE:
6830Sstevel@tonic-gate 					*regex_typep = COUNTED_GROUP;
6840Sstevel@tonic-gate 					break;
6850Sstevel@tonic-gate 				default:
6860Sstevel@tonic-gate 					*regex_typep = SIMPLE_GROUP;
6870Sstevel@tonic-gate 				}
6880Sstevel@tonic-gate 				if (*regex_typep != SIMPLE_GROUP) {
6890Sstevel@tonic-gate 					group_length = (unsigned int)
6900Sstevel@tonic-gate 						(compilep - regex_typep);
6910Sstevel@tonic-gate 					if (group_length >= 1024) {
6920Sstevel@tonic-gate 						ERROR_EXIT(&regcmp_lock,
6930Sstevel@tonic-gate 						arg_listp, compile_startp);
6940Sstevel@tonic-gate 					}
6950Sstevel@tonic-gate 					high_bits = group_length >>
6960Sstevel@tonic-gate 					    TIMES_256_SHIFT;
6970Sstevel@tonic-gate 					low_bits = group_length &
6980Sstevel@tonic-gate 					    SINGLE_BYTE_MASK;
6990Sstevel@tonic-gate 					*regex_typep =
7000Sstevel@tonic-gate 					    (unsigned char)
7010Sstevel@tonic-gate 					    ((unsigned int)
7020Sstevel@tonic-gate 						*regex_typep | high_bits);
7030Sstevel@tonic-gate 					regex_typep++;
7040Sstevel@tonic-gate 					*regex_typep =
7050Sstevel@tonic-gate 					    (unsigned char)low_bits;
7060Sstevel@tonic-gate 				}
7070Sstevel@tonic-gate 				can_repeat = B_TRUE;
7080Sstevel@tonic-gate 				*compilep = (unsigned char)END_GROUP;
7090Sstevel@tonic-gate 				regex_typep = compilep;
7100Sstevel@tonic-gate 				compilep++;
7110Sstevel@tonic-gate 				*compilep = (unsigned char)groupn;
7120Sstevel@tonic-gate 				groupn++;
7130Sstevel@tonic-gate 				compilep++;
7140Sstevel@tonic-gate 			}
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 			break; /* end case RIGHT_PAREN */
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 		case STAR: /* zero or more repetitions of the */
7190Sstevel@tonic-gate 				/* preceding expression */
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 			/*
7220Sstevel@tonic-gate 			 * <regex...>* compiles to <regex_type|ZERO_OR_MORE>\
7230Sstevel@tonic-gate 			 * <compiled_regex...>
7240Sstevel@tonic-gate 			 * (<regex...>)* compiles to
7250Sstevel@tonic-gate 			 * <ZERO_OR_MORE_GROUP|ADDED_LENGTH_BITS>\
7260Sstevel@tonic-gate 			 * <group_length><compiled_regex...>\
7270Sstevel@tonic-gate 			 * <END_GROUP|ZERO_OR_MORE><groupn>
7280Sstevel@tonic-gate 			 */
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 			if (can_repeat == B_FALSE) {
7310Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
7320Sstevel@tonic-gate 				    compile_startp);
7330Sstevel@tonic-gate 			} else {
7340Sstevel@tonic-gate 				can_repeat = B_FALSE;
7350Sstevel@tonic-gate 				*regex_typep = (unsigned char)
7360Sstevel@tonic-gate 				((unsigned int)*regex_typep | ZERO_OR_MORE);
7370Sstevel@tonic-gate 			}
7380Sstevel@tonic-gate 			break; /* end case '*' */
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 		case PLUS:
7410Sstevel@tonic-gate 			/* one or more repetitions of the preceding */
7420Sstevel@tonic-gate 				/* expression */
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 			/*
7450Sstevel@tonic-gate 			 * <regex...>+ compiles to <regex_type|ONE_OR_MORE>\
7460Sstevel@tonic-gate 			 * <compiled_regex...> (<regex...>)+ compiles to
7470Sstevel@tonic-gate 			 * <ONE_OR_MORE_GROUP|ADDED_LENGTH_BITS>\
7480Sstevel@tonic-gate 			 * <group_length><compiled_regex...>\
7490Sstevel@tonic-gate 			 * <END_GROUP|ONE_OR_MORE><groupn>
7500Sstevel@tonic-gate 			 */
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 			if (can_repeat == B_FALSE) {
7530Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
7540Sstevel@tonic-gate 					compile_startp);
7550Sstevel@tonic-gate 			} else {
7560Sstevel@tonic-gate 				can_repeat = B_FALSE;
7570Sstevel@tonic-gate 				*regex_typep =
7580Sstevel@tonic-gate 					(unsigned char)((unsigned int)*
7590Sstevel@tonic-gate 					regex_typep | ONE_OR_MORE);
7600Sstevel@tonic-gate 			}
7610Sstevel@tonic-gate 			break; /* end case '+' */
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		case LEFT_CURLY_BRACE:
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 			/*
7660Sstevel@tonic-gate 			 * repeat the preceding regular expression
7670Sstevel@tonic-gate 			 * at least min_count times
7680Sstevel@tonic-gate 			 * and at most max_count times
7690Sstevel@tonic-gate 			 *
7700Sstevel@tonic-gate 			 * <regex...>{min_count} compiles to
7710Sstevel@tonic-gate 			 * <regex type|COUNT><compiled_regex...>
7720Sstevel@tonic-gate 			 * <min_count><min_count>
7730Sstevel@tonic-gate 			 *
7740Sstevel@tonic-gate 			 * <regex...>{min_count,} compiles to
7750Sstevel@tonic-gate 			 * <regex type|COUNT><compiled_regex...>
7760Sstevel@tonic-gate 			 * <min_count><UNLIMITED>
7770Sstevel@tonic-gate 			 *
7780Sstevel@tonic-gate 			 * <regex...>{min_count,max_count} compiles to
7790Sstevel@tonic-gate 			 * <regex type>|COUNT><compiled_regex...>
7800Sstevel@tonic-gate 			 * <min_count><max_count>
7810Sstevel@tonic-gate 			 *
7820Sstevel@tonic-gate 			 * (<regex...>){min_count,max_count} compiles to
7830Sstevel@tonic-gate 			 * <COUNTED_GROUP|ADDED_LENGTH_BITS><group_length>\
7840Sstevel@tonic-gate 			 * <compiled_regex...><END_GROUP|COUNT><groupn>\
7850Sstevel@tonic-gate 			 * <minimum_match_count><maximum_match_count>
7860Sstevel@tonic-gate 			 */
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 			if (can_repeat == B_FALSE) {
7890Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
7900Sstevel@tonic-gate 					compile_startp);
7910Sstevel@tonic-gate 			}
7920Sstevel@tonic-gate 			can_repeat = B_FALSE;
7930Sstevel@tonic-gate 			*regex_typep = (unsigned char)((unsigned int)*
7940Sstevel@tonic-gate 					regex_typep | COUNT);
7950Sstevel@tonic-gate 			count_length = get_count(&min_count, regexp);
7960Sstevel@tonic-gate 			if (count_length <= 0) {
7970Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
7980Sstevel@tonic-gate 					compile_startp);
7990Sstevel@tonic-gate 			}
8000Sstevel@tonic-gate 			regexp += count_length;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 			if (*regexp == RIGHT_CURLY_BRACE) { /* {min_count} */
8030Sstevel@tonic-gate 				regexp++;
8040Sstevel@tonic-gate 				max_count = min_count;
8050Sstevel@tonic-gate 			} else if (*regexp == COMMA) { /* {min_count,..} */
8060Sstevel@tonic-gate 				regexp++;
8070Sstevel@tonic-gate 				/* {min_count,}   */
8080Sstevel@tonic-gate 				if (*regexp == RIGHT_CURLY_BRACE) {
8090Sstevel@tonic-gate 					regexp++;
8100Sstevel@tonic-gate 					max_count = UNLIMITED;
8110Sstevel@tonic-gate 				} else { /* {min_count,max_count} */
8120Sstevel@tonic-gate 					count_length = get_count(
8130Sstevel@tonic-gate 						&max_count, regexp);
8140Sstevel@tonic-gate 					if (count_length <= 0) {
8150Sstevel@tonic-gate 						ERROR_EXIT(&regcmp_lock,
8160Sstevel@tonic-gate 						arg_listp, compile_startp);
8170Sstevel@tonic-gate 					}
8180Sstevel@tonic-gate 					regexp += count_length;
8190Sstevel@tonic-gate 					if (*regexp != RIGHT_CURLY_BRACE) {
8200Sstevel@tonic-gate 						ERROR_EXIT(&regcmp_lock,
8210Sstevel@tonic-gate 						arg_listp, compile_startp);
8220Sstevel@tonic-gate 					}
8230Sstevel@tonic-gate 					regexp++;
8240Sstevel@tonic-gate 				}
8250Sstevel@tonic-gate 			} else { /* invalid expression */
8260Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
8270Sstevel@tonic-gate 					compile_startp);
8280Sstevel@tonic-gate 			}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 			if ((min_count > MAX_SINGLE_BYTE_INT) ||
8310Sstevel@tonic-gate 				((max_count != UNLIMITED) &&
8320Sstevel@tonic-gate 				(min_count > max_count))) {
8330Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
8340Sstevel@tonic-gate 					compile_startp);
8350Sstevel@tonic-gate 			} else {
8360Sstevel@tonic-gate 				*compilep = (unsigned char)min_count;
8370Sstevel@tonic-gate 				compilep++;
8380Sstevel@tonic-gate 				*compilep = (unsigned char)max_count;
8390Sstevel@tonic-gate 				compilep++;
8400Sstevel@tonic-gate 			}
8410Sstevel@tonic-gate 			break; /* end case LEFT_CURLY_BRACE */
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 		default: /* a single non-special character */
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 			/*
8460Sstevel@tonic-gate 			 * compiles to <ASCII_CHAR><ascii_char> or
8470Sstevel@tonic-gate 			 * <MULTIBYTE_CHAR><multibyte_char>
8480Sstevel@tonic-gate 			 */
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 			can_repeat = B_TRUE;
8510Sstevel@tonic-gate 			regex_typep = compilep;
8520Sstevel@tonic-gate 			expr_length = add_single_char_expr(compilep,
8530Sstevel@tonic-gate 					current_char);
8540Sstevel@tonic-gate 			compilep += expr_length;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 		} /* end switch (current_char) */
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 		/* GET THE NEXT CHARACTER FOR THE WHILE LOOP */
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 		char_size = get_wchar(&current_char, regexp);
8610Sstevel@tonic-gate 		if (char_size < 0) {
8620Sstevel@tonic-gate 			ERROR_EXIT(&regcmp_lock, arg_listp, compile_startp);
8630Sstevel@tonic-gate 		} else if (char_size > 0) {
8640Sstevel@tonic-gate 			regexp += char_size;
8650Sstevel@tonic-gate 		} else if /* (char_size == 0) && */ (next_argp != (char *)0) {
8660Sstevel@tonic-gate 			regexp = next_argp;
8670Sstevel@tonic-gate 			next_argp = va_arg(arg_listp, /* const */ char *);
8680Sstevel@tonic-gate 			char_size = get_wchar(&current_char, regexp);
8690Sstevel@tonic-gate 			if (char_size <= 0) {
8700Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
8710Sstevel@tonic-gate 					compile_startp);
8720Sstevel@tonic-gate 			} else {
8730Sstevel@tonic-gate 				regexp += char_size;
8740Sstevel@tonic-gate 			}
8750Sstevel@tonic-gate 		} else /* ((char_size == 0) && (next_argp == (char *)0)) */ {
8760Sstevel@tonic-gate 			if (pop_compilep() != (char *)0) {
8770Sstevel@tonic-gate 				/* unmatched parentheses */
8780Sstevel@tonic-gate 				ERROR_EXIT(&regcmp_lock, arg_listp,
8790Sstevel@tonic-gate 					compile_startp);
8800Sstevel@tonic-gate 			}
8810Sstevel@tonic-gate 			*compilep = (unsigned char)END_REGEX;
8820Sstevel@tonic-gate 			compilep++;
8830Sstevel@tonic-gate 			*compilep = '\0';
8840Sstevel@tonic-gate 			compilep++;
8850Sstevel@tonic-gate 			__i_size = (int)(compilep - compile_startp);
8860Sstevel@tonic-gate 			va_end(arg_listp);
8870Sstevel@tonic-gate 			lmutex_unlock(&regcmp_lock);
8880Sstevel@tonic-gate 			return (compile_startp);
8890Sstevel@tonic-gate 		}
8900Sstevel@tonic-gate 	} /* end for (;;) */
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate } /* regcmp() */
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate /* DEFINITIONS OF PRIVATE FUNCTIONS */
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate static int
add_char(char * compilep,wchar_t wchar)8980Sstevel@tonic-gate add_char(char *compilep, wchar_t wchar)
8990Sstevel@tonic-gate {
9000Sstevel@tonic-gate 	int expr_length;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	if ((unsigned int)wchar <= (unsigned int)0x7f) {
9030Sstevel@tonic-gate 		*compilep = (unsigned char)wchar;
9040Sstevel@tonic-gate 		expr_length = 1;
9050Sstevel@tonic-gate 	} else {
9060Sstevel@tonic-gate 		expr_length = wctomb(compilep, wchar);
9070Sstevel@tonic-gate 	}
9080Sstevel@tonic-gate 	return (expr_length);
9090Sstevel@tonic-gate }
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate static int
add_single_char_expr(char * compilep,wchar_t wchar)9120Sstevel@tonic-gate add_single_char_expr(char *compilep, wchar_t wchar)
9130Sstevel@tonic-gate {
9140Sstevel@tonic-gate 	int expr_length = 0;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	if ((unsigned int)wchar <= (unsigned int)0x7f) {
9170Sstevel@tonic-gate 		*compilep = (unsigned char)ASCII_CHAR;
9180Sstevel@tonic-gate 		compilep++;
9190Sstevel@tonic-gate 		*compilep = (unsigned char)wchar;
9200Sstevel@tonic-gate 		expr_length += 2;
9210Sstevel@tonic-gate 	} else {
9220Sstevel@tonic-gate 		*compilep = (unsigned char)MULTIBYTE_CHAR;
9230Sstevel@tonic-gate 		compilep++;
9240Sstevel@tonic-gate 		expr_length++;
9250Sstevel@tonic-gate 		expr_length += wctomb(compilep, wchar);
9260Sstevel@tonic-gate 	}
9270Sstevel@tonic-gate 	return (expr_length);
9280Sstevel@tonic-gate }
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate static int
get_count(int * countp,const char * regexp)9310Sstevel@tonic-gate get_count(int *countp, const char *regexp)
9320Sstevel@tonic-gate {
9330Sstevel@tonic-gate 	char count_char = '0';
9340Sstevel@tonic-gate 	int count = 0;
9350Sstevel@tonic-gate 	int count_length = 0;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	if (regexp == (char *)0) {
9380Sstevel@tonic-gate 		return ((int)0);
9390Sstevel@tonic-gate 	} else {
9400Sstevel@tonic-gate 		count_char = *regexp;
9410Sstevel@tonic-gate 		while (('0' <= count_char) && (count_char <= '9')) {
9420Sstevel@tonic-gate 			count = (10 * count) + (int)(count_char - '0');
9430Sstevel@tonic-gate 			count_length++;
9440Sstevel@tonic-gate 			regexp++;
9450Sstevel@tonic-gate 			count_char = *regexp;
9460Sstevel@tonic-gate 		}
9470Sstevel@tonic-gate 	}
9480Sstevel@tonic-gate 	*countp = count;
9490Sstevel@tonic-gate 	return (count_length);
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate static int
get_digit(const char * regexp)9530Sstevel@tonic-gate get_digit(const char *regexp)
9540Sstevel@tonic-gate {
9550Sstevel@tonic-gate 	char digit;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	if (regexp == (char *)0) {
9580Sstevel@tonic-gate 		return ((int)-1);
9590Sstevel@tonic-gate 	} else {
9600Sstevel@tonic-gate 		digit = *regexp;
9610Sstevel@tonic-gate 		if (('0' <= digit) && (digit <= '9')) {
9620Sstevel@tonic-gate 			return ((int)(digit - '0'));
9630Sstevel@tonic-gate 		} else {
9640Sstevel@tonic-gate 			return ((int)-1);
9650Sstevel@tonic-gate 		}
9660Sstevel@tonic-gate 	}
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate static int
get_wchar(wchar_t * wcharp,const char * regexp)9700Sstevel@tonic-gate get_wchar(wchar_t *wcharp, const char *regexp)
9710Sstevel@tonic-gate {
9720Sstevel@tonic-gate 	int char_size;
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	if (regexp == (char *)0) {
9750Sstevel@tonic-gate 		char_size = 0;
9760Sstevel@tonic-gate 		*wcharp = (wchar_t)((unsigned int)'\0');
9770Sstevel@tonic-gate 	} else if (*regexp == '\0') {
9780Sstevel@tonic-gate 		char_size = 0;
9790Sstevel@tonic-gate 		*wcharp = (wchar_t)((unsigned int)*regexp);
9800Sstevel@tonic-gate 	} else if ((unsigned char)*regexp <= (unsigned char)0x7f) {
9810Sstevel@tonic-gate 		char_size = 1;
9820Sstevel@tonic-gate 		*wcharp = (wchar_t)((unsigned int)*regexp);
9830Sstevel@tonic-gate 	} else {
9840Sstevel@tonic-gate 		char_size = mbtowc(wcharp, regexp, MB_LEN_MAX);
9850Sstevel@tonic-gate 	}
9860Sstevel@tonic-gate 	return (char_size);
9870Sstevel@tonic-gate }
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate static char *
pop_compilep(void)9900Sstevel@tonic-gate pop_compilep(void)
9910Sstevel@tonic-gate {
9920Sstevel@tonic-gate 	char *compilep;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	if (compilep_stackp >= &compilep_stack[STRINGP_STACK_SIZE]) {
9950Sstevel@tonic-gate 		return ((char *)0);
9960Sstevel@tonic-gate 	} else {
9970Sstevel@tonic-gate 		compilep = *compilep_stackp;
9980Sstevel@tonic-gate 		compilep_stackp++;
9990Sstevel@tonic-gate 		return (compilep);
10000Sstevel@tonic-gate 	}
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate static char *
push_compilep(char * compilep)10040Sstevel@tonic-gate push_compilep(char *compilep)
10050Sstevel@tonic-gate {
10060Sstevel@tonic-gate 	if (compilep_stackp <= &compilep_stack[0]) {
10070Sstevel@tonic-gate 		return ((char *)0);
10080Sstevel@tonic-gate 	} else {
10090Sstevel@tonic-gate 		compilep_stackp--;
10100Sstevel@tonic-gate 		*compilep_stackp = compilep;
10110Sstevel@tonic-gate 		return (compilep);
10120Sstevel@tonic-gate 	}
10130Sstevel@tonic-gate }
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate static boolean_t
valid_range(wchar_t lower_char,wchar_t upper_char)10160Sstevel@tonic-gate valid_range(wchar_t lower_char, wchar_t upper_char)
10170Sstevel@tonic-gate {
10180Sstevel@tonic-gate 	return (((lower_char <= 0x7f) && (upper_char <= 0x7f) &&
10190Sstevel@tonic-gate 	    !iswcntrl(lower_char) && !iswcntrl(upper_char) &&
10200Sstevel@tonic-gate 	    (lower_char < upper_char)) ||
10210Sstevel@tonic-gate 	    (((lower_char & WCHAR_CSMASK) ==
10220Sstevel@tonic-gate 	    (upper_char & WCHAR_CSMASK)) &&
10230Sstevel@tonic-gate 	    (lower_char < upper_char)));
10240Sstevel@tonic-gate }
1025