xref: /freebsd/bin/sh/mksyntax.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
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 <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include "parser.h"
43 
44 
45 struct synclass {
46 	const char *name;
47 	const char *comment;
48 };
49 
50 /* Syntax classes */
51 static const struct synclass synclass[] = {
52 	{ "CWORD",	"character is nothing special" },
53 	{ "CNL",	"newline character" },
54 	{ "CQNL",	"newline character in quotes" },
55 	{ "CBACK",	"a backslash character" },
56 	{ "CSBACK",	"a backslash character in single 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" },
69 	{ NULL,		NULL }
70 };
71 
72 
73 /*
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.
76  */
77 static const 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" },
83 	{ NULL, 	NULL }
84 };
85 
86 static const char writer[] = "\
87 /*\n\
88  * This file was generated by the mksyntax program.\n\
89  */\n\
90 \n";
91 
92 
93 static FILE *cfile;
94 static FILE *hfile;
95 
96 static void add_default(void);
97 static void finish(void);
98 static void init(const char *);
99 static void add(const char *, const char *);
100 static void output_type_macros(void);
101 
102 int
103 main(int argc __unused, char **argv __unused)
104 {
105 	int i;
106 	char buf[80];
107 	int pos;
108 
109 	/* Create output files */
110 	if ((cfile = fopen("syntax.c", "w")) == NULL) {
111 		perror("syntax.c");
112 		exit(2);
113 	}
114 	if ((hfile = fopen("syntax.h", "w")) == NULL) {
115 		perror("syntax.h");
116 		exit(2);
117 	}
118 	fputs(writer, hfile);
119 	fputs(writer, cfile);
120 
121 	fputs("#include <sys/cdefs.h>\n", hfile);
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 		sprintf(buf, "#define %s %d", synclass[i].name, i);
128 		fputs(buf, hfile);
129 		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
130 			putc('\t', hfile);
131 		fprintf(hfile, "/* %s */\n", synclass[i].comment);
132 	}
133 	putc('\n', hfile);
134 	fputs("/* Syntax classes for is_ functions */\n", hfile);
135 	for (i = 0 ; is_entry[i].name ; i++) {
136 		sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
137 		fputs(buf, hfile);
138 		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
139 			putc('\t', hfile);
140 		fprintf(hfile, "/* %s */\n", is_entry[i].comment);
141 	}
142 	putc('\n', hfile);
143 	fputs("#define SYNBASE (1 - CHAR_MIN)\n", hfile);
144 	fputs("#define PEOF -SYNBASE\n\n", hfile);
145 	putc('\n', hfile);
146 	fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
147 	fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
148 	fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
149 	fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
150 	putc('\n', hfile);
151 	output_type_macros();		/* is_digit, etc. */
152 	putc('\n', hfile);
153 
154 	/* Generate the syntax tables. */
155 	fputs("#include \"parser.h\"\n", cfile);
156 	fputs("#include \"shell.h\"\n", cfile);
157 	fputs("#include \"syntax.h\"\n\n", cfile);
158 
159 	fputs("/* syntax table used when not in quotes */\n", cfile);
160 	init("basesyntax");
161 	add_default();
162 	add("\n", "CNL");
163 	add("\\", "CBACK");
164 	add("'", "CSQUOTE");
165 	add("\"", "CDQUOTE");
166 	add("`", "CBQUOTE");
167 	add("$", "CVAR");
168 	add("}", "CENDVAR");
169 	add("<>();&| \t", "CSPCL");
170 	finish();
171 
172 	fputs("\n/* syntax table used when in double quotes */\n", cfile);
173 	init("dqsyntax");
174 	add_default();
175 	add("\n", "CQNL");
176 	add("\\", "CBACK");
177 	add("\"", "CENDQUOTE");
178 	add("`", "CBQUOTE");
179 	add("$", "CVAR");
180 	add("}", "CENDVAR");
181 	/* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */
182 	add("!*?[]=~:/-^", "CCTL");
183 	finish();
184 
185 	fputs("\n/* syntax table used when in single quotes */\n", cfile);
186 	init("sqsyntax");
187 	add_default();
188 	add("\n", "CQNL");
189 	add("\\", "CSBACK");
190 	add("'", "CENDQUOTE");
191 	/* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */
192 	add("!*?[]=~:/-^", "CCTL");
193 	finish();
194 
195 	fputs("\n/* syntax table used when in arithmetic */\n", cfile);
196 	init("arisyntax");
197 	add_default();
198 	add("\n", "CQNL");
199 	add("\\", "CBACK");
200 	add("`", "CBQUOTE");
201 	add("\"", "CIGN");
202 	add("$", "CVAR");
203 	add("}", "CENDVAR");
204 	add("(", "CLP");
205 	add(")", "CRP");
206 	finish();
207 
208 	fputs("\n/* character classification table */\n", cfile);
209 	init("is_type");
210 	add("0123456789", "ISDIGIT");
211 	add("abcdefghijklmnopqrstuvwxyz", "ISLOWER");
212 	add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER");
213 	add("_", "ISUNDER");
214 	add("#?$!-*@", "ISSPECL");
215 	finish();
216 
217 	exit(0);
218 }
219 
220 
221 /*
222  * Output the header and declaration of a syntax table.
223  */
224 
225 static void
226 init(const char *name)
227 {
228 	fprintf(hfile, "extern const char %s[];\n", name);
229 	fprintf(cfile, "const char %s[SYNBASE + CHAR_MAX + 1] = {\n", name);
230 }
231 
232 
233 static void
234 add_one(const char *key, const char *type)
235 {
236 	fprintf(cfile, "\t[SYNBASE + %s] = %s,\n", key, type);
237 }
238 
239 
240 /*
241  * Add default values to the syntax table.
242  */
243 
244 static void
245 add_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 /*
261  * Output the footer of a syntax table.
262  */
263 
264 static void
265 finish(void)
266 {
267 	fputs("};\n", cfile);
268 }
269 
270 
271 /*
272  * Add entries to the syntax table.
273  */
274 
275 static void
276 add(const char *p, const char *type)
277 {
278 	for (; *p; ++p) {
279 		char c = *p;
280 		switch (c) {
281 		case '\t': c = 't';  break;
282 		case '\n': c = 'n';  break;
283 		case '\'': c = '\''; break;
284 		case '\\': c = '\\'; break;
285 
286 		default:
287 			fprintf(cfile, "\t[SYNBASE + '%c'] = %s,\n", c, type);
288 			continue;
289 		}
290 		fprintf(cfile, "\t[SYNBASE + '\\%c'] = %s,\n", c, type);
291 	}
292 }
293 
294 
295 /*
296  * Output character classification macros (e.g. is_digit).  If digits are
297  * contiguous, we can test for them quickly.
298  */
299 
300 static const char *macro[] = {
301 	"#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)",
302 	"#define is_eof(c)\t((c) == PEOF)",
303 	"#define is_alpha(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER))",
304 	"#define is_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER))",
305 	"#define is_in_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))",
306 	"#define is_special(c)\t((is_type+SYNBASE)[(int)c] & (ISSPECL|ISDIGIT))",
307 	"#define digit_val(c)\t((c) - '0')",
308 	NULL
309 };
310 
311 static void
312 output_type_macros(void)
313 {
314 	const char **pp;
315 
316 	for (pp = macro ; *pp ; pp++)
317 		fprintf(hfile, "%s\n", *pp);
318 }
319