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.11.2.3 2002/07/19 04:38:51 tjr Exp $
39 * $DragonFly: src/bin/sh/mknodes.c,v 1.3 2004/11/07 20:54:52 eirikn Exp $
43 * This program reads the nodetypes file and nodes.c.pat file. It generates
44 * the files nodes.h and nodes.c.
53 #define MAXTYPES 50 /* max number of node types */
54 #define MAXFIELDS 20 /* max fields in a structure */
55 #define BUFLEN 100 /* size of character buffers */
58 #define T_NODE 1 /* union node *field */
59 #define T_NODELIST 2 /* struct nodelist *field */
61 #define T_INT 4 /* int field */
62 #define T_OTHER 5 /* other */
63 #define T_TEMP 6 /* don't copy this field */
66 struct field
{ /* a structure field */
67 char *name
; /* name of field */
68 int type
; /* type of field */
69 char *decl
; /* declaration of field */
73 struct str
{ /* struct representing a node structure */
74 char *tag
; /* structure tag */
75 int nfields
; /* number of fields in the structure */
76 struct field field
[MAXFIELDS
]; /* the fields of the structure */
77 int done
; /* set if fully parsed */
81 static int ntypes
; /* number of node types */
82 static char *nodename
[MAXTYPES
]; /* names of the nodes */
83 static struct str
*nodestr
[MAXTYPES
]; /* type of structure used by the node */
84 static int nstr
; /* number of structures */
85 static struct str str
[MAXTYPES
]; /* the structures */
86 static struct str
*curstr
; /* current structure */
88 static char line
[1024];
92 static void parsenode(void);
93 static void parsefield(void);
94 static void output(char *);
95 static void outsizes(FILE *);
96 static void outfunc(FILE *, int);
97 static void indent(int, FILE *);
98 static int nextfield(char *);
99 static void skipbl(void);
100 static int readline(void);
101 static void error(const char *, ...) __printf0like(1, 2);
102 static char *savestr(const char *);
106 main(int argc
, char *argv
[])
109 error("usage: mknodes file");
111 if ((infp
= fopen(argv
[1], "r")) == NULL
)
112 error("Can't open %s: %s", argv
[1], strerror(errno
));
114 if (line
[0] == ' ' || line
[0] == '\t')
116 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: %s", file
, strerror(errno
));
221 if ((hfile
= fopen("nodes.h", "w")) == NULL
)
222 error("Can't create nodes.h: %s", strerror(errno
));
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("union node *copyfunc(union node *);\n", hfile
);
247 fputs("void freefunc(union node *);\n", hfile
);
249 fputs(writer
, cfile
);
250 while (fgets(line
, sizeof line
, patfile
) != NULL
) {
251 for (p
= line
; *p
== ' ' || *p
== '\t' ; p
++);
252 if (strcmp(p
, "%SIZES\n") == 0)
254 else if (strcmp(p
, "%CALCSIZE\n") == 0)
256 else if (strcmp(p
, "%COPY\n") == 0)
266 outsizes(FILE *cfile
)
270 fprintf(cfile
, "static const short nodesize[%d] = {\n", ntypes
);
271 for (i
= 0 ; i
< ntypes
; i
++) {
272 fprintf(cfile
, " ALIGN(sizeof (struct %s)),\n", nodestr
[i
]->tag
);
274 fprintf(cfile
, "};\n");
279 outfunc(FILE *cfile
, int calcsize
)
285 fputs(" if (n == NULL)\n", cfile
);
287 fputs(" return;\n", cfile
);
289 fputs(" return NULL;\n", cfile
);
291 fputs(" funcblocksize += nodesize[n->type];\n", cfile
);
293 fputs(" new = funcblock;\n", cfile
);
294 fputs(" funcblock = (char *)funcblock + nodesize[n->type];\n", cfile
);
296 fputs(" switch (n->type) {\n", cfile
);
297 for (sp
= str
; sp
< &str
[nstr
] ; sp
++) {
298 for (i
= 0 ; i
< ntypes
; i
++) {
299 if (nodestr
[i
] == sp
)
300 fprintf(cfile
, " case %s:\n", nodename
[i
]);
302 for (i
= sp
->nfields
; --i
>= 1 ; ) {
308 fprintf(cfile
, "calcsize(n->%s.%s);\n",
312 fprintf(cfile
, "new->%s.%s = copynode(n->%s.%s);\n",
313 sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
319 fprintf(cfile
, "sizenodelist(n->%s.%s);\n",
323 fprintf(cfile
, "new->%s.%s = copynodelist(n->%s.%s);\n",
324 sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
330 fprintf(cfile
, "funcstringsize += strlen(n->%s.%s) + 1;\n",
334 fprintf(cfile
, "new->%s.%s = nodesavestr(n->%s.%s);\n",
335 sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
342 fprintf(cfile
, "new->%s.%s = n->%s.%s;\n",
343 sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
349 fputs("break;\n", cfile
);
351 fputs(" };\n", cfile
);
353 fputs(" new->type = n->type;\n", cfile
);
358 indent(int amount
, FILE *fp
)
360 while (amount
>= 8) {
364 while (--amount
>= 0) {
376 while (*p
== ' ' || *p
== '\t')
379 while (*p
!= ' ' && *p
!= '\t' && *p
!= '\0')
390 while (*linep
== ' ' || *linep
== '\t')
400 if (fgets(line
, 1024, infp
) == NULL
)
402 for (p
= line
; *p
!= '#' && *p
!= '\n' && *p
!= '\0' ; p
++);
403 while (p
> line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
408 if (p
- line
> BUFLEN
)
409 error("Line too long");
416 error(const char *msg
, ...)
421 fprintf(stderr
, "line %d: ", linno
);
422 vfprintf(stderr
, msg
, va
);
433 savestr(const char *s
)
437 if ((p
= malloc(strlen(s
) + 1)) == NULL
)
438 error("Out of space");