Example #1
0
        /// <summary>
        /// Executes a <c>cmd.exe</c> and passes it a specified command.
        /// </summary>
        /// <param name="command">The command to be passed.</param>
        /// <param name="handling">How to handle the output.</param>
        /// <param name="arrayOutput">
        /// A list where output lines will be added if <paramref name="handling"/> is <see cref="OutputHandling.ArrayOfLines"/>.
        /// </param>
        /// <param name="stringOutput">
        /// A string containing the entire output in if <paramref name="handling"/> is <see cref="OutputHandling.String"/>
        /// or the last line of the output if <paramref name="handling"/> is <see cref="OutputHandling.ArrayOfLines"/> or
        /// <see cref="OutputHandling.FlushLinesToScriptOutput"/>.
        /// </param>
        /// <returns>Exit code of the process.</returns>
        public static int ShellExec(string command, OutputHandling handling, PhpArray arrayOutput, out string stringOutput)
        {
            if (!MakeCommandSafe(ref command))
            {
                stringOutput = "";
                return(-1);
            }

            //using (Process p = new Process())
            //{
            //    IdentitySection identityConfig = null;

            //    try { identityConfig = WebConfigurationManager.GetSection("system.web/identity") as IdentitySection; }
            //    catch { }

            //    if (identityConfig != null)
            //    {
            //        p.StartInfo.UserName = identityConfig.UserName;
            //        if (identityConfig.Password != null)
            //        {
            //            p.StartInfo.Password = new SecureString();
            //            foreach (char c in identityConfig.Password) p.StartInfo.Password.AppendChar(c);
            //            p.StartInfo.Password.MakeReadOnly();
            //        }
            //    }

            //    p.StartInfo.FileName = "cmd.exe";
            //    p.StartInfo.Arguments = "/c " + command;
            //    p.StartInfo.UseShellExecute = false;
            //    p.StartInfo.CreateNoWindow = true;
            //    p.StartInfo.RedirectStandardOutput = true;
            //    p.Start();

            //    stringOutput = null;
            //    switch (handling)
            //    {
            //        case OutputHandling.String:
            //            stringOutput = p.StandardOutput.ReadToEnd();
            //            break;

            //        case OutputHandling.ArrayOfLines:
            //            {
            //                string line;
            //                while ((line = p.StandardOutput.ReadLine()) != null)
            //                {
            //                    stringOutput = line;
            //                    if (arrayOutput != null) arrayOutput.Add(line);
            //                }
            //                break;
            //            }

            //        case OutputHandling.FlushLinesToScriptOutput:
            //            {
            //                ScriptContext context = ScriptContext.CurrentContext;

            //                string line;
            //                while ((line = p.StandardOutput.ReadLine()) != null)
            //                {
            //                    stringOutput = line;
            //                    context.Output.WriteLine(line);
            //                    context.Output.Flush();
            //                }
            //                break;
            //            }

            //        case OutputHandling.RedirectToScriptOutput:
            //            {
            //                ScriptContext context = ScriptContext.CurrentContext;

            //                byte[] buffer = new byte[1024];
            //                int count;
            //                while ((count = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
            //                {
            //                    context.OutputStream.Write(buffer, 0, count);
            //                }
            //                break;
            //            }
            //    }

            //    p.WaitForExit();

            //    return p.ExitCode;
            //}

            throw new NotImplementedException();
        }
