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