mksyntax.c revision 221513
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
33114433Sobrien#if 0
341556Srgrimes#ifndef lint
3520425Sstevestatic char const copyright[] =
361556Srgrimes"@(#) Copyright (c) 1991, 1993\n\
371556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381556Srgrimes#endif /* not lint */
391556Srgrimes
401556Srgrimes#ifndef lint
4136150Scharnierstatic char sccsid[] = "@(#)mksyntax.c	8.2 (Berkeley) 5/4/95";
42114433Sobrien#endif /* not lint */
4336150Scharnier#endif
4499110Sobrien#include <sys/cdefs.h>
4599110Sobrien__FBSDID("$FreeBSD: head/bin/sh/mksyntax.c 221513 2011-05-05 20:55:55Z jilles $");
461556Srgrimes
471556Srgrimes/*
481556Srgrimes * This program creates syntax.h and syntax.c.
491556Srgrimes */
501556Srgrimes
511556Srgrimes#include <stdio.h>
5278469Sdes#include <stdlib.h>
5317987Speter#include <string.h>
541556Srgrimes#include "parser.h"
551556Srgrimes
561556Srgrimes
571556Srgrimesstruct synclass {
58201053Sjilles	const char *name;
59201053Sjilles	const char *comment;
601556Srgrimes};
611556Srgrimes
621556Srgrimes/* Syntax classes */
631556Srgrimesstruct synclass synclass[] = {
6417987Speter	{ "CWORD",	"character is nothing special" },
6517987Speter	{ "CNL",	"newline character" },
6617987Speter	{ "CBACK",	"a backslash character" },
67221513Sjilles	{ "CSBACK",	"a backslash character in single quotes" },
6817987Speter	{ "CSQUOTE",	"single quote" },
6917987Speter	{ "CDQUOTE",	"double quote" },
7017987Speter	{ "CENDQUOTE",	"a terminating quote" },
7117987Speter	{ "CBQUOTE",	"backwards single quote" },
7217987Speter	{ "CVAR",	"a dollar sign" },
7317987Speter	{ "CENDVAR",	"a '}' character" },
7417987Speter	{ "CLP",	"a left paren in arithmetic" },
7517987Speter	{ "CRP",	"a right paren in arithmetic" },
7617987Speter	{ "CEOF",	"end of file" },
7717987Speter	{ "CCTL",	"like CWORD, except it must be escaped" },
7817987Speter	{ "CSPCL",	"these terminate a word" },
79214305Sjilles	{ "CIGN",       "character should be ignored" },
8017987Speter	{ NULL,		NULL }
811556Srgrimes};
821556Srgrimes
831556Srgrimes
841556Srgrimes/*
851556Srgrimes * Syntax classes for is_ functions.  Warning:  if you add new classes
861556Srgrimes * you may have to change the definition of the is_in_name macro.
871556Srgrimes */
881556Srgrimesstruct synclass is_entry[] = {
8917987Speter	{ "ISDIGIT",	"a digit" },
9017987Speter	{ "ISUPPER",	"an upper case letter" },
9117987Speter	{ "ISLOWER",	"a lower case letter" },
9217987Speter	{ "ISUNDER",	"an underscore" },
9317987Speter	{ "ISSPECL",	"the name of a special parameter" },
9417987Speter	{ NULL, 	NULL }
951556Srgrimes};
961556Srgrimes
9717987Speterstatic char writer[] = "\
981556Srgrimes/*\n\
991556Srgrimes * This file was generated by the mksyntax program.\n\
1001556Srgrimes */\n\
1011556Srgrimes\n";
1021556Srgrimes
1031556Srgrimes
10417987Speterstatic FILE *cfile;
10517987Speterstatic FILE *hfile;
106201053Sjillesstatic const char *syntax[513];
10717987Speterstatic int base;
10817987Speterstatic int size;	/* number of values which a char variable can have */
10917987Speterstatic int nbits;	/* number of bits in a character */
11017987Speterstatic int digit_contig;/* true if digits are contiguous */
1111556Srgrimes
112201053Sjillesstatic void filltable(const char *);
11390111Simpstatic void init(void);
114201053Sjillesstatic void add(const char *, const char *);
115201053Sjillesstatic void print(const char *);
11690111Simpstatic void output_type_macros(void);
11790111Simpstatic void digit_convert(void);
1181556Srgrimes
11917987Speterint
12090111Simpmain(int argc __unused, char **argv __unused)
12117987Speter{
1221556Srgrimes	char c;
1231556Srgrimes	char d;
1241556Srgrimes	int sign;
1251556Srgrimes	int i;
1261556Srgrimes	char buf[80];
1271556Srgrimes	int pos;
1281556Srgrimes	static char digit[] = "0123456789";
1291556Srgrimes
1301556Srgrimes	/* Create output files */
1311556Srgrimes	if ((cfile = fopen("syntax.c", "w")) == NULL) {
1321556Srgrimes		perror("syntax.c");
1331556Srgrimes		exit(2);
1341556Srgrimes	}
1351556Srgrimes	if ((hfile = fopen("syntax.h", "w")) == NULL) {
1361556Srgrimes		perror("syntax.h");
1371556Srgrimes		exit(2);
1381556Srgrimes	}
1391556Srgrimes	fputs(writer, hfile);
1401556Srgrimes	fputs(writer, cfile);
1411556Srgrimes
1421556Srgrimes	/* Determine the characteristics of chars. */
1431556Srgrimes	c = -1;
144176392Smarcel	sign = (c > 0) ? 0 : 1;
1451556Srgrimes	for (nbits = 1 ; ; nbits++) {
1461556Srgrimes		d = (1 << nbits) - 1;
1471556Srgrimes		if (d == c)
1481556Srgrimes			break;
1491556Srgrimes	}
15020425Ssteve#if 0
1511556Srgrimes	printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits);
15220425Ssteve#endif
1531556Srgrimes	if (nbits > 9) {
1541556Srgrimes		fputs("Characters can't have more than 9 bits\n", stderr);
1551556Srgrimes		exit(2);
1561556Srgrimes	}
1571556Srgrimes	size = (1 << nbits) + 1;
1581556Srgrimes	base = 1;
1591556Srgrimes	if (sign)
1601556Srgrimes		base += 1 << (nbits - 1);
1611556Srgrimes	digit_contig = 1;
1621556Srgrimes	for (i = 0 ; i < 10 ; i++) {
1631556Srgrimes		if (digit[i] != '0' + i)
1641556Srgrimes			digit_contig = 0;
1651556Srgrimes	}
1661556Srgrimes
16720425Ssteve	fputs("#include <sys/cdefs.h>\n", hfile);
16817525Sache	fputs("#include <ctype.h>\n", hfile);
1691556Srgrimes
1701556Srgrimes	/* Generate the #define statements in the header file */
1711556Srgrimes	fputs("/* Syntax classes */\n", hfile);
1721556Srgrimes	for (i = 0 ; synclass[i].name ; i++) {
1731556Srgrimes		sprintf(buf, "#define %s %d", synclass[i].name, i);
1741556Srgrimes		fputs(buf, hfile);
17517987Speter		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
1761556Srgrimes			putc('\t', hfile);
1771556Srgrimes		fprintf(hfile, "/* %s */\n", synclass[i].comment);
1781556Srgrimes	}
1791556Srgrimes	putc('\n', hfile);
1801556Srgrimes	fputs("/* Syntax classes for is_ functions */\n", hfile);
1811556Srgrimes	for (i = 0 ; is_entry[i].name ; i++) {
1821556Srgrimes		sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
1831556Srgrimes		fputs(buf, hfile);
18417987Speter		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
1851556Srgrimes			putc('\t', hfile);
1861556Srgrimes		fprintf(hfile, "/* %s */\n", is_entry[i].comment);
1871556Srgrimes	}
1881556Srgrimes	putc('\n', hfile);
1891556Srgrimes	fprintf(hfile, "#define SYNBASE %d\n", base);
1901556Srgrimes	fprintf(hfile, "#define PEOF %d\n\n", -base);
1911556Srgrimes	putc('\n', hfile);
1921556Srgrimes	fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
1931556Srgrimes	fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
1941556Srgrimes	fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
1951556Srgrimes	fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
1961556Srgrimes	putc('\n', hfile);
1971556Srgrimes	output_type_macros();		/* is_digit, etc. */
1981556Srgrimes	putc('\n', hfile);
1991556Srgrimes
2001556Srgrimes	/* Generate the syntax tables. */
2011556Srgrimes	fputs("#include \"shell.h\"\n", cfile);
2021556Srgrimes	fputs("#include \"syntax.h\"\n\n", cfile);
2031556Srgrimes	init();
2041556Srgrimes	fputs("/* syntax table used when not in quotes */\n", cfile);
2051556Srgrimes	add("\n", "CNL");
2061556Srgrimes	add("\\", "CBACK");
2071556Srgrimes	add("'", "CSQUOTE");
2081556Srgrimes	add("\"", "CDQUOTE");
2091556Srgrimes	add("`", "CBQUOTE");
2101556Srgrimes	add("$", "CVAR");
2111556Srgrimes	add("}", "CENDVAR");
2121556Srgrimes	add("<>();&| \t", "CSPCL");
2131556Srgrimes	print("basesyntax");
2141556Srgrimes	init();
2151556Srgrimes	fputs("\n/* syntax table used when in double quotes */\n", cfile);
2161556Srgrimes	add("\n", "CNL");
2171556Srgrimes	add("\\", "CBACK");
2181556Srgrimes	add("\"", "CENDQUOTE");
2191556Srgrimes	add("`", "CBQUOTE");
2201556Srgrimes	add("$", "CVAR");
2211556Srgrimes	add("}", "CENDVAR");
22217987Speter	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
22317987Speter	add("!*?[=~:/-", "CCTL");
2241556Srgrimes	print("dqsyntax");
2251556Srgrimes	init();
2261556Srgrimes	fputs("\n/* syntax table used when in single quotes */\n", cfile);
2271556Srgrimes	add("\n", "CNL");
228221513Sjilles	add("\\", "CSBACK");
2291556Srgrimes	add("'", "CENDQUOTE");
23017987Speter	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
23117987Speter	add("!*?[=~:/-", "CCTL");
2321556Srgrimes	print("sqsyntax");
2331556Srgrimes	init();
2341556Srgrimes	fputs("\n/* syntax table used when in arithmetic */\n", cfile);
2351556Srgrimes	add("\n", "CNL");
2361556Srgrimes	add("\\", "CBACK");
2371556Srgrimes	add("`", "CBQUOTE");
238214305Sjilles	add("\"", "CIGN");
2391556Srgrimes	add("$", "CVAR");
2401556Srgrimes	add("}", "CENDVAR");
2411556Srgrimes	add("(", "CLP");
2421556Srgrimes	add(")", "CRP");
2431556Srgrimes	print("arisyntax");
2441556Srgrimes	filltable("0");
2451556Srgrimes	fputs("\n/* character classification table */\n", cfile);
2461556Srgrimes	add("0123456789", "ISDIGIT");
247211084Sjilles	add("abcdefghijklmnopqrstuvwxyz", "ISLOWER");
248211084Sjilles	add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER");
2491556Srgrimes	add("_", "ISUNDER");
2501556Srgrimes	add("#?$!-*@", "ISSPECL");
2511556Srgrimes	print("is_type");
2521556Srgrimes	if (! digit_contig)
2531556Srgrimes		digit_convert();
2541556Srgrimes	exit(0);
2551556Srgrimes}
2561556Srgrimes
2571556Srgrimes
2581556Srgrimes
2591556Srgrimes/*
2601556Srgrimes * Clear the syntax table.
2611556Srgrimes */
2621556Srgrimes
26317987Speterstatic void
264201053Sjillesfilltable(const char *dftval)
26517987Speter{
2661556Srgrimes	int i;
2671556Srgrimes
2681556Srgrimes	for (i = 0 ; i < size ; i++)
2691556Srgrimes		syntax[i] = dftval;
2701556Srgrimes}
2711556Srgrimes
2721556Srgrimes
2731556Srgrimes/*
2741556Srgrimes * Initialize the syntax table with default values.
2751556Srgrimes */
2761556Srgrimes
27717987Speterstatic void
27890111Simpinit(void)
27917987Speter{
2801556Srgrimes	filltable("CWORD");
2811556Srgrimes	syntax[0] = "CEOF";
2821556Srgrimes	syntax[base + CTLESC] = "CCTL";
2831556Srgrimes	syntax[base + CTLVAR] = "CCTL";
2841556Srgrimes	syntax[base + CTLENDVAR] = "CCTL";
2851556Srgrimes	syntax[base + CTLBACKQ] = "CCTL";
2861556Srgrimes	syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
2871556Srgrimes	syntax[base + CTLARI] = "CCTL";
2881556Srgrimes	syntax[base + CTLENDARI] = "CCTL";
28938887Stegge	syntax[base + CTLQUOTEMARK] = "CCTL";
290214512Sjilles	syntax[base + CTLQUOTEEND] = "CCTL";
2911556Srgrimes}
2921556Srgrimes
2931556Srgrimes
2941556Srgrimes/*
2951556Srgrimes * Add entries to the syntax table.
2961556Srgrimes */
2971556Srgrimes
29817987Speterstatic void
299201053Sjillesadd(const char *p, const char *type)
30017987Speter{
3011556Srgrimes	while (*p)
3021556Srgrimes		syntax[*p++ + base] = type;
3031556Srgrimes}
3041556Srgrimes
3051556Srgrimes
3061556Srgrimes
3071556Srgrimes/*
3081556Srgrimes * Output the syntax table.
3091556Srgrimes */
3101556Srgrimes
31117987Speterstatic void
312201053Sjillesprint(const char *name)
31317987Speter{
3141556Srgrimes	int i;
3151556Srgrimes	int col;
3161556Srgrimes
3171556Srgrimes	fprintf(hfile, "extern const char %s[];\n", name);
3181556Srgrimes	fprintf(cfile, "const char %s[%d] = {\n", name, size);
3191556Srgrimes	col = 0;
3201556Srgrimes	for (i = 0 ; i < size ; i++) {
3211556Srgrimes		if (i == 0) {
3221556Srgrimes			fputs("      ", cfile);
3231556Srgrimes		} else if ((i & 03) == 0) {
3241556Srgrimes			fputs(",\n      ", cfile);
3251556Srgrimes			col = 0;
3261556Srgrimes		} else {
3271556Srgrimes			putc(',', cfile);
3281556Srgrimes			while (++col < 9 * (i & 03))
3291556Srgrimes				putc(' ', cfile);
3301556Srgrimes		}
3311556Srgrimes		fputs(syntax[i], cfile);
3321556Srgrimes		col += strlen(syntax[i]);
3331556Srgrimes	}
3341556Srgrimes	fputs("\n};\n", cfile);
3351556Srgrimes}
3361556Srgrimes
3371556Srgrimes
3381556Srgrimes
3391556Srgrimes/*
3401556Srgrimes * Output character classification macros (e.g. is_digit).  If digits are
3411556Srgrimes * contiguous, we can test for them quickly.
3421556Srgrimes */
3431556Srgrimes
344201053Sjillesstatic const char *macro[] = {
345212190Sjilles	"#define is_digit(c)\t((is_type+SYNBASE)[(int)c] & ISDIGIT)",
346149026Sstefanf	"#define is_eof(c)\t((c) == PEOF)",
347215568Sjilles	"#define is_alpha(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER))",
348215568Sjilles	"#define is_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER))",
349215568Sjilles	"#define is_in_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))",
350212190Sjilles	"#define is_special(c)\t((is_type+SYNBASE)[(int)c] & (ISSPECL|ISDIGIT))",
3511556Srgrimes	NULL
3521556Srgrimes};
3531556Srgrimes
35417987Speterstatic void
35590111Simpoutput_type_macros(void)
35617987Speter{
357201053Sjilles	const char **pp;
3581556Srgrimes
3591556Srgrimes	if (digit_contig)
360193225Srse		macro[0] = "#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)";
3611556Srgrimes	for (pp = macro ; *pp ; pp++)
3621556Srgrimes		fprintf(hfile, "%s\n", *pp);
3631556Srgrimes	if (digit_contig)
3641556Srgrimes		fputs("#define digit_val(c)\t((c) - '0')\n", hfile);
3651556Srgrimes	else
3661556Srgrimes		fputs("#define digit_val(c)\t(digit_value[c])\n", hfile);
3671556Srgrimes}
3681556Srgrimes
3691556Srgrimes
3701556Srgrimes
3711556Srgrimes/*
3721556Srgrimes * Output digit conversion table (if digits are not contiguous).
3731556Srgrimes */
3741556Srgrimes
37517987Speterstatic void
37690111Simpdigit_convert(void)
37717987Speter{
3781556Srgrimes	int maxdigit;
3791556Srgrimes	static char digit[] = "0123456789";
3801556Srgrimes	char *p;
3811556Srgrimes	int i;
3821556Srgrimes
3831556Srgrimes	maxdigit = 0;
3841556Srgrimes	for (p = digit ; *p ; p++)
3851556Srgrimes		if (*p > maxdigit)
3861556Srgrimes			maxdigit = *p;
3871556Srgrimes	fputs("extern const char digit_value[];\n", hfile);
3881556Srgrimes	fputs("\n\nconst char digit_value[] = {\n", cfile);
3891556Srgrimes	for (i = 0 ; i <= maxdigit ; i++) {
3901556Srgrimes		for (p = digit ; *p && *p != i ; p++);
3911556Srgrimes		if (*p == '\0')
3921556Srgrimes			p = digit;
393104367Stjr		fprintf(cfile, "      %d,\n", (int)(p - digit));
3941556Srgrimes	}
3951556Srgrimes	fputs("};\n", cfile);
3961556Srgrimes}
397