예제 #1
0
        internal static bool SplitCmdletName(string name, out string verb, out string noun)
        {
            noun = verb = string.Empty;
            if (string.IsNullOrEmpty(name))
            {
                return(false);
            }
            int length = 0;

            for (int index = 0; index < name.Length; ++index)
            {
                if (SpecialCharacters.IsDash(name[index]))
                {
                    length = index;
                    break;
                }
            }
            if (length <= 0)
            {
                return(false);
            }
            verb = name.Substring(0, length);
            noun = name.Substring(length + 1);
            return(true);
        }
예제 #2
0
        internal static bool SplitCmdletName(string name, out string verb, out string noun)
        {
            noun = verb = string.Empty;
            if (string.IsNullOrEmpty(name))
            {
                return(false);
            }

            int index = 0;

            for (int i = 0; i < name.Length; i++)
            {
                if (SpecialCharacters.IsDash(name[i]))
                {
                    index = i;
                    break;
                }
            }

            if (index > 0)
            {
                verb = name.Substring(0, index);
                noun = name.Substring(index + 1);
                return(true);
            }

            return(false);
        }
예제 #3
0
        internal static object StringToNumber(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return((object)null);
            }
            input = input.Trim();
            if (string.IsNullOrEmpty(input))
            {
                return((object)null);
            }
            string str1 = "";

            if (input[0] == '+' || SpecialCharacters.IsDash(input[0]))
            {
                str1  = input[0].ToString();
                input = input.Substring(1);
            }
            string str2 = NumberTokenReader.ScanForNumber(input, 0);

            if (str2 == null)
            {
                return((object)null);
            }
            if (str2.Length < input.Length)
            {
                throw InterpreterError.NewInterpreterException((object)input, typeof(RuntimeException), (Token)null, "BadNumericConstant", (object)input);
            }
            return(NumberTokenReader.ConvertToNumber(str1 + str2, (Tokenizer)null));
        }
예제 #4
0
        internal MergedCompiledCommandParameter GetMatchingParameter(string name, bool throwOnParameterNotFound, bool tryExactMatching, InvocationInfo invocationInfo)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw PSTraceSource.NewArgumentException("name");
            }
            Collection <MergedCompiledCommandParameter> collection = new Collection <MergedCompiledCommandParameter>();

            if ((name.Length > 0) && SpecialCharacters.IsDash(name[0]))
            {
                name = name.Substring(1);
            }
            foreach (string str in this.bindableParameters.Keys)
            {
                if (nameCompareInfo.IsPrefix(str, name, CompareOptions.IgnoreCase))
                {
                    if (tryExactMatching && string.Equals(str, name, StringComparison.OrdinalIgnoreCase))
                    {
                        return(this.bindableParameters[str]);
                    }
                    collection.Add(this.bindableParameters[str]);
                }
            }
            foreach (string str2 in this.aliasedParameters.Keys)
            {
                if (nameCompareInfo.IsPrefix(str2, name, CompareOptions.IgnoreCase))
                {
                    if (tryExactMatching && string.Equals(str2, name, StringComparison.OrdinalIgnoreCase))
                    {
                        return(this.aliasedParameters[str2]);
                    }
                    if (!collection.Contains(this.aliasedParameters[str2]))
                    {
                        collection.Add(this.aliasedParameters[str2]);
                    }
                }
            }
            if (collection.Count > 1)
            {
                StringBuilder builder = new StringBuilder();
                foreach (MergedCompiledCommandParameter parameter in collection)
                {
                    builder.AppendFormat(" -{0}", parameter.Parameter.Name);
                }
                ParameterBindingException exception = new ParameterBindingException(ErrorCategory.InvalidArgument, invocationInfo, null, name, null, null, "ParameterBinderStrings", "AmbiguousParameter", new object[] { builder });
                throw exception;
            }
            if ((collection.Count == 0) && throwOnParameterNotFound)
            {
                ParameterBindingException exception2 = new ParameterBindingException(ErrorCategory.InvalidArgument, invocationInfo, null, name, null, null, "ParameterBinderStrings", "NamedParameterNotFound", new object[0]);
                throw exception2;
            }
            MergedCompiledCommandParameter parameter2 = null;

            if (collection.Count > 0)
            {
                parameter2 = collection[0];
            }
            return(parameter2);
        }
