1 /* $NetBSD: compat.c,v 1.54 2004/05/07 08:12:15 sjg Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Adam de Boor. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University 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 REGENTS 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 REGENTS 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 /* 36 * Copyright (c) 1988, 1989 by Adam de Boor 37 * Copyright (c) 1989 by Berkeley Softworks 38 * All rights reserved. 39 * 40 * This code is derived from software contributed to Berkeley by 41 * Adam de Boor. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by the University of 54 * California, Berkeley and its contributors. 55 * 4. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 */ 71 72 #ifndef MAKE_NATIVE 73 static char rcsid[] = "$NetBSD: compat.c,v 1.54 2004/05/07 08:12:15 sjg Exp $"; 74 #else 75 #include <sys/cdefs.h> 76 #ifndef lint 77 #if 0 78 static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94"; 79 #else 80 __RCSID("$NetBSD: compat.c,v 1.54 2004/05/07 08:12:15 sjg Exp $"); 81 #endif 82 #endif /* not lint */ 83 #endif 84 85 /*- 86 * compat.c -- 87 * The routines in this file implement the full-compatibility 88 * mode of PMake. Most of the special functionality of PMake 89 * is available in this mode. Things not supported: 90 * - different shells. 91 * - friendly variable substitution. 92 * 93 * Interface: 94 * Compat_Run Initialize things for this module and recreate 95 * thems as need creatin' 96 */ 97 98 #include <sys/types.h> 99 #include <sys/stat.h> 100 #include <sys/wait.h> 101 102 #include <ctype.h> 103 #include <errno.h> 104 #include <signal.h> 105 #include <stdio.h> 106 107 #include "make.h" 108 #include "hash.h" 109 #include "dir.h" 110 #include "job.h" 111 #include "pathnames.h" 112 113 /* 114 * The following array is used to make a fast determination of which 115 * characters are interpreted specially by the shell. If a command 116 * contains any of these characters, it is executed by the shell, not 117 * directly by us. 118 */ 119 120 static char meta[256]; 121 122 static GNode *curTarg = NILGNODE; 123 static GNode *ENDNode; 124 static void CompatInterrupt(int); 125 static int CompatMake(ClientData, ClientData); 126 127 static void 128 Compat_Init(void) 129 { 130 const char *cp; 131 132 Shell_Init(); /* setup default shell */ 133 134 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) { 135 meta[(unsigned char) *cp] = 1; 136 } 137 /* 138 * The null character serves as a sentinel in the string. 139 */ 140 meta[0] = 1; 141 } 142 143 /*- 144 *----------------------------------------------------------------------- 145 * CompatInterrupt -- 146 * Interrupt the creation of the current target and remove it if 147 * it ain't precious. 148 * 149 * Results: 150 * None. 151 * 152 * Side Effects: 153 * The target is removed and the process exits. If .INTERRUPT exists, 154 * its commands are run first WITH INTERRUPTS IGNORED.. 155 * 156 *----------------------------------------------------------------------- 157 */ 158 static void 159 CompatInterrupt(int signo) 160 { 161 GNode *gn; 162 163 if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) { 164 char *p1; 165 char *file = Var_Value (TARGET, curTarg, &p1); 166 167 if (!noExecute && eunlink(file) != -1) { 168 Error("*** %s removed", file); 169 } 170 if (p1) 171 free(p1); 172 173 /* 174 * Run .INTERRUPT only if hit with interrupt signal 175 */ 176 if (signo == SIGINT) { 177 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 178 if (gn != NILGNODE) { 179 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn); 180 } 181 } 182 183 } 184 exit (signo); 185 } 186 187 /*- 188 *----------------------------------------------------------------------- 189 * CompatRunCommand -- 190 * Execute the next command for a target. If the command returns an 191 * error, the node's made field is set to ERROR and creation stops. 192 * 193 * Input: 194 * cmdp Command to execute 195 * gnp Node from which the command came 196 * 197 * Results: 198 * 0 if the command succeeded, 1 if an error occurred. 199 * 200 * Side Effects: 201 * The node's 'made' field may be set to ERROR. 202 * 203 *----------------------------------------------------------------------- 204 */ 205 int 206 CompatRunCommand(ClientData cmdp, ClientData gnp) 207 { 208 char *cmdStart; /* Start of expanded command */ 209 char *cp, *bp; 210 Boolean silent, /* Don't print command */ 211 doIt, /* Execute even if -n */ 212 errCheck; /* Check errors */ 213 int reason; /* Reason for child's death */ 214 int status; /* Description of child's death */ 215 int cpid; /* Child actually found */ 216 ReturnStatus retstat; /* Status of fork */ 217 LstNode cmdNode; /* Node where current command is located */ 218 const char **av; /* Argument vector for thing to exec */ 219 int argc; /* Number of arguments in av or 0 if not 220 * dynamically allocated */ 221 Boolean local; /* TRUE if command should be executed 222 * locally */ 223 char *cmd = (char *) cmdp; 224 GNode *gn = (GNode *) gnp; 225 226 /* 227 * Avoid clobbered variable warnings by forcing the compiler 228 * to ``unregister'' variables 229 */ 230 #if __GNUC__ 231 (void) &av; 232 (void) &errCheck; 233 #endif 234 silent = gn->type & OP_SILENT; 235 errCheck = !(gn->type & OP_IGNORE); 236 doIt = FALSE; 237 238 cmdNode = Lst_Member (gn->commands, (ClientData)cmd); 239 cmdStart = Var_Subst (NULL, cmd, gn, FALSE); 240 241 /* 242 * brk_string will return an argv with a NULL in av[0], thus causing 243 * execvp to choke and die horribly. Besides, how can we execute a null 244 * command? In any case, we warn the user that the command expanded to 245 * nothing (is this the right thing to do?). 246 */ 247 248 if (*cmdStart == '\0') { 249 free(cmdStart); 250 Error("%s expands to empty string", cmd); 251 return(0); 252 } else { 253 cmd = cmdStart; 254 } 255 Lst_Replace (cmdNode, (ClientData)cmdStart); 256 257 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) { 258 (void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart); 259 return(0); 260 } else if (strcmp(cmdStart, "...") == 0) { 261 gn->type |= OP_SAVE_CMDS; 262 return(0); 263 } 264 265 while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) { 266 switch (*cmd) { 267 case '@': 268 silent = TRUE; 269 break; 270 case '-': 271 errCheck = FALSE; 272 break; 273 case '+': 274 doIt = TRUE; 275 if (!meta[0]) /* we came here from jobs */ 276 Compat_Init(); 277 break; 278 } 279 cmd++; 280 } 281 282 while (isspace((unsigned char)*cmd)) 283 cmd++; 284 285 /* 286 * Search for meta characters in the command. If there are no meta 287 * characters, there's no need to execute a shell to execute the 288 * command. 289 */ 290 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) { 291 continue; 292 } 293 294 /* 295 * Print the command before echoing if we're not supposed to be quiet for 296 * this one. We also print the command if -n given. 297 */ 298 if (!silent || NoExecute(gn)) { 299 printf ("%s\n", cmd); 300 fflush(stdout); 301 } 302 303 /* 304 * If we're not supposed to execute any commands, this is as far as 305 * we go... 306 */ 307 if (!doIt && NoExecute(gn)) { 308 return (0); 309 } 310 311 if (*cp != '\0') { 312 /* 313 * If *cp isn't the null character, we hit a "meta" character and 314 * need to pass the command off to the shell. 315 */ 316 static const char *shargv[4]; 317 318 shargv[0] = shellPath; 319 /* 320 * The following work for any of the builtin shell specs. 321 */ 322 if (DEBUG(SHELL)) 323 shargv[1] = "-xc"; 324 else 325 shargv[1] = "-c"; 326 shargv[2] = cmd; 327 shargv[3] = (char *)NULL; 328 av = shargv; 329 argc = 0; 330 bp = NULL; 331 } else { 332 /* 333 * No meta-characters, so no need to exec a shell. Break the command 334 * into words to form an argument vector we can execute. 335 */ 336 av = (const char **)brk_string(cmd, &argc, TRUE, &bp); 337 } 338 339 local = TRUE; 340 341 /* 342 * Fork and execute the single command. If the fork fails, we abort. 343 */ 344 cpid = vfork(); 345 if (cpid < 0) { 346 Fatal("Could not fork"); 347 } 348 if (cpid == 0) { 349 Check_Cwd(av); 350 if (local) 351 (void)execvp(av[0], (char *const *)UNCONST(av)); 352 else 353 (void)execv(av[0], (char *const *)UNCONST(av)); 354 execError("exec", av[0]); 355 _exit(1); 356 } 357 if (bp) { 358 free(av); 359 free(bp); 360 } 361 Lst_Replace (cmdNode, (ClientData) NULL); 362 363 /* 364 * The child is off and running. Now all we can do is wait... 365 */ 366 while (1) { 367 368 while ((retstat = wait(&reason)) != cpid) { 369 if (retstat == -1 && errno != EINTR) { 370 break; 371 } 372 } 373 374 if (retstat > -1) { 375 if (WIFSTOPPED(reason)) { 376 status = WSTOPSIG(reason); /* stopped */ 377 } else if (WIFEXITED(reason)) { 378 status = WEXITSTATUS(reason); /* exited */ 379 if (status != 0) { 380 if (DEBUG(ERROR)) { 381 printf("\n*** Failed target: %s\n*** Failed command: ", 382 gn->name); 383 for (cp = cmd; *cp; ) { 384 if (isspace((unsigned char)*cp)) { 385 putchar(' '); 386 while (isspace((unsigned char)*cp)) 387 cp++; 388 } else { 389 putchar(*cp); 390 cp++; 391 } 392 } 393 printf ("\n"); 394 } 395 printf ("*** Error code %d", status); 396 } 397 } else { 398 status = WTERMSIG(reason); /* signaled */ 399 printf ("*** Signal %d", status); 400 } 401 402 403 if (!WIFEXITED(reason) || (status != 0)) { 404 if (errCheck) { 405 gn->made = ERROR; 406 if (keepgoing) { 407 /* 408 * Abort the current target, but let others 409 * continue. 410 */ 411 printf (" (continuing)\n"); 412 } 413 } else { 414 /* 415 * Continue executing commands for this target. 416 * If we return 0, this will happen... 417 */ 418 printf (" (ignored)\n"); 419 status = 0; 420 } 421 } 422 break; 423 } else { 424 Fatal ("error in wait: %d: %s", retstat, strerror(errno)); 425 /*NOTREACHED*/ 426 } 427 } 428 free(cmdStart); 429 430 return (status); 431 } 432 433 /*- 434 *----------------------------------------------------------------------- 435 * CompatMake -- 436 * Make a target. 437 * 438 * Input: 439 * gnp The node to make 440 * pgnp Parent to abort if necessary 441 * 442 * Results: 443 * 0 444 * 445 * Side Effects: 446 * If an error is detected and not being ignored, the process exits. 447 * 448 *----------------------------------------------------------------------- 449 */ 450 static int 451 CompatMake(ClientData gnp, ClientData pgnp) 452 { 453 GNode *gn = (GNode *) gnp; 454 GNode *pgn = (GNode *) pgnp; 455 456 if (gn->made == UNMADE && (gn == pgn || (pgn->type & OP_MADE) == 0)) { 457 /* 458 * First mark ourselves to be made, then apply whatever transformations 459 * the suffix module thinks are necessary. Once that's done, we can 460 * descend and make all our children. If any of them has an error 461 * but the -k flag was given, our 'make' field will be set FALSE again. 462 * This is our signal to not attempt to do anything but abort our 463 * parent as well. 464 */ 465 gn->flags |= REMAKE; 466 gn->made = BEINGMADE; 467 if ((gn->type & OP_MADE) == 0) 468 Suff_FindDeps (gn); 469 Lst_ForEach (gn->children, CompatMake, (ClientData)gn); 470 if ((gn->flags & REMAKE) == 0) { 471 gn->made = ABORTED; 472 pgn->flags &= ~REMAKE; 473 goto cohorts; 474 } 475 476 if (Lst_Member (gn->iParents, pgn) != NILLNODE) { 477 char *p1; 478 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0); 479 if (p1) 480 free(p1); 481 } 482 483 /* 484 * All the children were made ok. Now cmtime contains the modification 485 * time of the newest child, we need to find out if we exist and when 486 * we were modified last. The criteria for datedness are defined by the 487 * Make_OODate function. 488 */ 489 if (DEBUG(MAKE)) { 490 printf("Examining %s...", gn->name); 491 } 492 if (! Make_OODate(gn)) { 493 gn->made = UPTODATE; 494 if (DEBUG(MAKE)) { 495 printf("up-to-date.\n"); 496 } 497 goto cohorts; 498 } else if (DEBUG(MAKE)) { 499 printf("out-of-date.\n"); 500 } 501 502 /* 503 * If the user is just seeing if something is out-of-date, exit now 504 * to tell him/her "yes". 505 */ 506 if (queryFlag) { 507 exit (1); 508 } 509 510 /* 511 * We need to be re-made. We also have to make sure we've got a $? 512 * variable. To be nice, we also define the $> variable using 513 * Make_DoAllVar(). 514 */ 515 Make_DoAllVar(gn); 516 517 /* 518 * Alter our type to tell if errors should be ignored or things 519 * should not be printed so CompatRunCommand knows what to do. 520 */ 521 if (Targ_Ignore (gn)) { 522 gn->type |= OP_IGNORE; 523 } 524 if (Targ_Silent (gn)) { 525 gn->type |= OP_SILENT; 526 } 527 528 if (Job_CheckCommands (gn, Fatal)) { 529 /* 530 * Our commands are ok, but we still have to worry about the -t 531 * flag... 532 */ 533 if (!touchFlag || (gn->type & OP_MAKE)) { 534 curTarg = gn; 535 Lst_ForEach (gn->commands, CompatRunCommand, (ClientData)gn); 536 curTarg = NILGNODE; 537 } else { 538 Job_Touch (gn, gn->type & OP_SILENT); 539 } 540 } else { 541 gn->made = ERROR; 542 } 543 544 if (gn->made != ERROR) { 545 /* 546 * If the node was made successfully, mark it so, update 547 * its modification time and timestamp all its parents. Note 548 * that for .ZEROTIME targets, the timestamping isn't done. 549 * This is to keep its state from affecting that of its parent. 550 */ 551 gn->made = MADE; 552 pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0; 553 if (!(gn->type & OP_EXEC)) { 554 pgn->flags |= CHILDMADE; 555 Make_TimeStamp(pgn, gn); 556 } 557 } else if (keepgoing) { 558 pgn->flags &= ~REMAKE; 559 } else { 560 PrintOnError("\n\nStop."); 561 exit (1); 562 } 563 } else if (gn->made == ERROR) { 564 /* 565 * Already had an error when making this beastie. Tell the parent 566 * to abort. 567 */ 568 pgn->flags &= ~REMAKE; 569 } else { 570 if (Lst_Member (gn->iParents, pgn) != NILLNODE) { 571 char *p1; 572 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0); 573 if (p1) 574 free(p1); 575 } 576 switch(gn->made) { 577 case BEINGMADE: 578 Error("Graph cycles through %s", gn->name); 579 gn->made = ERROR; 580 pgn->flags &= ~REMAKE; 581 break; 582 case MADE: 583 if ((gn->type & OP_EXEC) == 0) { 584 pgn->flags |= CHILDMADE; 585 Make_TimeStamp(pgn, gn); 586 } 587 break; 588 case UPTODATE: 589 if ((gn->type & OP_EXEC) == 0) { 590 Make_TimeStamp(pgn, gn); 591 } 592 break; 593 default: 594 break; 595 } 596 } 597 598 cohorts: 599 Lst_ForEach (gn->cohorts, CompatMake, pgnp); 600 return (0); 601 } 602 603 /*- 604 *----------------------------------------------------------------------- 605 * Compat_Run -- 606 * Initialize this mode and start making. 607 * 608 * Input: 609 * targs List of target nodes to re-create 610 * 611 * Results: 612 * None. 613 * 614 * Side Effects: 615 * Guess what? 616 * 617 *----------------------------------------------------------------------- 618 */ 619 void 620 Compat_Run(Lst targs) 621 { 622 GNode *gn = NULL;/* Current root target */ 623 int errors; /* Number of targets not remade due to errors */ 624 625 Compat_Init(); 626 627 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 628 signal(SIGINT, CompatInterrupt); 629 } 630 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 631 signal(SIGTERM, CompatInterrupt); 632 } 633 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 634 signal(SIGHUP, CompatInterrupt); 635 } 636 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 637 signal(SIGQUIT, CompatInterrupt); 638 } 639 640 ENDNode = Targ_FindNode(".END", TARG_CREATE); 641 /* 642 * If the user has defined a .BEGIN target, execute the commands attached 643 * to it. 644 */ 645 if (!queryFlag) { 646 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE); 647 if (gn != NILGNODE) { 648 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn); 649 if (gn->made == ERROR) { 650 PrintOnError("\n\nStop."); 651 exit(1); 652 } 653 } 654 } 655 656 /* 657 * Expand .USE nodes right now, because they can modify the structure 658 * of the tree. 659 */ 660 Lst_Destroy(Make_ExpandUse(targs), NOFREE); 661 662 /* 663 * For each entry in the list of targets to create, call CompatMake on 664 * it to create the thing. CompatMake will leave the 'made' field of gn 665 * in one of several states: 666 * UPTODATE gn was already up-to-date 667 * MADE gn was recreated successfully 668 * ERROR An error occurred while gn was being created 669 * ABORTED gn was not remade because one of its inferiors 670 * could not be made due to errors. 671 */ 672 errors = 0; 673 while (!Lst_IsEmpty (targs)) { 674 gn = (GNode *) Lst_DeQueue (targs); 675 CompatMake (gn, gn); 676 677 if (gn->made == UPTODATE) { 678 printf ("`%s' is up to date.\n", gn->name); 679 } else if (gn->made == ABORTED) { 680 printf ("`%s' not remade because of errors.\n", gn->name); 681 errors += 1; 682 } 683 } 684 685 /* 686 * If the user has defined a .END target, run its commands. 687 */ 688 if (errors == 0) { 689 Lst_ForEach(ENDNode->commands, CompatRunCommand, (ClientData)gn); 690 if (gn->made == ERROR) { 691 PrintOnError("\n\nStop."); 692 exit(1); 693 } 694 } 695 } 696