xref: /titanic_51/usr/src/cmd/regcmp/regcmp.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 /*	Copyright (c) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright (c) 1997, by Sun Microsystems, Inc.
28  * All rights reserved.
29  */
30 
31 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.6.1.1	*/
32 
33 #include <stdio.h>
34 #include <locale.h>
35 #include <libgen.h>
36 FILE *fopen();
37 FILE *iobuf;
38 int gotflg;
39 char ofile[64];
40 char a1[1024];
41 char a2[64];
42 int c;
43 
44 main(argc, argv) char **argv;
45 {
46 	register char *name, *str, *v;
47 	char *bp, *cp, *sv;
48 	char *message;
49 	int j, k, cflg = 0;
50 	int status = 0;
51 
52 	(void) setlocale(LC_ALL, "");
53 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
54 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
55 #endif
56 	(void) textdomain(TEXT_DOMAIN);
57 	if (argc > 1 && *argv[1] == '-') {
58 		cflg++;
59 		++argv;
60 		argc--;
61 	} else cflg = 0;
62 	while (--argc) {
63 		++argv;
64 		bp = *argv;
65 		if ((iobuf = fopen(*argv, "r")) == NULL) {
66 			message = gettext("can not open ");
67 			write(2, message, strlen(message));
68 			write(2, *argv, size(*argv));
69 			write(2, "\n", 1);
70 			/* continues to next file, if any, but the */
71 			/* exit status will indicate an error */
72 			status = 1;
73 			continue;
74 		}
75 		cp = ofile;
76 		while (*++bp)
77 			if (*bp == '/') *bp = '\0';
78 		while (*--bp == '\0');
79 		while (*bp != '\0' && bp > *argv) bp--;
80 		while (*bp == 0)
81 			bp++;
82 		while (*cp++ = *bp++);
83 		cp--; *cp++ = '.';
84 		if (cflg) *cp++ = 'c';
85 		else *cp++ = 'i';
86 		*cp = '\0';
87 		close(1);
88 		if (creat(ofile, 0644) < 0) {
89 			message = gettext("can not create .i file\n");
90 			write(2, message, strlen(message));
91 			exit(1);
92 		}
93 		gotflg = 0;
94 		while (1) {
95 			str = a1;
96 			name = a2;
97 			if (!gotflg)
98 				while (((c = getc(iobuf)) == '\n') ||
99 				    (c == ' '));
100 			else
101 				gotflg = 0;
102 			if (c == EOF) break;
103 			*name++ = c;
104 			while (((*name++ = c = getc(iobuf)) != ' ') &&
105 			    (c != EOF) && (c != '\n'));
106 				*--name = '\0';
107 			while (((c = getc(iobuf)) == ' ') || (c == '\n'));
108 			if (c != '"') {
109 				if (c == EOF) {
110 					message = gettext("unexpected eof\n");
111 					write(2, message, strlen(message));
112 					exit(1);
113 				}
114 				message = gettext("missing initial quote for ");
115 				write(2, message, strlen(message));
116 				write(2, a2, size(a2));
117 				message =
118 				    gettext(" : remainder of line ignored\n");
119 				write(2, message, strlen(message));
120 				while ((c = getc(iobuf)) != '\n');
121 				continue;
122 			}
123 			keeponl:
124 			while (gotflg || (c = getc(iobuf)) != EOF) {
125 				gotflg = 0;
126 				switch (c) {
127 				case '"':
128 					break;
129 				case '\\':
130 					switch (c = getc(iobuf)) {
131 					case 't':
132 						*str++ = '\011';
133 						continue;
134 					case 'n':
135 						*str++ = '\012';
136 						continue;
137 					case 'r':
138 						*str++ = '\015';
139 						continue;
140 					case 'b':
141 						*str++ = '\010';
142 						continue;
143 					case '\\':
144 						*str++ = '\\';
145 						continue;
146 					default:
147 						if (c <= '7' && c >= '0')
148 							*str++ = getnm(c);
149 						else *str++ = c;
150 						continue;
151 					}
152 				default:
153 					*str++ = c;
154 				}
155 				if (c == '"') break;
156 			}
157 			if (c == EOF) {
158 				message = gettext("unexpected eof\n");
159 				write(2, message, strlen(message));
160 				exit(1);
161 			}
162 			while (((c = getc(iobuf)) == '\n') || (c == ' '));
163 			if (c == '"') goto keeponl;
164 			else {
165 				gotflg++;
166 			}
167 			*str = '\0';
168 			if (!(sv = v = regcmp(a1, 0))) {
169 				message = gettext("fail: ");
170 				write(2, message, strlen(message));
171 				write(2, a2, size(a2));
172 				write(2, "\n", 1);
173 				continue;
174 			}
175 			printf("/* \"%s\" */\n", a1);
176 			printf("char %s[] = {\n", a2);
177 			while (__i_size > 0) {
178 				for (k = 0; k < 12; k++)
179 					if (__i_size-- > 0)
180 						printf("0%o, ", *v++);
181 				printf("\n");
182 			}
183 			printf("0};\n");
184 			free(sv);
185 		}
186 		fclose(iobuf);
187 	}
188 	exit(status);
189 }
190 size(p)
191 char *p;
192 {
193 	register i;
194 	register char *q;
195 
196 	i = 0;
197 	q = p;
198 	while (*q++) i++;
199 	return (i);
200 }
201 getnm(j) char j;
202 {
203 	register int i;
204 	register int k;
205 	i = j - '0';
206 	k = 1;
207 	while (++k < 4 && (c = getc(iobuf)) >= '0' && c <= '7')
208 		i = (i*8+(c-'0'));
209 	if (k >= 4)
210 		c = getc(iobuf);
211 	gotflg++;
212 	return (i);
213 }
214