mknodes.c revision 90111
1129471Spjd/*- 2129471Spjd * Copyright (c) 1991, 1993 3129471Spjd * The Regents of the University of California. All rights reserved. 4129471Spjd * 5129471Spjd * This code is derived from software contributed to Berkeley by 6129471Spjd * Kenneth Almquist. 7129471Spjd * 8129471Spjd * Redistribution and use in source and binary forms, with or without 9129471Spjd * modification, are permitted provided that the following conditions 10129471Spjd * are met: 11129471Spjd * 1. Redistributions of source code must retain the above copyright 12129471Spjd * notice, this list of conditions and the following disclaimer. 13129471Spjd * 2. Redistributions in binary form must reproduce the above copyright 14129471Spjd * notice, this list of conditions and the following disclaimer in the 15129471Spjd * documentation and/or other materials provided with the distribution. 16129471Spjd * 3. All advertising materials mentioning features or use of this software 17129471Spjd * must display the following acknowledgement: 18129471Spjd * This product includes software developed by the University of 19129471Spjd * California, Berkeley and its contributors. 20129471Spjd * 4. Neither the name of the University nor the names of its contributors 21129471Spjd * may be used to endorse or promote products derived from this software 22129471Spjd * without specific prior written permission. 23129471Spjd * 24129471Spjd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25129471Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26129471Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27129471Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28129471Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29129471Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30129471Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31129471Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32129471Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33129471Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34129471Spjd * SUCH DAMAGE. 35129471Spjd */ 36129471Spjd 37129471Spjd#ifndef lint 38129471Spjdstatic char const copyright[] = 39129471Spjd"@(#) Copyright (c) 1991, 1993\n\ 40129471Spjd The Regents of the University of California. All rights reserved.\n"; 41129471Spjd#endif /* not lint */ 42129471Spjd 43129471Spjd#ifndef lint 44129471Spjd#if 0 45129471Spjdstatic char sccsid[] = "@(#)mknodes.c 8.2 (Berkeley) 5/4/95"; 46129471Spjd#endif 47129471Spjdstatic const char rcsid[] = 48129471Spjd "$FreeBSD: head/bin/sh/mknodes.c 90111 2002-02-02 06:50:57Z imp $"; 49129471Spjd#endif /* not lint */ 50129471Spjd 51129471Spjd/* 52129471Spjd * This program reads the nodetypes file and nodes.c.pat file. It generates 53129471Spjd * the files nodes.h and nodes.c. 54129471Spjd */ 55129471Spjd 56129471Spjd#include <stdio.h> 57129471Spjd#include <stdlib.h> 58129471Spjd#include <string.h> 59129471Spjd#include <errno.h> 60129471Spjd#include <stdarg.h> 61129471Spjd 62129471Spjd#define MAXTYPES 50 /* max number of node types */ 63129471Spjd#define MAXFIELDS 20 /* max fields in a structure */ 64129471Spjd#define BUFLEN 100 /* size of character buffers */ 65129471Spjd 66129471Spjd/* field types */ 67129471Spjd#define T_NODE 1 /* union node *field */ 68129471Spjd#define T_NODELIST 2 /* struct nodelist *field */ 69129471Spjd#define T_STRING 3 70129471Spjd#define T_INT 4 /* int field */ 71129471Spjd#define T_OTHER 5 /* other */ 72129471Spjd#define T_TEMP 6 /* don't copy this field */ 73129471Spjd 74129471Spjd 75132381Spjdstruct field { /* a structure field */ 76129471Spjd char *name; /* name of field */ 77129471Spjd int type; /* type of field */ 78129471Spjd char *decl; /* declaration of field */ 79129471Spjd}; 80129471Spjd 81132381Spjd 82129471Spjdstruct str { /* struct representing a node structure */ 83129471Spjd char *tag; /* structure tag */ 84129471Spjd int nfields; /* number of fields in the structure */ 85129471Spjd struct field field[MAXFIELDS]; /* the fields of the structure */ 86129471Spjd int done; /* set if fully parsed */ 87129471Spjd}; 88129471Spjd 89129471Spjd 90132381Spjdstatic int ntypes; /* number of node types */ 91129471Spjdstatic char *nodename[MAXTYPES]; /* names of the nodes */ 92129471Spjdstatic struct str *nodestr[MAXTYPES]; /* type of structure used by the node */ 93129471Spjdstatic int nstr; /* number of structures */ 94132381Spjdstatic struct str str[MAXTYPES]; /* the structures */ 95129471Spjdstatic struct str *curstr; /* current structure */ 96129471Spjdstatic FILE *infp; 97129471Spjdstatic char line[1024]; 98129471Spjdstatic int linno; 99129471Spjdstatic char *linep; 100132381Spjd 101129471Spjdstatic void parsenode(void); 102129471Spjdstatic void parsefield(void); 103129471Spjdstatic void output(char *); 104129471Spjdstatic void outsizes(FILE *); 105129471Spjdstatic void outfunc(FILE *, int); 106129471Spjdstatic void indent(int, FILE *); 107129471Spjdstatic int nextfield(char *); 108129471Spjdstatic void skipbl(void); 109129471Spjdstatic int readline(void); 110129471Spjdstatic void error(const char *, ...) __printf0like(1, 2); 111129471Spjdstatic char *savestr(const char *); 112129471Spjd 113129471Spjd 114129471Spjdint 115129471Spjdmain(int argc, char *argv[]) 116129471Spjd{ 117129471Spjd if (argc != 3) 118129471Spjd error("usage: mknodes file"); 119129471Spjd infp = stdin; 120129471Spjd if ((infp = fopen(argv[1], "r")) == NULL) 121129471Spjd error("Can't open %s: %s", argv[1], strerror(errno)); 122129471Spjd while (readline()) { 123129471Spjd if (line[0] == ' ' || line[0] == '\t') 124132381Spjd parsefield(); 125129471Spjd else if (line[0] != '\0') 126132381Spjd parsenode(); 127129471Spjd } 128129471Spjd output(argv[2]); 129129471Spjd exit(0); 130130191Spjd} 131129471Spjd 132129471Spjd 133129471Spjd 134129471Spjdstatic void 135129471Spjdparsenode(void) 136129471Spjd{ 137129471Spjd char name[BUFLEN]; 138129471Spjd char tag[BUFLEN]; 139132381Spjd struct str *sp; 140132381Spjd 141132381Spjd if (curstr && curstr->nfields > 0) 142132381Spjd curstr->done = 1; 143132381Spjd nextfield(name); 144132381Spjd if (! nextfield(tag)) 145132381Spjd error("Tag expected"); 146132381Spjd if (*linep != '\0') 147132381Spjd error("Garbage at end of line"); 148132381Spjd nodename[ntypes] = savestr(name); 149132381Spjd for (sp = str ; sp < str + nstr ; sp++) { 150132381Spjd if (strcmp(sp->tag, tag) == 0) 151132381Spjd break; 152132381Spjd } 153132381Spjd if (sp >= str + nstr) { 154132381Spjd sp->tag = savestr(tag); 155132381Spjd sp->nfields = 0; 156132381Spjd curstr = sp; 157130191Spjd nstr++; 158130191Spjd } 159130191Spjd nodestr[ntypes] = sp; 160130191Spjd ntypes++; 161130191Spjd} 162130191Spjd 163130191Spjd 164130191Spjdstatic void 165129471Spjdparsefield(void) 166130193Spjd{ 167129471Spjd char name[BUFLEN]; 168129471Spjd char type[BUFLEN]; 169132381Spjd char decl[2 * BUFLEN]; 170132381Spjd struct field *fp; 171132381Spjd 172132381Spjd if (curstr == NULL || curstr->done) 173129471Spjd error("No current structure to add field to"); 174129471Spjd if (! nextfield(name)) 175129471Spjd error("No field name"); 176129471Spjd if (! nextfield(type)) 177129471Spjd error("No field type"); 178129471Spjd fp = &curstr->field[curstr->nfields]; 179129471Spjd fp->name = savestr(name); 180129471Spjd if (strcmp(type, "nodeptr") == 0) { 181130193Spjd fp->type = T_NODE; 182129471Spjd sprintf(decl, "union node *%s", name); 183129471Spjd } else if (strcmp(type, "nodelist") == 0) { 184129471Spjd fp->type = T_NODELIST; 185132381Spjd sprintf(decl, "struct nodelist *%s", name); 186129471Spjd } else if (strcmp(type, "string") == 0) { 187129471Spjd fp->type = T_STRING; 188129471Spjd sprintf(decl, "char *%s", name); 189129471Spjd } else if (strcmp(type, "int") == 0) { 190129471Spjd fp->type = T_INT; 191129471Spjd sprintf(decl, "int %s", name); 192129471Spjd } else if (strcmp(type, "other") == 0) { 193129471Spjd fp->type = T_OTHER; 194129471Spjd } else if (strcmp(type, "temp") == 0) { 195129471Spjd fp->type = T_TEMP; 196129471Spjd } else { 197129471Spjd error("Unknown type %s", type); 198129471Spjd } 199129471Spjd if (fp->type == T_OTHER || fp->type == T_TEMP) { 200129471Spjd skipbl(); 201129471Spjd fp->decl = savestr(linep); 202129471Spjd } else { 203129471Spjd if (*linep) 204129471Spjd error("Garbage at end of line"); 205129471Spjd fp->decl = savestr(decl); 206129471Spjd } 207129471Spjd curstr->nfields++; 208129471Spjd} 209129471Spjd 210129471Spjd 211132381Spjdchar writer[] = "\ 212132381Spjd/*\n\ 213132381Spjd * This file was generated by the mknodes program.\n\ 214129471Spjd */\n\ 215132381Spjd\n"; 216129471Spjd 217129471Spjdstatic void 218129471Spjdoutput(char *file) 219129471Spjd{ 220129471Spjd FILE *hfile; 221129471Spjd FILE *cfile; 222129471Spjd FILE *patfile; 223129471Spjd int i; 224129471Spjd struct str *sp; 225129471Spjd struct field *fp; 226129471Spjd char *p; 227129471Spjd 228129471Spjd if ((patfile = fopen(file, "r")) == NULL) 229129471Spjd error("Can't open %s: %s", file, strerror(errno)); 230129471Spjd if ((hfile = fopen("nodes.h", "w")) == NULL) 231129471Spjd error("Can't create nodes.h: %s", strerror(errno)); 232129471Spjd if ((cfile = fopen("nodes.c", "w")) == NULL) 233129471Spjd error("Can't create nodes.c"); 234129471Spjd fputs(writer, hfile); 235129471Spjd for (i = 0 ; i < ntypes ; i++) 236129471Spjd fprintf(hfile, "#define %s %d\n", nodename[i], i); 237129471Spjd fputs("\n\n\n", hfile); 238132381Spjd for (sp = str ; sp < &str[nstr] ; sp++) { 239132381Spjd fprintf(hfile, "struct %s {\n", sp->tag); 240129471Spjd for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) { 241129471Spjd fprintf(hfile, " %s;\n", fp->decl); 242129471Spjd } 243129471Spjd fputs("};\n\n\n", hfile); 244129471Spjd } 245129471Spjd fputs("union node {\n", hfile); 246129471Spjd fprintf(hfile, " int type;\n"); 247129471Spjd for (sp = str ; sp < &str[nstr] ; sp++) { 248129471Spjd fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag); 249129471Spjd } 250129471Spjd fputs("};\n\n\n", hfile); 251129471Spjd fputs("struct nodelist {\n", hfile); 252129471Spjd fputs("\tstruct nodelist *next;\n", hfile); 253129471Spjd fputs("\tunion node *n;\n", hfile); 254129471Spjd fputs("};\n\n\n", hfile); 255129471Spjd fputs("union node *copyfunc(union node *);\n", hfile); 256132381Spjd fputs("void freefunc(union node *);\n", hfile); 257129471Spjd 258129471Spjd fputs(writer, cfile); 259129471Spjd while (fgets(line, sizeof line, patfile) != NULL) { 260129471Spjd for (p = line ; *p == ' ' || *p == '\t' ; p++); 261129471Spjd if (strcmp(p, "%SIZES\n") == 0) 262129471Spjd outsizes(cfile); 263129471Spjd else if (strcmp(p, "%CALCSIZE\n") == 0) 264129471Spjd outfunc(cfile, 1); 265129471Spjd else if (strcmp(p, "%COPY\n") == 0) 266129471Spjd outfunc(cfile, 0); 267129471Spjd else 268129471Spjd fputs(line, cfile); 269129471Spjd } 270129471Spjd} 271129471Spjd 272129471Spjd 273129471Spjd 274129471Spjdstatic void 275129471Spjdoutsizes(FILE *cfile) 276129471Spjd{ 277129471Spjd int i; 278129471Spjd 279129471Spjd fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes); 280129471Spjd for (i = 0 ; i < ntypes ; i++) { 281132381Spjd fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag); 282132381Spjd } 283132381Spjd fprintf(cfile, "};\n"); 284132381Spjd} 285132381Spjd 286132381Spjd 287132381Spjdstatic void 288132381Spjdoutfunc(FILE *cfile, int calcsize) 289132381Spjd{ 290132381Spjd struct str *sp; 291132381Spjd struct field *fp; 292132381Spjd int i; 293132381Spjd 294132381Spjd fputs(" if (n == NULL)\n", cfile); 295132381Spjd if (calcsize) 296132381Spjd fputs(" return;\n", cfile); 297132381Spjd else 298132381Spjd fputs(" return NULL;\n", cfile); 299129471Spjd if (calcsize) 300129471Spjd fputs(" funcblocksize += nodesize[n->type];\n", cfile); 301129471Spjd else { 302129471Spjd fputs(" new = funcblock;\n", cfile); 303129471Spjd fputs(" funcblock = (char *)funcblock + nodesize[n->type];\n", cfile); 304129471Spjd } 305129471Spjd fputs(" switch (n->type) {\n", cfile); 306129471Spjd for (sp = str ; sp < &str[nstr] ; sp++) { 307129471Spjd for (i = 0 ; i < ntypes ; i++) { 308129471Spjd if (nodestr[i] == sp) 309129471Spjd fprintf(cfile, " case %s:\n", nodename[i]); 310129471Spjd } 311129471Spjd for (i = sp->nfields ; --i >= 1 ; ) { 312129471Spjd fp = &sp->field[i]; 313129471Spjd switch (fp->type) { 314129471Spjd case T_NODE: 315132381Spjd if (calcsize) { 316132381Spjd indent(12, cfile); 317129471Spjd fprintf(cfile, "calcsize(n->%s.%s);\n", 318132381Spjd sp->tag, fp->name); 319129471Spjd } else { 320129471Spjd indent(12, cfile); 321129471Spjd fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n", 322129471Spjd sp->tag, fp->name, sp->tag, fp->name); 323129548Spjd } 324129471Spjd break; 325132381Spjd case T_NODELIST: 326129471Spjd if (calcsize) { 327129471Spjd indent(12, cfile); 328129471Spjd fprintf(cfile, "sizenodelist(n->%s.%s);\n", 329129471Spjd sp->tag, fp->name); 330129471Spjd } else { 331129471Spjd indent(12, cfile); 332129471Spjd fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n", 333129471Spjd sp->tag, fp->name, sp->tag, fp->name); 334129471Spjd } 335129471Spjd break; 336129471Spjd case T_STRING: 337129471Spjd if (calcsize) { 338129471Spjd indent(12, cfile); 339129471Spjd fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n", 340129471Spjd sp->tag, fp->name); 341129471Spjd } else { 342129471Spjd indent(12, cfile); 343129471Spjd fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n", 344129471Spjd sp->tag, fp->name, sp->tag, fp->name); 345129471Spjd } 346129471Spjd break; 347129471Spjd case T_INT: 348129471Spjd case T_OTHER: 349129471Spjd if (! calcsize) { 350129471Spjd indent(12, cfile); 351129471Spjd fprintf(cfile, "new->%s.%s = n->%s.%s;\n", 352129471Spjd sp->tag, fp->name, sp->tag, fp->name); 353129471Spjd } 354129471Spjd break; 355129471Spjd } 356129471Spjd } 357129471Spjd indent(12, cfile); 358129471Spjd fputs("break;\n", cfile); 359129471Spjd } 360129471Spjd fputs(" };\n", cfile); 361129471Spjd if (! calcsize) 362129471Spjd fputs(" new->type = n->type;\n", cfile); 363129471Spjd} 364129471Spjd 365129471Spjd 366129471Spjdstatic void 367129471Spjdindent(int amount, FILE *fp) 368132381Spjd{ 369132381Spjd while (amount >= 8) { 370129471Spjd putc('\t', fp); 371129471Spjd amount -= 8; 372129471Spjd } 373129471Spjd while (--amount >= 0) { 374129471Spjd putc(' ', fp); 375129471Spjd } 376129471Spjd} 377129471Spjd 378129471Spjd 379129471Spjdstatic int 380129471Spjdnextfield(char *buf) 381129471Spjd{ 382129471Spjd char *p, *q; 383129471Spjd 384129471Spjd p = linep; 385129471Spjd while (*p == ' ' || *p == '\t') 386129471Spjd p++; 387129471Spjd q = buf; 388129471Spjd while (*p != ' ' && *p != '\t' && *p != '\0') 389129471Spjd *q++ = *p++; 390129471Spjd *q = '\0'; 391129471Spjd linep = p; 392129471Spjd return (q > buf); 393129471Spjd} 394129471Spjd 395129471Spjd 396129471Spjdstatic void 397129471Spjdskipbl(void) 398129471Spjd{ 399129471Spjd while (*linep == ' ' || *linep == '\t') 400129471Spjd linep++; 401129471Spjd} 402129471Spjd 403129471Spjd 404129471Spjdstatic int 405129471Spjdreadline(void) 406129471Spjd{ 407129471Spjd char *p; 408129471Spjd 409129471Spjd if (fgets(line, 1024, infp) == NULL) 410129471Spjd return 0; 411129471Spjd for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++); 412129471Spjd while (p > line && (p[-1] == ' ' || p[-1] == '\t')) 413129471Spjd p--; 414129471Spjd *p = '\0'; 415129471Spjd linep = line; 416129471Spjd linno++; 417129471Spjd if (p - line > BUFLEN) 418129471Spjd error("Line too long"); 419129471Spjd return 1; 420129471Spjd} 421129471Spjd 422129471Spjd 423129471Spjd 424129471Spjdstatic void 425129471Spjderror(const char *msg, ...) 426129471Spjd{ 427129471Spjd va_list va; 428129471Spjd va_start(va, msg); 429129471Spjd 430129471Spjd (void) fprintf(stderr, "line %d: ", linno); 431129471Spjd (void) vfprintf(stderr, msg, va); 432129471Spjd (void) fputc('\n', stderr); 433129471Spjd 434129471Spjd va_end(va); 435129471Spjd 436129471Spjd exit(2); 437129471Spjd} 438129471Spjd 439129471Spjd 440129471Spjd 441129471Spjdstatic char * 442129471Spjdsavestr(const char *s) 443129471Spjd{ 444129471Spjd char *p; 445129471Spjd 446129471Spjd if ((p = malloc(strlen(s) + 1)) == NULL) 447129471Spjd error("Out of space"); 448129471Spjd (void) strcpy(p, s); 449129471Spjd return p; 450129471Spjd} 451129471Spjd