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