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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)mknodes.c 8.2 (Berkeley) 5/4/95
38 * $FreeBSD: src/bin/sh/mknodes.c,v 1.20 2009/08/28 22:41:25 jilles Exp $
42 * This program reads the nodetypes file and nodes.c.pat file. It generates
43 * the files nodes.h and nodes.c.
52 #define MAXTYPES 50 /* max number of node types */
53 #define MAXFIELDS 20 /* max fields in a structure */
54 #define BUFLEN 100 /* size of character buffers */
57 #define T_NODE 1 /* union node *field */
58 #define T_NODELIST 2 /* struct nodelist *field */
60 #define T_INT 4 /* int field */
61 #define T_OTHER 5 /* other */
62 #define T_TEMP 6 /* don't copy this field */
65 struct field { /* a structure field */
66 char *name; /* name of field */
67 int type; /* type of field */
68 char *decl; /* declaration of field */
72 struct str { /* struct representing a node structure */
73 char *tag; /* structure tag */
74 int nfields; /* number of fields in the structure */
75 struct field field[MAXFIELDS]; /* the fields of the structure */
76 int done; /* set if fully parsed */
80 static int ntypes; /* number of node types */
81 static char *nodename[MAXTYPES]; /* names of the nodes */
82 static struct str *nodestr[MAXTYPES]; /* type of structure used by the node */
83 static int nstr; /* number of structures */
84 static struct str str[MAXTYPES]; /* the structures */
85 static struct str *curstr; /* current structure */
87 static char line[1024];
91 static void parsenode(void);
92 static void parsefield(void);
93 static void output(char *);
94 static void outsizes(FILE *);
95 static void outfunc(FILE *, int);
96 static void indent(int, FILE *);
97 static int nextfield(char *);
98 static void skipbl(void);
99 static int readline(void);
100 static void error(const char *, ...) __printf0like(1, 2) __dead2;
101 static char *savestr(const char *);
105 main(int argc, char *argv[])
108 error("usage: mknodes file");
110 if ((infp = fopen(argv[1], "r")) == NULL)
111 error("Can't open %s: %s", argv[1], strerror(errno));
113 if (line[0] == ' ' || line[0] == '\t')
115 else if (line[0] != '\0')
131 if (curstr && curstr->nfields > 0)
134 if (! nextfield(tag))
135 error("Tag expected");
137 error("Garbage at end of line");
138 nodename[ntypes] = savestr(name);
139 for (sp = str ; sp < str + nstr ; sp++) {
140 if (strcmp(sp->tag, tag) == 0)
143 if (sp >= str + nstr) {
144 sp->tag = savestr(tag);
149 nodestr[ntypes] = sp;
159 char decl[2 * BUFLEN];
162 if (curstr == NULL || curstr->done)
163 error("No current structure to add field to");
164 if (! nextfield(name))
165 error("No field name");
166 if (! nextfield(type))
167 error("No field type");
168 fp = &curstr->field[curstr->nfields];
169 fp->name = savestr(name);
170 if (strcmp(type, "nodeptr") == 0) {
172 sprintf(decl, "union node *%s", name);
173 } else if (strcmp(type, "nodelist") == 0) {
174 fp->type = T_NODELIST;
175 sprintf(decl, "struct nodelist *%s", name);
176 } else if (strcmp(type, "string") == 0) {
178 sprintf(decl, "char *%s", name);
179 } else if (strcmp(type, "int") == 0) {
181 sprintf(decl, "int %s", name);
182 } else if (strcmp(type, "other") == 0) {
184 } else if (strcmp(type, "temp") == 0) {
187 error("Unknown type %s", type);
189 if (fp->type == T_OTHER || fp->type == T_TEMP) {
191 fp->decl = savestr(linep);
194 error("Garbage at end of line");
195 fp->decl = savestr(decl);
203 * This file was generated by the mknodes program.\n\
218 if ((patfile = fopen(file, "r")) == NULL)
219 error("Can't open %s: %s", file, strerror(errno));
220 if ((hfile = fopen("nodes.h", "w")) == NULL)
221 error("Can't create nodes.h: %s", strerror(errno));
222 if ((cfile = fopen("nodes.c", "w")) == NULL)
223 error("Can't create nodes.c");
224 fputs(writer, hfile);
225 for (i = 0 ; i < ntypes ; i++)
226 fprintf(hfile, "#define %s %d\n", nodename[i], i);
227 fputs("\n\n\n", hfile);
228 for (sp = str ; sp < &str[nstr] ; sp++) {
229 fprintf(hfile, "struct %s {\n", sp->tag);
230 for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
231 fprintf(hfile, " %s;\n", fp->decl);
233 fputs("};\n\n\n", hfile);
235 fputs("union node {\n", hfile);
236 fprintf(hfile, " int type;\n");
237 for (sp = str ; sp < &str[nstr] ; sp++) {
238 fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag);
240 fputs("};\n\n\n", hfile);
241 fputs("struct nodelist {\n", hfile);
242 fputs("\tstruct nodelist *next;\n", hfile);
243 fputs("\tunion node *n;\n", hfile);
244 fputs("};\n\n\n", hfile);
245 fputs("struct funcdef;\n", hfile);
246 fputs("struct funcdef *copyfunc(union node *);\n", hfile);
247 fputs("union node *getfuncnode(struct funcdef *);\n", hfile);
248 fputs("void reffunc(struct funcdef *);\n", hfile);
249 fputs("void unreffunc(struct funcdef *);\n", hfile);
251 fputs(writer, cfile);
252 while (fgets(line, sizeof line, patfile) != NULL) {
253 for (p = line ; *p == ' ' || *p == '\t' ; p++);
254 if (strcmp(p, "%SIZES\n") == 0)
256 else if (strcmp(p, "%CALCSIZE\n") == 0)
258 else if (strcmp(p, "%COPY\n") == 0)
268 outsizes(FILE *cfile)
272 fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
273 for (i = 0 ; i < ntypes ; i++) {
274 fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
276 fprintf(cfile, "};\n");
281 outfunc(FILE *cfile, int calcsize)
287 fputs(" if (n == NULL)\n", cfile);
289 fputs(" return;\n", cfile);
291 fputs(" return NULL;\n", cfile);
293 fputs(" funcblocksize += nodesize[n->type];\n", cfile);
295 fputs(" new = funcblock;\n", cfile);
296 fputs(" funcblock = (char *)funcblock + nodesize[n->type];\n", cfile);
298 fputs(" switch (n->type) {\n", cfile);
299 for (sp = str ; sp < &str[nstr] ; sp++) {
300 for (i = 0 ; i < ntypes ; i++) {
301 if (nodestr[i] == sp)
302 fprintf(cfile, " case %s:\n", nodename[i]);
304 for (i = sp->nfields ; --i >= 1 ; ) {
310 fprintf(cfile, "calcsize(n->%s.%s);\n",
314 fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
315 sp->tag, fp->name, sp->tag, fp->name);
321 fprintf(cfile, "sizenodelist(n->%s.%s);\n",
325 fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
326 sp->tag, fp->name, sp->tag, fp->name);
332 fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
336 fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
337 sp->tag, fp->name, sp->tag, fp->name);
344 fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
345 sp->tag, fp->name, sp->tag, fp->name);
351 fputs("break;\n", cfile);
353 fputs(" };\n", cfile);
355 fputs(" new->type = n->type;\n", cfile);
360 indent(int amount, FILE *fp)
362 while (amount >= 8) {
366 while (--amount >= 0) {
378 while (*p == ' ' || *p == '\t')
381 while (*p != ' ' && *p != '\t' && *p != '\0')
392 while (*linep == ' ' || *linep == '\t')
402 if (fgets(line, 1024, infp) == NULL)
404 for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
405 while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
410 if (p - line > BUFLEN)
411 error("Line too long");
418 error(const char *msg, ...)
423 fprintf(stderr, "line %d: ", linno);
424 vfprintf(stderr, msg, va);
435 savestr(const char *s)
439 if ((p = malloc(strlen(s) + 1)) == NULL)
440 error("Out of space");