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(1): replace macro DBPRINTF with JobPrintln



details:   https://anonhg.NetBSD.org/src/rev/4f3745a46047
branches:  trunk
changeset: 946023:4f3745a46047
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Nov 14 13:07:39 2020 +0000

description:
make(1): replace macro DBPRINTF with JobPrintln

This macro had been bad in several ways.  Its name started with DB, an
unnecessary abbreviation for DEBUG.  Many places that used this macro
used it with the same format string, "%s\n".  This format string can
better be expressed in a function name, JobPrintln.

Only in a few places was the macro used with different format strings.
The one for "set -x" was even forced to arbitrarily separate the
argument from the format string in order to match the expected macro
parameters.

A better choice would have been to use the standard form "%s\n", "set
-x" from the beginning.  Anyway, that call is calling JobPrintln as well
now.

The remaining templates are user-specified, and if anyone should ever
define a completely custom shell with echo handling and error handling
enabled, they will easily crash make when the templates don't contain
exactly one %s conversion.  That's the responsibility of the user, and
it has always been.

diffstat:

 usr.bin/make/job.c |  65 +++++++++++++++++++++++++++++------------------------
 1 files changed, 36 insertions(+), 29 deletions(-)

diffs (145 lines):

diff -r 3528bf60d806 -r 4f3745a46047 usr.bin/make/job.c
--- a/usr.bin/make/job.c        Sat Nov 14 13:05:14 2020 +0000
+++ b/usr.bin/make/job.c        Sat Nov 14 13:07:39 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: job.c,v 1.317 2020/11/14 12:38:06 rillig Exp $ */
+/*     $NetBSD: job.c,v 1.318 2020/11/14 13:07:39 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -143,7 +143,7 @@
 #include "trace.h"
 
 /*     "@(#)job.c      8.2 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: job.c,v 1.317 2020/11/14 12:38:06 rillig Exp $");
+MAKE_RCSID("$NetBSD: job.c,v 1.318 2020/11/14 13:07:39 rillig Exp $");
 
 /* A shell defines how the commands are run.  All commands for a target are
  * written into a single file, which is then given to the shell to execute
@@ -692,6 +692,22 @@
     return esc;
 }
 
+static void
+JobPrintf(Job *job, const char *fmt, const char *arg)
+{
+    if (DEBUG(JOB))
+       debug_printf(fmt, arg);
+
+    (void)fprintf(job->cmdFILE, fmt, arg);
+    (void)fflush(job->cmdFILE);
+}
+
+static void
+JobPrintln(Job *job, const char *line)
+{
+    JobPrintf(job, "%s\n", line);
+}
+
 /*-
  *-----------------------------------------------------------------------
  * JobPrintCommand  --
@@ -733,12 +749,6 @@
 
     noSpecials = !GNode_ShouldExecute(job->node);
 
-#define DBPRINTF(fmt, arg) if (DEBUG(JOB)) {   \
-       debug_printf(fmt, arg);                 \
-    }                                          \
-    (void)fprintf(job->cmdFILE, fmt, arg);     \
-    (void)fflush(job->cmdFILE);
-
     numCommands++;
 
     Var_Subst(cmd, job->node, VARE_WANTRES, &cmd);
@@ -771,7 +781,7 @@
     if (shutUp) {
        if (!(job->flags & JOB_SILENT) && !noSpecials &&
            (commandShell->hasEchoCtl)) {
-           DBPRINTF("%s\n", commandShell->echoOff);
+           JobPrintln(job, commandShell->echoOff);
        } else {
            if (commandShell->hasErrCtl)
                shutUp = FALSE;
@@ -791,11 +801,11 @@
                 */
                if (!(job->flags & JOB_SILENT) && !shutUp &&
                    (commandShell->hasEchoCtl)) {
-                   DBPRINTF("%s\n", commandShell->echoOff);
-                   DBPRINTF("%s\n", commandShell->errOffOrExecIgnore);
-                   DBPRINTF("%s\n", commandShell->echoOn);
+                   JobPrintln(job, commandShell->echoOff);
+                   JobPrintln(job, commandShell->errOffOrExecIgnore);
+                   JobPrintln(job,  commandShell->echoOn);
                } else {
-                   DBPRINTF("%s\n", commandShell->errOffOrExecIgnore);
+                   JobPrintln(job, commandShell->errOffOrExecIgnore);
                }
            } else if (commandShell->errOffOrExecIgnore &&
                       commandShell->errOffOrExecIgnore[0] != '\0') {
@@ -811,14 +821,13 @@
                job->flags |= JOB_IGNERR;
                if (!(job->flags & JOB_SILENT) && !shutUp) {
                    if (commandShell->hasEchoCtl) {
-                       DBPRINTF("%s\n", commandShell->echoOff);
+                       JobPrintln(job, commandShell->echoOff);
                    }
-                   DBPRINTF(commandShell->errOnOrEcho, escCmd);
+                   JobPrintf(job, commandShell->errOnOrEcho, escCmd);
                    shutUp = TRUE;
                } else {
-                   if (!shutUp) {
-                       DBPRINTF(commandShell->errOnOrEcho, escCmd);
-                   }
+                   if (!shutUp)
+                       JobPrintf(job, commandShell->errOnOrEcho, escCmd);
                }
                cmdTemplate = commandShell->errOffOrExecIgnore;
                /*
@@ -844,10 +853,9 @@
        if (!commandShell->hasErrCtl && commandShell->errExit &&
            commandShell->errExit[0] != '\0') {
            if (!(job->flags & JOB_SILENT) && !shutUp) {
-               if (commandShell->hasEchoCtl) {
-                   DBPRINTF("%s\n", commandShell->echoOff);
-               }
-               DBPRINTF(commandShell->errOnOrEcho, escCmd);
+               if (commandShell->hasEchoCtl)
+                   JobPrintln(job, commandShell->echoOff);
+               JobPrintf(job, commandShell->errOnOrEcho, escCmd);
                shutUp = TRUE;
            }
            /* If it's a comment line or blank, treat as an ignored error */
@@ -862,11 +870,11 @@
 
     if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0 &&
        !(job->flags & JOB_TRACED)) {
-       DBPRINTF("set -%s\n", "x");
+       JobPrintln(job, "set -x");
        job->flags |= JOB_TRACED;
     }
 
-    DBPRINTF(cmdTemplate, cmd);
+    JobPrintf(job, cmdTemplate, cmd);
     free(cmdStart);
     free(escCmd);
     if (errOff) {
@@ -876,14 +884,13 @@
         * for the whole command...
         */
        if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl) {
-           DBPRINTF("%s\n", commandShell->echoOff);
+           JobPrintln(job, commandShell->echoOff);
            shutUp = TRUE;
        }
-       DBPRINTF("%s\n", commandShell->errOnOrEcho);
+       JobPrintln(job, commandShell->errOnOrEcho);
     }
-    if (shutUp && commandShell->hasEchoCtl) {
-       DBPRINTF("%s\n", commandShell->echoOn);
-    }
+    if (shutUp && commandShell->hasEchoCtl)
+       JobPrintln(job, commandShell->echoOn);
 }
 
 /* Print all commands to the shell file that is later executed.



Home | Main Index | Thread Index | Old Index