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