예제 #5
0
        internal static string EscapeSingleQuotedString(string stringContent)
        {
            if (string.IsNullOrEmpty(stringContent))
            {
                return(string.Empty);
            }
            StringBuilder builder = new StringBuilder(stringContent.Length);

            foreach (char ch in stringContent)
            {
                builder.Append(ch);
                if (SpecialCharacters.IsSingleQuote(ch))
                {
                    builder.Append(ch);
                }
            }
            return(builder.ToString());
        }
예제 #6
0
        private static string ConvertDash(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(str);
            }
            StringBuilder stringBuilder = new StringBuilder();

            foreach (char c in str)
            {
                if (SpecialCharacters.IsDash(c))
                {
                    stringBuilder.Append('-');
                }
                else
                {
                    stringBuilder.Append(c);
                }
            }
            return(stringBuilder.ToString());
        }
        /// <summary>
        /// Gets the parameters by matching its name.
        /// </summary>
        ///
        /// <param name="name">
        /// The name of the parameter.
        /// </param>
        ///
        /// <param name="throwOnParameterNotFound">
        /// If true and a matching parameter is not found, an exception will be
        /// throw. If false and a matching parameter is not found, null is returned.
        /// </param>
        ///
        /// <param name="tryExactMatching">
        /// If true we do exact matching, otherwise we do not.
        /// </param>
        ///
        /// <param name="invocationInfo">
        /// The invocation information about the code being run.
        /// </param>
        ///
        /// <returns>
        /// The a collection of the metadata associated with the parameters that
        /// match the specified name. If no matches were found, an empty collection
        /// is returned.
        /// </returns>
        ///
        /// <exception cref="ArgumentException">
        /// If <paramref name="name"/> is null or empty.
        /// </exception>
        ///
        internal MergedCompiledCommandParameter GetMatchingParameter(
            string name,
            bool throwOnParameterNotFound,
            bool tryExactMatching,
            InvocationInfo invocationInfo)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw PSTraceSource.NewArgumentException("name");
            }

            Collection <MergedCompiledCommandParameter> matchingParameters =
                new Collection <MergedCompiledCommandParameter>();

            // Skip the leading '-' if present
            if (name.Length > 0 && SpecialCharacters.IsDash(name[0]))
            {
                name = name.Substring(1);
            }


            // First try to match the bindable parameters

            foreach (string parameterName in _bindableParameters.Keys)
            {
                if (CultureInfo.InvariantCulture.CompareInfo.IsPrefix(parameterName, name, CompareOptions.IgnoreCase))
                {
                    // If it is an exact match then only return the exact match
                    // as the result

                    if (tryExactMatching && String.Equals(parameterName, name, StringComparison.OrdinalIgnoreCase))
                    {
                        return(_bindableParameters[parameterName]);
                    }
                    else
                    {
                        matchingParameters.Add(_bindableParameters[parameterName]);
                    }
                }
            }

            // Now check the aliases

            foreach (string parameterName in _aliasedParameters.Keys)
            {
                if (CultureInfo.InvariantCulture.CompareInfo.IsPrefix(parameterName, name, CompareOptions.IgnoreCase))
                {
                    // If it is an exact match then only return the exact match
                    // as the result

                    if (tryExactMatching && String.Equals(parameterName, name, StringComparison.OrdinalIgnoreCase))
                    {
                        return(_aliasedParameters[parameterName]);
                    }
                    else
                    {
                        if (!matchingParameters.Contains(_aliasedParameters[parameterName]))
                        {
                            matchingParameters.Add(_aliasedParameters[parameterName]);
                        }
                    }
                }
            }

            if (matchingParameters.Count > 1)
            {
                // Prefer parameters in the cmdlet over common parameters
                Collection <MergedCompiledCommandParameter> filteredParameters =
                    new Collection <MergedCompiledCommandParameter>();

                foreach (MergedCompiledCommandParameter matchingParameter in matchingParameters)
                {
                    if ((matchingParameter.BinderAssociation == ParameterBinderAssociation.DeclaredFormalParameters) ||
                        (matchingParameter.BinderAssociation == ParameterBinderAssociation.DynamicParameters))
                    {
                        filteredParameters.Add(matchingParameter);
                    }
                }

                if (filteredParameters.Count == 1)
                {
                    matchingParameters = filteredParameters;
                }
                else
                {
                    StringBuilder possibleMatches = new StringBuilder();

                    foreach (MergedCompiledCommandParameter matchingParameter in matchingParameters)
                    {
                        possibleMatches.Append(" -");
                        possibleMatches.Append(matchingParameter.Parameter.Name);
                    }

                    ParameterBindingException exception =
                        new ParameterBindingException(
                            ErrorCategory.InvalidArgument,
                            invocationInfo,
                            null,
                            name,
                            null,
                            null,
                            ParameterBinderStrings.AmbiguousParameter,
                            "AmbiguousParameter",
                            possibleMatches);

                    throw exception;
                }
            }
            else if (matchingParameters.Count == 0)
            {
                if (throwOnParameterNotFound)
                {
                    ParameterBindingException exception =
                        new ParameterBindingException(
                            ErrorCategory.InvalidArgument,
                            invocationInfo,
                            null,
                            name,
                            null,
                            null,
                            ParameterBinderStrings.NamedParameterNotFound,
                            "NamedParameterNotFound");

                    throw exception;
                }
            }

            MergedCompiledCommandParameter result = null;

            if (matchingParameters.Count > 0)
            {
                result = matchingParameters[0];
            }
            return(result);
        } // GetMatchingParameter
