mksyntax.c revision 127958
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 127958 2004-04-06 20:06:54Z markm $");
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	char *name;
59	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 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(char *);
111static void init(void);
112static void add(char *, char *);
113static void print(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	if (c < 0)
143		sign = 1;
144	else
145		sign = 0;
146	for (nbits = 1 ; ; nbits++) {
147		d = (1 << nbits) - 1;
148		if (d == c)
149			break;
150	}
151#if 0
152	printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits);
153#endif
154	if (nbits > 9) {
155		fputs("Characters can't have more than 9 bits\n", stderr);
156		exit(2);
157	}
158	size = (1 << nbits) + 1;
159	base = 1;
160	if (sign)
161		base += 1 << (nbits - 1);
162	digit_contig = 1;
163	for (i = 0 ; i < 10 ; i++) {
164		if (digit[i] != '0' + i)
165			digit_contig = 0;
166	}
167
168	fputs("#include <sys/cdefs.h>\n", hfile);
169	fputs("#include <ctype.h>\n", hfile);
170
171	/* Generate the #define statements in the header file */
172	fputs("/* Syntax classes */\n", hfile);
173	for (i = 0 ; synclass[i].name ; i++) {
174		sprintf(buf, "#define %s %d", synclass[i].name, 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", synclass[i].comment);
179	}
180	putc('\n', hfile);
181	fputs("/* Syntax classes for is_ functions */\n", hfile);
182	for (i = 0 ; is_entry[i].name ; i++) {
183		sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
184		fputs(buf, hfile);
185		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
186			putc('\t', hfile);
187		fprintf(hfile, "/* %s */\n", is_entry[i].comment);
188	}
189	putc('\n', hfile);
190	fprintf(hfile, "#define SYNBASE %d\n", base);
191	fprintf(hfile, "#define PEOF %d\n\n", -base);
192	putc('\n', hfile);
193	fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
194	fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
195	fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
196	fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
197	putc('\n', hfile);
198	output_type_macros();		/* is_digit, etc. */
199	putc('\n', hfile);
200
201	/* Generate the syntax tables. */
202	fputs("#include \"shell.h\"\n", cfile);
203	fputs("#include \"syntax.h\"\n\n", cfile);
204	init();
205	fputs("/* syntax table used when not in quotes */\n", cfile);
206	add("\n", "CNL");
207	add("\\", "CBACK");
208	add("'", "CSQUOTE");
209	add("\"", "CDQUOTE");
210	add("`", "CBQUOTE");
211	add("$", "CVAR");
212	add("}", "CENDVAR");
213	add("<>();&| \t", "CSPCL");
214	print("basesyntax");
215	init();
216	fputs("\n/* syntax table used when in double quotes */\n", cfile);
217	add("\n", "CNL");
218	add("\\", "CBACK");
219	add("\"", "CENDQUOTE");
220	add("`", "CBQUOTE");
221	add("$", "CVAR");
222	add("}", "CENDVAR");
223	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
224	add("!*?[=~:/-", "CCTL");
225	print("dqsyntax");
226	init();
227	fputs("\n/* syntax table used when in single quotes */\n", cfile);
228	add("\n", "CNL");
229	add("'", "CENDQUOTE");
230	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
231	add("!*?[=~:/-", "CCTL");
232	print("sqsyntax");
233	init();
234	fputs("\n/* syntax table used when in arithmetic */\n", cfile);
235	add("\n", "CNL");
236	add("\\", "CBACK");
237	add("`", "CBQUOTE");
238	add("'", "CSQUOTE");
239	add("\"", "CDQUOTE");
240	add("$", "CVAR");
241	add("}", "CENDVAR");
242	add("(", "CLP");
243	add(")", "CRP");
244	print("arisyntax");
245	filltable("0");
246	fputs("\n/* character classification table */\n", cfile);
247	add("0123456789", "ISDIGIT");
248	add("abcdefghijklmnopqrstucvwxyz", "ISLOWER");
249	add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER");
250	add("_", "ISUNDER");
251	add("#?$!-*@", "ISSPECL");
252	print("is_type");
253	if (! digit_contig)
254		digit_convert();
255	exit(0);
256}
257
258
259
260/*
261 * Clear the syntax table.
262 */
263
264static void
265filltable(char *dftval)
266{
267	int i;
268
269	for (i = 0 ; i < size ; i++)
270		syntax[i] = dftval;
271}
272
273
274/*
275 * Initialize the syntax table with default values.
276 */
277
278static void
279init(void)
280{
281	filltable("CWORD");
282	syntax[0] = "CEOF";
283	syntax[base + CTLESC] = "CCTL";
284	syntax[base + CTLVAR] = "CCTL";
285	syntax[base + CTLENDVAR] = "CCTL";
286	syntax[base + CTLBACKQ] = "CCTL";
287	syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
288	syntax[base + CTLARI] = "CCTL";
289	syntax[base + CTLENDARI] = "CCTL";
290	syntax[base + CTLQUOTEMARK] = "CCTL";
291}
292
293
294/*
295 * Add entries to the syntax table.
296 */
297
298static void
299add(char *p, char *type)
300{
301	while (*p)
302		syntax[*p++ + base] = type;
303}
304
305
306
307/*
308 * Output the syntax table.
309 */
310
311static void
312print(char *name)
313{
314	int i;
315	int col;
316
317	fprintf(hfile, "extern const char %s[];\n", name);
318	fprintf(cfile, "const char %s[%d] = {\n", name, size);
319	col = 0;
320	for (i = 0 ; i < size ; i++) {
321		if (i == 0) {
322			fputs("      ", cfile);
323		} else if ((i & 03) == 0) {
324			fputs(",\n      ", cfile);
325			col = 0;
326		} else {
327			putc(',', cfile);
328			while (++col < 9 * (i & 03))
329				putc(' ', cfile);
330		}
331		fputs(syntax[i], cfile);
332		col += strlen(syntax[i]);
333	}
334	fputs("\n};\n", cfile);
335}
336
337
338
339/*
340 * Output character classification macros (e.g. is_digit).  If digits are
341 * contiguous, we can test for them quickly.
342 */
343
344static char *macro[] = {
345	"#define is_digit(c)\t((is_type+SYNBASE)[c] & ISDIGIT)",
346	"#define is_alpha(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && isalpha((unsigned char) (c)))",
347	"#define is_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalpha((unsigned char) (c))))",
348	"#define is_in_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalnum((unsigned char) (c))))",
349	"#define is_special(c)\t((is_type+SYNBASE)[c] & (ISSPECL|ISDIGIT))",
350	NULL
351};
352
353static void
354output_type_macros(void)
355{
356	char **pp;
357
358	if (digit_contig)
359		macro[0] = "#define is_digit(c)\t((unsigned)((c) - '0') <= 9)";
360	for (pp = macro ; *pp ; pp++)
361		fprintf(hfile, "%s\n", *pp);
362	if (digit_contig)
363		fputs("#define digit_val(c)\t((c) - '0')\n", hfile);
364	else
365		fputs("#define digit_val(c)\t(digit_value[c])\n", hfile);
366}
367
368
369
370/*
371 * Output digit conversion table (if digits are not contiguous).
372 */
373
374static void
375digit_convert(void)
376{
377	int maxdigit;
378	static char digit[] = "0123456789";
379	char *p;
380	int i;
381
382	maxdigit = 0;
383	for (p = digit ; *p ; p++)
384		if (*p > maxdigit)
385			maxdigit = *p;
386	fputs("extern const char digit_value[];\n", hfile);
387	fputs("\n\nconst char digit_value[] = {\n", cfile);
388	for (i = 0 ; i <= maxdigit ; i++) {
389		for (p = digit ; *p && *p != i ; p++);
390		if (*p == '\0')
391			p = digit;
392		fprintf(cfile, "      %d,\n", (int)(p - digit));
393	}
394	fputs("};\n", cfile);
395}
396