Module Name:    src
Committed By:   rillig
Date:           Sun Jan  9 18:49:28 UTC 2022

Modified Files:
        src/usr.bin/make: Makefile main.c nonints.h parse.c var.c

Log Message:
make: in Cmd_Exec, return error message instead of format string

This change leaves only literal format strings in parse.c.  It allows
for more detailed error messages than the current "non-zero status" or
"exited on a signal".

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.119 -r1.120 src/usr.bin/make/Makefile
cvs rdiff -u -r1.567 -r1.568 src/usr.bin/make/main.c
cvs rdiff -u -r1.236 -r1.237 src/usr.bin/make/nonints.h
cvs rdiff -u -r1.647 -r1.648 src/usr.bin/make/parse.c
cvs rdiff -u -r1.999 -r1.1000 src/usr.bin/make/var.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/Makefile
diff -u src/usr.bin/make/Makefile:1.119 src/usr.bin/make/Makefile:1.120
--- src/usr.bin/make/Makefile:1.119	Sun Dec 12 10:53:37 2021
+++ src/usr.bin/make/Makefile	Sun Jan  9 18:49:28 2022
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.119 2021/12/12 10:53:37 rillig Exp $
+#	$NetBSD: Makefile,v 1.120 2022/01/09 18:49:28 rillig Exp $
 #	@(#)Makefile	5.2 (Berkeley) 12/28/90
 
 PROG=	make
@@ -122,11 +122,10 @@ CLEANFILES+=	*.o	# for filemon objects
 
 COPTS.arch.c+=	${GCC_NO_FORMAT_TRUNCATION}
 COPTS.dir.c+=	${GCC_NO_FORMAT_TRUNCATION}
-COPTS.job.c+=	-Wno-format-nonliteral
+COPTS.job.c+=	-Wno-format-nonliteral	# custom shell templates
 COPTS.main.c+=	${GCC_NO_FORMAT_TRUNCATION} ${GCC_NO_STRINGOP_TRUNCATION}
 COPTS.meta.c+=	${GCC_NO_FORMAT_TRUNCATION}
-COPTS.parse.c+=	-Wno-format-nonliteral
-COPTS.var.c+=	-Wno-format-nonliteral
+COPTS.var.c+=	-Wno-format-nonliteral	# strftime
 
 CPPFLAGS+=	-DMAKE_NATIVE
 

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.567 src/usr.bin/make/main.c:1.568
--- src/usr.bin/make/main.c:1.567	Fri Jan  7 21:00:49 2022
+++ src/usr.bin/make/main.c	Sun Jan  9 18:49:28 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.567 2022/01/07 21:00:49 rillig Exp $	*/
+/*	$NetBSD: main.c,v 1.568 2022/01/09 18:49:28 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -111,7 +111,7 @@
 #include "trace.h"
 
 /*	"@(#)main.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: main.c,v 1.567 2022/01/07 21:00:49 rillig Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.568 2022/01/09 18:49:28 rillig Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
 	    "The Regents of the University of California.  "
@@ -1697,16 +1697,10 @@ found:
 
 /*
  * Execute the command in cmd, and return its output (only stdout, not
- * stderr).  In the output, replace newlines with spaces.
- *
- * Results:
- *	The output of the command, can be empty.
- *	*errfmt returns a format string describing the command failure,
- *	if any, using a single %s conversion specification.
- *	TODO: replace errfmt with an actual error message.
+ * stderr, possibly empty).  In the output, replace newlines with spaces.
  */
 char *
