xref: /freebsd/bin/sh/mksyntax.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	$Id: mksyntax.c,v 1.5 1996/08/12 22:14:47 ache Exp $
37  */
38 
39 #ifndef lint
40 static char copyright[] =
41 "@(#) Copyright (c) 1991, 1993\n\
42 	The Regents of the University of California.  All rights reserved.\n";
43 #endif /* not lint */
44 
45 #ifndef lint
46 static char sccsid[] = "@(#)mksyntax.c	8.2 (Berkeley) 5/4/95";
47 #endif /* not lint */
48 
49 /*
50  * This program creates syntax.h and syntax.c.
51  */
52 
53 #include <stdio.h>
54 #include <string.h>
55 #include "parser.h"
56 
57 
58 struct synclass {
59 	char *name;
60 	char *comment;
61 };
62 
63 /* Syntax classes */
64 struct synclass synclass[] = {
65 	{ "CWORD",	"character is nothing special" },
66 	{ "CNL",	"newline character" },
67 	{ "CBACK",	"a backslash character" },
68 	{ "CSQUOTE",	"single quote" },
69 	{ "CDQUOTE",	"double quote" },
70 	{ "CENDQUOTE",	"a terminating quote" },
71 	{ "CBQUOTE",	"backwards single quote" },
72 	{ "CVAR",	"a dollar sign" },
73 	{ "CENDVAR",	"a '}' character" },
74 	{ "CLP",	"a left paren in arithmetic" },
75 	{ "CRP",	"a right paren in arithmetic" },
76 	{ "CEOF",	"end of file" },
77 	{ "CCTL",	"like CWORD, except it must be escaped" },
78 	{ "CSPCL",	"these terminate a word" },
79 	{ NULL,		NULL }
80 };
81 
82 
83 /*
84  * Syntax classes for is_ functions.  Warning:  if you add new classes
85  * you may have to change the definition of the is_in_name macro.
86  */
87 struct synclass is_entry[] = {
88 	{ "ISDIGIT",	"a digit" },
89 	{ "ISUPPER",	"an upper case letter" },
90 	{ "ISLOWER",	"a lower case letter" },
91 	{ "ISUNDER",	"an underscore" },
92 	{ "ISSPECL",	"the name of a special parameter" },
93 	{ NULL, 	NULL }
94 };
95 
96 static char writer[] = "\
97 /*\n\
98  * This file was generated by the mksyntax program.\n\
99  */\n\
100 \n";
101 
102 
103 static FILE *cfile;
104 static FILE *hfile;
105 static char *syntax[513];
106 static int base;
107 static int size;	/* number of values which a char variable can have */
108 static int nbits;	/* number of bits in a character */
109 static int digit_contig;/* true if digits are contiguous */
110 
111 static void filltable __P((char *));
112 static void init __P((void));
113 static void add __P((char *, char *));
114 static void print __P((char *));
115 static void output_type_macros __P((void));
116 static void digit_convert __P((void));
117 
118 int
119 main(argc, argv)
120 	int argc;
121 	char **argv;
122 {
123 	char c;
124 	char d;
125 	int sign;
126 	int i;
127 	char buf[80];
128 	int pos;
129 	static char digit[] = "0123456789";
130 
131 	/* Create output files */
132 	if ((cfile = fopen("syntax.c", "w")) == NULL) {
133 		perror("syntax.c");
134 		exit(2);
135 	}
136 	if ((hfile = fopen("syntax.h", "w")) == NULL) {
137 		perror("syntax.h");
138 		exit(2);
139 	}
140 	fputs(writer, hfile);
141 	fputs(writer, cfile);
142 
143 	/* Determine the characteristics of chars. */
144 	c = -1;
145 	if (c < 0)
146 		sign = 1;
147 	else
148 		sign = 0;
149 	for (nbits = 1 ; ; nbits++) {
150 		d = (1 << nbits) - 1;
151 		if (d == c)
152 			break;
153 	}
154 	printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits);
155 	if (nbits > 9) {
156 		fputs("Characters can't have more than 9 bits\n", stderr);
157 		exit(2);
158 	}
159 	size = (1 << nbits) + 1;
160 	base = 1;
161 	if (sign)
162 		base += 1 << (nbits - 1);
163 	digit_contig = 1;
164 	for (i = 0 ; i < 10 ; i++) {
165 		if (digit[i] != '0' + i)
166 			digit_contig = 0;
167 	}
168 
169 	fputs("#include <ctype.h>\n", hfile);
170 	fputs("#include <sys/cdefs.h>\n", hfile);
171 
172 	/* Generate the #define statements in the header file */
173 	fputs("/* Syntax classes */\n", hfile);
174 	for (i = 0 ; synclass[i].name ; i++) {
175 		sprintf(buf, "#define %s %d", synclass[i].name, i);
176 		fputs(buf, hfile);
177 		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
178 			putc('\t', hfile);
179 		fprintf(hfile, "/* %s */\n", synclass[i].comment);
180 	}
181 	putc('\n', hfile);
182 	fputs("/* Syntax classes for is_ functions */\n", hfile);
183 	for (i = 0 ; is_entry[i].name ; i++) {
184 		sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
185 		fputs(buf, hfile);
186 		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
187 			putc('\t', hfile);
188 		fprintf(hfile, "/* %s */\n", is_entry[i].comment);
189 	}
190 	putc('\n', hfile);
191 	fprintf(hfile, "#define SYNBASE %d\n", base);
192 	fprintf(hfile, "#define PEOF %d\n\n", -base);
193 	putc('\n', hfile);
194 	fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
195 	fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
196 	fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
197 	fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
198 	putc('\n', hfile);
199 	output_type_macros();		/* is_digit, etc. */
200 	putc('\n', hfile);
201 
202 	/* Generate the syntax tables. */
203 	fputs("#include \"shell.h\"\n", cfile);
204 	fputs("#include \"syntax.h\"\n\n", cfile);
205 	init();
206 	fputs("/* syntax table used when not in quotes */\n", cfile);
207 	add("\n", "CNL");
208 	add("\\", "CBACK");
209 	add("'", "CSQUOTE");
210 	add("\"", "CDQUOTE");
211 	add("`", "CBQUOTE");
212 	add("$", "CVAR");
213 	add("}", "CENDVAR");
214 	add("<>();&| \t", "CSPCL");
215 	print("basesyntax");
216 	init();
217 	fputs("\n/* syntax table used when in double quotes */\n", cfile);
218 	add("\n", "CNL");
219 	add("\\", "CBACK");
220 	add("\"", "CENDQUOTE");
221 	add("`", "CBQUOTE");
222 	add("$", "CVAR");
223 	add("}", "CENDVAR");
224 	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
225 	add("!*?[=~:/-", "CCTL");
226 	print("dqsyntax");
227 	init();
228 	fputs("\n/* syntax table used when in single quotes */\n", cfile);
229 	add("\n", "CNL");
230 	add("'", "CENDQUOTE");
231 	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
232 	add("!*?[=~:/-", "CCTL");
233 	print("sqsyntax");
234 	init();
235 	fputs("\n/* syntax table used when in arithmetic */\n", cfile);
236 	add("\n", "CNL");
237 	add("\\", "CBACK");
238 	add("`", "CBQUOTE");
239 	add("'", "CSQUOTE");
240 	add("\"", "CDQUOTE");
241 	add("$", "CVAR");
242 	add("}", "CENDVAR");
243 	add("(", "CLP");
244 	add(")", "CRP");
245 	print("arisyntax");
246 	filltable("0");
247 	fputs("\n/* character classification table */\n", cfile);
248 	add("0123456789", "ISDIGIT");
249 	add("abcdefghijklmnopqrstucvwxyz", "ISLOWER");
250 	add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER");
251 	add("_", "ISUNDER");
252 	add("#?$!-*@", "ISSPECL");
253 	print("is_type");
254 	if (! digit_contig)
255 		digit_convert();
256 	exit(0);
257 }
258 
259 
260 
261 /*
262  * Clear the syntax table.
263  */
264 
265 static void
266 filltable(dftval)
267 	char *dftval;
268 {
269 	int i;
270 
271 	for (i = 0 ; i < size ; i++)
272 		syntax[i] = dftval;
273 }
274 
275 
276 /*
277  * Initialize the syntax table with default values.
278  */
279 
280 static void
281 init()
282 {
283 	filltable("CWORD");
284 	syntax[0] = "CEOF";
285 	syntax[base + CTLESC] = "CCTL";
286 	syntax[base + CTLVAR] = "CCTL";
287 	syntax[base + CTLENDVAR] = "CCTL";
288 	syntax[base + CTLBACKQ] = "CCTL";
289 	syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
290 	syntax[base + CTLARI] = "CCTL";
291 	syntax[base + CTLENDARI] = "CCTL";
292 }
293 
294 
295 /*
296  * Add entries to the syntax table.
297  */
298 
299 static void
300 add(p, type)
301 	char *p, *type;
302 {
303 	while (*p)
304 		syntax[*p++ + base] = type;
305 }
306 
307 
308 
309 /*
310  * Output the syntax table.
311  */
312 
313 static void
314 print(name)
315 	char *name;
316 {
317 	int i;
318 	int col;
319 
320 	fprintf(hfile, "extern const char %s[];\n", name);
321 	fprintf(cfile, "const char %s[%d] = {\n", name, size);
322 	col = 0;
323 	for (i = 0 ; i < size ; i++) {
324 		if (i == 0) {
325 			fputs("      ", cfile);
326 		} else if ((i & 03) == 0) {
327 			fputs(",\n      ", cfile);
328 			col = 0;
329 		} else {
330 			putc(',', cfile);
331 			while (++col < 9 * (i & 03))
332 				putc(' ', cfile);
333 		}
334 		fputs(syntax[i], cfile);
335 		col += strlen(syntax[i]);
336 	}
337 	fputs("\n};\n", cfile);
338 }
339 
340 
341 
342 /*
343  * Output character classification macros (e.g. is_digit).  If digits are
344  * contiguous, we can test for them quickly.
345  */
346 
347 static char *macro[] = {
348 	"#define is_digit(c)\t((is_type+SYNBASE)[c] & ISDIGIT)",
349 	"#define is_alpha(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && isalpha((unsigned char) (c)))",
350 	"#define is_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalpha((unsigned char) (c))))",
351 	"#define is_in_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalnum((unsigned char) (c))))",
352 	"#define is_special(c)\t((is_type+SYNBASE)[c] & (ISSPECL|ISDIGIT))",
353 	NULL
354 };
355 
356 static void
357 output_type_macros()
358 {
359 	char **pp;
360 
361 	if (digit_contig)
362 		macro[0] = "#define is_digit(c)\t((unsigned)((c) - '0') <= 9)";
363 	for (pp = macro ; *pp ; pp++)
364 		fprintf(hfile, "%s\n", *pp);
365 	if (digit_contig)
366 		fputs("#define digit_val(c)\t((c) - '0')\n", hfile);
367 	else
368 		fputs("#define digit_val(c)\t(digit_value[c])\n", hfile);
369 }
370 
371 
372 
373 /*
374  * Output digit conversion table (if digits are not contiguous).
375  */
376 
377 static void
378 digit_convert()
379 {
380 	int maxdigit;
381 	static char digit[] = "0123456789";
382 	char *p;
383 	int i;
384 
385 	maxdigit = 0;
386 	for (p = digit ; *p ; p++)
387 		if (*p > maxdigit)
388 			maxdigit = *p;
389 	fputs("extern const char digit_value[];\n", hfile);
390 	fputs("\n\nconst char digit_value[] = {\n", cfile);
391 	for (i = 0 ; i <= maxdigit ; i++) {
392 		for (p = digit ; *p && *p != i ; p++);
393 		if (*p == '\0')
394 			p = digit;
395 		fprintf(cfile, "      %d,\n", p - digit);
396 	}
397 	fputs("};\n", cfile);
398 }
399