pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / ash / mksyntax.c
blob71aadd55739deb3a12b69ac428e9ec7f535cac1e
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #if 0
34 #ifndef lint
35 static char const copyright[] =
36 "@(#) Copyright (c) 1991, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
40 #ifndef lint
41 static char sccsid[] = "@(#)mksyntax.c 8.2 (Berkeley) 5/4/95";
42 #endif /* not lint */
43 #endif
45 __FBSDID("$FreeBSD: src/bin/sh/mksyntax.c,v 1.23 2004/04/06 20:06:51 markm Exp $");
49 * This program creates syntax.h and syntax.c.
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include "parser.h"
57 #ifdef __minix
58 #define __unused
59 #endif
61 struct synclass {
62 char *name;
63 char *comment;
66 /* Syntax classes */
67 struct synclass synclass[] = {
68 { "CWORD", "character is nothing special" },
69 { "CNL", "newline character" },
70 { "CBACK", "a backslash character" },
71 { "CSQUOTE", "single quote" },
72 { "CDQUOTE", "double quote" },
73 { "CENDQUOTE", "a terminating quote" },
74 { "CBQUOTE", "backwards single quote" },
75 { "CVAR", "a dollar sign" },
76 { "CENDVAR", "a '}' character" },
77 { "CLP", "a left paren in arithmetic" },
78 { "CRP", "a right paren in arithmetic" },
79 { "CEOF", "end of file" },
80 { "CCTL", "like CWORD, except it must be escaped" },
81 { "CSPCL", "these terminate a word" },
82 { NULL, NULL }
87 * Syntax classes for is_ functions. Warning: if you add new classes
88 * you may have to change the definition of the is_in_name macro.
90 struct synclass is_entry[] = {
91 { "ISDIGIT", "a digit" },
92 { "ISUPPER", "an upper case letter" },
93 { "ISLOWER", "a lower case letter" },
94 { "ISUNDER", "an underscore" },
95 { "ISSPECL", "the name of a special parameter" },
96 { NULL, NULL }
99 static char writer[] = "\
100 /*\n\
101 * This file was generated by the mksyntax program.\n\
102 */\n\
103 \n";
106 static FILE *cfile;
107 static FILE *hfile;
108 static char *syntax[513];
109 static int base;
110 static int size; /* number of values which a char variable can have */
111 static int nbits; /* number of bits in a character */
112 static int digit_contig;/* true if digits are contiguous */
114 static void filltable(char *);
115 static void init(void);
116 static void add(char *, char *);
117 static void print(char *);
118 static void output_type_macros(void);
119 static void digit_convert(void);
122 main(int argc __unused, char **argv __unused)
124 char c;
125 char d;
126 int sign;
127 int i;
128 char buf[80];
129 int pos;
130 static char digit[] = "0123456789";
132 /* Create output files */
133 if ((cfile = fopen("syntax.c", "w")) == NULL) {
134 perror("syntax.c");
135 exit(2);
137 if ((hfile = fopen("syntax.h", "w")) == NULL) {
138 perror("syntax.h");
139 exit(2);
141 fputs(writer, hfile);
142 fputs(writer, cfile);
144 /* Determine the characteristics of chars. */
145 c = -1;
146 if (c < 0)
147 sign = 1;
148 else
149 sign = 0;
150 for (nbits = 1 ; ; nbits++) {
151 d = (1 << nbits) - 1;
152 if (d == c)
153 break;
155 #if 0
156 printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits);
157 #endif
158 if (nbits > 9) {
159 fputs("Characters can't have more than 9 bits\n", stderr);
160 exit(2);
162 size = (1 << nbits) + 1;
163 base = 1;
164 if (sign)
165 base += 1 << (nbits - 1);
166 digit_contig = 1;
167 for (i = 0 ; i < 10 ; i++) {
168 if (digit[i] != '0' + i)
169 digit_contig = 0;
172 fputs("#include <ctype.h>\n", hfile);
174 /* Generate the #define statements in the header file */
175 fputs("/* Syntax classes */\n", hfile);
176 for (i = 0 ; synclass[i].name ; i++) {
177 sprintf(buf, "#define %s %d", synclass[i].name, i);
178 fputs(buf, hfile);
179 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
180 putc('\t', hfile);
181 fprintf(hfile, "/* %s */\n", synclass[i].comment);
183 putc('\n', hfile);
184 fputs("/* Syntax classes for is_ functions */\n", hfile);
185 for (i = 0 ; is_entry[i].name ; i++) {
186 sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
187 fputs(buf, hfile);
188 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
189 putc('\t', hfile);
190 fprintf(hfile, "/* %s */\n", is_entry[i].comment);
192 putc('\n', hfile);
193 fprintf(hfile, "#define SYNBASE %d\n", base);
194 fprintf(hfile, "#define PEOF %d\n\n", -base);
195 putc('\n', hfile);
196 fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
197 fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
198 fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
199 fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
200 putc('\n', hfile);
201 output_type_macros(); /* is_digit, etc. */
202 putc('\n', hfile);
204 /* Generate the syntax tables. */
205 fputs("#include \"shell.h\"\n", cfile);
206 fputs("#include \"syntax.h\"\n\n", cfile);
207 init();
208 fputs("/* syntax table used when not in quotes */\n", cfile);
209 add("\n", "CNL");
210 add("\\", "CBACK");
211 add("'", "CSQUOTE");
212 add("\"", "CDQUOTE");
213 add("`", "CBQUOTE");
214 add("$", "CVAR");
215 add("}", "CENDVAR");
216 add("<>();&| \t", "CSPCL");
217 print("basesyntax");
218 init();
219 fputs("\n/* syntax table used when in double quotes */\n", cfile);
220 add("\n", "CNL");
221 add("\\", "CBACK");
222 add("\"", "CENDQUOTE");
223 add("`", "CBQUOTE");
224 add("$", "CVAR");
225 add("}", "CENDVAR");
226 /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
227 add("!*?[=~:/-", "CCTL");
228 print("dqsyntax");
229 init();
230 fputs("\n/* syntax table used when in single quotes */\n", cfile);
231 add("\n", "CNL");
232 add("'", "CENDQUOTE");
233 /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
234 add("!*?[=~:/-", "CCTL");
235 print("sqsyntax");
236 init();
237 fputs("\n/* syntax table used when in arithmetic */\n", cfile);
238 add("\n", "CNL");
239 add("\\", "CBACK");
240 add("`", "CBQUOTE");
241 add("'", "CSQUOTE");
242 add("\"", "CDQUOTE");
243 add("$", "CVAR");
244 add("}", "CENDVAR");
245 add("(", "CLP");
246 add(")", "CRP");
247 print("arisyntax");
248 filltable("0");
249 fputs("\n/* character classification table */\n", cfile);
250 add("0123456789", "ISDIGIT");
251 add("abcdefghijklmnopqrstucvwxyz", "ISLOWER");
252 add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER");
253 add("_", "ISUNDER");
254 add("#?$!-*@", "ISSPECL");
255 print("is_type");
256 if (! digit_contig)
257 digit_convert();
258 exit(0);
264 * Clear the syntax table.
267 static void
268 filltable(char *dftval)
270 int i;
272 for (i = 0 ; i < size ; i++)
273 syntax[i] = dftval;
278 * Initialize the syntax table with default values.
281 static void
282 init(void)
284 filltable("CWORD");
285 syntax[0] = "CEOF";
286 syntax[base + CTLESC] = "CCTL";
287 syntax[base + CTLVAR] = "CCTL";
288 syntax[base + CTLENDVAR] = "CCTL";
289 syntax[base + CTLBACKQ] = "CCTL";
290 syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
291 syntax[base + CTLARI] = "CCTL";
292 syntax[base + CTLENDARI] = "CCTL";
293 syntax[base + CTLQUOTEMARK] = "CCTL";
298 * Add entries to the syntax table.
301 static void
302 add(char *p, char *type)
304 while (*p)
305 syntax[*p++ + base] = type;
311 * Output the syntax table.
314 static void
315 print(char *name)
317 int i;
318 int col;
320 fprintf(hfile, "extern const char %s[];\n", name);
321 fprintf(cfile, "const char %s[%d] = {\n", name, size);
322 col = 0;
323 for (i = 0 ; i < size ; i++) {
324 if (i == 0) {
325 fputs(" ", cfile);
326 } else if ((i & 03) == 0) {
327 fputs(",\n ", cfile);
328 col = 0;
329 } else {
330 putc(',', cfile);
331 while (++col < 9 * (i & 03))
332 putc(' ', cfile);
334 fputs(syntax[i], cfile);
335 col += strlen(syntax[i]);
337 fputs("\n};\n", cfile);
343 * Output character classification macros (e.g. is_digit). If digits are
344 * contiguous, we can test for them quickly.
347 static char *macro[] = {
348 "#define is_digit(c)\t((is_type+SYNBASE)[c] & ISDIGIT)",
349 "#define is_alpha(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && isalpha((unsigned char) (c)))",
350 "#define is_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalpha((unsigned char) (c))))",
351 "#define is_in_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalnum((unsigned char) (c))))",
352 "#define is_special(c)\t((is_type+SYNBASE)[c] & (ISSPECL|ISDIGIT))",
353 NULL
356 static void
357 output_type_macros(void)
359 char **pp;
361 if (digit_contig)
362 macro[0] = "#define is_digit(c)\t((unsigned)((c) - '0') <= 9)";
363 for (pp = macro ; *pp ; pp++)
364 fprintf(hfile, "%s\n", *pp);
365 if (digit_contig)
366 fputs("#define digit_val(c)\t((c) - '0')\n", hfile);
367 else
368 fputs("#define digit_val(c)\t(digit_value[c])\n", hfile);
374 * Output digit conversion table (if digits are not contiguous).
377 static void
378 digit_convert(void)
380 int maxdigit;
381 static char digit[] = "0123456789";
382 char *p;
383 int i;
385 maxdigit = 0;
386 for (p = digit ; *p ; p++)
387 if (*p > maxdigit)
388 maxdigit = *p;
389 fputs("extern const char digit_value[];\n", hfile);
390 fputs("\n\nconst char digit_value[] = {\n", cfile);
391 for (i = 0 ; i <= maxdigit ; i++) {
392 for (p = digit ; *p && *p != i ; p++);
393 if (*p == '\0')
394 p = digit;
395 fprintf(cfile, " %d,\n", (int)(p - digit));
397 fputs("};\n", cfile);
401 * $PchId: mksyntax.c,v 1.7 2006/05/23 12:04:27 philip Exp $