xref: /dragonfly/bin/sh/mksyntax.c (revision fcf53d9b)
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  * @(#) Copyright (c) 1991, 1993 The Regents of the University of California.  All rights reserved.
37  * @(#)mksyntax.c	8.2 (Berkeley) 5/4/95
38  * $FreeBSD: src/bin/sh/mksyntax.c,v 1.34 2010/11/20 14:30:28 jilles Exp $
39  */
40 
41 /*
42  * This program creates syntax.h and syntax.c.
43  */
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include "parser.h"
49 
50 
51 struct synclass {
52 	const char *name;
53 	const char *comment;
54 };
55 
56 /* Syntax classes */
57 struct synclass synclass[] = {
58 	{ "CWORD",	"character is nothing special" },
59 	{ "CNL",	"newline character" },
60 	{ "CBACK",	"a backslash character" },
61 	{ "CSQUOTE",	"single quote" },
62 	{ "CDQUOTE",	"double quote" },
63 	{ "CENDQUOTE",	"a terminating quote" },
64 	{ "CBQUOTE",	"backwards single quote" },
65 	{ "CVAR",	"a dollar sign" },
66 	{ "CENDVAR",	"a '}' character" },
67 	{ "CLP",	"a left paren in arithmetic" },
68 	{ "CRP",	"a right paren in arithmetic" },
69 	{ "CEOF",	"end of file" },
70 	{ "CCTL",	"like CWORD, except it must be escaped" },
71 	{ "CSPCL",	"these terminate a word" },
72 	{ "CIGN",       "character should be ignored" },
73 	{ NULL,		NULL }
74 };
75 
76 
77 /*
78  * Syntax classes for is_ functions.  Warning:  if you add new classes
79  * you may have to change the definition of the is_in_name macro.
80  */
81 struct synclass is_entry[] = {
82 	{ "ISDIGIT",	"a digit" },
83 	{ "ISUPPER",	"an upper case letter" },
84 	{ "ISLOWER",	"a lower case letter" },
85 	{ "ISUNDER",	"an underscore" },
86 	{ "ISSPECL",	"the name of a special parameter" },
87 	{ NULL, 	NULL }
88 };
89 
90 static char writer[] = "\
91 /*\n\
92  * This file was generated by the mksyntax program.\n\
93  */\n\
94 \n";
95 
96 
97 static FILE *cfile;
98 static FILE *hfile;
99 static const char *syntax[513];
100 static int base;
101 static int size;	/* number of values which a char variable can have */
102 static int nbits;	/* number of bits in a character */
103 static int digit_contig;/* true if digits are contiguous */
104 
105 static void filltable(const char *);
106 static void init(void);
107 static void add(const char *, const char *);
108 static void print(const char *);
109 static void output_type_macros(void);
110 static void digit_convert(void);
111 
112 int
113 main(int argc __unused, char **argv __unused)
114 {
115 	char c;
116 	char d;
117 	int sign;
118 	int i;
119 	char buf[80];
120 	int pos;
121 	static char digit[] = "0123456789";
122 
123 	/* Create output files */
124 	if ((cfile = fopen("syntax.c", "w")) == NULL) {
125 		perror("syntax.c");
126 		exit(2);
127 	}
128 	if ((hfile = fopen("syntax.h", "w")) == NULL) {
129 		perror("syntax.h");
130 		exit(2);
131 	}
132 	fputs(writer, hfile);
133 	fputs(writer, cfile);
134 
135 	/* Determine the characteristics of chars. */
136 	c = -1;
137 	sign = (c > 0) ? 0 : 1;
138 	for (nbits = 1 ; ; nbits++) {
139 		d = (1 << nbits) - 1;
140 		if (d == c)
141 			break;
142 	}
143 #if 0
144 	printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits);
145 #endif
146 	if (nbits > 9) {
147 		fputs("Characters can't have more than 9 bits\n", stderr);
148 		exit(2);
149 	}
150 	size = (1 << nbits) + 1;
151 	base = 1;
152 	if (sign)
153 		base += 1 << (nbits - 1);
154 	digit_contig = 1;
155 	for (i = 0 ; i < 10 ; i++) {
156 		if (digit[i] != '0' + i)
157 			digit_contig = 0;
158 	}
159 
160 	fputs("#include <sys/cdefs.h>\n", hfile);
161 	fputs("#include <ctype.h>\n", hfile);
162 
163 	/* Generate the #define statements in the header file */
164 	fputs("/* Syntax classes */\n", hfile);
165 	for (i = 0 ; synclass[i].name ; i++) {
166 		sprintf(buf, "#define %s %d", synclass[i].name, i);
167 		fputs(buf, hfile);
168 		for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
169 			putc('\t', hfile);
170 		fprintf(hfile, "/* %s */\n", synclass[i].comment);
171 	}
172 	putc('\n', hfile);
173 	fputs("/* Syntax classes for is_ functions */\n", hfile);
174 	for (i = 0 ; is_entry[i].name ; i++) {
175 		sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << 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", is_entry[i].comment);
180 	}
181 	putc('\n', hfile);
182 	fprintf(hfile, "#define SYNBASE %d\n", base);
183 	fprintf(hfile, "#define PEOF %d\n\n", -base);
184 	putc('\n', hfile);
185 	fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
186 	fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
187 	fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
188 	fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
189 	putc('\n', hfile);
190 	output_type_macros();		/* is_digit, etc. */
191 	putc('\n', hfile);
192 
193 	/* Generate the syntax tables. */
194 	fputs("#include \"shell.h\"\n", cfile);
195 	fputs("#include \"syntax.h\"\n\n", cfile);
196 	init();
197 	fputs("/* syntax table used when not in quotes */\n", cfile);
198 	add("\n", "CNL");
199 	add("\\", "CBACK");
200 	add("'", "CSQUOTE");
201 	add("\"", "CDQUOTE");
202 	add("`", "CBQUOTE");
203 	add("$", "CVAR");
204 	add("}", "CENDVAR");
205 	add("<>();&| \t", "CSPCL");
206 	print("basesyntax");
207 	init();
208 	fputs("\n/* syntax table used when in double quotes */\n", cfile);
209 	add("\n", "CNL");
210 	add("\\", "CBACK");
211 	add("\"", "CENDQUOTE");
212 	add("`", "CBQUOTE");
213 	add("$", "CVAR");
214 	add("}", "CENDVAR");
215 	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
216 	add("!*?[=~:/-", "CCTL");
217 	print("dqsyntax");
218 	init();
219 	fputs("\n/* syntax table used when in single quotes */\n", cfile);
220 	add("\n", "CNL");
221 	add("'", "CENDQUOTE");
222 	/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
223 	add("!*?[=~:/-", "CCTL");
224 	print("sqsyntax");
225 	init();
226 	fputs("\n/* syntax table used when in arithmetic */\n", cfile);
227 	add("\n", "CNL");
228 	add("\\", "CBACK");
229 	add("`", "CBQUOTE");
230 	add("\"", "CIGN");
231 	add("$", "CVAR");
232 	add("}", "CENDVAR");
233 	add("(", "CLP");
234 	add(")", "CRP");
235 	print("arisyntax");
236 	filltable("0");
237 	fputs("\n/* character classification table */\n", cfile);
238 	add("0123456789", "ISDIGIT");
239 	add("abcdefghijklmnopqrstuvwxyz", "ISLOWER");
240 	add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER");
241 	add("_", "ISUNDER");
242 	add("#?$!-*@", "ISSPECL");
243 	print("is_type");
244 	if (! digit_contig)
245 		digit_convert();
246 	exit(0);
247 }
248 
249 
250 
251 /*
252  * Clear the syntax table.
253  */
254 
255 static void
256 filltable(const char *dftval)
257 {
258 	int i;
259 
260 	for (i = 0 ; i < size ; i++)
261 		syntax[i] = dftval;
262 }
263 
264 
265 /*
266  * Initialize the syntax table with default values.
267  */
268 
269 static void
270 init(void)
271 {
272 	filltable("CWORD");
273 	syntax[0] = "CEOF";
274 	syntax[base + CTLESC] = "CCTL";
275 	syntax[base + CTLVAR] = "CCTL";
276 	syntax[base + CTLENDVAR] = "CCTL";
277 	syntax[base + CTLBACKQ] = "CCTL";
278 	syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
279 	syntax[base + CTLARI] = "CCTL";
280 	syntax[base + CTLENDARI] = "CCTL";
281 	syntax[base + CTLQUOTEMARK] = "CCTL";
282 	syntax[base + CTLQUOTEEND] = "CCTL";
283 }
284 
285 
286 /*
287  * Add entries to the syntax table.
288  */
289 
290 static void
291 add(const char *p, const char *type)
292 {
293 	while (*p)
294 		syntax[*p++ + base] = type;
295 }
296 
297 
298 
299 /*
300  * Output the syntax table.
301  */
302 
303 static void
304 print(const char *name)
305 {
306 	int i;
307 	int col;
308 
309 	fprintf(hfile, "extern const char %s[];\n", name);
310 	fprintf(cfile, "const char %s[%d] = {\n", name, size);
311 	col = 0;
312 	for (i = 0 ; i < size ; i++) {
313 		if (i == 0) {
314 			fputs("      ", cfile);
315 		} else if ((i & 03) == 0) {
316 			fputs(",\n      ", cfile);
317 			col = 0;
318 		} else {
319 			putc(',', cfile);
320 			while (++col < 9 * (i & 03))
321 				putc(' ', cfile);
322 		}
323 		fputs(syntax[i], cfile);
324 		col += strlen(syntax[i]);
325 	}
326 	fputs("\n};\n", cfile);
327 }
328 
329 
330 
331 /*
332  * Output character classification macros (e.g. is_digit).  If digits are
333  * contiguous, we can test for them quickly.
334  */
335 
336 static const char *macro[] = {
337 	"#define is_digit(c)\t((is_type+SYNBASE)[(int)c] & ISDIGIT)",
338 	"#define is_eof(c)\t((c) == PEOF)",
339 	"#define is_alpha(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER))",
340 	"#define is_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER))",
341 	"#define is_in_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))",
342 	"#define is_special(c)\t((is_type+SYNBASE)[(int)c] & (ISSPECL|ISDIGIT))",
343 	NULL
344 };
345 
346 static void
347 output_type_macros(void)
348 {
349 	const char **pp;
350 
351 	if (digit_contig)
352 		macro[0] = "#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)";
353 	for (pp = macro ; *pp ; pp++)
354 		fprintf(hfile, "%s\n", *pp);
355 	if (digit_contig)
356 		fputs("#define digit_val(c)\t((c) - '0')\n", hfile);
357 	else
358 		fputs("#define digit_val(c)\t(digit_value[c])\n", hfile);
359 }
360 
361 
362 
363 /*
364  * Output digit conversion table (if digits are not contiguous).
365  */
366 
367 static void
368 digit_convert(void)
369 {
370 	int maxdigit;
371 	static char digit[] = "0123456789";
372 	char *p;
373 	int i;
374 
375 	maxdigit = 0;
376 	for (p = digit ; *p ; p++)
377 		if (*p > maxdigit)
378 			maxdigit = *p;
379 	fputs("extern const char digit_value[];\n", hfile);
380 	fputs("\n\nconst char digit_value[] = {\n", cfile);
381 	for (i = 0 ; i <= maxdigit ; i++) {
382 		for (p = digit ; *p && *p != i ; p++);
383 		if (*p == '\0')
384 			p = digit;
385 		fprintf(cfile, "      %td,\n", p - digit);
386 	}
387 	fputs("};\n", cfile);
388 }
389