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