Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
SpecialVariables.cs432 linesDownload Raw Back to engine
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections.Generic;5using System.Management.Automation.Internal;6 7namespace System.Management.Automation8{9    //10    // SpecialVariables contains the names and variable paths to any variable that PowerShell depends11    // on in some way, either in that it is an automatic variable (created and updated automatically)12    // or configuration variables that users may or may not set.13    //14    // The convention is to have a const string field with either the exact variable name, or if that's15    // not possible, a suggestive name, such as Underbar.  Having a field is preferred over explicit strings16    // to make searching easier.17    //18    // The other convention is to have a VariablePath field with "VarPath" appended to the string19    // field name.  In general, it is preferred to use the VariablePath instead of the string20    // because we'll end up creating a VariablePath anyway, so doing it once is faster.21    //22    internal static class SpecialVariables23    {24        internal const string HistorySize = "MaximumHistoryCount";25 26        internal static readonly VariablePath HistorySizeVarPath = new VariablePath(HistorySize);27 28        internal const string MyInvocation = "MyInvocation";29 30        internal static readonly VariablePath MyInvocationVarPath = new VariablePath(MyInvocation);31 32        internal const string OFS = "OFS";33 34        internal static readonly VariablePath OFSVarPath = new VariablePath(OFS);35 36        internal const string PSStyle = "PSStyle";37 38        internal static readonly VariablePath PSStyleVarPath = new VariablePath(PSStyle);39 40        internal const string OutputEncoding = "OutputEncoding";41 42        internal static readonly VariablePath OutputEncodingVarPath = new VariablePath(OutputEncoding);43 44        internal const string PSApplicationOutputEncoding = nameof(PSApplicationOutputEncoding);45 46        internal static readonly VariablePath PSApplicationOutputEncodingVarPath = new VariablePath(PSApplicationOutputEncoding);47 48        internal const string VerboseHelpErrors = "VerboseHelpErrors";49 50        internal static readonly VariablePath VerboseHelpErrorsVarPath = new VariablePath(VerboseHelpErrors);51 52        #region Logging Variables53 54        internal const string LogEngineHealthEvent = "LogEngineHealthEvent";55 56        internal static readonly VariablePath LogEngineHealthEventVarPath = new VariablePath(LogEngineHealthEvent);57 58        internal const string LogEngineLifecycleEvent = "LogEngineLifecycleEvent";59 60        internal static readonly VariablePath LogEngineLifecycleEventVarPath = new VariablePath(LogEngineLifecycleEvent);61 62        internal const string LogCommandHealthEvent = "LogCommandHealthEvent";63 64        internal static readonly VariablePath LogCommandHealthEventVarPath = new VariablePath(LogCommandHealthEvent);65 66        internal const string LogCommandLifecycleEvent = "LogCommandLifecycleEvent";67 68        internal static readonly VariablePath LogCommandLifecycleEventVarPath = new VariablePath(LogCommandLifecycleEvent);69 70        internal const string LogProviderHealthEvent = "LogProviderHealthEvent";71 72        internal static readonly VariablePath LogProviderHealthEventVarPath = new VariablePath(LogProviderHealthEvent);73 74        internal const string LogProviderLifecycleEvent = "LogProviderLifecycleEvent";75 76        internal static readonly VariablePath LogProviderLifecycleEventVarPath = new VariablePath(LogProviderLifecycleEvent);77 78        internal const string LogSettingsEvent = "LogSettingsEvent";79 80        internal static readonly VariablePath LogSettingsEventVarPath = new VariablePath(LogSettingsEvent);81 82        internal const string PSLogUserData = "PSLogUserData";83 84        internal static readonly VariablePath PSLogUserDataPath = new VariablePath(PSLogUserData);85 86        #endregion Logging Variables87 88        internal const string NestedPromptLevel = "NestedPromptLevel";89 90        internal static readonly VariablePath NestedPromptCounterVarPath = new VariablePath("global:" + NestedPromptLevel);91 92        internal const string CurrentlyExecutingCommand = "CurrentlyExecutingCommand";93 94        internal static readonly VariablePath CurrentlyExecutingCommandVarPath = new VariablePath(CurrentlyExecutingCommand);95 96        internal const string PSBoundParameters = "PSBoundParameters";97 98        internal static readonly VariablePath PSBoundParametersVarPath = new VariablePath(PSBoundParameters);99 100        internal const string Matches = "Matches";101 102        internal static readonly VariablePath MatchesVarPath = new VariablePath(Matches);103 104        internal const string LastExitCode = "LASTEXITCODE";105 106        internal static readonly VariablePath LastExitCodeVarPath = new VariablePath("global:" + LastExitCode);107 108        internal const string PSDebugContext = "PSDebugContext";109 110        internal static readonly VariablePath PSDebugContextVarPath = new VariablePath(PSDebugContext);111 112        internal const string StackTrace = "StackTrace";113 114        internal static readonly VariablePath StackTraceVarPath = new VariablePath("global:" + StackTrace);115 116        internal const string FirstToken = "^";117 118        internal static readonly VariablePath FirstTokenVarPath = new VariablePath("global:" + FirstToken);119 120        internal const string LastToken = "$";121 122        internal static readonly VariablePath LastTokenVarPath = new VariablePath("global:" + LastToken);123 124        internal static bool IsUnderbar(string name) { return name.Length == 1 && name[0] == '_'; }125 126        internal const string PSItem = "PSItem";  // simple alias for $_127        internal const string Underbar = "_";128 129        internal static readonly VariablePath UnderbarVarPath = new VariablePath(Underbar);130 131        internal const string Question = "?";132 133        internal static readonly VariablePath QuestionVarPath = new VariablePath(Question);134 135        internal const string Args = "args";136 137        internal static readonly VariablePath ArgsVarPath = new VariablePath("local:" + Args);138 139        internal const string This = "this";140 141        internal static readonly VariablePath ThisVarPath = new VariablePath("this");142 143        internal const string Input = "input";144 145        internal static readonly VariablePath InputVarPath = new VariablePath("local:" + Input);146 147        internal const string PSCmdlet = "PSCmdlet";148 149        internal static readonly VariablePath PSCmdletVarPath = new VariablePath("PSCmdlet");150 151        internal const string Error = "error";152 153        internal static readonly VariablePath ErrorVarPath = new VariablePath("global:" + Error);154 155        internal const string EventError = "error";156 157        internal static readonly VariablePath EventErrorVarPath = new VariablePath("script:" + EventError);158#if !UNIX159        internal const string PathExt = "env:PATHEXT";160 161        internal static readonly VariablePath PathExtVarPath = new VariablePath(PathExt);162#endif163        internal const string PSEmailServer = "PSEmailServer";164 165        internal static readonly VariablePath PSEmailServerVarPath = new VariablePath(PSEmailServer);166 167        internal const string PSDefaultParameterValues = "PSDefaultParameterValues";168 169        internal static readonly VariablePath PSDefaultParameterValuesVarPath = new VariablePath(PSDefaultParameterValues);170 171        internal const string PSScriptRoot = "PSScriptRoot";172 173        internal static readonly VariablePath PSScriptRootVarPath = new VariablePath(PSScriptRoot);174 175        internal const string PSCommandPath = "PSCommandPath";176 177        internal static readonly VariablePath PSCommandPathVarPath = new VariablePath(PSCommandPath);178 179        internal const string PSSenderInfo = "PSSenderInfo";180 181        internal static readonly VariablePath PSSenderInfoVarPath = new VariablePath(PSSenderInfo);182 183        internal const string @foreach = "foreach";184 185        internal static readonly VariablePath foreachVarPath = new VariablePath("local:" + @foreach);186 187        internal const string @switch = "switch";188 189        internal static readonly VariablePath switchVarPath = new VariablePath("local:" + @switch);190 191        internal const string pwd = "PWD";192 193        internal static readonly VariablePath PWDVarPath = new VariablePath("global:" + pwd);194 195        internal const string Null = "null";196 197        internal static readonly VariablePath NullVarPath = new VariablePath("null");198 199        internal const string True = "true";200 201        internal static readonly VariablePath TrueVarPath = new VariablePath("true");202 203        internal const string False = "false";204 205        internal static readonly VariablePath FalseVarPath = new VariablePath("false");206 207        internal const string PSModuleAutoLoading = "PSModuleAutoLoadingPreference";208 209        internal static readonly VariablePath PSModuleAutoLoadingPreferenceVarPath = new VariablePath("global:" + PSModuleAutoLoading);210 211        #region Platform Variables212 213        internal const string IsLinux = "IsLinux";214 215        internal static readonly VariablePath IsLinuxPath = new VariablePath("IsLinux");216 217        internal const string IsMacOS = "IsMacOS";218 219        internal static readonly VariablePath IsMacOSPath = new VariablePath("IsMacOS");220 221        internal const string IsWindows = "IsWindows";222 223        internal static readonly VariablePath IsWindowsPath = new VariablePath("IsWindows");224 225        internal const string IsCoreCLR = "IsCoreCLR";226 227        internal static readonly VariablePath IsCoreCLRPath = new VariablePath("IsCoreCLR");228 229        #endregion230 231        #region Preference Variables232 233        internal const string DebugPreference = "DebugPreference";234 235        internal static readonly VariablePath DebugPreferenceVarPath = new VariablePath(DebugPreference);236 237        internal const string ErrorActionPreference = "ErrorActionPreference";238 239        internal static readonly VariablePath ErrorActionPreferenceVarPath = new VariablePath(ErrorActionPreference);240 241        internal const string ProgressPreference = "ProgressPreference";242 243        internal static readonly VariablePath ProgressPreferenceVarPath = new VariablePath(ProgressPreference);244 245        internal const string VerbosePreference = "VerbosePreference";246 247        internal static readonly VariablePath VerbosePreferenceVarPath = new VariablePath(VerbosePreference);248 249        internal const string WarningPreference = "WarningPreference";250 251        internal static readonly VariablePath WarningPreferenceVarPath = new VariablePath(WarningPreference);252 253        internal const string WhatIfPreference = "WhatIfPreference";254 255        internal static readonly VariablePath WhatIfPreferenceVarPath = new VariablePath(WhatIfPreference);256 257        internal const string ConfirmPreference = "ConfirmPreference";258 259        internal static readonly VariablePath ConfirmPreferenceVarPath = new VariablePath(ConfirmPreference);260 261        internal const string InformationPreference = "InformationPreference";262 263        internal static readonly VariablePath InformationPreferenceVarPath = new VariablePath(InformationPreference);264 265        #endregion Preference Variables266 267        internal const string PSNativeCommandUseErrorActionPreference = nameof(PSNativeCommandUseErrorActionPreference);268 269        internal static readonly VariablePath PSNativeCommandUseErrorActionPreferenceVarPath =270            new(PSNativeCommandUseErrorActionPreference);271 272        // Native command argument passing style273        internal const string NativeArgumentPassing = "PSNativeCommandArgumentPassing";274 275        internal static readonly VariablePath NativeArgumentPassingVarPath = new VariablePath(NativeArgumentPassing);276 277        internal const string ErrorView = "ErrorView";278 279        internal static readonly VariablePath ErrorViewVarPath = new VariablePath(ErrorView);280 281        /// <summary>282        /// Shell environment variable.283        /// </summary>284        internal const string PSSessionConfigurationName = "PSSessionConfigurationName";285 286        internal static readonly VariablePath PSSessionConfigurationNameVarPath = new VariablePath("global:" + PSSessionConfigurationName);287 288        /// <summary>289        /// Environment variable that will define the default290        /// application name for the connection uri.291        /// </summary>292        internal const string PSSessionApplicationName = "PSSessionApplicationName";293 294        internal static readonly VariablePath PSSessionApplicationNameVarPath = new VariablePath("global:" + PSSessionApplicationName);295 296        #region AllScope variables created in every session297 298        internal const string ExecutionContext = "ExecutionContext";299        internal const string Home = "HOME";300        internal const string Host = "Host";301        internal const string PID = "PID";302        internal const string PSCulture = "PSCulture";303        internal const string PSHome = "PSHOME";304        internal const string PSUICulture = "PSUICulture";305        internal const string PSVersionTable = "PSVersionTable";306        internal const string PSEdition = "PSEdition";307        internal const string ShellId = "ShellId";308        internal const string EnabledExperimentalFeatures = "EnabledExperimentalFeatures";309 310        #endregion AllScope variables created in every session311 312        internal static readonly string[] AutomaticVariables = {313                                                                   SpecialVariables.Underbar,314                                                                   SpecialVariables.Args,315                                                                   SpecialVariables.This,316                                                                   SpecialVariables.Input,317                                                                   SpecialVariables.PSCmdlet,318                                                                   SpecialVariables.PSBoundParameters,319                                                                   SpecialVariables.MyInvocation,320                                                                   SpecialVariables.PSScriptRoot,321                                                                   SpecialVariables.PSCommandPath,322                                                               };323 324        internal static readonly Type[] AutomaticVariableTypes = {325                                                                   /* Underbar */          typeof(object),326                                                                   /* Args */              typeof(object[]),327                                                                   /* This */              typeof(object),328                                                                   /* Input */             typeof(object),329                                                                   /* PSCmdlet */          typeof(PSScriptCmdlet),330                                                                   /* PSBoundParameters */ typeof(PSBoundParametersDictionary),331                                                                   /* MyInvocation */      typeof(InvocationInfo),332                                                                   /* PSScriptRoot */      typeof(string),333                                                                   /* PSCommandPath */     typeof(string),334                                                                 };335 336        // This array and the one below it exist to optimize the way common parameters work in advanced functions.337        // Common parameters work by setting preference variables in the scope of the function and restoring the old value afterward.338        // Variables that don't correspond to common cmdlet parameters don't need to be added here.339        internal static readonly string[] PreferenceVariables =340        {341            SpecialVariables.DebugPreference,342            SpecialVariables.VerbosePreference,343            SpecialVariables.ErrorActionPreference,344            SpecialVariables.WhatIfPreference,345            SpecialVariables.WarningPreference,346            SpecialVariables.InformationPreference,347            SpecialVariables.ConfirmPreference,348            SpecialVariables.ProgressPreference,349        };350 351        internal static readonly Type[] PreferenceVariableTypes =352        {353            /* DebugPreference */                         typeof(ActionPreference),354            /* VerbosePreference */                       typeof(ActionPreference),355            /* ErrorPreference */                         typeof(ActionPreference),356            /* WhatIfPreference */                        typeof(SwitchParameter),357            /* WarningPreference */                       typeof(ActionPreference),358            /* InformationPreference */                   typeof(ActionPreference),359            /* ConfirmPreference */                       typeof(ConfirmImpact),360            /* ProgressPreference */                      typeof(ActionPreference),361        };362 363        // The following variables are created in every session w/ AllScope.  We avoid creating local slots when we364        // see an assignment to any of these variables so that they get handled properly (either throwing an exception365        // because they are constant/readonly, or having the value persist in parent scopes where the allscope variable366        // also exists.367        internal static readonly Dictionary<string, Type> AllScopeVariables = new(StringComparer.OrdinalIgnoreCase) {368            { Question, typeof(bool) },369            { ExecutionContext, typeof(EngineIntrinsics) },370            { False, typeof(bool) },371            { Home, typeof(string) },372            { Host, typeof(object) },373            { PID, typeof(int) },374            { PSCulture, typeof(string) },375            { PSHome, typeof(string) },376            { PSUICulture, typeof(string) },377            { PSVersionTable, typeof(PSVersionHashTable) },378            { PSEdition, typeof(string) },379            { ShellId, typeof(string) },380            { True, typeof(bool) },381            { EnabledExperimentalFeatures, typeof(ReadOnlyBag<string>) }382        };383 384        private static readonly HashSet<string> s_classMethodsAccessibleVariables = new HashSet<string>385            (386                new string[]387                {388                    SpecialVariables.LastExitCode,389                    SpecialVariables.Error,390                    SpecialVariables.StackTrace,391                    SpecialVariables.OutputEncoding,392                    SpecialVariables.NestedPromptLevel,393                    SpecialVariables.pwd,394                    SpecialVariables.Matches,395                    SpecialVariables.PSApplicationOutputEncoding,396                },397                StringComparer.OrdinalIgnoreCase398            );399 400        internal static bool IsImplicitVariableAccessibleInClassMethod(VariablePath variablePath)401        {402            return s_classMethodsAccessibleVariables.Contains(variablePath.UserPath);403        }404    }405 406    internal enum AutomaticVariable407    {408        Underbar = 0,409        Args = 1,410        This = 2,411        Input = 3,412        PSCmdlet = 4,413        PSBoundParameters = 5,414        MyInvocation = 6,415        PSScriptRoot = 7,416        PSCommandPath = 8,417        NumberOfAutomaticVariables // 1 + the last, used to initialize global scope.418    }419 420    internal enum PreferenceVariable421    {422        Debug = 9,423        Verbose = 10,424        Error = 11,425        WhatIf = 12,426        Warning = 13,427        Information = 14,428        Confirm = 15,429        Progress = 16,430    }431}432