Ejemplo n.º 1
0
        public static ExpressionEvaluationResult RunPipeline(
            CommandEvaluationContext context,
            PipelineParseResult pipelineParseResult
            )
        {
            ExpressionEvaluationResult r = null;
            var workUnitParseResult      = pipelineParseResult;

            while (workUnitParseResult != null)
            {
                r = RunWorkUnit(context, workUnitParseResult);
                workUnitParseResult = workUnitParseResult.Next;
            }
            return(r);
        }
        public void ProcessCommandLine(
            string commandLine,
            ExpressionEvaluationCommandDelegate evalCommandDelegate,
            bool outputStartNextLine = false,
            bool enableHistory       = false)
        {
            if (commandLine != null)
            {
                if (outputStartNextLine)
                {
                    Out.LineBreak();
                }

                ExpressionEvaluationResult expressionEvaluationResult = null;

                try
                {
                    sc.CancelKeyPress += CancelKeyPress;
                    CommandLineProcessor.CancellationTokenSource = new CancellationTokenSource();
                    var task = Task.Run <ExpressionEvaluationResult>(
                        () => evalCommandDelegate(CommandLineProcessor.CommandEvaluationContext, commandLine, _prompt == null ? 0 : Out.GetPrint(_prompt).Length),
                        CommandLineProcessor.CancellationTokenSource.Token
                        );

                    try
                    {
                        try
                        {
                            task.Wait(CommandLineProcessor.CancellationTokenSource.Token);
                        } catch (ThreadInterruptedException) {
                            // get interrupted after send input
                        }
                        expressionEvaluationResult = task.Result;
                    }
                    catch (OperationCanceledException)
                    {
                        var res = task.Result;
                        Warningln($"command canceled: {commandLine}");
                    }
                    finally
                    {
                    }
                }
                catch (Exception ex)
                {
                    LogError(ex);
                }
                finally
                {
                    CommandLineProcessor.CancellationTokenSource.Dispose();
                    CommandLineProcessor.CancellationTokenSource = null;
                    sc.CancelKeyPress -= CancelKeyPress;
                    lock (ConsoleLock)
                    {
                        /*if (!WorkArea.rect.IsEmpty && (WorkArea.rect.Y != CursorTop || WorkArea.rect.X != CursorLeft))
                         *  LineBreak();*/// case of auto line break (spacing)

                        //Out.RestoreDefaultColors();   // FGZ removed
                    }
                }
            }
            if (enableHistory && !string.IsNullOrWhiteSpace(commandLine))
            {
                CommandLineProcessor.CmdsHistory.HistoryAppend(commandLine);
            }
        }
