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