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