Process.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.io;
  23. import haxe.io.BytesInput;
  24. import cs.system.io.StreamReader;
  25. import cs.system.io.StreamWriter;
  26. @:coreApi
  27. class Process {
  28. public var stdout(default,null) : haxe.io.Input;
  29. public var stderr(default,null) : haxe.io.Input;
  30. public var stdin(default, null) : haxe.io.Output;
  31. private var native:NativeProcess;
  32. public function new( cmd : String, args : Array<String> ) : Void
  33. {
  34. this.native = new NativeProcess();
  35. native.StartInfo.FileName = cmd;
  36. var buf = new StringBuf();
  37. for (arg in args)
  38. {
  39. buf.add("\"");
  40. buf.add(StringTools.replace(arg, "\"", "\\\""));
  41. buf.add("\" ");
  42. }
  43. native.StartInfo.Arguments = buf.toString();
  44. native.StartInfo.RedirectStandardError = native.StartInfo.RedirectStandardInput = native.StartInfo.RedirectStandardOutput = true;
  45. native.StartInfo.UseShellExecute = false;
  46. native.Start();
  47. this.stdout = new cs.io.NativeInput(native.StandardOutput.BaseStream);
  48. this.stderr = new cs.io.NativeInput(native.StandardError.BaseStream);
  49. this.stdin = new cs.io.NativeOutput(native.StandardInput.BaseStream);
  50. }
  51. public function getPid() : Int
  52. {
  53. return native.Id;
  54. }
  55. public function exitCode() : Int
  56. {
  57. native.WaitForExit();
  58. return native.ExitCode;
  59. }
  60. public function close() : Void
  61. {
  62. native.Close();
  63. }
  64. public function kill() : Void
  65. {
  66. native.Kill();
  67. }
  68. }
  69. /*
  70. FIXME: The actual process class is much more complex than this, so here it is included a very simplified version.
  71. */
  72. @:native('System.Diagnostics.Process') private extern class NativeProcess
  73. {
  74. var ExitCode(default, null):Int;
  75. var Id(default, null):Int;
  76. var StartInfo(default, null):NativeStartInfo;
  77. var StandardError(default, null):StreamReader;
  78. var StandardInput(default, null):StreamWriter;
  79. var StandardOutput(default, null):StreamReader;
  80. function new():Void;
  81. function Close():Void;
  82. function Kill():Void;
  83. function Start():Void;
  84. function WaitForExit():Void;
  85. }
  86. @:native('System.Diagnostics.ProcessStartInfo') private extern class NativeStartInfo
  87. {
  88. var Arguments:String;
  89. var FileName:String;
  90. var RedirectStandardError:Bool;
  91. var RedirectStandardInput:Bool;
  92. var RedirectStandardOutput:Bool;
  93. var UseShellExecute:Bool;
  94. function new(filename:String, args:String):Void;
  95. }