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