mknodes.c revision 17987
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 3. All advertising materials mentioning features or use of this software 171556Srgrimes * must display the following acknowledgement: 181556Srgrimes * This product includes software developed by the University of 191556Srgrimes * California, Berkeley and its contributors. 201556Srgrimes * 4. Neither the name of the University nor the names of its contributors 211556Srgrimes * may be used to endorse or promote products derived from this software 221556Srgrimes * without specific prior written permission. 231556Srgrimes * 241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3490143Smarkm * SUCH DAMAGE. 3590143Smarkm * 3690143Smarkm * $Id: mknodes.c,v 1.2 1994/09/24 02:57:55 davidg Exp $ 3790143Smarkm */ 3890143Smarkm 391556Srgrimes#ifndef lint 4036049Scharnierstatic char copyright[] = 4136049Scharnier"@(#) Copyright (c) 1991, 1993\n\ 4290143Smarkm The Regents of the University of California. All rights reserved.\n"; 431556Srgrimes#endif /* not lint */ 4441580Sbde 451556Srgrimes#ifndef lint 461556Srgrimesstatic char sccsid[] = "@(#)mknodes.c 8.2 (Berkeley) 5/4/95"; 4790143Smarkm#endif /* not lint */ 4841580Sbde 4996053Sjmallett/* 501556Srgrimes * This program reads the nodetypes file and nodes.c.pat file. It generates 511556Srgrimes * the files nodes.h and nodes.c. 521556Srgrimes */ 5337027Sjkoshy 541556Srgrimes#include <stdio.h> 5590143Smarkm#include <stdlib.h> 561556Srgrimes#include <string.h> 571556Srgrimes#if __STDC__ 5890110Simp#include <stdarg.h> 5990110Simp#else 601556Srgrimes#include <varargs.h> 611556Srgrimes#endif 621556Srgrimes 631556Srgrimes 641556Srgrimes#define MAXTYPES 50 /* max number of node types */ 651556Srgrimes#define MAXFIELDS 20 /* max fields in a structure */ 6690110Simp#define BUFLEN 100 /* size of character buffers */ 671556Srgrimes 6896053Sjmallett/* field types */ 6996053Sjmallett#define T_NODE 1 /* union node *field */ 7090143Smarkm#define T_NODELIST 2 /* struct nodelist *field */ 711556Srgrimes#define T_STRING 3 7237027Sjkoshy#define T_INT 4 /* int field */ 731556Srgrimes#define T_OTHER 5 /* other */ 7437027Sjkoshy#define T_TEMP 6 /* don't copy this field */ 7537027Sjkoshy 7637027Sjkoshy 7796053Sjmallettstruct field { /* a structure field */ 7896053Sjmallett char *name; /* name of field */ 7996053Sjmallett int type; /* type of field */ 8096053Sjmallett char *decl; /* declaration of field */ 8196053Sjmallett}; 8237027Sjkoshy 8337027Sjkoshy 8497845Sjmallettstruct str { /* struct representing a node structure */ 8597845Sjmallett char *tag; /* structure tag */ 861556Srgrimes int nfields; /* number of fields in the structure */ 871556Srgrimes struct field field[MAXFIELDS]; /* the fields of the structure */ 881556Srgrimes int done; /* set if fully parsed */ 8997845Sjmallett}; 9097845Sjmallett 911556Srgrimes 9296053Sjmallettstatic int ntypes; /* number of node types */ 9389575Smikehstatic char *nodename[MAXTYPES]; /* names of the nodes */ 9489575Smikehstatic struct str *nodestr[MAXTYPES]; /* type of structure used by the node */ 9597845Sjmallettstatic int nstr; /* number of structures */ 961556Srgrimesstatic struct str str[MAXTYPES]; /* the structures */ 9796053Sjmallettstatic struct str *curstr; /* current structure */ 9889575Smikehstatic FILE *infp = stdin; 991556Srgrimesstatic char line[1024]; 1006971Sdgstatic int linno; 10189575Smikehstatic char *linep; 1026971Sdg 1031556Srgrimesstatic void parsenode __P((void)); 1041556Srgrimesstatic void parsefield __P((void)); 1051556Srgrimesstatic void output __P((char *)); 1061556Srgrimesstatic void outsizes __P((FILE *)); 1071556Srgrimesstatic void outfunc __P((FILE *, int)); 10890110Simpstatic void indent __P((int, FILE *)); 1091556Srgrimesstatic int nextfield __P((char *)); 1101556Srgrimesstatic void skipbl __P((void)); 1111556Srgrimesstatic int readline __P((void)); 1121556Srgrimesstatic void error __P((const char *, ...)); 1131556Srgrimesstatic char *savestr __P((const char *)); 1141556Srgrimes 11590143Smarkm 11690143Smarkmint 1171556Srgrimesmain(argc, argv) 11890143Smarkm int argc; 1191556Srgrimes char **argv; 1201556Srgrimes{ 12190143Smarkm if (argc != 3) 1221556Srgrimes error("usage: mknodes file\n"); 1231556Srgrimes if ((infp = fopen(argv[1], "r")) == NULL) 1241556Srgrimes error("Can't open %s", argv[1]); 1251556Srgrimes while (readline()) { 1261556Srgrimes if (line[0] == ' ' || line[0] == '\t') 1271556Srgrimes parsefield(); 1281556Srgrimes else if (line[0] != '\0') 1291556Srgrimes parsenode(); 13090143Smarkm } 13190143Smarkm output(argv[2]); 1321556Srgrimes exit(0); 1331556Srgrimes} 13490143Smarkm 1351556Srgrimes 13690143Smarkm 1371556Srgrimesstatic void 1381556Srgrimesparsenode() 1391556Srgrimes{ 1401556Srgrimes char name[BUFLEN]; 141 char tag[BUFLEN]; 142 struct str *sp; 143 144 if (curstr && curstr->nfields > 0) 145 curstr->done = 1; 146 nextfield(name); 147 if (! nextfield(tag)) 148 error("Tag expected"); 149 if (*linep != '\0') 150 error("Garbage at end of line"); 151 nodename[ntypes] = savestr(name); 152 for (sp = str ; sp < str + nstr ; sp++) { 153 if (strcmp(sp->tag, tag) == 0) 154 break; 155 } 156 if (sp >= str + nstr) { 157 sp->tag = savestr(tag); 158 sp->nfields = 0; 159 curstr = sp; 160 nstr++; 161 } 162 nodestr[ntypes] = sp; 163 ntypes++; 164} 165 166 167static void 168parsefield() 169{ 170 char name[BUFLEN]; 171 char type[BUFLEN]; 172 char decl[2 * BUFLEN]; 173 struct field *fp; 174 175 if (curstr == NULL || curstr->done) 176 error("No current structure to add field to"); 177 if (! nextfield(name)) 178 error("No field name"); 179 if (! nextfield(type)) 180 error("No field type"); 181 fp = &curstr->field[curstr->nfields]; 182 fp->name = savestr(name); 183 if (strcmp(type, "nodeptr") == 0) { 184 fp->type = T_NODE; 185 sprintf(decl, "union node *%s", name); 186 } else if (strcmp(type, "nodelist") == 0) { 187 fp->type = T_NODELIST; 188 sprintf(decl, "struct nodelist *%s", name); 189 } else if (strcmp(type, "string") == 0) { 190 fp->type = T_STRING; 191 sprintf(decl, "char *%s", name); 192 } else if (strcmp(type, "int") == 0) { 193 fp->type = T_INT; 194 sprintf(decl, "int %s", name); 195 } else if (strcmp(type, "other") == 0) { 196 fp->type = T_OTHER; 197 } else if (strcmp(type, "temp") == 0) { 198 fp->type = T_TEMP; 199 } else { 200 error("Unknown type %s", type); 201 } 202 if (fp->type == T_OTHER || fp->type == T_TEMP) { 203 skipbl(); 204 fp->decl = savestr(linep); 205 } else { 206 if (*linep) 207 error("Garbage at end of line"); 208 fp->decl = savestr(decl); 209 } 210 curstr->nfields++; 211} 212 213 214char writer[] = "\ 215/*\n\ 216 * This file was generated by the mknodes program.\n\ 217 */\n\ 218\n"; 219 220static void 221output(file) 222 char *file; 223{ 224 FILE *hfile; 225 FILE *cfile; 226 FILE *patfile; 227 int i; 228 struct str *sp; 229 struct field *fp; 230 char *p; 231 232 if ((patfile = fopen(file, "r")) == NULL) 233 error("Can't open %s", file); 234 if ((hfile = fopen("nodes.h", "w")) == NULL) 235 error("Can't create nodes.h"); 236 if ((cfile = fopen("nodes.c", "w")) == NULL) 237 error("Can't create nodes.c"); 238 fputs(writer, hfile); 239 for (i = 0 ; i < ntypes ; i++) 240 fprintf(hfile, "#define %s %d\n", nodename[i], i); 241 fputs("\n\n\n", hfile); 242 for (sp = str ; sp < &str[nstr] ; sp++) { 243 fprintf(hfile, "struct %s {\n", sp->tag); 244 for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) { 245 fprintf(hfile, " %s;\n", fp->decl); 246 } 247 fputs("};\n\n\n", hfile); 248 } 249 fputs("union node {\n", hfile); 250 fprintf(hfile, " int type;\n"); 251 for (sp = str ; sp < &str[nstr] ; sp++) { 252 fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag); 253 } 254 fputs("};\n\n\n", hfile); 255 fputs("struct nodelist {\n", hfile); 256 fputs("\tstruct nodelist *next;\n", hfile); 257 fputs("\tunion node *n;\n", hfile); 258 fputs("};\n\n\n", hfile); 259 fputs("#ifdef __STDC__\n", hfile); 260 fputs("union node *copyfunc(union node *);\n", hfile); 261 fputs("void freefunc(union node *);\n", hfile); 262 fputs("#else\n", hfile); 263 fputs("union node *copyfunc();\n", hfile); 264 fputs("void freefunc();\n", hfile); 265 fputs("#endif\n", hfile); 266 267 fputs(writer, cfile); 268 while (fgets(line, sizeof line, patfile) != NULL) { 269 for (p = line ; *p == ' ' || *p == '\t' ; p++); 270 if (strcmp(p, "%SIZES\n") == 0) 271 outsizes(cfile); 272 else if (strcmp(p, "%CALCSIZE\n") == 0) 273 outfunc(cfile, 1); 274 else if (strcmp(p, "%COPY\n") == 0) 275 outfunc(cfile, 0); 276 else 277 fputs(line, cfile); 278 } 279} 280 281 282 283static void 284outsizes(cfile) 285 FILE *cfile; 286{ 287 int i; 288 289 fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes); 290 for (i = 0 ; i < ntypes ; i++) { 291 fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag); 292 } 293 fprintf(cfile, "};\n"); 294} 295 296 297static void 298outfunc(cfile, calcsize) 299 FILE *cfile; 300 int calcsize; 301{ 302 struct str *sp; 303 struct field *fp; 304 int i; 305 306 fputs(" if (n == NULL)\n", cfile); 307 if (calcsize) 308 fputs(" return;\n", cfile); 309 else 310 fputs(" return NULL;\n", cfile); 311 if (calcsize) 312 fputs(" funcblocksize += nodesize[n->type];\n", cfile); 313 else { 314 fputs(" new = funcblock;\n", cfile); 315 fputs(" funcblock += nodesize[n->type];\n", cfile); 316 } 317 fputs(" switch (n->type) {\n", cfile); 318 for (sp = str ; sp < &str[nstr] ; sp++) { 319 for (i = 0 ; i < ntypes ; i++) { 320 if (nodestr[i] == sp) 321 fprintf(cfile, " case %s:\n", nodename[i]); 322 } 323 for (i = sp->nfields ; --i >= 1 ; ) { 324 fp = &sp->field[i]; 325 switch (fp->type) { 326 case T_NODE: 327 if (calcsize) { 328 indent(12, cfile); 329 fprintf(cfile, "calcsize(n->%s.%s);\n", 330 sp->tag, fp->name); 331 } else { 332 indent(12, cfile); 333 fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n", 334 sp->tag, fp->name, sp->tag, fp->name); 335 } 336 break; 337 case T_NODELIST: 338 if (calcsize) { 339 indent(12, cfile); 340 fprintf(cfile, "sizenodelist(n->%s.%s);\n", 341 sp->tag, fp->name); 342 } else { 343 indent(12, cfile); 344 fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n", 345 sp->tag, fp->name, sp->tag, fp->name); 346 } 347 break; 348 case T_STRING: 349 if (calcsize) { 350 indent(12, cfile); 351 fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n", 352 sp->tag, fp->name); 353 } else { 354 indent(12, cfile); 355 fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n", 356 sp->tag, fp->name, sp->tag, fp->name); 357 } 358 break; 359 case T_INT: 360 case T_OTHER: 361 if (! calcsize) { 362 indent(12, cfile); 363 fprintf(cfile, "new->%s.%s = n->%s.%s;\n", 364 sp->tag, fp->name, sp->tag, fp->name); 365 } 366 break; 367 } 368 } 369 indent(12, cfile); 370 fputs("break;\n", cfile); 371 } 372 fputs(" };\n", cfile); 373 if (! calcsize) 374 fputs(" new->type = n->type;\n", cfile); 375} 376 377 378static void 379indent(amount, fp) 380 int amount; 381 FILE *fp; 382{ 383 while (amount >= 8) { 384 putc('\t', fp); 385 amount -= 8; 386 } 387 while (--amount >= 0) { 388 putc(' ', fp); 389 } 390} 391 392 393static int 394nextfield(buf) 395 char *buf; 396{ 397 register char *p, *q; 398 399 p = linep; 400 while (*p == ' ' || *p == '\t') 401 p++; 402 q = buf; 403 while (*p != ' ' && *p != '\t' && *p != '\0') 404 *q++ = *p++; 405 *q = '\0'; 406 linep = p; 407 return (q > buf); 408} 409 410 411static void 412skipbl() 413{ 414 while (*linep == ' ' || *linep == '\t') 415 linep++; 416} 417 418 419static int 420readline() 421{ 422 register char *p; 423 424 if (fgets(line, 1024, infp) == NULL) 425 return 0; 426 for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++); 427 while (p > line && (p[-1] == ' ' || p[-1] == '\t')) 428 p--; 429 *p = '\0'; 430 linep = line; 431 linno++; 432 if (p - line > BUFLEN) 433 error("Line too long"); 434 return 1; 435} 436 437 438 439static void 440#if __STDC__ 441error(const char *msg, ...) 442#else 443error(va_alist) 444 va_dcl 445#endif 446{ 447 va_list va; 448#if __STDC__ 449 va_start(va, msg); 450#else 451 char *msg; 452 va_start(va); 453 msg = va_arg(va, char *); 454#endif 455 456 (void) fprintf(stderr, "line %d: ", linno); 457 (void) vfprintf(stderr, msg, va); 458 (void) fputc('\n', stderr); 459 460 va_end(va); 461 462 exit(2); 463} 464 465 466 467static char * 468savestr(s) 469 const char *s; 470{ 471 register char *p; 472 473 if ((p = malloc(strlen(s) + 1)) == NULL) 474 error("Out of space"); 475 (void) strcpy(p, s); 476 return p; 477} 478