2020-03-24 11:08:37 -07:00
|
|
|
// Copyright (c) Microsoft Corporation.
|
2018-02-13 09:23:53 -08:00
|
|
|
// Licensed under the MIT License.
|
2016-07-29 13:02:49 -07:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
using System.Collections.Generic;
|
2019-04-30 10:25:11 +05:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
using Dbg = System.Management.Automation;
|
|
|
|
|
|
|
|
|
|
namespace System.Management.Automation
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-01-06 04:40:24 +05:00
|
|
|
/// Holds the state of a Monad Shell session.
|
2016-03-30 14:20:52 -07:00
|
|
|
/// </summary>
|
|
|
|
|
internal sealed partial class SessionStateInternal
|
|
|
|
|
{
|
|
|
|
|
#region cmdlets
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the value of the specified cmdlet from the cmdlet table.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cmdletName">
|
|
|
|
|
/// The name of the cmdlet value to retrieve.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// The CmdletInfo representing the cmdlet.
|
|
|
|
|
/// </returns>
|
|
|
|
|
internal CmdletInfo GetCmdlet(string cmdletName)
|
|
|
|
|
{
|
|
|
|
|
return GetCmdlet(cmdletName, CommandOrigin.Internal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the value of the specified cmdlet from the cmdlet table.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cmdletName">
|
|
|
|
|
/// The name of the cmdlet value to retrieve.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="origin">
|
|
|
|
|
/// The origin of hte command trying to retrieve this cmdlet.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// The CmdletInfo representing the cmdlet.
|
|
|
|
|
/// </returns>
|
|
|
|
|
internal CmdletInfo GetCmdlet(string cmdletName, CommandOrigin origin)
|
|
|
|
|
{
|
|
|
|
|
CmdletInfo result = null;
|
2018-12-31 15:10:15 +05:00
|
|
|
if (string.IsNullOrEmpty(cmdletName))
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
2016-08-02 23:09:38 -07:00
|
|
|
return null;
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use the scope enumerator to find the alias using the
|
|
|
|
|
// appropriate scoping rules
|
|
|
|
|
|
|
|
|
|
SessionStateScopeEnumerator scopeEnumerator =
|
2016-07-29 13:02:49 -07:00
|
|
|
new SessionStateScopeEnumerator(_currentScope);
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
foreach (SessionStateScope scope in scopeEnumerator)
|
|
|
|
|
{
|
|
|
|
|
result = scope.GetCmdlet(cmdletName);
|
|
|
|
|
|
|
|
|
|
if (result != null)
|
|
|
|
|
{
|
|
|
|
|
// Now check the visibility of the cmdlet...
|
|
|
|
|
SessionState.ThrowIfNotVisible(origin, result);
|
|
|
|
|
|
|
|
|
|
// Make sure the cmdlet isn't private or if it is that the current
|
|
|
|
|
// scope is the same scope the cmdlet was retrieved from.
|
|
|
|
|
|
|
|
|
|
if ((result.Options & ScopedItemOptions.Private) != 0 &&
|
2016-07-29 13:02:49 -07:00
|
|
|
scope != _currentScope)
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
result = null;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2018-12-08 18:53:30 +02:00
|
|
|
}
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the value of the specified cmdlet from the cmdlet table.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cmdletName">
|
|
|
|
|
/// The name of the cmdlet value to retrieve.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="scopeID">
|
|
|
|
|
/// A scope identifier that is either one of the "special" scopes like
|
|
|
|
|
/// "global", "script", "local", or "private, or a numeric ID of a relative scope
|
|
|
|
|
/// to the current scope.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// The CmdletInfo representing the cmdlet.
|
|
|
|
|
/// </returns>
|
|
|
|
|
/// <exception cref="ArgumentException">
|
|
|
|
|
/// If <paramref name="scopeID"/> is less than zero, or not
|
|
|
|
|
/// a number and not "script", "global", "local", or "private"
|
|
|
|
|
/// </exception>
|
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">
|
|
|
|
|
/// If <paramref name="scopeID"/> is less than zero or greater than the number of currently
|
|
|
|
|
/// active scopes.
|
|
|
|
|
/// </exception>
|
|
|
|
|
internal CmdletInfo GetCmdletAtScope(string cmdletName, string scopeID)
|
|
|
|
|
{
|
|
|
|
|
CmdletInfo result = null;
|
2018-12-31 15:10:15 +05:00
|
|
|
if (string.IsNullOrEmpty(cmdletName))
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
2016-08-02 23:09:38 -07:00
|
|
|
return null;
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SessionStateScope scope = GetScopeByID(scopeID);
|
|
|
|
|
result = scope.GetCmdlet(cmdletName);
|
|
|
|
|
|
|
|
|
|
// Make sure the alias isn't private or if it is that the current
|
|
|
|
|
// scope is the same scope the alias was retrieved from.
|
|
|
|
|
|
|
|
|
|
if (result != null &&
|
|
|
|
|
(result.Options & ScopedItemOptions.Private) != 0 &&
|
2016-07-29 13:02:49 -07:00
|
|
|
scope != _currentScope)
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
result = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2018-12-08 18:53:30 +02:00
|
|
|
}
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
2019-01-06 04:40:24 +05:00
|
|
|
/// Gets an IEnumerable for the cmdlet table.
|
2016-03-30 14:20:52 -07:00
|
|
|
/// </summary>
|
|
|
|
|
internal IDictionary<string, List<CmdletInfo>> GetCmdletTable()
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, List<CmdletInfo>> result =
|
|
|
|
|
new Dictionary<string, List<CmdletInfo>>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
SessionStateScopeEnumerator scopeEnumerator =
|
2016-07-29 13:02:49 -07:00
|
|
|
new SessionStateScopeEnumerator(_currentScope);
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
foreach (SessionStateScope scope in scopeEnumerator)
|
|
|
|
|
{
|
|
|
|
|
foreach (KeyValuePair<string, List<CmdletInfo>> entry in scope.CmdletTable)
|
|
|
|
|
{
|
|
|
|
|
if (!result.ContainsKey(entry.Key))
|
|
|
|
|
{
|
|
|
|
|
// Make sure the cmdlet isn't private or if it is that the current
|
|
|
|
|
// scope is the same scope the alias was retrieved from.
|
|
|
|
|
|
|
|
|
|
List<CmdletInfo> toBeAdded = new List<CmdletInfo>();
|
|
|
|
|
foreach (CmdletInfo cmdletInfo in entry.Value)
|
|
|
|
|
{
|
|
|
|
|
if ((cmdletInfo.Options & ScopedItemOptions.Private) == 0 ||
|
2016-07-29 13:02:49 -07:00
|
|
|
scope == _currentScope)
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
toBeAdded.Add(cmdletInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-23 22:20:06 -08:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
result.Add(entry.Key, toBeAdded);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2018-12-08 18:53:30 +02:00
|
|
|
}
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
2019-01-06 04:40:24 +05:00
|
|
|
/// Gets an IEnumerable for the cmdlet table for a given scope.
|
2016-03-30 14:20:52 -07:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="scopeID">
|
|
|
|
|
/// A scope identifier that is either one of the "special" scopes like
|
|
|
|
|
/// "global", "script", "local", or "private, or a numeric ID of a relative scope
|
|
|
|
|
/// to the current scope.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <exception cref="ArgumentException">
|
|
|
|
|
/// If <paramref name="scopeID"/> is less than zero, or not
|
|
|
|
|
/// a number and not "script", "global", "local", or "private"
|
|
|
|
|
/// </exception>
|
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">
|
|
|
|
|
/// If <paramref name="scopeID"/> is less than zero or greater than the number of currently
|
|
|
|
|
/// active scopes.
|
|
|
|
|
/// </exception>
|
|
|
|
|
internal IDictionary<string, List<CmdletInfo>> GetCmdletTableAtScope(string scopeID)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, List<CmdletInfo>> result =
|
|
|
|
|
new Dictionary<string, List<CmdletInfo>>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
SessionStateScope scope = GetScopeByID(scopeID);
|
|
|
|
|
|
|
|
|
|
foreach (KeyValuePair<string, List<CmdletInfo>> entry in scope.CmdletTable)
|
|
|
|
|
{
|
|
|
|
|
// Make sure the alias isn't private or if it is that the current
|
|
|
|
|
// scope is the same scope the alias was retrieved from.
|
|
|
|
|
List<CmdletInfo> toBeAdded = new List<CmdletInfo>();
|
|
|
|
|
foreach (CmdletInfo cmdletInfo in entry.Value)
|
|
|
|
|
{
|
|
|
|
|
if ((cmdletInfo.Options & ScopedItemOptions.Private) == 0 ||
|
2016-07-29 13:02:49 -07:00
|
|
|
scope == _currentScope)
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
toBeAdded.Add(cmdletInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-23 22:20:06 -08:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
result.Add(entry.Key, toBeAdded);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2018-12-08 18:53:30 +02:00
|
|
|
}
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
internal void RemoveCmdlet(string name, int index, bool force)
|
|
|
|
|
{
|
|
|
|
|
RemoveCmdlet(name, index, force, CommandOrigin.Internal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes a cmdlet from the function table.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">
|
|
|
|
|
/// The name of the cmdlet to remove.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="index">
|
|
|
|
|
/// The name of the cmdlet to remove.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="origin">
|
|
|
|
|
/// THe origin of the caller of this API
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="force">
|
|
|
|
|
/// If true, the cmdlet is removed even if it is ReadOnly.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <exception cref="ArgumentException">
|
|
|
|
|
/// If <paramref name="name"/> is null or empty.
|
|
|
|
|
/// </exception>
|
|
|
|
|
/// <exception cref="SessionStateUnauthorizedAccessException">
|
|
|
|
|
/// If the function is constant.
|
2017-01-16 13:31:14 -08:00
|
|
|
/// </exception>
|
2016-03-30 14:20:52 -07:00
|
|
|
internal void RemoveCmdlet(string name, int index, bool force, CommandOrigin origin)
|
|
|
|
|
{
|
2018-12-31 15:10:15 +05:00
|
|
|
if (string.IsNullOrEmpty(name))
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
2020-05-19 19:57:52 +01:00
|
|
|
throw PSTraceSource.NewArgumentException(nameof(name));
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use the scope enumerator to find an existing function
|
|
|
|
|
|
|
|
|
|
SessionStateScopeEnumerator scopeEnumerator =
|
2016-07-29 13:02:49 -07:00
|
|
|
new SessionStateScopeEnumerator(_currentScope);
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
foreach (SessionStateScope scope in scopeEnumerator)
|
|
|
|
|
{
|
|
|
|
|
CmdletInfo cmdletInfo =
|
|
|
|
|
scope.GetCmdlet(name);
|
|
|
|
|
|
|
|
|
|
if (cmdletInfo != null)
|
|
|
|
|
{
|
|
|
|
|
// Make sure the cmdlet isn't private or if it is that the current
|
|
|
|
|
// scope is the same scope the cmdlet was retrieved from.
|
|
|
|
|
|
|
|
|
|
if ((cmdletInfo.Options & ScopedItemOptions.Private) != 0 &&
|
2016-07-29 13:02:49 -07:00
|
|
|
scope != _currentScope)
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
cmdletInfo = null;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
scope.RemoveCmdlet(name, index, force);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-08 18:53:30 +02:00
|
|
|
}
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes a cmdlet entry from the cmdlet table.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">
|
|
|
|
|
/// The name of the cmdlet entry to remove.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="force">
|
|
|
|
|
/// If true, the cmdlet is removed even if it is ReadOnly.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <exception cref="ArgumentException">
|
|
|
|
|
/// If <paramref name="name"/> is null or empty.
|
|
|
|
|
/// </exception>
|
|
|
|
|
/// <exception cref="SessionStateUnauthorizedAccessException">
|
|
|
|
|
/// If the function is constant.
|
2017-01-16 13:31:14 -08:00
|
|
|
/// </exception>
|
2016-03-30 14:20:52 -07:00
|
|
|
internal void RemoveCmdletEntry(string name, bool force)
|
|
|
|
|
{
|
2018-12-31 15:10:15 +05:00
|
|
|
if (string.IsNullOrEmpty(name))
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
2020-05-19 19:57:52 +01:00
|
|
|
throw PSTraceSource.NewArgumentException(nameof(name));
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use the scope enumerator to find an existing function
|
|
|
|
|
|
|
|
|
|
SessionStateScopeEnumerator scopeEnumerator =
|
2016-07-29 13:02:49 -07:00
|
|
|
new SessionStateScopeEnumerator(_currentScope);
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
foreach (SessionStateScope scope in scopeEnumerator)
|
|
|
|
|
{
|
|
|
|
|
CmdletInfo cmdletInfo =
|
|
|
|
|
scope.GetCmdlet(name);
|
|
|
|
|
|
|
|
|
|
if (cmdletInfo != null)
|
|
|
|
|
{
|
|
|
|
|
// Make sure the cmdlet isn't private or if it is that the current
|
|
|
|
|
// scope is the same scope the cmdlet was retrieved from.
|
|
|
|
|
|
|
|
|
|
if ((cmdletInfo.Options & ScopedItemOptions.Private) != 0 &&
|
2016-07-29 13:02:49 -07:00
|
|
|
scope != _currentScope)
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
cmdletInfo = null;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
scope.RemoveCmdletEntry(name, force);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-08 18:53:30 +02:00
|
|
|
}
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
#endregion cmdlets
|
2018-12-08 18:53:30 +02:00
|
|
|
}
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|