예제 #8
0
        internal MergedCompiledCommandParameter GetMatchingParameter(
            string name,
            bool throwOnParameterNotFound,
            InvocationInfo invocationInfo)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw MergedCommandParameterMetadata.tracer.NewArgumentException(nameof(name));
            }
            Collection <MergedCompiledCommandParameter> collection = new Collection <MergedCompiledCommandParameter>();

            if (name.Length > 0 && SpecialCharacters.IsDash(name[0]))
            {
                name = name.Substring(1);
            }
            foreach (string key in this.bindableParameters.Keys)
            {
                if (this.nameCompareInfo.IsPrefix(key, name, CompareOptions.IgnoreCase))
                {
                    MergedCommandParameterMetadata.tracer.WriteLine("Found match: {0}", (object)key);
                    if (string.Equals(key, name, StringComparison.OrdinalIgnoreCase))
                    {
                        return(this.bindableParameters[key]);
                    }
                    collection.Add(this.bindableParameters[key]);
                }
            }
            foreach (string key in this.aliasedParameters.Keys)
            {
                if (this.nameCompareInfo.IsPrefix(key, name, CompareOptions.IgnoreCase))
                {
                    MergedCommandParameterMetadata.tracer.WriteLine("Found match: {0}", (object)key);
                    if (string.Equals(key, name, StringComparison.OrdinalIgnoreCase))
                    {
                        return(this.aliasedParameters[key]);
                    }
                    if (!collection.Contains(this.aliasedParameters[key]))
                    {
                        collection.Add(this.aliasedParameters[key]);
                    }
                }
            }
            if (collection.Count > 1)
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (MergedCompiledCommandParameter commandParameter in collection)
                {
                    stringBuilder.AppendFormat(" -{0}", (object)commandParameter.Parameter.Name);
                }
                ParameterBindingException bindingException = new ParameterBindingException(ErrorCategory.InvalidArgument, invocationInfo, (Token)null, name, (Type)null, (Type)null, "ParameterBinderStrings", "AmbiguousParameter", new object[1]
                {
                    (object)stringBuilder
                });
                MergedCommandParameterMetadata.tracer.TraceException((Exception)bindingException);
                throw bindingException;
            }
            if (collection.Count == 0 && throwOnParameterNotFound)
            {
                ParameterBindingException bindingException = new ParameterBindingException(ErrorCategory.InvalidArgument, invocationInfo, (Token)null, name, (Type)null, (Type)null, "ParameterBinderStrings", "NamedParameterNotFound", new object[0]);
                MergedCommandParameterMetadata.tracer.TraceException((Exception)bindingException);
                throw bindingException;
            }
            MergedCompiledCommandParameter commandParameter1 = (MergedCompiledCommandParameter)null;

            if (collection.Count > 0)
            {
                commandParameter1 = collection[0];
            }
            return(commandParameter1);
        }
