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