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