xref: /illumos-gate/usr/src/cmd/regcmp/regcmp.c (revision 490fea6b98d609ce9ae1306209c80bb255b84c95)
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 #include <stdio.h>
31 #include <locale.h>
32 #include <libgen.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 
39 FILE *iobuf;
40 int gotflg;
41 char ofile[64];
42 char a1[1024];
43 char a2[64];
44 int c;
45 
46 int	getnm(char);
47 int	size(char *);
48 
49 int
50 main(int argc, char **argv)
51 {
52 	char *name, *str, *v;
53 	char *bp, *cp, *sv;
54 	char *message;
55 	int k, cflg = 0;
56 	int status = 0;
57 
58 	(void) setlocale(LC_ALL, "");
59 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
60 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
61 #endif
62 	(void) textdomain(TEXT_DOMAIN);
63 	if (argc > 1 && *argv[1] == '-') {
64 		cflg++;
65 		++argv;
66 		argc--;
67 	} else cflg = 0;
68 	while (--argc) {
69 		++argv;
70 		bp = *argv;
71 		if ((iobuf = fopen(*argv, "r")) == NULL) {
72 			message = gettext("can not open ");
73 			write(2, message, strlen(message));
74 			write(2, *argv, size(*argv));
75 			write(2, "\n", 1);
76 			/* continues to next file, if any, but the */
77 			/* exit status will indicate an error */
78 			status = 1;
79 			continue;
80 		}
81 		cp = ofile;
82 		while (*++bp)
83 			if (*bp == '/') *bp = '\0';
84 		while (*--bp == '\0')
85 			;
86 		while (*bp != '\0' && bp > *argv) bp--;
87 		while (*bp == 0)
88 			bp++;
89 		while (*cp++ = *bp++)
90 			;
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 					;
109 			else
110 				gotflg = 0;
111 			if (c == EOF) break;
112 			*name++ = c;
113 			while (((*name++ = c = getc(iobuf)) != ' ') &&
114 			    (c != EOF) && (c != '\n'))
115 				;
116 			*--name = '\0';
117 			while (((c = getc(iobuf)) == ' ') || (c == '\n'))
118 				;
119 			if (c != '"') {
120 				if (c == EOF) {
121 					message = gettext("unexpected eof\n");
122 					write(2, message, strlen(message));
123 					exit(1);
124 				}
125 				message = gettext("missing initial quote for ");
126 				write(2, message, strlen(message));
127 				write(2, a2, size(a2));
128 				message =
129 				    gettext(" : remainder of line ignored\n");
130 				write(2, message, strlen(message));
131 				while ((c = getc(iobuf)) != '\n')
132 					;
133 				continue;
134 			}
135 			keeponl:
136 			while (gotflg || (c = getc(iobuf)) != EOF) {
137 				gotflg = 0;
138 				switch (c) {
139 				case '"':
140 					break;
141 				case '\\':
142 					switch (c = getc(iobuf)) {
143 					case 't':
144 						*str++ = '\011';
145 						continue;
146 					case 'n':
147 						*str++ = '\012';
148 						continue;
149 					case 'r':
150 						*str++ = '\015';
151 						continue;
152 					case 'b':
153 						*str++ = '\010';
154 						continue;
155 					case '\\':
156 						*str++ = '\\';
157 						continue;
158 					default:
159 						if (c <= '7' && c >= '0')
160 							*str++ = getnm((char)c);
161 						else *str++ = c;
162 						continue;
163 					}
164 				default:
165 					*str++ = c;
166 				}
167 				if (c == '"') break;
168 			}
169 			if (c == EOF) {
170 				message = gettext("unexpected eof\n");
171 				write(2, message, strlen(message));
172 				exit(1);
173 			}
174 			while (((c = getc(iobuf)) == '\n') || (c == ' '))
175 				;
176 			if (c == '"') goto keeponl;
177 			else {
178 				gotflg++;
179 			}
180 			*str = '\0';
181 			if (!(sv = v = regcmp(a1, 0))) {
182 				message = gettext("fail: ");
183 				write(2, message, strlen(message));
184 				write(2, a2, size(a2));
185 				write(2, "\n", 1);
186 				continue;
187 			}
188 			printf("/* \"%s\" */\n", a1);
189 			printf("char %s[] = {\n", a2);
190 			while (__i_size > 0) {
191 				for (k = 0; k < 12; k++)
192 					if (__i_size-- > 0)
193 						printf("0%o, ", *v++);
194 				printf("\n");
195 			}
196 			printf("0};\n");
197 			free(sv);
198 		}
199 		fclose(iobuf);
200 	}
201 	return (status);
202 }
203 
204 int
205 size(char *p)
206 {
207 	int i;
208 	char *q;
209 
210 	i = 0;
211 	q = p;
212 	while (*q++) i++;
213 	return (i);
214 }
215 
216 int
217 getnm(char j)
218 {
219 	int i;
220 	int k;
221 	i = j - '0';
222 	k = 1;
223 	while (++k < 4 && (c = getc(iobuf)) >= '0' && c <= '7')
224 		i = (i*8+(c-'0'));
225 	if (k >= 4)
226 		c = getc(iobuf);
227 	gotflg++;
228 	return (i);
229 }
230