summaryrefslogtreecommitdiff
path: root/usr/src/lib/libeti/form/common/regex.c
blob: f077f465f7313436a56a95a38046b183fd084955 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved	*/


/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright (c) 2018, Joyent, Inc.
 */

#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include "utility.h"

/*
 *	this code was taken from regcmp(3C)
 */
/*VARARGS*/
/*ARGSUSED*/

#define	SSIZE	50
#define	TGRP	48
#define	A256	01
#define	A512	02
#define	A768	03
#define	NBRA	10
#define	CIRCFL	32

#define	CBRA	60
#define	GRP	40
#define	SGRP	56
#define	PGRP	68
#define	EGRP	44
#define	RNGE	03
#define	CCHR	20
#define	CDOT	64
#define	CCL	24
#define	NCCL	8
#define	CDOL	28
#define	FCEOF	52 /* This was originally CEOF but it clashes with the header */
			/* definition so it was changed to FCEOF */
#define	CKET	12

#define	STAR	01
#define	PLUS	02
#define	MINUS	16

char	*__braslist[NBRA];
char	*__braelist[NBRA];
extern char *__loc1;
intptr_t	__bravar[NBRA];
intptr_t	*__st[SSIZE + 1];
intptr_t	*__eptr_, *__lptr_;
intptr_t	__cflg;

char *
libform_regex(char *addrc, char *addrl, char *a1)
{
	intptr_t cur, in;
	intptr_t *adx;
	char *p1, *p2;

	for (in = 0; in < NBRA; in++) {
		__braslist[in] = 0;
		__bravar[in] = -1;
	}
	__cflg = 0;
	cur = __execute(addrc, addrl);
	adx = (intptr_t *)&a1;
	for (in = 0; in < NBRA; in++) {
		if (((p1 = __braslist[in]) != 0) && (__bravar[in] >= 0)) {
			p2 = (char *)adx[__bravar[in]];
			while (p1 < __braelist[in]) *p2++ = *p1++;
			*p2 = '\0';
		}
	}
	if (!__cflg)
		return ((addrl == (char *)cur) ? (char *)0 : (char *)cur);
	else
		return ((char *)cur);
}

intptr_t
__execute(char *addrc, char *addrl)
{
	char *p1, *p2, c;
	intptr_t i;

	p1 = addrl;
	p2 = addrc;
	__eptr_ = (intptr_t *)&__st[SSIZE];
	__lptr_ = (intptr_t *)&__st[0];
	if (*p2 == CIRCFL) {
		__loc1 = p1;
		return ((i = __advance(p1, ++p2)) ? i : (intptr_t)addrl);
	}
	/* fast check for first character */
	if (*p2 == CCHR) {
		c = p2[1];
		do {
			if (*p1 != c)
				continue;
			__eptr_ = (intptr_t *)&__st[SSIZE];
			__lptr_ = (intptr_t *)&__st[0];
			if (i = __advance(p1, p2))  {
				__loc1 = p1;
				return (i);
			}
		} while (*p1++);
		return ((intptr_t)addrl);
	}
	/* regular algorithm */
	do {
		__eptr_ = (intptr_t *)&__st[SSIZE];
		__lptr_ = (intptr_t *)&__st[0];
		if (i = __advance(p1, p2))  {
			__loc1 = p1;
			return (i);
		}
	} while (*p1++);
	return ((intptr_t)addrl);
}

