mksyntax.c revision 247058
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if 0
34#ifndef lint
35static char const copyright[] =
36"@(#) Copyright (c) 1991, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)mksyntax.c	8.2 (Berkeley) 5/4/95";
42#endif /* not lint */
43#endif
44#include <sys/cdefs.h>
45__FBSDID("$FreeBSD: stable/9/bin/sh/mksyntax.c 247058 2013-02-20 21:57:01Z jilles $");
46
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
57struct synclass {
58	const char *name;
59	const char *comment;
60};
61
62/* Syntax classes */
63struct synclass synclass[] = {
64	{ "CWORD",	"character is nothing special" },
65	{ "CNL",	"newline character" },
66	{ "CBACK",	"a backslash character" },
67	{ "CSBACK",	"a backslash character in single quotes" },
68	{ "CSQUOTE",	"single quote" },
69	{ "CDQUOTE",	"double quote" },
70	{ "CENDQUOTE",	"a terminating quote" },
71	{ "CBQUOTE",	"backwards single quote" },
72	{ "CVAR",	"a dollar sign" },
73	{ "CENDVAR",	"a '}' character" },
74	{ "CLP",	"a left paren in arithmetic" },
75	{ "CRP",	"a right paren in arithmetic" },
76	{ "CEOF",	"end of file" },
77	{ "CCTL",	"like CWORD, except it must be escaped" },
78	{ "CSPCL",	"these terminate a word" },
79	{ "CIGN",       "character should be ignored" },
80	{ NULL,		NULL }
81};
82
83
84/*
85 * Syntax classes for is_ functions.  Warning:  if you add new classes
86 * you may have to change the definition of the is_in_name macro.
87 */
88struct synclass is_entry[] = {
89	{ "ISDIGIT",	"a digit" },
90	{ "ISUPPER",	"an upper case letter" },
91	{ "ISLOWER",	"a lower case letter" },
92	{ "ISUNDER",	"an underscore" },
93	{ "ISSPECL",	"the name of a special parameter" },
94	{ NULL, 	NULL }
95};
96
97static char writer[] = "\
98/*\n\
99 * This file was generated by the mksyntax program.\n\
100 */\n\
101\n";
102
103
104static FILE *cfile;
105static FILE *hfile;
106static const char *syntax[513];
107static int base;
108static int size;	/* number of values which a char variable can have */
109static int nbits;	/* number of bits in a character */
110
111static void filltable(const char *);
112static void init(void);
113static void add(const char *, const char *);
114static void print(const char *);
115static void output_type_macros(void);
116
117int
118main(int argc __unused, char **argv __unused)
119{
120	char c;
121	char d;
122	int sign;
123	int i;
124	char buf[80];
125	int pos;
126
127	/* Create output files */
128	if ((cfile = fopen("syntax.c", "w")) == NULL) {
129		perror("syntax.c");
130		exit(2);
131	}
132	if ((hfile = fopen("syntax.h", "w")) == NULL) {
133		perror("syntax.h");
134		exit(2);
135	}
136	fputs(writer, hfile);
137	fputs(writer, cfile);
138
139	/* Determine the characteristics of chars. */
140	c = -1;
141	sign = (c > 0) ? 0 : 1;
142	for (nbits = 1 ; ; nbits++) {
143		d = (1 << nbits) - 1;
144		if (d == c)
145			break;
146	}
147#if 0
148	printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits);
149#endif
150	if (nbits > 9) {
151		fputs("Characters can't have more than 9 bits\n", stderr);
152		exit(2);
153	}
154	size = (1 << nbits) + 1;
155	base = 1;
156	if (sign)
157		base += 1 << (nbits - 1);
158
159	fputs("#include <sys/cdefs.h>\n", hfile);
160	fputs("#include <ctype.h>\n", hfile);
161
162	/* Generate the #define statements in the header file */
163	fputs("/* Syntax classes */\n", hfile);
164	for (i = 0 ; synclass[i].name ; i++) {
165		sprintf(buf, "#define %s %d", synclass[i].name, i);
166		fputs(buf, hfile);
167		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
168			putc('\t', hfile);
169		fprintf(hfile, "/* %s */\n", synclass[i].comment);
170	}
171	putc('\n', hfile);
172	fputs("/* Syntax classes for is_ functions */\n", hfile);
173	for (i = 0 ; is_entry[i].name ; i++) {
174		sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
175		fputs(buf, hfile);
176		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
177			putc('\t', hfile);
178		fprintf(hfile, "/* %s */\n", is_entry[i].comment);
179	}
180	putc('\n', hfile);
181	fprintf(hfile, "#define SYNBASE %d\n", base);
182	fprintf(hfile, "#define PEOF %d\n\n", -base);
183	putc('\n', hfile);
184	fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
185	fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
186	fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
187	fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
188	putc('\n', hfile);
189	output_type_macros();		/* is_digit, etc. */
190	putc('\n', hfile);
191
192	/* Generate the syntax tables. */
193	fputs("#include \"shell.h\"\n", cfile);
194	fputs("#include \"syntax.h\"\n\n", cfile);
195	init();
196	fputs("/* syntax table used when not in quotes */\n", cfile);
197	add("\n", "CNL");
198	add("\\", "CBACK");
199	add("'", "CSQUOTE");
200	add("\"", "CDQUOTE");
201	add("`", "CBQUOTE");
202	add("$", "CVAR");
203	add("}", "CENDVAR");
204	add("<>();&| \t", "CSPCL");
205	print("basesyntax");
206	init();
207	fputs("\n/* syntax table used when in double quotes */\n", cfile);
208	add("\n", "CNL");
209	add("\\", "CBACK");
210	add("\"", "CENDQUOTE");
211	add("`", "CBQUOTE");
212	add("$", "CVAR");
213	add("}", "CENDVAR");
214	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
215	add("!*?[=~:/-", "CCTL");
216	print("dqsyntax");
217	init();
218	fputs("\n/* syntax table used when in single quotes */\n", cfile);
219	add("\n", "CNL");
220	add("\\", "CSBACK");
221	add("'", "CENDQUOTE");
222	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
223	add("!*?[=~:/-", "CCTL");
224	print("sqsyntax");
225	init();
226	fputs("\n/* syntax table used when in arithmetic */\n", cfile);
227	add("\n", "CNL");
228	add("\\", "CBACK");
229	add("`", "CBQUOTE");
230	add("\"", "CIGN");
231	add("$", "CVAR");
232	add("}", "CENDVAR");
233	add("(", "CLP");
234	add(")", "CRP");
235	print("arisyntax");
236	filltable("0");
237	fputs("\n/* character classification table */\n", cfile);
238	add("0123456789", "ISDIGIT");
239	add("abcdefghijklmnopqrstuvwxyz", "ISLOWER");
240	add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER");
241	add("_", "ISUNDER");
242	add("#?$!-*@", "ISSPECL");
243	print("is_type");
244	exit(0);
245}
246
247
248
249/*
250 * Clear the syntax table.
251 */
252
253static void
254filltable(const char *dftval)
255{
256	int i;
257
258	for (i = 0 ; i < size ; i++)
259		syntax[i] = dftval;
260}
261
262
263/*
264 * Initialize the syntax table with default values.
265 */
266
267static void
268init(void)
269{
270	filltable("CWORD");
271	syntax[0] = "CEOF";
272	syntax[base + CTLESC] = "CCTL";
273	syntax[base + CTLVAR] = "CCTL";
274	syntax[base + CTLENDVAR] = "CCTL";
275	syntax[base + CTLBACKQ] = "CCTL";
276	syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
277	syntax[base + CTLARI] = "CCTL";
278	syntax[base + CTLENDARI] = "CCTL";
279	syntax[base + CTLQUOTEMARK] = "CCTL";
280	syntax[base + CTLQUOTEEND] = "CCTL";
281}
282
283
284/*
285 * Add entries to the syntax table.
286 */
287
288static void
289add(const char *p, const char *type)
290{
291	while (*p)
292		syntax[*p++ + base] = type;
293}
294
295
296
297/*
298 * Output the syntax table.
299 */
300
301static void
302print(const char *name)
303{
304	int i;
305	int col;
306
307	fprintf(hfile, "extern const char %s[];\n", name);
308	fprintf(cfile, "const char %s[%d] = {\n", name, size);
309	col = 0;
310	for (i = 0 ; i < size ; i++) {
311		if (i == 0) {
312			fputs("      ", cfile);
313		} else if ((i & 03) == 0) {
314			fputs(",\n      ", cfile);
315			col = 0;
316		} else {
317			putc(',', cfile);
318			while (++col < 9 * (i & 03))
319				putc(' ', cfile);
320		}
321		fputs(syntax[i], cfile);
322		col += strlen(syntax[i]);
323	}
324	fputs("\n};\n", cfile);
325}
326
327
328
329/*
330 * Output character classification macros (e.g. is_digit).  If digits are
331 * contiguous, we can test for them quickly.
332 */
333
334static const char *macro[] = {
335	"#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)",
336	"#define is_eof(c)\t((c) == PEOF)",
337	"#define is_alpha(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER))",
338	"#define is_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER))",
339	"#define is_in_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))",
340	"#define is_special(c)\t((is_type+SYNBASE)[(int)c] & (ISSPECL|ISDIGIT))",
341	"#define digit_val(c)\t((c) - '0')",
342	NULL
343};
344
345static void
346output_type_macros(void)
347{
348	const char **pp;
349
350	for (pp = macro ; *pp ; pp++)
351		fprintf(hfile, "%s\n", *pp);
352}
353