Ejemplo n.º 1
0
        public void WriteOutput(Stream o, UserVar u = null)
        {
            UserVar v = Output;

            if (OutType == OutputType.None)
            {
                return;
            }
            UserVar wr = (u != null && !u.IsNull()) ? u : new UserVar(o);

            if (!wr.IsNull())
            {
                List <string> Out = new List <string>();
                if (Output.Is(typeof(String[])))
                {
                    foreach (string s in Output.Get <string[]>())
                    {
                        Out.Add(s);
                    }
                }
                else if (Output.Is(typeof(String)))
                {
                    Out.Add(Output.Get <string>());
                }
                foreach (string a in Out)
                {
                    Write(a, wr, u);
                }
            }
        }
Ejemplo n.º 2
0
        public void Set(string s, object o, Type t = null)
        {
            UserVar Var = new UserVar(o);

            if (!Equals(t, null))
            {
                Var.SetType(t);
            }
            Vars[s] = Var;
        }
Ejemplo n.º 3
0
        public void ParseOutput(int i, List <UserVar> Ps)
        {
            if (i < 0)
            {
                Output = new UserVar(""); return;
            }
            string Out = FormatStr[i][1];

            if (Out.StartsWith("`") && Out.EndsWith("`"))
            {
                UserVar v = Run(Out, true);
                Ps.Add(v);
                Output = v;
            }
            else
            {
                Output = new UserVar(FormatStr[i][1]);
            }
        }
Ejemplo n.º 4
0
 UserVar Run(string s, bool ExpectsOutput = false)
 {
     if (s.StartsWith("`") && s.EndsWith("`"))
     {
         if (RunCommand && !Equals(m, null))
         {
             string cm = s.Substring(1, s.Length - 2);
             m.HideOutput();
             m.RunCommand(cm, ExpectsOutput);
             m.ShowOutput();
             UserVar r = m.Ret;
             m.t.KillBuffer();
             if (r != null && !r.IsNull())
             {
                 return(r);
             }
             else
             {
                 return(new UserVar(m.Return));
             }
         }
     }
     return(new UserVar(new Null()));
 }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        public void Write(string f, UserVar v, UserVar ret = null)
        {
            FileInfo fi = null;

            if (IgnoreFileNames.Contains(f, StringComparer.InvariantCultureIgnoreCase))
            {
                return;
            }
            try
            {
                fi = new FileInfo(f);
            } catch (Exception)
            {
                m.Error("Invalid file name");
                return;
            }
            FileStream fil = null;
            UserVar    wr  = v;

            //MessageBox.Show(OutType.ToString());
            if (OutType == OutputType.Write)
            {
                fil = fi.OpenWrite();
                fil.SetLength(0);
            }

            if (OutType == OutputType.Append)
            {
                fil = (FileStream)fi.AppendText().BaseStream;
            }

            if (OutType == OutputType.Ret)
            {
                fil = fi.OpenWrite();
                wr  = ret == null ? new UserVar(new Null()) : ret;
            }
            if (v.Is(typeof(byte[])))
            {
                byte[] bt = v.Get <byte[]>();
                fil.Write(bt, 0, bt.Length);
            }
            else if (v.Is(typeof(string)))
            {
                byte[] bt = v.Get <string>().Select(a => (byte)a).ToArray();
                fil.Write(bt, 0, bt.Length);
            }
            else if (v.Is(typeof(string[])))
            {
                string[] s = v.Get <string[]>();
                foreach (string str in s)
                {
                    byte[] bt = str.Select(a => (byte)a).ToArray();
                    fil.Write(bt, 0, bt.Length);
                    fil.WriteByte((byte)'\n');
                }
            }
            else if (v.Is(typeof(MemoryStream)))
            {
                byte[] bt = v.Get <MemoryStream>().ToArray();
                fil.Write(bt, 0, bt.Length);
            }
            else
            {
                byte[] bt = v.Get().ToString().Select(a => (byte)a).ToArray();
                fil.Write(bt, 0, bt.Length);
            }
            fil.Flush();
            fil.Close();
            fil.Dispose();
            fil = null;
        }