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): extract EscapeShellDblQuot from JobPri...



details:   https://anonhg.NetBSD.org/src/rev/7b058fb79e57
branches:  trunk
changeset: 956560:7b058fb79e57
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Nov 01 17:07:03 2020 +0000

description:
make(1): extract EscapeShellDblQuot from JobPrintCommand

diffstat:

 usr.bin/make/job.c |  37 ++++++++++++++++++++++---------------
 1 files changed, 22 insertions(+), 15 deletions(-)

diffs (65 lines):

diff -r 54a2e4aad619 -r 7b058fb79e57 usr.bin/make/job.c
--- a/usr.bin/make/job.c        Sun Nov 01 16:57:02 2020 +0000
+++ b/usr.bin/make/job.c        Sun Nov 01 17:07:03 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: job.c,v 1.298 2020/11/01 16:57:02 rillig Exp $ */
+/*     $NetBSD: job.c,v 1.299 2020/11/01 17:07:03 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.298 2020/11/01 16:57:02 rillig Exp $");
+MAKE_RCSID("$NetBSD: job.c,v 1.299 2020/11/01 17:07:03 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
@@ -674,6 +674,24 @@
     *pp = p;
 }
 
+/* Escape a string for a double-quoted string literal in sh, csh and ksh. */
+static char *
+EscapeShellDblQuot(const char *cmd)
+{
+    size_t i, j;
+
+    /* Worst that could happen is every char needs escaping. */
+    char *esc = bmake_malloc(strlen(cmd) * 2 + 1);
+    for (i = 0, j = 0; cmd[i] != '\0'; i++, j++) {
+       if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' || cmd[i] == '"')
+           esc[j++] = '\\';
+       esc[j] = cmd[i];
+    }
+    esc[j] = '\0';
+
+    return esc;
+}
+
 /*-
  *-----------------------------------------------------------------------
  * JobPrintCommand  --
@@ -747,19 +765,8 @@
      * and this will need the characters '$ ` \ "' escaped
      */
 
-    if (!commandShell->hasErrCtl) {
-       int i, j;
-
-       /* Worst that could happen is every char needs escaping. */
-       escCmd = bmake_malloc((strlen(cmd) * 2) + 1);
-       for (i = 0, j = 0; cmd[i] != '\0'; i++, j++) {
-           if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' ||
-               cmd[i] == '"')
-               escCmd[j++] = '\\';
-           escCmd[j] = cmd[i];
-       }
-       escCmd[j] = '\0';
-    }
+    if (!commandShell->hasErrCtl)
+        escCmd = EscapeShellDblQuot(cmd);
 
     if (shutUp) {
        if (!(job->flags & JOB_SILENT) && !noSpecials &&



Home | Main Index | Thread Index | Old Index