예제 #1
0
        /*
         * Constructor
         *
         * Boots up the shell, loads all PATH and ENV information required
         */
        CSharpShell()
        {
            // Check we're running on a supported platform
            this.OsVersionCheck();

            // IO wrapper
            this.console = new ShellConsole();
        }
예제 #2
0
        /*
         * Reads in one command from the console.
         *
         * Commands trailing with \ will allow for the command to continue on the next line.
         * This takes precedence over multiline strings, e.g. the following command:
         *
         *     echo "Very long sentence that gets \
         *     split into two different lines"
         *
         * Will be resolved to:
         *
         *     echo "Very long sentence that gets split into two different lines"
         */
        public static string ReadCommand(ShellConsole io)
        {
            string cmd = "";

            while (true)
            {
                cmd += io.ReadLine();
                // multiline commands
                if (!cmd.EndsWith("\\"))
                {
                    break;
                }
                // Delete trailing backslash
                cmd = cmd.Remove(cmd.Length - 1);
            }

            return(cmd);
        }
예제 #3
0
        private static void ChangeDirectory(List <string> args, ShellConsole io)
        {
            // cd does nothing for no arguments
            if (args.Count == 0)
            {
                return;
            }

            Debug.WriteLine(args[0]);

            // Attempt to change directory, print error if not possible
            try
            {
                Directory.SetCurrentDirectory(args[0]);
            }
            catch (FileNotFoundException f)
            {
                io.WriteLine(f.Message);
            }
            catch (DirectoryNotFoundException d)
            {
                io.WriteLine(d.Message);
            }
        }
예제 #4
0
        public static bool RunSpecialCommand(string command, List <string> arguments, ShellConsole io)
        {
            foreach (string cmd in ReservedWords)
            {
                // If given command is special command
                if (cmd.ToLower() == command.ToLower())
                {
                    // Switch on command
                    switch (command.ToLower())
                    {
                    case "cd":
                        ChangeDirectory(arguments, io);
                        break;

                    case "exit":
                        Exit();
                        break;

                    default:
                        Console.WriteLine("Error - Program '" + command + "' matched special command, but reached default in command switch");
                        Environment.Exit(1);
                        // To keep compiler happy
                        break;
                    }
                    return(true);
                }
            }

            // No special command found
            return(false);
        }