Added more options and flags files per suggestions from -hackers.
[unix-history] / bin / sh / mknodes.c
CommitLineData
15637ed4
RG
1/*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * 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
37#ifndef lint
38char copyright[] =
39"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
40 All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
68de1ca3
AM
44/*static char sccsid[] = "from: @(#)mknodes.c 5.1 (Berkeley) 3/7/91";*/
45static char rcsid[] = "mknodes.c,v 1.4 1993/08/01 18:58:08 mycroft Exp";
15637ed4
RG
46#endif /* not lint */
47
48/*
49 * This program reads the nodetypes file and nodes.c.pat file. It generates
50 * the files nodes.h and nodes.c.
51 */
52
53#include <stdio.h>
54
55
56#define MAXTYPES 50 /* max number of node types */
57#define MAXFIELDS 20 /* max fields in a structure */
58#define BUFLEN 100 /* size of character buffers */
59
60/* field types */
61#define T_NODE 1 /* union node *field */
62#define T_NODELIST 2 /* struct nodelist *field */
63#define T_STRING 3
64#define T_INT 4 /* int field */
65#define T_OTHER 5 /* other */
66#define T_TEMP 6 /* don't copy this field */
67
68
69struct field { /* a structure field */
70 char *name; /* name of field */
71 int type; /* type of field */
72 char *decl; /* declaration of field */
73};
74
75
76struct str { /* struct representing a node structure */
77 char *tag; /* structure tag */
78 int nfields; /* number of fields in the structure */
79 struct field field[MAXFIELDS]; /* the fields of the structure */
80 int done; /* set if fully parsed */
81};
82
83
84int ntypes; /* number of node types */
85char *nodename[MAXTYPES]; /* names of the nodes */
86struct str *nodestr[MAXTYPES]; /* type of structure used by the node */
87int nstr; /* number of structures */
88struct str str[MAXTYPES]; /* the structures */
89struct str *curstr; /* current structure */
90
91
92FILE *infp = stdin;
93char line[1024];
94int linno;
95char *linep;
96
97
98char *savestr();
99#define equal(s1, s2) (strcmp(s1, s2) == 0)
100
101
102main(argc, argv)
103 char **argv;
104 {
105 if (argc != 3)
106 error("usage: mknodes file\n");
107 if ((infp = fopen(argv[1], "r")) == NULL)
108 error("Can't open %s", argv[1]);
109 while (readline()) {
110 if (line[0] == ' ' || line[0] == '\t')
111 parsefield();
112 else if (line[0] != '\0')
113 parsenode();
114 }
115 output(argv[2]);
fc31ae9d 116 return 0;
15637ed4
RG
117}
118
119
120
121parsenode() {
122 char name[BUFLEN];
123 char tag[BUFLEN];
124 struct str *sp;
125
126 if (curstr && curstr->nfields > 0)
127 curstr->done = 1;
128 nextfield(name);
129 if (! nextfield(tag))
130 error("Tag expected");
131 if (*linep != '\0')
132 error("Garbage at end of line");
133 nodename[ntypes] = savestr(name);
134 for (sp = str ; sp < str + nstr ; sp++) {
135 if (equal(sp->tag, tag))
136 break;
137 }
138 if (sp >= str + nstr) {
139 sp->tag = savestr(tag);
140 sp->nfields = 0;
141 curstr = sp;
142 nstr++;
143 }
144 nodestr[ntypes] = sp;
145 ntypes++;
146}
147
148
149parsefield() {
150 char name[BUFLEN];
151 char type[BUFLEN];
152 char decl[2 * BUFLEN];
153 struct field *fp;
154
155 if (curstr == NULL || curstr->done)
156 error("No current structure to add field to");
157 if (! nextfield(name))
158 error("No field name");
159 if (! nextfield(type))
160 error("No field type");
161 fp = &curstr->field[curstr->nfields];
162 fp->name = savestr(name);
163 if (equal(type, "nodeptr")) {
164 fp->type = T_NODE;
165 sprintf(decl, "union node *%s", name);
166 } else if (equal(type, "nodelist")) {
167 fp->type = T_NODELIST;
168 sprintf(decl, "struct nodelist *%s", name);
169 } else if (equal(type, "string")) {
170 fp->type = T_STRING;
171 sprintf(decl, "char *%s", name);
172 } else if (equal(type, "int")) {
173 fp->type = T_INT;
174 sprintf(decl, "int %s", name);
175 } else if (equal(type, "other")) {
176 fp->type = T_OTHER;
177 } else if (equal(type, "temp")) {
178 fp->type = T_TEMP;
179 } else {
180 error("Unknown type %s", type);
181 }
182 if (fp->type == T_OTHER || fp->type == T_TEMP) {
183 skipbl();
184 fp->decl = savestr(linep);
185 } else {
186 if (*linep)
187 error("Garbage at end of line");
188 fp->decl = savestr(decl);
189 }
190 curstr->nfields++;
191}
192
193
194char writer[] = "\
195/*\n\
196 * This file was generated by the mknodes program.\n\
197 */\n\
198\n";
199
200output(file)
201 char *file;
202 {
203 FILE *hfile;
204 FILE *cfile;
205 FILE *patfile;
206 int i;
207 struct str *sp;
208 struct field *fp;
209 char *p;
210
211 if ((patfile = fopen(file, "r")) == NULL)
212 error("Can't open %s", file);
213 if ((hfile = fopen("nodes.h", "w")) == NULL)
214 error("Can't create nodes.h");
215 if ((cfile = fopen("nodes.c", "w")) == NULL)
216 error("Can't create nodes.c");
217 fputs(writer, hfile);
218 for (i = 0 ; i < ntypes ; i++)
219 fprintf(hfile, "#define %s %d\n", nodename[i], i);
220 fputs("\n\n\n", hfile);
221 for (sp = str ; sp < &str[nstr] ; sp++) {
222 fprintf(hfile, "struct %s {\n", sp->tag);
223 for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
224 fprintf(hfile, " %s;\n", fp->decl);
225 }
226 fputs("};\n\n\n", hfile);
227 }
228 fputs("union node {\n", hfile);
229 fprintf(hfile, " int type;\n");
230 for (sp = str ; sp < &str[nstr] ; sp++) {
231 fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag);
232 }
233 fputs("};\n\n\n", hfile);
234 fputs("struct nodelist {\n", hfile);
235 fputs("\tstruct nodelist *next;\n", hfile);
236 fputs("\tunion node *n;\n", hfile);
237 fputs("};\n\n\n", hfile);
238 fputs("#ifdef __STDC__\n", hfile);
239 fputs("union node *copyfunc(union node *);\n", hfile);
240 fputs("void freefunc(union node *);\n", hfile);
241 fputs("#else\n", hfile);
242 fputs("union node *copyfunc();\n", hfile);
243 fputs("void freefunc();\n", hfile);
244 fputs("#endif\n", hfile);
245
246 fputs(writer, cfile);
247 while (fgets(line, sizeof line, patfile) != NULL) {
248 for (p = line ; *p == ' ' || *p == '\t' ; p++);
249 if (equal(p, "%SIZES\n"))
250 outsizes(cfile);
251 else if (equal(p, "%CALCSIZE\n"))
252 outfunc(cfile, 1);
253 else if (equal(p, "%COPY\n"))
254 outfunc(cfile, 0);
255 else
256 fputs(line, cfile);
257 }
258}
259
260
261
262outsizes(cfile)
263 FILE *cfile;
264 {
265 int i;
266
267 fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
268 for (i = 0 ; i < ntypes ; i++) {
269 fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
270 }
271 fprintf(cfile, "};\n");
272}
273
274
275outfunc(cfile, calcsize)
276 FILE *cfile;
277 {
278 struct str *sp;
279 struct field *fp;
280 int i;
281
282 fputs(" if (n == NULL)\n", cfile);
283 if (calcsize)
284 fputs(" return;\n", cfile);
285 else
286 fputs(" return NULL;\n", cfile);
287 if (calcsize)
288 fputs(" funcblocksize += nodesize[n->type];\n", cfile);
289 else {
290 fputs(" new = funcblock;\n", cfile);
68de1ca3 291 fputs(" funcblock += nodesize[n->type];\n", cfile);
15637ed4
RG
292 }
293 fputs(" switch (n->type) {\n", cfile);
294 for (sp = str ; sp < &str[nstr] ; sp++) {
295 for (i = 0 ; i < ntypes ; i++) {
296 if (nodestr[i] == sp)
297 fprintf(cfile, " case %s:\n", nodename[i]);
298 }
299 for (i = sp->nfields ; --i >= 1 ; ) {
300 fp = &sp->field[i];
301 switch (fp->type) {
302 case T_NODE:
303 if (calcsize) {
304 indent(12, cfile);
305 fprintf(cfile, "calcsize(n->%s.%s);\n",
306 sp->tag, fp->name);
307 } else {
308 indent(12, cfile);
309 fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
310 sp->tag, fp->name, sp->tag, fp->name);
311 }
312 break;
313 case T_NODELIST:
314 if (calcsize) {
315 indent(12, cfile);
316 fprintf(cfile, "sizenodelist(n->%s.%s);\n",
317 sp->tag, fp->name);
318 } else {
319 indent(12, cfile);
320 fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
321 sp->tag, fp->name, sp->tag, fp->name);
322 }
323 break;
324 case T_STRING:
325 if (calcsize) {
326 indent(12, cfile);
327 fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
328 sp->tag, fp->name);
329 } else {
330 indent(12, cfile);
331 fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
332 sp->tag, fp->name, sp->tag, fp->name);
333 }
334 break;
335 case T_INT:
336 case T_OTHER:
337 if (! calcsize) {
338 indent(12, cfile);
339 fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
340 sp->tag, fp->name, sp->tag, fp->name);
341 }
342 break;
343 }
344 }
345 indent(12, cfile);
346 fputs("break;\n", cfile);
347 }
348 fputs(" };\n", cfile);
349 if (! calcsize)
350 fputs(" new->type = n->type;\n", cfile);
351}
352
353
354indent(amount, fp)
355 FILE *fp;
356 {
357 while (amount >= 8) {
358 putc('\t', fp);
359 amount -= 8;
360 }
361 while (--amount >= 0) {
362 putc(' ', fp);
363 }
364}
365
366
367int
368nextfield(buf)
369 char *buf;
370 {
371 register char *p, *q;
372
373 p = linep;
374 while (*p == ' ' || *p == '\t')
375 p++;
376 q = buf;
377 while (*p != ' ' && *p != '\t' && *p != '\0')
378 *q++ = *p++;
379 *q = '\0';
380 linep = p;
381 return (q > buf);
382}
383
384
385skipbl() {
386 while (*linep == ' ' || *linep == '\t')
387 linep++;
388}
389
390
391int
392readline() {
393 register char *p;
394
395 if (fgets(line, 1024, infp) == NULL)
396 return 0;
397 for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
398 while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
399 p--;
400 *p = '\0';
401 linep = line;
402 linno++;
403 if (p - line > BUFLEN)
404 error("Line too long");
405 return 1;
406}
407
408
409
410error(msg, a1, a2, a3, a4, a5, a6)
411 char *msg;
412 {
413 fprintf(stderr, "line %d: ", linno);
414 fprintf(stderr, msg, a1, a2, a3, a4, a5, a6);
415 putc('\n', stderr);
416 exit(2);
417}
418
419
420
421char *
422savestr(s)
423 char *s;
424 {
425 register char *p;
426 char *malloc();
427
428 if ((p = malloc(strlen(s) + 1)) == NULL)
429 error("Out of space");
430 strcpy(p, s);
431 return p;
432}