Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make: in Cmd_Exec, return error message instead...



details:   https://anonhg.NetBSD.org/src/rev/a66c349880e6
branches:  trunk
changeset: 359562:a66c349880e6
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jan 09 18:49:28 2022 +0000

description:
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.

diffstat:

 usr.bin/make/Makefile  |   7 +++----
 usr.bin/make/main.c    |  42 +++++++++++++++++++-----------------------
 usr.bin/make/nonints.h |   4 ++--
 usr.bin/make/parse.c   |  20 ++++++++++----------
 usr.bin/make/var.c     |  48 +++++++++++++++++++++++++++---------------------
 5 files changed, 61 insertions(+), 60 deletions(-)

diffs (283 lines):

diff -r d1217e92ee48 -r a66c349880e6 usr.bin/make/Makefile
--- a/usr.bin/make/Makefile     Sun Jan 09 18:27:23 2022 +0000
+++ b/usr.bin/make/Makefile     Sun Jan 09 18:49:28 2022 +0000
@@ -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 @@
 
 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
 
diff -r d1217e92ee48 -r a66c349880e6 usr.bin/make/main.c
--- a/usr.bin/make/main.c       Sun Jan 09 18:27:23 2022 +0000
+++ b/usr.bin/make/main.c       Sun Jan 09 18:49:28 2022 +0000
@@ -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 @@
 
 /*
  * 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 @@
        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 @@
        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 @@
                /* NOTREACHED */
 
        case -1:
-               *errfmt = "Couldn't exec \"%s\"";
+               *error = str_concat3("Couldn't exec \"", cmd, "\"");
                return bmake_strdup("");
        }
 
@@ -1772,20 +1765,23 @@
 
        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;
 }
 
 /*
diff -r d1217e92ee48 -r a66c349880e6 usr.bin/make/nonints.h
--- a/usr.bin/make/nonints.h    Sun Jan 09 18:27:23 2022 +0000
+++ b/usr.bin/make/nonints.h    Sun Jan 09 18:49:28 2022 +0000
@@ -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 @@
 
 /* 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;
diff -r d1217e92ee48 -r a66c349880e6 usr.bin/make/parse.c
--- a/usr.bin/make/parse.c      Sun Jan 09 18:27:23 2022 +0000
+++ b/usr.bin/make/parse.c      Sun Jan 09 18:49:28 2022 +0000
@@ -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 @@
                    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 @@
                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);
 }
diff -r d1217e92ee48 -r a66c349880e6 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sun Jan 09 18:27:23 2022 +0000
+++ b/usr.bin/make/var.c        Sun Jan 09 18:49:28 2022 +0000
@@ -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 @@
 ApplyModifier_ShellCommand(const char **pp, ModChain *ch)
 {
        Expr *expr = ch->expr;
-       const char *errfmt;
        VarParseResult res;
        LazyBuf cmdBuf;
        FStr cmd;
@@ -2690,14 +2689,18 @@
                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 @@
        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 @@
        *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);
        }
 



Home | Main Index | Thread Index | Old Index