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