Esempio n. 1
0
 internal static void AddArgumentsToCommandProcessor(CommandProcessorBase commandProcessor, object[] arguments)
 {
     if (arguments != null)
     {
         for (int i = 0; i < arguments.Length; i++)
         {
             CommandParameterInternal internal2;
             string arg = arguments[i] as string;
             if (ArgumentLooksLikeParameter(arg))
             {
                 int index = arg.IndexOf(':');
                 if ((index != -1) && (index != (arg.Length - 1)))
                 {
                     internal2 = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, arg.Substring(1, index - 1), arg, PositionUtilities.EmptyExtent, arg.Substring(index + 1).Trim(), false);
                 }
                 else if ((i == (arguments.Length - 1)) || (arg[arg.Length - 1] != ':'))
                 {
                     internal2 = CommandParameterInternal.CreateParameter(PositionUtilities.EmptyExtent, arg.Substring(1), arg);
                 }
                 else
                 {
                     internal2 = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, arg.Substring(1, arg.Length - 2), arg, PositionUtilities.EmptyExtent, arguments[i + 1], false);
                     i++;
                 }
             }
             else
             {
                 internal2 = CommandParameterInternal.CreateArgument(PositionUtilities.EmptyExtent, arguments[i], false);
             }
             commandProcessor.AddParameter(internal2);
         }
     }
 }
Esempio n. 2
0
        internal static SteppablePipeline GetSteppablePipeline(PipelineAst pipelineAst, CommandOrigin commandOrigin)
        {
            PipelineProcessor pipe = new PipelineProcessor();

            System.Management.Automation.ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();
            foreach (CommandAst ast in pipelineAst.PipelineElements.Cast <CommandAst>())
            {
                List <CommandParameterInternal> list = new List <CommandParameterInternal>();
                foreach (CommandElementAst ast2 in ast.CommandElements)
                {
                    CommandParameterAst commandParameterAst = ast2 as CommandParameterAst;
                    if (commandParameterAst != null)
                    {
                        list.Add(GetCommandParameter(commandParameterAst, executionContextFromTLS));
                    }
                    else
                    {
                        ExpressionAst expressionAst = (ExpressionAst)ast2;
                        object        obj2          = Compiler.GetExpressionValue(expressionAst, executionContextFromTLS, (IList)null);
                        bool          splatted      = (expressionAst is VariableExpressionAst) && ((VariableExpressionAst)expressionAst).Splatted;
                        list.Add(CommandParameterInternal.CreateArgument(expressionAst.Extent, obj2, splatted));
                    }
                }
                List <CommandRedirection> list2 = new List <CommandRedirection>();
                foreach (RedirectionAst ast5 in ast.Redirections)
                {
                    list2.Add(GetCommandRedirection(ast5, executionContextFromTLS));
                }
                CommandProcessorBase base2 = AddCommand(pipe, list.ToArray(), ast, list2.ToArray(), executionContextFromTLS);
                base2.Command.CommandOriginInternal      = commandOrigin;
                base2.CommandScope.ScopeOrigin           = commandOrigin;
                base2.Command.MyInvocation.CommandOrigin = commandOrigin;
                CallStackFrame[] frameArray = executionContextFromTLS.Debugger.GetCallStack().ToArray <CallStackFrame>();
                if ((frameArray.Length > 0) && Regex.IsMatch(frameArray[0].Position.Text, "GetSteppablePipeline", RegexOptions.IgnoreCase))
                {
                    InvocationInfo myInvocation = base2.Command.MyInvocation;
                    myInvocation.InvocationName = frameArray[0].InvocationInfo.InvocationName;
                    if (frameArray.Length > 1)
                    {
                        IScriptExtent position = frameArray[1].Position;
                        if ((position != null) && (position != PositionUtilities.EmptyExtent))
                        {
                            myInvocation.DisplayScriptPosition = position;
                        }
                    }
                }
                if ((executionContextFromTLS.CurrentCommandProcessor != null) && (executionContextFromTLS.CurrentCommandProcessor.CommandRuntime != null))
                {
                    base2.CommandRuntime.SetMergeFromRuntime(executionContextFromTLS.CurrentCommandProcessor.CommandRuntime);
                }
            }
            return(new SteppablePipeline(executionContextFromTLS, pipe));
        }
Esempio n. 3
0
        private static CommandParameterInternal SplatEnumerableElement(object splattedArgument, IScriptExtent splatExtent)
        {
            PSObject obj2 = splattedArgument as PSObject;

            if (obj2 != null)
            {
                PSPropertyInfo info       = obj2.Properties["<CommandParameterName>"];
                object         baseObject = obj2.BaseObject;
                if (((info != null) && (info.Value is string)) && (baseObject is string))
                {
                    return(CommandParameterInternal.CreateParameter(splatExtent, (string)info.Value, (string)baseObject));
                }
            }
            return(CommandParameterInternal.CreateArgument(splatExtent, splattedArgument, false));
        }
