Deleted Added
sdiff udiff text old ( 81602 ) new ( 90111 )
full compact
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

--- 31 unchanged lines hidden (view full) ---

40 The Regents of the University of California. All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)mknodes.c 8.2 (Berkeley) 5/4/95";
46#endif
47static const char rcsid[] =
48 "$FreeBSD: head/bin/sh/mknodes.c 81602 2001-08-13 21:55:04Z peter $";
49#endif /* not lint */
50
51/*
52 * This program reads the nodetypes file and nodes.c.pat file. It generates
53 * the files nodes.h and nodes.c.
54 */
55
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <errno.h>
60#ifdef __STDC__
61#include <stdarg.h>
62#else
63#include <varargs.h>
64#endif
65
66
67#define MAXTYPES 50 /* max number of node types */
68#define MAXFIELDS 20 /* max fields in a structure */
69#define BUFLEN 100 /* size of character buffers */
70
71/* field types */
72#define T_NODE 1 /* union node *field */
73#define T_NODELIST 2 /* struct nodelist *field */
74#define T_STRING 3

--- 23 unchanged lines hidden (view full) ---

98static int nstr; /* number of structures */
99static struct str str[MAXTYPES]; /* the structures */
100static struct str *curstr; /* current structure */
101static FILE *infp;
102static char line[1024];
103static int linno;
104static char *linep;
105
106static void parsenode __P((void));
107static void parsefield __P((void));
108static void output __P((char *));
109static void outsizes __P((FILE *));
110static void outfunc __P((FILE *, int));
111static void indent __P((int, FILE *));
112static int nextfield __P((char *));
113static void skipbl __P((void));
114static int readline __P((void));
115static void error __P((const char *, ...)) __printf0like(1, 2);
116static char *savestr __P((const char *));
117
118
119int
120main(argc, argv)
121 int argc;
122 char **argv;
123{
124 if (argc != 3)
125 error("usage: mknodes file");
126 infp = stdin;
127 if ((infp = fopen(argv[1], "r")) == NULL)
128 error("Can't open %s: %s", argv[1], strerror(errno));
129 while (readline()) {
130 if (line[0] == ' ' || line[0] == '\t')
131 parsefield();
132 else if (line[0] != '\0')
133 parsenode();
134 }
135 output(argv[2]);
136 exit(0);
137}
138
139
140
141static void
142parsenode()
143{
144 char name[BUFLEN];
145 char tag[BUFLEN];
146 struct str *sp;
147
148 if (curstr && curstr->nfields > 0)
149 curstr->done = 1;
150 nextfield(name);

--- 13 unchanged lines hidden (view full) ---

164 nstr++;
165 }
166 nodestr[ntypes] = sp;
167 ntypes++;
168}
169
170
171static void
172parsefield()
173{
174 char name[BUFLEN];
175 char type[BUFLEN];
176 char decl[2 * BUFLEN];
177 struct field *fp;
178
179 if (curstr == NULL || curstr->done)
180 error("No current structure to add field to");

--- 36 unchanged lines hidden (view full) ---

217
218char writer[] = "\
219/*\n\
220 * This file was generated by the mknodes program.\n\
221 */\n\
222\n";
223
224static void
225output(file)
226 char *file;
227{
228 FILE *hfile;
229 FILE *cfile;
230 FILE *patfile;
231 int i;
232 struct str *sp;
233 struct field *fp;
234 char *p;

--- 20 unchanged lines hidden (view full) ---

255 for (sp = str ; sp < &str[nstr] ; sp++) {
256 fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag);
257 }
258 fputs("};\n\n\n", hfile);
259 fputs("struct nodelist {\n", hfile);
260 fputs("\tstruct nodelist *next;\n", hfile);
261 fputs("\tunion node *n;\n", hfile);
262 fputs("};\n\n\n", hfile);
263 fputs("#ifdef __STDC__\n", hfile);
264 fputs("union node *copyfunc(union node *);\n", hfile);
265 fputs("void freefunc(union node *);\n", hfile);
266 fputs("#else\n", hfile);
267 fputs("union node *copyfunc();\n", hfile);
268 fputs("void freefunc();\n", hfile);
269 fputs("#endif\n", hfile);
270
271 fputs(writer, cfile);
272 while (fgets(line, sizeof line, patfile) != NULL) {
273 for (p = line ; *p == ' ' || *p == '\t' ; p++);
274 if (strcmp(p, "%SIZES\n") == 0)
275 outsizes(cfile);
276 else if (strcmp(p, "%CALCSIZE\n") == 0)
277 outfunc(cfile, 1);
278 else if (strcmp(p, "%COPY\n") == 0)
279 outfunc(cfile, 0);
280 else
281 fputs(line, cfile);
282 }
283}
284
285
286
287static void
288outsizes(cfile)
289 FILE *cfile;
290{
291 int i;
292
293 fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
294 for (i = 0 ; i < ntypes ; i++) {
295 fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
296 }
297 fprintf(cfile, "};\n");
298}
299
300
301static void
302outfunc(cfile, calcsize)
303 FILE *cfile;
304 int calcsize;
305{
306 struct str *sp;
307 struct field *fp;
308 int i;
309
310 fputs(" if (n == NULL)\n", cfile);
311 if (calcsize)
312 fputs(" return;\n", cfile);

--- 62 unchanged lines hidden (view full) ---

375 }
376 fputs(" };\n", cfile);
377 if (! calcsize)
378 fputs(" new->type = n->type;\n", cfile);
379}
380
381
382static void
383indent(amount, fp)
384 int amount;
385 FILE *fp;
386{
387 while (amount >= 8) {
388 putc('\t', fp);
389 amount -= 8;
390 }
391 while (--amount >= 0) {
392 putc(' ', fp);
393 }
394}
395
396
397static int
398nextfield(buf)
399 char *buf;
400{
401 char *p, *q;
402
403 p = linep;
404 while (*p == ' ' || *p == '\t')
405 p++;
406 q = buf;
407 while (*p != ' ' && *p != '\t' && *p != '\0')
408 *q++ = *p++;
409 *q = '\0';
410 linep = p;
411 return (q > buf);
412}
413
414
415static void
416skipbl()
417{
418 while (*linep == ' ' || *linep == '\t')
419 linep++;
420}
421
422
423static int
424readline()
425{
426 char *p;
427
428 if (fgets(line, 1024, infp) == NULL)
429 return 0;
430 for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
431 while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
432 p--;
433 *p = '\0';
434 linep = line;
435 linno++;
436 if (p - line > BUFLEN)
437 error("Line too long");
438 return 1;
439}
440
441
442
443static void
444#ifdef __STDC__
445error(const char *msg, ...)
446#else
447error(va_alist)
448 va_dcl
449#endif
450{
451 va_list va;
452#ifdef __STDC__
453 va_start(va, msg);
454#else
455 const char *msg;
456 va_start(va);
457 msg = va_arg(va, char *);
458#endif
459
460 (void) fprintf(stderr, "line %d: ", linno);
461 (void) vfprintf(stderr, msg, va);
462 (void) fputc('\n', stderr);
463
464 va_end(va);
465
466 exit(2);
467}
468
469
470
471static char *
472savestr(s)
473 const char *s;
474{
475 char *p;
476
477 if ((p = malloc(strlen(s) + 1)) == NULL)
478 error("Out of space");
479 (void) strcpy(p, s);
480 return p;
481}