* Copyright (c) 1991 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* %sccs.include.redist.c%
"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)mknodes.c 5.1 (Berkeley) %G%";
* This program reads the nodetypes file and nodes.c.pat file. It generates
* the files nodes.h and nodes.c.
#define MAXTYPES 50 /* max number of node types */
#define MAXFIELDS 20 /* max fields in a structure */
#define BUFLEN 100 /* size of character buffers */
#define T_NODE 1 /* union node *field */
#define T_NODELIST 2 /* struct nodelist *field */
#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 */
int ntypes
; /* number of node types */
char *nodename
[MAXTYPES
]; /* names of the nodes */
struct str
*nodestr
[MAXTYPES
]; /* type of structure used by the node */
int nstr
; /* number of structures */
struct str str
[MAXTYPES
]; /* the structures */
struct str
*curstr
; /* current structure */
#define equal(s1, s2) (strcmp(s1, s2) == 0)
error("usage: mknodes file\n");
if ((infp
= fopen(argv
[1], "r")) == NULL
)
error("Can't open %s", argv
[1]);
if (line
[0] == ' ' || line
[0] == '\t')
else if (line
[0] != '\0')
if (curstr
&& curstr
->nfields
> 0)
error("Garbage at end of line");
nodename
[ntypes
] = savestr(name
);
for (sp
= str
; sp
< str
+ nstr
; sp
++) {
if (curstr
== NULL
|| curstr
->done
)
error("No current structure to add field to");
fp
= &curstr
->field
[curstr
->nfields
];
fp
->name
= savestr(name
);
if (equal(type
, "nodeptr")) {
sprintf(decl
, "union node *%s", name
);
} else if (equal(type
, "nodelist")) {
sprintf(decl
, "struct nodelist *%s", name
);
} else if (equal(type
, "string")) {
sprintf(decl
, "char *%s", name
);
} else if (equal(type
, "int")) {
sprintf(decl
, "int %s", name
);
} else if (equal(type
, "other")) {
} else if (equal(type
, "temp")) {
error("Unknown type %s", type
);
if (fp
->type
== T_OTHER
|| fp
->type
== T_TEMP
) {
fp
->decl
= savestr(linep
);
error("Garbage at end of line");
fp
->decl
= savestr(decl
);
* This file was generated by the mknodes program.\n\
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");
for (i
= 0 ; i
< ntypes
; i
++)
fprintf(hfile
, "#define %s %d\n", nodename
[i
], i
);
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("union node *copyfunc();\n", hfile
);
fputs("void freefunc();\n", hfile
);
fputs("#endif\n", hfile
);
while (fgets(line
, sizeof line
, patfile
) != NULL
) {
for (p
= line
; *p
== ' ' || *p
== '\t' ; p
++);
if (equal(p
, "%SIZES\n"))
else if (equal(p
, "%CALCSIZE\n"))
else if (equal(p
, "%COPY\n"))
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
);
fputs(" if (n == NULL)\n", cfile
);
fputs(" return;\n", cfile
);
fputs(" return NULL;\n", cfile
);
fputs(" funcblocksize += nodesize[n->type];\n", cfile
);
fputs(" new = funcblock;\n", cfile
);
fputs(" 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
++) {
fprintf(cfile
, " case %s:\n", nodename
[i
]);
for (i
= sp
->nfields
; --i
>= 1 ; ) {
fprintf(cfile
, "calcsize(n->%s.%s);\n",
fprintf(cfile
, "new->%s.%s = copynode(n->%s.%s);\n",
sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
fprintf(cfile
, "sizenodelist(n->%s.%s);\n",
fprintf(cfile
, "new->%s.%s = copynodelist(n->%s.%s);\n",
sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
fprintf(cfile
, "funcstringsize += strlen(n->%s.%s) + 1;\n",
fprintf(cfile
, "new->%s.%s = nodesavestr(n->%s.%s);\n",
sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
fprintf(cfile
, "new->%s.%s = n->%s.%s;\n",
sp
->tag
, fp
->name
, sp
->tag
, fp
->name
);
fputs("break;\n", cfile
);
fputs(" new->type = n->type;\n", cfile
);
while (*p
== ' ' || *p
== '\t')
while (*p
!= ' ' && *p
!= '\t' && *p
!= '\0')
while (*linep
== ' ' || *linep
== '\t')
if (fgets(line
, 1024, infp
) == NULL
)
for (p
= line
; *p
!= '#' && *p
!= '\n' && *p
!= '\0' ; p
++);
while (p
> line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
error(msg
, a1
, a2
, a3
, a4
, a5
, a6
)
fprintf(stderr
, "line %d: ", linno
);
fprintf(stderr
, msg
, a1
, a2
, a3
, a4
, a5
, a6
);
if ((p
= malloc(strlen(s
) + 1)) == NULL
)