private static bool RedirectStreams(Context ctx, Process /*!*/ process, PhpArray /*!*/ descriptors, PhpArray /*!*/ pipes) { var descriptors_enum = descriptors.GetFastEnumerator(); while (descriptors_enum.MoveNext()) { var desc_no = (int)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); } PhpResource resource; var value = descriptors_enum.CurrentValue; if (value.IsPhpArray(out var array)) { if (!array.TryGetValue(0, out var qualifierObj)) { // value must be either a resource or an array: PhpException.Throw(PhpError.Warning, Resources.LibResources.descriptor_item_missing_qualifier, desc_no.ToString()); return(false); } switch (qualifierObj.ToString(ctx)) { case "pipe": { // mode is ignored (it's determined by the stream): var php_stream = new NativeStream(ctx, stream, null, access, string.Empty, StreamContext.Default); pipes.Add(desc_no, php_stream); break; } case "file": { if (!array.TryGetValue(1, out var pathObj)) { PhpException.Throw(PhpError.Warning, Resources.LibResources.descriptor_item_missing_file_name, desc_no.ToString()); return(false); } if (!array.TryGetValue(2, out var modeObj)) { PhpException.Throw(PhpError.Warning, Resources.LibResources.descriptor_item_missing_mode, desc_no.ToString()); return(false); } var php_stream = PhpStream.Open(ctx, pathObj.ToString(ctx), modeObj.ToString(ctx), StreamOpenOptions.Empty, StreamContext.Default); if (php_stream == null) { return(false); } php_stream.IsWriteBuffered = false; if (!ActivePipe.BeginIO(stream, php_stream, access, desc_no)) { return(false); } break; } default: PhpException.Throw(PhpError.Warning, Resources.LibResources.invalid_handle_qualifier, qualifierObj.ToString(ctx)); return(false); } } else if ((resource = value.AsResource()) != null) { var 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, Resources.LibResources.descriptor_item_not_array_nor_resource, desc_no.ToString()); return(false); } } return(true); }
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(); 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); }