14b88c807SRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni *
44b88c807SRodney W. Grimes * Copyright (c) 1991, 1993
54b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved.
64b88c807SRodney W. Grimes *
74b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes * Kenneth Almquist.
94b88c807SRodney W. Grimes *
104b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
114b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions
124b88c807SRodney W. Grimes * are met:
134b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
154b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
164b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
174b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
194b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software
204b88c807SRodney W. Grimes * without specific prior written permission.
214b88c807SRodney W. Grimes *
224b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324b88c807SRodney W. Grimes * SUCH DAMAGE.
334b88c807SRodney W. Grimes */
344b88c807SRodney W. Grimes
354b88c807SRodney W. Grimes /*
364b88c807SRodney W. Grimes * This program reads the nodetypes file and nodes.c.pat file. It generates
374b88c807SRodney W. Grimes * the files nodes.h and nodes.c.
384b88c807SRodney W. Grimes */
394b88c807SRodney W. Grimes
404b88c807SRodney W. Grimes #include <stdio.h>
41aa9caaf6SPeter Wemm #include <stdlib.h>
42aa9caaf6SPeter Wemm #include <string.h>
436c48b6cfSMartin Cracauer #include <errno.h>
44aa9caaf6SPeter Wemm #include <stdarg.h>
454b88c807SRodney W. Grimes
464b88c807SRodney W. Grimes #define MAXTYPES 50 /* max number of node types */
474b88c807SRodney W. Grimes #define MAXFIELDS 20 /* max fields in a structure */
484b88c807SRodney W. Grimes #define BUFLEN 100 /* size of character buffers */
494b88c807SRodney W. Grimes
504b88c807SRodney W. Grimes /* field types */
514b88c807SRodney W. Grimes #define T_NODE 1 /* union node *field */
524b88c807SRodney W. Grimes #define T_NODELIST 2 /* struct nodelist *field */
534b88c807SRodney W. Grimes #define T_STRING 3
544b88c807SRodney W. Grimes #define T_INT 4 /* int field */
554b88c807SRodney W. Grimes #define T_OTHER 5 /* other */
564b88c807SRodney W. Grimes #define T_TEMP 6 /* don't copy this field */
574b88c807SRodney W. Grimes
584b88c807SRodney W. Grimes
594b88c807SRodney W. Grimes struct field { /* a structure field */
604b88c807SRodney W. Grimes char *name; /* name of field */
614b88c807SRodney W. Grimes int type; /* type of field */
624b88c807SRodney W. Grimes char *decl; /* declaration of field */
634b88c807SRodney W. Grimes };
644b88c807SRodney W. Grimes
654b88c807SRodney W. Grimes
664b88c807SRodney W. Grimes struct str { /* struct representing a node structure */
674b88c807SRodney W. Grimes char *tag; /* structure tag */
684b88c807SRodney W. Grimes int nfields; /* number of fields in the structure */
694b88c807SRodney W. Grimes struct field field[MAXFIELDS]; /* the fields of the structure */
704b88c807SRodney W. Grimes int done; /* set if fully parsed */
714b88c807SRodney W. Grimes };
724b88c807SRodney W. Grimes
734b88c807SRodney W. Grimes
74aa9caaf6SPeter Wemm static int ntypes; /* number of node types */
75aa9caaf6SPeter Wemm static char *nodename[MAXTYPES]; /* names of the nodes */
76aa9caaf6SPeter Wemm static struct str *nodestr[MAXTYPES]; /* type of structure used by the node */
77aa9caaf6SPeter Wemm static int nstr; /* number of structures */
78aa9caaf6SPeter Wemm static struct str str[MAXTYPES]; /* the structures */
79aa9caaf6SPeter Wemm static struct str *curstr; /* current structure */
80aa9caaf6SPeter Wemm static char line[1024];
81aa9caaf6SPeter Wemm static int linno;
82aa9caaf6SPeter Wemm static char *linep;
83aa9caaf6SPeter Wemm
845134c3f7SWarner Losh static void parsenode(void);
855134c3f7SWarner Losh static void parsefield(void);
865134c3f7SWarner Losh static void output(char *);
875134c3f7SWarner Losh static void outsizes(FILE *);
885134c3f7SWarner Losh static void outfunc(FILE *, int);
895134c3f7SWarner Losh static void indent(int, FILE *);
905134c3f7SWarner Losh static int nextfield(char *);
915134c3f7SWarner Losh static void skipbl(void);
92cc684f83SJilles Tjoelker static int readline(FILE *);
93f9bcf9caSColin Percival static void error(const char *, ...) __printf0like(1, 2) __dead2;
945134c3f7SWarner Losh static char *savestr(const char *);
954b88c807SRodney W. Grimes
964b88c807SRodney W. Grimes
97aa9caaf6SPeter Wemm int
main(int argc,char * argv[])985134c3f7SWarner Losh main(int argc, char *argv[])
994b88c807SRodney W. Grimes {
100cc684f83SJilles Tjoelker FILE *infp;
101cc684f83SJilles Tjoelker
1024b88c807SRodney W. Grimes if (argc != 3)
10316992ff4SPeter Wemm error("usage: mknodes file");
1044b88c807SRodney W. Grimes if ((infp = fopen(argv[1], "r")) == NULL)
1056c48b6cfSMartin Cracauer error("Can't open %s: %s", argv[1], strerror(errno));
106cc684f83SJilles Tjoelker while (readline(infp)) {
1074b88c807SRodney W. Grimes if (line[0] == ' ' || line[0] == '\t')
1084b88c807SRodney W. Grimes parsefield();
1094b88c807SRodney W. Grimes else if (line[0] != '\0')
1104b88c807SRodney W. Grimes parsenode();
1114b88c807SRodney W. Grimes }
112cc684f83SJilles Tjoelker fclose(infp);
1134b88c807SRodney W. Grimes output(argv[2]);
1144b88c807SRodney W. Grimes exit(0);
1154b88c807SRodney W. Grimes }
1164b88c807SRodney W. Grimes
1174b88c807SRodney W. Grimes
1184b88c807SRodney W. Grimes
119aa9caaf6SPeter Wemm static void
parsenode(void)1205134c3f7SWarner Losh parsenode(void)
121aa9caaf6SPeter Wemm {
1224b88c807SRodney W. Grimes char name[BUFLEN];
1234b88c807SRodney W. Grimes char tag[BUFLEN];
1244b88c807SRodney W. Grimes struct str *sp;
1254b88c807SRodney W. Grimes
1264b88c807SRodney W. Grimes if (curstr && curstr->nfields > 0)
1274b88c807SRodney W. Grimes curstr->done = 1;
1284b88c807SRodney W. Grimes nextfield(name);
1294b88c807SRodney W. Grimes if (! nextfield(tag))
1304b88c807SRodney W. Grimes error("Tag expected");
1314b88c807SRodney W. Grimes if (*linep != '\0')
1324b88c807SRodney W. Grimes error("Garbage at end of line");
1334b88c807SRodney W. Grimes nodename[ntypes] = savestr(name);
1344b88c807SRodney W. Grimes for (sp = str ; sp < str + nstr ; sp++) {
135aa9caaf6SPeter Wemm if (strcmp(sp->tag, tag) == 0)
1364b88c807SRodney W. Grimes break;
1374b88c807SRodney W. Grimes }
1384b88c807SRodney W. Grimes if (sp >= str + nstr) {
1394b88c807SRodney W. Grimes sp->tag = savestr(tag);
1404b88c807SRodney W. Grimes sp->nfields = 0;
1414b88c807SRodney W. Grimes curstr = sp;
1424b88c807SRodney W. Grimes nstr++;
1434b88c807SRodney W. Grimes }
1444b88c807SRodney W. Grimes nodestr[ntypes] = sp;
1454b88c807SRodney W. Grimes ntypes++;
1464b88c807SRodney W. Grimes }
1474b88c807SRodney W. Grimes
1484b88c807SRodney W. Grimes
149aa9caaf6SPeter Wemm static void
parsefield(void)1505134c3f7SWarner Losh parsefield(void)
151aa9caaf6SPeter Wemm {
1524b88c807SRodney W. Grimes char name[BUFLEN];
1534b88c807SRodney W. Grimes char type[BUFLEN];
1544b88c807SRodney W. Grimes char decl[2 * BUFLEN];
1554b88c807SRodney W. Grimes struct field *fp;
1564b88c807SRodney W. Grimes
1574b88c807SRodney W. Grimes if (curstr == NULL || curstr->done)
1584b88c807SRodney W. Grimes error("No current structure to add field to");
1594b88c807SRodney W. Grimes if (! nextfield(name))
1604b88c807SRodney W. Grimes error("No field name");
1614b88c807SRodney W. Grimes if (! nextfield(type))
1624b88c807SRodney W. Grimes error("No field type");
1634b88c807SRodney W. Grimes fp = &curstr->field[curstr->nfields];
1644b88c807SRodney W. Grimes fp->name = savestr(name);
165aa9caaf6SPeter Wemm if (strcmp(type, "nodeptr") == 0) {
1664b88c807SRodney W. Grimes fp->type = T_NODE;
1674b88c807SRodney W. Grimes sprintf(decl, "union node *%s", name);
168aa9caaf6SPeter Wemm } else if (strcmp(type, "nodelist") == 0) {
1694b88c807SRodney W. Grimes fp->type = T_NODELIST;
1704b88c807SRodney W. Grimes sprintf(decl, "struct nodelist *%s", name);
171aa9caaf6SPeter Wemm } else if (strcmp(type, "string") == 0) {
1724b88c807SRodney W. Grimes fp->type = T_STRING;
1734b88c807SRodney W. Grimes sprintf(decl, "char *%s", name);
174aa9caaf6SPeter Wemm } else if (strcmp(type, "int") == 0) {
1754b88c807SRodney W. Grimes fp->type = T_INT;
1764b88c807SRodney W. Grimes sprintf(decl, "int %s", name);
177aa9caaf6SPeter Wemm } else if (strcmp(type, "other") == 0) {
1784b88c807SRodney W. Grimes fp->type = T_OTHER;
179aa9caaf6SPeter Wemm } else if (strcmp(type, "temp") == 0) {
1804b88c807SRodney W. Grimes fp->type = T_TEMP;
1814b88c807SRodney W. Grimes } else {
1824b88c807SRodney W. Grimes error("Unknown type %s", type);
1834b88c807SRodney W. Grimes }
1844b88c807SRodney W. Grimes if (fp->type == T_OTHER || fp->type == T_TEMP) {
1854b88c807SRodney W. Grimes skipbl();
1864b88c807SRodney W. Grimes fp->decl = savestr(linep);
1874b88c807SRodney W. Grimes } else {
1884b88c807SRodney W. Grimes if (*linep)
1894b88c807SRodney W. Grimes error("Garbage at end of line");
1904b88c807SRodney W. Grimes fp->decl = savestr(decl);
1914b88c807SRodney W. Grimes }
1924b88c807SRodney W. Grimes curstr->nfields++;
1934b88c807SRodney W. Grimes }
1944b88c807SRodney W. Grimes
1954b88c807SRodney W. Grimes
19658bdb076SJilles Tjoelker static const char writer[] = "\
1974b88c807SRodney W. Grimes /*\n\
1984b88c807SRodney W. Grimes * This file was generated by the mknodes program.\n\
1994b88c807SRodney W. Grimes */\n\
2004b88c807SRodney W. Grimes \n";
2014b88c807SRodney W. Grimes
202aa9caaf6SPeter Wemm static void
output(char * file)2035134c3f7SWarner Losh output(char *file)
2044b88c807SRodney W. Grimes {
2054b88c807SRodney W. Grimes FILE *hfile;
2064b88c807SRodney W. Grimes FILE *cfile;
2074b88c807SRodney W. Grimes FILE *patfile;
2084b88c807SRodney W. Grimes int i;
2094b88c807SRodney W. Grimes struct str *sp;
2104b88c807SRodney W. Grimes struct field *fp;
2114b88c807SRodney W. Grimes char *p;
2124b88c807SRodney W. Grimes
2134b88c807SRodney W. Grimes if ((patfile = fopen(file, "r")) == NULL)
2146c48b6cfSMartin Cracauer error("Can't open %s: %s", file, strerror(errno));
2154b88c807SRodney W. Grimes if ((hfile = fopen("nodes.h", "w")) == NULL)
2166c48b6cfSMartin Cracauer error("Can't create nodes.h: %s", strerror(errno));
2174b88c807SRodney W. Grimes if ((cfile = fopen("nodes.c", "w")) == NULL)
2184b88c807SRodney W. Grimes error("Can't create nodes.c");
2194b88c807SRodney W. Grimes fputs(writer, hfile);
2204b88c807SRodney W. Grimes for (i = 0 ; i < ntypes ; i++)
2214b88c807SRodney W. Grimes fprintf(hfile, "#define %s %d\n", nodename[i], i);
2224b88c807SRodney W. Grimes fputs("\n\n\n", hfile);
2234b88c807SRodney W. Grimes for (sp = str ; sp < &str[nstr] ; sp++) {
2244b88c807SRodney W. Grimes fprintf(hfile, "struct %s {\n", sp->tag);
2254b88c807SRodney W. Grimes for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
2264b88c807SRodney W. Grimes fprintf(hfile, " %s;\n", fp->decl);
2274b88c807SRodney W. Grimes }
2284b88c807SRodney W. Grimes fputs("};\n\n\n", hfile);
2294b88c807SRodney W. Grimes }
2304b88c807SRodney W. Grimes fputs("union node {\n", hfile);
2314b88c807SRodney W. Grimes fprintf(hfile, " int type;\n");
2324b88c807SRodney W. Grimes for (sp = str ; sp < &str[nstr] ; sp++) {
2334b88c807SRodney W. Grimes fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag);
2344b88c807SRodney W. Grimes }
2354b88c807SRodney W. Grimes fputs("};\n\n\n", hfile);
2364b88c807SRodney W. Grimes fputs("struct nodelist {\n", hfile);
2374b88c807SRodney W. Grimes fputs("\tstruct nodelist *next;\n", hfile);
2384b88c807SRodney W. Grimes fputs("\tunion node *n;\n", hfile);
2394b88c807SRodney W. Grimes fputs("};\n\n\n", hfile);
240e16947f8SJilles Tjoelker fputs("struct funcdef;\n", hfile);
241eb33e843SJilles Tjoelker fputs("struct funcdef *copyfunc(union node *);\n", hfile);
242e16947f8SJilles Tjoelker fputs("union node *getfuncnode(struct funcdef *);\n", hfile);
243eb33e843SJilles Tjoelker fputs("void reffunc(struct funcdef *);\n", hfile);
244eb33e843SJilles Tjoelker fputs("void unreffunc(struct funcdef *);\n", hfile);
245cc684f83SJilles Tjoelker if (ferror(hfile))
246cc684f83SJilles Tjoelker error("Can't write to nodes.h");
247cc684f83SJilles Tjoelker if (fclose(hfile))
248cc684f83SJilles Tjoelker error("Can't close nodes.h");
2494b88c807SRodney W. Grimes
2504b88c807SRodney W. Grimes fputs(writer, cfile);
2514b88c807SRodney W. Grimes while (fgets(line, sizeof line, patfile) != NULL) {
2524b88c807SRodney W. Grimes for (p = line ; *p == ' ' || *p == '\t' ; p++);
253aa9caaf6SPeter Wemm if (strcmp(p, "%SIZES\n") == 0)
2544b88c807SRodney W. Grimes outsizes(cfile);
255aa9caaf6SPeter Wemm else if (strcmp(p, "%CALCSIZE\n") == 0)
2564b88c807SRodney W. Grimes outfunc(cfile, 1);
257aa9caaf6SPeter Wemm else if (strcmp(p, "%COPY\n") == 0)
2584b88c807SRodney W. Grimes outfunc(cfile, 0);
2594b88c807SRodney W. Grimes else
2604b88c807SRodney W. Grimes fputs(line, cfile);
2614b88c807SRodney W. Grimes }
262cc684f83SJilles Tjoelker fclose(patfile);
263cc684f83SJilles Tjoelker if (ferror(cfile))
264cc684f83SJilles Tjoelker error("Can't write to nodes.c");
265cc684f83SJilles Tjoelker if (fclose(cfile))
266cc684f83SJilles Tjoelker error("Can't close nodes.c");
2674b88c807SRodney W. Grimes }
2684b88c807SRodney W. Grimes
2694b88c807SRodney W. Grimes
2704b88c807SRodney W. Grimes
271aa9caaf6SPeter Wemm static void
outsizes(FILE * cfile)2725134c3f7SWarner Losh outsizes(FILE *cfile)
2734b88c807SRodney W. Grimes {
2744b88c807SRodney W. Grimes int i;
2754b88c807SRodney W. Grimes
2764b88c807SRodney W. Grimes fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
2774b88c807SRodney W. Grimes for (i = 0 ; i < ntypes ; i++) {
2784b88c807SRodney W. Grimes fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
2794b88c807SRodney W. Grimes }
2804b88c807SRodney W. Grimes fprintf(cfile, "};\n");
2814b88c807SRodney W. Grimes }
2824b88c807SRodney W. Grimes
2834b88c807SRodney W. Grimes
284aa9caaf6SPeter Wemm static void
outfunc(FILE * cfile,int calcsize)2855134c3f7SWarner Losh outfunc(FILE *cfile, int calcsize)
2864b88c807SRodney W. Grimes {
2874b88c807SRodney W. Grimes struct str *sp;
2884b88c807SRodney W. Grimes struct field *fp;
2894b88c807SRodney W. Grimes int i;
2904b88c807SRodney W. Grimes
2914b88c807SRodney W. Grimes fputs(" if (n == NULL)\n", cfile);
2924b88c807SRodney W. Grimes if (calcsize)
2934b88c807SRodney W. Grimes fputs(" return;\n", cfile);
2944b88c807SRodney W. Grimes else
2954b88c807SRodney W. Grimes fputs(" return NULL;\n", cfile);
2964b88c807SRodney W. Grimes if (calcsize)
297a83f6e1aSJilles Tjoelker fputs(" result->blocksize += nodesize[n->type];\n", cfile);
2984b88c807SRodney W. Grimes else {
299a83f6e1aSJilles Tjoelker fputs(" new = state->block;\n", cfile);
300a83f6e1aSJilles Tjoelker fputs(" state->block = (char *)state->block + nodesize[n->type];\n", cfile);
3014b88c807SRodney W. Grimes }
3024b88c807SRodney W. Grimes fputs(" switch (n->type) {\n", cfile);
3034b88c807SRodney W. Grimes for (sp = str ; sp < &str[nstr] ; sp++) {
3044b88c807SRodney W. Grimes for (i = 0 ; i < ntypes ; i++) {
3054b88c807SRodney W. Grimes if (nodestr[i] == sp)
3064b88c807SRodney W. Grimes fprintf(cfile, " case %s:\n", nodename[i]);
3074b88c807SRodney W. Grimes }
3084b88c807SRodney W. Grimes for (i = sp->nfields ; --i >= 1 ; ) {
3094b88c807SRodney W. Grimes fp = &sp->field[i];
3104b88c807SRodney W. Grimes switch (fp->type) {
3114b88c807SRodney W. Grimes case T_NODE:
3124b88c807SRodney W. Grimes if (calcsize) {
3134b88c807SRodney W. Grimes indent(12, cfile);
314a83f6e1aSJilles Tjoelker fprintf(cfile, "calcsize(n->%s.%s, result);\n",
3154b88c807SRodney W. Grimes sp->tag, fp->name);
3164b88c807SRodney W. Grimes } else {
3174b88c807SRodney W. Grimes indent(12, cfile);
318a83f6e1aSJilles Tjoelker fprintf(cfile, "new->%s.%s = copynode(n->%s.%s, state);\n",
3194b88c807SRodney W. Grimes sp->tag, fp->name, sp->tag, fp->name);
3204b88c807SRodney W. Grimes }
3214b88c807SRodney W. Grimes break;
3224b88c807SRodney W. Grimes case T_NODELIST:
3234b88c807SRodney W. Grimes if (calcsize) {
3244b88c807SRodney W. Grimes indent(12, cfile);
325a83f6e1aSJilles Tjoelker fprintf(cfile, "sizenodelist(n->%s.%s, result);\n",
3264b88c807SRodney W. Grimes sp->tag, fp->name);
3274b88c807SRodney W. Grimes } else {
3284b88c807SRodney W. Grimes indent(12, cfile);
329a83f6e1aSJilles Tjoelker fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s, state);\n",
3304b88c807SRodney W. Grimes sp->tag, fp->name, sp->tag, fp->name);
3314b88c807SRodney W. Grimes }
3324b88c807SRodney W. Grimes break;
3334b88c807SRodney W. Grimes case T_STRING:
3344b88c807SRodney W. Grimes if (calcsize) {
3354b88c807SRodney W. Grimes indent(12, cfile);
336a83f6e1aSJilles Tjoelker fprintf(cfile, "result->stringsize += strlen(n->%s.%s) + 1;\n",
3374b88c807SRodney W. Grimes sp->tag, fp->name);
3384b88c807SRodney W. Grimes } else {
3394b88c807SRodney W. Grimes indent(12, cfile);
340a83f6e1aSJilles Tjoelker fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s, state);\n",
3414b88c807SRodney W. Grimes sp->tag, fp->name, sp->tag, fp->name);
3424b88c807SRodney W. Grimes }
3434b88c807SRodney W. Grimes break;
3444b88c807SRodney W. Grimes case T_INT:
3454b88c807SRodney W. Grimes case T_OTHER:
3464b88c807SRodney W. Grimes if (! calcsize) {
3474b88c807SRodney W. Grimes indent(12, cfile);
3484b88c807SRodney W. Grimes fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
3494b88c807SRodney W. Grimes sp->tag, fp->name, sp->tag, fp->name);
3504b88c807SRodney W. Grimes }
3514b88c807SRodney W. Grimes break;
3524b88c807SRodney W. Grimes }
3534b88c807SRodney W. Grimes }
3544b88c807SRodney W. Grimes indent(12, cfile);
3554b88c807SRodney W. Grimes fputs("break;\n", cfile);
3564b88c807SRodney W. Grimes }
3574b88c807SRodney W. Grimes fputs(" };\n", cfile);
3584b88c807SRodney W. Grimes if (! calcsize)
3594b88c807SRodney W. Grimes fputs(" new->type = n->type;\n", cfile);
3604b88c807SRodney W. Grimes }
3614b88c807SRodney W. Grimes
3624b88c807SRodney W. Grimes
363aa9caaf6SPeter Wemm static void
indent(int amount,FILE * fp)3645134c3f7SWarner Losh indent(int amount, FILE *fp)
3654b88c807SRodney W. Grimes {
3664b88c807SRodney W. Grimes while (amount >= 8) {
3674b88c807SRodney W. Grimes putc('\t', fp);
3684b88c807SRodney W. Grimes amount -= 8;
3694b88c807SRodney W. Grimes }
3704b88c807SRodney W. Grimes while (--amount >= 0) {
3714b88c807SRodney W. Grimes putc(' ', fp);
3724b88c807SRodney W. Grimes }
3734b88c807SRodney W. Grimes }
3744b88c807SRodney W. Grimes
3754b88c807SRodney W. Grimes
376aa9caaf6SPeter Wemm static int
nextfield(char * buf)3775134c3f7SWarner Losh nextfield(char *buf)
3784b88c807SRodney W. Grimes {
3797e461ef4SSteve Price char *p, *q;
3804b88c807SRodney W. Grimes
3814b88c807SRodney W. Grimes p = linep;
3824b88c807SRodney W. Grimes while (*p == ' ' || *p == '\t')
3834b88c807SRodney W. Grimes p++;
3844b88c807SRodney W. Grimes q = buf;
3854b88c807SRodney W. Grimes while (*p != ' ' && *p != '\t' && *p != '\0')
3864b88c807SRodney W. Grimes *q++ = *p++;
3874b88c807SRodney W. Grimes *q = '\0';
3884b88c807SRodney W. Grimes linep = p;
3894b88c807SRodney W. Grimes return (q > buf);
3904b88c807SRodney W. Grimes }
3914b88c807SRodney W. Grimes
3924b88c807SRodney W. Grimes
393aa9caaf6SPeter Wemm static void
skipbl(void)3945134c3f7SWarner Losh skipbl(void)
395aa9caaf6SPeter Wemm {
3964b88c807SRodney W. Grimes while (*linep == ' ' || *linep == '\t')
3974b88c807SRodney W. Grimes linep++;
3984b88c807SRodney W. Grimes }
3994b88c807SRodney W. Grimes
4004b88c807SRodney W. Grimes
401aa9caaf6SPeter Wemm static int
readline(FILE * infp)402cc684f83SJilles Tjoelker readline(FILE *infp)
403aa9caaf6SPeter Wemm {
4047e461ef4SSteve Price char *p;
4054b88c807SRodney W. Grimes
4064b88c807SRodney W. Grimes if (fgets(line, 1024, infp) == NULL)
4074b88c807SRodney W. Grimes return 0;
4084b88c807SRodney W. Grimes for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
4094b88c807SRodney W. Grimes while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
4104b88c807SRodney W. Grimes p--;
4114b88c807SRodney W. Grimes *p = '\0';
4124b88c807SRodney W. Grimes linep = line;
4134b88c807SRodney W. Grimes linno++;
4144b88c807SRodney W. Grimes if (p - line > BUFLEN)
4154b88c807SRodney W. Grimes error("Line too long");
4164b88c807SRodney W. Grimes return 1;
4174b88c807SRodney W. Grimes }
4184b88c807SRodney W. Grimes
4194b88c807SRodney W. Grimes
4204b88c807SRodney W. Grimes
421aa9caaf6SPeter Wemm static void
error(const char * msg,...)422aa9caaf6SPeter Wemm error(const char *msg, ...)
4234b88c807SRodney W. Grimes {
424aa9caaf6SPeter Wemm va_list va;
425aa9caaf6SPeter Wemm va_start(va, msg);
426aa9caaf6SPeter Wemm
427aa9caaf6SPeter Wemm (void) fprintf(stderr, "line %d: ", linno);
428aa9caaf6SPeter Wemm (void) vfprintf(stderr, msg, va);
429aa9caaf6SPeter Wemm (void) fputc('\n', stderr);
430aa9caaf6SPeter Wemm
431aa9caaf6SPeter Wemm va_end(va);
432aa9caaf6SPeter Wemm
4334b88c807SRodney W. Grimes exit(2);
4344b88c807SRodney W. Grimes }
4354b88c807SRodney W. Grimes
4364b88c807SRodney W. Grimes
4374b88c807SRodney W. Grimes
438aa9caaf6SPeter Wemm static char *
savestr(const char * s)4395134c3f7SWarner Losh savestr(const char *s)
4404b88c807SRodney W. Grimes {
4417e461ef4SSteve Price char *p;
4424b88c807SRodney W. Grimes
4434b88c807SRodney W. Grimes if ((p = malloc(strlen(s) + 1)) == NULL)
4444b88c807SRodney W. Grimes error("Out of space");
445aa9caaf6SPeter Wemm (void) strcpy(p, s);
4464b88c807SRodney W. Grimes return p;
4474b88c807SRodney W. Grimes }
448