xref: /freebsd/bin/sh/mknodes.c (revision aa9caaf65748ace0f3cd08c2deaf3ef3048d6e4d)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
3589730b29SDavid Greenman  *
36aa9caaf6SPeter Wemm  *	$Id: mknodes.c,v 1.2 1994/09/24 02:57:55 davidg Exp $
374b88c807SRodney W. Grimes  */
384b88c807SRodney W. Grimes 
394b88c807SRodney W. Grimes #ifndef lint
404b88c807SRodney W. Grimes static char copyright[] =
414b88c807SRodney W. Grimes "@(#) Copyright (c) 1991, 1993\n\
424b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
434b88c807SRodney W. Grimes #endif /* not lint */
444b88c807SRodney W. Grimes 
454b88c807SRodney W. Grimes #ifndef lint
46aa9caaf6SPeter Wemm static char sccsid[] = "@(#)mknodes.c	8.2 (Berkeley) 5/4/95";
474b88c807SRodney W. Grimes #endif /* not lint */
484b88c807SRodney W. Grimes 
494b88c807SRodney W. Grimes /*
504b88c807SRodney W. Grimes  * This program reads the nodetypes file and nodes.c.pat file.  It generates
514b88c807SRodney W. Grimes  * the files nodes.h and nodes.c.
524b88c807SRodney W. Grimes  */
534b88c807SRodney W. Grimes 
544b88c807SRodney W. Grimes #include <stdio.h>
55aa9caaf6SPeter Wemm #include <stdlib.h>
56aa9caaf6SPeter Wemm #include <string.h>
57aa9caaf6SPeter Wemm #if __STDC__
58aa9caaf6SPeter Wemm #include <stdarg.h>
59aa9caaf6SPeter Wemm #else
60aa9caaf6SPeter Wemm #include <varargs.h>
61aa9caaf6SPeter Wemm #endif
624b88c807SRodney W. Grimes 
634b88c807SRodney W. Grimes 
644b88c807SRodney W. Grimes #define MAXTYPES 50		/* max number of node types */
654b88c807SRodney W. Grimes #define MAXFIELDS 20		/* max fields in a structure */
664b88c807SRodney W. Grimes #define BUFLEN 100		/* size of character buffers */
674b88c807SRodney W. Grimes 
684b88c807SRodney W. Grimes /* field types */
694b88c807SRodney W. Grimes #define T_NODE 1		/* union node *field */
704b88c807SRodney W. Grimes #define T_NODELIST 2		/* struct nodelist *field */
714b88c807SRodney W. Grimes #define T_STRING 3
724b88c807SRodney W. Grimes #define T_INT 4			/* int field */
734b88c807SRodney W. Grimes #define T_OTHER 5		/* other */
744b88c807SRodney W. Grimes #define T_TEMP 6		/* don't copy this field */
754b88c807SRodney W. Grimes 
764b88c807SRodney W. Grimes 
774b88c807SRodney W. Grimes struct field {			/* a structure field */
784b88c807SRodney W. Grimes 	char *name;		/* name of field */
794b88c807SRodney W. Grimes 	int type;			/* type of field */
804b88c807SRodney W. Grimes 	char *decl;		/* declaration of field */
814b88c807SRodney W. Grimes };
824b88c807SRodney W. Grimes 
834b88c807SRodney W. Grimes 
844b88c807SRodney W. Grimes struct str {			/* struct representing a node structure */
854b88c807SRodney W. Grimes 	char *tag;		/* structure tag */
864b88c807SRodney W. Grimes 	int nfields;		/* number of fields in the structure */
874b88c807SRodney W. Grimes 	struct field field[MAXFIELDS];	/* the fields of the structure */
884b88c807SRodney W. Grimes 	int done;			/* set if fully parsed */
894b88c807SRodney W. Grimes };
904b88c807SRodney W. Grimes 
914b88c807SRodney W. Grimes 
92aa9caaf6SPeter Wemm static int ntypes;			/* number of node types */
93aa9caaf6SPeter Wemm static char *nodename[MAXTYPES];	/* names of the nodes */
94aa9caaf6SPeter Wemm static struct str *nodestr[MAXTYPES];	/* type of structure used by the node */
95aa9caaf6SPeter Wemm static int nstr;			/* number of structures */
96aa9caaf6SPeter Wemm static struct str str[MAXTYPES];	/* the structures */
97aa9caaf6SPeter Wemm static struct str *curstr;		/* current structure */
98aa9caaf6SPeter Wemm static FILE *infp = stdin;
99aa9caaf6SPeter Wemm static char line[1024];
100aa9caaf6SPeter Wemm static int linno;
101aa9caaf6SPeter Wemm static char *linep;
102aa9caaf6SPeter Wemm 
103aa9caaf6SPeter Wemm static void parsenode __P((void));
104aa9caaf6SPeter Wemm static void parsefield __P((void));
105aa9caaf6SPeter Wemm static void output __P((char *));
106aa9caaf6SPeter Wemm static void outsizes __P((FILE *));
107aa9caaf6SPeter Wemm static void outfunc __P((FILE *, int));
108aa9caaf6SPeter Wemm static void indent __P((int, FILE *));
109aa9caaf6SPeter Wemm static int nextfield __P((char *));
110aa9caaf6SPeter Wemm static void skipbl __P((void));
111aa9caaf6SPeter Wemm static int readline __P((void));
112aa9caaf6SPeter Wemm static void error __P((const char *, ...));
113aa9caaf6SPeter Wemm static char *savestr __P((const char *));
1144b88c807SRodney W. Grimes 
1154b88c807SRodney W. Grimes 
116aa9caaf6SPeter Wemm int
1174b88c807SRodney W. Grimes main(argc, argv)
118aa9caaf6SPeter Wemm 	int argc;
1194b88c807SRodney W. Grimes 	char **argv;
1204b88c807SRodney W. Grimes {
1214b88c807SRodney W. Grimes 	if (argc != 3)
1224b88c807SRodney W. Grimes 		error("usage: mknodes file\n");
1234b88c807SRodney W. Grimes 	if ((infp = fopen(argv[1], "r")) == NULL)
1244b88c807SRodney W. Grimes 		error("Can't open %s", argv[1]);
1254b88c807SRodney W. Grimes 	while (readline()) {
1264b88c807SRodney W. Grimes 		if (line[0] == ' ' || line[0] == '\t')
1274b88c807SRodney W. Grimes 			parsefield();
1284b88c807SRodney W. Grimes 		else if (line[0] != '\0')
1294b88c807SRodney W. Grimes 			parsenode();
1304b88c807SRodney W. Grimes 	}
1314b88c807SRodney W. Grimes 	output(argv[2]);
1324b88c807SRodney W. Grimes 	exit(0);
1334b88c807SRodney W. Grimes }
1344b88c807SRodney W. Grimes 
1354b88c807SRodney W. Grimes 
1364b88c807SRodney W. Grimes 
137aa9caaf6SPeter Wemm static void
138aa9caaf6SPeter Wemm parsenode()
139aa9caaf6SPeter Wemm {
1404b88c807SRodney W. Grimes 	char name[BUFLEN];
1414b88c807SRodney W. Grimes 	char tag[BUFLEN];
1424b88c807SRodney W. Grimes 	struct str *sp;
1434b88c807SRodney W. Grimes 
1444b88c807SRodney W. Grimes 	if (curstr && curstr->nfields > 0)
1454b88c807SRodney W. Grimes 		curstr->done = 1;
1464b88c807SRodney W. Grimes 	nextfield(name);
1474b88c807SRodney W. Grimes 	if (! nextfield(tag))
1484b88c807SRodney W. Grimes 		error("Tag expected");
1494b88c807SRodney W. Grimes 	if (*linep != '\0')
1504b88c807SRodney W. Grimes 		error("Garbage at end of line");
1514b88c807SRodney W. Grimes 	nodename[ntypes] = savestr(name);
1524b88c807SRodney W. Grimes 	for (sp = str ; sp < str + nstr ; sp++) {
153aa9caaf6SPeter Wemm 		if (strcmp(sp->tag, tag) == 0)
1544b88c807SRodney W. Grimes 			break;
1554b88c807SRodney W. Grimes 	}
1564b88c807SRodney W. Grimes 	if (sp >= str + nstr) {
1574b88c807SRodney W. Grimes 		sp->tag = savestr(tag);
1584b88c807SRodney W. Grimes 		sp->nfields = 0;
1594b88c807SRodney W. Grimes 		curstr = sp;
1604b88c807SRodney W. Grimes 		nstr++;
1614b88c807SRodney W. Grimes 	}
1624b88c807SRodney W. Grimes 	nodestr[ntypes] = sp;
1634b88c807SRodney W. Grimes 	ntypes++;
1644b88c807SRodney W. Grimes }
1654b88c807SRodney W. Grimes 
1664b88c807SRodney W. Grimes 
167aa9caaf6SPeter Wemm static void
168aa9caaf6SPeter Wemm parsefield()
169aa9caaf6SPeter Wemm {
1704b88c807SRodney W. Grimes 	char name[BUFLEN];
1714b88c807SRodney W. Grimes 	char type[BUFLEN];
1724b88c807SRodney W. Grimes 	char decl[2 * BUFLEN];
1734b88c807SRodney W. Grimes 	struct field *fp;
1744b88c807SRodney W. Grimes 
1754b88c807SRodney W. Grimes 	if (curstr == NULL || curstr->done)
1764b88c807SRodney W. Grimes 		error("No current structure to add field to");
1774b88c807SRodney W. Grimes 	if (! nextfield(name))
1784b88c807SRodney W. Grimes 		error("No field name");
1794b88c807SRodney W. Grimes 	if (! nextfield(type))
1804b88c807SRodney W. Grimes 		error("No field type");
1814b88c807SRodney W. Grimes 	fp = &curstr->field[curstr->nfields];
1824b88c807SRodney W. Grimes 	fp->name = savestr(name);
183aa9caaf6SPeter Wemm 	if (strcmp(type, "nodeptr") == 0) {
1844b88c807SRodney W. Grimes 		fp->type = T_NODE;
1854b88c807SRodney W. Grimes 		sprintf(decl, "union node *%s", name);
186aa9caaf6SPeter Wemm 	} else if (strcmp(type, "nodelist") == 0) {
1874b88c807SRodney W. Grimes 		fp->type = T_NODELIST;
1884b88c807SRodney W. Grimes 		sprintf(decl, "struct nodelist *%s", name);
189aa9caaf6SPeter Wemm 	} else if (strcmp(type, "string") == 0) {
1904b88c807SRodney W. Grimes 		fp->type = T_STRING;
1914b88c807SRodney W. Grimes 		sprintf(decl, "char *%s", name);
192aa9caaf6SPeter Wemm 	} else if (strcmp(type, "int") == 0) {
1934b88c807SRodney W. Grimes 		fp->type = T_INT;
1944b88c807SRodney W. Grimes 		sprintf(decl, "int %s", name);
195aa9caaf6SPeter Wemm 	} else if (strcmp(type, "other") == 0) {
1964b88c807SRodney W. Grimes 		fp->type = T_OTHER;
197aa9caaf6SPeter Wemm 	} else if (strcmp(type, "temp") == 0) {
1984b88c807SRodney W. Grimes 		fp->type = T_TEMP;
1994b88c807SRodney W. Grimes 	} else {
2004b88c807SRodney W. Grimes 		error("Unknown type %s", type);
2014b88c807SRodney W. Grimes 	}
2024b88c807SRodney W. Grimes 	if (fp->type == T_OTHER || fp->type == T_TEMP) {
2034b88c807SRodney W. Grimes 		skipbl();
2044b88c807SRodney W. Grimes 		fp->decl = savestr(linep);
2054b88c807SRodney W. Grimes 	} else {
2064b88c807SRodney W. Grimes 		if (*linep)
2074b88c807SRodney W. Grimes 			error("Garbage at end of line");
2084b88c807SRodney W. Grimes 		fp->decl = savestr(decl);
2094b88c807SRodney W. Grimes 	}
2104b88c807SRodney W. Grimes 	curstr->nfields++;
2114b88c807SRodney W. Grimes }
2124b88c807SRodney W. Grimes 
2134b88c807SRodney W. Grimes 
2144b88c807SRodney W. Grimes char writer[] = "\
2154b88c807SRodney W. Grimes /*\n\
2164b88c807SRodney W. Grimes  * This file was generated by the mknodes program.\n\
2174b88c807SRodney W. Grimes  */\n\
2184b88c807SRodney W. Grimes \n";
2194b88c807SRodney W. Grimes 
220aa9caaf6SPeter Wemm static void
2214b88c807SRodney W. Grimes output(file)
2224b88c807SRodney W. Grimes 	char *file;
2234b88c807SRodney W. Grimes {
2244b88c807SRodney W. Grimes 	FILE *hfile;
2254b88c807SRodney W. Grimes 	FILE *cfile;
2264b88c807SRodney W. Grimes 	FILE *patfile;
2274b88c807SRodney W. Grimes 	int i;
2284b88c807SRodney W. Grimes 	struct str *sp;
2294b88c807SRodney W. Grimes 	struct field *fp;
2304b88c807SRodney W. Grimes 	char *p;
2314b88c807SRodney W. Grimes 
2324b88c807SRodney W. Grimes 	if ((patfile = fopen(file, "r")) == NULL)
2334b88c807SRodney W. Grimes 		error("Can't open %s", file);
2344b88c807SRodney W. Grimes 	if ((hfile = fopen("nodes.h", "w")) == NULL)
2354b88c807SRodney W. Grimes 		error("Can't create nodes.h");
2364b88c807SRodney W. Grimes 	if ((cfile = fopen("nodes.c", "w")) == NULL)
2374b88c807SRodney W. Grimes 		error("Can't create nodes.c");
2384b88c807SRodney W. Grimes 	fputs(writer, hfile);
2394b88c807SRodney W. Grimes 	for (i = 0 ; i < ntypes ; i++)
2404b88c807SRodney W. Grimes 		fprintf(hfile, "#define %s %d\n", nodename[i], i);
2414b88c807SRodney W. Grimes 	fputs("\n\n\n", hfile);
2424b88c807SRodney W. Grimes 	for (sp = str ; sp < &str[nstr] ; sp++) {
2434b88c807SRodney W. Grimes 		fprintf(hfile, "struct %s {\n", sp->tag);
2444b88c807SRodney W. Grimes 		for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
2454b88c807SRodney W. Grimes 			fprintf(hfile, "      %s;\n", fp->decl);
2464b88c807SRodney W. Grimes 		}
2474b88c807SRodney W. Grimes 		fputs("};\n\n\n", hfile);
2484b88c807SRodney W. Grimes 	}
2494b88c807SRodney W. Grimes 	fputs("union node {\n", hfile);
2504b88c807SRodney W. Grimes 	fprintf(hfile, "      int type;\n");
2514b88c807SRodney W. Grimes 	for (sp = str ; sp < &str[nstr] ; sp++) {
2524b88c807SRodney W. Grimes 		fprintf(hfile, "      struct %s %s;\n", sp->tag, sp->tag);
2534b88c807SRodney W. Grimes 	}
2544b88c807SRodney W. Grimes 	fputs("};\n\n\n", hfile);
2554b88c807SRodney W. Grimes 	fputs("struct nodelist {\n", hfile);
2564b88c807SRodney W. Grimes 	fputs("\tstruct nodelist *next;\n", hfile);
2574b88c807SRodney W. Grimes 	fputs("\tunion node *n;\n", hfile);
2584b88c807SRodney W. Grimes 	fputs("};\n\n\n", hfile);
2594b88c807SRodney W. Grimes 	fputs("#ifdef __STDC__\n", hfile);
2604b88c807SRodney W. Grimes 	fputs("union node *copyfunc(union node *);\n", hfile);
2614b88c807SRodney W. Grimes 	fputs("void freefunc(union node *);\n", hfile);
2624b88c807SRodney W. Grimes 	fputs("#else\n", hfile);
2634b88c807SRodney W. Grimes 	fputs("union node *copyfunc();\n", hfile);
2644b88c807SRodney W. Grimes 	fputs("void freefunc();\n", hfile);
2654b88c807SRodney W. Grimes 	fputs("#endif\n", hfile);
2664b88c807SRodney W. Grimes 
2674b88c807SRodney W. Grimes 	fputs(writer, cfile);
2684b88c807SRodney W. Grimes 	while (fgets(line, sizeof line, patfile) != NULL) {
2694b88c807SRodney W. Grimes 		for (p = line ; *p == ' ' || *p == '\t' ; p++);
270aa9caaf6SPeter Wemm 		if (strcmp(p, "%SIZES\n") == 0)
2714b88c807SRodney W. Grimes 			outsizes(cfile);
272aa9caaf6SPeter Wemm 		else if (strcmp(p, "%CALCSIZE\n") == 0)
2734b88c807SRodney W. Grimes 			outfunc(cfile, 1);
274aa9caaf6SPeter Wemm 		else if (strcmp(p, "%COPY\n") == 0)
2754b88c807SRodney W. Grimes 			outfunc(cfile, 0);
2764b88c807SRodney W. Grimes 		else
2774b88c807SRodney W. Grimes 			fputs(line, cfile);
2784b88c807SRodney W. Grimes 	}
2794b88c807SRodney W. Grimes }
2804b88c807SRodney W. Grimes 
2814b88c807SRodney W. Grimes 
2824b88c807SRodney W. Grimes 
283aa9caaf6SPeter Wemm static void
2844b88c807SRodney W. Grimes outsizes(cfile)
2854b88c807SRodney W. Grimes 	FILE *cfile;
2864b88c807SRodney W. Grimes {
2874b88c807SRodney W. Grimes 	int i;
2884b88c807SRodney W. Grimes 
2894b88c807SRodney W. Grimes 	fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
2904b88c807SRodney W. Grimes 	for (i = 0 ; i < ntypes ; i++) {
2914b88c807SRodney W. Grimes 		fprintf(cfile, "      ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
2924b88c807SRodney W. Grimes 	}
2934b88c807SRodney W. Grimes 	fprintf(cfile, "};\n");
2944b88c807SRodney W. Grimes }
2954b88c807SRodney W. Grimes 
2964b88c807SRodney W. Grimes 
297aa9caaf6SPeter Wemm static void
2984b88c807SRodney W. Grimes outfunc(cfile, calcsize)
2994b88c807SRodney W. Grimes 	FILE *cfile;
300aa9caaf6SPeter Wemm 	int calcsize;
3014b88c807SRodney W. Grimes {
3024b88c807SRodney W. Grimes 	struct str *sp;
3034b88c807SRodney W. Grimes 	struct field *fp;
3044b88c807SRodney W. Grimes 	int i;
3054b88c807SRodney W. Grimes 
3064b88c807SRodney W. Grimes 	fputs("      if (n == NULL)\n", cfile);
3074b88c807SRodney W. Grimes 	if (calcsize)
3084b88c807SRodney W. Grimes 		fputs("	    return;\n", cfile);
3094b88c807SRodney W. Grimes 	else
3104b88c807SRodney W. Grimes 		fputs("	    return NULL;\n", cfile);
3114b88c807SRodney W. Grimes 	if (calcsize)
3124b88c807SRodney W. Grimes 		fputs("      funcblocksize += nodesize[n->type];\n", cfile);
3134b88c807SRodney W. Grimes 	else {
3144b88c807SRodney W. Grimes 		fputs("      new = funcblock;\n", cfile);
3154b88c807SRodney W. Grimes 		fputs("      funcblock += nodesize[n->type];\n", cfile);
3164b88c807SRodney W. Grimes 	}
3174b88c807SRodney W. Grimes 	fputs("      switch (n->type) {\n", cfile);
3184b88c807SRodney W. Grimes 	for (sp = str ; sp < &str[nstr] ; sp++) {
3194b88c807SRodney W. Grimes 		for (i = 0 ; i < ntypes ; i++) {
3204b88c807SRodney W. Grimes 			if (nodestr[i] == sp)
3214b88c807SRodney W. Grimes 				fprintf(cfile, "      case %s:\n", nodename[i]);
3224b88c807SRodney W. Grimes 		}
3234b88c807SRodney W. Grimes 		for (i = sp->nfields ; --i >= 1 ; ) {
3244b88c807SRodney W. Grimes 			fp = &sp->field[i];
3254b88c807SRodney W. Grimes 			switch (fp->type) {
3264b88c807SRodney W. Grimes 			case T_NODE:
3274b88c807SRodney W. Grimes 				if (calcsize) {
3284b88c807SRodney W. Grimes 					indent(12, cfile);
3294b88c807SRodney W. Grimes 					fprintf(cfile, "calcsize(n->%s.%s);\n",
3304b88c807SRodney W. Grimes 						sp->tag, fp->name);
3314b88c807SRodney W. Grimes 				} else {
3324b88c807SRodney W. Grimes 					indent(12, cfile);
3334b88c807SRodney W. Grimes 					fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
3344b88c807SRodney W. Grimes 						sp->tag, fp->name, sp->tag, fp->name);
3354b88c807SRodney W. Grimes 				}
3364b88c807SRodney W. Grimes 				break;
3374b88c807SRodney W. Grimes 			case T_NODELIST:
3384b88c807SRodney W. Grimes 				if (calcsize) {
3394b88c807SRodney W. Grimes 					indent(12, cfile);
3404b88c807SRodney W. Grimes 					fprintf(cfile, "sizenodelist(n->%s.%s);\n",
3414b88c807SRodney W. Grimes 						sp->tag, fp->name);
3424b88c807SRodney W. Grimes 				} else {
3434b88c807SRodney W. Grimes 					indent(12, cfile);
3444b88c807SRodney W. Grimes 					fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
3454b88c807SRodney W. Grimes 						sp->tag, fp->name, sp->tag, fp->name);
3464b88c807SRodney W. Grimes 				}
3474b88c807SRodney W. Grimes 				break;
3484b88c807SRodney W. Grimes 			case T_STRING:
3494b88c807SRodney W. Grimes 				if (calcsize) {
3504b88c807SRodney W. Grimes 					indent(12, cfile);
3514b88c807SRodney W. Grimes 					fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
3524b88c807SRodney W. Grimes 						sp->tag, fp->name);
3534b88c807SRodney W. Grimes 				} else {
3544b88c807SRodney W. Grimes 					indent(12, cfile);
3554b88c807SRodney W. Grimes 					fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
3564b88c807SRodney W. Grimes 						sp->tag, fp->name, sp->tag, fp->name);
3574b88c807SRodney W. Grimes 				}
3584b88c807SRodney W. Grimes 				break;
3594b88c807SRodney W. Grimes 			case T_INT:
3604b88c807SRodney W. Grimes 			case T_OTHER:
3614b88c807SRodney W. Grimes 				if (! calcsize) {
3624b88c807SRodney W. Grimes 					indent(12, cfile);
3634b88c807SRodney W. Grimes 					fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
3644b88c807SRodney W. Grimes 						sp->tag, fp->name, sp->tag, fp->name);
3654b88c807SRodney W. Grimes 				}
3664b88c807SRodney W. Grimes 				break;
3674b88c807SRodney W. Grimes 			}
3684b88c807SRodney W. Grimes 		}
3694b88c807SRodney W. Grimes 		indent(12, cfile);
3704b88c807SRodney W. Grimes 		fputs("break;\n", cfile);
3714b88c807SRodney W. Grimes 	}
3724b88c807SRodney W. Grimes 	fputs("      };\n", cfile);
3734b88c807SRodney W. Grimes 	if (! calcsize)
3744b88c807SRodney W. Grimes 		fputs("      new->type = n->type;\n", cfile);
3754b88c807SRodney W. Grimes }
3764b88c807SRodney W. Grimes 
3774b88c807SRodney W. Grimes 
378aa9caaf6SPeter Wemm static void
3794b88c807SRodney W. Grimes indent(amount, fp)
380aa9caaf6SPeter Wemm 	int amount;
3814b88c807SRodney W. Grimes 	FILE *fp;
3824b88c807SRodney W. Grimes {
3834b88c807SRodney W. Grimes 	while (amount >= 8) {
3844b88c807SRodney W. Grimes 		putc('\t', fp);
3854b88c807SRodney W. Grimes 		amount -= 8;
3864b88c807SRodney W. Grimes 	}
3874b88c807SRodney W. Grimes 	while (--amount >= 0) {
3884b88c807SRodney W. Grimes 		putc(' ', fp);
3894b88c807SRodney W. Grimes 	}
3904b88c807SRodney W. Grimes }
3914b88c807SRodney W. Grimes 
3924b88c807SRodney W. Grimes 
393aa9caaf6SPeter Wemm static int
3944b88c807SRodney W. Grimes nextfield(buf)
3954b88c807SRodney W. Grimes 	char *buf;
3964b88c807SRodney W. Grimes {
3974b88c807SRodney W. Grimes 	register char *p, *q;
3984b88c807SRodney W. Grimes 
3994b88c807SRodney W. Grimes 	p = linep;
4004b88c807SRodney W. Grimes 	while (*p == ' ' || *p == '\t')
4014b88c807SRodney W. Grimes 		p++;
4024b88c807SRodney W. Grimes 	q = buf;
4034b88c807SRodney W. Grimes 	while (*p != ' ' && *p != '\t' && *p != '\0')
4044b88c807SRodney W. Grimes 		*q++ = *p++;
4054b88c807SRodney W. Grimes 	*q = '\0';
4064b88c807SRodney W. Grimes 	linep = p;
4074b88c807SRodney W. Grimes 	return (q > buf);
4084b88c807SRodney W. Grimes }
4094b88c807SRodney W. Grimes 
4104b88c807SRodney W. Grimes 
411aa9caaf6SPeter Wemm static void
412aa9caaf6SPeter Wemm skipbl()
413aa9caaf6SPeter Wemm {
4144b88c807SRodney W. Grimes 	while (*linep == ' ' || *linep == '\t')
4154b88c807SRodney W. Grimes 		linep++;
4164b88c807SRodney W. Grimes }
4174b88c807SRodney W. Grimes 
4184b88c807SRodney W. Grimes 
419aa9caaf6SPeter Wemm static int
420aa9caaf6SPeter Wemm readline()
421aa9caaf6SPeter Wemm {
4224b88c807SRodney W. Grimes 	register char *p;
4234b88c807SRodney W. Grimes 
4244b88c807SRodney W. Grimes 	if (fgets(line, 1024, infp) == NULL)
4254b88c807SRodney W. Grimes 		return 0;
4264b88c807SRodney W. Grimes 	for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
4274b88c807SRodney W. Grimes 	while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
4284b88c807SRodney W. Grimes 		p--;
4294b88c807SRodney W. Grimes 	*p = '\0';
4304b88c807SRodney W. Grimes 	linep = line;
4314b88c807SRodney W. Grimes 	linno++;
4324b88c807SRodney W. Grimes 	if (p - line > BUFLEN)
4334b88c807SRodney W. Grimes 		error("Line too long");
4344b88c807SRodney W. Grimes 	return 1;
4354b88c807SRodney W. Grimes }
4364b88c807SRodney W. Grimes 
4374b88c807SRodney W. Grimes 
4384b88c807SRodney W. Grimes 
439aa9caaf6SPeter Wemm static void
440aa9caaf6SPeter Wemm #if __STDC__
441aa9caaf6SPeter Wemm error(const char *msg, ...)
442aa9caaf6SPeter Wemm #else
443aa9caaf6SPeter Wemm error(va_alist)
444aa9caaf6SPeter Wemm 	va_dcl
445aa9caaf6SPeter Wemm #endif
4464b88c807SRodney W. Grimes {
447aa9caaf6SPeter Wemm 	va_list va;
448aa9caaf6SPeter Wemm #if __STDC__
449aa9caaf6SPeter Wemm 	va_start(va, msg);
450aa9caaf6SPeter Wemm #else
451aa9caaf6SPeter Wemm 	char *msg;
452aa9caaf6SPeter Wemm 	va_start(va);
453aa9caaf6SPeter Wemm 	msg = va_arg(va, char *);
454aa9caaf6SPeter Wemm #endif
455aa9caaf6SPeter Wemm 
456aa9caaf6SPeter Wemm 	(void) fprintf(stderr, "line %d: ", linno);
457aa9caaf6SPeter Wemm 	(void) vfprintf(stderr, msg, va);
458aa9caaf6SPeter Wemm 	(void) fputc('\n', stderr);
459aa9caaf6SPeter Wemm 
460aa9caaf6SPeter Wemm 	va_end(va);
461aa9caaf6SPeter Wemm 
4624b88c807SRodney W. Grimes 	exit(2);
4634b88c807SRodney W. Grimes }
4644b88c807SRodney W. Grimes 
4654b88c807SRodney W. Grimes 
4664b88c807SRodney W. Grimes 
467aa9caaf6SPeter Wemm static char *
4684b88c807SRodney W. Grimes savestr(s)
469aa9caaf6SPeter Wemm 	const char *s;
4704b88c807SRodney W. Grimes {
4714b88c807SRodney W. Grimes 	register char *p;
4724b88c807SRodney W. Grimes 
4734b88c807SRodney W. Grimes 	if ((p = malloc(strlen(s) + 1)) == NULL)
4744b88c807SRodney W. Grimes 		error("Out of space");
475aa9caaf6SPeter Wemm 	(void) strcpy(p, s);
4764b88c807SRodney W. Grimes 	return p;
4774b88c807SRodney W. Grimes }
478