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