xref: /freebsd/bin/sh/mknodes.c (revision eb33e843b84fbb6d20613f9bec9fa0013d61d7e3)
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  * 4. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
3309a80d48SDavid E. O'Brien #if 0
344b88c807SRodney W. Grimes #ifndef lint
35ab0a2172SSteve Price static char const copyright[] =
364b88c807SRodney W. Grimes "@(#) Copyright (c) 1991, 1993\n\
374b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
384b88c807SRodney W. Grimes #endif /* not lint */
394b88c807SRodney W. Grimes 
404b88c807SRodney W. Grimes #ifndef lint
413d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)mknodes.c	8.2 (Berkeley) 5/4/95";
424b88c807SRodney W. Grimes #endif /* not lint */
4309a80d48SDavid E. O'Brien #endif
442749b141SDavid E. O'Brien #include <sys/cdefs.h>
452749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
464b88c807SRodney W. Grimes 
474b88c807SRodney W. Grimes /*
484b88c807SRodney W. Grimes  * This program reads the nodetypes file and nodes.c.pat file.  It generates
494b88c807SRodney W. Grimes  * the files nodes.h and nodes.c.
504b88c807SRodney W. Grimes  */
514b88c807SRodney W. Grimes 
524b88c807SRodney W. Grimes #include <stdio.h>
53aa9caaf6SPeter Wemm #include <stdlib.h>
54aa9caaf6SPeter Wemm #include <string.h>
556c48b6cfSMartin Cracauer #include <errno.h>
56aa9caaf6SPeter Wemm #include <stdarg.h>
574b88c807SRodney W. Grimes 
584b88c807SRodney W. Grimes #define MAXTYPES 50		/* max number of node types */
594b88c807SRodney W. Grimes #define MAXFIELDS 20		/* max fields in a structure */
604b88c807SRodney W. Grimes #define BUFLEN 100		/* size of character buffers */
614b88c807SRodney W. Grimes 
624b88c807SRodney W. Grimes /* field types */
634b88c807SRodney W. Grimes #define T_NODE 1		/* union node *field */
644b88c807SRodney W. Grimes #define T_NODELIST 2		/* struct nodelist *field */
654b88c807SRodney W. Grimes #define T_STRING 3
664b88c807SRodney W. Grimes #define T_INT 4			/* int field */
674b88c807SRodney W. Grimes #define T_OTHER 5		/* other */
684b88c807SRodney W. Grimes #define T_TEMP 6		/* don't copy this field */
694b88c807SRodney W. Grimes 
704b88c807SRodney W. Grimes 
714b88c807SRodney W. Grimes struct field {			/* a structure field */
724b88c807SRodney W. Grimes 	char *name;		/* name of field */
734b88c807SRodney W. Grimes 	int type;			/* type of field */
744b88c807SRodney W. Grimes 	char *decl;		/* declaration of field */
754b88c807SRodney W. Grimes };
764b88c807SRodney W. Grimes 
774b88c807SRodney W. Grimes 
784b88c807SRodney W. Grimes struct str {			/* struct representing a node structure */
794b88c807SRodney W. Grimes 	char *tag;		/* structure tag */
804b88c807SRodney W. Grimes 	int nfields;		/* number of fields in the structure */
814b88c807SRodney W. Grimes 	struct field field[MAXFIELDS];	/* the fields of the structure */
824b88c807SRodney W. Grimes 	int done;			/* set if fully parsed */
834b88c807SRodney W. Grimes };
844b88c807SRodney W. Grimes 
854b88c807SRodney W. Grimes 
86aa9caaf6SPeter Wemm static int ntypes;			/* number of node types */
87aa9caaf6SPeter Wemm static char *nodename[MAXTYPES];	/* names of the nodes */
88aa9caaf6SPeter Wemm static struct str *nodestr[MAXTYPES];	/* type of structure used by the node */
89aa9caaf6SPeter Wemm static int nstr;			/* number of structures */
90aa9caaf6SPeter Wemm static struct str str[MAXTYPES];	/* the structures */
91aa9caaf6SPeter Wemm static struct str *curstr;		/* current structure */
92ef15bfc5SPeter Wemm static FILE *infp;
93aa9caaf6SPeter Wemm static char line[1024];
94aa9caaf6SPeter Wemm static int linno;
95aa9caaf6SPeter Wemm static char *linep;
96aa9caaf6SPeter Wemm 
975134c3f7SWarner Losh static void parsenode(void);
985134c3f7SWarner Losh static void parsefield(void);
995134c3f7SWarner Losh static void output(char *);
1005134c3f7SWarner Losh static void outsizes(FILE *);
1015134c3f7SWarner Losh static void outfunc(FILE *, int);
1025134c3f7SWarner Losh static void indent(int, FILE *);
1035134c3f7SWarner Losh static int nextfield(char *);
1045134c3f7SWarner Losh static void skipbl(void);
1055134c3f7SWarner Losh static int readline(void);
106f9bcf9caSColin Percival static void error(const char *, ...) __printf0like(1, 2) __dead2;
1075134c3f7SWarner Losh static char *savestr(const char *);
1084b88c807SRodney W. Grimes 
1094b88c807SRodney W. Grimes 
110aa9caaf6SPeter Wemm int
1115134c3f7SWarner Losh main(int argc, char *argv[])
1124b88c807SRodney W. Grimes {
1134b88c807SRodney W. Grimes 	if (argc != 3)
11416992ff4SPeter Wemm 		error("usage: mknodes file");
115ef15bfc5SPeter Wemm 	infp = stdin;
1164b88c807SRodney W. Grimes 	if ((infp = fopen(argv[1], "r")) == NULL)
1176c48b6cfSMartin Cracauer 		error("Can't open %s: %s", argv[1], strerror(errno));
1184b88c807SRodney W. Grimes 	while (readline()) {
1194b88c807SRodney W. Grimes 		if (line[0] == ' ' || line[0] == '\t')
1204b88c807SRodney W. Grimes 			parsefield();
1214b88c807SRodney W. Grimes 		else if (line[0] != '\0')
1224b88c807SRodney W. Grimes 			parsenode();
1234b88c807SRodney W. Grimes 	}
1244b88c807SRodney W. Grimes 	output(argv[2]);
1254b88c807SRodney W. Grimes 	exit(0);
1264b88c807SRodney W. Grimes }
1274b88c807SRodney W. Grimes 
1284b88c807SRodney W. Grimes 
1294b88c807SRodney W. Grimes 
130aa9caaf6SPeter Wemm static void
1315134c3f7SWarner Losh parsenode(void)
132aa9caaf6SPeter Wemm {
1334b88c807SRodney W. Grimes 	char name[BUFLEN];
1344b88c807SRodney W. Grimes 	char tag[BUFLEN];
1354b88c807SRodney W. Grimes 	struct str *sp;
1364b88c807SRodney W. Grimes 
1374b88c807SRodney W. Grimes 	if (curstr && curstr->nfields > 0)
1384b88c807SRodney W. Grimes 		curstr->done = 1;
1394b88c807SRodney W. Grimes 	nextfield(name);
1404b88c807SRodney W. Grimes 	if (! nextfield(tag))
1414b88c807SRodney W. Grimes 		error("Tag expected");
1424b88c807SRodney W. Grimes 	if (*linep != '\0')
1434b88c807SRodney W. Grimes 		error("Garbage at end of line");
1444b88c807SRodney W. Grimes 	nodename[ntypes] = savestr(name);
1454b88c807SRodney W. Grimes 	for (sp = str ; sp < str + nstr ; sp++) {
146aa9caaf6SPeter Wemm 		if (strcmp(sp->tag, tag) == 0)
1474b88c807SRodney W. Grimes 			break;
1484b88c807SRodney W. Grimes 	}
1494b88c807SRodney W. Grimes 	if (sp >= str + nstr) {
1504b88c807SRodney W. Grimes 		sp->tag = savestr(tag);
1514b88c807SRodney W. Grimes 		sp->nfields = 0;
1524b88c807SRodney W. Grimes 		curstr = sp;
1534b88c807SRodney W. Grimes 		nstr++;
1544b88c807SRodney W. Grimes 	}
1554b88c807SRodney W. Grimes 	nodestr[ntypes] = sp;
1564b88c807SRodney W. Grimes 	ntypes++;
1574b88c807SRodney W. Grimes }
1584b88c807SRodney W. Grimes 
1594b88c807SRodney W. Grimes 
160aa9caaf6SPeter Wemm static void
1615134c3f7SWarner Losh parsefield(void)
162aa9caaf6SPeter Wemm {
1634b88c807SRodney W. Grimes 	char name[BUFLEN];
1644b88c807SRodney W. Grimes 	char type[BUFLEN];
1654b88c807SRodney W. Grimes 	char decl[2 * BUFLEN];
1664b88c807SRodney W. Grimes 	struct field *fp;
1674b88c807SRodney W. Grimes 
1684b88c807SRodney W. Grimes 	if (curstr == NULL || curstr->done)
1694b88c807SRodney W. Grimes 		error("No current structure to add field to");
1704b88c807SRodney W. Grimes 	if (! nextfield(name))
1714b88c807SRodney W. Grimes 		error("No field name");
1724b88c807SRodney W. Grimes 	if (! nextfield(type))
1734b88c807SRodney W. Grimes 		error("No field type");
1744b88c807SRodney W. Grimes 	fp = &curstr->field[curstr->nfields];
1754b88c807SRodney W. Grimes 	fp->name = savestr(name);
176aa9caaf6SPeter Wemm 	if (strcmp(type, "nodeptr") == 0) {
1774b88c807SRodney W. Grimes 		fp->type = T_NODE;
1784b88c807SRodney W. Grimes 		sprintf(decl, "union node *%s", name);
179aa9caaf6SPeter Wemm 	} else if (strcmp(type, "nodelist") == 0) {
1804b88c807SRodney W. Grimes 		fp->type = T_NODELIST;
1814b88c807SRodney W. Grimes 		sprintf(decl, "struct nodelist *%s", name);
182aa9caaf6SPeter Wemm 	} else if (strcmp(type, "string") == 0) {
1834b88c807SRodney W. Grimes 		fp->type = T_STRING;
1844b88c807SRodney W. Grimes 		sprintf(decl, "char *%s", name);
185aa9caaf6SPeter Wemm 	} else if (strcmp(type, "int") == 0) {
1864b88c807SRodney W. Grimes 		fp->type = T_INT;
1874b88c807SRodney W. Grimes 		sprintf(decl, "int %s", name);
188aa9caaf6SPeter Wemm 	} else if (strcmp(type, "other") == 0) {
1894b88c807SRodney W. Grimes 		fp->type = T_OTHER;
190aa9caaf6SPeter Wemm 	} else if (strcmp(type, "temp") == 0) {
1914b88c807SRodney W. Grimes 		fp->type = T_TEMP;
1924b88c807SRodney W. Grimes 	} else {
1934b88c807SRodney W. Grimes 		error("Unknown type %s", type);
1944b88c807SRodney W. Grimes 	}
1954b88c807SRodney W. Grimes 	if (fp->type == T_OTHER || fp->type == T_TEMP) {
1964b88c807SRodney W. Grimes 		skipbl();
1974b88c807SRodney W. Grimes 		fp->decl = savestr(linep);
1984b88c807SRodney W. Grimes 	} else {
1994b88c807SRodney W. Grimes 		if (*linep)
2004b88c807SRodney W. Grimes 			error("Garbage at end of line");
2014b88c807SRodney W. Grimes 		fp->decl = savestr(decl);
2024b88c807SRodney W. Grimes 	}
2034b88c807SRodney W. Grimes 	curstr->nfields++;
2044b88c807SRodney W. Grimes }
2054b88c807SRodney W. Grimes 
2064b88c807SRodney W. Grimes 
2074b88c807SRodney W. Grimes char writer[] = "\
2084b88c807SRodney W. Grimes /*\n\
2094b88c807SRodney W. Grimes  * This file was generated by the mknodes program.\n\
2104b88c807SRodney W. Grimes  */\n\
2114b88c807SRodney W. Grimes \n";
2124b88c807SRodney W. Grimes 
213aa9caaf6SPeter Wemm static void
2145134c3f7SWarner Losh output(char *file)
2154b88c807SRodney W. Grimes {
2164b88c807SRodney W. Grimes 	FILE *hfile;
2174b88c807SRodney W. Grimes 	FILE *cfile;
2184b88c807SRodney W. Grimes 	FILE *patfile;
2194b88c807SRodney W. Grimes 	int i;
2204b88c807SRodney W. Grimes 	struct str *sp;
2214b88c807SRodney W. Grimes 	struct field *fp;
2224b88c807SRodney W. Grimes 	char *p;
2234b88c807SRodney W. Grimes 
2244b88c807SRodney W. Grimes 	if ((patfile = fopen(file, "r")) == NULL)
2256c48b6cfSMartin Cracauer 		error("Can't open %s: %s", file, strerror(errno));
2264b88c807SRodney W. Grimes 	if ((hfile = fopen("nodes.h", "w")) == NULL)
2276c48b6cfSMartin Cracauer 		error("Can't create nodes.h: %s", strerror(errno));
2284b88c807SRodney W. Grimes 	if ((cfile = fopen("nodes.c", "w")) == NULL)
2294b88c807SRodney W. Grimes 		error("Can't create nodes.c");
2304b88c807SRodney W. Grimes 	fputs(writer, hfile);
2314b88c807SRodney W. Grimes 	for (i = 0 ; i < ntypes ; i++)
2324b88c807SRodney W. Grimes 		fprintf(hfile, "#define %s %d\n", nodename[i], i);
2334b88c807SRodney W. Grimes 	fputs("\n\n\n", hfile);
2344b88c807SRodney W. Grimes 	for (sp = str ; sp < &str[nstr] ; sp++) {
2354b88c807SRodney W. Grimes 		fprintf(hfile, "struct %s {\n", sp->tag);
2364b88c807SRodney W. Grimes 		for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
2374b88c807SRodney W. Grimes 			fprintf(hfile, "      %s;\n", fp->decl);
2384b88c807SRodney W. Grimes 		}
2394b88c807SRodney W. Grimes 		fputs("};\n\n\n", hfile);
2404b88c807SRodney W. Grimes 	}
2414b88c807SRodney W. Grimes 	fputs("union node {\n", hfile);
2424b88c807SRodney W. Grimes 	fprintf(hfile, "      int type;\n");
2434b88c807SRodney W. Grimes 	for (sp = str ; sp < &str[nstr] ; sp++) {
2444b88c807SRodney W. Grimes 		fprintf(hfile, "      struct %s %s;\n", sp->tag, sp->tag);
2454b88c807SRodney W. Grimes 	}
2464b88c807SRodney W. Grimes 	fputs("};\n\n\n", hfile);
2474b88c807SRodney W. Grimes 	fputs("struct nodelist {\n", hfile);
2484b88c807SRodney W. Grimes 	fputs("\tstruct nodelist *next;\n", hfile);
2494b88c807SRodney W. Grimes 	fputs("\tunion node *n;\n", hfile);
2504b88c807SRodney W. Grimes 	fputs("};\n\n\n", hfile);
251eb33e843SJilles Tjoelker 	fputs("struct funcdef {\n", hfile);
252eb33e843SJilles Tjoelker 	fputs("\tunsigned int refcount;\n", hfile);
253eb33e843SJilles Tjoelker 	fputs("\tunion node n;\n", hfile);
254eb33e843SJilles Tjoelker 	fputs("};\n\n\n", hfile);
255eb33e843SJilles Tjoelker 	fputs("struct funcdef *copyfunc(union node *);\n", hfile);
256eb33e843SJilles Tjoelker 	fputs("void reffunc(struct funcdef *);\n", hfile);
257eb33e843SJilles Tjoelker 	fputs("void unreffunc(struct funcdef *);\n", hfile);
2584b88c807SRodney W. Grimes 
2594b88c807SRodney W. Grimes 	fputs(writer, cfile);
2604b88c807SRodney W. Grimes 	while (fgets(line, sizeof line, patfile) != NULL) {
2614b88c807SRodney W. Grimes 		for (p = line ; *p == ' ' || *p == '\t' ; p++);
262aa9caaf6SPeter Wemm 		if (strcmp(p, "%SIZES\n") == 0)
2634b88c807SRodney W. Grimes 			outsizes(cfile);
264aa9caaf6SPeter Wemm 		else if (strcmp(p, "%CALCSIZE\n") == 0)
2654b88c807SRodney W. Grimes 			outfunc(cfile, 1);
266aa9caaf6SPeter Wemm 		else if (strcmp(p, "%COPY\n") == 0)
2674b88c807SRodney W. Grimes 			outfunc(cfile, 0);
2684b88c807SRodney W. Grimes 		else
2694b88c807SRodney W. Grimes 			fputs(line, cfile);
2704b88c807SRodney W. Grimes 	}
2714b88c807SRodney W. Grimes }
2724b88c807SRodney W. Grimes 
2734b88c807SRodney W. Grimes 
2744b88c807SRodney W. Grimes 
275aa9caaf6SPeter Wemm static void
2765134c3f7SWarner Losh outsizes(FILE *cfile)
2774b88c807SRodney W. Grimes {
2784b88c807SRodney W. Grimes 	int i;
2794b88c807SRodney W. Grimes 
2804b88c807SRodney W. Grimes 	fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
2814b88c807SRodney W. Grimes 	for (i = 0 ; i < ntypes ; i++) {
2824b88c807SRodney W. Grimes 		fprintf(cfile, "      ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
2834b88c807SRodney W. Grimes 	}
2844b88c807SRodney W. Grimes 	fprintf(cfile, "};\n");
2854b88c807SRodney W. Grimes }
2864b88c807SRodney W. Grimes 
2874b88c807SRodney W. Grimes 
288aa9caaf6SPeter Wemm static void
2895134c3f7SWarner Losh outfunc(FILE *cfile, int calcsize)
2904b88c807SRodney W. Grimes {
2914b88c807SRodney W. Grimes 	struct str *sp;
2924b88c807SRodney W. Grimes 	struct field *fp;
2934b88c807SRodney W. Grimes 	int i;
2944b88c807SRodney W. Grimes 
2954b88c807SRodney W. Grimes 	fputs("      if (n == NULL)\n", cfile);
2964b88c807SRodney W. Grimes 	if (calcsize)
2974b88c807SRodney W. Grimes 		fputs("	    return;\n", cfile);
2984b88c807SRodney W. Grimes 	else
2994b88c807SRodney W. Grimes 		fputs("	    return NULL;\n", cfile);
3004b88c807SRodney W. Grimes 	if (calcsize)
3014b88c807SRodney W. Grimes 		fputs("      funcblocksize += nodesize[n->type];\n", cfile);
3024b88c807SRodney W. Grimes 	else {
3034b88c807SRodney W. Grimes 		fputs("      new = funcblock;\n", cfile);
3047e461ef4SSteve Price 		fputs("      funcblock = (char *)funcblock + nodesize[n->type];\n", cfile);
3054b88c807SRodney W. Grimes 	}
3064b88c807SRodney W. Grimes 	fputs("      switch (n->type) {\n", cfile);
3074b88c807SRodney W. Grimes 	for (sp = str ; sp < &str[nstr] ; sp++) {
3084b88c807SRodney W. Grimes 		for (i = 0 ; i < ntypes ; i++) {
3094b88c807SRodney W. Grimes 			if (nodestr[i] == sp)
3104b88c807SRodney W. Grimes 				fprintf(cfile, "      case %s:\n", nodename[i]);
3114b88c807SRodney W. Grimes 		}
3124b88c807SRodney W. Grimes 		for (i = sp->nfields ; --i >= 1 ; ) {
3134b88c807SRodney W. Grimes 			fp = &sp->field[i];
3144b88c807SRodney W. Grimes 			switch (fp->type) {
3154b88c807SRodney W. Grimes 			case T_NODE:
3164b88c807SRodney W. Grimes 				if (calcsize) {
3174b88c807SRodney W. Grimes 					indent(12, cfile);
3184b88c807SRodney W. Grimes 					fprintf(cfile, "calcsize(n->%s.%s);\n",
3194b88c807SRodney W. Grimes 						sp->tag, fp->name);
3204b88c807SRodney W. Grimes 				} else {
3214b88c807SRodney W. Grimes 					indent(12, cfile);
3224b88c807SRodney W. Grimes 					fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
3234b88c807SRodney W. Grimes 						sp->tag, fp->name, sp->tag, fp->name);
3244b88c807SRodney W. Grimes 				}
3254b88c807SRodney W. Grimes 				break;
3264b88c807SRodney W. Grimes 			case T_NODELIST:
3274b88c807SRodney W. Grimes 				if (calcsize) {
3284b88c807SRodney W. Grimes 					indent(12, cfile);
3294b88c807SRodney W. Grimes 					fprintf(cfile, "sizenodelist(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 = copynodelist(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_STRING:
3384b88c807SRodney W. Grimes 				if (calcsize) {
3394b88c807SRodney W. Grimes 					indent(12, cfile);
3404b88c807SRodney W. Grimes 					fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\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 = nodesavestr(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_INT:
3494b88c807SRodney W. Grimes 			case T_OTHER:
3504b88c807SRodney W. Grimes 				if (! calcsize) {
3514b88c807SRodney W. Grimes 					indent(12, cfile);
3524b88c807SRodney W. Grimes 					fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
3534b88c807SRodney W. Grimes 						sp->tag, fp->name, sp->tag, fp->name);
3544b88c807SRodney W. Grimes 				}
3554b88c807SRodney W. Grimes 				break;
3564b88c807SRodney W. Grimes 			}
3574b88c807SRodney W. Grimes 		}
3584b88c807SRodney W. Grimes 		indent(12, cfile);
3594b88c807SRodney W. Grimes 		fputs("break;\n", cfile);
3604b88c807SRodney W. Grimes 	}
3614b88c807SRodney W. Grimes 	fputs("      };\n", cfile);
3624b88c807SRodney W. Grimes 	if (! calcsize)
3634b88c807SRodney W. Grimes 		fputs("      new->type = n->type;\n", cfile);
3644b88c807SRodney W. Grimes }
3654b88c807SRodney W. Grimes 
3664b88c807SRodney W. Grimes 
367aa9caaf6SPeter Wemm static void
3685134c3f7SWarner Losh indent(int amount, FILE *fp)
3694b88c807SRodney W. Grimes {
3704b88c807SRodney W. Grimes 	while (amount >= 8) {
3714b88c807SRodney W. Grimes 		putc('\t', fp);
3724b88c807SRodney W. Grimes 		amount -= 8;
3734b88c807SRodney W. Grimes 	}
3744b88c807SRodney W. Grimes 	while (--amount >= 0) {
3754b88c807SRodney W. Grimes 		putc(' ', fp);
3764b88c807SRodney W. Grimes 	}
3774b88c807SRodney W. Grimes }
3784b88c807SRodney W. Grimes 
3794b88c807SRodney W. Grimes 
380aa9caaf6SPeter Wemm static int
3815134c3f7SWarner Losh nextfield(char *buf)
3824b88c807SRodney W. Grimes {
3837e461ef4SSteve Price 	char *p, *q;
3844b88c807SRodney W. Grimes 
3854b88c807SRodney W. Grimes 	p = linep;
3864b88c807SRodney W. Grimes 	while (*p == ' ' || *p == '\t')
3874b88c807SRodney W. Grimes 		p++;
3884b88c807SRodney W. Grimes 	q = buf;
3894b88c807SRodney W. Grimes 	while (*p != ' ' && *p != '\t' && *p != '\0')
3904b88c807SRodney W. Grimes 		*q++ = *p++;
3914b88c807SRodney W. Grimes 	*q = '\0';
3924b88c807SRodney W. Grimes 	linep = p;
3934b88c807SRodney W. Grimes 	return (q > buf);
3944b88c807SRodney W. Grimes }
3954b88c807SRodney W. Grimes 
3964b88c807SRodney W. Grimes 
397aa9caaf6SPeter Wemm static void
3985134c3f7SWarner Losh skipbl(void)
399aa9caaf6SPeter Wemm {
4004b88c807SRodney W. Grimes 	while (*linep == ' ' || *linep == '\t')
4014b88c807SRodney W. Grimes 		linep++;
4024b88c807SRodney W. Grimes }
4034b88c807SRodney W. Grimes 
4044b88c807SRodney W. Grimes 
405aa9caaf6SPeter Wemm static int
4065134c3f7SWarner Losh readline(void)
407aa9caaf6SPeter Wemm {
4087e461ef4SSteve Price 	char *p;
4094b88c807SRodney W. Grimes 
4104b88c807SRodney W. Grimes 	if (fgets(line, 1024, infp) == NULL)
4114b88c807SRodney W. Grimes 		return 0;
4124b88c807SRodney W. Grimes 	for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
4134b88c807SRodney W. Grimes 	while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
4144b88c807SRodney W. Grimes 		p--;
4154b88c807SRodney W. Grimes 	*p = '\0';
4164b88c807SRodney W. Grimes 	linep = line;
4174b88c807SRodney W. Grimes 	linno++;
4184b88c807SRodney W. Grimes 	if (p - line > BUFLEN)
4194b88c807SRodney W. Grimes 		error("Line too long");
4204b88c807SRodney W. Grimes 	return 1;
4214b88c807SRodney W. Grimes }
4224b88c807SRodney W. Grimes 
4234b88c807SRodney W. Grimes 
4244b88c807SRodney W. Grimes 
425aa9caaf6SPeter Wemm static void
426aa9caaf6SPeter Wemm error(const char *msg, ...)
4274b88c807SRodney W. Grimes {
428aa9caaf6SPeter Wemm 	va_list va;
429aa9caaf6SPeter Wemm 	va_start(va, msg);
430aa9caaf6SPeter Wemm 
431aa9caaf6SPeter Wemm 	(void) fprintf(stderr, "line %d: ", linno);
432aa9caaf6SPeter Wemm 	(void) vfprintf(stderr, msg, va);
433aa9caaf6SPeter Wemm 	(void) fputc('\n', stderr);
434aa9caaf6SPeter Wemm 
435aa9caaf6SPeter Wemm 	va_end(va);
436aa9caaf6SPeter Wemm 
4374b88c807SRodney W. Grimes 	exit(2);
4384b88c807SRodney W. Grimes }
4394b88c807SRodney W. Grimes 
4404b88c807SRodney W. Grimes 
4414b88c807SRodney W. Grimes 
442aa9caaf6SPeter Wemm static char *
4435134c3f7SWarner Losh savestr(const char *s)
4444b88c807SRodney W. Grimes {
4457e461ef4SSteve Price 	char *p;
4464b88c807SRodney W. Grimes 
4474b88c807SRodney W. Grimes 	if ((p = malloc(strlen(s) + 1)) == NULL)
4484b88c807SRodney W. Grimes 		error("Out of space");
449aa9caaf6SPeter Wemm 	(void) strcpy(p, s);
4504b88c807SRodney W. Grimes 	return p;
4514b88c807SRodney W. Grimes }
452