예제 #9
0
        private static string ScanForNumber(string input, int offset)
        {
            int startIndex = offset;

            if (offset >= input.Length)
            {
                return((string)null);
            }
            char c1 = input[offset];

            if (!char.IsDigit(c1) && c1 != '.')
            {
                return((string)null);
            }
            bool flag1 = true;
            bool flag2 = false;
            bool flag3 = false;

            if (c1 == '.')
            {
                if (offset + 1 >= input.Length || !char.IsDigit(input[offset + 1]))
                {
                    return((string)null);
                }
                flag1 = false;
            }
            if (c1 == '0' && offset + 1 < input.Length && char.ToUpperInvariant(input[offset + 1]) == 'X')
            {
                ++offset;
                flag2 = true;
                flag1 = false;
            }
            while (++offset < input.Length)
            {
                char c2 = input[offset];
                if (!char.IsDigit(c2) && (!flag2 || char.ToUpperInvariant(c2) > 'F' || char.ToUpperInvariant(c2) < 'A'))
                {
                    if (c2 == '.')
                    {
                        if (flag1 && (offset + 1 >= input.Length || input[offset + 1] != '.'))
                        {
                            flag1 = false;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else if (char.ToUpperInvariant(c2) == 'E' && !flag3)
                    {
                        if (offset + 1 < input.Length && (input[offset + 1] == '+' || SpecialCharacters.IsDash(input[offset + 1])))
                        {
                            ++offset;
                        }
                        flag3 = true;
                        flag1 = false;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (offset < input.Length)
            {
                switch (char.ToUpperInvariant(input[offset]))
                {
                case 'D':
                case 'L':
                    ++offset;
                    break;
                }
            }
            if (NumberTokenReader.IsMultiplier(input, offset))
            {
                offset += 2;
            }
            return(input.Substring(startIndex, offset - startIndex));
        }
예제 #10
0
        private string Match(string input, int offset, out object tokenData)
        {
            tokenData = (object)null;
            int startIndex = offset;

            if (this.Tokenizer.AllowSignedNumber && offset < input.Length && (input[offset] == '+' || SpecialCharacters.IsDash(input[offset])))
            {
                ++offset;
            }
            string str1 = NumberTokenReader.ScanForNumber(input, offset);

            if (string.IsNullOrEmpty(str1))
            {
                return((string)null);
            }
            offset += str1.Length;
            if (offset < input.Length)
            {
                if (this.Tokenizer.Mode == ParseMode.Command)
                {
                    if ("{}();,|&\r\n\t ".IndexOf(input[offset]) == -1)
                    {
                        return((string)null);
                    }
                }
                else if (char.IsLetterOrDigit(input[offset]))
                {
                    if (this.Tokenizer.Mode == ParseMode.Expression)
                    {
                        this.Tokenizer.Parser.ReportException((object)str1, typeof(ParseException), this.Tokenizer.PositionToken(offset), "BadNumericConstant", (object)str1);
                    }
                    return((string)null);
                }
            }
            try
            {
                string str2 = input.Substring(startIndex, offset - startIndex);
                tokenData = NumberTokenReader.ConvertToNumber(str2, this.Tokenizer);
                return(str2);
            }
            catch (RuntimeException ex)
            {
                if (this.Tokenizer.Mode != ParseMode.Expression)
                {
                    return((string)null);
                }
                if (ex.ErrorRecord != null)
                {
                    ex.ErrorRecord.SetInvocationInfo(new InvocationInfo((CommandInfo)null, this.Tokenizer.PositionToken(offset)));
                }
                throw;
            }
        }