xref: /illumos-gate/usr/src/lib/libeti/form/common/regex.c (revision 02b0e3b7c418f908fdce638a840f8d57e03e1346)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include "utility.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  *	this code was taken from REGCMP(3X)
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate /*VARARGS*/
427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	SSIZE	50
457c478bd9Sstevel@tonic-gate #define	TGRP	48
467c478bd9Sstevel@tonic-gate #define	A256	01
477c478bd9Sstevel@tonic-gate #define	A512	02
487c478bd9Sstevel@tonic-gate #define	A768	03
497c478bd9Sstevel@tonic-gate #define	NBRA	10
507c478bd9Sstevel@tonic-gate #define	CIRCFL	32
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	CBRA	60
537c478bd9Sstevel@tonic-gate #define	GRP	40
547c478bd9Sstevel@tonic-gate #define	SGRP	56
557c478bd9Sstevel@tonic-gate #define	PGRP	68
567c478bd9Sstevel@tonic-gate #define	EGRP	44
577c478bd9Sstevel@tonic-gate #define	RNGE	03
587c478bd9Sstevel@tonic-gate #define	CCHR	20
597c478bd9Sstevel@tonic-gate #define	CDOT	64
607c478bd9Sstevel@tonic-gate #define	CCL	24
617c478bd9Sstevel@tonic-gate #define	NCCL	8
627c478bd9Sstevel@tonic-gate #define	CDOL	28
637c478bd9Sstevel@tonic-gate #define	FCEOF	52 /* This was originally CEOF but it clashes with the header */
647c478bd9Sstevel@tonic-gate 			/* definition so it was changed to FCEOF */
657c478bd9Sstevel@tonic-gate #define	CKET	12
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	STAR	01
687c478bd9Sstevel@tonic-gate #define	PLUS	02
697c478bd9Sstevel@tonic-gate #define	MINUS	16
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate char	*__braslist[NBRA];
727c478bd9Sstevel@tonic-gate char	*__braelist[NBRA];
737c478bd9Sstevel@tonic-gate char	*__loc1;
747c478bd9Sstevel@tonic-gate intptr_t	__bravar[NBRA];
757c478bd9Sstevel@tonic-gate intptr_t	*__st[SSIZE + 1];
767c478bd9Sstevel@tonic-gate intptr_t	*__eptr_, *__lptr_;
777c478bd9Sstevel@tonic-gate intptr_t	__cflg;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate char *
807c478bd9Sstevel@tonic-gate libform_regex(char *addrc, char *addrl, char *a1)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	intptr_t cur, in;
837c478bd9Sstevel@tonic-gate 	intptr_t *adx;
847c478bd9Sstevel@tonic-gate 	char *p1, *p2;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	for (in = 0; in < NBRA; in++) {
877c478bd9Sstevel@tonic-gate 		__braslist[in] = 0;
887c478bd9Sstevel@tonic-gate 		__bravar[in] = -1;
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 	__cflg = 0;
917c478bd9Sstevel@tonic-gate 	cur = __execute(addrc, addrl);
927c478bd9Sstevel@tonic-gate 	adx = (intptr_t *)&a1;
937c478bd9Sstevel@tonic-gate 	for (in = 0; in < NBRA; in++) {
947c478bd9Sstevel@tonic-gate 		if (((p1 = __braslist[in]) != 0) && (__bravar[in] >= 0)) {
957c478bd9Sstevel@tonic-gate 			p2 = (char *)adx[__bravar[in]];
967c478bd9Sstevel@tonic-gate 			while (p1 < __braelist[in]) *p2++ = *p1++;
977c478bd9Sstevel@tonic-gate 			*p2 = '\0';
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 	if (!__cflg)
1017c478bd9Sstevel@tonic-gate 		return ((addrl == (char *)cur) ? (char *)0 : (char *)cur);
1027c478bd9Sstevel@tonic-gate 	else
1037c478bd9Sstevel@tonic-gate 		return ((char *)cur);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate intptr_t
1077c478bd9Sstevel@tonic-gate __execute(char *addrc, char *addrl)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	char *p1, *p2, c;
1107c478bd9Sstevel@tonic-gate 	intptr_t i;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	p1 = addrl;
1137c478bd9Sstevel@tonic-gate 	p2 = addrc;
1147c478bd9Sstevel@tonic-gate 	__eptr_ = (intptr_t *)&__st[SSIZE];
1157c478bd9Sstevel@tonic-gate 	__lptr_ = (intptr_t *)&__st[0];
1167c478bd9Sstevel@tonic-gate 	if (*p2 == CIRCFL) {
1177c478bd9Sstevel@tonic-gate 		__loc1 = p1;
1187c478bd9Sstevel@tonic-gate 		return ((i = __advance(p1, ++p2)) ? i : (intptr_t)addrl);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 	/* fast check for first character */
1217c478bd9Sstevel@tonic-gate 	if (*p2 == CCHR) {
1227c478bd9Sstevel@tonic-gate 		c = p2[1];
1237c478bd9Sstevel@tonic-gate 		do {
1247c478bd9Sstevel@tonic-gate 			if (*p1 != c)
1257c478bd9Sstevel@tonic-gate 				continue;
1267c478bd9Sstevel@tonic-gate 			__eptr_ = (intptr_t *)&__st[SSIZE];
1277c478bd9Sstevel@tonic-gate 			__lptr_ = (intptr_t *)&__st[0];
1287c478bd9Sstevel@tonic-gate 			if (i = __advance(p1, p2))  {
1297c478bd9Sstevel@tonic-gate 				__loc1 = p1;
1307c478bd9Sstevel@tonic-gate 				return (i);
1317c478bd9Sstevel@tonic-gate 			}
1327c478bd9Sstevel@tonic-gate 		} while (*p1++);
1337c478bd9Sstevel@tonic-gate 		return ((intptr_t)addrl);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	/* regular algorithm */
1367c478bd9Sstevel@tonic-gate 	do {
1377c478bd9Sstevel@tonic-gate 	__eptr_ = (intptr_t *)&__st[SSIZE];
1387c478bd9Sstevel@tonic-gate 	__lptr_ = (intptr_t *)&__st[0];
1397c478bd9Sstevel@tonic-gate 		if (i = __advance(p1, p2))  {
1407c478bd9Sstevel@tonic-gate 			__loc1 = p1;
1417c478bd9Sstevel@tonic-gate 			return (i);
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 	} while (*p1++);
1447c478bd9Sstevel@tonic-gate 	return ((intptr_t)addrl);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate intptr_t
1487c478bd9Sstevel@tonic-gate __advance(char *alp, char *aep)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	char *lp, *ep, *curlp;
1517c478bd9Sstevel@tonic-gate 	char *sep, *dp;
1527c478bd9Sstevel@tonic-gate 	intptr_t i, lcnt, dcnt, gflg;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	lp = alp;
1557c478bd9Sstevel@tonic-gate 	ep = aep;
1567c478bd9Sstevel@tonic-gate 	gflg = 0;
1577c478bd9Sstevel@tonic-gate 	for (; ; ) {
1587c478bd9Sstevel@tonic-gate 		switch (*ep++) {
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	case CCHR:
1617c478bd9Sstevel@tonic-gate 		if (*ep++ == *lp++)
1627c478bd9Sstevel@tonic-gate 			continue;
1637c478bd9Sstevel@tonic-gate 		return (0);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	case EGRP|RNGE:
1667c478bd9Sstevel@tonic-gate 		return ((intptr_t)lp);
1677c478bd9Sstevel@tonic-gate 	case EGRP:
1687c478bd9Sstevel@tonic-gate 	case GRP:
1697c478bd9Sstevel@tonic-gate 		ep++;
1707c478bd9Sstevel@tonic-gate 		continue;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	case EGRP|STAR:
1737c478bd9Sstevel@tonic-gate 		(void) __xpop(0);
174*02b0e3b7SToomas Soome 		/* FALLTHROUGH */
1757c478bd9Sstevel@tonic-gate 	case EGRP|PLUS:
1767c478bd9Sstevel@tonic-gate 		(void) __xpush(0, ++ep);
1777c478bd9Sstevel@tonic-gate 		return ((intptr_t)lp);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	case CDOT:
1807c478bd9Sstevel@tonic-gate 		if (*lp++)
1817c478bd9Sstevel@tonic-gate 			continue;
1827c478bd9Sstevel@tonic-gate 		return (0);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	case CDOL:
1857c478bd9Sstevel@tonic-gate 		if (*lp == 0)
1867c478bd9Sstevel@tonic-gate 			continue;
1877c478bd9Sstevel@tonic-gate 		lp++;
1887c478bd9Sstevel@tonic-gate 		return (0);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	case FCEOF:
1917c478bd9Sstevel@tonic-gate 		__cflg = 1;
1927c478bd9Sstevel@tonic-gate 		return ((intptr_t)lp);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	case TGRP:
1957c478bd9Sstevel@tonic-gate 	case TGRP|A768:
1967c478bd9Sstevel@tonic-gate 	case TGRP|A512:
1977c478bd9Sstevel@tonic-gate 	case TGRP|A256:
1987c478bd9Sstevel@tonic-gate 		i = (((ep[-1] & 03) << 8) + (*ep) & 0377);
1997c478bd9Sstevel@tonic-gate 		ep++;
2007c478bd9Sstevel@tonic-gate 		(void) __xpush(0, ep + i + 2);
2017c478bd9Sstevel@tonic-gate 		(void) __xpush(0, ++ep);
2027c478bd9Sstevel@tonic-gate 		(void) __xpush(0, ++ep);
2037c478bd9Sstevel@tonic-gate 		gflg = 1;
2047c478bd9Sstevel@tonic-gate 		(void) __getrnge(&lcnt, &dcnt, &ep[i]);
2057c478bd9Sstevel@tonic-gate 		while (lcnt--)
2067c478bd9Sstevel@tonic-gate 			if (!(lp = (char *)__advance(lp, ep)))
2077c478bd9Sstevel@tonic-gate 				return (0);
2087c478bd9Sstevel@tonic-gate 		(void) __xpush(1, curlp = lp);
2097c478bd9Sstevel@tonic-gate 		while (dcnt--)
2107c478bd9Sstevel@tonic-gate 			if (!(dp = (char *)__advance(lp, ep))) break;
2117c478bd9Sstevel@tonic-gate 			else
2127c478bd9Sstevel@tonic-gate 				(void) __xpush(1, lp = dp);
2137c478bd9Sstevel@tonic-gate 		ep = (char *)__xpop(0);
2147c478bd9Sstevel@tonic-gate 		goto star;
2157c478bd9Sstevel@tonic-gate 	case CCHR|RNGE:
2167c478bd9Sstevel@tonic-gate 		sep = ep++;
2177c478bd9Sstevel@tonic-gate 		(void) __getrnge(&lcnt, &dcnt, ep);
2187c478bd9Sstevel@tonic-gate 		while (lcnt--)
2197c478bd9Sstevel@tonic-gate 			if (*lp++ != *sep)
2207c478bd9Sstevel@tonic-gate 				return (0);
2217c478bd9Sstevel@tonic-gate 		curlp = lp;
2227c478bd9Sstevel@tonic-gate 		while (dcnt--)
2237c478bd9Sstevel@tonic-gate 			if (*lp++ != *sep) break;
2247c478bd9Sstevel@tonic-gate 		if (dcnt < 0) lp++;
2257c478bd9Sstevel@tonic-gate 		ep += 2;
2267c478bd9Sstevel@tonic-gate 		goto star;
2277c478bd9Sstevel@tonic-gate 	case CDOT|RNGE:
2287c478bd9Sstevel@tonic-gate 		(void) __getrnge(&lcnt, &dcnt, ep);
2297c478bd9Sstevel@tonic-gate 		while (lcnt--)
2307c478bd9Sstevel@tonic-gate 			if (*lp++ == '\0')
2317c478bd9Sstevel@tonic-gate 				return (0);
2327c478bd9Sstevel@tonic-gate 		curlp = lp;
2337c478bd9Sstevel@tonic-gate 		while (dcnt--)
2347c478bd9Sstevel@tonic-gate 			if (*lp++ == '\0') break;
2357c478bd9Sstevel@tonic-gate 		if (dcnt < 0) lp++;
2367c478bd9Sstevel@tonic-gate 		ep += 2;
2377c478bd9Sstevel@tonic-gate 		goto star;
2387c478bd9Sstevel@tonic-gate 	case CCL|RNGE:
2397c478bd9Sstevel@tonic-gate 	case NCCL|RNGE:
2407c478bd9Sstevel@tonic-gate 		(void) __getrnge(&lcnt, &dcnt, (ep + (*ep & 0377)));
2417c478bd9Sstevel@tonic-gate 		while (lcnt--)
2427c478bd9Sstevel@tonic-gate 			if (!__cclass(ep, *lp++, ep[-1] == (CCL | RNGE)))
2437c478bd9Sstevel@tonic-gate 				return (0);
2447c478bd9Sstevel@tonic-gate 		curlp = lp;
2457c478bd9Sstevel@tonic-gate 		while (dcnt--)
2467c478bd9Sstevel@tonic-gate 			if (!__cclass(ep, *lp++, ep[-1] == (CCL|RNGE)))
2477c478bd9Sstevel@tonic-gate 				break;
2487c478bd9Sstevel@tonic-gate 		if (dcnt < 0) lp++;
2497c478bd9Sstevel@tonic-gate 		ep += (*ep + 2);
2507c478bd9Sstevel@tonic-gate 		goto star;
2517c478bd9Sstevel@tonic-gate 	case CCL:
2527c478bd9Sstevel@tonic-gate 		if (__cclass(ep, *lp++, 1)) {
2537c478bd9Sstevel@tonic-gate 			ep += *ep;
2547c478bd9Sstevel@tonic-gate 			continue;
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 		return (0);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	case NCCL:
2597c478bd9Sstevel@tonic-gate 		if (__cclass(ep, *lp++, 0)) {
2607c478bd9Sstevel@tonic-gate 			ep += *ep;
2617c478bd9Sstevel@tonic-gate 			continue;
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 		return (0);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	case CBRA:
2667c478bd9Sstevel@tonic-gate 		__braslist[*ep++] = lp;
2677c478bd9Sstevel@tonic-gate 		continue;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	case CKET:
2707c478bd9Sstevel@tonic-gate 		__braelist[*ep] = lp;
2717c478bd9Sstevel@tonic-gate 		__bravar[*ep] = ep[1];
2727c478bd9Sstevel@tonic-gate 		ep += 2;
2737c478bd9Sstevel@tonic-gate 		continue;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	case CDOT|PLUS:
2767c478bd9Sstevel@tonic-gate 		if (*lp++ == '\0')
2777c478bd9Sstevel@tonic-gate 			return (0);
278*02b0e3b7SToomas Soome 		/* FALLTHROUGH */
2797c478bd9Sstevel@tonic-gate 	case CDOT|STAR:
2807c478bd9Sstevel@tonic-gate 		curlp = lp;
281*02b0e3b7SToomas Soome 		while (*lp++)
282*02b0e3b7SToomas Soome 			;
2837c478bd9Sstevel@tonic-gate 		goto star;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	case CCHR|PLUS:
2867c478bd9Sstevel@tonic-gate 		if (*lp++ != *ep)
2877c478bd9Sstevel@tonic-gate 			return (0);
288*02b0e3b7SToomas Soome 		/* FALLTHROUGH */
2897c478bd9Sstevel@tonic-gate 	case CCHR|STAR:
2907c478bd9Sstevel@tonic-gate 		curlp = lp;
291*02b0e3b7SToomas Soome 		while (*lp++ == *ep)
292*02b0e3b7SToomas Soome 			;
2937c478bd9Sstevel@tonic-gate 		ep++;
2947c478bd9Sstevel@tonic-gate 		goto star;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	case PGRP:
2977c478bd9Sstevel@tonic-gate 	case PGRP|A256:
2987c478bd9Sstevel@tonic-gate 	case PGRP|A512:
2997c478bd9Sstevel@tonic-gate 	case PGRP|A768:
3007c478bd9Sstevel@tonic-gate 		if (!(lp = (char *)__advance(lp, ep+1)))
3017c478bd9Sstevel@tonic-gate 			return (0);
302*02b0e3b7SToomas Soome 		/* FALLTHROUGH */
3037c478bd9Sstevel@tonic-gate 	case SGRP|A768:
3047c478bd9Sstevel@tonic-gate 	case SGRP|A512:
3057c478bd9Sstevel@tonic-gate 	case SGRP|A256:
3067c478bd9Sstevel@tonic-gate 	case SGRP:
3077c478bd9Sstevel@tonic-gate 		i = (((ep[-1]&03) << 8) + (*ep & 0377));
3087c478bd9Sstevel@tonic-gate 		ep++;
3097c478bd9Sstevel@tonic-gate 		(void) __xpush(0, ep + i);
3107c478bd9Sstevel@tonic-gate 		(void) __xpush(1, curlp = lp);
3117c478bd9Sstevel@tonic-gate 		while (i = __advance(lp, ep))
3127c478bd9Sstevel@tonic-gate 			(void) __xpush(1, lp = (char *)i);
3137c478bd9Sstevel@tonic-gate 		ep = (char *)__xpop(0);
3147c478bd9Sstevel@tonic-gate 		gflg = 1;
3157c478bd9Sstevel@tonic-gate 		goto star;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	case CCL|PLUS:
3187c478bd9Sstevel@tonic-gate 	case NCCL|PLUS:
3197c478bd9Sstevel@tonic-gate 		if (!__cclass(ep, *lp++, ep[-1] == (CCL | PLUS)))
3207c478bd9Sstevel@tonic-gate 			return (0);
321*02b0e3b7SToomas Soome 		/* FALLTHROUGH */
3227c478bd9Sstevel@tonic-gate 	case CCL|STAR:
3237c478bd9Sstevel@tonic-gate 	case NCCL|STAR:
3247c478bd9Sstevel@tonic-gate 		curlp = lp;
3257c478bd9Sstevel@tonic-gate 		while (__cclass(ep, *lp++, ((ep[-1] == (CCL | STAR)) ||
326*02b0e3b7SToomas Soome 		    (ep[-1] == (CCL | PLUS)))))
327*02b0e3b7SToomas Soome 			;
3287c478bd9Sstevel@tonic-gate 		ep += *ep;
3297c478bd9Sstevel@tonic-gate 		goto star;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	star:
3327c478bd9Sstevel@tonic-gate 		do {
3337c478bd9Sstevel@tonic-gate 			if (!gflg) lp--;
3347c478bd9Sstevel@tonic-gate 			else if (!(lp = (char *)__xpop(1))) break;
3357c478bd9Sstevel@tonic-gate 			if (i = __advance(lp, ep))
3367c478bd9Sstevel@tonic-gate 				return (i);
3377c478bd9Sstevel@tonic-gate 		} while (lp > curlp);
3387c478bd9Sstevel@tonic-gate 		return (0);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	default:
3417c478bd9Sstevel@tonic-gate 		return (0);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate intptr_t
3477c478bd9Sstevel@tonic-gate __cclass(char *aset, char ac, intptr_t af)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	char *set, c;
3507c478bd9Sstevel@tonic-gate 	intptr_t n;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	set = (char *)aset;
3537c478bd9Sstevel@tonic-gate 	if ((c = ac) == 0)
3547c478bd9Sstevel@tonic-gate 		return (0);
3557c478bd9Sstevel@tonic-gate 	n = *set++;
3567c478bd9Sstevel@tonic-gate 	while (--n) {
3577c478bd9Sstevel@tonic-gate 		if (*set == MINUS) {
3587c478bd9Sstevel@tonic-gate 			if ((set[2] - set[1]) < 0)
3597c478bd9Sstevel@tonic-gate 				return (0);
3607c478bd9Sstevel@tonic-gate 			if (*++set <= c) {
3617c478bd9Sstevel@tonic-gate 				if (c <= *++set)
3627c478bd9Sstevel@tonic-gate 					return (af);
3637c478bd9Sstevel@tonic-gate 			} else
3647c478bd9Sstevel@tonic-gate 				++set;
3657c478bd9Sstevel@tonic-gate 			++set;
3667c478bd9Sstevel@tonic-gate 			n -= 2;
3677c478bd9Sstevel@tonic-gate 			continue;
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate 		if (*set++ == c)
3707c478bd9Sstevel@tonic-gate 			return (af);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	return (!af);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate intptr_t
3767c478bd9Sstevel@tonic-gate __xpush(intptr_t i, char *p)
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate 	if (__lptr_ >= __eptr_) {
3797c478bd9Sstevel@tonic-gate 		(void) write(2, "stack overflow\n", 15);
3807c478bd9Sstevel@tonic-gate 		(void) exit(1);
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 	if (i)
3837c478bd9Sstevel@tonic-gate 		*__lptr_++ = (intptr_t)p;
3847c478bd9Sstevel@tonic-gate 	else
3857c478bd9Sstevel@tonic-gate 		*__eptr_-- = (intptr_t)p;
3867c478bd9Sstevel@tonic-gate 	return (1);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate intptr_t
3907c478bd9Sstevel@tonic-gate __xpop(intptr_t i)
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate 	if (i)
3937c478bd9Sstevel@tonic-gate 		return ((__lptr_ < (intptr_t *)&__st[0]) ? 0 : *--__lptr_);
3947c478bd9Sstevel@tonic-gate 	else
3957c478bd9Sstevel@tonic-gate 		return ((__eptr_ > (intptr_t *)&__st[SSIZE]) ? 0 : *++__eptr_);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate intptr_t
3997c478bd9Sstevel@tonic-gate __getrnge(intptr_t *i, intptr_t *j, char *k)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	*i = (*k++&0377);
4027c478bd9Sstevel@tonic-gate 	if (*k == (char)-1)
4037c478bd9Sstevel@tonic-gate 		*j = 20000;
4047c478bd9Sstevel@tonic-gate 	else
4057c478bd9Sstevel@tonic-gate 		*j = ((*k&0377) - *i);
4067c478bd9Sstevel@tonic-gate 	return (1);
4077c478bd9Sstevel@tonic-gate }
408