Merge branch 'vendor/MDOCML'
[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: head/bin/sh/mksyntax.c 246522 2013-02-07 22:42:33Z jilles $
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         { "CSBACK",     "a backslash character in single quotes" },
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         { "CIGN",       "character should be ignored" },
74         { NULL,         NULL }
75 };
76
77
78 /*
79  * Syntax classes for is_ functions.  Warning:  if you add new classes
80  * you may have to change the definition of the is_in_name macro.
81  */
82 struct synclass is_entry[] = {
83         { "ISDIGIT",    "a digit" },
84         { "ISUPPER",    "an upper case letter" },
85         { "ISLOWER",    "a lower case letter" },
86         { "ISUNDER",    "an underscore" },
87         { "ISSPECL",    "the name of a special parameter" },
88         { NULL,         NULL }
89 };
90
91 static char writer[] = "\
92 /*\n\
93  * This file was generated by the mksyntax program.\n\
94  */\n\
95 \n";
96
97
98 static FILE *cfile;
99 static FILE *hfile;
100
101 static void add_default(void);
102 static void finish(void);
103 static void init(const char *);
104 static void add(const char *, const char *);
105 static void output_type_macros(void);
106
107 int
108 main(int argc __unused, char **argv __unused)
109 {
110         int i;
111         char buf[80];
112         int pos;
113
114         /* Create output files */
115         if ((cfile = fopen("syntax.c", "w")) == NULL) {
116                 perror("syntax.c");
117                 exit(2);
118         }
119         if ((hfile = fopen("syntax.h", "w")) == NULL) {
120                 perror("syntax.h");
121                 exit(2);
122         }
123         fputs(writer, hfile);
124         fputs(writer, cfile);
125
126         fputs("#include <sys/cdefs.h>\n", hfile);
127         fputs("#include <limits.h>\n\n", hfile);
128
129         /* Generate the #define statements in the header file */
130         fputs("/* Syntax classes */\n", hfile);
131         for (i = 0 ; synclass[i].name ; i++) {
132                 sprintf(buf, "#define %s %d", synclass[i].name, i);
133                 fputs(buf, hfile);
134                 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
135                         putc('\t', hfile);
136                 fprintf(hfile, "/* %s */\n", synclass[i].comment);
137         }
138         putc('\n', hfile);
139         fputs("/* Syntax classes for is_ functions */\n", hfile);
140         for (i = 0 ; is_entry[i].name ; i++) {
141                 sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
142                 fputs(buf, hfile);
143                 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
144                         putc('\t', hfile);
145                 fprintf(hfile, "/* %s */\n", is_entry[i].comment);
146         }
147         putc('\n', hfile);
148         fputs("#define SYNBASE (1 - CHAR_MIN)\n", hfile);
149         fputs("#define PEOF -SYNBASE\n\n", hfile);
150         putc('\n', hfile);
151         fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
152         fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
153         fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
154         fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
155         putc('\n', hfile);
156         output_type_macros();           /* is_digit, etc. */
157         putc('\n', hfile);
158
159         /* Generate the syntax tables. */
160         fputs("#include \"parser.h\"\n", cfile);
161         fputs("#include \"shell.h\"\n", cfile);
162         fputs("#include \"syntax.h\"\n\n", cfile);
163
164         fputs("/* syntax table used when not in quotes */\n", cfile);
165         init("basesyntax");
166         add_default();
167         add("\n", "CNL");
168         add("\\", "CBACK");
169         add("'", "CSQUOTE");
170         add("\"", "CDQUOTE");
171         add("`", "CBQUOTE");
172         add("$", "CVAR");
173         add("}", "CENDVAR");
174         add("<>();&| \t", "CSPCL");
175         finish();
176
177         fputs("\n/* syntax table used when in double quotes */\n", cfile);
178         init("dqsyntax");
179         add_default();
180         add("\n", "CNL");
181         add("\\", "CBACK");
182         add("\"", "CENDQUOTE");
183         add("`", "CBQUOTE");
184         add("$", "CVAR");
185         add("}", "CENDVAR");
186         /* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */
187         add("!*?[]=~:/-^", "CCTL");
188         finish();
189
190         fputs("\n/* syntax table used when in single quotes */\n", cfile);
191         init("sqsyntax");
192         add_default();
193         add("\n", "CNL");
194         add("\\", "CSBACK");
195         add("'", "CENDQUOTE");
196         /* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */
197         add("!*?[]=~:/-^", "CCTL");
198         finish();
199
200         fputs("\n/* syntax table used when in arithmetic */\n", cfile);
201         init("arisyntax");
202         add_default();
203         add("\n", "CNL");
204         add("\\", "CBACK");
205         add("`", "CBQUOTE");
206         add("\"", "CIGN");
207         add("$", "CVAR");
208         add("}", "CENDVAR");
209         add("(", "CLP");
210         add(")", "CRP");
211         finish();
212
213         fputs("\n/* character classification table */\n", cfile);
214         init("is_type");
215         add("0123456789", "ISDIGIT");
216         add("abcdefghijklmnopqrstuvwxyz", "ISLOWER");
217         add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER");
218         add("_", "ISUNDER");
219         add("#?$!-*@", "ISSPECL");
220         finish();
221
222         exit(0);
223 }
224
225
226 /*
227  * Output the header and declaration of a syntax table.
228  */
229
230 static void
231 init(const char *name)
232 {
233         fprintf(hfile, "extern const char %s[];\n", name);
234         fprintf(cfile, "const char %s[SYNBASE + CHAR_MAX + 1] = {\n", name);
235 }
236
237
238 static void
239 add_one(const char *key, const char *type)
240 {
241         fprintf(cfile, "\t[SYNBASE + %s] = %s,\n", key, type);
242 }
243
244
245 /*
246  * Add default values to the syntax table.
247  */
248
249 static void
250 add_default(void)
251 {
252         add_one("PEOF",                "CEOF");
253         add_one("CTLESC",              "CCTL");
254         add_one("CTLVAR",              "CCTL");
255         add_one("CTLENDVAR",           "CCTL");
256         add_one("CTLBACKQ",            "CCTL");
257         add_one("CTLBACKQ + CTLQUOTE", "CCTL");
258         add_one("CTLARI",              "CCTL");
259         add_one("CTLENDARI",           "CCTL");
260         add_one("CTLQUOTEMARK",        "CCTL");
261         add_one("CTLQUOTEEND",         "CCTL");
262 }
263
264
265 /*
266  * Output the footer of a syntax table.
267  */
268
269 static void
270 finish(void)
271 {
272         fputs("};\n", cfile);
273 }
274
275
276 /*
277  * Add entries to the syntax table.
278  */
279
280 static void
281 add(const char *p, const char *type)
282 {
283         for (; *p; ++p) {
284                 char c = *p;
285                 switch (c) {
286                 case '\t': c = 't';  break;
287                 case '\n': c = 'n';  break;
288                 case '\'': c = '\''; break;
289                 case '\\': c = '\\'; break;
290
291                 default:
292                         fprintf(cfile, "\t[SYNBASE + '%c'] = %s,\n", c, type);
293                         continue;
294                 }
295                 fprintf(cfile, "\t[SYNBASE + '\\%c'] = %s,\n", c, type);
296         }
297 }
298
299
300 /*
301  * Output character classification macros (e.g. is_digit).  If digits are
302  * contiguous, we can test for them quickly.
303  */
304
305 static const char *macro[] = {
306         "#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)",
307         "#define is_eof(c)\t((c) == PEOF)",
308         "#define is_alpha(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER))",
309         "#define is_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER))",
310         "#define is_in_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))",
311         "#define is_special(c)\t((is_type+SYNBASE)[(int)c] & (ISSPECL|ISDIGIT))",
312         "#define digit_val(c)\t((c) - '0')",
313         NULL
314 };
315
316 static void
317 output_type_macros(void)
318 {
319         const char **pp;
320
321         for (pp = macro ; *pp ; pp++)
322                 fprintf(hfile, "%s\n", *pp);
323 }