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