mksyntax.c revision 36150
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 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static char const copyright[] =
39"@(#) Copyright (c) 1991, 1993\n\
40	The Regents of the University of California.  All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)mksyntax.c	8.2 (Berkeley) 5/4/95";
46#endif
47static const char rcsid[] =
48	"$Id$";
49#endif /* not lint */
50
51/*
52 * This program creates syntax.h and syntax.c.
53 */
54
55#include <stdio.h>
56#include <string.h>
57#include "parser.h"
58
59
60struct synclass {
61	char *name;
62	char *comment;
63};
64
65/* Syntax classes */
66struct synclass synclass[] = {
67	{ "CWORD",	"character is nothing special" },
68	{ "CNL",	"newline character" },
69	{ "CBACK",	"a backslash character" },
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	{ 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 */
89struct 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
98static char writer[] = "\
99/*\n\
100 * This file was generated by the mksyntax program.\n\
101 */\n\
102\n";
103
104
105static FILE *cfile;
106static FILE *hfile;
107static char *syntax[513];
108static int base;
109static int size;	/* number of values which a char variable can have */
110static int nbits;	/* number of bits in a character */
111static int digit_contig;/* true if digits are contiguous */
112
113static void filltable __P((char *));
114static void init __P((void));
115static void add __P((char *, char *));
116static void print __P((char *));
117static void output_type_macros __P((void));
118static void digit_convert __P((void));
119
120int
121main(argc, argv)
122	int argc __unused;
123	char **argv __unused;
124{
125	char c;
126	char d;
127	int sign;
128	int i;
129	char buf[80];
130	int pos;
131	static char digit[] = "0123456789";
132
133	/* Create output files */
134	if ((cfile = fopen("syntax.c", "w")) == NULL) {
135		perror("syntax.c");
136		exit(2);
137	}
138	if ((hfile = fopen("syntax.h", "w")) == NULL) {
139		perror("syntax.h");
140		exit(2);
141	}
142	fputs(writer, hfile);
143	fputs(writer, cfile);
144
145	/* Determine the characteristics of chars. */
146	c = -1;
147	if (c < 0)
148		sign = 1;
149	else
150		sign = 0;
151	for (nbits = 1 ; ; nbits++) {
152		d = (1 << nbits) - 1;
153		if (d == c)
154			break;
155	}
156#if 0
157	printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits);
158#endif
159	if (nbits > 9) {
160		fputs("Characters can't have more than 9 bits\n", stderr);
161		exit(2);
162	}
163	size = (1 << nbits) + 1;
164	base = 1;
165	if (sign)
166		base += 1 << (nbits - 1);
167	digit_contig = 1;
168	for (i = 0 ; i < 10 ; i++) {
169		if (digit[i] != '0' + i)
170			digit_contig = 0;
171	}
172
173	fputs("#include <sys/cdefs.h>\n", hfile);
174	fputs("#include <ctype.h>\n", hfile);
175
176	/* Generate the #define statements in the header file */
177	fputs("/* Syntax classes */\n", hfile);
178	for (i = 0 ; synclass[i].name ; i++) {
179		sprintf(buf, "#define %s %d", synclass[i].name, i);
180		fputs(buf, hfile);
181		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
182			putc('\t', hfile);
183		fprintf(hfile, "/* %s */\n", synclass[i].comment);
184	}
185	putc('\n', hfile);
186	fputs("/* Syntax classes for is_ functions */\n", hfile);
187	for (i = 0 ; is_entry[i].name ; i++) {
188		sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
189		fputs(buf, hfile);
190		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
191			putc('\t', hfile);
192		fprintf(hfile, "/* %s */\n", is_entry[i].comment);
193	}
194	putc('\n', hfile);
195	fprintf(hfile, "#define SYNBASE %d\n", base);
196	fprintf(hfile, "#define PEOF %d\n\n", -base);
197	putc('\n', hfile);
198	fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
199	fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
200	fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
201	fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
202	putc('\n', hfile);
203	output_type_macros();		/* is_digit, etc. */
204	putc('\n', hfile);
205
206	/* Generate the syntax tables. */
207	fputs("#include \"shell.h\"\n", cfile);
208	fputs("#include \"syntax.h\"\n\n", cfile);
209	init();
210	fputs("/* syntax table used when not in quotes */\n", cfile);
211	add("\n", "CNL");
212	add("\\", "CBACK");
213	add("'", "CSQUOTE");
214	add("\"", "CDQUOTE");
215	add("`", "CBQUOTE");
216	add("$", "CVAR");
217	add("}", "CENDVAR");
218	add("<>();&| \t", "CSPCL");
219	print("basesyntax");
220	init();
221	fputs("\n/* syntax table used when in double quotes */\n", cfile);
222	add("\n", "CNL");
223	add("\\", "CBACK");
224	add("\"", "CENDQUOTE");
225	add("`", "CBQUOTE");
226	add("$", "CVAR");
227	add("}", "CENDVAR");
228	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
229	add("!*?[=~:/-", "CCTL");
230	print("dqsyntax");
231	init();
232	fputs("\n/* syntax table used when in single quotes */\n", cfile);
233	add("\n", "CNL");
234	add("'", "CENDQUOTE");
235	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
236	add("!*?[=~:/-", "CCTL");
237	print("sqsyntax");
238	init();
239	fputs("\n/* syntax table used when in arithmetic */\n", cfile);
240	add("\n", "CNL");
241	add("\\", "CBACK");
242	add("`", "CBQUOTE");
243	add("'", "CSQUOTE");
244	add("\"", "CDQUOTE");
245	add("$", "CVAR");
246	add("}", "CENDVAR");
247	add("(", "CLP");
248	add(")", "CRP");
249	print("arisyntax");
250	filltable("0");
251	fputs("\n/* character classification table */\n", cfile);
252	add("0123456789", "ISDIGIT");
253	add("abcdefghijklmnopqrstucvwxyz", "ISLOWER");
254	add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER");
255	add("_", "ISUNDER");
256	add("#?$!-*@", "ISSPECL");
257	print("is_type");
258	if (! digit_contig)
259		digit_convert();
260	exit(0);
261}
262
263
264
265/*
266 * Clear the syntax table.
267 */
268
269static void
270filltable(dftval)
271	char *dftval;
272{
273	int i;
274
275	for (i = 0 ; i < size ; i++)
276		syntax[i] = dftval;
277}
278
279
280/*
281 * Initialize the syntax table with default values.
282 */
283
284static void
285init()
286{
287	filltable("CWORD");
288	syntax[0] = "CEOF";
289	syntax[base + CTLESC] = "CCTL";
290	syntax[base + CTLVAR] = "CCTL";
291	syntax[base + CTLENDVAR] = "CCTL";
292	syntax[base + CTLBACKQ] = "CCTL";
293	syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
294	syntax[base + CTLARI] = "CCTL";
295	syntax[base + CTLENDARI] = "CCTL";
296}
297
298
299/*
300 * Add entries to the syntax table.
301 */
302
303static void
304add(p, type)
305	char *p, *type;
306{
307	while (*p)
308		syntax[*p++ + base] = type;
309}
310
311
312
313/*
314 * Output the syntax table.
315 */
316
317static void
318print(name)
319	char *name;
320{
321	int i;
322	int col;
323
324	fprintf(hfile, "extern const char %s[];\n", name);
325	fprintf(cfile, "const char %s[%d] = {\n", name, size);
326	col = 0;
327	for (i = 0 ; i < size ; i++) {
328		if (i == 0) {
329			fputs("      ", cfile);
330		} else if ((i & 03) == 0) {
331			fputs(",\n      ", cfile);
332			col = 0;
333		} else {
334			putc(',', cfile);
335			while (++col < 9 * (i & 03))
336				putc(' ', cfile);
337		}
338		fputs(syntax[i], cfile);
339		col += strlen(syntax[i]);
340	}
341	fputs("\n};\n", cfile);
342}
343
344
345
346/*
347 * Output character classification macros (e.g. is_digit).  If digits are
348 * contiguous, we can test for them quickly.
349 */
350
351static char *macro[] = {
352	"#define is_digit(c)\t((is_type+SYNBASE)[c] & ISDIGIT)",
353	"#define is_alpha(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && isalpha((unsigned char) (c)))",
354	"#define is_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalpha((unsigned char) (c))))",
355	"#define is_in_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalnum((unsigned char) (c))))",
356	"#define is_special(c)\t((is_type+SYNBASE)[c] & (ISSPECL|ISDIGIT))",
357	NULL
358};
359
360static void
361output_type_macros()
362{
363	char **pp;
364
365	if (digit_contig)
366		macro[0] = "#define is_digit(c)\t((unsigned)((c) - '0') <= 9)";
367	for (pp = macro ; *pp ; pp++)
368		fprintf(hfile, "%s\n", *pp);
369	if (digit_contig)
370		fputs("#define digit_val(c)\t((c) - '0')\n", hfile);
371	else
372		fputs("#define digit_val(c)\t(digit_value[c])\n", hfile);
373}
374
375
376
377/*
378 * Output digit conversion table (if digits are not contiguous).
379 */
380
381static void
382digit_convert()
383{
384	int maxdigit;
385	static char digit[] = "0123456789";
386	char *p;
387	int i;
388
389	maxdigit = 0;
390	for (p = digit ; *p ; p++)
391		if (*p > maxdigit)
392			maxdigit = *p;
393	fputs("extern const char digit_value[];\n", hfile);
394	fputs("\n\nconst char digit_value[] = {\n", cfile);
395	for (i = 0 ; i <= maxdigit ; i++) {
396		for (p = digit ; *p && *p != i ; p++);
397		if (*p == '\0')
398			p = digit;
399		fprintf(cfile, "      %d,\n", p - digit);
400	}
401	fputs("};\n", cfile);
402}
403