xref: /freebsd/bin/sh/mksyntax.c (revision aa9caaf65748ace0f3cd08c2deaf3ef3048d6e4d)
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  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
3589730b29SDavid Greenman  *
36aa9caaf6SPeter Wemm  *	$Id: mksyntax.c,v 1.5 1996/08/12 22:14:47 ache Exp $
374b88c807SRodney W. Grimes  */
384b88c807SRodney W. Grimes 
394b88c807SRodney W. Grimes #ifndef lint
404b88c807SRodney W. Grimes static char copyright[] =
414b88c807SRodney W. Grimes "@(#) Copyright (c) 1991, 1993\n\
424b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
434b88c807SRodney W. Grimes #endif /* not lint */
444b88c807SRodney W. Grimes 
454b88c807SRodney W. Grimes #ifndef lint
46aa9caaf6SPeter Wemm static char sccsid[] = "@(#)mksyntax.c	8.2 (Berkeley) 5/4/95";
474b88c807SRodney W. Grimes #endif /* not lint */
484b88c807SRodney W. Grimes 
494b88c807SRodney W. Grimes /*
504b88c807SRodney W. Grimes  * This program creates syntax.h and syntax.c.
514b88c807SRodney W. Grimes  */
524b88c807SRodney W. Grimes 
534b88c807SRodney W. Grimes #include <stdio.h>
54aa9caaf6SPeter Wemm #include <string.h>
554b88c807SRodney W. Grimes #include "parser.h"
564b88c807SRodney W. Grimes 
574b88c807SRodney W. Grimes 
584b88c807SRodney W. Grimes struct synclass {
594b88c807SRodney W. Grimes 	char *name;
604b88c807SRodney W. Grimes 	char *comment;
614b88c807SRodney W. Grimes };
624b88c807SRodney W. Grimes 
634b88c807SRodney W. Grimes /* Syntax classes */
644b88c807SRodney W. Grimes struct synclass synclass[] = {
65aa9caaf6SPeter Wemm 	{ "CWORD",	"character is nothing special" },
66aa9caaf6SPeter Wemm 	{ "CNL",	"newline character" },
67aa9caaf6SPeter Wemm 	{ "CBACK",	"a backslash character" },
68aa9caaf6SPeter Wemm 	{ "CSQUOTE",	"single quote" },
69aa9caaf6SPeter Wemm 	{ "CDQUOTE",	"double quote" },
70aa9caaf6SPeter Wemm 	{ "CENDQUOTE",	"a terminating quote" },
71aa9caaf6SPeter Wemm 	{ "CBQUOTE",	"backwards single quote" },
72aa9caaf6SPeter Wemm 	{ "CVAR",	"a dollar sign" },
73aa9caaf6SPeter Wemm 	{ "CENDVAR",	"a '}' character" },
74aa9caaf6SPeter Wemm 	{ "CLP",	"a left paren in arithmetic" },
75aa9caaf6SPeter Wemm 	{ "CRP",	"a right paren in arithmetic" },
76aa9caaf6SPeter Wemm 	{ "CEOF",	"end of file" },
77aa9caaf6SPeter Wemm 	{ "CCTL",	"like CWORD, except it must be escaped" },
78aa9caaf6SPeter Wemm 	{ "CSPCL",	"these terminate a word" },
79aa9caaf6SPeter Wemm 	{ NULL,		NULL }
804b88c807SRodney W. Grimes };
814b88c807SRodney W. Grimes 
824b88c807SRodney W. Grimes 
834b88c807SRodney W. Grimes /*
844b88c807SRodney W. Grimes  * Syntax classes for is_ functions.  Warning:  if you add new classes
854b88c807SRodney W. Grimes  * you may have to change the definition of the is_in_name macro.
864b88c807SRodney W. Grimes  */
874b88c807SRodney W. Grimes struct synclass is_entry[] = {
88aa9caaf6SPeter Wemm 	{ "ISDIGIT",	"a digit" },
89aa9caaf6SPeter Wemm 	{ "ISUPPER",	"an upper case letter" },
90aa9caaf6SPeter Wemm 	{ "ISLOWER",	"a lower case letter" },
91aa9caaf6SPeter Wemm 	{ "ISUNDER",	"an underscore" },
92aa9caaf6SPeter Wemm 	{ "ISSPECL",	"the name of a special parameter" },
93aa9caaf6SPeter Wemm 	{ NULL, 	NULL }
944b88c807SRodney W. Grimes };
954b88c807SRodney W. Grimes 
96aa9caaf6SPeter Wemm static char writer[] = "\
974b88c807SRodney W. Grimes /*\n\
984b88c807SRodney W. Grimes  * This file was generated by the mksyntax program.\n\
994b88c807SRodney W. Grimes  */\n\
1004b88c807SRodney W. Grimes \n";
1014b88c807SRodney W. Grimes 
1024b88c807SRodney W. Grimes 
103aa9caaf6SPeter Wemm static FILE *cfile;
104aa9caaf6SPeter Wemm static FILE *hfile;
105aa9caaf6SPeter Wemm static char *syntax[513];
106aa9caaf6SPeter Wemm static int base;
107aa9caaf6SPeter Wemm static int size;	/* number of values which a char variable can have */
108aa9caaf6SPeter Wemm static int nbits;	/* number of bits in a character */
109aa9caaf6SPeter Wemm static int digit_contig;/* true if digits are contiguous */
1104b88c807SRodney W. Grimes 
111aa9caaf6SPeter Wemm static void filltable __P((char *));
112aa9caaf6SPeter Wemm static void init __P((void));
113aa9caaf6SPeter Wemm static void add __P((char *, char *));
114aa9caaf6SPeter Wemm static void print __P((char *));
115aa9caaf6SPeter Wemm static void output_type_macros __P((void));
116aa9caaf6SPeter Wemm static void digit_convert __P((void));
1174b88c807SRodney W. Grimes 
118aa9caaf6SPeter Wemm int
119aa9caaf6SPeter Wemm main(argc, argv)
120aa9caaf6SPeter Wemm 	int argc;
121aa9caaf6SPeter Wemm 	char **argv;
122aa9caaf6SPeter Wemm {
1234b88c807SRodney W. Grimes 	char c;
1244b88c807SRodney W. Grimes 	char d;
1254b88c807SRodney W. Grimes 	int sign;
1264b88c807SRodney W. Grimes 	int i;
1274b88c807SRodney W. Grimes 	char buf[80];
1284b88c807SRodney W. Grimes 	int pos;
1294b88c807SRodney W. Grimes 	static char digit[] = "0123456789";
1304b88c807SRodney W. Grimes 
1314b88c807SRodney W. Grimes 	/* Create output files */
1324b88c807SRodney W. Grimes 	if ((cfile = fopen("syntax.c", "w")) == NULL) {
1334b88c807SRodney W. Grimes 		perror("syntax.c");
1344b88c807SRodney W. Grimes 		exit(2);
1354b88c807SRodney W. Grimes 	}
1364b88c807SRodney W. Grimes 	if ((hfile = fopen("syntax.h", "w")) == NULL) {
1374b88c807SRodney W. Grimes 		perror("syntax.h");
1384b88c807SRodney W. Grimes 		exit(2);
1394b88c807SRodney W. Grimes 	}
1404b88c807SRodney W. Grimes 	fputs(writer, hfile);
1414b88c807SRodney W. Grimes 	fputs(writer, cfile);
1424b88c807SRodney W. Grimes 
1434b88c807SRodney W. Grimes 	/* Determine the characteristics of chars. */
1444b88c807SRodney W. Grimes 	c = -1;
1454b88c807SRodney W. Grimes 	if (c < 0)
1464b88c807SRodney W. Grimes 		sign = 1;
1474b88c807SRodney W. Grimes 	else
1484b88c807SRodney W. Grimes 		sign = 0;
1494b88c807SRodney W. Grimes 	for (nbits = 1 ; ; nbits++) {
1504b88c807SRodney W. Grimes 		d = (1 << nbits) - 1;
1514b88c807SRodney W. Grimes 		if (d == c)
1524b88c807SRodney W. Grimes 			break;
1534b88c807SRodney W. Grimes 	}
1544b88c807SRodney W. Grimes 	printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits);
1554b88c807SRodney W. Grimes 	if (nbits > 9) {
1564b88c807SRodney W. Grimes 		fputs("Characters can't have more than 9 bits\n", stderr);
1574b88c807SRodney W. Grimes 		exit(2);
1584b88c807SRodney W. Grimes 	}
1594b88c807SRodney W. Grimes 	size = (1 << nbits) + 1;
1604b88c807SRodney W. Grimes 	base = 1;
1614b88c807SRodney W. Grimes 	if (sign)
1624b88c807SRodney W. Grimes 		base += 1 << (nbits - 1);
1634b88c807SRodney W. Grimes 	digit_contig = 1;
1644b88c807SRodney W. Grimes 	for (i = 0 ; i < 10 ; i++) {
1654b88c807SRodney W. Grimes 		if (digit[i] != '0' + i)
1664b88c807SRodney W. Grimes 			digit_contig = 0;
1674b88c807SRodney W. Grimes 	}
1684b88c807SRodney W. Grimes 
169ba726b8aSAndrey A. Chernov 	fputs("#include <ctype.h>\n", hfile);
1704b88c807SRodney W. Grimes 	fputs("#include <sys/cdefs.h>\n", hfile);
1714b88c807SRodney W. Grimes 
1724b88c807SRodney W. Grimes 	/* Generate the #define statements in the header file */
1734b88c807SRodney W. Grimes 	fputs("/* Syntax classes */\n", hfile);
1744b88c807SRodney W. Grimes 	for (i = 0 ; synclass[i].name ; i++) {
1754b88c807SRodney W. Grimes 		sprintf(buf, "#define %s %d", synclass[i].name, i);
1764b88c807SRodney W. Grimes 		fputs(buf, hfile);
177aa9caaf6SPeter Wemm 		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
1784b88c807SRodney W. Grimes 			putc('\t', hfile);
1794b88c807SRodney W. Grimes 		fprintf(hfile, "/* %s */\n", synclass[i].comment);
1804b88c807SRodney W. Grimes 	}
1814b88c807SRodney W. Grimes 	putc('\n', hfile);
1824b88c807SRodney W. Grimes 	fputs("/* Syntax classes for is_ functions */\n", hfile);
1834b88c807SRodney W. Grimes 	for (i = 0 ; is_entry[i].name ; i++) {
1844b88c807SRodney W. Grimes 		sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
1854b88c807SRodney W. Grimes 		fputs(buf, hfile);
186aa9caaf6SPeter Wemm 		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
1874b88c807SRodney W. Grimes 			putc('\t', hfile);
1884b88c807SRodney W. Grimes 		fprintf(hfile, "/* %s */\n", is_entry[i].comment);
1894b88c807SRodney W. Grimes 	}
1904b88c807SRodney W. Grimes 	putc('\n', hfile);
1914b88c807SRodney W. Grimes 	fprintf(hfile, "#define SYNBASE %d\n", base);
1924b88c807SRodney W. Grimes 	fprintf(hfile, "#define PEOF %d\n\n", -base);
1934b88c807SRodney W. Grimes 	putc('\n', hfile);
1944b88c807SRodney W. Grimes 	fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
1954b88c807SRodney W. Grimes 	fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
1964b88c807SRodney W. Grimes 	fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
1974b88c807SRodney W. Grimes 	fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
1984b88c807SRodney W. Grimes 	putc('\n', hfile);
1994b88c807SRodney W. Grimes 	output_type_macros();		/* is_digit, etc. */
2004b88c807SRodney W. Grimes 	putc('\n', hfile);
2014b88c807SRodney W. Grimes 
2024b88c807SRodney W. Grimes 	/* Generate the syntax tables. */
2034b88c807SRodney W. Grimes 	fputs("#include \"shell.h\"\n", cfile);
2044b88c807SRodney W. Grimes 	fputs("#include \"syntax.h\"\n\n", cfile);
2054b88c807SRodney W. Grimes 	init();
2064b88c807SRodney W. Grimes 	fputs("/* syntax table used when not in quotes */\n", cfile);
2074b88c807SRodney W. Grimes 	add("\n", "CNL");
2084b88c807SRodney W. Grimes 	add("\\", "CBACK");
2094b88c807SRodney W. Grimes 	add("'", "CSQUOTE");
2104b88c807SRodney W. Grimes 	add("\"", "CDQUOTE");
2114b88c807SRodney W. Grimes 	add("`", "CBQUOTE");
2124b88c807SRodney W. Grimes 	add("$", "CVAR");
2134b88c807SRodney W. Grimes 	add("}", "CENDVAR");
2144b88c807SRodney W. Grimes 	add("<>();&| \t", "CSPCL");
2154b88c807SRodney W. Grimes 	print("basesyntax");
2164b88c807SRodney W. Grimes 	init();
2174b88c807SRodney W. Grimes 	fputs("\n/* syntax table used when in double quotes */\n", cfile);
2184b88c807SRodney W. Grimes 	add("\n", "CNL");
2194b88c807SRodney W. Grimes 	add("\\", "CBACK");
2204b88c807SRodney W. Grimes 	add("\"", "CENDQUOTE");
2214b88c807SRodney W. Grimes 	add("`", "CBQUOTE");
2224b88c807SRodney W. Grimes 	add("$", "CVAR");
2234b88c807SRodney W. Grimes 	add("}", "CENDVAR");
224aa9caaf6SPeter Wemm 	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
225aa9caaf6SPeter Wemm 	add("!*?[=~:/-", "CCTL");
2264b88c807SRodney W. Grimes 	print("dqsyntax");
2274b88c807SRodney W. Grimes 	init();
2284b88c807SRodney W. Grimes 	fputs("\n/* syntax table used when in single quotes */\n", cfile);
2294b88c807SRodney W. Grimes 	add("\n", "CNL");
2304b88c807SRodney W. Grimes 	add("'", "CENDQUOTE");
231aa9caaf6SPeter Wemm 	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
232aa9caaf6SPeter Wemm 	add("!*?[=~:/-", "CCTL");
2334b88c807SRodney W. Grimes 	print("sqsyntax");
2344b88c807SRodney W. Grimes 	init();
2354b88c807SRodney W. Grimes 	fputs("\n/* syntax table used when in arithmetic */\n", cfile);
2364b88c807SRodney W. Grimes 	add("\n", "CNL");
2374b88c807SRodney W. Grimes 	add("\\", "CBACK");
2384b88c807SRodney W. Grimes 	add("`", "CBQUOTE");
2394b88c807SRodney W. Grimes 	add("'", "CSQUOTE");
2404b88c807SRodney W. Grimes 	add("\"", "CDQUOTE");
2414b88c807SRodney W. Grimes 	add("$", "CVAR");
2424b88c807SRodney W. Grimes 	add("}", "CENDVAR");
2434b88c807SRodney W. Grimes 	add("(", "CLP");
2444b88c807SRodney W. Grimes 	add(")", "CRP");
2454b88c807SRodney W. Grimes 	print("arisyntax");
2464b88c807SRodney W. Grimes 	filltable("0");
2474b88c807SRodney W. Grimes 	fputs("\n/* character classification table */\n", cfile);
2484b88c807SRodney W. Grimes 	add("0123456789", "ISDIGIT");
2494b88c807SRodney W. Grimes 	add("abcdefghijklmnopqrstucvwxyz", "ISLOWER");
2504b88c807SRodney W. Grimes 	add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER");
2514b88c807SRodney W. Grimes 	add("_", "ISUNDER");
2524b88c807SRodney W. Grimes 	add("#?$!-*@", "ISSPECL");
2534b88c807SRodney W. Grimes 	print("is_type");
2544b88c807SRodney W. Grimes 	if (! digit_contig)
2554b88c807SRodney W. Grimes 		digit_convert();
2564b88c807SRodney W. Grimes 	exit(0);
2574b88c807SRodney W. Grimes }
2584b88c807SRodney W. Grimes 
2594b88c807SRodney W. Grimes 
2604b88c807SRodney W. Grimes 
2614b88c807SRodney W. Grimes /*
2624b88c807SRodney W. Grimes  * Clear the syntax table.
2634b88c807SRodney W. Grimes  */
2644b88c807SRodney W. Grimes 
265aa9caaf6SPeter Wemm static void
2664b88c807SRodney W. Grimes filltable(dftval)
2674b88c807SRodney W. Grimes 	char *dftval;
2684b88c807SRodney W. Grimes {
2694b88c807SRodney W. Grimes 	int i;
2704b88c807SRodney W. Grimes 
2714b88c807SRodney W. Grimes 	for (i = 0 ; i < size ; i++)
2724b88c807SRodney W. Grimes 		syntax[i] = dftval;
2734b88c807SRodney W. Grimes }
2744b88c807SRodney W. Grimes 
2754b88c807SRodney W. Grimes 
2764b88c807SRodney W. Grimes /*
2774b88c807SRodney W. Grimes  * Initialize the syntax table with default values.
2784b88c807SRodney W. Grimes  */
2794b88c807SRodney W. Grimes 
280aa9caaf6SPeter Wemm static void
281aa9caaf6SPeter Wemm init()
282aa9caaf6SPeter Wemm {
2834b88c807SRodney W. Grimes 	filltable("CWORD");
2844b88c807SRodney W. Grimes 	syntax[0] = "CEOF";
2854b88c807SRodney W. Grimes 	syntax[base + CTLESC] = "CCTL";
2864b88c807SRodney W. Grimes 	syntax[base + CTLVAR] = "CCTL";
2874b88c807SRodney W. Grimes 	syntax[base + CTLENDVAR] = "CCTL";
2884b88c807SRodney W. Grimes 	syntax[base + CTLBACKQ] = "CCTL";
2894b88c807SRodney W. Grimes 	syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
2904b88c807SRodney W. Grimes 	syntax[base + CTLARI] = "CCTL";
2914b88c807SRodney W. Grimes 	syntax[base + CTLENDARI] = "CCTL";
2924b88c807SRodney W. Grimes }
2934b88c807SRodney W. Grimes 
2944b88c807SRodney W. Grimes 
2954b88c807SRodney W. Grimes /*
2964b88c807SRodney W. Grimes  * Add entries to the syntax table.
2974b88c807SRodney W. Grimes  */
2984b88c807SRodney W. Grimes 
299aa9caaf6SPeter Wemm static void
3004b88c807SRodney W. Grimes add(p, type)
3014b88c807SRodney W. Grimes 	char *p, *type;
3024b88c807SRodney W. Grimes {
3034b88c807SRodney W. Grimes 	while (*p)
3044b88c807SRodney W. Grimes 		syntax[*p++ + base] = type;
3054b88c807SRodney W. Grimes }
3064b88c807SRodney W. Grimes 
3074b88c807SRodney W. Grimes 
3084b88c807SRodney W. Grimes 
3094b88c807SRodney W. Grimes /*
3104b88c807SRodney W. Grimes  * Output the syntax table.
3114b88c807SRodney W. Grimes  */
3124b88c807SRodney W. Grimes 
313aa9caaf6SPeter Wemm static void
3144b88c807SRodney W. Grimes print(name)
3154b88c807SRodney W. Grimes 	char *name;
3164b88c807SRodney W. Grimes {
3174b88c807SRodney W. Grimes 	int i;
3184b88c807SRodney W. Grimes 	int col;
3194b88c807SRodney W. Grimes 
3204b88c807SRodney W. Grimes 	fprintf(hfile, "extern const char %s[];\n", name);
3214b88c807SRodney W. Grimes 	fprintf(cfile, "const char %s[%d] = {\n", name, size);
3224b88c807SRodney W. Grimes 	col = 0;
3234b88c807SRodney W. Grimes 	for (i = 0 ; i < size ; i++) {
3244b88c807SRodney W. Grimes 		if (i == 0) {
3254b88c807SRodney W. Grimes 			fputs("      ", cfile);
3264b88c807SRodney W. Grimes 		} else if ((i & 03) == 0) {
3274b88c807SRodney W. Grimes 			fputs(",\n      ", cfile);
3284b88c807SRodney W. Grimes 			col = 0;
3294b88c807SRodney W. Grimes 		} else {
3304b88c807SRodney W. Grimes 			putc(',', cfile);
3314b88c807SRodney W. Grimes 			while (++col < 9 * (i & 03))
3324b88c807SRodney W. Grimes 				putc(' ', cfile);
3334b88c807SRodney W. Grimes 		}
3344b88c807SRodney W. Grimes 		fputs(syntax[i], cfile);
3354b88c807SRodney W. Grimes 		col += strlen(syntax[i]);
3364b88c807SRodney W. Grimes 	}
3374b88c807SRodney W. Grimes 	fputs("\n};\n", cfile);
3384b88c807SRodney W. Grimes }
3394b88c807SRodney W. Grimes 
3404b88c807SRodney W. Grimes 
3414b88c807SRodney W. Grimes 
3424b88c807SRodney W. Grimes /*
3434b88c807SRodney W. Grimes  * Output character classification macros (e.g. is_digit).  If digits are
3444b88c807SRodney W. Grimes  * contiguous, we can test for them quickly.
3454b88c807SRodney W. Grimes  */
3464b88c807SRodney W. Grimes 
347aa9caaf6SPeter Wemm static char *macro[] = {
3484b88c807SRodney W. Grimes 	"#define is_digit(c)\t((is_type+SYNBASE)[c] & ISDIGIT)",
349c07cbf9cSAndrey A. Chernov 	"#define is_alpha(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && isalpha((unsigned char) (c)))",
350c07cbf9cSAndrey A. Chernov 	"#define is_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalpha((unsigned char) (c))))",
351c07cbf9cSAndrey A. Chernov 	"#define is_in_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalnum((unsigned char) (c))))",
3524b88c807SRodney W. Grimes 	"#define is_special(c)\t((is_type+SYNBASE)[c] & (ISSPECL|ISDIGIT))",
3534b88c807SRodney W. Grimes 	NULL
3544b88c807SRodney W. Grimes };
3554b88c807SRodney W. Grimes 
356aa9caaf6SPeter Wemm static void
357aa9caaf6SPeter Wemm output_type_macros()
358aa9caaf6SPeter Wemm {
3594b88c807SRodney W. Grimes 	char **pp;
3604b88c807SRodney W. Grimes 
3614b88c807SRodney W. Grimes 	if (digit_contig)
3624b88c807SRodney W. Grimes 		macro[0] = "#define is_digit(c)\t((unsigned)((c) - '0') <= 9)";
3634b88c807SRodney W. Grimes 	for (pp = macro ; *pp ; pp++)
3644b88c807SRodney W. Grimes 		fprintf(hfile, "%s\n", *pp);
3654b88c807SRodney W. Grimes 	if (digit_contig)
3664b88c807SRodney W. Grimes 		fputs("#define digit_val(c)\t((c) - '0')\n", hfile);
3674b88c807SRodney W. Grimes 	else
3684b88c807SRodney W. Grimes 		fputs("#define digit_val(c)\t(digit_value[c])\n", hfile);
3694b88c807SRodney W. Grimes }
3704b88c807SRodney W. Grimes 
3714b88c807SRodney W. Grimes 
3724b88c807SRodney W. Grimes 
3734b88c807SRodney W. Grimes /*
3744b88c807SRodney W. Grimes  * Output digit conversion table (if digits are not contiguous).
3754b88c807SRodney W. Grimes  */
3764b88c807SRodney W. Grimes 
377aa9caaf6SPeter Wemm static void
378aa9caaf6SPeter Wemm digit_convert()
379aa9caaf6SPeter Wemm {
3804b88c807SRodney W. Grimes 	int maxdigit;
3814b88c807SRodney W. Grimes 	static char digit[] = "0123456789";
3824b88c807SRodney W. Grimes 	char *p;
3834b88c807SRodney W. Grimes 	int i;
3844b88c807SRodney W. Grimes 
3854b88c807SRodney W. Grimes 	maxdigit = 0;
3864b88c807SRodney W. Grimes 	for (p = digit ; *p ; p++)
3874b88c807SRodney W. Grimes 		if (*p > maxdigit)
3884b88c807SRodney W. Grimes 			maxdigit = *p;
3894b88c807SRodney W. Grimes 	fputs("extern const char digit_value[];\n", hfile);
3904b88c807SRodney W. Grimes 	fputs("\n\nconst char digit_value[] = {\n", cfile);
3914b88c807SRodney W. Grimes 	for (i = 0 ; i <= maxdigit ; i++) {
3924b88c807SRodney W. Grimes 		for (p = digit ; *p && *p != i ; p++);
3934b88c807SRodney W. Grimes 		if (*p == '\0')
3944b88c807SRodney W. Grimes 			p = digit;
3954b88c807SRodney W. Grimes 		fprintf(cfile, "      %d,\n", p - digit);
3964b88c807SRodney W. Grimes 	}
3974b88c807SRodney W. Grimes 	fputs("};\n", cfile);
3984b88c807SRodney W. Grimes }
399