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