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 * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 33114433Sobrien#if 0 341556Srgrimes#ifndef lint 3520425Sstevestatic char const copyright[] = 361556Srgrimes"@(#) Copyright (c) 1991, 1993\n\ 371556Srgrimes The Regents of the University of California. All rights reserved.\n"; 381556Srgrimes#endif /* not lint */ 391556Srgrimes 401556Srgrimes#ifndef lint 4136150Scharnierstatic char sccsid[] = "@(#)mknodes.c 8.2 (Berkeley) 5/4/95"; 42114433Sobrien#endif /* not lint */ 4336150Scharnier#endif 4499110Sobrien#include <sys/cdefs.h> 4599110Sobrien__FBSDID("$FreeBSD$"); 461556Srgrimes 471556Srgrimes/* 481556Srgrimes * This program reads the nodetypes file and nodes.c.pat file. It generates 491556Srgrimes * the files nodes.h and nodes.c. 501556Srgrimes */ 511556Srgrimes 521556Srgrimes#include <stdio.h> 5317987Speter#include <stdlib.h> 5417987Speter#include <string.h> 5553891Scracauer#include <errno.h> 5617987Speter#include <stdarg.h> 571556Srgrimes 581556Srgrimes#define MAXTYPES 50 /* max number of node types */ 591556Srgrimes#define MAXFIELDS 20 /* max fields in a structure */ 601556Srgrimes#define BUFLEN 100 /* size of character buffers */ 611556Srgrimes 621556Srgrimes/* field types */ 631556Srgrimes#define T_NODE 1 /* union node *field */ 641556Srgrimes#define T_NODELIST 2 /* struct nodelist *field */ 651556Srgrimes#define T_STRING 3 661556Srgrimes#define T_INT 4 /* int field */ 671556Srgrimes#define T_OTHER 5 /* other */ 681556Srgrimes#define T_TEMP 6 /* don't copy this field */ 691556Srgrimes 701556Srgrimes 711556Srgrimesstruct field { /* a structure field */ 721556Srgrimes char *name; /* name of field */ 731556Srgrimes int type; /* type of field */ 741556Srgrimes char *decl; /* declaration of field */ 751556Srgrimes}; 761556Srgrimes 771556Srgrimes 781556Srgrimesstruct str { /* struct representing a node structure */ 791556Srgrimes char *tag; /* structure tag */ 801556Srgrimes int nfields; /* number of fields in the structure */ 811556Srgrimes struct field field[MAXFIELDS]; /* the fields of the structure */ 821556Srgrimes int done; /* set if fully parsed */ 831556Srgrimes}; 841556Srgrimes 851556Srgrimes 8617987Speterstatic int ntypes; /* number of node types */ 8717987Speterstatic char *nodename[MAXTYPES]; /* names of the nodes */ 8817987Speterstatic struct str *nodestr[MAXTYPES]; /* type of structure used by the node */ 8917987Speterstatic int nstr; /* number of structures */ 9017987Speterstatic struct str str[MAXTYPES]; /* the structures */ 9117987Speterstatic struct str *curstr; /* current structure */ 9281602Speterstatic FILE *infp; 9317987Speterstatic char line[1024]; 9417987Speterstatic int linno; 9517987Speterstatic char *linep; 961556Srgrimes 9790111Simpstatic void parsenode(void); 9890111Simpstatic void parsefield(void); 9990111Simpstatic void output(char *); 10090111Simpstatic void outsizes(FILE *); 10190111Simpstatic void outfunc(FILE *, int); 10290111Simpstatic void indent(int, FILE *); 10390111Simpstatic int nextfield(char *); 10490111Simpstatic void skipbl(void); 10590111Simpstatic int readline(void); 106181269Scpercivastatic void error(const char *, ...) __printf0like(1, 2) __dead2; 10790111Simpstatic char *savestr(const char *); 1081556Srgrimes 1091556Srgrimes 11017987Speterint 11190111Simpmain(int argc, char *argv[]) 11217987Speter{ 1131556Srgrimes if (argc != 3) 11418016Speter error("usage: mknodes file"); 11581602Speter infp = stdin; 1161556Srgrimes if ((infp = fopen(argv[1], "r")) == NULL) 11753891Scracauer error("Can't open %s: %s", argv[1], strerror(errno)); 1181556Srgrimes while (readline()) { 1191556Srgrimes if (line[0] == ' ' || line[0] == '\t') 1201556Srgrimes parsefield(); 1211556Srgrimes else if (line[0] != '\0') 1221556Srgrimes parsenode(); 1231556Srgrimes } 1241556Srgrimes output(argv[2]); 1251556Srgrimes exit(0); 1261556Srgrimes} 1271556Srgrimes 1281556Srgrimes 1291556Srgrimes 13017987Speterstatic void 13190111Simpparsenode(void) 13217987Speter{ 1331556Srgrimes char name[BUFLEN]; 1341556Srgrimes char tag[BUFLEN]; 1351556Srgrimes struct str *sp; 1361556Srgrimes 1371556Srgrimes if (curstr && curstr->nfields > 0) 1381556Srgrimes curstr->done = 1; 1391556Srgrimes nextfield(name); 1401556Srgrimes if (! nextfield(tag)) 1411556Srgrimes error("Tag expected"); 1421556Srgrimes if (*linep != '\0') 1431556Srgrimes error("Garbage at end of line"); 1441556Srgrimes nodename[ntypes] = savestr(name); 1451556Srgrimes for (sp = str ; sp < str + nstr ; sp++) { 14617987Speter if (strcmp(sp->tag, tag) == 0) 1471556Srgrimes break; 1481556Srgrimes } 1491556Srgrimes if (sp >= str + nstr) { 1501556Srgrimes sp->tag = savestr(tag); 1511556Srgrimes sp->nfields = 0; 1521556Srgrimes curstr = sp; 1531556Srgrimes nstr++; 1541556Srgrimes } 1551556Srgrimes nodestr[ntypes] = sp; 1561556Srgrimes ntypes++; 1571556Srgrimes} 1581556Srgrimes 1591556Srgrimes 16017987Speterstatic void 16190111Simpparsefield(void) 16217987Speter{ 1631556Srgrimes char name[BUFLEN]; 1641556Srgrimes char type[BUFLEN]; 1651556Srgrimes char decl[2 * BUFLEN]; 1661556Srgrimes struct field *fp; 1671556Srgrimes 1681556Srgrimes if (curstr == NULL || curstr->done) 1691556Srgrimes error("No current structure to add field to"); 1701556Srgrimes if (! nextfield(name)) 1711556Srgrimes error("No field name"); 1721556Srgrimes if (! nextfield(type)) 1731556Srgrimes error("No field type"); 1741556Srgrimes fp = &curstr->field[curstr->nfields]; 1751556Srgrimes fp->name = savestr(name); 17617987Speter if (strcmp(type, "nodeptr") == 0) { 1771556Srgrimes fp->type = T_NODE; 1781556Srgrimes sprintf(decl, "union node *%s", name); 17917987Speter } else if (strcmp(type, "nodelist") == 0) { 1801556Srgrimes fp->type = T_NODELIST; 1811556Srgrimes sprintf(decl, "struct nodelist *%s", name); 18217987Speter } else if (strcmp(type, "string") == 0) { 1831556Srgrimes fp->type = T_STRING; 1841556Srgrimes sprintf(decl, "char *%s", name); 18517987Speter } else if (strcmp(type, "int") == 0) { 1861556Srgrimes fp->type = T_INT; 1871556Srgrimes sprintf(decl, "int %s", name); 18817987Speter } else if (strcmp(type, "other") == 0) { 1891556Srgrimes fp->type = T_OTHER; 19017987Speter } else if (strcmp(type, "temp") == 0) { 1911556Srgrimes fp->type = T_TEMP; 1921556Srgrimes } else { 1931556Srgrimes error("Unknown type %s", type); 1941556Srgrimes } 1951556Srgrimes if (fp->type == T_OTHER || fp->type == T_TEMP) { 1961556Srgrimes skipbl(); 1971556Srgrimes fp->decl = savestr(linep); 1981556Srgrimes } else { 1991556Srgrimes if (*linep) 2001556Srgrimes error("Garbage at end of line"); 2011556Srgrimes fp->decl = savestr(decl); 2021556Srgrimes } 2031556Srgrimes curstr->nfields++; 2041556Srgrimes} 2051556Srgrimes 2061556Srgrimes 2071556Srgrimeschar writer[] = "\ 2081556Srgrimes/*\n\ 2091556Srgrimes * This file was generated by the mknodes program.\n\ 2101556Srgrimes */\n\ 2111556Srgrimes\n"; 2121556Srgrimes 21317987Speterstatic void 21490111Simpoutput(char *file) 21517987Speter{ 2161556Srgrimes FILE *hfile; 2171556Srgrimes FILE *cfile; 2181556Srgrimes FILE *patfile; 2191556Srgrimes int i; 2201556Srgrimes struct str *sp; 2211556Srgrimes struct field *fp; 2221556Srgrimes char *p; 2231556Srgrimes 2241556Srgrimes if ((patfile = fopen(file, "r")) == NULL) 22553891Scracauer error("Can't open %s: %s", file, strerror(errno)); 2261556Srgrimes if ((hfile = fopen("nodes.h", "w")) == NULL) 22753891Scracauer error("Can't create nodes.h: %s", strerror(errno)); 2281556Srgrimes if ((cfile = fopen("nodes.c", "w")) == NULL) 2291556Srgrimes error("Can't create nodes.c"); 2301556Srgrimes fputs(writer, hfile); 2311556Srgrimes for (i = 0 ; i < ntypes ; i++) 2321556Srgrimes fprintf(hfile, "#define %s %d\n", nodename[i], i); 2331556Srgrimes fputs("\n\n\n", hfile); 2341556Srgrimes for (sp = str ; sp < &str[nstr] ; sp++) { 2351556Srgrimes fprintf(hfile, "struct %s {\n", sp->tag); 2361556Srgrimes for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) { 2371556Srgrimes fprintf(hfile, " %s;\n", fp->decl); 2381556Srgrimes } 2391556Srgrimes fputs("};\n\n\n", hfile); 2401556Srgrimes } 2411556Srgrimes fputs("union node {\n", hfile); 2421556Srgrimes fprintf(hfile, " int type;\n"); 2431556Srgrimes for (sp = str ; sp < &str[nstr] ; sp++) { 2441556Srgrimes fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag); 2451556Srgrimes } 2461556Srgrimes fputs("};\n\n\n", hfile); 2471556Srgrimes fputs("struct nodelist {\n", hfile); 2481556Srgrimes fputs("\tstruct nodelist *next;\n", hfile); 2491556Srgrimes fputs("\tunion node *n;\n", hfile); 2501556Srgrimes fputs("};\n\n\n", hfile); 251196634Sjilles fputs("struct funcdef;\n", hfile); 252196483Sjilles fputs("struct funcdef *copyfunc(union node *);\n", hfile); 253196634Sjilles fputs("union node *getfuncnode(struct funcdef *);\n", hfile); 254196483Sjilles fputs("void reffunc(struct funcdef *);\n", hfile); 255196483Sjilles fputs("void unreffunc(struct funcdef *);\n", hfile); 2561556Srgrimes 2571556Srgrimes fputs(writer, cfile); 2581556Srgrimes while (fgets(line, sizeof line, patfile) != NULL) { 2591556Srgrimes for (p = line ; *p == ' ' || *p == '\t' ; p++); 26017987Speter if (strcmp(p, "%SIZES\n") == 0) 2611556Srgrimes outsizes(cfile); 26217987Speter else if (strcmp(p, "%CALCSIZE\n") == 0) 2631556Srgrimes outfunc(cfile, 1); 26417987Speter else if (strcmp(p, "%COPY\n") == 0) 2651556Srgrimes outfunc(cfile, 0); 2661556Srgrimes else 2671556Srgrimes fputs(line, cfile); 2681556Srgrimes } 2691556Srgrimes} 2701556Srgrimes 2711556Srgrimes 2721556Srgrimes 27317987Speterstatic void 27490111Simpoutsizes(FILE *cfile) 27517987Speter{ 2761556Srgrimes int i; 2771556Srgrimes 2781556Srgrimes fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes); 2791556Srgrimes for (i = 0 ; i < ntypes ; i++) { 2801556Srgrimes fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag); 2811556Srgrimes } 2821556Srgrimes fprintf(cfile, "};\n"); 2831556Srgrimes} 2841556Srgrimes 2851556Srgrimes 28617987Speterstatic void 28790111Simpoutfunc(FILE *cfile, int calcsize) 28817987Speter{ 2891556Srgrimes struct str *sp; 2901556Srgrimes struct field *fp; 2911556Srgrimes int i; 2921556Srgrimes 2931556Srgrimes fputs(" if (n == NULL)\n", cfile); 2941556Srgrimes if (calcsize) 2951556Srgrimes fputs(" return;\n", cfile); 2961556Srgrimes else 2971556Srgrimes fputs(" return NULL;\n", cfile); 2981556Srgrimes if (calcsize) 2991556Srgrimes fputs(" funcblocksize += nodesize[n->type];\n", cfile); 3001556Srgrimes else { 3011556Srgrimes fputs(" new = funcblock;\n", cfile); 30225226Ssteve fputs(" funcblock = (char *)funcblock + nodesize[n->type];\n", cfile); 3031556Srgrimes } 3041556Srgrimes fputs(" switch (n->type) {\n", cfile); 3051556Srgrimes for (sp = str ; sp < &str[nstr] ; sp++) { 3061556Srgrimes for (i = 0 ; i < ntypes ; i++) { 3071556Srgrimes if (nodestr[i] == sp) 3081556Srgrimes fprintf(cfile, " case %s:\n", nodename[i]); 3091556Srgrimes } 3101556Srgrimes for (i = sp->nfields ; --i >= 1 ; ) { 3111556Srgrimes fp = &sp->field[i]; 3121556Srgrimes switch (fp->type) { 3131556Srgrimes case T_NODE: 3141556Srgrimes if (calcsize) { 3151556Srgrimes indent(12, cfile); 3161556Srgrimes fprintf(cfile, "calcsize(n->%s.%s);\n", 3171556Srgrimes sp->tag, fp->name); 3181556Srgrimes } else { 3191556Srgrimes indent(12, cfile); 3201556Srgrimes fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n", 3211556Srgrimes sp->tag, fp->name, sp->tag, fp->name); 3221556Srgrimes } 3231556Srgrimes break; 3241556Srgrimes case T_NODELIST: 3251556Srgrimes if (calcsize) { 3261556Srgrimes indent(12, cfile); 3271556Srgrimes fprintf(cfile, "sizenodelist(n->%s.%s);\n", 3281556Srgrimes sp->tag, fp->name); 3291556Srgrimes } else { 3301556Srgrimes indent(12, cfile); 3311556Srgrimes fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n", 3321556Srgrimes sp->tag, fp->name, sp->tag, fp->name); 3331556Srgrimes } 3341556Srgrimes break; 3351556Srgrimes case T_STRING: 3361556Srgrimes if (calcsize) { 3371556Srgrimes indent(12, cfile); 3381556Srgrimes fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n", 3391556Srgrimes sp->tag, fp->name); 3401556Srgrimes } else { 3411556Srgrimes indent(12, cfile); 3421556Srgrimes fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n", 3431556Srgrimes sp->tag, fp->name, sp->tag, fp->name); 3441556Srgrimes } 3451556Srgrimes break; 3461556Srgrimes case T_INT: 3471556Srgrimes case T_OTHER: 3481556Srgrimes if (! calcsize) { 3491556Srgrimes indent(12, cfile); 3501556Srgrimes fprintf(cfile, "new->%s.%s = n->%s.%s;\n", 3511556Srgrimes sp->tag, fp->name, sp->tag, fp->name); 3521556Srgrimes } 3531556Srgrimes break; 3541556Srgrimes } 3551556Srgrimes } 3561556Srgrimes indent(12, cfile); 3571556Srgrimes fputs("break;\n", cfile); 3581556Srgrimes } 3591556Srgrimes fputs(" };\n", cfile); 3601556Srgrimes if (! calcsize) 3611556Srgrimes fputs(" new->type = n->type;\n", cfile); 3621556Srgrimes} 3631556Srgrimes 3641556Srgrimes 36517987Speterstatic void 36690111Simpindent(int amount, FILE *fp) 36717987Speter{ 3681556Srgrimes while (amount >= 8) { 3691556Srgrimes putc('\t', fp); 3701556Srgrimes amount -= 8; 3711556Srgrimes } 3721556Srgrimes while (--amount >= 0) { 3731556Srgrimes putc(' ', fp); 3741556Srgrimes } 3751556Srgrimes} 3761556Srgrimes 3771556Srgrimes 37817987Speterstatic int 37990111Simpnextfield(char *buf) 38017987Speter{ 38125226Ssteve char *p, *q; 3821556Srgrimes 3831556Srgrimes p = linep; 3841556Srgrimes while (*p == ' ' || *p == '\t') 3851556Srgrimes p++; 3861556Srgrimes q = buf; 3871556Srgrimes while (*p != ' ' && *p != '\t' && *p != '\0') 3881556Srgrimes *q++ = *p++; 3891556Srgrimes *q = '\0'; 3901556Srgrimes linep = p; 3911556Srgrimes return (q > buf); 3921556Srgrimes} 3931556Srgrimes 3941556Srgrimes 39517987Speterstatic void 39690111Simpskipbl(void) 39717987Speter{ 3981556Srgrimes while (*linep == ' ' || *linep == '\t') 3991556Srgrimes linep++; 4001556Srgrimes} 4011556Srgrimes 4021556Srgrimes 40317987Speterstatic int 40490111Simpreadline(void) 40517987Speter{ 40625226Ssteve char *p; 4071556Srgrimes 4081556Srgrimes if (fgets(line, 1024, infp) == NULL) 4091556Srgrimes return 0; 4101556Srgrimes for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++); 4111556Srgrimes while (p > line && (p[-1] == ' ' || p[-1] == '\t')) 4121556Srgrimes p--; 4131556Srgrimes *p = '\0'; 4141556Srgrimes linep = line; 4151556Srgrimes linno++; 4161556Srgrimes if (p - line > BUFLEN) 4171556Srgrimes error("Line too long"); 4181556Srgrimes return 1; 4191556Srgrimes} 4201556Srgrimes 4211556Srgrimes 4221556Srgrimes 42317987Speterstatic void 42417987Spetererror(const char *msg, ...) 42517987Speter{ 42617987Speter va_list va; 42717987Speter va_start(va, msg); 42817987Speter 42917987Speter (void) fprintf(stderr, "line %d: ", linno); 43017987Speter (void) vfprintf(stderr, msg, va); 43117987Speter (void) fputc('\n', stderr); 43217987Speter 43317987Speter va_end(va); 43417987Speter 4351556Srgrimes exit(2); 4361556Srgrimes} 4371556Srgrimes 4381556Srgrimes 4391556Srgrimes 44017987Speterstatic char * 44190111Simpsavestr(const char *s) 44217987Speter{ 44325226Ssteve char *p; 4441556Srgrimes 4451556Srgrimes if ((p = malloc(strlen(s) + 1)) == NULL) 4461556Srgrimes error("Out of space"); 44717987Speter (void) strcpy(p, s); 4481556Srgrimes return p; 4491556Srgrimes} 450