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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved. 37 * @(#)mknodes.c 8.2 (Berkeley) 5/4/95 38 * $FreeBSD: src/bin/sh/mknodes.c,v 1.11.2.3 2002/07/19 04:38:51 tjr Exp $ 39 * $DragonFly: src/bin/sh/mknodes.c,v 1.3 2004/11/07 20:54:52 eirikn Exp $ 40 */ 41 42 /* 43 * This program reads the nodetypes file and nodes.c.pat file. It generates 44 * the files nodes.h and nodes.c. 45 */ 46 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <errno.h> 51 #include <stdarg.h> 52 53 #define MAXTYPES 50 /* max number of node types */ 54 #define MAXFIELDS 20 /* max fields in a structure */ 55 #define BUFLEN 100 /* size of character buffers */ 56 57 /* field types */ 58 #define T_NODE 1 /* union node *field */ 59 #define T_NODELIST 2 /* struct nodelist *field */ 60 #define T_STRING 3 61 #define T_INT 4 /* int field */ 62 #define T_OTHER 5 /* other */ 63 #define T_TEMP 6 /* don't copy this field */ 64 65 66 struct field { /* a structure field */ 67 char *name; /* name of field */ 68 int type; /* type of field */ 69 char *decl; /* declaration of field */ 70 }; 71 72 73 struct str { /* struct representing a node structure */ 74 char *tag; /* structure tag */ 75 int nfields; /* number of fields in the structure */ 76 struct field field[MAXFIELDS]; /* the fields of the structure */ 77 int done; /* set if fully parsed */ 78 }; 79 80 81 static int ntypes; /* number of node types */ 82 static char *nodename[MAXTYPES]; /* names of the nodes */ 83 static struct str *nodestr[MAXTYPES]; /* type of structure used by the node */ 84 static int nstr; /* number of structures */ 85 static struct str str[MAXTYPES]; /* the structures */ 86 static struct str *curstr; /* current structure */ 87 static FILE *infp; 88 static char line[1024]; 89 static int linno; 90 static char *linep; 91 92 static void parsenode(void); 93 static void parsefield(void); 94 static void output(char *); 95 static void outsizes(FILE *); 96 static void outfunc(FILE *, int); 97 static void indent(int, FILE *); 98 static int nextfield(char *); 99 static void skipbl(void); 100 static int readline(void); 101 static void error(const char *, ...) __printf0like(1, 2); 102 static char *savestr(const char *); 103 104 105 int 106 main(int argc, char *argv[]) 107 { 108 if (argc != 3) 109 error("usage: mknodes file"); 110 infp = stdin; 111 if ((infp = fopen(argv[1], "r")) == NULL) 112 error("Can't open %s: %s", argv[1], strerror(errno)); 113 while (readline()) { 114 if (line[0] == ' ' || line[0] == '\t') 115 parsefield(); 116 else if (line[0] != '\0') 117 parsenode(); 118 } 119 output(argv[2]); 120 exit(0); 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: %s", file, strerror(errno)); 221 if ((hfile = fopen("nodes.h", "w")) == NULL) 222 error("Can't create nodes.h: %s", strerror(errno)); 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("union node *copyfunc(union node *);\n", hfile); 247 fputs("void freefunc(union node *);\n", hfile); 248 249 fputs(writer, cfile); 250 while (fgets(line, sizeof line, patfile) != NULL) { 251 for (p = line ; *p == ' ' || *p == '\t' ; p++); 252 if (strcmp(p, "%SIZES\n") == 0) 253 outsizes(cfile); 254 else if (strcmp(p, "%CALCSIZE\n") == 0) 255 outfunc(cfile, 1); 256 else if (strcmp(p, "%COPY\n") == 0) 257 outfunc(cfile, 0); 258 else 259 fputs(line, cfile); 260 } 261 } 262 263 264 265 static void 266 outsizes(FILE *cfile) 267 { 268 int i; 269 270 fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes); 271 for (i = 0 ; i < ntypes ; i++) { 272 fprintf(cfile, " ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag); 273 } 274 fprintf(cfile, "};\n"); 275 } 276 277 278 static void 279 outfunc(FILE *cfile, int calcsize) 280 { 281 struct str *sp; 282 struct field *fp; 283 int i; 284 285 fputs(" if (n == NULL)\n", cfile); 286 if (calcsize) 287 fputs(" return;\n", cfile); 288 else 289 fputs(" return NULL;\n", cfile); 290 if (calcsize) 291 fputs(" funcblocksize += nodesize[n->type];\n", cfile); 292 else { 293 fputs(" new = funcblock;\n", cfile); 294 fputs(" funcblock = (char *)funcblock + nodesize[n->type];\n", cfile); 295 } 296 fputs(" switch (n->type) {\n", cfile); 297 for (sp = str ; sp < &str[nstr] ; sp++) { 298 for (i = 0 ; i < ntypes ; i++) { 299 if (nodestr[i] == sp) 300 fprintf(cfile, " case %s:\n", nodename[i]); 301 } 302 for (i = sp->nfields ; --i >= 1 ; ) { 303 fp = &sp->field[i]; 304 switch (fp->type) { 305 case T_NODE: 306 if (calcsize) { 307 indent(12, cfile); 308 fprintf(cfile, "calcsize(n->%s.%s);\n", 309 sp->tag, fp->name); 310 } else { 311 indent(12, cfile); 312 fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n", 313 sp->tag, fp->name, sp->tag, fp->name); 314 } 315 break; 316 case T_NODELIST: 317 if (calcsize) { 318 indent(12, cfile); 319 fprintf(cfile, "sizenodelist(n->%s.%s);\n", 320 sp->tag, fp->name); 321 } else { 322 indent(12, cfile); 323 fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n", 324 sp->tag, fp->name, sp->tag, fp->name); 325 } 326 break; 327 case T_STRING: 328 if (calcsize) { 329 indent(12, cfile); 330 fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n", 331 sp->tag, fp->name); 332 } else { 333 indent(12, cfile); 334 fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n", 335 sp->tag, fp->name, sp->tag, fp->name); 336 } 337 break; 338 case T_INT: 339 case T_OTHER: 340 if (! calcsize) { 341 indent(12, cfile); 342 fprintf(cfile, "new->%s.%s = n->%s.%s;\n", 343 sp->tag, fp->name, sp->tag, fp->name); 344 } 345 break; 346 } 347 } 348 indent(12, cfile); 349 fputs("break;\n", cfile); 350 } 351 fputs(" };\n", cfile); 352 if (! calcsize) 353 fputs(" new->type = n->type;\n", cfile); 354 } 355 356 357 static void 358 indent(int amount, FILE *fp) 359 { 360 while (amount >= 8) { 361 putc('\t', fp); 362 amount -= 8; 363 } 364 while (--amount >= 0) { 365 putc(' ', fp); 366 } 367 } 368 369 370 static int 371 nextfield(char *buf) 372 { 373 char *p, *q; 374 375 p = linep; 376 while (*p == ' ' || *p == '\t') 377 p++; 378 q = buf; 379 while (*p != ' ' && *p != '\t' && *p != '\0') 380 *q++ = *p++; 381 *q = '\0'; 382 linep = p; 383 return (q > buf); 384 } 385 386 387 static void 388 skipbl(void) 389 { 390 while (*linep == ' ' || *linep == '\t') 391 linep++; 392 } 393 394 395 static int 396 readline(void) 397 { 398 char *p; 399 400 if (fgets(line, 1024, infp) == NULL) 401 return 0; 402 for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++); 403 while (p > line && (p[-1] == ' ' || p[-1] == '\t')) 404 p--; 405 *p = '\0'; 406 linep = line; 407 linno++; 408 if (p - line > BUFLEN) 409 error("Line too long"); 410 return 1; 411 } 412 413 414 415 static void 416 error(const char *msg, ...) 417 { 418 va_list va; 419 va_start(va, msg); 420 421 fprintf(stderr, "line %d: ", linno); 422 vfprintf(stderr, msg, va); 423 fputc('\n', stderr); 424 425 va_end(va); 426 427 exit(2); 428 } 429 430 431 432 static char * 433 savestr(const char *s) 434 { 435 char *p; 436 437 if ((p = malloc(strlen(s) + 1)) == NULL) 438 error("Out of space"); 439 strcpy(p, s); 440 return p; 441 } 442