xref: /onnv-gate/usr/src/cmd/expr/compile.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1995-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*
28*0Sstevel@tonic-gate  * xcompile, xstep, xadvance - simulate compile(3g), step(3g), advance(3g)
29*0Sstevel@tonic-gate  *	using regcomp(3c), regexec(3c) interfaces. This is an XCU4
30*0Sstevel@tonic-gate  *	porting aid. switches out to libgen compile/step if collation
31*0Sstevel@tonic-gate  *	table not present.
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  *	Goal is to work with vi and sed/ed.
34*0Sstevel@tonic-gate  * 	Returns expbuf in dhl format (encoding of first two bytes).
35*0Sstevel@tonic-gate  * 	Note also that this is profoundly single threaded.  You
36*0Sstevel@tonic-gate  *	cannot call compile twice with two separate search strings
37*0Sstevel@tonic-gate  *	because the second call will wipe out the earlier stored string.
38*0Sstevel@tonic-gate  *	This must be fixed, plus a general cleanup should be performed
39*0Sstevel@tonic-gate  *	if this is to be integrated into libc.
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  */
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include <stdio.h>
46*0Sstevel@tonic-gate #include <widec.h>
47*0Sstevel@tonic-gate #include <sys/types.h>
48*0Sstevel@tonic-gate #include <regex.h>
49*0Sstevel@tonic-gate #include <locale.h>
50*0Sstevel@tonic-gate #include <stdlib.h>
51*0Sstevel@tonic-gate #include <locale.h>
52*0Sstevel@tonic-gate #include <string.h>
53*0Sstevel@tonic-gate #include <unistd.h>
54*0Sstevel@tonic-gate #include <regexpr.h>
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /*
57*0Sstevel@tonic-gate  * psuedo compile/step/advance global variables
58*0Sstevel@tonic-gate  */
59*0Sstevel@tonic-gate extern int nbra;
60*0Sstevel@tonic-gate extern char *locs; 		/* for stopping execess recursion */
61*0Sstevel@tonic-gate extern char *loc1;  		/* 1st character which matched RE */
62*0Sstevel@tonic-gate extern char *loc2; 		/* char after lst char in matched RE */
63*0Sstevel@tonic-gate extern char *braslist[]; 	/* start of nbra subexp  */
64*0Sstevel@tonic-gate extern char *braelist[]; 	/* end of nbra subexp    */
65*0Sstevel@tonic-gate extern int regerrno;
66*0Sstevel@tonic-gate extern int reglength;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate int regcomp_flags;		/* interface to specify cflags for regcomp */
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate void regex_comp_free(void *a);
71*0Sstevel@tonic-gate static int dhl_step(const char *str, const char *ep);
72*0Sstevel@tonic-gate static int dhl_advance(const char *str, const char *ep);
73*0Sstevel@tonic-gate static int map_errnos(int);		/* Convert regcomp error */
74*0Sstevel@tonic-gate static int dhl_doit(const char *, const regex_t *, const int flags);
75*0Sstevel@tonic-gate static char * dhl_compile(const char *instr, char *ep, char *endbuf);
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate /*
78*0Sstevel@tonic-gate  * # of sub re's: NOTE: For now limit on bra list defined here
79*0Sstevel@tonic-gate  * but fix is to add maxbra define to to regex.h
80*0Sstevel@tonic-gate  * One problem is that a bigger number is a performance hit since
81*0Sstevel@tonic-gate  * regexec() has a slow initialization loop that goes around SEPSIZE times
82*0Sstevel@tonic-gate  */
83*0Sstevel@tonic-gate #define	SEPSIZE 20
84*0Sstevel@tonic-gate static regmatch_t rm[SEPSIZE];		/* ptr to list of RE matches */
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /*
87*0Sstevel@tonic-gate  * Structure to contain dl encoded first two bytes for vi, plus hold two
88*0Sstevel@tonic-gate  * regex structures, one for advance and one for step.
89*0Sstevel@tonic-gate  */
90*0Sstevel@tonic-gate static struct regex_comp {
91*0Sstevel@tonic-gate 	char 	r_head[2];		/* Header for DL encoding for vi */
92*0Sstevel@tonic-gate 	regex_t r_stp;			/* For use by step */
93*0Sstevel@tonic-gate 	regex_t r_adv;			/* For use by advance */
94*0Sstevel@tonic-gate } reg_comp;
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate /*
97*0Sstevel@tonic-gate  * global value for the size of a regex_comp structure:
98*0Sstevel@tonic-gate  */
99*0Sstevel@tonic-gate size_t regexc_size = sizeof (reg_comp);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate char *
compile(const char * instr,char * expbuf,char * endbuf)103*0Sstevel@tonic-gate compile(const char *instr, char *expbuf, char *endbuf)
104*0Sstevel@tonic-gate {
105*0Sstevel@tonic-gate 	return (dhl_compile(instr, expbuf, endbuf));
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate int
step(const char * instr,const char * expbuf)109*0Sstevel@tonic-gate step(const char *instr, const char *expbuf)
110*0Sstevel@tonic-gate {
111*0Sstevel@tonic-gate 	return (dhl_step(instr, expbuf));
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate int
advance(const char * instr,const char * expbuf)115*0Sstevel@tonic-gate advance(const char *instr, const char *expbuf)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	return (dhl_advance(instr, expbuf));
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate /*
122*0Sstevel@tonic-gate  * the compile and step routines here simulate the old libgen routines of
123*0Sstevel@tonic-gate  * compile/step Re: regexpr(3G). in order to do this, we must assume
124*0Sstevel@tonic-gate  * that expbuf[] consists of the following format:
125*0Sstevel@tonic-gate  *	1) the first two bytes consist of a special encoding - see below.
126*0Sstevel@tonic-gate  *	2) the next part is a regex_t used by regexec()/regcomp() for step
127*0Sstevel@tonic-gate  *	3) the final part is a regex_t used by regexec()/regcomp() for advance
128*0Sstevel@tonic-gate  *
129*0Sstevel@tonic-gate  * the special encoding of the first two bytes is referenced throughout
130*0Sstevel@tonic-gate  * vi. apparently expbuf[0] is set to:
131*0Sstevel@tonic-gate  *	= 0 upon initialization
132*0Sstevel@tonic-gate  *	= 1 if the first char of the RE is a ^
133*0Sstevel@tonic-gate  *	= 0 if the first char of the RE isn't a ^
134*0Sstevel@tonic-gate  * and expbuf[1-35+]	= bitmap of the type of RE chars in the expression.
135*0Sstevel@tonic-gate  * this is apparently 0 if there's no RE.
136*0Sstevel@tonic-gate  * Here, we use expbuf[0] in a similar fashion; and expbuf[1] is non-zero
137*0Sstevel@tonic-gate  * if there's at least 1 RE in the string.
138*0Sstevel@tonic-gate  * I say "apparently" as the code to compile()/step() is poorly written.
139*0Sstevel@tonic-gate  */
140*0Sstevel@tonic-gate static char *
dhl_compile(instr,expbuf,endbuf)141*0Sstevel@tonic-gate dhl_compile(instr, expbuf, endbuf)
142*0Sstevel@tonic-gate const char *instr;		/* the regular expression		*/
143*0Sstevel@tonic-gate char *expbuf;			/* where the compiled RE gets placed	*/
144*0Sstevel@tonic-gate char *endbuf;			/* ending addr of expbuf		*/
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate 	int rv;
147*0Sstevel@tonic-gate 	int alloc = 0;
148*0Sstevel@tonic-gate 	char adv_instr[4096];	/* PLENTY big temp buffer */
149*0Sstevel@tonic-gate 	char *instrp;		/* PLENTY big temp buffer */
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	if (*instr == (char) NULL) {
152*0Sstevel@tonic-gate 		regerrno = 41;
153*0Sstevel@tonic-gate 		return (NULL);
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	/*
157*0Sstevel@tonic-gate 	 * Check values of expbuf and endbuf
158*0Sstevel@tonic-gate 	 */
159*0Sstevel@tonic-gate 	if (expbuf == NULL) {
160*0Sstevel@tonic-gate 		if ((expbuf = malloc(regexc_size)) == NULL) {
161*0Sstevel@tonic-gate 			regerrno = 50;
162*0Sstevel@tonic-gate 			return (NULL);
163*0Sstevel@tonic-gate 		}
164*0Sstevel@tonic-gate 		memset(&reg_comp, 0, regexc_size);
165*0Sstevel@tonic-gate 		alloc = 1;
166*0Sstevel@tonic-gate 		endbuf = expbuf + regexc_size;
167*0Sstevel@tonic-gate 	} else {		/* Check if enough memory was allocated */
168*0Sstevel@tonic-gate 		if (expbuf + regexc_size > endbuf) {
169*0Sstevel@tonic-gate 			regerrno = 50;
170*0Sstevel@tonic-gate 			return (NULL);
171*0Sstevel@tonic-gate 		}
172*0Sstevel@tonic-gate 		memcpy(&reg_comp, expbuf, regexc_size);
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	/*
176*0Sstevel@tonic-gate 	 * Clear global flags
177*0Sstevel@tonic-gate 	 */
178*0Sstevel@tonic-gate 	nbra = 0;
179*0Sstevel@tonic-gate 	regerrno = 0;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	/*
182*0Sstevel@tonic-gate 	 * Free any data being held for previous search strings
183*0Sstevel@tonic-gate 	 */
184*0Sstevel@tonic-gate 	regex_comp_free(&reg_comp);
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	/*
187*0Sstevel@tonic-gate 	 * We call regcomp twice, once to get a regex_t for use by step()
188*0Sstevel@tonic-gate 	 * and then again with for use by advance()
189*0Sstevel@tonic-gate 	 */
190*0Sstevel@tonic-gate 	if ((rv = regcomp(&reg_comp.r_stp, instr, regcomp_flags)) != 0) {
191*0Sstevel@tonic-gate 		regerrno = map_errnos(rv);	/* Convert regcomp error */
192*0Sstevel@tonic-gate 		goto out;
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 	/*
195*0Sstevel@tonic-gate 	 * To support advance, which assumes an implicit ^ to match at start
196*0Sstevel@tonic-gate 	 * of line we prepend a ^ to the pattern by copying to a temp buffer
197*0Sstevel@tonic-gate 	 */
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	if (instr[0] == '^')
200*0Sstevel@tonic-gate 		instrp = (char *) instr; /* String already has leading ^ */
201*0Sstevel@tonic-gate 	else {
202*0Sstevel@tonic-gate 		adv_instr[0] = '^';
203*0Sstevel@tonic-gate 		strncpy(&adv_instr[1], instr, 2048);
204*0Sstevel@tonic-gate 		instrp = adv_instr;
205*0Sstevel@tonic-gate 	}
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	if ((rv = regcomp(&reg_comp.r_adv, instrp, regcomp_flags)) != 0) {
208*0Sstevel@tonic-gate 		regerrno = map_errnos(rv);	/* Convert regcomp error */
209*0Sstevel@tonic-gate 		goto out;
210*0Sstevel@tonic-gate 	}
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	/*
213*0Sstevel@tonic-gate 	 * update global variables
214*0Sstevel@tonic-gate 	 */
215*0Sstevel@tonic-gate 	nbra = (int) reg_comp.r_adv.re_nsub > 0 ?
216*0Sstevel@tonic-gate 	    (int) reg_comp.r_adv.re_nsub : 0;
217*0Sstevel@tonic-gate 	regerrno = 0;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	/*
220*0Sstevel@tonic-gate 	 * Set the header flags for use by vi
221*0Sstevel@tonic-gate 	 */
222*0Sstevel@tonic-gate 	if (instr[0] == '^') 		/* if beginning of string,	*/
223*0Sstevel@tonic-gate 		reg_comp.r_head[0] = 1;	/* set special flag		*/
224*0Sstevel@tonic-gate 	else
225*0Sstevel@tonic-gate 		reg_comp.r_head[0] = 0;	/* clear special flag		*/
226*0Sstevel@tonic-gate 	/*
227*0Sstevel@tonic-gate 	 * note that for a single BRE, nbra will be 0 here.
228*0Sstevel@tonic-gate 	 * we're guaranteed that, at this point, a RE has been found.
229*0Sstevel@tonic-gate 	 */
230*0Sstevel@tonic-gate 	reg_comp.r_head[1] = 1;	/* set special flag		*/
231*0Sstevel@tonic-gate 	/*
232*0Sstevel@tonic-gate 	 * Copy our reg_comp structure to expbuf
233*0Sstevel@tonic-gate 	 */
234*0Sstevel@tonic-gate 	(void) memcpy(expbuf, (char *) &reg_comp, regexc_size);
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate out:
237*0Sstevel@tonic-gate 	/*
238*0Sstevel@tonic-gate 	 * Return code from libgen regcomp with mods.  Note weird return
239*0Sstevel@tonic-gate 	 * value - if space is malloc'd return pointer to start of space,
240*0Sstevel@tonic-gate 	 * if user provided his own space, return pointer to 1+last byte
241*0Sstevel@tonic-gate 	 * of his space.
242*0Sstevel@tonic-gate 	 */
243*0Sstevel@tonic-gate 	if (regerrno != 0) {
244*0Sstevel@tonic-gate 		if (alloc)
245*0Sstevel@tonic-gate 			free(expbuf);
246*0Sstevel@tonic-gate 		return (NULL);
247*0Sstevel@tonic-gate 	}
248*0Sstevel@tonic-gate 	reglength = regexc_size;
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	if (alloc)
251*0Sstevel@tonic-gate 		return (expbuf);
252*0Sstevel@tonic-gate 	else
253*0Sstevel@tonic-gate 		return (expbuf + regexc_size);
254*0Sstevel@tonic-gate }
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate /*
258*0Sstevel@tonic-gate  * dhl_step: step through a string until a RE match is found, or end of str
259*0Sstevel@tonic-gate  */
260*0Sstevel@tonic-gate static int
dhl_step(str,ep)261*0Sstevel@tonic-gate dhl_step(str, ep)
262*0Sstevel@tonic-gate const char *str;		/* characters to be checked for a match	*/
263*0Sstevel@tonic-gate const char *ep;			/* compiled RE from dhl_compile()	*/
264*0Sstevel@tonic-gate {
265*0Sstevel@tonic-gate 	/*
266*0Sstevel@tonic-gate 	 * Check if we're passed a null ep
267*0Sstevel@tonic-gate 	 */
268*0Sstevel@tonic-gate 	if (ep == NULL) {
269*0Sstevel@tonic-gate 		regerrno = 41;	/* No remembered search string error */
270*0Sstevel@tonic-gate 		return (0);
271*0Sstevel@tonic-gate 	}
272*0Sstevel@tonic-gate 	/*
273*0Sstevel@tonic-gate 	 * Call common routine with r_stp (step) structure
274*0Sstevel@tonic-gate 	 */
275*0Sstevel@tonic-gate 	return (dhl_doit(str, &(((struct regex_comp *) ep)->r_stp),
276*0Sstevel@tonic-gate 	    ((locs != NULL) ? REG_NOTBOL : 0)));
277*0Sstevel@tonic-gate }
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate /*
280*0Sstevel@tonic-gate  * dhl_advance: implement advance
281*0Sstevel@tonic-gate  */
282*0Sstevel@tonic-gate static int
dhl_advance(str,ep)283*0Sstevel@tonic-gate dhl_advance(str, ep)
284*0Sstevel@tonic-gate const char *str;		/* characters to be checked for a match	*/
285*0Sstevel@tonic-gate const char *ep;			/* compiled RE from dhl_compile()	*/
286*0Sstevel@tonic-gate {
287*0Sstevel@tonic-gate 	int rv;
288*0Sstevel@tonic-gate 	/*
289*0Sstevel@tonic-gate 	 * Check if we're passed a null ep
290*0Sstevel@tonic-gate 	 */
291*0Sstevel@tonic-gate 	if (ep == NULL) {
292*0Sstevel@tonic-gate 		regerrno = 41;	/* No remembered search string error */
293*0Sstevel@tonic-gate 		return (0);
294*0Sstevel@tonic-gate 	}
295*0Sstevel@tonic-gate 	/*
296*0Sstevel@tonic-gate 	 * Call common routine with r_adv (advance) structure
297*0Sstevel@tonic-gate 	 */
298*0Sstevel@tonic-gate 	rv = dhl_doit(str, &(((struct regex_comp *) ep)->r_adv), 0);
299*0Sstevel@tonic-gate 	loc1 = NULL;		/* Clear it per the compile man page */
300*0Sstevel@tonic-gate 	return (rv);
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate /*
304*0Sstevel@tonic-gate  * dhl_doit - common code for step and advance
305*0Sstevel@tonic-gate  */
306*0Sstevel@tonic-gate static int
dhl_doit(str,rep,flags)307*0Sstevel@tonic-gate dhl_doit(str, rep, flags)
308*0Sstevel@tonic-gate const char *str;		/* characters to be checked for a match	*/
309*0Sstevel@tonic-gate const regex_t *rep;
310*0Sstevel@tonic-gate const int flags;		/* flags to be passed to regexec directly */
311*0Sstevel@tonic-gate {
312*0Sstevel@tonic-gate 	int rv;
313*0Sstevel@tonic-gate 	int i;
314*0Sstevel@tonic-gate 	regmatch_t *prm;	/* ptr to current regmatch_t		*/
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	/*
317*0Sstevel@tonic-gate 	 * Check if we're passed a null regex_t
318*0Sstevel@tonic-gate 	 */
319*0Sstevel@tonic-gate 	if (rep == NULL) {
320*0Sstevel@tonic-gate 		regerrno = 41;	/* No remembered search string error */
321*0Sstevel@tonic-gate 		return (0);
322*0Sstevel@tonic-gate 	}
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 	regerrno = 0;
325*0Sstevel@tonic-gate 	prm = &rm[0];
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	if ((rv = regexec(rep, str, SEPSIZE, prm, flags)) != REG_OK) {
328*0Sstevel@tonic-gate 		if (rv == REG_NOMATCH)
329*0Sstevel@tonic-gate 			return (0);
330*0Sstevel@tonic-gate 		regerrno = map_errnos(rv);
331*0Sstevel@tonic-gate 		return (0);
332*0Sstevel@tonic-gate 	}
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 	loc1 = (char *)str + prm->rm_so;
335*0Sstevel@tonic-gate 	loc2 = (char *)str + prm->rm_eo;
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	/*
338*0Sstevel@tonic-gate 	 * Now we need to fill up the bra lists with all of the sub re's
339*0Sstevel@tonic-gate 	 * Note we subtract nsub -1, and preincrement prm.
340*0Sstevel@tonic-gate 	 */
341*0Sstevel@tonic-gate 	for (i = 0; i <= rep->re_nsub; i++) {
342*0Sstevel@tonic-gate 		prm++;		/* XXX inc past first subexp */
343*0Sstevel@tonic-gate 		braslist[i] = (char *)str + prm->rm_so;
344*0Sstevel@tonic-gate 		braelist[i] = (char *)str + prm->rm_eo;
345*0Sstevel@tonic-gate 		if (i >= SEPSIZE) {
346*0Sstevel@tonic-gate 			regerrno = 50; 	/* regex overflow */
347*0Sstevel@tonic-gate 			return (0);
348*0Sstevel@tonic-gate 		}
349*0Sstevel@tonic-gate 	}
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	/*
352*0Sstevel@tonic-gate 	 * Inverse logic, a zero from regexec - success, is a 1
353*0Sstevel@tonic-gate 	 * from advance/step.
354*0Sstevel@tonic-gate 	 */
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 	return (rv == 0);
357*0Sstevel@tonic-gate }
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate /*
361*0Sstevel@tonic-gate  *	regerrno to compile/step error mapping:
362*0Sstevel@tonic-gate  *	This is really a big compromise.  Some errors don't map at all
363*0Sstevel@tonic-gate  *	like regcomp error 15 is generated by both compile() error types
364*0Sstevel@tonic-gate  *  	44 & 46.  So which one should we map to?
365*0Sstevel@tonic-gate  *	Note REG_ESUB Can't happen- 9 is no longer max num of subexpressions
366*0Sstevel@tonic-gate  *	To do your errors right use xregerr() to get the regcomp error
367*0Sstevel@tonic-gate  *	string and print that.
368*0Sstevel@tonic-gate  *
369*0Sstevel@tonic-gate  * |	regcomp/regexec		     | 	Compile/step/advance		    |
370*0Sstevel@tonic-gate  * +---------------------------------+--------------------------------------+
371*0Sstevel@tonic-gate  * 0 REG_OK	  Pattern matched	1  - Pattern matched
372*0Sstevel@tonic-gate  * 1 REG_NOMATCH  No match		0  - Pattern didn't match
373*0Sstevel@tonic-gate  * 2 REG_ECOLLATE Bad collation elmnt.	67 - Returned by compile on mbtowc err
374*0Sstevel@tonic-gate  * 3 REG_EESCAPE  trailing \ in patrn	45 - } expected after \.
375*0Sstevel@tonic-gate  * 4 REG_ENEWLINE \n before end pattrn	36 - Illegal or missing delimiter.
376*0Sstevel@tonic-gate  * 5 REG_ENSUB	  Over 9 \( \) pairs 	43 - Too many \(
377*0Sstevel@tonic-gate  * 6 REG_ESUBREG  Bad number in \[0-9]  25 - ``\digit'' out of range.
378*0Sstevel@tonic-gate  * 7 REG_EBRACK   [ ] inbalance		49 - [ ] imbalance.
379*0Sstevel@tonic-gate  * 8 REG_EPAREN   ( ) inbalance         42 - \(~\) imbalance.
380*0Sstevel@tonic-gate  * 9 REG_EBRACE   \{ \} inbalance       45 - } expected after \.
381*0Sstevel@tonic-gate  * 10 REG_ERANGE  bad range endpoint	11 - Range endpoint too large.
382*0Sstevel@tonic-gate  * 11 REG_ESPACE  no memory for pattern 50 - Regular expression overflow.
383*0Sstevel@tonic-gate  * 12 REG_BADRPT  invalid repetition	36 - Illegal or missing delimiter.
384*0Sstevel@tonic-gate  * 13 REG_ECTYPE  invalid char-class    67 - illegal byte sequence
385*0Sstevel@tonic-gate  * 14 REG_BADPAT  syntax error		50 - Regular expression overflow.
386*0Sstevel@tonic-gate  * 15 REG_BADBR   \{ \} contents bad	46 - First number exceeds 2nd in \{~\}
387*0Sstevel@tonic-gate  * 16 REG_EFATAL  internal error	50 - Regular expression overflow.
388*0Sstevel@tonic-gate  * 17 REG_ECHAR   bad mulitbyte char	67 - illegal byte sequence
389*0Sstevel@tonic-gate  * 18 REG_STACK   stack overflow	50 - Regular expression overflow.
390*0Sstevel@tonic-gate  * 19 REG_ENOSYS  function not supported 50- Regular expression overflow.
391*0Sstevel@tonic-gate  *
392*0Sstevel@tonic-gate  *	For reference here's the compile/step errno's. We don't generate
393*0Sstevel@tonic-gate  *	41 here - it's done earlier, nor 44 since we can't tell if from 46.
394*0Sstevel@tonic-gate  *
395*0Sstevel@tonic-gate  *	11 - Range endpoint too large.
396*0Sstevel@tonic-gate  *	16 - Bad number.
397*0Sstevel@tonic-gate  *	25 - ``\digit'' out of range.
398*0Sstevel@tonic-gate  *	36 - Illegal or missing delimiter.
399*0Sstevel@tonic-gate  *	41 - No remembered search string.
400*0Sstevel@tonic-gate  *	42 - \(~\) imbalance.
401*0Sstevel@tonic-gate  *	43 - Too many \(.
402*0Sstevel@tonic-gate  *	44 - More than 2 numbers given in "\{~\}"
403*0Sstevel@tonic-gate  *	45 - } expected after \.
404*0Sstevel@tonic-gate  *	46 - First number exceeds 2nd in "\{~\}"
405*0Sstevel@tonic-gate  *	49 - [ ] imbalance.
406*0Sstevel@tonic-gate  *	50 - Regular expression overflow.
407*0Sstevel@tonic-gate  */
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate static int
map_errnos(int Errno)410*0Sstevel@tonic-gate map_errnos(int Errno)
411*0Sstevel@tonic-gate {
412*0Sstevel@tonic-gate 	switch (Errno) {
413*0Sstevel@tonic-gate 	case REG_ECOLLATE:
414*0Sstevel@tonic-gate 		regerrno = 67;
415*0Sstevel@tonic-gate 		break;
416*0Sstevel@tonic-gate 	case REG_EESCAPE:
417*0Sstevel@tonic-gate 		regerrno = 45;
418*0Sstevel@tonic-gate 		break;
419*0Sstevel@tonic-gate 	case REG_ENEWLINE:
420*0Sstevel@tonic-gate 		regerrno = 36;
421*0Sstevel@tonic-gate 		break;
422*0Sstevel@tonic-gate 	case REG_ENSUB:
423*0Sstevel@tonic-gate 		regerrno = 43;
424*0Sstevel@tonic-gate 		break;
425*0Sstevel@tonic-gate 	case REG_ESUBREG:
426*0Sstevel@tonic-gate 		regerrno = 25;
427*0Sstevel@tonic-gate 		break;
428*0Sstevel@tonic-gate 	case REG_EBRACK:
429*0Sstevel@tonic-gate 		regerrno = 49;
430*0Sstevel@tonic-gate 		break;
431*0Sstevel@tonic-gate 	case REG_EPAREN:
432*0Sstevel@tonic-gate 		regerrno = 42;
433*0Sstevel@tonic-gate 		break;
434*0Sstevel@tonic-gate 	case REG_EBRACE:
435*0Sstevel@tonic-gate 		regerrno = 45;
436*0Sstevel@tonic-gate 		break;
437*0Sstevel@tonic-gate 	case REG_ERANGE:
438*0Sstevel@tonic-gate 		regerrno = 11;
439*0Sstevel@tonic-gate 		break;
440*0Sstevel@tonic-gate 	case REG_ESPACE:
441*0Sstevel@tonic-gate 		regerrno = 50;
442*0Sstevel@tonic-gate 		break;
443*0Sstevel@tonic-gate 	case REG_BADRPT:
444*0Sstevel@tonic-gate 		regerrno = 36;
445*0Sstevel@tonic-gate 		break;
446*0Sstevel@tonic-gate 	case REG_ECTYPE:
447*0Sstevel@tonic-gate 		regerrno = 67;
448*0Sstevel@tonic-gate 		break;
449*0Sstevel@tonic-gate 	case REG_BADPAT:
450*0Sstevel@tonic-gate 		regerrno = 50;
451*0Sstevel@tonic-gate 		break;
452*0Sstevel@tonic-gate 	case REG_BADBR:
453*0Sstevel@tonic-gate 		regerrno = 46;
454*0Sstevel@tonic-gate 		break;
455*0Sstevel@tonic-gate 	case REG_EFATAL:
456*0Sstevel@tonic-gate 		regerrno = 50;
457*0Sstevel@tonic-gate 		break;
458*0Sstevel@tonic-gate 	case REG_ECHAR:
459*0Sstevel@tonic-gate 		regerrno = 67;
460*0Sstevel@tonic-gate 		break;
461*0Sstevel@tonic-gate 	case REG_STACK:
462*0Sstevel@tonic-gate 		regerrno = 50;
463*0Sstevel@tonic-gate 		break;
464*0Sstevel@tonic-gate 	case REG_ENOSYS:
465*0Sstevel@tonic-gate 		regerrno = 50;
466*0Sstevel@tonic-gate 		break;
467*0Sstevel@tonic-gate 	default:
468*0Sstevel@tonic-gate 		regerrno = 50;
469*0Sstevel@tonic-gate 		break;
470*0Sstevel@tonic-gate 	}
471*0Sstevel@tonic-gate 	return (regerrno);
472*0Sstevel@tonic-gate }
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate /*
475*0Sstevel@tonic-gate  *  This is a routine to clean up the subtle substructure of the struct
476*0Sstevel@tonic-gate  *  regex_comp type for use by clients of this module.  Since the struct
477*0Sstevel@tonic-gate  *  type is private, we use a generic interface, and trust the
478*0Sstevel@tonic-gate  *  application to be damn sure that this operation is valid for the
479*0Sstevel@tonic-gate  *  named memory.
480*0Sstevel@tonic-gate  */
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate void
regex_comp_free(void * a)483*0Sstevel@tonic-gate regex_comp_free(void * a)
484*0Sstevel@tonic-gate {
485*0Sstevel@tonic-gate 	/*
486*0Sstevel@tonic-gate 	 * Free any data being held for previous search strings
487*0Sstevel@tonic-gate 	 */
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 	if (((struct regex_comp *) a) == NULL) {
490*0Sstevel@tonic-gate 		return;
491*0Sstevel@tonic-gate 	}
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 	regfree(&((struct regex_comp *)a)->r_stp);
494*0Sstevel@tonic-gate 	regfree(&((struct regex_comp *)a)->r_adv);
495*0Sstevel@tonic-gate }
496