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 */
9217987Speterstatic char line[1024];
9317987Speterstatic int linno;
9417987Speterstatic char *linep;
951556Srgrimes
9690111Simpstatic void parsenode(void);
9790111Simpstatic void parsefield(void);
9890111Simpstatic void output(char *);
9990111Simpstatic void outsizes(FILE *);
10090111Simpstatic void outfunc(FILE *, int);
10190111Simpstatic void indent(int, FILE *);
10290111Simpstatic int nextfield(char *);
10390111Simpstatic void skipbl(void);
104292786Sjillesstatic int readline(FILE *);
105181269Scpercivastatic void error(const char *, ...) __printf0like(1, 2) __dead2;
10690111Simpstatic char *savestr(const char *);
1071556Srgrimes
1081556Srgrimes
10917987Speterint
11090111Simpmain(int argc, char *argv[])
11117987Speter{
112292786Sjilles	FILE *infp;
113292786Sjilles
1141556Srgrimes	if (argc != 3)
11518016Speter		error("usage: mknodes file");
1161556Srgrimes	if ((infp = fopen(argv[1], "r")) == NULL)
11753891Scracauer		error("Can't open %s: %s", argv[1], strerror(errno));
118292786Sjilles	while (readline(infp)) {
1191556Srgrimes		if (line[0] == ' ' || line[0] == '\t')
1201556Srgrimes			parsefield();
1211556Srgrimes		else if (line[0] != '\0')
1221556Srgrimes			parsenode();
1231556Srgrimes	}
124292786Sjilles	fclose(infp);
1251556Srgrimes	output(argv[2]);
1261556Srgrimes	exit(0);
1271556Srgrimes}
1281556Srgrimes
1291556Srgrimes
1301556Srgrimes
13117987Speterstatic void
13290111Simpparsenode(void)
13317987Speter{
1341556Srgrimes	char name[BUFLEN];
1351556Srgrimes	char tag[BUFLEN];
1361556Srgrimes	struct str *sp;
1371556Srgrimes
1381556Srgrimes	if (curstr && curstr->nfields > 0)
1391556Srgrimes		curstr->done = 1;
1401556Srgrimes	nextfield(name);
1411556Srgrimes	if (! nextfield(tag))
1421556Srgrimes		error("Tag expected");
1431556Srgrimes	if (*linep != '\0')
1441556Srgrimes		error("Garbage at end of line");
1451556Srgrimes	nodename[ntypes] = savestr(name);
1461556Srgrimes	for (sp = str ; sp < str + nstr ; sp++) {
14717987Speter		if (strcmp(sp->tag, tag) == 0)
1481556Srgrimes			break;
1491556Srgrimes	}
1501556Srgrimes	if (sp >= str + nstr) {
1511556Srgrimes		sp->tag = savestr(tag);
1521556Srgrimes		sp->nfields = 0;
1531556Srgrimes		curstr = sp;
1541556Srgrimes		nstr++;
1551556Srgrimes	}
1561556Srgrimes	nodestr[ntypes] = sp;
1571556Srgrimes	ntypes++;
1581556Srgrimes}
1591556Srgrimes
1601556Srgrimes
16117987Speterstatic void
16290111Simpparsefield(void)
16317987Speter{
1641556Srgrimes	char name[BUFLEN];
1651556Srgrimes	char type[BUFLEN];
1661556Srgrimes	char decl[2 * BUFLEN];
1671556Srgrimes	struct field *fp;
1681556Srgrimes
1691556Srgrimes	if (curstr == NULL || curstr->done)
1701556Srgrimes		error("No current structure to add field to");
1711556Srgrimes	if (! nextfield(name))
1721556Srgrimes		error("No field name");
1731556Srgrimes	if (! nextfield(type))
1741556Srgrimes		error("No field type");
1751556Srgrimes	fp = &curstr->field[curstr->nfields];
1761556Srgrimes	fp->name = savestr(name);
17717987Speter	if (strcmp(type, "nodeptr") == 0) {
1781556Srgrimes		fp->type = T_NODE;
1791556Srgrimes		sprintf(decl, "union node *%s", name);
18017987Speter	} else if (strcmp(type, "nodelist") == 0) {
1811556Srgrimes		fp->type = T_NODELIST;
1821556Srgrimes		sprintf(decl, "struct nodelist *%s", name);
18317987Speter	} else if (strcmp(type, "string") == 0) {
1841556Srgrimes		fp->type = T_STRING;
1851556Srgrimes		sprintf(decl, "char *%s", name);
18617987Speter	} else if (strcmp(type, "int") == 0) {
1871556Srgrimes		fp->type = T_INT;
1881556Srgrimes		sprintf(decl, "int %s", name);
18917987Speter	} else if (strcmp(type, "other") == 0) {
1901556Srgrimes		fp->type = T_OTHER;
19117987Speter	} else if (strcmp(type, "temp") == 0) {
1921556Srgrimes		fp->type = T_TEMP;
1931556Srgrimes	} else {
1941556Srgrimes		error("Unknown type %s", type);
1951556Srgrimes	}
1961556Srgrimes	if (fp->type == T_OTHER || fp->type == T_TEMP) {
1971556Srgrimes		skipbl();
1981556Srgrimes		fp->decl = savestr(linep);
1991556Srgrimes	} else {
2001556Srgrimes		if (*linep)
2011556Srgrimes			error("Garbage at end of line");
2021556Srgrimes		fp->decl = savestr(decl);
2031556Srgrimes	}
2041556Srgrimes	curstr->nfields++;
2051556Srgrimes}
2061556Srgrimes
2071556Srgrimes
2081556Srgrimeschar writer[] = "\
2091556Srgrimes/*\n\
2101556Srgrimes * This file was generated by the mknodes program.\n\
2111556Srgrimes */\n\
2121556Srgrimes\n";
2131556Srgrimes
21417987Speterstatic void
21590111Simpoutput(char *file)
21617987Speter{
2171556Srgrimes	FILE *hfile;
2181556Srgrimes	FILE *cfile;
2191556Srgrimes	FILE *patfile;
2201556Srgrimes	int i;
2211556Srgrimes	struct str *sp;
2221556Srgrimes	struct field *fp;
2231556Srgrimes	char *p;
2241556Srgrimes
2251556Srgrimes	if ((patfile = fopen(file, "r")) == NULL)
22653891Scracauer		error("Can't open %s: %s", file, strerror(errno));
2271556Srgrimes	if ((hfile = fopen("nodes.h", "w")) == NULL)
22853891Scracauer		error("Can't create nodes.h: %s", strerror(errno));
2291556Srgrimes	if ((cfile = fopen("nodes.c", "w")) == NULL)
2301556Srgrimes		error("Can't create nodes.c");
2311556Srgrimes	fputs(writer, hfile);
2321556Srgrimes	for (i = 0 ; i < ntypes ; i++)
2331556Srgrimes		fprintf(hfile, "#define %s %d\n", nodename[i], i);
2341556Srgrimes	fputs("\n\n\n", hfile);
2351556Srgrimes	for (sp = str ; sp < &str[nstr] ; sp++) {
2361556Srgrimes		fprintf(hfile, "struct %s {\n", sp->tag);
2371556Srgrimes		for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
2381556Srgrimes			fprintf(hfile, "      %s;\n", fp->decl);
2391556Srgrimes		}
2401556Srgrimes		fputs("};\n\n\n", hfile);
2411556Srgrimes	}
2421556Srgrimes	fputs("union node {\n", hfile);
2431556Srgrimes	fprintf(hfile, "      int type;\n");
2441556Srgrimes	for (sp = str ; sp < &str[nstr] ; sp++) {
2451556Srgrimes		fprintf(hfile, "      struct %s %s;\n", sp->tag, sp->tag);
2461556Srgrimes	}
2471556Srgrimes	fputs("};\n\n\n", hfile);
2481556Srgrimes	fputs("struct nodelist {\n", hfile);
2491556Srgrimes	fputs("\tstruct nodelist *next;\n", hfile);
2501556Srgrimes	fputs("\tunion node *n;\n", hfile);
2511556Srgrimes	fputs("};\n\n\n", hfile);
252196634Sjilles	fputs("struct funcdef;\n", hfile);
253196483Sjilles	fputs("struct funcdef *copyfunc(union node *);\n", hfile);
254196634Sjilles	fputs("union node *getfuncnode(struct funcdef *);\n", hfile);
255196483Sjilles	fputs("void reffunc(struct funcdef *);\n", hfile);
256196483Sjilles	fputs("void unreffunc(struct funcdef *);\n", hfile);
257292786Sjilles	if (ferror(hfile))
258292786Sjilles		error("Can't write to nodes.h");
259292786Sjilles	if (fclose(hfile))
260292786Sjilles		error("Can't close nodes.h");
2611556Srgrimes
2621556Srgrimes	fputs(writer, cfile);
2631556Srgrimes	while (fgets(line, sizeof line, patfile) != NULL) {
2641556Srgrimes		for (p = line ; *p == ' ' || *p == '\t' ; p++);
26517987Speter		if (strcmp(p, "%SIZES\n") == 0)
2661556Srgrimes			outsizes(cfile);
26717987Speter		else if (strcmp(p, "%CALCSIZE\n") == 0)
2681556Srgrimes			outfunc(cfile, 1);
26917987Speter		else if (strcmp(p, "%COPY\n") == 0)
2701556Srgrimes			outfunc(cfile, 0);
2711556Srgrimes		else
2721556Srgrimes			fputs(line, cfile);
2731556Srgrimes	}
274292786Sjilles	fclose(patfile);
275292786Sjilles	if (ferror(cfile))
276292786Sjilles		error("Can't write to nodes.c");
277292786Sjilles	if (fclose(cfile))
278292786Sjilles		error("Can't close nodes.c");
2791556Srgrimes}
2801556Srgrimes
2811556Srgrimes
2821556Srgrimes
28317987Speterstatic void
28490111Simpoutsizes(FILE *cfile)
28517987Speter{
2861556Srgrimes	int i;
2871556Srgrimes
2881556Srgrimes	fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
2891556Srgrimes	for (i = 0 ; i < ntypes ; i++) {
2901556Srgrimes		fprintf(cfile, "      ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
2911556Srgrimes	}
2921556Srgrimes	fprintf(cfile, "};\n");
2931556Srgrimes}
2941556Srgrimes
2951556Srgrimes
29617987Speterstatic void
29790111Simpoutfunc(FILE *cfile, int calcsize)
29817987Speter{
2991556Srgrimes	struct str *sp;
3001556Srgrimes	struct field *fp;
3011556Srgrimes	int i;
3021556Srgrimes
3031556Srgrimes	fputs("      if (n == NULL)\n", cfile);
3041556Srgrimes	if (calcsize)
3051556Srgrimes		fputs("	    return;\n", cfile);
3061556Srgrimes	else
3071556Srgrimes		fputs("	    return NULL;\n", cfile);
3081556Srgrimes	if (calcsize)
3091556Srgrimes		fputs("      funcblocksize += nodesize[n->type];\n", cfile);
3101556Srgrimes	else {
3111556Srgrimes		fputs("      new = funcblock;\n", cfile);
31225226Ssteve		fputs("      funcblock = (char *)funcblock + nodesize[n->type];\n", cfile);
3131556Srgrimes	}
3141556Srgrimes	fputs("      switch (n->type) {\n", cfile);
3151556Srgrimes	for (sp = str ; sp < &str[nstr] ; sp++) {
3161556Srgrimes		for (i = 0 ; i < ntypes ; i++) {
3171556Srgrimes			if (nodestr[i] == sp)
3181556Srgrimes				fprintf(cfile, "      case %s:\n", nodename[i]);
3191556Srgrimes		}
3201556Srgrimes		for (i = sp->nfields ; --i >= 1 ; ) {
3211556Srgrimes			fp = &sp->field[i];
3221556Srgrimes			switch (fp->type) {
3231556Srgrimes			case T_NODE:
3241556Srgrimes				if (calcsize) {
3251556Srgrimes					indent(12, cfile);
3261556Srgrimes					fprintf(cfile, "calcsize(n->%s.%s);\n",
3271556Srgrimes						sp->tag, fp->name);
3281556Srgrimes				} else {
3291556Srgrimes					indent(12, cfile);
3301556Srgrimes					fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
3311556Srgrimes						sp->tag, fp->name, sp->tag, fp->name);
3321556Srgrimes				}
3331556Srgrimes				break;
3341556Srgrimes			case T_NODELIST:
3351556Srgrimes				if (calcsize) {
3361556Srgrimes					indent(12, cfile);
3371556Srgrimes					fprintf(cfile, "sizenodelist(n->%s.%s);\n",
3381556Srgrimes						sp->tag, fp->name);
3391556Srgrimes				} else {
3401556Srgrimes					indent(12, cfile);
3411556Srgrimes					fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
3421556Srgrimes						sp->tag, fp->name, sp->tag, fp->name);
3431556Srgrimes				}
3441556Srgrimes				break;
3451556Srgrimes			case T_STRING:
3461556Srgrimes				if (calcsize) {
3471556Srgrimes					indent(12, cfile);
3481556Srgrimes					fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
3491556Srgrimes						sp->tag, fp->name);
3501556Srgrimes				} else {
3511556Srgrimes					indent(12, cfile);
3521556Srgrimes					fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
3531556Srgrimes						sp->tag, fp->name, sp->tag, fp->name);
3541556Srgrimes				}
3551556Srgrimes				break;
3561556Srgrimes			case T_INT:
3571556Srgrimes			case T_OTHER:
3581556Srgrimes				if (! calcsize) {
3591556Srgrimes					indent(12, cfile);
3601556Srgrimes					fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
3611556Srgrimes						sp->tag, fp->name, sp->tag, fp->name);
3621556Srgrimes				}
3631556Srgrimes				break;
3641556Srgrimes			}
3651556Srgrimes		}
3661556Srgrimes		indent(12, cfile);
3671556Srgrimes		fputs("break;\n", cfile);
3681556Srgrimes	}
3691556Srgrimes	fputs("      };\n", cfile);
3701556Srgrimes	if (! calcsize)
3711556Srgrimes		fputs("      new->type = n->type;\n", cfile);
3721556Srgrimes}
3731556Srgrimes
3741556Srgrimes
37517987Speterstatic void
37690111Simpindent(int amount, FILE *fp)
37717987Speter{
3781556Srgrimes	while (amount >= 8) {
3791556Srgrimes		putc('\t', fp);
3801556Srgrimes		amount -= 8;
3811556Srgrimes	}
3821556Srgrimes	while (--amount >= 0) {
3831556Srgrimes		putc(' ', fp);
3841556Srgrimes	}
3851556Srgrimes}
3861556Srgrimes
3871556Srgrimes
38817987Speterstatic int
38990111Simpnextfield(char *buf)
39017987Speter{
39125226Ssteve	char *p, *q;
3921556Srgrimes
3931556Srgrimes	p = linep;
3941556Srgrimes	while (*p == ' ' || *p == '\t')
3951556Srgrimes		p++;
3961556Srgrimes	q = buf;
3971556Srgrimes	while (*p != ' ' && *p != '\t' && *p != '\0')
3981556Srgrimes		*q++ = *p++;
3991556Srgrimes	*q = '\0';
4001556Srgrimes	linep = p;
4011556Srgrimes	return (q > buf);
4021556Srgrimes}
4031556Srgrimes
4041556Srgrimes
40517987Speterstatic void
40690111Simpskipbl(void)
40717987Speter{
4081556Srgrimes	while (*linep == ' ' || *linep == '\t')
4091556Srgrimes		linep++;
4101556Srgrimes}
4111556Srgrimes
4121556Srgrimes
41317987Speterstatic int
414292786Sjillesreadline(FILE *infp)
41517987Speter{
41625226Ssteve	char *p;
4171556Srgrimes
4181556Srgrimes	if (fgets(line, 1024, infp) == NULL)
4191556Srgrimes		return 0;
4201556Srgrimes	for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
4211556Srgrimes	while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
4221556Srgrimes		p--;
4231556Srgrimes	*p = '\0';
4241556Srgrimes	linep = line;
4251556Srgrimes	linno++;
4261556Srgrimes	if (p - line > BUFLEN)
4271556Srgrimes		error("Line too long");
4281556Srgrimes	return 1;
4291556Srgrimes}
4301556Srgrimes
4311556Srgrimes
4321556Srgrimes
43317987Speterstatic void
43417987Spetererror(const char *msg, ...)
43517987Speter{
43617987Speter	va_list va;
43717987Speter	va_start(va, msg);
43817987Speter
43917987Speter	(void) fprintf(stderr, "line %d: ", linno);
44017987Speter	(void) vfprintf(stderr, msg, va);
44117987Speter	(void) fputc('\n', stderr);
44217987Speter
44317987Speter	va_end(va);
44417987Speter
4451556Srgrimes	exit(2);
4461556Srgrimes}
4471556Srgrimes
4481556Srgrimes
4491556Srgrimes
45017987Speterstatic char *
45190111Simpsavestr(const char *s)
45217987Speter{
45325226Ssteve	char *p;
4541556Srgrimes
4551556Srgrimes	if ((p = malloc(strlen(s) + 1)) == NULL)
4561556Srgrimes		error("Out of space");
45717987Speter	(void) strcpy(p, s);
4581556Srgrimes	return p;
4591556Srgrimes}
460