xref: /illumos-gate/usr/src/lib/libeti/form/common/regcmp.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2	*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
36*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
37*7c478bd9Sstevel@tonic-gate #include "utility.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /* this code was taken from REGCMP(3X) */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #define	SSIZE	16
42*7c478bd9Sstevel@tonic-gate #define	TGRP	48
43*7c478bd9Sstevel@tonic-gate #define	A256	02
44*7c478bd9Sstevel@tonic-gate #define	ZERO	01
45*7c478bd9Sstevel@tonic-gate #define	NBRA	10
46*7c478bd9Sstevel@tonic-gate #define	CIRCFL	32;
47*7c478bd9Sstevel@tonic-gate #define	SLOP	5
48*7c478bd9Sstevel@tonic-gate #define	FEOF	0 /* This was originally EOF but it clashes with the header */
49*7c478bd9Sstevel@tonic-gate 			/* definition so it was changed to FEOF */
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #define	CBRA	60
52*7c478bd9Sstevel@tonic-gate #define	GRP	40
53*7c478bd9Sstevel@tonic-gate #define	SGRP	56
54*7c478bd9Sstevel@tonic-gate #define	PGRP	68
55*7c478bd9Sstevel@tonic-gate #define	EGRP	44
56*7c478bd9Sstevel@tonic-gate #define	RNGE	03
57*7c478bd9Sstevel@tonic-gate #define	CCHR	20
58*7c478bd9Sstevel@tonic-gate #define	CDOT	64
59*7c478bd9Sstevel@tonic-gate #define	CCL	24
60*7c478bd9Sstevel@tonic-gate #define	NCCL	8
61*7c478bd9Sstevel@tonic-gate #define	CDOL	28
62*7c478bd9Sstevel@tonic-gate #define	FCEOF	52 /* This was originally CEOF but it clashes with the header */
63*7c478bd9Sstevel@tonic-gate 			/* definition so it was changed to FCEOF */
64*7c478bd9Sstevel@tonic-gate #define	CKET	12
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate #define	STAR	01
67*7c478bd9Sstevel@tonic-gate #define	PLUS	02
68*7c478bd9Sstevel@tonic-gate #define	MINUS	16
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate intptr_t	*__sp_;
71*7c478bd9Sstevel@tonic-gate intptr_t	*__stmax;
72*7c478bd9Sstevel@tonic-gate int	__i_size;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
75*7c478bd9Sstevel@tonic-gate char *
76*7c478bd9Sstevel@tonic-gate libform_regcmp(char *cs1, char *cs2)
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	char c;
79*7c478bd9Sstevel@tonic-gate 	char *ep, *sp;
80*7c478bd9Sstevel@tonic-gate 	int *adx;
81*7c478bd9Sstevel@tonic-gate 	int i, cflg;
82*7c478bd9Sstevel@tonic-gate 	char *lastep, *sep, *eptr;
83*7c478bd9Sstevel@tonic-gate 	int nbra, ngrp;
84*7c478bd9Sstevel@tonic-gate 	int cclcnt;
85*7c478bd9Sstevel@tonic-gate 	intptr_t stack[SSIZE];
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	__sp_ = stack;
88*7c478bd9Sstevel@tonic-gate 	*__sp_ = -1;
89*7c478bd9Sstevel@tonic-gate 	__stmax = &stack[SSIZE];
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	adx = (int *)&cs1;
92*7c478bd9Sstevel@tonic-gate 	i = nbra = ngrp = 0;
93*7c478bd9Sstevel@tonic-gate 	while (*adx)
94*7c478bd9Sstevel@tonic-gate 		i += __size((char *)(intptr_t)*adx++);
95*7c478bd9Sstevel@tonic-gate 	adx = (int *)&cs1;
96*7c478bd9Sstevel@tonic-gate 	sp = (char *)(intptr_t)*adx++;
97*7c478bd9Sstevel@tonic-gate 	if ((sep = ep = malloc((unsigned)(2 * i + SLOP))) == NULL)
98*7c478bd9Sstevel@tonic-gate 		return (NULL);
99*7c478bd9Sstevel@tonic-gate 	if ((c = *sp++) == FEOF)
100*7c478bd9Sstevel@tonic-gate 		goto cerror;
101*7c478bd9Sstevel@tonic-gate 	if (c == '^') {
102*7c478bd9Sstevel@tonic-gate 		c = *sp++;
103*7c478bd9Sstevel@tonic-gate 		*ep++ = CIRCFL;
104*7c478bd9Sstevel@tonic-gate 	}
105*7c478bd9Sstevel@tonic-gate 	if ((c == '*') || (c == '+') || (c == '{'))
106*7c478bd9Sstevel@tonic-gate 		goto cerror;
107*7c478bd9Sstevel@tonic-gate 	sp--;
108*7c478bd9Sstevel@tonic-gate 	for (;;) {
109*7c478bd9Sstevel@tonic-gate 		if ((c = *sp++) == FEOF) {
110*7c478bd9Sstevel@tonic-gate 			if (*adx) {
111*7c478bd9Sstevel@tonic-gate 				sp = (char *)(intptr_t)*adx++;
112*7c478bd9Sstevel@tonic-gate 				continue;
113*7c478bd9Sstevel@tonic-gate 			}
114*7c478bd9Sstevel@tonic-gate 			*ep++ = FCEOF;
115*7c478bd9Sstevel@tonic-gate 			if (--nbra > NBRA || *__sp_ != -1)
116*7c478bd9Sstevel@tonic-gate 				goto cerror;
117*7c478bd9Sstevel@tonic-gate 			__i_size = (int) (ep - sep);
118*7c478bd9Sstevel@tonic-gate 			return (sep);
119*7c478bd9Sstevel@tonic-gate 		}
120*7c478bd9Sstevel@tonic-gate 		if ((c != '*') && (c != '{') && (c != '+'))
121*7c478bd9Sstevel@tonic-gate 			lastep = ep;
122*7c478bd9Sstevel@tonic-gate 		switch (c) {
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 		case '(':
125*7c478bd9Sstevel@tonic-gate 			if (!__rpush(ep)) goto cerror;
126*7c478bd9Sstevel@tonic-gate 			*ep++ = CBRA;
127*7c478bd9Sstevel@tonic-gate 			*ep++ = -1;
128*7c478bd9Sstevel@tonic-gate 			continue;
129*7c478bd9Sstevel@tonic-gate 		case ')':
130*7c478bd9Sstevel@tonic-gate 			if (!(eptr = (char *)__rpop())) goto cerror;
131*7c478bd9Sstevel@tonic-gate 			if ((c = *sp++) == '$') {
132*7c478bd9Sstevel@tonic-gate 				if ('0' > (c = *sp++) || c > '9')
133*7c478bd9Sstevel@tonic-gate 					goto cerror;
134*7c478bd9Sstevel@tonic-gate 				*ep++ = CKET;
135*7c478bd9Sstevel@tonic-gate 				*ep++ = *++eptr = nbra++;
136*7c478bd9Sstevel@tonic-gate 				*ep++ = (c-'0');
137*7c478bd9Sstevel@tonic-gate 				continue;
138*7c478bd9Sstevel@tonic-gate 			}
139*7c478bd9Sstevel@tonic-gate 			*ep++ = EGRP;
140*7c478bd9Sstevel@tonic-gate 			*ep++ = ngrp++;
141*7c478bd9Sstevel@tonic-gate 			sp--;
142*7c478bd9Sstevel@tonic-gate 			switch (c) {
143*7c478bd9Sstevel@tonic-gate 			case '+':
144*7c478bd9Sstevel@tonic-gate 				*eptr = PGRP;
145*7c478bd9Sstevel@tonic-gate 				break;
146*7c478bd9Sstevel@tonic-gate 			case '*':
147*7c478bd9Sstevel@tonic-gate 				*eptr = SGRP;
148*7c478bd9Sstevel@tonic-gate 				break;
149*7c478bd9Sstevel@tonic-gate 			case '{':
150*7c478bd9Sstevel@tonic-gate 				*eptr = TGRP;
151*7c478bd9Sstevel@tonic-gate 				break;
152*7c478bd9Sstevel@tonic-gate 			default:
153*7c478bd9Sstevel@tonic-gate 				*eptr = GRP;
154*7c478bd9Sstevel@tonic-gate 				continue;
155*7c478bd9Sstevel@tonic-gate 			}
156*7c478bd9Sstevel@tonic-gate 			i = (int) (ep - eptr - 2);
157*7c478bd9Sstevel@tonic-gate 			for (cclcnt = 0; i >= 256; cclcnt++)
158*7c478bd9Sstevel@tonic-gate 				i -= 256;
159*7c478bd9Sstevel@tonic-gate 			if (cclcnt > 3) goto cerror;
160*7c478bd9Sstevel@tonic-gate 			*eptr |= cclcnt;
161*7c478bd9Sstevel@tonic-gate 			*++eptr = (char) i;
162*7c478bd9Sstevel@tonic-gate 			continue;
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 		case '\\':
165*7c478bd9Sstevel@tonic-gate 			*ep++ = CCHR;
166*7c478bd9Sstevel@tonic-gate 			if ((c = *sp++) == FEOF)
167*7c478bd9Sstevel@tonic-gate 				goto cerror;
168*7c478bd9Sstevel@tonic-gate 			*ep++ = c;
169*7c478bd9Sstevel@tonic-gate 			continue;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 		case '{':
172*7c478bd9Sstevel@tonic-gate 			*lastep |= RNGE;
173*7c478bd9Sstevel@tonic-gate 			cflg = 0;
174*7c478bd9Sstevel@tonic-gate 		nlim:
175*7c478bd9Sstevel@tonic-gate 			if ((c = *sp++) == '}') goto cerror;
176*7c478bd9Sstevel@tonic-gate 			i = 0;
177*7c478bd9Sstevel@tonic-gate 			do {
178*7c478bd9Sstevel@tonic-gate 				if ('0' <= c && c <= '9')
179*7c478bd9Sstevel@tonic-gate 					i = (i*10+(c-'0'));
180*7c478bd9Sstevel@tonic-gate 				else goto cerror;
181*7c478bd9Sstevel@tonic-gate 			} while (((c = *sp++) != '}') && (c != ','));
182*7c478bd9Sstevel@tonic-gate 			if (i > 255) goto cerror;
183*7c478bd9Sstevel@tonic-gate 			*ep++ = (char) i;
184*7c478bd9Sstevel@tonic-gate 			if (c == ',') {
185*7c478bd9Sstevel@tonic-gate 				if (cflg++) goto cerror;
186*7c478bd9Sstevel@tonic-gate 				if ((c = *sp++) == '}') {
187*7c478bd9Sstevel@tonic-gate 					*ep++ = -1;
188*7c478bd9Sstevel@tonic-gate 					continue;
189*7c478bd9Sstevel@tonic-gate 				} else {
190*7c478bd9Sstevel@tonic-gate 					sp--;
191*7c478bd9Sstevel@tonic-gate 					goto nlim;
192*7c478bd9Sstevel@tonic-gate 				}
193*7c478bd9Sstevel@tonic-gate 			}
194*7c478bd9Sstevel@tonic-gate 			if (!cflg)
195*7c478bd9Sstevel@tonic-gate 				*ep++ = (char) i;
196*7c478bd9Sstevel@tonic-gate 			else if ((ep[-1]&0377) < (ep[-2]&0377))
197*7c478bd9Sstevel@tonic-gate 				goto cerror;
198*7c478bd9Sstevel@tonic-gate 			continue;
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 		case '.':
201*7c478bd9Sstevel@tonic-gate 			*ep++ = CDOT;
202*7c478bd9Sstevel@tonic-gate 			continue;
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 		case '+':
205*7c478bd9Sstevel@tonic-gate 			if (*lastep == CBRA || *lastep == CKET)
206*7c478bd9Sstevel@tonic-gate 				goto cerror;
207*7c478bd9Sstevel@tonic-gate 			*lastep |= PLUS;
208*7c478bd9Sstevel@tonic-gate 			continue;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 		case '*':
211*7c478bd9Sstevel@tonic-gate 			if (*lastep == CBRA || *lastep == CKET)
212*7c478bd9Sstevel@tonic-gate 			goto cerror;
213*7c478bd9Sstevel@tonic-gate 			*lastep |= STAR;
214*7c478bd9Sstevel@tonic-gate 			continue;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 		case '$':
217*7c478bd9Sstevel@tonic-gate 			if ((*sp != FEOF) || (*adx))
218*7c478bd9Sstevel@tonic-gate 				goto defchar;
219*7c478bd9Sstevel@tonic-gate 			*ep++ = CDOL;
220*7c478bd9Sstevel@tonic-gate 			continue;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 		case '[':
223*7c478bd9Sstevel@tonic-gate 			*ep++ = CCL;
224*7c478bd9Sstevel@tonic-gate 			*ep++ = 0;
225*7c478bd9Sstevel@tonic-gate 			cclcnt = 1;
226*7c478bd9Sstevel@tonic-gate 			if ((c = *sp++) == '^') {
227*7c478bd9Sstevel@tonic-gate 				c = *sp++;
228*7c478bd9Sstevel@tonic-gate 				ep[-2] = NCCL;
229*7c478bd9Sstevel@tonic-gate 			}
230*7c478bd9Sstevel@tonic-gate 			do {
231*7c478bd9Sstevel@tonic-gate 				if (c == FEOF)
232*7c478bd9Sstevel@tonic-gate 					goto cerror;
233*7c478bd9Sstevel@tonic-gate 				if ((c == '-') && (cclcnt > 1) &&
234*7c478bd9Sstevel@tonic-gate 				    (*sp != ']')) {
235*7c478bd9Sstevel@tonic-gate 					*ep = ep[-1];
236*7c478bd9Sstevel@tonic-gate 					ep++;
237*7c478bd9Sstevel@tonic-gate 					ep[-2] = MINUS;
238*7c478bd9Sstevel@tonic-gate 					cclcnt++;
239*7c478bd9Sstevel@tonic-gate 					continue;
240*7c478bd9Sstevel@tonic-gate 				}
241*7c478bd9Sstevel@tonic-gate 				*ep++ = c;
242*7c478bd9Sstevel@tonic-gate 				cclcnt++;
243*7c478bd9Sstevel@tonic-gate 			} while ((c = *sp++) != ']');
244*7c478bd9Sstevel@tonic-gate 			lastep[1] = (char) cclcnt;
245*7c478bd9Sstevel@tonic-gate 			continue;
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 		defchar:
248*7c478bd9Sstevel@tonic-gate 		default:
249*7c478bd9Sstevel@tonic-gate 			*ep++ = CCHR;
250*7c478bd9Sstevel@tonic-gate 			*ep++ = c;
251*7c478bd9Sstevel@tonic-gate 		}
252*7c478bd9Sstevel@tonic-gate 	}
253*7c478bd9Sstevel@tonic-gate cerror:
254*7c478bd9Sstevel@tonic-gate 	free(sep);
255*7c478bd9Sstevel@tonic-gate 	return (0);
256*7c478bd9Sstevel@tonic-gate }
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate int
259*7c478bd9Sstevel@tonic-gate __size(char *strg)
260*7c478bd9Sstevel@tonic-gate {
261*7c478bd9Sstevel@tonic-gate 	int	i;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	i = 1;
264*7c478bd9Sstevel@tonic-gate 	while (*strg++)
265*7c478bd9Sstevel@tonic-gate 		i++;
266*7c478bd9Sstevel@tonic-gate 	return (i);
267*7c478bd9Sstevel@tonic-gate }
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate intptr_t
270*7c478bd9Sstevel@tonic-gate __rpop(void)
271*7c478bd9Sstevel@tonic-gate {
272*7c478bd9Sstevel@tonic-gate 	return ((*__sp_ == -1)?0:*__sp_--);
273*7c478bd9Sstevel@tonic-gate }
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate int
276*7c478bd9Sstevel@tonic-gate __rpush(char *ptr)
277*7c478bd9Sstevel@tonic-gate {
278*7c478bd9Sstevel@tonic-gate 	if (__sp_ >= __stmax)
279*7c478bd9Sstevel@tonic-gate 		return (0);
280*7c478bd9Sstevel@tonic-gate 	*++__sp_ = (intptr_t)ptr;
281*7c478bd9Sstevel@tonic-gate 	return (1);
282*7c478bd9Sstevel@tonic-gate }
283