]>
git.saurik.com Git - apple/shell_cmds.git/blob - sh/mksyntax.c
2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
37 static char const copyright
[] =
38 "@(#) Copyright (c) 1991, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid
[] = "@(#)mksyntax.c 8.2 (Berkeley) 5/4/95";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD: head/bin/sh/mksyntax.c 326025 2017-11-20 19:49:47Z pfg $");
50 * This program creates syntax.h and syntax.c.
65 static const struct synclass synclass
[] = {
66 { "CWORD", "character is nothing special" },
67 { "CNL", "newline character" },
68 { "CBACK", "a backslash character" },
69 { "CSBACK", "a backslash character in single quotes" },
70 { "CSQUOTE", "single quote" },
71 { "CDQUOTE", "double quote" },
72 { "CENDQUOTE", "a terminating quote" },
73 { "CBQUOTE", "backwards single quote" },
74 { "CVAR", "a dollar sign" },
75 { "CENDVAR", "a '}' character" },
76 { "CLP", "a left paren in arithmetic" },
77 { "CRP", "a right paren in arithmetic" },
78 { "CEOF", "end of file" },
79 { "CCTL", "like CWORD, except it must be escaped" },
80 { "CSPCL", "these terminate a word" },
81 { "CIGN", "character should be ignored" },
87 * Syntax classes for is_ functions. Warning: if you add new classes
88 * you may have to change the definition of the is_in_name macro.
90 static const struct synclass is_entry
[] = {
91 { "ISDIGIT", "a digit" },
92 { "ISUPPER", "an upper case letter" },
93 { "ISLOWER", "a lower case letter" },
94 { "ISUNDER", "an underscore" },
95 { "ISSPECL", "the name of a special parameter" },
99 static const char writer
[] = "\
101 * This file was generated by the mksyntax program.\n\
109 static void add_default(void);
110 static void finish(void);
111 static void init(const char *);
112 static void add(const char *, const char *);
113 static void output_type_macros(void);
116 main(int argc __unused
, char **argv __unused
)
122 /* Create output files */
123 if ((cfile
= fopen("syntax.c", "w")) == NULL
) {
127 if ((hfile
= fopen("syntax.h", "w")) == NULL
) {
131 fputs(writer
, hfile
);
132 fputs(writer
, cfile
);
134 fputs("#include <sys/cdefs.h>\n", hfile
);
135 fputs("#include <limits.h>\n\n", hfile
);
137 /* Generate the #define statements in the header file */
138 fputs("/* Syntax classes */\n", hfile
);
139 for (i
= 0 ; synclass
[i
].name
; i
++) {
140 sprintf(buf
, "#define %s %d", synclass
[i
].name
, i
);
142 for (pos
= strlen(buf
) ; pos
< 32 ; pos
= (pos
+ 8) & ~07)
144 fprintf(hfile
, "/* %s */\n", synclass
[i
].comment
);
147 fputs("/* Syntax classes for is_ functions */\n", hfile
);
148 for (i
= 0 ; is_entry
[i
].name
; i
++) {
149 sprintf(buf
, "#define %s %#o", is_entry
[i
].name
, 1 << i
);
151 for (pos
= strlen(buf
) ; pos
< 32 ; pos
= (pos
+ 8) & ~07)
153 fprintf(hfile
, "/* %s */\n", is_entry
[i
].comment
);
156 fputs("#define SYNBASE (1 - CHAR_MIN)\n", hfile
);
157 fputs("#define PEOF -SYNBASE\n\n", hfile
);
159 fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile
);
160 fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile
);
161 fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile
);
162 fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile
);
164 output_type_macros(); /* is_digit, etc. */
167 /* Generate the syntax tables. */
168 fputs("#include \"parser.h\"\n", cfile
);
169 fputs("#include \"shell.h\"\n", cfile
);
170 fputs("#include \"syntax.h\"\n\n", cfile
);
172 fputs("/* syntax table used when not in quotes */\n", cfile
);
178 add("\"", "CDQUOTE");
182 add("<>();&| \t", "CSPCL");
185 fputs("\n/* syntax table used when in double quotes */\n", cfile
);
190 add("\"", "CENDQUOTE");
194 /* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */
195 add("!*?[]=~:/-^", "CCTL");
198 fputs("\n/* syntax table used when in single quotes */\n", cfile
);
203 add("'", "CENDQUOTE");
204 /* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */
205 add("!*?[]=~:/-^", "CCTL");
208 fputs("\n/* syntax table used when in arithmetic */\n", cfile
);
221 fputs("\n/* character classification table */\n", cfile
);
223 add("0123456789", "ISDIGIT");
224 add("abcdefghijklmnopqrstuvwxyz", "ISLOWER");
225 add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER");
227 add("#?$!-*@", "ISSPECL");
235 * Output the header and declaration of a syntax table.
239 init(const char *name
)
241 fprintf(hfile
, "extern const char %s[];\n", name
);
242 fprintf(cfile
, "const char %s[SYNBASE + CHAR_MAX + 1] = {\n", name
);
247 add_one(const char *key
, const char *type
)
249 fprintf(cfile
, "\t[SYNBASE + %s] = %s,\n", key
, type
);
254 * Add default values to the syntax table.
260 add_one("PEOF", "CEOF");
261 add_one("CTLESC", "CCTL");
262 add_one("CTLVAR", "CCTL");
263 add_one("CTLENDVAR", "CCTL");
264 add_one("CTLBACKQ", "CCTL");
265 add_one("CTLBACKQ + CTLQUOTE", "CCTL");
266 add_one("CTLARI", "CCTL");
267 add_one("CTLENDARI", "CCTL");
268 add_one("CTLQUOTEMARK", "CCTL");
269 add_one("CTLQUOTEEND", "CCTL");
274 * Output the footer of a syntax table.
280 fputs("};\n", cfile
);
285 * Add entries to the syntax table.
289 add(const char *p
, const char *type
)
294 case '\t': c
= 't'; break;
295 case '\n': c
= 'n'; break;
296 case '\'': c
= '\''; break;
297 case '\\': c
= '\\'; break;
300 fprintf(cfile
, "\t[SYNBASE + '%c'] = %s,\n", c
, type
);
303 fprintf(cfile
, "\t[SYNBASE + '\\%c'] = %s,\n", c
, type
);
309 * Output character classification macros (e.g. is_digit). If digits are
310 * contiguous, we can test for them quickly.
313 static const char *macro
[] = {
314 "#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)",
315 "#define is_eof(c)\t((c) == PEOF)",
316 "#define is_alpha(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER))",
317 "#define is_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER))",
318 "#define is_in_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))",
319 "#define is_special(c)\t((is_type+SYNBASE)[(int)c] & (ISSPECL|ISDIGIT))",
320 "#define digit_val(c)\t((c) - '0')",
325 output_type_macros(void)
329 for (pp
= macro
; *pp
; pp
++)
330 fprintf(hfile
, "%s\n", *pp
);