public static PhpResource OpenPipe(string command, string mode) { if (String.IsNullOrEmpty(mode)) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("invalid_file_mode", mode)); return null; } bool read = mode[0] == 'r'; bool write = mode[0] == 'w' || mode[0] == 'a' || mode[0] == 'x'; if (!read && !write) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("invalid_file_mode", mode)); return null; } Process process = CreateProcessExecutingCommand(ref command, false); if (process == null) return null; process.StartInfo.RedirectStandardOutput = read; process.StartInfo.RedirectStandardInput = write; if (!StartProcess(process, true)) return null; Stream stream = (read) ? process.StandardOutput.BaseStream : process.StandardInput.BaseStream; StreamAccessOptions access = (read) ? StreamAccessOptions.Read : StreamAccessOptions.Write; ProcessWrapper wrapper = new ProcessWrapper(process); PhpStream php_stream = new NativeStream(stream, wrapper, access, String.Empty, StreamContext.Default); return php_stream; }
private static bool RedirectStreams(Process/*!*/ process, PhpArray/*!*/ descriptors, PhpArray/*!*/ pipes) { using (var descriptors_enum = descriptors.GetFastEnumerator()) while (descriptors_enum.MoveNext()) { int desc_no = descriptors_enum.CurrentKey.Integer; StreamAccessOptions access; Stream stream; switch (desc_no) { case 0: stream = process.StandardInput.BaseStream; access = StreamAccessOptions.Write; break; case 1: stream = process.StandardOutput.BaseStream; access = StreamAccessOptions.Read; break; case 2: stream = process.StandardError.BaseStream; access = StreamAccessOptions.Read; break; default: Debug.Fail(null); return false; } object value = PhpVariable.Dereference(descriptors_enum.CurrentValue); PhpResource resource; PhpArray array; if ((array = PhpArray.AsPhpArray(value)) != null) { if (!array.Contains(0)) { // value must be either a resource or an array: PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_missing_qualifier", desc_no)); return false; } string qualifier = Core.Convert.ObjectToString(array[0]); switch (qualifier) { case "pipe": { // mode is ignored (it's determined by the stream): PhpStream php_stream = new NativeStream(stream, null, access, String.Empty, StreamContext.Default); pipes.Add(desc_no, php_stream); break; } case "file": { if (!array.Contains(1)) { PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_missing_file_name", desc_no)); return false; } if (!array.Contains(2)) { PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_missing_mode", desc_no)); return false; } string path = Core.Convert.ObjectToString(array[1]); string mode = Core.Convert.ObjectToString(array[2]); PhpStream php_stream = PhpStream.Open(path, mode, StreamOpenOptions.Empty, StreamContext.Default); if (php_stream == null) return false; if (!ActivePipe.BeginIO(stream, php_stream, access, desc_no)) return false; break; } default: PhpException.Throw(PhpError.Warning, LibResources.GetString("invalid_handle_qualifier", qualifier)); return false; } } else if ((resource = value as PhpResource) != null) { PhpStream php_stream = PhpStream.GetValid(resource); if (php_stream == null) return false; if (!ActivePipe.BeginIO(stream, php_stream, access, desc_no)) return false; } else { // value must be either a resource or an array: PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_not_array_nor_resource", desc_no)); return false; } } return true; }