mknodes.c revision 36150
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
3820425Sstevestatic char const 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
4436150Scharnier#if 0
4536150Scharnierstatic char sccsid[] = "@(#)mknodes.c	8.2 (Berkeley) 5/4/95";
4636150Scharnier#endif
4736150Scharnierstatic const char rcsid[] =
4836150Scharnier	"$Id$";
491556Srgrimes#endif /* not lint */
501556Srgrimes
511556Srgrimes/*
521556Srgrimes * This program reads the nodetypes file and nodes.c.pat file.  It generates
531556Srgrimes * the files nodes.h and nodes.c.
541556Srgrimes */
551556Srgrimes
561556Srgrimes#include <stdio.h>
5717987Speter#include <stdlib.h>
5817987Speter#include <string.h>
5925226Ssteve#ifdef __STDC__
6017987Speter#include <stdarg.h>
6117987Speter#else
6217987Speter#include <varargs.h>
6317987Speter#endif
641556Srgrimes
651556Srgrimes
661556Srgrimes#define MAXTYPES 50		/* max number of node types */
671556Srgrimes#define MAXFIELDS 20		/* max fields in a structure */
681556Srgrimes#define BUFLEN 100		/* size of character buffers */
691556Srgrimes
701556Srgrimes/* field types */
711556Srgrimes#define T_NODE 1		/* union node *field */
721556Srgrimes#define T_NODELIST 2		/* struct nodelist *field */
731556Srgrimes#define T_STRING 3
741556Srgrimes#define T_INT 4			/* int field */
751556Srgrimes#define T_OTHER 5		/* other */
761556Srgrimes#define T_TEMP 6		/* don't copy this field */
771556Srgrimes
781556Srgrimes
791556Srgrimesstruct field {			/* a structure field */
801556Srgrimes	char *name;		/* name of field */
811556Srgrimes	int type;			/* type of field */
821556Srgrimes	char *decl;		/* declaration of field */
831556Srgrimes};
841556Srgrimes
851556Srgrimes
861556Srgrimesstruct str {			/* struct representing a node structure */
871556Srgrimes	char *tag;		/* structure tag */
881556Srgrimes	int nfields;		/* number of fields in the structure */
891556Srgrimes	struct field field[MAXFIELDS];	/* the fields of the structure */
901556Srgrimes	int done;			/* set if fully parsed */
911556Srgrimes};
921556Srgrimes
931556Srgrimes
9417987Speterstatic int ntypes;			/* number of node types */
9517987Speterstatic char *nodename[MAXTYPES];	/* names of the nodes */
9617987Speterstatic struct str *nodestr[MAXTYPES];	/* type of structure used by the node */
9717987Speterstatic int nstr;			/* number of structures */
9817987Speterstatic struct str str[MAXTYPES];	/* the structures */
9917987Speterstatic struct str *curstr;		/* current structure */
10017987Speterstatic FILE *infp = stdin;
10117987Speterstatic char line[1024];
10217987Speterstatic int linno;
10317987Speterstatic char *linep;
1041556Srgrimes
10517987Speterstatic void parsenode __P((void));
10617987Speterstatic void parsefield __P((void));
10717987Speterstatic void output __P((char *));
10817987Speterstatic void outsizes __P((FILE *));
10917987Speterstatic void outfunc __P((FILE *, int));
11017987Speterstatic void indent __P((int, FILE *));
11117987Speterstatic int nextfield __P((char *));
11217987Speterstatic void skipbl __P((void));
11317987Speterstatic int readline __P((void));
11417987Speterstatic void error __P((const char *, ...));
11517987Speterstatic char *savestr __P((const char *));
1161556Srgrimes
1171556Srgrimes
11817987Speterint
1191556Srgrimesmain(argc, argv)
12017987Speter	int argc;
1211556Srgrimes	char **argv;
12217987Speter{
1231556Srgrimes	if (argc != 3)
12418016Speter		error("usage: mknodes file");
1251556Srgrimes	if ((infp = fopen(argv[1], "r")) == NULL)
1261556Srgrimes		error("Can't open %s", argv[1]);
1271556Srgrimes	while (readline()) {
1281556Srgrimes		if (line[0] == ' ' || line[0] == '\t')
1291556Srgrimes			parsefield();
1301556Srgrimes		else if (line[0] != '\0')
1311556Srgrimes			parsenode();
1321556Srgrimes	}
1331556Srgrimes	output(argv[2]);
1341556Srgrimes	exit(0);
1351556Srgrimes}
1361556Srgrimes
1371556Srgrimes
1381556Srgrimes
13917987Speterstatic void
14017987Speterparsenode()
14117987Speter{
1421556Srgrimes	char name[BUFLEN];
1431556Srgrimes	char tag[BUFLEN];
1441556Srgrimes	struct str *sp;
1451556Srgrimes
1461556Srgrimes	if (curstr && curstr->nfields > 0)
1471556Srgrimes		curstr->done = 1;
1481556Srgrimes	nextfield(name);
1491556Srgrimes	if (! nextfield(tag))
1501556Srgrimes		error("Tag expected");
1511556Srgrimes	if (*linep != '\0')
1521556Srgrimes		error("Garbage at end of line");
1531556Srgrimes	nodename[ntypes] = savestr(name);
1541556Srgrimes	for (sp = str ; sp < str + nstr ; sp++) {
15517987Speter		if (strcmp(sp->tag, tag) == 0)
1561556Srgrimes			break;
1571556Srgrimes	}
1581556Srgrimes	if (sp >= str + nstr) {
1591556Srgrimes		sp->tag = savestr(tag);
1601556Srgrimes		sp->nfields = 0;
1611556Srgrimes		curstr = sp;
1621556Srgrimes		nstr++;
1631556Srgrimes	}
1641556Srgrimes	nodestr[ntypes] = sp;
1651556Srgrimes	ntypes++;
1661556Srgrimes}
1671556Srgrimes
1681556Srgrimes
16917987Speterstatic void
17017987Speterparsefield()
17117987Speter{
1721556Srgrimes	char name[BUFLEN];
1731556Srgrimes	char type[BUFLEN];
1741556Srgrimes	char decl[2 * BUFLEN];
1751556Srgrimes	struct field *fp;
1761556Srgrimes
1771556Srgrimes	if (curstr == NULL || curstr->done)
1781556Srgrimes		error("No current structure to add field to");
1791556Srgrimes	if (! nextfield(name))
1801556Srgrimes		error("No field name");
1811556Srgrimes	if (! nextfield(type))
1821556Srgrimes		error("No field type");
1831556Srgrimes	fp = &curstr->field[curstr->nfields];
1841556Srgrimes	fp->name = savestr(name);
18517987Speter	if (strcmp(type, "nodeptr") == 0) {
1861556Srgrimes		fp->type = T_NODE;
1871556Srgrimes		sprintf(decl, "union node *%s", name);
18817987Speter	} else if (strcmp(type, "nodelist") == 0) {
1891556Srgrimes		fp->type = T_NODELIST;
1901556Srgrimes		sprintf(decl, "struct nodelist *%s", name);
19117987Speter	} else if (strcmp(type, "string") == 0) {
1921556Srgrimes		fp->type = T_STRING;
1931556Srgrimes		sprintf(decl, "char *%s", name);
19417987Speter	} else if (strcmp(type, "int") == 0) {
1951556Srgrimes		fp->type = T_INT;
1961556Srgrimes		sprintf(decl, "int %s", name);
19717987Speter	} else if (strcmp(type, "other") == 0) {
1981556Srgrimes		fp->type = T_OTHER;
19917987Speter	} else if (strcmp(type, "temp") == 0) {
2001556Srgrimes		fp->type = T_TEMP;
2011556Srgrimes	} else {
2021556Srgrimes		error("Unknown type %s", type);
2031556Srgrimes	}
2041556Srgrimes	if (fp->type == T_OTHER || fp->type == T_TEMP) {
2051556Srgrimes		skipbl();
2061556Srgrimes		fp->decl = savestr(linep);
2071556Srgrimes	} else {
2081556Srgrimes		if (*linep)
2091556Srgrimes			error("Garbage at end of line");
2101556Srgrimes		fp->decl = savestr(decl);
2111556Srgrimes	}
2121556Srgrimes	curstr->nfields++;
2131556Srgrimes}
2141556Srgrimes
2151556Srgrimes
2161556Srgrimeschar writer[] = "\
2171556Srgrimes/*\n\
2181556Srgrimes * This file was generated by the mknodes program.\n\
2191556Srgrimes */\n\
2201556Srgrimes\n";
2211556Srgrimes
22217987Speterstatic void
2231556Srgrimesoutput(file)
2241556Srgrimes	char *file;
22517987Speter{
2261556Srgrimes	FILE *hfile;
2271556Srgrimes	FILE *cfile;
2281556Srgrimes	FILE *patfile;
2291556Srgrimes	int i;
2301556Srgrimes	struct str *sp;
2311556Srgrimes	struct field *fp;
2321556Srgrimes	char *p;
2331556Srgrimes
2341556Srgrimes	if ((patfile = fopen(file, "r")) == NULL)
2351556Srgrimes		error("Can't open %s", file);
2361556Srgrimes	if ((hfile = fopen("nodes.h", "w")) == NULL)
2371556Srgrimes		error("Can't create nodes.h");
2381556Srgrimes	if ((cfile = fopen("nodes.c", "w")) == NULL)
2391556Srgrimes		error("Can't create nodes.c");
2401556Srgrimes	fputs(writer, hfile);
2411556Srgrimes	for (i = 0 ; i < ntypes ; i++)
2421556Srgrimes		fprintf(hfile, "#define %s %d\n", nodename[i], i);
2431556Srgrimes	fputs("\n\n\n", hfile);
2441556Srgrimes	for (sp = str ; sp < &str[nstr] ; sp++) {
2451556Srgrimes		fprintf(hfile, "struct %s {\n", sp->tag);
2461556Srgrimes		for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
2471556Srgrimes			fprintf(hfile, "      %s;\n", fp->decl);
2481556Srgrimes		}
2491556Srgrimes		fputs("};\n\n\n", hfile);
2501556Srgrimes	}
2511556Srgrimes	fputs("union node {\n", hfile);
2521556Srgrimes	fprintf(hfile, "      int type;\n");
2531556Srgrimes	for (sp = str ; sp < &str[nstr] ; sp++) {
2541556Srgrimes		fprintf(hfile, "      struct %s %s;\n", sp->tag, sp->tag);
2551556Srgrimes	}
2561556Srgrimes	fputs("};\n\n\n", hfile);
2571556Srgrimes	fputs("struct nodelist {\n", hfile);
2581556Srgrimes	fputs("\tstruct nodelist *next;\n", hfile);
2591556Srgrimes	fputs("\tunion node *n;\n", hfile);
2601556Srgrimes	fputs("};\n\n\n", hfile);
2611556Srgrimes	fputs("#ifdef __STDC__\n", hfile);
2621556Srgrimes	fputs("union node *copyfunc(union node *);\n", hfile);
2631556Srgrimes	fputs("void freefunc(union node *);\n", hfile);
2641556Srgrimes	fputs("#else\n", hfile);
2651556Srgrimes	fputs("union node *copyfunc();\n", hfile);
2661556Srgrimes	fputs("void freefunc();\n", hfile);
2671556Srgrimes	fputs("#endif\n", hfile);
2681556Srgrimes
2691556Srgrimes	fputs(writer, cfile);
2701556Srgrimes	while (fgets(line, sizeof line, patfile) != NULL) {
2711556Srgrimes		for (p = line ; *p == ' ' || *p == '\t' ; p++);
27217987Speter		if (strcmp(p, "%SIZES\n") == 0)
2731556Srgrimes			outsizes(cfile);
27417987Speter		else if (strcmp(p, "%CALCSIZE\n") == 0)
2751556Srgrimes			outfunc(cfile, 1);
27617987Speter		else if (strcmp(p, "%COPY\n") == 0)
2771556Srgrimes			outfunc(cfile, 0);
2781556Srgrimes		else
2791556Srgrimes			fputs(line, cfile);
2801556Srgrimes	}
2811556Srgrimes}
2821556Srgrimes
2831556Srgrimes
2841556Srgrimes
28517987Speterstatic void
2861556Srgrimesoutsizes(cfile)
2871556Srgrimes	FILE *cfile;
28817987Speter{
2891556Srgrimes	int i;
2901556Srgrimes
2911556Srgrimes	fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
2921556Srgrimes	for (i = 0 ; i < ntypes ; i++) {
2931556Srgrimes		fprintf(cfile, "      ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
2941556Srgrimes	}
2951556Srgrimes	fprintf(cfile, "};\n");
2961556Srgrimes}
2971556Srgrimes
2981556Srgrimes
29917987Speterstatic void
3001556Srgrimesoutfunc(cfile, calcsize)
3011556Srgrimes	FILE *cfile;
30217987Speter	int calcsize;
30317987Speter{
3041556Srgrimes	struct str *sp;
3051556Srgrimes	struct field *fp;
3061556Srgrimes	int i;
3071556Srgrimes
3081556Srgrimes	fputs("      if (n == NULL)\n", cfile);
3091556Srgrimes	if (calcsize)
3101556Srgrimes		fputs("	    return;\n", cfile);
3111556Srgrimes	else
3121556Srgrimes		fputs("	    return NULL;\n", cfile);
3131556Srgrimes	if (calcsize)
3141556Srgrimes		fputs("      funcblocksize += nodesize[n->type];\n", cfile);
3151556Srgrimes	else {
3161556Srgrimes		fputs("      new = funcblock;\n", cfile);
31725226Ssteve		fputs("      funcblock = (char *)funcblock + nodesize[n->type];\n", cfile);
3181556Srgrimes	}
3191556Srgrimes	fputs("      switch (n->type) {\n", cfile);
3201556Srgrimes	for (sp = str ; sp < &str[nstr] ; sp++) {
3211556Srgrimes		for (i = 0 ; i < ntypes ; i++) {
3221556Srgrimes			if (nodestr[i] == sp)
3231556Srgrimes				fprintf(cfile, "      case %s:\n", nodename[i]);
3241556Srgrimes		}
3251556Srgrimes		for (i = sp->nfields ; --i >= 1 ; ) {
3261556Srgrimes			fp = &sp->field[i];
3271556Srgrimes			switch (fp->type) {
3281556Srgrimes			case T_NODE:
3291556Srgrimes				if (calcsize) {
3301556Srgrimes					indent(12, cfile);
3311556Srgrimes					fprintf(cfile, "calcsize(n->%s.%s);\n",
3321556Srgrimes						sp->tag, fp->name);
3331556Srgrimes				} else {
3341556Srgrimes					indent(12, cfile);
3351556Srgrimes					fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
3361556Srgrimes						sp->tag, fp->name, sp->tag, fp->name);
3371556Srgrimes				}
3381556Srgrimes				break;
3391556Srgrimes			case T_NODELIST:
3401556Srgrimes				if (calcsize) {
3411556Srgrimes					indent(12, cfile);
3421556Srgrimes					fprintf(cfile, "sizenodelist(n->%s.%s);\n",
3431556Srgrimes						sp->tag, fp->name);
3441556Srgrimes				} else {
3451556Srgrimes					indent(12, cfile);
3461556Srgrimes					fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
3471556Srgrimes						sp->tag, fp->name, sp->tag, fp->name);
3481556Srgrimes				}
3491556Srgrimes				break;
3501556Srgrimes			case T_STRING:
3511556Srgrimes				if (calcsize) {
3521556Srgrimes					indent(12, cfile);
3531556Srgrimes					fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
3541556Srgrimes						sp->tag, fp->name);
3551556Srgrimes				} else {
3561556Srgrimes					indent(12, cfile);
3571556Srgrimes					fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
3581556Srgrimes						sp->tag, fp->name, sp->tag, fp->name);
3591556Srgrimes				}
3601556Srgrimes				break;
3611556Srgrimes			case T_INT:
3621556Srgrimes			case T_OTHER:
3631556Srgrimes				if (! calcsize) {
3641556Srgrimes					indent(12, cfile);
3651556Srgrimes					fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
3661556Srgrimes						sp->tag, fp->name, sp->tag, fp->name);
3671556Srgrimes				}
3681556Srgrimes				break;
3691556Srgrimes			}
3701556Srgrimes		}
3711556Srgrimes		indent(12, cfile);
3721556Srgrimes		fputs("break;\n", cfile);
3731556Srgrimes	}
3741556Srgrimes	fputs("      };\n", cfile);
3751556Srgrimes	if (! calcsize)
3761556Srgrimes		fputs("      new->type = n->type;\n", cfile);
3771556Srgrimes}
3781556Srgrimes
3791556Srgrimes
38017987Speterstatic void
3811556Srgrimesindent(amount, fp)
38217987Speter	int amount;
3831556Srgrimes	FILE *fp;
38417987Speter{
3851556Srgrimes	while (amount >= 8) {
3861556Srgrimes		putc('\t', fp);
3871556Srgrimes		amount -= 8;
3881556Srgrimes	}
3891556Srgrimes	while (--amount >= 0) {
3901556Srgrimes		putc(' ', fp);
3911556Srgrimes	}
3921556Srgrimes}
3931556Srgrimes
3941556Srgrimes
39517987Speterstatic int
3961556Srgrimesnextfield(buf)
3971556Srgrimes	char *buf;
39817987Speter{
39925226Ssteve	char *p, *q;
4001556Srgrimes
4011556Srgrimes	p = linep;
4021556Srgrimes	while (*p == ' ' || *p == '\t')
4031556Srgrimes		p++;
4041556Srgrimes	q = buf;
4051556Srgrimes	while (*p != ' ' && *p != '\t' && *p != '\0')
4061556Srgrimes		*q++ = *p++;
4071556Srgrimes	*q = '\0';
4081556Srgrimes	linep = p;
4091556Srgrimes	return (q > buf);
4101556Srgrimes}
4111556Srgrimes
4121556Srgrimes
41317987Speterstatic void
41417987Speterskipbl()
41517987Speter{
4161556Srgrimes	while (*linep == ' ' || *linep == '\t')
4171556Srgrimes		linep++;
4181556Srgrimes}
4191556Srgrimes
4201556Srgrimes
42117987Speterstatic int
42217987Speterreadline()
42317987Speter{
42425226Ssteve	char *p;
4251556Srgrimes
4261556Srgrimes	if (fgets(line, 1024, infp) == NULL)
4271556Srgrimes		return 0;
4281556Srgrimes	for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
4291556Srgrimes	while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
4301556Srgrimes		p--;
4311556Srgrimes	*p = '\0';
4321556Srgrimes	linep = line;
4331556Srgrimes	linno++;
4341556Srgrimes	if (p - line > BUFLEN)
4351556Srgrimes		error("Line too long");
4361556Srgrimes	return 1;
4371556Srgrimes}
4381556Srgrimes
4391556Srgrimes
4401556Srgrimes
44117987Speterstatic void
44225226Ssteve#ifdef __STDC__
44317987Spetererror(const char *msg, ...)
44417987Speter#else
44517987Spetererror(va_alist)
44617987Speter	va_dcl
44717987Speter#endif
44817987Speter{
44917987Speter	va_list va;
45025226Ssteve#ifdef __STDC__
45117987Speter	va_start(va, msg);
45217987Speter#else
4531556Srgrimes	char *msg;
45417987Speter	va_start(va);
45517987Speter	msg = va_arg(va, char *);
45617987Speter#endif
45717987Speter
45817987Speter	(void) fprintf(stderr, "line %d: ", linno);
45917987Speter	(void) vfprintf(stderr, msg, va);
46017987Speter	(void) fputc('\n', stderr);
46117987Speter
46217987Speter	va_end(va);
46317987Speter
4641556Srgrimes	exit(2);
4651556Srgrimes}
4661556Srgrimes
4671556Srgrimes
4681556Srgrimes
46917987Speterstatic char *
4701556Srgrimessavestr(s)
47117987Speter	const char *s;
47217987Speter{
47325226Ssteve	char *p;
4741556Srgrimes
4751556Srgrimes	if ((p = malloc(strlen(s) + 1)) == NULL)
4761556Srgrimes		error("Out of space");
47717987Speter	(void) strcpy(p, s);
4781556Srgrimes	return p;
4791556Srgrimes}
480