MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections.Generic;5 6using Dbg = System.Management.Automation;7 8namespace System.Management.Automation9{10 /// <summary>11 /// Holds the state of a Monad Shell session.12 /// </summary>13 internal sealed partial class SessionStateInternal14 {15 #region cmdlets16 17 /// <summary>18 /// Gets the value of the specified cmdlet from the cmdlet table.19 /// </summary>20 /// <param name="cmdletName">21 /// The name of the cmdlet value to retrieve.22 /// </param>23 /// <returns>24 /// The CmdletInfo representing the cmdlet.25 /// </returns>26 internal CmdletInfo GetCmdlet(string cmdletName)27 {28 return GetCmdlet(cmdletName, CommandOrigin.Internal);29 }30 31 /// <summary>32 /// Gets the value of the specified cmdlet from the cmdlet table.33 /// </summary>34 /// <param name="cmdletName">35 /// The name of the cmdlet value to retrieve.36 /// </param>37 /// <param name="origin">38 /// The origin of the command trying to retrieve this cmdlet.39 /// </param>40 /// <returns>41 /// The CmdletInfo representing the cmdlet.42 /// </returns>43 internal CmdletInfo GetCmdlet(string cmdletName, CommandOrigin origin)44 {45 CmdletInfo result = null;46 if (string.IsNullOrEmpty(cmdletName))47 {48 return null;49 }50 51 // Use the scope enumerator to find the alias using the52 // appropriate scoping rules53 54 SessionStateScopeEnumerator scopeEnumerator =55 new SessionStateScopeEnumerator(_currentScope);56 57 foreach (SessionStateScope scope in scopeEnumerator)58 {59 result = scope.GetCmdlet(cmdletName);60 61 if (result != null)62 {63 // Now check the visibility of the cmdlet...64 SessionState.ThrowIfNotVisible(origin, result);65 66 // Make sure the cmdlet isn't private or if it is that the current67 // scope is the same scope the cmdlet was retrieved from.68 69 if ((result.Options & ScopedItemOptions.Private) != 0 &&70 scope != _currentScope)71 {72 result = null;73 }74 else75 {76 break;77 }78 }79 }80 81 return result;82 }83 84 /// <summary>85 /// Gets the value of the specified cmdlet from the cmdlet table.86 /// </summary>87 /// <param name="cmdletName">88 /// The name of the cmdlet value to retrieve.89 /// </param>90 /// <param name="scopeID">91 /// A scope identifier that is either one of the "special" scopes like92 /// "global", "script", "local", or "private, or a numeric ID of a relative scope93 /// to the current scope.94 /// </param>95 /// <returns>96 /// The CmdletInfo representing the cmdlet.97 /// </returns>98 /// <exception cref="ArgumentException">99 /// If <paramref name="scopeID"/> is less than zero, or not100 /// a number and not "script", "global", "local", or "private"101 /// </exception>102 /// <exception cref="ArgumentOutOfRangeException">103 /// If <paramref name="scopeID"/> is less than zero or greater than the number of currently104 /// active scopes.105 /// </exception>106 internal CmdletInfo GetCmdletAtScope(string cmdletName, string scopeID)107 {108 CmdletInfo result = null;109 if (string.IsNullOrEmpty(cmdletName))110 {111 return null;112 }113 114 SessionStateScope scope = GetScopeByID(scopeID);115 result = scope.GetCmdlet(cmdletName);116 117 // Make sure the alias isn't private or if it is that the current118 // scope is the same scope the alias was retrieved from.119 120 if (result != null &&121 (result.Options & ScopedItemOptions.Private) != 0 &&122 scope != _currentScope)123 {124 result = null;125 }126 127 return result;128 }129 130 /// <summary>131 /// Gets an IEnumerable for the cmdlet table.132 /// </summary>133 internal IDictionary<string, List<CmdletInfo>> GetCmdletTable()134 {135 Dictionary<string, List<CmdletInfo>> result =136 new Dictionary<string, List<CmdletInfo>>(StringComparer.OrdinalIgnoreCase);137 138 SessionStateScopeEnumerator scopeEnumerator =139 new SessionStateScopeEnumerator(_currentScope);140 141 foreach (SessionStateScope scope in scopeEnumerator)142 {143 foreach (KeyValuePair<string, List<CmdletInfo>> entry in scope.CmdletTable)144 {145 if (!result.ContainsKey(entry.Key))146 {147 // Make sure the cmdlet isn't private or if it is that the current148 // scope is the same scope the alias was retrieved from.149 150 List<CmdletInfo> toBeAdded = new List<CmdletInfo>();151 foreach (CmdletInfo cmdletInfo in entry.Value)152 {153 if ((cmdletInfo.Options & ScopedItemOptions.Private) == 0 ||154 scope == _currentScope)155 {156 toBeAdded.Add(cmdletInfo);157 }158 }159 160 result.Add(entry.Key, toBeAdded);161 }162 }163 }164 165 return result;166 }167 168 /// <summary>169 /// Gets an IEnumerable for the cmdlet table for a given scope.170 /// </summary>171 /// <param name="scopeID">172 /// A scope identifier that is either one of the "special" scopes like173 /// "global", "script", "local", or "private, or a numeric ID of a relative scope174 /// to the current scope.175 /// </param>176 /// <exception cref="ArgumentException">177 /// If <paramref name="scopeID"/> is less than zero, or not178 /// a number and not "script", "global", "local", or "private"179 /// </exception>180 /// <exception cref="ArgumentOutOfRangeException">181 /// If <paramref name="scopeID"/> is less than zero or greater than the number of currently182 /// active scopes.183 /// </exception>184 internal IDictionary<string, List<CmdletInfo>> GetCmdletTableAtScope(string scopeID)185 {186 Dictionary<string, List<CmdletInfo>> result =187 new Dictionary<string, List<CmdletInfo>>(StringComparer.OrdinalIgnoreCase);188 189 SessionStateScope scope = GetScopeByID(scopeID);190 191 foreach (KeyValuePair<string, List<CmdletInfo>> entry in scope.CmdletTable)192 {193 // Make sure the alias isn't private or if it is that the current194 // scope is the same scope the alias was retrieved from.195 List<CmdletInfo> toBeAdded = new List<CmdletInfo>();196 foreach (CmdletInfo cmdletInfo in entry.Value)197 {198 if ((cmdletInfo.Options & ScopedItemOptions.Private) == 0 ||199 scope == _currentScope)200 {201 toBeAdded.Add(cmdletInfo);202 }203 }204 205 result.Add(entry.Key, toBeAdded);206 }207 208 return result;209 }210 211 internal void RemoveCmdlet(string name, int index, bool force)212 {213 RemoveCmdlet(name, index, force, CommandOrigin.Internal);214 }215 216 /// <summary>217 /// Removes a cmdlet from the function table.218 /// </summary>219 /// <param name="name">220 /// The name of the cmdlet to remove.221 /// </param>222 /// <param name="index">223 /// The name of the cmdlet to remove.224 /// </param>225 /// <param name="origin">226 /// THe origin of the caller of this API227 /// </param>228 /// <param name="force">229 /// If true, the cmdlet is removed even if it is ReadOnly.230 /// </param>231 /// <exception cref="ArgumentException">232 /// If <paramref name="name"/> is null or empty.233 /// </exception>234 /// <exception cref="SessionStateUnauthorizedAccessException">235 /// If the function is constant.236 /// </exception>237 internal void RemoveCmdlet(string name, int index, bool force, CommandOrigin origin)238 {239 if (string.IsNullOrEmpty(name))240 {241 throw PSTraceSource.NewArgumentException(nameof(name));242 }243 244 // Use the scope enumerator to find an existing function245 246 SessionStateScopeEnumerator scopeEnumerator =247 new SessionStateScopeEnumerator(_currentScope);248 249 foreach (SessionStateScope scope in scopeEnumerator)250 {251 CmdletInfo cmdletInfo =252 scope.GetCmdlet(name);253 254 if (cmdletInfo != null)255 {256 // Make sure the cmdlet isn't private or if it is that the current257 // scope is the same scope the cmdlet was retrieved from.258 259 if ((cmdletInfo.Options & ScopedItemOptions.Private) != 0 &&260 scope != _currentScope)261 {262 cmdletInfo = null;263 }264 else265 {266 scope.RemoveCmdlet(name, index, force);267 break;268 }269 }270 }271 }272 273 /// <summary>274 /// Removes a cmdlet entry from the cmdlet table.275 /// </summary>276 /// <param name="name">277 /// The name of the cmdlet entry to remove.278 /// </param>279 /// <param name="force">280 /// If true, the cmdlet is removed even if it is ReadOnly.281 /// </param>282 /// <exception cref="ArgumentException">283 /// If <paramref name="name"/> is null or empty.284 /// </exception>285 /// <exception cref="SessionStateUnauthorizedAccessException">286 /// If the function is constant.287 /// </exception>288 internal void RemoveCmdletEntry(string name, bool force)289 {290 if (string.IsNullOrEmpty(name))291 {292 throw PSTraceSource.NewArgumentException(nameof(name));293 }294 295 // Use the scope enumerator to find an existing function296 297 SessionStateScopeEnumerator scopeEnumerator =298 new SessionStateScopeEnumerator(_currentScope);299 300 foreach (SessionStateScope scope in scopeEnumerator)301 {302 CmdletInfo cmdletInfo =303 scope.GetCmdlet(name);304 305 if (cmdletInfo != null)306 {307 // Make sure the cmdlet isn't private or if it is that the current308 // scope is the same scope the cmdlet was retrieved from.309 310 if ((cmdletInfo.Options & ScopedItemOptions.Private) != 0 &&311 scope != _currentScope)312 {313 cmdletInfo = null;314 }315 else316 {317 scope.RemoveCmdletEntry(name, force);318 break;319 }320 }321 }322 }323 324 #endregion cmdlets325 }326}327 