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