Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(402)

Unified Diff: tools/gn/escape.cc

Issue 305653008: Escape more characters for GN shell writing. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/gn/escape_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/gn/escape.cc
diff --git a/tools/gn/escape.cc b/tools/gn/escape.cc
index 1cc5daafd7848566dc33980e9fce402f7429f0ca..1658a8ad1edd90acc9423fb498ce0ef4b57c9dd2 100644
--- a/tools/gn/escape.cc
+++ b/tools/gn/escape.cc
@@ -8,6 +8,24 @@
namespace {
+// A "1" in this lookup table means that char is valid in the shell.
+const char kShellValid[0x80] = {
+// 00-1f: all are invalid
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+// ' ' ! " # $ % & ' ( ) * + , - . /
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
+// 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0,
+// @ A B C D E F G H I J K L M N O
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+// P Q R S T U V W X Y Z [ \ ] ^ _
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
+// ` a b c d e f g h i j k l m n o
+ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+// p q r s t u v w x y z { | } ~
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0 };
+
template<typename DestString>
void EscapeStringToString(const base::StringPiece& str,
const EscapeOptions& options,
@@ -17,14 +35,12 @@ void EscapeStringToString(const base::StringPiece& str,
for (size_t i = 0; i < str.size(); i++) {
if (str[i] == '$' && (options.mode & ESCAPE_NINJA)) {
- // Escape dollars signs since ninja treats these specially.
+ // Escape dollars signs since ninja treats these specially. If we're also
+ // escaping for the shell, we need to backslash-escape that again.
scottmg 2014/05/30 18:02:15 this should be if !WIN i think
+ if (options.mode & ESCAPE_SHELL)
+ dest->push_back('\\');
dest->push_back('$');
dest->push_back('$');
- } else if (str[i] == '"' && (options.mode & ESCAPE_SHELL)) {
- // Escape quotes with backslashes for the command-line (Ninja doesn't
- // care).
- dest->push_back('\\');
- dest->push_back('"');
} else if (str[i] == ' ') {
if (options.mode & ESCAPE_NINJA) {
// For Ninja just escape spaces with $.
@@ -61,6 +77,12 @@ void EscapeStringToString(const base::StringPiece& str,
} else if (str[i] == ':' && (options.mode & ESCAPE_NINJA)) {
dest->push_back('$');
dest->push_back(':');
+ } else if ((options.mode & ESCAPE_SHELL) &&
scottmg 2014/05/30 18:02:15 and this too, or at least \ doesn't escape them on
+ (static_cast<unsigned>(str[i]) >= 0x80 ||
+ !kShellValid[static_cast<int>(str[i])])) {
+ // All other invalid shell chars get backslash-escaped.
+ dest->push_back('\\');
+ dest->push_back(str[i]);
} else {
dest->push_back(str[i]);
}
« no previous file with comments | « no previous file | tools/gn/escape_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698