/********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ using System.Collections.Generic; using System.Management.Automation.Runspaces; using Dbg = System.Management.Automation; namespace System.Management.Automation { /// /// Holds the state of a Monad Shell session /// internal sealed partial class SessionStateInternal { #region aliases /// /// Add a new alias entry to this session state object... /// /// The entry to add /// /// 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. /// internal void AddSessionStateEntry(SessionStateAliasEntry entry, string scopeID) { AliasInfo alias = new AliasInfo(entry.Name, entry.Definition, this.ExecutionContext, entry.Options) { Visibility = entry.Visibility, Module = entry.Module, Description = entry.Description }; // Create alias in the global scope... this.SetAliasItemAtScope(alias, scopeID, true, CommandOrigin.Internal); } /// /// Gets an IEnumerable for the alias table /// /// internal IDictionary GetAliasTable() { Dictionary result = new Dictionary(StringComparer.OrdinalIgnoreCase); SessionStateScopeEnumerator scopeEnumerator = new SessionStateScopeEnumerator(_currentScope); foreach (SessionStateScope scope in scopeEnumerator) { foreach (AliasInfo entry in scope.AliasTable) { if (!result.ContainsKey(entry.Name)) { // 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 ((entry.Options & ScopedItemOptions.Private) == 0 || scope == _currentScope) { result.Add(entry.Name, entry); } } } } return result; } // GetAliasTable /// /// Gets an IEnumerable for the alias table for a given scope /// /// /// /// 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. /// /// /// /// If is less than zero, or not /// a number and not "script", "global", "local", or "private" /// /// /// /// If is less than zero or greater than the number of currently /// active scopes. /// /// internal IDictionary GetAliasTableAtScope(string scopeID) { Dictionary result = new Dictionary(StringComparer.OrdinalIgnoreCase); SessionStateScope scope = GetScopeByID(scopeID); foreach (AliasInfo entry in scope.AliasTable) { // 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 ((entry.Options & ScopedItemOptions.Private) == 0 || scope == _currentScope) { result.Add(entry.Name, entry); } } return result; } // GetAliasTableAtScope /// /// List of aliases to export from this session state object... /// internal List ExportedAliases { get; } = new List(); /// /// Gets the value of the specified alias from the alias table. /// /// /// /// The name of the alias value to retrieve. /// /// /// /// The origin of the command calling this API. /// /// /// The AliasInfo representing the alias. /// /// internal AliasInfo GetAlias(string aliasName, CommandOrigin origin) { AliasInfo result = null; if (String.IsNullOrEmpty(aliasName)) { return null; } // Use the scope enumerator to find the alias using the // appropriate scoping rules SessionStateScopeEnumerator scopeEnumerator = new SessionStateScopeEnumerator(_currentScope); foreach (SessionStateScope scope in scopeEnumerator) { result = scope.GetAlias(aliasName); if (result != null) { // Now check the visibility of the variable... SessionState.ThrowIfNotVisible(origin, result); // 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.Options & ScopedItemOptions.Private) != 0 && scope != _currentScope) { result = null; } else { break; } } } return result; } // GetAlias /// /// Gets the value of the specified alias from the alias table. /// /// /// /// The name of the alias value to retrieve. /// /// /// /// The AliasInfo representing the alias. /// /// internal AliasInfo GetAlias(string aliasName) { return GetAlias(aliasName, CommandOrigin.Internal); } /// /// Gets the value of the specified alias from the alias table. /// /// /// /// The name of the alias value to retrieve. /// /// /// /// 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. /// /// /// /// The AliasInfo representing the alias. /// /// /// /// If is less than zero, or not /// a number and not "script", "global", "local", or "private" /// /// /// /// If is less than zero or greater than the number of currently /// active scopes. /// /// internal AliasInfo GetAliasAtScope(string aliasName, string scopeID) { AliasInfo result = null; if (String.IsNullOrEmpty(aliasName)) { return null; } SessionStateScope scope = GetScopeByID(scopeID); result = scope.GetAlias(aliasName); // 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 && scope != _currentScope) { result = null; } return result; } // GetAliasAtScope /// /// Sets the alias with specified name to the specified value in the current scope. /// /// /// /// The name of the alias to set. /// /// /// /// The value to set the alias to. /// /// /// /// If true, the value will be set even if the alias is ReadOnly. /// /// /// /// THe origin of the caller of this API /// /// /// /// The resulting AliasInfo for the alias that was set. /// /// /// /// If or is null or empty. /// /// /// /// If the alias is read-only or constant. /// /// internal AliasInfo SetAliasValue(string aliasName, string value, bool force, CommandOrigin origin) { if (String.IsNullOrEmpty(aliasName)) { throw PSTraceSource.NewArgumentException("aliasName"); } if (String.IsNullOrEmpty(value)) { throw PSTraceSource.NewArgumentException("value"); } AliasInfo info = _currentScope.SetAliasValue(aliasName, value, this.ExecutionContext, force, origin); return info; } // SetAliasValue /// /// Sets the alias with specified name to the specified value in the current scope. /// BUGBUG: this overload only exists for the test suites. They should be cleaned up /// and this overload removed. /// /// /// /// The name of the alias to set. /// /// /// /// The value to set the alias to. /// /// /// /// If true, the value will be set even if the alias is ReadOnly. /// /// /// /// The resulting AliasInfo for the alias that was set. /// /// /// /// If or is null or empty. /// /// /// /// If the alias is read-only or constant. /// /// internal AliasInfo SetAliasValue(string aliasName, string value, bool force) { return SetAliasValue(aliasName, value, force, CommandOrigin.Internal); } /// /// Sets the alias with specified name to the specified value in the current scope. /// /// /// /// The name of the alias to set. /// /// /// /// The value to set the alias to. /// /// /// /// The options to set on the alias. /// /// /// /// If true, the value will be set even if the alias is ReadOnly. /// /// /// /// The origin of the caller of this API /// /// /// /// The resulting AliasInfo for the alias that was set. /// /// /// /// If or is null or empty. /// /// /// /// If the alias is read-only or constant. /// /// internal AliasInfo SetAliasValue( string aliasName, string value, ScopedItemOptions options, bool force, CommandOrigin origin) { if (String.IsNullOrEmpty(aliasName)) { throw PSTraceSource.NewArgumentException("aliasName"); } if (String.IsNullOrEmpty(value)) { throw PSTraceSource.NewArgumentException("value"); } AliasInfo info = _currentScope.SetAliasValue(aliasName, value, options, this.ExecutionContext, force, origin); return info; } // SetAliasValue /// /// Sets the alias with specified name to the specified value in the current scope. /// BUGBUG: this api only exists for the test suites. They should be fixed and it should be removed. /// /// /// /// The name of the alias to set. /// /// /// /// The value to set the alias to. /// /// /// /// The options to set on the alias. /// /// /// /// If true, the value will be set even if the alias is ReadOnly. /// /// /// /// The resulting AliasInfo for the alias that was set. /// /// /// /// If or is null or empty. /// /// /// /// If the alias is read-only or constant. /// /// internal AliasInfo SetAliasValue( string aliasName, string value, ScopedItemOptions options, bool force) { return SetAliasValue(aliasName, value, options, force, CommandOrigin.Internal); } /// /// Sets the alias with specified name to the specified value in the current scope. /// /// /// /// The AliasInfo representing the alias. /// /// /// /// If true, the alias will be set even if there is an existing ReadOnly /// alias. /// /// /// /// Specifies the origin of the command setting the alias. /// /// /// /// The resulting AliasInfo for the alias that was set. /// /// /// /// If is null. /// /// /// /// If the alias is read-only or constant. /// /// internal AliasInfo SetAliasItem(AliasInfo alias, bool force, CommandOrigin origin) { if (alias == null) { throw PSTraceSource.NewArgumentNullException("alias"); } AliasInfo info = _currentScope.SetAliasItem(alias, force, origin); return info; } // SetAliasItem /// /// Sets the alias with specified name to the specified value in the current scope. /// /// /// /// The AliasInfo representing the alias. /// /// /// /// 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. /// /// /// /// If true, the alias will be set even if there is an existing ReadOnly /// alias. /// /// /// /// Specifies the command origin of the calling command. /// /// /// /// The resulting AliasInfo for the alias that was set. /// /// /// /// If is less than zero, or not /// a number and not "script", "global", "local", or "private" /// /// /// /// If is less than zero or greater than the number of currently /// active scopes. /// /// /// /// If is null. /// /// /// /// If the alias is read-only or constant. /// /// internal AliasInfo SetAliasItemAtScope(AliasInfo alias, string scopeID, bool force, CommandOrigin origin) { if (alias == null) { throw PSTraceSource.NewArgumentNullException("alias"); } // If the "private" scope was specified, make sure the options contain // the Private flag if (String.Equals(scopeID, StringLiterals.Private, StringComparison.OrdinalIgnoreCase)) { alias.Options |= ScopedItemOptions.Private; } SessionStateScope scope = GetScopeByID(scopeID); AliasInfo info = scope.SetAliasItem(alias, force, origin); return info; } // SetAliasItemAtScope /// /// Sets the alias with specified name to the specified value in the current scope. /// /// /// /// The AliasInfo representing the alias. /// /// /// /// 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. /// /// /// /// If true, the alias will be set even if there is an existing ReadOnly /// alias. /// /// /// /// The resulting AliasInfo for the alias that was set. /// /// /// /// If is less than zero, or not /// a number and not "script", "global", "local", or "private" /// /// /// /// If is less than zero or greater than the number of currently /// active scopes. /// /// /// /// If is null. /// /// /// /// If the alias is read-only or constant. /// /// internal AliasInfo SetAliasItemAtScope(AliasInfo alias, string scopeID, bool force) { return SetAliasItemAtScope(alias, scopeID, force, CommandOrigin.Internal); } /// /// Removes the specified alias. /// /// /// /// The name of the alias to remove. /// /// /// /// If true the alias will be removed even if its ReadOnly. /// /// /// /// If is null or empty. /// /// /// /// If the alias is constant. /// /// internal void RemoveAlias(string aliasName, bool force) { if (String.IsNullOrEmpty(aliasName)) { throw PSTraceSource.NewArgumentException("aliasName"); } // Use the scope enumerator to find an existing function SessionStateScopeEnumerator scopeEnumerator = new SessionStateScopeEnumerator(_currentScope); foreach (SessionStateScope scope in scopeEnumerator) { AliasInfo alias = scope.GetAlias(aliasName); if (alias != null) { // 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 ((alias.Options & ScopedItemOptions.Private) != 0 && scope != _currentScope) { alias = null; } else { scope.RemoveAlias(aliasName, force); break; } } } } // RemoveAlias /// /// Gets the aliases by command name (used by metadata-driven help) /// /// /// internal IEnumerable GetAliasesByCommandName(string command) { SessionStateScopeEnumerator scopeEnumerator = new SessionStateScopeEnumerator(_currentScope); foreach (SessionStateScope scope in scopeEnumerator) { foreach (string alias in scope.GetAliasesByCommandName(command)) { yield return alias; } } yield break; } #endregion aliases } // SessionStateInternal class }