usr.sbin/makefs: Sync with sys/vfs/hammer2
[dragonfly.git] / bin / sh / mknodes.c
blob3199a2fe7cb120f06157390a42802debceda8ffd
1 /*-
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
8 * Kenneth Almquist.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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
32 * SUCH DAMAGE.
35 #if 0
36 #ifndef lint
37 static char const copyright[] =
38 "@(#) Copyright (c) 1991, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
42 #ifndef lint
43 static char sccsid[] = "@(#)mknodes.c 8.2 (Berkeley) 5/4/95";
44 #endif /* not lint */
45 #endif
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.
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <errno.h>
58 #include <stdarg.h>
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 */
64 /* field types */
65 #define T_NODE 1 /* union node *field */
66 #define T_NODELIST 2 /* struct nodelist *field */
67 #define T_STRING 3
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];
95 static int linno;
96 static char *linep;
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[])
114 FILE *infp;
116 if (argc != 3)
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')
122 parsefield();
123 else if (line[0] != '\0')
124 parsenode();
126 fclose(infp);
127 output(argv[2]);
128 exit(0);
133 static void
134 parsenode(void)
136 char name[BUFLEN];
137 char tag[BUFLEN];
138 struct str *sp;
140 if (curstr && curstr->nfields > 0)
141 curstr->done = 1;
142 nextfield(name);
143 if (! nextfield(tag))
144 error("Tag expected");
145 if (*linep != '\0')
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)
150 break;
152 if (sp >= str + nstr) {
153 sp->tag = savestr(tag);
154 sp->nfields = 0;
155 curstr = sp;
156 nstr++;
158 nodestr[ntypes] = sp;
159 ntypes++;
163 static void
164 parsefield(void)
166 char name[BUFLEN];
167 char type[BUFLEN];
168 char decl[2 * BUFLEN];
169 struct field *fp;
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) {
180 fp->type = T_NODE;
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) {
186 fp->type = T_STRING;
187 sprintf(decl, "char *%s", name);
188 } else if (strcmp(type, "int") == 0) {
189 fp->type = T_INT;
190 sprintf(decl, "int %s", name);
191 } else if (strcmp(type, "other") == 0) {
192 fp->type = T_OTHER;
193 } else if (strcmp(type, "temp") == 0) {
194 fp->type = T_TEMP;
195 } else {
196 error("Unknown type %s", type);
198 if (fp->type == T_OTHER || fp->type == T_TEMP) {
199 skipbl();
200 fp->decl = savestr(linep);
201 } else {
202 if (*linep)
203 error("Garbage at end of line");
204 fp->decl = savestr(decl);
206 curstr->nfields++;
210 static const char writer[] = "\
211 /*\n\
212 * This file was generated by the mknodes program.\n\
213 */\n\
214 \n";
216 static void
217 output(char *file)
219 FILE *hfile;
220 FILE *cfile;
221 FILE *patfile;
222 int i;
223 struct str *sp;
224 struct field *fp;
225 char *p;
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);
259 if (ferror(hfile))
260 error("Can't write to nodes.h");
261 if (fclose(hfile))
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)
268 outsizes(cfile);
269 else if (strcmp(p, "%CALCSIZE\n") == 0)
270 outfunc(cfile, 1);
271 else if (strcmp(p, "%COPY\n") == 0)
272 outfunc(cfile, 0);
273 else
274 fputs(line, cfile);
276 fclose(patfile);
277 if (ferror(cfile))
278 error("Can't write to nodes.c");
279 if (fclose(cfile))
280 error("Can't close nodes.c");
285 static void
286 outsizes(FILE *cfile)
288 int i;
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");
298 static void
299 outfunc(FILE *cfile, int calcsize)
301 struct str *sp;
302 struct field *fp;
303 int i;
305 fputs(" if (n == NULL)\n", cfile);
306 if (calcsize)
307 fputs(" return;\n", cfile);
308 else
309 fputs(" return NULL;\n", cfile);
310 if (calcsize)
311 fputs(" result->blocksize += nodesize[n->type];\n", cfile);
312 else {
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 ; ) {
323 fp = &sp->field[i];
324 switch (fp->type) {
325 case T_NODE:
326 if (calcsize) {
327 indent(12, cfile);
328 fprintf(cfile, "calcsize(n->%s.%s, result);\n",
329 sp->tag, fp->name);
330 } else {
331 indent(12, cfile);
332 fprintf(cfile, "new->%s.%s = copynode(n->%s.%s, state);\n",
333 sp->tag, fp->name, sp->tag, fp->name);
335 break;
336 case T_NODELIST:
337 if (calcsize) {
338 indent(12, cfile);
339 fprintf(cfile, "sizenodelist(n->%s.%s, result);\n",
340 sp->tag, fp->name);
341 } else {
342 indent(12, cfile);
343 fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s, state);\n",
344 sp->tag, fp->name, sp->tag, fp->name);
346 break;
347 case T_STRING:
348 if (calcsize) {
349 indent(12, cfile);
350 fprintf(cfile, "result->stringsize += strlen(n->%s.%s) + 1;\n",
351 sp->tag, fp->name);
352 } else {
353 indent(12, cfile);
354 fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s, state);\n",
355 sp->tag, fp->name, sp->tag, fp->name);
357 break;
358 case T_INT:
359 case T_OTHER:
360 if (! calcsize) {
361 indent(12, cfile);
362 fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
363 sp->tag, fp->name, sp->tag, fp->name);
365 break;
368 indent(12, cfile);
369 fputs("break;\n", cfile);
371 fputs(" };\n", cfile);
372 if (! calcsize)
373 fputs(" new->type = n->type;\n", cfile);
377 static void
378 indent(int amount, FILE *fp)
380 while (amount >= 8) {
381 putc('\t', fp);
382 amount -= 8;
384 while (--amount >= 0) {
385 putc(' ', fp);
390 static int
391 nextfield(char *buf)
393 char *p, *q;
395 p = linep;
396 while (*p == ' ' || *p == '\t')
397 p++;
398 q = buf;
399 while (*p != ' ' && *p != '\t' && *p != '\0')
400 *q++ = *p++;
401 *q = '\0';
402 linep = p;
403 return (q > buf);
407 static void
408 skipbl(void)
410 while (*linep == ' ' || *linep == '\t')
411 linep++;
415 static int
416 readline(FILE *infp)
418 char *p;
420 if (fgets(line, 1024, infp) == NULL)
421 return 0;
422 for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
423 while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
424 p--;
425 *p = '\0';
426 linep = line;
427 linno++;
428 if (p - line > BUFLEN)
429 error("Line too long");
430 return 1;
435 static void
436 error(const char *msg, ...)
438 va_list va;
439 va_start(va, msg);
441 (void) fprintf(stderr, "line %d: ", linno);
442 (void) vfprintf(stderr, msg, va);
443 (void) fputc('\n', stderr);
445 va_end(va);
447 exit(2);
452 static char *
453 savestr(const char *s)
455 char *p;
457 if ((p = malloc(strlen(s) + 1)) == NULL)
458 error("Out of space");
459 (void) strcpy(p, s);
460 return p;