public FindCommandResult FindCommand(string cmd) { Debug.Log("Searching for: " + cmd); FindCommandResult result = new FindCommandResult(); string [] envPathSplit = BaseSession.GetEnvValue("PATH").Split(new char[] { ':' }); for (int i = 0; i < envPathSplit.Length; i++) { NixPath path = new NixPath(envPathSplit[i]); path.AppendPath(cmd); if (RootDrive.IsFile(path)) { result.Path = path.ToString(); result.Builtin = false; result.Found = true; return(result); } } if (BinPrograms.ContainsKey(cmd)) { result.Path = cmd; result.Builtin = true; result.Found = true; return(result); } result.Found = false; return(result); }
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); } }