-Cmd_Exec(const char *cmd, const char **errfmt)
+Cmd_Exec(const char *cmd, char **error)
 {
 	const char *args[4];	/* Arguments for invoking the shell */
 	int pipefds[2];
@@ -1715,12 +1709,10 @@ Cmd_Exec(const char *cmd, const char **e
 	int status;		/* command exit status */
 	Buffer buf;		/* buffer to store the result */
 	ssize_t bytes_read;
-	char *res;		/* result */
+	char *output;
 	char *cp;
 	int saved_errno;
 
-	*errfmt = NULL;
-
 	if (shellName == NULL)
 		Shell_Init();
 
@@ -1730,8 +1722,9 @@ Cmd_Exec(const char *cmd, const char **e
 	args[3] = NULL;
 
 	if (pipe(pipefds) == -1) {
-		*errfmt = "Couldn't create pipe for \"%s\"";
-		return bmake_strdup("");
+		*error = str_concat3(
+		    "Couldn't create pipe for \"", cmd, "\"");
+		return bmake_strdup("");;
 	}
 
 	Var_ReexportVars();
@@ -1747,7 +1740,7 @@ Cmd_Exec(const char *cmd, const char **e
 		/* NOTREACHED */
 
 	case -1:
-		*errfmt = "Couldn't exec \"%s\"";
+		*error = str_concat3("Couldn't exec \"", cmd, "\"");
 		return bmake_strdup("");
 	}
 
@@ -1772,20 +1765,23 @@ Cmd_Exec(const char *cmd, const char **e
 
 	if (Buf_EndsWith(&buf, '\n'))
 		buf.data[buf.len - 1] = '\0';
-	res = Buf_DoneData(&buf);
 
-	for (cp = res; *cp != '\0'; cp++)
+	output = Buf_DoneData(&buf);
+	for (cp = output; *cp != '\0'; cp++)
 		if (*cp == '\n')
 			*cp = ' ';
 
 	if (WIFSIGNALED(status))
-		*errfmt = "\"%s\" exited on a signal";
+		*error = str_concat3("\"", cmd, "\" exited on a signal");
 	else if (WEXITSTATUS(status) != 0)
-		*errfmt = "\"%s\" returned non-zero status";
+		*error = str_concat3(
+		    "\"", cmd, "\" returned non-zero status");
 	else if (saved_errno != 0)
-		*errfmt = "Couldn't read shell's output for \"%s\"";
-
-	return res;
+		*error = str_concat3(
+		    "Couldn't read shell's output for \"", cmd, "\"");
+	else
+		*error = NULL;
+	return output;
 }
 
 /*

Index: src/usr.bin/make/nonints.h
diff -u src/usr.bin/make/nonints.h:1.236 src/usr.bin/make/nonints.h:1.237
--- src/usr.bin/make/nonints.h:1.236	Sun Jan  9 12:43:52 2022
+++ src/usr.bin/make/nonints.h	Sun Jan  9 18:49:28 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: nonints.h,v 1.236 2022/01/09 12:43:52 rillig Exp $	*/
+/*	$NetBSD: nonints.h,v 1.237 2022/01/09 18:49:28 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -129,7 +129,7 @@ void JobReapChild(pid_t, int, bool);
 
 /* main.c */
 void Main_ParseArgLine(const char *);
-char *Cmd_Exec(const char *, const char **) MAKE_ATTR_USE;
+char *Cmd_Exec(const char *, char **) MAKE_ATTR_USE;
 void Error(const char *, ...) MAKE_ATTR_PRINTFLIKE(1, 2);
 void Fatal(const char *, ...) MAKE_ATTR_PRINTFLIKE(1, 2) MAKE_ATTR_DEAD;
 void Punt(const char *, ...) MAKE_ATTR_PRINTFLIKE(1, 2) MAKE_ATTR_DEAD;

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.647 src/usr.bin/make/parse.c:1.648
--- src/usr.bin/make/parse.c:1.647	Sun Jan  9 12:43:52 2022
+++ src/usr.bin/make/parse.c	Sun Jan  9 18:49:28 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.647 2022/01/09 12:43:52 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.648 2022/01/09 18:49:28 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -106,7 +106,7 @@
 #include "pathnames.h"
 
 /*	"@(#)parse.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: parse.c,v 1.647 2022/01/09 12:43:52 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.648 2022/01/09 18:49:28 rillig Exp $");
 
 /*
  * A file being read.
@@ -1618,8 +1618,7 @@ VarAssign_EvalShell(const char *name, co
 		    FStr *out_avalue)
 {
 	FStr cmd;
-	const char *errfmt;
-	char *cmdOut;
+	char *output, *error;
 
 	cmd = FStr_InitRefer(uvalue);
 	if (strchr(cmd.str, '$') != NULL) {
@@ -1630,12 +1629,13 @@ VarAssign_EvalShell(const char *name, co
 		cmd = FStr_InitOwn(expanded);
 	}
 
-	cmdOut = Cmd_Exec(cmd.str, &errfmt);
-	Var_SetExpand(scope, name, cmdOut);
-	*out_avalue = FStr_InitOwn(cmdOut);
-
-	if (errfmt != NULL)
-		Parse_Error(PARSE_WARNING, errfmt, cmd.str);
+	output = Cmd_Exec(cmd.str, &error);
+	Var_SetExpand(scope, name, output);
+	*out_avalue = FStr_InitOwn(output);
+	if (error != NULL) {
+		Parse_Error(PARSE_WARNING, "%s", error);
+		free(error);
+	}
 
 	FStr_Done(&cmd);
 }

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.999 src/usr.bin/make/var.c:1.1000
--- src/usr.bin/make/var.c:1.999	Sun Jan  9 16:56:08 2022
+++ src/usr.bin/make/var.c	Sun Jan  9 18:49:28 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.999 2022/01/09 16:56:08 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.1000 2022/01/09 18:49:28 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -140,7 +140,7 @@
 #include "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.999 2022/01/09 16:56:08 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.1000 2022/01/09 18:49:28 rillig Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -2679,7 +2679,6 @@ static ApplyModifierResult
 ApplyModifier_ShellCommand(const char **pp, ModChain *ch)
 {
 	Expr *expr = ch->expr;
-	const char *errfmt;
 	VarParseResult res;
 	LazyBuf cmdBuf;
 	FStr cmd;
@@ -2690,14 +2689,18 @@ ApplyModifier_ShellCommand(const char **
 		return AMR_CLEANUP;
 	cmd = LazyBuf_DoneGet(&cmdBuf);
 
-
-	errfmt = NULL;
-	if (Expr_ShouldEval(expr))
-		Expr_SetValueOwn(expr, Cmd_Exec(cmd.str, &errfmt));
-	else
+	if (Expr_ShouldEval(expr)) {
+		char *output, *error;
+		output = Cmd_Exec(cmd.str, &error);
+		Expr_SetValueOwn(expr, output);
+		if (error != NULL) {
+			/* XXX: why still return AMR_OK? */
+			Error("%s", error);
+			free(error);
+		}
+	} else
 		Expr_SetValueRefer(expr, "");
-	if (errfmt != NULL)
-		Error(errfmt, cmd.str);	/* XXX: why still return AMR_OK? */
+
 	FStr_Done(&cmd);
 	Expr_Define(expr);
 
@@ -3542,13 +3545,14 @@ found_op:
 	if (op[0] == '+')
 		Var_Append(scope, expr->name, val.str);
 	else if (op[0] == '!') {
-		const char *errfmt;
-		char *cmd_output = Cmd_Exec(val.str, &errfmt);
-		if (errfmt != NULL)
-			Error(errfmt, val.str);
-		else
-			Var_Set(scope, expr->name, cmd_output);
-		free(cmd_output);
+		char *output, *error;
+		output = Cmd_Exec(val.str, &error);
+		if (error != NULL) {
+			Error("%s", error);
+			free(error);
+		} else
+			Var_Set(scope, expr->name, output);
+		free(output);
 	} else if (op[0] == '?' && expr->defined == DEF_REGULAR) {
 		/* Do nothing. */
 	} else
@@ -3734,10 +3738,12 @@ ApplyModifier_SunShell(const char **pp, 
 	*pp = p + 2;
 
 	if (Expr_ShouldEval(expr)) {
-		const char *errfmt;
-		char *output = Cmd_Exec(Expr_Str(expr), &errfmt);
-		if (errfmt != NULL)
-			Error(errfmt, Expr_Str(expr));
+		char *output, *error;
+		output = Cmd_Exec(Expr_Str(expr), &error);
+		if (error != NULL) {
+			Error("%s", error);
+			free(error);
+		}
 		Expr_SetValueOwn(expr, output);
 	}
 

Reply via email to