1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
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.
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.
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
35/*
36 * This program creates syntax.h and syntax.c.
37 */
38
39#include "parser.h"
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43
44#ifndef __unused
45#define __unused __attribute__((__unused__))
46#endif
47
48struct synclass {
49 const char *name;
50 const char *comment;
51};
52
53/* Syntax classes */
54static const struct synclass synclass[] = {
55 {"CWORD", "character is nothing special"},
56 {"CNL", "newline character"},
57 {"CQNL", "newline character in quotes"},
58 {"CBACK", "a backslash character"},
59 {"CSBACK", "a backslash character in single quotes"},
60 {"CSQUOTE", "single quote"},
61 {"CDQUOTE", "double quote"},
62 {"CENDQUOTE", "a terminating quote"},
63 {"CBQUOTE", "backwards single quote"},
64 {"CVAR", "a dollar sign"},
65 {"CENDVAR", "a '}' character"},
66 {"CLP", "a left paren in arithmetic"},
67 {"CRP", "a right paren in arithmetic"},
68 {"CEOF", "end of file"},
69 {"CCTL", "like CWORD, except it must be escaped"},
70 {"CSPCL", "these terminate a word"},
71 {"CIGN", "character should be ignored"},
72 {NULL, NULL}
73};
74
75/*
76 * Syntax classes for is_ functions. Warning: if you add new classes
77 * you may have to change the definition of the is_in_name macro.
78 */
79static const struct synclass is_entry[] = {
80 {"ISDIGIT", "a digit"},
81 {"ISUPPER", "an upper case letter"},
82 {"ISLOWER", "a lower case letter"},
83 {"ISUNDER", "an underscore"},
84 {"ISSPECL", "the name of a special parameter"},
85 {NULL, NULL}
86};
87
88static const char writer[] = "\
89/*\n\
90 * This file was generated by the mksyntax program.\n\
91 */\n\
92\n";
93
94static FILE *cfile;
95static FILE *hfile;
96
97static void add_default(void);
98static void finish(void);
99static void init(const char *);
100static void add(const char *, const char *);
101static void output_type_macros(void);
102
103int
104main(int argc __unused, char **argv __unused)
105{
106 int i;
107 char buf[80];
108 int pos;
109
110 /* Create output files */
111 if ((cfile = fopen("syntax.c", "w")) == NULL) {
112 perror("syntax.c");
113 exit(2);
114 }
115 if ((hfile = fopen("syntax.h", "w")) == NULL) {
116 perror("syntax.h");
117 exit(2);
118 }
119 fputs(writer, hfile);
120 fputs(writer, cfile);
121
122 fputs("#include <limits.h>\n\n", hfile);
123
124 /* Generate the #define statements in the header file */
125 fputs("/* Syntax classes */\n", hfile);
126 for (i = 0; synclass[i].name; i++) {
127 if (strcmp(synclass[i].name, "CEOF") == 0) {
128 fputs("#ifdef CEOF\n#undef CEOF\n#endif\n", hfile);
129 }
130 sprintf(buf, "#define %s %d", synclass[i].name, i);
131 fputs(buf, hfile);
132 for (pos = strlen(buf); pos < 32; pos = (pos + 8) & ~07)
133 putc('\t', hfile);
134 fprintf(hfile, "/* %s */\n", synclass[i].comment);
135 }
136 putc('\n', hfile);
137 fputs("/* Syntax classes for is_ functions */\n", hfile);
138 for (i = 0; is_entry[i].name; i++) {
139 sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
140 fputs(buf, hfile);
141 for (pos = strlen(buf); pos < 32; pos = (pos + 8) & ~07)
142 putc('\t', hfile);
143 fprintf(hfile, "/* %s */\n", is_entry[i].comment);
144 }
145 putc('\n', hfile);
146 fputs("#define SYNBASE (1 - CHAR_MIN)\n", hfile);
147 fputs("#define PEOF -SYNBASE\n\n", hfile);
148 putc('\n', hfile);
149 fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
150 fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
151 fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
152 fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
153 putc('\n', hfile);
154 output_type_macros(); /* is_digit, etc. */
155 putc('\n', hfile);
156
157 /* Generate the syntax tables. */
158 fputs("#include \"parser.h\"\n", cfile);
159 fputs("#include \"shell.h\"\n", cfile);
160 fputs("#include \"syntax.h\"\n\n", cfile);
161
162 fputs("/* syntax table used when not in quotes */\n", cfile);
163 init("basesyntax");
164 add_default();
165 add("\n", "CNL");
166 add("\\", "CBACK");
167 add("'", "CSQUOTE");
168 add("\"", "CDQUOTE");
169 add("`", "CBQUOTE");
170 add("$", "CVAR");
171 add("}", "CENDVAR");
172 add("<>();&| \t", "CSPCL");
173 finish();
174
175 fputs("\n/* syntax table used when in double quotes */\n", cfile);
176 init("dqsyntax");
177 add_default();
178 add("\n", "CQNL");
179 add("\\", "CBACK");
180 add("\"", "CENDQUOTE");
181 add("`", "CBQUOTE");
182 add("$", "CVAR");
183 add("}", "CENDVAR");
184 /* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */
185 add("!*?[]=~:/-^", "CCTL");
186 finish();
187
188 fputs("\n/* syntax table used when in single quotes */\n", cfile);
189 init("sqsyntax");
190 add_default();
191 add("\n", "CQNL");
192 add("\\", "CSBACK");
193 add("'", "CENDQUOTE");
194 /* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */
195 add("!*?[]=~:/-^", "CCTL");
196 finish();
197
198 fputs("\n/* syntax table used when in arithmetic */\n", cfile);
199 init("arisyntax");
200 add_default();
201 add("\n", "CQNL");
202 add("\\", "CBACK");
203 add("`", "CBQUOTE");
204 add("\"", "CIGN");
205 add("$", "CVAR");
206 add("}", "CENDVAR");
207 add("(", "CLP");
208 add(")", "CRP");
209 finish();
210
211 fputs("\n/* character classification table */\n", cfile);
212 init("is_type");
213 add("0123456789", "ISDIGIT");
214 add("abcdefghijklmnopqrstuvwxyz", "ISLOWER");
215 add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER");
216 add("_", "ISUNDER");
217 add("#?$!-*@", "ISSPECL");
218 finish();
219
220 exit(0);
221}
222
223/*
224 * Output the header and declaration of a syntax table.
225 */
226
227static void
228init(const char *name)
229{
230 fprintf(hfile, "extern const char %s[];\n", name);
231 fprintf(cfile, "const char %s[SYNBASE + CHAR_MAX + 1] = {\n", name);
232}
233
234static void
235add_one(const char *key, const char *type)
236{
237 fprintf(cfile, "\t[SYNBASE + %s] = %s,\n", key, type);
238}
239
240/*
241 * Add default values to the syntax table.
242 */
243
244static void
245add_default(void)
246{
247 add_one("PEOF", "CEOF");
248 add_one("CTLESC", "CCTL");
249 add_one("CTLVAR", "CCTL");
250 add_one("CTLENDVAR", "CCTL");
251 add_one("CTLBACKQ", "CCTL");
252 add_one("CTLBACKQ + CTLQUOTE", "CCTL");
253 add_one("CTLARI", "CCTL");
254 add_one("CTLENDARI", "CCTL");
255 add_one("CTLQUOTEMARK", "CCTL");
256 add_one("CTLQUOTEEND", "CCTL");
257}
258
259/*
260 * Output the footer of a syntax table.
261 */
262
263static void
264finish(void)
265{
266 fputs("};\n", cfile);
267}
268
269/*
270 * Add entries to the syntax table.
271 */
272
273static void
274add(const char *p, const char *type)
275{
276 for (; *p; ++p) {
277 char c = *p;
278 switch (c) {
279 case '\t':
280 c = 't';
281 break;
282 case '\n':
283 c = 'n';
284 break;
285 case '\'':
286 c = '\'';
287 break;
288 case '\\':
289 c = '\\';
290 break;
291
292 default:
293 fprintf(cfile, "\t[SYNBASE + '%c'] = %s,\n", c, type);
294 continue;
295 }
296 fprintf(cfile, "\t[SYNBASE + '\\%c'] = %s,\n", c, type);
297 }
298}
299
300/*
301 * Output character classification macros (e.g. is_digit). If digits are
302 * contiguous, we can test for them quickly.
303 */
304
305static const char *macro[] = {
306 "#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)",
307 "#define is_eof(c)\t((c) == PEOF)",
308 "#define is_alpha(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER))",
309 "#define is_name(c)\t((is_type+SYNBASE)[(int)c] & "
310 "(ISUPPER|ISLOWER|ISUNDER))",
311 "#define is_in_name(c)\t((is_type+SYNBASE)[(int)c] & "
312 "(ISUPPER|ISLOWER|ISUNDER|ISDIGIT))",
313 "#define is_special(c)\t((is_type+SYNBASE)[(int)c] & "
314 "(ISSPECL|ISDIGIT))",
315 "#define digit_val(c)\t((c) - '0')",
316 NULL
317};
318
319static void
320output_type_macros(void)
321{
322 const char **pp;
323
324 for (pp = macro; *pp; pp++)
325 fprintf(hfile, "%s\n", *pp);
326}