Exemplo n.º 1
0
        private void DataReceived(object sender, DataReceivedEventArgs dataReceivedEventArgs)
        {
            var stdout = dataReceivedEventArgs.Data;

            if (stdout == null)
            {
                return;
            }
            var regex = new Regex(@"(?<currentdir>[a-zA-Z]:\\[^>]*)(>$)", RegexOptions.Multiline);
            var match = regex.Match(stdout);

            if (match.Success)
            {
                CurrentDir = match.Groups["currentdir"].ToString();
                if (CurrentDir.Contains(HomeDir))
                {
                    stdout = "[" + CurrentDir.Replace(HomeDir, "~") + "] " + "(な・ω・ゆ)" + " $ ";
                }
                else
                {
                    stdout = "[" + CurrentDir + "] " + "(な・ω・ゆ)" + " $ ";
                }
            }
            BackBuffer += stdout.Replace(CurrentDir + ">", "") + "\r\n";
        }
Exemplo n.º 2
0
        private bool Shell(bool superuser)
        {
            while (true)
            {
                string currentCommand = "";
                Console.Write(AliasCurrentDir + prompt);
                string[] tokens = Console.ReadLine().Split(' ');
                currentCommand = tokens[0];
                commandsChoice selectedCommand = commandsChoice.error;

                if (CommandCheck(tokens, currentCommand, ref selectedCommand, superuser) == false)
                {
                    continue;
                }
                else
                {
                    switch (selectedCommand)
                    {
                    case commandsChoice.logout:
                    {
                        return(false);
                    }

                    case commandsChoice.exit:
                    {
                        return(true);
                    }

                    case commandsChoice.clear:
                    {
                        Console.Clear();
                        break;          //or continue
                    }

                    case commandsChoice.where:
                    {
                        Console.WriteLine(CurrentDir);
                        break;
                    }

                    case commandsChoice.addUser:
                    {
                        UserDatabase db = new UserDatabase(@"C:\Users\Aleksa\source\repos\OOSWinSimpleShell\OOSWinSimpleShell\Users.db");
                        if (db.GetUser(tokens[1]) != null)
                        {
                            Console.WriteLine("Username '{0}' already exists.", tokens[1]);
                            break;
                        }

                        Console.Write("Password:"******"C:\Users\Aleksa\source\repos\OOSWinSimpleShell\OOSWinSimpleShell\Users.db");
                        if (db.GetUser(tokens[1]) == null)
                        {
                            Console.WriteLine("User '{0}' doesn't exist.", tokens[1]);
                            break;
                        }

                        Console.Write("sudo password: "******"root").IsPasswordValid(PasswordEnter()))
                        {
                            db.RemoveUser(tokens[1]);
                        }
                        else
                        {
                            Console.WriteLine("\nWrong sudo password.");
                        }
                        Console.WriteLine();
                        break;
                    }

                    case commandsChoice.go:
                    {
                        if (tokens[1].Equals(".."))
                        {
                            if (AliasCurrentDir == CurrentDir && CurrentDir.Contains("\\"))
                            {
                                AliasCurrentDir = CurrentDir = CurrentDir.Substring(0, CurrentDir.LastIndexOf("\\"));
                            }
                            else if (CurrentDir.Contains("\\"))
                            {
                                CurrentDir = CurrentDir.Substring(0, CurrentDir.LastIndexOf("\\"));
                            }
                        }
                        else
                        {
                            tokens[1] = tokens[1].Trim();
                            if ((Directory.Exists(tokens[1]) && tokens[1].Contains("\\")) || Directory.Exists(CurrentDir + "\\" + tokens[1]))
                            {
                                if (AliasCurrentDir == CurrentDir && tokens[1].Contains("\\") && Directory.Exists(CurrentDir + "\\" + tokens[1]))
                                {
                                    AliasCurrentDir = CurrentDir += "\\" + tokens[1];
                                }
                                else if (AliasCurrentDir != CurrentDir && tokens[1].Contains("\\") && Directory.Exists(CurrentDir + "\\" + tokens[1]))
                                {
                                    CurrentDir += "\\" + tokens[1];
                                }
                                else if (AliasCurrentDir == CurrentDir && Directory.Exists(CurrentDir + "\\" + tokens[1]))
                                {
                                    AliasCurrentDir = CurrentDir += "\\" + tokens[1];
                                }
                                else if (AliasCurrentDir != CurrentDir && Directory.Exists(CurrentDir + "\\" + tokens[1]))
                                {
                                    CurrentDir += "\\" + tokens[1];
                                }
                                else if (AliasCurrentDir == CurrentDir && !Directory.Exists(CurrentDir + "\\" + tokens[1]))
                                {
                                    AliasCurrentDir = CurrentDir = tokens[1];
                                }
                                else
                                {
                                    CurrentDir = tokens[1];
                                }
                            }
                            else
                            {
                                Console.WriteLine("Path is not valid.");
                            }
                        }
                        break;
                    }

                    case commandsChoice.alias:
                    {
                        if (tokens.Length == 2)
                        {
                            AliasCurrentDir = tokens[1];
                        }
                        else
                        {
                            AliasCurrentDir = CurrentDir;
                        }
                        break;
                    }

                    case commandsChoice.create:
                    {
                        string fileName = null;
                        string path     = null;

                        SetFileNameAndPath(ref tokens, ref path, ref fileName);         // help method
                        Mkdir(fileName, path, tokens);

                        break;
                    }

                    case commandsChoice.list:
                    {
                        if (tokens.Length == 2)
                        {
                            tokens[1] = tokens[1].Trim();
                            if (Directory.Exists(tokens[1]))
                            {
                                DirectorySearch(tokens[1], "\t");
                            }
                            else
                            {
                                Console.WriteLine("Path is not valid.");
                            }
                        }
                        else
                        {
                            DirectorySearch(CurrentDir, "\t");
                        }
                        break;
                    }

                    case commandsChoice.findDat:
                    {
                        tokens[2] = tokens[2].Trim();
                        if (Directory.Exists(tokens[2]))
                        {
                            FindFile(tokens[2], tokens[1]);
                        }
                        else
                        {
                            Console.WriteLine("Path is not valid.");
                        }
                        break;
                    }

                    case commandsChoice.print:
                    {
                        if (File.Exists(CurrentDir + "\\" + tokens[1]))
                        {
                            string extension = Path.GetExtension(tokens[1]);
                            if (extension == String.Empty)
                            {
                                Console.WriteLine("File {0} doesn't have extension information.", CurrentDir);
                            }
                            else if (extension.Contains("."))
                            {
                                extension = extension.TrimStart('.');
                                if (extension.Equals("txt"))
                                {
                                    Console.Write(File.ReadAllText(CurrentDir + "\\" + tokens[1]));
                                }
                                else
                                {
                                    Console.WriteLine("File '{0}' is not a text file.", CurrentDir + tokens[1]);
                                }
                            }
                            else
                            {
                                Console.WriteLine("File doesn't have an extension.");
                            }
                        }
                        else
                        {
                            Console.WriteLine("File '{0}' doesn't exist in path {1}.", tokens[1], CurrentDir);
                        }
                        break;
                    }

                    case commandsChoice.find:
                    {
                        if (tokens[1].StartsWith("\"") && tokens[1].EndsWith("\"") && tokens[1].Length > 2)
                        {
                            if (File.Exists(CurrentDir + "\\" + tokens[2]))
                            {
                                tokens[1] = tokens[1].Substring(1, tokens[1].Length - 2);


                                var lines   = File.ReadLines(CurrentDir + "\\" + tokens[2]);
                                int lineNmb = 1;
                                foreach (var line in lines)
                                {
                                    if (line.Contains(tokens[1]))
                                    {
                                        Console.Write(lineNmb + ":");
                                        Console.Write(line.Substring(0, line.IndexOf(tokens[1])));
                                        Console.ForegroundColor = ConsoleColor.Red;
                                        Console.Write(tokens[1]);
                                        Console.ResetColor();
                                        Console.WriteLine(line.Substring(line.IndexOf(tokens[1]) + tokens[1].Length));
                                        break;
                                    }
                                    lineNmb++;
                                }
                            }
                            else
                            {
                                Console.WriteLine("File '{0}' doesn't exist in path {1}.", tokens[2], CurrentDir);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Text option doesn't have the correct signature.");
                        }
                        break;
                    }
                    }
                }
            }
        }