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
38 static char const copyright[] =
39 "@(#) Copyright (c) 1991, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)mknodes.c 8.2 (Berkeley) 5/4/95";
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD: src/bin/sh/mknodes.c,v 1.11.2.3 2002/07/19 04:38:51 tjr Exp $");
52 * This program reads the nodetypes file and nodes.c.pat file. It generates
53 * the files nodes.h and nodes.c.
62 #define MAXTYPES 50 /* max number of node types */
63 #define MAXFIELDS 20 /* max fields in a structure */
64 #define BUFLEN 100 /* size of character buffers */
67 #define T_NODE 1 /* union node *field */
68 #define T_NODELIST 2 /* struct nodelist *field */
70 #define T_INT 4 /* int field */
71 #define T_OTHER 5 /* other */
72 #define T_TEMP 6 /* don't copy this field */
75 struct field { /* a structure field */
76 char *name; /* name of field */
77 int type; /* type of field */
78 char *decl; /* declaration of field */
82 struct str { /* struct representing a node structure */
83 char *tag; /* structure tag */
84 int nfields; /* number of fields in the structure */
85 struct field field[MAXFIELDS]; /* the fields of the structure */
86 int done; /* set if fully parsed */
90 static int ntypes; /* number of node types */
91 static char *nodename[MAXTYPES]; /* names of the nodes */
92 static struct str *nodestr[MAXTYPES]; /* type of structure used by the node */
93 static int nstr; /* number of structures */
94 static struct str str[MAXTYPES]; /* the structures */
95 static struct str *curstr; /* current structure */
97 static char line[1024];
101 static void parsenode(void);
102 static void parsefield(void);
103 static void output(char *);
104 static void outsizes(FILE *);
105 static void outfunc(FILE *, int);
106 static void indent(int, FILE *);
107 static int nextfield(char *);
108 static void skipbl(void);
109 static int readline(void);
110 static void error(const char *, ...) __printf0like(1, 2);
111 static char *savestr(const char *);
115 main(int argc, char *argv[])
118 error("usage: mknodes file");
120 if ((infp = fopen(argv[1], "r")) == NULL)
121 error("Can't open %s: %s", argv[1], strerror(errno));
123 if (line[0] == ' ' || line[0] == '\t')
125 else if (line[0] != '\0')
141 if (curstr && curstr->nfields > 0)
144 if (! nextfield(tag))
145 error("Tag expected");
147 error("Garbage at end of line");
148 nodename[ntypes] = savestr(name);
149 for (sp = str ; sp < str + nstr ; sp++) {
150 if (strcmp(sp->tag, tag) == 0)
153 if (sp >= str + nstr) {
154 sp->tag = savestr(tag);
159 nodestr[ntypes] = sp;
169 char decl[2 * BUFLEN];
172 if (curstr == NULL || curstr->done)
173 error("No current structure to add field to");
174 if (! nextfield(name))
175 error("No field name");
176 if (! nextfield(type))
177 error("No field type");
178 fp = &curstr->field[curstr->nfields];
179 fp->name = savestr(name);
180 if (strcmp(type, "nodeptr") == 0) {
182 sprintf(decl, "union node *%s", name);
183 } else if (strcmp(type, "nodelist") == 0) {
184 fp->type = T_NODELIST;
185 sprintf(decl, "struct nodelist *%s", name);
186 } else if (strcmp(type, "string") == 0) {
188 sprintf(decl, "char *%s", name);
189 } else if (strcmp(type, "int") == 0) {
191 sprintf(decl, "int %s", name);
192 } else if (strcmp(type, "other") == 0) {
194 } else if (strcmp(type, "temp") == 0) {
197 error("Unknown type %s", type);
199 if (fp->type == T_OTHER || fp->type == T_TEMP) {
201 fp->decl = savestr(linep);
204 error("Garbage at end of line");
205 fp->decl = savestr(decl);
213 * This file was generated by the mknodes program.\n\
228 if ((patfile = fopen(file, "r")) == NULL)
229 error("Can't open %s: %s", file, strerror(errno));
230 if ((hfile = fopen("nodes.h", "w")) == NULL)
231 error("Can't create nodes.h: %s", strerror(errno));
232 if ((cfile = fopen("nodes.c", "w")) == NULL)
233 error("Can't create nodes.c");
234 fputs(writer, hfile);
235 for (i = 0 ; i < ntypes ; i++)
236 fprintf(hfile, "#define %s %d\n", nodename[i], i);
237 fputs("\n\n\n", hfile);
238 for (sp = str ; sp < &str[nstr] ; sp++) {
239 fprintf(hfile, "struct %s {\n", sp->tag);
240 for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
241 fprintf(hfile, " %s;\n", fp->decl);
243 fputs("};\n\n\n", hfile);
245 fputs("union node {\n", hfile);
246 fprintf(hfile, " int type;\n");
247 for (sp = str ; sp < &str[nstr] ; sp++) {
248 fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag);
250 fputs("};\n\n\n", hfile);
251 fputs("struct nodelist {\n", hfile);
252 fputs("\tstruct nodelist *next;\n", hfile);
253 fputs("\tunion node *n;\n", hfile);
254 fputs("};\n\n\n", hfile);
255 fputs("union node *copyfunc(union node *);\n", hfile);
256 fputs("void freefunc(union node *);\n", hfile);
258 fputs(writer, cfile);
259 while (fgets(line, sizeof line, patfile) != NULL) {
260 for (p = line ; *p == ' ' || *p == '\t' ; p++);
261 if (strcmp(p, "%SIZES\n") == 0)
263 else if (strcmp(p, "%CALCSIZE\n") == 0)
265 else if (strcmp(p, "%COPY\n") == 0)
275 outsizes(FILE *cfile)
279 fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
280 for (i = 0 ; i < ntypes ; i++) {
281 fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
283 fprintf(cfile, "};\n");
288 outfunc(FILE *cfile, int calcsize)
294 fputs(" if (n == NULL)\n", cfile);
296 fputs(" return;\n", cfile);
298 fputs(" return NULL;\n", cfile);
300 fputs(" funcblocksize += nodesize[n->type];\n", cfile);
302 fputs(" new = funcblock;\n", cfile);
303 fputs(" funcblock = (char *)funcblock + nodesize[n->type];\n", cfile);
305 fputs(" switch (n->type) {\n", cfile);
306 for (sp = str ; sp < &str[nstr] ; sp++) {
307 for (i = 0 ; i < ntypes ; i++) {
308 if (nodestr[i] == sp)
309 fprintf(cfile, " case %s:\n", nodename[i]);
311 for (i = sp->nfields ; --i >= 1 ; ) {
317 fprintf(cfile, "calcsize(n->%s.%s);\n",
321 fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
322 sp->tag, fp->name, sp->tag, fp->name);
328 fprintf(cfile, "sizenodelist(n->%s.%s);\n",
332 fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
333 sp->tag, fp->name, sp->tag, fp->name);
339 fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
343 fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
344 sp->tag, fp->name, sp->tag, fp->name);
351 fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
352 sp->tag, fp->name, sp->tag, fp->name);
358 fputs("break;\n", cfile);
360 fputs(" };\n", cfile);
362 fputs(" new->type = n->type;\n", cfile);
367 indent(int amount, FILE *fp)
369 while (amount >= 8) {
373 while (--amount >= 0) {
385 while (*p == ' ' || *p == '\t')
388 while (*p != ' ' && *p != '\t' && *p != '\0')
399 while (*linep == ' ' || *linep == '\t')
409 if (fgets(line, 1024, infp) == NULL)
411 for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
412 while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
417 if (p - line > BUFLEN)
418 error("Line too long");
425 error(const char *msg, ...)
430 (void) fprintf(stderr, "line %d: ", linno);
431 (void) vfprintf(stderr, msg, va);
432 (void) fputc('\n', stderr);
442 savestr(const char *s)
446 if ((p = malloc(strlen(s) + 1)) == NULL)
447 error("Out of space");