Beispiel #1
0
        /// <summary>
        /// Determines if the passed in name is either the fully-qualified pssnapin name or
        /// short name of the provider.
        /// </summary>
        /// <param name="providerName">
        /// The name to compare with the provider name.
        /// </param>
        /// <returns>
        /// True if the name is the fully-qualified pssnapin name or the short name of the provider.
        /// </returns>
        internal bool NameEquals(string providerName)
        {
            PSSnapinQualifiedName qualifiedProviderName = PSSnapinQualifiedName.GetInstance(providerName);

            bool result = false;
            if (qualifiedProviderName != null)
            {
                // If the pssnapin name and provider name are specified, then both must match
                do // false loop
                {
                    if (!string.IsNullOrEmpty(qualifiedProviderName.PSSnapInName))
                    {
                        // After converting core snapins to load as modules, the providers will have Module property populated
                        if (!string.Equals(qualifiedProviderName.PSSnapInName, this.PSSnapInName, StringComparison.OrdinalIgnoreCase) &&
                            !string.Equals(qualifiedProviderName.PSSnapInName, this.ModuleName, StringComparison.OrdinalIgnoreCase))
                        {
                            break;
                        }
                    }

                    result = string.Equals(qualifiedProviderName.ShortName, this.Name, StringComparison.OrdinalIgnoreCase);
                } while (false);
            }
            else
            {
                // If only the provider name is specified, then only the name must match
                result = string.Equals(providerName, Name, StringComparison.OrdinalIgnoreCase);
            }

            return result;
        }
Beispiel #2
0
            internal static List <CompletionResult> PSv2GenerateMatchSetOfCmdlets(CompletionExecutionHelper helper, string lastWord, string quote, bool completingAtStartOfLine)
            {
                bool flag;
                List <CompletionResult> results = new List <CompletionResult>();

                if (PSv2IsCommandLikeCmdlet(lastWord, out flag))
                {
                    Exception exception;
                    helper.CurrentPowerShell.AddCommand("Get-Command").AddParameter("Name", lastWord + "*").AddCommand("Sort-Object").AddParameter("Property", "Name");
                    Collection <PSObject> collection = helper.ExecuteCurrentPowerShell(out exception, null);
                    if ((collection == null) || (collection.Count <= 0))
                    {
                        return(results);
                    }
                    CommandAndName[] cmdlets = new CommandAndName[collection.Count];
                    for (int i = 0; i < collection.Count; i++)
                    {
                        PSObject psObject = collection[i];
                        string   fullName = CmdletInfo.GetFullName(psObject);
                        cmdlets[i] = new CommandAndName(psObject, PSSnapinQualifiedName.GetInstance(fullName));
                    }
                    if (flag)
                    {
                        foreach (CommandAndName name in cmdlets)
                        {
                            AddCommandResult(name, true, completingAtStartOfLine, quote, results);
                        }
                        return(results);
                    }
                    PrependSnapInNameForSameCmdletNames(cmdlets, completingAtStartOfLine, quote, results);
                }
                return(results);
            }
 private string[] GenerateMatchSetOfCmdlets(
     string lastWord,
     bool isPSSnapInSpecified,
     CultureInfo ci)
 {
     using (CommandCompletionBase.tracer.TraceMethod(lastWord, new object[0]))
     {
         string[] strArray = (string[])null;
         Collection <PSObject> collection = this.exec.ExecuteCommand(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "& {{ trap {{ continue }} ; get-command '{0}' -type Cmdlet }}", (object)(lastWord + "*")));
         if (collection != null && collection.Count > 0)
         {
             PSSnapinQualifiedName[] snapinQualifiedNameArray = new PSSnapinQualifiedName[collection.Count];
             for (int index = 0; index < collection.Count; ++index)
             {
                 string fullName = CmdletInfo.GetFullName(collection[index]);
                 snapinQualifiedNameArray[index] = PSSnapinQualifiedName.GetInstance(fullName);
             }
             Array.Sort <PSSnapinQualifiedName>(snapinQualifiedNameArray, new Comparison <PSSnapinQualifiedName>(CommandCompletionBase.PSSnapinQualifiedNameComparison));
             if (isPSSnapInSpecified)
             {
                 strArray = new string[collection.Count];
                 for (int index = 0; index < snapinQualifiedNameArray.Length; ++index)
                 {
                     strArray[index] = snapinQualifiedNameArray[index].FullName;
                 }
             }
             else
             {
                 strArray = CommandCompletionBase.PrependPSSnapInNameForSameCmdletNames(snapinQualifiedNameArray);
             }
         }
         return(strArray);
     }
 }
        private CommandInfo GetFunction(string function)
        {
            CommandInfo commandInfo = (CommandInfo)this._context.EngineSessionState.GetFunction(function);

            if (commandInfo != null)
            {
                if (commandInfo is FilterInfo)
                {
                    CommandDiscovery.discoveryTracer.WriteLine("Filter found: {0}", (object)function);
                }
                else
                {
                    CommandDiscovery.discoveryTracer.WriteLine("Function found: {0}  {1}", (object)function);
                }
            }
            else if (function.IndexOf('\\') > 0)
            {
                PSSnapinQualifiedName instance = PSSnapinQualifiedName.GetInstance(function);
                if (instance != null && !string.IsNullOrEmpty(instance.PSSnapInName))
                {
                    string              psSnapInName  = instance.PSSnapInName;
                    PSModuleInfo        psModuleInfo1 = (PSModuleInfo)null;
                    List <PSModuleInfo> modules       = this._context.Modules.GetModules(new string[1]
                    {
                        psSnapInName
                    }, false);
                    if (modules != null)
                    {
                        if (modules.Count == 1)
                        {
                            if (modules[0].ModuleType != ModuleType.Binary)
                            {
                                psModuleInfo1 = modules[0];
                            }
                        }
                        else
                        {
                            foreach (PSModuleInfo psModuleInfo2 in modules)
                            {
                                if (psModuleInfo2.ModuleType != ModuleType.Binary)
                                {
                                    psModuleInfo1 = psModuleInfo2;
                                    break;
                                }
                            }
                        }
                        if (psModuleInfo1 != null && psModuleInfo1.ExportedFunctions.ContainsKey(instance.ShortName))
                        {
                            commandInfo = (CommandInfo)psModuleInfo1.ExportedFunctions[instance.ShortName];
                        }
                    }
                }
            }
            return(commandInfo);
        }
