1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
|
/*
* cook - file construction tool
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1997, 1998, 1999 Peter Miller;
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
* MANIFEST: functions to parse #directive lines in cookbooks
*
* The hashline.y and parse.y parsers share the same lexer.
* This means using the classic sed hack for yacc output.
* Note that the expression grammars must be as similar as possible
* in the two grammars.
*
* The state table in the condition frames is very simple:
* state 0: before #if
* state 1: after #if (and variants)
* state 2: after #elif
* state 3: after #else
* state 0: after #endif
*/
%{
#include <ac/stdio.h>
#include <ac/stddef.h>
#include <ac/string.h>
#include <ac/time.h>
#include <ac/stdlib.h>
#include <cook.h>
#include <expr.h>
#include <expr/catenate.h>
#include <expr/constant.h>
#include <expr/function.h>
#include <expr/list.h>
#include <hashline.h>
#include <lex.h>
#include <mem.h>
#include <option.h>
#include <os.h>
#include <sub.h>
#include <trace.h>
#include <str_list.h>
static string_list_ty done_once;
typedef struct cond cond;
struct cond
{
int pass;
int state;
cond *next;
};
static cond *stack;
static cond *cond_free_list;
#ifdef DEBUG
#define YYDEBUG 1
#define printf trace_where(__FILE__, __LINE__), lex_trace
extern int yydebug;
#endif
#define yyerror parse_error
static expr_position_ty *curpos _((void));
static expr_position_ty *
curpos()
{
static expr_position_ty pos;
pos.pos_name = lex_cur_file();
pos.pos_line = lex_cur_line();
return &pos;
}
/*
* NAME
* open_include - open an include file
*
* SYNOPSIS
* void open_include(string_ty *filename);
*
* DESCRIPTION
* The open_include function is used to search for a given file name in
* the include path and lex_open it when found.
*
* RETURNS
* void
*/
static void open_include_once _((string_ty *, string_ty *));
static void
open_include_once(logical, physical)
string_ty *logical;
string_ty *physical;
{
if (!string_list_member(&done_once, physical))
lex_open_include(logical, physical);
}
void
hashline_reset()
{
string_list_destructor(&done_once);
}
static void open_include _((string_ty *, int));
static void
open_include(filename, local)
string_ty *filename;
int local;
{
int j;
string_ty *path;
trace(("open_include(filename = %08lX, local = %d) entry",
filename, local));
trace_string(filename->str_text);
if (filename->str_text[0] != '/')
{
if (local)
{
string_ty *s;
s = lex_cur_file();
if (strchr(s->str_text, '/'))
{
s = os_dirname(s);
if (!s)
{
bomb:
yyerror("unable to construct include file name");
goto ret;
}
path = str_format("%S/%S", s, filename);
str_free(s);
}
else
path = str_copy(filename);
switch (os_exists(path))
{
case -1:
str_free(path);
goto bomb;
case 1:
open_include_once(filename, path);
str_free(path);
goto ret;
}
str_free(path);
}
for (j = 0; j < option.o_search_path.nstrings; ++j)
{
path = str_format("%S/%S", option.o_search_path.string[j], filename);
switch (os_exists(path))
{
case -1:
str_free(path);
goto bomb;
case 1:
open_include_once(filename, path);
str_free(path);
goto ret;
}
str_free(path);
}
}
open_include_once(filename, filename);
ret:
trace((/*{*/"}\n"));
}
/*
* NAME
* hashline - the # control line processor
*
* SYNOPSIS
* void hashline(void);
*
* DESCRIPTION
* The hashline function is used to process # control lines.
*
* RETURNS
* void
*/
void
hashline()
{
int yyparse _((void)); /* forward */
trace(("hashline()\n{\n"/*}*/));
#if YYDEBUG
yydebug = trace_pretest_;
#endif
hashline_lex_reset();
yyparse();
trace((/*{*/"}\n"));
}
/*
* NAME
* cond_alloc - allocate a condition structure
*
* SYNOPSIS
* cond *cond_alloc(void);
*
* DESCRIPTION
* The cond_alloc function is used to allocate a condition structure
* from dynamic memory.
*
* RETURNS
* cond * - pointer to condition structure.
*
* CAVEAT
* A free list is maintained to avoid malloc overheads.
*/
static cond *cond_alloc _((void));
static cond *
cond_alloc()
{
cond *c;
if (cond_free_list)
{
c = cond_free_list;
cond_free_list = c->next;
}
else
c = (cond *)mem_alloc(sizeof(cond));
return c;
}
/*
* NAME
* cond_free - free condition structure
*
* SYNOPSIS
* void cond_free(cond*);
*
* DESCRIPTION
* The cond_free function is used to indicate that a condition structure
* is finished with.
*
* RETURNS
* void
*
* CAVEAT
* A free list is maintained to avoid malloc overheads.
*/
static void cond_free _((cond *));
static void
cond_free(c)
cond *c;
{
c->next = cond_free_list;
cond_free_list = c;
}
/*
* NAME
* hash_include - process #include directive
*
* SYNOPSIS
* void hash_include(expr_list_ty *filename);
*
* DESCRIPTION
* The hash_include function is used to process #include directives.
*
* RETURNS
* void
*/
static void hash_include _((expr_list_ty *));
static void
hash_include(elp)
expr_list_ty *elp;
{
string_list_ty *result;
string_ty *s;
size_t j;
/*
* if conditional is false, don't do
*/
if (stack && !stack->pass)
return;
/*
* turn the expressions into words
*/
result = expr_list_evaluate(elp, 0);
if (!result)
{
hashline_error("expression evaluation failed");
return;
}
/*
* include each file
*/
for (j = 0; j < result->nstrings; ++j)
{
s = result->string[j];
if
(
s->str_length > 2
&&
s->str_text[0] == '<'
&&
s->str_text[s->str_length - 1] == '>'
)
{
s = str_n_from_c(s->str_text + 1, s->str_length - 2);
open_include(s, 0);
str_free(s);
}
else
{
if (s->str_length)
open_include(s, 1);
else
yyerror("expression produces null file name to include");
}
}
string_list_delete(result);
}
/*
* NAME
* hash_include - process #include-cooked directive
*
* SYNOPSIS
* void hash_include_cooked(expr_list_ty *filename);
*
* DESCRIPTION
* The hash_include_cooked function is used to
* process #include-cooked directives.
*
* RETURNS
* void
*/
static void hash_include_cooked _((expr_list_ty *, int));
static void
hash_include_cooked(elp, warn)
expr_list_ty *elp;
int warn;
{
string_list_ty *logical;
string_ty *s;
long j;
string_list_ty physical;
long nerr;
/*
* if conditional is false, don't do
*/
if (stack && !stack->pass)
return;
/*
* turn the expressions into words
*/
logical = expr_list_evaluate(elp, 0);
if (!logical)
{
hashline_error("expression evaluation failed");
return;
}
/*
* make sure we like the words they used
*/
nerr = 0;
for (j = 0; j < logical->nstrings; ++j)
{
s = logical->string[j];
if
(
s->str_length > 2
&&
s->str_text[0] == '<'
&&
s->str_text[s->str_length - 1] == '>'
)
{
yyerror("may not use angle brackets with #include-cooked");
++nerr;
}
else if (!s->str_length)
{
yyerror("expression produces null file name to include");
++nerr;
}
}
if (nerr)
{
string_list_delete(logical);
return;
}
/*
* append to the auto-cook list
*
* If any of the auto-cook list are out-of-date,
* they are recooked, and then cook starts over.
*/
cook_auto(logical);
/*
* resolve the words into paths
*/
string_list_constructor(&physical);
cook_mtime_resolve(&physical, logical, 0);
/*
* include the resolved paths,
* warning if they do not exist
* (they will later, hopefully)
*/
assert(logical->nstrings == physical.nstrings);
for (j = 0; j < physical.nstrings; ++j)
{
s = physical.string[j];
if (os_exists(s))
open_include_once(logical->string[j], s);
else if (warn)
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set(scp, "File_Name", "%S", s);
lex_warning
(
scp,
i18n("include cooked \"$filename\": file not found")
);
sub_context_delete(scp);
}
}
string_list_destructor(&physical);
string_list_delete(logical);
}
/*
* NAME
* hash_if - process #if directive
*
* SYNOPSIS
* void hash_if(expr_ty *);
*
* DESCRIPTION
* The hash_if function is used to process #if directives.
*
* RETURNS
* void
*/
static void hash_if _((expr_ty *));
static void
hash_if(ep)
expr_ty *ep;
{
cond *c;
trace(("hash_if(ep = %08lX)\n{\n"/*}*/, ep));
c = cond_alloc();
c->next = stack;
if (stack && !stack->pass)
{
c->pass = 0;
c->state = 1;
lex_passing(0);
}
else
{
switch (expr_eval_condition(ep, 0))
{
case -1:
yyerror("expression evaluation failed");
/* fall through... */
case 0:
c->pass = 0;
c->state = 2;
lex_passing(0);
break;
default:
c->pass = 1;
c->state = 1;
lex_passing(1);
break;
}
}
stack = c;
trace((/*{*/"}\n"));
}
/*
* NAME
* hash_ifdef - process #ifdef directive
*
* SYNOPSIS
* void hash_ifdef(expr_ty*);
*
* DESCRIPTION
* The hash_ifdef function is used to process #ifdef directives.
*
* RETURNS
* void
*/
static void hash_ifdef _((expr_ty *));
static void
hash_ifdef(ep)
expr_ty *ep;
{
expr_ty *e1;
expr_ty *e2;
string_ty *s;
trace(("hash_ifdef(ep = %08lX)\n{\n"/*}*/, ep));
s = str_from_c("defined");
e1 = expr_constant_new(s, curpos());
str_free(s);
e2 = expr_function_new2(e1, ep);
expr_delete(e1);
hash_if(e2);
expr_delete(e2);
trace((/*{*/"}\n"));
}
/*
* NAME
* hash_ifndef - process #ifndef directives
*
* SYNOPSIS
* void hash_ifndef(expr_ty *);
*
* DESCRIPTION
* The hash_ifndef function is used to process #ifndef directives.
*
* RETURNS
* void
*/
static void hash_ifndef _((expr_ty *));
static void
hash_ifndef(ep)
expr_ty *ep;
{
expr_ty *e1;
expr_ty *e2;
expr_ty *e3;
string_ty *s;
trace(("hash_ifndef(ep = %08lX)\n{\n"/*}*/, ep));
s = str_from_c("defined");
e1 = expr_constant_new(s, curpos());
str_free(s);
e2 = expr_function_new2(e1, ep);
expr_delete(e1);
s = str_from_c("not");
e1 = expr_constant_new(s, curpos());
e3 = expr_function_new2(e1, e2);
expr_delete(e1);
expr_delete(e2);
hash_if(e3);
expr_delete(e3);
trace((/*{*/"}\n"));
}
/*
* NAME
* hash_elif - process #elif directive
*
* SYNOPSIS
* void hash_elif(expr_ty*);
*
* DESCRIPTION
* The hash_elif function is used to provess #elif directives.
*
* RETURNS
* void
*/
static void hash_elif _((expr_ty *));
static void
hash_elif(ep)
expr_ty *ep;
{
trace(("hash_elif(ep = %08lX)\n{\n"/*}*/, ep));
if (!stack)
yyerror("#elif without matching #if");
else
{
switch (stack->state)
{
case 1:
stack->pass = 0;
stack->state = 1;
lex_passing(0);
break;
case 2:
switch (expr_eval_condition(ep, 0))
{
case -1:
yyerror("expression evaluation failed");
/* fall through... */
case 0:
stack->pass = 0;
stack->state = 2;
lex_passing(0);
break;
default:
stack->pass = 1;
stack->state = 1;
lex_passing(1);
break;
}
break;
case 3:
stack->pass = 0;
stack->state = 3;
yyerror("#elif after #else");
lex_passing(0);
break;
}
}
trace((/*{*/"}\n"));
}
/*
* NAME
* hash_else - process #else directive
*
* SYNOPSIS
* void hash_else(void);
*
* DESCRIPTION
* The hash_else function is used to process #else directives.
*
* RETURNS
* void
*/
static void hash_else _((void));
static void
hash_else()
{
trace(("hash_else()\n{\n"/*}*/));
if (!stack)
yyerror("#else without matching #if");
else
{
switch (stack->state)
{
case 1:
stack->pass = 0;
stack->state = 3;
lex_passing(0);
break;
case 2:
stack->pass = 1;
stack->state = 3;
lex_passing(1);
break;
case 3:
stack->pass = 0;
stack->state = 3;
yyerror("#else after #else");
lex_passing(0);
break;
}
}
trace((/*{*/"}\n"));
}
/*
* NAME
* hash_endif - process #endif directive
*
* SYNOPSIS
* void hash_endif(void);
*
* DESCRIPTION
* The hash_endif function is used to process #endif directives.
*
* RETURNS
* void
*/
static void hash_endif _((void));
static void
hash_endif()
{
trace(("hash_endif()\n{\n"/*}*/));
if (!stack)
yyerror("#endif without matching #if");
else
{
cond *c;
c = stack;
stack = c->next;
cond_free(c);
lex_passing(stack ? stack->pass : 1);
}
trace((/*{*/"}\n"));
}
/*
* NAME
* hash_line - process #line directive
*
* SYNOPSIS
* void hash_line(expr_list_ty *elp);
*
* DESCRIPTION
* The hash_line function is used to process #line directives.
*
* RETURNS
* void
*/
static void hash_line _((expr_list_ty *));
static void
hash_line(elp)
expr_list_ty *elp;
{
string_list_ty *result;
trace(("hash_line(elp = %08lX)\n{\n"/*}*/, elp));
if (stack && !stack->pass)
goto ret;
/*
* evaluate the expressions
*/
result = expr_list_evaluate(elp, 0);
if (!result)
{
hashline_error("expression evaluation failed");
goto ret;
}
switch (result->nstrings)
{
case 1:
lex_lino_set(result->string[0], (string_ty *)0);
break;
case 2:
lex_lino_set(result->string[0], result->string[1]);
break;
default:
yyerror("#line needs positive decimal line number");
break;
}
string_list_delete(result);
ret:
trace((/*{*/"}\n"));
}
/*
* NAME
* hash_pragma - process #pragma directive
*
* SYNOPSIS
* void hash_pragma(expr_list_ty *elp);
*
* DESCRIPTION
* The hash_pragma function is used to process #pragma directives.
*
* RETURNS
* void
*/
static void hash_pragma _((expr_list_ty *));
static void
hash_pragma(elp)
expr_list_ty *elp;
{
static expr_ty *once;
trace(("hash_pragma(elp = %08lX)\n{\n"/*}*/, elp));
if (stack && !stack->pass)
goto ret;
/*
* see if it was "#pragma once"
*/
if (!once)
{
string_ty *s;
s = str_from_c("once");
curpos());
str_free(s);
}
if
(
elp->el_nexprs == 1
&&
expr_equal(elp->el_expr[0], once)
)
{
string_list_append_unique(&done_once, lex_cur_file());
goto ret;
}
/*
* add more pragma's here
*/
ret:
trace((/*{*/"}\n"));
}
%}
/*
* this list must be IDENTICAL to the list in parse.y
*/
%token CATENATE
%token COLON
%token COLON2
%token DATA
%token DATAEND
%token ELSE
%token EQUALS
%token FAIL
%token FILE_BOUNDARY
%token FUNCTION
%token HOST_BINDING
%token IF
%token LBRACE
%token LBRAK
%token LOOP
%token LOOPSTOP
%token PLUS_EQUALS
%token RBRACE
%token RBRAK
%token RETURN
%token SEMICOLON
%token SET
%token SINGLE_THREAD
%token THEN
%token UNSETENV
%token WORD
/*
* this list must not appear in parse.y
*/
%token HASH_ELIF
%token HASH_ELSE
%token HASH_ENDIF
%token HASH_IF
%token HASH_IFDEF
%token HASH_IFNDEF
%token HASH_INCLUDE
%token HASH_INCLUDE_COOKED
%token HASH_INCLUDE_COOKED2
%token HASH_LINE
%token HASH_PRAGMA
%token HASH_JUNK
%left CATENATE
%right ELSE
%union
{
expr_ty *lv_expr;
expr_list_ty lv_elist;
string_ty *lv_word;
}
%type <lv_elist> elist
%type <lv_word> WORD
%type <lv_expr> expr
%%
/*
* note that the grammar accepts a single line.
* this means that 0 (end-of-input) must be sent on end-of-line.
*/
hashline
: HASH_INCLUDE elist
{
hash_include(&$2);
expr_list_destructor(&$2);
}
| HASH_INCLUDE_COOKED elist
{
hash_include_cooked(&$2, 1);
expr_list_destructor(&$2);
}
| HASH_INCLUDE_COOKED2 elist
{
hash_include_cooked(&$2, 0);
expr_list_destructor(&$2);
}
| HASH_IF expr
{
hash_if($2);
expr_delete($2);
}
| HASH_IFDEF expr
{
hash_ifdef($2);
expr_delete($2);
}
| HASH_IFNDEF expr
{
hash_ifndef($2);
expr_delete($2);
}
| HASH_ELIF expr
{
hash_elif($2);
expr_delete($2);
}
| HASH_ELSE
{
hash_else();
}
| HASH_ENDIF
{
hash_endif();
}
| HASH_PRAGMA elist
{
hash_pragma(&$2);
expr_list_destructor(&$2);
}
| HASH_LINE elist
{
hash_line(&$2);
expr_list_destructor(&$2);
}
| error
;
/*
* this expression form is the same as in parse.y
* except that the lbrak processing is not necessary.
*/
expr
: WORD
{
$$ = expr_constant_new($1, curpos());
str_free($1);
}
| LBRAK elist RBRAK
{
$$ = expr_function_new(&$2);
expr_list_destructor(&$2);
}
| expr CATENATE expr
{
$$ = expr_catenate_new($1, $3);
expr_delete($1);
expr_delete($3);
}
;
elist
: expr
{
expr_list_constructor(&$$);
expr_list_append(&$$, $1);
expr_delete($1);
}
| elist expr
{
$$ = $1;
expr_list_append(&$$, $2);
expr_delete($2);
}
;
|