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"
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 <unistd.h>
38*0Sstevel@tonic-gate #include "utility.h"
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate /*
41*0Sstevel@tonic-gate * this code was taken from REGCMP(3X)
42*0Sstevel@tonic-gate */
43*0Sstevel@tonic-gate /*VARARGS*/
44*0Sstevel@tonic-gate /*ARGSUSED*/
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate #define SSIZE 50
47*0Sstevel@tonic-gate #define TGRP 48
48*0Sstevel@tonic-gate #define A256 01
49*0Sstevel@tonic-gate #define A512 02
50*0Sstevel@tonic-gate #define A768 03
51*0Sstevel@tonic-gate #define NBRA 10
52*0Sstevel@tonic-gate #define CIRCFL 32
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate #define CBRA 60
55*0Sstevel@tonic-gate #define GRP 40
56*0Sstevel@tonic-gate #define SGRP 56
57*0Sstevel@tonic-gate #define PGRP 68
58*0Sstevel@tonic-gate #define EGRP 44
59*0Sstevel@tonic-gate #define RNGE 03
60*0Sstevel@tonic-gate #define CCHR 20
61*0Sstevel@tonic-gate #define CDOT 64
62*0Sstevel@tonic-gate #define CCL 24
63*0Sstevel@tonic-gate #define NCCL 8
64*0Sstevel@tonic-gate #define CDOL 28
65*0Sstevel@tonic-gate #define FCEOF 52 /* This was originally CEOF but it clashes with the header */
66*0Sstevel@tonic-gate /* definition so it was changed to FCEOF */
67*0Sstevel@tonic-gate #define CKET 12
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate #define STAR 01
70*0Sstevel@tonic-gate #define PLUS 02
71*0Sstevel@tonic-gate #define MINUS 16
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate char *__braslist[NBRA];
74*0Sstevel@tonic-gate char *__braelist[NBRA];
75*0Sstevel@tonic-gate char *__loc1;
76*0Sstevel@tonic-gate intptr_t __bravar[NBRA];
77*0Sstevel@tonic-gate intptr_t *__st[SSIZE + 1];
78*0Sstevel@tonic-gate intptr_t *__eptr_, *__lptr_;
79*0Sstevel@tonic-gate intptr_t __cflg;
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate char *
libform_regex(char * addrc,char * addrl,char * a1)82*0Sstevel@tonic-gate libform_regex(char *addrc, char *addrl, char *a1)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate intptr_t cur, in;
85*0Sstevel@tonic-gate intptr_t *adx;
86*0Sstevel@tonic-gate char *p1, *p2;
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate for (in = 0; in < NBRA; in++) {
89*0Sstevel@tonic-gate __braslist[in] = 0;
90*0Sstevel@tonic-gate __bravar[in] = -1;
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate __cflg = 0;
93*0Sstevel@tonic-gate cur = __execute(addrc, addrl);
94*0Sstevel@tonic-gate adx = (intptr_t *)&a1;
95*0Sstevel@tonic-gate for (in = 0; in < NBRA; in++) {
96*0Sstevel@tonic-gate if (((p1 = __braslist[in]) != 0) && (__bravar[in] >= 0)) {
97*0Sstevel@tonic-gate p2 = (char *)adx[__bravar[in]];
98*0Sstevel@tonic-gate while (p1 < __braelist[in]) *p2++ = *p1++;
99*0Sstevel@tonic-gate *p2 = '\0';
100*0Sstevel@tonic-gate }
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate if (!__cflg)
103*0Sstevel@tonic-gate return ((addrl == (char *)cur) ? (char *)0 : (char *)cur);
104*0Sstevel@tonic-gate else
105*0Sstevel@tonic-gate return ((char *)cur);
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate intptr_t
__execute(char * addrc,char * addrl)109*0Sstevel@tonic-gate __execute(char *addrc, char *addrl)
110*0Sstevel@tonic-gate {
111*0Sstevel@tonic-gate char *p1, *p2, c;
112*0Sstevel@tonic-gate intptr_t i;
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate p1 = addrl;
115*0Sstevel@tonic-gate p2 = addrc;
116*0Sstevel@tonic-gate __eptr_ = (intptr_t *)&__st[SSIZE];
117*0Sstevel@tonic-gate __lptr_ = (intptr_t *)&__st[0];
118*0Sstevel@tonic-gate if (*p2 == CIRCFL) {
119*0Sstevel@tonic-gate __loc1 = p1;
120*0Sstevel@tonic-gate return ((i = __advance(p1, ++p2)) ? i : (intptr_t)addrl);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate /* fast check for first character */
123*0Sstevel@tonic-gate if (*p2 == CCHR) {
124*0Sstevel@tonic-gate c = p2[1];
125*0Sstevel@tonic-gate do {
126*0Sstevel@tonic-gate if (*p1 != c)
127*0Sstevel@tonic-gate continue;
128*0Sstevel@tonic-gate __eptr_ = (intptr_t *)&__st[SSIZE];
129*0Sstevel@tonic-gate __lptr_ = (intptr_t *)&__st[0];
130*0Sstevel@tonic-gate if (i = __advance(p1, p2)) {
131*0Sstevel@tonic-gate __loc1 = p1;
132*0Sstevel@tonic-gate return (i);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate } while (*p1++);
135*0Sstevel@tonic-gate return ((intptr_t)addrl);
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate /* regular algorithm */
138*0Sstevel@tonic-gate do {
139*0Sstevel@tonic-gate __eptr_ = (intptr_t *)&__st[SSIZE];
140*0Sstevel@tonic-gate __lptr_ = (intptr_t *)&__st[0];
141*0Sstevel@tonic-gate if (i = __advance(p1, p2)) {
142*0Sstevel@tonic-gate __loc1 = p1;
143*0Sstevel@tonic-gate return (i);
144*0Sstevel@tonic-gate }
145*0Sstevel@tonic-gate } while (*p1++);
146*0Sstevel@tonic-gate return ((intptr_t)addrl);
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate intptr_t
__advance(char * alp,char * aep)150*0Sstevel@tonic-gate __advance(char *alp, char *aep)
151*0Sstevel@tonic-gate {
152*0Sstevel@tonic-gate char *lp, *ep, *curlp;
153*0Sstevel@tonic-gate char *sep, *dp;
154*0Sstevel@tonic-gate intptr_t i, lcnt, dcnt, gflg;
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate lp = alp;
157*0Sstevel@tonic-gate ep = aep;
158*0Sstevel@tonic-gate gflg = 0;
159*0Sstevel@tonic-gate for (; ; ) {
160*0Sstevel@tonic-gate switch (*ep++) {
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate case CCHR:
163*0Sstevel@tonic-gate if (*ep++ == *lp++)
164*0Sstevel@tonic-gate continue;
165*0Sstevel@tonic-gate return (0);
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate case EGRP|RNGE:
168*0Sstevel@tonic-gate return ((intptr_t)lp);
169*0Sstevel@tonic-gate case EGRP:
170*0Sstevel@tonic-gate case GRP:
171*0Sstevel@tonic-gate ep++;
172*0Sstevel@tonic-gate continue;
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate case EGRP|STAR:
175*0Sstevel@tonic-gate (void) __xpop(0);
176*0Sstevel@tonic-gate case EGRP|PLUS:
177*0Sstevel@tonic-gate (void) __xpush(0, ++ep);
178*0Sstevel@tonic-gate return ((intptr_t)lp);
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate case CDOT:
181*0Sstevel@tonic-gate if (*lp++)
182*0Sstevel@tonic-gate continue;
183*0Sstevel@tonic-gate return (0);
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate case CDOL:
186*0Sstevel@tonic-gate if (*lp == 0)
187*0Sstevel@tonic-gate continue;
188*0Sstevel@tonic-gate lp++;
189*0Sstevel@tonic-gate return (0);
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gate case FCEOF:
192*0Sstevel@tonic-gate __cflg = 1;
193*0Sstevel@tonic-gate return ((intptr_t)lp);
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gate case TGRP:
196*0Sstevel@tonic-gate case TGRP|A768:
197*0Sstevel@tonic-gate case TGRP|A512:
198*0Sstevel@tonic-gate case TGRP|A256:
199*0Sstevel@tonic-gate i = (((ep[-1] & 03) << 8) + (*ep) & 0377);
200*0Sstevel@tonic-gate ep++;
201*0Sstevel@tonic-gate (void) __xpush(0, ep + i + 2);
202*0Sstevel@tonic-gate (void) __xpush(0, ++ep);
203*0Sstevel@tonic-gate (void) __xpush(0, ++ep);
204*0Sstevel@tonic-gate gflg = 1;
205*0Sstevel@tonic-gate (void) __getrnge(&lcnt, &dcnt, &ep[i]);
206*0Sstevel@tonic-gate while (lcnt--)
207*0Sstevel@tonic-gate if (!(lp = (char *)__advance(lp, ep)))
208*0Sstevel@tonic-gate return (0);
209*0Sstevel@tonic-gate (void) __xpush(1, curlp = lp);
210*0Sstevel@tonic-gate while (dcnt--)
211*0Sstevel@tonic-gate if (!(dp = (char *)__advance(lp, ep))) break;
212*0Sstevel@tonic-gate else
213*0Sstevel@tonic-gate (void) __xpush(1, lp = dp);
214*0Sstevel@tonic-gate ep = (char *)__xpop(0);
215*0Sstevel@tonic-gate goto star;
216*0Sstevel@tonic-gate case CCHR|RNGE:
217*0Sstevel@tonic-gate sep = ep++;
218*0Sstevel@tonic-gate (void) __getrnge(&lcnt, &dcnt, ep);
219*0Sstevel@tonic-gate while (lcnt--)
220*0Sstevel@tonic-gate if (*lp++ != *sep)
221*0Sstevel@tonic-gate return (0);
222*0Sstevel@tonic-gate curlp = lp;
223*0Sstevel@tonic-gate while (dcnt--)
224*0Sstevel@tonic-gate if (*lp++ != *sep) break;
225*0Sstevel@tonic-gate if (dcnt < 0) lp++;
226*0Sstevel@tonic-gate ep += 2;
227*0Sstevel@tonic-gate goto star;
228*0Sstevel@tonic-gate case CDOT|RNGE:
229*0Sstevel@tonic-gate (void) __getrnge(&lcnt, &dcnt, ep);
230*0Sstevel@tonic-gate while (lcnt--)
231*0Sstevel@tonic-gate if (*lp++ == '\0')
232*0Sstevel@tonic-gate return (0);
233*0Sstevel@tonic-gate curlp = lp;
234*0Sstevel@tonic-gate while (dcnt--)
235*0Sstevel@tonic-gate if (*lp++ == '\0') break;
236*0Sstevel@tonic-gate if (dcnt < 0) lp++;
237*0Sstevel@tonic-gate ep += 2;
238*0Sstevel@tonic-gate goto star;
239*0Sstevel@tonic-gate case CCL|RNGE:
240*0Sstevel@tonic-gate case NCCL|RNGE:
241*0Sstevel@tonic-gate (void) __getrnge(&lcnt, &dcnt, (ep + (*ep & 0377)));
242*0Sstevel@tonic-gate while (lcnt--)
243*0Sstevel@tonic-gate if (!__cclass(ep, *lp++, ep[-1] == (CCL | RNGE)))
244*0Sstevel@tonic-gate return (0);
245*0Sstevel@tonic-gate curlp = lp;
246*0Sstevel@tonic-gate while (dcnt--)
247*0Sstevel@tonic-gate if (!__cclass(ep, *lp++, ep[-1] == (CCL|RNGE)))
248*0Sstevel@tonic-gate break;
249*0Sstevel@tonic-gate if (dcnt < 0) lp++;
250*0Sstevel@tonic-gate ep += (*ep + 2);
251*0Sstevel@tonic-gate goto star;
252*0Sstevel@tonic-gate case CCL:
253*0Sstevel@tonic-gate if (__cclass(ep, *lp++, 1)) {
254*0Sstevel@tonic-gate ep += *ep;
255*0Sstevel@tonic-gate continue;
256*0Sstevel@tonic-gate }
257*0Sstevel@tonic-gate return (0);
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gate case NCCL:
260*0Sstevel@tonic-gate if (__cclass(ep, *lp++, 0)) {
261*0Sstevel@tonic-gate ep += *ep;
262*0Sstevel@tonic-gate continue;
263*0Sstevel@tonic-gate }
264*0Sstevel@tonic-gate return (0);
265*0Sstevel@tonic-gate
266*0Sstevel@tonic-gate case CBRA:
267*0Sstevel@tonic-gate __braslist[*ep++] = lp;
268*0Sstevel@tonic-gate continue;
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate case CKET:
271*0Sstevel@tonic-gate __braelist[*ep] = lp;
272*0Sstevel@tonic-gate __bravar[*ep] = ep[1];
273*0Sstevel@tonic-gate ep += 2;
274*0Sstevel@tonic-gate continue;
275*0Sstevel@tonic-gate
276*0Sstevel@tonic-gate case CDOT|PLUS:
277*0Sstevel@tonic-gate if (*lp++ == '\0')
278*0Sstevel@tonic-gate return (0);
279*0Sstevel@tonic-gate case CDOT|STAR:
280*0Sstevel@tonic-gate curlp = lp;
281*0Sstevel@tonic-gate while (*lp++);
282*0Sstevel@tonic-gate goto star;
283*0Sstevel@tonic-gate
284*0Sstevel@tonic-gate case CCHR|PLUS:
285*0Sstevel@tonic-gate if (*lp++ != *ep)
286*0Sstevel@tonic-gate return (0);
287*0Sstevel@tonic-gate case CCHR|STAR:
288*0Sstevel@tonic-gate curlp = lp;
289*0Sstevel@tonic-gate while (*lp++ == *ep);
290*0Sstevel@tonic-gate ep++;
291*0Sstevel@tonic-gate goto star;
292*0Sstevel@tonic-gate
293*0Sstevel@tonic-gate case PGRP:
294*0Sstevel@tonic-gate case PGRP|A256:
295*0Sstevel@tonic-gate case PGRP|A512:
296*0Sstevel@tonic-gate case PGRP|A768:
297*0Sstevel@tonic-gate if (!(lp = (char *)__advance(lp, ep+1)))
298*0Sstevel@tonic-gate return (0);
299*0Sstevel@tonic-gate case SGRP|A768:
300*0Sstevel@tonic-gate case SGRP|A512:
301*0Sstevel@tonic-gate case SGRP|A256:
302*0Sstevel@tonic-gate case SGRP:
303*0Sstevel@tonic-gate i = (((ep[-1]&03) << 8) + (*ep & 0377));
304*0Sstevel@tonic-gate ep++;
305*0Sstevel@tonic-gate (void) __xpush(0, ep + i);
306*0Sstevel@tonic-gate (void) __xpush(1, curlp = lp);
307*0Sstevel@tonic-gate while (i = __advance(lp, ep))
308*0Sstevel@tonic-gate (void) __xpush(1, lp = (char *)i);
309*0Sstevel@tonic-gate ep = (char *)__xpop(0);
310*0Sstevel@tonic-gate gflg = 1;
311*0Sstevel@tonic-gate goto star;
312*0Sstevel@tonic-gate
313*0Sstevel@tonic-gate case CCL|PLUS:
314*0Sstevel@tonic-gate case NCCL|PLUS:
315*0Sstevel@tonic-gate if (!__cclass(ep, *lp++, ep[-1] == (CCL | PLUS)))
316*0Sstevel@tonic-gate return (0);
317*0Sstevel@tonic-gate case CCL|STAR:
318*0Sstevel@tonic-gate case NCCL|STAR:
319*0Sstevel@tonic-gate curlp = lp;
320*0Sstevel@tonic-gate while (__cclass(ep, *lp++, ((ep[-1] == (CCL | STAR)) ||
321*0Sstevel@tonic-gate (ep[-1] == (CCL | PLUS)))));
322*0Sstevel@tonic-gate ep += *ep;
323*0Sstevel@tonic-gate goto star;
324*0Sstevel@tonic-gate
325*0Sstevel@tonic-gate star:
326*0Sstevel@tonic-gate do {
327*0Sstevel@tonic-gate if (!gflg) lp--;
328*0Sstevel@tonic-gate else if (!(lp = (char *)__xpop(1))) break;
329*0Sstevel@tonic-gate if (i = __advance(lp, ep))
330*0Sstevel@tonic-gate return (i);
331*0Sstevel@tonic-gate } while (lp > curlp);
332*0Sstevel@tonic-gate return (0);
333*0Sstevel@tonic-gate
334*0Sstevel@tonic-gate default:
335*0Sstevel@tonic-gate return (0);
336*0Sstevel@tonic-gate }
337*0Sstevel@tonic-gate }
338*0Sstevel@tonic-gate }
339*0Sstevel@tonic-gate
340*0Sstevel@tonic-gate intptr_t
__cclass(char * aset,char ac,intptr_t af)341*0Sstevel@tonic-gate __cclass(char *aset, char ac, intptr_t af)
342*0Sstevel@tonic-gate {
343*0Sstevel@tonic-gate char *set, c;
344*0Sstevel@tonic-gate intptr_t n;
345*0Sstevel@tonic-gate
346*0Sstevel@tonic-gate set = (char *)aset;
347*0Sstevel@tonic-gate if ((c = ac) == 0)
348*0Sstevel@tonic-gate return (0);
349*0Sstevel@tonic-gate n = *set++;
350*0Sstevel@tonic-gate while (--n) {
351*0Sstevel@tonic-gate if (*set == MINUS) {
352*0Sstevel@tonic-gate if ((set[2] - set[1]) < 0)
353*0Sstevel@tonic-gate return (0);
354*0Sstevel@tonic-gate if (*++set <= c) {
355*0Sstevel@tonic-gate if (c <= *++set)
356*0Sstevel@tonic-gate return (af);
357*0Sstevel@tonic-gate } else
358*0Sstevel@tonic-gate ++set;
359*0Sstevel@tonic-gate ++set;
360*0Sstevel@tonic-gate n -= 2;
361*0Sstevel@tonic-gate continue;
362*0Sstevel@tonic-gate }
363*0Sstevel@tonic-gate if (*set++ == c)
364*0Sstevel@tonic-gate return (af);
365*0Sstevel@tonic-gate }
366*0Sstevel@tonic-gate return (!af);
367*0Sstevel@tonic-gate }
368*0Sstevel@tonic-gate
369*0Sstevel@tonic-gate intptr_t
__xpush(intptr_t i,char * p)370*0Sstevel@tonic-gate __xpush(intptr_t i, char *p)
371*0Sstevel@tonic-gate {
372*0Sstevel@tonic-gate if (__lptr_ >= __eptr_) {
373*0Sstevel@tonic-gate (void) write(2, "stack overflow\n", 15);
374*0Sstevel@tonic-gate (void) exit(1);
375*0Sstevel@tonic-gate }
376*0Sstevel@tonic-gate if (i)
377*0Sstevel@tonic-gate *__lptr_++ = (intptr_t)p;
378*0Sstevel@tonic-gate else
379*0Sstevel@tonic-gate *__eptr_-- = (intptr_t)p;
380*0Sstevel@tonic-gate return (1);
381*0Sstevel@tonic-gate }
382*0Sstevel@tonic-gate
383*0Sstevel@tonic-gate intptr_t
__xpop(intptr_t i)384*0Sstevel@tonic-gate __xpop(intptr_t i)
385*0Sstevel@tonic-gate {
386*0Sstevel@tonic-gate if (i)
387*0Sstevel@tonic-gate return ((__lptr_ < (intptr_t *)&__st[0]) ? 0 : *--__lptr_);
388*0Sstevel@tonic-gate else
389*0Sstevel@tonic-gate return ((__eptr_ > (intptr_t *)&__st[SSIZE]) ? 0 : *++__eptr_);
390*0Sstevel@tonic-gate }
391*0Sstevel@tonic-gate
392*0Sstevel@tonic-gate intptr_t
__getrnge(intptr_t * i,intptr_t * j,char * k)393*0Sstevel@tonic-gate __getrnge(intptr_t *i, intptr_t *j, char *k)
394*0Sstevel@tonic-gate {
395*0Sstevel@tonic-gate *i = (*k++&0377);
396*0Sstevel@tonic-gate if (*k == (char)-1)
397*0Sstevel@tonic-gate *j = 20000;
398*0Sstevel@tonic-gate else
399*0Sstevel@tonic-gate *j = ((*k&0377) - *i);
400*0Sstevel@tonic-gate return (1);
401*0Sstevel@tonic-gate }
402