1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Kenneth Almquist.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char const copyright[] =
"@(#) Copyright (c) 1991, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)mknodes.c 8.2 (Berkeley) 5/4/95";
#endif
static const char rcsid[] =
"$Id$";
#endif /* not lint */
/*
* This program reads the nodetypes file and nodes.c.pat file. It generates
* the files nodes.h and nodes.c.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#define MAXTYPES 50 /* max number of node types */
#define MAXFIELDS 20 /* max fields in a structure */
#define BUFLEN 100 /* size of character buffers */
/* field types */
#define T_NODE 1 /* union node *field */
#define T_NODELIST 2 /* struct nodelist *field */
#define T_STRING 3
#define T_INT 4 /* int field */
#define T_OTHER 5 /* other */
#define T_TEMP 6 /* don't copy this field */
struct field { /* a structure field */
char *name; /* name of field */
int type; /* type of field */
char *decl; /* declaration of field */
};
struct str { /* struct representing a node structure */
char *tag; /* structure tag */
int nfields; /* number of fields in the structure */
struct field field[MAXFIELDS]; /* the fields of the structure */
int done; /* set if fully parsed */
};
static int ntypes; /* number of node types */
static char *nodename[MAXTYPES]; /* names of the nodes */
static struct str *nodestr[MAXTYPES]; /* type of structure used by the node */
static int nstr; /* number of structures */
static struct str str[MAXTYPES]; /* the structures */
static struct str *curstr; /* current structure */
static FILE *infp = stdin;
static char line[1024];
static int linno;
static char *linep;
static void parsenode __P((void));
static void parsefield __P((void));
static void output __P((char *));
static void outsizes __P((FILE *));
static void outfunc __P((FILE *, int));
static void indent __P((int, FILE *));
static int nextfield __P((char *));
static void skipbl __P((void));
static int readline __P((void));
static void error __P((const char *, ...));
static char *savestr __P((const char *));
int
main(argc, argv)
int argc;
char **argv;
{
if (argc != 3)
error("usage: mknodes file");
if ((infp = fopen(argv[1], "r")) == NULL)
error("Can't open %s", argv[1]);
while (readline()) {
if (line[0] == ' ' || line[0] == '\t')
parsefield();
else if (line[0] != '\0')
parsenode();
}
output(argv[2]);
exit(0);
}
static void
parsenode()
{
char name[BUFLEN];
char tag[BUFLEN];
struct str *sp;
if (curstr && curstr->nfields > 0)
curstr->done = 1;
nextfield(name);
if (! nextfield(tag))
error("Tag expected");
if (*linep != '\0')
error("Garbage at end of line");
nodename[ntypes] = savestr(name);
for (sp = str ; sp < str + nstr ; sp++) {
if (strcmp(sp->tag, tag) == 0)
break;
}
if (sp >= str + nstr) {
sp->tag = savestr(tag);
sp->nfields = 0;
curstr = sp;
nstr++;
}
nodestr[ntypes] = sp;
ntypes++;
}
static void
parsefield()
{
char name[BUFLEN];
char type[BUFLEN];
char decl[2 * BUFLEN];
struct field *fp;
if (curstr == NULL || curstr->done)
error("No current structure to add field to");
if (! nextfield(name))
error("No field name");
if (! nextfield(type))
error("No field type");
fp = &curstr->field[curstr->nfields];
fp->name = savestr(name);
if (strcmp(type, "nodeptr") == 0) {
fp->type = T_NODE;
sprintf(decl, "union node *%s", name);
} else if (strcmp(type, "nodelist") == 0) {
fp->type = T_NODELIST;
sprintf(decl, "struct nodelist *%s", name);
} else if (strcmp(type, "string") == 0) {
fp->type = T_STRING;
sprintf(decl, "char *%s", name);
} else if (strcmp(type, "int") == 0) {
fp->type = T_INT;
sprintf(decl, "int %s", name);
} else if (strcmp(type, "other") == 0) {
fp->type = T_OTHER;
} else if (strcmp(type, "temp") == 0) {
fp->type = T_TEMP;
} else {
error("Unknown type %s", type);
}
if (fp->type == T_OTHER || fp->type == T_TEMP) {
skipbl();
fp->decl = savestr(linep);
} else {
if (*linep)
error("Garbage at end of line");
fp->decl = savestr(decl);
}
curstr->nfields++;
}
char writer[] = "\
/*\n\
* This file was generated by the mknodes program.\n\
*/\n\
\n";
static void
output(file)
char *file;
{
FILE *hfile;
FILE *cfile;
FILE *patfile;
int i;
struct str *sp;
struct field *fp;
char *p;
if ((patfile = fopen(file, "r")) == NULL)
error("Can't open %s", file);
if ((hfile = fopen("nodes.h", "w")) == NULL)
error("Can't create nodes.h");
if ((cfile = fopen("nodes.c", "w")) == NULL)
error("Can't create nodes.c");
fputs(writer, hfile);
for (i = 0 ; i < ntypes ; i++)
fprintf(hfile, "#define %s %d\n", nodename[i], i);
fputs("\n\n\n", hfile);
for (sp = str ; sp < &str[nstr] ; sp++) {
fprintf(hfile, "struct %s {\n", sp->tag);
for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
fprintf(hfile, " %s;\n", fp->decl);
}
fputs("};\n\n\n", hfile);
}
fputs("union node {\n", hfile);
fprintf(hfile, " int type;\n");
for (sp = str ; sp < &str[nstr] ; sp++) {
fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag);
}
fputs("};\n\n\n", hfile);
fputs("struct nodelist {\n", hfile);
fputs("\tstruct nodelist *next;\n", hfile);
fputs("\tunion node *n;\n", hfile);
fputs("};\n\n\n", hfile);
fputs("#ifdef __STDC__\n", hfile);
fputs("union node *copyfunc(union node *);\n", hfile);
fputs("void freefunc(union node *);\n", hfile);
fputs("#else\n", hfile);
fputs("union node *copyfunc();\n", hfile);
fputs("void freefunc();\n", hfile);
fputs("#endif\n", hfile);
fputs(writer, cfile);
while (fgets(line, sizeof line, patfile) != NULL) {
for (p = line ; *p == ' ' || *p == '\t' ; p++);
if (strcmp(p, "%SIZES\n") == 0)
outsizes(cfile);
else if (strcmp(p, "%CALCSIZE\n") == 0)
outfunc(cfile, 1);
else if (strcmp(p, "%COPY\n") == 0)
outfunc(cfile, 0);
else
fputs(line, cfile);
}
}
static void
outsizes(cfile)
FILE *cfile;
{
int i;
fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
for (i = 0 ; i < ntypes ; i++) {
fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
}
fprintf(cfile, "};\n");
}
static void
outfunc(cfile, calcsize)
FILE *cfile;
int calcsize;
{
struct str *sp;
struct field *fp;
int i;
fputs(" if (n == NULL)\n", cfile);
if (calcsize)
fputs(" return;\n", cfile);
else
fputs(" return NULL;\n", cfile);
if (calcsize)
fputs(" funcblocksize += nodesize[n->type];\n", cfile);
else {
fputs(" new = funcblock;\n", cfile);
fputs(" funcblock = (char *)funcblock + nodesize[n->type];\n", cfile);
}
fputs(" switch (n->type) {\n", cfile);
for (sp = str ; sp < &str[nstr] ; sp++) {
for (i = 0 ; i < ntypes ; i++) {
if (nodestr[i] == sp)
fprintf(cfile, " case %s:\n", nodename[i]);
}
for (i = sp->nfields ; --i >= 1 ; ) {
fp = &sp->field[i];
switch (fp->type) {
case T_NODE:
if (calcsize) {
indent(12, cfile);
fprintf(cfile, "calcsize(n->%s.%s);\n",
sp->tag, fp->name);
} else {
indent(12, cfile);
fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
sp->tag, fp->name, sp->tag, fp->name);
}
break;
case T_NODELIST:
if (calcsize) {
indent(12, cfile);
fprintf(cfile, "sizenodelist(n->%s.%s);\n",
sp->tag, fp->name);
} else {
indent(12, cfile);
fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
sp->tag, fp->name, sp->tag, fp->name);
}
break;
case T_STRING:
if (calcsize) {
indent(12, cfile);
fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
sp->tag, fp->name);
} else {
indent(12, cfile);
fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
sp->tag, fp->name, sp->tag, fp->name);
}
break;
case T_INT:
case T_OTHER:
if (! calcsize) {
indent(12, cfile);
fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
sp->tag, fp->name, sp->tag, fp->name);
}
break;
}
}
indent(12, cfile);
fputs("break;\n", cfile);
}
fputs(" };\n", cfile);
if (! calcsize)
fputs(" new->type = n->type;\n", cfile);
}
static void
indent(amount, fp)
int amount;
FILE *fp;
{
while (amount >= 8) {
putc('\t', fp);
amount -= 8;
}
while (--amount >= 0) {
putc(' ', fp);
}
}
static int
nextfield(buf)
char *buf;
{
char *p, *q;
p = linep;
while (*p == ' ' || *p == '\t')
p++;
q = buf;
while (*p != ' ' && *p != '\t' && *p != '\0')
*q++ = *p++;
*q = '\0';
linep = p;
return (q > buf);
}
static void
skipbl()
{
while (*linep == ' ' || *linep == '\t')
linep++;
}
static int
readline()
{
char *p;
if (fgets(line, 1024, infp) == NULL)
return 0;
for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
p--;
*p = '\0';
linep = line;
linno++;
if (p - line > BUFLEN)
error("Line too long");
return 1;
}
static void
#ifdef __STDC__
error(const char *msg, ...)
#else
error(va_alist)
va_dcl
#endif
{
va_list va;
#ifdef __STDC__
va_start(va, msg);
#else
char *msg;
va_start(va);
msg = va_arg(va, char *);
#endif
(void) fprintf(stderr, "line %d: ", linno);
(void) vfprintf(stderr, msg, va);
(void) fputc('\n', stderr);
va_end(va);
exit(2);
}
static char *
savestr(s)
const char *s;
{
char *p;
if ((p = malloc(strlen(s) + 1)) == NULL)
error("Out of space");
(void) strcpy(p, s);
return p;
}
|