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