]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/dash/contrib/src/mksyntax.c
Inital import
[l4.git] / l4 / pkg / dash / contrib / src / mksyntax.c
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1997-2005
5  *      Herbert Xu <herbert@gondor.apana.org.au>.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Kenneth Almquist.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 /*
36  * This program creates syntax.h and syntax.c.
37  */
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/types.h>
43 #include "parser.h"
44
45
46 struct synclass {
47         char *name;
48         char *comment;
49 };
50
51 /* Syntax classes */
52 struct synclass synclass[] = {
53         { "CWORD",      "character is nothing special" },
54         { "CNL",        "newline character" },
55         { "CBACK",      "a backslash character" },
56         { "CDBACK",     "a backslash character in double quotes" },
57         { "CSQUOTE",    "single quote" },
58         { "CDQUOTE",    "double quote" },
59         { "CENDQUOTE",  "a terminating quote" },
60         { "CBQUOTE",    "backwards single quote" },
61         { "CVAR",       "a dollar sign" },
62         { "CENDVAR",    "a '}' character" },
63         { "CLP",        "a left paren in arithmetic" },
64         { "CRP",        "a right paren in arithmetic" },
65         { "CEOF",       "end of file" },
66         { "CCTL",       "like CWORD, except it must be escaped" },
67         { "CSPCL",      "these terminate a word" },
68         { "CIGN",       "character should be ignored" },
69         { NULL,         NULL }
70 };
71
72
73 /*
74  * Syntax classes for is_ functions.  Warning:  if you add new classes
75  * you may have to change the definition of the is_in_name macro.
76  */
77 struct synclass is_entry[] = {
78         { "ISDIGIT",    "a digit" },
79         { "ISUPPER",    "an upper case letter" },
80         { "ISLOWER",    "a lower case letter" },
81         { "ISUNDER",    "an underscore" },
82         { "ISSPECL",    "the name of a special parameter" },
83         { NULL,         NULL }
84 };
85
86 static char writer[] = "\
87 /*\n\
88  * This file was generated by the mksyntax program.\n\
89  */\n\
90 \n";
91
92
93 static FILE *cfile;
94 static FILE *hfile;
95 static char *syntax[513];
96
97 static void filltable(char *);
98 static void init(void);
99 static void add(char *, char *);
100 static void print(char *);
101 static void output_type_macros(void);
102 int main(int, char **);
103
104 int
105 main(int argc, char **argv)
106 {
107         int i;
108         char buf[80];
109         int pos;
110
111         /* Create output files */
112         if ((cfile = fopen("syntax.c", "w")) == NULL) {
113                 perror("syntax.c");
114                 exit(2);
115         }
116         if ((hfile = fopen("syntax.h", "w")) == NULL) {
117                 perror("syntax.h");
118                 exit(2);
119         }
120         fputs(writer, hfile);
121         fputs(writer, cfile);
122
123         fputs("#include <ctype.h>\n", hfile);
124         fputs("\n", hfile);
125         fputs("#ifdef CEOF\n", hfile);
126         fputs("#undef CEOF\n", hfile);
127         fputs("#endif\n", hfile);
128         fputs("\n", hfile);
129
130         /* Generate the #define statements in the header file */
131         fputs("/* Syntax classes */\n", hfile);
132         for (i = 0 ; synclass[i].name ; i++) {
133                 sprintf(buf, "#define %s %d", synclass[i].name, i);
134                 fputs(buf, hfile);
135                 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
136                         putc('\t', hfile);
137                 fprintf(hfile, "/* %s */\n", synclass[i].comment);
138         }
139         putc('\n', hfile);
140         fputs("/* Syntax classes for is_ functions */\n", hfile);
141         for (i = 0 ; is_entry[i].name ; i++) {
142                 sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
143                 fputs(buf, hfile);
144                 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
145                         putc('\t', hfile);
146                 fprintf(hfile, "/* %s */\n", is_entry[i].comment);
147         }
148         putc('\n', hfile);
149         fprintf(hfile, "#define SYNBASE %d\n", 130);
150         fprintf(hfile, "#define PEOF %d\n\n", -130);
151         fprintf(hfile, "#define PEOA %d\n\n", -129);
152         putc('\n', hfile);
153         fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
154         fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
155         fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
156         fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
157         putc('\n', hfile);
158         output_type_macros();           /* is_digit, etc. */
159         putc('\n', hfile);
160
161         /* Generate the syntax tables. */
162         fputs("#include \"shell.h\"\n", cfile);
163         fputs("#include \"syntax.h\"\n\n", cfile);
164         init();
165         fputs("/* syntax table used when not in quotes */\n", cfile);
166         add("\n", "CNL");
167         add("\\", "CBACK");
168         add("'", "CSQUOTE");
169         add("\"", "CDQUOTE");
170         add("`", "CBQUOTE");
171         add("$", "CVAR");
172         add("}", "CENDVAR");
173         add("<>();&| \t", "CSPCL");
174         syntax[1] = "CSPCL";
175         print("basesyntax");
176         init();
177         fputs("\n/* syntax table used when in double quotes */\n", cfile);
178         add("\n", "CNL");
179         add("\\", "CDBACK");
180         add("\"", "CENDQUOTE");
181         add("`", "CBQUOTE");
182         add("$", "CVAR");
183         add("}", "CENDVAR");
184         /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
185         add("!*?[=~:/-]", "CCTL");
186         print("dqsyntax");
187         init();
188         fputs("\n/* syntax table used when in single quotes */\n", cfile);
189         add("\n", "CNL");
190         add("'", "CENDQUOTE");
191         /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
192         add("!*?[=~:/-]\\", "CCTL");
193         print("sqsyntax");
194         init();
195         fputs("\n/* syntax table used when in arithmetic */\n", cfile);
196         add("\n", "CNL");
197         add("\\", "CDBACK");
198         add("`", "CBQUOTE");
199         add("$", "CVAR");
200         add("}", "CENDVAR");
201         add("(", "CLP");
202         add(")", "CRP");
203         print("arisyntax");
204         filltable("0");
205         fputs("\n/* character classification table */\n", cfile);
206         add("0123456789", "ISDIGIT");
207         add("abcdefghijklmnopqrstucvwxyz", "ISLOWER");
208         add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER");
209         add("_", "ISUNDER");
210         add("#?$!-*@", "ISSPECL");
211         print("is_type");
212         exit(0);
213         /* NOTREACHED */
214 }
215
216
217
218 /*
219  * Clear the syntax table.
220  */
221
222 static void
223 filltable(char *dftval)
224 {
225         int i;
226
227         for (i = 0 ; i < 257; i++)
228                 syntax[i] = dftval;
229 }
230
231
232 /*
233  * Initialize the syntax table with default values.
234  */
235
236 static void
237 init(void)
238 {
239         int ctl;
240
241         filltable("CWORD");
242         syntax[0] = "CEOF";
243         syntax[1] = "CIGN";
244         for (ctl = CTL_FIRST; ctl <= CTL_LAST; ctl++ )
245                 syntax[130 + ctl] = "CCTL";
246 }
247
248
249 /*
250  * Add entries to the syntax table.
251  */
252
253 static void
254 add(char *p, char *type)
255 {
256         while (*p)
257                 syntax[(signed char)*p++ + 130] = type;
258 }
259
260
261
262 /*
263  * Output the syntax table.
264  */
265
266 static void
267 print(char *name)
268 {
269         int i;
270         int col;
271
272         fprintf(hfile, "extern const char %s[];\n", name);
273         fprintf(cfile, "const char %s[%d] = {\n", name, 257);
274         col = 0;
275         for (i = 0 ; i < 257; i++) {
276                 if (i == 0) {
277                         fputs("      ", cfile);
278                 } else if ((i & 03) == 0) {
279                         fputs(",\n      ", cfile);
280                         col = 0;
281                 } else {
282                         putc(',', cfile);
283                         while (++col < 9 * (i & 03))
284                                 putc(' ', cfile);
285                 }
286                 fputs(syntax[i], cfile);
287                 col += strlen(syntax[i]);
288         }
289         fputs("\n};\n", cfile);
290 }
291
292
293
294 /*
295  * Output character classification macros (e.g. is_digit).  If digits are
296  * contiguous, we can test for them quickly.
297  */
298
299 static char *macro[] = {
300         "#define is_digit(c)\t((unsigned)((c) - '0') <= 9)\n",
301         "#define is_alpha(c)\tisalpha((unsigned char)(c))\n",
302         "#define is_name(c)\t((c) == '_' || isalpha((unsigned char)(c)))\n",
303         "#define is_in_name(c)\t((c) == '_' || isalnum((unsigned char)(c)))\n",
304         "#define is_special(c)\t((is_type+SYNBASE)[(signed char)(c)] & (ISSPECL|ISDIGIT))\n",
305         NULL
306 };
307
308 static void
309 output_type_macros(void)
310 {
311         char **pp;
312
313         for (pp = macro ; *pp ; pp++)
314                 fputs(*pp, hfile);
315         fputs("#define digit_val(c)\t((c) - '0')\n", hfile);
316 }