mknodes.c revision 196483
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: head/bin/sh/mknodes.c 196483 2009-08-23 21:09:46Z jilles $"); 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); 251196483Sjilles fputs("struct funcdef {\n", hfile); 252196483Sjilles fputs("\tunsigned int refcount;\n", hfile); 253196483Sjilles fputs("\tunion node n;\n", hfile); 254196483Sjilles fputs("};\n\n\n", hfile); 255196483Sjilles fputs("struct funcdef *copyfunc(union node *);\n", hfile); 256196483Sjilles fputs("void reffunc(struct funcdef *);\n", hfile); 257196483Sjilles fputs("void unreffunc(struct funcdef *);\n", hfile); 2581556Srgrimes 2591556Srgrimes fputs(writer, cfile); 2601556Srgrimes while (fgets(line, sizeof line, patfile) != NULL) { 2611556Srgrimes for (p = line ; *p == ' ' || *p == '\t' ; p++); 26217987Speter if (strcmp(p, "%SIZES\n") == 0) 2631556Srgrimes outsizes(cfile); 26417987Speter else if (strcmp(p, "%CALCSIZE\n") == 0) 2651556Srgrimes outfunc(cfile, 1); 26617987Speter else if (strcmp(p, "%COPY\n") == 0) 2671556Srgrimes outfunc(cfile, 0); 2681556Srgrimes else 2691556Srgrimes fputs(line, cfile); 2701556Srgrimes } 2711556Srgrimes} 2721556Srgrimes 2731556Srgrimes 2741556Srgrimes 27517987Speterstatic void 27690111Simpoutsizes(FILE *cfile) 27717987Speter{ 2781556Srgrimes int i; 2791556Srgrimes 2801556Srgrimes fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes); 2811556Srgrimes for (i = 0 ; i < ntypes ; i++) { 2821556Srgrimes fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag); 2831556Srgrimes } 2841556Srgrimes fprintf(cfile, "};\n"); 2851556Srgrimes} 2861556Srgrimes 2871556Srgrimes 28817987Speterstatic void 28990111Simpoutfunc(FILE *cfile, int calcsize) 29017987Speter{ 2911556Srgrimes struct str *sp; 2921556Srgrimes struct field *fp; 2931556Srgrimes int i; 2941556Srgrimes 2951556Srgrimes fputs(" if (n == NULL)\n", cfile); 2961556Srgrimes if (calcsize) 2971556Srgrimes fputs(" return;\n", cfile); 2981556Srgrimes else 2991556Srgrimes fputs(" return NULL;\n", cfile); 3001556Srgrimes if (calcsize) 3011556Srgrimes fputs(" funcblocksize += nodesize[n->type];\n", cfile); 3021556Srgrimes else { 3031556Srgrimes fputs(" new = funcblock;\n", cfile); 30425226Ssteve fputs(" funcblock = (char *)funcblock + nodesize[n->type];\n", cfile); 3051556Srgrimes } 3061556Srgrimes fputs(" switch (n->type) {\n", cfile); 3071556Srgrimes for (sp = str ; sp < &str[nstr] ; sp++) { 3081556Srgrimes for (i = 0 ; i < ntypes ; i++) { 3091556Srgrimes if (nodestr[i] == sp) 3101556Srgrimes fprintf(cfile, " case %s:\n", nodename[i]); 3111556Srgrimes } 3121556Srgrimes for (i = sp->nfields ; --i >= 1 ; ) { 3131556Srgrimes fp = &sp->field[i]; 3141556Srgrimes switch (fp->type) { 3151556Srgrimes case T_NODE: 3161556Srgrimes if (calcsize) { 3171556Srgrimes indent(12, cfile); 3181556Srgrimes fprintf(cfile, "calcsize(n->%s.%s);\n", 3191556Srgrimes sp->tag, fp->name); 3201556Srgrimes } else { 3211556Srgrimes indent(12, cfile); 3221556Srgrimes fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n", 3231556Srgrimes sp->tag, fp->name, sp->tag, fp->name); 3241556Srgrimes } 3251556Srgrimes break; 3261556Srgrimes case T_NODELIST: 3271556Srgrimes if (calcsize) { 3281556Srgrimes indent(12, cfile); 3291556Srgrimes fprintf(cfile, "sizenodelist(n->%s.%s);\n", 3301556Srgrimes sp->tag, fp->name); 3311556Srgrimes } else { 3321556Srgrimes indent(12, cfile); 3331556Srgrimes fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n", 3341556Srgrimes sp->tag, fp->name, sp->tag, fp->name); 3351556Srgrimes } 3361556Srgrimes break; 3371556Srgrimes case T_STRING: 3381556Srgrimes if (calcsize) { 3391556Srgrimes indent(12, cfile); 3401556Srgrimes fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n", 3411556Srgrimes sp->tag, fp->name); 3421556Srgrimes } else { 3431556Srgrimes indent(12, cfile); 3441556Srgrimes fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n", 3451556Srgrimes sp->tag, fp->name, sp->tag, fp->name); 3461556Srgrimes } 3471556Srgrimes break; 3481556Srgrimes case T_INT: 3491556Srgrimes case T_OTHER: 3501556Srgrimes if (! calcsize) { 3511556Srgrimes indent(12, cfile); 3521556Srgrimes fprintf(cfile, "new->%s.%s = n->%s.%s;\n", 3531556Srgrimes sp->tag, fp->name, sp->tag, fp->name); 3541556Srgrimes } 3551556Srgrimes break; 3561556Srgrimes } 3571556Srgrimes } 3581556Srgrimes indent(12, cfile); 3591556Srgrimes fputs("break;\n", cfile); 3601556Srgrimes } 3611556Srgrimes fputs(" };\n", cfile); 3621556Srgrimes if (! calcsize) 3631556Srgrimes fputs(" new->type = n->type;\n", cfile); 3641556Srgrimes} 3651556Srgrimes 3661556Srgrimes 36717987Speterstatic void 36890111Simpindent(int amount, FILE *fp) 36917987Speter{ 3701556Srgrimes while (amount >= 8) { 3711556Srgrimes putc('\t', fp); 3721556Srgrimes amount -= 8; 3731556Srgrimes } 3741556Srgrimes while (--amount >= 0) { 3751556Srgrimes putc(' ', fp); 3761556Srgrimes } 3771556Srgrimes} 3781556Srgrimes 3791556Srgrimes 38017987Speterstatic int 38190111Simpnextfield(char *buf) 38217987Speter{ 38325226Ssteve char *p, *q; 3841556Srgrimes 3851556Srgrimes p = linep; 3861556Srgrimes while (*p == ' ' || *p == '\t') 3871556Srgrimes p++; 3881556Srgrimes q = buf; 3891556Srgrimes while (*p != ' ' && *p != '\t' && *p != '\0') 3901556Srgrimes *q++ = *p++; 3911556Srgrimes *q = '\0'; 3921556Srgrimes linep = p; 3931556Srgrimes return (q > buf); 3941556Srgrimes} 3951556Srgrimes 3961556Srgrimes 39717987Speterstatic void 39890111Simpskipbl(void) 39917987Speter{ 4001556Srgrimes while (*linep == ' ' || *linep == '\t') 4011556Srgrimes linep++; 4021556Srgrimes} 4031556Srgrimes 4041556Srgrimes 40517987Speterstatic int 40690111Simpreadline(void) 40717987Speter{ 40825226Ssteve char *p; 4091556Srgrimes 4101556Srgrimes if (fgets(line, 1024, infp) == NULL) 4111556Srgrimes return 0; 4121556Srgrimes for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++); 4131556Srgrimes while (p > line && (p[-1] == ' ' || p[-1] == '\t')) 4141556Srgrimes p--; 4151556Srgrimes *p = '\0'; 4161556Srgrimes linep = line; 4171556Srgrimes linno++; 4181556Srgrimes if (p - line > BUFLEN) 4191556Srgrimes error("Line too long"); 4201556Srgrimes return 1; 4211556Srgrimes} 4221556Srgrimes 4231556Srgrimes 4241556Srgrimes 42517987Speterstatic void 42617987Spetererror(const char *msg, ...) 42717987Speter{ 42817987Speter va_list va; 42917987Speter va_start(va, msg); 43017987Speter 43117987Speter (void) fprintf(stderr, "line %d: ", linno); 43217987Speter (void) vfprintf(stderr, msg, va); 43317987Speter (void) fputc('\n', stderr); 43417987Speter 43517987Speter va_end(va); 43617987Speter 4371556Srgrimes exit(2); 4381556Srgrimes} 4391556Srgrimes 4401556Srgrimes 4411556Srgrimes 44217987Speterstatic char * 44390111Simpsavestr(const char *s) 44417987Speter{ 44525226Ssteve char *p; 4461556Srgrimes 4471556Srgrimes if ((p = malloc(strlen(s) + 1)) == NULL) 4481556Srgrimes error("Out of space"); 44917987Speter (void) strcpy(p, s); 4501556Srgrimes return p; 4511556Srgrimes} 452