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. 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[] = "@(#)mknodes.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 reads the nodetypes file and nodes.c.pat file. It generates 49 * the files nodes.h and nodes.c. 50 */ 51 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <errno.h> 56 #include <stdarg.h> 57 58 #define MAXTYPES 50 /* max number of node types */ 59 #define MAXFIELDS 20 /* max fields in a structure */ 60 #define BUFLEN 100 /* size of character buffers */ 61 62 /* field types */ 63 #define T_NODE 1 /* union node *field */ 64 #define T_NODELIST 2 /* struct nodelist *field */ 65 #define T_STRING 3 66 #define T_INT 4 /* int field */ 67 #define T_OTHER 5 /* other */ 68 #define T_TEMP 6 /* don't copy this field */ 69 70 71 struct field { /* a structure field */ 72 char *name; /* name of field */ 73 int type; /* type of field */ 74 char *decl; /* declaration of field */ 75 }; 76 77 78 struct str { /* struct representing a node structure */ 79 char *tag; /* structure tag */ 80 int nfields; /* number of fields in the structure */ 81 struct field field[MAXFIELDS]; /* the fields of the structure */ 82 int done; /* set if fully parsed */ 83 }; 84 85 86 static int ntypes; /* number of node types */ 87 static char *nodename[MAXTYPES]; /* names of the nodes */ 88 static struct str *nodestr[MAXTYPES]; /* type of structure used by the node */ 89 static int nstr; /* number of structures */ 90 static struct str str[MAXTYPES]; /* the structures */ 91 static struct str *curstr; /* current structure */ 92 static char line[1024]; 93 static int linno; 94 static char *linep; 95 96 static void parsenode(void); 97 static void parsefield(void); 98 static void output(char *); 99 static void outsizes(FILE *); 100 static void outfunc(FILE *, int); 101 static void indent(int, FILE *); 102 static int nextfield(char *); 103 static void skipbl(void); 104 static int readline(FILE *); 105 static void error(const char *, ...) __printf0like(1, 2) __dead2; 106 static char *savestr(const char *); 107 108 109 int 110 main(int argc, char *argv[]) 111 { 112 FILE *infp; 113 114 if (argc != 3) 115 error("usage: mknodes file"); 116 if ((infp = fopen(argv[1], "r")) == NULL) 117 error("Can't open %s: %s", argv[1], strerror(errno)); 118 while (readline(infp)) { 119 if (line[0] == ' ' || line[0] == '\t') 120 parsefield(); 121 else if (line[0] != '\0') 122 parsenode(); 123 } 124 fclose(infp); 125 output(argv[2]); 126 exit(0); 127 } 128 129 130 131 static void 132 parsenode(void) 133 { 134 char name[BUFLEN]; 135 char tag[BUFLEN]; 136 struct str *sp; 137 138 if (curstr && curstr->nfields > 0) 139 curstr->done = 1; 140 nextfield(name); 141 if (! nextfield(tag)) 142 error("Tag expected"); 143 if (*linep != '\0') 144 error("Garbage at end of line"); 145 nodename[ntypes] = savestr(name); 146 for (sp = str ; sp < str + nstr ; sp++) { 147 if (strcmp(sp->tag, tag) == 0) 148 break; 149 } 150 if (sp >= str + nstr) { 151 sp->tag = savestr(tag); 152 sp->nfields = 0; 153 curstr = sp; 154 nstr++; 155 } 156 nodestr[ntypes] = sp; 157 ntypes++; 158 } 159 160 161 static void 162 parsefield(void) 163 { 164 char name[BUFLEN]; 165 char type[BUFLEN]; 166 char decl[2 * BUFLEN]; 167 struct field *fp; 168 169 if (curstr == NULL || curstr->done) 170 error("No current structure to add field to"); 171 if (! nextfield(name)) 172 error("No field name"); 173 if (! nextfield(type)) 174 error("No field type"); 175 fp = &curstr->field[curstr->nfields]; 176 fp->name = savestr(name); 177 if (strcmp(type, "nodeptr") == 0) { 178 fp->type = T_NODE; 179 sprintf(decl, "union node *%s", name); 180 } else if (strcmp(type, "nodelist") == 0) { 181 fp->type = T_NODELIST; 182 sprintf(decl, "struct nodelist *%s", name); 183 } else if (strcmp(type, "string") == 0) { 184 fp->type = T_STRING; 185 sprintf(decl, "char *%s", name); 186 } else if (strcmp(type, "int") == 0) { 187 fp->type = T_INT; 188 sprintf(decl, "int %s", name); 189 } else if (strcmp(type, "other") == 0) { 190 fp->type = T_OTHER; 191 } else if (strcmp(type, "temp") == 0) { 192 fp->type = T_TEMP; 193 } else { 194 error("Unknown type %s", type); 195 } 196 if (fp->type == T_OTHER || fp->type == T_TEMP) { 197 skipbl(); 198 fp->decl = savestr(linep); 199 } else { 200 if (*linep) 201 error("Garbage at end of line"); 202 fp->decl = savestr(decl); 203 } 204 curstr->nfields++; 205 } 206 207 208 static const char writer[] = "\ 209 /*\n\ 210 * This file was generated by the mknodes program.\n\ 211 */\n\ 212 \n"; 213 214 static void 215 output(char *file) 216 { 217 FILE *hfile; 218 FILE *cfile; 219 FILE *patfile; 220 int i; 221 struct str *sp; 222 struct field *fp; 223 char *p; 224 225 if ((patfile = fopen(file, "r")) == NULL) 226 error("Can't open %s: %s", file, strerror(errno)); 227 if ((hfile = fopen("nodes.h", "w")) == NULL) 228 error("Can't create nodes.h: %s", strerror(errno)); 229 if ((cfile = fopen("nodes.c", "w")) == NULL) 230 error("Can't create nodes.c"); 231 fputs(writer, hfile); 232 for (i = 0 ; i < ntypes ; i++) 233 fprintf(hfile, "#define %s %d\n", nodename[i], i); 234 fputs("\n\n\n", hfile); 235 for (sp = str ; sp < &str[nstr] ; sp++) { 236 fprintf(hfile, "struct %s {\n", sp->tag); 237 for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) { 238 fprintf(hfile, " %s;\n", fp->decl); 239 } 240 fputs("};\n\n\n", hfile); 241 } 242 fputs("union node {\n", hfile); 243 fprintf(hfile, " int type;\n"); 244 for (sp = str ; sp < &str[nstr] ; sp++) { 245 fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag); 246 } 247 fputs("};\n\n\n", hfile); 248 fputs("struct nodelist {\n", hfile); 249 fputs("\tstruct nodelist *next;\n", hfile); 250 fputs("\tunion node *n;\n", hfile); 251 fputs("};\n\n\n", hfile); 252 fputs("struct funcdef;\n", hfile); 253 fputs("struct funcdef *copyfunc(union node *);\n", hfile); 254 fputs("union node *getfuncnode(struct funcdef *);\n", hfile); 255 fputs("void reffunc(struct funcdef *);\n", hfile); 256 fputs("void unreffunc(struct funcdef *);\n", hfile); 257 if (ferror(hfile)) 258 error("Can't write to nodes.h"); 259 if (fclose(hfile)) 260 error("Can't close nodes.h"); 261 262 fputs(writer, cfile); 263 while (fgets(line, sizeof line, patfile) != NULL) { 264 for (p = line ; *p == ' ' || *p == '\t' ; p++); 265 if (strcmp(p, "%SIZES\n") == 0) 266 outsizes(cfile); 267 else if (strcmp(p, "%CALCSIZE\n") == 0) 268 outfunc(cfile, 1); 269 else if (strcmp(p, "%COPY\n") == 0) 270 outfunc(cfile, 0); 271 else 272 fputs(line, cfile); 273 } 274 fclose(patfile); 275 if (ferror(cfile)) 276 error("Can't write to nodes.c"); 277 if (fclose(cfile)) 278 error("Can't close nodes.c"); 279 } 280 281 282 283 static void 284 outsizes(FILE *cfile) 285 { 286 int i; 287 288 fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes); 289 for (i = 0 ; i < ntypes ; i++) { 290 fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag); 291 } 292 fprintf(cfile, "};\n"); 293 } 294 295 296 static void 297 outfunc(FILE *cfile, int calcsize) 298 { 299 struct str *sp; 300 struct field *fp; 301 int i; 302 303 fputs(" if (n == NULL)\n", cfile); 304 if (calcsize) 305 fputs(" return;\n", cfile); 306 else 307 fputs(" return NULL;\n", cfile); 308 if (calcsize) 309 fputs(" result->blocksize += nodesize[n->type];\n", cfile); 310 else { 311 fputs(" new = state->block;\n", cfile); 312 fputs(" state->block = (char *)state->block + nodesize[n->type];\n", cfile); 313 } 314 fputs(" switch (n->type) {\n", cfile); 315 for (sp = str ; sp < &str[nstr] ; sp++) { 316 for (i = 0 ; i < ntypes ; i++) { 317 if (nodestr[i] == sp) 318 fprintf(cfile, " case %s:\n", nodename[i]); 319 } 320 for (i = sp->nfields ; --i >= 1 ; ) { 321 fp = &sp->field[i]; 322 switch (fp->type) { 323 case T_NODE: 324 if (calcsize) { 325 indent(12, cfile); 326 fprintf(cfile, "calcsize(n->%s.%s, result);\n", 327 sp->tag, fp->name); 328 } else { 329 indent(12, cfile); 330 fprintf(cfile, "new->%s.%s = copynode(n->%s.%s, state);\n", 331 sp->tag, fp->name, sp->tag, fp->name); 332 } 333 break; 334 case T_NODELIST: 335 if (calcsize) { 336 indent(12, cfile); 337 fprintf(cfile, "sizenodelist(n->%s.%s, result);\n", 338 sp->tag, fp->name); 339 } else { 340 indent(12, cfile); 341 fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s, state);\n", 342 sp->tag, fp->name, sp->tag, fp->name); 343 } 344 break; 345 case T_STRING: 346 if (calcsize) { 347 indent(12, cfile); 348 fprintf(cfile, "result->stringsize += strlen(n->%s.%s) + 1;\n", 349 sp->tag, fp->name); 350 } else { 351 indent(12, cfile); 352 fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s, state);\n", 353 sp->tag, fp->name, sp->tag, fp->name); 354 } 355 break; 356 case T_INT: 357 case T_OTHER: 358 if (! calcsize) { 359 indent(12, cfile); 360 fprintf(cfile, "new->%s.%s = n->%s.%s;\n", 361 sp->tag, fp->name, sp->tag, fp->name); 362 } 363 break; 364 } 365 } 366 indent(12, cfile); 367 fputs("break;\n", cfile); 368 } 369 fputs(" };\n", cfile); 370 if (! calcsize) 371 fputs(" new->type = n->type;\n", cfile); 372 } 373 374 375 static void 376 indent(int amount, FILE *fp) 377 { 378 while (amount >= 8) { 379 putc('\t', fp); 380 amount -= 8; 381 } 382 while (--amount >= 0) { 383 putc(' ', fp); 384 } 385 } 386 387 388 static int 389 nextfield(char *buf) 390 { 391 char *p, *q; 392 393 p = linep; 394 while (*p == ' ' || *p == '\t') 395 p++; 396 q = buf; 397 while (*p != ' ' && *p != '\t' && *p != '\0') 398 *q++ = *p++; 399 *q = '\0'; 400 linep = p; 401 return (q > buf); 402 } 403 404 405 static void 406 skipbl(void) 407 { 408 while (*linep == ' ' || *linep == '\t') 409 linep++; 410 } 411 412 413 static int 414 readline(FILE *infp) 415 { 416 char *p; 417 418 if (fgets(line, 1024, infp) == NULL) 419 return 0; 420 for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++); 421 while (p > line && (p[-1] == ' ' || p[-1] == '\t')) 422 p--; 423 *p = '\0'; 424 linep = line; 425 linno++; 426 if (p - line > BUFLEN) 427 error("Line too long"); 428 return 1; 429 } 430 431 432 433 static void 434 error(const char *msg, ...) 435 { 436 va_list va; 437 va_start(va, msg); 438 439 (void) fprintf(stderr, "line %d: ", linno); 440 (void) vfprintf(stderr, msg, va); 441 (void) fputc('\n', stderr); 442 443 va_end(va); 444 445 exit(2); 446 } 447 448 449 450 static char * 451 savestr(const char *s) 452 { 453 char *p; 454 455 if ((p = malloc(strlen(s) + 1)) == NULL) 456 error("Out of space"); 457 (void) strcpy(p, s); 458 return p; 459 } 460