Beispiel #5
0
        internal bool IsMatch(string providerName)
        {
            PSSnapinQualifiedName instance    = PSSnapinQualifiedName.GetInstance(providerName);
            WildcardPattern       namePattern = null;

            if ((instance != null) && WildcardPattern.ContainsWildcardCharacters(instance.ShortName))
            {
                namePattern = new WildcardPattern(instance.ShortName, WildcardOptions.IgnoreCase);
            }
            return(this.IsMatch(namePattern, instance));
        }
Beispiel #6
0
        internal bool IsMatch(string providerName)
        {
            PSSnapinQualifiedName psSnapinQualifiedName = PSSnapinQualifiedName.GetInstance(providerName);

            WildcardPattern namePattern = null;

            if (psSnapinQualifiedName != null && WildcardPattern.ContainsWildcardCharacters(psSnapinQualifiedName.ShortName))
            {
                namePattern = WildcardPattern.Get(psSnapinQualifiedName.ShortName, WildcardOptions.IgnoreCase);
            }

            return IsMatch(namePattern, psSnapinQualifiedName);
        }
Beispiel #7
0
        internal bool NameEquals(string providerName)
        {
            PSSnapinQualifiedName instance = PSSnapinQualifiedName.GetInstance(providerName);
            bool flag = false;

            if (instance != null)
            {
                if ((!string.IsNullOrEmpty(instance.PSSnapInName) && !string.Equals(instance.PSSnapInName, this.PSSnapInName, StringComparison.OrdinalIgnoreCase)) && !string.Equals(instance.PSSnapInName, this.ModuleName, StringComparison.OrdinalIgnoreCase))
                {
                    return(flag);
                }
                return(string.Equals(instance.ShortName, this.Name, StringComparison.OrdinalIgnoreCase));
            }
            return(string.Equals(providerName, this.Name, StringComparison.OrdinalIgnoreCase));
        }
Beispiel #8
0
        private CommandInfo GetFunctionFromModules(string command)
        {
            FunctionInfo info = null;

            if (command.IndexOf('\\') > 0)
            {
                PSSnapinQualifiedName instance = PSSnapinQualifiedName.GetInstance(command);
                if ((instance != null) && !string.IsNullOrEmpty(instance.PSSnapInName))
                {
                    PSModuleInfo importedModuleByName = this.GetImportedModuleByName(instance.PSSnapInName);
                    if (importedModuleByName != null)
                    {
                        importedModuleByName.ExportedFunctions.TryGetValue(instance.ShortName, out info);
                    }
                }
            }
            return(info);
        }
