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