Ejemplo n.º 1
0
        /// <summary>
        /// Handles application start on another thread, only called from Main
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        async static Task MainAsync(string[] args)
        {
            _tokenSource = new CancellationTokenSource();

            App = new Application();
            App.Init();

            Shell = new CommandShell(_tokenSource.Token);
            await Shell.Start();
        }
Ejemplo n.º 2
0
        public static void Main()
        {
            Console.OutputEncoding = Encoding.UTF8;

            var shell = CommandShell.Run();

            while (true)
            {
                shell.AcceptCommand();
            }
        }
Ejemplo n.º 3
0
        public string GetCommand(IEnumerable <string> parameters)
        {
            var enumerable = parameters as string[] ?? parameters.ToArray();

            if (enumerable.Count() != CommandShell.Count(c => c == ParameterEscape))
            {
                return("INVALID PARAMETERS");
            }

            var builtCommand = CommandShell;
            var reggie       = new Regex(Regex.Escape(ParameterEscape.ToString()));
            var i            = 1;

            return(enumerable.Aggregate(builtCommand, (current, parameter) => reggie.Replace(current, parameter, i++)));
        }
Ejemplo n.º 4
0
        /**
         * Type "man" to see internal commands.
         * Use Tab key a lot to see a list of possible completions.
         */

        static void Main(string[] args)
        {
            var cli = new CommandShell(); // creates a new default commands hell, a little CMD on steroids.

            cli.Start();                  // starts the shell session.
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Executes the command in cmd.exe and returns the output
        /// </summary>
        /// <param name="arguments"></param>
        /// <param name="shell"></param>
        /// <returns></returns>
        public CommandResult ExecuteCommand(string arguments, CommandShell shell = CommandShell.Cmd)
        {
            if (string.IsNullOrEmpty(arguments))
            {
                return(null);
            }

            string command = shell == CommandShell.Cmd ? "cmd.exe" : "powershell.exe";

            string nextDirectory = _workingDirectory;

            #region testing if the command is 'cd' and treat it. Verify if the dir (new path) exists]
            if (fetchCommand(arguments) == "cd")
            {
                string path = fetchPathFromCdCommand(arguments);
                nextDirectory = $"{_workingDirectory}\\{path}\\";
                try
                {
                    nextDirectory = Path.GetFullPath(nextDirectory);
                    bool directoryExists = Directory.Exists(nextDirectory);
                    if (!directoryExists)
                    {
                        throw new Exception();
                    }
                }
                catch (Exception ex)
                {
                    // the path doesnt exist
                    return(new CommandResult
                    {
                        WorkingDirectory = _workingDirectory,
                        Result = "O sistema não pode encontrar o caminho especificado.",
                        CommandSent = arguments,
                        Success = false
                    });
                }
            }
            #endregion

            try
            {
                // code taken from https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx
                Process process = new Process();
                process.StartInfo.UseShellExecute        = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError  = true;
                process.StartInfo.WorkingDirectory       = _workingDirectory;
                process.StartInfo.FileName  = command;
                process.StartInfo.Arguments = "/C " + arguments;

                process.Start();

                string output  = process.StandardOutput.ReadToEnd();
                string error   = process.StandardError.ReadToEnd();
                bool   success = process.HasExited && process.ExitCode == 0;


                // wait for 20 seconds
                bool exited = process.WaitForExit(20000);

                _workingDirectory = nextDirectory;

                string result = string.IsNullOrEmpty(error) ? $"\n{output}" : $"{error}{output}";

                return(new CommandResult
                {
                    WorkingDirectory = _workingDirectory,
                    CommandSent = arguments,
                    Success = success,
                    Result = result
                });
            }
            catch (Exception ex)
            {
                return(new CommandResult
                {
                    WorkingDirectory = _workingDirectory,
                    CommandSent = arguments,
                    Success = false,
                    Result = ex.Message
                });
            }
        }