parser.c (32556B)
1 #include <assert.h> 2 #include <ctype.h> 3 #include <errno.h> 4 #include <glob.h> 5 #include <libgen.h> 6 #include <limits.h> 7 #include <stdarg.h> 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 12 #include "config.h" 13 #include "make.h" 14 15 #define MAXREPL 256 16 #define TABSIZ 64 17 #define MAXTOKEN FILENAME_MAX 18 #define ITEM 128 19 #define IMMED 129 /* ::= */ 20 21 typedef struct macro Macro; 22 23 enum inputype { 24 FTFILE, 25 FTEXPAN, 26 }; 27 28 enum { 29 STBEGIN, 30 STINTERNAL, 31 STREPLACE, 32 STTO, 33 STEND, 34 }; 35 36 struct input { 37 int siz; 38 int type; 39 40 FILE *fp; 41 struct loc loc; 42 43 int pos; 44 char *buf; 45 46 struct input *prev; 47 }; 48 49 struct macro { 50 char *name; 51 char *value; 52 int where; 53 54 struct macro *next; 55 }; 56 57 static struct input *input; 58 static char token[MAXTOKEN]; 59 static int tok; 60 static Macro *htab[TABSIZ]; 61 62 void 63 dumpmacros(void) 64 { 65 Macro **pp, *p; 66 67 for (pp = htab; pp < &htab[TABSIZ]; ++pp) { 68 for (p = *pp; p; p = p->next) 69 printf("%s = %s\n", p->name, getmacro(p->name)); 70 } 71 } 72 73 static Macro * 74 lookup(char *name) 75 { 76 Macro *mp; 77 int h = hash(name) & (TABSIZ - 1); 78 79 for (mp = htab[h]; mp && strcmp(mp->name, name); mp = mp->next) 80 ; 81 82 if (mp) 83 return mp; 84 85 mp = emalloc(sizeof(*mp)); 86 mp->name = estrdup(name); 87 mp->value = estrdup(""); 88 mp->next = htab[h]; 89 mp->where = UNDEF; 90 htab[h] = mp; 91 92 return mp; 93 } 94 95 static char * 96 macroinfo(char *name, int *pwhere, Macro **mpp) 97 { 98 char *s, *t; 99 int hide, where; 100 Macro *mp = lookup(name); 101 102 hide = 0; 103 if (!strcmp(name, "SHELL") || !strcmp(name, "MAKEFLAGS")) 104 hide = 1; 105 106 s = mp->value; 107 where = mp->where; 108 109 if (!hide && (where == UNDEF || where == INTERNAL || eflag)) { 110 t = getenv(name); 111 if (t) { 112 where = ENVIRON; 113 s = t; 114 } 115 } 116 117 if (pwhere) 118 *pwhere = where; 119 if (mpp) 120 *mpp = mp; 121 122 return s; 123 } 124 125 char * 126 getmacro(char *name) 127 { 128 return macroinfo(name, NULL, NULL); 129 } 130 131 void 132 setmacro(char *name, char *val, int where, int export) 133 { 134 int owhere, set; 135 char *s; 136 Macro *mp; 137 138 assert(where != ENVIRON); 139 140 s = macroinfo(name, &owhere, &mp); 141 142 /* 143 * default values are defined before anything else, and marked 144 * as INTERNAL because they are injected as parseable text, and 145 * MAKEFILE and INTERNAL variables are always overriden. ENVIRON 146 * macros are generated in macroinfo() and this is why this function 147 * should not receive a where == ENVIRON ever 148 */ 149 switch (owhere) { 150 case UNDEF: 151 case INTERNAL: 152 case MAKEFILE: 153 set = 1; 154 break; 155 case ENVIRON: 156 set = (where == MAKEFLAGS || where == CMDLINE); 157 set |= (where == MAKEFILE && !eflag); 158 break; 159 case MAKEFLAGS: 160 set = (where == CMDLINE || where == MAKEFLAGS); 161 break; 162 case CMDLINE: 163 set = (where == CMDLINE); 164 break; 165 default: 166 abort(); 167 } 168 169 if (!set) { 170 debug("hidding override of %s from '%s' to '%s'", name, s, val); 171 } else { 172 debug("override %s from '%s' to '%s'", name, s, val); 173 free(mp->value); 174 mp->value = estrdup(val); 175 mp->where = where; 176 177 if (export && strcmp(name, "SHELL") != 0) { 178 debug("exporting macro %s", name); 179 exportvar(name, val); 180 } 181 } 182 } 183 184 void 185 freeloc(struct loc *loc) 186 { 187 free(loc->fname); 188 } 189 190 static struct loc * 191 getloc(void) 192 { 193 struct input *ip; 194 195 for (ip = input; ip && ip->type != FTFILE; ip = ip->prev) 196 ; 197 if (!ip) 198 return NULL; 199 200 return &ip->loc; 201 } 202 203 void 204 error(char *fmt, ...) 205 { 206 va_list va; 207 struct loc *loc; 208 209 fprintf(stderr, "make: error: "); 210 if ((loc = getloc()) != NULL) 211 fprintf(stderr, "%s:%d: ", loc->fname, loc->lineno); 212 213 va_start(va, fmt); 214 vfprintf(stderr, fmt, va); 215 va_end(va); 216 putc('\n', stderr); 217 218 exit(EXIT_FAILURE); 219 } 220 221 void 222 warning(char *fmt, ...) 223 { 224 va_list va; 225 struct loc *loc; 226 227 fprintf(stderr, "make: warning: "); 228 if ((loc = getloc()) != NULL) 229 fprintf(stderr, "%s:%d: ", loc->fname, loc->lineno); 230 231 va_start(va, fmt); 232 vfprintf(stderr, fmt, va); 233 va_end(va); 234 putc('\n', stderr); 235 } 236 237 static void 238 pop(void) 239 { 240 struct input *ip = input->prev; 241 242 if (input->type == FTFILE) { 243 if (input->fp) 244 fclose(input->fp); 245 freeloc(&input->loc); 246 } 247 free(input->buf); 248 free(input); 249 250 input = ip; 251 } 252 253 static void 254 push(int type, ...) 255 { 256 int line, len, pos; 257 FILE *fp = NULL; 258 char *buf, *s, *fname = NULL; 259 va_list va; 260 struct input *ip; 261 262 va_start(va, type); 263 switch (type) { 264 case FTFILE: 265 fp = va_arg(va, FILE *); 266 s = va_arg(va, char *); 267 line = va_arg(va, int); 268 fname = estrdup(s); 269 buf = emalloc(BUFSIZ); 270 pos = len = BUFSIZ; 271 break; 272 case FTEXPAN: 273 s = va_arg(va, char *); 274 buf = estrdup(s); 275 line = pos = 0; 276 len = strlen(s); 277 break; 278 } 279 va_end(va); 280 281 ip = emalloc(sizeof(*ip)); 282 ip->siz = len; 283 ip->buf = buf; 284 ip->type = type; 285 ip->fp = fp; 286 ip->loc.fname = fname; 287 ip->loc.lineno = line; 288 ip->pos = pos; 289 ip->prev = input; 290 291 input = ip; 292 } 293 294 static char * 295 trim(char *s) 296 { 297 size_t len; 298 299 while (isspace(*s)) 300 s++; 301 302 for (len = strlen(s); len > 0 && isspace(s[len - 1]); --len) 303 s[len - 1] = '\0'; 304 305 return s; 306 } 307 308 static void 309 include(char *s) 310 { 311 FILE *fp; 312 char *fil, *t, *end; 313 314 s = trim(s); 315 if (*s == '<' || *s == '"') { 316 char delim = (*s == '<') ? '>' : '"'; 317 s++; 318 end = strchr(s, delim); 319 if (end) 320 *end = '\0'; 321 } 322 fil = expandstring(s, NULL, getloc()); 323 324 t = trim(fil); 325 if (strlen(t) != 0) { 326 debug("including '%s'", t); 327 if ((fp = fopen(t, "r")) == NULL) 328 error("opening %s:%s", t, strerror(errno)); 329 push(FTFILE, fp, t, 0); 330 } 331 332 free(fil); 333 } 334 335 static void 336 sinclude(char *s) 337 { 338 FILE *fp; 339 char *fil, *t, *end; 340 341 s = trim(s); 342 if (*s == '<' || *s == '"') { 343 char delim = (*s == '<') ? '>' : '"'; 344 s++; 345 end = strchr(s, delim); 346 if (end) 347 *end = '\0'; 348 } 349 fil = expandstring(s, NULL, getloc()); 350 351 t = trim(fil); 352 if (strlen(t) != 0) { 353 debug("trying to include '%s'", t); 354 if ((fp = fopen(t, "r")) != NULL) 355 push(FTFILE, fp, t, 0); 356 else 357 debug("skipping missing include %s", t); 358 } 359 360 free(fil); 361 } 362 363 #if FEATURE_MAKE_BMAKE 364 /* bmake compat: .if .ifdef .ifndef .elif .else .endif and .error .warning 365 * conditionals, which POSIX 2024 does not have */ 366 #define MAXCOND 32 367 static struct { 368 int active; 369 int matched; 370 int parent_active; 371 } condstack[MAXCOND]; 372 static int conddepth = 0; 373 374 static int 375 cond_active(void) 376 { 377 if (conddepth == 0) 378 return 1; 379 return condstack[conddepth].active && condstack[conddepth].parent_active; 380 } 381 382 static void 383 cond_if(int truth) 384 { 385 int parent; 386 if (conddepth >= MAXCOND) 387 error("too many nested .if directives"); 388 parent = cond_active(); 389 conddepth++; 390 condstack[conddepth].parent_active = parent; 391 condstack[conddepth].matched = truth; 392 condstack[conddepth].active = truth; 393 } 394 395 static void 396 cond_elif(int truth) 397 { 398 if (conddepth == 0) 399 error(".elif without .if"); 400 if (condstack[conddepth].matched) 401 condstack[conddepth].active = 0; 402 else { 403 condstack[conddepth].matched = condstack[conddepth].matched || truth; 404 condstack[conddepth].active = truth; 405 } 406 } 407 408 static void 409 cond_else(void) 410 { 411 if (conddepth == 0) 412 error(".else without .if"); 413 condstack[conddepth].active = !condstack[conddepth].matched; 414 condstack[conddepth].matched = 1; 415 } 416 417 static void 418 cond_endif(void) 419 { 420 if (conddepth == 0) 421 error(".endif without .if"); 422 conddepth--; 423 } 424 425 static int 426 eval_if(char *expr) 427 { 428 char *s, *p, *var, *val, *end; 429 int neg = 0, result; 430 431 s = trim(expr); 432 if (!*s) 433 return 0; 434 435 while (*s == '(') { 436 end = s + strlen(s); 437 if (end > s && end[-1] == ')') 438 end[-1] = '\0'; 439 s++; 440 s = trim(s); 441 } 442 443 if (*s == '!') { 444 neg = 1; 445 s++; 446 s = trim(s); 447 } 448 449 if (strncmp(s, "defined", 7) == 0 && (s[7] == '(' || s[7] == ' ')) { 450 var = trim(s + 7); 451 if (*var == '(') 452 var++; 453 end = var + strlen(var); 454 if (end > var && end[-1] == ')') 455 end[-1] = '\0'; 456 var = trim(var); 457 result = getmacro(var)[0] != '\0'; 458 return neg ? !result : result; 459 } 460 461 if (strncmp(s, "make", 4) == 0 && (s[4] == '(' || s[4] == ' ')) { 462 var = trim(s + 4); 463 if (*var == '(') 464 var++; 465 end = var + strlen(var); 466 if (end > var && end[-1] == ')') 467 end[-1] = '\0'; 468 var = trim(var); 469 result = getmacro(var)[0] != '\0'; 470 return neg ? !result : result; 471 } 472 473 { 474 char *expanded = expandstring(s, NULL, getloc()); 475 s = expanded; 476 } 477 478 p = strstr(s, "=="); 479 if (!p) 480 p = strstr(s, "!="); 481 if (p) { 482 int eq = (p[1] == '='); 483 *p = '\0'; 484 val = trim(p + 2); 485 var = trim(s); 486 { 487 char *ev = expandstring(var, NULL, getloc()); 488 result = strcmp(ev, val) == 0; 489 free(ev); 490 } 491 if (!eq) 492 result = !result; 493 free(s); 494 return neg ? !result : result; 495 } 496 497 /* bare expression: true if non-empty after expansion */ 498 result = s[0] != '\0'; 499 free(s); 500 return neg ? !result : result; 501 } 502 503 #else 504 static int 505 cond_active(void) 506 { 507 return 1; 508 } 509 #endif 510 511 static char * 512 nextline(void) 513 { 514 int c; 515 FILE *fp; 516 char *s, *lim; 517 #if FEATURE_MAKE_BMAKE 518 char d[BUFSIZ]; 519 #endif 520 521 assert(input->type == FTFILE); 522 523 repeat: 524 fp = input->fp; 525 if (!fp || feof(fp)) 526 return NULL; 527 528 lim = &input->buf[input->siz]; 529 for (s = input->buf; s < lim; *s++ = c) { 530 c = getc(fp); 531 if (c == '\n' || c == EOF) { 532 input->loc.lineno++; 533 *s++ = '\n'; 534 break; 535 } 536 if (c > UCHAR_MAX || c < 0) 537 error("invalid character '%c' (%d)", c, c); 538 } 539 540 if (s == lim) 541 error("too long line"); 542 if (ferror(fp)) 543 error(strerror(errno)); 544 *s = '\0'; 545 546 if (!strcmp(input->buf, "")) 547 goto repeat; 548 549 #if FEATURE_MAKE_BMAKE 550 /* bmake compat: .include .sinclude .-include and the conditionals */ 551 if (input->buf[0] == '.') { 552 size_t dl; 553 /* copy and strip trailing whitespace/newline so strcmp works */ 554 strncpy(d, input->buf + 1, sizeof(d) - 1); 555 d[sizeof(d) - 1] = '\0'; 556 dl = strlen(d); 557 while (dl > 0 && isspace((unsigned char)d[dl - 1])) 558 d[--dl] = '\0'; 559 if (strncmp(d, "include", 7) == 0 && isblank(d[7])) { 560 input->pos = input->siz; 561 if (cond_active()) 562 include(d + 7); 563 goto repeat; 564 } 565 if (strncmp(d, "sinclude", 8) == 0 && isblank(d[8])) { 566 input->pos = input->siz; 567 if (cond_active()) 568 sinclude(d + 8); 569 goto repeat; 570 } 571 if (strncmp(d, "-include", 8) == 0 && isblank(d[8])) { 572 input->pos = input->siz; 573 if (cond_active()) 574 sinclude(d + 8); 575 goto repeat; 576 } 577 if (strncmp(d, "optinclude", 10) == 0 && isblank(d[10])) { 578 input->pos = input->siz; 579 if (cond_active()) 580 sinclude(d + 10); 581 goto repeat; 582 } 583 /* conditionals */ 584 if (strncmp(d, "if", 2) == 0 && (isblank(d[2]) || d[2] == '\0')) { 585 if (cond_active()) { 586 if (d[2] == '\0') 587 cond_if(eval_if("")); 588 else 589 cond_if(eval_if(d + 3)); 590 } else { 591 cond_if(0); 592 } 593 goto repeat; 594 } 595 if (strncmp(d, "ifdef", 5) == 0 && (isblank(d[5]) || d[5] == '\0')) { 596 if (cond_active()) { 597 char *v = trim(d[5] ? d + 6 : ""); 598 cond_if(getmacro(v)[0] != '\0'); 599 } else { 600 cond_if(0); 601 } 602 goto repeat; 603 } 604 if (strncmp(d, "ifndef", 6) == 0 && (isblank(d[6]) || d[6] == '\0')) { 605 if (cond_active()) { 606 char *v = trim(d[6] ? d + 7 : ""); 607 cond_if(getmacro(v)[0] == '\0'); 608 } else { 609 cond_if(0); 610 } 611 goto repeat; 612 } 613 if (strncmp(d, "elif", 4) == 0 && (isblank(d[4]) || d[4] == '\0')) { 614 if (condstack[conddepth].parent_active) 615 cond_elif(eval_if(d[4] ? d + 5 : "")); 616 else 617 cond_elif(0); 618 goto repeat; 619 } 620 if (strcmp(d, "else") == 0) { 621 cond_else(); 622 goto repeat; 623 } 624 if (strcmp(d, "endif") == 0) { 625 cond_endif(); 626 goto repeat; 627 } 628 if (strncmp(d, "error", 5) == 0 && isblank(d[5])) { 629 if (cond_active()) 630 error("%s", trim(d + 6)); 631 goto repeat; 632 } 633 if (strncmp(d, "warning", 7) == 0 && isblank(d[7])) { 634 if (cond_active()) 635 warning("%s", trim(d + 8)); 636 goto repeat; 637 } 638 } 639 640 #endif 641 642 /* POSIX include */ 643 if (!strncmp(input->buf, "include", 7) && isblank(input->buf[7])) { 644 input->pos = input->siz; 645 if (cond_active()) 646 include(input->buf + 7); 647 goto repeat; 648 } 649 650 /* -include is POSIX 2024's, sinclude is gmake's */ 651 if ( 652 (!strncmp(input->buf, "-include", 8) && isblank(input->buf[8])) 653 #if FEATURE_MAKE_GMAKE 654 || (!strncmp(input->buf, "sinclude", 8) && isblank(input->buf[8])) 655 #endif 656 ) { 657 input->pos = input->siz; 658 if (cond_active()) 659 sinclude(input->buf + 8); 660 goto repeat; 661 } 662 663 /* if we are inside an inactive conditional branch, skip this line */ 664 if (!cond_active()) { 665 input->pos = input->siz; 666 goto repeat; 667 } 668 669 input->pos = 0; 670 671 return input->buf; 672 } 673 674 static int 675 empty(struct input *ip) 676 { 677 return ip->pos == ip->siz || ip->buf[ip->pos] == '\0'; 678 } 679 680 static int 681 moreinput(void) 682 { 683 while (input) { 684 if (!empty(input)) 685 break; 686 687 switch (input->type) { 688 case FTEXPAN: 689 pop(); 690 break; 691 case FTFILE: 692 if (!nextline()) 693 pop(); 694 break; 695 } 696 } 697 698 return input != NULL; 699 } 700 701 static int 702 nextc(void) 703 { 704 if (!moreinput()) 705 return EOF; 706 707 return input->buf[input->pos++]; 708 } 709 710 /* 711 * this function only can be called after a call to nextc 712 * that didn't return EOF. it can return '\0', but as 713 * it is used only to check against '$' then it is not 714 * a problem 715 */ 716 static int 717 ahead(void) 718 { 719 return input->buf[input->pos]; 720 } 721 722 static int 723 back(int c) 724 { 725 if (c == EOF) 726 return c; 727 assert(input->pos > 0); 728 return input->buf[--input->pos] = c; 729 } 730 731 static void 732 comment(void) 733 { 734 int c; 735 736 while ((c = nextc()) != EOF && c != '\n') { 737 if (c == '\\' && nextc() == EOF) 738 break; 739 } 740 } 741 742 static void 743 skipspaces(void) 744 { 745 int c; 746 747 for (c = nextc(); c == ' ' || c == '\t'; c = nextc()) 748 ; 749 back(c); 750 } 751 752 static int 753 validchar(int c) 754 { 755 if (c == EOF) 756 return 0; 757 return c == '.' || c == '/' || c == '_' || c == '-' || isalnum(c); 758 } 759 760 /* what a target, a dependency or a macro name in a rule line may hold. 761 * bmake and gmake compat: * [ ] ~ , and + ? that do not start += or ?= are 762 * names too, as in g++ and the glob patterns of those makes */ 763 static int 764 itemchar(int c) 765 { 766 if (validchar(c)) 767 return 1; 768 #if MAKE_SHARED 769 if (c == '*' || c == '[' || c == ']' || c == '~' || c == ',') 770 return 1; 771 return (c == '+' || c == '?') && ahead() != '='; 772 #else 773 return 0; 774 #endif 775 } 776 777 static char * 778 expandmacro(char *name) 779 { 780 char *s; 781 782 s = expandstring(getmacro(name), NULL, getloc()); 783 debug("macro %s expanded to '%s'", name, s); 784 785 return s; 786 } 787 788 static void 789 replace(char *line, char *repl, char *to) 790 { 791 int siz, at, len, replsiz, tosiz, pos; 792 char *oline, *cur, *buf; 793 794 debug("replacing '%s', with '%s' to '%s'", line, repl, to); 795 oline = line; 796 tosiz = strlen(to); 797 replsiz = strlen(repl); 798 799 buf = NULL; 800 for (pos = 0; *line; pos += siz) { 801 cur = NULL; 802 siz = 0; 803 804 for (siz = 0; *line == ' ' || *line == '\t'; ++siz) { 805 cur = erealloc(cur, siz + 1); 806 cur[siz] = *line++; 807 } 808 809 len = strcspn(line, " \t"); 810 at = len - replsiz; 811 if (at < 0 || memcmp(line + at, repl, replsiz)) { 812 cur = erealloc(cur, siz + len); 813 memcpy(cur + siz, line, len); 814 siz += len; 815 } else { 816 cur = erealloc(cur, siz + at + tosiz); 817 memcpy(cur + siz, line, at); 818 memcpy(cur + siz + at, to, tosiz); 819 siz += at + tosiz; 820 } 821 822 line += len; 823 buf = erealloc(buf, pos + siz); 824 memcpy(buf + pos, cur, siz); 825 free(cur); 826 } 827 828 if (pos > 0) { 829 buf = erealloc(buf, pos + 1); 830 buf[pos] = '\0'; 831 debug("\treplace '%s' with '%s'", oline, buf); 832 push(FTEXPAN, buf); 833 } 834 835 free(buf); 836 } 837 838 static void 839 expandsimple(Target *tp) 840 { 841 char *s; 842 Target **p; 843 int len, c, first; 844 845 switch (c = nextc()) { 846 case '@': 847 if (!tp || !tp->target) 848 return; 849 push(FTEXPAN, tp->target); 850 break; 851 case '<': 852 if (!tp || !tp->req) 853 return; 854 push(FTEXPAN, tp->req); 855 break; 856 case '*': 857 if (!tp || !tp->target) 858 return; 859 s = strrchr(tp->target, '.'); 860 if (!s) { 861 push(FTEXPAN, tp->target); 862 return; 863 } 864 865 len = s - tp->target; 866 s = emalloc(len + 1); 867 memcpy(s, tp->target, len); 868 s[len] = '\0'; 869 push(FTEXPAN, s); 870 free(s); 871 break; 872 case '?': 873 if (!tp) 874 return; 875 876 if (tp->req && stamp(tp->req) > tp->stamp) { 877 push(FTEXPAN, " "); 878 push(FTEXPAN, tp->req); 879 } 880 881 for (p = tp->deps; p && *p; ++p) { 882 if (stamp((*p)->name) > tp->stamp) { 883 push(FTEXPAN, " "); 884 push(FTEXPAN, (*p)->name); 885 } 886 } 887 break; 888 case '>': 889 case '^': 890 if (!tp) 891 return; 892 first = 1; 893 if (tp->req) { 894 push(FTEXPAN, tp->req); 895 first = 0; 896 } 897 for (p = tp->deps; p && *p; ++p) { 898 if (!first) 899 push(FTEXPAN, " "); 900 push(FTEXPAN, (*p)->name); 901 first = 0; 902 } 903 break; 904 default: 905 token[0] = c; 906 token[1] = '\0'; 907 s = expandmacro(token); 908 push(FTEXPAN, s); 909 free(s); 910 break; 911 } 912 } 913 914 static int 915 internal(int ch) 916 { 917 switch (ch) { 918 case '@': 919 case '?': 920 case '*': 921 case '<': 922 case '>': 923 case '^': 924 return 1; 925 default: 926 return 0; 927 } 928 } 929 930 /* $(@D) and $(@F) and their kin: the directory part of a name or the rest. 931 * the string given is freed */ 932 static char * 933 dirbase(char *name, int which) 934 { 935 char *copy, *r; 936 937 copy = estrdup(name); 938 r = estrdup(which == 'D' ? dirname(copy) : basename(copy)); 939 free(copy); 940 free(name); 941 return r; 942 } 943 944 /* gmake compat: a macro name that holds a macro, as in $(tag-static-$(static)): the inner 945 * one is read to its closing bracket and its value joins the name. the 946 * pieces of a $(name:from=to) hold them too, kept as text until used */ 947 #if FEATURE_MAKE_GMAKE 948 static int 949 rawref(char *dst, int di, int max) 950 { 951 int c, open, close, depth = 1; 952 953 dst[di++] = '$'; 954 open = nextc(); 955 if (open != '(' && open != '{') 956 error("invalid macro name in expansion"); 957 close = open == '(' ? ')' : '}'; 958 dst[di++] = open; 959 while (depth && (c = nextc()) != EOF) { 960 if (di >= max - 2) 961 error("expansion text too long"); 962 depth += (c == open) - (c == close); 963 dst[di++] = c; 964 } 965 if (depth) 966 error("found eof while parsing expansion"); 967 dst[di] = '\0'; 968 return di; 969 } 970 971 static int 972 nested(char *name, int namei, Target *tp) 973 { 974 char ref[MAXTOKEN], *val; 975 int n; 976 977 rawref(ref, 0, MAXTOKEN); 978 val = expandstring(ref, tp, getloc()); 979 n = strlen(val); 980 if (namei + n >= MAXTOKEN - 1) 981 error("expansion text too long"); 982 memcpy(name + namei, val, n); 983 free(val); 984 return namei + n; 985 } 986 #endif 987 988 static void 989 expansion(Target *tp) 990 { 991 int delim, c, repli, toi, namei, st; 992 char name[MAXTOKEN], repl[MAXREPL], to[MAXREPL]; 993 char *s, *erepl, *eto; 994 995 c = nextc(); 996 if (c == '(') 997 delim = ')'; 998 else if (c == '{') 999 delim = '}'; 1000 else 1001 delim = 0; 1002 1003 if (!delim) { 1004 back(c); 1005 expandsimple(tp); 1006 return; 1007 } 1008 1009 s = NULL; 1010 namei = repli = toi = 0; 1011 st = STBEGIN; 1012 1013 while (st != STEND && (c = nextc()) != EOF) { 1014 switch (st) { 1015 case STBEGIN: 1016 if (c == ':') { 1017 st = STREPLACE; 1018 name[namei] = '\0'; 1019 s = expandmacro(name); 1020 break; 1021 } 1022 if (c == delim) { 1023 name[namei] = '\0'; 1024 s = expandmacro(name); 1025 goto no_replace; 1026 } 1027 if (namei == MAXTOKEN - 1) 1028 error("expansion text too long"); 1029 1030 if (namei == 0 && internal(c)) { 1031 name[namei++] = '$'; 1032 name[namei++] = c; 1033 name[namei] = '\0'; 1034 st = STINTERNAL; 1035 s = expandstring(name, tp, getloc()); 1036 break; 1037 } 1038 1039 #if FEATURE_MAKE_GMAKE 1040 if (c == '$') { 1041 namei = nested(name, namei, tp); 1042 break; 1043 } 1044 // g++ in CXXFLAGS-g++ 1045 if (c == '+') { 1046 name[namei++] = c; 1047 break; 1048 } 1049 #endif 1050 if (!validchar(c)) 1051 error("invalid macro name in expansion"); 1052 name[namei++] = c; 1053 break; 1054 case STINTERNAL: 1055 if (c == 'D' || c == 'F') { 1056 s = dirbase(s, c); 1057 break; 1058 } 1059 if (c == delim) 1060 goto no_replace; 1061 if (c != ':') 1062 error("invalid internal macro in expansion"); 1063 st = STREPLACE; 1064 break; 1065 case STREPLACE: 1066 if (c == '=') { 1067 st = STTO; 1068 break; 1069 } 1070 if (c == delim) 1071 error( 1072 "invalid replacement pattern in " 1073 "expansion" 1074 ); 1075 #if FEATURE_MAKE_GMAKE 1076 if (c == '$') { 1077 repli = rawref(repl, repli, MAXREPL); 1078 break; 1079 } 1080 #endif 1081 if (repli == MAXREPL - 1) 1082 error("macro replacement too big"); 1083 repl[repli++] = c; 1084 break; 1085 case STTO: 1086 if (c == delim) { 1087 st = STEND; 1088 break; 1089 } 1090 1091 #if FEATURE_MAKE_GMAKE 1092 if (c == '$') { 1093 toi = rawref(to, toi, MAXREPL); 1094 break; 1095 } 1096 #endif 1097 if (toi == MAXREPL - 1) 1098 error("macro substiturion too big"); 1099 to[toi++] = c; 1100 break; 1101 } 1102 } 1103 1104 if (c == EOF) 1105 error("found eof while parsing expansion"); 1106 1107 repl[repli] = '\0'; 1108 to[toi] = '\0'; 1109 1110 erepl = expandstring(repl, tp, getloc()); 1111 #if FEATURE_MAKE_GMAKE 1112 eto = expandstring(to, tp, getloc()); 1113 #else 1114 eto = estrdup(to); 1115 #endif 1116 replace(s, erepl, eto); 1117 1118 free(eto); 1119 free(erepl); 1120 free(s); 1121 return; 1122 1123 no_replace: 1124 push(FTEXPAN, s); 1125 free(s); 1126 } 1127 1128 /* 1129 * horrible hack to do string expansion 1130 * we cannot use normal push and nextc because that 1131 * would consume characters of the current file too 1132 * for that reason it cleans the input and it recovers 1133 * it later 1134 */ 1135 char * 1136 expandstring(char *line, Target *tp, struct loc *loc) 1137 { 1138 int c, n; 1139 char *s; 1140 struct input *ip = input; 1141 1142 input = NULL; 1143 push(FTFILE, NULL, loc->fname, loc->lineno); 1144 push(FTEXPAN, line); 1145 1146 n = 0; 1147 s = NULL; 1148 while ((c = nextc()) != EOF) { 1149 if (c != '$') { 1150 s = erealloc(s, ++n); 1151 s[n - 1] = c; 1152 continue; 1153 } 1154 1155 if ((c = nextc()) == '$') { 1156 s = erealloc(s, n += 2); 1157 s[n - 2] = '$'; 1158 s[n - 1] = '$'; 1159 } else { 1160 back(c); 1161 expansion(tp); 1162 } 1163 } 1164 1165 s = erealloc(s, n + 1); 1166 s[n] = '\0'; 1167 input = ip; 1168 1169 return s; 1170 } 1171 1172 static int 1173 item(void) 1174 { 1175 int c; 1176 char *s; 1177 char buf[MAXTOKEN]; 1178 1179 for (s = buf; s < &buf[MAXTOKEN] - 1;) { 1180 c = nextc(); 1181 if (c == '$' && ahead() != '$') 1182 expansion(NULL); 1183 else if (itemchar(c)) 1184 *s++ = c; 1185 else 1186 break; 1187 } 1188 back(c); 1189 1190 if (s >= &buf[MAXTOKEN] - 1) 1191 error("token too long"); 1192 if (s == buf) 1193 error("invalid empty token"); 1194 *s++ = '\0'; 1195 memcpy(token, buf, s - buf); 1196 1197 return ITEM; 1198 } 1199 1200 static int 1201 next(void) 1202 { 1203 int c; 1204 1205 repeat: 1206 /* 1207 * it is better to avoid skipspaces() here, because 1208 * it can generate the need for 2 calls to back(), 1209 * and we need the character anyway 1210 */ 1211 c = nextc(); 1212 if (c == ' ' || c == '\t') 1213 goto repeat; 1214 1215 if (c == '\\') { 1216 if ((c = nextc()) == '\n') 1217 goto repeat; 1218 back(c); 1219 c = '\\'; 1220 } 1221 1222 switch (c) { 1223 case EOF: 1224 strcpy(token, "<EOF>"); 1225 tok = EOF; 1226 break; 1227 case '$': 1228 if ((c = nextc()) == '$') 1229 goto single; 1230 back(c); 1231 expansion(NULL); 1232 goto repeat; 1233 case ':': 1234 // ::= expands its text where it is read 1235 if (ahead() == ':') { 1236 nextc(); 1237 if (ahead() == '=') { 1238 nextc(); 1239 strcpy(token, "::="); 1240 tok = IMMED; 1241 break; 1242 } 1243 back(':'); 1244 } 1245 #if FEATURE_MAKE_GMAKE 1246 /* gmake compat: := is the same */ 1247 else if (ahead() == '=') { 1248 nextc(); 1249 strcpy(token, ":="); 1250 tok = IMMED; 1251 break; 1252 } 1253 #endif 1254 goto single; 1255 case '#': 1256 comment(); 1257 c = '\n'; 1258 /* fallthrough */ 1259 case ';': 1260 case '=': 1261 case '\n': 1262 single: 1263 token[0] = c; 1264 token[1] = '\0'; 1265 tok = c; 1266 break; 1267 case '+': 1268 if (ahead() == '=') { 1269 nextc(); 1270 token[0] = '+'; 1271 token[1] = '='; 1272 token[2] = '\0'; 1273 tok = '+'; 1274 break; 1275 } 1276 goto name; 1277 case '?': 1278 if (ahead() == '=') { 1279 nextc(); 1280 token[0] = '?'; 1281 token[1] = '='; 1282 token[2] = '\0'; 1283 tok = '?'; 1284 break; 1285 } 1286 goto name; 1287 case '!': 1288 if (nextc() == '=') { 1289 token[0] = '!'; 1290 token[1] = '='; 1291 token[2] = '\0'; 1292 tok = '!'; 1293 break; 1294 } 1295 error("unexpected character '!'"); 1296 default: 1297 name: 1298 if (!itemchar(c)) 1299 error("unexpected character '%c'", c); 1300 back(c); 1301 tok = item(); 1302 break; 1303 } 1304 1305 return tok; 1306 } 1307 1308 static char * 1309 readmacrodef(void) 1310 { 1311 int n, c; 1312 char *line; 1313 1314 n = 0; 1315 line = NULL; 1316 while ((c = nextc()) != EOF) { 1317 line = erealloc(line, n + 1); 1318 if (c == '\n') 1319 break; 1320 if (c == '#') { 1321 comment(); 1322 break; 1323 } 1324 if (c == '\\') { 1325 if ((c = nextc()) != '\n') { 1326 back(c); 1327 c = '\\'; 1328 } else { 1329 skipspaces(); 1330 c = ' '; 1331 } 1332 } 1333 1334 line[n++] = c; 1335 } 1336 if (c == EOF) 1337 error("EOF while looking for end of line"); 1338 line[n] = '\0'; 1339 1340 return line; 1341 } 1342 1343 static struct action 1344 readcmd(void) 1345 { 1346 int n, c; 1347 struct loc *loc; 1348 struct action act; 1349 1350 skipspaces(); 1351 1352 loc = getloc(); 1353 act.loc.fname = estrdup(loc->fname); 1354 act.loc.lineno = loc->lineno; 1355 1356 n = 0; 1357 act.line = NULL; 1358 while ((c = nextc()) != EOF) { 1359 act.line = erealloc(act.line, n + 1); 1360 if (c == '\n') 1361 break; 1362 if (c == '\\') { 1363 if ((c = nextc()) == '\n') { 1364 if ((c = nextc()) != '\t') 1365 back(c); 1366 continue; 1367 } 1368 back(c); 1369 c = '\\'; 1370 } 1371 act.line[n++] = c; 1372 } 1373 if (c == EOF) 1374 error("EOF while looking for end of command"); 1375 act.line[n] = '\0'; 1376 1377 return act; 1378 } 1379 1380 #if MAKE_SHARED 1381 /* bmake and gmake compat: a dependency with glob characters stands for the 1382 * files that match. one that matches nothing stays as it is and fails as 1383 * a missing file would */ 1384 static int 1385 globdeps(char ***deps, int n) 1386 { 1387 char **out = NULL; 1388 int i, j, m = 0; 1389 glob_t g; 1390 1391 for (i = 0; i < n; i++) { 1392 if (strpbrk((*deps)[i], "*?[") && glob((*deps)[i], 0, NULL, &g) == 0) { 1393 for (j = 0; j < (int)g.gl_pathc; j++) { 1394 out = erealloc(out, (m + 1) * sizeof(char *)); 1395 out[m++] = estrdup(g.gl_pathv[j]); 1396 } 1397 globfree(&g); 1398 free((*deps)[i]); 1399 continue; 1400 } 1401 out = erealloc(out, (m + 1) * sizeof(char *)); 1402 out[m++] = (*deps)[i]; 1403 } 1404 free(*deps); 1405 *deps = out; 1406 return m; 1407 } 1408 #endif 1409 1410 static void 1411 rule(char *targets[], int ntargets) 1412 { 1413 int c, i, j, ndeps, nactions; 1414 struct action *acts; 1415 char **deps = NULL; 1416 1417 #if MAKE_SHARED 1418 // bmake and gmake compat: a name that expands to nothing, as ${SO}: does when 1419 // SO is unset, makes a rule that is not there 1420 #else 1421 if (ntargets == 0) 1422 error("missing target"); 1423 #endif 1424 for (ndeps = 0; next() == ITEM; ++ndeps) { 1425 deps = erealloc(deps, (ndeps + 1) * sizeof(char *)); 1426 deps[ndeps] = estrdup(token); 1427 } 1428 1429 if (tok != '\n' && tok != ';') 1430 error("garbage at the end of the line"); 1431 1432 #if MAKE_SHARED 1433 ndeps = globdeps(&deps, ndeps); 1434 #endif 1435 1436 nactions = 0; 1437 acts = NULL; 1438 if (tok == ';') { 1439 nactions++; 1440 acts = erealloc(acts, nactions * sizeof(*acts)); 1441 acts[nactions - 1] = readcmd(); 1442 } 1443 1444 for (;;) { 1445 if ((c = nextc()) == '#') { 1446 comment(); 1447 continue; 1448 } 1449 #if MAKE_SHARED 1450 /* bmake and gmake compat: a blank line does not end the commands */ 1451 if (c == '\n') 1452 continue; 1453 #endif 1454 if (c != '\t') 1455 break; 1456 nactions++; 1457 acts = erealloc(acts, nactions * sizeof(*acts)); 1458 acts[nactions - 1] = readcmd(); 1459 } 1460 back(c); 1461 1462 for (i = 0; i < ntargets; i++) { 1463 addtarget(targets[i], ndeps); 1464 for (j = 0; j < ndeps; j++) 1465 adddep(targets[i], deps[j]); 1466 if (nactions > 0) 1467 addrule(targets[i], acts, nactions); 1468 } 1469 1470 for (i = 0; i < ndeps; i++) 1471 free(deps[i]); 1472 free(deps); 1473 1474 for (i = 0; i < nactions; i++) { 1475 free(acts[i].line); 1476 freeloc(&acts[i].loc); 1477 } 1478 free(acts); 1479 } 1480 1481 static void 1482 assign(char *macros[], int where, int n) 1483 { 1484 char *defs; 1485 1486 if (n != 1) 1487 error("invalid macro definition"); 1488 1489 skipspaces(); 1490 defs = readmacrodef(); 1491 setmacro(*macros, defs, where, NOEXPORT); 1492 free(defs); 1493 } 1494 1495 static void 1496 assign_append(char *macros[], int where, int n) 1497 { 1498 char *defs, *old, *new; 1499 size_t olen, dlen; 1500 1501 if (n != 1) 1502 error("invalid macro definition"); 1503 1504 skipspaces(); 1505 defs = readmacrodef(); 1506 old = getmacro(*macros); 1507 olen = strlen(old); 1508 dlen = strlen(defs); 1509 /* old + " " + defs + '\0' */ 1510 new = emalloc(olen + (olen > 0 ? 1 : 0) + dlen + 1); 1511 memcpy(new, old, olen); 1512 if (olen > 0) 1513 new[olen++] = ' '; 1514 memcpy(new + olen, defs, dlen); 1515 new[olen + dlen] = '\0'; 1516 setmacro(*macros, new, where, NOEXPORT); 1517 free(new); 1518 free(defs); 1519 } 1520 1521 /* ::= reads the text as it is now, so what a later line sets does not move it */ 1522 static void 1523 assign_immediate(char *macros[], int where, int n) 1524 { 1525 char *defs, *text; 1526 1527 if (n != 1) 1528 error("invalid macro definition"); 1529 1530 skipspaces(); 1531 defs = readmacrodef(); 1532 text = expandstring(defs, NULL, getloc()); 1533 setmacro(*macros, text, where, NOEXPORT); 1534 free(text); 1535 free(defs); 1536 } 1537 1538 /* ?= sets a macro only when nothing, the environment included, has */ 1539 static void 1540 assign_default(char *macros[], int where, int n) 1541 { 1542 char *defs; 1543 1544 if (n != 1) 1545 error("invalid macro definition"); 1546 1547 skipspaces(); 1548 defs = readmacrodef(); 1549 if (*getmacro(*macros) == '\0') 1550 setmacro(*macros, defs, where, NOEXPORT); 1551 free(defs); 1552 } 1553 1554 /* != reads a macro from a command's output */ 1555 static void 1556 assign_shell(char *macros[], int where, int n) 1557 { 1558 char *cmd, *out; 1559 FILE *p; 1560 size_t cap, len; 1561 int c; 1562 1563 if (n != 1) 1564 error("invalid macro definition"); 1565 1566 skipspaces(); 1567 cmd = readmacrodef(); 1568 { 1569 char *expanded = expandstring(cmd, NULL, getloc()); 1570 free(cmd); 1571 cmd = expanded; 1572 } 1573 p = wpopen(cmd, NULL, "r"); 1574 if (!p) 1575 error("popen: %s", strerror(errno)); 1576 cap = 64; 1577 out = emalloc(cap); 1578 len = 0; 1579 while ((c = getc(p)) != EOF) { 1580 if (len + 1 >= cap) { 1581 cap *= 2; 1582 out = erealloc(out, cap); 1583 } 1584 if (c == '\n') 1585 break; 1586 out[len++] = c; 1587 } 1588 out[len] = '\0'; 1589 wpclose(p); 1590 setmacro(*macros, out, where, NOEXPORT); 1591 free(out); 1592 free(cmd); 1593 } 1594 1595 void 1596 parseinput(int where) 1597 { 1598 int i, n; 1599 char **targets; 1600 1601 while (moreinput()) { 1602 n = 0; 1603 targets = NULL; 1604 1605 next(); 1606 if (tok == '\n') 1607 continue; 1608 1609 while (tok == ITEM) { 1610 n++; 1611 targets = erealloc(targets, n * sizeof(char *)); 1612 targets[n - 1] = estrdup(token); 1613 next(); 1614 } 1615 1616 switch (tok) { 1617 case ':': 1618 rule(targets, n); 1619 break; 1620 case '=': 1621 assign(targets, where, n); 1622 break; 1623 case IMMED: 1624 assign_immediate(targets, where, n); 1625 break; 1626 case '+': 1627 assign_append(targets, where, n); 1628 break; 1629 case '?': 1630 assign_default(targets, where, n); 1631 break; 1632 case '!': 1633 assign_shell(targets, where, n); 1634 break; 1635 default: 1636 error("unexpected token '%s'(%d)", token, tok); 1637 } 1638 1639 for (i = 0; i < n; i++) 1640 free(targets[i]); 1641 free(targets); 1642 } 1643 } 1644 1645 int 1646 parse(char *fname) 1647 { 1648 FILE *fp; 1649 1650 if (!fname) { 1651 fp = stdin; 1652 fname = "<stdin>"; 1653 } else if ((fp = fopen(fname, "r")) == NULL) { 1654 return 0; 1655 } 1656 1657 debug("parsing %s", fname); 1658 push(FTFILE, fp, fname, 0); 1659 parseinput(MAKEFILE); 1660 1661 return 1; 1662 } 1663 1664 void 1665 inject(char *s) 1666 { 1667 push(FTFILE, NULL, "<internal>", 0); 1668 push(FTEXPAN, s); 1669 parseinput(INTERNAL); 1670 }