summaryrefslogtreecommitdiff
path: root/compat.c
diff options
context:
space:
mode:
Diffstat (limited to 'compat.c')
-rw-r--r--compat.c57
1 files changed, 31 insertions, 26 deletions
diff --git a/compat.c b/compat.c
index 3a8b1c4b2640..5d1b3ab52344 100644
--- a/compat.c
+++ b/compat.c
@@ -1,4 +1,4 @@
-/* $NetBSD: compat.c,v 1.255 2024/04/20 10:18:55 rillig Exp $ */
+/* $NetBSD: compat.c,v 1.259 2024/06/15 20:02:45 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -94,7 +94,7 @@
#include "pathnames.h"
/* "@(#)compat.c 8.2 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: compat.c,v 1.255 2024/04/20 10:18:55 rillig Exp $");
+MAKE_RCSID("$NetBSD: compat.c,v 1.259 2024/06/15 20:02:45 rillig Exp $");
static GNode *curTarg = NULL;
static pid_t compatChild;
@@ -203,6 +203,24 @@ UseShell(const char *cmd MAKE_ATTR_UNUSED)
#endif
}
+static int
+Compat_Spawn(const char **av)
+{
+ int pid = vfork();
+ if (pid < 0)
+ Fatal("Could not fork");
+
+ if (pid == 0) {
+#ifdef USE_META
+ if (useMeta)
+ meta_compat_child();
+#endif
+ (void)execvp(av[0], (char *const *)UNCONST(av));
+ execDie("exec", av[0]);
+ }
+ return pid;
+}
+
/*
* Execute the next command for a target. If the command returns an error,
* the node's made field is set to ERROR and creation stops.
@@ -225,21 +243,18 @@ Compat_RunCommand(const char *cmdp, GNode *gn, StringListNode *ln)
volatile bool errCheck; /* Check errors */
WAIT_T reason; /* Reason for child's death */
WAIT_T status; /* Description of child's death */
- pid_t cpid; /* Child actually found */
pid_t retstat; /* Result of wait */
- const char **volatile av; /* Argument vector for thing to exec */
+ const char **av; /* Arguments for the child process */
char **volatile mav; /* Copy of the argument vector for freeing */
bool useShell; /* True if command should be executed using a
* shell */
- const char *volatile cmd = cmdp;
+ const char *cmd = cmdp;
silent = (gn->type & OP_SILENT) != OP_NONE;
errCheck = !(gn->type & OP_IGNORE);
doIt = false;
- EvalStack_Push(gn->name, NULL, NULL);
- cmdStart = Var_Subst(cmd, gn, VARE_WANTRES);
- EvalStack_Pop();
+ cmdStart = Var_SubstInTarget(cmd, gn);
/* TODO: handle errors */
if (cmdStart[0] == '\0') {
@@ -264,11 +279,13 @@ Compat_RunCommand(const char *cmdp, GNode *gn, StringListNode *ln)
* usual '$$'.
*/
Lst_Append(&endNode->commands, cmdStart);
- return true;
+ goto register_command;
}
}
if (strcmp(cmdStart, "...") == 0) {
gn->type |= OP_SAVE_CMDS;
+ register_command:
+ Parse_RegisterCommand(cmdStart);
return true;
}
@@ -288,7 +305,7 @@ Compat_RunCommand(const char *cmdp, GNode *gn, StringListNode *ln)
while (ch_isspace(*cmd))
cmd++;
if (cmd[0] == '\0')
- return true;
+ goto register_command;
useShell = UseShell(cmd);
@@ -298,7 +315,7 @@ Compat_RunCommand(const char *cmdp, GNode *gn, StringListNode *ln)
}
if (!doIt && !GNode_ShouldExecute(gn))
- return true;
+ goto register_command;
DEBUG1(JOB, "Execute: '%s'\n", cmd);
@@ -333,19 +350,7 @@ Compat_RunCommand(const char *cmdp, GNode *gn, StringListNode *ln)
Var_ReexportVars(gn);
- compatChild = cpid = vfork();
- if (cpid < 0)
- Fatal("Could not fork");
-
- if (cpid == 0) {
-#ifdef USE_META
- if (useMeta)
- meta_compat_child();
-#endif
- (void)execvp(av[0], (char *const *)UNCONST(av));
- execDie("exec", av[0]);
- }
-
+ compatChild = Compat_Spawn(av);
free(mav);
free(bp);
@@ -355,11 +360,11 @@ Compat_RunCommand(const char *cmdp, GNode *gn, StringListNode *ln)
#ifdef USE_META
if (useMeta)
- meta_compat_parent(cpid);
+ meta_compat_parent(compatChild);
#endif
/* The child is off and running. Now all we can do is wait... */
- while ((retstat = wait(&reason)) != cpid) {
+ while ((retstat = wait(&reason)) != compatChild) {
if (retstat > 0)
JobReapChild(retstat, reason, false); /* not ours? */
if (retstat == -1 && errno != EINTR)