2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char const copyright[] =
36 "@(#) Copyright (c) 1991, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)mknodes.c 8.2 (Berkeley) 5/4/95";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 * This program reads the nodetypes file and nodes.c.pat file. It generates
49 * the files nodes.h and nodes.c.
58 #define MAXTYPES 50 /* max number of node types */
59 #define MAXFIELDS 20 /* max fields in a structure */
60 #define BUFLEN 100 /* size of character buffers */
63 #define T_NODE 1 /* union node *field */
64 #define T_NODELIST 2 /* struct nodelist *field */
66 #define T_INT 4 /* int field */
67 #define T_OTHER 5 /* other */
68 #define T_TEMP 6 /* don't copy this field */
71 struct field { /* a structure field */
72 char *name; /* name of field */
73 int type; /* type of field */
74 char *decl; /* declaration of field */
78 struct str { /* struct representing a node structure */
79 char *tag; /* structure tag */
80 int nfields; /* number of fields in the structure */
81 struct field field[MAXFIELDS]; /* the fields of the structure */
82 int done; /* set if fully parsed */
86 static int ntypes; /* number of node types */
87 static char *nodename[MAXTYPES]; /* names of the nodes */
88 static struct str *nodestr[MAXTYPES]; /* type of structure used by the node */
89 static int nstr; /* number of structures */
90 static struct str str[MAXTYPES]; /* the structures */
91 static struct str *curstr; /* current structure */
93 static char line[1024];
97 static void parsenode(void);
98 static void parsefield(void);
99 static void output(char *);
100 static void outsizes(FILE *);
101 static void outfunc(FILE *, int);
102 static void indent(int, FILE *);
103 static int nextfield(char *);
104 static void skipbl(void);
105 static int readline(void);
106 static void error(const char *, ...) __printf0like(1, 2) __dead2;
107 static char *savestr(const char *);
111 main(int argc, char *argv[])
114 error("usage: mknodes file");
116 if ((infp = fopen(argv[1], "r")) == NULL)
117 error("Can't open %s: %s", argv[1], strerror(errno));
119 if (line[0] == ' ' || line[0] == '\t')
121 else if (line[0] != '\0')
137 if (curstr && curstr->nfields > 0)
140 if (! nextfield(tag))
141 error("Tag expected");
143 error("Garbage at end of line");
144 nodename[ntypes] = savestr(name);
145 for (sp = str ; sp < str + nstr ; sp++) {
146 if (strcmp(sp->tag, tag) == 0)
149 if (sp >= str + nstr) {
150 sp->tag = savestr(tag);
155 nodestr[ntypes] = sp;
165 char decl[2 * BUFLEN];
168 if (curstr == NULL || curstr->done)
169 error("No current structure to add field to");
170 if (! nextfield(name))
171 error("No field name");
172 if (! nextfield(type))
173 error("No field type");
174 fp = &curstr->field[curstr->nfields];
175 fp->name = savestr(name);
176 if (strcmp(type, "nodeptr") == 0) {
178 sprintf(decl, "union node *%s", name);
179 } else if (strcmp(type, "nodelist") == 0) {
180 fp->type = T_NODELIST;
181 sprintf(decl, "struct nodelist *%s", name);
182 } else if (strcmp(type, "string") == 0) {
184 sprintf(decl, "char *%s", name);
185 } else if (strcmp(type, "int") == 0) {
187 sprintf(decl, "int %s", name);
188 } else if (strcmp(type, "other") == 0) {
190 } else if (strcmp(type, "temp") == 0) {
193 error("Unknown type %s", type);
195 if (fp->type == T_OTHER || fp->type == T_TEMP) {
197 fp->decl = savestr(linep);
200 error("Garbage at end of line");
201 fp->decl = savestr(decl);
207 static const char writer[] = "\
209 * This file was generated by the mknodes program.\n\
224 if ((patfile = fopen(file, "r")) == NULL)
225 error("Can't open %s: %s", file, strerror(errno));
226 if ((hfile = fopen("nodes.h", "w")) == NULL)
227 error("Can't create nodes.h: %s", strerror(errno));
228 if ((cfile = fopen("nodes.c", "w")) == NULL)
229 error("Can't create nodes.c");
230 fputs(writer, hfile);
231 for (i = 0 ; i < ntypes ; i++)
232 fprintf(hfile, "#define %s %d\n", nodename[i], i);
233 fputs("\n\n\n", hfile);
234 for (sp = str ; sp < &str[nstr] ; sp++) {
235 fprintf(hfile, "struct %s {\n", sp->tag);
236 for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
237 fprintf(hfile, " %s;\n", fp->decl);
239 fputs("};\n\n\n", hfile);
241 fputs("union node {\n", hfile);
242 fprintf(hfile, " int type;\n");
243 for (sp = str ; sp < &str[nstr] ; sp++) {
244 fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag);
246 fputs("};\n\n\n", hfile);
247 fputs("struct nodelist {\n", hfile);
248 fputs("\tstruct nodelist *next;\n", hfile);
249 fputs("\tunion node *n;\n", hfile);
250 fputs("};\n\n\n", hfile);
251 fputs("struct funcdef;\n", hfile);
252 fputs("struct funcdef *copyfunc(union node *);\n", hfile);
253 fputs("union node *getfuncnode(struct funcdef *);\n", hfile);
254 fputs("void reffunc(struct funcdef *);\n", hfile);
255 fputs("void unreffunc(struct funcdef *);\n", hfile);
257 fputs(writer, cfile);
258 while (fgets(line, sizeof line, patfile) != NULL) {
259 for (p = line ; *p == ' ' || *p == '\t' ; p++);
260 if (strcmp(p, "%SIZES\n") == 0)
262 else if (strcmp(p, "%CALCSIZE\n") == 0)
264 else if (strcmp(p, "%COPY\n") == 0)
274 outsizes(FILE *cfile)
278 fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
279 for (i = 0 ; i < ntypes ; i++) {
280 fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
282 fprintf(cfile, "};\n");
287 outfunc(FILE *cfile, int calcsize)
293 fputs(" if (n == NULL)\n", cfile);
295 fputs(" return;\n", cfile);
297 fputs(" return NULL;\n", cfile);
299 fputs(" funcblocksize += nodesize[n->type];\n", cfile);
301 fputs(" new = funcblock;\n", cfile);
302 fputs(" funcblock = (char *)funcblock + nodesize[n->type];\n", cfile);
304 fputs(" switch (n->type) {\n", cfile);
305 for (sp = str ; sp < &str[nstr] ; sp++) {
306 for (i = 0 ; i < ntypes ; i++) {
307 if (nodestr[i] == sp)
308 fprintf(cfile, " case %s:\n", nodename[i]);
310 for (i = sp->nfields ; --i >= 1 ; ) {
316 fprintf(cfile, "calcsize(n->%s.%s);\n",
320 fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
321 sp->tag, fp->name, sp->tag, fp->name);
327 fprintf(cfile, "sizenodelist(n->%s.%s);\n",
331 fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
332 sp->tag, fp->name, sp->tag, fp->name);
338 fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
342 fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
343 sp->tag, fp->name, sp->tag, fp->name);
350 fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
351 sp->tag, fp->name, sp->tag, fp->name);
357 fputs("break;\n", cfile);
359 fputs(" };\n", cfile);
361 fputs(" new->type = n->type;\n", cfile);
366 indent(int amount, FILE *fp)
368 while (amount >= 8) {
372 while (--amount >= 0) {
384 while (*p == ' ' || *p == '\t')
387 while (*p != ' ' && *p != '\t' && *p != '\0')
398 while (*linep == ' ' || *linep == '\t')
408 if (fgets(line, 1024, infp) == NULL)
410 for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
411 while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
416 if (p - line > BUFLEN)
417 error("Line too long");
424 error(const char *msg, ...)
429 (void) fprintf(stderr, "line %d: ", linno);
430 (void) vfprintf(stderr, msg, va);
431 (void) fputc('\n', stderr);
441 savestr(const char *s)
445 if ((p = malloc(strlen(s) + 1)) == NULL)
446 error("Out of space");