Esempio n. 4
0
        /// <summary>
        /// Binds the specified parameters to the native command
        /// </summary>
        /// <param name="parameters">
        /// The parameters to bind.
        /// </param>
        /// <param name="outputRedirected">
        /// true if minishell output is redirected.
        /// </param>
        /// <param name="hostName">
        /// name of the calling host.
        /// </param>
        /// <remarks>
        /// For any parameters that do not have a name, they are added to the command
        /// line arguments for the command
        /// </remarks>
        internal Collection <CommandParameterInternal> BindParameters(Collection <CommandParameterInternal> parameters, bool outputRedirected, string hostName)
        {
            MinishellParameters seen = 0;
            string inputFormat       = null;
            string outputFormat      = null;

            for (int i = 0; i < parameters.Count; i++)
            {
                var parameter = parameters[i];

                if (parameter.ParameterNameSpecified)
                {
                    var parameterName = parameter.ParameterName;

                    if (CommandParameter.StartsWith(parameterName, StringComparison.OrdinalIgnoreCase))
                    {
                        HandleSeenParameter(ref seen, MinishellParameters.Command, CommandParameter);

                        // Value must be specified for -Command parameter.
                        if (i + 1 >= parameters.Count)
                        {
                            throw NewParameterBindingException(null, ErrorCategory.InvalidArgument, CommandParameter,
                                                               typeof(ScriptBlock), null,
                                                               NativeCP.NoValueForCommandParameter,
                                                               "NoValueForCommandParameter");
                        }

                        i += 1;

                        // Value of -Command parameter must be scriptblock
                        var scriptBlockArgument = parameters[i];
                        var argumentValue       = PSObject.Base(scriptBlockArgument.ArgumentValue);
                        if (!scriptBlockArgument.ArgumentSpecified || !(argumentValue is ScriptBlock))
                        {
                            throw NewParameterBindingException(null, ErrorCategory.InvalidArgument, CommandParameter,
                                                               typeof(ScriptBlock), argumentValue.GetType(),
                                                               NativeCP.IncorrectValueForCommandParameter,
                                                               "IncorrectValueForCommandParameter");
                        }

                        // Replace the parameters with -EncodedCommand <base64 encoded scriptblock>
                        parameters[i - 1] = CommandParameterInternal.CreateParameter(EncodedCommandParameter, "-" + EncodedCommandParameter, parameter.ParameterAst);
                        string encodedScript = StringToBase64Converter.StringToBase64String(argumentValue.ToString());
                        parameters[i] = CommandParameterInternal.CreateArgument(encodedScript, scriptBlockArgument.ArgumentAst);
                    }
                    else if (InputFormatParameter.StartsWith(parameterName, StringComparison.OrdinalIgnoreCase))
                    {
                        HandleSeenParameter(ref seen, MinishellParameters.InputFormat, InputFormatParameter);

                        // Value for -Inputformat must be specified
                        if (i + 1 >= parameters.Count)
                        {
                            throw NewParameterBindingException(null, ErrorCategory.InvalidArgument, InputFormatParameter,
                                                               typeof(string), null,
                                                               NativeCP.NoValueForInputFormatParameter,
                                                               "NoValueForInputFormatParameter");
                        }

                        // Update the argument (partial arguments are allowed)
                        i += 1;
                        var inputFormatArg = parameters[i];
                        inputFormat       = ProcessFormatParameterValue(InputFormatParameter, inputFormatArg.ArgumentValue);
                        parameters[i - 1] = CommandParameterInternal.CreateParameter(InputFormatParameter, "-" + InputFormatParameter, parameter.ParameterAst);
                        parameters[i]     = CommandParameterInternal.CreateArgument(inputFormat, inputFormatArg.ArgumentAst);
                    }
                    else if (OutputFormatParameter.StartsWith(parameterName, StringComparison.OrdinalIgnoreCase))
                    {
                        HandleSeenParameter(ref seen, MinishellParameters.OutputFormat, OutputFormatParameter);

                        // Value for -Inputformat must be specified
                        if (i + 1 >= parameters.Count)
                        {
                            throw NewParameterBindingException(null, ErrorCategory.InvalidArgument, OutputFormatParameter,
                                                               typeof(string), null,
                                                               NativeCP.NoValueForOutputFormatParameter,
                                                               "NoValueForInputFormatParameter");
                        }

                        // Update the argument (partial arguments are allowed)
                        i += 1;
                        var outputFormatArg = parameters[i];
                        outputFormat      = ProcessFormatParameterValue(OutputFormatParameter, outputFormatArg.ArgumentValue);
                        parameters[i - 1] = CommandParameterInternal.CreateParameter(OutputFormatParameter, "-" + OutputFormatParameter, parameter.ParameterAst);
                        parameters[i]     = CommandParameterInternal.CreateArgument(outputFormat, outputFormatArg.ArgumentAst);
                    }
                    else if (ArgsParameter.StartsWith(parameterName, StringComparison.OrdinalIgnoreCase))
                    {
                        HandleSeenParameter(ref seen, MinishellParameters.Arguments, ArgsParameter);

                        // Value for -Args parameter must be specified
                        if (i + 1 >= parameters.Count)
                        {
                            throw NewParameterBindingException(null, ErrorCategory.InvalidArgument, ArgsParameter,
                                                               typeof(string), null, NativeCP.NoValuesSpecifiedForArgs,
                                                               "NoValuesSpecifiedForArgs");
                        }

                        // Get the encoded value for -args parameter
                        i += 1;
                        var argsArg     = parameters[i];
                        var encodedArgs = ConvertArgsValueToEncodedString(argsArg.ArgumentValue);
                        parameters[i - 1] = CommandParameterInternal.CreateParameter(EncodedArgsParameter, "-" + EncodedArgsParameter, parameter.ParameterAst);
                        // NOTE: do not pass the ArgumentAst; it will fail validation in BindParameters if there
                        // are multiple arguments (array) but encodedArgs is an encoded string.
                        parameters[i] = CommandParameterInternal.CreateArgument(encodedArgs);
                    }
                }
                else
                {
                    // -Command is positional parameter. Bind first scriptblock to it, others are errors.
                    var scriptBlockArgument = parameters[i];
                    var argumentValue       = PSObject.Base(scriptBlockArgument.ArgumentValue);
                    if (argumentValue is ScriptBlock)
                    {
                        HandleSeenParameter(ref seen, MinishellParameters.Command, CommandParameter);

                        // Replace the argument with -EncodedCommand <base64 encoded scriptblock>
                        string encodedScript = StringToBase64Converter.StringToBase64String(argumentValue.ToString());
                        parameters[i] = CommandParameterInternal.CreateParameterWithArgument(
                            parameter.ArgumentAst, EncodedCommandParameter, "-" + EncodedCommandParameter,
                            parameter.ArgumentAst, encodedScript,
                            spaceAfterParameter: true);
                    }
                }
            }

            // Add InputFormat and OutputFormat parameter if not specified
            if (inputFormat == null)
            {
                // For minishell default input format is xml
                parameters.Add(CommandParameterInternal.CreateParameter(InputFormatParameter, "-" + InputFormatParameter));
                parameters.Add(CommandParameterInternal.CreateArgument(XmlFormatValue));
                inputFormat = XmlFormatValue;
            }

            if (outputFormat == null)
            {
                // If output is redirected, output format should be xml
                outputFormat = outputRedirected ? XmlFormatValue : TextFormatValue;
                parameters.Add(CommandParameterInternal.CreateParameter(OutputFormatParameter, "-" + OutputFormatParameter));
                parameters.Add(CommandParameterInternal.CreateArgument(outputFormat));
            }

            // Set the output and input format class variable
            InputFormat = XmlFormatValue.StartsWith(inputFormat, StringComparison.OrdinalIgnoreCase)
                ? NativeCommandIOFormat.Xml
                : NativeCommandIOFormat.Text;
            OutputFormat = XmlFormatValue.StartsWith(outputFormat, StringComparison.OrdinalIgnoreCase)
                ? NativeCommandIOFormat.Xml
                : NativeCommandIOFormat.Text;

            // Note if a minishell is invoked from a non-console host, we need to
            // pass -nonInteractive flag. Our console host's name is "ConsoleHost".
            // Correct check would be see if current host has access to console and
            // pass noninteractive flag if doesn't.
            if (string.IsNullOrEmpty(hostName) || !hostName.Equals("ConsoleHost", StringComparison.OrdinalIgnoreCase))
            {
                NonInteractive = true;
                parameters.Insert(0, CommandParameterInternal.CreateParameter(NonInteractiveParameter, "-" + NonInteractiveParameter));
            }

            ((NativeCommandParameterBinder)DefaultParameterBinder).BindParameters(parameters);

            Diagnostics.Assert(s_emptyReturnCollection.Count == 0, "This list shouldn't be used for anything as it's shared.");

            return(s_emptyReturnCollection);
        } // BindParameters