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