Module Name:    src
Committed By:   martin
Date:           Thu Nov 23 13:26:01 UTC 2017

Modified Files:
        src/bin/sh [netbsd-8]: output.c

Log Message:
Pull up following revision(s) (requested by kre in ticket #384):
        bin/sh/output.c: revision 1.37
Improve quoting in xtrace (-x) output ... if a string ("word") to be
output includes a single quote (') then see if using double-quotes
to quote it is reasonable (if no chars that are magic in " also appear).
If so, and if the string is not entirely the ' character, then
use " quoting.  This avoids some ugly looking results (occasionally).
Also, fix a bug introduced about 20 months ago where null strings
in xtrace output are dropped, instead of made explicit ('').
To observe this, before you get the fix: set -x; echo ''   (or similar.)
Move a comment from the wrong place to the right place.


To generate a diff of this commit:
cvs rdiff -u -r1.36 -r1.36.2.1 src/bin/sh/output.c

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

Modified files:

Index: src/bin/sh/output.c
diff -u src/bin/sh/output.c:1.36 src/bin/sh/output.c:1.36.2.1
--- src/bin/sh/output.c:1.36	Thu May 18 13:31:10 2017
+++ src/bin/sh/output.c	Thu Nov 23 13:26:01 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: output.c,v 1.36 2017/05/18 13:31:10 kre Exp $	*/
+/*	$NetBSD: output.c,v 1.36.2.1 2017/11/23 13:26:01 martin Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)output.c	8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: output.c,v 1.36 2017/05/18 13:31:10 kre Exp $");
+__RCSID("$NetBSD: output.c,v 1.36.2.1 2017/11/23 13:26:01 martin Exp $");
 #endif
 #endif /* not lint */
 
@@ -146,6 +146,11 @@ out2shstr(const char *p)
 }
 
 
+/*
+ * ' is in this list, not because it does not require quoting
+ * (which applies to all the others) but because '' quoting cannot
+ * be used to quote it.
+ */
 static const char norm_chars [] = \
     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/+-=_,.'";
 
@@ -158,25 +163,42 @@ inquote(const char *p)
 	return s == NULL ? p[l] != '\0' : s - p > (off_t)l;
 }
 
-
 void
 outshstr(const char *p, struct output *file)
 {
-	/*
-	 * ' is in this list, not because it does not require quoting
-	 * (which applies to all the others) but because '' quoting cannot
-	 * be used to quote it.
-	 */
-	int need_q = p[0] == 0 || p[strspn(p, norm_chars)] != 0;
+	int need_q;
 	int inq;
 	char c;
 
+	if (strchr(p, '\'') != NULL && p[1] != '\0') {
+		/*
+		 * string contains ' in it, and is not only the '
+		 * see if " quoting will work
+		 */
+		size_t i = strcspn(p, "\\\"$`");
+
+		if (p[i] == '\0') {
+			/*
+			 * string contains no $ ` \ or " chars, perfect...
+			 *
+			 * We know it contains ' so needs quoting, so
+			 * this is easy...
+			 */
+			outc('"', file);
+			outstr(p, file);
+			outc('"', file);
+			return;
+		}
+	}
+
+	need_q = p[0] == 0 || p[strspn(p, norm_chars)] != 0;
+
 	/*
 	 * Don't emit ' unless something needs quoting before closing '
 	 */
-	if (need_q) {
-		if ((inq = inquote(p)) != 0)
-			outc('\'', file);
+	if (need_q && (p[0] == 0 || inquote(p) != 0)) {
+		outc('\'', file);
+		inq = 1;
 	} else
 		inq = 0;
 

Reply via email to