From f8854f0e4477f87ef68649e769b8126e7586865a Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 29 Jul 2017 11:01:32 -0400 Subject: subshells and shell-specific cmds --- ShiftOS_TheReturn/TerminalBackend.cs | 138 +++++++++++++++++++++++++++-------- 1 file changed, 107 insertions(+), 31 deletions(-) (limited to 'ShiftOS_TheReturn') diff --git a/ShiftOS_TheReturn/TerminalBackend.cs b/ShiftOS_TheReturn/TerminalBackend.cs index 689465f..dcc4625 100644 --- a/ShiftOS_TheReturn/TerminalBackend.cs +++ b/ShiftOS_TheReturn/TerminalBackend.cs @@ -40,6 +40,29 @@ namespace ShiftOS.Engine /// public static class TerminalBackend { + private static string _shellOverrideString = ""; + + + /// + /// Gets the current shell prompt override string. + /// + public static string ShellOverride + { + get + { + return (string.IsNullOrWhiteSpace(_shellOverrideString) || SaveSystem.CurrentSave == null) ? $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ " : _shellOverrideString; + } + } + + /// + /// Sets the shell override string to the specified value. Empty string or to use the default ShiftOS string. + /// + /// The string to use as a shell prompt. + public static void SetShellOverride(string value) + { + _shellOverrideString = value; + } + /// /// Occurs when a command is processed. /// @@ -133,6 +156,17 @@ namespace ShiftOS.Engine public class TerminalCommand { + public virtual bool MatchShell() + { + if(ShellMatch != "metacmd") + { + return (ShellMatch == _shellOverrideString); + } + return true; + } + + public string ShellMatch { get; set; } + public override int GetHashCode() { int hash = 0; @@ -178,11 +212,19 @@ namespace ShiftOS.Engine public virtual void Invoke(Dictionary args) { List errors = new List(); + if (ShellMatch != "metacmd") + { + if (ShellMatch != TerminalBackend._shellOverrideString) + { + errors.Add("Command not found."); + } + } + if (errors.Count > 0) { foreach (var error in errors) { - Console.WriteLine("Command error: " + error); + Console.WriteLine(error); } return; } @@ -197,10 +239,28 @@ namespace ShiftOS.Engine } } + [MetaCommand] + [Command("exit")] + public static void Exit() + { + if (_shellOverrideString != "") + _shellOverrideString = ""; + else + { + Console.WriteLine("error: cannot exit system shell"); + } + } + + public class WinOpenCommand : TerminalCommand { public Type ShiftOSWindow { get; set; } + public override bool MatchShell() + { + return (_shellOverrideString == ""); + } + public override void Invoke(Dictionary args) { @@ -296,6 +356,13 @@ namespace ShiftOS.Engine var tc = new TerminalCommand(); tc.RequiresElevation = !(type.GetCustomAttributes(false).FirstOrDefault(x => x is KernelModeAttribute) == null); + var shellConstraint = mth.GetCustomAttributes(false).FirstOrDefault(x => x is ShellConstraintAttribute) as ShellConstraintAttribute; + tc.ShellMatch = (shellConstraint == null) ? "" : shellConstraint.Shell; + + if(mth.GetCustomAttributes(false).FirstOrDefault(x=>x is MetaCommandAttribute) != null) + { + tc.ShellMatch = "metacmd"; + } tc.CommandInfo = cmd as Command; tc.RequiresElevation = tc.RequiresElevation || !(mth.GetCustomAttributes(false).FirstOrDefault(x => x is KernelModeAttribute) == null); @@ -471,42 +538,23 @@ namespace ShiftOS.Engine return true; } +#if DEBUG + [Command("setshell", hide = true)] + [RequiresArgument("id")] + public static void Debug_SetShellOverrideCMD(Dictionary args) + { + SetShellOverride(args["id"].ToString()); + } +#endif + /// /// Prints the user prompt to the terminal. /// public static void PrintPrompt() { - if (SaveSystem.CurrentSave != null) + if (PrefixEnabled) { - ConsoleEx.BackgroundColor = SkinEngine.LoadedSkin.TerminalBackColorCC; - ConsoleEx.Italic = false; - ConsoleEx.Underline = false; - - ConsoleEx.ForegroundColor = ConsoleColor.Magenta; - ConsoleEx.Bold = true; - - Console.Write(SaveSystem.CurrentSave.Username); - ConsoleEx.Bold = false; - ConsoleEx.ForegroundColor = ConsoleColor.Gray; - Console.Write("@"); - ConsoleEx.Italic = true; - ConsoleEx.Bold = true; - ConsoleEx.ForegroundColor = ConsoleColor.Yellow; - Console.Write(SaveSystem.CurrentSave.SystemName); - ConsoleEx.Italic = false; - ConsoleEx.Bold = false; - ConsoleEx.ForegroundColor = ConsoleColor.Gray; - Console.Write(":~"); - Console.ForegroundColor = ConsoleColor.White; - ConsoleEx.Italic = true; - if (KernelWatchdog.InKernelMode == true) - Console.Write("#"); - else - Console.Write("$"); - ConsoleEx.Italic = false; - ConsoleEx.Bold = false; - ConsoleEx.ForegroundColor = SkinEngine.LoadedSkin.TerminalForeColorCC; - Console.Write(" "); + Console.Write(ShellOverride); ConsoleEx.Flush(); } } @@ -536,4 +584,32 @@ namespace ShiftOS.Engine } } + + /// + /// Marks this command so that it can be run in ANY shell. + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public class MetaCommandAttribute : Attribute + { + + } + + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public class ShellConstraintAttribute : Attribute + { + /// + /// Instructs the terminal command interpreter to disallow running of this command unless the user shell override matches up with the value provided here. + /// + /// The required shell string. Null or whitespace to match with the default ShiftOS shell. + public ShellConstraintAttribute(string shell) + { + Shell = shell; + } + + + /// + /// Gets the required shell string for the command. + /// + public string Shell { get; private set; } + } } -- cgit v1.2.3