2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. 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
37 static char const copyright
[] =
38 "@(#) Copyright (c) 1991, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid
[] = "@(#)mknodes.c 8.2 (Berkeley) 5/4/95";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD: head/bin/sh/mknodes.c 326025 2017-11-20 19:49:47Z pfg $");
50 * This program reads the nodetypes file and nodes.c.pat file. It generates
51 * the files nodes.h and nodes.c.
60 #define MAXTYPES 50 /* max number of node types */
61 #define MAXFIELDS 20 /* max fields in a structure */
62 #define BUFLEN 100 /* size of character buffers */
65 #define T_NODE 1 /* union node *field */
66 #define T_NODELIST 2 /* struct nodelist *field */
68 #define T_INT 4 /* int field */
69 #define T_OTHER 5 /* other */
70 #define T_TEMP 6 /* don't copy this field */
73 struct field
{ /* a structure field */
74 char *name
; /* name of field */
75 int type
; /* type of field */
76 char *decl
; /* declaration of field */
80 struct str
{ /* struct representing a node structure */
81 char *tag
; /* structure tag */
82 int nfields
; /* number of fields in the structure */
83 struct field field
[MAXFIELDS
]; /* the fields of the structure */
84 int done
; /* set if fully parsed */
88 static int ntypes
; /* number of node types */
89 static char *nodename
[MAXTYPES
]; /* names of the nodes */
90 static struct str
*nodestr
[MAXTYPES
]; /* type of structure used by the node */
91 static int nstr
; /* number of structures */
92 static struct str str
[MAXTYPES
]; /* the structures */
93 static struct str
*curstr
; /* current structure */
94 static char line
[1024];
98 static void parsenode(void);
99 static void parsefield(void);
100 static void output(char *);
101 static void outsizes(FILE *);
102 static void outfunc(FILE *, int);
103 static void indent(int, FILE *);
104 static int nextfield(char *);
105 static void skipbl(void);
106 static int readline(FILE *);
107 static void error(const char *, ...) __printf0like(1, 2) __dead2
;
108 static char *savestr(const char *);
112 main(int argc
, char *argv
[])
117 error("usage: mknodes file");
118 if ((infp
= fopen(argv
[1], "r")) == NULL
)
119 error("Can't open %s: %s", argv
[1], strerror(errno
));
120 while (readline(infp
)) {
121 if (line
[0] == ' ' || line
[0] == '\t')
123 else if (line
[0] != '\0')
140 if (curstr
&& curstr
->nfields
> 0)
143 if (! nextfield(tag
))
144 error("Tag expected");
146 error("Garbage at end of line");
147 nodename
[ntypes
] = savestr(name
);
148 for (sp
= str
; sp
< str
+ nstr
; sp
++) {
149 if (strcmp(sp
->tag
, tag
) == 0)
152 if (sp
>= str
+ nstr
) {
153 sp
->tag
= savestr(tag
);
158 nodestr
[ntypes
] = sp
;
168 char decl
[2 * BUFLEN
];
171 if (curstr
== NULL
|| curstr
->done
)
172 error("No current structure to add field to");
173 if (! nextfield(name
))
174 error("No field name");
175 if (! nextfield(type
))
176 error("No field type");
177 fp
= &curstr
->field
[curstr
->nfields
];
178 fp
->name
= savestr(name
);
179 if (strcmp(type
, "nodeptr") == 0) {
181 sprintf(decl
, "union node *%s", name
);
182 } else if (strcmp(type
, "nodelist") == 0) {
183 fp
->type
= T_NODELIST
;
184 sprintf(decl
, "struct nodelist *%s", name
);
185 } else if (strcmp(type
, "string") == 0) {
187 sprintf(decl
, "char *%s", name
);
188 } else if (strcmp(type
, "int") == 0) {
190 sprintf(decl
, "int %s", name
);
191 } else if (strcmp(type
, "other") == 0) {
193 } else if (strcmp(type
, "temp") == 0) {
196 error("Unknown type %s", type
);
198 if (fp
->type
== T_OTHER
|| fp
->type
== T_TEMP
) {
200 fp
->decl
= savestr(linep
);
203 error("Garbage at end of line");
204 fp
->decl
= savestr(decl
);
210 static const char writer
[] = "\
212 * This file was generated by the mknodes program.\n\
227 if ((patfile
= fopen(file
, "r")) == NULL
)
228 error("Can't open %s: %s", file
, strerror(errno
));
229 if ((hfile
= fopen("nodes.h", "w")) == NULL
)
230 error("Can't create nodes.h: %s", strerror(errno
));
231 if ((cfile
= fopen("nodes.c", "w")) == NULL
)
232 error("Can't create nodes.c");
233 fputs(writer
, hfile
);
234 for (i
= 0 ; i
< ntypes
; i
++)
235 fprintf(hfile
, "#define %s %d\n", nodename
[i
], i
);
236 fputs("\n\n\n", hfile
);
237 for (sp
= str
; sp
< &str
[nstr
] ; sp
++) {
238 fprintf(hfile
, "struct %s {\n", sp
->tag
);
239 for (i
= sp
->nfields
, fp
= sp
->field
; --i
>= 0 ; fp
++) {
240 fprintf(hfile
, " %s;\n", fp
->decl
);
242 fputs("};\n\n\n", hfile
);
244 fputs("union node {\n", hfile
);
245 fprintf(hfile
, " int type;\n");
246 for (sp
= str
; sp
< &str
[nstr
] ; sp
++) {
247 fprintf(hfile
, " struct %s %s;\n", sp
->tag
, sp
->tag
);
249 fputs("};\n\n\n", hfile
);
250 fputs("struct nodelist {\n", hfile
);
251 fputs("\tstruct nodelist *next;\n", hfile
);
252 fputs("\tunion node *n;\n", hfile
);
253 fputs("};\n\n\n", hfile
);
254 fputs("struct funcdef;\n", hfile
);
255 fputs("struct funcdef *copyfunc(union node *);\n", hfile
);
256 fputs("union node *getfuncnode(struct funcdef *);\n", hfile
);
257 fputs("void reffunc(struct funcdef *);\n", hfile
);
258 fputs("void unreffunc(struct funcdef *);\n", hfile
);
260 error("Can't write to nodes.h");
262 error("Can't close nodes.h");
264 fputs(writer
, cfile
);
265 while (fgets(line
, sizeof line
, patfile
) != NULL
) {
266 for (p
= line
; *p
== ' ' || *p
== '\t' ; p
++);
267 if (strcmp(p
, "%SIZES\n") == 0)
269 else if (strcmp(p
, "%CALCSIZE\n") == 0)
271 else if (strcmp(p
, "%COPY\n") == 0)
278 error("Can't write to nodes.c");
280 error("Can't close nodes.c");
286 outsizes(FILE *cfile
)
290 fprintf(cfile
, "static const short nodesize[%d] = {\n", ntypes
);
291 for (i
= 0 ; i
< ntypes
; i
++) {
292 fprintf(cfile
, " ALIGN(sizeof (struct %s)),\n", nodestr
[i
]->tag
);
294 fprintf(cfile
, "};\n");
299 outfunc(FILE *cfile
, int calcsize
)
305 fputs(" if (n == NULL)\n", cfile
);
307 fputs(" return;\n", cfile
);
309 fputs(" return NULL;\n", cfile
);
311 fputs(" result->blocksize += nodesize[n->type];\n", cfile
);
313 fputs(" new = state->block;\n", cfile
);
314 fputs(" state->block = (char *)state->block + nodesize[n->type];\n", cfile
);
316 fputs(" switch (n->type) {\n", cfile
);
317 for (sp
= str
; sp
< &str
[nstr
] ; sp
++) {
318 for (i
= 0 ; i
< ntypes
; i
++) {
319 if (nodestr
[i
] == sp
)
320 fprintf(cfile
, " case %s:\n", nodename
[i
]);
322 for (i
= sp
->nfields
; --i
>= 1 ; ) {
328 fprintf(cfile
, "calcsize(n->%s.%s, result);\n",
332 fprintf(cfile
, "new->%s.%s = copynode(n->%s.%s, state);\n",
333 sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
339 fprintf(cfile
, "sizenodelist(n->%s.%s, result);\n",
343 fprintf(cfile
, "new->%s.%s = copynodelist(n->%s.%s, state);\n",
344 sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
350 fprintf(cfile
, "result->stringsize += strlen(n->%s.%s) + 1;\n",
354 fprintf(cfile
, "new->%s.%s = nodesavestr(n->%s.%s, state);\n",
355 sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
362 fprintf(cfile
, "new->%s.%s = n->%s.%s;\n",
363 sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
369 fputs("break;\n", cfile
);
371 fputs(" };\n", cfile
);
373 fputs(" new->type = n->type;\n", cfile
);
378 indent(int amount
, FILE *fp
)
380 while (amount
>= 8) {
384 while (--amount
>= 0) {
396 while (*p
== ' ' || *p
== '\t')
399 while (*p
!= ' ' && *p
!= '\t' && *p
!= '\0')
410 while (*linep
== ' ' || *linep
== '\t')
420 if (fgets(line
, 1024, infp
) == NULL
)
422 for (p
= line
; *p
!= '#' && *p
!= '\n' && *p
!= '\0' ; p
++);
423 while (p
> line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
428 if (p
- line
> BUFLEN
)
429 error("Line too long");
436 error(const char *msg
, ...)
441 (void) fprintf(stderr
, "line %d: ", linno
);
442 (void) vfprintf(stderr
, msg
, va
);
443 (void) fputc('\n', stderr
);
453 savestr(const char *s
)
457 if ((p
= malloc(strlen(s
) + 1)) == NULL
)
458 error("Out of space");