Exemplo n.º 1
0
            public ProgramRunType GetProgramRunType(string cmd)
            {
                NixPath path = new NixPath(cmd);

                ProgramRunType result = new ProgramRunType();
                string         pathTo = RootDrive.GetPathTo(RootDrive.FollowLinks(path));

                Debug.Log("GPRT: " + path.ToString() + " | " + pathTo);
                using (StreamReader reader = new StreamReader(pathTo))
                {
                    string firstLine = reader.ReadLine();
                    if (firstLine.Length > 2)
                    {
                        if (firstLine[0] == '#' && firstLine[1] == '!')
                        {
                            Debug.Log("Has a shebang! " + firstLine);
                            result.BinaryCode = false;
                            result.Shebang    = firstLine.Substring(2);
                        }
                        // Definitly not a good way of determining if it is a DLL
                        // Need to look further into the file to determine if it really is.
                        else if (firstLine[0] == 'M' && firstLine[1] == 'Z')
                        {
                            Debug.Log("Possibly a DLL");
                            result.BinaryCode = true;
                        }
                    }
                }
                return(result);
            }
Exemplo n.º 2
0
        /// <inheritdoc />
        public bool RunCodeCustom(string code, ProgramRunType runType = ProgramRunType.CallProgram)
        {
            Link.check_connection();
            var command = "RunCode2";

            Link.send_line(command);
            Link.send_item(this);
            Link.send_line(code.Replace("\n\n", "<br>").Replace("\n", "<br>"));
            Link.send_int((int)runType);
            var progstatus = Link.rec_int();

            Link.check_status();
            return(progstatus == 0);
        }
Exemplo n.º 3
0
            public void Execute(Session session, string input, Stream StdOut, Stream StdIn)
            {
                Debug.Log("Execute: >" + input + "<");
                if (input.Length == 0)
                {
                    return;
                }

                /*if (input == "test")
                 * {
                 *  System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(@"F:\git\ScalingOctoDubstep\root\usr\bin\Test");
                 *
                 *  string type = "Test";
                 *  try
                 *  {
                 *      Type[] types = assembly.GetTypes();
                 *      object o = Activator.CreateInstance(types[0], NewPid());
                 *      Nix.Bin.Program proga = (Nix.Bin.Program)o;
                 *      proga.StdOut = Shell.StdOut;
                 *      proga.Execute(this, BaseSession, new string[] { "Test" });
                 *  }
                 *  catch (Exception exp)
                 *  {
                 *      Debug.Log("QWEASD: " + exp.Message);
                 *  }
                 *  return;
                 * }*/
                Regex regex = new Regex("(\".*\")|([^ \\t\\n\\r]+)");

                MatchCollection matches = regex.Matches(input);
                //string[] args = new string[matches.Count];
                List <string> args = new List <string>(matches.Count + 1);
                int           i    = 0;

                foreach (Match match in regex.Matches(input))
                {
                    if (match.Value[0] == '"')
                    {
                        args.Add(match.Value.Substring(1, match.Value.Length - 2));
                    }
                    else
                    {
                        args.Add(match.Value);
                    }
                }

                string binName = args[0];

                FindCommandResult cmdLookup = FindCommand(binName);

                if (!cmdLookup.Found)
                {
                    Shell.StdOut.WriteLine("Unable to find command: " + binName);
                    return;
                }
                args[0] = cmdLookup.Path;

                Bin.Program prog = null;
                if (cmdLookup.Builtin)
                {
                    Debug.Log("Attempting to create program: " + binName);
                    prog = (Bin.Program)Activator.CreateInstance(BinPrograms[binName], NewPid());
                    if (prog == null)
                    {
                        Shell.StdOut.WriteLine("Unable to create " + binName + " program");
                    }
                }
                else
                {
                    ProgramRunType programRunType = GetProgramRunType(cmdLookup.Path);
                    Debug.Log("Program type: " + programRunType.Shebang);
                    while (programRunType.Shebang.Length > 0)
                    {
                        cmdLookup = FindCommand(programRunType.Shebang);
                        if (!cmdLookup.Found)
                        {
                            Shell.StdOut.WriteLine("Unable to execute command: " + programRunType.Shebang + " for " + binName);
                            return;
                        }
                        args.Insert(0, cmdLookup.Path);
                        if (cmdLookup.Builtin)
                        {
                            binName = cmdLookup.Path;
                            prog    = (Bin.Program)Activator.CreateInstance(BinPrograms[binName], NewPid());
                            programRunType.Shebang = "";
                        }
                        else
                        {
                            programRunType = GetProgramRunType(cmdLookup.Path);
                        }
                    }
                    if (programRunType.BinaryCode)
                    {
                        // Do something about loading up a DLL
                    }
                }

                if (prog == null)
                {
                    Shell.StdOut.WriteLine("Unable to find command: " + binName);
                }
                else
                {
                    if (StdOut == null)
                    {
                        prog.StdOut = Shell.StdOut;
                    }
                    else
                    {
                        prog.StdOut = StdOut;
                    }

                    if (StdIn != null)
                    {
                        prog.StdIn = StdIn;
                    }
                    session.PushForegroundProgram(prog);
                    Debug.Log("Attempting to run program: " + binName);
                    String argsStr = "";
                    for (int j = 0; j < args.Count; j++)
                    {
                        if (j > 0)
                        {
                            argsStr += ", ";
                        }
                        argsStr += args[j];
                    }
                    Debug.Log("- With args: " + argsStr);
                    prog.Execute(this, session, args);
                    session.PopForegroundProgram();
                    Debug.Log("Fin " + binName);
                }
            }