File size: 13,074 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace System.Management.Automation
{
/// <summary>
/// Provides information about a mapping between a command name and a real command.
/// </summary>
public class AliasInfo : CommandInfo
{
#region ctor
/// <summary>
/// Creates an instance of the AliasInfo class with the specified name and referenced command.
/// </summary>
/// <param name="name">
/// The name of the command.
/// </param>
/// <param name="definition">
/// The token that the alias refers to.
/// </param>
/// <param name="context">
/// The execution context for this engine, used to lookup the current session state.
/// </param>
/// <exception cref="ArgumentException">
/// If <paramref name="definition"/> is null or empty.
/// </exception>
/// <exception cref="ArgumentNullException">
/// If <paramref name="context"/> is null.
/// </exception>
internal AliasInfo(string name, string definition, ExecutionContext context) : base(name, CommandTypes.Alias)
{
_definition = definition;
this.Context = context;
if (context != null)
{
this.Module = context.SessionState.Internal.Module;
}
}
/// <summary>
/// Creates an instance of the AliasInfo class with the specified name and referenced command.
/// </summary>
/// <param name="name">
/// The name of the command.
/// </param>
/// <param name="definition">
/// The token that the alias refers to.
/// </param>
/// <param name="context">
/// The execution context for this engine instance, used to look up session state.
/// </param>
/// <param name="options">
/// The options to set on the alias. Note, Constant can only be set at creation time.
/// </param>
/// <exception cref="ArgumentException">
/// If <paramref name="definition"/> is null or empty.
/// </exception>
/// <exception cref="ArgumentNullException">
/// If <paramref name="context"/> is null.
/// </exception>
internal AliasInfo(
string name,
string definition,
ExecutionContext context,
ScopedItemOptions options) : base(name, CommandTypes.Alias)
{
_definition = definition;
this.Context = context;
_options = options;
if (context != null)
{
this.Module = context.SessionState.Internal.Module;
}
}
#endregion ctor
internal override HelpCategory HelpCategory
{
get { return HelpCategory.Alias; }
}
/// <summary>
/// Gets the command information for the command that is immediately referenced by this alias.
/// </summary>
public CommandInfo ReferencedCommand
{
get
{
// Need to lookup the referenced command every time
// to ensure we get the latest session state information
CommandInfo referencedCommand = null;
if ((_definition != null) && (Context != null))
{
CommandSearcher commandSearcher =
new CommandSearcher(
_definition,
SearchResolutionOptions.None,
CommandTypes.All,
Context);
if (commandSearcher.MoveNext())
{
System.Collections.Generic.IEnumerator<CommandInfo> ie = commandSearcher;
referencedCommand = ie.Current;
// referencedCommand = commandSearcher.Current;
}
}
return referencedCommand;
}
}
/// <summary>
/// Gets the command information for the command that
/// the alias eventually resolves to.
/// </summary>
/// <remarks>
/// An alias may reference another alias. This property follows the reference
/// chain of aliases to its end.
/// </remarks>
/// <!--
/// If the command didn't resolve to anything but aliases, the UnresolvedCommandName
/// property contains the last name the resolution succeeded in finding.
/// -->
public CommandInfo ResolvedCommand
{
get
{
// Need to lookup the resolved command every time to ensure
// we use the latest session state information
CommandInfo result = null;
if (_definition != null)
{
List<string> cyclePrevention = new List<string>();
cyclePrevention.Add(Name);
string commandNameToResolve = _definition;
result = ReferencedCommand;
while (result != null && result.CommandType == CommandTypes.Alias)
{
result = ((AliasInfo)result).ReferencedCommand;
if (result is AliasInfo)
{
// Check for the cycle by checking for the alias name
// in the cyclePrevention dictionary
if (SessionStateUtilities.CollectionContainsValue(cyclePrevention, result.Name, StringComparer.OrdinalIgnoreCase))
{
result = null;
break;
}
cyclePrevention.Add(result.Name);
commandNameToResolve = result.Definition;
}
}
if (result == null)
{
// Since we couldn't resolve the command that the alias
// points to, remember the definition so that we can
// provide better error reporting.
UnresolvedCommandName = commandNameToResolve;
}
}
return result;
}
}
/// <summary>
/// Gets the name of the command to which the alias refers.
/// </summary>
public override string Definition
{
get
{
return _definition;
}
}
private string _definition = string.Empty;
/// <summary>
/// Sets the new definition for the alias.
/// </summary>
/// <param name="definition">
/// The new definition for the alias.
/// </param>
/// <param name="force">
/// If true, the value will be set even if the alias is ReadOnly.
/// </param>
/// <exception cref="SessionStateUnauthorizedAccessException">
/// If the alias is readonly or constant.
/// </exception>
internal void SetDefinition(string definition, bool force)
{
// Check to see if the variable is writable
if ((_options & ScopedItemOptions.Constant) != 0 ||
(!force && (_options & ScopedItemOptions.ReadOnly) != 0))
{
SessionStateUnauthorizedAccessException e =
new SessionStateUnauthorizedAccessException(
Name,
SessionStateCategory.Alias,
"AliasNotWritable",
SessionStateStrings.AliasNotWritable);
throw e;
}
_definition = definition;
}
/// <summary>
/// Gets or sets the scope options for the alias.
/// </summary>
/// <exception cref="System.Management.Automation.SessionStateUnauthorizedAccessException">
/// If the trying to set an alias that is constant or
/// if the value trying to be set is ScopedItemOptions.Constant
/// </exception>
public ScopedItemOptions Options
{
get
{
return _options;
}
set
{
SetOptions(value, false);
}
}
/// <summary>
/// Sets the options for the alias and allows changes ReadOnly options only if force is specified.
/// </summary>
/// <param name="newOptions">
/// The new options value.
/// </param>
/// <param name="force">
/// If true the change to the options will happen even if the existing options are read-only.
/// </param>
internal void SetOptions(ScopedItemOptions newOptions, bool force)
{
// Check to see if the variable is constant, if so
// throw an exception because the options cannot be changed.
if ((_options & ScopedItemOptions.Constant) != 0)
{
SessionStateUnauthorizedAccessException e =
new SessionStateUnauthorizedAccessException(
Name,
SessionStateCategory.Alias,
"AliasIsConstant",
SessionStateStrings.AliasIsConstant);
throw e;
}
// Check to see if the variable is readonly, if so
// throw an exception because the options cannot be changed.
if (!force && (_options & ScopedItemOptions.ReadOnly) != 0)
{
SessionStateUnauthorizedAccessException e =
new SessionStateUnauthorizedAccessException(
Name,
SessionStateCategory.Alias,
"AliasIsReadOnly",
SessionStateStrings.AliasIsReadOnly);
throw e;
}
// Now check to see if the caller is trying to set
// the options to constant. This is only allowed at
// variable creation
if ((newOptions & ScopedItemOptions.Constant) != 0)
{
// user is trying to set the variable to constant after
// creating the variable. Do not allow this (as per spec).
SessionStateUnauthorizedAccessException e =
new SessionStateUnauthorizedAccessException(
Name,
SessionStateCategory.Alias,
"AliasCannotBeMadeConstant",
SessionStateStrings.AliasCannotBeMadeConstant);
throw e;
}
if ((newOptions & ScopedItemOptions.AllScope) == 0 &&
(_options & ScopedItemOptions.AllScope) != 0)
{
// user is trying to remove the AllScope option from the alias.
// Do not allow this (as per spec).
SessionStateUnauthorizedAccessException e =
new SessionStateUnauthorizedAccessException(
this.Name,
SessionStateCategory.Alias,
"AliasAllScopeOptionCannotBeRemoved",
SessionStateStrings.AliasAllScopeOptionCannotBeRemoved);
throw e;
}
_options = newOptions;
}
private ScopedItemOptions _options = ScopedItemOptions.None;
/// <summary>
/// Gets or sets the description for the alias.
/// </summary>
public string Description { get; set; } = string.Empty;
/// <summary>
/// If ResolvedCommand returns null, this property will
/// return the name of the command that could not be resolved.
/// If ResolvedCommand has not yet been called or was able
/// to resolve the command, this property will return null.
/// </summary>
internal string UnresolvedCommandName { get; private set; }
/// <summary>
/// The objects output from an alias are the objects output from the resolved
/// command. If we can't resolve the command, assume nothing is output - so use void.
/// </summary>
public override ReadOnlyCollection<PSTypeName> OutputType
{
get
{
CommandInfo resolvedCommand = this.ResolvedCommand;
if (resolvedCommand != null)
{
return resolvedCommand.OutputType;
}
return null;
}
}
}
}
|