🐚Shell Structure
Explaining the inner workings of all the kernel shells
Kernel shells can be built by implementing two different interfaces and base classes. Why two? Because the shell handler relies on:
BaseShell
andIShell
: To hold shell type and initialization codeBaseShellInfo
andIShellInfo
: To hold shell commands and the base shell
Shell Handler
The shell handler, Shell
, uses the available shell list, which holds the BaseShellInfo
abstract class, to manipulate with that shell. That class can be get, depending on the needed type, with the Shell.GetShellInfo()
function in the ︎KS.Shell
namespace.
The shell handler also contains two properties: CurrentShellType
and LastShellType
. The former property holds the current shell type, which can be used with the shell management functions. The latter property holds the last shell type, which is usually the shell that you exited. However, there are three cases:
If there are no shells in the shell stack, it returns the primary
Shell
If there is only one shell in the stack, it returns the current shell as the last one
If there are two or more shells in the stack, it returns the last shell type
Base Shell
The BaseShell
abstract class, which your shell must override, contains the shell type name (ShellType
), the flag to bail from the shell (Bail
), and the shell initialization code with the shell arguments (InitializeShell()
).
The shell initialization code usually waits for the Bail
value to become true
(the shell requested bailing, usually done by exiting the shell using the exit
universal command), as in the below example code.
While it's waiting for this to happen, the shell does what it's programmed to do, but in two conditions:
All shells must call the
Shell.GetLine()
function, which usually is adaptive to your shell type. This is the below example code inside the shell initialization code to illustrate this:
All shells must also handle both the
ThreadInterruptedException
, which must setBail
totrue
, and the general exceptions, which must callcontinue
after dumping the exception to the debugger or to the console. For example, the below example code, inside theInitializeShell()
function:
The shell registration is required once you're done implementing the shell and all its required values, which will show you how to implement them in the next three pages. The function responsible for this action is ShellTypeManager.RegisterShell()
in the KS.Shell.ShellBase.Shells
namespace.
Be sure to unregister your shell using the UnregisterShell()
function, or the shell registry function will not update your BaseShellInfo
class in the available shell lists!
Shell Presets
While Shell.GetLine()
prompts for input, it decides which shell preset, PromptPresetBase
, is used according to the list of presets, ShellPresets
, that should make a new prompt preset class that you made for your shell.
CurrentPreset
specifies the current PromptPresetBase
class, which is usually found in the ShellPresets
list. It usually calls the PromptPresetManager.CurrentPresets[ShellType]
variable.
The first preset should implement a preset called Default
in the ShellPresets
dictionary.
PromptPresetManager.SetPreset()
queries both the shell pre-defined presets, ShellPresets
, and the custom presets, CustomShellPresets
. After that, it sets the preset to the specified preset in the internal CurrentPresets
.
Every preset must implement a base class, PromptPresetBase
and IPromptPreset
, as in below:
The only essential values that you must override in your shell preset class are:
PresetName
: Read-only property. The shell preset name. If this preset is your first preset, it must beDefault
.
PresetPrompt
: Read-only property. Usually calls the overridable internal functionPresetPromptBuilder()
. If it's simple, overriding it with a string is enough.
Optionally, these variables can be overridden:
PresetPromptCompletion
: Read-only property. Usually calls the overridable internal functionPresetPromptCompletionBuilder()
. If it's simple, overriding it with a string is enough.
Shell Information
Every BaseShell
class you create must accompany it with a separate class that implements the BaseShellInfo
and IShellInfo
classes, as in below:
This is where your commands get together by overriding the Commands
variable with the new dictionary containing all your commands, like below (in the UESH shell):
In addition, you can override the ShellPresets
class with a new dictionary containing all the presets for your shell, like below:
ShellBase
, however, must be overridden with an instance of your shell in this form:
Additionally, CurrentPreset
must be overridden with a variable that queries your shell type with the CurrentPresets
variable as in below:
The ShellType
variable found within the BaseShellInfo
class is a wrapper for the ShellBase.ShellType
variable for easier access. It's not overridable and is defined like this:
Command Info
Each command you define in your shell must provide a new instance of the CommandInfo
class holding details about the specified command. The new instance of the class can be made using the constructor defined below:
where:
Command
: The commandType
: Your shell typeHelpDefinition
: The brief summary of what the command doesCommandArgumentInfo
: Argument information about your commandCommandBase
: An instance of theBaseCommand
containing command execution informationCommandFlags
: All command flags
To implement CommandArgumentInfo
, call the constructor either with no parameters, which implies that there is no argument required to run this command, or with the following options listed below.
where:
HelpUsages
: Defines the command usagesArgumentsRequired
: Specifies whether the arguments are requiredMinimumArguments
: Specifies how many arguments are required to execute the commandAutoCompleter
: Optional. Auto completer function that returns an array of suggestions
The base command is required to be implemented, since it contains overridable command execution code. Your command must implement the command base class below:
The only function that you need to override is Execute()
, which you can override like below:
Additionally, you can override the extra help function, HelpHelper()
, like this:
Finally, the command flags (CommandFlags
) can be defined. One or more of the command flags can be defined using the OR (|
) operator when defining the command flags. These flags are available:
Strict
: The command is strict, meaning that it's only available for administrators.The flag value is 1
NoMaintenance
: This command can't run in maintenance mode.The flag value is 2
Obsolete
: The command is obsolete.The flag value is 4
SettingVariable
: The command is setting a UESH variable.The flag value is 8
RedirectionSupported
: Redirection is supported, meaning that all the output to the commands can be redirected to a file.The flag value is 16
Wrappable
: This command is wrappable to pages.The flag value is 32
Hidden
: This command can be executed, but not shown in command list generated by help.The flag value is 64
More?
For information about the help system and how it works, consult the below page:
For command parsing, click the below button:
For shell scripting, click the below button:
Last updated