Example #1
0
        /// <summary>
        /// Creates a command processor for "get-help [helpTarget]"
        /// </summary>
        /// <param name="context">context for the command processor</param>
        /// <param name="helpTarget">help target</param>
        /// <param name="helpCategory">help category</param>
        /// <returns>command processor for "get-help [helpTarget]"</returns>
        internal static CommandProcessorBase CreateGetHelpCommandProcessor(
            ExecutionContext context,
            string helpTarget,
            HelpCategory helpCategory)
        {
            if (context == null)
            {
                throw PSTraceSource.NewArgumentNullException("context");
            }
            if (string.IsNullOrEmpty(helpTarget))
            {
                throw PSTraceSource.NewArgumentNullException("helpTarget");
            }

            CommandProcessorBase helpCommandProcessor = context.CreateCommand("get-help", false);
            var cpi = CommandParameterInternal.CreateParameterWithArgument(
                /*parameterAst*/ null, "Name", "-Name:",
                /*argumentAst*/ null, helpTarget,
                false);

            helpCommandProcessor.AddParameter(cpi);
            cpi = CommandParameterInternal.CreateParameterWithArgument(
                /*parameterAst*/ null, "Category", "-Category:",
                /*argumentAst*/ null, helpCategory.ToString(),
                false);
            helpCommandProcessor.AddParameter(cpi);
            return(helpCommandProcessor);
        }
Example #2
0
        private PipelineProcessor BuildRedirectionPipeline(
            string path,
            ExecutionContext context)
        {
            CommandProcessorBase command = context.CreateCommand("out-file");

            command.AddParameter("-encoding", (object)"unicode");
            if (this.Appending)
            {
                command.AddParameter("-append", (object)true);
            }
            command.AddParameter("-filepath", (object)path);
            PipelineProcessor pipelineProcessor = new PipelineProcessor();

            pipelineProcessor.Add(command);
            try
            {
                pipelineProcessor.StartStepping(true);
            }
            catch (RuntimeException ex)
            {
                if (ex.ErrorRecord.Exception is ArgumentException)
                {
                    throw InterpreterError.NewInterpreterExceptionWithInnerException((object)null, typeof(RuntimeException), this._token, "RedirectionFailed", ex.ErrorRecord.Exception, (object)path, (object)ex.ErrorRecord.Exception.Message);
                }
                ex.ErrorRecord.SetInvocationInfo(new InvocationInfo((CommandInfo)null, this._token, context));
                throw ex;
            }
            return(pipelineProcessor);
        }
Example #3
0
        internal static CommandProcessorBase CreateGetHelpCommandProcessor(ExecutionContext context, string helpTarget, HelpCategory helpCategory)
        {
            if (context == null)
            {
                throw PSTraceSource.NewArgumentNullException("context");
            }
            if (string.IsNullOrEmpty(helpTarget))
            {
                throw PSTraceSource.NewArgumentNullException("helpTarget");
            }
            CommandProcessorBase     base2     = context.CreateCommand("get-help", false);
            CommandParameterInternal parameter = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, "Name", "-Name:", PositionUtilities.EmptyExtent, helpTarget, false);

            base2.AddParameter(parameter);
            parameter = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, "Category", "-Category:", PositionUtilities.EmptyExtent, helpCategory.ToString(), false);
            base2.AddParameter(parameter);
            return(base2);
        }
Example #4
0
        internal Pipe GetRedirectionPipe(ExecutionContext context, System.Management.Automation.Internal.PipelineProcessor parentPipelineProcessor)
        {
            if (string.IsNullOrWhiteSpace(this.File))
            {
                return(new Pipe {
                    NullPipe = true
                });
            }
            CommandProcessorBase     commandProcessor = context.CreateCommand("out-file", false);
            CommandParameterInternal parameter        = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, "Encoding", "-Encoding:", PositionUtilities.EmptyExtent, "Unicode", false);

            commandProcessor.AddParameter(parameter);
            if (this.Appending)
            {
                parameter = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, "Append", "-Append:", PositionUtilities.EmptyExtent, true, false);
                commandProcessor.AddParameter(parameter);
            }
            parameter = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, "Filepath", "-Filepath:", PositionUtilities.EmptyExtent, this.File, false);
            commandProcessor.AddParameter(parameter);
            this.PipelineProcessor = new System.Management.Automation.Internal.PipelineProcessor();
            this.PipelineProcessor.Add(commandProcessor);
            try
            {
                this.PipelineProcessor.StartStepping(true);
            }
            catch (RuntimeException exception)
            {
                if (exception.ErrorRecord.Exception is ArgumentException)
                {
                    throw InterpreterError.NewInterpreterExceptionWithInnerException(null, typeof(RuntimeException), null, "RedirectionFailed", ParserStrings.RedirectionFailed, exception.ErrorRecord.Exception, new object[] { this.File, exception.ErrorRecord.Exception.Message });
                }
                throw;
            }
            if (parentPipelineProcessor != null)
            {
                parentPipelineProcessor.AddRedirectionPipe(this.PipelineProcessor);
            }
            return(new Pipe(context, this.PipelineProcessor));
        }
