2
0

Process.hx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2005-2007, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package sys.io;
  26. import haxe.io.BytesInput;
  27. import cs.system.io.StreamReader;
  28. import cs.system.io.StreamWriter;
  29. class Process {
  30. public var stdout(default,null) : haxe.io.Input;
  31. public var stderr(default,null) : haxe.io.Input;
  32. public var stdin(default, null) : haxe.io.Output;
  33. private var native:NativeProcess;
  34. public function new( cmd : String, args : Array<String> ) : Void
  35. {
  36. this.native = new NativeProcess();
  37. native.StartInfo.FileName = cmd;
  38. var buf = new StringBuf();
  39. for (arg in args)
  40. {
  41. buf.add("\"");
  42. buf.add(StringTools.replace(arg, "\"", "\\\""));
  43. buf.add("\" ");
  44. }
  45. native.StartInfo.Arguments = buf.toString();
  46. native.StartInfo.RedirectStandardError = native.StartInfo.RedirectStandardInput = native.StartInfo.RedirectStandardOutput = true;
  47. native.StartInfo.UseShellExecute = false;
  48. native.Start();
  49. this.stdout = new cs.io.NativeInput(native.StandardOutput.BaseStream);
  50. this.stderr = new cs.io.NativeInput(native.StandardError.BaseStream);
  51. this.stdin = new cs.io.NativeOutput(native.StandardInput.BaseStream);
  52. }
  53. public function getPid() : Int
  54. {
  55. return native.Id;
  56. }
  57. public function exitCode() : Int
  58. {
  59. native.WaitForExit();
  60. return native.ExitCode;
  61. }
  62. public function close() : Void
  63. {
  64. native.Close();
  65. }
  66. public function kill() : Void
  67. {
  68. native.Kill();
  69. }
  70. }
  71. /*
  72. FIXME: The actual process class is much more complex than this, so here it is included a very simplified version.
  73. */
  74. @:native('System.Diagnostics.Process') private extern class NativeProcess
  75. {
  76. var ExitCode(default, null):Int;
  77. var Id(default, null):Int;
  78. var StartInfo(default, null):NativeStartInfo;
  79. var StandardError(default, null):StreamReader;
  80. var StandardInput(default, null):StreamWriter;
  81. var StandardOutput(default, null):StreamReader;
  82. function new():Void;
  83. function Close():Void;
  84. function Kill():Void;
  85. function Start():Void;
  86. function WaitForExit():Void;
  87. }
  88. @:native('System.Diagnostics.ProcessStartInfo') private extern class NativeStartInfo
  89. {
  90. var Arguments:String;
  91. var FileName:String;
  92. var RedirectStandardError:Bool;
  93. var RedirectStandardInput:Bool;
  94. var RedirectStandardOutput:Bool;
  95. var UseShellExecute:Bool;
  96. function new(filename:String, args:String):Void;
  97. }