mknodes.c (81602) | mknodes.c (90111) |
---|---|
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[] = | 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 $"; | 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> | 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> | 60#include <stdarg.h> |
62#else 63#include <varargs.h> 64#endif | |
65 | 61 |
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 | 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 |
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 *)); | 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 *); |
117 118 119int | 112 113 114int |
120main(argc, argv) 121 int argc; 122 char **argv; | 115main(int argc, 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 | 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 |
142parsenode() | 135parsenode(void) |
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 | 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 |
172parsefield() | 165parsefield(void) |
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 | 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 |
225output(file) 226 char *file; | 218output(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); | 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); |
263 fputs("#ifdef __STDC__\n", hfile); | |
264 fputs("union node *copyfunc(union node *);\n", hfile); 265 fputs("void freefunc(union node *);\n", hfile); | 255 fputs("union node *copyfunc(union node *);\n", hfile); 256 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 | 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 |
288outsizes(cfile) 289 FILE *cfile; | 275outsizes(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 | 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 |
302outfunc(cfile, calcsize) 303 FILE *cfile; 304 int calcsize; | 288outfunc(FILE *cfile, 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 | 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 |
383indent(amount, fp) 384 int amount; 385 FILE *fp; | 367indent(int amount, 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 | 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 |
398nextfield(buf) 399 char *buf; | 380nextfield(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 | 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 |
416skipbl() | 397skipbl(void) |
417{ 418 while (*linep == ' ' || *linep == '\t') 419 linep++; 420} 421 422 423static int | 398{ 399 while (*linep == ' ' || *linep == '\t') 400 linep++; 401} 402 403 404static int |
424readline() | 405readline(void) |
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 | 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 |
444#ifdef __STDC__ | |
445error(const char *msg, ...) | 425error(const char *msg, ...) |
446#else 447error(va_alist) 448 va_dcl 449#endif | |
450{ 451 va_list va; | 426{ 427 va_list va; |
452#ifdef __STDC__ | |
453 va_start(va, msg); | 428 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 * | 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 * |
472savestr(s) 473 const char *s; | 442savestr(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} | 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} |