Commit | Line | Data |
---|---|---|
984263bc | 1 | /*- |
3e3895bf KP |
2 | * SPDX-License-Identifier: BSD-3-Clause |
3 | * | |
984263bc MD |
4 | * Copyright (c) 1991, 1993 |
5 | * The Regents of the University of California. All rights reserved. | |
6 | * | |
7 | * This code is derived from software contributed to Berkeley by | |
8 | * Kenneth Almquist. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
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. | |
69763be3 | 18 | * 3. Neither the name of the University nor the names of its contributors |
984263bc MD |
19 | * may be used to endorse or promote products derived from this software |
20 | * without specific prior written permission. | |
21 | * | |
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 | |
32 | * SUCH DAMAGE. | |
33 | */ | |
34 | ||
02d0b1ce MD |
35 | #if 0 |
36 | #ifndef lint | |
37 | static char const copyright[] = | |
38 | "@(#) Copyright (c) 1991, 1993\n\ | |
39 | The Regents of the University of California. All rights reserved.\n"; | |
40 | #endif /* not lint */ | |
41 | ||
42 | #ifndef lint | |
43 | static char sccsid[] = "@(#)mksyntax.c 8.2 (Berkeley) 5/4/95"; | |
44 | #endif /* not lint */ | |
45 | #endif | |
46 | #include <sys/cdefs.h> | |
3e3895bf | 47 | __FBSDID("$FreeBSD: head/bin/sh/mksyntax.c 334008 2018-05-21 21:52:48Z jilles $"); |
02d0b1ce | 48 | |
984263bc MD |
49 | /* |
50 | * This program creates syntax.h and syntax.c. | |
51 | */ | |
52 | ||
53 | #include <stdio.h> | |
54 | #include <stdlib.h> | |
55 | #include <string.h> | |
56 | #include "parser.h" | |
57 | ||
58 | ||
59 | struct synclass { | |
492efe05 CP |
60 | const char *name; |
61 | const char *comment; | |
984263bc MD |
62 | }; |
63 | ||
64 | /* Syntax classes */ | |
02d0b1ce | 65 | static const struct synclass synclass[] = { |
984263bc MD |
66 | { "CWORD", "character is nothing special" }, |
67 | { "CNL", "newline character" }, | |
3e3895bf | 68 | { "CQNL", "newline character in quotes" }, |
984263bc | 69 | { "CBACK", "a backslash character" }, |
e1489450 | 70 | { "CSBACK", "a backslash character in single quotes" }, |
984263bc MD |
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" }, | |
99512ac4 | 82 | { "CIGN", "character should be ignored" }, |
984263bc MD |
83 | { NULL, NULL } |
84 | }; | |
85 | ||
86 | ||
87 | /* | |
88 | * Syntax classes for is_ functions. Warning: if you add new classes | |
89 | * you may have to change the definition of the is_in_name macro. | |
90 | */ | |
02d0b1ce | 91 | static const struct synclass is_entry[] = { |
984263bc MD |
92 | { "ISDIGIT", "a digit" }, |
93 | { "ISUPPER", "an upper case letter" }, | |
94 | { "ISLOWER", "a lower case letter" }, | |
95 | { "ISUNDER", "an underscore" }, | |
96 | { "ISSPECL", "the name of a special parameter" }, | |
97 | { NULL, NULL } | |
98 | }; | |
99 | ||
02d0b1ce | 100 | static const char writer[] = "\ |
984263bc MD |
101 | /*\n\ |
102 | * This file was generated by the mksyntax program.\n\ | |
103 | */\n\ | |
104 | \n"; | |
105 | ||
106 | ||
107 | static FILE *cfile; | |
108 | static FILE *hfile; | |
f00eae14 PA |
109 | |
110 | static void add_default(void); | |
111 | static void finish(void); | |
112 | static void init(const char *); | |
492efe05 | 113 | static void add(const char *, const char *); |
984263bc | 114 | static void output_type_macros(void); |
984263bc MD |
115 | |
116 | int | |
117 | main(int argc __unused, char **argv __unused) | |
118 | { | |
984263bc MD |
119 | int i; |
120 | char buf[80]; | |
121 | int pos; | |
984263bc MD |
122 | |
123 | /* Create output files */ | |
124 | if ((cfile = fopen("syntax.c", "w")) == NULL) { | |
125 | perror("syntax.c"); | |
126 | exit(2); | |
127 | } | |
128 | if ((hfile = fopen("syntax.h", "w")) == NULL) { | |
129 | perror("syntax.h"); | |
130 | exit(2); | |
131 | } | |
132 | fputs(writer, hfile); | |
133 | fputs(writer, cfile); | |
134 | ||
984263bc | 135 | fputs("#include <sys/cdefs.h>\n", hfile); |
f00eae14 | 136 | fputs("#include <limits.h>\n\n", hfile); |
984263bc MD |
137 | |
138 | /* Generate the #define statements in the header file */ | |
139 | fputs("/* Syntax classes */\n", hfile); | |
140 | for (i = 0 ; synclass[i].name ; i++) { | |
141 | sprintf(buf, "#define %s %d", synclass[i].name, i); | |
142 | fputs(buf, hfile); | |
143 | for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07) | |
144 | putc('\t', hfile); | |
145 | fprintf(hfile, "/* %s */\n", synclass[i].comment); | |
146 | } | |
147 | putc('\n', hfile); | |
148 | fputs("/* Syntax classes for is_ functions */\n", hfile); | |
149 | for (i = 0 ; is_entry[i].name ; i++) { | |
150 | sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i); | |
151 | fputs(buf, hfile); | |
152 | for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07) | |
153 | putc('\t', hfile); | |
154 | fprintf(hfile, "/* %s */\n", is_entry[i].comment); | |
155 | } | |
156 | putc('\n', hfile); | |
f00eae14 PA |
157 | fputs("#define SYNBASE (1 - CHAR_MIN)\n", hfile); |
158 | fputs("#define PEOF -SYNBASE\n\n", hfile); | |
984263bc MD |
159 | putc('\n', hfile); |
160 | fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile); | |
161 | fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile); | |
162 | fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile); | |
163 | fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile); | |
164 | putc('\n', hfile); | |
165 | output_type_macros(); /* is_digit, etc. */ | |
166 | putc('\n', hfile); | |
167 | ||
168 | /* Generate the syntax tables. */ | |
f00eae14 | 169 | fputs("#include \"parser.h\"\n", cfile); |
984263bc MD |
170 | fputs("#include \"shell.h\"\n", cfile); |
171 | fputs("#include \"syntax.h\"\n\n", cfile); | |
f00eae14 | 172 | |
984263bc | 173 | fputs("/* syntax table used when not in quotes */\n", cfile); |
f00eae14 PA |
174 | init("basesyntax"); |
175 | add_default(); | |
984263bc MD |
176 | add("\n", "CNL"); |
177 | add("\\", "CBACK"); | |
178 | add("'", "CSQUOTE"); | |
179 | add("\"", "CDQUOTE"); | |
180 | add("`", "CBQUOTE"); | |
181 | add("$", "CVAR"); | |
182 | add("}", "CENDVAR"); | |
183 | add("<>();&| \t", "CSPCL"); | |
f00eae14 PA |
184 | finish(); |
185 | ||
984263bc | 186 | fputs("\n/* syntax table used when in double quotes */\n", cfile); |
f00eae14 PA |
187 | init("dqsyntax"); |
188 | add_default(); | |
3e3895bf | 189 | add("\n", "CQNL"); |
984263bc MD |
190 | add("\\", "CBACK"); |
191 | add("\"", "CENDQUOTE"); | |
192 | add("`", "CBQUOTE"); | |
193 | add("$", "CVAR"); | |
194 | add("}", "CENDVAR"); | |
4353aa4e PA |
195 | /* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */ |
196 | add("!*?[]=~:/-^", "CCTL"); | |
f00eae14 PA |
197 | finish(); |
198 | ||
984263bc | 199 | fputs("\n/* syntax table used when in single quotes */\n", cfile); |
f00eae14 PA |
200 | init("sqsyntax"); |
201 | add_default(); | |
3e3895bf | 202 | add("\n", "CQNL"); |
e1489450 | 203 | add("\\", "CSBACK"); |
984263bc | 204 | add("'", "CENDQUOTE"); |
4353aa4e PA |
205 | /* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */ |
206 | add("!*?[]=~:/-^", "CCTL"); | |
f00eae14 PA |
207 | finish(); |
208 | ||
984263bc | 209 | fputs("\n/* syntax table used when in arithmetic */\n", cfile); |
f00eae14 PA |
210 | init("arisyntax"); |
211 | add_default(); | |
3e3895bf | 212 | add("\n", "CQNL"); |
984263bc MD |
213 | add("\\", "CBACK"); |
214 | add("`", "CBQUOTE"); | |
99512ac4 | 215 | add("\"", "CIGN"); |
984263bc MD |
216 | add("$", "CVAR"); |
217 | add("}", "CENDVAR"); | |
218 | add("(", "CLP"); | |
219 | add(")", "CRP"); | |
f00eae14 PA |
220 | finish(); |
221 | ||
984263bc | 222 | fputs("\n/* character classification table */\n", cfile); |
f00eae14 | 223 | init("is_type"); |
984263bc | 224 | add("0123456789", "ISDIGIT"); |
99512ac4 PA |
225 | add("abcdefghijklmnopqrstuvwxyz", "ISLOWER"); |
226 | add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER"); | |
984263bc MD |
227 | add("_", "ISUNDER"); |
228 | add("#?$!-*@", "ISSPECL"); | |
f00eae14 PA |
229 | finish(); |
230 | ||
984263bc MD |
231 | exit(0); |
232 | } | |
233 | ||
234 | ||
984263bc | 235 | /* |
f00eae14 | 236 | * Output the header and declaration of a syntax table. |
984263bc MD |
237 | */ |
238 | ||
239 | static void | |
f00eae14 | 240 | init(const char *name) |
984263bc | 241 | { |
f00eae14 PA |
242 | fprintf(hfile, "extern const char %s[];\n", name); |
243 | fprintf(cfile, "const char %s[SYNBASE + CHAR_MAX + 1] = {\n", name); | |
244 | } | |
984263bc | 245 | |
f00eae14 PA |
246 | |
247 | static void | |
248 | add_one(const char *key, const char *type) | |
249 | { | |
250 | fprintf(cfile, "\t[SYNBASE + %s] = %s,\n", key, type); | |
984263bc MD |
251 | } |
252 | ||
253 | ||
254 | /* | |
f00eae14 | 255 | * Add default values to the syntax table. |
984263bc MD |
256 | */ |
257 | ||
258 | static void | |
f00eae14 | 259 | add_default(void) |
984263bc | 260 | { |
f00eae14 PA |
261 | add_one("PEOF", "CEOF"); |
262 | add_one("CTLESC", "CCTL"); | |
263 | add_one("CTLVAR", "CCTL"); | |
264 | add_one("CTLENDVAR", "CCTL"); | |
265 | add_one("CTLBACKQ", "CCTL"); | |
266 | add_one("CTLBACKQ + CTLQUOTE", "CCTL"); | |
267 | add_one("CTLARI", "CCTL"); | |
268 | add_one("CTLENDARI", "CCTL"); | |
269 | add_one("CTLQUOTEMARK", "CCTL"); | |
270 | add_one("CTLQUOTEEND", "CCTL"); | |
984263bc MD |
271 | } |
272 | ||
273 | ||
274 | /* | |
f00eae14 | 275 | * Output the footer of a syntax table. |
984263bc MD |
276 | */ |
277 | ||
278 | static void | |
f00eae14 | 279 | finish(void) |
984263bc | 280 | { |
f00eae14 | 281 | fputs("};\n", cfile); |
984263bc MD |
282 | } |
283 | ||
284 | ||
984263bc | 285 | /* |
f00eae14 | 286 | * Add entries to the syntax table. |
984263bc MD |
287 | */ |
288 | ||
289 | static void | |
f00eae14 | 290 | add(const char *p, const char *type) |
984263bc | 291 | { |
f00eae14 PA |
292 | for (; *p; ++p) { |
293 | char c = *p; | |
294 | switch (c) { | |
295 | case '\t': c = 't'; break; | |
296 | case '\n': c = 'n'; break; | |
297 | case '\'': c = '\''; break; | |
298 | case '\\': c = '\\'; break; | |
299 | ||
300 | default: | |
301 | fprintf(cfile, "\t[SYNBASE + '%c'] = %s,\n", c, type); | |
302 | continue; | |
984263bc | 303 | } |
f00eae14 | 304 | fprintf(cfile, "\t[SYNBASE + '\\%c'] = %s,\n", c, type); |
984263bc | 305 | } |
984263bc MD |
306 | } |
307 | ||
308 | ||
984263bc MD |
309 | /* |
310 | * Output character classification macros (e.g. is_digit). If digits are | |
311 | * contiguous, we can test for them quickly. | |
312 | */ | |
313 | ||
492efe05 | 314 | static const char *macro[] = { |
f00eae14 | 315 | "#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)", |
a3143cf4 | 316 | "#define is_eof(c)\t((c) == PEOF)", |
99512ac4 PA |
317 | "#define is_alpha(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER))", |
318 | "#define is_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER))", | |
319 | "#define is_in_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))", | |
320 | "#define is_special(c)\t((is_type+SYNBASE)[(int)c] & (ISSPECL|ISDIGIT))", | |
f00eae14 | 321 | "#define digit_val(c)\t((c) - '0')", |
984263bc MD |
322 | NULL |
323 | }; | |
324 | ||
325 | static void | |
326 | output_type_macros(void) | |
327 | { | |
492efe05 | 328 | const char **pp; |
984263bc | 329 | |
984263bc MD |
330 | for (pp = macro ; *pp ; pp++) |
331 | fprintf(hfile, "%s\n", *pp); | |
984263bc | 332 | } |