unstack - fix ipcvecs
[minix.git] / commands / ash / mknodes.c
blobd9582d010431128baa7287b0a7bfde09d4239804
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
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.
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.
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 */
40 #ifndef lint
41 static char sccsid[] = "@(#)mknodes.c 8.2 (Berkeley) 5/4/95";
42 #endif /* not lint */
43 #endif
45 __FBSDID("$FreeBSD: src/bin/sh/mknodes.c,v 1.17 2004/04/06 20:06:51 markm Exp $");
49 * This program reads the nodetypes file and nodes.c.pat file. It generates
50 * the files nodes.h and nodes.c.
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <errno.h>
57 #include <stdarg.h>
59 #define MAXTYPES 50 /* max number of node types */
60 #define MAXFIELDS 20 /* max fields in a structure */
61 #define BUFLEN 100 /* size of character buffers */
63 /* field types */
64 #define T_NODE 1 /* union node *field */
65 #define T_NODELIST 2 /* struct nodelist *field */
66 #define T_STRING 3
67 #define T_INT 4 /* int field */
68 #define T_OTHER 5 /* other */
69 #define T_TEMP 6 /* don't copy this field */
72 struct field { /* a structure field */
73 char *name; /* name of field */
74 int type; /* type of field */
75 char *decl; /* declaration of field */
79 struct str { /* struct representing a node structure */
80 char *tag; /* structure tag */
81 int nfields; /* number of fields in the structure */
82 struct field field[MAXFIELDS]; /* the fields of the structure */
83 int done; /* set if fully parsed */
87 static int ntypes; /* number of node types */
88 static char *nodename[MAXTYPES]; /* names of the nodes */
89 static struct str *nodestr[MAXTYPES]; /* type of structure used by the node */
90 static int nstr; /* number of structures */
91 static struct str str[MAXTYPES]; /* the structures */
92 static struct str *curstr; /* current structure */
93 static FILE *infp;
94 static char line[1024];
95 static int linno;
96 static char *linep;
98 #ifndef __printf0like
99 #define __printf0like(a,b)
100 #endif
102 static void parsenode(void);
103 static void parsefield(void);
104 static void output(char *);
105 static void outsizes(FILE *);
106 static void outfunc(FILE *, int);
107 static void indent(int, FILE *);
108 static int nextfield(char *);
109 static void skipbl(void);
110 static int readline(void);
111 static void error(const char *, ...) __printf0like(1, 2);
112 static char *savestr(const char *);
116 main(int argc, char *argv[])
118 if (argc != 3)
119 error("usage: mknodes file");
120 infp = stdin;
121 if ((infp = fopen(argv[1], "r")) == NULL)
122 error("Can't open %s: %s", argv[1], strerror(errno));
123 while (readline()) {
124 if (line[0] == ' ' || line[0] == '\t')
125 parsefield();
126 else if (line[0] != '\0')
127 parsenode();
129 output(argv[2]);
130 exit(0);
135 static void
136 parsenode(void)
138 char name[BUFLEN];
139 char tag[BUFLEN];
140 struct str *sp;
142 if (curstr && curstr->nfields > 0)
143 curstr->done = 1;
144 nextfield(name);
145 if (! nextfield(tag))
146 error("Tag expected");
147 if (*linep != '\0')
148 error("Garbage at end of line");
149 nodename[ntypes] = savestr(name);
150 for (sp = str ; sp < str + nstr ; sp++) {
151 if (strcmp(sp->tag, tag) == 0)
152 break;
154 if (sp >= str + nstr) {
155 sp->tag = savestr(tag);
156 sp->nfields = 0;
157 curstr = sp;
158 nstr++;
160 nodestr[ntypes] = sp;
161 ntypes++;
165 static void
166 parsefield(void)
168 char name[BUFLEN];
169 char type[BUFLEN];
170 char decl[2 * BUFLEN];
171 struct field *fp;
173 if (curstr == NULL || curstr->done)
174 error("No current structure to add field to");
175 if (! nextfield(name))
176 error("No field name");
177 if (! nextfield(type))
178 error("No field type");
179 fp = &curstr->field[curstr->nfields];
180 fp->name = savestr(name);
181 if (strcmp(type, "nodeptr") == 0) {
182 fp->type = T_NODE;
183 sprintf(decl, "union node *%s", name);
184 } else if (strcmp(type, "nodelist") == 0) {
185 fp->type = T_NODELIST;
186 sprintf(decl, "struct nodelist *%s", name);
187 } else if (strcmp(type, "string") == 0) {
188 fp->type = T_STRING;
189 sprintf(decl, "char *%s", name);
190 } else if (strcmp(type, "int") == 0) {
191 fp->type = T_INT;
192 sprintf(decl, "int %s", name);
193 } else if (strcmp(type, "other") == 0) {
194 fp->type = T_OTHER;
195 } else if (strcmp(type, "temp") == 0) {
196 fp->type = T_TEMP;
197 } else {
198 error("Unknown type %s", type);
200 if (fp->type == T_OTHER || fp->type == T_TEMP) {
201 skipbl();
202 fp->decl = savestr(linep);
203 } else {
204 if (*linep)
205 error("Garbage at end of line");
206 fp->decl = savestr(decl);
208 curstr->nfields++;
212 char writer[] = "\
213 /*\n\
214 * This file was generated by the mknodes program.\n\
215 */\n\
216 \n";
218 static void
219 output(char *file)
221 FILE *hfile;
222 FILE *cfile;
223 FILE *patfile;
224 int i;
225 struct str *sp;
226 struct field *fp;
227 char *p;
229 if ((patfile = fopen(file, "r")) == NULL)
230 error("Can't open %s: %s", file, strerror(errno));
231 if ((hfile = fopen("nodes.h", "w")) == NULL)
232 error("Can't create nodes.h: %s", strerror(errno));
233 if ((cfile = fopen("nodes.c", "w")) == NULL)
234 error("Can't create nodes.c");
235 fputs(writer, hfile);
236 for (i = 0 ; i < ntypes ; i++)
237 fprintf(hfile, "#define %s %d\n", nodename[i], i);
238 fputs("\n\n\n", hfile);
239 for (sp = str ; sp < &str[nstr] ; sp++) {
240 fprintf(hfile, "struct %s {\n", sp->tag);
241 for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
242 fprintf(hfile, " %s;\n", fp->decl);
244 fputs("};\n\n\n", hfile);
246 fputs("union node {\n", hfile);
247 fprintf(hfile, " int type;\n");
248 for (sp = str ; sp < &str[nstr] ; sp++) {
249 fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag);
251 fputs("};\n\n\n", hfile);
252 fputs("struct nodelist {\n", hfile);
253 fputs("\tstruct nodelist *next;\n", hfile);
254 fputs("\tunion node *n;\n", hfile);
255 fputs("};\n\n\n", hfile);
256 fputs("union node *copyfunc(union node *);\n", hfile);
257 fputs("void freefunc(union node *);\n", hfile);
259 fputs(writer, cfile);
260 while (fgets(line, sizeof line, patfile) != NULL) {
261 for (p = line ; *p == ' ' || *p == '\t' ; p++);
262 if (strcmp(p, "%SIZES\n") == 0)
263 outsizes(cfile);
264 else if (strcmp(p, "%CALCSIZE\n") == 0)
265 outfunc(cfile, 1);
266 else if (strcmp(p, "%COPY\n") == 0)
267 outfunc(cfile, 0);
268 else
269 fputs(line, cfile);
275 static void
276 outsizes(FILE *cfile)
278 int i;
280 fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
281 for (i = 0 ; i < ntypes ; i++) {
282 fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
284 fprintf(cfile, "};\n");
288 static void
289 outfunc(FILE *cfile, int calcsize)
291 struct str *sp;
292 struct field *fp;
293 int i;
295 fputs(" if (n == NULL)\n", cfile);
296 if (calcsize)
297 fputs(" return;\n", cfile);
298 else
299 fputs(" return NULL;\n", cfile);
300 if (calcsize)
301 fputs(" funcblocksize += nodesize[n->type];\n", cfile);
302 else {
303 fputs(" new = funcblock;\n", cfile);
304 fputs(" funcblock = (char *)funcblock + nodesize[n->type];\n", cfile);
306 fputs(" switch (n->type) {\n", cfile);
307 for (sp = str ; sp < &str[nstr] ; sp++) {
308 for (i = 0 ; i < ntypes ; i++) {
309 if (nodestr[i] == sp)
310 fprintf(cfile, " case %s:\n", nodename[i]);
312 for (i = sp->nfields ; --i >= 1 ; ) {
313 fp = &sp->field[i];
314 switch (fp->type) {
315 case T_NODE:
316 if (calcsize) {
317 indent(12, cfile);
318 fprintf(cfile, "calcsize(n->%s.%s);\n",
319 sp->tag, fp->name);
320 } else {
321 indent(12, cfile);
322 fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
323 sp->tag, fp->name, sp->tag, fp->name);
325 break;
326 case T_NODELIST:
327 if (calcsize) {
328 indent(12, cfile);
329 fprintf(cfile, "sizenodelist(n->%s.%s);\n",
330 sp->tag, fp->name);
331 } else {
332 indent(12, cfile);
333 fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
334 sp->tag, fp->name, sp->tag, fp->name);
336 break;
337 case T_STRING:
338 if (calcsize) {
339 indent(12, cfile);
340 fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
341 sp->tag, fp->name);
342 } else {
343 indent(12, cfile);
344 fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
345 sp->tag, fp->name, sp->tag, fp->name);
347 break;
348 case T_INT:
349 case T_OTHER:
350 if (! calcsize) {
351 indent(12, cfile);
352 fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
353 sp->tag, fp->name, sp->tag, fp->name);
355 break;
358 indent(12, cfile);
359 fputs("break;\n", cfile);
361 fputs(" };\n", cfile);
362 if (! calcsize)
363 fputs(" new->type = n->type;\n", cfile);
367 static void
368 indent(int amount, FILE *fp)
370 while (amount >= 8) {
371 putc('\t', fp);
372 amount -= 8;
374 while (--amount >= 0) {
375 putc(' ', fp);
380 static int
381 nextfield(char *buf)
383 char *p, *q;
385 p = linep;
386 while (*p == ' ' || *p == '\t')
387 p++;
388 q = buf;
389 while (*p != ' ' && *p != '\t' && *p != '\0')
390 *q++ = *p++;
391 *q = '\0';
392 linep = p;
393 return (q > buf);
397 static void
398 skipbl(void)
400 while (*linep == ' ' || *linep == '\t')
401 linep++;
405 static int
406 readline(void)
408 char *p;
410 if (fgets(line, 1024, infp) == NULL)
411 return 0;
412 for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
413 while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
414 p--;
415 *p = '\0';
416 linep = line;
417 linno++;
418 if (p - line > BUFLEN)
419 error("Line too long");
420 return 1;
425 static void
426 error(const char *msg, ...)
428 va_list va;
429 va_start(va, msg);
431 (void) fprintf(stderr, "line %d: ", linno);
432 (void) vfprintf(stderr, msg, va);
433 (void) fputc('\n', stderr);
435 va_end(va);
437 exit(2);
442 static char *
443 savestr(const char *s)
445 char *p;
447 if ((p = malloc(strlen(s) + 1)) == NULL)
448 error("Out of space");
449 (void) strcpy(p, s);
450 return p;
454 * $PchId: mknodes.c,v 1.6 2006/05/23 12:05:14 philip Exp $