PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/System.Management.Automation/engine/SessionStateCmdletAPIs.cs

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