xref: /freebsd/bin/sh/mknodes.c (revision 58bdb0761cb947019e37f98c4c8e2dc10b4473dc)
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 
207*58bdb076SJilles Tjoelker static const 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);
251e16947f8SJilles Tjoelker 	fputs("struct funcdef;\n", hfile);
252eb33e843SJilles Tjoelker 	fputs("struct funcdef *copyfunc(union node *);\n", hfile);
253e16947f8SJilles Tjoelker 	fputs("union node *getfuncnode(struct funcdef *);\n", hfile);
254eb33e843SJilles Tjoelker 	fputs("void reffunc(struct funcdef *);\n", hfile);
255eb33e843SJilles Tjoelker 	fputs("void unreffunc(struct funcdef *);\n", hfile);
2564b88c807SRodney W. Grimes 
2574b88c807SRodney W. Grimes 	fputs(writer, cfile);
2584b88c807SRodney W. Grimes 	while (fgets(line, sizeof line, patfile) != NULL) {
2594b88c807SRodney W. Grimes 		for (p = line ; *p == ' ' || *p == '\t' ; p++);
260aa9caaf6SPeter Wemm 		if (strcmp(p, "%SIZES\n") == 0)
2614b88c807SRodney W. Grimes 			outsizes(cfile);
262aa9caaf6SPeter Wemm 		else if (strcmp(p, "%CALCSIZE\n") == 0)
2634b88c807SRodney W. Grimes 			outfunc(cfile, 1);
264aa9caaf6SPeter Wemm 		else if (strcmp(p, "%COPY\n") == 0)
2654b88c807SRodney W. Grimes 			outfunc(cfile, 0);
2664b88c807SRodney W. Grimes 		else
2674b88c807SRodney W. Grimes 			fputs(line, cfile);
2684b88c807SRodney W. Grimes 	}
2694b88c807SRodney W. Grimes }
2704b88c807SRodney W. Grimes 
2714b88c807SRodney W. Grimes 
2724b88c807SRodney W. Grimes 
273aa9caaf6SPeter Wemm static void
2745134c3f7SWarner Losh outsizes(FILE *cfile)
2754b88c807SRodney W. Grimes {
2764b88c807SRodney W. Grimes 	int i;
2774b88c807SRodney W. Grimes 
2784b88c807SRodney W. Grimes 	fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
2794b88c807SRodney W. Grimes 	for (i = 0 ; i < ntypes ; i++) {
2804b88c807SRodney W. Grimes 		fprintf(cfile, "      ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
2814b88c807SRodney W. Grimes 	}
2824b88c807SRodney W. Grimes 	fprintf(cfile, "};\n");
2834b88c807SRodney W. Grimes }
2844b88c807SRodney W. Grimes 
2854b88c807SRodney W. Grimes 
286aa9caaf6SPeter Wemm static void
2875134c3f7SWarner Losh outfunc(FILE *cfile, int calcsize)
2884b88c807SRodney W. Grimes {
2894b88c807SRodney W. Grimes 	struct str *sp;
2904b88c807SRodney W. Grimes 	struct field *fp;
2914b88c807SRodney W. Grimes 	int i;
2924b88c807SRodney W. Grimes 
2934b88c807SRodney W. Grimes 	fputs("      if (n == NULL)\n", cfile);
2944b88c807SRodney W. Grimes 	if (calcsize)
2954b88c807SRodney W. Grimes 		fputs("	    return;\n", cfile);
2964b88c807SRodney W. Grimes 	else
2974b88c807SRodney W. Grimes 		fputs("	    return NULL;\n", cfile);
2984b88c807SRodney W. Grimes 	if (calcsize)
2994b88c807SRodney W. Grimes 		fputs("      funcblocksize += nodesize[n->type];\n", cfile);
3004b88c807SRodney W. Grimes 	else {
3014b88c807SRodney W. Grimes 		fputs("      new = funcblock;\n", cfile);
3027e461ef4SSteve Price 		fputs("      funcblock = (char *)funcblock + nodesize[n->type];\n", cfile);
3034b88c807SRodney W. Grimes 	}
3044b88c807SRodney W. Grimes 	fputs("      switch (n->type) {\n", cfile);
3054b88c807SRodney W. Grimes 	for (sp = str ; sp < &str[nstr] ; sp++) {
3064b88c807SRodney W. Grimes 		for (i = 0 ; i < ntypes ; i++) {
3074b88c807SRodney W. Grimes 			if (nodestr[i] == sp)
3084b88c807SRodney W. Grimes 				fprintf(cfile, "      case %s:\n", nodename[i]);
3094b88c807SRodney W. Grimes 		}
3104b88c807SRodney W. Grimes 		for (i = sp->nfields ; --i >= 1 ; ) {
3114b88c807SRodney W. Grimes 			fp = &sp->field[i];
3124b88c807SRodney W. Grimes 			switch (fp->type) {
3134b88c807SRodney W. Grimes 			case T_NODE:
3144b88c807SRodney W. Grimes 				if (calcsize) {
3154b88c807SRodney W. Grimes 					indent(12, cfile);
3164b88c807SRodney W. Grimes 					fprintf(cfile, "calcsize(n->%s.%s);\n",
3174b88c807SRodney W. Grimes 						sp->tag, fp->name);
3184b88c807SRodney W. Grimes 				} else {
3194b88c807SRodney W. Grimes 					indent(12, cfile);
3204b88c807SRodney W. Grimes 					fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
3214b88c807SRodney W. Grimes 						sp->tag, fp->name, sp->tag, fp->name);
3224b88c807SRodney W. Grimes 				}
3234b88c807SRodney W. Grimes 				break;
3244b88c807SRodney W. Grimes 			case T_NODELIST:
3254b88c807SRodney W. Grimes 				if (calcsize) {
3264b88c807SRodney W. Grimes 					indent(12, cfile);
3274b88c807SRodney W. Grimes 					fprintf(cfile, "sizenodelist(n->%s.%s);\n",
3284b88c807SRodney W. Grimes 						sp->tag, fp->name);
3294b88c807SRodney W. Grimes 				} else {
3304b88c807SRodney W. Grimes 					indent(12, cfile);
3314b88c807SRodney W. Grimes 					fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
3324b88c807SRodney W. Grimes 						sp->tag, fp->name, sp->tag, fp->name);
3334b88c807SRodney W. Grimes 				}
3344b88c807SRodney W. Grimes 				break;
3354b88c807SRodney W. Grimes 			case T_STRING:
3364b88c807SRodney W. Grimes 				if (calcsize) {
3374b88c807SRodney W. Grimes 					indent(12, cfile);
3384b88c807SRodney W. Grimes 					fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
3394b88c807SRodney W. Grimes 						sp->tag, fp->name);
3404b88c807SRodney W. Grimes 				} else {
3414b88c807SRodney W. Grimes 					indent(12, cfile);
3424b88c807SRodney W. Grimes 					fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
3434b88c807SRodney W. Grimes 						sp->tag, fp->name, sp->tag, fp->name);
3444b88c807SRodney W. Grimes 				}
3454b88c807SRodney W. Grimes 				break;
3464b88c807SRodney W. Grimes 			case T_INT:
3474b88c807SRodney W. Grimes 			case T_OTHER:
3484b88c807SRodney W. Grimes 				if (! calcsize) {
3494b88c807SRodney W. Grimes 					indent(12, cfile);
3504b88c807SRodney W. Grimes 					fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
3514b88c807SRodney W. Grimes 						sp->tag, fp->name, sp->tag, fp->name);
3524b88c807SRodney W. Grimes 				}
3534b88c807SRodney W. Grimes 				break;
3544b88c807SRodney W. Grimes 			}
3554b88c807SRodney W. Grimes 		}
3564b88c807SRodney W. Grimes 		indent(12, cfile);
3574b88c807SRodney W. Grimes 		fputs("break;\n", cfile);
3584b88c807SRodney W. Grimes 	}
3594b88c807SRodney W. Grimes 	fputs("      };\n", cfile);
3604b88c807SRodney W. Grimes 	if (! calcsize)
3614b88c807SRodney W. Grimes 		fputs("      new->type = n->type;\n", cfile);
3624b88c807SRodney W. Grimes }
3634b88c807SRodney W. Grimes 
3644b88c807SRodney W. Grimes 
365aa9caaf6SPeter Wemm static void
3665134c3f7SWarner Losh indent(int amount, FILE *fp)
3674b88c807SRodney W. Grimes {
3684b88c807SRodney W. Grimes 	while (amount >= 8) {
3694b88c807SRodney W. Grimes 		putc('\t', fp);
3704b88c807SRodney W. Grimes 		amount -= 8;
3714b88c807SRodney W. Grimes 	}
3724b88c807SRodney W. Grimes 	while (--amount >= 0) {
3734b88c807SRodney W. Grimes 		putc(' ', fp);
3744b88c807SRodney W. Grimes 	}
3754b88c807SRodney W. Grimes }
3764b88c807SRodney W. Grimes 
3774b88c807SRodney W. Grimes 
378aa9caaf6SPeter Wemm static int
3795134c3f7SWarner Losh nextfield(char *buf)
3804b88c807SRodney W. Grimes {
3817e461ef4SSteve Price 	char *p, *q;
3824b88c807SRodney W. Grimes 
3834b88c807SRodney W. Grimes 	p = linep;
3844b88c807SRodney W. Grimes 	while (*p == ' ' || *p == '\t')
3854b88c807SRodney W. Grimes 		p++;
3864b88c807SRodney W. Grimes 	q = buf;
3874b88c807SRodney W. Grimes 	while (*p != ' ' && *p != '\t' && *p != '\0')
3884b88c807SRodney W. Grimes 		*q++ = *p++;
3894b88c807SRodney W. Grimes 	*q = '\0';
3904b88c807SRodney W. Grimes 	linep = p;
3914b88c807SRodney W. Grimes 	return (q > buf);
3924b88c807SRodney W. Grimes }
3934b88c807SRodney W. Grimes 
3944b88c807SRodney W. Grimes 
395aa9caaf6SPeter Wemm static void
3965134c3f7SWarner Losh skipbl(void)
397aa9caaf6SPeter Wemm {
3984b88c807SRodney W. Grimes 	while (*linep == ' ' || *linep == '\t')
3994b88c807SRodney W. Grimes 		linep++;
4004b88c807SRodney W. Grimes }
4014b88c807SRodney W. Grimes 
4024b88c807SRodney W. Grimes 
403aa9caaf6SPeter Wemm static int
4045134c3f7SWarner Losh readline(void)
405aa9caaf6SPeter Wemm {
4067e461ef4SSteve Price 	char *p;
4074b88c807SRodney W. Grimes 
4084b88c807SRodney W. Grimes 	if (fgets(line, 1024, infp) == NULL)
4094b88c807SRodney W. Grimes 		return 0;
4104b88c807SRodney W. Grimes 	for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
4114b88c807SRodney W. Grimes 	while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
4124b88c807SRodney W. Grimes 		p--;
4134b88c807SRodney W. Grimes 	*p = '\0';
4144b88c807SRodney W. Grimes 	linep = line;
4154b88c807SRodney W. Grimes 	linno++;
4164b88c807SRodney W. Grimes 	if (p - line > BUFLEN)
4174b88c807SRodney W. Grimes 		error("Line too long");
4184b88c807SRodney W. Grimes 	return 1;
4194b88c807SRodney W. Grimes }
4204b88c807SRodney W. Grimes 
4214b88c807SRodney W. Grimes 
4224b88c807SRodney W. Grimes 
423aa9caaf6SPeter Wemm static void
424aa9caaf6SPeter Wemm error(const char *msg, ...)
4254b88c807SRodney W. Grimes {
426aa9caaf6SPeter Wemm 	va_list va;
427aa9caaf6SPeter Wemm 	va_start(va, msg);
428aa9caaf6SPeter Wemm 
429aa9caaf6SPeter Wemm 	(void) fprintf(stderr, "line %d: ", linno);
430aa9caaf6SPeter Wemm 	(void) vfprintf(stderr, msg, va);
431aa9caaf6SPeter Wemm 	(void) fputc('\n', stderr);
432aa9caaf6SPeter Wemm 
433aa9caaf6SPeter Wemm 	va_end(va);
434aa9caaf6SPeter Wemm 
4354b88c807SRodney W. Grimes 	exit(2);
4364b88c807SRodney W. Grimes }
4374b88c807SRodney W. Grimes 
4384b88c807SRodney W. Grimes 
4394b88c807SRodney W. Grimes 
440aa9caaf6SPeter Wemm static char *
4415134c3f7SWarner Losh savestr(const char *s)
4424b88c807SRodney W. Grimes {
4437e461ef4SSteve Price 	char *p;
4444b88c807SRodney W. Grimes 
4454b88c807SRodney W. Grimes 	if ((p = malloc(strlen(s) + 1)) == NULL)
4464b88c807SRodney W. Grimes 		error("Out of space");
447aa9caaf6SPeter Wemm 	(void) strcpy(p, s);
4484b88c807SRodney W. Grimes 	return p;
4494b88c807SRodney W. Grimes }
450