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