Merge from vendor branch CVS:
[dragonfly.git] / bin / sh / mksyntax.c
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.14.2.3 2002/07/19 04:38:51 tjr Exp $
39  * $DragonFly: src/bin/sh/mksyntax.c,v 1.3 2004/03/19 18:39:41 cpressey Exp $
40  */
41
42 /*
43  * This program creates syntax.h and syntax.c.
44  */
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include "parser.h"
50
51
52 struct synclass {
53         const char *name;
54         const char *comment;
55 };
56
57 /* Syntax classes */
58 struct synclass synclass[] = {
59         { "CWORD",      "character is nothing special" },
60         { "CNL",        "newline character" },
61         { "CBACK",      "a backslash character" },
62         { "CSQUOTE",    "single quote" },
63         { "CDQUOTE",    "double quote" },
64         { "CENDQUOTE",  "a terminating quote" },
65         { "CBQUOTE",    "backwards single quote" },
66         { "CVAR",       "a dollar sign" },
67         { "CENDVAR",    "a '}' character" },
68         { "CLP",        "a left paren in arithmetic" },
69         { "CRP",        "a right paren in arithmetic" },
70         { "CEOF",       "end of file" },
71         { "CCTL",       "like CWORD, except it must be escaped" },
72         { "CSPCL",      "these terminate a word" },
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         if (c < 0)
138                 sign = 1;
139         else
140                 sign = 0;
141         for (nbits = 1 ; ; nbits++) {
142                 d = (1 << nbits) - 1;
143                 if (d == c)
144                         break;
145         }
146 #if 0
147         printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits);
148 #endif
149         if (nbits > 9) {
150                 fputs("Characters can't have more than 9 bits\n", stderr);
151                 exit(2);
152         }
153         size = (1 << nbits) + 1;
154         base = 1;
155         if (sign)
156                 base += 1 << (nbits - 1);
157         digit_contig = 1;
158         for (i = 0 ; i < 10 ; i++) {
159                 if (digit[i] != '0' + i)
160                         digit_contig = 0;
161         }
162
163         fputs("#include <sys/cdefs.h>\n", hfile);
164         fputs("#include <ctype.h>\n", hfile);
165
166         /* Generate the #define statements in the header file */
167         fputs("/* Syntax classes */\n", hfile);
168         for (i = 0 ; synclass[i].name ; i++) {
169                 sprintf(buf, "#define %s %d", synclass[i].name, i);
170                 fputs(buf, hfile);
171                 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
172                         putc('\t', hfile);
173                 fprintf(hfile, "/* %s */\n", synclass[i].comment);
174         }
175         putc('\n', hfile);
176         fputs("/* Syntax classes for is_ functions */\n", hfile);
177         for (i = 0 ; is_entry[i].name ; i++) {
178                 sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << 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", is_entry[i].comment);
183         }
184         putc('\n', hfile);
185         fprintf(hfile, "#define SYNBASE %d\n", base);
186         fprintf(hfile, "#define PEOF %d\n\n", -base);
187         putc('\n', hfile);
188         fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
189         fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
190         fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
191         fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
192         putc('\n', hfile);
193         output_type_macros();           /* is_digit, etc. */
194         putc('\n', hfile);
195
196         /* Generate the syntax tables. */
197         fputs("#include \"shell.h\"\n", cfile);
198         fputs("#include \"syntax.h\"\n\n", cfile);
199         init();
200         fputs("/* syntax table used when not in quotes */\n", cfile);
201         add("\n", "CNL");
202         add("\\", "CBACK");
203         add("'", "CSQUOTE");
204         add("\"", "CDQUOTE");
205         add("`", "CBQUOTE");
206         add("$", "CVAR");
207         add("}", "CENDVAR");
208         add("<>();&| \t", "CSPCL");
209         print("basesyntax");
210         init();
211         fputs("\n/* syntax table used when in double quotes */\n", cfile);
212         add("\n", "CNL");
213         add("\\", "CBACK");
214         add("\"", "CENDQUOTE");
215         add("`", "CBQUOTE");
216         add("$", "CVAR");
217         add("}", "CENDVAR");
218         /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
219         add("!*?[=~:/-", "CCTL");
220         print("dqsyntax");
221         init();
222         fputs("\n/* syntax table used when in single quotes */\n", cfile);
223         add("\n", "CNL");
224         add("'", "CENDQUOTE");
225         /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
226         add("!*?[=~:/-", "CCTL");
227         print("sqsyntax");
228         init();
229         fputs("\n/* syntax table used when in arithmetic */\n", cfile);
230         add("\n", "CNL");
231         add("\\", "CBACK");
232         add("`", "CBQUOTE");
233         add("'", "CSQUOTE");
234         add("\"", "CDQUOTE");
235         add("$", "CVAR");
236         add("}", "CENDVAR");
237         add("(", "CLP");
238         add(")", "CRP");
239         print("arisyntax");
240         filltable("0");
241         fputs("\n/* character classification table */\n", cfile);
242         add("0123456789", "ISDIGIT");
243         add("abcdefghijklmnopqrstucvwxyz", "ISLOWER");
244         add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER");
245         add("_", "ISUNDER");
246         add("#?$!-*@", "ISSPECL");
247         print("is_type");
248         if (! digit_contig)
249                 digit_convert();
250         exit(0);
251 }
252
253
254
255 /*
256  * Clear the syntax table.
257  */
258
259 static void
260 filltable(const char *dftval)
261 {
262         int i;
263
264         for (i = 0 ; i < size ; i++)
265                 syntax[i] = dftval;
266 }
267
268
269 /*
270  * Initialize the syntax table with default values.
271  */
272
273 static void
274 init(void)
275 {
276         filltable("CWORD");
277         syntax[0] = "CEOF";
278         syntax[base + CTLESC] = "CCTL";
279         syntax[base + CTLVAR] = "CCTL";
280         syntax[base + CTLENDVAR] = "CCTL";
281         syntax[base + CTLBACKQ] = "CCTL";
282         syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
283         syntax[base + CTLARI] = "CCTL";
284         syntax[base + CTLENDARI] = "CCTL";
285         syntax[base + CTLQUOTEMARK] = "CCTL";
286 }
287
288
289 /*
290  * Add entries to the syntax table.
291  */
292
293 static void
294 add(const char *p, const char *type)
295 {
296         while (*p)
297                 syntax[*p++ + base] = type;
298 }
299
300
301
302 /*
303  * Output the syntax table.
304  */
305
306 static void
307 print(const char *name)
308 {
309         int i;
310         int col;
311
312         fprintf(hfile, "extern const char %s[];\n", name);
313         fprintf(cfile, "const char %s[%d] = {\n", name, size);
314         col = 0;
315         for (i = 0 ; i < size ; i++) {
316                 if (i == 0) {
317                         fputs("      ", cfile);
318                 } else if ((i & 03) == 0) {
319                         fputs(",\n      ", cfile);
320                         col = 0;
321                 } else {
322                         putc(',', cfile);
323                         while (++col < 9 * (i & 03))
324                                 putc(' ', cfile);
325                 }
326                 fputs(syntax[i], cfile);
327                 col += strlen(syntax[i]);
328         }
329         fputs("\n};\n", cfile);
330 }
331
332
333
334 /*
335  * Output character classification macros (e.g. is_digit).  If digits are
336  * contiguous, we can test for them quickly.
337  */
338
339 static const char *macro[] = {
340         "#define is_digit(c)\t((is_type+SYNBASE)[c] & ISDIGIT)",
341         "#define is_alpha(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && isalpha((unsigned char) (c)))",
342         "#define is_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalpha((unsigned char) (c))))",
343         "#define is_in_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalnum((unsigned char) (c))))",
344         "#define is_special(c)\t((is_type+SYNBASE)[c] & (ISSPECL|ISDIGIT))",
345         NULL
346 };
347
348 static void
349 output_type_macros(void)
350 {
351         const char **pp;
352
353         if (digit_contig)
354                 macro[0] = "#define is_digit(c)\t((unsigned)((c) - '0') <= 9)";
355         for (pp = macro ; *pp ; pp++)
356                 fprintf(hfile, "%s\n", *pp);
357         if (digit_contig)
358                 fputs("#define digit_val(c)\t((c) - '0')\n", hfile);
359         else
360                 fputs("#define digit_val(c)\t(digit_value[c])\n", hfile);
361 }
362
363
364
365 /*
366  * Output digit conversion table (if digits are not contiguous).
367  */
368
369 static void
370 digit_convert(void)
371 {
372         int maxdigit;
373         static char digit[] = "0123456789";
374         char *p;
375         int i;
376
377         maxdigit = 0;
378         for (p = digit ; *p ; p++)
379                 if (*p > maxdigit)
380                         maxdigit = *p;
381         fputs("extern const char digit_value[];\n", hfile);
382         fputs("\n\nconst char digit_value[] = {\n", cfile);
383         for (i = 0 ; i <= maxdigit ; i++) {
384                 for (p = digit ; *p && *p != i ; p++);
385                 if (*p == '\0')
386                         p = digit;
387                 fprintf(cfile, "      %d,\n", p - digit);
388         }
389         fputs("};\n", cfile);
390 }