sh: Add $'quoting' (C-style escape sequences).
[dragonfly.git] / bin / sh / mksyntax.c
CommitLineData
984263bc
MD
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.
1de703da
MD
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
e1489450 38 * $FreeBSD: src/bin/sh/mksyntax.c,v 1.35 2011/05/05 20:55:55 jilles Exp $
984263bc
MD
39 */
40
984263bc
MD
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
51struct synclass {
492efe05
CP
52 const char *name;
53 const char *comment;
984263bc
MD
54};
55
56/* Syntax classes */
57struct synclass synclass[] = {
58 { "CWORD", "character is nothing special" },
59 { "CNL", "newline character" },
60 { "CBACK", "a backslash character" },
e1489450 61 { "CSBACK", "a backslash character in single quotes" },
984263bc
MD
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" },
99512ac4 73 { "CIGN", "character should be ignored" },
984263bc
MD
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 */
82struct 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
91static char writer[] = "\
92/*\n\
93 * This file was generated by the mksyntax program.\n\
94 */\n\
95\n";
96
97
98static FILE *cfile;
99static FILE *hfile;
492efe05 100static const char *syntax[513];
984263bc
MD
101static int base;
102static int size; /* number of values which a char variable can have */
103static int nbits; /* number of bits in a character */
104static int digit_contig;/* true if digits are contiguous */
105
492efe05 106static void filltable(const char *);
984263bc 107static void init(void);
492efe05
CP
108static void add(const char *, const char *);
109static void print(const char *);
984263bc
MD
110static void output_type_macros(void);
111static void digit_convert(void);
112
113int
114main(int argc __unused, char **argv __unused)
115{
116 char c;
117 char d;
118 int sign;
119 int i;
120 char buf[80];
121 int pos;
122 static char digit[] = "0123456789";
123
124 /* Create output files */
125 if ((cfile = fopen("syntax.c", "w")) == NULL) {
126 perror("syntax.c");
127 exit(2);
128 }
129 if ((hfile = fopen("syntax.h", "w")) == NULL) {
130 perror("syntax.h");
131 exit(2);
132 }
133 fputs(writer, hfile);
134 fputs(writer, cfile);
135
136 /* Determine the characteristics of chars. */
137 c = -1;
99512ac4 138 sign = (c > 0) ? 0 : 1;
984263bc
MD
139 for (nbits = 1 ; ; nbits++) {
140 d = (1 << nbits) - 1;
141 if (d == c)
142 break;
143 }
144#if 0
145 printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits);
146#endif
147 if (nbits > 9) {
148 fputs("Characters can't have more than 9 bits\n", stderr);
149 exit(2);
150 }
151 size = (1 << nbits) + 1;
152 base = 1;
153 if (sign)
154 base += 1 << (nbits - 1);
155 digit_contig = 1;
156 for (i = 0 ; i < 10 ; i++) {
157 if (digit[i] != '0' + i)
158 digit_contig = 0;
159 }
160
161 fputs("#include <sys/cdefs.h>\n", hfile);
162 fputs("#include <ctype.h>\n", hfile);
163
164 /* Generate the #define statements in the header file */
165 fputs("/* Syntax classes */\n", hfile);
166 for (i = 0 ; synclass[i].name ; i++) {
167 sprintf(buf, "#define %s %d", synclass[i].name, i);
168 fputs(buf, hfile);
169 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
170 putc('\t', hfile);
171 fprintf(hfile, "/* %s */\n", synclass[i].comment);
172 }
173 putc('\n', hfile);
174 fputs("/* Syntax classes for is_ functions */\n", hfile);
175 for (i = 0 ; is_entry[i].name ; i++) {
176 sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
177 fputs(buf, hfile);
178 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
179 putc('\t', hfile);
180 fprintf(hfile, "/* %s */\n", is_entry[i].comment);
181 }
182 putc('\n', hfile);
183 fprintf(hfile, "#define SYNBASE %d\n", base);
184 fprintf(hfile, "#define PEOF %d\n\n", -base);
185 putc('\n', hfile);
186 fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
187 fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
188 fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
189 fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
190 putc('\n', hfile);
191 output_type_macros(); /* is_digit, etc. */
192 putc('\n', hfile);
193
194 /* Generate the syntax tables. */
195 fputs("#include \"shell.h\"\n", cfile);
196 fputs("#include \"syntax.h\"\n\n", cfile);
197 init();
198 fputs("/* syntax table used when not in quotes */\n", cfile);
199 add("\n", "CNL");
200 add("\\", "CBACK");
201 add("'", "CSQUOTE");
202 add("\"", "CDQUOTE");
203 add("`", "CBQUOTE");
204 add("$", "CVAR");
205 add("}", "CENDVAR");
206 add("<>();&| \t", "CSPCL");
207 print("basesyntax");
208 init();
209 fputs("\n/* syntax table used when in double quotes */\n", cfile);
210 add("\n", "CNL");
211 add("\\", "CBACK");
212 add("\"", "CENDQUOTE");
213 add("`", "CBQUOTE");
214 add("$", "CVAR");
215 add("}", "CENDVAR");
216 /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
217 add("!*?[=~:/-", "CCTL");
218 print("dqsyntax");
219 init();
220 fputs("\n/* syntax table used when in single quotes */\n", cfile);
221 add("\n", "CNL");
e1489450 222 add("\\", "CSBACK");
984263bc
MD
223 add("'", "CENDQUOTE");
224 /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
225 add("!*?[=~:/-", "CCTL");
226 print("sqsyntax");
227 init();
228 fputs("\n/* syntax table used when in arithmetic */\n", cfile);
229 add("\n", "CNL");
230 add("\\", "CBACK");
231 add("`", "CBQUOTE");
99512ac4 232 add("\"", "CIGN");
984263bc
MD
233 add("$", "CVAR");
234 add("}", "CENDVAR");
235 add("(", "CLP");
236 add(")", "CRP");
237 print("arisyntax");
238 filltable("0");
239 fputs("\n/* character classification table */\n", cfile);
240 add("0123456789", "ISDIGIT");
99512ac4
PA
241 add("abcdefghijklmnopqrstuvwxyz", "ISLOWER");
242 add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER");
984263bc
MD
243 add("_", "ISUNDER");
244 add("#?$!-*@", "ISSPECL");
245 print("is_type");
246 if (! digit_contig)
247 digit_convert();
248 exit(0);
249}
250
251
252
253/*
254 * Clear the syntax table.
255 */
256
257static void
492efe05 258filltable(const char *dftval)
984263bc
MD
259{
260 int i;
261
262 for (i = 0 ; i < size ; i++)
263 syntax[i] = dftval;
264}
265
266
267/*
268 * Initialize the syntax table with default values.
269 */
270
271static void
272init(void)
273{
274 filltable("CWORD");
275 syntax[0] = "CEOF";
276 syntax[base + CTLESC] = "CCTL";
277 syntax[base + CTLVAR] = "CCTL";
278 syntax[base + CTLENDVAR] = "CCTL";
279 syntax[base + CTLBACKQ] = "CCTL";
280 syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL";
281 syntax[base + CTLARI] = "CCTL";
282 syntax[base + CTLENDARI] = "CCTL";
283 syntax[base + CTLQUOTEMARK] = "CCTL";
99512ac4 284 syntax[base + CTLQUOTEEND] = "CCTL";
984263bc
MD
285}
286
287
288/*
289 * Add entries to the syntax table.
290 */
291
292static void
492efe05 293add(const char *p, const char *type)
984263bc
MD
294{
295 while (*p)
296 syntax[*p++ + base] = type;
297}
298
299
300
301/*
302 * Output the syntax table.
303 */
304
305static void
492efe05 306print(const char *name)
984263bc
MD
307{
308 int i;
309 int col;
310
311 fprintf(hfile, "extern const char %s[];\n", name);
312 fprintf(cfile, "const char %s[%d] = {\n", name, size);
313 col = 0;
314 for (i = 0 ; i < size ; i++) {
315 if (i == 0) {
316 fputs(" ", cfile);
317 } else if ((i & 03) == 0) {
318 fputs(",\n ", cfile);
319 col = 0;
320 } else {
321 putc(',', cfile);
322 while (++col < 9 * (i & 03))
323 putc(' ', cfile);
324 }
325 fputs(syntax[i], cfile);
326 col += strlen(syntax[i]);
327 }
328 fputs("\n};\n", cfile);
329}
330
331
332
333/*
334 * Output character classification macros (e.g. is_digit). If digits are
335 * contiguous, we can test for them quickly.
336 */
337
492efe05 338static const char *macro[] = {
99512ac4 339 "#define is_digit(c)\t((is_type+SYNBASE)[(int)c] & ISDIGIT)",
a3143cf4 340 "#define is_eof(c)\t((c) == PEOF)",
99512ac4
PA
341 "#define is_alpha(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER))",
342 "#define is_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER))",
343 "#define is_in_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))",
344 "#define is_special(c)\t((is_type+SYNBASE)[(int)c] & (ISSPECL|ISDIGIT))",
984263bc
MD
345 NULL
346};
347
348static void
349output_type_macros(void)
350{
492efe05 351 const char **pp;
984263bc
MD
352
353 if (digit_contig)
99512ac4 354 macro[0] = "#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)";
984263bc
MD
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
369static void
370digit_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;
a276dc6b 387 fprintf(cfile, " %td,\n", p - digit);
984263bc
MD
388 }
389 fputs("};\n", cfile);
390}