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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * $Id: mksyntax.c,v 1.6 1996/09/01 10:20:56 peter Exp $ 37 */ 38 39 #ifndef lint 40 static char const copyright[] = 41 "@(#) Copyright (c) 1991, 1993\n\ 42 The Regents of the University of California. All rights reserved.\n"; 43 #endif /* not lint */ 44 45 #ifndef lint 46 static char const sccsid[] = "@(#)mksyntax.c 8.2 (Berkeley) 5/4/95"; 47 #endif /* not lint */ 48 49 /* 50 * This program creates syntax.h and syntax.c. 51 */ 52 53 #include <stdio.h> 54 #include <string.h> 55 #include "parser.h" 56 57 58 struct synclass { 59 char *name; 60 char *comment; 61 }; 62 63 /* Syntax classes */ 64 struct synclass synclass[] = { 65 { "CWORD", "character is nothing special" }, 66 { "CNL", "newline character" }, 67 { "CBACK", "a backslash character" }, 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 { NULL, NULL } 80 }; 81 82 83 /* 84 * Syntax classes for is_ functions. Warning: if you add new classes 85 * you may have to change the definition of the is_in_name macro. 86 */ 87 struct synclass is_entry[] = { 88 { "ISDIGIT", "a digit" }, 89 { "ISUPPER", "an upper case letter" }, 90 { "ISLOWER", "a lower case letter" }, 91 { "ISUNDER", "an underscore" }, 92 { "ISSPECL", "the name of a special parameter" }, 93 { NULL, NULL } 94 }; 95 96 static char writer[] = "\ 97 /*\n\ 98 * This file was generated by the mksyntax program.\n\ 99 */\n\ 100 \n"; 101 102 103 static FILE *cfile; 104 static FILE *hfile; 105 static char *syntax[513]; 106 static int base; 107 static int size; /* number of values which a char variable can have */ 108 static int nbits; /* number of bits in a character */ 109 static int digit_contig;/* true if digits are contiguous */ 110 111 static void filltable __P((char *)); 112 static void init __P((void)); 113 static void add __P((char *, char *)); 114 static void print __P((char *)); 115 static void output_type_macros __P((void)); 116 static void digit_convert __P((void)); 117 118 int 119 main(argc, argv) 120 int argc; 121 char **argv; 122 { 123 char c; 124 char d; 125 int sign; 126 int i; 127 char buf[80]; 128 int pos; 129 static char digit[] = "0123456789"; 130 131 /* Create output files */ 132 if ((cfile = fopen("syntax.c", "w")) == NULL) { 133 perror("syntax.c"); 134 exit(2); 135 } 136 if ((hfile = fopen("syntax.h", "w")) == NULL) { 137 perror("syntax.h"); 138 exit(2); 139 } 140 fputs(writer, hfile); 141 fputs(writer, cfile); 142 143 /* Determine the characteristics of chars. */ 144 c = -1; 145 if (c < 0) 146 sign = 1; 147 else 148 sign = 0; 149 for (nbits = 1 ; ; nbits++) { 150 d = (1 << nbits) - 1; 151 if (d == c) 152 break; 153 } 154 #if 0 155 printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits); 156 #endif 157 if (nbits > 9) { 158 fputs("Characters can't have more than 9 bits\n", stderr); 159 exit(2); 160 } 161 size = (1 << nbits) + 1; 162 base = 1; 163 if (sign) 164 base += 1 << (nbits - 1); 165 digit_contig = 1; 166 for (i = 0 ; i < 10 ; i++) { 167 if (digit[i] != '0' + i) 168 digit_contig = 0; 169 } 170 171 fputs("#include <sys/cdefs.h>\n", hfile); 172 fputs("#include <ctype.h>\n", hfile); 173 174 /* Generate the #define statements in the header file */ 175 fputs("/* Syntax classes */\n", hfile); 176 for (i = 0 ; synclass[i].name ; i++) { 177 sprintf(buf, "#define %s %d", synclass[i].name, i); 178 fputs(buf, hfile); 179 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07) 180 putc('\t', hfile); 181 fprintf(hfile, "/* %s */\n", synclass[i].comment); 182 } 183 putc('\n', hfile); 184 fputs("/* Syntax classes for is_ functions */\n", hfile); 185 for (i = 0 ; is_entry[i].name ; i++) { 186 sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i); 187 fputs(buf, hfile); 188 for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07) 189 putc('\t', hfile); 190 fprintf(hfile, "/* %s */\n", is_entry[i].comment); 191 } 192 putc('\n', hfile); 193 fprintf(hfile, "#define SYNBASE %d\n", base); 194 fprintf(hfile, "#define PEOF %d\n\n", -base); 195 putc('\n', hfile); 196 fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile); 197 fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile); 198 fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile); 199 fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile); 200 putc('\n', hfile); 201 output_type_macros(); /* is_digit, etc. */ 202 putc('\n', hfile); 203 204 /* Generate the syntax tables. */ 205 fputs("#include \"shell.h\"\n", cfile); 206 fputs("#include \"syntax.h\"\n\n", cfile); 207 init(); 208 fputs("/* syntax table used when not in quotes */\n", cfile); 209 add("\n", "CNL"); 210 add("\\", "CBACK"); 211 add("'", "CSQUOTE"); 212 add("\"", "CDQUOTE"); 213 add("`", "CBQUOTE"); 214 add("$", "CVAR"); 215 add("}", "CENDVAR"); 216 add("<>();&| \t", "CSPCL"); 217 print("basesyntax"); 218 init(); 219 fputs("\n/* syntax table used when in double quotes */\n", cfile); 220 add("\n", "CNL"); 221 add("\\", "CBACK"); 222 add("\"", "CENDQUOTE"); 223 add("`", "CBQUOTE"); 224 add("$", "CVAR"); 225 add("}", "CENDVAR"); 226 /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */ 227 add("!*?[=~:/-", "CCTL"); 228 print("dqsyntax"); 229 init(); 230 fputs("\n/* syntax table used when in single quotes */\n", cfile); 231 add("\n", "CNL"); 232 add("'", "CENDQUOTE"); 233 /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */ 234 add("!*?[=~:/-", "CCTL"); 235 print("sqsyntax"); 236 init(); 237 fputs("\n/* syntax table used when in arithmetic */\n", cfile); 238 add("\n", "CNL"); 239 add("\\", "CBACK"); 240 add("`", "CBQUOTE"); 241 add("'", "CSQUOTE"); 242 add("\"", "CDQUOTE"); 243 add("$", "CVAR"); 244 add("}", "CENDVAR"); 245 add("(", "CLP"); 246 add(")", "CRP"); 247 print("arisyntax"); 248 filltable("0"); 249 fputs("\n/* character classification table */\n", cfile); 250 add("0123456789", "ISDIGIT"); 251 add("abcdefghijklmnopqrstucvwxyz", "ISLOWER"); 252 add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER"); 253 add("_", "ISUNDER"); 254 add("#?$!-*@", "ISSPECL"); 255 print("is_type"); 256 if (! digit_contig) 257 digit_convert(); 258 exit(0); 259 } 260 261 262 263 /* 264 * Clear the syntax table. 265 */ 266 267 static void 268 filltable(dftval) 269 char *dftval; 270 { 271 int i; 272 273 for (i = 0 ; i < size ; i++) 274 syntax[i] = dftval; 275 } 276 277 278 /* 279 * Initialize the syntax table with default values. 280 */ 281 282 static void 283 init() 284 { 285 filltable("CWORD"); 286 syntax[0] = "CEOF"; 287 syntax[base + CTLESC] = "CCTL"; 288 syntax[base + CTLVAR] = "CCTL"; 289 syntax[base + CTLENDVAR] = "CCTL"; 290 syntax[base + CTLBACKQ] = "CCTL"; 291 syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL"; 292 syntax[base + CTLARI] = "CCTL"; 293 syntax[base + CTLENDARI] = "CCTL"; 294 } 295 296 297 /* 298 * Add entries to the syntax table. 299 */ 300 301 static void 302 add(p, type) 303 char *p, *type; 304 { 305 while (*p) 306 syntax[*p++ + base] = type; 307 } 308 309 310 311 /* 312 * Output the syntax table. 313 */ 314 315 static void 316 print(name) 317 char *name; 318 { 319 int i; 320 int col; 321 322 fprintf(hfile, "extern const char %s[];\n", name); 323 fprintf(cfile, "const char %s[%d] = {\n", name, size); 324 col = 0; 325 for (i = 0 ; i < size ; i++) { 326 if (i == 0) { 327 fputs(" ", cfile); 328 } else if ((i & 03) == 0) { 329 fputs(",\n ", cfile); 330 col = 0; 331 } else { 332 putc(',', cfile); 333 while (++col < 9 * (i & 03)) 334 putc(' ', cfile); 335 } 336 fputs(syntax[i], cfile); 337 col += strlen(syntax[i]); 338 } 339 fputs("\n};\n", cfile); 340 } 341 342 343 344 /* 345 * Output character classification macros (e.g. is_digit). If digits are 346 * contiguous, we can test for them quickly. 347 */ 348 349 static char *macro[] = { 350 "#define is_digit(c)\t((is_type+SYNBASE)[c] & ISDIGIT)", 351 "#define is_alpha(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && isalpha((unsigned char) (c)))", 352 "#define is_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalpha((unsigned char) (c))))", 353 "#define is_in_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalnum((unsigned char) (c))))", 354 "#define is_special(c)\t((is_type+SYNBASE)[c] & (ISSPECL|ISDIGIT))", 355 NULL 356 }; 357 358 static void 359 output_type_macros() 360 { 361 char **pp; 362 363 if (digit_contig) 364 macro[0] = "#define is_digit(c)\t((unsigned)((c) - '0') <= 9)"; 365 for (pp = macro ; *pp ; pp++) 366 fprintf(hfile, "%s\n", *pp); 367 if (digit_contig) 368 fputs("#define digit_val(c)\t((c) - '0')\n", hfile); 369 else 370 fputs("#define digit_val(c)\t(digit_value[c])\n", hfile); 371 } 372 373 374 375 /* 376 * Output digit conversion table (if digits are not contiguous). 377 */ 378 379 static void 380 digit_convert() 381 { 382 int maxdigit; 383 static char digit[] = "0123456789"; 384 char *p; 385 int i; 386 387 maxdigit = 0; 388 for (p = digit ; *p ; p++) 389 if (*p > maxdigit) 390 maxdigit = *p; 391 fputs("extern const char digit_value[];\n", hfile); 392 fputs("\n\nconst char digit_value[] = {\n", cfile); 393 for (i = 0 ; i <= maxdigit ; i++) { 394 for (p = digit ; *p && *p != i ; p++); 395 if (*p == '\0') 396 p = digit; 397 fprintf(cfile, " %d,\n", p - digit); 398 } 399 fputs("};\n", cfile); 400 } 401