xref: /titanic_53/usr/src/cmd/regcmp/regcmp.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997, by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.6.1.1	*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <locale.h>
35*7c478bd9Sstevel@tonic-gate #include <libgen.h>
36*7c478bd9Sstevel@tonic-gate FILE *fopen();
37*7c478bd9Sstevel@tonic-gate FILE *iobuf;
38*7c478bd9Sstevel@tonic-gate int gotflg;
39*7c478bd9Sstevel@tonic-gate char ofile[64];
40*7c478bd9Sstevel@tonic-gate char a1[1024];
41*7c478bd9Sstevel@tonic-gate char a2[64];
42*7c478bd9Sstevel@tonic-gate int c;
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate main(argc, argv) char **argv;
45*7c478bd9Sstevel@tonic-gate {
46*7c478bd9Sstevel@tonic-gate 	register char *name, *str, *v;
47*7c478bd9Sstevel@tonic-gate 	char *bp, *cp, *sv;
48*7c478bd9Sstevel@tonic-gate 	char *message;
49*7c478bd9Sstevel@tonic-gate 	int j, k, cflg = 0;
50*7c478bd9Sstevel@tonic-gate 	int status = 0;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
53*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
54*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
55*7c478bd9Sstevel@tonic-gate #endif
56*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
57*7c478bd9Sstevel@tonic-gate 	if (argc > 1 && *argv[1] == '-') {
58*7c478bd9Sstevel@tonic-gate 		cflg++;
59*7c478bd9Sstevel@tonic-gate 		++argv;
60*7c478bd9Sstevel@tonic-gate 		argc--;
61*7c478bd9Sstevel@tonic-gate 	} else cflg = 0;
62*7c478bd9Sstevel@tonic-gate 	while (--argc) {
63*7c478bd9Sstevel@tonic-gate 		++argv;
64*7c478bd9Sstevel@tonic-gate 		bp = *argv;
65*7c478bd9Sstevel@tonic-gate 		if ((iobuf = fopen(*argv, "r")) == NULL) {
66*7c478bd9Sstevel@tonic-gate 			message = gettext("can not open ");
67*7c478bd9Sstevel@tonic-gate 			write(2, message, strlen(message));
68*7c478bd9Sstevel@tonic-gate 			write(2, *argv, size(*argv));
69*7c478bd9Sstevel@tonic-gate 			write(2, "\n", 1);
70*7c478bd9Sstevel@tonic-gate 			/* continues to next file, if any, but the */
71*7c478bd9Sstevel@tonic-gate 			/* exit status will indicate an error */
72*7c478bd9Sstevel@tonic-gate 			status = 1;
73*7c478bd9Sstevel@tonic-gate 			continue;
74*7c478bd9Sstevel@tonic-gate 		}
75*7c478bd9Sstevel@tonic-gate 		cp = ofile;
76*7c478bd9Sstevel@tonic-gate 		while (*++bp)
77*7c478bd9Sstevel@tonic-gate 			if (*bp == '/') *bp = '\0';
78*7c478bd9Sstevel@tonic-gate 		while (*--bp == '\0');
79*7c478bd9Sstevel@tonic-gate 		while (*bp != '\0' && bp > *argv) bp--;
80*7c478bd9Sstevel@tonic-gate 		while (*bp == 0)
81*7c478bd9Sstevel@tonic-gate 			bp++;
82*7c478bd9Sstevel@tonic-gate 		while (*cp++ = *bp++);
83*7c478bd9Sstevel@tonic-gate 		cp--; *cp++ = '.';
84*7c478bd9Sstevel@tonic-gate 		if (cflg) *cp++ = 'c';
85*7c478bd9Sstevel@tonic-gate 		else *cp++ = 'i';
86*7c478bd9Sstevel@tonic-gate 		*cp = '\0';
87*7c478bd9Sstevel@tonic-gate 		close(1);
88*7c478bd9Sstevel@tonic-gate 		if (creat(ofile, 0644) < 0) {
89*7c478bd9Sstevel@tonic-gate 			message = gettext("can not create .i file\n");
90*7c478bd9Sstevel@tonic-gate 			write(2, message, strlen(message));
91*7c478bd9Sstevel@tonic-gate 			exit(1);
92*7c478bd9Sstevel@tonic-gate 		}
93*7c478bd9Sstevel@tonic-gate 		gotflg = 0;
94*7c478bd9Sstevel@tonic-gate 		while (1) {
95*7c478bd9Sstevel@tonic-gate 			str = a1;
96*7c478bd9Sstevel@tonic-gate 			name = a2;
97*7c478bd9Sstevel@tonic-gate 			if (!gotflg)
98*7c478bd9Sstevel@tonic-gate 				while (((c = getc(iobuf)) == '\n') ||
99*7c478bd9Sstevel@tonic-gate 				    (c == ' '));
100*7c478bd9Sstevel@tonic-gate 			else
101*7c478bd9Sstevel@tonic-gate 				gotflg = 0;
102*7c478bd9Sstevel@tonic-gate 			if (c == EOF) break;
103*7c478bd9Sstevel@tonic-gate 			*name++ = c;
104*7c478bd9Sstevel@tonic-gate 			while (((*name++ = c = getc(iobuf)) != ' ') &&
105*7c478bd9Sstevel@tonic-gate 			    (c != EOF) && (c != '\n'));
106*7c478bd9Sstevel@tonic-gate 				*--name = '\0';
107*7c478bd9Sstevel@tonic-gate 			while (((c = getc(iobuf)) == ' ') || (c == '\n'));
108*7c478bd9Sstevel@tonic-gate 			if (c != '"') {
109*7c478bd9Sstevel@tonic-gate 				if (c == EOF) {
110*7c478bd9Sstevel@tonic-gate 					message = gettext("unexpected eof\n");
111*7c478bd9Sstevel@tonic-gate 					write(2, message, strlen(message));
112*7c478bd9Sstevel@tonic-gate 					exit(1);
113*7c478bd9Sstevel@tonic-gate 				}
114*7c478bd9Sstevel@tonic-gate 				message = gettext("missing initial quote for ");
115*7c478bd9Sstevel@tonic-gate 				write(2, message, strlen(message));
116*7c478bd9Sstevel@tonic-gate 				write(2, a2, size(a2));
117*7c478bd9Sstevel@tonic-gate 				message =
118*7c478bd9Sstevel@tonic-gate 				    gettext(" : remainder of line ignored\n");
119*7c478bd9Sstevel@tonic-gate 				write(2, message, strlen(message));
120*7c478bd9Sstevel@tonic-gate 				while ((c = getc(iobuf)) != '\n');
121*7c478bd9Sstevel@tonic-gate 				continue;
122*7c478bd9Sstevel@tonic-gate 			}
123*7c478bd9Sstevel@tonic-gate 			keeponl:
124*7c478bd9Sstevel@tonic-gate 			while (gotflg || (c = getc(iobuf)) != EOF) {
125*7c478bd9Sstevel@tonic-gate 				gotflg = 0;
126*7c478bd9Sstevel@tonic-gate 				switch (c) {
127*7c478bd9Sstevel@tonic-gate 				case '"':
128*7c478bd9Sstevel@tonic-gate 					break;
129*7c478bd9Sstevel@tonic-gate 				case '\\':
130*7c478bd9Sstevel@tonic-gate 					switch (c = getc(iobuf)) {
131*7c478bd9Sstevel@tonic-gate 					case 't':
132*7c478bd9Sstevel@tonic-gate 						*str++ = '\011';
133*7c478bd9Sstevel@tonic-gate 						continue;
134*7c478bd9Sstevel@tonic-gate 					case 'n':
135*7c478bd9Sstevel@tonic-gate 						*str++ = '\012';
136*7c478bd9Sstevel@tonic-gate 						continue;
137*7c478bd9Sstevel@tonic-gate 					case 'r':
138*7c478bd9Sstevel@tonic-gate 						*str++ = '\015';
139*7c478bd9Sstevel@tonic-gate 						continue;
140*7c478bd9Sstevel@tonic-gate 					case 'b':
141*7c478bd9Sstevel@tonic-gate 						*str++ = '\010';
142*7c478bd9Sstevel@tonic-gate 						continue;
143*7c478bd9Sstevel@tonic-gate 					case '\\':
144*7c478bd9Sstevel@tonic-gate 						*str++ = '\\';
145*7c478bd9Sstevel@tonic-gate 						continue;
146*7c478bd9Sstevel@tonic-gate 					default:
147*7c478bd9Sstevel@tonic-gate 						if (c <= '7' && c >= '0')
148*7c478bd9Sstevel@tonic-gate 							*str++ = getnm(c);
149*7c478bd9Sstevel@tonic-gate 						else *str++ = c;
150*7c478bd9Sstevel@tonic-gate 						continue;
151*7c478bd9Sstevel@tonic-gate 					}
152*7c478bd9Sstevel@tonic-gate 				default:
153*7c478bd9Sstevel@tonic-gate 					*str++ = c;
154*7c478bd9Sstevel@tonic-gate 				}
155*7c478bd9Sstevel@tonic-gate 				if (c == '"') break;
156*7c478bd9Sstevel@tonic-gate 			}
157*7c478bd9Sstevel@tonic-gate 			if (c == EOF) {
158*7c478bd9Sstevel@tonic-gate 				message = gettext("unexpected eof\n");
159*7c478bd9Sstevel@tonic-gate 				write(2, message, strlen(message));
160*7c478bd9Sstevel@tonic-gate 				exit(1);
161*7c478bd9Sstevel@tonic-gate 			}
162*7c478bd9Sstevel@tonic-gate 			while (((c = getc(iobuf)) == '\n') || (c == ' '));
163*7c478bd9Sstevel@tonic-gate 			if (c == '"') goto keeponl;
164*7c478bd9Sstevel@tonic-gate 			else {
165*7c478bd9Sstevel@tonic-gate 				gotflg++;
166*7c478bd9Sstevel@tonic-gate 			}
167*7c478bd9Sstevel@tonic-gate 			*str = '\0';
168*7c478bd9Sstevel@tonic-gate 			if (!(sv = v = regcmp(a1, 0))) {
169*7c478bd9Sstevel@tonic-gate 				message = gettext("fail: ");
170*7c478bd9Sstevel@tonic-gate 				write(2, message, strlen(message));
171*7c478bd9Sstevel@tonic-gate 				write(2, a2, size(a2));
172*7c478bd9Sstevel@tonic-gate 				write(2, "\n", 1);
173*7c478bd9Sstevel@tonic-gate 				continue;
174*7c478bd9Sstevel@tonic-gate 			}
175*7c478bd9Sstevel@tonic-gate 			printf("/* \"%s\" */\n", a1);
176*7c478bd9Sstevel@tonic-gate 			printf("char %s[] = {\n", a2);
177*7c478bd9Sstevel@tonic-gate 			while (__i_size > 0) {
178*7c478bd9Sstevel@tonic-gate 				for (k = 0; k < 12; k++)
179*7c478bd9Sstevel@tonic-gate 					if (__i_size-- > 0)
180*7c478bd9Sstevel@tonic-gate 						printf("0%o, ", *v++);
181*7c478bd9Sstevel@tonic-gate 				printf("\n");
182*7c478bd9Sstevel@tonic-gate 			}
183*7c478bd9Sstevel@tonic-gate 			printf("0};\n");
184*7c478bd9Sstevel@tonic-gate 			free(sv);
185*7c478bd9Sstevel@tonic-gate 		}
186*7c478bd9Sstevel@tonic-gate 		fclose(iobuf);
187*7c478bd9Sstevel@tonic-gate 	}
188*7c478bd9Sstevel@tonic-gate 	exit(status);
189*7c478bd9Sstevel@tonic-gate }
190*7c478bd9Sstevel@tonic-gate size(p)
191*7c478bd9Sstevel@tonic-gate char *p;
192*7c478bd9Sstevel@tonic-gate {
193*7c478bd9Sstevel@tonic-gate 	register i;
194*7c478bd9Sstevel@tonic-gate 	register char *q;
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	i = 0;
197*7c478bd9Sstevel@tonic-gate 	q = p;
198*7c478bd9Sstevel@tonic-gate 	while (*q++) i++;
199*7c478bd9Sstevel@tonic-gate 	return (i);
200*7c478bd9Sstevel@tonic-gate }
201*7c478bd9Sstevel@tonic-gate getnm(j) char j;
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate 	register int i;
204*7c478bd9Sstevel@tonic-gate 	register int k;
205*7c478bd9Sstevel@tonic-gate 	i = j - '0';
206*7c478bd9Sstevel@tonic-gate 	k = 1;
207*7c478bd9Sstevel@tonic-gate 	while (++k < 4 && (c = getc(iobuf)) >= '0' && c <= '7')
208*7c478bd9Sstevel@tonic-gate 		i = (i*8+(c-'0'));
209*7c478bd9Sstevel@tonic-gate 	if (k >= 4)
210*7c478bd9Sstevel@tonic-gate 		c = getc(iobuf);
211*7c478bd9Sstevel@tonic-gate 	gotflg++;
212*7c478bd9Sstevel@tonic-gate 	return (i);
213*7c478bd9Sstevel@tonic-gate }
214