2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3 * Copyright (c) 1988, 1989 by Adam de Boor
4 * Copyright (c) 1989 by Berkeley Softworks
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
43 static const char rcsid[] =
50 * The routines in this file implement the full-compatibility
51 * mode of PMake. Most of the special functionality of PMake
52 * is available in this mode. Things not supported:
54 * - friendly variable substitution.
57 * Compat_Run Initialize things for this module and recreate
58 * thems as need creatin'
62 #include <sys/types.h>
75 * The following array is used to make a fast determination of which
76 * characters are interpreted specially by the shell. If a command
77 * contains any of these characters, it is executed by the shell, not
81 static char meta[256];
83 static GNode *curTarg = NILGNODE;
84 static GNode *ENDNode;
85 static void CompatInterrupt __P((int));
86 static int CompatRunCommand __P((ClientData, ClientData));
87 static int CompatMake __P((ClientData, ClientData));
89 static char *sh_builtin[] = {
90 "alias", "cd", "eval", "exec", "exit", "read", "set", "ulimit",
91 "unalias", "umask", "unset", "wait", ":", 0};
94 *-----------------------------------------------------------------------
96 * Interrupt the creation of the current target and remove it if
103 * The target is removed and the process exits. If .INTERRUPT exists,
104 * its commands are run first WITH INTERRUPTS IGNORED..
106 *-----------------------------------------------------------------------
109 CompatInterrupt (signo)
114 if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) {
116 char *file = Var_Value (TARGET, curTarg, &p1);
118 if (!noExecute && eunlink(file) != -1) {
119 printf ("*** %s removed\n", file);
124 * Run .INTERRUPT only if hit with interrupt signal
126 if (signo == SIGINT) {
127 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
128 if (gn != NILGNODE) {
129 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
134 if (signo == SIGQUIT)
136 (void) signal(signo, SIG_DFL);
137 (void) kill(getpid(), signo);
141 *-----------------------------------------------------------------------
145 * Returns 1 if a specified line must be executed by the shell,
146 * 0 if it can be run via execve, and -1 if the command is a no-op.
151 *-----------------------------------------------------------------------
160 av = brk_string(cmd, &ac, TRUE);
161 for(p = sh_builtin; *p != 0; p++)
162 if (strcmp(av[1], *p) == 0)
168 *-----------------------------------------------------------------------
169 * CompatRunCommand --
170 * Execute the next command for a target. If the command returns an
171 * error, the node's made field is set to ERROR and creation stops.
174 * 0 if the command succeeded, 1 if an error occurred.
177 * The node's 'made' field may be set to ERROR.
179 *-----------------------------------------------------------------------
182 CompatRunCommand (cmdp, gnp)
183 ClientData cmdp; /* Command to execute */
184 ClientData gnp; /* Node from which the command came */
186 char *cmdStart; /* Start of expanded command */
188 Boolean silent, /* Don't print command */
189 errCheck; /* Check errors */
190 int reason; /* Reason for child's death */
191 int status; /* Description of child's death */
192 int cpid; /* Child actually found */
193 ReturnStatus stat; /* Status of fork */
194 LstNode cmdNode; /* Node where current command is located */
195 char **av; /* Argument vector for thing to exec */
196 int argc; /* Number of arguments in av or 0 if not
197 * dynamically allocated */
198 Boolean local; /* TRUE if command should be executed
200 int internal; /* Various values.. */
201 char *cmd = (char *) cmdp;
202 GNode *gn = (GNode *) gnp;
205 * Avoid clobbered variable warnings by forcing the compiler
206 * to ``unregister'' variables
212 silent = gn->type & OP_SILENT;
213 errCheck = !(gn->type & OP_IGNORE);
215 cmdNode = Lst_Member (gn->commands, (ClientData)cmd);
216 cmdStart = Var_Subst (NULL, cmd, gn, FALSE);
219 * brk_string will return an argv with a NULL in av[0], thus causing
220 * execvp to choke and die horribly. Besides, how can we execute a null
221 * command? In any case, we warn the user that the command expanded to
222 * nothing (is this the right thing to do?).
225 if (*cmdStart == '\0') {
227 Error("%s expands to empty string", cmd);
232 Lst_Replace (cmdNode, (ClientData)cmdStart);
234 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
235 (void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart);
237 } else if (strcmp(cmdStart, "...") == 0) {
238 gn->type |= OP_SAVE_CMDS;
242 while ((*cmd == '@') || (*cmd == '-')) {
251 while (isspace((unsigned char)*cmd))
255 * Search for meta characters in the command. If there are no meta
256 * characters, there's no need to execute a shell to execute the
259 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
264 * Print the command before echoing if we're not supposed to be quiet for
265 * this one. We also print the command if -n given.
267 if (!silent || noExecute) {
268 printf ("%s\n", cmd);
273 * If we're not supposed to execute any commands, this is as far as
282 * If *cp isn't the null character, we hit a "meta" character and
283 * need to pass the command off to the shell. We give the shell the
284 * -e flag as well as -c if it's supposed to exit when it hits an
287 static char *shargv[4] = { "/bin/sh" };
289 shargv[1] = (errCheck ? "-ec" : "-c");
291 shargv[3] = (char *)NULL;
294 } else if ((internal = shellneed(cmd))) {
296 * This command must be passed by the shell for other reasons..
297 * or.. possibly not at all.
299 static char *shargv[4] = { "/bin/sh" };
301 if (internal == -1) {
302 /* Command does not need to be executed */
306 shargv[1] = (errCheck ? "-ec" : "-c");
308 shargv[3] = (char *)NULL;
313 * No meta-characters, so no need to exec a shell. Break the command
314 * into words to form an argument vector we can execute.
315 * brk_string sticks our name in av[0], so we have to
318 av = brk_string(cmd, &argc, TRUE);
325 * Fork and execute the single command. If the fork fails, we abort.
329 Fatal("Could not fork");
334 (void) write (2, av[0], strlen (av[0]));
335 (void) write (2, ":", 1);
336 (void) write (2, strerror(errno), strlen(strerror(errno)));
337 (void) write (2, "\n", 1);
339 (void)execv(av[0], av);
345 * we need to print out the command associated with this Gnode in
346 * Targ_PrintCmd from Targ_PrintGraph when debugging at level g2,
347 * in main(), Fatal() and DieHorribly(), therefore do not free it
350 if (!DEBUG(GRAPH2)) {
352 Lst_Replace (cmdNode, cmdp);
356 * The child is off and running. Now all we can do is wait...
360 while ((stat = wait(&reason)) != cpid) {
361 if (stat == -1 && errno != EINTR) {
367 if (WIFSTOPPED(reason)) {
368 status = WSTOPSIG(reason); /* stopped */
369 } else if (WIFEXITED(reason)) {
370 status = WEXITSTATUS(reason); /* exited */
372 printf ("*** Error code %d", status);
375 status = WTERMSIG(reason); /* signaled */
376 printf ("*** Signal %d", status);
380 if (!WIFEXITED(reason) || (status != 0)) {
385 * Abort the current target, but let others
388 printf (" (continuing)\n");
392 * Continue executing commands for this target.
393 * If we return 0, this will happen...
395 printf (" (ignored)\n");
401 Fatal ("error in wait: %d", stat);
410 *-----------------------------------------------------------------------
418 * If an error is detected and not being ignored, the process exits.
420 *-----------------------------------------------------------------------
423 CompatMake (gnp, pgnp)
424 ClientData gnp; /* The node to make */
425 ClientData pgnp; /* Parent to abort if necessary */
427 GNode *gn = (GNode *) gnp;
428 GNode *pgn = (GNode *) pgnp;
429 if (gn->type & OP_USE) {
430 Make_HandleUse(gn, pgn);
431 } else if (gn->made == UNMADE) {
433 * First mark ourselves to be made, then apply whatever transformations
434 * the suffix module thinks are necessary. Once that's done, we can
435 * descend and make all our children. If any of them has an error
436 * but the -k flag was given, our 'make' field will be set FALSE again.
437 * This is our signal to not attempt to do anything but abort our
441 gn->made = BEINGMADE;
443 Lst_ForEach (gn->children, CompatMake, (ClientData)gn);
450 if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
452 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
457 * All the children were made ok. Now cmtime contains the modification
458 * time of the newest child, we need to find out if we exist and when
459 * we were modified last. The criteria for datedness are defined by the
460 * Make_OODate function.
463 printf("Examining %s...", gn->name);
465 if (! Make_OODate(gn)) {
468 printf("up-to-date.\n");
471 } else if (DEBUG(MAKE)) {
472 printf("out-of-date.\n");
476 * If the user is just seeing if something is out-of-date, exit now
477 * to tell him/her "yes".
484 * We need to be re-made. We also have to make sure we've got a $?
485 * variable. To be nice, we also define the $> variable using
491 * Alter our type to tell if errors should be ignored or things
492 * should not be printed so CompatRunCommand knows what to do.
494 if (Targ_Ignore (gn)) {
495 gn->type |= OP_IGNORE;
497 if (Targ_Silent (gn)) {
498 gn->type |= OP_SILENT;
501 if (Job_CheckCommands (gn, Fatal)) {
503 * Our commands are ok, but we still have to worry about the -t
508 Lst_ForEach (gn->commands, CompatRunCommand, (ClientData)gn);
511 Job_Touch (gn, gn->type & OP_SILENT);
517 if (gn->made != ERROR) {
519 * If the node was made successfully, mark it so, update
520 * its modification time and timestamp all its parents. Note
521 * that for .ZEROTIME targets, the timestamping isn't done.
522 * This is to keep its state from affecting that of its parent.
527 * We can't re-stat the thing, but we can at least take care of
528 * rules where a target depends on a source that actually creates
529 * the target, but only if it has changed, e.g.
537 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
539 * In this case, if the definitions produced by yacc haven't
540 * changed from before, parse.h won't have been updated and
541 * gn->mtime will reflect the current modification time for
542 * parse.h. This is something of a kludge, I admit, but it's a
545 * XXX: People like to use a rule like
549 * To force things that depend on FRC to be made, so we have to
550 * check for gn->children being empty as well...
552 if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
557 * This is what Make does and it's actually a good thing, as it
560 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
562 * to function as intended. Unfortunately, thanks to the stateless
563 * nature of NFS (and the speed of this program), there are times
564 * when the modification time of a file created on a remote
565 * machine will not be modified before the stat() implied by
566 * the Dir_MTime occurs, thus leading us to believe that the file
567 * is unchanged, wreaking havoc with files that depend on this one.
569 * I have decided it is better to make too much than to make too
570 * little, so this stuff is commented out unless you're sure it's
574 if (noExecute || Dir_MTime(gn) == 0) {
577 if (gn->cmtime > gn->mtime)
578 gn->mtime = gn->cmtime;
580 printf("update time: %s\n", Targ_FmtTime(gn->mtime));
583 if (!(gn->type & OP_EXEC)) {
584 pgn->childMade = TRUE;
585 Make_TimeStamp(pgn, gn);
587 } else if (keepgoing) {
592 printf ("\n\nStop in %s.\n", Var_Value(".CURDIR", gn, &p1));
596 } else if (gn->made == ERROR) {
598 * Already had an error when making this beastie. Tell the parent
603 if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
605 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
610 Error("Graph cycles through %s\n", gn->name);
615 if ((gn->type & OP_EXEC) == 0) {
616 pgn->childMade = TRUE;
617 Make_TimeStamp(pgn, gn);
621 if ((gn->type & OP_EXEC) == 0) {
622 Make_TimeStamp(pgn, gn);
634 *-----------------------------------------------------------------------
636 * Initialize this mode and start making.
644 *-----------------------------------------------------------------------
648 Lst targs; /* List of target nodes to re-create */
650 char *cp; /* Pointer to string of shell meta-characters */
651 GNode *gn = NULL;/* Current root target */
652 int errors; /* Number of targets not remade due to errors */
654 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
655 signal(SIGINT, CompatInterrupt);
657 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
658 signal(SIGTERM, CompatInterrupt);
660 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
661 signal(SIGHUP, CompatInterrupt);
663 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
664 signal(SIGQUIT, CompatInterrupt);
667 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
668 meta[(unsigned char) *cp] = 1;
671 * The null character serves as a sentinel in the string.
675 ENDNode = Targ_FindNode(".END", TARG_CREATE);
677 * If the user has defined a .BEGIN target, execute the commands attached
681 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
682 if (gn != NILGNODE) {
683 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
684 if (gn->made == ERROR) {
685 printf("\n\nStop.\n");
692 * For each entry in the list of targets to create, call CompatMake on
693 * it to create the thing. CompatMake will leave the 'made' field of gn
694 * in one of several states:
695 * UPTODATE gn was already up-to-date
696 * MADE gn was recreated successfully
697 * ERROR An error occurred while gn was being created
698 * ABORTED gn was not remade because one of its inferiors
699 * could not be made due to errors.
702 while (!Lst_IsEmpty (targs)) {
703 gn = (GNode *) Lst_DeQueue (targs);
706 if (gn->made == UPTODATE) {
707 printf ("`%s' is up to date.\n", gn->name);
708 } else if (gn->made == ABORTED) {
709 printf ("`%s' not remade because of errors.\n", gn->name);
715 * If the user has defined a .END target, run its commands.
718 Lst_ForEach(ENDNode->commands, CompatRunCommand, (ClientData)gn);