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