/// <summary>
        /// Search an alias help target.
        /// </summary>
        /// <remarks>
        /// This will,
        ///     a. use _sessionState object to get a list of alias that match the target.
        ///     b. for each alias, retrieve help info as in ExactMatchHelp.
        /// </remarks>
        /// <param name="helpRequest">help request object</param>
        /// <param name="searchOnlyContent">
        /// If true, searches for pattern in the help content. Individual
        /// provider can decide which content to search in.
        ///
        /// If false, searches for pattern in the command names.
        /// </param>
        /// <returns>a IEnumerable of helpinfo object</returns>
        internal override IEnumerable <HelpInfo> SearchHelp(HelpRequest helpRequest, bool searchOnlyContent)
        {
            // aliases do not have help content...so doing nothing in that case
            if (!searchOnlyContent)
            {
                string    target    = helpRequest.Target;
                string    pattern   = target;
                Hashtable hashtable = new Hashtable(StringComparer.OrdinalIgnoreCase);

                if (!WildcardPattern.ContainsWildcardCharacters(target))
                {
                    pattern += "*";
                }

                WildcardPattern matcher = WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase);
                IDictionary <string, AliasInfo> aliasTable = _sessionState.Internal.GetAliasTable();

                foreach (string name in aliasTable.Keys)
                {
                    if (matcher.IsMatch(name))
                    {
                        HelpRequest exactMatchHelpRequest = helpRequest.Clone();
                        exactMatchHelpRequest.Target = name;
                        // Duplicates??
                        foreach (HelpInfo helpInfo in ExactMatchHelp(exactMatchHelpRequest))
                        {
                            // Component/Role/Functionality match is done only for SearchHelp
                            // as "get-help * -category alias" should not forwad help to
                            // CommandHelpProvider..(ExactMatchHelp does forward help to
                            // CommandHelpProvider)
                            if (!Match(helpInfo, helpRequest))
                            {
                                continue;
                            }

                            if (hashtable.ContainsKey(name))
                            {
                                continue;
                            }

                            hashtable.Add(name, null);

                            yield return(helpInfo);
                        }
                    }
                }

                CommandSearcher searcher =
                    new CommandSearcher(
                        pattern,
                        SearchResolutionOptions.ResolveAliasPatterns, CommandTypes.Alias,
                        _context);

                while (searcher.MoveNext())
                {
                    CommandInfo current = ((IEnumerator <CommandInfo>)searcher).Current;

                    if (_context.CurrentPipelineStopping)
                    {
                        yield break;
                    }

                    AliasInfo alias = current as AliasInfo;

                    if (alias != null)
                    {
                        string      name = alias.Name;
                        HelpRequest exactMatchHelpRequest = helpRequest.Clone();
                        exactMatchHelpRequest.Target = name;

                        // Duplicates??
                        foreach (HelpInfo helpInfo in ExactMatchHelp(exactMatchHelpRequest))
                        {
                            // Component/Role/Functionality match is done only for SearchHelp
                            // as "get-help * -category alias" should not forwad help to
                            // CommandHelpProvider..(ExactMatchHelp does forward help to
                            // CommandHelpProvider)
                            if (!Match(helpInfo, helpRequest))
                            {
                                continue;
                            }

                            if (hashtable.ContainsKey(name))
                            {
                                continue;
                            }

                            hashtable.Add(name, null);

                            yield return(helpInfo);
                        }
                    }
                }

                foreach (CommandInfo current in ModuleUtils.GetMatchingCommands(pattern, _context, helpRequest.CommandOrigin))
                {
                    if (_context.CurrentPipelineStopping)
                    {
                        yield break;
                    }

                    AliasInfo alias = current as AliasInfo;

                    if (alias != null)
                    {
                        string name = alias.Name;

                        HelpInfo helpInfo = AliasHelpInfo.GetHelpInfo(alias);

                        if (hashtable.ContainsKey(name))
                        {
                            continue;
                        }

                        hashtable.Add(name, null);

                        yield return(helpInfo);
                    }
                }
            }
        }
        internal override IEnumerable <HelpInfo> SearchHelp(HelpRequest helpRequest, bool searchOnlyContent)
        {
            string item = helpRequest.Target;
            Collection <string> iteratorVariable1 = new Collection <string>();
            WildcardPattern     pattern           = null;
            bool iteratorVariable3 = !WildcardPattern.ContainsWildcardCharacters(helpRequest.Target);

            if (!searchOnlyContent)
            {
                if (iteratorVariable3)
                {
                    if (item.IndexOf('-') >= 0)
                    {
                        iteratorVariable1.Add(item + "*");
                    }
                    else
                    {
                        iteratorVariable1.Add("*" + item + "*");
                    }
                }
                else
                {
                    iteratorVariable1.Add(item);
                }
            }
            else
            {
                iteratorVariable1.Add("*");
                string target = helpRequest.Target;
                if (iteratorVariable3)
                {
                    target = "*" + helpRequest.Target + "*";
                }
                pattern = new WildcardPattern(target, WildcardOptions.IgnoreCase | WildcardOptions.Compiled);
            }
            int       iteratorVariable4 = 0;
            Hashtable iteratorVariable5 = new Hashtable(StringComparer.OrdinalIgnoreCase);
            Hashtable iteratorVariable6 = new Hashtable(StringComparer.OrdinalIgnoreCase);

            foreach (string iteratorVariable7 in iteratorVariable1)
            {
                CommandSearcher commandSearcherForSearch = this.GetCommandSearcherForSearch(iteratorVariable7, this._context);
                while (commandSearcherForSearch.MoveNext())
                {
                    if (this._context.CurrentPipelineStopping)
                    {
                        break;
                    }
                    CommandInfo current    = commandSearcherForSearch.Current;
                    CmdletInfo  cmdletInfo = current as CmdletInfo;
                    HelpInfo    helpInfo   = null;
                    string      key        = null;
                    if (cmdletInfo != null)
                    {
                        helpInfo = this.GetHelpInfo(cmdletInfo, !iteratorVariable3);
                        key      = cmdletInfo.FullName;
                    }
                    else
                    {
                        IScriptCommandInfo scriptCommandInfo = current as IScriptCommandInfo;
                        if (scriptCommandInfo != null)
                        {
                            key      = current.Name;
                            helpInfo = this.GetHelpInfo(scriptCommandInfo, !iteratorVariable3, searchOnlyContent);
                        }
                    }
                    if (helpInfo != null)
                    {
                        if (!SessionState.IsVisible(helpRequest.CommandOrigin, current))
                        {
                            if (!iteratorVariable6.ContainsKey(key))
                            {
                                iteratorVariable6.Add(key, null);
                            }
                        }
                        else if ((!iteratorVariable5.ContainsKey(key) && Match(helpInfo, helpRequest, current)) && (!searchOnlyContent || helpInfo.MatchPatternInContent(pattern)))
                        {
                            iteratorVariable5.Add(key, null);
                            iteratorVariable4++;
                            yield return(helpInfo);

                            if ((iteratorVariable4 < helpRequest.MaxResults) || (helpRequest.MaxResults <= 0))
                            {
                                continue;
                            }
                            break;
                        }
                    }
                }
                if (this.HelpCategory == (System.Management.Automation.HelpCategory.Cmdlet | System.Management.Automation.HelpCategory.Alias))
                {
                    foreach (CommandInfo iteratorVariable13 in ModuleUtils.GetMatchingCommands(iteratorVariable7, this._context, helpRequest.CommandOrigin, false))
                    {
                        if (this._context.CurrentPipelineStopping)
                        {
                            break;
                        }
                        if (SessionState.IsVisible(helpRequest.CommandOrigin, iteratorVariable13))
                        {
                            CmdletInfo iteratorVariable14 = iteratorVariable13 as CmdletInfo;
                            HelpInfo   iteratorVariable15 = null;
                            string     fullName           = null;
                            if (iteratorVariable14 != null)
                            {
                                iteratorVariable15 = this.GetHelpInfo(iteratorVariable14, !iteratorVariable3);
                                fullName           = iteratorVariable14.FullName;
                            }
                            else
                            {
                                IScriptCommandInfo info2 = iteratorVariable13 as IScriptCommandInfo;
                                if (info2 != null)
                                {
                                    fullName           = iteratorVariable13.Name;
                                    iteratorVariable15 = this.GetHelpInfo(info2, !iteratorVariable3, searchOnlyContent);
                                }
                            }
                            if ((((iteratorVariable15 != null) && !iteratorVariable5.ContainsKey(fullName)) && (!iteratorVariable6.ContainsKey(fullName) && Match(iteratorVariable15, helpRequest, iteratorVariable13))) && (!searchOnlyContent || iteratorVariable15.MatchPatternInContent(pattern)))
                            {
                                iteratorVariable5.Add(fullName, null);
                                iteratorVariable4++;
                                yield return(iteratorVariable15);

                                if ((iteratorVariable4 >= helpRequest.MaxResults) && (helpRequest.MaxResults > 0))
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
Example #3
0
 private void setupPathSearcher()
 {
     if (this.pathSearcher != null)
     {
         return;
     }
     if ((this.commandResolutionOptions & SearchResolutionOptions.CommandNameIsPattern) != SearchResolutionOptions.None)
     {
         this.canDoPathLookup       = true;
         this.canDoPathLookupResult = CommandSearcher.CanDoPathLookupResult.Yes;
         this.pathSearcher          = new CommandPathSearch((IEnumerable <string>) new Collection <string>()
         {
             this.commandName
         }, this._context.CommandDiscovery.GetLookupDirectoryPaths(), this._context);
     }
     else
     {
         this.canDoPathLookupResult = CommandSearcher.CanDoPathLookup(this.commandName);
         if (this.canDoPathLookupResult == CommandSearcher.CanDoPathLookupResult.Yes)
         {
             this.canDoPathLookup = true;
             this.pathSearcher    = new CommandPathSearch(this.ConstructSearchPatternsFromName(this.commandName), this._context.CommandDiscovery.GetLookupDirectoryPaths(), this._context);
         }
         else if (this.canDoPathLookupResult == CommandSearcher.CanDoPathLookupResult.PathIsRooted)
         {
             this.canDoPathLookup = true;
             string directoryName           = Path.GetDirectoryName(this.commandName);
             Collection <string> collection = new Collection <string>();
             collection.Add(directoryName);
             CommandDiscovery.discoveryTracer.WriteLine("The path is rooted, so only doing the lookup in the specified directory: {0}", (object)directoryName);
             string fileName = Path.GetFileName(this.commandName);
             if (!string.IsNullOrEmpty(fileName))
             {
                 this.pathSearcher = new CommandPathSearch(this.ConstructSearchPatternsFromName(fileName), (IEnumerable <string>)collection, this._context);
             }
             else
             {
                 this.canDoPathLookup = false;
             }
         }
         else
         {
             if (this.canDoPathLookupResult != CommandSearcher.CanDoPathLookupResult.DirectorySeparator)
             {
                 return;
             }
             this.canDoPathLookup = true;
             string str = this.ResolvePSPath(Path.GetDirectoryName(this.commandName));
             CommandDiscovery.discoveryTracer.WriteLine("The path is relative, so only doing the lookup in the specified directory: {0}", (object)str);
             if (str == null)
             {
                 this.canDoPathLookup = false;
             }
             else
             {
                 Collection <string> collection = new Collection <string>();
                 collection.Add(str);
                 string fileName = Path.GetFileName(this.commandName);
                 if (!string.IsNullOrEmpty(fileName))
                 {
                     this.pathSearcher = new CommandPathSearch(this.ConstructSearchPatternsFromName(fileName), (IEnumerable <string>)collection, this._context);
                 }
                 else
                 {
                     this.canDoPathLookup = false;
                 }
             }
         }
     }
 }
Example #4
0
        private CommandInfo ProcessBuiltinScriptState()
        {
            CommandInfo commandInfo = (CommandInfo)null;

            if (this._context.EngineSessionState != null && this._context.EngineSessionState.ProviderCount > 0 && CommandSearcher.IsQualifiedPSPath(this.commandName))
            {
                commandInfo = this.GetNextFromPath();
            }
            return(commandInfo);
        }
Example #5
0
        internal CommandInfo LookupCommandInfo(
            string commandName,
            CommandOrigin commandOrigin)
        {
            CommandInfo commandInfo    = (CommandInfo)null;
            string      commandName1   = commandName;
            Exception   innerException = (Exception)null;

            while (true)
            {
                CommandDiscovery.discoveryTracer.WriteLine("Looking up command: {0}", (object)commandName);
                if (!string.IsNullOrEmpty(commandName))
                {
                    CommandSearcher commandSearcher = new CommandSearcher(commandName, SearchResolutionOptions.AllowDuplicateCmdletNames, CommandTypes.All, this._context);
                    commandSearcher.CommandOrigin = commandOrigin;
                    try
                    {
                        if (!commandSearcher.MoveNext())
                        {
                            if (!commandName.Contains("-"))
                            {
                                CommandDiscovery.discoveryTracer.WriteLine("The command [{0}] was not found, trying again with get- prepended", (object)commandName);
                                commandName = "get" + (object)'-' + commandName;
                            }
                            else
                            {
                                goto label_13;
                            }
                        }
                        else
                        {
                            commandInfo = ((IEnumerator <CommandInfo>)commandSearcher).Current;
                            goto label_13;
                        }
                    }
                    catch (ArgumentException ex)
                    {
                        innerException = (Exception)ex;
                        goto label_13;
                    }
                    catch (PathTooLongException ex)
                    {
                        innerException = (Exception)ex;
                        goto label_13;
                    }
                    catch (FileLoadException ex)
                    {
                        innerException = (Exception)ex;
                        goto label_13;
                    }
                    catch (FormatException ex)
                    {
                        innerException = (Exception)ex;
                        goto label_13;
                    }
                    catch (MetadataException ex)
                    {
                        innerException = (Exception)ex;
                        goto label_13;
                    }
                }
                else
                {
                    break;
                }
            }
            CommandDiscovery.discoveryTracer.TraceError("Command name empty or null");
label_13:
            if (commandInfo == null)
            {
                CommandDiscovery.discoveryTracer.TraceError("'{0}' is not recognized as a cmdlet, function, operable program or script file.", (object)commandName);
                CommandNotFoundException notFoundException = new CommandNotFoundException(commandName1, innerException, "CommandNotFoundException", new object[0]);
                CommandDiscovery.tracer.TraceException((Exception)notFoundException);
                throw notFoundException;
            }
            return(commandInfo);
        }
Example #6
0
        internal override IEnumerable <HelpInfo> SearchHelp(HelpRequest helpRequest, bool searchOnlyContent)
        {
            if (!searchOnlyContent)
            {
                string    target            = helpRequest.Target;
                string    pattern           = target;
                Hashtable iteratorVariable2 = new Hashtable(StringComparer.OrdinalIgnoreCase);
                if (!WildcardPattern.ContainsWildcardCharacters(target))
                {
                    pattern = pattern + "*";
                }
                WildcardPattern iteratorVariable3          = new WildcardPattern(pattern, WildcardOptions.IgnoreCase);
                IDictionary <string, AliasInfo> aliasTable = this._sessionState.Internal.GetAliasTable();
                foreach (string iteratorVariable5 in aliasTable.Keys)
                {
                    if (iteratorVariable3.IsMatch(iteratorVariable5))
                    {
                        HelpRequest iteratorVariable6 = helpRequest.Clone();
                        iteratorVariable6.Target = iteratorVariable5;
                        foreach (HelpInfo iteratorVariable7 in this.ExactMatchHelp(iteratorVariable6))
                        {
                            if (!Match(iteratorVariable7, helpRequest) || iteratorVariable2.ContainsKey(iteratorVariable5))
                            {
                                continue;
                            }
                            iteratorVariable2.Add(iteratorVariable5, null);
                            yield return(iteratorVariable7);
                        }
                    }
                }
                CommandSearcher iteratorVariable8 = new CommandSearcher(pattern, SearchResolutionOptions.ResolveAliasPatterns, CommandTypes.Alias, this._context);
                while (iteratorVariable8.MoveNext())
                {
                    CommandInfo current = iteratorVariable8.Current;
                    if (this._context.CurrentPipelineStopping)
                    {
                        goto Label_0423;
                    }
                    AliasInfo iteratorVariable10 = current as AliasInfo;
                    if (iteratorVariable10 != null)
                    {
                        string      name = iteratorVariable10.Name;
                        HelpRequest iteratorVariable12 = helpRequest.Clone();
                        iteratorVariable12.Target = name;
                        foreach (HelpInfo iteratorVariable13 in this.ExactMatchHelp(iteratorVariable12))
                        {
                            if (!Match(iteratorVariable13, helpRequest) || iteratorVariable2.ContainsKey(name))
                            {
                                continue;
                            }
                            iteratorVariable2.Add(name, null);
                            yield return(iteratorVariable13);
                        }
                    }
                }
                foreach (CommandInfo iteratorVariable14 in ModuleUtils.GetMatchingCommands(pattern, this._context, helpRequest.CommandOrigin, false))
                {
                    if (this._context.CurrentPipelineStopping)
                    {
                        break;
                    }
                    AliasInfo aliasInfo = iteratorVariable14 as AliasInfo;
                    if (aliasInfo != null)
                    {
                        string   key      = aliasInfo.Name;
                        HelpInfo helpInfo = AliasHelpInfo.GetHelpInfo(aliasInfo);
                        if (!iteratorVariable2.ContainsKey(key))
                        {
                            iteratorVariable2.Add(key, null);
                            yield return(helpInfo);
                        }
                    }
                }
            }
Label_0423:
            yield break;
        }