1 /* $NetBSD: compat.c,v 1.58 2005/05/08 04:19:12 christos 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.58 2005/05/08 04:19:12 christos 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.58 2005/05/08 04:19:12 christos 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 126 static void 127 Compat_Init(void) 128 { 129 const char *cp; 130 131 Shell_Init(); /* setup default shell */ 132 133 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) { 134 meta[(unsigned char) *cp] = 1; 135 } 136 /* 137 * The null character serves as a sentinel in the string. 138 */ 139 meta[0] = 1; 140 } 141 142 /*- 143 *----------------------------------------------------------------------- 144 * CompatInterrupt -- 145 * Interrupt the creation of the current target and remove it if 146 * it ain't precious. 147 * 148 * Results: 149 * None. 150 * 151 * Side Effects: 152 * The target is removed and the process exits. If .INTERRUPT exists, 153 * its commands are run first WITH INTERRUPTS IGNORED.. 154 * 155 *----------------------------------------------------------------------- 156 */ 157 static void 158 CompatInterrupt(int signo) 159 { 160 GNode *gn; 161 162 if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) { 163 char *p1; 164 char *file = Var_Value(TARGET, curTarg, &p1); 165 166 if (!noExecute && eunlink(file) != -1) { 167 Error("*** %s removed", file); 168 } 169 if (p1) 170 free(p1); 171 172 /* 173 * Run .INTERRUPT only if hit with interrupt signal 174 */ 175 if (signo == SIGINT) { 176 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 177 if (gn != NILGNODE) { 178 Compat_Make(gn, gn); 179 } 180 } 181 182 } 183 exit (signo); 184 } 185 186 /*- 187 *----------------------------------------------------------------------- 188 * CompatRunCommand -- 189 * Execute the next command for a target. If the command returns an 190 * error, the node's made field is set to ERROR and creation stops. 191 * 192 * Input: 193 * cmdp Command to execute 194 * gnp Node from which the command came 195 * 196 * Results: 197 * 0 if the command succeeded, 1 if an error occurred. 198 * 199 * Side Effects: 200 * The node's 'made' field may be set to ERROR. 201 * 202 *----------------------------------------------------------------------- 203 */ 204 int 205 CompatRunCommand(ClientData cmdp, ClientData gnp) 206 { 207 char *cmdStart; /* Start of expanded command */ 208 char *cp, *bp; 209 Boolean silent, /* Don't print command */ 210 doIt, /* Execute even if -n */ 211 errCheck; /* Check errors */ 212 int reason; /* Reason for child's death */ 213 int status; /* Description of child's death */ 214 int cpid; /* Child actually found */ 215 ReturnStatus retstat; /* Status of fork */ 216 LstNode cmdNode; /* Node where current command is located */ 217 const char **av; /* Argument vector for thing to exec */ 218 int argc; /* Number of arguments in av or 0 if not 219 * dynamically allocated */ 220 Boolean local; /* TRUE if command should be executed 221 * locally */ 222 char *cmd = (char *) cmdp; 223 GNode *gn = (GNode *) gnp; 224 225 /* 226 * Avoid clobbered variable warnings by forcing the compiler 227 * to ``unregister'' variables 228 */ 229 #if __GNUC__ 230 (void) &av; 231 (void) &errCheck; 232 (void) &cmd; 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 * Compat_Make -- 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 int 451 Compat_Make(ClientData gnp, ClientData pgnp) 452 { 453 GNode *gn = (GNode *) gnp; 454 GNode *pgn = (GNode *) pgnp; 455 456 if (!meta[0]) /* we came here from jobs */ 457 Compat_Init(); 458 if (gn->made == UNMADE && (gn == pgn || (pgn->type & OP_MADE) == 0)) { 459 /* 460 * First mark ourselves to be made, then apply whatever transformations 461 * the suffix module thinks are necessary. Once that's done, we can 462 * descend and make all our children. If any of them has an error 463 * but the -k flag was given, our 'make' field will be set FALSE again. 464 * This is our signal to not attempt to do anything but abort our 465 * parent as well. 466 */ 467 gn->flags |= REMAKE; 468 gn->made = BEINGMADE; 469 if ((gn->type & OP_MADE) == 0) 470 Suff_FindDeps(gn); 471 Lst_ForEach(gn->children, Compat_Make, (ClientData)gn); 472 if ((gn->flags & REMAKE) == 0) { 473 gn->made = ABORTED; 474 pgn->flags &= ~REMAKE; 475 goto cohorts; 476 } 477 478 if (Lst_Member(gn->iParents, pgn) != NILLNODE) { 479 char *p1; 480 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0); 481 if (p1) 482 free(p1); 483 } 484 485 /* 486 * All the children were made ok. Now cmtime contains the modification 487 * time of the newest child, we need to find out if we exist and when 488 * we were modified last. The criteria for datedness are defined by the 489 * Make_OODate function. 490 */ 491 if (DEBUG(MAKE)) { 492 printf("Examining %s...", gn->name); 493 } 494 if (! Make_OODate(gn)) { 495 gn->made = UPTODATE; 496 if (DEBUG(MAKE)) { 497 printf("up-to-date.\n"); 498 } 499 goto cohorts; 500 } else if (DEBUG(MAKE)) { 501 printf("out-of-date.\n"); 502 } 503 504 /* 505 * If the user is just seeing if something is out-of-date, exit now 506 * to tell him/her "yes". 507 */ 508 if (queryFlag) { 509 exit (1); 510 } 511 512 /* 513 * We need to be re-made. We also have to make sure we've got a $? 514 * variable. To be nice, we also define the $> variable using 515 * Make_DoAllVar(). 516 */ 517 Make_DoAllVar(gn); 518 519 /* 520 * Alter our type to tell if errors should be ignored or things 521 * should not be printed so CompatRunCommand knows what to do. 522 */ 523 if (Targ_Ignore(gn)) { 524 gn->type |= OP_IGNORE; 525 } 526 if (Targ_Silent(gn)) { 527 gn->type |= OP_SILENT; 528 } 529 530 if (Job_CheckCommands(gn, Fatal)) { 531 /* 532 * Our commands are ok, but we still have to worry about the -t 533 * flag... 534 */ 535 if (!touchFlag || (gn->type & OP_MAKE)) { 536 curTarg = gn; 537 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn); 538 curTarg = NILGNODE; 539 } else { 540 Job_Touch(gn, gn->type & OP_SILENT); 541 } 542 } else { 543 gn->made = ERROR; 544 } 545 546 if (gn->made != ERROR) { 547 /* 548 * If the node was made successfully, mark it so, update 549 * its modification time and timestamp all its parents. Note 550 * that for .ZEROTIME targets, the timestamping isn't done. 551 * This is to keep its state from affecting that of its parent. 552 */ 553 gn->made = MADE; 554 pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0; 555 if (!(gn->type & OP_EXEC)) { 556 pgn->flags |= CHILDMADE; 557 Make_TimeStamp(pgn, gn); 558 } 559 } else if (keepgoing) { 560 pgn->flags &= ~REMAKE; 561 } else { 562 PrintOnError("\n\nStop."); 563 exit (1); 564 } 565 } else if (gn->made == ERROR) { 566 /* 567 * Already had an error when making this beastie. Tell the parent 568 * to abort. 569 */ 570 pgn->flags &= ~REMAKE; 571 } else { 572 if (Lst_Member(gn->iParents, pgn) != NILLNODE) { 573 char *p1; 574 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0); 575 if (p1) 576 free(p1); 577 } 578 switch(gn->made) { 579 case BEINGMADE: 580 Error("Graph cycles through %s", gn->name); 581 gn->made = ERROR; 582 pgn->flags &= ~REMAKE; 583 break; 584 case MADE: 585 if ((gn->type & OP_EXEC) == 0) { 586 pgn->flags |= CHILDMADE; 587 Make_TimeStamp(pgn, gn); 588 } 589 break; 590 case UPTODATE: 591 if ((gn->type & OP_EXEC) == 0) { 592 Make_TimeStamp(pgn, gn); 593 } 594 break; 595 default: 596 break; 597 } 598 } 599 600 cohorts: 601 Lst_ForEach(gn->cohorts, Compat_Make, pgnp); 602 return (0); 603 } 604 605 /*- 606 *----------------------------------------------------------------------- 607 * Compat_Run -- 608 * Initialize this mode and start making. 609 * 610 * Input: 611 * targs List of target nodes to re-create 612 * 613 * Results: 614 * None. 615 * 616 * Side Effects: 617 * Guess what? 618 * 619 *----------------------------------------------------------------------- 620 */ 621 void 622 Compat_Run(Lst targs) 623 { 624 GNode *gn = NULL;/* Current root target */ 625 int errors; /* Number of targets not remade due to errors */ 626 627 Compat_Init(); 628 629 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 630 signal(SIGINT, CompatInterrupt); 631 } 632 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 633 signal(SIGTERM, CompatInterrupt); 634 } 635 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 636 signal(SIGHUP, CompatInterrupt); 637 } 638 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 639 signal(SIGQUIT, CompatInterrupt); 640 } 641 642 ENDNode = Targ_FindNode(".END", TARG_CREATE); 643 ENDNode->type = OP_SPECIAL; 644 /* 645 * If the user has defined a .BEGIN target, execute the commands attached 646 * to it. 647 */ 648 if (!queryFlag) { 649 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE); 650 if (gn != NILGNODE) { 651 Compat_Make(gn, gn); 652 if (gn->made == ERROR) { 653 PrintOnError("\n\nStop."); 654 exit(1); 655 } 656 } 657 } 658 659 /* 660 * Expand .USE nodes right now, because they can modify the structure 661 * of the tree. 662 */ 663 Lst_Destroy(Make_ExpandUse(targs), NOFREE); 664 665 /* 666 * For each entry in the list of targets to create, call Compat_Make on 667 * it to create the thing. Compat_Make will leave the 'made' field of gn 668 * in one of several states: 669 * UPTODATE gn was already up-to-date 670 * MADE gn was recreated successfully 671 * ERROR An error occurred while gn was being created 672 * ABORTED gn was not remade because one of its inferiors 673 * could not be made due to errors. 674 */ 675 errors = 0; 676 while (!Lst_IsEmpty (targs)) { 677 gn = (GNode *) Lst_DeQueue(targs); 678 Compat_Make(gn, gn); 679 680 if (gn->made == UPTODATE) { 681 printf ("`%s' is up to date.\n", gn->name); 682 } else if (gn->made == ABORTED) { 683 printf ("`%s' not remade because of errors.\n", gn->name); 684 errors += 1; 685 } 686 } 687 688 /* 689 * If the user has defined a .END target, run its commands. 690 */ 691 if (errors == 0) { 692 Compat_Make(ENDNode, ENDNode); 693 if (gn->made == ERROR) { 694 PrintOnError("\n\nStop."); 695 exit(1); 696 } 697 } 698 } 699