mksyntax.c revision 206473
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: head/bin/sh/mksyntax.c 206473 2010-04-11 12:24:47Z 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	{ "CSQUOTE",	"single quote" },
68	{ "CDQUOTE",	"double quote" },
69	{ "CENDQUOTE",	"a terminating quote" },
70	{ "CBQUOTE",	"backwards single quote" },
71	{ "CVAR",	"a dollar sign" },
72	{ "CENDVAR",	"a '}' character" },
73	{ "CLP",	"a left paren in arithmetic" },
74	{ "CRP",	"a right paren in arithmetic" },
75	{ "CEOF",	"end of file" },
76	{ "CCTL",	"like CWORD, except it must be escaped" },
77	{ "CSPCL",	"these terminate a word" },
78	{ NULL,		NULL }
79};
80
81
82/*
83 * Syntax classes for is_ functions.  Warning:  if you add new classes
84 * you may have to change the definition of the is_in_name macro.
85 */
86struct synclass is_entry[] = {
87	{ "ISDIGIT",	"a digit" },
88	{ "ISUPPER",	"an upper case letter" },
89	{ "ISLOWER",	"a lower case letter" },
90	{ "ISUNDER",	"an underscore" },
91	{ "ISSPECL",	"the name of a special parameter" },
92	{ NULL, 	NULL }
93};
94
95static char writer[] = "\
96/*\n\
97 * This file was generated by the mksyntax program.\n\
98 */\n\
99\n";
100
101
102static FILE *cfile;
103static FILE *hfile;
104static const char *syntax[513];
105static int base;
106static int size;	/* number of values which a char variable can have */
107static int nbits;	/* number of bits in a character */
108static int digit_contig;/* true if digits are contiguous */
109
110static void filltable(const char *);
111static void init(void);
112static void add(const char *, const char *);
113static void print(const char *);
114static void output_type_macros(void);
115static void digit_convert(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	static char digit[] = "0123456789";
127
128	/* Create output files */
129	if ((cfile = fopen("syntax.c", "w")) == NULL) {
130		perror("syntax.c");
131		exit(2);
132	}
133	if ((hfile = fopen("syntax.h", "w")) == NULL) {
134		perror("syntax.h");
135		exit(2);
136	}
137	fputs(writer, hfile);
138	fputs(writer, cfile);
139
140	/* Determine the characteristics of chars. */
141	c = -1;
142	sign = (c > 0) ? 0 : 1;
143	for (nbits = 1 ; ; nbits++) {
144		d = (1 << nbits) - 1;
145		if (d == c)
146			break;
147	}
148#if 0
149	printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits);
150#endif
151	if (nbits > 9) {
152		fputs("Characters can't have more than 9 bits\n", stderr);
153		exit(2);
154	}
155	size = (1 << nbits) + 1;
156	base = 1;
157	if (sign)
158		base += 1 << (nbits - 1);
159	digit_contig = 1;
160	for (i = 0 ; i < 10 ; i++) {
161		if (digit[i] != '0' + i)
162			digit_contig = 0;
163	}
164
165	fputs("#include <sys/cdefs.h>\n", hfile);
166	fputs("#include <ctype.h>\n", hfile);
167
168	/* Generate the #define statements in the header file */
169	fputs("/* Syntax classes */\n", hfile);
170	for (i = 0 ; synclass[i].name ; i++) {
171		sprintf(buf, "#define %s %d", synclass[i].name, i);
172		fputs(buf, hfile);
173		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
174			putc('\t', hfile);
175		fprintf(hfile, "/* %s */\n", synclass[i].comment);
176	}
177	putc('\n', hfile);
178	fputs("/* Syntax classes for is_ functions */\n", hfile);
179	for (i = 0 ; is_entry[i].name ; i++) {
180		sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
181		fputs(buf, hfile);
182		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
183			putc('\t', hfile);
184		fprintf(hfile, "/* %s */\n", is_entry[i].comment);
185	}
186	putc('\n', hfile);
187	fprintf(hfile, "#define SYNBASE %d\n", base);
188	fprintf(hfile, "#define PEOF %d\n\n", -base);
189	putc('\n', hfile);
190	fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
191	fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
192	fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
193	fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
194	putc('\n', hfile);
195	output_type_macros();		/* is_digit, etc. */
196	putc('\n', hfile);
197
198	/* Generate the syntax tables. */
199	fputs("#include \"shell.h\"\n", cfile);
200	fputs("#include \"syntax.h\"\n\n", cfile);
201	init();
202	fputs("/* syntax table used when not in quotes */\n", cfile);
203	add("\n", "CNL");
204	add("\\", "CBACK");
205	add("'", "CSQUOTE");
206	add("\"", "CDQUOTE");
207	add("`", "CBQUOTE");
208	add("$", "CVAR");
209	add("}", "CENDVAR");
210	add("<>();&| \t", "CSPCL");
211	print("basesyntax");
212	init();
213	fputs("\n/* syntax table used when in double quotes */\n", cfile);
214	add("\n", "CNL");
215	add("\\", "CBACK");
216	add("\"", "CENDQUOTE");
217	add("`", "CBQUOTE");
218	add("$", "CVAR");
219	add("}", "CENDVAR");
220	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
221	add("!*?[=~:/-", "CCTL");
222	print("dqsyntax");
223	init();
224	fputs("\n/* syntax table used when in single quotes */\n", cfile);
225	add("\n", "CNL");
226	add("'", "CENDQUOTE");
227	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
228	add("!*?[=~:/-", "CCTL");
229	print("sqsyntax");
230	init();
231	fputs("\n/* syntax table used when in arithmetic */\n", cfile);
232	add("\n", "CNL");
233	add("\\", "CBACK");
234	add("`", "CBQUOTE");
235	add("\"", "CDQUOTE");
236	add("$", "CVAR");
237	add("}", "CENDVAR");
238	add("(", "CLP");
239	add(")", "CRP");
240	print("arisyntax");
241	filltable("0");
242	fputs("\n/* character classification table */\n", cfile);
243	add("0123456789", "ISDIGIT");
244	add("abcdefghijklmnopqrstucvwxyz", "ISLOWER");
245	add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER");
246	add("_", "ISUNDER");
247	add("#?$!-*@", "ISSPECL");
248	print("is_type");
249	if (! digit_contig)
250		digit_convert();
251	exit(0);
252}
253
254
255
256/*
257 * Clear the syntax table.
258 */
259
260static void
261filltable(const char *dftval)
262{
263	int i;
264
265	for (i = 0 ; i < size ; i++)
266		syntax[i] = dftval;
267}
268
269
270/*
271 * Initialize the syntax table with default values.
272 */
273
274static void
275init(void)
276{
277	filltable("CWORD");
278	syntax[0] = "CEOF";
279	syntax[base + CTLESC] = "CCTL";
280	syntax[base + CTLVAR] = "CCTL";
281	syntax[base + CTLENDVAR] = "CCTL";
282	syntax[base + CTLBACKQ] = "CCTL";
283	syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
284	syntax[base + CTLARI] = "CCTL";
285	syntax[base + CTLENDARI] = "CCTL";
286	syntax[base + CTLQUOTEMARK] = "CCTL";
287}
288
289
290/*
291 * Add entries to the syntax table.
292 */
293
294static void
295add(const char *p, const char *type)
296{
297	while (*p)
298		syntax[*p++ + base] = type;
299}
300
301
302
303/*
304 * Output the syntax table.
305 */
306
307static void
308print(const char *name)
309{
310	int i;
311	int col;
312
313	fprintf(hfile, "extern const char %s[];\n", name);
314	fprintf(cfile, "const char %s[%d] = {\n", name, size);
315	col = 0;
316	for (i = 0 ; i < size ; i++) {
317		if (i == 0) {
318			fputs("      ", cfile);
319		} else if ((i & 03) == 0) {
320			fputs(",\n      ", cfile);
321			col = 0;
322		} else {
323			putc(',', cfile);
324			while (++col < 9 * (i & 03))
325				putc(' ', cfile);
326		}
327		fputs(syntax[i], cfile);
328		col += strlen(syntax[i]);
329	}
330	fputs("\n};\n", cfile);
331}
332
333
334
335/*
336 * Output character classification macros (e.g. is_digit).  If digits are
337 * contiguous, we can test for them quickly.
338 */
339
340static const char *macro[] = {
341	"#define is_digit(c)\t((is_type+SYNBASE)[c] & ISDIGIT)",
342	"#define is_eof(c)\t((c) == PEOF)",
343	"#define is_alpha(c)\t(((c) < CTLESC || (c) > CTLQUOTEMARK) && isalpha((unsigned char) (c)))",
344	"#define is_name(c)\t(((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalpha((unsigned char) (c))))",
345	"#define is_in_name(c)\t(((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalnum((unsigned char) (c))))",
346	"#define is_special(c)\t((is_type+SYNBASE)[c] & (ISSPECL|ISDIGIT))",
347	NULL
348};
349
350static void
351output_type_macros(void)
352{
353	const char **pp;
354
355	if (digit_contig)
356		macro[0] = "#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)";
357	for (pp = macro ; *pp ; pp++)
358		fprintf(hfile, "%s\n", *pp);
359	if (digit_contig)
360		fputs("#define digit_val(c)\t((c) - '0')\n", hfile);
361	else
362		fputs("#define digit_val(c)\t(digit_value[c])\n", hfile);
363}
364
365
366
367/*
368 * Output digit conversion table (if digits are not contiguous).
369 */
370
371static void
372digit_convert(void)
373{
374	int maxdigit;
375	static char digit[] = "0123456789";
376	char *p;
377	int i;
378
379	maxdigit = 0;
380	for (p = digit ; *p ; p++)
381		if (*p > maxdigit)
382			maxdigit = *p;
383	fputs("extern const char digit_value[];\n", hfile);
384	fputs("\n\nconst char digit_value[] = {\n", cfile);
385	for (i = 0 ; i <= maxdigit ; i++) {
386		for (p = digit ; *p && *p != i ; p++);
387		if (*p == '\0')
388			p = digit;
389		fprintf(cfile, "      %d,\n", (int)(p - digit));
390	}
391	fputs("};\n", cfile);
392}
393