dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / regcmp / regcmp.c
blob28094df9310b01da8cf9871a604cca1a036d55a8
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
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>
40 * Note: First of all, this used to live in libgen.h. Second, this is not
41 * reentrant. The code that was there that checked for _REENTRANT made
42 * __i_size defined as a macro to an undefined function so a reentrant
43 * version never worked anyway.
45 extern int __i_size;
47 FILE *iobuf;
48 int gotflg;
49 char ofile[64];
50 char a1[1024];
51 char a2[64];
52 int c;
54 int getnm(char);
55 int size(char *);
57 int
58 main(int argc, char **argv)
60 char *name, *str, *v;
61 char *bp, *cp, *sv;
62 char *message;
63 int k, cflg = 0;
64 int status = 0;
66 (void) setlocale(LC_ALL, "");
67 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
68 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
69 #endif
70 (void) textdomain(TEXT_DOMAIN);
71 if (argc > 1 && *argv[1] == '-') {
72 cflg++;
73 ++argv;
74 argc--;
75 } else cflg = 0;
76 while (--argc) {
77 ++argv;
78 bp = *argv;
79 if ((iobuf = fopen(*argv, "r")) == NULL) {
80 message = gettext("can not open ");
81 write(2, message, strlen(message));
82 write(2, *argv, size(*argv));
83 write(2, "\n", 1);
84 /* continues to next file, if any, but the */
85 /* exit status will indicate an error */
86 status = 1;
87 continue;
89 cp = ofile;
90 while (*++bp)
91 if (*bp == '/') *bp = '\0';
92 while (*--bp == '\0')
94 while (*bp != '\0' && bp > *argv) bp--;
95 while (*bp == 0)
96 bp++;
97 while (*cp++ = *bp++)
99 cp--; *cp++ = '.';
100 if (cflg) *cp++ = 'c';
101 else *cp++ = 'i';
102 *cp = '\0';
103 close(1);
104 if (creat(ofile, 0644) < 0) {
105 message = gettext("can not create .i file\n");
106 write(2, message, strlen(message));
107 exit(1);
109 gotflg = 0;
110 while (1) {
111 str = a1;
112 name = a2;
113 if (!gotflg)
114 while (((c = getc(iobuf)) == '\n') ||
115 (c == ' '))
117 else
118 gotflg = 0;
119 if (c == EOF) break;
120 *name++ = c;
121 while (((*name++ = c = getc(iobuf)) != ' ') &&
122 (c != EOF) && (c != '\n'))
124 *--name = '\0';
125 while (((c = getc(iobuf)) == ' ') || (c == '\n'))
127 if (c != '"') {
128 if (c == EOF) {
129 message = gettext("unexpected eof\n");
130 write(2, message, strlen(message));
131 exit(1);
133 message = gettext("missing initial quote for ");
134 write(2, message, strlen(message));
135 write(2, a2, size(a2));
136 message =
137 gettext(" : remainder of line ignored\n");
138 write(2, message, strlen(message));
139 while ((c = getc(iobuf)) != '\n')
141 continue;
143 keeponl:
144 while (gotflg || (c = getc(iobuf)) != EOF) {
145 gotflg = 0;
146 switch (c) {
147 case '"':
148 break;
149 case '\\':
150 switch (c = getc(iobuf)) {
151 case 't':
152 *str++ = '\011';
153 continue;
154 case 'n':
155 *str++ = '\012';
156 continue;
157 case 'r':
158 *str++ = '\015';
159 continue;
160 case 'b':
161 *str++ = '\010';
162 continue;
163 case '\\':
164 *str++ = '\\';
165 continue;
166 default:
167 if (c <= '7' && c >= '0')
168 *str++ = getnm((char)c);
169 else *str++ = c;
170 continue;
172 default:
173 *str++ = c;
175 if (c == '"') break;
177 if (c == EOF) {
178 message = gettext("unexpected eof\n");
179 write(2, message, strlen(message));
180 exit(1);
182 while (((c = getc(iobuf)) == '\n') || (c == ' '))
184 if (c == '"') goto keeponl;
185 else {
186 gotflg++;
188 *str = '\0';
189 if (!(sv = v = regcmp(a1, 0))) {
190 message = gettext("fail: ");
191 write(2, message, strlen(message));
192 write(2, a2, size(a2));
193 write(2, "\n", 1);
194 continue;
196 printf("/* \"%s\" */\n", a1);
197 printf("char %s[] = {\n", a2);
198 while (__i_size > 0) {
199 for (k = 0; k < 12; k++)
200 if (__i_size-- > 0)
201 printf("0%o, ", *v++);
202 printf("\n");
204 printf("0};\n");
205 free(sv);
207 fclose(iobuf);
209 return (status);
213 size(char *p)
215 int i;
216 char *q;
218 i = 0;
219 q = p;
220 while (*q++) i++;
221 return (i);
225 getnm(char j)
227 int i;
228 int k;
229 i = j - '0';
230 k = 1;
231 while (++k < 4 && (c = getc(iobuf)) >= '0' && c <= '7')
232 i = (i*8+(c-'0'));
233 if (k >= 4)
234 c = getc(iobuf);
235 gotflg++;
236 return (i);