intptr_t
__advance(char *alp, char *aep)
{
	char *lp, *ep, *curlp;
	char *sep, *dp;
	intptr_t i, lcnt, dcnt, gflg;

	lp = alp;
	ep = aep;
	gflg = 0;
	for (; ; ) {
		switch (*ep++) {

	case CCHR:
		if (*ep++ == *lp++)
			continue;
		return (0);

	case EGRP|RNGE:
		return ((intptr_t)lp);
	case EGRP:
	case GRP:
		ep++;
		continue;

	case EGRP|STAR:
		(void) __xpop(0);
		/* FALLTHROUGH */
	case EGRP|PLUS:
		(void) __xpush(0, ++ep);
		return ((intptr_t)lp);

	case CDOT:
		if (*lp++)
			continue;
		return (0);

	case CDOL:
		if (*lp == 0)
			continue;
		lp++;
		return (0);

	case FCEOF:
		__cflg = 1;
		return ((intptr_t)lp);

	case TGRP:
	case TGRP|A768:
	case TGRP|A512:
	case TGRP|A256:
		i = (((ep[-1] & 03) << 8) + (*ep) & 0377);
		ep++;
		(void) __xpush(0, ep + i + 2);
		(void) __xpush(0, ++ep);
		(void) __xpush(0, ++ep);
		gflg = 1;
		(void) __getrnge(&lcnt, &dcnt, &ep[i]);
		while (lcnt--)
			if (!(lp = (char *)__advance(lp, ep)))
				return (0);
		(void) __xpush(1, curlp = lp);
		while (dcnt--)
			if (!(dp = (char *)__advance(lp, ep))) break;
			else
				(void) __xpush(1, lp = dp);
		ep = (char *)__xpop(0);
		goto star;
	case CCHR|RNGE:
		sep = ep++;
		(void) __getrnge(&lcnt, &dcnt, ep);
		while (lcnt--)
			if (*lp++ != *sep)
				return (0);
		curlp = lp;
		while (dcnt--)
			if (*lp++ != *sep) break;
		if (dcnt < 0) lp++;
		ep += 2;
		goto star;
	case CDOT|RNGE:
		(void) __getrnge(&lcnt, &dcnt, ep);
		while (lcnt--)
			if (*lp++ == '\0')
				return (0);
		curlp = lp;
		while (dcnt--)
			if (*lp++ == '\0') break;
		if (dcnt < 0) lp++;
		ep += 2;
		goto star;
	case CCL|RNGE:
	case NCCL|RNGE:
		(void) __getrnge(&lcnt, &dcnt, (ep + (*ep & 0377)));
		while (lcnt--)
			if (!__cclass(ep, *lp++, ep[-1] == (CCL | RNGE)))
				return (0);
		curlp = lp;
		while (dcnt--)
			if (!__cclass(ep, *lp++, ep[-1] == (CCL|RNGE)))
				break;
		if (dcnt < 0) lp++;
		ep += (*ep + 2);
		goto star;
	case CCL:
		if (__cclass(ep, *lp++, 1)) {
			ep += *ep;
			continue;
		}
		return (0);

	case NCCL:
		if (__cclass(ep, *lp++, 0)) {
			ep += *ep;
			continue;
		}
		return (0);

	case CBRA:
		__braslist[*ep++] = lp;
		continue;

	case CKET:
		__braelist[*ep] = lp;
		__bravar[*ep] = ep[1];
		ep += 2;
		continue;

	case CDOT|PLUS:
		if (*lp++ == '\0')
			return (0);
		/* FALLTHROUGH */
	case CDOT|STAR:
		curlp = lp;
		while (*lp++)
			;
		goto star;

	case CCHR|PLUS:
		if (*lp++ != *ep)
			return (0);
		/* FALLTHROUGH */
	case CCHR|STAR:
		curlp = lp;
		while (*lp++ == *ep)
			;
		ep++;
		goto star;

	case PGRP:
	case PGRP|A256:
	case PGRP|A512:
	case PGRP|A768:
		if (!(lp = (char *)__advance(lp, ep+1)))
			return (0);
		/* FALLTHROUGH */
	case SGRP|A768:
	case SGRP|A512:
	case SGRP|A256:
	case SGRP:
		i = (((ep[-1]&03) << 8) + (*ep & 0377));
		ep++;
		(void) __xpush(0, ep + i);
		(void) __xpush(1, curlp = lp);
		while (i = __advance(lp, ep))
			(void) __xpush(1, lp = (char *)i);
		ep = (char *)__xpop(0);
		gflg = 1;
		goto star;

	case CCL|PLUS:
	case NCCL|PLUS:
		if (!__cclass(ep, *lp++, ep[-1] == (CCL | PLUS)))
			return (0);
		/* FALLTHROUGH */
	case CCL|STAR:
	case NCCL|STAR:
		curlp = lp;
		while (__cclass(ep, *lp++, ((ep[-1] == (CCL | STAR)) ||
		    (ep[-1] == (CCL | PLUS)))))
			;
		ep += *ep;
		goto star;

	star:
		do {
			if (!gflg) lp--;
			else if (!(lp = (char *)__xpop(1))) break;
			if (i = __advance(lp, ep))
				return (i);
		} while (lp > curlp);
		return (0);

	default:
		return (0);
	}
	}
}

intptr_t
__cclass(char *aset, char ac, intptr_t af)
{
	char *set, c;
	intptr_t n;

	set = (char *)aset;
	if ((c = ac) == 0)
		return (0);
	n = *set++;
	while (--n) {
		if (*set == MINUS) {
			if ((set[2] - set[1]) < 0)
				return (0);
			if (*++set <= c) {
				if (c <= *++set)
					return (af);
			} else
				++set;
			++set;
			n -= 2;
			continue;
		}
		if (*set++ == c)
			return (af);
	}
	return (!af);
}

intptr_t
__xpush(intptr_t i, char *p)
{
	if (__lptr_ >= __eptr_) {
		(void) write(2, "stack overflow\n", 15);
		(void) exit(1);
	}
	if (i)
		*__lptr_++ = (intptr_t)p;
	else
		*__eptr_-- = (intptr_t)p;
	return (1);
}

intptr_t
__xpop(intptr_t i)
{
	if (i)
		return ((__lptr_ < (intptr_t *)&__st[0]) ? 0 : *--__lptr_);
	else
		return ((__eptr_ > (intptr_t *)&__st[SSIZE]) ? 0 : *++__eptr_);
}

intptr_t
__getrnge(intptr_t *i, intptr_t *j, char *k)
{
	*i = (*k++&0377);
	if (*k == (char)-1)
		*j = 20000;
	else
		*j = ((*k&0377) - *i);
	return (1);
}