1 /* $NetBSD: compat.c,v 1.55 2004/07/01 04:39:30 jmc 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.55 2004/07/01 04:39:30 jmc 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.55 2004/07/01 04:39:30 jmc 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 (void) &cmd; 234 #endif 235 silent = gn->type & OP_SILENT; 236 errCheck = !(gn->type & OP_IGNORE); 237 doIt = FALSE; 238 239 cmdNode = Lst_Member (gn->commands, (ClientData)cmd); 240 cmdStart = Var_Subst (NULL, cmd, gn, FALSE); 241 242 /* 243 * brk_string will return an argv with a NULL in av[0], thus causing 244 * execvp to choke and die horribly. Besides, how can we execute a null 245 * command? In any case, we warn the user that the command expanded to 246 * nothing (is this the right thing to do?). 247 */ 248 249 if (*cmdStart == '\0') { 250 free(cmdStart); 251 Error("%s expands to empty string", cmd); 252 return(0); 253 } else { 254 cmd = cmdStart; 255 } 256 Lst_Replace (cmdNode, (ClientData)cmdStart); 257 258 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) { 259 (void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart); 260 return(0); 261 } else if (strcmp(cmdStart, "...") == 0) { 262 gn->type |= OP_SAVE_CMDS; 263 return(0); 264 } 265 266 while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) { 267 switch (*cmd) { 268 case '@': 269 silent = TRUE; 270 break; 271 case '-': 272 errCheck = FALSE; 273 break; 274 case '+': 275 doIt = TRUE; 276 if (!meta[0]) /* we came here from jobs */ 277 Compat_Init(); 278 break; 279 } 280 cmd++; 281 } 282 283 while (isspace((unsigned char)*cmd)) 284 cmd++; 285 286 /* 287 * Search for meta characters in the command. If there are no meta 288 * characters, there's no need to execute a shell to execute the 289 * command. 290 */ 291 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) { 292 continue; 293 } 294 295 /* 296 * Print the command before echoing if we're not supposed to be quiet for 297 * this one. We also print the command if -n given. 298 */ 299 if (!silent || NoExecute(gn)) { 300 printf ("%s\n", cmd); 301 fflush(stdout); 302 } 303 304 /* 305 * If we're not supposed to execute any commands, this is as far as 306 * we go... 307 */ 308 if (!doIt && NoExecute(gn)) { 309 return (0); 310 } 311 312 if (*cp != '\0') { 313 /* 314 * If *cp isn't the null character, we hit a "meta" character and 315 * need to pass the command off to the shell. 316 */ 317 static const char *shargv[4]; 318 319 shargv[0] = shellPath; 320 /* 321 * The following work for any of the builtin shell specs. 322 */ 323 if (DEBUG(SHELL)) 324 shargv[1] = "-xc"; 325 else 326 shargv[1] = "-c"; 327 shargv[2] = cmd; 328 shargv[3] = (char *)NULL; 329 av = shargv; 330 argc = 0; 331 bp = NULL; 332 } else { 333 /* 334 * No meta-characters, so no need to exec a shell. Break the command 335 * into words to form an argument vector we can execute. 336 */ 337 av = (const char **)brk_string(cmd, &argc, TRUE, &bp); 338 } 339 340 local = TRUE; 341 342 /* 343 * Fork and execute the single command. If the fork fails, we abort. 344 */ 345 cpid = vfork(); 346 if (cpid < 0) { 347 Fatal("Could not fork"); 348 } 349 if (cpid == 0) { 350 Check_Cwd(av); 351 if (local) 352 (void)execvp(av[0], (char *const *)UNCONST(av)); 353 else 354 (void)execv(av[0], (char *const *)UNCONST(av)); 355 execError("exec", av[0]); 356 _exit(1); 357 } 358 if (bp) { 359 free(av); 360 free(bp); 361 } 362 Lst_Replace (cmdNode, (ClientData) NULL); 363 364 /* 365 * The child is off and running. Now all we can do is wait... 366 */ 367 while (1) { 368 369 while ((retstat = wait(&reason)) != cpid) { 370 if (retstat == -1 && errno != EINTR) { 371 break; 372 } 373 } 374 375 if (retstat > -1) { 376 if (WIFSTOPPED(reason)) { 377 status = WSTOPSIG(reason); /* stopped */ 378 } else if (WIFEXITED(reason)) { 379 status = WEXITSTATUS(reason); /* exited */ 380 if (status != 0) { 381 if (DEBUG(ERROR)) { 382 printf("\n*** Failed target: %s\n*** Failed command: ", 383 gn->name); 384 for (cp = cmd; *cp; ) { 385 if (isspace((unsigned char)*cp)) { 386 putchar(' '); 387 while (isspace((unsigned char)*cp)) 388 cp++; 389 } else { 390 putchar(*cp); 391 cp++; 392 } 393 } 394 printf ("\n"); 395 } 396 printf ("*** Error code %d", status); 397 } 398 } else { 399 status = WTERMSIG(reason); /* signaled */ 400 printf ("*** Signal %d", status); 401 } 402 403 404 if (!WIFEXITED(reason) || (status != 0)) { 405 if (errCheck) { 406 gn->made = ERROR; 407 if (keepgoing) { 408 /* 409 * Abort the current target, but let others 410 * continue. 411 */ 412 printf (" (continuing)\n"); 413 } 414 } else { 415 /* 416 * Continue executing commands for this target. 417 * If we return 0, this will happen... 418 */ 419 printf (" (ignored)\n"); 420 status = 0; 421 } 422 } 423 break; 424 } else { 425 Fatal ("error in wait: %d: %s", retstat, strerror(errno)); 426 /*NOTREACHED*/ 427 } 428 } 429 free(cmdStart); 430 431 return (status); 432 } 433 434 /*- 435 *----------------------------------------------------------------------- 436 * CompatMake -- 437 * Make a target. 438 * 439 * Input: 440 * gnp The node to make 441 * pgnp Parent to abort if necessary 442 * 443 * Results: 444 * 0 445 * 446 * Side Effects: 447 * If an error is detected and not being ignored, the process exits. 448 * 449 *----------------------------------------------------------------------- 450 */ 451 static int 452 CompatMake(ClientData gnp, ClientData pgnp) 453 { 454 GNode *gn = (GNode *) gnp; 455 GNode *pgn = (GNode *) pgnp; 456 457 if (gn->made == UNMADE && (gn == pgn || (pgn->type & OP_MADE) == 0)) { 458 /* 459 * First mark ourselves to be made, then apply whatever transformations 460 * the suffix module thinks are necessary. Once that's done, we can 461 * descend and make all our children. If any of them has an error 462 * but the -k flag was given, our 'make' field will be set FALSE again. 463 * This is our signal to not attempt to do anything but abort our 464 * parent as well. 465 */ 466 gn->flags |= REMAKE; 467 gn->made = BEINGMADE; 468 if ((gn->type & OP_MADE) == 0) 469 Suff_FindDeps (gn); 470 Lst_ForEach (gn->children, CompatMake, (ClientData)gn); 471 if ((gn->flags & REMAKE) == 0) { 472 gn->made = ABORTED; 473 pgn->flags &= ~REMAKE; 474 goto cohorts; 475 } 476 477 if (Lst_Member (gn->iParents, pgn) != NILLNODE) { 478 char *p1; 479 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0); 480 if (p1) 481 free(p1); 482 } 483 484 /* 485 * All the children were made ok. Now cmtime contains the modification 486 * time of the newest child, we need to find out if we exist and when 487 * we were modified last. The criteria for datedness are defined by the 488 * Make_OODate function. 489 */ 490 if (DEBUG(MAKE)) { 491 printf("Examining %s...", gn->name); 492 } 493 if (! Make_OODate(gn)) { 494 gn->made = UPTODATE; 495 if (DEBUG(MAKE)) { 496 printf("up-to-date.\n"); 497 } 498 goto cohorts; 499 } else if (DEBUG(MAKE)) { 500 printf("out-of-date.\n"); 501 } 502 503 /* 504 * If the user is just seeing if something is out-of-date, exit now 505 * to tell him/her "yes". 506 */ 507 if (queryFlag) { 508 exit (1); 509 } 510 511 /* 512 * We need to be re-made. We also have to make sure we've got a $? 513 * variable. To be nice, we also define the $> variable using 514 * Make_DoAllVar(). 515 */ 516 Make_DoAllVar(gn); 517 518 /* 519 * Alter our type to tell if errors should be ignored or things 520 * should not be printed so CompatRunCommand knows what to do. 521 */ 522 if (Targ_Ignore (gn)) { 523 gn->type |= OP_IGNORE; 524 } 525 if (Targ_Silent (gn)) { 526 gn->type |= OP_SILENT; 527 } 528 529 if (Job_CheckCommands (gn, Fatal)) { 530 /* 531 * Our commands are ok, but we still have to worry about the -t 532 * flag... 533 */ 534 if (!touchFlag || (gn->type & OP_MAKE)) { 535 curTarg = gn; 536 Lst_ForEach (gn->commands, CompatRunCommand, (ClientData)gn); 537 curTarg = NILGNODE; 538 } else { 539 Job_Touch (gn, gn->type & OP_SILENT); 540 } 541 } else { 542 gn->made = ERROR; 543 } 544 545 if (gn->made != ERROR) { 546 /* 547 * If the node was made successfully, mark it so, update 548 * its modification time and timestamp all its parents. Note 549 * that for .ZEROTIME targets, the timestamping isn't done. 550 * This is to keep its state from affecting that of its parent. 551 */ 552 gn->made = MADE; 553 pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0; 554 if (!(gn->type & OP_EXEC)) { 555 pgn->flags |= CHILDMADE; 556 Make_TimeStamp(pgn, gn); 557 } 558 } else if (keepgoing) { 559 pgn->flags &= ~REMAKE; 560 } else { 561 PrintOnError("\n\nStop."); 562 exit (1); 563 } 564 } else if (gn->made == ERROR) { 565 /* 566 * Already had an error when making this beastie. Tell the parent 567 * to abort. 568 */ 569 pgn->flags &= ~REMAKE; 570 } else { 571 if (Lst_Member (gn->iParents, pgn) != NILLNODE) { 572 char *p1; 573 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0); 574 if (p1) 575 free(p1); 576 } 577 switch(gn->made) { 578 case BEINGMADE: 579 Error("Graph cycles through %s", gn->name); 580 gn->made = ERROR; 581 pgn->flags &= ~REMAKE; 582 break; 583 case MADE: 584 if ((gn->type & OP_EXEC) == 0) { 585 pgn->flags |= CHILDMADE; 586 Make_TimeStamp(pgn, gn); 587 } 588 break; 589 case UPTODATE: 590 if ((gn->type & OP_EXEC) == 0) { 591 Make_TimeStamp(pgn, gn); 592 } 593 break; 594 default: 595 break; 596 } 597 } 598 599 cohorts: 600 Lst_ForEach (gn->cohorts, CompatMake, pgnp); 601 return (0); 602 } 603 604 /*- 605 *----------------------------------------------------------------------- 606 * Compat_Run -- 607 * Initialize this mode and start making. 608 * 609 * Input: 610 * targs List of target nodes to re-create 611 * 612 * Results: 613 * None. 614 * 615 * Side Effects: 616 * Guess what? 617 * 618 *----------------------------------------------------------------------- 619 */ 620 void 621 Compat_Run(Lst targs) 622 { 623 GNode *gn = NULL;/* Current root target */ 624 int errors; /* Number of targets not remade due to errors */ 625 626 Compat_Init(); 627 628 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 629 signal(SIGINT, CompatInterrupt); 630 } 631 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 632 signal(SIGTERM, CompatInterrupt); 633 } 634 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 635 signal(SIGHUP, CompatInterrupt); 636 } 637 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 638 signal(SIGQUIT, CompatInterrupt); 639 } 640 641 ENDNode = Targ_FindNode(".END", TARG_CREATE); 642 /* 643 * If the user has defined a .BEGIN target, execute the commands attached 644 * to it. 645 */ 646 if (!queryFlag) { 647 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE); 648 if (gn != NILGNODE) { 649 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn); 650 if (gn->made == ERROR) { 651 PrintOnError("\n\nStop."); 652 exit(1); 653 } 654 } 655 } 656 657 /* 658 * Expand .USE nodes right now, because they can modify the structure 659 * of the tree. 660 */ 661 Lst_Destroy(Make_ExpandUse(targs), NOFREE); 662 663 /* 664 * For each entry in the list of targets to create, call CompatMake on 665 * it to create the thing. CompatMake will leave the 'made' field of gn 666 * in one of several states: 667 * UPTODATE gn was already up-to-date 668 * MADE gn was recreated successfully 669 * ERROR An error occurred while gn was being created 670 * ABORTED gn was not remade because one of its inferiors 671 * could not be made due to errors. 672 */ 673 errors = 0; 674 while (!Lst_IsEmpty (targs)) { 675 gn = (GNode *) Lst_DeQueue (targs); 676 CompatMake (gn, gn); 677 678 if (gn->made == UPTODATE) { 679 printf ("`%s' is up to date.\n", gn->name); 680 } else if (gn->made == ABORTED) { 681 printf ("`%s' not remade because of errors.\n", gn->name); 682 errors += 1; 683 } 684 } 685 686 /* 687 * If the user has defined a .END target, run its commands. 688 */ 689 if (errors == 0) { 690 Lst_ForEach(ENDNode->commands, CompatRunCommand, (ClientData)gn); 691 if (gn->made == ERROR) { 692 PrintOnError("\n\nStop."); 693 exit(1); 694 } 695 } 696 } 697