Ejemplo n.º 3
0
        public void ProcessCommandLine(
            string commandLine,
            ExpressionEvaluationCommandDelegate evalCommandDelegate,
            bool outputStartNextLine    = false,
            bool enableHistory          = false,
            bool enablePrePostComOutput = true)
        {
            if (commandLine != null)
            {
                if (outputStartNextLine)
                {
                    Out.LineBreak();
                }
                ExpressionEvaluationResult expressionEvaluationResult = null;

                try
                {
                    sc.CancelKeyPress += CancelKeyPress;
                    CommandLineProcessor.CancellationTokenSource = new CancellationTokenSource();
                    Out.IsModified = false;
                    Err.IsModified = false;

                    var task = Task.Run <ExpressionEvaluationResult>(
                        () => evalCommandDelegate(
                            CommandLineProcessor.CommandEvaluationContext,
                            commandLine,
                            _prompt == null ? 0 : Out.GetPrint(_prompt).Length,            // TODO has no sens with multi line prompt !!!
                            (enablePrePostComOutput && CommandLineProcessor != null) ?
                            CommandLineProcessor.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPreAnalysisOutput) : ""),
                        CommandLineProcessor.CancellationTokenSource.Token
                        );

                    try
                    {
                        try
                        {
                            task.Wait(CommandLineProcessor.CancellationTokenSource.Token);
                        }
                        catch (ThreadInterruptedException)
                        {
                            // get interrupted after send input
                        }
                        expressionEvaluationResult = task.Result;
                    }
                    catch (OperationCanceledException)
                    {
                        //var res = task.Result;
                        expressionEvaluationResult = task.Result;
                        Out.Warningln($"command canceled: {commandLine}");
                    }
                    finally { }
                }
                catch (Exception ex)
                {
                    LogError(ex);
                }
                finally
                {
                    if (enablePrePostComOutput && CommandLineProcessor != null)
                    {
                        if (Out.IsModified || Err.IsModified)
                        {
                            if (!(Out.CursorLeft == 0 && Out.CursorTop == 0))
                            {
                                Out.Echo(CommandLineProcessor.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPostExecOutModifiedOutput));
                            }
                        }
                        Out.Echo(CommandLineProcessor.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPostExecOutput));
                    }

                    CommandLineProcessor.CancellationTokenSource.Dispose();
                    CommandLineProcessor.CancellationTokenSource = null;
                    sc.CancelKeyPress -= CancelKeyPress;
                }
            }

            if (enableHistory && !string.IsNullOrWhiteSpace(commandLine))
            {
                CommandLineProcessor.CmdsHistory.HistoryAppend(commandLine);
            }
        }
        public ExpressionEvaluationResult ProcessCommandLine(
            string commandLine,
            Delegates.ExpressionEvaluationCommandDelegate evalCommandDelegate,
            bool outputStartNextLine    = false,
            bool enableHistory          = false,
            bool enablePrePostComOutput = true)
        {
            var clp = _commandLineProcessor;

            if (commandLine == null)
            {
                return(null);
            }

            if (outputStartNextLine)
            {
                Console.Out.LineBreak();
            }

            if (string.IsNullOrWhiteSpace(commandLine))
            {
                if (enablePrePostComOutput && clp != null)
                {
                    Console.Out.Echo(clp.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPreAnalysisOutput));
                }

                return(null);
            }

            ExpressionEvaluationResult expressionEvaluationResult = null;

            try
            {
                sc.CancelKeyPress          += CancelKeyPress;
                clp.CancellationTokenSource = new CancellationTokenSource();
                Console.Out.IsModified      = false;
                Console.Err.IsModified      = false;

                clp.ModuleManager.ModuleHookManager.InvokeHooks(
                    clp.CommandEvaluationContext, Hooks.PreProcessCommandLine, commandLine);

                var task = Task.Run <ExpressionEvaluationResult>(
                    () => evalCommandDelegate(
                        clp.CommandEvaluationContext,
                        commandLine,
                        _prompt == null ? 0 : Console.Out.GetPrint(_prompt).Length,            // TODO has no sens with multi line prompt !!!
                        (enablePrePostComOutput && clp != null) ?
                        clp.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPreAnalysisOutput) : ""),
                    clp.CancellationTokenSource.Token
                    );

                try
                {
                    try
                    {
                        task.Wait(clp.CancellationTokenSource.Token);       // TODO: not if {com} &
                    }
                    catch (ThreadInterruptedException)
                    {
                        // get interrupted after send input
                    }
                    expressionEvaluationResult = task.Result;
                }
                catch (OperationCanceledException)
                {
                    clp.ModuleManager.ModuleHookManager.InvokeHooks <CommandLineReader>(
                        clp.CommandEvaluationContext, Hooks.ProcessCommandLineCanceled);

                    expressionEvaluationResult = task.Result;
                    Console.Out.Warningln($"command canceled: {commandLine}");
                }
                finally { }
            }
            catch (Exception evalCommandException)
            {
                clp.ModuleManager.ModuleHookManager.InvokeHooks(
                    clp.CommandEvaluationContext, Hooks.ProcessCommandLineError);
                Console.LogError(evalCommandException);
                expressionEvaluationResult = new ExpressionEvaluationResult(
                    commandLine,
                    null,
                    Parsing.ParseResultType.Empty,
                    null,
                    (int)ReturnCode.Error,
                    evalCommandException,
                    evalCommandException.Message
                    );
            }
            finally
            {
                clp.CancellationTokenSource.Dispose();
                clp.CancellationTokenSource = null;
                sc.CancelKeyPress          -= CancelKeyPress;

                clp.ModuleManager.ModuleHookManager
                .InvokeHooks(clp.CommandEvaluationContext, Hooks.PostProcessCommandLine);

                // post com output

                if (enablePrePostComOutput && clp != null)
                {
                    if (Console.Out.IsModified || Console.Err.IsModified)
                    {
                        if (!(Console.Out.CursorLeft == 0 && Console.Out.CursorTop == 0))
                        {
                            Console.Out.Echo(clp.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPostExecOutModifiedOutput));
                        }
                    }
                    Console.Out.Echo(clp.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPostExecOutput));
                }
            }

            if (enableHistory)
            {
                clp.CmdsHistory.HistoryAppend(clp.CommandEvaluationContext, commandLine);
            }

            return(expressionEvaluationResult);
        }