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 { "CSQUOTE", "single quote" },
68 { "CDQUOTE", "double quote" },
69 { "CENDQUOTE", "a terminating quote" },
70 { "CBQUOTE", "backwards single quote" },
71 { "CVAR", "a dollar sign" },
72 { "CENDVAR", "a '}' character" },
73 { "CLP", "a left paren in arithmetic" },
74 { "CRP", "a right paren in arithmetic" },
75 { "CEOF", "end of file" },
76 { "CCTL", "like CWORD, except it must be escaped" },
77 { "CSPCL", "these terminate a word" },
83 * Syntax classes for is_ functions. Warning: if you add new classes
84 * you may have to change the definition of the is_in_name macro.
86 struct synclass is_entry[] = {
87 { "ISDIGIT", "a digit" },
88 { "ISUPPER", "an upper case letter" },
89 { "ISLOWER", "a lower case letter" },
90 { "ISUNDER", "an underscore" },
91 { "ISSPECL", "the name of a special parameter" },
95 static char writer[] = "\
97 * This file was generated by the mksyntax program.\n\
105 static void add_default(void);
106 static void finish(void);
107 static void init(const char *);
108 static void add(const char *, const char *);
109 static void output_type_macros(void);
112 main(int argc __unused, char **argv __unused)
118 /* Create output files */
119 if ((cfile = fopen("syntax.c", "w")) == NULL) {
123 if ((hfile = fopen("syntax.h", "w")) == NULL) {
127 fputs(writer, hfile);
128 fputs(writer, cfile);
130 fputs("#include <sys/cdefs.h>\n", hfile);
131 fputs("#include <ctype.h>\n", hfile);
132 fputs("#include <limits.h>\n\n", hfile);
134 /* Generate the #define statements in the header file */
135 fputs("/* Syntax classes */\n", hfile);
136 for (i = 0 ; synclass[i].name ; i++) {
137 sprintf(buf, "#define %s %d", synclass[i].name, i);
139 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
141 fprintf(hfile, "/* %s */\n", synclass[i].comment);
144 fputs("/* Syntax classes for is_ functions */\n", hfile);
145 for (i = 0 ; is_entry[i].name ; i++) {
146 sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
148 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
150 fprintf(hfile, "/* %s */\n", is_entry[i].comment);
153 fputs("#define SYNBASE (1 - CHAR_MIN)\n", hfile);
154 fputs("#define PEOF -SYNBASE\n\n", hfile);
156 fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
157 fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
158 fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
159 fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
161 output_type_macros(); /* is_digit, etc. */
164 /* Generate the syntax tables. */
165 fputs("#include \"parser.h\"\n", cfile);
166 fputs("#include \"shell.h\"\n", cfile);
167 fputs("#include \"syntax.h\"\n\n", cfile);
169 fputs("/* syntax table used when not in quotes */\n", cfile);
175 add("\"", "CDQUOTE");
179 add("<>();&| \t", "CSPCL");
182 fputs("\n/* syntax table used when in double quotes */\n", cfile);
187 add("\"", "CENDQUOTE");
191 /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
192 add("!*?[=~:/-", "CCTL");
195 fputs("\n/* syntax table used when in single quotes */\n", cfile);
199 add("'", "CENDQUOTE");
200 /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
201 add("!*?[=~:/-", "CCTL");
204 fputs("\n/* syntax table used when in arithmetic */\n", cfile);
211 add("\"", "CDQUOTE");
218 fputs("\n/* character classification table */\n", cfile);
220 add("0123456789", "ISDIGIT");
221 add("abcdefghijklmnopqrstucvwxyz", "ISLOWER");
222 add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER");
224 add("#?$!-*@", "ISSPECL");
232 * Output the header and declaration of a syntax table.
236 init(const char *name)
238 fprintf(hfile, "extern const char %s[];\n", name);
239 fprintf(cfile, "const char %s[SYNBASE + CHAR_MAX + 1] = {\n", name);
244 add_one(const char *key, const char *type)
246 fprintf(cfile, "\t[SYNBASE + %s] = %s,\n", key, type);
251 * Add default values to the syntax table.
257 add_one("PEOF", "CEOF");
258 add_one("CTLESC", "CCTL");
259 add_one("CTLVAR", "CCTL");
260 add_one("CTLENDVAR", "CCTL");
261 add_one("CTLBACKQ", "CCTL");
262 add_one("CTLBACKQ + CTLQUOTE", "CCTL");
263 add_one("CTLARI", "CCTL");
264 add_one("CTLENDARI", "CCTL");
265 add_one("CTLQUOTEMARK", "CCTL");
270 * Output the footer of a syntax table.
276 fputs("};\n", cfile);
281 * Add entries to the syntax table.
285 add(const char *p, const char *type)
290 case '\t': c = 't'; break;
291 case '\n': c = 'n'; break;
292 case '\'': c = '\''; break;
293 case '\\': c = '\\'; break;
296 fprintf(cfile, "\t[SYNBASE + '%c'] = %s,\n", c, type);
299 fprintf(cfile, "\t[SYNBASE + '\\%c'] = %s,\n", c, type);
305 * Output character classification macros (e.g. is_digit). If digits are
306 * contiguous, we can test for them quickly.
309 static const char *macro[] = {
310 "#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)",
311 "#define is_eof(c)\t((c) == PEOF)",
312 "#define is_alpha(c)\t(((c) < CTLESC || (c) > CTLQUOTEMARK) && isalpha((unsigned char) (c)))",
313 "#define is_name(c)\t(((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalpha((unsigned char) (c))))",
314 "#define is_in_name(c)\t(((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalnum((unsigned char) (c))))",
315 "#define is_special(c)\t((is_type+SYNBASE)[c] & (ISSPECL|ISDIGIT))",
316 "#define digit_val(c)\t((c) - '0')",
321 output_type_macros(void)
325 for (pp = macro ; *pp ; pp++)
326 fprintf(hfile, "%s\n", *pp);