unstack, sort: cleanup and improvement
[minix.git] / commands / ash / mksyntax.c
blob40b825052a006c8573dd7b8a0975a9334a9d7e8f
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #if 0
34 #ifndef lint
35 static char const copyright[] =
36 "@(#) Copyright (c) 1991, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
40 #ifndef lint
41 static char sccsid[] = "@(#)mksyntax.c 8.2 (Berkeley) 5/4/95";
42 #endif /* not lint */
43 #endif
44 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD: src/bin/sh/mksyntax.c,v 1.23 2004/04/06 20:06:51 markm Exp $");
50 * This program creates syntax.h and syntax.c.
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include "parser.h"
58 struct synclass {
59 char *name;
60 char *comment;
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 }
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.
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 }
96 static char writer[] = "\
97 /*\n\
98 * This file was generated by the mksyntax program.\n\
99 */\n\
100 \n";
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 */
111 static void filltable(char *);
112 static void init(void);
113 static void add(char *, char *);
114 static void print(char *);
115 static void output_type_macros(void);
116 static void digit_convert(void);
118 #ifndef __unused
119 #define __unused __attribute__((__unused__))
120 #endif
123 main(int argc __unused, char **argv __unused)
125 char c;
126 char d;
127 int sign;
128 int i;
129 char buf[80];
130 int pos;
131 static char digit[] = "0123456789";
133 /* Create output files */
134 if ((cfile = fopen("syntax.c", "w")) == NULL) {
135 perror("syntax.c");
136 exit(2);
138 if ((hfile = fopen("syntax.h", "w")) == NULL) {
139 perror("syntax.h");
140 exit(2);
142 fputs(writer, hfile);
143 fputs(writer, cfile);
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;
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);
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;
173 fputs("#include <ctype.h>\n", hfile);
175 /* Generate the #define statements in the header file */
176 fputs("/* Syntax classes */\n", hfile);
177 for (i = 0 ; synclass[i].name ; i++) {
178 sprintf(buf, "#define %s %d", synclass[i].name, i);
179 fputs(buf, hfile);
180 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
181 putc('\t', hfile);
182 fprintf(hfile, "/* %s */\n", synclass[i].comment);
184 putc('\n', hfile);
185 fputs("/* Syntax classes for is_ functions */\n", hfile);
186 for (i = 0 ; is_entry[i].name ; i++) {
187 sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
188 fputs(buf, hfile);
189 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
190 putc('\t', hfile);
191 fprintf(hfile, "/* %s */\n", is_entry[i].comment);
193 putc('\n', hfile);
194 fprintf(hfile, "#define SYNBASE %d\n", base);
195 fprintf(hfile, "#define PEOF %d\n\n", -base);
196 putc('\n', hfile);
197 fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
198 fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
199 fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
200 fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
201 putc('\n', hfile);
202 output_type_macros(); /* is_digit, etc. */
203 putc('\n', hfile);
205 /* Generate the syntax tables. */
206 fputs("#include \"shell.h\"\n", cfile);
207 fputs("#include \"syntax.h\"\n\n", cfile);
208 init();
209 fputs("/* syntax table used when not in quotes */\n", cfile);
210 add("\n", "CNL");
211 add("\\", "CBACK");
212 add("'", "CSQUOTE");
213 add("\"", "CDQUOTE");
214 add("`", "CBQUOTE");
215 add("$", "CVAR");
216 add("}", "CENDVAR");
217 add("<>();&| \t", "CSPCL");
218 print("basesyntax");
219 init();
220 fputs("\n/* syntax table used when in double quotes */\n", cfile);
221 add("\n", "CNL");
222 add("\\", "CBACK");
223 add("\"", "CENDQUOTE");
224 add("`", "CBQUOTE");
225 add("$", "CVAR");
226 add("}", "CENDVAR");
227 /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
228 add("!*?[=~:/-", "CCTL");
229 print("dqsyntax");
230 init();
231 fputs("\n/* syntax table used when in single quotes */\n", cfile);
232 add("\n", "CNL");
233 add("'", "CENDQUOTE");
234 /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
235 add("!*?[=~:/-", "CCTL");
236 print("sqsyntax");
237 init();
238 fputs("\n/* syntax table used when in arithmetic */\n", cfile);
239 add("\n", "CNL");
240 add("\\", "CBACK");
241 add("`", "CBQUOTE");
242 add("'", "CSQUOTE");
243 add("\"", "CDQUOTE");
244 add("$", "CVAR");
245 add("}", "CENDVAR");
246 add("(", "CLP");
247 add(")", "CRP");
248 print("arisyntax");
249 filltable("0");
250 fputs("\n/* character classification table */\n", cfile);
251 add("0123456789", "ISDIGIT");
252 add("abcdefghijklmnopqrstucvwxyz", "ISLOWER");
253 add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER");
254 add("_", "ISUNDER");
255 add("#?$!-*@", "ISSPECL");
256 print("is_type");
257 if (! digit_contig)
258 digit_convert();
259 exit(0);
265 * Clear the syntax table.
268 static void
269 filltable(char *dftval)
271 int i;
273 for (i = 0 ; i < size ; i++)
274 syntax[i] = dftval;
279 * Initialize the syntax table with default values.
282 static void
283 init(void)
285 filltable("CWORD");
286 syntax[0] = "CEOF";
287 syntax[base + CTLESC] = "CCTL";
288 syntax[base + CTLVAR] = "CCTL";
289 syntax[base + CTLENDVAR] = "CCTL";
290 syntax[base + CTLBACKQ] = "CCTL";
291 syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
292 syntax[base + CTLARI] = "CCTL";
293 syntax[base + CTLENDARI] = "CCTL";
294 syntax[base + CTLQUOTEMARK] = "CCTL";
299 * Add entries to the syntax table.
302 static void
303 add(char *p, char *type)
305 while (*p)
306 syntax[*p++ + base] = type;
312 * Output the syntax table.
315 static void
316 print(char *name)
318 int i;
319 int col;
321 fprintf(hfile, "extern const char %s[];\n", name);
322 fprintf(cfile, "const char %s[%d] = {\n", name, size);
323 col = 0;
324 for (i = 0 ; i < size ; i++) {
325 if (i == 0) {
326 fputs(" ", cfile);
327 } else if ((i & 03) == 0) {
328 fputs(",\n ", cfile);
329 col = 0;
330 } else {
331 putc(',', cfile);
332 while (++col < 9 * (i & 03))
333 putc(' ', cfile);
335 fputs(syntax[i], cfile);
336 col += strlen(syntax[i]);
338 fputs("\n};\n", cfile);
344 * Output character classification macros (e.g. is_digit). If digits are
345 * contiguous, we can test for them quickly.
348 static char *macro[] = {
349 "#define is_digit(c)\t((is_type+SYNBASE)[c] & ISDIGIT)",
350 "#define is_alpha(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && isalpha((unsigned char) (c)))",
351 "#define is_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalpha((unsigned char) (c))))",
352 "#define is_in_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalnum((unsigned char) (c))))",
353 "#define is_special(c)\t((is_type+SYNBASE)[c] & (ISSPECL|ISDIGIT))",
354 NULL
357 static void
358 output_type_macros(void)
360 char **pp;
362 if (digit_contig)
363 macro[0] = "#define is_digit(c)\t((unsigned)((c) - '0') <= 9)";
364 for (pp = macro ; *pp ; pp++)
365 fprintf(hfile, "%s\n", *pp);
366 if (digit_contig)
367 fputs("#define digit_val(c)\t((c) - '0')\n", hfile);
368 else
369 fputs("#define digit_val(c)\t(digit_value[c])\n", hfile);
375 * Output digit conversion table (if digits are not contiguous).
378 static void
379 digit_convert(void)
381 int maxdigit;
382 static char digit[] = "0123456789";
383 char *p;
384 int i;
386 maxdigit = 0;
387 for (p = digit ; *p ; p++)
388 if (*p > maxdigit)
389 maxdigit = *p;
390 fputs("extern const char digit_value[];\n", hfile);
391 fputs("\n\nconst char digit_value[] = {\n", cfile);
392 for (i = 0 ; i <= maxdigit ; i++) {
393 for (p = digit ; *p && *p != i ; p++);
394 if (*p == '\0')
395 p = digit;
396 fprintf(cfile, " %d,\n", (int)(p - digit));
398 fputs("};\n", cfile);
402 * $PchId: mksyntax.c,v 1.7 2006/05/23 12:04:27 philip Exp $