Exemplo n.º 1
0
        public bool RunCommand(string interp, bool ExpectsOutput = false, bool pretty = false)
        {
            if (pretty)
            {
                t.ColorWrite("$f> {0}", interp);
            }
            if (Hide)
            {
                t.hide = true;
            }
            else
            {
                t.hide = false;
            }
            Return = null;
            Command cmd = null;
            Thread  ct  = new Thread(() =>
            {
                Argumenter a      = new Argumenter(interp, true);
                CurrentArgumenter = a;
                a.SetM(this);
                if (a.GetRaw(0).Trim().Length == 0)
                {
                    return;
                }
                a.SetM(this);
                Command c = null;
                Action <Command> PickThis = (c_) =>
                {
                    a.Switches = c.Switches;
                    a.Params   = c.Parameters;
                    a.AllSP    = c.AllSP;
                    cmd        = c;
                };
                try
                {
                    c = Cmds[a.GetRaw(0).ToLower()];
                    PickThis(c);
                }
                catch (Exception)
                {
                    try
                    {
                        if (c == null)
                        {
                            CommandGroup g = Groups[a.GetRaw(0).ToLower()];
                            c = g.C[a.GetRaw(1).ToLower()];
                            PickThis(c);
                            if (c != null)
                            {
                                a.AddOffset();
                            }
                        }
                    } catch (Exception) { }
                }
                if (c == null)
                {
                    t.ColorWrite("$cInvalid command.");
                    return;
                }
                if (!Equals(c, null))
                {
                    if (a.Parse(c.ParseSW, c.ParsePR))
                    {
                        t.SetForeColor('f');

                        if (a.OutType != Argumenter.OutputType.None)
                        {
                            t.StartBuffer(true);
                        }
                        a.ExpectsOutput = ExpectsOutput;
#if (DEBUG)
                        Return = c.Run(t, a);
#else
                        try
                        {
                            Return = c.Run(t, a);
                        } catch (Exception e)
                        {
                            Trace(e);
                        }
#endif
                        if (!Equals(cmd.Exit, null))
                        {
                            cmd.Exit.Invoke();
                        }
                        if (Return == null)
                        {
                            Ret = new UserVar(new Null());
                        }
                        else
                        {
                            Ret = new UserVar(Return);
                        }

                        if (a.OutType != Argumenter.OutputType.None)
                        {
                            Stream s = t.EndStreamBuffer();
                            a.WriteOutput(s, Ret);
                            t.KillBuffer();
                        }
                    }
                }
            });

            ct.SetApartmentState(ApartmentState.STA);
            ct.Start();
            while (ct.IsAlive)
            {
                Thread.Sleep(100);
                if (killthread)
                {
                    ct.Abort();
                    ct.Interrupt();
                    if (!Equals(cmd.Exit, null))
                    {
                        cmd.Exit.Invoke();
                    }
                }
            }
            return(false);
        }