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 90111 2002-02-02 06:50:57Z imp $";
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#include <stdarg.h>
61
62#define MAXTYPES 50 /* max number of node types */
63#define MAXFIELDS 20 /* max fields in a structure */
64#define BUFLEN 100 /* size of character buffers */
65
66/* field types */
67#define T_NODE 1 /* union node *field */
68#define T_NODELIST 2 /* struct nodelist *field */
69#define T_STRING 3

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

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

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

157 nstr++;
158 }
159 nodestr[ntypes] = sp;
160 ntypes++;
161}
162
163
164static void
165parsefield(void)
166{
167 char name[BUFLEN];
168 char type[BUFLEN];
169 char decl[2 * BUFLEN];
170 struct field *fp;
171
172 if (curstr == NULL || curstr->done)
173 error("No current structure to add field to");

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

210
211char writer[] = "\
212/*\n\
213 * This file was generated by the mknodes program.\n\
214 */\n\
215\n";
216
217static void
218output(char *file)
219{
220 FILE *hfile;
221 FILE *cfile;
222 FILE *patfile;
223 int i;
224 struct str *sp;
225 struct field *fp;
226 char *p;

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

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

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

359 }
360 fputs(" };\n", cfile);
361 if (! calcsize)
362 fputs(" new->type = n->type;\n", cfile);
363}
364
365
366static void
367indent(int amount, FILE *fp)
368{
369 while (amount >= 8) {
370 putc('\t', fp);
371 amount -= 8;
372 }
373 while (--amount >= 0) {
374 putc(' ', fp);
375 }
376}
377
378
379static int
380nextfield(char *buf)
381{
382 char *p, *q;
383
384 p = linep;
385 while (*p == ' ' || *p == '\t')
386 p++;
387 q = buf;
388 while (*p != ' ' && *p != '\t' && *p != '\0')
389 *q++ = *p++;
390 *q = '\0';
391 linep = p;
392 return (q > buf);
393}
394
395
396static void
397skipbl(void)
398{
399 while (*linep == ' ' || *linep == '\t')
400 linep++;
401}
402
403
404static int
405readline(void)
406{
407 char *p;
408
409 if (fgets(line, 1024, infp) == NULL)
410 return 0;
411 for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
412 while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
413 p--;
414 *p = '\0';
415 linep = line;
416 linno++;
417 if (p - line > BUFLEN)
418 error("Line too long");
419 return 1;
420}
421
422
423
424static void
425error(const char *msg, ...)
426{
427 va_list va;
428 va_start(va, msg);
429
430 (void) fprintf(stderr, "line %d: ", linno);
431 (void) vfprintf(stderr, msg, va);
432 (void) fputc('\n', stderr);
433
434 va_end(va);
435
436 exit(2);
437}
438
439
440
441static char *
442savestr(const char *s)
443{
444 char *p;
445
446 if ((p = malloc(strlen(s) + 1)) == NULL)
447 error("Out of space");
448 (void) strcpy(p, s);
449 return p;
450}