private void HandleSeenParameter(ref MinishellParameters seen, MinishellParameters parameter, string parameterName)
 {
     if ((seen & parameter) == parameter)
     {
         throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, parameterName, null, null, "ParameterSpecifiedAlready", new object[] { parameterName });
     }
     seen |= parameter;
 }
Exemple #2
0
 /// <summary>
 /// Handles error handling if some parameter is specified more than once.
 /// </summary>
 private void HandleSeenParameter(ref MinishellParameters seen, MinishellParameters parameter, string parameterName)
 {
     if ((seen & parameter) == parameter)
     {
         throw NewParameterBindingException(null, ErrorCategory.InvalidArgument,
                                            "-" + parameterName, null, null,
                                            NativeCP.ParameterSpecifiedAlready,
                                            "ParameterSpecifiedAlready",
                                            parameterName);
     }
     else
     {
         seen |= parameter;
     }
 }
Exemple #3
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 not 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;
        }
 /// <summary>
 /// Handles error handling if some parameter is specified more than once.
 /// </summary>
 private void HandleSeenParameter(ref MinishellParameters seen, MinishellParameters parameter, string parameterName)
 {
     if ((seen & parameter) == parameter)
     {
         throw NewParameterBindingException(null, ErrorCategory.InvalidArgument,
                                            "-" + parameterName, null, null,
                                            NativeCP.ParameterSpecifiedAlready,
                                            "ParameterSpecifiedAlready",
                                            parameterName);
     }
     else
     {
         seen = seen | parameter;
     }
 }
        private ArrayList ProcessMinishellParameters(ArrayList args, bool outputRedirected, string hostName)
        {
            ArrayList           list = new ArrayList();
            string              str  = null;
            string              str2 = null;
            MinishellParameters seen = 0;

            for (int i = 0; i < args.Count; i++)
            {
                object obj2 = args[i];
                if (StartsWith("-command", obj2))
                {
                    this.HandleSeenParameter(ref seen, MinishellParameters.Command, "-command");
                    list.Add("-encodedCommand");
                    if ((i + 1) >= args.Count)
                    {
                        throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-command", typeof(ScriptBlock), null, "NoValueForCommandParameter", new object[0]);
                    }
                    ScriptBlock block = args[i + 1] as ScriptBlock;
                    if (block == null)
                    {
                        throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-command", typeof(ScriptBlock), args[i + 1].GetType(), "IncorrectValueForCommandParameter", new object[0]);
                    }
                    string str3 = StringToBase64Converter.StringToBase64String(block.ToString());
                    list.Add(str3);
                    i++;
                }
                else if (obj2 is ScriptBlock)
                {
                    this.HandleSeenParameter(ref seen, MinishellParameters.Command, "-command");
                    list.Add("-encodedCommand");
                    string str4 = StringToBase64Converter.StringToBase64String(obj2.ToString());
                    list.Add(str4);
                }
                else if (StartsWith("-inputFormat", obj2))
                {
                    this.HandleSeenParameter(ref seen, MinishellParameters.InputFormat, "-inputFormat");
                    list.Add("-inputFormat");
                    if ((i + 1) >= args.Count)
                    {
                        throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-inputFormat", typeof(string), null, "NoValueForInputFormatParameter", new object[0]);
                    }
                    str = this.ProcessFormatParameterValue("-inputFormat", args[i + 1]);
                    i++;
                    list.Add(str);
                }
                else if (StartsWith("-outputFormat", obj2))
                {
                    this.HandleSeenParameter(ref seen, MinishellParameters.OutputFormat, "-outputFormat");
                    list.Add("-outputFormat");
                    if ((i + 1) >= args.Count)
                    {
                        throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-outputFormat", typeof(string), null, "NoValueForOutputFormatParameter", new object[0]);
                    }
                    str2 = this.ProcessFormatParameterValue("-outputFormat", args[i + 1]);
                    i++;
                    list.Add(str2);
                }
                else if (StartsWith("-args", obj2))
                {
                    this.HandleSeenParameter(ref seen, MinishellParameters.Arguments, "-args");
                    list.Add("-encodedarguments");
                    if ((i + 1) >= args.Count)
                    {
                        throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-args", typeof(string), null, "NoValuesSpecifiedForArgs", new object[0]);
                    }
                    string str5 = ConvertArgsValueToEncodedString(args[i + 1]);
                    i++;
                    list.Add(str5);
                }
                else
                {
                    list.Add(obj2);
                }
            }
            if (str == null)
            {
                list.Add("-inputFormat");
                list.Add("xml");
                str = "xml";
            }
            if (str2 == null)
            {
                list.Add("-outputFormat");
                if (outputRedirected)
                {
                    list.Add("xml");
                    str2 = "xml";
                }
                else
                {
                    list.Add("text");
                    str2 = "text";
                }
            }
            if (StartsWith(str, "xml"))
            {
                this.inputFormatValue = NativeCommandIOFormat.Xml;
            }
            else
            {
                this.inputFormatValue = NativeCommandIOFormat.Text;
            }
            if (StartsWith(str2, "xml"))
            {
                this.outputFormatValue = NativeCommandIOFormat.Xml;
            }
            else
            {
                this.outputFormatValue = NativeCommandIOFormat.Text;
            }
            if (string.IsNullOrEmpty(hostName) || !hostName.Equals("ConsoleHost", StringComparison.OrdinalIgnoreCase))
            {
                this.nonInteractive = true;
                list.Insert(0, "-noninteractive");
            }
            return(list);
        }
 private void HandleSeenParameter(ref MinishellParameters seen, MinishellParameters parameter, string parameterName)
 {
     if ((seen & parameter) == parameter)
     {
         throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, parameterName, null, null, "ParameterSpecifiedAlready", new object[] { parameterName });
     }
     seen |= parameter;
 }