mksyntax.c revision 90111
1219820Sjeff/*- 2219820Sjeff * Copyright (c) 1991, 1993 3219820Sjeff * The Regents of the University of California. All rights reserved. 4219820Sjeff * 5219820Sjeff * This code is derived from software contributed to Berkeley by 6219820Sjeff * Kenneth Almquist. 7219820Sjeff * 8219820Sjeff * Redistribution and use in source and binary forms, with or without 9219820Sjeff * modification, are permitted provided that the following conditions 10219820Sjeff * are met: 11219820Sjeff * 1. Redistributions of source code must retain the above copyright 12219820Sjeff * notice, this list of conditions and the following disclaimer. 13219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 14219820Sjeff * notice, this list of conditions and the following disclaimer in the 15219820Sjeff * documentation and/or other materials provided with the distribution. 16219820Sjeff * 3. All advertising materials mentioning features or use of this software 17219820Sjeff * must display the following acknowledgement: 18219820Sjeff * This product includes software developed by the University of 19219820Sjeff * California, Berkeley and its contributors. 20219820Sjeff * 4. Neither the name of the University nor the names of its contributors 21219820Sjeff * may be used to endorse or promote products derived from this software 22219820Sjeff * without specific prior written permission. 23219820Sjeff * 24219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27219820Sjeff * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34219820Sjeff * SUCH DAMAGE. 35219820Sjeff */ 36219820Sjeff 37219820Sjeff#ifndef lint 38219820Sjeffstatic char const copyright[] = 39219820Sjeff"@(#) Copyright (c) 1991, 1993\n\ 40219820Sjeff The Regents of the University of California. All rights reserved.\n"; 41219820Sjeff#endif /* not lint */ 42219820Sjeff 43219820Sjeff#ifndef lint 44219820Sjeff#if 0 45219820Sjeffstatic char sccsid[] = "@(#)mksyntax.c 8.2 (Berkeley) 5/4/95"; 46219820Sjeff#endif 47219820Sjeffstatic const char rcsid[] = 48219820Sjeff "$FreeBSD: head/bin/sh/mksyntax.c 90111 2002-02-02 06:50:57Z imp $"; 49219820Sjeff#endif /* not lint */ 50219820Sjeff 51219820Sjeff/* 52219820Sjeff * This program creates syntax.h and syntax.c. 53219820Sjeff */ 54219820Sjeff 55219820Sjeff#include <stdio.h> 56219820Sjeff#include <stdlib.h> 57219820Sjeff#include <string.h> 58219820Sjeff#include "parser.h" 59219820Sjeff 60219820Sjeff 61219820Sjeffstruct synclass { 62219820Sjeff char *name; 63219820Sjeff char *comment; 64219820Sjeff}; 65219820Sjeff 66219820Sjeff/* Syntax classes */ 67219820Sjeffstruct synclass synclass[] = { 68219820Sjeff { "CWORD", "character is nothing special" }, 69219820Sjeff { "CNL", "newline character" }, 70219820Sjeff { "CBACK", "a backslash character" }, 71219820Sjeff { "CSQUOTE", "single quote" }, 72219820Sjeff { "CDQUOTE", "double quote" }, 73219820Sjeff { "CENDQUOTE", "a terminating quote" }, 74219820Sjeff { "CBQUOTE", "backwards single quote" }, 75219820Sjeff { "CVAR", "a dollar sign" }, 76219820Sjeff { "CENDVAR", "a '}' character" }, 77219820Sjeff { "CLP", "a left paren in arithmetic" }, 78219820Sjeff { "CRP", "a right paren in arithmetic" }, 79219820Sjeff { "CEOF", "end of file" }, 80219820Sjeff { "CCTL", "like CWORD, except it must be escaped" }, 81219820Sjeff { "CSPCL", "these terminate a word" }, 82219820Sjeff { NULL, NULL } 83219820Sjeff}; 84219820Sjeff 85219820Sjeff 86219820Sjeff/* 87219820Sjeff * Syntax classes for is_ functions. Warning: if you add new classes 88219820Sjeff * you may have to change the definition of the is_in_name macro. 89219820Sjeff */ 90219820Sjeffstruct synclass is_entry[] = { 91219820Sjeff { "ISDIGIT", "a digit" }, 92219820Sjeff { "ISUPPER", "an upper case letter" }, 93219820Sjeff { "ISLOWER", "a lower case letter" }, 94219820Sjeff { "ISUNDER", "an underscore" }, 95219820Sjeff { "ISSPECL", "the name of a special parameter" }, 96219820Sjeff { NULL, NULL } 97219820Sjeff}; 98219820Sjeff 99219820Sjeffstatic char writer[] = "\ 100219820Sjeff/*\n\ 101219820Sjeff * This file was generated by the mksyntax program.\n\ 102219820Sjeff */\n\ 103219820Sjeff\n"; 104219820Sjeff 105219820Sjeff 106219820Sjeffstatic FILE *cfile; 107219820Sjeffstatic FILE *hfile; 108219820Sjeffstatic char *syntax[513]; 109219820Sjeffstatic int base; 110219820Sjeffstatic int size; /* number of values which a char variable can have */ 111219820Sjeffstatic int nbits; /* number of bits in a character */ 112219820Sjeffstatic int digit_contig;/* true if digits are contiguous */ 113219820Sjeff 114219820Sjeffstatic void filltable(char *); 115219820Sjeffstatic void init(void); 116219820Sjeffstatic void add(char *, char *); 117219820Sjeffstatic void print(char *); 118219820Sjeffstatic void output_type_macros(void); 119219820Sjeffstatic void digit_convert(void); 120219820Sjeff 121219820Sjeffint 122219820Sjeffmain(int argc __unused, char **argv __unused) 123219820Sjeff{ 124219820Sjeff char c; 125219820Sjeff char d; 126219820Sjeff int sign; 127219820Sjeff int i; 128219820Sjeff char buf[80]; 129219820Sjeff int pos; 130219820Sjeff static char digit[] = "0123456789"; 131219820Sjeff 132219820Sjeff /* Create output files */ 133219820Sjeff if ((cfile = fopen("syntax.c", "w")) == NULL) { 134219820Sjeff perror("syntax.c"); 135219820Sjeff exit(2); 136219820Sjeff } 137219820Sjeff if ((hfile = fopen("syntax.h", "w")) == NULL) { 138219820Sjeff perror("syntax.h"); 139219820Sjeff exit(2); 140219820Sjeff } 141219820Sjeff fputs(writer, hfile); 142219820Sjeff fputs(writer, cfile); 143219820Sjeff 144219820Sjeff /* Determine the characteristics of chars. */ 145219820Sjeff c = -1; 146219820Sjeff if (c < 0) 147219820Sjeff sign = 1; 148219820Sjeff else 149219820Sjeff sign = 0; 150219820Sjeff for (nbits = 1 ; ; nbits++) { 151219820Sjeff d = (1 << nbits) - 1; 152219820Sjeff if (d == c) 153219820Sjeff break; 154219820Sjeff } 155219820Sjeff#if 0 156219820Sjeff printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits); 157219820Sjeff#endif 158219820Sjeff if (nbits > 9) { 159219820Sjeff fputs("Characters can't have more than 9 bits\n", stderr); 160219820Sjeff exit(2); 161219820Sjeff } 162219820Sjeff size = (1 << nbits) + 1; 163219820Sjeff base = 1; 164219820Sjeff if (sign) 165219820Sjeff base += 1 << (nbits - 1); 166219820Sjeff digit_contig = 1; 167219820Sjeff for (i = 0 ; i < 10 ; i++) { 168219820Sjeff if (digit[i] != '0' + i) 169219820Sjeff digit_contig = 0; 170219820Sjeff } 171219820Sjeff 172219820Sjeff fputs("#include <sys/cdefs.h>\n", hfile); 173219820Sjeff fputs("#include <ctype.h>\n", hfile); 174219820Sjeff 175219820Sjeff /* Generate the #define statements in the header file */ 176219820Sjeff fputs("/* Syntax classes */\n", hfile); 177219820Sjeff for (i = 0 ; synclass[i].name ; i++) { 178219820Sjeff sprintf(buf, "#define %s %d", synclass[i].name, i); 179219820Sjeff fputs(buf, hfile); 180219820Sjeff for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07) 181219820Sjeff putc('\t', hfile); 182219820Sjeff fprintf(hfile, "/* %s */\n", synclass[i].comment); 183219820Sjeff } 184219820Sjeff putc('\n', hfile); 185219820Sjeff fputs("/* Syntax classes for is_ functions */\n", hfile); 186219820Sjeff for (i = 0 ; is_entry[i].name ; i++) { 187219820Sjeff sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i); 188219820Sjeff fputs(buf, hfile); 189219820Sjeff for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07) 190219820Sjeff putc('\t', hfile); 191219820Sjeff fprintf(hfile, "/* %s */\n", is_entry[i].comment); 192219820Sjeff } 193219820Sjeff putc('\n', hfile); 194219820Sjeff fprintf(hfile, "#define SYNBASE %d\n", base); 195219820Sjeff fprintf(hfile, "#define PEOF %d\n\n", -base); 196219820Sjeff putc('\n', hfile); 197219820Sjeff fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile); 198219820Sjeff fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile); 199219820Sjeff fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile); 200219820Sjeff fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile); 201219820Sjeff putc('\n', hfile); 202219820Sjeff output_type_macros(); /* is_digit, etc. */ 203219820Sjeff putc('\n', hfile); 204219820Sjeff 205219820Sjeff /* Generate the syntax tables. */ 206219820Sjeff fputs("#include \"shell.h\"\n", cfile); 207219820Sjeff fputs("#include \"syntax.h\"\n\n", cfile); 208219820Sjeff init(); 209219820Sjeff fputs("/* syntax table used when not in quotes */\n", cfile); 210219820Sjeff add("\n", "CNL"); 211219820Sjeff add("\\", "CBACK"); 212219820Sjeff add("'", "CSQUOTE"); 213219820Sjeff add("\"", "CDQUOTE"); 214219820Sjeff add("`", "CBQUOTE"); 215219820Sjeff add("$", "CVAR"); 216219820Sjeff add("}", "CENDVAR"); 217219820Sjeff add("<>();&| \t", "CSPCL"); 218219820Sjeff print("basesyntax"); 219219820Sjeff init(); 220219820Sjeff fputs("\n/* syntax table used when in double quotes */\n", cfile); 221219820Sjeff add("\n", "CNL"); 222219820Sjeff add("\\", "CBACK"); 223219820Sjeff add("\"", "CENDQUOTE"); 224219820Sjeff add("`", "CBQUOTE"); 225219820Sjeff add("$", "CVAR"); 226219820Sjeff add("}", "CENDVAR"); 227219820Sjeff /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */ 228219820Sjeff add("!*?[=~:/-", "CCTL"); 229219820Sjeff print("dqsyntax"); 230219820Sjeff init(); 231219820Sjeff fputs("\n/* syntax table used when in single quotes */\n", cfile); 232219820Sjeff add("\n", "CNL"); 233219820Sjeff add("'", "CENDQUOTE"); 234219820Sjeff /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */ 235219820Sjeff add("!*?[=~:/-", "CCTL"); 236219820Sjeff print("sqsyntax"); 237 init(); 238 fputs("\n/* syntax table used when in arithmetic */\n", cfile); 239 add("\n", "CNL"); 240 add("\\", "CBACK"); 241 add("`", "CBQUOTE"); 242 add("'", "CSQUOTE"); 243 add("\"", "CDQUOTE"); 244 add("$", "CVAR"); 245 add("}", "CENDVAR"); 246 add("(", "CLP"); 247 add(")", "CRP"); 248 print("arisyntax"); 249 filltable("0"); 250 fputs("\n/* character classification table */\n", cfile); 251 add("0123456789", "ISDIGIT"); 252 add("abcdefghijklmnopqrstucvwxyz", "ISLOWER"); 253 add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER"); 254 add("_", "ISUNDER"); 255 add("#?$!-*@", "ISSPECL"); 256 print("is_type"); 257 if (! digit_contig) 258 digit_convert(); 259 exit(0); 260} 261 262 263 264/* 265 * Clear the syntax table. 266 */ 267 268static void 269filltable(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 282static void 283init(void) 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 syntax[base + CTLQUOTEMARK] = "CCTL"; 295} 296 297 298/* 299 * Add entries to the syntax table. 300 */ 301 302static void 303add(char *p, char *type) 304{ 305 while (*p) 306 syntax[*p++ + base] = type; 307} 308 309 310 311/* 312 * Output the syntax table. 313 */ 314 315static void 316print(char *name) 317{ 318 int i; 319 int col; 320 321 fprintf(hfile, "extern const char %s[];\n", name); 322 fprintf(cfile, "const char %s[%d] = {\n", name, size); 323 col = 0; 324 for (i = 0 ; i < size ; i++) { 325 if (i == 0) { 326 fputs(" ", cfile); 327 } else if ((i & 03) == 0) { 328 fputs(",\n ", cfile); 329 col = 0; 330 } else { 331 putc(',', cfile); 332 while (++col < 9 * (i & 03)) 333 putc(' ', cfile); 334 } 335 fputs(syntax[i], cfile); 336 col += strlen(syntax[i]); 337 } 338 fputs("\n};\n", cfile); 339} 340 341 342 343/* 344 * Output character classification macros (e.g. is_digit). If digits are 345 * contiguous, we can test for them quickly. 346 */ 347 348static char *macro[] = { 349 "#define is_digit(c)\t((is_type+SYNBASE)[c] & ISDIGIT)", 350 "#define is_alpha(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && isalpha((unsigned char) (c)))", 351 "#define is_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalpha((unsigned char) (c))))", 352 "#define is_in_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLQUOTEMARK) && ((c) == '_' || isalnum((unsigned char) (c))))", 353 "#define is_special(c)\t((is_type+SYNBASE)[c] & (ISSPECL|ISDIGIT))", 354 NULL 355}; 356 357static void 358output_type_macros(void) 359{ 360 char **pp; 361 362 if (digit_contig) 363 macro[0] = "#define is_digit(c)\t((unsigned)((c) - '0') <= 9)"; 364 for (pp = macro ; *pp ; pp++) 365 fprintf(hfile, "%s\n", *pp); 366 if (digit_contig) 367 fputs("#define digit_val(c)\t((c) - '0')\n", hfile); 368 else 369 fputs("#define digit_val(c)\t(digit_value[c])\n", hfile); 370} 371 372 373 374/* 375 * Output digit conversion table (if digits are not contiguous). 376 */ 377 378static void 379digit_convert(void) 380{ 381 int maxdigit; 382 static char digit[] = "0123456789"; 383 char *p; 384 int i; 385 386 maxdigit = 0; 387 for (p = digit ; *p ; p++) 388 if (*p > maxdigit) 389 maxdigit = *p; 390 fputs("extern const char digit_value[];\n", hfile); 391 fputs("\n\nconst char digit_value[] = {\n", cfile); 392 for (i = 0 ; i <= maxdigit ; i++) { 393 for (p = digit ; *p && *p != i ; p++); 394 if (*p == '\0') 395 p = digit; 396 fprintf(cfile, " %d,\n", p - digit); 397 } 398 fputs("};\n", cfile); 399} 400