17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 1995-2003 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
2548bbca8Daniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bdstevel@tonic-gate */
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate/*
297c478bdstevel@tonic-gate * xcompile, xstep, xadvance - simulate compile(3g), step(3g), advance(3g)
307c478bdstevel@tonic-gate *	using regcomp(3c), regexec(3c) interfaces. This is an XCU4
317c478bdstevel@tonic-gate *	porting aid. switches out to libgen compile/step if collation
327c478bdstevel@tonic-gate *	table not present.
337c478bdstevel@tonic-gate *
347c478bdstevel@tonic-gate *	Goal is to work with vi and sed/ed.
35023a3eeToomas Soome *	Returns expbuf in dhl format (encoding of first two bytes).
36023a3eeToomas Soome *	Note also that this is profoundly single threaded.  You
377c478bdstevel@tonic-gate *	cannot call compile twice with two separate search strings
387c478bdstevel@tonic-gate *	because the second call will wipe out the earlier stored string.
397c478bdstevel@tonic-gate *	This must be fixed, plus a general cleanup should be performed
407c478bdstevel@tonic-gate *	if this is to be integrated into libc.
417c478bdstevel@tonic-gate *
427c478bdstevel@tonic-gate */
437c478bdstevel@tonic-gate
447c478bdstevel@tonic-gate#include <stdio.h>
457c478bdstevel@tonic-gate#include <widec.h>
467c478bdstevel@tonic-gate#include <sys/types.h>
477c478bdstevel@tonic-gate#include <regex.h>
487c478bdstevel@tonic-gate#include <locale.h>
497c478bdstevel@tonic-gate#include <stdlib.h>
507c478bdstevel@tonic-gate#include <locale.h>
517c478bdstevel@tonic-gate#include <string.h>
527c478bdstevel@tonic-gate#include <unistd.h>
537c478bdstevel@tonic-gate#include <regexpr.h>
547c478bdstevel@tonic-gate
557c478bdstevel@tonic-gate/*
567c478bdstevel@tonic-gate * psuedo compile/step/advance global variables
577c478bdstevel@tonic-gate */
587c478bdstevel@tonic-gateextern int nbra;
59023a3eeToomas Soomeextern char *locs;		/* for stopping execess recursion */
60023a3eeToomas Soomeextern char *loc1;		/* 1st character which matched RE */
61023a3eeToomas Soomeextern char *loc2;		/* char after lst char in matched RE */
62023a3eeToomas Soomeextern char *braslist[];	/* start of nbra subexp  */
63023a3eeToomas Soomeextern char *braelist[];	/* end of nbra subexp    */
647c478bdstevel@tonic-gateextern int regerrno;
657c478bdstevel@tonic-gateextern int reglength;
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gateint regcomp_flags;		/* interface to specify cflags for regcomp */
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gatevoid regex_comp_free(void *a);
707c478bdstevel@tonic-gatestatic int dhl_step(const char *str, const char *ep);
717c478bdstevel@tonic-gatestatic int dhl_advance(const char *str, const char *ep);
727c478bdstevel@tonic-gatestatic int map_errnos(int);		/* Convert regcomp error */
737c478bdstevel@tonic-gatestatic int dhl_doit(const char *, const regex_t *, const int flags);
74023a3eeToomas Soomestatic char *dhl_compile(const char *instr, char *ep, char *endbuf);
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gate/*
777c478bdstevel@tonic-gate * # of sub re's: NOTE: For now limit on bra list defined here
787c478bdstevel@tonic-gate * but fix is to add maxbra define to to regex.h
797c478bdstevel@tonic-gate * One problem is that a bigger number is a performance hit since
807c478bdstevel@tonic-gate * regexec() has a slow initialization loop that goes around SEPSIZE times
817c478bdstevel@tonic-gate */
827c478bdstevel@tonic-gate#define	SEPSIZE 20
837c478bdstevel@tonic-gatestatic regmatch_t rm[SEPSIZE];		/* ptr to list of RE matches */
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate/*
867c478bdstevel@tonic-gate * Structure to contain dl encoded first two bytes for vi, plus hold two
877c478bdstevel@tonic-gate * regex structures, one for advance and one for step.
887c478bdstevel@tonic-gate */
897c478bdstevel@tonic-gatestatic struct regex_comp {
90023a3eeToomas Soome	char	r_head[2];		/* Header for DL encoding for vi */
917c478bdstevel@tonic-gate	regex_t r_stp;			/* For use by step */
927c478bdstevel@tonic-gate	regex_t r_adv;			/* For use by advance */
937c478bdstevel@tonic-gate} reg_comp;
947c478bdstevel@tonic-gate
957c478bdstevel@tonic-gate/*
967c478bdstevel@tonic-gate * global value for the size of a regex_comp structure:
977c478bdstevel@tonic-gate */
987c478bdstevel@tonic-gatesize_t regexc_size = sizeof (reg_comp);
997c478bdstevel@tonic-gate
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gatechar *
1027c478bdstevel@tonic-gatecompile(const char *instr, char *expbuf, char *endbuf)
1037c478bdstevel@tonic-gate{
1047c478bdstevel@tonic-gate	return (dhl_compile(instr, expbuf, endbuf));
1057c478bdstevel@tonic-gate}
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gateint
1087c478bdstevel@tonic-gatestep(const char *instr, const char *expbuf)
1097c478bdstevel@tonic-gate{
1107c478bdstevel@tonic-gate	return (dhl_step(instr, expbuf));
1117c478bdstevel@tonic-gate}
1127c478bdstevel@tonic-gate
1137c478bdstevel@tonic-gateint
1147c478bdstevel@tonic-gateadvance(const char *instr, const char *expbuf)
1157c478bdstevel@tonic-gate{
1167c478bdstevel@tonic-gate	return (dhl_advance(instr, expbuf));
1177c478bdstevel@tonic-gate}
1187c478bdstevel@tonic-gate
1197c478bdstevel@tonic-gate
1207c478bdstevel@tonic-gate/*
1217c478bdstevel@tonic-gate * the compile and step routines here simulate the old libgen routines of
1227c478bdstevel@tonic-gate * compile/step Re: regexpr(3G). in order to do this, we must assume
1237c478bdstevel@tonic-gate * that expbuf[] consists of the following format:
1247c478bdstevel@tonic-gate *	1) the first two bytes consist of a special encoding - see below.
1257c478bdstevel@tonic-gate *	2) the next part is a regex_t used by regexec()/regcomp() for step
1267c478bdstevel@tonic-gate *	3) the final part is a regex_t used by regexec()/regcomp() for advance
1277c478bdstevel@tonic-gate *
1287c478bdstevel@tonic-gate * the special encoding of the first two bytes is referenced throughout
1297c478bdstevel@tonic-gate * vi. apparently expbuf[0] is set to:
1307c478bdstevel@tonic-gate *	= 0 upon initialization
1317c478bdstevel@tonic-gate *	= 1 if the first char of the RE is a ^
1327c478bdstevel@tonic-gate *	= 0 if the first char of the RE isn't a ^
1337c478bdstevel@tonic-gate * and expbuf[1-35+]	= bitmap of the type of RE chars in the expression.
1347c478bdstevel@tonic-gate * this is apparently 0 if there's no RE.
1357c478bdstevel@tonic-gate * Here, we use expbuf[0] in a similar fashion; and expbuf[1] is non-zero
1367c478bdstevel@tonic-gate * if there's at least 1 RE in the string.
1377c478bdstevel@tonic-gate * I say "apparently" as the code to compile()/step() is poorly written.
1387c478bdstevel@tonic-gate */
1397c478bdstevel@tonic-gatestatic char *
140023a3eeToomas Soomedhl_compile(const char *instr,	/* the regular expression		*/
141023a3eeToomas Soome    char *expbuf,		/* where the compiled RE gets placed	*/
142023a3eeToomas Soome    char *endbuf)		/* ending addr of expbuf		*/
1437c478bdstevel@tonic-gate{
1447c478bdstevel@tonic-gate	int rv;
1457c478bdstevel@tonic-gate	int alloc = 0;
1467c478bdstevel@tonic-gate	char adv_instr[4096];	/* PLENTY big temp buffer */
1477c478bdstevel@tonic-gate	char *instrp;		/* PLENTY big temp buffer */
1487c478bdstevel@tonic-gate
149023a3eeToomas Soome	if (*instr == '\0') {
1507c478bdstevel@tonic-gate		regerrno = 41;
1517c478bdstevel@tonic-gate		return (NULL);
1527c478bdstevel@tonic-gate	}
1537c478bdstevel@tonic-gate
1547c478bdstevel@tonic-gate	/*
1557c478bdstevel@tonic-gate	 * Check values of expbuf and endbuf
1567c478bdstevel@tonic-gate	 */
1577c478bdstevel@tonic-gate	if (expbuf == NULL) {
1587c478bdstevel@tonic-gate		if ((expbuf = malloc(regexc_size)) == NULL) {
1597c478bdstevel@tonic-gate			regerrno = 50;
1607c478bdstevel@tonic-gate			return (NULL);
1617c478bdstevel@tonic-gate		}
1627c478bdstevel@tonic-gate		memset(&reg_comp, 0, regexc_size);
1637c478bdstevel@tonic-gate		alloc = 1;
1647c478bdstevel@tonic-gate		endbuf = expbuf + regexc_size;
1657c478bdstevel@tonic-gate	} else {		/* Check if enough memory was allocated */
1667c478bdstevel@tonic-gate		if (expbuf + regexc_size > endbuf) {
1677c478bdstevel@tonic-gate			regerrno = 50;
1687c478bdstevel@tonic-gate			return (NULL);
1697c478bdstevel@tonic-gate		}
1707c478bdstevel@tonic-gate		memcpy(&reg_comp, expbuf, regexc_size);
1717c478bdstevel@tonic-gate	}
1727c478bdstevel@tonic-gate
1737c478bdstevel@tonic-gate	/*
1747c478bdstevel@tonic-gate	 * Clear global flags
1757c478bdstevel@tonic-gate	 */
1767c478bdstevel@tonic-gate	nbra = 0;
1777c478bdstevel@tonic-gate	regerrno = 0;
1787c478bdstevel@tonic-gate
1797c478bdstevel@tonic-gate	/*
1807c478bdstevel@tonic-gate	 * Free any data being held for previous search strings
1817c478bdstevel@tonic-gate	 */
1827c478bdstevel@tonic-gate	regex_comp_free(&reg_comp);
1837c478bdstevel@tonic-gate
1847c478bdstevel@tonic-gate	/*
1857c478bdstevel@tonic-gate	 * We call regcomp twice, once to get a regex_t for use by step()
1867c478bdstevel@tonic-gate	 * and then again with for use by advance()
1877c478bdstevel@tonic-gate	 */
1887c478bdstevel@tonic-gate	if ((rv = regcomp(&reg_comp.r_stp, instr, regcomp_flags)) != 0) {
1897c478bdstevel@tonic-gate		regerrno = map_errnos(rv);	/* Convert regcomp error */
1907c478bdstevel@tonic-gate		goto out;
1917c478bdstevel@tonic-gate	}
1927c478bdstevel@tonic-gate	/*
1937c478bdstevel@tonic-gate	 * To support advance, which assumes an implicit ^ to match at start
1947c478bdstevel@tonic-gate	 * of line we prepend a ^ to the pattern by copying to a temp buffer
1957c478bdstevel@tonic-gate	 */
1967c478bdstevel@tonic-gate
1977c478bdstevel@tonic-gate	if (instr[0] == '^')
198023a3eeToomas Soome		instrp = (char *)instr; /* String already has leading ^ */
1997c478bdstevel@tonic-gate	else {
2007c478bdstevel@tonic-gate		adv_instr[0] = '^';
2017c478bdstevel@tonic-gate		strncpy(&adv_instr[1], instr, 2048);
2027c478bdstevel@tonic-gate		instrp = adv_instr;
2037c478bdstevel@tonic-gate	}
2047c478bdstevel@tonic-gate
2057c478bdstevel@tonic-gate	if ((rv = regcomp(&reg_comp.r_adv, instrp, regcomp_flags)) != 0) {
2067c478bdstevel@tonic-gate		regerrno = map_errnos(rv);	/* Convert regcomp error */
2077c478bdstevel@tonic-gate		goto out;
2087c478bdstevel@tonic-gate	}
2097c478bdstevel@tonic-gate
2107c478bdstevel@tonic-gate	/*
2117c478bdstevel@tonic-gate	 * update global variables
2127c478bdstevel@tonic-gate	 */
213023a3eeToomas Soome	nbra = (int)reg_comp.r_adv.re_nsub > 0 ?
214023a3eeToomas Soome	    (int)reg_comp.r_adv.re_nsub : 0;
2157c478bdstevel@tonic-gate	regerrno = 0;
2167c478bdstevel@tonic-gate
2177c478bdstevel@tonic-gate	/*
2187c478bdstevel@tonic-gate	 * Set the header flags for use by vi
2197c478bdstevel@tonic-gate	 */
220023a3eeToomas Soome	if (instr[0] == '^')		/* if beginning of string,	*/
2217c478bdstevel@tonic-gate		reg_comp.r_head[0] = 1;	/* set special flag		*/
2227c478bdstevel@tonic-gate	else
2237c478bdstevel@tonic-gate		reg_comp.r_head[0] = 0;	/* clear special flag		*/
2247c478bdstevel@tonic-gate	/*
2257c478bdstevel@tonic-gate	 * note that for a single BRE, nbra will be 0 here.
2267c478bdstevel@tonic-gate	 * we're guaranteed that, at this point, a RE has been found.
2277c478bdstevel@tonic-gate	 */
2287c478bdstevel@tonic-gate	reg_comp.r_head[1] = 1;	/* set special flag		*/
2297c478bdstevel@tonic-gate	/*
2307c478bdstevel@tonic-gate	 * Copy our reg_comp structure to expbuf
2317c478bdstevel@tonic-gate	 */
232023a3eeToomas Soome	(void) memcpy(expbuf, (char *)&reg_comp, regexc_size);
2337c478bdstevel@tonic-gate
2347c478bdstevel@tonic-gateout:
2357c478bdstevel@tonic-gate	/*
2367c478bdstevel@tonic-gate	 * Return code from libgen regcomp with mods.  Note weird return
2377c478bdstevel@tonic-gate	 * value - if space is malloc'd return pointer to start of space,
23848bbca8Daniel Hoffman	 * if user provided their own space, return pointer to 1+last byte
23948bbca8Daniel Hoffman	 * of that space.
2407c478bdstevel@tonic-gate	 */
2417c478bdstevel@tonic-gate	if (regerrno != 0) {
2427c478bdstevel@tonic-gate		if (alloc)
2437c478bdstevel@tonic-gate			free(expbuf);
2447c478bdstevel@tonic-gate		return (NULL);
2457c478bdstevel@tonic-gate	}
2467c478bdstevel@tonic-gate	reglength = regexc_size;
2477c478bdstevel@tonic-gate
2487c478bdstevel@tonic-gate	if (alloc)
2497c478bdstevel@tonic-gate		return (expbuf);
2507c478bdstevel@tonic-gate	else
2517c478bdstevel@tonic-gate		return (expbuf + regexc_size);
2527c478bdstevel@tonic-gate}
2537c478bdstevel@tonic-gate
2547c478bdstevel@tonic-gate
2557c478bdstevel@tonic-gate/*
2567c478bdstevel@tonic-gate * dhl_step: step through a string until a RE match is found, or end of str
2577c478bdstevel@tonic-gate */
2587c478bdstevel@tonic-gatestatic int
259023a3eeToomas Soomedhl_step(const char *str,	/* characters to be checked for a match	*/
260023a3eeToomas Soome    const char *ep)		/* compiled RE from dhl_compile()	*/
2617c478bdstevel@tonic-gate{
2627c478bdstevel@tonic-gate	/*
2637c478bdstevel@tonic-gate	 * Check if we're passed a null ep
2647c478bdstevel@tonic-gate	 */
2657c478bdstevel@tonic-gate	if (ep == NULL) {
2667c478bdstevel@tonic-gate		regerrno = 41;	/* No remembered search string error */
2677c478bdstevel@tonic-gate		return (0);
2687c478bdstevel@tonic-gate	}
2697c478bdstevel@tonic-gate	/*
2707c478bdstevel@tonic-gate	 * Call common routine with r_stp (step) structure
2717c478bdstevel@tonic-gate	 */
272023a3eeToomas Soome	return (dhl_doit(str, &(((struct regex_comp *)ep)->r_stp),
2737c478bdstevel@tonic-gate	    ((locs != NULL) ? REG_NOTBOL : 0)));
2747c478bdstevel@tonic-gate}
2757c478bdstevel@tonic-gate
2767c478bdstevel@tonic-gate/*
2777c478bdstevel@tonic-gate * dhl_advance: implement advance
2787c478bdstevel@tonic-gate */
2797c478bdstevel@tonic-gatestatic int
280023a3eeToomas Soomedhl_advance(const char *str,	/* characters to be checked for a match	*/
281023a3eeToomas Soome    const char *ep)		/* compiled RE from dhl_compile()	*/
2827c478bdstevel@tonic-gate{
2837c478bdstevel@tonic-gate	int rv;
2847c478bdstevel@tonic-gate	/*
2857c478bdstevel@tonic-gate	 * Check if we're passed a null ep
2867c478bdstevel@tonic-gate	 */
2877c478bdstevel@tonic-gate	if (ep == NULL) {
2887c478bdstevel@tonic-gate		regerrno = 41;	/* No remembered search string error */
2897c478bdstevel@tonic-gate		return (0);
2907c478bdstevel@tonic-gate	}
2917c478bdstevel@tonic-gate	/*
2927c478bdstevel@tonic-gate	 * Call common routine with r_adv (advance) structure
2937c478bdstevel@tonic-gate	 */
294023a3eeToomas Soome	rv = dhl_doit(str, &(((struct regex_comp *)ep)->r_adv), 0);
2957c478bdstevel@tonic-gate	loc1 = NULL;		/* Clear it per the compile man page */
2967c478bdstevel@tonic-gate	return (rv);
2977c478bdstevel@tonic-gate}
2987c478bdstevel@tonic-gate
2997c478bdstevel@tonic-gate/*
3007c478bdstevel@tonic-gate * dhl_doit - common code for step and advance
3017c478bdstevel@tonic-gate */
3027c478bdstevel@tonic-gatestatic int
303023a3eeToomas Soomedhl_doit(const char *str,	/* characters to be checked for a match	*/
304023a3eeToomas Soome    const regex_t *rep,
305023a3eeToomas Soome    const int flags)		/* flags to be passed to regexec directly */
3067c478bdstevel@tonic-gate{
3077c478bdstevel@tonic-gate	int rv;
3087c478bdstevel@tonic-gate	int i;
3097c478bdstevel@tonic-gate	regmatch_t *prm;	/* ptr to current regmatch_t		*/
3107c478bdstevel@tonic-gate
3117c478bdstevel@tonic-gate	/*
3127c478bdstevel@tonic-gate	 * Check if we're passed a null regex_t
3137c478bdstevel@tonic-gate	 */
3147c478bdstevel@tonic-gate	if (rep == NULL) {
3157c478bdstevel@tonic-gate		regerrno = 41;	/* No remembered search string error */
3167c478bdstevel@tonic-gate		return (0);
3177c478bdstevel@tonic-gate	}
3187c478bdstevel@tonic-gate
3197c478bdstevel@tonic-gate	regerrno = 0;
3207c478bdstevel@tonic-gate	prm = &rm[0];
3217c478bdstevel@tonic-gate
3227c478bdstevel@tonic-gate	if ((rv = regexec(rep, str, SEPSIZE, prm, flags)) != REG_OK) {
3237c478bdstevel@tonic-gate		if (rv == REG_NOMATCH)
3247c478bdstevel@tonic-gate			return (0);
3257c478bdstevel@tonic-gate		regerrno = map_errnos(rv);
3267c478bdstevel@tonic-gate		return (0);
3277c478bdstevel@tonic-gate	}
3287c478bdstevel@tonic-gate
3297c478bdstevel@tonic-gate	loc1 = (char *)str + prm->rm_so;
3307c478bdstevel@tonic-gate	loc2 = (char *)str + prm->rm_eo;
3317c478bdstevel@tonic-gate
3327c478bdstevel@tonic-gate	/*
3337c478bdstevel@tonic-gate	 * Now we need to fill up the bra lists with all of the sub re's
3347c478bdstevel@tonic-gate	 * Note we subtract nsub -1, and preincrement prm.
3357c478bdstevel@tonic-gate	 */
3367c478bdstevel@tonic-gate	for (i = 0; i <= rep->re_nsub; i++) {
3377c478bdstevel@tonic-gate		prm++;		/* XXX inc past first subexp */
3387c478bdstevel@tonic-gate		braslist[i] = (char *)str + prm->rm_so;
3397c478bdstevel@tonic-gate		braelist[i] = (char *)str + prm->rm_eo;
3407c478bdstevel@tonic-gate		if (i >= SEPSIZE) {
341023a3eeToomas Soome			regerrno = 50;	/* regex overflow */
3427c478bdstevel@tonic-gate			return (0);
3437c478bdstevel@tonic-gate		}
3447c478bdstevel@tonic-gate	}
3457c478bdstevel@tonic-gate
3467c478bdstevel@tonic-gate	/*
3477c478bdstevel@tonic-gate	 * Inverse logic, a zero from regexec - success, is a 1
3487c478bdstevel@tonic-gate	 * from advance/step.
3497c478bdstevel@tonic-gate	 */
3507c478bdstevel@tonic-gate
3517c478bdstevel@tonic-gate	return (rv == 0);
3527c478bdstevel@tonic-gate}
3537c478bdstevel@tonic-gate
3547c478bdstevel@tonic-gate
3557c478bdstevel@tonic-gate/*
3567c478bdstevel@tonic-gate *	regerrno to compile/step error mapping:
3577c478bdstevel@tonic-gate *	This is really a big compromise.  Some errors don't map at all
3587c478bdstevel@tonic-gate *	like regcomp error 15 is generated by both compile() error types
359023a3eeToomas Soome *	44 & 46.  So which one should we map to?
3607c478bdstevel@tonic-gate *	Note REG_ESUB Can't happen- 9 is no longer max num of subexpressions
3617c478bdstevel@tonic-gate *	To do your errors right use xregerr() to get the regcomp error
3627c478bdstevel@tonic-gate *	string and print that.
3637c478bdstevel@tonic-gate *
364023a3eeToomas Soome * |    regcomp/regexec              |  Compile/step/advance                |
3657c478bdstevel@tonic-gate * +---------------------------------+--------------------------------------+
3667c478bdstevel@tonic-gate * 0 REG_OK	  Pattern matched	1  - Pattern matched
3677c478bdstevel@tonic-gate * 1 REG_NOMATCH  No match		0  - Pattern didn't match
3687c478bdstevel@tonic-gate * 2 REG_ECOLLATE Bad collation elmnt.	67 - Returned by compile on mbtowc err
3697c478bdstevel@tonic-gate * 3 REG_EESCAPE  trailing \ in patrn	45 - } expected after \.
3707c478bdstevel@tonic-gate * 4 REG_ENEWLINE \n before end pattrn	36 - Illegal or missing delimiter.
371023a3eeToomas Soome * 5 REG_ENSUB    Over 9 \( \) pairs	43 - Too many \(
3727c478bdstevel@tonic-gate * 6 REG_ESUBREG  Bad number in \[0-9]  25 - ``\digit'' out of range.
3737c478bdstevel@tonic-gate * 7 REG_EBRACK   [ ] inbalance		49 - [ ] imbalance.
3747c478bdstevel@tonic-gate * 8 REG_EPAREN   ( ) inbalance         42 - \(~\) imbalance.
3757c478bdstevel@tonic-gate * 9 REG_EBRACE   \{ \} inbalance       45 - } expected after \.
3767c478bdstevel@tonic-gate * 10 REG_ERANGE  bad range endpoint	11 - Range endpoint too large.
3777c478bdstevel@tonic-gate * 11 REG_ESPACE  no memory for pattern 50 - Regular expression overflow.
3787c478bdstevel@tonic-gate * 12 REG_BADRPT  invalid repetition	36 - Illegal or missing delimiter.
3797c478bdstevel@tonic-gate * 13 REG_ECTYPE  invalid char-class    67 - illegal byte sequence
3807c478bdstevel@tonic-gate * 14 REG_BADPAT  syntax error		50 - Regular expression overflow.
3817c478bdstevel@tonic-gate * 15 REG_BADBR   \{ \} contents bad	46 - First number exceeds 2nd in \{~\}
3827c478bdstevel@tonic-gate * 16 REG_EFATAL  internal error	50 - Regular expression overflow.
3837c478bdstevel@tonic-gate * 17 REG_ECHAR   bad mulitbyte char	67 - illegal byte sequence
3847c478bdstevel@tonic-gate * 18 REG_STACK   stack overflow	50 - Regular expression overflow.
3857c478bdstevel@tonic-gate * 19 REG_ENOSYS  function not supported 50- Regular expression overflow.
3867c478bdstevel@tonic-gate *
3877c478bdstevel@tonic-gate *	For reference here's the compile/step errno's. We don't generate
3887c478bdstevel@tonic-gate *	41 here - it's done earlier, nor 44 since we can't tell if from 46.
3897c478bdstevel@tonic-gate *
3907c478bdstevel@tonic-gate *	11 - Range endpoint too large.
3917c478bdstevel@tonic-gate *	16 - Bad number.
3927c478bdstevel@tonic-gate *	25 - ``\digit'' out of range.
3937c478bdstevel@tonic-gate *	36 - Illegal or missing delimiter.
3947c478bdstevel@tonic-gate *	41 - No remembered search string.
3957c478bdstevel@tonic-gate *	42 - \(~\) imbalance.
3967c478bdstevel@tonic-gate *	43 - Too many \(.
3977c478bdstevel@tonic-gate *	44 - More than 2 numbers given in "\{~\}"
3987c478bdstevel@tonic-gate *	45 - } expected after \.
3997c478bdstevel@tonic-gate *	46 - First number exceeds 2nd in "\{~\}"
4007c478bdstevel@tonic-gate *	49 - [ ] imbalance.
4017c478bdstevel@tonic-gate *	50 - Regular expression overflow.
4027c478bdstevel@tonic-gate */
4037c478bdstevel@tonic-gate
4047c478bdstevel@tonic-gatestatic int
4057c478bdstevel@tonic-gatemap_errnos(int Errno)
4067c478bdstevel@tonic-gate{
4077c478bdstevel@tonic-gate	switch (Errno) {
4087c478bdstevel@tonic-gate	case REG_ECOLLATE:
4097c478bdstevel@tonic-gate		regerrno = 67;
4107c478bdstevel@tonic-gate		break;
4117c478bdstevel@tonic-gate	case REG_EESCAPE:
4127c478bdstevel@tonic-gate		regerrno = 45;
4137c478bdstevel@tonic-gate		break;
4147c478bdstevel@tonic-gate	case REG_ENEWLINE:
4157c478bdstevel@tonic-gate		regerrno = 36;
4167c478bdstevel@tonic-gate		break;
4177c478bdstevel@tonic-gate	case REG_ENSUB:
4187c478bdstevel@tonic-gate		regerrno = 43;
4197c478bdstevel@tonic-gate		break;
4207c478bdstevel@tonic-gate	case REG_ESUBREG:
4217c478bdstevel@tonic-gate		regerrno = 25;
4227c478bdstevel@tonic-gate		break;
4237c478bdstevel@tonic-gate	case REG_EBRACK:
4247c478bdstevel@tonic-gate		regerrno = 49;
4257c478bdstevel@tonic-gate		break;
4267c478bdstevel@tonic-gate	case REG_EPAREN:
4277c478bdstevel@tonic-gate		regerrno = 42;
4287c478bdstevel@tonic-gate		break;
4297c478bdstevel@tonic-gate	case REG_EBRACE:
4307c478bdstevel@tonic-gate		regerrno = 45;
4317c478bdstevel@tonic-gate		break;
4327c478bdstevel@tonic-gate	case REG_ERANGE:
4337c478bdstevel@tonic-gate		regerrno = 11;
4347c478bdstevel@tonic-gate		break;
4357c478bdstevel@tonic-gate	case REG_ESPACE:
4367c478bdstevel@tonic-gate		regerrno = 50;
4377c478bdstevel@tonic-gate		break;
4387c478bdstevel@tonic-gate	case REG_BADRPT:
4397c478bdstevel@tonic-gate		regerrno = 36;
4407c478bdstevel@tonic-gate		break;
4417c478bdstevel@tonic-gate	case REG_ECTYPE:
4427c478bdstevel@tonic-gate		regerrno = 67;
4437c478bdstevel@tonic-gate		break;
4447c478bdstevel@tonic-gate	case REG_BADPAT:
4457c478bdstevel@tonic-gate		regerrno = 50;
4467c478bdstevel@tonic-gate		break;
4477c478bdstevel@tonic-gate	case REG_BADBR:
4487c478bdstevel@tonic-gate		regerrno = 46;
4497c478bdstevel@tonic-gate		break;
4507c478bdstevel@tonic-gate	case REG_EFATAL:
4517c478bdstevel@tonic-gate		regerrno = 50;
4527c478bdstevel@tonic-gate		break;
4537c478bdstevel@tonic-gate	case REG_ECHAR:
4547c478bdstevel@tonic-gate		regerrno = 67;
4557c478bdstevel@tonic-gate		break;
4567c478bdstevel@tonic-gate	case REG_STACK:
4577c478bdstevel@tonic-gate		regerrno = 50;
4587c478bdstevel@tonic-gate		break;
4597c478bdstevel@tonic-gate	case REG_ENOSYS:
4607c478bdstevel@tonic-gate		regerrno = 50;
4617c478bdstevel@tonic-gate		break;
4627c478bdstevel@tonic-gate	default:
4637c478bdstevel@tonic-gate		regerrno = 50;
4647c478bdstevel@tonic-gate		break;
4657c478bdstevel@tonic-gate	}
4667c478bdstevel@tonic-gate	return (regerrno);
4677c478bdstevel@tonic-gate}
4687c478bdstevel@tonic-gate
4697c478bdstevel@tonic-gate/*
4707c478bdstevel@tonic-gate *  This is a routine to clean up the subtle substructure of the struct
4717c478bdstevel@tonic-gate *  regex_comp type for use by clients of this module.  Since the struct
4727c478bdstevel@tonic-gate *  type is private, we use a generic interface, and trust the
4737c478bdstevel@tonic-gate *  application to be damn sure that this operation is valid for the
4747c478bdstevel@tonic-gate *  named memory.
4757c478bdstevel@tonic-gate */
4767c478bdstevel@tonic-gate
4777c478bdstevel@tonic-gatevoid
478023a3eeToomas Soomeregex_comp_free(void *a)
4797c478bdstevel@tonic-gate{
4807c478bdstevel@tonic-gate	/*
4817c478bdstevel@tonic-gate	 * Free any data being held for previous search strings
4827c478bdstevel@tonic-gate	 */
4837c478bdstevel@tonic-gate
484023a3eeToomas Soome	if (a == NULL) {
4857c478bdstevel@tonic-gate		return;
4867c478bdstevel@tonic-gate	}
4877c478bdstevel@tonic-gate
4887c478bdstevel@tonic-gate	regfree(&((struct regex_comp *)a)->r_stp);
4897c478bdstevel@tonic-gate	regfree(&((struct regex_comp *)a)->r_adv);
4907c478bdstevel@tonic-gate}
491