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
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
35 static char const copyright[] =
36 "@(#) Copyright (c) 1991, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)mksyntax.c 8.2 (Berkeley) 5/4/95";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 * This program creates syntax.h and syntax.c.
63 struct synclass synclass[] = {
64 { "CWORD", "character is nothing special" },
65 { "CNL", "newline character" },
66 { "CBACK", "a backslash character" },
67 { "CSBACK", "a backslash character in single quotes" },
68 { "CSQUOTE", "single quote" },
69 { "CDQUOTE", "double quote" },
70 { "CENDQUOTE", "a terminating quote" },
71 { "CBQUOTE", "backwards single quote" },
72 { "CVAR", "a dollar sign" },
73 { "CENDVAR", "a '}' character" },
74 { "CLP", "a left paren in arithmetic" },
75 { "CRP", "a right paren in arithmetic" },
76 { "CEOF", "end of file" },
77 { "CCTL", "like CWORD, except it must be escaped" },
78 { "CSPCL", "these terminate a word" },
79 { "CIGN", "character should be ignored" },
85 * Syntax classes for is_ functions. Warning: if you add new classes
86 * you may have to change the definition of the is_in_name macro.
88 struct synclass is_entry[] = {
89 { "ISDIGIT", "a digit" },
90 { "ISUPPER", "an upper case letter" },
91 { "ISLOWER", "a lower case letter" },
92 { "ISUNDER", "an underscore" },
93 { "ISSPECL", "the name of a special parameter" },
97 static char writer[] = "\
99 * This file was generated by the mksyntax program.\n\
106 static const char *syntax[513];
108 static int size; /* number of values which a char variable can have */
109 static int nbits; /* number of bits in a character */
110 static int digit_contig;/* true if digits are contiguous */
112 static void filltable(const char *);
113 static void init(void);
114 static void add(const char *, const char *);
115 static void print(const char *);
116 static void output_type_macros(void);
117 static void digit_convert(void);
120 main(int argc __unused, char **argv __unused)
128 static char digit[] = "0123456789";
130 /* Create output files */
131 if ((cfile = fopen("syntax.c", "w")) == NULL) {
135 if ((hfile = fopen("syntax.h", "w")) == NULL) {
139 fputs(writer, hfile);
140 fputs(writer, cfile);
142 /* Determine the characteristics of chars. */
144 sign = (c > 0) ? 0 : 1;
145 for (nbits = 1 ; ; nbits++) {
146 d = (1 << nbits) - 1;
151 printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits);
154 fputs("Characters can't have more than 9 bits\n", stderr);
157 size = (1 << nbits) + 1;
160 base += 1 << (nbits - 1);
162 for (i = 0 ; i < 10 ; i++) {
163 if (digit[i] != '0' + i)
167 fputs("#include <sys/cdefs.h>\n", hfile);
168 fputs("#include <ctype.h>\n", hfile);
170 /* Generate the #define statements in the header file */
171 fputs("/* Syntax classes */\n", hfile);
172 for (i = 0 ; synclass[i].name ; i++) {
173 sprintf(buf, "#define %s %d", synclass[i].name, i);
175 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
177 fprintf(hfile, "/* %s */\n", synclass[i].comment);
180 fputs("/* Syntax classes for is_ functions */\n", hfile);
181 for (i = 0 ; is_entry[i].name ; i++) {
182 sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
184 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
186 fprintf(hfile, "/* %s */\n", is_entry[i].comment);
189 fprintf(hfile, "#define SYNBASE %d\n", base);
190 fprintf(hfile, "#define PEOF %d\n\n", -base);
192 fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
193 fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
194 fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
195 fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
197 output_type_macros(); /* is_digit, etc. */
200 /* Generate the syntax tables. */
201 fputs("#include \"shell.h\"\n", cfile);
202 fputs("#include \"syntax.h\"\n\n", cfile);
204 fputs("/* syntax table used when not in quotes */\n", cfile);
208 add("\"", "CDQUOTE");
212 add("<>();&| \t", "CSPCL");
215 fputs("\n/* syntax table used when in double quotes */\n", cfile);
218 add("\"", "CENDQUOTE");
222 /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
223 add("!*?[=~:/-", "CCTL");
226 fputs("\n/* syntax table used when in single quotes */\n", cfile);
229 add("'", "CENDQUOTE");
230 /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
231 add("!*?[=~:/-", "CCTL");
234 fputs("\n/* syntax table used when in arithmetic */\n", cfile);
245 fputs("\n/* character classification table */\n", cfile);
246 add("0123456789", "ISDIGIT");
247 add("abcdefghijklmnopqrstuvwxyz", "ISLOWER");
248 add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER");
250 add("#?$!-*@", "ISSPECL");
260 * Clear the syntax table.
264 filltable(const char *dftval)
268 for (i = 0 ; i < size ; i++)
274 * Initialize the syntax table with default values.
282 syntax[base + CTLESC] = "CCTL";
283 syntax[base + CTLVAR] = "CCTL";
284 syntax[base + CTLENDVAR] = "CCTL";
285 syntax[base + CTLBACKQ] = "CCTL";
286 syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
287 syntax[base + CTLARI] = "CCTL";
288 syntax[base + CTLENDARI] = "CCTL";
289 syntax[base + CTLQUOTEMARK] = "CCTL";
290 syntax[base + CTLQUOTEEND] = "CCTL";
295 * Add entries to the syntax table.
299 add(const char *p, const char *type)
302 syntax[*p++ + base] = type;
308 * Output the syntax table.
312 print(const char *name)
317 fprintf(hfile, "extern const char %s[];\n", name);
318 fprintf(cfile, "const char %s[%d] = {\n", name, size);
320 for (i = 0 ; i < size ; i++) {
323 } else if ((i & 03) == 0) {
324 fputs(",\n ", cfile);
328 while (++col < 9 * (i & 03))
331 fputs(syntax[i], cfile);
332 col += strlen(syntax[i]);
334 fputs("\n};\n", cfile);
340 * Output character classification macros (e.g. is_digit). If digits are
341 * contiguous, we can test for them quickly.
344 static const char *macro[] = {
345 "#define is_digit(c)\t((is_type+SYNBASE)[(int)c] & ISDIGIT)",
346 "#define is_eof(c)\t((c) == PEOF)",
347 "#define is_alpha(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER))",
348 "#define is_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER))",
349 "#define is_in_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))",
350 "#define is_special(c)\t((is_type+SYNBASE)[(int)c] & (ISSPECL|ISDIGIT))",
355 output_type_macros(void)
360 macro[0] = "#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)";
361 for (pp = macro ; *pp ; pp++)
362 fprintf(hfile, "%s\n", *pp);
364 fputs("#define digit_val(c)\t((c) - '0')\n", hfile);
366 fputs("#define digit_val(c)\t(digit_value[c])\n", hfile);
372 * Output digit conversion table (if digits are not contiguous).
379 static char digit[] = "0123456789";
384 for (p = digit ; *p ; p++)
387 fputs("extern const char digit_value[];\n", hfile);
388 fputs("\n\nconst char digit_value[] = {\n", cfile);
389 for (i = 0 ; i <= maxdigit ; i++) {
390 for (p = digit ; *p && *p != i ; p++);
393 fprintf(cfile, " %d,\n", (int)(p - digit));
395 fputs("};\n", cfile);