xref: /freebsd/bin/sh/mknodes.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
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);
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 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("union node *copyfunc(union node *);\n", hfile);
252 	fputs("void freefunc(union node *);\n", hfile);
253 
254 	fputs(writer, cfile);
255 	while (fgets(line, sizeof line, patfile) != NULL) {
256 		for (p = line ; *p == ' ' || *p == '\t' ; p++);
257 		if (strcmp(p, "%SIZES\n") == 0)
258 			outsizes(cfile);
259 		else if (strcmp(p, "%CALCSIZE\n") == 0)
260 			outfunc(cfile, 1);
261 		else if (strcmp(p, "%COPY\n") == 0)
262 			outfunc(cfile, 0);
263 		else
264 			fputs(line, cfile);
265 	}
266 }
267 
268 
269 
270 static void
271 outsizes(FILE *cfile)
272 {
273 	int i;
274 
275 	fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
276 	for (i = 0 ; i < ntypes ; i++) {
277 		fprintf(cfile, "      ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
278 	}
279 	fprintf(cfile, "};\n");
280 }
281 
282 
283 static void
284 outfunc(FILE *cfile, int calcsize)
285 {
286 	struct str *sp;
287 	struct field *fp;
288 	int i;
289 
290 	fputs("      if (n == NULL)\n", cfile);
291 	if (calcsize)
292 		fputs("	    return;\n", cfile);
293 	else
294 		fputs("	    return NULL;\n", cfile);
295 	if (calcsize)
296 		fputs("      funcblocksize += nodesize[n->type];\n", cfile);
297 	else {
298 		fputs("      new = funcblock;\n", cfile);
299 		fputs("      funcblock = (char *)funcblock + nodesize[n->type];\n", cfile);
300 	}
301 	fputs("      switch (n->type) {\n", cfile);
302 	for (sp = str ; sp < &str[nstr] ; sp++) {
303 		for (i = 0 ; i < ntypes ; i++) {
304 			if (nodestr[i] == sp)
305 				fprintf(cfile, "      case %s:\n", nodename[i]);
306 		}
307 		for (i = sp->nfields ; --i >= 1 ; ) {
308 			fp = &sp->field[i];
309 			switch (fp->type) {
310 			case T_NODE:
311 				if (calcsize) {
312 					indent(12, cfile);
313 					fprintf(cfile, "calcsize(n->%s.%s);\n",
314 						sp->tag, fp->name);
315 				} else {
316 					indent(12, cfile);
317 					fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
318 						sp->tag, fp->name, sp->tag, fp->name);
319 				}
320 				break;
321 			case T_NODELIST:
322 				if (calcsize) {
323 					indent(12, cfile);
324 					fprintf(cfile, "sizenodelist(n->%s.%s);\n",
325 						sp->tag, fp->name);
326 				} else {
327 					indent(12, cfile);
328 					fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
329 						sp->tag, fp->name, sp->tag, fp->name);
330 				}
331 				break;
332 			case T_STRING:
333 				if (calcsize) {
334 					indent(12, cfile);
335 					fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
336 						sp->tag, fp->name);
337 				} else {
338 					indent(12, cfile);
339 					fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
340 						sp->tag, fp->name, sp->tag, fp->name);
341 				}
342 				break;
343 			case T_INT:
344 			case T_OTHER:
345 				if (! calcsize) {
346 					indent(12, cfile);
347 					fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
348 						sp->tag, fp->name, sp->tag, fp->name);
349 				}
350 				break;
351 			}
352 		}
353 		indent(12, cfile);
354 		fputs("break;\n", cfile);
355 	}
356 	fputs("      };\n", cfile);
357 	if (! calcsize)
358 		fputs("      new->type = n->type;\n", cfile);
359 }
360 
361 
362 static void
363 indent(int amount, FILE *fp)
364 {
365 	while (amount >= 8) {
366 		putc('\t', fp);
367 		amount -= 8;
368 	}
369 	while (--amount >= 0) {
370 		putc(' ', fp);
371 	}
372 }
373 
374 
375 static int
376 nextfield(char *buf)
377 {
378 	char *p, *q;
379 
380 	p = linep;
381 	while (*p == ' ' || *p == '\t')
382 		p++;
383 	q = buf;
384 	while (*p != ' ' && *p != '\t' && *p != '\0')
385 		*q++ = *p++;
386 	*q = '\0';
387 	linep = p;
388 	return (q > buf);
389 }
390 
391 
392 static void
393 skipbl(void)
394 {
395 	while (*linep == ' ' || *linep == '\t')
396 		linep++;
397 }
398 
399 
400 static int
401 readline(void)
402 {
403 	char *p;
404 
405 	if (fgets(line, 1024, infp) == NULL)
406 		return 0;
407 	for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
408 	while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
409 		p--;
410 	*p = '\0';
411 	linep = line;
412 	linno++;
413 	if (p - line > BUFLEN)
414 		error("Line too long");
415 	return 1;
416 }
417 
418 
419 
420 static void
421 error(const char *msg, ...)
422 {
423 	va_list va;
424 	va_start(va, msg);
425 
426 	(void) fprintf(stderr, "line %d: ", linno);
427 	(void) vfprintf(stderr, msg, va);
428 	(void) fputc('\n', stderr);
429 
430 	va_end(va);
431 
432 	exit(2);
433 }
434 
435 
436 
437 static char *
438 savestr(const char *s)
439 {
440 	char *p;
441 
442 	if ((p = malloc(strlen(s) + 1)) == NULL)
443 		error("Out of space");
444 	(void) strcpy(p, s);
445 	return p;
446 }
447