1 %{ 2 /* 3 * Copyright (c) 2004-2006 Kungliga Tekniska H�gskolan 4 * (Royal Institute of Technology, Stockholm, Sweden). 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * 3. Neither the name of the Institute nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifdef HAVE_CONFIG_H 36 #include <config.h> 37 RCSID("$Id: slc-gram.y 20767 2007-06-01 11:24:52Z lha $"); 38 #endif 39 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <err.h> 43 #include <ctype.h> 44 #include <limits.h> 45 #include <getarg.h> 46 #include <vers.h> 47 #include <roken.h> 48 49 #include "slc.h" 50 extern FILE *yyin; 51 extern struct assignment *assignment; 52 extern int yyparse(void); 53 %} 54 55 %union { 56 char *string; 57 struct assignment *assignment; 58 } 59 60 %token <string> LITERAL 61 %token <string> STRING 62 %type <assignment> assignment assignments 63 64 %start start 65 66 %% 67 68 start : assignments 69 { 70 assignment = $1; 71 } 72 ; 73 74 assignments : assignment assignments 75 { 76 $1->next = $2; 77 $$ = $1; 78 } 79 | assignment 80 ; 81 82 assignment : LITERAL '=' STRING 83 { 84 $$ = malloc(sizeof(*$$)); 85 $$->name = $1; 86 $$->type = a_value; 87 $$->lineno = lineno; 88 $$->u.value = $3; 89 $$->next = NULL; 90 } 91 | LITERAL '=' '{' assignments '}' 92 { 93 $$ = malloc(sizeof(*$$)); 94 $$->name = $1; 95 $$->type = a_assignment; 96 $$->lineno = lineno; 97 $$->u.assignment = $4; 98 $$->next = NULL; 99 } 100 ; 101 102 %% 103 char *filename; 104 FILE *cfile, *hfile; 105 int error_flag; 106 struct assignment *assignment; 107 108 109 static void 110 ex(struct assignment *a, const char *fmt, ...) 111 { 112 va_list ap; 113 fprintf(stderr, "%s:%d: ", a->name, a->lineno); 114 va_start(ap, fmt); 115 vfprintf(stderr, fmt, ap); 116 va_end(ap); 117 fprintf(stderr, "\n"); 118 } 119 120 121 122 static int 123 check_option(struct assignment *as) 124 { 125 struct assignment *a; 126 int seen_long = 0; 127 int seen_short = 0; 128 int seen_type = 0; 129 int seen_argument = 0; 130 int seen_help = 0; 131 int seen_default = 0; 132 int ret = 0; 133 134 for(a = as; a != NULL; a = a->next) { 135 if(strcmp(a->name, "long") == 0) 136 seen_long++; 137 else if(strcmp(a->name, "short") == 0) 138 seen_short++; 139 else if(strcmp(a->name, "type") == 0) 140 seen_type++; 141 else if(strcmp(a->name, "argument") == 0) 142 seen_argument++; 143 else if(strcmp(a->name, "help") == 0) 144 seen_help++; 145 else if(strcmp(a->name, "default") == 0) 146 seen_default++; 147 else { 148 ex(a, "unknown name"); 149 ret++; 150 } 151 } 152 if(seen_long == 0 && seen_short == 0) { 153 ex(as, "neither long nor short option"); 154 ret++; 155 } 156 if(seen_long > 1) { 157 ex(as, "multiple long options"); 158 ret++; 159 } 160 if(seen_short > 1) { 161 ex(as, "multiple short options"); 162 ret++; 163 } 164 if(seen_type > 1) { 165 ex(as, "multiple types"); 166 ret++; 167 } 168 if(seen_argument > 1) { 169 ex(as, "multiple arguments"); 170 ret++; 171 } 172 if(seen_help > 1) { 173 ex(as, "multiple help strings"); 174 ret++; 175 } 176 if(seen_default > 1) { 177 ex(as, "multiple default values"); 178 ret++; 179 } 180 return ret; 181 } 182 183 static int 184 check_command(struct assignment *as) 185 { 186 struct assignment *a; 187 int seen_name = 0; 188 int seen_function = 0; 189 int seen_help = 0; 190 int seen_argument = 0; 191 int seen_minargs = 0; 192 int seen_maxargs = 0; 193 int ret = 0; 194 for(a = as; a != NULL; a = a->next) { 195 if(strcmp(a->name, "name") == 0) 196 seen_name++; 197 else if(strcmp(a->name, "function") == 0) { 198 seen_function++; 199 } else if(strcmp(a->name, "option") == 0) 200 ret += check_option(a->u.assignment); 201 else if(strcmp(a->name, "help") == 0) { 202 seen_help++; 203 } else if(strcmp(a->name, "argument") == 0) { 204 seen_argument++; 205 } else if(strcmp(a->name, "min_args") == 0) { 206 seen_minargs++; 207 } else if(strcmp(a->name, "max_args") == 0) { 208 seen_maxargs++; 209 } else { 210 ex(a, "unknown name"); 211 ret++; 212 } 213 } 214 if(seen_name == 0) { 215 ex(as, "no command name"); 216 ret++; 217 } 218 if(seen_function > 1) { 219 ex(as, "multiple function names"); 220 ret++; 221 } 222 if(seen_help > 1) { 223 ex(as, "multiple help strings"); 224 ret++; 225 } 226 if(seen_argument > 1) { 227 ex(as, "multiple argument strings"); 228 ret++; 229 } 230 if(seen_minargs > 1) { 231 ex(as, "multiple min_args strings"); 232 ret++; 233 } 234 if(seen_maxargs > 1) { 235 ex(as, "multiple max_args strings"); 236 ret++; 237 } 238 239 return ret; 240 } 241 242 static int 243 check(struct assignment *as) 244 { 245 struct assignment *a; 246 int ret = 0; 247 for(a = as; a != NULL; a = a->next) { 248 if(strcmp(a->name, "command")) { 249 fprintf(stderr, "unknown type %s line %d\n", a->name, a->lineno); 250 ret++; 251 continue; 252 } 253 if(a->type != a_assignment) { 254 fprintf(stderr, "bad command definition %s line %d\n", a->name, a->lineno); 255 ret++; 256 continue; 257 } 258 ret += check_command(a->u.assignment); 259 } 260 return ret; 261 } 262 263 static struct assignment * 264 find_next(struct assignment *as, const char *name) 265 { 266 for(as = as->next; as != NULL; as = as->next) { 267 if(strcmp(as->name, name) == 0) 268 return as; 269 } 270 return NULL; 271 } 272 273 static struct assignment * 274 find(struct assignment *as, const char *name) 275 { 276 for(; as != NULL; as = as->next) { 277 if(strcmp(as->name, name) == 0) 278 return as; 279 } 280 return NULL; 281 } 282 283 static void 284 space(FILE *f, int level) 285 { 286 fprintf(f, "%*.*s", level * 4, level * 4, " "); 287 } 288 289 static void 290 cprint(int level, const char *fmt, ...) 291 { 292 va_list ap; 293 va_start(ap, fmt); 294 space(cfile, level); 295 vfprintf(cfile, fmt, ap); 296 va_end(ap); 297 } 298 299 static void 300 hprint(int level, const char *fmt, ...) 301 { 302 va_list ap; 303 va_start(ap, fmt); 304 space(hfile, level); 305 vfprintf(hfile, fmt, ap); 306 va_end(ap); 307 } 308 309 static void gen_name(char *str); 310 311 static void 312 gen_command(struct assignment *as) 313 { 314 struct assignment *a, *b; 315 char *f; 316 a = find(as, "name"); 317 f = strdup(a->u.value); 318 gen_name(f); 319 cprint(1, " { "); 320 fprintf(cfile, "\"%s\", ", a->u.value); 321 fprintf(cfile, "%s_wrap, ", f); 322 b = find(as, "argument"); 323 if(b) 324 fprintf(cfile, "\"%s %s\", ", a->u.value, b->u.value); 325 else 326 fprintf(cfile, "\"%s\", ", a->u.value); 327 b = find(as, "help"); 328 if(b) 329 fprintf(cfile, "\"%s\"", b->u.value); 330 else 331 fprintf(cfile, "NULL"); 332 fprintf(cfile, " },\n"); 333 for(a = a->next; a != NULL; a = a->next) 334 if(strcmp(a->name, "name") == 0) 335 cprint(1, " { \"%s\" },\n", a->u.value); 336 cprint(0, "\n"); 337 } 338 339 static void 340 gen_name(char *str) 341 { 342 char *p; 343 for(p = str; *p != '\0'; p++) 344 if(!isalnum((unsigned char)*p)) 345 *p = '_'; 346 } 347 348 static char * 349 make_name(struct assignment *as) 350 { 351 struct assignment *lopt; 352 struct assignment *type; 353 char *s; 354 355 lopt = find(as, "long"); 356 if(lopt == NULL) 357 lopt = find(as, "name"); 358 if(lopt == NULL) 359 return NULL; 360 361 type = find(as, "type"); 362 if(strcmp(type->u.value, "-flag") == 0) 363 asprintf(&s, "%s_flag", lopt->u.value); 364 else 365 asprintf(&s, "%s_%s", lopt->u.value, type->u.value); 366 gen_name(s); 367 return s; 368 } 369 370 371 static void defval_int(const char *name, struct assignment *defval) 372 { 373 if(defval != NULL) 374 cprint(1, "opt.%s = %s;\n", name, defval->u.value); 375 else 376 cprint(1, "opt.%s = 0;\n", name); 377 } 378 static void defval_string(const char *name, struct assignment *defval) 379 { 380 if(defval != NULL) 381 cprint(1, "opt.%s = \"%s\";\n", name, defval->u.value); 382 else 383 cprint(1, "opt.%s = NULL;\n", name); 384 } 385 static void defval_strings(const char *name, struct assignment *defval) 386 { 387 cprint(1, "opt.%s.num_strings = 0;\n", name); 388 cprint(1, "opt.%s.strings = NULL;\n", name); 389 } 390 391 static void free_strings(const char *name) 392 { 393 cprint(1, "free_getarg_strings (&opt.%s);\n", name); 394 } 395 396 struct type_handler { 397 const char *typename; 398 const char *c_type; 399 const char *getarg_type; 400 void (*defval)(const char*, struct assignment*); 401 void (*free)(const char*); 402 } type_handlers[] = { 403 { "integer", 404 "int", 405 "arg_integer", 406 defval_int, 407 NULL 408 }, 409 { "string", 410 "char*", 411 "arg_string", 412 defval_string, 413 NULL 414 }, 415 { "strings", 416 "struct getarg_strings", 417 "arg_strings", 418 defval_strings, 419 free_strings 420 }, 421 { "flag", 422 "int", 423 "arg_flag", 424 defval_int, 425 NULL 426 }, 427 { "-flag", 428 "int", 429 "arg_negative_flag", 430 defval_int, 431 NULL 432 }, 433 { NULL } 434 }; 435 436 static struct type_handler *find_handler(struct assignment *type) 437 { 438 struct type_handler *th; 439 for(th = type_handlers; th->typename != NULL; th++) 440 if(strcmp(type->u.value, th->typename) == 0) 441 return th; 442 ex(type, "unknown type \"%s\"", type->u.value); 443 exit(1); 444 } 445 446 static void 447 gen_options(struct assignment *opt1, const char *name) 448 { 449 struct assignment *tmp; 450 451 hprint(0, "struct %s_options {\n", name); 452 453 for(tmp = opt1; 454 tmp != NULL; 455 tmp = find_next(tmp, "option")) { 456 struct assignment *type; 457 struct type_handler *th; 458 char *s; 459 460 s = make_name(tmp->u.assignment); 461 type = find(tmp->u.assignment, "type"); 462 th = find_handler(type); 463 hprint(1, "%s %s;\n", th->c_type, s); 464 free(s); 465 } 466 hprint(0, "};\n"); 467 } 468 469 static void 470 gen_wrapper(struct assignment *as) 471 { 472 struct assignment *name; 473 struct assignment *arg; 474 struct assignment *opt1; 475 struct assignment *function; 476 struct assignment *tmp; 477 char *n, *f; 478 int nargs = 0; 479 480 name = find(as, "name"); 481 n = strdup(name->u.value); 482 gen_name(n); 483 arg = find(as, "argument"); 484 opt1 = find(as, "option"); 485 function = find(as, "function"); 486 if(function) 487 f = function->u.value; 488 else 489 f = n; 490 491 492 if(opt1 != NULL) { 493 gen_options(opt1, n); 494 hprint(0, "int %s(struct %s_options*, int, char **);\n", f, n); 495 } else { 496 hprint(0, "int %s(void*, int, char **);\n", f); 497 } 498 499 fprintf(cfile, "static int\n"); 500 fprintf(cfile, "%s_wrap(int argc, char **argv)\n", n); 501 fprintf(cfile, "{\n"); 502 if(opt1 != NULL) 503 cprint(1, "struct %s_options opt;\n", n); 504 cprint(1, "int ret;\n"); 505 cprint(1, "int optidx = 0;\n"); 506 cprint(1, "struct getargs args[] = {\n"); 507 for(tmp = find(as, "option"); 508 tmp != NULL; 509 tmp = find_next(tmp, "option")) { 510 struct assignment *type = find(tmp->u.assignment, "type"); 511 struct assignment *lopt = find(tmp->u.assignment, "long"); 512 struct assignment *sopt = find(tmp->u.assignment, "short"); 513 struct assignment *aarg = find(tmp->u.assignment, "argument"); 514 struct assignment *help = find(tmp->u.assignment, "help"); 515 516 struct type_handler *th; 517 518 cprint(2, "{ "); 519 if(lopt) 520 fprintf(cfile, "\"%s\", ", lopt->u.value); 521 else 522 fprintf(cfile, "NULL, "); 523 if(sopt) 524 fprintf(cfile, "'%c', ", *sopt->u.value); 525 else 526 fprintf(cfile, "0, "); 527 th = find_handler(type); 528 fprintf(cfile, "%s, ", th->getarg_type); 529 fprintf(cfile, "NULL, "); 530 if(help) 531 fprintf(cfile, "\"%s\", ", help->u.value); 532 else 533 fprintf(cfile, "NULL, "); 534 if(aarg) 535 fprintf(cfile, "\"%s\"", aarg->u.value); 536 else 537 fprintf(cfile, "NULL"); 538 fprintf(cfile, " },\n"); 539 } 540 cprint(2, "{ \"help\", 'h', arg_flag, NULL, NULL, NULL }\n"); 541 cprint(1, "};\n"); 542 cprint(1, "int help_flag = 0;\n"); 543 544 for(tmp = find(as, "option"); 545 tmp != NULL; 546 tmp = find_next(tmp, "option")) { 547 char *s; 548 struct assignment *type = find(tmp->u.assignment, "type"); 549 550 struct assignment *defval = find(tmp->u.assignment, "default"); 551 552 struct type_handler *th; 553 554 s = make_name(tmp->u.assignment); 555 th = find_handler(type); 556 (*th->defval)(s, defval); 557 free(s); 558 } 559 560 for(tmp = find(as, "option"); 561 tmp != NULL; 562 tmp = find_next(tmp, "option")) { 563 char *s; 564 s = make_name(tmp->u.assignment); 565 cprint(1, "args[%d].value = &opt.%s;\n", nargs++, s); 566 free(s); 567 } 568 cprint(1, "args[%d].value = &help_flag;\n", nargs++); 569 cprint(1, "if(getarg(args, %d, argc, argv, &optidx))\n", nargs); 570 cprint(2, "goto usage;\n"); 571 572 { 573 int min_args = -1; 574 int max_args = -1; 575 char *end; 576 if(arg == NULL) { 577 max_args = 0; 578 } else { 579 if((tmp = find(as, "min_args")) != NULL) { 580 min_args = strtol(tmp->u.value, &end, 0); 581 if(*end != '\0') { 582 ex(tmp, "min_args is not numeric"); 583 exit(1); 584 } 585 if(min_args < 0) { 586 ex(tmp, "min_args must be non-negative"); 587 exit(1); 588 } 589 } 590 if((tmp = find(as, "max_args")) != NULL) { 591 max_args = strtol(tmp->u.value, &end, 0); 592 if(*end != '\0') { 593 ex(tmp, "max_args is not numeric"); 594 exit(1); 595 } 596 if(max_args < 0) { 597 ex(tmp, "max_args must be non-negative"); 598 exit(1); 599 } 600 } 601 } 602 if(min_args != -1 || max_args != -1) { 603 if(min_args == max_args) { 604 cprint(1, "if(argc - optidx != %d) {\n", 605 min_args); 606 cprint(2, "fprintf(stderr, \"Need exactly %u parameters (%%u given).\\n\\n\", argc - optidx);\n", min_args); 607 cprint(2, "goto usage;\n"); 608 cprint(1, "}\n"); 609 } else { 610 if(max_args != -1) { 611 cprint(1, "if(argc - optidx > %d) {\n", max_args); 612 cprint(2, "fprintf(stderr, \"Arguments given (%%u) are more than expected (%u).\\n\\n\", argc - optidx);\n", max_args); 613 cprint(2, "goto usage;\n"); 614 cprint(1, "}\n"); 615 } 616 if(min_args != -1) { 617 cprint(1, "if(argc - optidx < %d) {\n", min_args); 618 cprint(2, "fprintf(stderr, \"Arguments given (%%u) are less than expected (%u).\\n\\n\", argc - optidx);\n", min_args); 619 cprint(2, "goto usage;\n"); 620 cprint(1, "}\n"); 621 } 622 } 623 } 624 } 625 626 cprint(1, "if(help_flag)\n"); 627 cprint(2, "goto usage;\n"); 628 629 cprint(1, "ret = %s(%s, argc - optidx, argv + optidx);\n", 630 f, opt1 ? "&opt": "NULL"); 631 632 /* free allocated data */ 633 for(tmp = find(as, "option"); 634 tmp != NULL; 635 tmp = find_next(tmp, "option")) { 636 char *s; 637 struct assignment *type = find(tmp->u.assignment, "type"); 638 struct type_handler *th; 639 th = find_handler(type); 640 if(th->free == NULL) 641 continue; 642 s = make_name(tmp->u.assignment); 643 (*th->free)(s); 644 free(s); 645 } 646 cprint(1, "return ret;\n"); 647 648 cprint(0, "usage:\n"); 649 cprint(1, "arg_printusage (args, %d, \"%s\", \"%s\");\n", nargs, 650 name->u.value, arg ? arg->u.value : ""); 651 /* free allocated data */ 652 for(tmp = find(as, "option"); 653 tmp != NULL; 654 tmp = find_next(tmp, "option")) { 655 char *s; 656 struct assignment *type = find(tmp->u.assignment, "type"); 657 struct type_handler *th; 658 th = find_handler(type); 659 if(th->free == NULL) 660 continue; 661 s = make_name(tmp->u.assignment); 662 (*th->free)(s); 663 free(s); 664 } 665 cprint(1, "return 0;\n"); 666 cprint(0, "}\n"); 667 cprint(0, "\n"); 668 } 669 670 char cname[PATH_MAX]; 671 char hname[PATH_MAX]; 672 673 static void 674 gen(struct assignment *as) 675 { 676 struct assignment *a; 677 cprint(0, "#include <stdio.h>\n"); 678 cprint(0, "#include <getarg.h>\n"); 679 cprint(0, "#include <sl.h>\n"); 680 cprint(0, "#include \"%s\"\n\n", hname); 681 682 hprint(0, "#include <stdio.h>\n"); 683 hprint(0, "#include <sl.h>\n"); 684 hprint(0, "\n"); 685 686 687 for(a = as; a != NULL; a = a->next) 688 gen_wrapper(a->u.assignment); 689 690 cprint(0, "SL_cmd commands[] = {\n"); 691 for(a = as; a != NULL; a = a->next) 692 gen_command(a->u.assignment); 693 cprint(1, "{ NULL }\n"); 694 cprint(0, "};\n"); 695 696 hprint(0, "extern SL_cmd commands[];\n"); 697 } 698 699 int version_flag; 700 int help_flag; 701 struct getargs args[] = { 702 { "version", 0, arg_flag, &version_flag }, 703 { "help", 0, arg_flag, &help_flag } 704 }; 705 int num_args = sizeof(args) / sizeof(args[0]); 706 707 static void 708 usage(int code) 709 { 710 arg_printusage(args, num_args, NULL, "command-table"); 711 exit(code); 712 } 713 714 int 715 main(int argc, char **argv) 716 { 717 char *p; 718 719 int optidx = 0; 720 721 setprogname(argv[0]); 722 if(getarg(args, num_args, argc, argv, &optidx)) 723 usage(1); 724 if(help_flag) 725 usage(0); 726 if(version_flag) { 727 print_version(NULL); 728 exit(0); 729 } 730 731 if(argc == optidx) 732 usage(1); 733 734 filename = argv[optidx]; 735 yyin = fopen(filename, "r"); 736 if(yyin == NULL) 737 err(1, "%s", filename); 738 p = strrchr(filename, '/'); 739 if(p) 740 strlcpy(cname, p + 1, sizeof(cname)); 741 else 742 strlcpy(cname, filename, sizeof(cname)); 743 p = strrchr(cname, '.'); 744 if(p) 745 *p = '\0'; 746 strlcpy(hname, cname, sizeof(hname)); 747 strlcat(cname, ".c", sizeof(cname)); 748 strlcat(hname, ".h", sizeof(hname)); 749 yyparse(); 750 if(error_flag) 751 exit(1); 752 if(check(assignment) == 0) { 753 cfile = fopen(cname, "w"); 754 if(cfile == NULL) 755 err(1, "%s", cname); 756 hfile = fopen(hname, "w"); 757 if(hfile == NULL) 758 err(1, "%s", hname); 759 gen(assignment); 760 fclose(cfile); 761 fclose(hfile); 762 } 763 fclose(yyin); 764 return 0; 765 } 766