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