Example #2
0
        /// <summary>
        /// Executes a <c>cmd.exe</c> and passes it a specified command.
        /// </summary>
        /// <param name="command">The command to be passed.</param>
        /// <param name="handling">How to handle the output.</param>
        /// <param name="arrayOutput">
        /// A list where output lines will be added if <paramref name="handling"/> is <see cref="OutputHandling.ArrayOfLines"/>.
        /// </param>
        /// <param name="stringOutput">
        /// A string containing the entire output in if <paramref name="handling"/> is <see cref="OutputHandling.String"/>
        /// or the last line of the output if <paramref name="handling"/> is <see cref="OutputHandling.ArrayOfLines"/> or
        /// <see cref="OutputHandling.FlushLinesToScriptOutput"/>.
        /// </param>
        /// <returns>Exit code of the process.</returns>
        public static int ShellExec(string command, OutputHandling handling, IList arrayOutput, out string stringOutput)
        {
            if (!MakeCommandSafe(ref command))
            {
                stringOutput = "";
                return(-1);
            }

            using (Process p = new Process())
            {
                IdentitySection identityConfig = null;

                try { identityConfig = WebConfigurationManager.GetSection("system.web/identity") as IdentitySection; }
                catch { }

                if (identityConfig != null)
                {
                    p.StartInfo.UserName = identityConfig.UserName;
                    if (identityConfig.Password != null)
                    {
                        p.StartInfo.Password = new SecureString();
                        foreach (char c in identityConfig.Password)
                        {
                            p.StartInfo.Password.AppendChar(c);
                        }
                        p.StartInfo.Password.MakeReadOnly();
                    }
                }

                p.StartInfo.FileName               = "cmd.exe";
                p.StartInfo.Arguments              = "/c " + command;
                p.StartInfo.UseShellExecute        = false;
                p.StartInfo.CreateNoWindow         = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.Start();

                stringOutput = null;
                switch (handling)
                {
                case OutputHandling.String:
                    stringOutput = p.StandardOutput.ReadToEnd();
                    break;

                case OutputHandling.ArrayOfLines:
                {
                    string line;
                    while ((line = p.StandardOutput.ReadLine()) != null)
                    {
                        stringOutput = line;
                        if (arrayOutput != null)
                        {
                            arrayOutput.Add(line);
                        }
                    }
                    break;
                }

                case OutputHandling.FlushLinesToScriptOutput:
                {
                    ScriptContext context = ScriptContext.CurrentContext;

                    string line;
                    while ((line = p.StandardOutput.ReadLine()) != null)
                    {
                        stringOutput = line;
                        context.Output.WriteLine(line);
                        context.Output.Flush();
                    }
                    break;
                }

                case OutputHandling.RedirectToScriptOutput:
                {
                    ScriptContext context = ScriptContext.CurrentContext;

                    byte[] buffer = new byte[1024];
                    int    count;
                    while ((count = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        context.OutputStream.Write(buffer, 0, count);
                    }
                    break;
                }
                }

                p.WaitForExit();

                return(p.ExitCode);
            }
        }
 public void Print <T>(T s)
 {
     OutputHandling.Message(s);
 }
Example #4
0
		/// <summary>
		/// Executes a <c>cmd.exe</c> and passes it a specified command.
		/// </summary>
		/// <param name="command">The command to be passed.</param>
		/// <param name="handling">How to handle the output.</param>
		/// <param name="arrayOutput">
		/// A list where output lines will be added if <paramref name="handling"/> is <see cref="OutputHandling.ArrayOfLines"/>.
		/// </param>
		/// <param name="stringOutput">
		/// A string containing the entire output in if <paramref name="handling"/> is <see cref="OutputHandling.String"/>
		/// or the last line of the output if <paramref name="handling"/> is <see cref="OutputHandling.ArrayOfLines"/> or
		/// <see cref="OutputHandling.FlushLinesToScriptOutput"/>. 
		/// </param>
		/// <returns>Exit code of the process.</returns>
		public static int ShellExec(string command, OutputHandling handling, IList arrayOutput, out string stringOutput)
		{
			if (!MakeCommandSafe(ref command))
			{
				stringOutput = "";
				return -1;
			}

			using (Process p = new Process())
			{
                IdentitySection identityConfig = null;

                try { identityConfig = WebConfigurationManager.GetSection("system.web/identity") as IdentitySection; }
                catch { }

                if (identityConfig != null)
                {
                    p.StartInfo.UserName = identityConfig.UserName;
                    if (identityConfig.Password != null)
                    {
                        p.StartInfo.Password = new SecureString();
                        foreach (char c in identityConfig.Password) p.StartInfo.Password.AppendChar(c);
                        p.StartInfo.Password.MakeReadOnly();
                    }                    
                }

				p.StartInfo.FileName = "cmd.exe";
				p.StartInfo.Arguments = "/c " + command;
				p.StartInfo.UseShellExecute = false;
				p.StartInfo.CreateNoWindow = true;
				p.StartInfo.RedirectStandardOutput = true;
				p.Start();

				stringOutput = null;
				switch (handling)
				{
					case OutputHandling.String:
						stringOutput = p.StandardOutput.ReadToEnd();
						break;

					case OutputHandling.ArrayOfLines:
						{
							string line;
							while ((line = p.StandardOutput.ReadLine()) != null)
							{
								stringOutput = line;
								if (arrayOutput != null) arrayOutput.Add(line);
							}
							break;
						}

					case OutputHandling.FlushLinesToScriptOutput:
						{
							ScriptContext context = ScriptContext.CurrentContext;

							string line;
							while ((line = p.StandardOutput.ReadLine()) != null)
							{
								stringOutput = line;
								context.Output.WriteLine(line);
								context.Output.Flush();
							}
							break;
						}

					case OutputHandling.RedirectToScriptOutput:
						{
							ScriptContext context = ScriptContext.CurrentContext;

							byte[] buffer = new byte[1024];
							int count;
							while ((count = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
							{
								context.OutputStream.Write(buffer, 0, count);
							}
							break;
						}
				}

				p.WaitForExit();

				return p.ExitCode;
			}
		}
 public static void Download(Uri uri, string fileName, WebClient webClient)
 {
     OutputHandling.Message($"Downloading {fileName}...", ConsoleColor.Cyan);
     webClient.DownloadFile(uri, fileName);
     OutputHandling.Message($"{fileName} has been successfully downloaded", ConsoleColor.Green);
 }
        public static void MainMenu()
        {
            Console.WriteLine();
            OutputHandling.Message("1 - Add a book");
            OutputHandling.Message("2 - Print book info");
            OutputHandling.Message("3 - Update book info");
            OutputHandling.Message("4 - Delete book from database");
            OutputHandling.Message("5 - Exit");

            ConsoleKeyInfo cki = Console.ReadKey(true);

            if (cki.Key.Equals(ConsoleKey.D1) || cki.Key.Equals(ConsoleKey.NumPad1))
            {
                Console.Clear();
                string title = InputHandling.ReadString("Book Title: ");
                int    pId   = InputHandling.ReadValue("PublisherId: ");
                int    year  = InputHandling.ReadValue("Year: ");
                int    price = InputHandling.ReadValue("Price: ");
                BooksCrud.InsertBook(title, pId, year, price);
                MainMenu();
            }

            else if (cki.Key.Equals(ConsoleKey.D2) || cki.Key.Equals(ConsoleKey.NumPad2))
            {
                Console.Clear();
                int          bId    = InputHandling.ReadValue("Book Id:");
                SqlParameter bookId = new SqlParameter {
                    Value = bId, SqlDbType = SqlDbType.Int, ParameterName = "bookId"
                };
                EnumerableRowCollection <DataRow> bookInfo = BooksCrud.ReadBook(bookId);

                foreach (DataRow row in bookInfo)
                {
                    Console.WriteLine($"Id: {row[0]}");
                    Console.WriteLine($"Title: {row[1]}");
                    Console.WriteLine($"Publisher: {row[2]}");
                    Console.WriteLine($"Year: {row[3]}");
                    Console.WriteLine($"Price: {row[4]}");
                }
                MainMenu();
            }

            else if (cki.Key.Equals(ConsoleKey.D3) || cki.Key.Equals(ConsoleKey.NumPad3))
            {
                Console.Clear();
                int bId = InputHandling.ReadValue("Book Id:");
                UpdateMenu(bId);
                MainMenu();
            }

            else if (cki.Key.Equals(ConsoleKey.D4) || cki.Key.Equals(ConsoleKey.NumPad4))
            {
                int          bId    = InputHandling.ReadValue("Book Id:");
                SqlParameter bookId = new SqlParameter {
                    Value = bId, SqlDbType = SqlDbType.Int, ParameterName = "bookId"
                };
                BooksCrud.DeleteBook(bookId);
                MainMenu();
            }

            else if (cki.Key.Equals(ConsoleKey.D5) || cki.Key.Equals(ConsoleKey.NumPad5))
            {
                ProgramFlowHandling.Exit("Program will now exit...");
                dbBooksConn.Dispose();
            }
            else
            {
                MainMenu();
            }
        }
        /// <summary>
        /// Executes a <c>cmd.exe</c> and passes it a specified command.
        /// </summary>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="command">The command to be passed.</param>
        /// <param name="handling">How to handle the output.</param>
        /// <param name="arrayOutput">
        /// A list where output lines will be added if <paramref name="handling"/> is <see cref="OutputHandling.ArrayOfLines"/>.
        /// </param>
        /// <param name="stringOutput">
        /// A string containing the entire output in if <paramref name="handling"/> is <see cref="OutputHandling.String"/>
        /// or the last line of the output if <paramref name="handling"/> is <see cref="OutputHandling.ArrayOfLines"/> or
        /// <see cref="OutputHandling.FlushLinesToScriptOutput"/>.
        /// </param>
        /// <returns>Exit code of the process.</returns>
        public static int ShellExec(Context ctx, string command, OutputHandling handling, PhpArray arrayOutput, out string stringOutput)
        {
            if (!MakeCommandSafe(ref command))
            {
                stringOutput = string.Empty;
                return(-1);
            }

            using (var p = new Process())
            {
                //IdentitySection identityConfig = null;

                //try { identityConfig = WebConfigurationManager.GetSection("system.web/identity") as IdentitySection; }
                //catch { }

                //if (identityConfig != null)
                //{
                //    p.StartInfo.UserName = identityConfig.UserName;
                //    if (identityConfig.Password != null)
                //    {
                //        p.StartInfo.Password = new SecureString();
                //        foreach (char c in identityConfig.Password) p.StartInfo.Password.AppendChar(c);
                //        p.StartInfo.Password.MakeReadOnly();
                //    }
                //}

                if (CurrentPlatform.IsWindows)
                {
                    p.StartInfo.FileName  = "cmd.exe";
                    p.StartInfo.Arguments = "/c " + command;
                }
                else
                {
                    p.StartInfo.FileName  = "/bin/bash";
                    p.StartInfo.Arguments = command;
                }

                p.StartInfo.UseShellExecute        = false;
                p.StartInfo.CreateNoWindow         = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.Start();

                stringOutput = null;
                switch (handling)
                {
                case OutputHandling.String:
                    stringOutput = p.StandardOutput.ReadToEnd();
                    break;

                case OutputHandling.ArrayOfLines:
                {
                    string line;
                    while ((line = p.StandardOutput.ReadLine()) != null)
                    {
                        arrayOutput?.Add(line);
                        stringOutput = line;
                    }
                    break;
                }

                case OutputHandling.FlushLinesToScriptOutput:
                {
                    string line;
                    while ((line = p.StandardOutput.ReadLine()) != null)
                    {
                        stringOutput = line;
                        ctx.Output.WriteLine(line);
                        ctx.Output.Flush();
                    }
                    break;
                }

                case OutputHandling.RedirectToScriptOutput:
                {
                    byte[] buffer = new byte[1024];
                    int    count;
                    while ((count = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ctx.OutputStream.Write(buffer, 0, count);
                    }
                    break;
                }
                }

                p.WaitForExit();

                //
                return(p.ExitCode);
            }
        }
 public void Print(Student s)
 {
     OutputHandling.Message(s);
 }
 private static void PrintNumberOfRows()
 {
     Console.Clear();
     OutputHandling.Message($"Number of rows: {CRUDPublisher.GetNumberOfRows()}");
     MainMenu();
 }
Example #10
0
        public static int ShellExec(Context ctx, string command, OutputHandling handling, PhpArray?arrayOutput, out string?stringOutput)
        {
            if (!MakeCommandSafe(ref command))
            {
                stringOutput = null;
                return(-1);
            }

            using (var p = new Process())
            {
                //IdentitySection identityConfig = null;

                //try { identityConfig = WebConfigurationManager.GetSection("system.web/identity") as IdentitySection; }
                //catch { }

                //if (identityConfig != null)
                //{
                //    p.StartInfo.UserName = identityConfig.UserName;
                //    if (identityConfig.Password != null)
                //    {
                //        p.StartInfo.Password = new SecureString();
                //        foreach (char c in identityConfig.Password) p.StartInfo.Password.AppendChar(c);
                //        p.StartInfo.Password.MakeReadOnly();
                //    }
                //}

                // prepare arguments
                {
                    var arguments = StringBuilderUtilities.Pool.Get();

                    if (CurrentPlatform.IsWindows)
                    {
                        p.StartInfo.FileName = "cmd.exe";
                        AppendArgument(arguments, "/c");
                    }
                    else
                    {
                        p.StartInfo.FileName = "/bin/bash";
                        AppendArgument(arguments, "-c");
                    }

                    AppendArgument(arguments, command);

                    p.StartInfo.Arguments = StringBuilderUtilities.GetStringAndReturn(arguments);
                }

                p.StartInfo.UseShellExecute        = false;
                p.StartInfo.CreateNoWindow         = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.Start();

                stringOutput = null;
                switch (handling)
                {
                case OutputHandling.String:
                    stringOutput = p.StandardOutput.ReadToEnd();
                    break;

                case OutputHandling.ArrayOfLines:
                {
                    string line;
                    while ((line = p.StandardOutput.ReadLine()) != null)
                    {
                        arrayOutput?.Add(line);
                        stringOutput = line;
                    }
                    break;
                }

                case OutputHandling.FlushLinesToScriptOutput:
                {
                    string line;
                    while ((line = p.StandardOutput.ReadLine()) != null)
                    {
                        stringOutput = line;
                        ctx.Output.WriteLine(line);
                        ctx.Output.Flush();
                    }
                    break;
                }

                case OutputHandling.RedirectToScriptOutput:
                    p.StandardOutput.BaseStream
                    .CopyToAsync(ctx.OutputStream)
                    .GetAwaiter()
                    .GetResult();

                    break;
                }

                p.WaitForExit();

                //
                return(p.ExitCode);
            }
        }
Example #11
0
		/// <summary>
		/// Executes a <c>cmd.exe</c> and passes it a specified command.
		/// </summary>
		/// <param name="command">The command to be passed.</param>
		/// <param name="handling">How to handle the output.</param>
		/// <param name="arrayOutput">
		/// A list where output lines will be added if <paramref name="handling"/> is <see cref="OutputHandling.ArrayOfLines"/>.
		/// </param>
		/// <param name="stringOutput">
		/// A string containing the entire output in if <paramref name="handling"/> is <see cref="OutputHandling.String"/>
		/// or the last line of the output if <paramref name="handling"/> is <see cref="OutputHandling.ArrayOfLines"/> or
		/// <see cref="OutputHandling.FlushLinesToScriptOutput"/>. 
		/// </param>
		/// <returns>Exit code of the process.</returns>
		public static int ShellExec(string command, OutputHandling handling, IList arrayOutput, out string stringOutput)
		{
			if (!MakeCommandSafe(ref command))
			{
				stringOutput = "";
				return -1;
			}

			using (Process p = new Process())
			{
                IdentitySection identityConfig = null;

                try { identityConfig = WebConfigurationManager.GetSection("system.web/identity") as IdentitySection; }
                catch { }

                if (identityConfig != null)
                {
                    p.StartInfo.UserName = identityConfig.UserName;
                    if (identityConfig.Password != null)
                    {
                        p.StartInfo.Password = new SecureString();
                        foreach (char c in identityConfig.Password) p.StartInfo.Password.AppendChar(c);
                        p.StartInfo.Password.MakeReadOnly();
                    }                    
                }

                if (Environment.OSVersion.Platform == PlatformID.Unix)
                {
                    // TODO: Use same code in ShellExec() and CreateProcessExecutingCommand()
                    string bin, args;

                    command = command.Trim();
                    string quoteChar = "\"";
                    if (command.Substring(0, 1) == quoteChar)
                    {
                        // Parse commands surrounded by single and double quotes (['ls' -la] and ["ls" -la]).
                        int end = command.IndexOf(quoteChar, 1);
                        bin = command.Substring(1, end - 1);
                        args = command.Substring(end + 1);

                        // TODO: Support redirections.
                        args = args.Replace("2>&1", "");

                        p.StartInfo.Arguments = args;
                        // System.Console.WriteLine("bin: " + bin + ", args: " + args);
                    }
                    else
                    {
                        int i = command.IndexOf(" ");
                        if (i >= 0)
                        {
                            // "ls -la"
                            bin = command.Substring(0, i);
                            args = command.Substring(i + 1);

                            p.StartInfo.Arguments = args;
                        }
                        else
                        {
                            // "ls"
                            bin = command;
                        }
                    }
                    p.StartInfo.FileName = bin;
                }
                else
                {
                    p.StartInfo.FileName = "cmd.exe";
                    p.StartInfo.Arguments = "/c " + command;
                }

				p.StartInfo.UseShellExecute = false;
				p.StartInfo.CreateNoWindow = true;
				p.StartInfo.RedirectStandardOutput = true;
				p.Start();

				stringOutput = null;
				switch (handling)
				{
					case OutputHandling.String:
						stringOutput = p.StandardOutput.ReadToEnd();
						break;

					case OutputHandling.ArrayOfLines:
						{
							string line;
							while ((line = p.StandardOutput.ReadLine()) != null)
							{
								stringOutput = line;
								if (arrayOutput != null) arrayOutput.Add(line);
							}
							break;
						}

					case OutputHandling.FlushLinesToScriptOutput:
						{
							ScriptContext context = ScriptContext.CurrentContext;

							string line;
							while ((line = p.StandardOutput.ReadLine()) != null)
							{
								stringOutput = line;
								context.Output.WriteLine(line);
								context.Output.Flush();
							}
							break;
						}

					case OutputHandling.RedirectToScriptOutput:
						{
							ScriptContext context = ScriptContext.CurrentContext;

							byte[] buffer = new byte[1024];
							int count;
							while ((count = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
							{
								context.OutputStream.Write(buffer, 0, count);
							}
							break;
						}
				}

				p.WaitForExit();

				return p.ExitCode;
			}
		}
Example #12
0
 public override void ProduceSound()
 {
     OutputHandling.Message("Woof! Woof!");
 }
Example #13
0
 public override void ProduceSound()
 {
     OutputHandling.Message("Ribbit!");
 }
 public static void ExitProgram()
 {
     OutputHandling.Question("Press any key to exit...");
     Console.ReadKey();
 }
 public static void PrintFileContents(string fileContents)
 {
     OutputHandling.Message(fileContents);
 }
Example #16
0
 public override void ProduceSound()
 {
     OutputHandling.Message("Meow!!!");
 }