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