2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1997-2005
5 * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This program creates syntax.h and syntax.c.
42 #include <sys/types.h>
52 struct synclass synclass[] = {
53 { "CWORD", "character is nothing special" },
54 { "CNL", "newline character" },
55 { "CBACK", "a backslash character" },
56 { "CDBACK", "a backslash character in double quotes" },
57 { "CSQUOTE", "single quote" },
58 { "CDQUOTE", "double quote" },
59 { "CENDQUOTE", "a terminating quote" },
60 { "CBQUOTE", "backwards single quote" },
61 { "CVAR", "a dollar sign" },
62 { "CENDVAR", "a '}' character" },
63 { "CLP", "a left paren in arithmetic" },
64 { "CRP", "a right paren in arithmetic" },
65 { "CEOF", "end of file" },
66 { "CCTL", "like CWORD, except it must be escaped" },
67 { "CSPCL", "these terminate a word" },
68 { "CIGN", "character should be ignored" },
74 * Syntax classes for is_ functions. Warning: if you add new classes
75 * you may have to change the definition of the is_in_name macro.
77 struct synclass is_entry[] = {
78 { "ISDIGIT", "a digit" },
79 { "ISUPPER", "an upper case letter" },
80 { "ISLOWER", "a lower case letter" },
81 { "ISUNDER", "an underscore" },
82 { "ISSPECL", "the name of a special parameter" },
86 static char writer[] = "\
88 * This file was generated by the mksyntax program.\n\
95 static char *syntax[513];
97 static void filltable(char *);
98 static void init(void);
99 static void add(char *, char *);
100 static void print(char *);
101 static void output_type_macros(void);
102 int main(int, char **);
105 main(int argc, char **argv)
111 /* Create output files */
112 if ((cfile = fopen("syntax.c", "w")) == NULL) {
116 if ((hfile = fopen("syntax.h", "w")) == NULL) {
120 fputs(writer, hfile);
121 fputs(writer, cfile);
123 fputs("#include <ctype.h>\n", hfile);
125 fputs("#ifdef CEOF\n", hfile);
126 fputs("#undef CEOF\n", hfile);
127 fputs("#endif\n", hfile);
130 /* Generate the #define statements in the header file */
131 fputs("/* Syntax classes */\n", hfile);
132 for (i = 0 ; synclass[i].name ; i++) {
133 sprintf(buf, "#define %s %d", synclass[i].name, i);
135 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
137 fprintf(hfile, "/* %s */\n", synclass[i].comment);
140 fputs("/* Syntax classes for is_ functions */\n", hfile);
141 for (i = 0 ; is_entry[i].name ; i++) {
142 sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
144 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
146 fprintf(hfile, "/* %s */\n", is_entry[i].comment);
149 fprintf(hfile, "#define SYNBASE %d\n", 130);
150 fprintf(hfile, "#define PEOF %d\n\n", -130);
151 fprintf(hfile, "#define PEOA %d\n\n", -129);
153 fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
154 fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
155 fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
156 fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
158 output_type_macros(); /* is_digit, etc. */
161 /* Generate the syntax tables. */
162 fputs("#include \"shell.h\"\n", cfile);
163 fputs("#include \"syntax.h\"\n\n", cfile);
165 fputs("/* syntax table used when not in quotes */\n", cfile);
169 add("\"", "CDQUOTE");
173 add("<>();&| \t", "CSPCL");
177 fputs("\n/* syntax table used when in double quotes */\n", cfile);
180 add("\"", "CENDQUOTE");
184 /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
185 add("!*?[=~:/-]", "CCTL");
188 fputs("\n/* syntax table used when in single quotes */\n", cfile);
190 add("'", "CENDQUOTE");
191 /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
192 add("!*?[=~:/-]\\", "CCTL");
195 fputs("\n/* syntax table used when in arithmetic */\n", cfile);
205 fputs("\n/* character classification table */\n", cfile);
206 add("0123456789", "ISDIGIT");
207 add("abcdefghijklmnopqrstucvwxyz", "ISLOWER");
208 add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER");
210 add("#?$!-*@", "ISSPECL");
219 * Clear the syntax table.
223 filltable(char *dftval)
227 for (i = 0 ; i < 257; i++)
233 * Initialize the syntax table with default values.
244 for (ctl = CTL_FIRST; ctl <= CTL_LAST; ctl++ )
245 syntax[130 + ctl] = "CCTL";
250 * Add entries to the syntax table.
254 add(char *p, char *type)
257 syntax[(signed char)*p++ + 130] = type;
263 * Output the syntax table.
272 fprintf(hfile, "extern const char %s[];\n", name);
273 fprintf(cfile, "const char %s[%d] = {\n", name, 257);
275 for (i = 0 ; i < 257; i++) {
278 } else if ((i & 03) == 0) {
279 fputs(",\n ", cfile);
283 while (++col < 9 * (i & 03))
286 fputs(syntax[i], cfile);
287 col += strlen(syntax[i]);
289 fputs("\n};\n", cfile);
295 * Output character classification macros (e.g. is_digit). If digits are
296 * contiguous, we can test for them quickly.
299 static char *macro[] = {
300 "#define is_digit(c)\t((unsigned)((c) - '0') <= 9)\n",
301 "#define is_alpha(c)\tisalpha((unsigned char)(c))\n",
302 "#define is_name(c)\t((c) == '_' || isalpha((unsigned char)(c)))\n",
303 "#define is_in_name(c)\t((c) == '_' || isalnum((unsigned char)(c)))\n",
304 "#define is_special(c)\t((is_type+SYNBASE)[(signed char)(c)] & (ISSPECL|ISDIGIT))\n",
309 output_type_macros(void)
313 for (pp = macro ; *pp ; pp++)
315 fputs("#define digit_val(c)\t((c) - '0')\n", hfile);