mksyntax.c revision 214305
11553Srgrimes/*-
21553Srgrimes * Copyright (c) 1991, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * This code is derived from software contributed to Berkeley by
61553Srgrimes * Kenneth Almquist.
71553Srgrimes *
81553Srgrimes * Redistribution and use in source and binary forms, with or without
91553Srgrimes * modification, are permitted provided that the following conditions
101553Srgrimes * are met:
111553Srgrimes * 1. Redistributions of source code must retain the above copyright
121553Srgrimes *    notice, this list of conditions and the following disclaimer.
131553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141553Srgrimes *    notice, this list of conditions and the following disclaimer in the
151553Srgrimes *    documentation and/or other materials provided with the distribution.
161553Srgrimes * 4. Neither the name of the University nor the names of its contributors
171553Srgrimes *    may be used to endorse or promote products derived from this software
181553Srgrimes *    without specific prior written permission.
191553Srgrimes *
201553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301553Srgrimes * SUCH DAMAGE.
311553Srgrimes */
321553Srgrimes
331553Srgrimes#if 0
341553Srgrimes#ifndef lint
351553Srgrimesstatic char const copyright[] =
361553Srgrimes"@(#) Copyright (c) 1991, 1993\n\
371553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381553Srgrimes#endif /* not lint */
391553Srgrimes
401553Srgrimes#ifndef lint
411553Srgrimesstatic char sccsid[] = "@(#)mksyntax.c	8.2 (Berkeley) 5/4/95";
421553Srgrimes#endif /* not lint */
431553Srgrimes#endif
441553Srgrimes#include <sys/cdefs.h>
451553Srgrimes__FBSDID("$FreeBSD: head/bin/sh/mksyntax.c 214305 2010-10-24 22:25:38Z jilles $");
461553Srgrimes
471553Srgrimes/*
481553Srgrimes * This program creates syntax.h and syntax.c.
491553Srgrimes */
501553Srgrimes
511553Srgrimes#include <stdio.h>
521553Srgrimes#include <stdlib.h>
531553Srgrimes#include <string.h>
541553Srgrimes#include "parser.h"
551553Srgrimes
561553Srgrimes
571553Srgrimesstruct synclass {
581553Srgrimes	const char *name;
591553Srgrimes	const char *comment;
601553Srgrimes};
611553Srgrimes
621553Srgrimes/* Syntax classes */
631553Srgrimesstruct synclass synclass[] = {
641553Srgrimes	{ "CWORD",	"character is nothing special" },
651553Srgrimes	{ "CNL",	"newline character" },
661553Srgrimes	{ "CBACK",	"a backslash character" },
671553Srgrimes	{ "CSQUOTE",	"single quote" },
681553Srgrimes	{ "CDQUOTE",	"double quote" },
691553Srgrimes	{ "CENDQUOTE",	"a terminating quote" },
701553Srgrimes	{ "CBQUOTE",	"backwards single quote" },
711553Srgrimes	{ "CVAR",	"a dollar sign" },
721553Srgrimes	{ "CENDVAR",	"a '}' character" },
731553Srgrimes	{ "CLP",	"a left paren in arithmetic" },
741553Srgrimes	{ "CRP",	"a right paren in arithmetic" },
751553Srgrimes	{ "CEOF",	"end of file" },
761553Srgrimes	{ "CCTL",	"like CWORD, except it must be escaped" },
771553Srgrimes	{ "CSPCL",	"these terminate a word" },
781553Srgrimes	{ "CIGN",       "character should be ignored" },
791553Srgrimes	{ NULL,		NULL }
801553Srgrimes};
811553Srgrimes
821553Srgrimes
831553Srgrimes/*
841553Srgrimes * Syntax classes for is_ functions.  Warning:  if you add new classes
851553Srgrimes * you may have to change the definition of the is_in_name macro.
861553Srgrimes */
871553Srgrimesstruct synclass is_entry[] = {
881553Srgrimes	{ "ISDIGIT",	"a digit" },
891553Srgrimes	{ "ISUPPER",	"an upper case letter" },
901553Srgrimes	{ "ISLOWER",	"a lower case letter" },
911553Srgrimes	{ "ISUNDER",	"an underscore" },
921553Srgrimes	{ "ISSPECL",	"the name of a special parameter" },
931553Srgrimes	{ NULL, 	NULL }
941553Srgrimes};
951553Srgrimes
96static char writer[] = "\
97/*\n\
98 * This file was generated by the mksyntax program.\n\
99 */\n\
100\n";
101
102
103static FILE *cfile;
104static FILE *hfile;
105static const char *syntax[513];
106static int base;
107static int size;	/* number of values which a char variable can have */
108static int nbits;	/* number of bits in a character */
109static int digit_contig;/* true if digits are contiguous */
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);
116static void digit_convert(void);
117
118int
119main(int argc __unused, char **argv __unused)
120{
121	char c;
122	char d;
123	int sign;
124	int i;
125	char buf[80];
126	int pos;
127	static char digit[] = "0123456789";
128
129	/* Create output files */
130	if ((cfile = fopen("syntax.c", "w")) == NULL) {
131		perror("syntax.c");
132		exit(2);
133	}
134	if ((hfile = fopen("syntax.h", "w")) == NULL) {
135		perror("syntax.h");
136		exit(2);
137	}
138	fputs(writer, hfile);
139	fputs(writer, cfile);
140
141	/* Determine the characteristics of chars. */
142	c = -1;
143	sign = (c > 0) ? 0 : 1;
144	for (nbits = 1 ; ; nbits++) {
145		d = (1 << nbits) - 1;
146		if (d == c)
147			break;
148	}
149#if 0
150	printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits);
151#endif
152	if (nbits > 9) {
153		fputs("Characters can't have more than 9 bits\n", stderr);
154		exit(2);
155	}
156	size = (1 << nbits) + 1;
157	base = 1;
158	if (sign)
159		base += 1 << (nbits - 1);
160	digit_contig = 1;
161	for (i = 0 ; i < 10 ; i++) {
162		if (digit[i] != '0' + i)
163			digit_contig = 0;
164	}
165
166	fputs("#include <sys/cdefs.h>\n", hfile);
167	fputs("#include <ctype.h>\n", hfile);
168
169	/* Generate the #define statements in the header file */
170	fputs("/* Syntax classes */\n", hfile);
171	for (i = 0 ; synclass[i].name ; i++) {
172		sprintf(buf, "#define %s %d", synclass[i].name, i);
173		fputs(buf, hfile);
174		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
175			putc('\t', hfile);
176		fprintf(hfile, "/* %s */\n", synclass[i].comment);
177	}
178	putc('\n', hfile);
179	fputs("/* Syntax classes for is_ functions */\n", hfile);
180	for (i = 0 ; is_entry[i].name ; i++) {
181		sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
182		fputs(buf, hfile);
183		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
184			putc('\t', hfile);
185		fprintf(hfile, "/* %s */\n", is_entry[i].comment);
186	}
187	putc('\n', hfile);
188	fprintf(hfile, "#define SYNBASE %d\n", base);
189	fprintf(hfile, "#define PEOF %d\n\n", -base);
190	putc('\n', hfile);
191	fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
192	fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
193	fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
194	fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
195	putc('\n', hfile);
196	output_type_macros();		/* is_digit, etc. */
197	putc('\n', hfile);
198
199	/* Generate the syntax tables. */
200	fputs("#include \"shell.h\"\n", cfile);
201	fputs("#include \"syntax.h\"\n\n", cfile);
202	init();
203	fputs("/* syntax table used when not in quotes */\n", cfile);
204	add("\n", "CNL");
205	add("\\", "CBACK");
206	add("'", "CSQUOTE");
207	add("\"", "CDQUOTE");
208	add("`", "CBQUOTE");
209	add("$", "CVAR");
210	add("}", "CENDVAR");
211	add("<>();&| \t", "CSPCL");
212	print("basesyntax");
213	init();
214	fputs("\n/* syntax table used when in double quotes */\n", cfile);
215	add("\n", "CNL");
216	add("\\", "CBACK");
217	add("\"", "CENDQUOTE");
218	add("`", "CBQUOTE");
219	add("$", "CVAR");
220	add("}", "CENDVAR");
221	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
222	add("!*?[=~:/-", "CCTL");
223	print("dqsyntax");
224	init();
225	fputs("\n/* syntax table used when in single quotes */\n", cfile);
226	add("\n", "CNL");
227	add("'", "CENDQUOTE");
228	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
229	add("!*?[=~:/-", "CCTL");
230	print("sqsyntax");
231	init();
232	fputs("\n/* syntax table used when in arithmetic */\n", cfile);
233	add("\n", "CNL");
234	add("\\", "CBACK");
235	add("`", "CBQUOTE");
236	add("\"", "CIGN");
237	add("$", "CVAR");
238	add("}", "CENDVAR");
239	add("(", "CLP");
240	add(")", "CRP");
241	print("arisyntax");
242	filltable("0");
243	fputs("\n/* character classification table */\n", cfile);
244	add("0123456789", "ISDIGIT");
245	add("abcdefghijklmnopqrstuvwxyz", "ISLOWER");
246	add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER");
247	add("_", "ISUNDER");
248	add("#?$!-*@", "ISSPECL");
249	print("is_type");
250	if (! digit_contig)
251		digit_convert();
252	exit(0);
253}
254
255
256
257/*
258 * Clear the syntax table.
259 */
260
261static void
262filltable(const char *dftval)
263{
264	int i;
265
266	for (i = 0 ; i < size ; i++)
267		syntax[i] = dftval;
268}
269
270
271/*
272 * Initialize the syntax table with default values.
273 */
274
275static void
276init(void)
277{
278	filltable("CWORD");
279	syntax[0] = "CEOF";
280	syntax[base + CTLESC] = "CCTL";
281	syntax[base + CTLVAR] = "CCTL";
282	syntax[base + CTLENDVAR] = "CCTL";
283	syntax[base + CTLBACKQ] = "CCTL";
284	syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
285	syntax[base + CTLARI] = "CCTL";
286	syntax[base + CTLENDARI] = "CCTL";
287	syntax[base + CTLQUOTEMARK] = "CCTL";
288}
289
290
291/*
292 * Add entries to the syntax table.
293 */
294
295static void
296add(const char *p, const char *type)
297{
298	while (*p)
299		syntax[*p++ + base] = type;
300}
301
302
303
304/*
305 * Output the syntax table.
306 */
307
308static void
309print(const char *name)
310{
311	int i;
312	int col;
313
314	fprintf(hfile, "extern const char %s[];\n", name);
315	fprintf(cfile, "const char %s[%d] = {\n", name, size);
316	col = 0;
317	for (i = 0 ; i < size ; i++) {
318		if (i == 0) {
319			fputs("      ", cfile);
320		} else if ((i & 03) == 0) {
321			fputs(",\n      ", cfile);
322			col = 0;
323		} else {
324			putc(',', cfile);
325			while (++col < 9 * (i & 03))
326				putc(' ', cfile);
327		}
328		fputs(syntax[i], cfile);
329		col += strlen(syntax[i]);
330	}
331	fputs("\n};\n", cfile);
332}
333
334
335
336/*
337 * Output character classification macros (e.g. is_digit).  If digits are
338 * contiguous, we can test for them quickly.
339 */
340
341static const char *macro[] = {
342	"#define is_digit(c)\t((is_type+SYNBASE)[(int)c] & ISDIGIT)",
343	"#define is_eof(c)\t((c) == PEOF)",
344	"#define is_alpha(c)\t(((c) < CTLESC || (c) > CTLQUOTEMARK) && (is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER))",
345	"#define is_name(c)\t(((c) < CTLESC || (c) > CTLQUOTEMARK) && (is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER))",
346	"#define is_in_name(c)\t(((c) < CTLESC || (c) > CTLQUOTEMARK) && (is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))",
347	"#define is_special(c)\t((is_type+SYNBASE)[(int)c] & (ISSPECL|ISDIGIT))",
348	NULL
349};
350
351static void
352output_type_macros(void)
353{
354	const char **pp;
355
356	if (digit_contig)
357		macro[0] = "#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)";
358	for (pp = macro ; *pp ; pp++)
359		fprintf(hfile, "%s\n", *pp);
360	if (digit_contig)
361		fputs("#define digit_val(c)\t((c) - '0')\n", hfile);
362	else
363		fputs("#define digit_val(c)\t(digit_value[c])\n", hfile);
364}
365
366
367
368/*
369 * Output digit conversion table (if digits are not contiguous).
370 */
371
372static void
373digit_convert(void)
374{
375	int maxdigit;
376	static char digit[] = "0123456789";
377	char *p;
378	int i;
379
380	maxdigit = 0;
381	for (p = digit ; *p ; p++)
382		if (*p > maxdigit)
383			maxdigit = *p;
384	fputs("extern const char digit_value[];\n", hfile);
385	fputs("\n\nconst char digit_value[] = {\n", cfile);
386	for (i = 0 ; i <= maxdigit ; i++) {
387		for (p = digit ; *p && *p != i ; p++);
388		if (*p == '\0')
389			p = digit;
390		fprintf(cfile, "      %d,\n", (int)(p - digit));
391	}
392	fputs("};\n", cfile);
393}
394