SysTools.hx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package haxe;
  2. import haxe.ds.ReadOnlyArray;
  3. class SysTools {
  4. /**
  5. Character codes of the characters that will be escaped by `quoteWinArg(_, true)`.
  6. **/
  7. public static final winMetaCharacters:ReadOnlyArray<Int> = [
  8. " ".code, "(".code, ")".code, "%".code, "!".code, "^".code, "\"".code, "<".code, ">".code, "&".code, "|".code, "\n".code, "\r".code, ",".code, ";".code
  9. ];
  10. /**
  11. Returns a String that can be used as a single command line argument
  12. on Unix.
  13. The input will be quoted, or escaped if necessary.
  14. **/
  15. public static function quoteUnixArg(argument:String):String {
  16. // Based on cpython's shlex.quote().
  17. // https://hg.python.org/cpython/file/a3f076d4f54f/Lib/shlex.py#l278
  18. if (argument == "")
  19. return "''";
  20. if (!~/[^a-zA-Z0-9_@%+=:,.\/-]/.match(argument))
  21. return argument;
  22. // use single quotes, and put single quotes into double quotes
  23. // the string $'b is then quoted as '$'"'"'b'
  24. return "'" + StringTools.replace(argument, "'", "'\"'\"'") + "'";
  25. }
  26. /**
  27. Returns a String that can be used as a single command line argument
  28. on Windows.
  29. The input will be quoted, or escaped if necessary, such that the output
  30. will be parsed as a single argument using the rule specified in
  31. http://msdn.microsoft.com/en-us/library/ms880421
  32. Examples:
  33. ```haxe
  34. quoteWinArg("abc") == "abc";
  35. quoteWinArg("ab c") == '"ab c"';
  36. ```
  37. **/
  38. public static function quoteWinArg(argument:String, escapeMetaCharacters:Bool):String {
  39. // If there is no space, tab, back-slash, or double-quotes, and it is not an empty string.
  40. if (!~/^(\/)?[^ \t\/\\"]+$/.match(argument)) {
  41. // Based on cpython's subprocess.list2cmdline().
  42. // https://hg.python.org/cpython/file/50741316dd3a/Lib/subprocess.py#l620
  43. var result = new StringBuf();
  44. var needquote = argument.indexOf(" ") != -1 || argument.indexOf("\t") != -1 || argument == "" || argument.indexOf("/") > 0;
  45. if (needquote)
  46. result.add('"');
  47. var bs_buf = new StringBuf();
  48. for (i in 0...argument.length) {
  49. switch (argument.charCodeAt(i)) {
  50. case "\\".code:
  51. // Don't know if we need to double yet.
  52. bs_buf.add("\\");
  53. case '"'.code:
  54. // Double backslashes.
  55. var bs = bs_buf.toString();
  56. result.add(bs);
  57. result.add(bs);
  58. bs_buf = new StringBuf();
  59. result.add('\\"');
  60. case var c:
  61. // Normal char
  62. if (bs_buf.length > 0) {
  63. result.add(bs_buf.toString());
  64. bs_buf = new StringBuf();
  65. }
  66. result.addChar(c);
  67. }
  68. }
  69. // Add remaining backslashes, if any.
  70. result.add(bs_buf.toString());
  71. if (needquote) {
  72. result.add(bs_buf.toString());
  73. result.add('"');
  74. }
  75. argument = result.toString();
  76. }
  77. if (escapeMetaCharacters) {
  78. var result = new StringBuf();
  79. for (i in 0...argument.length) {
  80. var c = argument.charCodeAt(i);
  81. if (winMetaCharacters.indexOf(c) >= 0) {
  82. result.addChar("^".code);
  83. }
  84. result.addChar(c);
  85. }
  86. return result.toString();
  87. } else {
  88. return argument;
  89. }
  90. }
  91. }