xref: /freebsd/bin/sh/mksyntax.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes  * Kenneth Almquist.
94b88c807SRodney W. Grimes  *
104b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
114b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
124b88c807SRodney W. Grimes  * are met:
134b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
154b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
164b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
174b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
194b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
204b88c807SRodney W. Grimes  *    without specific prior written permission.
214b88c807SRodney W. Grimes  *
224b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324b88c807SRodney W. Grimes  * SUCH DAMAGE.
334b88c807SRodney W. Grimes  */
344b88c807SRodney W. Grimes 
354b88c807SRodney W. Grimes /*
364b88c807SRodney W. Grimes  * This program creates syntax.h and syntax.c.
374b88c807SRodney W. Grimes  */
384b88c807SRodney W. Grimes 
394b88c807SRodney W. Grimes #include <stdio.h>
4026f6b0fbSDag-Erling Smørgrav #include <stdlib.h>
41aa9caaf6SPeter Wemm #include <string.h>
424b88c807SRodney W. Grimes #include "parser.h"
434b88c807SRodney W. Grimes 
444b88c807SRodney W. Grimes 
454b88c807SRodney W. Grimes struct synclass {
46384aedabSJilles Tjoelker 	const char *name;
47384aedabSJilles Tjoelker 	const char *comment;
484b88c807SRodney W. Grimes };
494b88c807SRodney W. Grimes 
504b88c807SRodney W. Grimes /* Syntax classes */
5158bdb076SJilles Tjoelker static const struct synclass synclass[] = {
52aa9caaf6SPeter Wemm 	{ "CWORD",	"character is nothing special" },
53aa9caaf6SPeter Wemm 	{ "CNL",	"newline character" },
54*dc0dbd74SJilles Tjoelker 	{ "CQNL",	"newline character in quotes" },
55aa9caaf6SPeter Wemm 	{ "CBACK",	"a backslash character" },
56a62ab027SJilles Tjoelker 	{ "CSBACK",	"a backslash character in single quotes" },
57aa9caaf6SPeter Wemm 	{ "CSQUOTE",	"single quote" },
58aa9caaf6SPeter Wemm 	{ "CDQUOTE",	"double quote" },
59aa9caaf6SPeter Wemm 	{ "CENDQUOTE",	"a terminating quote" },
60aa9caaf6SPeter Wemm 	{ "CBQUOTE",	"backwards single quote" },
61aa9caaf6SPeter Wemm 	{ "CVAR",	"a dollar sign" },
62aa9caaf6SPeter Wemm 	{ "CENDVAR",	"a '}' character" },
63aa9caaf6SPeter Wemm 	{ "CLP",	"a left paren in arithmetic" },
64aa9caaf6SPeter Wemm 	{ "CRP",	"a right paren in arithmetic" },
65aa9caaf6SPeter Wemm 	{ "CEOF",	"end of file" },
66aa9caaf6SPeter Wemm 	{ "CCTL",	"like CWORD, except it must be escaped" },
67aa9caaf6SPeter Wemm 	{ "CSPCL",	"these terminate a word" },
68d94c8673SJilles Tjoelker 	{ "CIGN",       "character should be ignored" },
69aa9caaf6SPeter Wemm 	{ NULL,		NULL }
704b88c807SRodney W. Grimes };
714b88c807SRodney W. Grimes 
724b88c807SRodney W. Grimes 
734b88c807SRodney W. Grimes /*
744b88c807SRodney W. Grimes  * Syntax classes for is_ functions.  Warning:  if you add new classes
754b88c807SRodney W. Grimes  * you may have to change the definition of the is_in_name macro.
764b88c807SRodney W. Grimes  */
7758bdb076SJilles Tjoelker static const struct synclass is_entry[] = {
78aa9caaf6SPeter Wemm 	{ "ISDIGIT",	"a digit" },
79aa9caaf6SPeter Wemm 	{ "ISUPPER",	"an upper case letter" },
80aa9caaf6SPeter Wemm 	{ "ISLOWER",	"a lower case letter" },
81aa9caaf6SPeter Wemm 	{ "ISUNDER",	"an underscore" },
82aa9caaf6SPeter Wemm 	{ "ISSPECL",	"the name of a special parameter" },
83aa9caaf6SPeter Wemm 	{ NULL, 	NULL }
844b88c807SRodney W. Grimes };
854b88c807SRodney W. Grimes 
8658bdb076SJilles Tjoelker static const char writer[] = "\
874b88c807SRodney W. Grimes /*\n\
884b88c807SRodney W. Grimes  * This file was generated by the mksyntax program.\n\
894b88c807SRodney W. Grimes  */\n\
904b88c807SRodney W. Grimes \n";
914b88c807SRodney W. Grimes 
924b88c807SRodney W. Grimes 
93aa9caaf6SPeter Wemm static FILE *cfile;
94aa9caaf6SPeter Wemm static FILE *hfile;
954b88c807SRodney W. Grimes 
961767d529SJilles Tjoelker static void add_default(void);
971767d529SJilles Tjoelker static void finish(void);
981767d529SJilles Tjoelker static void init(const char *);
99384aedabSJilles Tjoelker static void add(const char *, const char *);
1005134c3f7SWarner Losh static void output_type_macros(void);
1014b88c807SRodney W. Grimes 
102aa9caaf6SPeter Wemm int
main(int argc __unused,char ** argv __unused)1035134c3f7SWarner Losh main(int argc __unused, char **argv __unused)
104aa9caaf6SPeter Wemm {
1054b88c807SRodney W. Grimes 	int i;
1064b88c807SRodney W. Grimes 	char buf[80];
1074b88c807SRodney W. Grimes 	int pos;
1084b88c807SRodney W. Grimes 
1094b88c807SRodney W. Grimes 	/* Create output files */
1104b88c807SRodney W. Grimes 	if ((cfile = fopen("syntax.c", "w")) == NULL) {
1114b88c807SRodney W. Grimes 		perror("syntax.c");
1124b88c807SRodney W. Grimes 		exit(2);
1134b88c807SRodney W. Grimes 	}
1144b88c807SRodney W. Grimes 	if ((hfile = fopen("syntax.h", "w")) == NULL) {
1154b88c807SRodney W. Grimes 		perror("syntax.h");
1164b88c807SRodney W. Grimes 		exit(2);
1174b88c807SRodney W. Grimes 	}
1184b88c807SRodney W. Grimes 	fputs(writer, hfile);
1194b88c807SRodney W. Grimes 	fputs(writer, cfile);
1204b88c807SRodney W. Grimes 
1214b88c807SRodney W. Grimes 	fputs("#include <sys/cdefs.h>\n", hfile);
1221767d529SJilles Tjoelker 	fputs("#include <limits.h>\n\n", hfile);
1234b88c807SRodney W. Grimes 
1244b88c807SRodney W. Grimes 	/* Generate the #define statements in the header file */
1254b88c807SRodney W. Grimes 	fputs("/* Syntax classes */\n", hfile);
1264b88c807SRodney W. Grimes 	for (i = 0 ; synclass[i].name ; i++) {
1274b88c807SRodney W. Grimes 		sprintf(buf, "#define %s %d", synclass[i].name, i);
1284b88c807SRodney W. Grimes 		fputs(buf, hfile);
129aa9caaf6SPeter Wemm 		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
1304b88c807SRodney W. Grimes 			putc('\t', hfile);
1314b88c807SRodney W. Grimes 		fprintf(hfile, "/* %s */\n", synclass[i].comment);
1324b88c807SRodney W. Grimes 	}
1334b88c807SRodney W. Grimes 	putc('\n', hfile);
1344b88c807SRodney W. Grimes 	fputs("/* Syntax classes for is_ functions */\n", hfile);
1354b88c807SRodney W. Grimes 	for (i = 0 ; is_entry[i].name ; i++) {
1364b88c807SRodney W. Grimes 		sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
1374b88c807SRodney W. Grimes 		fputs(buf, hfile);
138aa9caaf6SPeter Wemm 		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
1394b88c807SRodney W. Grimes 			putc('\t', hfile);
1404b88c807SRodney W. Grimes 		fprintf(hfile, "/* %s */\n", is_entry[i].comment);
1414b88c807SRodney W. Grimes 	}
1424b88c807SRodney W. Grimes 	putc('\n', hfile);
1431767d529SJilles Tjoelker 	fputs("#define SYNBASE (1 - CHAR_MIN)\n", hfile);
1441767d529SJilles Tjoelker 	fputs("#define PEOF -SYNBASE\n\n", hfile);
1454b88c807SRodney W. Grimes 	putc('\n', hfile);
1464b88c807SRodney W. Grimes 	fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
1474b88c807SRodney W. Grimes 	fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
1484b88c807SRodney W. Grimes 	fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
1494b88c807SRodney W. Grimes 	fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
1504b88c807SRodney W. Grimes 	putc('\n', hfile);
1514b88c807SRodney W. Grimes 	output_type_macros();		/* is_digit, etc. */
1524b88c807SRodney W. Grimes 	putc('\n', hfile);
1534b88c807SRodney W. Grimes 
1544b88c807SRodney W. Grimes 	/* Generate the syntax tables. */
1551767d529SJilles Tjoelker 	fputs("#include \"parser.h\"\n", cfile);
1564b88c807SRodney W. Grimes 	fputs("#include \"shell.h\"\n", cfile);
1574b88c807SRodney W. Grimes 	fputs("#include \"syntax.h\"\n\n", cfile);
1581767d529SJilles Tjoelker 
1594b88c807SRodney W. Grimes 	fputs("/* syntax table used when not in quotes */\n", cfile);
1601767d529SJilles Tjoelker 	init("basesyntax");
1611767d529SJilles Tjoelker 	add_default();
1624b88c807SRodney W. Grimes 	add("\n", "CNL");
1634b88c807SRodney W. Grimes 	add("\\", "CBACK");
1644b88c807SRodney W. Grimes 	add("'", "CSQUOTE");
1654b88c807SRodney W. Grimes 	add("\"", "CDQUOTE");
1664b88c807SRodney W. Grimes 	add("`", "CBQUOTE");
1674b88c807SRodney W. Grimes 	add("$", "CVAR");
1684b88c807SRodney W. Grimes 	add("}", "CENDVAR");
1694b88c807SRodney W. Grimes 	add("<>();&| \t", "CSPCL");
1701767d529SJilles Tjoelker 	finish();
1711767d529SJilles Tjoelker 
1724b88c807SRodney W. Grimes 	fputs("\n/* syntax table used when in double quotes */\n", cfile);
1731767d529SJilles Tjoelker 	init("dqsyntax");
1741767d529SJilles Tjoelker 	add_default();
175*dc0dbd74SJilles Tjoelker 	add("\n", "CQNL");
1764b88c807SRodney W. Grimes 	add("\\", "CBACK");
1774b88c807SRodney W. Grimes 	add("\"", "CENDQUOTE");
1784b88c807SRodney W. Grimes 	add("`", "CBQUOTE");
1794b88c807SRodney W. Grimes 	add("$", "CVAR");
1804b88c807SRodney W. Grimes 	add("}", "CENDVAR");
1813a1b9c9eSJilles Tjoelker 	/* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */
1823a1b9c9eSJilles Tjoelker 	add("!*?[]=~:/-^", "CCTL");
1831767d529SJilles Tjoelker 	finish();
1841767d529SJilles Tjoelker 
1854b88c807SRodney W. Grimes 	fputs("\n/* syntax table used when in single quotes */\n", cfile);
1861767d529SJilles Tjoelker 	init("sqsyntax");
1871767d529SJilles Tjoelker 	add_default();
188*dc0dbd74SJilles Tjoelker 	add("\n", "CQNL");
189a62ab027SJilles Tjoelker 	add("\\", "CSBACK");
1904b88c807SRodney W. Grimes 	add("'", "CENDQUOTE");
1913a1b9c9eSJilles Tjoelker 	/* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */
1923a1b9c9eSJilles Tjoelker 	add("!*?[]=~:/-^", "CCTL");
1931767d529SJilles Tjoelker 	finish();
1941767d529SJilles Tjoelker 
1954b88c807SRodney W. Grimes 	fputs("\n/* syntax table used when in arithmetic */\n", cfile);
1961767d529SJilles Tjoelker 	init("arisyntax");
1971767d529SJilles Tjoelker 	add_default();
198*dc0dbd74SJilles Tjoelker 	add("\n", "CQNL");
1994b88c807SRodney W. Grimes 	add("\\", "CBACK");
2004b88c807SRodney W. Grimes 	add("`", "CBQUOTE");
201d94c8673SJilles Tjoelker 	add("\"", "CIGN");
2024b88c807SRodney W. Grimes 	add("$", "CVAR");
2034b88c807SRodney W. Grimes 	add("}", "CENDVAR");
2044b88c807SRodney W. Grimes 	add("(", "CLP");
2054b88c807SRodney W. Grimes 	add(")", "CRP");
2061767d529SJilles Tjoelker 	finish();
2071767d529SJilles Tjoelker 
2084b88c807SRodney W. Grimes 	fputs("\n/* character classification table */\n", cfile);
2091767d529SJilles Tjoelker 	init("is_type");
2104b88c807SRodney W. Grimes 	add("0123456789", "ISDIGIT");
21140969e73SJilles Tjoelker 	add("abcdefghijklmnopqrstuvwxyz", "ISLOWER");
21240969e73SJilles Tjoelker 	add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER");
2134b88c807SRodney W. Grimes 	add("_", "ISUNDER");
2144b88c807SRodney W. Grimes 	add("#?$!-*@", "ISSPECL");
2151767d529SJilles Tjoelker 	finish();
2161767d529SJilles Tjoelker 
2174b88c807SRodney W. Grimes 	exit(0);
2184b88c807SRodney W. Grimes }
2194b88c807SRodney W. Grimes 
2204b88c807SRodney W. Grimes 
2214b88c807SRodney W. Grimes /*
2221767d529SJilles Tjoelker  * Output the header and declaration of a syntax table.
2234b88c807SRodney W. Grimes  */
2244b88c807SRodney W. Grimes 
225aa9caaf6SPeter Wemm static void
init(const char * name)2261767d529SJilles Tjoelker init(const char *name)
2274b88c807SRodney W. Grimes {
2281767d529SJilles Tjoelker 	fprintf(hfile, "extern const char %s[];\n", name);
2291767d529SJilles Tjoelker 	fprintf(cfile, "const char %s[SYNBASE + CHAR_MAX + 1] = {\n", name);
2301767d529SJilles Tjoelker }
2314b88c807SRodney W. Grimes 
2321767d529SJilles Tjoelker 
2331767d529SJilles Tjoelker static void
add_one(const char * key,const char * type)2341767d529SJilles Tjoelker add_one(const char *key, const char *type)
2351767d529SJilles Tjoelker {
2361767d529SJilles Tjoelker 	fprintf(cfile, "\t[SYNBASE + %s] = %s,\n", key, type);
2374b88c807SRodney W. Grimes }
2384b88c807SRodney W. Grimes 
2394b88c807SRodney W. Grimes 
2404b88c807SRodney W. Grimes /*
2411767d529SJilles Tjoelker  * Add default values to the syntax table.
2424b88c807SRodney W. Grimes  */
2434b88c807SRodney W. Grimes 
244aa9caaf6SPeter Wemm static void
add_default(void)2451767d529SJilles Tjoelker add_default(void)
246aa9caaf6SPeter Wemm {
2471767d529SJilles Tjoelker 	add_one("PEOF",                "CEOF");
2481767d529SJilles Tjoelker 	add_one("CTLESC",              "CCTL");
2491767d529SJilles Tjoelker 	add_one("CTLVAR",              "CCTL");
2501767d529SJilles Tjoelker 	add_one("CTLENDVAR",           "CCTL");
2511767d529SJilles Tjoelker 	add_one("CTLBACKQ",            "CCTL");
2521767d529SJilles Tjoelker 	add_one("CTLBACKQ + CTLQUOTE", "CCTL");
2531767d529SJilles Tjoelker 	add_one("CTLARI",              "CCTL");
2541767d529SJilles Tjoelker 	add_one("CTLENDARI",           "CCTL");
2551767d529SJilles Tjoelker 	add_one("CTLQUOTEMARK",        "CCTL");
2561767d529SJilles Tjoelker 	add_one("CTLQUOTEEND",         "CCTL");
2571767d529SJilles Tjoelker }
2581767d529SJilles Tjoelker 
2591767d529SJilles Tjoelker 
2601767d529SJilles Tjoelker /*
2611767d529SJilles Tjoelker  * Output the footer of a syntax table.
2621767d529SJilles Tjoelker  */
2631767d529SJilles Tjoelker 
2641767d529SJilles Tjoelker static void
finish(void)2651767d529SJilles Tjoelker finish(void)
2661767d529SJilles Tjoelker {
2671767d529SJilles Tjoelker 	fputs("};\n", cfile);
2684b88c807SRodney W. Grimes }
2694b88c807SRodney W. Grimes 
2704b88c807SRodney W. Grimes 
2714b88c807SRodney W. Grimes /*
2724b88c807SRodney W. Grimes  * Add entries to the syntax table.
2734b88c807SRodney W. Grimes  */
2744b88c807SRodney W. Grimes 
275aa9caaf6SPeter Wemm static void
add(const char * p,const char * type)276384aedabSJilles Tjoelker add(const char *p, const char *type)
2774b88c807SRodney W. Grimes {
2781767d529SJilles Tjoelker 	for (; *p; ++p) {
2791767d529SJilles Tjoelker 		char c = *p;
2801767d529SJilles Tjoelker 		switch (c) {
2811767d529SJilles Tjoelker 		case '\t': c = 't';  break;
2821767d529SJilles Tjoelker 		case '\n': c = 'n';  break;
2831767d529SJilles Tjoelker 		case '\'': c = '\''; break;
2841767d529SJilles Tjoelker 		case '\\': c = '\\'; break;
2851767d529SJilles Tjoelker 
2861767d529SJilles Tjoelker 		default:
2871767d529SJilles Tjoelker 			fprintf(cfile, "\t[SYNBASE + '%c'] = %s,\n", c, type);
2881767d529SJilles Tjoelker 			continue;
2894b88c807SRodney W. Grimes 		}
2901767d529SJilles Tjoelker 		fprintf(cfile, "\t[SYNBASE + '\\%c'] = %s,\n", c, type);
2914b88c807SRodney W. Grimes 	}
2924b88c807SRodney W. Grimes }
2934b88c807SRodney W. Grimes 
2944b88c807SRodney W. Grimes 
2954b88c807SRodney W. Grimes /*
2964b88c807SRodney W. Grimes  * Output character classification macros (e.g. is_digit).  If digits are
2974b88c807SRodney W. Grimes  * contiguous, we can test for them quickly.
2984b88c807SRodney W. Grimes  */
2994b88c807SRodney W. Grimes 
300384aedabSJilles Tjoelker static const char *macro[] = {
301eaf77199SJilles Tjoelker 	"#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)",
302716b138bSStefan Farfeleder 	"#define is_eof(c)\t((c) == PEOF)",
303467fdf32SJilles Tjoelker 	"#define is_alpha(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER))",
304467fdf32SJilles Tjoelker 	"#define is_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER))",
305467fdf32SJilles Tjoelker 	"#define is_in_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))",
306fe5d61a4SJilles Tjoelker 	"#define is_special(c)\t((is_type+SYNBASE)[(int)c] & (ISSPECL|ISDIGIT))",
307eaf77199SJilles Tjoelker 	"#define digit_val(c)\t((c) - '0')",
3084b88c807SRodney W. Grimes 	NULL
3094b88c807SRodney W. Grimes };
3104b88c807SRodney W. Grimes 
311aa9caaf6SPeter Wemm static void
output_type_macros(void)3125134c3f7SWarner Losh output_type_macros(void)
313aa9caaf6SPeter Wemm {
314384aedabSJilles Tjoelker 	const char **pp;
3154b88c807SRodney W. Grimes 
3164b88c807SRodney W. Grimes 	for (pp = macro ; *pp ; pp++)
3174b88c807SRodney W. Grimes 		fprintf(hfile, "%s\n", *pp);
3184b88c807SRodney W. Grimes }
319