private static async Task <int> Start() { ICommand cmd = null; var args = ArgumentsHelper.SplitArgs(ArgumentsHelper.GetRealCommandLine()); try { cmd = ArgumentsHelper.ParseCommand(args); if (cmd != null) { try { return(await cmd.Execute().ConfigureAwait(false)); } finally { (cmd as IDisposable)?.Dispose(); } } return(0); } catch (ApplicationException ex) { Logger.Instance.Log(ex.Message, LogLevel.Error); // one liner errors. return(Constants.GSUDO_ERROR_EXITCODE); } catch (Exception ex) { Logger.Instance.Log(ex.ToString(), LogLevel.Error); // verbose errors. return(Constants.GSUDO_ERROR_EXITCODE); } finally { if (InputArguments.KillCache) { await new KillCacheCommand(verbose: false).Execute().ConfigureAwait(false); } try { // cleanup console before returning. Console.CursorVisible = true; Console.ResetColor(); if (InputArguments.Debug && !Console.IsInputRedirected && cmd.GetType().In(typeof(ServiceCommand), typeof(ElevateCommand))) { Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } catch { } } }
public Task <int> Execute() { var caller = Process.GetCurrentProcess().GetParentProcessExcludingShim().MainModule.ModuleName; var length = (int)NativeMethods.GetConsoleCommandHistoryLength(caller); if (length == 0) { throw new ApplicationException("Failed to find last invoked command (GetConsoleCommandHistoryLength==0)"); } IntPtr CommandBuffer = Marshal.AllocHGlobal(length); var ret = NativeMethods.GetConsoleCommandHistory(CommandBuffer, length, caller); if (ret == 0) { throw new ApplicationException($"Failed to find last invoked command (GetConsoleCommandHistory=0; LastErr={Marshal.GetLastWin32Error()})"); } string commandToElevate; var commandHistory = Marshal.PtrToStringAuto(CommandBuffer, length / 2) .Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries) .Reverse() // look for last commands first .Skip(1) // skip gsudo call ; if (Pattern == "!!") { commandToElevate = commandHistory.FirstOrDefault(); } else if (Pattern.StartsWith("!?", StringComparison.OrdinalIgnoreCase)) { commandToElevate = commandHistory.FirstOrDefault(s => s.Contains(Pattern.Substring(2).Trim())); } else // Pattern.StartsWith ("!command") { commandToElevate = commandHistory.FirstOrDefault(s => s.StartsWith(Pattern.Substring(1).Trim(), StringComparison.OrdinalIgnoreCase)); } if (commandToElevate == null) { throw new ApplicationException("Failed to find last invoked command in history."); } Logger.Instance.Log("Command to run: " + commandToElevate, LogLevel.Info); return(new RunCommand() { CommandToRun = ArgumentsHelper.SplitArgs(commandToElevate) } .Execute()); }
public void Arguments_QuotedTests() { var input = "\"my exe name\" \"my params\" OtherParam1 OtherParam2 OtherParam3"; var expected = new string[] { "\"my exe name\"", "\"my params\"", "OtherParam1", "OtherParam2", "OtherParam3" }; var actual = ArgumentsHelper.SplitArgs(input).ToArray(); Assert.AreEqual(expected.Length, actual.Length); for (int i = 0; i < expected.Length; i++) { Assert.AreEqual(expected[i], actual[i]); } }
public void Arguments_NoQuotesTests() { var input = "HEllo I Am my params OtherParam1 OtherParam2 OtherParam3"; var expected = new string[] { "HEllo", "I", "Am", "my", "params", "OtherParam1", "OtherParam2", "OtherParam3" }; var actual = ArgumentsHelper.SplitArgs(input).ToArray(); Assert.AreEqual(expected.Length, actual.Length); for (int i = 0; i < expected.Length; i++) { Assert.AreEqual(expected[i], actual[i]); } }
private static async Task <int> Start() { ICommand cmd = null; var args = ArgumentsHelper.SplitArgs(ArgumentsHelper.GetRealCommandLine()); var parserError = ArgumentsHelper.ParseCommonSettings(ref args); if (parserError.HasValue) { return(parserError.Value); } cmd = ArgumentsHelper.ParseCommand(args); try { if (cmd != null) { try { return(await cmd.Execute().ConfigureAwait(false)); } finally { (cmd as IDisposable)?.Dispose(); } } else { return(await new HelpCommand().Execute().ConfigureAwait(false)); } } catch (Exception ex) { Logger.Instance.Log(ex.ToString(), LogLevel.Error); return(Constants.GSUDO_ERROR_EXITCODE); } finally { try { // cleanup console before returning. Console.CursorVisible = true; Console.ResetColor(); } catch { } } }