1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1997-2005
5  *	Herbert Xu <herbert@gondor.apana.org.au>.  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 /*
36  * This program reads the nodetypes file and nodes.c.pat file.  It generates
37  * the files nodes.h and nodes.c.
38  */
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <stdarg.h>
44 
45 #define MAXTYPES 50		/* max number of node types */
46 #define MAXFIELDS 20		/* max fields in a structure */
47 #define BUFLEN 100		/* size of character buffers */
48 
49 /* field types */
50 #define T_NODE 1		/* union node *field */
51 #define T_NODELIST 2		/* struct nodelist *field */
52 #define T_STRING 3
53 #define T_INT 4			/* int field */
54 #define T_OTHER 5		/* other */
55 #define T_TEMP 6		/* don't copy this field */
56 
57 
58 struct field {			/* a structure field */
59 	char *name;		/* name of field */
60 	int type;			/* type of field */
61 	char *decl;		/* declaration of field */
62 };
63 
64 
65 struct str {			/* struct representing a node structure */
66 	char *tag;		/* structure tag */
67 	int nfields;		/* number of fields in the structure */
68 	struct field field[MAXFIELDS];	/* the fields of the structure */
69 	int done;			/* set if fully parsed */
70 };
71 
72 
73 static int ntypes;			/* number of node types */
74 static char *nodename[MAXTYPES];	/* names of the nodes */
75 static struct str *nodestr[MAXTYPES];	/* type of structure used by the node */
76 static int nstr;			/* number of structures */
77 static struct str str[MAXTYPES];	/* the structures */
78 static struct str *curstr;		/* current structure */
79 static FILE *infp;
80 static char line[1024];
81 static int linno;
82 static char *linep;
83 
84 static void parsenode(void);
85 static void parsefield(void);
86 static void output(char *);
87 static void outsizes(FILE *);
88 static void outfunc(FILE *, int);
89 static void outencode(FILE *);
90 static void outdecode(FILE *);
91 static void indent(int, FILE *);
92 static int nextfield(char *);
93 static void skipbl(void);
94 static int readline(void);
95 static void error(const char *, ...);
96 static char *savestr(const char *);
97 int main(int, char **);
98 
99 
100 int
main(int argc,char ** argv)101 main(int argc, char **argv)
102 {
103 
104 	/*
105 	 * some versions of linux complain: initializer element is not
106 	 * constant if this is done at compile time.
107 	 */
108 	infp = stdin;
109 
110 	if (argc != 3)
111 		error("usage: mknodes file");
112 	if ((infp = fopen(argv[1], "r")) == NULL)
113 		error("Can't open %s", argv[1]);
114 	while (readline()) {
115 		if (line[0] == ' ' || line[0] == '\t')
116 			parsefield();
117 		else if (line[0] != '\0')
118 			parsenode();
119 	}
120 	output(argv[2]);
121 	exit(0);
122 	/* NOTREACHED */
123 }
124 
125 
126 
127 static void
parsenode(void)128 parsenode(void)
129 {
130 	char name[BUFLEN];
131 	char tag[BUFLEN];
132 	struct str *sp;
133 
134 	if (curstr && curstr->nfields > 0)
135 		curstr->done = 1;
136 	nextfield(name);
137 	if (! nextfield(tag))
138 		error("Tag expected");
139 	if (*linep != '\0')
140 		error("Garbage at end of line");
141 	nodename[ntypes] = savestr(name);
142 	for (sp = str ; sp < str + nstr ; sp++) {
143 		if (strcmp(sp->tag, tag) == 0)
144 			break;
145 	}
146 	if (sp >= str + nstr) {
147 		sp->tag = savestr(tag);
148 		sp->nfields = 0;
149 		curstr = sp;
150 		nstr++;
151 	}
152 	nodestr[ntypes] = sp;
153 	ntypes++;
154 }
155 
156 
157 static void
parsefield(void)158 parsefield(void)
159 {
160 	char name[BUFLEN];
161 	char type[BUFLEN];
162 	char decl[2 * BUFLEN];
163 	struct field *fp;
164 
165 	if (curstr == NULL || curstr->done)
166 		error("No current structure to add field to");
167 	if (! nextfield(name))
168 		error("No field name");
169 	if (! nextfield(type))
170 		error("No field type");
171 	fp = &curstr->field[curstr->nfields];
172 	fp->name = savestr(name);
173 	if (strcmp(type, "nodeptr") == 0) {
174 		fp->type = T_NODE;
175 		sprintf(decl, "union node *%s", name);
176 	} else if (strcmp(type, "nodelist") == 0) {
177 		fp->type = T_NODELIST;
178 		sprintf(decl, "struct nodelist *%s", name);
179 	} else if (strcmp(type, "string") == 0) {
180 		fp->type = T_STRING;
181 		sprintf(decl, "char *%s", name);
182 	} else if (strcmp(type, "int") == 0) {
183 		fp->type = T_INT;
184 		sprintf(decl, "int %s", name);
185 	} else if (strcmp(type, "other") == 0) {
186 		fp->type = T_OTHER;
187 	} else if (strcmp(type, "temp") == 0) {
188 		fp->type = T_TEMP;
189 	} else {
190 		error("Unknown type %s", type);
191 	}
192 	if (fp->type == T_OTHER || fp->type == T_TEMP) {
193 		skipbl();
194 		fp->decl = savestr(linep);
195 	} else {
196 		if (*linep)
197 			error("Garbage at end of line");
198 		fp->decl = savestr(decl);
199 	}
200 	curstr->nfields++;
201 }
202 
203 
204 char writer[] = "\
205 /*\n\
206  * This file was generated by the mknodes program.\n\
207  */\n\
208 \n";
209 
210 static void
output(char * file)211 output(char *file)
212 {
213 	FILE *hfile;
214 	FILE *cfile;
215 	FILE *patfile;
216 	int i;
217 	struct str *sp;
218 	struct field *fp;
219 	char *p;
220 
221 	if ((patfile = fopen(file, "r")) == NULL)
222 		error("Can't open %s", file);
223 	if ((hfile = fopen("nodes.h", "w")) == NULL)
224 		error("Can't create nodes.h");
225 	if ((cfile = fopen("nodes.c", "w")) == NULL)
226 		error("Can't create nodes.c");
227 	fputs(writer, hfile);
228 	fputs("#include <zircon/types.h>\n\n", 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 funcnode {\n", hfile);
250 	fputs("\tint count;\n", hfile);
251 	fputs("\tunion node n;\n", hfile);
252 	fputs("};\n\n\n", hfile);
253 	fputs("struct funcnode *copyfunc(union node *);\n", hfile);
254 	fputs("void freefunc(struct funcnode *);\n", hfile);
255 	fputs("zx_status_t codec_encode(struct nodelist *nlist, zx_handle_t *vmo);\n", hfile);
256 	fputs("struct nodelist* codec_decode(char *buffer, size_t length);\n", hfile);
257 
258 	fputs(writer, cfile);
259 	while (fgets(line, sizeof line, patfile) != NULL) {
260 		for (p = line ; *p == ' ' || *p == '\t' ; p++);
261 		if (strcmp(p, "%SIZES\n") == 0)
262 			outsizes(cfile);
263 		else if (strcmp(p, "%CALCSIZE\n") == 0)
264 			outfunc(cfile, 1);
265 		else if (strcmp(p, "%COPY\n") == 0)
266 			outfunc(cfile, 0);
267 		else if (strcmp(p, "%ENCODE\n") == 0)
268 			outencode(cfile);
269 		else if (strcmp(p, "%DECODE\n") == 0)
270 			outdecode(cfile);
271 		else
272 			fputs(line, cfile);
273 	}
274 }
275 
276 
277 
278 static void
outsizes(FILE * cfile)279 outsizes(FILE *cfile)
280 {
281 	int i;
282 
283 	fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
284 	for (i = 0 ; i < ntypes ; i++) {
285 		fprintf(cfile, "      SHELL_ALIGN(sizeof (struct %s)),\n",
286 		    nodestr[i]->tag);
287 	}
288 	fprintf(cfile, "};\n");
289 }
290 
291 
292 static void
outfunc(FILE * cfile,int calcsize)293 outfunc(FILE *cfile, int calcsize)
294 {
295 	struct str *sp;
296 	struct field *fp;
297 	int i;
298 
299 	fputs("\tif (n == NULL)\n", cfile);
300 	if (calcsize)
301 		fputs("\t\treturn;\n", cfile);
302 	else
303 		fputs("\t\treturn NULL;\n", cfile);
304 	if (calcsize)
305 		fputs("\tfuncblocksize += nodesize[n->type];\n", cfile);
306 	else {
307 		fputs("\tnew = funcblock;\n", cfile);
308 		fputs("\tfuncblock = (char *) funcblock + nodesize[n->type];\n", cfile);
309 	}
310 	fputs("\tswitch (n->type) {\n", cfile);
311 	for (sp = str ; sp < &str[nstr] ; sp++) {
312 		for (i = 0 ; i < ntypes ; i++) {
313 			if (nodestr[i] == sp)
314 				fprintf(cfile, "\tcase %s:\n", nodename[i]);
315 		}
316 		for (i = sp->nfields ; --i >= 1 ; ) {
317 			fp = &sp->field[i];
318 			switch (fp->type) {
319 			case T_NODE:
320 				if (calcsize) {
321 					indent(16, cfile);
322 					fprintf(cfile, "calcsize(n->%s.%s);\n",
323 						sp->tag, fp->name);
324 				} else {
325 					indent(16, cfile);
326 					fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
327 						sp->tag, fp->name, sp->tag, fp->name);
328 				}
329 				break;
330 			case T_NODELIST:
331 				if (calcsize) {
332 					indent(16, cfile);
333 					fprintf(cfile, "sizenodelist(n->%s.%s);\n",
334 						sp->tag, fp->name);
335 				} else {
336 					indent(16, cfile);
337 					fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
338 						sp->tag, fp->name, sp->tag, fp->name);
339 				}
340 				break;
341 			case T_STRING:
342 				if (calcsize) {
343 					indent(16, cfile);
344 					fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
345 						sp->tag, fp->name);
346 				} else {
347 					indent(16, cfile);
348 					fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
349 						sp->tag, fp->name, sp->tag, fp->name);
350 				}
351 				break;
352 			case T_INT:
353 			case T_OTHER:
354 				if (! calcsize) {
355 					indent(16, cfile);
356 					fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
357 						sp->tag, fp->name, sp->tag, fp->name);
358 				}
359 				break;
360 			}
361 		}
362 		indent(16, cfile);
363 		fputs("break;\n", cfile);
364 	}
365 	fputs("\t};\n", cfile);
366 	if (! calcsize)
367 		fputs("\tnew->type = n->type;\n", cfile);
368 }
369 
370 
371 static void
outencode(FILE * cfile)372 outencode(FILE *cfile)
373 {
374 	struct str *sp;
375 	struct field *fp;
376 	int i;
377 
378 	fputs("\tif (n == NULL)\n", cfile);
379 	fputs("\t\treturn;\n", cfile);
380 	fputs("\tswitch (n->type) {\n", cfile);
381 	for (sp = str ; sp < &str[nstr] ; sp++) {
382 		for (i = 0 ; i < ntypes ; i++) {
383 			if (nodestr[i] == sp)
384 				fprintf(cfile, "\tcase %s:\n", nodename[i]);
385 		}
386 		indent(16, cfile);
387 		fprintf(cfile, "writenode(n, sizeof(struct %s), nodesize[n->type]);\n", sp->tag);
388 		for (i = sp->nfields ; --i >= 1 ; ) {
389 			fp = &sp->field[i];
390 			switch (fp->type) {
391 			case T_NODE:
392 				indent(16, cfile);
393 				fprintf(cfile, "encodenode(n->%s.%s);\n", sp->tag, fp->name);
394 				break;
395 			case T_NODELIST:
396 				indent(16, cfile);
397 				fprintf(cfile, "encodenodelist(n->%s.%s);\n", sp->tag, fp->name);
398 				break;
399 			case T_STRING:
400 				indent(16, cfile);
401 				fprintf(cfile, "encodestring(n->%s.%s);\n", sp->tag, fp->name);
402 				break;
403 			case T_INT:
404 			case T_OTHER:
405 				break;
406 			}
407 		}
408 		indent(16, cfile);
409 		fputs("break;\n", cfile);
410 	}
411 	fputs("\t};\n", cfile);
412 }
413 
414 
415 static void
outdecode(FILE * cfile)416 outdecode(FILE *cfile)
417 {
418 	struct str *sp;
419 	struct field *fp;
420 	int i;
421 
422 	fputs("\tif (*npp == NULL)\n", cfile);
423 	fputs("\t\treturn;\n", cfile);
424 	fputs("\t*npp = funcblock;\n", cfile);
425 	fputs("\tunion node *n = *npp;\n", cfile);
426 	fputs("\tfuncblock = (char *) funcblock + nodesize[n->type];\n", cfile);
427 	fputs("\tswitch (n->type) {\n", cfile);
428 	for (sp = str ; sp < &str[nstr] ; sp++) {
429 		for (i = 0 ; i < ntypes ; i++) {
430 			if (nodestr[i] == sp)
431 				fprintf(cfile, "\tcase %s:\n", nodename[i]);
432 		}
433 		for (i = sp->nfields ; --i >= 1 ; ) {
434 			fp = &sp->field[i];
435 			switch (fp->type) {
436 			case T_NODE:
437 				indent(16, cfile);
438 				fprintf(cfile, "decodenode(&n->%s.%s);\n", sp->tag, fp->name);
439 				break;
440 			case T_NODELIST:
441 				indent(16, cfile);
442 				fprintf(cfile, "decodenodelist(&n->%s.%s);\n", sp->tag, fp->name);
443 				break;
444 			case T_STRING:
445 				indent(16, cfile);
446 				fprintf(cfile, "n->%s.%s = decodestring();\n", sp->tag, fp->name);
447 				break;
448 			case T_INT:
449 			case T_OTHER:
450 				break;
451 			}
452 		}
453 		indent(16, cfile);
454 		fputs("break;\n", cfile);
455 	}
456 	fputs("\t};\n", cfile);
457 }
458 
459 
460 static void
indent(int amount,FILE * fp)461 indent(int amount, FILE *fp)
462 {
463 	while (amount >= 8) {
464 		putc('\t', fp);
465 		amount -= 8;
466 	}
467 	while (--amount >= 0) {
468 		putc(' ', fp);
469 	}
470 }
471 
472 
473 static int
nextfield(char * buf)474 nextfield(char *buf)
475 {
476 	char *p, *q;
477 
478 	p = linep;
479 	while (*p == ' ' || *p == '\t')
480 		p++;
481 	q = buf;
482 	while (*p != ' ' && *p != '\t' && *p != '\0')
483 		*q++ = *p++;
484 	*q = '\0';
485 	linep = p;
486 	return (q > buf);
487 }
488 
489 
490 static void
skipbl(void)491 skipbl(void)
492 {
493 	while (*linep == ' ' || *linep == '\t')
494 		linep++;
495 }
496 
497 
498 static int
readline(void)499 readline(void)
500 {
501 	char *p;
502 
503 	if (fgets(line, 1024, infp) == NULL)
504 		return 0;
505 	for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
506 	while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
507 		p--;
508 	*p = '\0';
509 	linep = line;
510 	linno++;
511 	if (p - line > BUFLEN)
512 		error("Line too long");
513 	return 1;
514 }
515 
516 
517 
518 static void
error(const char * msg,...)519 error(const char *msg, ...)
520 {
521 	va_list va;
522 
523 	va_start(va, msg);
524 
525 	(void) fprintf(stderr, "line %d: ", linno);
526 	(void) vfprintf(stderr, msg, va);
527 	(void) fputc('\n', stderr);
528 
529 	va_end(va);
530 
531 	exit(2);
532 	/* NOTREACHED */
533 }
534 
535 
536 
537 static char *
savestr(const char * s)538 savestr(const char *s)
539 {
540 	char *p;
541 
542 	if ((p = malloc(strlen(s) + 1)) == NULL)
543 		error("Out of space");
544 	(void) strcpy(p, s);
545 	return p;
546 }
547