1 /* $NetBSD: compat.c,v 1.84 2011/09/16 15:38:03 joerg Exp $ */
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
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. 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.
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
36 * Copyright (c) 1988, 1989 by Adam de Boor
37 * Copyright (c) 1989 by Berkeley Softworks
38 * All rights reserved.
40 * This code is derived from software contributed to Berkeley by
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
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.
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
73 static char rcsid
[] = "$NetBSD: compat.c,v 1.84 2011/09/16 15:38:03 joerg Exp $";
75 #include <sys/cdefs.h>
78 static char sccsid
[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
80 __RCSID("$NetBSD: compat.c,v 1.84 2011/09/16 15:38:03 joerg Exp $");
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:
91 * - friendly variable substitution.
94 * Compat_Run Initialize things for this module and recreate
95 * thems as need creatin'
98 #include <sys/types.h>
100 #include <sys/wait.h>
111 #include "pathnames.h"
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
120 static char meta
[256];
122 static GNode
*curTarg
= NULL
;
123 static GNode
*ENDNode
;
124 static void CompatInterrupt(int) __dead
;
131 Shell_Init(); /* setup default shell */
133 for (cp
= "#=|^(){};&<>*?[]:$`\\\n"; *cp
!= '\0'; cp
++) {
134 meta
[(unsigned char) *cp
] = 1;
137 * The null character serves as a sentinel in the string.
143 *-----------------------------------------------------------------------
145 * Interrupt the creation of the current target and remove it if
152 * The target is removed and the process exits. If .INTERRUPT exists,
153 * its commands are run first WITH INTERRUPTS IGNORED..
155 *-----------------------------------------------------------------------
158 CompatInterrupt(int signo
)
162 if ((curTarg
!= NULL
) && !Targ_Precious (curTarg
)) {
164 char *file
= Var_Value(TARGET
, curTarg
, &p1
);
166 if (!noExecute
&& eunlink(file
) != -1) {
167 Error("*** %s removed", file
);
173 * Run .INTERRUPT only if hit with interrupt signal
175 if (signo
== SIGINT
) {
176 gn
= Targ_FindNode(".INTERRUPT", TARG_NOCREATE
);
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.
193 * cmdp Command to execute
194 * gnp Node from which the command came
197 * 0 if the command succeeded, 1 if an error occurred.
200 * The node's 'made' field may be set to ERROR.
202 *-----------------------------------------------------------------------
205 CompatRunCommand(void *cmdp
, void *gnp
)
207 char *cmdStart
; /* Start of expanded command */
209 Boolean silent
, /* Don't print command */
210 doIt
; /* Execute even if -n */
211 volatile Boolean errCheck
; /* Check errors */
212 int reason
; /* Reason for child's death */
213 int status
; /* Description of child's death */
214 pid_t cpid
; /* Child actually found */
215 pid_t retstat
; /* Result of wait */
216 LstNode cmdNode
; /* Node where current command is located */
217 const char ** volatile av
; /* Argument vector for thing to exec */
218 char ** volatile mav
;/* Copy of the argument vector for freeing */
219 int argc
; /* Number of arguments in av or 0 if not
220 * dynamically allocated */
221 Boolean local
; /* TRUE if command should be executed
223 Boolean useShell
; /* TRUE if command should be executed
225 char * volatile cmd
= (char *)cmdp
;
226 GNode
*gn
= (GNode
*)gnp
;
228 silent
= gn
->type
& OP_SILENT
;
229 errCheck
= !(gn
->type
& OP_IGNORE
);
232 cmdNode
= Lst_Member(gn
->commands
, cmd
);
233 cmdStart
= Var_Subst(NULL
, cmd
, gn
, FALSE
);
236 * brk_string will return an argv with a NULL in av[0], thus causing
237 * execvp to choke and die horribly. Besides, how can we execute a null
238 * command? In any case, we warn the user that the command expanded to
239 * nothing (is this the right thing to do?).
242 if (*cmdStart
== '\0') {
244 Error("%s expands to empty string", cmd
);
248 Lst_Replace(cmdNode
, cmdStart
);
250 if ((gn
->type
& OP_SAVE_CMDS
) && (gn
!= ENDNode
)) {
251 (void)Lst_AtEnd(ENDNode
->commands
, cmdStart
);
254 if (strcmp(cmdStart
, "...") == 0) {
255 gn
->type
|= OP_SAVE_CMDS
;
259 while ((*cmd
== '@') || (*cmd
== '-') || (*cmd
== '+')) {
262 silent
= DEBUG(LOUD
) ? FALSE
: TRUE
;
269 if (!meta
[0]) /* we came here from jobs */
276 while (isspace((unsigned char)*cmd
))
279 #if !defined(MAKE_NATIVE)
281 * In a non-native build, the host environment might be weird enough
282 * that it's necessary to go through a shell to get the correct
283 * behaviour. Or perhaps the shell has been replaced with something
284 * that does extra logging, and that should not be bypassed.
289 * Search for meta characters in the command. If there are no meta
290 * characters, there's no need to execute a shell to execute the
293 for (cp
= cmd
; !meta
[(unsigned char)*cp
]; cp
++) {
296 useShell
= (*cp
!= '\0');
300 * Print the command before echoing if we're not supposed to be quiet for
301 * this one. We also print the command if -n given.
303 if (!silent
|| NoExecute(gn
)) {
309 * If we're not supposed to execute any commands, this is as far as
312 if (!doIt
&& NoExecute(gn
)) {
316 fprintf(debug_file
, "Execute: '%s'\n", cmd
);
321 * We need to pass the command off to the shell, typically
322 * because the command contains a "meta" character.
324 static const char *shargv
[4];
326 shargv
[0] = shellPath
;
328 * The following work for any of the builtin shell specs.
342 * No meta-characters, so no need to exec a shell. Break the command
343 * into words to form an argument vector we can execute.
345 mav
= brk_string(cmd
, &argc
, TRUE
, &bp
);
362 * Fork and execute the single command. If the fork fails, we abort.
366 Fatal("Could not fork");
377 (void)execvp(av
[0], (char *const *)UNCONST(av
));
379 (void)execv(av
[0], (char *const *)UNCONST(av
));
380 execError("exec", av
[0]);
387 Lst_Replace(cmdNode
, NULL
);
391 meta_compat_parent();
396 * The child is off and running. Now all we can do is wait...
400 while ((retstat
= wait(&reason
)) != cpid
) {
402 JobReapChild(retstat
, reason
, FALSE
); /* not ours? */
403 if (retstat
== -1 && errno
!= EINTR
) {
409 if (WIFSTOPPED(reason
)) {
410 status
= WSTOPSIG(reason
); /* stopped */
411 } else if (WIFEXITED(reason
)) {
412 status
= WEXITSTATUS(reason
); /* exited */
413 #if defined(USE_META) && defined(USE_FILEMON_ONCE)
415 meta_cmd_finish(NULL
);
420 fprintf(debug_file
, "\n*** Failed target: %s\n*** Failed command: ",
422 for (cp
= cmd
; *cp
; ) {
423 if (isspace((unsigned char)*cp
)) {
424 fprintf(debug_file
, " ");
425 while (isspace((unsigned char)*cp
))
428 fprintf(debug_file
, "%c", *cp
);
432 fprintf(debug_file
, "\n");
434 printf("*** Error code %d", status
);
437 status
= WTERMSIG(reason
); /* signaled */
438 printf("*** Signal %d", status
);
442 if (!WIFEXITED(reason
) || (status
!= 0)) {
446 meta_job_error(NULL
, gn
, 0, status
);
452 * Abort the current target, but let others
455 printf(" (continuing)\n");
459 * Continue executing commands for this target.
460 * If we return 0, this will happen...
462 printf(" (ignored)\n");
468 Fatal("error in wait: %d: %s", retstat
, strerror(errno
));
478 *-----------------------------------------------------------------------
483 * gnp The node to make
484 * pgnp Parent to abort if necessary
490 * If an error is detected and not being ignored, the process exits.
492 *-----------------------------------------------------------------------
495 Compat_Make(void *gnp
, void *pgnp
)
497 GNode
*gn
= (GNode
*)gnp
;
498 GNode
*pgn
= (GNode
*)pgnp
;
500 if (!meta
[0]) /* we came here from jobs */
502 if (gn
->made
== UNMADE
&& (gn
== pgn
|| (pgn
->type
& OP_MADE
) == 0)) {
504 * First mark ourselves to be made, then apply whatever transformations
505 * the suffix module thinks are necessary. Once that's done, we can
506 * descend and make all our children. If any of them has an error
507 * but the -k flag was given, our 'make' field will be set FALSE again.
508 * This is our signal to not attempt to do anything but abort our
512 gn
->made
= BEINGMADE
;
513 if ((gn
->type
& OP_MADE
) == 0)
515 Lst_ForEach(gn
->children
, Compat_Make
, gn
);
516 if ((gn
->flags
& REMAKE
) == 0) {
518 pgn
->flags
&= ~REMAKE
;
522 if (Lst_Member(gn
->iParents
, pgn
) != NULL
) {
524 Var_Set(IMPSRC
, Var_Value(TARGET
, gn
, &p1
), pgn
, 0);
530 * All the children were made ok. Now cmgn->mtime contains the
531 * modification time of the newest child, we need to find out if we
532 * exist and when we were modified last. The criteria for datedness
533 * are defined by the Make_OODate function.
536 fprintf(debug_file
, "Examining %s...", gn
->name
);
538 if (! Make_OODate(gn
)) {
541 fprintf(debug_file
, "up-to-date.\n");
544 } else if (DEBUG(MAKE
)) {
545 fprintf(debug_file
, "out-of-date.\n");
549 * If the user is just seeing if something is out-of-date, exit now
550 * to tell him/her "yes".
557 * We need to be re-made. We also have to make sure we've got a $?
558 * variable. To be nice, we also define the $> variable using
564 * Alter our type to tell if errors should be ignored or things
565 * should not be printed so CompatRunCommand knows what to do.
567 if (Targ_Ignore(gn
)) {
568 gn
->type
|= OP_IGNORE
;
570 if (Targ_Silent(gn
)) {
571 gn
->type
|= OP_SILENT
;
574 if (Job_CheckCommands(gn
, Fatal
)) {
576 * Our commands are ok, but we still have to worry about the -t
579 if (!touchFlag
|| (gn
->type
& OP_MAKE
)) {
582 if (useMeta
&& !NoExecute(gn
)) {
583 meta_job_start(NULL
, gn
);
586 Lst_ForEach(gn
->commands
, CompatRunCommand
, gn
);
589 Job_Touch(gn
, gn
->type
& OP_SILENT
);
595 if (useMeta
&& !NoExecute(gn
)) {
596 meta_job_finish(NULL
);
600 if (gn
->made
!= ERROR
) {
602 * If the node was made successfully, mark it so, update
603 * its modification time and timestamp all its parents. Note
604 * that for .ZEROTIME targets, the timestamping isn't done.
605 * This is to keep its state from affecting that of its parent.
608 pgn
->flags
|= Make_Recheck(gn
) == 0 ? FORCE
: 0;
609 if (!(gn
->type
& OP_EXEC
)) {
610 pgn
->flags
|= CHILDMADE
;
611 Make_TimeStamp(pgn
, gn
);
613 } else if (keepgoing
) {
614 pgn
->flags
&= ~REMAKE
;
616 PrintOnError(gn
, "\n\nStop.");
619 } else if (gn
->made
== ERROR
) {
621 * Already had an error when making this beastie. Tell the parent
624 pgn
->flags
&= ~REMAKE
;
626 if (Lst_Member(gn
->iParents
, pgn
) != NULL
) {
628 Var_Set(IMPSRC
, Var_Value(TARGET
, gn
, &p1
), pgn
, 0);
634 Error("Graph cycles through %s", gn
->name
);
636 pgn
->flags
&= ~REMAKE
;
639 if ((gn
->type
& OP_EXEC
) == 0) {
640 pgn
->flags
|= CHILDMADE
;
641 Make_TimeStamp(pgn
, gn
);
645 if ((gn
->type
& OP_EXEC
) == 0) {
646 Make_TimeStamp(pgn
, gn
);
655 Lst_ForEach(gn
->cohorts
, Compat_Make
, pgnp
);
660 *-----------------------------------------------------------------------
662 * Initialize this mode and start making.
665 * targs List of target nodes to re-create
673 *-----------------------------------------------------------------------
676 Compat_Run(Lst targs
)
678 GNode
*gn
= NULL
;/* Current root target */
679 int errors
; /* Number of targets not remade due to errors */
683 if (bmake_signal(SIGINT
, SIG_IGN
) != SIG_IGN
) {
684 bmake_signal(SIGINT
, CompatInterrupt
);
686 if (bmake_signal(SIGTERM
, SIG_IGN
) != SIG_IGN
) {
687 bmake_signal(SIGTERM
, CompatInterrupt
);
689 if (bmake_signal(SIGHUP
, SIG_IGN
) != SIG_IGN
) {
690 bmake_signal(SIGHUP
, CompatInterrupt
);
692 if (bmake_signal(SIGQUIT
, SIG_IGN
) != SIG_IGN
) {
693 bmake_signal(SIGQUIT
, CompatInterrupt
);
696 ENDNode
= Targ_FindNode(".END", TARG_CREATE
);
697 ENDNode
->type
= OP_SPECIAL
;
699 * If the user has defined a .BEGIN target, execute the commands attached
703 gn
= Targ_FindNode(".BEGIN", TARG_NOCREATE
);
706 if (gn
->made
== ERROR
) {
707 PrintOnError(gn
, "\n\nStop.");
714 * Expand .USE nodes right now, because they can modify the structure
717 Make_ExpandUse(targs
);
720 * For each entry in the list of targets to create, call Compat_Make on
721 * it to create the thing. Compat_Make will leave the 'made' field of gn
722 * in one of several states:
723 * UPTODATE gn was already up-to-date
724 * MADE gn was recreated successfully
725 * ERROR An error occurred while gn was being created
726 * ABORTED gn was not remade because one of its inferiors
727 * could not be made due to errors.
730 while (!Lst_IsEmpty (targs
)) {
731 gn
= (GNode
*)Lst_DeQueue(targs
);
734 if (gn
->made
== UPTODATE
) {
735 printf("`%s' is up to date.\n", gn
->name
);
736 } else if (gn
->made
== ABORTED
) {
737 printf("`%s' not remade because of errors.\n", gn
->name
);
743 * If the user has defined a .END target, run its commands.
746 Compat_Make(ENDNode
, ENDNode
);
747 if (gn
->made
== ERROR
) {
748 PrintOnError(gn
, "\n\nStop.");