1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef TOOLS_GN_ESCAPE_H_ 6 #define TOOLS_GN_ESCAPE_H_ 7 8 #include <iosfwd> 9 #include <string_view> 10 11 enum EscapingMode { 12 // No escaping. 13 ESCAPE_NONE, 14 15 // Space only. 16 ESCAPE_SPACE, 17 18 // Ninja string escaping. 19 ESCAPE_NINJA, 20 21 // Ninja/makefile depfile string escaping. 22 ESCAPE_DEPFILE, 23 24 // For writing commands to ninja files. This assumes the output is "one 25 // thing" like a filename, so will escape or quote spaces as necessary for 26 // both Ninja and the shell to keep that thing together. 27 ESCAPE_NINJA_COMMAND, 28 29 // For writing preformatted shell commands to Ninja files. This assumes the 30 // output already has the proper quoting and may include special shell 31 // characters which we want to pass to the shell (like when writing tool 32 // commands). Only Ninja "$" are escaped. 33 ESCAPE_NINJA_PREFORMATTED_COMMAND, 34 }; 35 36 enum EscapingPlatform { 37 // Do escaping for the current platform. 38 ESCAPE_PLATFORM_CURRENT, 39 40 // Force escaping for the given platform. 41 ESCAPE_PLATFORM_POSIX, 42 ESCAPE_PLATFORM_WIN, 43 }; 44 45 struct EscapeOptions { 46 EscapingMode mode = ESCAPE_NONE; 47 48 // Controls how "fork" escaping is done. You will generally want to keep the 49 // default "current" platform. 50 EscapingPlatform platform = ESCAPE_PLATFORM_CURRENT; 51 52 // When the escaping mode is ESCAPE_SHELL, the escaper will normally put 53 // quotes around things with spaces. If this value is set to true, we'll 54 // disable the quoting feature and just add the spaces. 55 // 56 // This mode is for when quoting is done at some higher-level. Defaults to 57 // false. Note that Windows has strange behavior where the meaning of the 58 // backslashes changes according to if it is followed by a quote. The 59 // escaping rules assume that a double-quote will be appended to the result. 60 bool inhibit_quoting = false; 61 }; 62 63 // Escapes the given input, returnining the result. 64 // 65 // If needed_quoting is non-null, whether the string was or should have been 66 // (if inhibit_quoting was set) quoted will be written to it. This value should 67 // be initialized to false by the caller and will be written to only if it's 68 // true (the common use-case is for chaining calls). 69 std::string EscapeString(const std::string_view& str, 70 const EscapeOptions& options, 71 bool* needed_quoting); 72 73 // Same as EscapeString but writes the results to the given stream, saving a 74 // copy. 75 void EscapeStringToStream(std::ostream& out, 76 const std::string_view& str, 77 const EscapeOptions& options); 78 79 #endif // TOOLS_GN_ESCAPE_H_ 80