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
43 static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
45 static const char rcsid[] =
52 * The routines in this file implement the full-compatibility
53 * mode of PMake. Most of the special functionality of PMake
54 * is available in this mode. Things not supported:
56 * - friendly variable substitution.
59 * Compat_Run Initialize things for this module and recreate
60 * thems as need creatin'
64 #include <sys/types.h>
77 * The following array is used to make a fast determination of which
78 * characters are interpreted specially by the shell. If a command
79 * contains any of these characters, it is executed by the shell, not
83 static char meta[256];
85 static GNode *curTarg = NILGNODE;
86 static GNode *ENDNode;
87 static void CompatInterrupt __P((int));
88 static int CompatRunCommand __P((ClientData, ClientData));
89 static int CompatMake __P((ClientData, ClientData));
91 static char *sh_builtin[] = {
92 "alias", "cd", "eval", "exec", "exit", "read", "set", "ulimit",
93 "unalias", "umask", "unset", "wait", ":", 0};
96 *-----------------------------------------------------------------------
98 * Interrupt the creation of the current target and remove it if
105 * The target is removed and the process exits. If .INTERRUPT exists,
106 * its commands are run first WITH INTERRUPTS IGNORED..
108 *-----------------------------------------------------------------------
111 CompatInterrupt (signo)
116 if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) {
118 char *file = Var_Value (TARGET, curTarg, &p1);
120 if (!noExecute && eunlink(file) != -1) {
121 printf ("*** %s removed\n", file);
126 * Run .INTERRUPT only if hit with interrupt signal
128 if (signo == SIGINT) {
129 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
130 if (gn != NILGNODE) {
131 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
136 if (signo == SIGQUIT)
138 (void) signal(signo, SIG_DFL);
139 (void) kill(getpid(), signo);
143 *-----------------------------------------------------------------------
147 * Returns 1 if a specified line must be executed by the shell,
148 * 0 if it can be run via execve, and -1 if the command is a no-op.
153 *-----------------------------------------------------------------------
162 av = brk_string(cmd, &ac, TRUE);
163 for(p = sh_builtin; *p != 0; p++)
164 if (strcmp(av[1], *p) == 0)
170 *-----------------------------------------------------------------------
171 * CompatRunCommand --
172 * Execute the next command for a target. If the command returns an
173 * error, the node's made field is set to ERROR and creation stops.
176 * 0 if the command succeeded, 1 if an error occurred.
179 * The node's 'made' field may be set to ERROR.
181 *-----------------------------------------------------------------------
184 CompatRunCommand (cmdp, gnp)
185 ClientData cmdp; /* Command to execute */
186 ClientData gnp; /* Node from which the command came */
188 char *cmdStart; /* Start of expanded command */
190 Boolean silent, /* Don't print command */
191 errCheck; /* Check errors */
192 int reason; /* Reason for child's death */
193 int status; /* Description of child's death */
194 int cpid; /* Child actually found */
195 ReturnStatus stat; /* Status of fork */
196 LstNode cmdNode; /* Node where current command is located */
197 char **av; /* Argument vector for thing to exec */
198 int argc; /* Number of arguments in av or 0 if not
199 * dynamically allocated */
200 Boolean local; /* TRUE if command should be executed
202 int internal; /* Various values.. */
203 char *cmd = (char *) cmdp;
204 GNode *gn = (GNode *) gnp;
207 * Avoid clobbered variable warnings by forcing the compiler
208 * to ``unregister'' variables
214 silent = gn->type & OP_SILENT;
215 errCheck = !(gn->type & OP_IGNORE);
217 cmdNode = Lst_Member (gn->commands, (ClientData)cmd);
218 cmdStart = Var_Subst (NULL, cmd, gn, FALSE);
221 * brk_string will return an argv with a NULL in av[0], thus causing
222 * execvp to choke and die horribly. Besides, how can we execute a null
223 * command? In any case, we warn the user that the command expanded to
224 * nothing (is this the right thing to do?).
227 if (*cmdStart == '\0') {
229 Error("%s expands to empty string", cmd);
234 Lst_Replace (cmdNode, (ClientData)cmdStart);
236 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
237 (void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart);
239 } else if (strcmp(cmdStart, "...") == 0) {
240 gn->type |= OP_SAVE_CMDS;
244 while ((*cmd == '@') || (*cmd == '-')) {
253 while (isspace((unsigned char)*cmd))
257 * Search for meta characters in the command. If there are no meta
258 * characters, there's no need to execute a shell to execute the
261 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
266 * Print the command before echoing if we're not supposed to be quiet for
267 * this one. We also print the command if -n given.
269 if (!silent || noExecute) {
270 printf ("%s\n", cmd);
275 * If we're not supposed to execute any commands, this is as far as
284 * If *cp isn't the null character, we hit a "meta" character and
285 * need to pass the command off to the shell. We give the shell the
286 * -e flag as well as -c if it's supposed to exit when it hits an
289 static char *shargv[4] = { "/bin/sh" };
291 shargv[1] = (errCheck ? "-ec" : "-c");
293 shargv[3] = (char *)NULL;
296 } else if ((internal = shellneed(cmd))) {
298 * This command must be passed by the shell for other reasons..
299 * or.. possibly not at all.
301 static char *shargv[4] = { "/bin/sh" };
303 if (internal == -1) {
304 /* Command does not need to be executed */
308 shargv[1] = (errCheck ? "-ec" : "-c");
310 shargv[3] = (char *)NULL;
315 * No meta-characters, so no need to exec a shell. Break the command
316 * into words to form an argument vector we can execute.
317 * brk_string sticks our name in av[0], so we have to
320 av = brk_string(cmd, &argc, TRUE);
327 * Fork and execute the single command. If the fork fails, we abort.
331 Fatal("Could not fork");
336 (void) write (2, av[0], strlen (av[0]));
337 (void) write (2, ":", 1);
338 (void) write (2, strerror(errno), strlen(strerror(errno)));
339 (void) write (2, "\n", 1);
341 (void)execv(av[0], av);
347 * we need to print out the command associated with this Gnode in
348 * Targ_PrintCmd from Targ_PrintGraph when debugging at level g2,
349 * in main(), Fatal() and DieHorribly(), therefore do not free it
352 if (!DEBUG(GRAPH2)) {
354 Lst_Replace (cmdNode, cmdp);
358 * The child is off and running. Now all we can do is wait...
362 while ((stat = wait(&reason)) != cpid) {
363 if (stat == -1 && errno != EINTR) {
369 if (WIFSTOPPED(reason)) {
370 status = WSTOPSIG(reason); /* stopped */
371 } else if (WIFEXITED(reason)) {
372 status = WEXITSTATUS(reason); /* exited */
374 printf ("*** Error code %d", status);
377 status = WTERMSIG(reason); /* signaled */
378 printf ("*** Signal %d", status);
382 if (!WIFEXITED(reason) || (status != 0)) {
387 * Abort the current target, but let others
390 printf (" (continuing)\n");
394 * Continue executing commands for this target.
395 * If we return 0, this will happen...
397 printf (" (ignored)\n");
403 Fatal ("error in wait: %d", stat);
412 *-----------------------------------------------------------------------
420 * If an error is detected and not being ignored, the process exits.
422 *-----------------------------------------------------------------------
425 CompatMake (gnp, pgnp)
426 ClientData gnp; /* The node to make */
427 ClientData pgnp; /* Parent to abort if necessary */
429 GNode *gn = (GNode *) gnp;
430 GNode *pgn = (GNode *) pgnp;
431 if (gn->type & OP_USE) {
432 Make_HandleUse(gn, pgn);
433 } else if (gn->made == UNMADE) {
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
443 gn->made = BEINGMADE;
445 Lst_ForEach (gn->children, CompatMake, (ClientData)gn);
452 if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
454 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
459 * All the children were made ok. Now cmtime contains the modification
460 * time of the newest child, we need to find out if we exist and when
461 * we were modified last. The criteria for datedness are defined by the
462 * Make_OODate function.
465 printf("Examining %s...", gn->name);
467 if (! Make_OODate(gn)) {
470 printf("up-to-date.\n");
473 } else if (DEBUG(MAKE)) {
474 printf("out-of-date.\n");
478 * If the user is just seeing if something is out-of-date, exit now
479 * to tell him/her "yes".
486 * We need to be re-made. We also have to make sure we've got a $?
487 * variable. To be nice, we also define the $> variable using
493 * Alter our type to tell if errors should be ignored or things
494 * should not be printed so CompatRunCommand knows what to do.
496 if (Targ_Ignore (gn)) {
497 gn->type |= OP_IGNORE;
499 if (Targ_Silent (gn)) {
500 gn->type |= OP_SILENT;
503 if (Job_CheckCommands (gn, Fatal)) {
505 * Our commands are ok, but we still have to worry about the -t
510 Lst_ForEach (gn->commands, CompatRunCommand, (ClientData)gn);
513 Job_Touch (gn, gn->type & OP_SILENT);
519 if (gn->made != ERROR) {
521 * If the node was made successfully, mark it so, update
522 * its modification time and timestamp all its parents. Note
523 * that for .ZEROTIME targets, the timestamping isn't done.
524 * This is to keep its state from affecting that of its parent.
529 * We can't re-stat the thing, but we can at least take care of
530 * rules where a target depends on a source that actually creates
531 * the target, but only if it has changed, e.g.
539 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
541 * In this case, if the definitions produced by yacc haven't
542 * changed from before, parse.h won't have been updated and
543 * gn->mtime will reflect the current modification time for
544 * parse.h. This is something of a kludge, I admit, but it's a
547 * XXX: People like to use a rule like
551 * To force things that depend on FRC to be made, so we have to
552 * check for gn->children being empty as well...
554 if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
559 * This is what Make does and it's actually a good thing, as it
562 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
564 * to function as intended. Unfortunately, thanks to the stateless
565 * nature of NFS (and the speed of this program), there are times
566 * when the modification time of a file created on a remote
567 * machine will not be modified before the stat() implied by
568 * the Dir_MTime occurs, thus leading us to believe that the file
569 * is unchanged, wreaking havoc with files that depend on this one.
571 * I have decided it is better to make too much than to make too
572 * little, so this stuff is commented out unless you're sure it's
576 if (noExecute || Dir_MTime(gn) == 0) {
579 if (gn->cmtime > gn->mtime)
580 gn->mtime = gn->cmtime;
582 printf("update time: %s\n", Targ_FmtTime(gn->mtime));
585 if (!(gn->type & OP_EXEC)) {
586 pgn->childMade = TRUE;
587 Make_TimeStamp(pgn, gn);
589 } else if (keepgoing) {
594 printf ("\n\nStop in %s.\n", Var_Value(".CURDIR", gn, &p1));
598 } else if (gn->made == ERROR) {
600 * Already had an error when making this beastie. Tell the parent
605 if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
607 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
612 Error("Graph cycles through %s\n", gn->name);
617 if ((gn->type & OP_EXEC) == 0) {
618 pgn->childMade = TRUE;
619 Make_TimeStamp(pgn, gn);
623 if ((gn->type & OP_EXEC) == 0) {
624 Make_TimeStamp(pgn, gn);
636 *-----------------------------------------------------------------------
638 * Initialize this mode and start making.
646 *-----------------------------------------------------------------------
650 Lst targs; /* List of target nodes to re-create */
652 char *cp; /* Pointer to string of shell meta-characters */
653 GNode *gn = NULL;/* Current root target */
654 int errors; /* Number of targets not remade due to errors */
656 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
657 signal(SIGINT, CompatInterrupt);
659 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
660 signal(SIGTERM, CompatInterrupt);
662 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
663 signal(SIGHUP, CompatInterrupt);
665 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
666 signal(SIGQUIT, CompatInterrupt);
669 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
670 meta[(unsigned char) *cp] = 1;
673 * The null character serves as a sentinel in the string.
677 ENDNode = Targ_FindNode(".END", TARG_CREATE);
679 * If the user has defined a .BEGIN target, execute the commands attached
683 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
684 if (gn != NILGNODE) {
685 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
686 if (gn->made == ERROR) {
687 printf("\n\nStop.\n");
694 * For each entry in the list of targets to create, call CompatMake on
695 * it to create the thing. CompatMake will leave the 'made' field of gn
696 * in one of several states:
697 * UPTODATE gn was already up-to-date
698 * MADE gn was recreated successfully
699 * ERROR An error occurred while gn was being created
700 * ABORTED gn was not remade because one of its inferiors
701 * could not be made due to errors.
704 while (!Lst_IsEmpty (targs)) {
705 gn = (GNode *) Lst_DeQueue (targs);
708 if (gn->made == UPTODATE) {
709 printf ("`%s' is up to date.\n", gn->name);
710 } else if (gn->made == ABORTED) {
711 printf ("`%s' not remade because of errors.\n", gn->name);
717 * If the user has defined a .END target, run its commands.
720 Lst_ForEach(ENDNode->commands, CompatRunCommand, (ClientData)gn);