]>
git.saurik.com Git - apple/shell_cmds.git/blob - sh/mknodes.c
f289890a399247e0ac3aa939c4852494baf43810
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 // When building for iOS, we don't have a new enough OS X SDK.
62 #define __printf0like(fmtarg, firstvararg) \
63 __attribute__((__format__ (__printf0__, fmtarg, firstvararg)))
66 #define MAXTYPES 50 /* max number of node types */
67 #define MAXFIELDS 20 /* max fields in a structure */
68 #define BUFLEN 100 /* size of character buffers */
71 #define T_NODE 1 /* union node *field */
72 #define T_NODELIST 2 /* struct nodelist *field */
74 #define T_INT 4 /* int field */
75 #define T_OTHER 5 /* other */
76 #define T_TEMP 6 /* don't copy this field */
79 struct field
{ /* a structure field */
80 char *name
; /* name of field */
81 int type
; /* type of field */
82 char *decl
; /* declaration of field */
86 struct str
{ /* struct representing a node structure */
87 char *tag
; /* structure tag */
88 int nfields
; /* number of fields in the structure */
89 struct field field
[MAXFIELDS
]; /* the fields of the structure */
90 int done
; /* set if fully parsed */
94 static int ntypes
; /* number of node types */
95 static char *nodename
[MAXTYPES
]; /* names of the nodes */
96 static struct str
*nodestr
[MAXTYPES
]; /* type of structure used by the node */
97 static int nstr
; /* number of structures */
98 static struct str str
[MAXTYPES
]; /* the structures */
99 static struct str
*curstr
; /* current structure */
100 static char line
[1024];
104 static void parsenode(void);
105 static void parsefield(void);
106 static void output(char *);
107 static void outsizes(FILE *);
108 static void outfunc(FILE *, int);
109 static void indent(int, FILE *);
110 static int nextfield(char *);
111 static void skipbl(void);
112 static int readline(FILE *);
113 static void error(const char *, ...) __printf0like(1, 2) __dead2
;
114 static char *savestr(const char *);
118 main(int argc
, char *argv
[])
123 error("usage: mknodes file");
124 if ((infp
= fopen(argv
[1], "r")) == NULL
)
125 error("Can't open %s: %s", argv
[1], strerror(errno
));
126 while (readline(infp
)) {
127 if (line
[0] == ' ' || line
[0] == '\t')
129 else if (line
[0] != '\0')
146 if (curstr
&& curstr
->nfields
> 0)
149 if (! nextfield(tag
))
150 error("Tag expected");
152 error("Garbage at end of line");
153 nodename
[ntypes
] = savestr(name
);
154 for (sp
= str
; sp
< str
+ nstr
; sp
++) {
155 if (strcmp(sp
->tag
, tag
) == 0)
158 if (sp
>= str
+ nstr
) {
159 sp
->tag
= savestr(tag
);
164 nodestr
[ntypes
] = sp
;
174 char decl
[2 * BUFLEN
];
177 if (curstr
== NULL
|| curstr
->done
)
178 error("No current structure to add field to");
179 if (! nextfield(name
))
180 error("No field name");
181 if (! nextfield(type
))
182 error("No field type");
183 fp
= &curstr
->field
[curstr
->nfields
];
184 fp
->name
= savestr(name
);
185 if (strcmp(type
, "nodeptr") == 0) {
187 sprintf(decl
, "union node *%s", name
);
188 } else if (strcmp(type
, "nodelist") == 0) {
189 fp
->type
= T_NODELIST
;
190 sprintf(decl
, "struct nodelist *%s", name
);
191 } else if (strcmp(type
, "string") == 0) {
193 sprintf(decl
, "char *%s", name
);
194 } else if (strcmp(type
, "int") == 0) {
196 sprintf(decl
, "int %s", name
);
197 } else if (strcmp(type
, "other") == 0) {
199 } else if (strcmp(type
, "temp") == 0) {
202 error("Unknown type %s", type
);
204 if (fp
->type
== T_OTHER
|| fp
->type
== T_TEMP
) {
206 fp
->decl
= savestr(linep
);
209 error("Garbage at end of line");
210 fp
->decl
= savestr(decl
);
216 static const char writer
[] = "\
218 * This file was generated by the mknodes program.\n\
233 if ((patfile
= fopen(file
, "r")) == NULL
)
234 error("Can't open %s: %s", file
, strerror(errno
));
235 if ((hfile
= fopen("nodes.h", "w")) == NULL
)
236 error("Can't create nodes.h: %s", strerror(errno
));
237 if ((cfile
= fopen("nodes.c", "w")) == NULL
)
238 error("Can't create nodes.c");
239 fputs(writer
, hfile
);
240 for (i
= 0 ; i
< ntypes
; i
++)
241 fprintf(hfile
, "#define %s %d\n", nodename
[i
], i
);
242 fputs("\n\n\n", hfile
);
243 for (sp
= str
; sp
< &str
[nstr
] ; sp
++) {
244 fprintf(hfile
, "struct %s {\n", sp
->tag
);
245 for (i
= sp
->nfields
, fp
= sp
->field
; --i
>= 0 ; fp
++) {
246 fprintf(hfile
, " %s;\n", fp
->decl
);
248 fputs("};\n\n\n", hfile
);
250 fputs("union node {\n", hfile
);
251 fprintf(hfile
, " int type;\n");
252 for (sp
= str
; sp
< &str
[nstr
] ; sp
++) {
253 fprintf(hfile
, " struct %s %s;\n", sp
->tag
, sp
->tag
);
255 fputs("};\n\n\n", hfile
);
256 fputs("struct nodelist {\n", hfile
);
257 fputs("\tstruct nodelist *next;\n", hfile
);
258 fputs("\tunion node *n;\n", hfile
);
259 fputs("};\n\n\n", hfile
);
260 fputs("struct funcdef;\n", hfile
);
261 fputs("struct funcdef *copyfunc(union node *);\n", hfile
);
262 fputs("union node *getfuncnode(struct funcdef *);\n", hfile
);
263 fputs("void reffunc(struct funcdef *);\n", hfile
);
264 fputs("void unreffunc(struct funcdef *);\n", hfile
);
266 error("Can't write to nodes.h");
268 error("Can't close nodes.h");
270 fputs(writer
, cfile
);
271 while (fgets(line
, sizeof line
, patfile
) != NULL
) {
272 for (p
= line
; *p
== ' ' || *p
== '\t' ; p
++);
273 if (strcmp(p
, "%SIZES\n") == 0)
275 else if (strcmp(p
, "%CALCSIZE\n") == 0)
277 else if (strcmp(p
, "%COPY\n") == 0)
284 error("Can't write to nodes.c");
286 error("Can't close nodes.c");
292 outsizes(FILE *cfile
)
296 fprintf(cfile
, "static const short nodesize[%d] = {\n", ntypes
);
297 for (i
= 0 ; i
< ntypes
; i
++) {
298 fprintf(cfile
, " ALIGN(sizeof (struct %s)),\n", nodestr
[i
]->tag
);
300 fprintf(cfile
, "};\n");
305 outfunc(FILE *cfile
, int calcsize
)
311 fputs(" if (n == NULL)\n", cfile
);
313 fputs(" return;\n", cfile
);
315 fputs(" return NULL;\n", cfile
);
317 fputs(" result->blocksize += nodesize[n->type];\n", cfile
);
319 fputs(" new = state->block;\n", cfile
);
320 fputs(" state->block = (char *)state->block + nodesize[n->type];\n", cfile
);
322 fputs(" switch (n->type) {\n", cfile
);
323 for (sp
= str
; sp
< &str
[nstr
] ; sp
++) {
324 for (i
= 0 ; i
< ntypes
; i
++) {
325 if (nodestr
[i
] == sp
)
326 fprintf(cfile
, " case %s:\n", nodename
[i
]);
328 for (i
= sp
->nfields
; --i
>= 1 ; ) {
334 fprintf(cfile
, "calcsize(n->%s.%s, result);\n",
338 fprintf(cfile
, "new->%s.%s = copynode(n->%s.%s, state);\n",
339 sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
345 fprintf(cfile
, "sizenodelist(n->%s.%s, result);\n",
349 fprintf(cfile
, "new->%s.%s = copynodelist(n->%s.%s, state);\n",
350 sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
356 fprintf(cfile
, "result->stringsize += strlen(n->%s.%s) + 1;\n",
360 fprintf(cfile
, "new->%s.%s = nodesavestr(n->%s.%s, state);\n",
361 sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
368 fprintf(cfile
, "new->%s.%s = n->%s.%s;\n",
369 sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
375 fputs("break;\n", cfile
);
377 fputs(" };\n", cfile
);
379 fputs(" new->type = n->type;\n", cfile
);
384 indent(int amount
, FILE *fp
)
386 while (amount
>= 8) {
390 while (--amount
>= 0) {
402 while (*p
== ' ' || *p
== '\t')
405 while (*p
!= ' ' && *p
!= '\t' && *p
!= '\0')
416 while (*linep
== ' ' || *linep
== '\t')
426 if (fgets(line
, 1024, infp
) == NULL
)
428 for (p
= line
; *p
!= '#' && *p
!= '\n' && *p
!= '\0' ; p
++);
429 while (p
> line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
434 if (p
- line
> BUFLEN
)
435 error("Line too long");
442 error(const char *msg
, ...)
447 (void) fprintf(stderr
, "line %d: ", linno
);
448 (void) vfprintf(stderr
, msg
, va
);
449 (void) fputc('\n', stderr
);
459 savestr(const char *s
)
463 if ((p
= malloc(strlen(s
) + 1)) == NULL
)
464 error("Out of space");