mknodes.c revision 18016
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 341556Srgrimes * SUCH DAMAGE. 353044Sdg * 3618016Speter * $Id: mknodes.c,v 1.3 1996/09/01 10:20:51 peter Exp $ 371556Srgrimes */ 381556Srgrimes 391556Srgrimes#ifndef lint 401556Srgrimesstatic char copyright[] = 411556Srgrimes"@(#) Copyright (c) 1991, 1993\n\ 421556Srgrimes The Regents of the University of California. All rights reserved.\n"; 431556Srgrimes#endif /* not lint */ 441556Srgrimes 451556Srgrimes#ifndef lint 4617987Speterstatic char sccsid[] = "@(#)mknodes.c 8.2 (Berkeley) 5/4/95"; 471556Srgrimes#endif /* not lint */ 481556Srgrimes 491556Srgrimes/* 501556Srgrimes * This program reads the nodetypes file and nodes.c.pat file. It generates 511556Srgrimes * the files nodes.h and nodes.c. 521556Srgrimes */ 531556Srgrimes 541556Srgrimes#include <stdio.h> 5517987Speter#include <stdlib.h> 5617987Speter#include <string.h> 5717987Speter#if __STDC__ 5817987Speter#include <stdarg.h> 5917987Speter#else 6017987Speter#include <varargs.h> 6117987Speter#endif 621556Srgrimes 631556Srgrimes 641556Srgrimes#define MAXTYPES 50 /* max number of node types */ 651556Srgrimes#define MAXFIELDS 20 /* max fields in a structure */ 661556Srgrimes#define BUFLEN 100 /* size of character buffers */ 671556Srgrimes 681556Srgrimes/* field types */ 691556Srgrimes#define T_NODE 1 /* union node *field */ 701556Srgrimes#define T_NODELIST 2 /* struct nodelist *field */ 711556Srgrimes#define T_STRING 3 721556Srgrimes#define T_INT 4 /* int field */ 731556Srgrimes#define T_OTHER 5 /* other */ 741556Srgrimes#define T_TEMP 6 /* don't copy this field */ 751556Srgrimes 761556Srgrimes 771556Srgrimesstruct field { /* a structure field */ 781556Srgrimes char *name; /* name of field */ 791556Srgrimes int type; /* type of field */ 801556Srgrimes char *decl; /* declaration of field */ 811556Srgrimes}; 821556Srgrimes 831556Srgrimes 841556Srgrimesstruct str { /* struct representing a node structure */ 851556Srgrimes 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 */ 891556Srgrimes}; 901556Srgrimes 911556Srgrimes 9217987Speterstatic int ntypes; /* number of node types */ 9317987Speterstatic char *nodename[MAXTYPES]; /* names of the nodes */ 9417987Speterstatic struct str *nodestr[MAXTYPES]; /* type of structure used by the node */ 9517987Speterstatic int nstr; /* number of structures */ 9617987Speterstatic struct str str[MAXTYPES]; /* the structures */ 9717987Speterstatic struct str *curstr; /* current structure */ 9817987Speterstatic FILE *infp = stdin; 9917987Speterstatic char line[1024]; 10017987Speterstatic int linno; 10117987Speterstatic char *linep; 1021556Srgrimes 10317987Speterstatic void parsenode __P((void)); 10417987Speterstatic void parsefield __P((void)); 10517987Speterstatic void output __P((char *)); 10617987Speterstatic void outsizes __P((FILE *)); 10717987Speterstatic void outfunc __P((FILE *, int)); 10817987Speterstatic void indent __P((int, FILE *)); 10917987Speterstatic int nextfield __P((char *)); 11017987Speterstatic void skipbl __P((void)); 11117987Speterstatic int readline __P((void)); 11217987Speterstatic void error __P((const char *, ...)); 11317987Speterstatic char *savestr __P((const char *)); 1141556Srgrimes 1151556Srgrimes 11617987Speterint 1171556Srgrimesmain(argc, argv) 11817987Speter int argc; 1191556Srgrimes char **argv; 12017987Speter{ 1211556Srgrimes if (argc != 3) 12218016Speter error("usage: mknodes file"); 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(); 1301556Srgrimes } 1311556Srgrimes output(argv[2]); 1321556Srgrimes exit(0); 1331556Srgrimes} 1341556Srgrimes 1351556Srgrimes 1361556Srgrimes 13717987Speterstatic void 13817987Speterparsenode() 13917987Speter{ 1401556Srgrimes char name[BUFLEN]; 1411556Srgrimes char tag[BUFLEN]; 1421556Srgrimes struct str *sp; 1431556Srgrimes 1441556Srgrimes if (curstr && curstr->nfields > 0) 1451556Srgrimes curstr->done = 1; 1461556Srgrimes nextfield(name); 1471556Srgrimes if (! nextfield(tag)) 1481556Srgrimes error("Tag expected"); 1491556Srgrimes if (*linep != '\0') 1501556Srgrimes error("Garbage at end of line"); 1511556Srgrimes nodename[ntypes] = savestr(name); 1521556Srgrimes for (sp = str ; sp < str + nstr ; sp++) { 15317987Speter if (strcmp(sp->tag, tag) == 0) 1541556Srgrimes break; 1551556Srgrimes } 1561556Srgrimes if (sp >= str + nstr) { 1571556Srgrimes sp->tag = savestr(tag); 1581556Srgrimes sp->nfields = 0; 1591556Srgrimes curstr = sp; 1601556Srgrimes nstr++; 1611556Srgrimes } 1621556Srgrimes nodestr[ntypes] = sp; 1631556Srgrimes ntypes++; 1641556Srgrimes} 1651556Srgrimes 1661556Srgrimes 16717987Speterstatic void 16817987Speterparsefield() 16917987Speter{ 1701556Srgrimes char name[BUFLEN]; 1711556Srgrimes char type[BUFLEN]; 1721556Srgrimes char decl[2 * BUFLEN]; 1731556Srgrimes struct field *fp; 1741556Srgrimes 1751556Srgrimes if (curstr == NULL || curstr->done) 1761556Srgrimes error("No current structure to add field to"); 1771556Srgrimes if (! nextfield(name)) 1781556Srgrimes error("No field name"); 1791556Srgrimes if (! nextfield(type)) 1801556Srgrimes error("No field type"); 1811556Srgrimes fp = &curstr->field[curstr->nfields]; 1821556Srgrimes fp->name = savestr(name); 18317987Speter if (strcmp(type, "nodeptr") == 0) { 1841556Srgrimes fp->type = T_NODE; 1851556Srgrimes sprintf(decl, "union node *%s", name); 18617987Speter } else if (strcmp(type, "nodelist") == 0) { 1871556Srgrimes fp->type = T_NODELIST; 1881556Srgrimes sprintf(decl, "struct nodelist *%s", name); 18917987Speter } else if (strcmp(type, "string") == 0) { 1901556Srgrimes fp->type = T_STRING; 1911556Srgrimes sprintf(decl, "char *%s", name); 19217987Speter } else if (strcmp(type, "int") == 0) { 1931556Srgrimes fp->type = T_INT; 1941556Srgrimes sprintf(decl, "int %s", name); 19517987Speter } else if (strcmp(type, "other") == 0) { 1961556Srgrimes fp->type = T_OTHER; 19717987Speter } else if (strcmp(type, "temp") == 0) { 1981556Srgrimes fp->type = T_TEMP; 1991556Srgrimes } else { 2001556Srgrimes error("Unknown type %s", type); 2011556Srgrimes } 2021556Srgrimes if (fp->type == T_OTHER || fp->type == T_TEMP) { 2031556Srgrimes skipbl(); 2041556Srgrimes fp->decl = savestr(linep); 2051556Srgrimes } else { 2061556Srgrimes if (*linep) 2071556Srgrimes error("Garbage at end of line"); 2081556Srgrimes fp->decl = savestr(decl); 2091556Srgrimes } 2101556Srgrimes curstr->nfields++; 2111556Srgrimes} 2121556Srgrimes 2131556Srgrimes 2141556Srgrimeschar writer[] = "\ 2151556Srgrimes/*\n\ 2161556Srgrimes * This file was generated by the mknodes program.\n\ 2171556Srgrimes */\n\ 2181556Srgrimes\n"; 2191556Srgrimes 22017987Speterstatic void 2211556Srgrimesoutput(file) 2221556Srgrimes char *file; 22317987Speter{ 2241556Srgrimes FILE *hfile; 2251556Srgrimes FILE *cfile; 2261556Srgrimes FILE *patfile; 2271556Srgrimes int i; 2281556Srgrimes struct str *sp; 2291556Srgrimes struct field *fp; 2301556Srgrimes char *p; 2311556Srgrimes 2321556Srgrimes if ((patfile = fopen(file, "r")) == NULL) 2331556Srgrimes error("Can't open %s", file); 2341556Srgrimes if ((hfile = fopen("nodes.h", "w")) == NULL) 2351556Srgrimes error("Can't create nodes.h"); 2361556Srgrimes if ((cfile = fopen("nodes.c", "w")) == NULL) 2371556Srgrimes error("Can't create nodes.c"); 2381556Srgrimes fputs(writer, hfile); 2391556Srgrimes for (i = 0 ; i < ntypes ; i++) 2401556Srgrimes fprintf(hfile, "#define %s %d\n", nodename[i], i); 2411556Srgrimes fputs("\n\n\n", hfile); 2421556Srgrimes for (sp = str ; sp < &str[nstr] ; sp++) { 2431556Srgrimes fprintf(hfile, "struct %s {\n", sp->tag); 2441556Srgrimes for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) { 2451556Srgrimes fprintf(hfile, " %s;\n", fp->decl); 2461556Srgrimes } 2471556Srgrimes fputs("};\n\n\n", hfile); 2481556Srgrimes } 2491556Srgrimes fputs("union node {\n", hfile); 2501556Srgrimes fprintf(hfile, " int type;\n"); 2511556Srgrimes for (sp = str ; sp < &str[nstr] ; sp++) { 2521556Srgrimes fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag); 2531556Srgrimes } 2541556Srgrimes fputs("};\n\n\n", hfile); 2551556Srgrimes fputs("struct nodelist {\n", hfile); 2561556Srgrimes fputs("\tstruct nodelist *next;\n", hfile); 2571556Srgrimes fputs("\tunion node *n;\n", hfile); 2581556Srgrimes fputs("};\n\n\n", hfile); 2591556Srgrimes fputs("#ifdef __STDC__\n", hfile); 2601556Srgrimes fputs("union node *copyfunc(union node *);\n", hfile); 2611556Srgrimes fputs("void freefunc(union node *);\n", hfile); 2621556Srgrimes fputs("#else\n", hfile); 2631556Srgrimes fputs("union node *copyfunc();\n", hfile); 2641556Srgrimes fputs("void freefunc();\n", hfile); 2651556Srgrimes fputs("#endif\n", hfile); 2661556Srgrimes 2671556Srgrimes fputs(writer, cfile); 2681556Srgrimes while (fgets(line, sizeof line, patfile) != NULL) { 2691556Srgrimes for (p = line ; *p == ' ' || *p == '\t' ; p++); 27017987Speter if (strcmp(p, "%SIZES\n") == 0) 2711556Srgrimes outsizes(cfile); 27217987Speter else if (strcmp(p, "%CALCSIZE\n") == 0) 2731556Srgrimes outfunc(cfile, 1); 27417987Speter else if (strcmp(p, "%COPY\n") == 0) 2751556Srgrimes outfunc(cfile, 0); 2761556Srgrimes else 2771556Srgrimes fputs(line, cfile); 2781556Srgrimes } 2791556Srgrimes} 2801556Srgrimes 2811556Srgrimes 2821556Srgrimes 28317987Speterstatic void 2841556Srgrimesoutsizes(cfile) 2851556Srgrimes FILE *cfile; 28617987Speter{ 2871556Srgrimes int i; 2881556Srgrimes 2891556Srgrimes fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes); 2901556Srgrimes for (i = 0 ; i < ntypes ; i++) { 2911556Srgrimes fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag); 2921556Srgrimes } 2931556Srgrimes fprintf(cfile, "};\n"); 2941556Srgrimes} 2951556Srgrimes 2961556Srgrimes 29717987Speterstatic void 2981556Srgrimesoutfunc(cfile, calcsize) 2991556Srgrimes FILE *cfile; 30017987Speter int calcsize; 30117987Speter{ 3021556Srgrimes struct str *sp; 3031556Srgrimes struct field *fp; 3041556Srgrimes int i; 3051556Srgrimes 3061556Srgrimes fputs(" if (n == NULL)\n", cfile); 3071556Srgrimes if (calcsize) 3081556Srgrimes fputs(" return;\n", cfile); 3091556Srgrimes else 3101556Srgrimes fputs(" return NULL;\n", cfile); 3111556Srgrimes if (calcsize) 3121556Srgrimes fputs(" funcblocksize += nodesize[n->type];\n", cfile); 3131556Srgrimes else { 3141556Srgrimes fputs(" new = funcblock;\n", cfile); 3151556Srgrimes fputs(" funcblock += nodesize[n->type];\n", cfile); 3161556Srgrimes } 3171556Srgrimes fputs(" switch (n->type) {\n", cfile); 3181556Srgrimes for (sp = str ; sp < &str[nstr] ; sp++) { 3191556Srgrimes for (i = 0 ; i < ntypes ; i++) { 3201556Srgrimes if (nodestr[i] == sp) 3211556Srgrimes fprintf(cfile, " case %s:\n", nodename[i]); 3221556Srgrimes } 3231556Srgrimes for (i = sp->nfields ; --i >= 1 ; ) { 3241556Srgrimes fp = &sp->field[i]; 3251556Srgrimes switch (fp->type) { 3261556Srgrimes case T_NODE: 3271556Srgrimes if (calcsize) { 3281556Srgrimes indent(12, cfile); 3291556Srgrimes fprintf(cfile, "calcsize(n->%s.%s);\n", 3301556Srgrimes sp->tag, fp->name); 3311556Srgrimes } else { 3321556Srgrimes indent(12, cfile); 3331556Srgrimes fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n", 3341556Srgrimes sp->tag, fp->name, sp->tag, fp->name); 3351556Srgrimes } 3361556Srgrimes break; 3371556Srgrimes case T_NODELIST: 3381556Srgrimes if (calcsize) { 3391556Srgrimes indent(12, cfile); 3401556Srgrimes fprintf(cfile, "sizenodelist(n->%s.%s);\n", 3411556Srgrimes sp->tag, fp->name); 3421556Srgrimes } else { 3431556Srgrimes indent(12, cfile); 3441556Srgrimes fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n", 3451556Srgrimes sp->tag, fp->name, sp->tag, fp->name); 3461556Srgrimes } 3471556Srgrimes break; 3481556Srgrimes case T_STRING: 3491556Srgrimes if (calcsize) { 3501556Srgrimes indent(12, cfile); 3511556Srgrimes fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n", 3521556Srgrimes sp->tag, fp->name); 3531556Srgrimes } else { 3541556Srgrimes indent(12, cfile); 3551556Srgrimes fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n", 3561556Srgrimes sp->tag, fp->name, sp->tag, fp->name); 3571556Srgrimes } 3581556Srgrimes break; 3591556Srgrimes case T_INT: 3601556Srgrimes case T_OTHER: 3611556Srgrimes if (! calcsize) { 3621556Srgrimes indent(12, cfile); 3631556Srgrimes fprintf(cfile, "new->%s.%s = n->%s.%s;\n", 3641556Srgrimes sp->tag, fp->name, sp->tag, fp->name); 3651556Srgrimes } 3661556Srgrimes break; 3671556Srgrimes } 3681556Srgrimes } 3691556Srgrimes indent(12, cfile); 3701556Srgrimes fputs("break;\n", cfile); 3711556Srgrimes } 3721556Srgrimes fputs(" };\n", cfile); 3731556Srgrimes if (! calcsize) 3741556Srgrimes fputs(" new->type = n->type;\n", cfile); 3751556Srgrimes} 3761556Srgrimes 3771556Srgrimes 37817987Speterstatic void 3791556Srgrimesindent(amount, fp) 38017987Speter int amount; 3811556Srgrimes FILE *fp; 38217987Speter{ 3831556Srgrimes while (amount >= 8) { 3841556Srgrimes putc('\t', fp); 3851556Srgrimes amount -= 8; 3861556Srgrimes } 3871556Srgrimes while (--amount >= 0) { 3881556Srgrimes putc(' ', fp); 3891556Srgrimes } 3901556Srgrimes} 3911556Srgrimes 3921556Srgrimes 39317987Speterstatic int 3941556Srgrimesnextfield(buf) 3951556Srgrimes char *buf; 39617987Speter{ 3971556Srgrimes register char *p, *q; 3981556Srgrimes 3991556Srgrimes p = linep; 4001556Srgrimes while (*p == ' ' || *p == '\t') 4011556Srgrimes p++; 4021556Srgrimes q = buf; 4031556Srgrimes while (*p != ' ' && *p != '\t' && *p != '\0') 4041556Srgrimes *q++ = *p++; 4051556Srgrimes *q = '\0'; 4061556Srgrimes linep = p; 4071556Srgrimes return (q > buf); 4081556Srgrimes} 4091556Srgrimes 4101556Srgrimes 41117987Speterstatic void 41217987Speterskipbl() 41317987Speter{ 4141556Srgrimes while (*linep == ' ' || *linep == '\t') 4151556Srgrimes linep++; 4161556Srgrimes} 4171556Srgrimes 4181556Srgrimes 41917987Speterstatic int 42017987Speterreadline() 42117987Speter{ 4221556Srgrimes register char *p; 4231556Srgrimes 4241556Srgrimes if (fgets(line, 1024, infp) == NULL) 4251556Srgrimes return 0; 4261556Srgrimes for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++); 4271556Srgrimes while (p > line && (p[-1] == ' ' || p[-1] == '\t')) 4281556Srgrimes p--; 4291556Srgrimes *p = '\0'; 4301556Srgrimes linep = line; 4311556Srgrimes linno++; 4321556Srgrimes if (p - line > BUFLEN) 4331556Srgrimes error("Line too long"); 4341556Srgrimes return 1; 4351556Srgrimes} 4361556Srgrimes 4371556Srgrimes 4381556Srgrimes 43917987Speterstatic void 44017987Speter#if __STDC__ 44117987Spetererror(const char *msg, ...) 44217987Speter#else 44317987Spetererror(va_alist) 44417987Speter va_dcl 44517987Speter#endif 44617987Speter{ 44717987Speter va_list va; 44817987Speter#if __STDC__ 44917987Speter va_start(va, msg); 45017987Speter#else 4511556Srgrimes char *msg; 45217987Speter va_start(va); 45317987Speter msg = va_arg(va, char *); 45417987Speter#endif 45517987Speter 45617987Speter (void) fprintf(stderr, "line %d: ", linno); 45717987Speter (void) vfprintf(stderr, msg, va); 45817987Speter (void) fputc('\n', stderr); 45917987Speter 46017987Speter va_end(va); 46117987Speter 4621556Srgrimes exit(2); 4631556Srgrimes} 4641556Srgrimes 4651556Srgrimes 4661556Srgrimes 46717987Speterstatic char * 4681556Srgrimessavestr(s) 46917987Speter const char *s; 47017987Speter{ 4711556Srgrimes register char *p; 4721556Srgrimes 4731556Srgrimes if ((p = malloc(strlen(s) + 1)) == NULL) 4741556Srgrimes error("Out of space"); 47517987Speter (void) strcpy(p, s); 4761556Srgrimes return p; 4771556Srgrimes} 478