Magellan Linux

Contents of /trunk/mkinitrd-magellan/klibc/usr/dash/mknodes.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (show annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 10887 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

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