mknodes.c revision 196483
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 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. 19 * 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. 31 */ 32 33#if 0 34#ifndef lint 35static char const copyright[] = 36"@(#) Copyright (c) 1991, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41static char sccsid[] = "@(#)mknodes.c 8.2 (Berkeley) 5/4/95"; 42#endif /* not lint */ 43#endif 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: head/bin/sh/mknodes.c 196483 2009-08-23 21:09:46Z jilles $"); 46 47/* 48 * This program reads the nodetypes file and nodes.c.pat file. It generates 49 * the files nodes.h and nodes.c. 50 */ 51 52#include <stdio.h> 53#include <stdlib.h> 54#include <string.h> 55#include <errno.h> 56#include <stdarg.h> 57 58#define MAXTYPES 50 /* max number of node types */ 59#define MAXFIELDS 20 /* max fields in a structure */ 60#define BUFLEN 100 /* size of character buffers */ 61 62/* field types */ 63#define T_NODE 1 /* union node *field */ 64#define T_NODELIST 2 /* struct nodelist *field */ 65#define T_STRING 3 66#define T_INT 4 /* int field */ 67#define T_OTHER 5 /* other */ 68#define T_TEMP 6 /* don't copy this field */ 69 70 71struct field { /* a structure field */ 72 char *name; /* name of field */ 73 int type; /* type of field */ 74 char *decl; /* declaration of field */ 75}; 76 77 78struct str { /* struct representing a node structure */ 79 char *tag; /* structure tag */ 80 int nfields; /* number of fields in the structure */ 81 struct field field[MAXFIELDS]; /* the fields of the structure */ 82 int done; /* set if fully parsed */ 83}; 84 85 86static int ntypes; /* number of node types */ 87static char *nodename[MAXTYPES]; /* names of the nodes */ 88static struct str *nodestr[MAXTYPES]; /* type of structure used by the node */ 89static int nstr; /* number of structures */ 90static struct str str[MAXTYPES]; /* the structures */ 91static struct str *curstr; /* current structure */ 92static FILE *infp; 93static char line[1024]; 94static int linno; 95static char *linep; 96 97static void parsenode(void); 98static void parsefield(void); 99static void output(char *); 100static void outsizes(FILE *); 101static void outfunc(FILE *, int); 102static void indent(int, FILE *); 103static int nextfield(char *); 104static void skipbl(void); 105static int readline(void); 106static void error(const char *, ...) __printf0like(1, 2) __dead2; 107static char *savestr(const char *); 108 109 110int 111main(int argc, char *argv[]) 112{ 113 if (argc != 3) 114 error("usage: mknodes file"); 115 infp = stdin; 116 if ((infp = fopen(argv[1], "r")) == NULL) 117 error("Can't open %s: %s", argv[1], strerror(errno)); 118 while (readline()) { 119 if (line[0] == ' ' || line[0] == '\t') 120 parsefield(); 121 else if (line[0] != '\0') 122 parsenode(); 123 } 124 output(argv[2]); 125 exit(0); 126} 127 128 129 130static void 131parsenode(void) 132{ 133 char name[BUFLEN]; 134 char tag[BUFLEN]; 135 struct str *sp; 136 137 if (curstr && curstr->nfields > 0) 138 curstr->done = 1; 139 nextfield(name); 140 if (! nextfield(tag)) 141 error("Tag expected"); 142 if (*linep != '\0') 143 error("Garbage at end of line"); 144 nodename[ntypes] = savestr(name); 145 for (sp = str ; sp < str + nstr ; sp++) { 146 if (strcmp(sp->tag, tag) == 0) 147 break; 148 } 149 if (sp >= str + nstr) { 150 sp->tag = savestr(tag); 151 sp->nfields = 0; 152 curstr = sp; 153 nstr++; 154 } 155 nodestr[ntypes] = sp; 156 ntypes++; 157} 158 159 160static void 161parsefield(void) 162{ 163 char name[BUFLEN]; 164 char type[BUFLEN]; 165 char decl[2 * BUFLEN]; 166 struct field *fp; 167 168 if (curstr == NULL || curstr->done) 169 error("No current structure to add field to"); 170 if (! nextfield(name)) 171 error("No field name"); 172 if (! nextfield(type)) 173 error("No field type"); 174 fp = &curstr->field[curstr->nfields]; 175 fp->name = savestr(name); 176 if (strcmp(type, "nodeptr") == 0) { 177 fp->type = T_NODE; 178 sprintf(decl, "union node *%s", name); 179 } else if (strcmp(type, "nodelist") == 0) { 180 fp->type = T_NODELIST; 181 sprintf(decl, "struct nodelist *%s", name); 182 } else if (strcmp(type, "string") == 0) { 183 fp->type = T_STRING; 184 sprintf(decl, "char *%s", name); 185 } else if (strcmp(type, "int") == 0) { 186 fp->type = T_INT; 187 sprintf(decl, "int %s", name); 188 } else if (strcmp(type, "other") == 0) { 189 fp->type = T_OTHER; 190 } else if (strcmp(type, "temp") == 0) { 191 fp->type = T_TEMP; 192 } else { 193 error("Unknown type %s", type); 194 } 195 if (fp->type == T_OTHER || fp->type == T_TEMP) { 196 skipbl(); 197 fp->decl = savestr(linep); 198 } else { 199 if (*linep) 200 error("Garbage at end of line"); 201 fp->decl = savestr(decl); 202 } 203 curstr->nfields++; 204} 205 206 207char writer[] = "\ 208/*\n\ 209 * This file was generated by the mknodes program.\n\ 210 */\n\ 211\n"; 212 213static void 214output(char *file) 215{ 216 FILE *hfile; 217 FILE *cfile; 218 FILE *patfile; 219 int i; 220 struct str *sp; 221 struct field *fp; 222 char *p; 223 224 if ((patfile = fopen(file, "r")) == NULL) 225 error("Can't open %s: %s", file, strerror(errno)); 226 if ((hfile = fopen("nodes.h", "w")) == NULL) 227 error("Can't create nodes.h: %s", strerror(errno)); 228 if ((cfile = fopen("nodes.c", "w")) == NULL) 229 error("Can't create nodes.c"); 230 fputs(writer, hfile); 231 for (i = 0 ; i < ntypes ; i++) 232 fprintf(hfile, "#define %s %d\n", nodename[i], i); 233 fputs("\n\n\n", hfile); 234 for (sp = str ; sp < &str[nstr] ; sp++) { 235 fprintf(hfile, "struct %s {\n", sp->tag); 236 for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) { 237 fprintf(hfile, " %s;\n", fp->decl); 238 } 239 fputs("};\n\n\n", hfile); 240 } 241 fputs("union node {\n", hfile); 242 fprintf(hfile, " int type;\n"); 243 for (sp = str ; sp < &str[nstr] ; sp++) { 244 fprintf(hfile, " struct %s %s;\n", sp->tag, sp->tag); 245 } 246 fputs("};\n\n\n", hfile); 247 fputs("struct nodelist {\n", hfile); 248 fputs("\tstruct nodelist *next;\n", hfile); 249 fputs("\tunion node *n;\n", hfile); 250 fputs("};\n\n\n", hfile); 251 fputs("struct funcdef {\n", hfile); 252 fputs("\tunsigned int refcount;\n", hfile); 253 fputs("\tunion node n;\n", hfile); 254 fputs("};\n\n\n", hfile); 255 fputs("struct funcdef *copyfunc(union node *);\n", hfile); 256 fputs("void reffunc(struct funcdef *);\n", hfile); 257 fputs("void unreffunc(struct funcdef *);\n", hfile); 258 259 fputs(writer, cfile); 260 while (fgets(line, sizeof line, patfile) != NULL) { 261 for (p = line ; *p == ' ' || *p == '\t' ; p++); 262 if (strcmp(p, "%SIZES\n") == 0) 263 outsizes(cfile); 264 else if (strcmp(p, "%CALCSIZE\n") == 0) 265 outfunc(cfile, 1); 266 else if (strcmp(p, "%COPY\n") == 0) 267 outfunc(cfile, 0); 268 else 269 fputs(line, cfile); 270 } 271} 272 273 274 275static void 276outsizes(FILE *cfile) 277{ 278 int i; 279 280 fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes); 281 for (i = 0 ; i < ntypes ; i++) { 282 fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag); 283 } 284 fprintf(cfile, "};\n"); 285} 286 287 288static void 289outfunc(FILE *cfile, int calcsize) 290{ 291 struct str *sp; 292 struct field *fp; 293 int i; 294 295 fputs(" if (n == NULL)\n", cfile); 296 if (calcsize) 297 fputs(" return;\n", cfile); 298 else 299 fputs(" return NULL;\n", cfile); 300 if (calcsize) 301 fputs(" funcblocksize += nodesize[n->type];\n", cfile); 302 else { 303 fputs(" new = funcblock;\n", cfile); 304 fputs(" funcblock = (char *)funcblock + nodesize[n->type];\n", cfile); 305 } 306 fputs(" switch (n->type) {\n", cfile); 307 for (sp = str ; sp < &str[nstr] ; sp++) { 308 for (i = 0 ; i < ntypes ; i++) { 309 if (nodestr[i] == sp) 310 fprintf(cfile, " case %s:\n", nodename[i]); 311 } 312 for (i = sp->nfields ; --i >= 1 ; ) { 313 fp = &sp->field[i]; 314 switch (fp->type) { 315 case T_NODE: 316 if (calcsize) { 317 indent(12, cfile); 318 fprintf(cfile, "calcsize(n->%s.%s);\n", 319 sp->tag, fp->name); 320 } else { 321 indent(12, cfile); 322 fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n", 323 sp->tag, fp->name, sp->tag, fp->name); 324 } 325 break; 326 case T_NODELIST: 327 if (calcsize) { 328 indent(12, cfile); 329 fprintf(cfile, "sizenodelist(n->%s.%s);\n", 330 sp->tag, fp->name); 331 } else { 332 indent(12, cfile); 333 fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n", 334 sp->tag, fp->name, sp->tag, fp->name); 335 } 336 break; 337 case T_STRING: 338 if (calcsize) { 339 indent(12, cfile); 340 fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n", 341 sp->tag, fp->name); 342 } else { 343 indent(12, cfile); 344 fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n", 345 sp->tag, fp->name, sp->tag, fp->name); 346 } 347 break; 348 case T_INT: 349 case T_OTHER: 350 if (! calcsize) { 351 indent(12, cfile); 352 fprintf(cfile, "new->%s.%s = n->%s.%s;\n", 353 sp->tag, fp->name, sp->tag, fp->name); 354 } 355 break; 356 } 357 } 358 indent(12, cfile); 359 fputs("break;\n", cfile); 360 } 361 fputs(" };\n", cfile); 362 if (! calcsize) 363 fputs(" new->type = n->type;\n", cfile); 364} 365 366 367static void 368indent(int amount, FILE *fp) 369{ 370 while (amount >= 8) { 371 putc('\t', fp); 372 amount -= 8; 373 } 374 while (--amount >= 0) { 375 putc(' ', fp); 376 } 377} 378 379 380static int 381nextfield(char *buf) 382{ 383 char *p, *q; 384 385 p = linep; 386 while (*p == ' ' || *p == '\t') 387 p++; 388 q = buf; 389 while (*p != ' ' && *p != '\t' && *p != '\0') 390 *q++ = *p++; 391 *q = '\0'; 392 linep = p; 393 return (q > buf); 394} 395 396 397static void 398skipbl(void) 399{ 400 while (*linep == ' ' || *linep == '\t') 401 linep++; 402} 403 404 405static int 406readline(void) 407{ 408 char *p; 409 410 if (fgets(line, 1024, infp) == NULL) 411 return 0; 412 for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++); 413 while (p > line && (p[-1] == ' ' || p[-1] == '\t')) 414 p--; 415 *p = '\0'; 416 linep = line; 417 linno++; 418 if (p - line > BUFLEN) 419 error("Line too long"); 420 return 1; 421} 422 423 424 425static void 426error(const char *msg, ...) 427{ 428 va_list va; 429 va_start(va, msg); 430 431 (void) fprintf(stderr, "line %d: ", linno); 432 (void) vfprintf(stderr, msg, va); 433 (void) fputc('\n', stderr); 434 435 va_end(va); 436 437 exit(2); 438} 439 440 441 442static char * 443savestr(const char *s) 444{ 445 char *p; 446 447 if ((p = malloc(strlen(s) + 1)) == NULL) 448 error("Out of space"); 449 (void) strcpy(p, s); 450 return p; 451} 452