xref: /freebsd/bin/sh/mknodes.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Kenneth Almquist.
9  *
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.
21  *
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.
33  */
34 
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 */
41 
42 #endif
43 #include <sys/cdefs.h>
44 /*
45  * This program reads the nodetypes file and nodes.c.pat file.  It generates
46  * the files nodes.h and nodes.c.
47  */
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <errno.h>
53 #include <stdarg.h>
54 
55 #define MAXTYPES 50		/* max number of node types */
56 #define MAXFIELDS 20		/* max fields in a structure */
57 #define BUFLEN 100		/* size of character buffers */
58 
59 /* field types */
60 #define T_NODE 1		/* union node *field */
61 #define T_NODELIST 2		/* struct nodelist *field */
62 #define T_STRING 3
63 #define T_INT 4			/* int field */
64 #define T_OTHER 5		/* other */
65 #define T_TEMP 6		/* don't copy this field */
66 
67 
68 struct field {			/* a structure field */
69 	char *name;		/* name of field */
70 	int type;			/* type of field */
71 	char *decl;		/* declaration of field */
72 };
73 
74 
75 struct str {			/* struct representing a node structure */
76 	char *tag;		/* structure tag */
77 	int nfields;		/* number of fields in the structure */
78 	struct field field[MAXFIELDS];	/* the fields of the structure */
79 	int done;			/* set if fully parsed */
80 };
81 
82 
83 static int ntypes;			/* number of node types */
84 static char *nodename[MAXTYPES];	/* names of the nodes */
85 static struct str *nodestr[MAXTYPES];	/* type of structure used by the node */
86 static int nstr;			/* number of structures */
87 static struct str str[MAXTYPES];	/* the structures */
88 static struct str *curstr;		/* current structure */
89 static char line[1024];
90 static int linno;
91 static char *linep;
92 
93 static void parsenode(void);
94 static void parsefield(void);
95 static void output(char *);
96 static void outsizes(FILE *);
97 static void outfunc(FILE *, int);
98 static void indent(int, FILE *);
99 static int nextfield(char *);
100 static void skipbl(void);
101 static int readline(FILE *);
102 static void error(const char *, ...) __printf0like(1, 2) __dead2;
103 static char *savestr(const char *);
104 
105 
106 int
107 main(int argc, char *argv[])
108 {
109 	FILE *infp;
110 
111 	if (argc != 3)
112 		error("usage: mknodes file");
113 	if ((infp = fopen(argv[1], "r")) == NULL)
114 		error("Can't open %s: %s", argv[1], strerror(errno));
115 	while (readline(infp)) {
116 		if (line[0] == ' ' || line[0] == '\t')
117 			parsefield();
118 		else if (line[0] != '\0')
119 			parsenode();
120 	}
121 	fclose(infp);
122 	output(argv[2]);
123 	exit(0);
124 }
125 
126 
127 
128 static void
129 parsenode(void)
130 {
131 	char name[BUFLEN];
132 	char tag[BUFLEN];
133 	struct str *sp;
134 
135 	if (curstr && curstr->nfields > 0)
136 		curstr->done = 1;
137 	nextfield(name);
138 	if (! nextfield(tag))
139 		error("Tag expected");
140 	if (*linep != '\0')
141 		error("Garbage at end of line");
142 	nodename[ntypes] = savestr(name);
143 	for (sp = str ; sp < str + nstr ; sp++) {
144 		if (strcmp(sp->tag, tag) == 0)
145 			break;
146 	}
147 	if (sp >= str + nstr) {
148 		sp->tag = savestr(tag);
149 		sp->nfields = 0;
150 		curstr = sp;
151 		nstr++;
152 	}
153 	nodestr[ntypes] = sp;
154 	ntypes++;
155 }
156 
157 
158 static void
159 parsefield(void)
160 {
161 	char name[BUFLEN];
162 	char type[BUFLEN];
163 	char decl[2 * BUFLEN];
164 	struct field *fp;
165 
166 	if (curstr == NULL || curstr->done)
167 		error("No current structure to add field to");
168 	if (! nextfield(name))
169 		error("No field name");
170 	if (! nextfield(type))
171 		error("No field type");
172 	fp = &curstr->field[curstr->nfields];
173 	fp->name = savestr(name);
174 	if (strcmp(type, "nodeptr") == 0) {
175 		fp->type = T_NODE;
176 		sprintf(decl, "union node *%s", name);
177 	} else if (strcmp(type, "nodelist") == 0) {
178 		fp->type = T_NODELIST;
179 		sprintf(decl, "struct nodelist *%s", name);
180 	} else if (strcmp(type, "string") == 0) {
181 		fp->type = T_STRING;
182 		sprintf(decl, "char *%s", name);
183 	} else if (strcmp(type, "int") == 0) {
184 		fp->type = T_INT;
185 		sprintf(decl, "int %s", name);
186 	} else if (strcmp(type, "other") == 0) {
187 		fp->type = T_OTHER;
188 	} else if (strcmp(type, "temp") == 0) {
189 		fp->type = T_TEMP;
190 	} else {
191 		error("Unknown type %s", type);
192 	}
193 	if (fp->type == T_OTHER || fp->type == T_TEMP) {
194 		skipbl();
195 		fp->decl = savestr(linep);
196 	} else {
197 		if (*linep)
198 			error("Garbage at end of line");
199 		fp->decl = savestr(decl);
200 	}
201 	curstr->nfields++;
202 }
203 
204 
205 static const char writer[] = "\
206 /*\n\
207  * This file was generated by the mknodes program.\n\
208  */\n\
209 \n";
210 
211 static void
212 output(char *file)
213 {
214 	FILE *hfile;
215 	FILE *cfile;
216 	FILE *patfile;
217 	int i;
218 	struct str *sp;
219 	struct field *fp;
220 	char *p;
221 
222 	if ((patfile = fopen(file, "r")) == NULL)
223 		error("Can't open %s: %s", file, strerror(errno));
224 	if ((hfile = fopen("nodes.h", "w")) == NULL)
225 		error("Can't create nodes.h: %s", strerror(errno));
226 	if ((cfile = fopen("nodes.c", "w")) == NULL)
227 		error("Can't create nodes.c");
228 	fputs(writer, hfile);
229 	for (i = 0 ; i < ntypes ; i++)
230 		fprintf(hfile, "#define %s %d\n", nodename[i], i);
231 	fputs("\n\n\n", hfile);
232 	for (sp = str ; sp < &str[nstr] ; sp++) {
233 		fprintf(hfile, "struct %s {\n", sp->tag);
234 		for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
235 			fprintf(hfile, "      %s;\n", fp->decl);
236 		}
237 		fputs("};\n\n\n", hfile);
238 	}
239 	fputs("union node {\n", hfile);
240 	fprintf(hfile, "      int type;\n");
241 	for (sp = str ; sp < &str[nstr] ; sp++) {
242 		fprintf(hfile, "      struct %s %s;\n", sp->tag, sp->tag);
243 	}
244 	fputs("};\n\n\n", hfile);
245 	fputs("struct nodelist {\n", hfile);
246 	fputs("\tstruct nodelist *next;\n", hfile);
247 	fputs("\tunion node *n;\n", hfile);
248 	fputs("};\n\n\n", hfile);
249 	fputs("struct funcdef;\n", hfile);
250 	fputs("struct funcdef *copyfunc(union node *);\n", hfile);
251 	fputs("union node *getfuncnode(struct funcdef *);\n", hfile);
252 	fputs("void reffunc(struct funcdef *);\n", hfile);
253 	fputs("void unreffunc(struct funcdef *);\n", hfile);
254 	if (ferror(hfile))
255 		error("Can't write to nodes.h");
256 	if (fclose(hfile))
257 		error("Can't close nodes.h");
258 
259 	fputs(writer, cfile);
260 	while (fgets(line, sizeof line, patfile) != NULL) {
261 		for (p = line ; *p == ' ' || *p == '\t' ; p++);
262 		if (strcmp(p, "%SIZES\n") == 0)
263 			outsizes(cfile);
264 		else if (strcmp(p, "%CALCSIZE\n") == 0)
265 			outfunc(cfile, 1);
266 		else if (strcmp(p, "%COPY\n") == 0)
267 			outfunc(cfile, 0);
268 		else
269 			fputs(line, cfile);
270 	}
271 	fclose(patfile);
272 	if (ferror(cfile))
273 		error("Can't write to nodes.c");
274 	if (fclose(cfile))
275 		error("Can't close nodes.c");
276 }
277 
278 
279 
280 static void
281 outsizes(FILE *cfile)
282 {
283 	int i;
284 
285 	fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
286 	for (i = 0 ; i < ntypes ; i++) {
287 		fprintf(cfile, "      ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
288 	}
289 	fprintf(cfile, "};\n");
290 }
291 
292 
293 static void
294 outfunc(FILE *cfile, int calcsize)
295 {
296 	struct str *sp;
297 	struct field *fp;
298 	int i;
299 
300 	fputs("      if (n == NULL)\n", cfile);
301 	if (calcsize)
302 		fputs("	    return;\n", cfile);
303 	else
304 		fputs("	    return NULL;\n", cfile);
305 	if (calcsize)
306 		fputs("      result->blocksize += nodesize[n->type];\n", cfile);
307 	else {
308 		fputs("      new = state->block;\n", cfile);
309 		fputs("      state->block = (char *)state->block + nodesize[n->type];\n", cfile);
310 	}
311 	fputs("      switch (n->type) {\n", cfile);
312 	for (sp = str ; sp < &str[nstr] ; sp++) {
313 		for (i = 0 ; i < ntypes ; i++) {
314 			if (nodestr[i] == sp)
315 				fprintf(cfile, "      case %s:\n", nodename[i]);
316 		}
317 		for (i = sp->nfields ; --i >= 1 ; ) {
318 			fp = &sp->field[i];
319 			switch (fp->type) {
320 			case T_NODE:
321 				if (calcsize) {
322 					indent(12, cfile);
323 					fprintf(cfile, "calcsize(n->%s.%s, result);\n",
324 						sp->tag, fp->name);
325 				} else {
326 					indent(12, cfile);
327 					fprintf(cfile, "new->%s.%s = copynode(n->%s.%s, state);\n",
328 						sp->tag, fp->name, sp->tag, fp->name);
329 				}
330 				break;
331 			case T_NODELIST:
332 				if (calcsize) {
333 					indent(12, cfile);
334 					fprintf(cfile, "sizenodelist(n->%s.%s, result);\n",
335 						sp->tag, fp->name);
336 				} else {
337 					indent(12, cfile);
338 					fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s, state);\n",
339 						sp->tag, fp->name, sp->tag, fp->name);
340 				}
341 				break;
342 			case T_STRING:
343 				if (calcsize) {
344 					indent(12, cfile);
345 					fprintf(cfile, "result->stringsize += strlen(n->%s.%s) + 1;\n",
346 						sp->tag, fp->name);
347 				} else {
348 					indent(12, cfile);
349 					fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s, state);\n",
350 						sp->tag, fp->name, sp->tag, fp->name);
351 				}
352 				break;
353 			case T_INT:
354 			case T_OTHER:
355 				if (! calcsize) {
356 					indent(12, cfile);
357 					fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
358 						sp->tag, fp->name, sp->tag, fp->name);
359 				}
360 				break;
361 			}
362 		}
363 		indent(12, cfile);
364 		fputs("break;\n", cfile);
365 	}
366 	fputs("      };\n", cfile);
367 	if (! calcsize)
368 		fputs("      new->type = n->type;\n", cfile);
369 }
370 
371 
372 static void
373 indent(int amount, FILE *fp)
374 {
375 	while (amount >= 8) {
376 		putc('\t', fp);
377 		amount -= 8;
378 	}
379 	while (--amount >= 0) {
380 		putc(' ', fp);
381 	}
382 }
383 
384 
385 static int
386 nextfield(char *buf)
387 {
388 	char *p, *q;
389 
390 	p = linep;
391 	while (*p == ' ' || *p == '\t')
392 		p++;
393 	q = buf;
394 	while (*p != ' ' && *p != '\t' && *p != '\0')
395 		*q++ = *p++;
396 	*q = '\0';
397 	linep = p;
398 	return (q > buf);
399 }
400 
401 
402 static void
403 skipbl(void)
404 {
405 	while (*linep == ' ' || *linep == '\t')
406 		linep++;
407 }
408 
409 
410 static int
411 readline(FILE *infp)
412 {
413 	char *p;
414 
415 	if (fgets(line, 1024, infp) == NULL)
416 		return 0;
417 	for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
418 	while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
419 		p--;
420 	*p = '\0';
421 	linep = line;
422 	linno++;
423 	if (p - line > BUFLEN)
424 		error("Line too long");
425 	return 1;
426 }
427 
428 
429 
430 static void
431 error(const char *msg, ...)
432 {
433 	va_list va;
434 	va_start(va, msg);
435 
436 	(void) fprintf(stderr, "line %d: ", linno);
437 	(void) vfprintf(stderr, msg, va);
438 	(void) fputc('\n', stderr);
439 
440 	va_end(va);
441 
442 	exit(2);
443 }
444 
445 
446 
447 static char *
448 savestr(const char *s)
449 {
450 	char *p;
451 
452 	if ((p = malloc(strlen(s) + 1)) == NULL)
453 		error("Out of space");
454 	(void) strcpy(p, s);
455 	return p;
456 }
457