mknodes.c revision 1556
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.
351556Srgrimes */
361556Srgrimes
371556Srgrimes#ifndef lint
381556Srgrimesstatic char copyright[] =
391556Srgrimes"@(#) Copyright (c) 1991, 1993\n\
401556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
411556Srgrimes#endif /* not lint */
421556Srgrimes
431556Srgrimes#ifndef lint
441556Srgrimesstatic char sccsid[] = "@(#)mknodes.c	8.1 (Berkeley) 5/31/93";
451556Srgrimes#endif /* not lint */
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>
531556Srgrimes
541556Srgrimes
551556Srgrimes#define MAXTYPES 50		/* max number of node types */
561556Srgrimes#define MAXFIELDS 20		/* max fields in a structure */
571556Srgrimes#define BUFLEN 100		/* size of character buffers */
581556Srgrimes
591556Srgrimes/* field types */
601556Srgrimes#define T_NODE 1		/* union node *field */
611556Srgrimes#define T_NODELIST 2		/* struct nodelist *field */
621556Srgrimes#define T_STRING 3
631556Srgrimes#define T_INT 4			/* int field */
641556Srgrimes#define T_OTHER 5		/* other */
651556Srgrimes#define T_TEMP 6		/* don't copy this field */
661556Srgrimes
671556Srgrimes
681556Srgrimesstruct field {			/* a structure field */
691556Srgrimes	char *name;		/* name of field */
701556Srgrimes	int type;			/* type of field */
711556Srgrimes	char *decl;		/* declaration of field */
721556Srgrimes};
731556Srgrimes
741556Srgrimes
751556Srgrimesstruct str {			/* struct representing a node structure */
761556Srgrimes	char *tag;		/* structure tag */
771556Srgrimes	int nfields;		/* number of fields in the structure */
781556Srgrimes	struct field field[MAXFIELDS];	/* the fields of the structure */
791556Srgrimes	int done;			/* set if fully parsed */
801556Srgrimes};
811556Srgrimes
821556Srgrimes
831556Srgrimesint ntypes;			/* number of node types */
841556Srgrimeschar *nodename[MAXTYPES];	/* names of the nodes */
851556Srgrimesstruct str *nodestr[MAXTYPES];	/* type of structure used by the node */
861556Srgrimesint nstr;			/* number of structures */
871556Srgrimesstruct str str[MAXTYPES];	/* the structures */
881556Srgrimesstruct str *curstr;		/* current structure */
891556Srgrimes
901556Srgrimes
911556SrgrimesFILE *infp = stdin;
921556Srgrimeschar line[1024];
931556Srgrimesint linno;
941556Srgrimeschar *linep;
951556Srgrimes
961556Srgrimes
971556Srgrimeschar *savestr();
981556Srgrimes#define equal(s1, s2)	(strcmp(s1, s2) == 0)
991556Srgrimes
1001556Srgrimes
1011556Srgrimesmain(argc, argv)
1021556Srgrimes	char **argv;
1031556Srgrimes	{
1041556Srgrimes	if (argc != 3)
1051556Srgrimes		error("usage: mknodes file\n");
1061556Srgrimes	if ((infp = fopen(argv[1], "r")) == NULL)
1071556Srgrimes		error("Can't open %s", argv[1]);
1081556Srgrimes	while (readline()) {
1091556Srgrimes		if (line[0] == ' ' || line[0] == '\t')
1101556Srgrimes			parsefield();
1111556Srgrimes		else if (line[0] != '\0')
1121556Srgrimes			parsenode();
1131556Srgrimes	}
1141556Srgrimes	output(argv[2]);
1151556Srgrimes	exit(0);
1161556Srgrimes}
1171556Srgrimes
1181556Srgrimes
1191556Srgrimes
1201556Srgrimesparsenode() {
1211556Srgrimes	char name[BUFLEN];
1221556Srgrimes	char tag[BUFLEN];
1231556Srgrimes	struct str *sp;
1241556Srgrimes
1251556Srgrimes	if (curstr && curstr->nfields > 0)
1261556Srgrimes		curstr->done = 1;
1271556Srgrimes	nextfield(name);
1281556Srgrimes	if (! nextfield(tag))
1291556Srgrimes		error("Tag expected");
1301556Srgrimes	if (*linep != '\0')
1311556Srgrimes		error("Garbage at end of line");
1321556Srgrimes	nodename[ntypes] = savestr(name);
1331556Srgrimes	for (sp = str ; sp < str + nstr ; sp++) {
1341556Srgrimes		if (equal(sp->tag, tag))
1351556Srgrimes			break;
1361556Srgrimes	}
1371556Srgrimes	if (sp >= str + nstr) {
1381556Srgrimes		sp->tag = savestr(tag);
1391556Srgrimes		sp->nfields = 0;
1401556Srgrimes		curstr = sp;
1411556Srgrimes		nstr++;
1421556Srgrimes	}
1431556Srgrimes	nodestr[ntypes] = sp;
1441556Srgrimes	ntypes++;
1451556Srgrimes}
1461556Srgrimes
1471556Srgrimes
1481556Srgrimesparsefield() {
1491556Srgrimes	char name[BUFLEN];
1501556Srgrimes	char type[BUFLEN];
1511556Srgrimes	char decl[2 * BUFLEN];
1521556Srgrimes	struct field *fp;
1531556Srgrimes
1541556Srgrimes	if (curstr == NULL || curstr->done)
1551556Srgrimes		error("No current structure to add field to");
1561556Srgrimes	if (! nextfield(name))
1571556Srgrimes		error("No field name");
1581556Srgrimes	if (! nextfield(type))
1591556Srgrimes		error("No field type");
1601556Srgrimes	fp = &curstr->field[curstr->nfields];
1611556Srgrimes	fp->name = savestr(name);
1621556Srgrimes	if (equal(type, "nodeptr")) {
1631556Srgrimes		fp->type = T_NODE;
1641556Srgrimes		sprintf(decl, "union node *%s", name);
1651556Srgrimes	} else if (equal(type, "nodelist")) {
1661556Srgrimes		fp->type = T_NODELIST;
1671556Srgrimes		sprintf(decl, "struct nodelist *%s", name);
1681556Srgrimes	} else if (equal(type, "string")) {
1691556Srgrimes		fp->type = T_STRING;
1701556Srgrimes		sprintf(decl, "char *%s", name);
1711556Srgrimes	} else if (equal(type, "int")) {
1721556Srgrimes		fp->type = T_INT;
1731556Srgrimes		sprintf(decl, "int %s", name);
1741556Srgrimes	} else if (equal(type, "other")) {
1751556Srgrimes		fp->type = T_OTHER;
1761556Srgrimes	} else if (equal(type, "temp")) {
1771556Srgrimes		fp->type = T_TEMP;
1781556Srgrimes	} else {
1791556Srgrimes		error("Unknown type %s", type);
1801556Srgrimes	}
1811556Srgrimes	if (fp->type == T_OTHER || fp->type == T_TEMP) {
1821556Srgrimes		skipbl();
1831556Srgrimes		fp->decl = savestr(linep);
1841556Srgrimes	} else {
1851556Srgrimes		if (*linep)
1861556Srgrimes			error("Garbage at end of line");
1871556Srgrimes		fp->decl = savestr(decl);
1881556Srgrimes	}
1891556Srgrimes	curstr->nfields++;
1901556Srgrimes}
1911556Srgrimes
1921556Srgrimes
1931556Srgrimeschar writer[] = "\
1941556Srgrimes/*\n\
1951556Srgrimes * This file was generated by the mknodes program.\n\
1961556Srgrimes */\n\
1971556Srgrimes\n";
1981556Srgrimes
1991556Srgrimesoutput(file)
2001556Srgrimes	char *file;
2011556Srgrimes	{
2021556Srgrimes	FILE *hfile;
2031556Srgrimes	FILE *cfile;
2041556Srgrimes	FILE *patfile;
2051556Srgrimes	int i;
2061556Srgrimes	struct str *sp;
2071556Srgrimes	struct field *fp;
2081556Srgrimes	char *p;
2091556Srgrimes
2101556Srgrimes	if ((patfile = fopen(file, "r")) == NULL)
2111556Srgrimes		error("Can't open %s", file);
2121556Srgrimes	if ((hfile = fopen("nodes.h", "w")) == NULL)
2131556Srgrimes		error("Can't create nodes.h");
2141556Srgrimes	if ((cfile = fopen("nodes.c", "w")) == NULL)
2151556Srgrimes		error("Can't create nodes.c");
2161556Srgrimes	fputs(writer, hfile);
2171556Srgrimes	for (i = 0 ; i < ntypes ; i++)
2181556Srgrimes		fprintf(hfile, "#define %s %d\n", nodename[i], i);
2191556Srgrimes	fputs("\n\n\n", hfile);
2201556Srgrimes	for (sp = str ; sp < &str[nstr] ; sp++) {
2211556Srgrimes		fprintf(hfile, "struct %s {\n", sp->tag);
2221556Srgrimes		for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
2231556Srgrimes			fprintf(hfile, "      %s;\n", fp->decl);
2241556Srgrimes		}
2251556Srgrimes		fputs("};\n\n\n", hfile);
2261556Srgrimes	}
2271556Srgrimes	fputs("union node {\n", hfile);
2281556Srgrimes	fprintf(hfile, "      int type;\n");
2291556Srgrimes	for (sp = str ; sp < &str[nstr] ; sp++) {
2301556Srgrimes		fprintf(hfile, "      struct %s %s;\n", sp->tag, sp->tag);
2311556Srgrimes	}
2321556Srgrimes	fputs("};\n\n\n", hfile);
2331556Srgrimes	fputs("struct nodelist {\n", hfile);
2341556Srgrimes	fputs("\tstruct nodelist *next;\n", hfile);
2351556Srgrimes	fputs("\tunion node *n;\n", hfile);
2361556Srgrimes	fputs("};\n\n\n", hfile);
2371556Srgrimes	fputs("#ifdef __STDC__\n", hfile);
2381556Srgrimes	fputs("union node *copyfunc(union node *);\n", hfile);
2391556Srgrimes	fputs("void freefunc(union node *);\n", hfile);
2401556Srgrimes	fputs("#else\n", hfile);
2411556Srgrimes	fputs("union node *copyfunc();\n", hfile);
2421556Srgrimes	fputs("void freefunc();\n", hfile);
2431556Srgrimes	fputs("#endif\n", hfile);
2441556Srgrimes
2451556Srgrimes	fputs(writer, cfile);
2461556Srgrimes	while (fgets(line, sizeof line, patfile) != NULL) {
2471556Srgrimes		for (p = line ; *p == ' ' || *p == '\t' ; p++);
2481556Srgrimes		if (equal(p, "%SIZES\n"))
2491556Srgrimes			outsizes(cfile);
2501556Srgrimes		else if (equal(p, "%CALCSIZE\n"))
2511556Srgrimes			outfunc(cfile, 1);
2521556Srgrimes		else if (equal(p, "%COPY\n"))
2531556Srgrimes			outfunc(cfile, 0);
2541556Srgrimes		else
2551556Srgrimes			fputs(line, cfile);
2561556Srgrimes	}
2571556Srgrimes}
2581556Srgrimes
2591556Srgrimes
2601556Srgrimes
2611556Srgrimesoutsizes(cfile)
2621556Srgrimes	FILE *cfile;
2631556Srgrimes	{
2641556Srgrimes	int i;
2651556Srgrimes
2661556Srgrimes	fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
2671556Srgrimes	for (i = 0 ; i < ntypes ; i++) {
2681556Srgrimes		fprintf(cfile, "      ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
2691556Srgrimes	}
2701556Srgrimes	fprintf(cfile, "};\n");
2711556Srgrimes}
2721556Srgrimes
2731556Srgrimes
2741556Srgrimesoutfunc(cfile, calcsize)
2751556Srgrimes	FILE *cfile;
2761556Srgrimes	{
2771556Srgrimes	struct str *sp;
2781556Srgrimes	struct field *fp;
2791556Srgrimes	int i;
2801556Srgrimes
2811556Srgrimes	fputs("      if (n == NULL)\n", cfile);
2821556Srgrimes	if (calcsize)
2831556Srgrimes		fputs("	    return;\n", cfile);
2841556Srgrimes	else
2851556Srgrimes		fputs("	    return NULL;\n", cfile);
2861556Srgrimes	if (calcsize)
2871556Srgrimes		fputs("      funcblocksize += nodesize[n->type];\n", cfile);
2881556Srgrimes	else {
2891556Srgrimes		fputs("      new = funcblock;\n", cfile);
2901556Srgrimes		fputs("      funcblock += nodesize[n->type];\n", cfile);
2911556Srgrimes	}
2921556Srgrimes	fputs("      switch (n->type) {\n", cfile);
2931556Srgrimes	for (sp = str ; sp < &str[nstr] ; sp++) {
2941556Srgrimes		for (i = 0 ; i < ntypes ; i++) {
2951556Srgrimes			if (nodestr[i] == sp)
2961556Srgrimes				fprintf(cfile, "      case %s:\n", nodename[i]);
2971556Srgrimes		}
2981556Srgrimes		for (i = sp->nfields ; --i >= 1 ; ) {
2991556Srgrimes			fp = &sp->field[i];
3001556Srgrimes			switch (fp->type) {
3011556Srgrimes			case T_NODE:
3021556Srgrimes				if (calcsize) {
3031556Srgrimes					indent(12, cfile);
3041556Srgrimes					fprintf(cfile, "calcsize(n->%s.%s);\n",
3051556Srgrimes						sp->tag, fp->name);
3061556Srgrimes				} else {
3071556Srgrimes					indent(12, cfile);
3081556Srgrimes					fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
3091556Srgrimes						sp->tag, fp->name, sp->tag, fp->name);
3101556Srgrimes				}
3111556Srgrimes				break;
3121556Srgrimes			case T_NODELIST:
3131556Srgrimes				if (calcsize) {
3141556Srgrimes					indent(12, cfile);
3151556Srgrimes					fprintf(cfile, "sizenodelist(n->%s.%s);\n",
3161556Srgrimes						sp->tag, fp->name);
3171556Srgrimes				} else {
3181556Srgrimes					indent(12, cfile);
3191556Srgrimes					fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
3201556Srgrimes						sp->tag, fp->name, sp->tag, fp->name);
3211556Srgrimes				}
3221556Srgrimes				break;
3231556Srgrimes			case T_STRING:
3241556Srgrimes				if (calcsize) {
3251556Srgrimes					indent(12, cfile);
3261556Srgrimes					fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
3271556Srgrimes						sp->tag, fp->name);
3281556Srgrimes				} else {
3291556Srgrimes					indent(12, cfile);
3301556Srgrimes					fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
3311556Srgrimes						sp->tag, fp->name, sp->tag, fp->name);
3321556Srgrimes				}
3331556Srgrimes				break;
3341556Srgrimes			case T_INT:
3351556Srgrimes			case T_OTHER:
3361556Srgrimes				if (! calcsize) {
3371556Srgrimes					indent(12, cfile);
3381556Srgrimes					fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
3391556Srgrimes						sp->tag, fp->name, sp->tag, fp->name);
3401556Srgrimes				}
3411556Srgrimes				break;
3421556Srgrimes			}
3431556Srgrimes		}
3441556Srgrimes		indent(12, cfile);
3451556Srgrimes		fputs("break;\n", cfile);
3461556Srgrimes	}
3471556Srgrimes	fputs("      };\n", cfile);
3481556Srgrimes	if (! calcsize)
3491556Srgrimes		fputs("      new->type = n->type;\n", cfile);
3501556Srgrimes}
3511556Srgrimes
3521556Srgrimes
3531556Srgrimesindent(amount, fp)
3541556Srgrimes	FILE *fp;
3551556Srgrimes	{
3561556Srgrimes	while (amount >= 8) {
3571556Srgrimes		putc('\t', fp);
3581556Srgrimes		amount -= 8;
3591556Srgrimes	}
3601556Srgrimes	while (--amount >= 0) {
3611556Srgrimes		putc(' ', fp);
3621556Srgrimes	}
3631556Srgrimes}
3641556Srgrimes
3651556Srgrimes
3661556Srgrimesint
3671556Srgrimesnextfield(buf)
3681556Srgrimes	char *buf;
3691556Srgrimes	{
3701556Srgrimes	register char *p, *q;
3711556Srgrimes
3721556Srgrimes	p = linep;
3731556Srgrimes	while (*p == ' ' || *p == '\t')
3741556Srgrimes		p++;
3751556Srgrimes	q = buf;
3761556Srgrimes	while (*p != ' ' && *p != '\t' && *p != '\0')
3771556Srgrimes		*q++ = *p++;
3781556Srgrimes	*q = '\0';
3791556Srgrimes	linep = p;
3801556Srgrimes	return (q > buf);
3811556Srgrimes}
3821556Srgrimes
3831556Srgrimes
3841556Srgrimesskipbl() {
3851556Srgrimes	while (*linep == ' ' || *linep == '\t')
3861556Srgrimes		linep++;
3871556Srgrimes}
3881556Srgrimes
3891556Srgrimes
3901556Srgrimesint
3911556Srgrimesreadline() {
3921556Srgrimes	register char *p;
3931556Srgrimes
3941556Srgrimes	if (fgets(line, 1024, infp) == NULL)
3951556Srgrimes		return 0;
3961556Srgrimes	for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
3971556Srgrimes	while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
3981556Srgrimes		p--;
3991556Srgrimes	*p = '\0';
4001556Srgrimes	linep = line;
4011556Srgrimes	linno++;
4021556Srgrimes	if (p - line > BUFLEN)
4031556Srgrimes		error("Line too long");
4041556Srgrimes	return 1;
4051556Srgrimes}
4061556Srgrimes
4071556Srgrimes
4081556Srgrimes
4091556Srgrimeserror(msg, a1, a2, a3, a4, a5, a6)
4101556Srgrimes	char *msg;
4111556Srgrimes	{
4121556Srgrimes	fprintf(stderr, "line %d: ", linno);
4131556Srgrimes	fprintf(stderr, msg, a1, a2, a3, a4, a5, a6);
4141556Srgrimes	putc('\n', stderr);
4151556Srgrimes	exit(2);
4161556Srgrimes}
4171556Srgrimes
4181556Srgrimes
4191556Srgrimes
4201556Srgrimeschar *
4211556Srgrimessavestr(s)
4221556Srgrimes	char *s;
4231556Srgrimes	{
4241556Srgrimes	register char *p;
4251556Srgrimes	char *malloc();
4261556Srgrimes
4271556Srgrimes	if ((p = malloc(strlen(s) + 1)) == NULL)
4281556Srgrimes		error("Out of space");
4291556Srgrimes	strcpy(p, s);
4301556Srgrimes	return p;
4311556Srgrimes}
432