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 */
23518a3de1SToomas Soome /* 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
31981fe1b1SJohn Levon /*
32981fe1b1SJohn Levon * Copyright (c) 2018, Joyent, Inc.
33981fe1b1SJohn Levon */
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include "utility.h"
387c478bd9Sstevel@tonic-gate
39*bbf21555SRichard Lowe /* this code was taken from regcmp(3C) */
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #define SSIZE 16
427c478bd9Sstevel@tonic-gate #define TGRP 48
437c478bd9Sstevel@tonic-gate #define A256 02
447c478bd9Sstevel@tonic-gate #define ZERO 01
457c478bd9Sstevel@tonic-gate #define NBRA 10
467c478bd9Sstevel@tonic-gate #define CIRCFL 32;
477c478bd9Sstevel@tonic-gate #define SLOP 5
487c478bd9Sstevel@tonic-gate #define FEOF 0 /* This was originally EOF but it clashes with the header */
497c478bd9Sstevel@tonic-gate /* definition so it was changed to FEOF */
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate #define CBRA 60
527c478bd9Sstevel@tonic-gate #define GRP 40
537c478bd9Sstevel@tonic-gate #define SGRP 56
547c478bd9Sstevel@tonic-gate #define PGRP 68
557c478bd9Sstevel@tonic-gate #define EGRP 44
567c478bd9Sstevel@tonic-gate #define RNGE 03
577c478bd9Sstevel@tonic-gate #define CCHR 20
587c478bd9Sstevel@tonic-gate #define CDOT 64
597c478bd9Sstevel@tonic-gate #define CCL 24
607c478bd9Sstevel@tonic-gate #define NCCL 8
617c478bd9Sstevel@tonic-gate #define CDOL 28
627c478bd9Sstevel@tonic-gate #define FCEOF 52 /* This was originally CEOF but it clashes with the header */
637c478bd9Sstevel@tonic-gate /* definition so it was changed to FCEOF */
647c478bd9Sstevel@tonic-gate #define CKET 12
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate #define STAR 01
677c478bd9Sstevel@tonic-gate #define PLUS 02
687c478bd9Sstevel@tonic-gate #define MINUS 16
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate intptr_t *__sp_;
717c478bd9Sstevel@tonic-gate intptr_t *__stmax;
72518a3de1SToomas Soome extern int __i_size;
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
757c478bd9Sstevel@tonic-gate char *
libform_regcmp(char * cs1,char * cs2)767c478bd9Sstevel@tonic-gate libform_regcmp(char *cs1, char *cs2)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate char c;
797c478bd9Sstevel@tonic-gate char *ep, *sp;
807c478bd9Sstevel@tonic-gate int *adx;
817c478bd9Sstevel@tonic-gate int i, cflg;
827c478bd9Sstevel@tonic-gate char *lastep, *sep, *eptr;
837c478bd9Sstevel@tonic-gate int nbra, ngrp;
847c478bd9Sstevel@tonic-gate int cclcnt;
857c478bd9Sstevel@tonic-gate intptr_t stack[SSIZE];
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate __sp_ = stack;
887c478bd9Sstevel@tonic-gate *__sp_ = -1;
897c478bd9Sstevel@tonic-gate __stmax = &stack[SSIZE];
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate adx = (int *)&cs1;
927c478bd9Sstevel@tonic-gate i = nbra = ngrp = 0;
937c478bd9Sstevel@tonic-gate while (*adx)
947c478bd9Sstevel@tonic-gate i += __size((char *)(intptr_t)*adx++);
957c478bd9Sstevel@tonic-gate adx = (int *)&cs1;
967c478bd9Sstevel@tonic-gate sp = (char *)(intptr_t)*adx++;
977c478bd9Sstevel@tonic-gate if ((sep = ep = malloc((unsigned)(2 * i + SLOP))) == NULL)
987c478bd9Sstevel@tonic-gate return (NULL);
997c478bd9Sstevel@tonic-gate if ((c = *sp++) == FEOF)
1007c478bd9Sstevel@tonic-gate goto cerror;
1017c478bd9Sstevel@tonic-gate if (c == '^') {
1027c478bd9Sstevel@tonic-gate c = *sp++;
1037c478bd9Sstevel@tonic-gate *ep++ = CIRCFL;
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate if ((c == '*') || (c == '+') || (c == '{'))
1067c478bd9Sstevel@tonic-gate goto cerror;
1077c478bd9Sstevel@tonic-gate sp--;
1087c478bd9Sstevel@tonic-gate for (;;) {
1097c478bd9Sstevel@tonic-gate if ((c = *sp++) == FEOF) {
1107c478bd9Sstevel@tonic-gate if (*adx) {
1117c478bd9Sstevel@tonic-gate sp = (char *)(intptr_t)*adx++;
1127c478bd9Sstevel@tonic-gate continue;
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate *ep++ = FCEOF;
1157c478bd9Sstevel@tonic-gate if (--nbra > NBRA || *__sp_ != -1)
1167c478bd9Sstevel@tonic-gate goto cerror;
117518a3de1SToomas Soome __i_size = (int)(ep - sep);
1187c478bd9Sstevel@tonic-gate return (sep);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate if ((c != '*') && (c != '{') && (c != '+'))
1217c478bd9Sstevel@tonic-gate lastep = ep;
1227c478bd9Sstevel@tonic-gate switch (c) {
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate case '(':
1257c478bd9Sstevel@tonic-gate if (!__rpush(ep)) goto cerror;
1267c478bd9Sstevel@tonic-gate *ep++ = CBRA;
1277c478bd9Sstevel@tonic-gate *ep++ = -1;
1287c478bd9Sstevel@tonic-gate continue;
1297c478bd9Sstevel@tonic-gate case ')':
1307c478bd9Sstevel@tonic-gate if (!(eptr = (char *)__rpop())) goto cerror;
1317c478bd9Sstevel@tonic-gate if ((c = *sp++) == '$') {
1327c478bd9Sstevel@tonic-gate if ('0' > (c = *sp++) || c > '9')
1337c478bd9Sstevel@tonic-gate goto cerror;
1347c478bd9Sstevel@tonic-gate *ep++ = CKET;
1357c478bd9Sstevel@tonic-gate *ep++ = *++eptr = nbra++;
1367c478bd9Sstevel@tonic-gate *ep++ = (c-'0');
1377c478bd9Sstevel@tonic-gate continue;
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate *ep++ = EGRP;
1407c478bd9Sstevel@tonic-gate *ep++ = ngrp++;
1417c478bd9Sstevel@tonic-gate sp--;
1427c478bd9Sstevel@tonic-gate switch (c) {
1437c478bd9Sstevel@tonic-gate case '+':
1447c478bd9Sstevel@tonic-gate *eptr = PGRP;
1457c478bd9Sstevel@tonic-gate break;
1467c478bd9Sstevel@tonic-gate case '*':
1477c478bd9Sstevel@tonic-gate *eptr = SGRP;
1487c478bd9Sstevel@tonic-gate break;
1497c478bd9Sstevel@tonic-gate case '{':
1507c478bd9Sstevel@tonic-gate *eptr = TGRP;
1517c478bd9Sstevel@tonic-gate break;
1527c478bd9Sstevel@tonic-gate default:
1537c478bd9Sstevel@tonic-gate *eptr = GRP;
1547c478bd9Sstevel@tonic-gate continue;
1557c478bd9Sstevel@tonic-gate }
156518a3de1SToomas Soome i = (int)(ep - eptr - 2);
1577c478bd9Sstevel@tonic-gate for (cclcnt = 0; i >= 256; cclcnt++)
1587c478bd9Sstevel@tonic-gate i -= 256;
1597c478bd9Sstevel@tonic-gate if (cclcnt > 3) goto cerror;
1607c478bd9Sstevel@tonic-gate *eptr |= cclcnt;
161518a3de1SToomas Soome *++eptr = (char)i;
1627c478bd9Sstevel@tonic-gate continue;
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate case '\\':
1657c478bd9Sstevel@tonic-gate *ep++ = CCHR;
1667c478bd9Sstevel@tonic-gate if ((c = *sp++) == FEOF)
1677c478bd9Sstevel@tonic-gate goto cerror;
1687c478bd9Sstevel@tonic-gate *ep++ = c;
1697c478bd9Sstevel@tonic-gate continue;
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate case '{':
1727c478bd9Sstevel@tonic-gate *lastep |= RNGE;
1737c478bd9Sstevel@tonic-gate cflg = 0;
1747c478bd9Sstevel@tonic-gate nlim:
1757c478bd9Sstevel@tonic-gate if ((c = *sp++) == '}') goto cerror;
1767c478bd9Sstevel@tonic-gate i = 0;
1777c478bd9Sstevel@tonic-gate do {
1787c478bd9Sstevel@tonic-gate if ('0' <= c && c <= '9')
1797c478bd9Sstevel@tonic-gate i = (i*10+(c-'0'));
1807c478bd9Sstevel@tonic-gate else goto cerror;
1817c478bd9Sstevel@tonic-gate } while (((c = *sp++) != '}') && (c != ','));
1827c478bd9Sstevel@tonic-gate if (i > 255) goto cerror;
183518a3de1SToomas Soome *ep++ = (char)i;
1847c478bd9Sstevel@tonic-gate if (c == ',') {
1857c478bd9Sstevel@tonic-gate if (cflg++) goto cerror;
1867c478bd9Sstevel@tonic-gate if ((c = *sp++) == '}') {
1877c478bd9Sstevel@tonic-gate *ep++ = -1;
1887c478bd9Sstevel@tonic-gate continue;
1897c478bd9Sstevel@tonic-gate } else {
1907c478bd9Sstevel@tonic-gate sp--;
1917c478bd9Sstevel@tonic-gate goto nlim;
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate if (!cflg)
195518a3de1SToomas Soome *ep++ = (char)i;
1967c478bd9Sstevel@tonic-gate else if ((ep[-1]&0377) < (ep[-2]&0377))
1977c478bd9Sstevel@tonic-gate goto cerror;
1987c478bd9Sstevel@tonic-gate continue;
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate case '.':
2017c478bd9Sstevel@tonic-gate *ep++ = CDOT;
2027c478bd9Sstevel@tonic-gate continue;
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate case '+':
2057c478bd9Sstevel@tonic-gate if (*lastep == CBRA || *lastep == CKET)
2067c478bd9Sstevel@tonic-gate goto cerror;
2077c478bd9Sstevel@tonic-gate *lastep |= PLUS;
2087c478bd9Sstevel@tonic-gate continue;
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate case '*':
2117c478bd9Sstevel@tonic-gate if (*lastep == CBRA || *lastep == CKET)
212981fe1b1SJohn Levon goto cerror;
2137c478bd9Sstevel@tonic-gate *lastep |= STAR;
2147c478bd9Sstevel@tonic-gate continue;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate case '$':
2177c478bd9Sstevel@tonic-gate if ((*sp != FEOF) || (*adx))
2187c478bd9Sstevel@tonic-gate goto defchar;
2197c478bd9Sstevel@tonic-gate *ep++ = CDOL;
2207c478bd9Sstevel@tonic-gate continue;
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate case '[':
2237c478bd9Sstevel@tonic-gate *ep++ = CCL;
2247c478bd9Sstevel@tonic-gate *ep++ = 0;
2257c478bd9Sstevel@tonic-gate cclcnt = 1;
2267c478bd9Sstevel@tonic-gate if ((c = *sp++) == '^') {
2277c478bd9Sstevel@tonic-gate c = *sp++;
2287c478bd9Sstevel@tonic-gate ep[-2] = NCCL;
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate do {
2317c478bd9Sstevel@tonic-gate if (c == FEOF)
2327c478bd9Sstevel@tonic-gate goto cerror;
2337c478bd9Sstevel@tonic-gate if ((c == '-') && (cclcnt > 1) &&
2347c478bd9Sstevel@tonic-gate (*sp != ']')) {
2357c478bd9Sstevel@tonic-gate *ep = ep[-1];
2367c478bd9Sstevel@tonic-gate ep++;
2377c478bd9Sstevel@tonic-gate ep[-2] = MINUS;
2387c478bd9Sstevel@tonic-gate cclcnt++;
2397c478bd9Sstevel@tonic-gate continue;
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate *ep++ = c;
2427c478bd9Sstevel@tonic-gate cclcnt++;
2437c478bd9Sstevel@tonic-gate } while ((c = *sp++) != ']');
244518a3de1SToomas Soome lastep[1] = (char)cclcnt;
2457c478bd9Sstevel@tonic-gate continue;
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate defchar:
2487c478bd9Sstevel@tonic-gate default:
2497c478bd9Sstevel@tonic-gate *ep++ = CCHR;
2507c478bd9Sstevel@tonic-gate *ep++ = c;
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate cerror:
2547c478bd9Sstevel@tonic-gate free(sep);
2557c478bd9Sstevel@tonic-gate return (0);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate int
__size(char * strg)2597c478bd9Sstevel@tonic-gate __size(char *strg)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate int i;
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate i = 1;
2647c478bd9Sstevel@tonic-gate while (*strg++)
2657c478bd9Sstevel@tonic-gate i++;
2667c478bd9Sstevel@tonic-gate return (i);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate intptr_t
__rpop(void)2707c478bd9Sstevel@tonic-gate __rpop(void)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate return ((*__sp_ == -1)?0:*__sp_--);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate int
__rpush(char * ptr)2767c478bd9Sstevel@tonic-gate __rpush(char *ptr)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate if (__sp_ >= __stmax)
2797c478bd9Sstevel@tonic-gate return (0);
2807c478bd9Sstevel@tonic-gate *++__sp_ = (intptr_t)ptr;
2817c478bd9Sstevel@tonic-gate return (1);
2827c478bd9Sstevel@tonic-gate }
283