Beispiel #9
0
        private CmdletInfo GetNextCmdlet()
        {
            CmdletInfo result = null;

            if (this.matchingCmdlet == null)
            {
                if ((this.commandResolutionOptions & SearchResolutionOptions.CommandNameIsPattern) != SearchResolutionOptions.None)
                {
                    Collection <CmdletInfo> collection = new Collection <CmdletInfo>();
                    PSSnapinQualifiedName   instance   = PSSnapinQualifiedName.GetInstance(this.commandName);
                    if (instance == null)
                    {
                        return(result);
                    }
                    WildcardPattern pattern = new WildcardPattern(instance.ShortName, WildcardOptions.IgnoreCase);
                    foreach (List <CmdletInfo> list in this._context.EngineSessionState.GetCmdletTable().Values)
                    {
                        foreach (CmdletInfo info2 in list)
                        {
                            if (pattern.IsMatch(info2.Name) && (string.IsNullOrEmpty(instance.PSSnapInName) || instance.PSSnapInName.Equals(info2.ModuleName, StringComparison.OrdinalIgnoreCase)))
                            {
                                collection.Add(info2);
                            }
                        }
                    }
                    this.matchingCmdlet = collection.GetEnumerator();
                }
                else
                {
                    this.matchingCmdlet = this._context.CommandDiscovery.GetCmdletInfo(this.commandName, (this.commandResolutionOptions & SearchResolutionOptions.SearchAllScopes) != SearchResolutionOptions.None);
                }
            }
            if (!this.matchingCmdlet.MoveNext())
            {
                this.currentState   = SearchState.SearchingBuiltinScripts;
                this.matchingCmdlet = null;
            }
            else
            {
                result = this.matchingCmdlet.Current;
            }
            return(traceResult(result));
        }
