xref: /freebsd/bin/sh/mksyntax.c (revision 40969e73962334a267beabf938dca4b62676e40c)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
3309a80d48SDavid E. O'Brien #if 0
344b88c807SRodney W. Grimes #ifndef lint
35ab0a2172SSteve Price static char const copyright[] =
364b88c807SRodney W. Grimes "@(#) Copyright (c) 1991, 1993\n\
374b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
384b88c807SRodney W. Grimes #endif /* not lint */
394b88c807SRodney W. Grimes 
404b88c807SRodney W. Grimes #ifndef lint
413d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)mksyntax.c	8.2 (Berkeley) 5/4/95";
424b88c807SRodney W. Grimes #endif /* not lint */
4309a80d48SDavid E. O'Brien #endif
442749b141SDavid E. O'Brien #include <sys/cdefs.h>
452749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
464b88c807SRodney W. Grimes 
474b88c807SRodney W. Grimes /*
484b88c807SRodney W. Grimes  * This program creates syntax.h and syntax.c.
494b88c807SRodney W. Grimes  */
504b88c807SRodney W. Grimes 
514b88c807SRodney W. Grimes #include <stdio.h>
5226f6b0fbSDag-Erling Smørgrav #include <stdlib.h>
53aa9caaf6SPeter Wemm #include <string.h>
544b88c807SRodney W. Grimes #include "parser.h"
554b88c807SRodney W. Grimes 
564b88c807SRodney W. Grimes 
574b88c807SRodney W. Grimes struct synclass {
58384aedabSJilles Tjoelker 	const char *name;
59384aedabSJilles Tjoelker 	const char *comment;
604b88c807SRodney W. Grimes };
614b88c807SRodney W. Grimes 
624b88c807SRodney W. Grimes /* Syntax classes */
634b88c807SRodney W. Grimes struct synclass synclass[] = {
64aa9caaf6SPeter Wemm 	{ "CWORD",	"character is nothing special" },
65aa9caaf6SPeter Wemm 	{ "CNL",	"newline character" },
66aa9caaf6SPeter Wemm 	{ "CBACK",	"a backslash character" },
67aa9caaf6SPeter Wemm 	{ "CSQUOTE",	"single quote" },
68aa9caaf6SPeter Wemm 	{ "CDQUOTE",	"double quote" },
69aa9caaf6SPeter Wemm 	{ "CENDQUOTE",	"a terminating quote" },
70aa9caaf6SPeter Wemm 	{ "CBQUOTE",	"backwards single quote" },
71aa9caaf6SPeter Wemm 	{ "CVAR",	"a dollar sign" },
72aa9caaf6SPeter Wemm 	{ "CENDVAR",	"a '}' character" },
73aa9caaf6SPeter Wemm 	{ "CLP",	"a left paren in arithmetic" },
74aa9caaf6SPeter Wemm 	{ "CRP",	"a right paren in arithmetic" },
75aa9caaf6SPeter Wemm 	{ "CEOF",	"end of file" },
76aa9caaf6SPeter Wemm 	{ "CCTL",	"like CWORD, except it must be escaped" },
77aa9caaf6SPeter Wemm 	{ "CSPCL",	"these terminate a word" },
78aa9caaf6SPeter Wemm 	{ NULL,		NULL }
794b88c807SRodney W. Grimes };
804b88c807SRodney W. Grimes 
814b88c807SRodney W. Grimes 
824b88c807SRodney W. Grimes /*
834b88c807SRodney W. Grimes  * Syntax classes for is_ functions.  Warning:  if you add new classes
844b88c807SRodney W. Grimes  * you may have to change the definition of the is_in_name macro.
854b88c807SRodney W. Grimes  */
864b88c807SRodney W. Grimes struct synclass is_entry[] = {
87aa9caaf6SPeter Wemm 	{ "ISDIGIT",	"a digit" },
88aa9caaf6SPeter Wemm 	{ "ISUPPER",	"an upper case letter" },
89aa9caaf6SPeter Wemm 	{ "ISLOWER",	"a lower case letter" },
90aa9caaf6SPeter Wemm 	{ "ISUNDER",	"an underscore" },
91aa9caaf6SPeter Wemm 	{ "ISSPECL",	"the name of a special parameter" },
92aa9caaf6SPeter Wemm 	{ NULL, 	NULL }
934b88c807SRodney W. Grimes };
944b88c807SRodney W. Grimes 
95aa9caaf6SPeter Wemm static char writer[] = "\
964b88c807SRodney W. Grimes /*\n\
974b88c807SRodney W. Grimes  * This file was generated by the mksyntax program.\n\
984b88c807SRodney W. Grimes  */\n\
994b88c807SRodney W. Grimes \n";
1004b88c807SRodney W. Grimes 
1014b88c807SRodney W. Grimes 
102aa9caaf6SPeter Wemm static FILE *cfile;
103aa9caaf6SPeter Wemm static FILE *hfile;
104384aedabSJilles Tjoelker static const char *syntax[513];
105aa9caaf6SPeter Wemm static int base;
106aa9caaf6SPeter Wemm static int size;	/* number of values which a char variable can have */
107aa9caaf6SPeter Wemm static int nbits;	/* number of bits in a character */
108aa9caaf6SPeter Wemm static int digit_contig;/* true if digits are contiguous */
1094b88c807SRodney W. Grimes 
110384aedabSJilles Tjoelker static void filltable(const char *);
1115134c3f7SWarner Losh static void init(void);
112384aedabSJilles Tjoelker static void add(const char *, const char *);
113384aedabSJilles Tjoelker static void print(const char *);
1145134c3f7SWarner Losh static void output_type_macros(void);
1155134c3f7SWarner Losh static void digit_convert(void);
1164b88c807SRodney W. Grimes 
117aa9caaf6SPeter Wemm int
1185134c3f7SWarner Losh main(int argc __unused, char **argv __unused)
119aa9caaf6SPeter Wemm {
1204b88c807SRodney W. Grimes 	char c;
1214b88c807SRodney W. Grimes 	char d;
1224b88c807SRodney W. Grimes 	int sign;
1234b88c807SRodney W. Grimes 	int i;
1244b88c807SRodney W. Grimes 	char buf[80];
1254b88c807SRodney W. Grimes 	int pos;
1264b88c807SRodney W. Grimes 	static char digit[] = "0123456789";
1274b88c807SRodney W. Grimes 
1284b88c807SRodney W. Grimes 	/* Create output files */
1294b88c807SRodney W. Grimes 	if ((cfile = fopen("syntax.c", "w")) == NULL) {
1304b88c807SRodney W. Grimes 		perror("syntax.c");
1314b88c807SRodney W. Grimes 		exit(2);
1324b88c807SRodney W. Grimes 	}
1334b88c807SRodney W. Grimes 	if ((hfile = fopen("syntax.h", "w")) == NULL) {
1344b88c807SRodney W. Grimes 		perror("syntax.h");
1354b88c807SRodney W. Grimes 		exit(2);
1364b88c807SRodney W. Grimes 	}
1374b88c807SRodney W. Grimes 	fputs(writer, hfile);
1384b88c807SRodney W. Grimes 	fputs(writer, cfile);
1394b88c807SRodney W. Grimes 
1404b88c807SRodney W. Grimes 	/* Determine the characteristics of chars. */
1414b88c807SRodney W. Grimes 	c = -1;
1422912059aSMarcel Moolenaar 	sign = (c > 0) ? 0 : 1;
1434b88c807SRodney W. Grimes 	for (nbits = 1 ; ; nbits++) {
1444b88c807SRodney W. Grimes 		d = (1 << nbits) - 1;
1454b88c807SRodney W. Grimes 		if (d == c)
1464b88c807SRodney W. Grimes 			break;
1474b88c807SRodney W. Grimes 	}
148ab0a2172SSteve Price #if 0
1494b88c807SRodney W. Grimes 	printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits);
150ab0a2172SSteve Price #endif
1514b88c807SRodney W. Grimes 	if (nbits > 9) {
1524b88c807SRodney W. Grimes 		fputs("Characters can't have more than 9 bits\n", stderr);
1534b88c807SRodney W. Grimes 		exit(2);
1544b88c807SRodney W. Grimes 	}
1554b88c807SRodney W. Grimes 	size = (1 << nbits) + 1;
1564b88c807SRodney W. Grimes 	base = 1;
1574b88c807SRodney W. Grimes 	if (sign)
1584b88c807SRodney W. Grimes 		base += 1 << (nbits - 1);
1594b88c807SRodney W. Grimes 	digit_contig = 1;
1604b88c807SRodney W. Grimes 	for (i = 0 ; i < 10 ; i++) {
1614b88c807SRodney W. Grimes 		if (digit[i] != '0' + i)
1624b88c807SRodney W. Grimes 			digit_contig = 0;
1634b88c807SRodney W. Grimes 	}
1644b88c807SRodney W. Grimes 
1654b88c807SRodney W. Grimes 	fputs("#include <sys/cdefs.h>\n", hfile);
166ab0a2172SSteve Price 	fputs("#include <ctype.h>\n", hfile);
1674b88c807SRodney W. Grimes 
1684b88c807SRodney W. Grimes 	/* Generate the #define statements in the header file */
1694b88c807SRodney W. Grimes 	fputs("/* Syntax classes */\n", hfile);
1704b88c807SRodney W. Grimes 	for (i = 0 ; synclass[i].name ; i++) {
1714b88c807SRodney W. Grimes 		sprintf(buf, "#define %s %d", synclass[i].name, i);
1724b88c807SRodney W. Grimes 		fputs(buf, hfile);
173aa9caaf6SPeter Wemm 		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
1744b88c807SRodney W. Grimes 			putc('\t', hfile);
1754b88c807SRodney W. Grimes 		fprintf(hfile, "/* %s */\n", synclass[i].comment);
1764b88c807SRodney W. Grimes 	}
1774b88c807SRodney W. Grimes 	putc('\n', hfile);
1784b88c807SRodney W. Grimes 	fputs("/* Syntax classes for is_ functions */\n", hfile);
1794b88c807SRodney W. Grimes 	for (i = 0 ; is_entry[i].name ; i++) {
1804b88c807SRodney W. Grimes 		sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
1814b88c807SRodney W. Grimes 		fputs(buf, hfile);
182aa9caaf6SPeter Wemm 		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
1834b88c807SRodney W. Grimes 			putc('\t', hfile);
1844b88c807SRodney W. Grimes 		fprintf(hfile, "/* %s */\n", is_entry[i].comment);
1854b88c807SRodney W. Grimes 	}
1864b88c807SRodney W. Grimes 	putc('\n', hfile);
1874b88c807SRodney W. Grimes 	fprintf(hfile, "#define SYNBASE %d\n", base);
1884b88c807SRodney W. Grimes 	fprintf(hfile, "#define PEOF %d\n\n", -base);
1894b88c807SRodney W. Grimes 	putc('\n', hfile);
1904b88c807SRodney W. Grimes 	fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
1914b88c807SRodney W. Grimes 	fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
1924b88c807SRodney W. Grimes 	fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
1934b88c807SRodney W. Grimes 	fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
1944b88c807SRodney W. Grimes 	putc('\n', hfile);
1954b88c807SRodney W. Grimes 	output_type_macros();		/* is_digit, etc. */
1964b88c807SRodney W. Grimes 	putc('\n', hfile);
1974b88c807SRodney W. Grimes 
1984b88c807SRodney W. Grimes 	/* Generate the syntax tables. */
1994b88c807SRodney W. Grimes 	fputs("#include \"shell.h\"\n", cfile);
2004b88c807SRodney W. Grimes 	fputs("#include \"syntax.h\"\n\n", cfile);
2014b88c807SRodney W. Grimes 	init();
2024b88c807SRodney W. Grimes 	fputs("/* syntax table used when not in quotes */\n", cfile);
2034b88c807SRodney W. Grimes 	add("\n", "CNL");
2044b88c807SRodney W. Grimes 	add("\\", "CBACK");
2054b88c807SRodney W. Grimes 	add("'", "CSQUOTE");
2064b88c807SRodney W. Grimes 	add("\"", "CDQUOTE");
2074b88c807SRodney W. Grimes 	add("`", "CBQUOTE");
2084b88c807SRodney W. Grimes 	add("$", "CVAR");
2094b88c807SRodney W. Grimes 	add("}", "CENDVAR");
2104b88c807SRodney W. Grimes 	add("<>();&| \t", "CSPCL");
2114b88c807SRodney W. Grimes 	print("basesyntax");
2124b88c807SRodney W. Grimes 	init();
2134b88c807SRodney W. Grimes 	fputs("\n/* syntax table used when in double quotes */\n", cfile);
2144b88c807SRodney W. Grimes 	add("\n", "CNL");
2154b88c807SRodney W. Grimes 	add("\\", "CBACK");
2164b88c807SRodney W. Grimes 	add("\"", "CENDQUOTE");
2174b88c807SRodney W. Grimes 	add("`", "CBQUOTE");
2184b88c807SRodney W. Grimes 	add("$", "CVAR");
2194b88c807SRodney W. Grimes 	add("}", "CENDVAR");
220aa9caaf6SPeter Wemm 	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
221aa9caaf6SPeter Wemm 	add("!*?[=~:/-", "CCTL");
2224b88c807SRodney W. Grimes 	print("dqsyntax");
2234b88c807SRodney W. Grimes 	init();
2244b88c807SRodney W. Grimes 	fputs("\n/* syntax table used when in single quotes */\n", cfile);
2254b88c807SRodney W. Grimes 	add("\n", "CNL");
2264b88c807SRodney W. Grimes 	add("'", "CENDQUOTE");
227aa9caaf6SPeter Wemm 	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
228aa9caaf6SPeter Wemm 	add("!*?[=~:/-", "CCTL");
2294b88c807SRodney W. Grimes 	print("sqsyntax");
2304b88c807SRodney W. Grimes 	init();
2314b88c807SRodney W. Grimes 	fputs("\n/* syntax table used when in arithmetic */\n", cfile);
2324b88c807SRodney W. Grimes 	add("\n", "CNL");
2334b88c807SRodney W. Grimes 	add("\\", "CBACK");
2344b88c807SRodney W. Grimes 	add("`", "CBQUOTE");
2357f728c60SJilles Tjoelker 	add("\"", "CDQUOTE");
2364b88c807SRodney W. Grimes 	add("$", "CVAR");
2374b88c807SRodney W. Grimes 	add("}", "CENDVAR");
2384b88c807SRodney W. Grimes 	add("(", "CLP");
2394b88c807SRodney W. Grimes 	add(")", "CRP");
2404b88c807SRodney W. Grimes 	print("arisyntax");
2414b88c807SRodney W. Grimes 	filltable("0");
2424b88c807SRodney W. Grimes 	fputs("\n/* character classification table */\n", cfile);
2434b88c807SRodney W. Grimes 	add("0123456789", "ISDIGIT");
244*40969e73SJilles Tjoelker 	add("abcdefghijklmnopqrstuvwxyz", "ISLOWER");
245*40969e73SJilles Tjoelker 	add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER");
2464b88c807SRodney W. Grimes 	add("_", "ISUNDER");
2474b88c807SRodney W. Grimes 	add("#?$!-*@", "ISSPECL");
2484b88c807SRodney W. Grimes 	print("is_type");
2494b88c807SRodney W. Grimes 	if (! digit_contig)
2504b88c807SRodney W. Grimes 		digit_convert();
2514b88c807SRodney W. Grimes 	exit(0);
2524b88c807SRodney W. Grimes }
2534b88c807SRodney W. Grimes 
2544b88c807SRodney W. Grimes 
2554b88c807SRodney W. Grimes 
2564b88c807SRodney W. Grimes /*
2574b88c807SRodney W. Grimes  * Clear the syntax table.
2584b88c807SRodney W. Grimes  */
2594b88c807SRodney W. Grimes 
260aa9caaf6SPeter Wemm static void
261384aedabSJilles Tjoelker filltable(const char *dftval)
2624b88c807SRodney W. Grimes {
2634b88c807SRodney W. Grimes 	int i;
2644b88c807SRodney W. Grimes 
2654b88c807SRodney W. Grimes 	for (i = 0 ; i < size ; i++)
2664b88c807SRodney W. Grimes 		syntax[i] = dftval;
2674b88c807SRodney W. Grimes }
2684b88c807SRodney W. Grimes 
2694b88c807SRodney W. Grimes 
2704b88c807SRodney W. Grimes /*
2714b88c807SRodney W. Grimes  * Initialize the syntax table with default values.
2724b88c807SRodney W. Grimes  */
2734b88c807SRodney W. Grimes 
274aa9caaf6SPeter Wemm static void
2755134c3f7SWarner Losh init(void)
276aa9caaf6SPeter Wemm {
2774b88c807SRodney W. Grimes 	filltable("CWORD");
2784b88c807SRodney W. Grimes 	syntax[0] = "CEOF";
2794b88c807SRodney W. Grimes 	syntax[base + CTLESC] = "CCTL";
2804b88c807SRodney W. Grimes 	syntax[base + CTLVAR] = "CCTL";
2814b88c807SRodney W. Grimes 	syntax[base + CTLENDVAR] = "CCTL";
2824b88c807SRodney W. Grimes 	syntax[base + CTLBACKQ] = "CCTL";
2834b88c807SRodney W. Grimes 	syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
2844b88c807SRodney W. Grimes 	syntax[base + CTLARI] = "CCTL";
2854b88c807SRodney W. Grimes 	syntax[base + CTLENDARI] = "CCTL";
2866f47734fSTor Egge 	syntax[base + CTLQUOTEMARK] = "CCTL";
2874b88c807SRodney W. Grimes }
2884b88c807SRodney W. Grimes 
2894b88c807SRodney W. Grimes 
2904b88c807SRodney W. Grimes /*
2914b88c807SRodney W. Grimes  * Add entries to the syntax table.
2924b88c807SRodney W. Grimes  */
2934b88c807SRodney W. Grimes 
294aa9caaf6SPeter Wemm static void
295384aedabSJilles Tjoelker add(const char *p, const char *type)
2964b88c807SRodney W. Grimes {
2974b88c807SRodney W. Grimes 	while (*p)
2984b88c807SRodney W. Grimes 		syntax[*p++ + base] = type;
2994b88c807SRodney W. Grimes }
3004b88c807SRodney W. Grimes 
3014b88c807SRodney W. Grimes 
3024b88c807SRodney W. Grimes 
3034b88c807SRodney W. Grimes /*
3044b88c807SRodney W. Grimes  * Output the syntax table.
3054b88c807SRodney W. Grimes  */
3064b88c807SRodney W. Grimes 
307aa9caaf6SPeter Wemm static void
308384aedabSJilles Tjoelker print(const char *name)
3094b88c807SRodney W. Grimes {
3104b88c807SRodney W. Grimes 	int i;
3114b88c807SRodney W. Grimes 	int col;
3124b88c807SRodney W. Grimes 
3134b88c807SRodney W. Grimes 	fprintf(hfile, "extern const char %s[];\n", name);
3144b88c807SRodney W. Grimes 	fprintf(cfile, "const char %s[%d] = {\n", name, size);
3154b88c807SRodney W. Grimes 	col = 0;
3164b88c807SRodney W. Grimes 	for (i = 0 ; i < size ; i++) {
3174b88c807SRodney W. Grimes 		if (i == 0) {
3184b88c807SRodney W. Grimes 			fputs("      ", cfile);
3194b88c807SRodney W. Grimes 		} else if ((i & 03) == 0) {
3204b88c807SRodney W. Grimes 			fputs(",\n      ", cfile);
3214b88c807SRodney W. Grimes 			col = 0;
3224b88c807SRodney W. Grimes 		} else {
3234b88c807SRodney W. Grimes 			putc(',', cfile);
3244b88c807SRodney W. Grimes 			while (++col < 9 * (i & 03))
3254b88c807SRodney W. Grimes 				putc(' ', cfile);
3264b88c807SRodney W. Grimes 		}
3274b88c807SRodney W. Grimes 		fputs(syntax[i], cfile);
3284b88c807SRodney W. Grimes 		col += strlen(syntax[i]);
3294b88c807SRodney W. Grimes 	}
3304b88c807SRodney W. Grimes 	fputs("\n};\n", cfile);
3314b88c807SRodney W. Grimes }
3324b88c807SRodney W. Grimes 
3334b88c807SRodney W. Grimes 
3344b88c807SRodney W. Grimes 
3354b88c807SRodney W. Grimes /*
3364b88c807SRodney W. Grimes  * Output character classification macros (e.g. is_digit).  If digits are
3374b88c807SRodney W. Grimes  * contiguous, we can test for them quickly.
3384b88c807SRodney W. Grimes  */
3394b88c807SRodney W. Grimes 
340384aedabSJilles Tjoelker static const char *macro[] = {
341c4c83940STor Egge 	"#define is_digit(c)\t((is_type+SYNBASE)[c] & ISDIGIT)",
342716b138bSStefan Farfeleder 	"#define is_eof(c)\t((c) == PEOF)",
343716b138bSStefan Farfeleder 	"#define is_alpha(c)\t(((c) < CTLESC || (c) > CTLQUOTEMARK) && isalpha((unsigned char) (c)))",
344716b138bSStefan Farfeleder 	"#define is_name(c)\t(((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalpha((unsigned char) (c))))",
345716b138bSStefan Farfeleder 	"#define is_in_name(c)\t(((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalnum((unsigned char) (c))))",
3464b88c807SRodney W. Grimes 	"#define is_special(c)\t((is_type+SYNBASE)[c] & (ISSPECL|ISDIGIT))",
3474b88c807SRodney W. Grimes 	NULL
3484b88c807SRodney W. Grimes };
3494b88c807SRodney W. Grimes 
350aa9caaf6SPeter Wemm static void
3515134c3f7SWarner Losh output_type_macros(void)
352aa9caaf6SPeter Wemm {
353384aedabSJilles Tjoelker 	const char **pp;
3544b88c807SRodney W. Grimes 
3554b88c807SRodney W. Grimes 	if (digit_contig)
356f001f896SRalf S. Engelschall 		macro[0] = "#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)";
3574b88c807SRodney W. Grimes 	for (pp = macro ; *pp ; pp++)
3584b88c807SRodney W. Grimes 		fprintf(hfile, "%s\n", *pp);
3594b88c807SRodney W. Grimes 	if (digit_contig)
3604b88c807SRodney W. Grimes 		fputs("#define digit_val(c)\t((c) - '0')\n", hfile);
3614b88c807SRodney W. Grimes 	else
3624b88c807SRodney W. Grimes 		fputs("#define digit_val(c)\t(digit_value[c])\n", hfile);
3634b88c807SRodney W. Grimes }
3644b88c807SRodney W. Grimes 
3654b88c807SRodney W. Grimes 
3664b88c807SRodney W. Grimes 
3674b88c807SRodney W. Grimes /*
3684b88c807SRodney W. Grimes  * Output digit conversion table (if digits are not contiguous).
3694b88c807SRodney W. Grimes  */
3704b88c807SRodney W. Grimes 
371aa9caaf6SPeter Wemm static void
3725134c3f7SWarner Losh digit_convert(void)
373aa9caaf6SPeter Wemm {
3744b88c807SRodney W. Grimes 	int maxdigit;
3754b88c807SRodney W. Grimes 	static char digit[] = "0123456789";
3764b88c807SRodney W. Grimes 	char *p;
3774b88c807SRodney W. Grimes 	int i;
3784b88c807SRodney W. Grimes 
3794b88c807SRodney W. Grimes 	maxdigit = 0;
3804b88c807SRodney W. Grimes 	for (p = digit ; *p ; p++)
3814b88c807SRodney W. Grimes 		if (*p > maxdigit)
3824b88c807SRodney W. Grimes 			maxdigit = *p;
3834b88c807SRodney W. Grimes 	fputs("extern const char digit_value[];\n", hfile);
3844b88c807SRodney W. Grimes 	fputs("\n\nconst char digit_value[] = {\n", cfile);
3854b88c807SRodney W. Grimes 	for (i = 0 ; i <= maxdigit ; i++) {
3864b88c807SRodney W. Grimes 		for (p = digit ; *p && *p != i ; p++);
3874b88c807SRodney W. Grimes 		if (*p == '\0')
3884b88c807SRodney W. Grimes 			p = digit;
38954beeb39STim J. Robbins 		fprintf(cfile, "      %d,\n", (int)(p - digit));
3904b88c807SRodney W. Grimes 	}
3914b88c807SRodney W. Grimes 	fputs("};\n", cfile);
3924b88c807SRodney W. Grimes }
393