Example #5
0
        private CommandProcessorBase CreateCommandProcessor(
            out int index,
            ExecutionContext context)
        {
            index = 0;
            CommandProcessorBase commandProcessorBase = (CommandProcessorBase)null;

            if (this._expression != null)
            {
                return((CommandProcessorBase) new ScriptCommandProcessor(ScriptBlock.CreateSynthesized(this._expression, this.NodeToken), context, false));
            }
            bool createScope;
            SessionStateInternal commandSessionState;
            bool   forceSessionState;
            object commandName = this.GetCommandName(out createScope, out index, out commandSessionState, out forceSessionState, context);

            switch (commandName)
            {
            case ScriptBlock scriptblock:
                commandProcessorBase = CommandDiscovery.CreateCommandProcessorForScript(scriptblock, context, createScope);
                if (commandProcessorBase.CommandSessionState == null || forceSessionState)
                {
                    commandProcessorBase.CommandSessionState = commandSessionState;
                    break;
                }
                break;

            case CommandInfo commandInfo:
                commandProcessorBase = context.CommandDiscovery.LookupCommandProcessor(commandInfo, context.EngineSessionState.currentScope.ScopeOrigin, new bool?(false));
                break;

            case string stringParser:
label_7:
                if (string.IsNullOrEmpty(stringParser))
                {
                    throw InterpreterError.NewInterpreterException(commandName, typeof(RuntimeException), (Token)this._elements[0], "BadExpression", (object)((Token)this._elements[0]).TokenText);
                }
                try
                {
                    if (commandSessionState != context.EngineSessionState)
                    {
                        SessionStateInternal engineSessionState = context.EngineSessionState;
                        try
                        {
                            context.EngineSessionState = commandSessionState;
                            commandProcessorBase       = context.CreateCommand(stringParser);
                            break;
                        }
                        finally
                        {
                            context.EngineSessionState = engineSessionState;
                        }
                    }
                    else
                    {
                        commandProcessorBase = context.CreateCommand(stringParser);
                        break;
                    }
                }
                catch (ParseException ex)
                {
                    throw;
                }
                catch (RuntimeException ex)
                {
                    if (ex.ErrorRecord.InvocationInfo == null)
                    {
                        ex.ErrorRecord.SetInvocationInfo(new InvocationInfo((CommandInfo)null, (Token)this._elements[0], context));
                    }
                    throw;
                }

            default:
                stringParser = PSObject.ToStringParser(context, commandName);
                goto label_7;
            }
            if (commandProcessorBase.CommandSessionState == null || forceSessionState)
            {
                commandProcessorBase.CommandSessionState = commandSessionState;
            }
            InternalCommand command = commandProcessorBase.Command;

            command.MyInvocation.ScriptToken = (Token)this._elements[0];
            if (context.IsStrictVersion(2) && this._elements.Count == index + 1 && (this._elements[index - 1] is Token && this._elements[index] is ArrayLiteralNode))
            {
                Token element = (Token)this._elements[index - 1];
                if (element.Script[element.End] == '(')
                {
                    throw InterpreterError.NewInterpreterException((object)null, typeof(RuntimeException), (Token)this._elements[index - 1], "StrictModeFunctionCallWithParens");
                }
            }
            if (createScope)
            {
                switch (command)
                {
                case ScriptCommand _:
                case PSScriptCmdlet _:
                    commandProcessorBase.UseLocalScope = true;
                    commandProcessorBase.CommandScope  = commandProcessorBase.CommandSessionState == null?context.EngineSessionState.NewScope(commandProcessorBase.FromScriptFile) : commandProcessorBase.CommandSessionState.NewScope(commandProcessorBase.FromScriptFile);

                    goto label_27;
                }
            }
            commandProcessorBase.UseLocalScope = false;
            commandProcessorBase.CommandScope  = commandProcessorBase.CommandSessionState.CurrentScope;
label_27:
            commandProcessorBase.Command.CallingToken = (Token)this._elements[0];
            return(commandProcessorBase);
        }