2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1997-2005
5 * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This program reads the nodetypes file and nodes.c.pat file. It generates
37 * the files nodes.h and nodes.c.
45 #define MAXTYPES 50 /* max number of node types */
46 #define MAXFIELDS 20 /* max fields in a structure */
47 #define BUFLEN 100 /* size of character buffers */
50 #define T_NODE 1 /* union node *field */
51 #define T_NODELIST 2 /* struct nodelist *field */
53 #define T_INT 4 /* int field */
54 #define T_OTHER 5 /* other */
55 #define T_TEMP 6 /* don't copy this field */
58 struct field { /* a structure field */
59 char *name; /* name of field */
60 int type; /* type of field */
61 char *decl; /* declaration of field */
65 struct str { /* struct representing a node structure */
66 char *tag; /* structure tag */
67 int nfields; /* number of fields in the structure */
68 struct field field[MAXFIELDS]; /* the fields of the structure */
69 int done; /* set if fully parsed */
73 static int ntypes; /* number of node types */
74 static char *nodename[MAXTYPES]; /* names of the nodes */
75 static struct str *nodestr[MAXTYPES]; /* type of structure used by the node */
76 static int nstr; /* number of structures */
77 static struct str str[MAXTYPES]; /* the structures */
78 static struct str *curstr; /* current structure */
80 static char line[1024];
84 static void parsenode(void);
85 static void parsefield(void);
86 static void output(char *);
87 static void outsizes(FILE *);
88 static void outfunc(FILE *, int);
89 static void indent(int, FILE *);
90 static int nextfield(char *);
91 static void skipbl(void);
92 static int readline(void);
93 static void error(const char *, ...);
94 static char *savestr(const char *);
95 int main(int, char **);
99 main(int argc, char **argv)
103 * some versions of linux complain: initializer element is not
104 * constant if this is done at compile time.
109 error("usage: mknodes file");
110 if ((infp = fopen(argv[1], "r")) == NULL)
111 error("Can't open %s", argv[1]);
113 if (line[0] == ' ' || line[0] == '\t')
115 else if (line[0] != '\0')
132 if (curstr && curstr->nfields > 0)
135 if (! nextfield(tag))
136 error("Tag expected");
138 error("Garbage at end of line");
139 nodename[ntypes] = savestr(name);
140 for (sp = str ; sp < str + nstr ; sp++) {
141 if (strcmp(sp->tag, tag) == 0)
144 if (sp >= str + nstr) {
145 sp->tag = savestr(tag);
150 nodestr[ntypes] = sp;
160 char decl[2 * BUFLEN];
163 if (curstr == NULL || curstr->done)
164 error("No current structure to add field to");
165 if (! nextfield(name))
166 error("No field name");
167 if (! nextfield(type))
168 error("No field type");
169 fp = &curstr->field[curstr->nfields];
170 fp->name = savestr(name);
171 if (strcmp(type, "nodeptr") == 0) {
173 sprintf(decl, "union node *%s", name);
174 } else if (strcmp(type, "nodelist") == 0) {
175 fp->type = T_NODELIST;
176 sprintf(decl, "struct nodelist *%s", name);
177 } else if (strcmp(type, "string") == 0) {
179 sprintf(decl, "char *%s", name);
180 } else if (strcmp(type, "int") == 0) {
182 sprintf(decl, "int %s", name);
183 } else if (strcmp(type, "other") == 0) {
185 } else if (strcmp(type, "temp") == 0) {
188 error("Unknown type %s", type);
190 if (fp->type == T_OTHER || fp->type == T_TEMP) {
192 fp->decl = savestr(linep);
195 error("Garbage at end of line");
196 fp->decl = savestr(decl);
204 * This file was generated by the mknodes program.\n\
219 if ((patfile = fopen(file, "r")) == NULL)
220 error("Can't open %s", file);
221 if ((hfile = fopen("nodes.h", "w")) == NULL)
222 error("Can't create nodes.h");
223 if ((cfile = fopen("nodes.c", "w")) == NULL)
224 error("Can't create nodes.c");
225 fputs(writer, hfile);
226 for (i = 0 ; i < ntypes ; i++)
227 fprintf(hfile, "#define %s %d\n", nodename[i], i);
228 fputs("\n\n\n", hfile);
229 for (sp = str ; sp < &str[nstr] ; sp++) {
230 fprintf(hfile, "struct %s {\n", sp->tag);
231 for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
232 fprintf(hfile, " %s;\n", fp->decl);
234 fputs("};\n\n\n", hfile);
236 fputs("union node {\n", hfile);
237 fprintf(hfile, " int type;\n");
238 for (sp = str ; sp < &str[nstr] ; sp++) {
239 fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag);
241 fputs("};\n\n\n", hfile);
242 fputs("struct nodelist {\n", hfile);
243 fputs("\tstruct nodelist *next;\n", hfile);
244 fputs("\tunion node *n;\n", hfile);
245 fputs("};\n\n\n", hfile);
246 fputs("struct funcnode {\n", hfile);
247 fputs("\tint count;\n", hfile);
248 fputs("\tunion node n;\n", hfile);
249 fputs("};\n\n\n", hfile);
250 fputs("struct funcnode *copyfunc(union node *);\n", hfile);
251 fputs("void freefunc(struct funcnode *);\n", hfile);
253 fputs(writer, cfile);
254 while (fgets(line, sizeof line, patfile) != NULL) {
255 for (p = line ; *p == ' ' || *p == '\t' ; p++);
256 if (strcmp(p, "%SIZES\n") == 0)
258 else if (strcmp(p, "%CALCSIZE\n") == 0)
260 else if (strcmp(p, "%COPY\n") == 0)
270 outsizes(FILE *cfile)
274 fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
275 for (i = 0 ; i < ntypes ; i++) {
276 fprintf(cfile, " SHELL_ALIGN(sizeof (struct %s)),\n",
279 fprintf(cfile, "};\n");
284 outfunc(FILE *cfile, int calcsize)
290 fputs(" if (n == NULL)\n", cfile);
292 fputs(" return;\n", cfile);
294 fputs(" return NULL;\n", cfile);
296 fputs(" funcblocksize += nodesize[n->type];\n", cfile);
298 fputs(" new = funcblock;\n", cfile);
299 fputs(" funcblock = (char *) funcblock + nodesize[n->type];\n", cfile);
301 fputs(" switch (n->type) {\n", cfile);
302 for (sp = str ; sp < &str[nstr] ; sp++) {
303 for (i = 0 ; i < ntypes ; i++) {
304 if (nodestr[i] == sp)
305 fprintf(cfile, " case %s:\n", nodename[i]);
307 for (i = sp->nfields ; --i >= 1 ; ) {
313 fprintf(cfile, "calcsize(n->%s.%s);\n",
317 fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
318 sp->tag, fp->name, sp->tag, fp->name);
324 fprintf(cfile, "sizenodelist(n->%s.%s);\n",
328 fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
329 sp->tag, fp->name, sp->tag, fp->name);
335 fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
339 fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
340 sp->tag, fp->name, sp->tag, fp->name);
347 fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
348 sp->tag, fp->name, sp->tag, fp->name);
354 fputs("break;\n", cfile);
356 fputs(" };\n", cfile);
358 fputs(" new->type = n->type;\n", cfile);
363 indent(int amount, FILE *fp)
365 while (amount >= 8) {
369 while (--amount >= 0) {
381 while (*p == ' ' || *p == '\t')
384 while (*p != ' ' && *p != '\t' && *p != '\0')
395 while (*linep == ' ' || *linep == '\t')
405 if (fgets(line, 1024, infp) == NULL)
407 for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
408 while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
413 if (p - line > BUFLEN)
414 error("Line too long");
421 error(const char *msg, ...)
427 (void) fprintf(stderr, "line %d: ", linno);
428 (void) vfprintf(stderr, msg, va);
429 (void) fputc('\n', stderr);
440 savestr(const char *s)
444 if ((p = malloc(strlen(s) + 1)) == NULL)
445 error("Out of space");