Beispiel #10
0
        internal override IEnumerable <HelpInfo> SearchHelp(
            HelpRequest helpRequest,
            bool searchOnlyContent)
        {
            using (ProviderHelpProvider.tracer.TraceMethod())
            {
                int             countOfHelpInfoObjectsFound = 0;
                string          target          = helpRequest.Target;
                string          pattern         = target;
                WildcardPattern wildCardPattern = (WildcardPattern)null;
                bool            decoratedSearch = !WildcardPattern.ContainsWildcardCharacters(target);
                if (!searchOnlyContent)
                {
                    if (decoratedSearch)
                    {
                        // ISSUE: reference to a compiler-generated field
                        this.\u003Cpattern\u003E5__d += "*";
                    }
                }
                else
                {
                    string pattern1 = helpRequest.Target;
                    if (decoratedSearch)
                    {
                        pattern1 = "*" + helpRequest.Target + "*";
                    }
                    wildCardPattern = new WildcardPattern(pattern1, WildcardOptions.Compiled | WildcardOptions.IgnoreCase);
                    pattern         = "*";
                }
                PSSnapinQualifiedName snapinQualifiedNameForPattern = PSSnapinQualifiedName.GetInstance(pattern);
                if (snapinQualifiedNameForPattern != null)
                {
                    foreach (ProviderInfo providerInfo in this._sessionState.Provider.GetAll())
                    {
                        if (providerInfo.IsMatch(pattern))
                        {
                            try
                            {
                                this.LoadHelpFile(providerInfo);
                            }
                            catch (IOException ex)
                            {
                                if (!decoratedSearch)
                                {
                                    this.ReportHelpFileError((Exception)ex, providerInfo.Name, providerInfo.HelpFile);
                                }
                            }
                            catch (SecurityException ex)
                            {
                                if (!decoratedSearch)
                                {
                                    this.ReportHelpFileError((Exception)ex, providerInfo.Name, providerInfo.HelpFile);
                                }
                            }
                            catch (XmlException ex)
                            {
                                if (!decoratedSearch)
                                {
                                    this.ReportHelpFileError((Exception)ex, providerInfo.Name, providerInfo.HelpFile);
                                }
                            }
                            HelpInfo helpInfo = this.GetCache(providerInfo.PSSnapInName + "\\" + providerInfo.Name);
                            if (helpInfo != null && (!searchOnlyContent || helpInfo.MatchPatternInContent(wildCardPattern)))
                            {
                                ++countOfHelpInfoObjectsFound;
                                yield return(helpInfo);

                                if (countOfHelpInfoObjectsFound >= helpRequest.MaxResults && helpRequest.MaxResults > 0)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #11
0
        /// <summary>
        /// Search for provider help based on a search target.
        /// </summary>
        /// <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></returns>
        internal override IEnumerable <HelpInfo> SearchHelp(HelpRequest helpRequest, bool searchOnlyContent)
        {
            int    countOfHelpInfoObjectsFound = 0;
            string target  = helpRequest.Target;
            string pattern = target;
            // this will be used only when searchOnlyContent == true
            WildcardPattern wildCardPattern = null;

            bool decoratedSearch = !WildcardPattern.ContainsWildcardCharacters(target);

            if (!searchOnlyContent)
            {
                if (decoratedSearch)
                {
                    pattern += "*";
                }
            }
            else
            {
                string searchTarget = helpRequest.Target;
                if (decoratedSearch)
                {
                    searchTarget = "*" + helpRequest.Target + "*";
                }

                wildCardPattern = WildcardPattern.Get(searchTarget, WildcardOptions.Compiled | WildcardOptions.IgnoreCase);
                // search in all providers
                pattern = "*";
            }

            PSSnapinQualifiedName snapinQualifiedNameForPattern =
                PSSnapinQualifiedName.GetInstance(pattern);

            if (null == snapinQualifiedNameForPattern)
            {
                yield break;
            }

            foreach (ProviderInfo providerInfo in _sessionState.Provider.GetAll())
            {
                if (providerInfo.IsMatch(pattern))
                {
                    try
                    {
                        LoadHelpFile(providerInfo);
                    }
                    catch (IOException ioException)
                    {
                        if (!decoratedSearch)
                        {
                            ReportHelpFileError(ioException, providerInfo.Name, providerInfo.HelpFile);
                        }
                    }
                    catch (System.Security.SecurityException securityException)
                    {
                        if (!decoratedSearch)
                        {
                            ReportHelpFileError(securityException, providerInfo.Name, providerInfo.HelpFile);
                        }
                    }
                    catch (XmlException xmlException)
                    {
                        if (!decoratedSearch)
                        {
                            ReportHelpFileError(xmlException, providerInfo.Name, providerInfo.HelpFile);
                        }
                    }

                    HelpInfo helpInfo = GetCache(providerInfo.PSSnapInName + "\\" + providerInfo.Name);

                    if (helpInfo != null)
                    {
                        if (searchOnlyContent)
                        {
                            // ignore help objects that do not have pattern in its help
                            // content.
                            if (!helpInfo.MatchPatternInContent(wildCardPattern))
                            {
                                continue;
                            }
                        }

                        countOfHelpInfoObjectsFound++;
                        yield return(helpInfo);

                        if (countOfHelpInfoObjectsFound >= helpRequest.MaxResults && helpRequest.MaxResults > 0)
                        {
                            yield break;
                        }
                    }
                }
            }
        }
Beispiel #12
0
        private CmdletInfo GetNextCmdlet()
        {
            CmdletInfo result = (CmdletInfo)null;

            if (this.matchingCmdlet == null)
            {
                Collection <CmdletInfo> matchingCmdlets;
                if ((this.commandResolutionOptions & SearchResolutionOptions.CommandNameIsPattern) != SearchResolutionOptions.None)
                {
                    matchingCmdlets = new Collection <CmdletInfo>();
                    PSSnapinQualifiedName instance = PSSnapinQualifiedName.GetInstance(this.commandName);
                    if (instance == null)
                    {
                        return(result);
                    }
                    WildcardPattern wildcardPattern = new WildcardPattern(instance.ShortName, WildcardOptions.IgnoreCase);
                    Dictionary <string, List <CmdletInfo> > cmdletCache = this._context.EngineSessionState.CmdletCache;
                    while (true)
                    {
                        lock (cmdletCache)
                        {
                            foreach (List <CmdletInfo> cmdletInfoList in cmdletCache.Values)
                            {
                                foreach (CmdletInfo cmdletInfo in cmdletInfoList)
                                {
                                    if (wildcardPattern.IsMatch(cmdletInfo.Name) && (string.IsNullOrEmpty(instance.PSSnapInName) || instance.PSSnapInName.Equals(cmdletInfo.ModuleName, StringComparison.OrdinalIgnoreCase)))
                                    {
                                        matchingCmdlets.Add(cmdletInfo);
                                    }
                                }
                            }
                        }
                        if (cmdletCache != this._context.TopLevelSessionState.CmdletCache)
                        {
                            cmdletCache = this._context.TopLevelSessionState.CmdletCache;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    matchingCmdlets = this._context.CommandDiscovery.GetCmdletInfo(this.commandName);
                    if (matchingCmdlets.Count > 1)
                    {
                        if ((this.commandResolutionOptions & SearchResolutionOptions.ReturnFirstDuplicateCmdletName) != SearchResolutionOptions.None)
                        {
                            this.matchingCmdlet = matchingCmdlets.GetEnumerator();
                            while (this.matchingCmdlet.MoveNext())
                            {
                                if (result == null)
                                {
                                    result = this.matchingCmdlet.Current;
                                }
                            }
                            return(this.traceResult(result));
                        }
                        if ((this.commandResolutionOptions & SearchResolutionOptions.AllowDuplicateCmdletNames) == SearchResolutionOptions.None)
                        {
                            throw this.NewAmbiguousCmdletName(this.commandName, matchingCmdlets);
                        }
                    }
                }
                this.matchingCmdlet = matchingCmdlets.GetEnumerator();
            }
            if (!this.matchingCmdlet.MoveNext())
            {
                this.currentState   = CommandSearcher.SearchState.CmdletResolution;
                this.matchingCmdlet = (IEnumerator <CmdletInfo>)null;
            }
            else
            {
                result = this.matchingCmdlet.Current;
            }
            return(this.traceResult(result));
        }
Beispiel #13
0
        internal Collection <CmdletInfo> GetCmdletInfo(string cmdletName)
        {
            PSSnapinQualifiedName instance = PSSnapinQualifiedName.GetInstance(cmdletName);

            return(instance == null ? new Collection <CmdletInfo>() : this.GetCmdletInfo(instance));
        }
Beispiel #14
0
        internal override IEnumerable <HelpInfo> SearchHelp(HelpRequest helpRequest, bool searchOnlyContent)
        {
            int             iteratorVariable0 = 0;
            string          pattern           = helpRequest.Target;
            string          name = pattern;
            WildcardPattern iteratorVariable3 = null;
            bool            iteratorVariable4 = !WildcardPattern.ContainsWildcardCharacters(pattern);

            if (!searchOnlyContent)
            {
                if (iteratorVariable4)
                {
                    name = name + "*";
                }
            }
            else
            {
                string target = helpRequest.Target;
                if (iteratorVariable4)
                {
                    target = "*" + helpRequest.Target + "*";
                }
                iteratorVariable3 = new WildcardPattern(target, WildcardOptions.IgnoreCase | WildcardOptions.Compiled);
                name = "*";
            }
            PSSnapinQualifiedName instance = PSSnapinQualifiedName.GetInstance(name);

            if (instance != null)
            {
                foreach (ProviderInfo iteratorVariable6 in this._sessionState.Provider.GetAll())
                {
                    if (!iteratorVariable6.IsMatch(name))
                    {
                        continue;
                    }
                    try
                    {
                        this.LoadHelpFile(iteratorVariable6);
                    }
                    catch (IOException exception)
                    {
                        if (!iteratorVariable4)
                        {
                            this.ReportHelpFileError(exception, iteratorVariable6.Name, iteratorVariable6.HelpFile);
                        }
                    }
                    catch (SecurityException exception2)
                    {
                        if (!iteratorVariable4)
                        {
                            this.ReportHelpFileError(exception2, iteratorVariable6.Name, iteratorVariable6.HelpFile);
                        }
                    }
                    catch (XmlException exception3)
                    {
                        if (!iteratorVariable4)
                        {
                            this.ReportHelpFileError(exception3, iteratorVariable6.Name, iteratorVariable6.HelpFile);
                        }
                    }
                    HelpInfo cache = this.GetCache(iteratorVariable6.PSSnapInName + @"\" + iteratorVariable6.Name);
                    if ((cache != null) && (!searchOnlyContent || cache.MatchPatternInContent(iteratorVariable3)))
                    {
                        iteratorVariable0++;
                        yield return(cache);

                        if ((iteratorVariable0 >= helpRequest.MaxResults) && (helpRequest.MaxResults > 0))
                        {
                            break;
                        }
                    }
                }
            }
        }