xref: /freebsd/bin/sh/mksyntax.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
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 #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>
47 __FBSDID("$FreeBSD$");
48 
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 {
60 	const char *name;
61 	const char *comment;
62 };
63 
64 /* Syntax classes */
65 static const struct synclass synclass[] = {
66 	{ "CWORD",	"character is nothing special" },
67 	{ "CNL",	"newline character" },
68 	{ "CQNL",	"newline character in quotes" },
69 	{ "CBACK",	"a backslash character" },
70 	{ "CSBACK",	"a backslash character in single quotes" },
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" },
82 	{ "CIGN",       "character should be ignored" },
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  */
91 static const struct synclass is_entry[] = {
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 
100 static const char writer[] = "\
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;
109 
110 static void add_default(void);
111 static void finish(void);
112 static void init(const char *);
113 static void add(const char *, const char *);
114 static void output_type_macros(void);
115 
116 int
117 main(int argc __unused, char **argv __unused)
118 {
119 	int i;
120 	char buf[80];
121 	int pos;
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 
135 	fputs("#include <sys/cdefs.h>\n", hfile);
136 	fputs("#include <limits.h>\n\n", hfile);
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);
157 	fputs("#define SYNBASE (1 - CHAR_MIN)\n", hfile);
158 	fputs("#define PEOF -SYNBASE\n\n", hfile);
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. */
169 	fputs("#include \"parser.h\"\n", cfile);
170 	fputs("#include \"shell.h\"\n", cfile);
171 	fputs("#include \"syntax.h\"\n\n", cfile);
172 
173 	fputs("/* syntax table used when not in quotes */\n", cfile);
174 	init("basesyntax");
175 	add_default();
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");
184 	finish();
185 
186 	fputs("\n/* syntax table used when in double quotes */\n", cfile);
187 	init("dqsyntax");
188 	add_default();
189 	add("\n", "CQNL");
190 	add("\\", "CBACK");
191 	add("\"", "CENDQUOTE");
192 	add("`", "CBQUOTE");
193 	add("$", "CVAR");
194 	add("}", "CENDVAR");
195 	/* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */
196 	add("!*?[]=~:/-^", "CCTL");
197 	finish();
198 
199 	fputs("\n/* syntax table used when in single quotes */\n", cfile);
200 	init("sqsyntax");
201 	add_default();
202 	add("\n", "CQNL");
203 	add("\\", "CSBACK");
204 	add("'", "CENDQUOTE");
205 	/* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */
206 	add("!*?[]=~:/-^", "CCTL");
207 	finish();
208 
209 	fputs("\n/* syntax table used when in arithmetic */\n", cfile);
210 	init("arisyntax");
211 	add_default();
212 	add("\n", "CQNL");
213 	add("\\", "CBACK");
214 	add("`", "CBQUOTE");
215 	add("\"", "CIGN");
216 	add("$", "CVAR");
217 	add("}", "CENDVAR");
218 	add("(", "CLP");
219 	add(")", "CRP");
220 	finish();
221 
222 	fputs("\n/* character classification table */\n", cfile);
223 	init("is_type");
224 	add("0123456789", "ISDIGIT");
225 	add("abcdefghijklmnopqrstuvwxyz", "ISLOWER");
226 	add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER");
227 	add("_", "ISUNDER");
228 	add("#?$!-*@", "ISSPECL");
229 	finish();
230 
231 	exit(0);
232 }
233 
234 
235 /*
236  * Output the header and declaration of a syntax table.
237  */
238 
239 static void
240 init(const char *name)
241 {
242 	fprintf(hfile, "extern const char %s[];\n", name);
243 	fprintf(cfile, "const char %s[SYNBASE + CHAR_MAX + 1] = {\n", name);
244 }
245 
246 
247 static void
248 add_one(const char *key, const char *type)
249 {
250 	fprintf(cfile, "\t[SYNBASE + %s] = %s,\n", key, type);
251 }
252 
253 
254 /*
255  * Add default values to the syntax table.
256  */
257 
258 static void
259 add_default(void)
260 {
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");
271 }
272 
273 
274 /*
275  * Output the footer of a syntax table.
276  */
277 
278 static void
279 finish(void)
280 {
281 	fputs("};\n", cfile);
282 }
283 
284 
285 /*
286  * Add entries to the syntax table.
287  */
288 
289 static void
290 add(const char *p, const char *type)
291 {
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;
303 		}
304 		fprintf(cfile, "\t[SYNBASE + '\\%c'] = %s,\n", c, type);
305 	}
306 }
307 
308 
309 /*
310  * Output character classification macros (e.g. is_digit).  If digits are
311  * contiguous, we can test for them quickly.
312  */
313 
314 static const char *macro[] = {
315 	"#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)",
316 	"#define is_eof(c)\t((c) == PEOF)",
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))",
321 	"#define digit_val(c)\t((c) - '0')",
322 	NULL
323 };
324 
325 static void
326 output_type_macros(void)
327 {
328 	const char **pp;
329 
330 	for (pp = macro ; *pp ; pp++)
331 		fprintf(hfile, "%s\n", *pp);
332 }
333