コード例 #1
0
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-nocommands", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-nofunctions", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-nopolicies", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-notraces", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-noprovide", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-noresources", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-verifiedonly", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-trustedonly", null),
#if ISOLATED_PLUGINS
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-noisolated", null),
#else
                            new Option(null, OptionFlags.Unsafe | OptionFlags.Unsupported, Index.Invalid, Index.Invalid,
                                       "-noisolated", null),
#endif
                            new Option(null, OptionFlags.MustHaveObjectValue, Index.Invalid, Index.Invalid, "-clientdata", null),
                            new Option(null, OptionFlags.MustHaveObjectValue, Index.Invalid, Index.Invalid, "-data", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                        });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            //
                            // NOTE: There should be a minimum of one and a maximum
                            //       of three arguments after the final option.
                            //
                            if ((argumentIndex != Index.Invalid) &&
                                ((argumentIndex + 3) >= arguments.Count))
                            {
                                string path = ((argumentIndex + 2) < arguments.Count) ?
                                              (string)arguments[argumentIndex + 2] : String.Empty;

                                Interpreter slaveInterpreter = null;

                                code = interpreter.GetNestedSlaveInterpreter(
                                    path, LookupFlags.Interpreter, false,
                                    ref slaveInterpreter, ref result);

                                if (code == ReturnCode.Ok)
                                {
                                    Variant     value           = null;
                                    IClientData localClientData = clientData;

                                    if (options.IsPresent("-clientdata", ref value))
                                    {
                                        IObject @object = (IObject)value.Value;

                                        if ((@object.Value == null) ||
                                            (@object.Value is IClientData))
                                        {
                                            localClientData = (IClientData)@object.Value;
                                        }
                                        else
                                        {
                                            result = "option value has invalid clientData";
                                            code   = ReturnCode.Error;
                                        }
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        if (options.IsPresent("-data", ref value))
                                        {
                                            IObject @object = (IObject)value.Value;

                                            if (@object != null)
                                            {
                                                localClientData = _Public.ClientData.WrapOrReplace(
                                                    localClientData, @object.Value);
                                            }
                                            else
                                            {
                                                result = "option value has invalid data";
                                                code   = ReturnCode.Error;
                                            }
                                        }

                                        if (code == ReturnCode.Ok)
                                        {
                                            //
                                            // NOTE: All plugins loaded by this command are considered
                                            //       as having been loaded "on demand".
                                            //
                                            PluginFlags pluginFlags = PluginFlags.Demand;

                                            //
                                            // NOTE: Add the plugin flags for the target interpreter.
                                            //
                                            pluginFlags |= slaveInterpreter.PluginFlags;

#if ISOLATED_PLUGINS
                                            //
                                            // NOTE: Disable loading this plugin into an isolated
                                            //       application domain (i.e. load it into the default
                                            //       application domain for the target interpreter).
                                            //
                                            if (options.IsPresent("-noisolated"))
                                            {
                                                pluginFlags &= ~PluginFlags.Isolated;
                                            }
#endif

                                            if (options.IsPresent("-nocommands"))
                                            {
                                                pluginFlags |= PluginFlags.NoCommands;
                                            }

                                            if (options.IsPresent("-nofunctions"))
                                            {
                                                pluginFlags |= PluginFlags.NoFunctions;
                                            }

                                            if (options.IsPresent("-nopolicies"))
                                            {
                                                pluginFlags |= PluginFlags.NoPolicies;
                                            }

                                            if (options.IsPresent("-notraces"))
                                            {
                                                pluginFlags |= PluginFlags.NoTraces;
                                            }

                                            if (options.IsPresent("-noprovide"))
                                            {
                                                pluginFlags |= PluginFlags.NoProvide;
                                            }

                                            if (options.IsPresent("-noresources"))
                                            {
                                                pluginFlags |= PluginFlags.NoResources;
                                            }

                                            if (options.IsPresent("-verifiedonly"))
                                            {
                                                pluginFlags |= PluginFlags.VerifiedOnly;
                                            }

                                            if (options.IsPresent("-trustedonly"))
                                            {
                                                pluginFlags |= PluginFlags.TrustedOnly;
                                            }

                                            string fileName = PathOps.ResolveFullPath(
                                                interpreter, arguments[argumentIndex]);

                                            if (!String.IsNullOrEmpty(fileName))
                                            {
                                                string typeName = null;

                                                if ((argumentIndex + 1) < arguments.Count)
                                                {
                                                    typeName = arguments[argumentIndex + 1];
                                                }

                                                IPlugin plugin = null;
                                                long    token  = 0;

                                                try
                                                {
                                                    code = slaveInterpreter.LoadPlugin(
                                                        fileName,
#if CAS_POLICY
                                                        null, null, AssemblyHashAlgorithm.None,
#endif
                                                        typeName, localClientData, pluginFlags,
                                                        ref plugin, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        code = slaveInterpreter.AddPlugin(
                                                            plugin, localClientData, ref token,
                                                            ref result);
                                                    }
                                                }
                                                finally
                                                {
                                                    if (code != ReturnCode.Ok)
                                                    {
                                                        if (token != 0)
                                                        {
                                                            //
                                                            // NOTE: Terminate and remove the plugin now.
                                                            //       This does not unload the associated
                                                            //       AppDomain, if any.
                                                            //
                                                            ReturnCode removeCode;
                                                            Result     removeResult = null;

                                                            removeCode = slaveInterpreter.RemovePlugin(
                                                                token, localClientData, ref removeResult);

                                                            if (removeCode != ReturnCode.Ok)
                                                            {
                                                                DebugOps.Complain(
                                                                    slaveInterpreter, removeCode,
                                                                    removeResult);
                                                            }
                                                        }

                                                        if (plugin != null)
                                                        {
                                                            //
                                                            // NOTE: Unload the plugin.  This basically does
                                                            //       "nothing" unless the plugin was isolated.
                                                            //       In that case, it unloads the associated
                                                            //       AppDomain.
                                                            //
                                                            ReturnCode unloadCode;
                                                            Result     unloadResult = null;

                                                            unloadCode = slaveInterpreter.UnloadPlugin(
                                                                plugin, localClientData, pluginFlags |
                                                                PluginFlags.SkipTerminate, ref unloadResult);

                                                            if (unloadCode != ReturnCode.Ok)
                                                            {
                                                                DebugOps.Complain(
                                                                    slaveInterpreter, unloadCode,
                                                                    unloadResult);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                result = "invalid file name";
                                                code   = ReturnCode.Error;
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"load ?options? fileName ?packageName? ?interp?\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"load ?options? fileName ?packageName? ?interp?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #2
0
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 1)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.MustHaveReturnCodeValue, Index.Invalid, Index.Invalid, "-code", null),
                            new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-errorinfo", null),
                            new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-errorcode", null)
                        });

                        int argumentIndex = Index.Invalid;

                        if (arguments.Count > 1)
                        {
                            code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);
                        }
                        else
                        {
                            code = ReturnCode.Ok;
                        }

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex == Index.Invalid) || ((argumentIndex + 1) == arguments.Count))
                            {
                                //
                                // NOTE: Always reset these first.  They may be set to something
                                //       below.
                                //
                                interpreter.ErrorInfo = null;
                                interpreter.ErrorCode = null;

                                //
                                // NOTE: Initialize the variable that will hold the supplied option
                                //       values, if any.
                                //
                                Variant value = null;

                                //
                                // NOTE: Process the -code option.
                                //
                                if (options.IsPresent("-code", ref value))
                                {
                                    interpreter.ReturnCode = (ReturnCode)value.Value;
                                }
                                else
                                {
                                    interpreter.ReturnCode = ReturnCode.Ok;
                                }

                                //
                                // NOTE: If the -code option indicates an error then we also want to
                                //       process the values for the -errorinfo and -errorcode options.
                                //
                                if (interpreter.ReturnCode == ReturnCode.Error)
                                {
                                    if (options.IsPresent("-errorcode", ref value))
                                    {
                                        interpreter.ErrorCode = value.ToString();
                                        Engine.SetErrorCodeSet(interpreter, true);
                                    }

                                    if (options.IsPresent("-errorinfo", ref value))
                                    {
                                        interpreter.ErrorInfo = value.ToString();
                                        Engine.SetErrorInProgress(interpreter, true);
                                    }
                                }

                                //
                                // NOTE: Is an actual string value being returned?
                                //
                                if (argumentIndex != Index.Invalid)
                                {
                                    result = arguments[argumentIndex];
                                }
                                else
                                {
                                    result = String.Empty;
                                }

                                code = ReturnCode.Return;
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"return ?-code code? ?-errorinfo info? ?-errorcode code? ?string?\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"return ?-code code? ?-errorinfo info? ?-errorcode code? ?string?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #3
0
ファイル: Regexp.cs プロジェクト: jdruin/F5Eagle
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 3)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.Unsupported, Index.Invalid, Index.Invalid, "-about", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-all", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-debug", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-ecma", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-compiled", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-explicit", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-reverse", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-expanded", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-indexes", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-indices", null), /* COMPAT: Tcl. */
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-inline", null),
                            new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-limit", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-line", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-lineanchor", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-linestop", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nocase", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-noculture", null),
                            new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-start", null),
                            new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-length", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                        });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex != Index.Invalid) && ((argumentIndex + 1) < arguments.Count))
                            {
                                bool all     = false;
                                bool debug   = false;
                                bool indexes = false;
                                bool inline  = false;

                                string pattern = arguments[argumentIndex];
                                string input   = arguments[argumentIndex + 1];

                                RegexOptions regExOptions = StringOps.DefaultRegExOptions;

                                Variant value = null;
                                int     limit = Count.Invalid;

                                if (options.IsPresent("-limit", ref value))
                                {
                                    limit = (int)value.Value;
                                }

                                int length = input.Length;

                                if (options.IsPresent("-length", ref value))
                                {
                                    length = (int)value.Value;

                                    if ((length < 0) || (length > input.Length))
                                    {
                                        length = input.Length;
                                    }
                                }

                                int startIndex = 0;

                                if (options.IsPresent("-start", ref value))
                                {
                                    //
                                    // NOTE: Handle "end-X", etc.
                                    //
                                    code = Value.GetIndex(
                                        value.ToString(), length,
                                        ValueFlags.AnyIndex,
                                        interpreter.CultureInfo,
                                        ref startIndex, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        if (startIndex < 0)
                                        {
                                            startIndex = 0;
                                        }

                                        if (startIndex > length)
                                        {
                                            startIndex = length;
                                        }
                                    }
                                }

                                if (code == ReturnCode.Ok)
                                {
                                    if (options.IsPresent("-all"))
                                    {
                                        all = true;
                                    }

                                    if (options.IsPresent("-debug"))
                                    {
                                        debug = true;
                                    }

                                    if (options.IsPresent("-indexes") || options.IsPresent("-indices"))
                                    {
                                        indexes = true;
                                    }

                                    if (options.IsPresent("-inline"))
                                    {
                                        inline = true;
                                    }

                                    if (options.IsPresent("-ecma"))
                                    {
                                        regExOptions |= RegexOptions.ECMAScript;
                                    }

                                    if (options.IsPresent("-compiled"))
                                    {
                                        regExOptions |= RegexOptions.Compiled;
                                    }

                                    if (options.IsPresent("-explicit"))
                                    {
                                        regExOptions |= RegexOptions.ExplicitCapture;
                                    }

                                    if (options.IsPresent("-reverse"))
                                    {
                                        regExOptions |= RegexOptions.RightToLeft;
                                    }

                                    if (options.IsPresent("-expanded"))
                                    {
                                        regExOptions |= RegexOptions.IgnorePatternWhitespace;
                                    }

                                    if (options.IsPresent("-line"))
                                    {
                                        regExOptions &= ~RegexOptions.Singleline;
                                        regExOptions |= RegexOptions.Multiline;
                                    }

                                    if (options.IsPresent("-lineanchor"))
                                    {
                                        regExOptions |= RegexOptions.Multiline;
                                    }

                                    if (options.IsPresent("-linestop"))
                                    {
                                        regExOptions &= ~RegexOptions.Singleline;
                                    }

                                    if (options.IsPresent("-nocase"))
                                    {
                                        regExOptions |= RegexOptions.IgnoreCase;
                                    }

                                    if (options.IsPresent("-noculture"))
                                    {
                                        regExOptions |= RegexOptions.CultureInvariant;
                                    }

                                    if (!inline || ((argumentIndex + 2) >= arguments.Count))
                                    {
                                        Regex regEx = null;

                                        try
                                        {
                                            regEx = new Regex(pattern, regExOptions);
                                        }
                                        catch (Exception e)
                                        {
                                            Engine.SetExceptionErrorCode(interpreter, e);

                                            result = String.Format(
                                                "couldn't compile regular expression pattern: {0}",
                                                e.Message);

                                            code = ReturnCode.Error;
                                        }

                                        //
                                        // NOTE: If the result is still Ok, then we know that the regular
                                        //       expression pattern was compiled and the regEx object was
                                        //       created successfully.
                                        //
                                        if (code == ReturnCode.Ok)
                                        {
                                            //
                                            // NOTE: Inline matches list, only created and populated when
                                            //       we are operating in inline mode.
                                            //
                                            StringList matches = null;

                                            if (inline)
                                            {
                                                //
                                                // NOTE: Inline mode, create an empty inline matches
                                                //       list.
                                                //
                                                matches = new StringList();
                                            }

                                            /*
                                             * The following loop is to handle multiple matches within the
                                             * same source string;  each iteration handles one match.  If "-all"
                                             * hasn't been specified then the loop body only gets executed once.
                                             * We terminate the loop when the starting offset is past the end of the
                                             * string.
                                             */

                                            int   matchIndex  = startIndex;
                                            int   matchLength = length;
                                            int   matchCount  = 0;
                                            Match match       = null;

                                            while (true)
                                            {
                                                //
                                                // NOTE: Perform the regular expresssion matching.  This cannot
                                                //       raise an exception because we know the input argument
                                                //       is valid.
                                                //
                                                if (matchLength < 0)
                                                {
                                                    matchLength = 0;
                                                }

                                                if ((matchIndex + matchLength) > input.Length)
                                                {
                                                    matchLength = input.Length - matchIndex;
                                                }

                                                if (debug)
                                                {
                                                    TraceOps.DebugWriteTo(interpreter, String.Format(
                                                                              "{0}: Trying index {1}, length {2}", this.Name,
                                                                              matchIndex, matchLength), false);
                                                }

                                                if (match != null)
                                                {
                                                    match = match.NextMatch();
                                                }
                                                else
                                                {
                                                    match = regEx.Match(input, matchIndex, matchLength);
                                                }

                                                //
                                                // NOTE: Did the overall match succeed?
                                                //
                                                if (match.Success)
                                                {
                                                    if (debug)
                                                    {
                                                        TraceOps.DebugWriteTo(interpreter, String.Format(
                                                                                  "{0}: Match success {1}", this.Name,
                                                                                  FormatOps.DisplayRegExMatch(match)), false);
                                                    }

                                                    //
                                                    // NOTE: We found another match.
                                                    //
                                                    matchCount++;

                                                    //
                                                    // NOTE: Check if we should return this match.
                                                    //
                                                    if ((limit < 0) || ((limit >= 0) && (matchCount <= limit)))
                                                    {
                                                        //
                                                        // NOTE: Advance the argument index just beyond the
                                                        //       pattern and input arguments.
                                                        //
                                                        int nextArgumentIndex = argumentIndex + 2;

                                                        //
                                                        // NOTE: Process each match group and either set the
                                                        //       corresponding variable value or add it to the
                                                        //       inline result.
                                                        //
                                                        foreach (Group group in match.Groups)
                                                        {
                                                            //
                                                            // NOTE: Having a null group should not happen; but,
                                                            //       just in case it does, skip over them.
                                                            //
                                                            if (group == null)
                                                            {
                                                                continue;
                                                            }

                                                            //
                                                            // NOTE: Set the value for this match group based on
                                                            //       whether we are operating in indexes mode.
                                                            //
                                                            string matchValue;

                                                            if (group.Success)
                                                            {
                                                                if (indexes)
                                                                {
                                                                    //
                                                                    // NOTE: Return the first and last indexes of
                                                                    //       this match group.
                                                                    //
                                                                    matchValue = StringList.MakeList(
                                                                        group.Index, group.Index + group.Length - 1);
                                                                }
                                                                else
                                                                {
                                                                    //
                                                                    // NOTE: Return the value of this match group.
                                                                    //
                                                                    matchValue = group.Value;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (indexes)
                                                                {
                                                                    //
                                                                    // NOTE: Return invalid indexes for this match
                                                                    //       group (we did not match this group).
                                                                    //
                                                                    matchValue = StringList.MakeList(
                                                                        Index.Invalid, Index.Invalid);
                                                                }
                                                                else
                                                                {
                                                                    //
                                                                    // NOTE: Return an empty value for this match
                                                                    //       group (we did not match this group).
                                                                    //
                                                                    matchValue = String.Empty;
                                                                }
                                                            }

                                                            //
                                                            // NOTE: Are we using inline mode?
                                                            //
                                                            if (inline)
                                                            {
                                                                //
                                                                // NOTE: Inline mode, add match value to inline
                                                                //       matches list.
                                                                //
                                                                matches.Add(matchValue);
                                                            }
                                                            else
                                                            {
                                                                //
                                                                // NOTE: If they supplied a variable name for this match
                                                                //       group, attempt to set the variable now.
                                                                //
                                                                if (nextArgumentIndex < arguments.Count)
                                                                {
                                                                    //
                                                                    // NOTE: Potentially re-entrant here due to variable
                                                                    //       traces.
                                                                    //
                                                                    code = interpreter.SetVariableValue(VariableFlags.None,
                                                                                                        arguments[nextArgumentIndex], matchValue, null, ref result);

                                                                    if (code != ReturnCode.Ok)
                                                                    {
                                                                        result = String.Format("couldn't set variable \"{0}\"",
                                                                                               arguments[nextArgumentIndex]);

                                                                        break;
                                                                    }

                                                                    //
                                                                    // NOTE: Advance to the next match variable name, if any.
                                                                    //
                                                                    nextArgumentIndex++;
                                                                }
                                                            }
                                                        }

                                                        //
                                                        // NOTE: If the inner loop failed to set a match variable, break
                                                        //       out of the outer loop as well.
                                                        //
                                                        if (code != ReturnCode.Ok)
                                                        {
                                                            break;
                                                        }

                                                        //
                                                        // NOTE: If we are not in inline mode, fill in any remaining match variables
                                                        //       with an empty string or "-1 -1" if we are in indexes mode.
                                                        //
                                                        if (!inline)
                                                        {
                                                            for (; nextArgumentIndex < arguments.Count; nextArgumentIndex++)
                                                            {
                                                                //
                                                                // NOTE: Potentially re-entrant here due to variable
                                                                //       traces.
                                                                //
                                                                string matchValue;

                                                                if (indexes)
                                                                {
                                                                    //
                                                                    // NOTE: Return invalid indexes for this match
                                                                    //       group (we did not match this group).
                                                                    //
                                                                    matchValue = StringList.MakeList(
                                                                        Index.Invalid, Index.Invalid);
                                                                }
                                                                else
                                                                {
                                                                    //
                                                                    // NOTE: Return an empty value for this match
                                                                    //       group (we did not match this group).
                                                                    //
                                                                    matchValue = String.Empty;
                                                                }

                                                                code = interpreter.SetVariableValue(VariableFlags.None,
                                                                                                    arguments[nextArgumentIndex], matchValue, null, ref result);

                                                                if (code != ReturnCode.Ok)
                                                                {
                                                                    result = String.Format("couldn't set variable \"{0}\"",
                                                                                           arguments[nextArgumentIndex]);

                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    if (debug)
                                                    {
                                                        TraceOps.DebugWriteTo(interpreter, String.Format(
                                                                                  "{0}: Match failure", this.Name), false);
                                                    }

                                                    //
                                                    // NOTE: We are done matching.
                                                    //
                                                    break;
                                                }

                                                //
                                                // NOTE: Only keep going if we want all matches.
                                                //
                                                if (!all)
                                                {
                                                    break;
                                                }

                                                //
                                                // NOTE: Adjust the match index and length to remove what we have
                                                //       already matched.
                                                //
                                                Group groupZero = RegExOps.GetMatchGroup(match, 0);

                                                if (groupZero == null)
                                                {
                                                    result = String.Format(
                                                        "cannot advance beyond {0} / {1}: group zero missing",
                                                        matchIndex, matchLength);

                                                    code = ReturnCode.Error;
                                                    break;
                                                }

                                                if ((groupZero.Index + groupZero.Length) == matchIndex)
                                                {
                                                    matchIndex++;
                                                }
                                                else
                                                {
                                                    matchIndex = (groupZero.Index + groupZero.Length);
                                                }

                                                //
                                                // NOTE: Did we run out of input string to match against?
                                                //
                                                if (matchIndex >= length)
                                                {
                                                    break;
                                                }
                                            }

                                            //
                                            // NOTE: If we did not encounter an error above (setting a match variable),
                                            //       we will now set the overall command result based on whether or not
                                            //       we are using inline mode.
                                            //
                                            if (code == ReturnCode.Ok)
                                            {
                                                if (inline)
                                                {
                                                    result = matches;
                                                }
                                                else
                                                {
                                                    result = (all ? matchCount : ConversionOps.ToInt(matchCount != 0));
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        result = "regexp match variables not allowed when using -inline";

                                        code = ReturnCode.Error;
                                    }
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #4
0
ファイル: Lsort.cs プロジェクト: jdruin/F5Eagle
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.None, 1, Index.Invalid, "-ascii", null),
                            new Option(null, OptionFlags.None, 1, Index.Invalid, "-dictionary", null),
                            new Option(null, OptionFlags.None, 1, Index.Invalid, "-integer", null),
                            new Option(null, OptionFlags.None, 1, Index.Invalid, "-random", null),
                            new Option(null, OptionFlags.None, 1, Index.Invalid, "-real", null),
                            new Option(null, OptionFlags.None, 2, Index.Invalid, "-increasing", null),
                            new Option(null, OptionFlags.None, 2, Index.Invalid, "-decreasing", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nocase", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-unique", null),
                            new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-command", null),
                            new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-index", null) // NOTE: Of sub-lists, not list.
                        });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex != Index.Invalid) && ((argumentIndex + 1) == arguments.Count))
                            {
                                StringList list = null;

                                //
                                // WARNING: Cannot cache list representation here, the list
                                //          is modified below.
                                //
                                code = Parser.SplitList(
                                    interpreter, arguments[argumentIndex], 0,
                                    Length.Invalid, false, ref list, ref result);

                                if (code == ReturnCode.Ok)
                                {
                                    Variant value     = null;
                                    string  indexText = null;

                                    if (options.IsPresent("-index", ref value))
                                    {
                                        indexText = value.ToString();
                                    }

                                    bool ascending = true; // FIXME: PRI 5: Default handling.

                                    if (options.IsPresent("-decreasing"))
                                    {
                                        ascending = false;
                                    }
                                    else if (options.IsPresent("-increasing"))
                                    {
                                        ascending = true;
                                    }

                                    bool noCase = false;

                                    if (options.IsPresent("-nocase"))
                                    {
                                        noCase = true;
                                    }

                                    bool unique = false;

                                    if (options.IsPresent("-unique"))
                                    {
                                        unique = true;
                                    }

                                    IntDictionary      duplicates = null;
                                    IComparer <string> comparer   = null;

                                    if (options.IsPresent("-command", ref value))
                                    {
                                        StringList callbackArguments = null;

                                        if (value.IsList())
                                        {
                                            callbackArguments = (StringList)value.Value;
                                        }
                                        else
                                        {
                                            string temporary = value.ToString();

                                            code = Parser.SplitList(
                                                interpreter, temporary, 0, Length.Invalid,
                                                true, ref callbackArguments);
                                        }

                                        if (code == ReturnCode.Ok)
                                        {
                                            ICallback callback = CommandCallback.Create(
                                                MarshalFlags.Default, CallbackFlags.Default,
                                                ObjectFlags.Callback, ByRefArgumentFlags.None,
                                                interpreter, _Public.ClientData.Empty, null,
                                                callbackArguments, ref result);

                                            if (callback != null)
                                            {
                                                comparer = new _Comparers.StringCommandComparer(
                                                    interpreter, callback, ascending, indexText, false,
                                                    unique, interpreter.CultureInfo, ref duplicates);
                                            }
                                            else
                                            {
                                                code = ReturnCode.Error;
                                            }
                                        }
                                    }
                                    else if (options.IsPresent("-dictionary"))
                                    {
                                        comparer = new _Comparers.StringDictionaryComparer(
                                            interpreter, ascending, indexText, false, unique,
                                            interpreter.CultureInfo, ref duplicates);
                                    }
                                    else if (options.IsPresent("-integer"))
                                    {
                                        comparer = new _Comparers.StringIntegerComparer(
                                            interpreter, ascending, indexText, false, unique,
                                            interpreter.CultureInfo, ref duplicates);
                                    }
                                    else if (options.IsPresent("-random"))
                                    {
                                        comparer = new _Comparers.StringRandomComparer(
                                            interpreter, ascending, indexText, false, unique,
                                            interpreter.CultureInfo, interpreter.RandomNumberGenerator,
                                            ref duplicates);
                                    }
                                    else if (options.IsPresent("-real"))
                                    {
                                        comparer = new _Comparers.StringRealComparer(
                                            interpreter, ascending, indexText, false, unique,
                                            interpreter.CultureInfo, ref duplicates);
                                    }
                                    else if (options.IsPresent("-ascii") || true) // FIXME: PRI 5: Default handling.
                                    {
                                        comparer = new _Comparers.StringAsciiComparer(
                                            interpreter, ascending, indexText, false, noCase,
                                            unique, interpreter.CultureInfo, ref duplicates);
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        try
                                        {
                                            lock (interpreter.SyncRoot) /* TRANSACTIONAL */
                                            {
                                                if (comparer != null)
                                                {
                                                    list.Sort(comparer);
                                                }
                                                else
                                                {
                                                    //
                                                    // FIXME: This will never be hit because we always default
                                                    //        to using the StringAsciiComparer (above).
                                                    //
                                                    list.Sort(); // use .NET Framework defaults
                                                }
                                            }

                                            //
                                            // NOTE: If we are in unique mode, remove any duplicates from
                                            //       the final resulting list now.
                                            //
                                            if (unique)
                                            {
                                                StringList uniqueList = new StringList();

                                                //
                                                // NOTE: Process each element in the list to see if it has
                                                //       been counted as a duplicate value by the comparer.
                                                //
                                                //       If the value has not been added to the final resulting
                                                //       list yet, add it now and mark the value so that it will
                                                //       never be added again (i.e. we only want the first value
                                                //       from every group of duplicates and we want all the other
                                                //       values as well).
                                                //
                                                // HACK: In the worst possible case, this loop can have a runtime
                                                //       of O(N^2), including called functions, primarily due to
                                                //       the inability of .NET to provide proper context to
                                                //       IComparer callbacks.  This code could be avoided entirely
                                                //       if there was an interface for sorting comparison callbacks
                                                //       that provided the indexes of the elements being compared
                                                //       in addition to their values.
                                                //
                                                foreach (string element in list) /* O(N) */
                                                {
                                                    //
                                                    // NOTE: Has this value been marked as having been previously
                                                    //       added to the final resulting list?
                                                    //
                                                    int count =
                                                        ListOps.GetDuplicateCount(comparer, duplicates, element);

                                                    if (count != Count.Invalid)
                                                    {
                                                        //
                                                        // NOTE: Add this element into the final resulting list.
                                                        //       Either it has no duplicates or we have not yet
                                                        //       added it to the final resulting list.
                                                        //
                                                        uniqueList.Add(element);

                                                        //
                                                        // NOTE: If this value had any duplicates, mark the value
                                                        //       as having been added to the final resulting list.
                                                        //
                                                        if (!ListOps.SetDuplicateCount(comparer, duplicates, element, Count.Invalid))
                                                        {
                                                            result = String.Format(
                                                                "failed to update duplicate count for element \"{0}\"",
                                                                element);

                                                            code = ReturnCode.Error;
                                                            break;
                                                        }
                                                    }
                                                }

                                                //
                                                // NOTE: The list of unique elements is now the result.
                                                //
                                                if (code == ReturnCode.Ok)
                                                {
                                                    list = uniqueList;
                                                }
                                            }

                                            if (code == ReturnCode.Ok)
                                            {
                                                result = list;
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            Engine.SetExceptionErrorCode(interpreter, e);

                                            if (e.InnerException != null)
                                            {
                                                result = e.InnerException.Message;
                                            }
                                            else if (e is ScriptException)
                                            {
                                                result = e.Message;
                                            }
                                            else
                                            {
                                                result = e;
                                            }

                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"lsort ?options? list\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"lsort ?options? list\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #5
0
        ////////////////////////////////////////////////////////////////////////

        #region IExecute Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            if (interpreter == null)
            {
                result = "invalid interpreter";
                return(ReturnCode.Error);
            }

            _Cmdlets.Script script = GetScriptCmdlet(clientData, ref result);

            if (script == null)
            {
                return(ReturnCode.Error);
            }

            if (arguments == null)
            {
                result = "invalid argument list";
                return(ReturnCode.Error);
            }

            if (arguments.Count < 2)
            {
                result = String.Format(
                    "wrong # args: should be \"{0} option ?arg ...?\"",
                    this.Name);

                return(ReturnCode.Error);
            }

            ReturnCode code       = ReturnCode.Ok;
            string     subCommand = arguments[1];
            bool       tried      = false;

            code = Utility.TryExecuteSubCommandFromEnsemble(
                interpreter, this, clientData, arguments, true,
                false, ref subCommand, ref tried, ref result);

            if ((code == ReturnCode.Ok) && !tried)
            {
                switch (subCommand)
                {
                case "about":
                {
                    if (arguments.Count == 2)
                    {
                        IPlugin plugin = this.Plugin;

                        if (plugin != null)
                        {
                            code = plugin.About(
                                interpreter, ref result);
                        }
                        else
                        {
                            result = "invalid command plugin";
                            code   = ReturnCode.Error;
                        }
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1}\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                case "debug":
                {
                    if (arguments.Count == 3)
                    {
                        try
                        {
                            script.WriteDebug(arguments[2]);         /* throw */
                            result = String.Empty;
                        }
                        catch (Exception e)
                        {
                            Engine.SetExceptionErrorCode(interpreter, e);

                            result = e;
                            code   = ReturnCode.Error;
                        }
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1} text\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                case "error":
                {
                    if (arguments.Count == 4)
                    {
                        object enumValue = Utility.TryParseEnum(
                            typeof(ReturnCode), arguments[2],
                            true, true, ref result);

                        if (enumValue is ReturnCode)
                        {
                            try
                            {
                                script.WriteErrorRecord(
                                    (ReturnCode)enumValue,
                                    arguments[3]);         /* throw */

                                result = String.Empty;
                            }
                            catch (Exception e)
                            {
                                Engine.SetExceptionErrorCode(interpreter, e);

                                result = e;
                                code   = ReturnCode.Error;
                            }
                        }
                        else
                        {
                            code = ReturnCode.Error;
                        }
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1} code result\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                case "invoke":
                {
                    if (arguments.Count >= 3)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                                new Option(null, OptionFlags.None, Index.Invalid,
                                           Index.Invalid, "-addtohistory", null)
                            }, Utility.GetFixupReturnValueOptions().Values);

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 2,
                                                      Index.Invalid, true, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex != Index.Invalid) &&
                                ((argumentIndex + 1) == arguments.Count))
                            {
                                Type        returnType;
                                ObjectFlags objectFlags;
                                string      objectName;
                                string      interpName;
                                bool        create;
                                bool        dispose;
                                bool        alias;
                                bool        aliasRaw;
                                bool        aliasAll;
                                bool        aliasReference;
                                bool        toString;

                                Utility.ProcessFixupReturnValueOptions(
                                    options, null, out returnType, out objectFlags,
                                    out objectName, out interpName, out create,
                                    out dispose, out alias, out aliasRaw,
                                    out aliasAll, out aliasReference, out toString);

                                bool addToHistory = false;

                                if (options.IsPresent("-addtohistory"))
                                {
                                    addToHistory = true;
                                }

                                Collection <PSObject> returnValue = null;

                                try
                                {
                                    code = InvokePipeline(
                                        arguments[argumentIndex], addToHistory,
                                        ref returnValue, ref result);
                                }
                                catch (Exception e)
                                {
                                    Engine.SetExceptionErrorCode(interpreter, e);

                                    result = e;
                                    code   = ReturnCode.Error;
                                }

                                if (code == ReturnCode.Ok)
                                {
                                    ObjectOptionType objectOptionType =
                                        Utility.GetOptionType(aliasRaw, aliasAll);

                                    code = Utility.FixupReturnValue(interpreter,
                                                                    GetBinder(interpreter, this.Plugin),
                                                                    interpreter.CultureInfo, returnType,
                                                                    objectFlags, Utility.GetInvokeOptions(
                                                                        objectOptionType), objectOptionType,
                                                                    objectName, interpName, returnValue,
                                                                    create, dispose, alias, aliasReference,
                                                                    toString, ref result);
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(
                                        options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = String.Format(
                                        "wrong # args: should be \"{0} {1} ?options? script\"",
                                        this.Name, subCommand);
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1} ?options? script\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                case "options":
                {
                    if (arguments.Count == 2)
                    {
                        IPlugin plugin = this.Plugin;

                        if (plugin != null)
                        {
                            code = plugin.Options(
                                interpreter, ref result);
                        }
                        else
                        {
                            //
                            // NOTE: There is (normally) no plugin
                            //       context for this library.
                            //
                            code = GetDefineConstants(ref result);
                        }
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1}\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                case "progress":
                {
                    if (arguments.Count >= 5)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                                new Option(null, OptionFlags.MustHaveValue |
                                           OptionFlags.NoCase, Index.Invalid, Index.Invalid,
                                           "-currentOperation", null),
                                new Option(null, OptionFlags.MustHaveIntegerValue |
                                           OptionFlags.NoCase, Index.Invalid, Index.Invalid,
                                           "-parentActivityId", null),
                                new Option(null, OptionFlags.MustHaveIntegerValue |
                                           OptionFlags.NoCase, Index.Invalid, Index.Invalid,
                                           "-percentComplete", null),
                                new Option(typeof(ProgressRecordType),
                                           OptionFlags.MustHaveIntegerValue | OptionFlags.NoCase,
                                           Index.Invalid, Index.Invalid, "-recordType",
                                           new Variant((ProgressRecordType)(-1))),
                                new Option(null, OptionFlags.MustHaveIntegerValue |
                                           OptionFlags.NoCase, Index.Invalid, Index.Invalid,
                                           "-secondsRemaining", null),
                                new Option(null, OptionFlags.None, Index.Invalid,
                                           Index.Invalid, Option.EndOfOptions, null)
                            });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 2,
                                                      Index.Invalid, true, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex != Index.Invalid) &&
                                ((argumentIndex + 3) == arguments.Count))
                            {
                                Variant value            = null;
                                string  currentOperation = null;

                                if (options.IsPresent("-currentOperation", true, ref value))
                                {
                                    currentOperation = value.ToString();
                                }

                                int parentActivityId = Identifier.Invalid;

                                if (options.IsPresent("-parentActivityId", true, ref value))
                                {
                                    parentActivityId = (int)value.Value;
                                }

                                int percentComplete = Percent.Invalid;

                                if (options.IsPresent("-percentComplete", true, ref value))
                                {
                                    percentComplete = (int)value.Value;
                                }

                                ProgressRecordType recordType = (ProgressRecordType)(-1);

                                if (options.IsPresent("-recordType", true, ref value))
                                {
                                    recordType = (ProgressRecordType)value.Value;
                                }

                                int secondsRemaining = Count.Invalid;

                                if (options.IsPresent("-secondsRemaining", true, ref value))
                                {
                                    secondsRemaining = (int)value.Value;
                                }

                                int activityId = Identifier.Invalid;

                                code = Value.GetInteger2(
                                    (IGetValue)arguments[argumentIndex], ValueFlags.AnyInteger,
                                    interpreter.CultureInfo, ref activityId, ref result);

                                if (code == ReturnCode.Ok)
                                {
                                    try
                                    {
                                        ProgressRecord progressRecord = new ProgressRecord(
                                            activityId, arguments[argumentIndex + 1],
                                            arguments[argumentIndex + 2]);         /* throw */

                                        if (currentOperation != null)
                                        {
                                            progressRecord.CurrentOperation = currentOperation;
                                        }

                                        if (parentActivityId != Identifier.Invalid)
                                        {
                                            progressRecord.ParentActivityId = parentActivityId;         /* throw */
                                        }
                                        if (percentComplete != Percent.Invalid)
                                        {
                                            progressRecord.PercentComplete = percentComplete;         /* throw */
                                        }
                                        if (recordType != (ProgressRecordType)(-1))
                                        {
                                            progressRecord.RecordType = recordType;         /* throw */
                                        }
                                        if (secondsRemaining != Count.Invalid)
                                        {
                                            progressRecord.SecondsRemaining = secondsRemaining;
                                        }

                                        script.WriteProgress(progressRecord);         /* throw */

                                        result = String.Empty;
                                    }
                                    catch (Exception e)
                                    {
                                        Engine.SetExceptionErrorCode(interpreter, e);

                                        result = e;
                                        code   = ReturnCode.Error;
                                    }
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(
                                        options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = String.Format(
                                        "wrong # args: should be \"{0} {1} ?options? activityId activity statusDescription\"",
                                        this.Name, subCommand);
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1} ?options? activityId activity statusDescription\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                case "remove":
                {
                    if (arguments.Count == 2)
                    {
                        code = script.RemoveMetaCommand(interpreter, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            result = String.Empty;
                        }
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1}\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                case "status":
                {
                    if (arguments.Count == 2)
                    {
                        result = StringList.MakeList(
                            "Disposed", script.Disposed,         /* PEDANTIC */
                            "FlagsCallback", script.FlagsCallback,
                            "StateCallback", script.StateCallback,
                            "ParameterCallback", script.ParameterCallback,
                            "Listener", script.Listener,
                            "PreInitialize", script.PreInitialize,
                            "CreateFlags", script.CreateFlags,
                            "EngineFlags", script.EngineFlags,
                            "SubstitutionFlags", script.SubstitutionFlags,
                            "EventFlags", script.EventFlags,
                            "ExpressionFlags", script.ExpressionFlags,
                            "Console", script.Console,
                            "Unsafe", script.Unsafe,
                            "Standard", script.Standard,
                            "Force", script.Force,
                            "Exceptions", script.Exceptions,
                            "Policies", script.Policies,
                            "Deny", script.Deny,
                            "MetaCommand", script.MetaCommand,
                            "Text", script.Text,
                            "Interpreter", script.Interpreter,
                            "Tokens", script.Tokens,
                            "CommandRuntime", script.CommandRuntime,
                            "Stopping", script.Stopping);         /* throw */
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1}\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                case "verbose":
                {
                    if (arguments.Count == 3)
                    {
                        try
                        {
                            script.WriteVerbose(arguments[2]);         /* throw */
                            result = String.Empty;
                        }
                        catch (Exception e)
                        {
                            Engine.SetExceptionErrorCode(interpreter, e);

                            result = e;
                            code   = ReturnCode.Error;
                        }
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1} text\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                default:
                {
                    result = Utility.BadSubCommand(
                        interpreter, null, null, subCommand, this, null, null);

                    code = ReturnCode.Error;
                    break;
                }
                }
            }

            return(code);
        }
コード例 #6
0
        ///////////////////////////////////////////////////////////////////////

        #region IExecute Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            if (interpreter == null)
            {
                result = "invalid interpreter";
                return(ReturnCode.Error);
            }

            if (arguments == null)
            {
                result = "invalid argument list";
                return(ReturnCode.Error);
            }

            if (arguments.Count < 2)
            {
                result = "wrong # args: should be \"source ?options? fileName\"";
                return(ReturnCode.Error);
            }

            ReturnCode code = ReturnCode.Ok;

            OptionDictionary options = new OptionDictionary(
                new IOption[] {
                new Option(null, OptionFlags.MustHaveEncodingValue,
                           Index.Invalid, Index.Invalid, "-encoding", null),
                new Option(null, OptionFlags.MustHaveBooleanValue,
                           Index.Invalid, Index.Invalid, "-withinfo", null),
                new Option(null, OptionFlags.None, Index.Invalid,
                           Index.Invalid, Option.EndOfOptions, null)
            });

            int argumentIndex = Index.Invalid;

            if (arguments.Count > 2)
            {
                if (interpreter.GetOptions(
                        options, arguments, 0, 1, Index.Invalid, false,
                        ref argumentIndex, ref result) != ReturnCode.Ok)
                {
                    return(ReturnCode.Error);
                }
            }
            else
            {
                argumentIndex = 1;
            }

            if ((argumentIndex == Index.Invalid) ||
                ((argumentIndex + 1) != arguments.Count))
            {
                if ((argumentIndex != Index.Invalid) &&
                    Option.LooksLikeOption(arguments[argumentIndex]))
                {
                    result = OptionDictionary.BadOption(
                        options, arguments[argumentIndex]);
                }
                else
                {
                    result = "wrong # args: should be \"source ?options? fileName\"";
                }

                return(ReturnCode.Error);
            }

            Variant  value    = null;
            Encoding encoding = null;

            if (options.IsPresent("-encoding", ref value))
            {
                encoding = (Encoding)value.Value;
            }

            bool withInfo = false;

            if (options.IsPresent("-withinfo", ref value))
            {
                withInfo = (bool)value.Value;
            }

            if (code == ReturnCode.Ok)
            {
                string name = StringList.MakeList(
                    "source", arguments[argumentIndex]);

                ICallFrame frame = interpreter.NewTrackingCallFrame(
                    name, CallFrameFlags.Source);

                interpreter.PushAutomaticCallFrame(frame);

                try
                {
#if ARGUMENT_CACHE
                    CacheFlags savedCacheFlags = CacheFlags.None;

                    if (withInfo)
                    {
                        interpreter.BeginNoArgumentCache(
                            ref savedCacheFlags);
                    }

                    try
                    {
#endif
#if DEBUGGER && BREAKPOINTS
                    InterpreterFlags savedInterpreterFlags =
                        InterpreterFlags.None;

                    if (withInfo)
                    {
                        interpreter.BeginArgumentLocation(
                            ref savedInterpreterFlags);
                    }

                    try
                    {
#endif

                    code = interpreter.EvaluateFile(
                        encoding, arguments[argumentIndex],
                        ref result);
#if DEBUGGER && BREAKPOINTS
                }
                finally
                {
                    if (withInfo)
                    {
                        interpreter.EndArgumentLocation(
                            ref savedInterpreterFlags);
                    }
                }
#endif
#if ARGUMENT_CACHE
                }
                finally
                {
                    if (withInfo)
                    {
                        interpreter.EndNoArgumentCache(
                            ref savedCacheFlags);
                    }
                }
#endif
                }
                finally
                {
                    //
                    // NOTE: Pop the original call frame that we pushed above
                    //       and any intervening scope call frames that may be
                    //       leftover (i.e. they were not explicitly closed).
                    //
                    /* IGNORED */
                    interpreter.PopScopeCallFramesAndOneMore();
                }
            }

            return(code);
        }
コード例 #7
0
        ///////////////////////////////////////////////////////////////////////

        #region IExecute Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        string subCommand = arguments[1];
                        bool   tried      = false;

                        code = ScriptOps.TryExecuteSubCommandFromEnsemble(
                            interpreter, this, clientData, arguments, true,
                            false, ref subCommand, ref tried, ref result);

                        if ((code == ReturnCode.Ok) && !tried)
                        {
                            if (code == ReturnCode.Ok)
                            {
                                switch (subCommand)
                                {
                                case "command":
                                {
                                    if (arguments.Count >= 3)
                                    {
                                        OptionDictionary options = new OptionDictionary(
                                            new IOption[] {
                                                new Option(typeof(EngineFlags), OptionFlags.MustHaveEnumValue | OptionFlags.Unsafe,
                                                           Index.Invalid, Index.Invalid, "-engineflags", new Variant(interpreter.EngineFlags)),
                                                new Option(typeof(SubstitutionFlags), OptionFlags.MustHaveEnumValue, Index.Invalid, Index.Invalid, "-substitutionflags",
                                                           new Variant(interpreter.SubstitutionFlags)),
                                                new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-startindex", null),
                                                new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-characters", null),
                                                new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-nested", null),
                                                new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-noready", null),
                                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                            });

                                        int argumentIndex = Index.Invalid;

                                        code = interpreter.GetOptions(
                                            options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex,
                                            ref result);

                                        if (code == ReturnCode.Ok)
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                ((argumentIndex + 1) == arguments.Count))
                                            {
                                                Variant     value       = null;
                                                EngineFlags engineFlags = interpreter.EngineFlags;

                                                if (options.IsPresent("-engineflags", ref value))
                                                {
                                                    engineFlags = (EngineFlags)value.Value;
                                                }

                                                SubstitutionFlags substitutionFlags = interpreter.SubstitutionFlags;

                                                if (options.IsPresent("-substitutionflags", ref value))
                                                {
                                                    substitutionFlags = (SubstitutionFlags)value.Value;
                                                }

                                                int startIndex = 0;

                                                if (options.IsPresent("-startindex", ref value))
                                                {
                                                    startIndex = (int)value.Value;
                                                }

                                                int characters = arguments[argumentIndex].Length;

                                                if (options.IsPresent("-characters", ref value))
                                                {
                                                    characters = (int)value.Value;
                                                }

                                                bool nested = false;

                                                if (options.IsPresent("-nested", ref value))
                                                {
                                                    nested = (bool)value.Value;
                                                }

                                                bool noReady = false;

                                                if (options.IsPresent("-noready", ref value))
                                                {
                                                    noReady = (bool)value.Value;
                                                }

                                                IParseState state = new ParseState(
                                                    engineFlags, substitutionFlags);

                                                code = Parser.ParseCommand(
                                                    interpreter, arguments[argumentIndex],
                                                    startIndex, characters, nested, state,
                                                    noReady, ref result);

                                                if (code == ReturnCode.Ok)
                                                {
                                                    //
                                                    // NOTE: Success, return the entire
                                                    //       state as a string.
                                                    //
                                                    result = state.ToString();
                                                }
                                            }
                                            else
                                            {
                                                if ((argumentIndex != Index.Invalid) &&
                                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                                {
                                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                                }
                                                else
                                                {
                                                    result = "wrong # args: should be \"parse command ?options? text\"";
                                                }

                                                code = ReturnCode.Error;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        result = "wrong # args: should be \"parse command ?options? text\"";
                                        code   = ReturnCode.Error;
                                    }
                                    break;
                                }

                                case "expression":
                                {
                                    if (arguments.Count >= 3)
                                    {
                                        OptionDictionary options = new OptionDictionary(
                                            new IOption[] {
                                                new Option(typeof(EngineFlags), OptionFlags.MustHaveEnumValue | OptionFlags.Unsafe,
                                                           Index.Invalid, Index.Invalid, "-engineflags", new Variant(interpreter.EngineFlags)),
                                                new Option(typeof(SubstitutionFlags), OptionFlags.MustHaveEnumValue, Index.Invalid, Index.Invalid, "-substitutionflags",
                                                           new Variant(interpreter.SubstitutionFlags)),
                                                new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-startindex", null),
                                                new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-characters", null),
                                                new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-noready", null),
                                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                            });

                                        int argumentIndex = Index.Invalid;

                                        code = interpreter.GetOptions(
                                            options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex,
                                            ref result);

                                        if (code == ReturnCode.Ok)
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                ((argumentIndex + 1) == arguments.Count))
                                            {
                                                Variant     value       = null;
                                                EngineFlags engineFlags = interpreter.EngineFlags;

                                                if (options.IsPresent("-engineflags", ref value))
                                                {
                                                    engineFlags = (EngineFlags)value.Value;
                                                }

                                                SubstitutionFlags substitutionFlags = interpreter.SubstitutionFlags;

                                                if (options.IsPresent("-substitutionflags", ref value))
                                                {
                                                    substitutionFlags = (SubstitutionFlags)value.Value;
                                                }

                                                int startIndex = 0;

                                                if (options.IsPresent("-startindex", ref value))
                                                {
                                                    startIndex = (int)value.Value;
                                                }

                                                int characters = arguments[argumentIndex].Length;

                                                if (options.IsPresent("-characters", ref value))
                                                {
                                                    characters = (int)value.Value;
                                                }

                                                bool noReady = false;

                                                if (options.IsPresent("-noready", ref value))
                                                {
                                                    noReady = (bool)value.Value;
                                                }

                                                IParseState state = new ParseState(
                                                    engineFlags, substitutionFlags);

                                                code = ExpressionParser.ParseExpression(
                                                    interpreter, arguments[argumentIndex],
                                                    startIndex, characters, state, noReady,
                                                    ref result);

                                                if (code == ReturnCode.Ok)
                                                {
                                                    //
                                                    // NOTE: Success, return the entire
                                                    //       state as a string.
                                                    //
                                                    result = state.ToString();
                                                }
                                            }
                                            else
                                            {
                                                if ((argumentIndex != Index.Invalid) &&
                                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                                {
                                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                                }
                                                else
                                                {
                                                    result = "wrong # args: should be \"parse expression ?options? text\"";
                                                }

                                                code = ReturnCode.Error;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        result = "wrong # args: should be \"parse expression ?options? text\"";
                                        code   = ReturnCode.Error;
                                    }
                                    break;
                                }

                                case "options":
                                {
                                    if (arguments.Count >= 4)
                                    {
                                        OptionDictionary options = new OptionDictionary(
                                            new IOption[] {
                                                new Option(typeof(OptionBehaviorFlags), OptionFlags.MustHaveEnumValue, Index.Invalid, Index.Invalid, "-flags",
                                                           new Variant(OptionBehaviorFlags.Default)),
                                                new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-optionsvar", null),
                                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-indexes", null),
                                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-allowinteger", null),
                                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-strict", null),
                                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-verbose", null),
                                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nocase", null),
                                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-novalue", null),
                                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-noset", null),
                                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-noready", null),
                                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-simple", null),
                                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                            });

                                        int argumentIndex = Index.Invalid;

                                        code = interpreter.GetOptions(
                                            options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex,
                                            ref result);

                                        if (code == ReturnCode.Ok)
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                ((argumentIndex + 2) == arguments.Count))
                                            {
                                                Variant value          = null;
                                                string  optionsVarName = Vars.OptionSet.Options;

                                                if (options.IsPresent("-optionsvar", ref value))
                                                {
                                                    optionsVarName = value.ToString();
                                                }

                                                OptionBehaviorFlags flags = OptionBehaviorFlags.Default;

                                                if (options.IsPresent("-flags", ref value))
                                                {
                                                    flags = (OptionBehaviorFlags)value.Value;
                                                }

                                                bool indexes = false;

                                                if (options.IsPresent("-indexes"))
                                                {
                                                    indexes = true;
                                                }

                                                bool allowInteger = false;

                                                if (options.IsPresent("-allowinteger"))
                                                {
                                                    allowInteger = true;
                                                }

                                                bool strict = false;

                                                if (options.IsPresent("-strict"))
                                                {
                                                    strict = true;
                                                }

                                                bool verbose = false;

                                                if (options.IsPresent("-verbose"))
                                                {
                                                    verbose = true;
                                                }

                                                bool noCase = false;

                                                if (options.IsPresent("-nocase"))
                                                {
                                                    noCase = true;
                                                }

                                                bool noValue = false;

                                                if (options.IsPresent("-novalue"))
                                                {
                                                    noValue = true;
                                                }

                                                bool noSet = false;

                                                if (options.IsPresent("-noset"))
                                                {
                                                    noSet = true;
                                                }

                                                bool noReady = false;

                                                if (options.IsPresent("-noready"))
                                                {
                                                    noReady = true;
                                                }

                                                bool simple = false;

                                                if (options.IsPresent("-simple"))
                                                {
                                                    simple = true;
                                                }

                                                OptionDictionary newOptions  = null;
                                                AppDomain        appDomain   = interpreter.GetAppDomain();
                                                CultureInfo      cultureInfo = interpreter.CultureInfo;

                                                if (simple)
                                                {
                                                    newOptions = OptionDictionary.FromString(
                                                        interpreter, arguments[argumentIndex],
                                                        appDomain, Value.GetTypeValueFlags(
                                                            allowInteger, strict, verbose, noCase),
                                                        cultureInfo, ref result);
                                                }
                                                else
                                                {
                                                    newOptions = OptionDictionary.FromString(
                                                        interpreter, arguments[argumentIndex],
                                                        appDomain, allowInteger, strict, verbose,
                                                        noCase, cultureInfo, ref result);
                                                }

                                                if (newOptions != null)
                                                {
                                                    StringList list = StringList.FromString(
                                                        arguments[argumentIndex + 1], ref result);

                                                    if (list != null)
                                                    {
                                                        ArgumentList newArguments = new ArgumentList(list);

                                                        int nextIndex = Index.Invalid;
                                                        int endIndex  = Index.Invalid;

                                                        code = interpreter.GetOptions(
                                                            newOptions, newArguments, 0, 0, Index.Invalid, flags,
                                                            noCase, noValue, noSet, ref nextIndex, ref endIndex,
                                                            ref result);

                                                        if (code == ReturnCode.Ok)
                                                        {
                                                            VariableFlags variableFlags = VariableFlags.None;

                                                            if (noReady)
                                                            {
                                                                variableFlags |= VariableFlags.NoReady;
                                                            }

                                                            if (indexes)
                                                            {
                                                                code = interpreter.SetVariableValue2(
                                                                    variableFlags, optionsVarName,
                                                                    Vars.OptionSet.NextIndex, nextIndex.ToString(),
                                                                    null, ref result);

                                                                if (code == ReturnCode.Ok)
                                                                {
                                                                    code = interpreter.SetVariableValue2(
                                                                        variableFlags, optionsVarName,
                                                                        Vars.OptionSet.EndIndex, endIndex.ToString(),
                                                                        null, ref result);
                                                                }
                                                            }

                                                            if (code == ReturnCode.Ok)
                                                            {
                                                                foreach (KeyValuePair <string, IOption> pair in newOptions)
                                                                {
                                                                    IOption option = pair.Value;

                                                                    if (option == null)
                                                                    {
                                                                        continue;
                                                                    }

                                                                    if (option.IsIgnored(newOptions))
                                                                    {
                                                                        continue;
                                                                    }

                                                                    /* REUSED */
                                                                    value = null;

                                                                    bool present = option.IsPresent(newOptions, ref value);

                                                                    if (present &&
                                                                        !option.CanBePresent(newOptions, ref result))
                                                                    {
                                                                        code = ReturnCode.Error;
                                                                        break;
                                                                    }

                                                                    code = interpreter.SetVariableValue2(
                                                                        variableFlags, optionsVarName, pair.Key,
                                                                        present.ToString(), null, ref result);

                                                                    if (code != ReturnCode.Ok)
                                                                    {
                                                                        break;
                                                                    }

                                                                    if (option.MustHaveValue(newOptions))
                                                                    {
                                                                        //
                                                                        // NOTE: If the option was not actually present,
                                                                        //       grab and use the default value instead.
                                                                        //
                                                                        if (!present)
                                                                        {
                                                                            value = option.Value;
                                                                        }

                                                                        //
                                                                        // NOTE: Only set the value if the option was
                                                                        //       actually present OR there is a bonafide
                                                                        //       default value.
                                                                        //
                                                                        if (present || (value != null))
                                                                        {
                                                                            string index = pair.Key +
                                                                                           Characters.Comma + Vars.OptionSet.Value;

                                                                            code = interpreter.SetVariableValue2(
                                                                                variableFlags, optionsVarName, index,
                                                                                (value != null) ? value.ToString() : null,
                                                                                null, ref result);

                                                                            if (code != ReturnCode.Ok)
                                                                            {
                                                                                break;
                                                                            }
                                                                        }
                                                                    }
                                                                }

                                                                if (code == ReturnCode.Ok)
                                                                {
                                                                    result = String.Empty;
                                                                }
                                                            }
                                                        }
                                                    }
                                                    else
                                                    {
                                                        code = ReturnCode.Error;
                                                    }
                                                }
                                                else
                                                {
                                                    code = ReturnCode.Error;
                                                }
                                            }
                                            else
                                            {
                                                if ((argumentIndex != Index.Invalid) &&
                                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                                {
                                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                                }
                                                else
                                                {
                                                    result = "wrong # args: should be \"parse options ?options? optionList argumentList\"";
                                                }

                                                code = ReturnCode.Error;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        result = "wrong # args: should be \"parse options ?options? options optionList argumentList\"";
                                        code   = ReturnCode.Error;
                                    }
                                    break;
                                }

                                case "script":
                                {
                                    if (arguments.Count >= 3)
                                    {
                                        OptionDictionary options = new OptionDictionary(
                                            new IOption[] {
                                                new Option(typeof(EngineFlags), OptionFlags.MustHaveEnumValue | OptionFlags.Unsafe,
                                                           Index.Invalid, Index.Invalid, "-engineflags", new Variant(interpreter.EngineFlags)),
                                                new Option(typeof(SubstitutionFlags), OptionFlags.MustHaveEnumValue, Index.Invalid, Index.Invalid, "-substitutionflags",
                                                           new Variant(interpreter.SubstitutionFlags)),
                                                new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-filename", null),
                                                new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-currentline", null),
                                                new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-startindex", null),
                                                new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-characters", null),
                                                new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-nested", null),
                                                new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-syntax", null),
                                                new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-strict", null),
                                                new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-roundtrip", null),
                                                new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-noready", null),
                                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                            });

                                        int argumentIndex = Index.Invalid;

                                        code = interpreter.GetOptions(
                                            options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex,
                                            ref result);

                                        if (code == ReturnCode.Ok)
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                ((argumentIndex + 1) == arguments.Count))
                                            {
                                                Variant     value       = null;
                                                EngineFlags engineFlags = interpreter.EngineFlags;

                                                if (options.IsPresent("-engineflags", ref value))
                                                {
                                                    engineFlags = (EngineFlags)value.Value;
                                                }

                                                SubstitutionFlags substitutionFlags = interpreter.SubstitutionFlags;

                                                if (options.IsPresent("-substitutionflags", ref value))
                                                {
                                                    substitutionFlags = (SubstitutionFlags)value.Value;
                                                }

                                                string fileName = null;

                                                if (options.IsPresent("-filename", ref value))
                                                {
                                                    fileName = value.ToString();
                                                }

                                                int currentLine = Parser.StartLine;

                                                if (options.IsPresent("-currentline", ref value))
                                                {
                                                    currentLine = (int)value.Value;
                                                }

                                                int startIndex = 0;

                                                if (options.IsPresent("-startindex", ref value))
                                                {
                                                    startIndex = (int)value.Value;
                                                }

                                                int characters = arguments[argumentIndex].Length;

                                                if (options.IsPresent("-characters", ref value))
                                                {
                                                    characters = (int)value.Value;
                                                }

                                                bool nested = false;

                                                if (options.IsPresent("-nested", ref value))
                                                {
                                                    nested = (bool)value.Value;
                                                }

                                                bool syntax = false;

                                                if (options.IsPresent("-syntax", ref value))
                                                {
                                                    syntax = (bool)value.Value;
                                                }

                                                bool strict = false;

                                                if (options.IsPresent("-strict", ref value))
                                                {
                                                    strict = (bool)value.Value;
                                                }

                                                bool roundTrip = false;

                                                if (options.IsPresent("-roundtrip", ref value))
                                                {
                                                    roundTrip = (bool)value.Value;
                                                }

                                                bool noReady = false;

                                                if (options.IsPresent("-noready", ref value))
                                                {
                                                    noReady = (bool)value.Value;
                                                }

                                                IParseState state = new ParseState(
                                                    engineFlags, substitutionFlags);

                                                TokenList tokens = null;

                                                code = Parser.ParseScript(
                                                    interpreter, fileName, currentLine,
                                                    arguments[argumentIndex], startIndex,
                                                    characters, engineFlags, substitutionFlags,
                                                    nested, noReady, syntax, strict, ref state,
                                                    ref tokens, ref result);

                                                if (code == ReturnCode.Ok)
                                                {
                                                    if (roundTrip)
                                                    {
                                                        //
                                                        // NOTE: Return only the tokens that
                                                        //       are absolutely necessary to
                                                        //       rebuild the script text.
                                                        //
                                                        TokenList newTokens = new TokenList();

                                                        for (int index = 0; index < tokens.Count; index++)
                                                        {
                                                            IToken token = tokens[index];

                                                            if (token.Type == TokenType.Variable)
                                                            {
                                                                index += token.Components;
                                                            }
                                                            else if ((token.Type != TokenType.Separator) &&
                                                                     (token.Components != 0))
                                                            {
                                                                continue;
                                                            }

                                                            newTokens.Add(token);
                                                        }

                                                        result = newTokens.ToString();
                                                    }
                                                    else
                                                    {
                                                        //
                                                        // NOTE: Replace final token list
                                                        //       with the one we have been
                                                        //       building.
                                                        //
                                                        state.Tokens = tokens;

                                                        //
                                                        // NOTE: Success, return the entire
                                                        //       state as a string.
                                                        //
                                                        result = state.ToString();
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                if ((argumentIndex != Index.Invalid) &&
                                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                                {
                                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                                }
                                                else
                                                {
                                                    result = "wrong # args: should be \"parse script ?options? text\"";
                                                }

                                                code = ReturnCode.Error;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        result = "wrong # args: should be \"parse script ?options? text\"";
                                        code   = ReturnCode.Error;
                                    }
                                    break;
                                }

                                default:
                                {
                                    result = ScriptOps.BadSubCommand(
                                        interpreter, null, null, subCommand, this, null, null);

                                    code = ReturnCode.Error;
                                    break;
                                }
                                }
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"parse type ?arg ...?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #8
0
        ///////////////////////////////////////////////////////////////////////

        #region IExecute Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            if (interpreter == null)
            {
                result = "invalid interpreter";
                return(ReturnCode.Error);
            }

            if (arguments == null)
            {
                result = "invalid argument list";
                return(ReturnCode.Error);
            }

            if (arguments.Count < 2)
            {
                result = "wrong # args: should be \"hash option ?arg ...?\"";
                return(ReturnCode.Error);
            }

            ReturnCode code;
            string     subCommand = arguments[1];
            bool       tried      = false;

            code = ScriptOps.TryExecuteSubCommandFromEnsemble(
                interpreter, this, clientData, arguments, true,
                false, ref subCommand, ref tried, ref result);

            if ((code != ReturnCode.Ok) || tried)
            {
                return(code);
            }

            //
            // NOTE: These algorithms are known to be supported by the
            //       framework.
            //
            //       Normal: MD5, RIPEMD160, SHA, SHA1, SHA256, SHA384, SHA512
            //
            //        Keyed: MACTripleDES
            //
            //         HMAC: HMACMD5, HMACRIPEMD160, HMACSHA1, HMACSHA256,
            //               HMACSHA384, HMACSHA512
            //
            switch (subCommand)
            {
            case "keyed":
            {
                if (arguments.Count >= 4)
                {
                    OptionDictionary options = new OptionDictionary(
                        new IOption[] {
                            new Option(null, OptionFlags.None,
                                       Index.Invalid, Index.Invalid, "-raw",
                                       null),
                            new Option(null, OptionFlags.None,
                                       Index.Invalid, Index.Invalid, "-filename",
                                       null), /* COMPAT: Tcllib. */
                            new Option(null, OptionFlags.MustHaveEncodingValue,
                                       Index.Invalid, Index.Invalid, "-encoding",
                                       null),
                            new Option(null, OptionFlags.None,
                                       Index.Invalid, Index.Invalid,
                                       Option.EndOfOptions, null)
                        });

                    int argumentIndex = Index.Invalid;

                    code = interpreter.GetOptions(
                        options, arguments, 0, 2, Index.Invalid, false,
                        ref argumentIndex, ref result);

                    if (code == ReturnCode.Ok)
                    {
                        if ((argumentIndex != Index.Invalid) &&
                            ((argumentIndex + 2) <= arguments.Count) &&
                            ((argumentIndex + 3) >= arguments.Count))
                        {
                            Variant value = null;
                            bool    raw   = false;

                            if (options.IsPresent("-raw"))
                            {
                                raw = true;
                            }

                            bool isFileName = false;

                            if (options.IsPresent("-filename", ref value))
                            {
                                isFileName = true;
                            }

                            Encoding encoding = null;

                            if (options.IsPresent("-encoding", ref value))
                            {
                                encoding = (Encoding)value.Value;
                            }

                            if (code == ReturnCode.Ok)
                            {
                                try
                                {
                                    using (KeyedHashAlgorithm algorithm =
                                               KeyedHashAlgorithm.Create(
                                                   arguments[argumentIndex]))
                                    {
                                        if (algorithm != null)
                                        {
                                            algorithm.Initialize();

                                            if ((argumentIndex + 3) == arguments.Count)
                                            {
                                                byte[] bytes = null;

                                                code = StringOps.GetBytes(
                                                    encoding, arguments[argumentIndex + 2],
                                                    EncodingType.Binary, ref bytes, ref result);

                                                if (code == ReturnCode.Ok)
                                                {
                                                    algorithm.Key = bytes;
                                                }
                                            }

                                            if (code == ReturnCode.Ok)
                                            {
                                                if (isFileName)
                                                {
                                                    Stream stream = null;

                                                    try
                                                    {
                                                        code = RuntimeOps.NewStream(
                                                            interpreter,
                                                            arguments[argumentIndex + 1],
                                                            FileMode.Open, FileAccess.Read,
                                                            ref stream, ref result);

                                                        if (code == ReturnCode.Ok)
                                                        {
                                                            if (raw)
                                                            {
                                                                result = new ByteList(
                                                                    algorithm.ComputeHash(stream));
                                                            }
                                                            else
                                                            {
                                                                result = FormatOps.Hash(
                                                                    algorithm.ComputeHash(stream));
                                                            }
                                                        }
                                                    }
                                                    finally
                                                    {
                                                        if (stream != null)
                                                        {
                                                            stream.Close();
                                                            stream = null;
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    byte[] bytes = null;

                                                    code = StringOps.GetBytes(
                                                        encoding, arguments[argumentIndex + 1],
                                                        EncodingType.Binary, ref bytes, ref result);

                                                    if (raw)
                                                    {
                                                        result = new ByteList(
                                                            algorithm.ComputeHash(bytes));
                                                    }
                                                    else
                                                    {
                                                        result = FormatOps.Hash(
                                                            algorithm.ComputeHash(bytes));
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            result = String.Format(
                                                "unsupported keyed hash algorithm \"{0}\"",
                                                arguments[argumentIndex]);

                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Engine.SetExceptionErrorCode(interpreter, e);

                                    result = e;
                                    code   = ReturnCode.Error;
                                }
                            }
                        }
                        else
                        {
                            if ((argumentIndex != Index.Invalid) &&
                                Option.LooksLikeOption(arguments[argumentIndex]))
                            {
                                result = OptionDictionary.BadOption(
                                    options, arguments[argumentIndex]);
                            }
                            else
                            {
                                result = String.Format(
                                    "wrong # args: should be \"{0} {1} ?options? algorithm string ?key?\"",
                                    this.Name, subCommand);
                            }

                            code = ReturnCode.Error;
                        }
                    }
                }
                else
                {
                    result = String.Format(
                        "wrong # args: should be \"{0} {1} ?options? algorithm string ?key?\"",
                        this.Name, subCommand);

                    code = ReturnCode.Error;
                }
                break;
            }

            case "list":
            {
                if ((arguments.Count == 2) || (arguments.Count == 3))
                {
                    string type = null;

                    if (arguments.Count == 3)
                    {
                        type = arguments[2];
                    }

                    switch (type)
                    {
                    case null:
                    case "all":
                    {
                        StringList list = new StringList();

                        lock (syncRoot)
                        {
                            if (defaultAlgorithms != null)
                            {
                                list.AddRange(defaultAlgorithms);
                            }
                        }

                        if (keyedHashAlgorithmNames != null)
                        {
                            foreach (string hashAlgorithmName in keyedHashAlgorithmNames)
                            {
                                list.Add(StringList.MakeList("keyed", hashAlgorithmName));
                            }
                        }

                        if (macHashAlgorithmNames != null)
                        {
                            foreach (string hashAlgorithmName in macHashAlgorithmNames)
                            {
                                list.Add(StringList.MakeList("mac", hashAlgorithmName));
                            }
                        }

                        if (normalHashAlgorithmNames != null)
                        {
                            foreach (string hashAlgorithmName in normalHashAlgorithmNames)
                            {
                                list.Add(StringList.MakeList("normal", hashAlgorithmName));
                            }
                        }

                        result = list;
                        break;
                    }

                    case "default":
                    {
                        lock (syncRoot)
                        {
                            result = (defaultAlgorithms != null) ?
                                     new StringList(defaultAlgorithms) : null;
                        }
                        break;
                    }

                    case "keyed":
                    {
                        result = (keyedHashAlgorithmNames != null) ?
                                 new StringList(keyedHashAlgorithmNames) : null;

                        break;
                    }

                    case "mac":
                    {
                        result = (macHashAlgorithmNames != null) ?
                                 new StringList(macHashAlgorithmNames) : null;

                        break;
                    }

                    case "normal":
                    {
                        result = (normalHashAlgorithmNames != null) ?
                                 new StringList(normalHashAlgorithmNames) : null;

                        break;
                    }

                    default:
                    {
                        result = "unknown algorithm list, must be: all, default, keyed, mac, or normal";
                        code   = ReturnCode.Error;
                        break;
                    }
                    }
                }
                else
                {
                    result = String.Format(
                        "wrong # args: should be \"{0} {1} ?type?\"",
                        this.Name, subCommand);

                    code = ReturnCode.Error;
                }
                break;
            }

            case "mac":
            {
                if (arguments.Count >= 4)
                {
                    OptionDictionary options = new OptionDictionary(
                        new IOption[] {
                            new Option(null, OptionFlags.None,
                                       Index.Invalid, Index.Invalid, "-raw",
                                       null),
                            new Option(null, OptionFlags.None,
                                       Index.Invalid, Index.Invalid, "-filename",
                                       null), /* COMPAT: Tcllib. */
                            new Option(null, OptionFlags.MustHaveEncodingValue,
                                       Index.Invalid, Index.Invalid, "-encoding",
                                       null),
                            new Option(null, OptionFlags.None,
                                       Index.Invalid, Index.Invalid,
                                       Option.EndOfOptions, null)
                        });

                    int argumentIndex = Index.Invalid;

                    code = interpreter.GetOptions(
                        options, arguments, 0, 2, Index.Invalid, false,
                        ref argumentIndex, ref result);

                    if (code == ReturnCode.Ok)
                    {
                        if ((argumentIndex != Index.Invalid) &&
                            ((argumentIndex + 2) <= arguments.Count) &&
                            ((argumentIndex + 3) >= arguments.Count))
                        {
                            Variant value = null;
                            bool    raw   = false;

                            if (options.IsPresent("-raw"))
                            {
                                raw = true;
                            }

                            bool isFileName = false;

                            if (options.IsPresent("-filename", ref value))
                            {
                                isFileName = true;
                            }

                            Encoding encoding = null;

                            if (options.IsPresent("-encoding", ref value))
                            {
                                encoding = (Encoding)value.Value;
                            }

                            if (code == ReturnCode.Ok)
                            {
                                try
                                {
                                    using (HMAC algorithm = HMAC.Create(
                                               arguments[argumentIndex]))
                                    {
                                        if (algorithm != null)
                                        {
                                            algorithm.Initialize();

                                            if ((argumentIndex + 3) == arguments.Count)
                                            {
                                                byte[] bytes = null;

                                                code = StringOps.GetBytes(
                                                    encoding, arguments[argumentIndex + 2],
                                                    EncodingType.Binary, ref bytes, ref result);

                                                if (code == ReturnCode.Ok)
                                                {
                                                    algorithm.Key = bytes;
                                                }
                                            }

                                            if (code == ReturnCode.Ok)
                                            {
                                                if (isFileName)
                                                {
                                                    Stream stream = null;

                                                    try
                                                    {
                                                        code = RuntimeOps.NewStream(
                                                            interpreter,
                                                            arguments[argumentIndex + 1],
                                                            FileMode.Open, FileAccess.Read,
                                                            ref stream, ref result);

                                                        if (code == ReturnCode.Ok)
                                                        {
                                                            if (raw)
                                                            {
                                                                result = new ByteList(
                                                                    algorithm.ComputeHash(stream));
                                                            }
                                                            else
                                                            {
                                                                result = FormatOps.Hash(
                                                                    algorithm.ComputeHash(stream));
                                                            }
                                                        }
                                                    }
                                                    finally
                                                    {
                                                        if (stream != null)
                                                        {
                                                            stream.Close();
                                                            stream = null;
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    byte[] bytes = null;

                                                    code = StringOps.GetBytes(
                                                        encoding, arguments[argumentIndex + 1],
                                                        EncodingType.Binary, ref bytes, ref result);

                                                    if (raw)
                                                    {
                                                        result = new ByteList(
                                                            algorithm.ComputeHash(bytes));
                                                    }
                                                    else
                                                    {
                                                        result = FormatOps.Hash(
                                                            algorithm.ComputeHash(bytes));
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            result = String.Format(
                                                "unsupported hmac algorithm \"{0}\"",
                                                arguments[argumentIndex]);

                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Engine.SetExceptionErrorCode(interpreter, e);

                                    result = e;
                                    code   = ReturnCode.Error;
                                }
                            }
                        }
                        else
                        {
                            if ((argumentIndex != Index.Invalid) &&
                                Option.LooksLikeOption(arguments[argumentIndex]))
                            {
                                result = OptionDictionary.BadOption(
                                    options, arguments[argumentIndex]);
                            }
                            else
                            {
                                result = String.Format(
                                    "wrong # args: should be \"{0} {1} ?options? algorithm string ?key?\"",
                                    this.Name, subCommand);
                            }

                            code = ReturnCode.Error;
                        }
                    }
                }
                else
                {
                    result = String.Format(
                        "wrong # args: should be \"{0} {1} ?options? algorithm string ?key?\"",
                        this.Name, subCommand);

                    code = ReturnCode.Error;
                }
                break;
            }

            case "normal":
            {
                if (arguments.Count >= 4)
                {
                    OptionDictionary options = new OptionDictionary(
                        new IOption[] {
                            new Option(null, OptionFlags.None,
                                       Index.Invalid, Index.Invalid, "-raw",
                                       null),
                            new Option(null, OptionFlags.None,
                                       Index.Invalid, Index.Invalid, "-filename",
                                       null), /* COMPAT: Tcllib. */
                            new Option(null, OptionFlags.MustHaveEncodingValue,
                                       Index.Invalid, Index.Invalid, "-encoding",
                                       null),
                            new Option(null, OptionFlags.None,
                                       Index.Invalid, Index.Invalid,
                                       Option.EndOfOptions, null)
                        });

                    int argumentIndex = Index.Invalid;

                    code = interpreter.GetOptions(
                        options, arguments, 0, 2, Index.Invalid, false,
                        ref argumentIndex, ref result);

                    if (code == ReturnCode.Ok)
                    {
                        if ((argumentIndex != Index.Invalid) &&
                            ((argumentIndex + 2) == arguments.Count))
                        {
                            Variant value = null;
                            bool    raw   = false;

                            if (options.IsPresent("-raw"))
                            {
                                raw = true;
                            }

                            bool isFileName = false;

                            if (options.IsPresent("-filename", ref value))
                            {
                                isFileName = true;
                            }

                            Encoding encoding = null;

                            if (options.IsPresent("-encoding", ref value))
                            {
                                encoding = (Encoding)value.Value;
                            }

                            if (code == ReturnCode.Ok)
                            {
                                try
                                {
                                    using (HashAlgorithm algorithm =
                                               HashAlgorithm.Create(
                                                   arguments[argumentIndex]))
                                    {
                                        if (algorithm != null)
                                        {
                                            algorithm.Initialize();

                                            if (isFileName)
                                            {
                                                Stream stream = null;

                                                try
                                                {
                                                    code = RuntimeOps.NewStream(
                                                        interpreter,
                                                        arguments[argumentIndex + 1],
                                                        FileMode.Open, FileAccess.Read,
                                                        ref stream, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        if (raw)
                                                        {
                                                            result = new ByteList(
                                                                algorithm.ComputeHash(stream));
                                                        }
                                                        else
                                                        {
                                                            result = FormatOps.Hash(
                                                                algorithm.ComputeHash(stream));
                                                        }
                                                    }
                                                }
                                                finally
                                                {
                                                    if (stream != null)
                                                    {
                                                        stream.Close();
                                                        stream = null;
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                byte[] bytes = null;

                                                code = StringOps.GetBytes(
                                                    encoding, arguments[argumentIndex + 1],
                                                    EncodingType.Binary, ref bytes, ref result);

                                                if (code == ReturnCode.Ok)
                                                {
                                                    if (raw)
                                                    {
                                                        result = new ByteList(
                                                            algorithm.ComputeHash(bytes));
                                                    }
                                                    else
                                                    {
                                                        result = FormatOps.Hash(
                                                            algorithm.ComputeHash(bytes));
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            result = String.Format(
                                                "unsupported hash algorithm \"{0}\"",
                                                arguments[argumentIndex]);

                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Engine.SetExceptionErrorCode(interpreter, e);

                                    result = e;
                                    code   = ReturnCode.Error;
                                }
                            }
                        }
                        else
                        {
                            if ((argumentIndex != Index.Invalid) &&
                                Option.LooksLikeOption(arguments[argumentIndex]))
                            {
                                result = OptionDictionary.BadOption(
                                    options, arguments[argumentIndex]);
                            }
                            else
                            {
                                result = String.Format(
                                    "wrong # args: should be \"{0} {1} ?options? algorithm string\"",
                                    this.Name, subCommand);
                            }

                            code = ReturnCode.Error;
                        }
                    }
                }
                else
                {
                    result = String.Format(
                        "wrong # args: should be \"{0} {1} ?options? algorithm string\"",
                        this.Name, subCommand);

                    code = ReturnCode.Error;
                }
                break;
            }

            default:
            {
                result = ScriptOps.BadSubCommand(
                    interpreter, null, null, subCommand, this, null, null);

                code = ReturnCode.Error;
                break;
            }
            }

            return(code);
        }
コード例 #9
0
ファイル: Report.cs プロジェクト: jdruin/F5Eagle
        ///////////////////////////////////////////////////////////////////////

        #region IExecute Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            if (interpreter == null)
            {
                result = "invalid interpreter";
                return(ReturnCode.Error);
            }

            if (arguments == null)
            {
                result = "invalid argument list";
                return(ReturnCode.Error);
            }

            if (arguments.Count < 2)
            {
                result = String.Format(
                    "wrong # args: should be \"{0} option ?arg ...?\"",
                    this.Name);

                return(ReturnCode.Error);
            }

            ReturnCode code;
            string     subCommand = arguments[1];
            bool       tried      = false;

            code = Utility.TryExecuteSubCommandFromEnsemble(
                interpreter, this, clientData, arguments, true,
                false, ref subCommand, ref tried, ref result);

            if ((code == ReturnCode.Ok) && !tried)
            {
                switch (subCommand)
                {
                case "alert":
                {
                    if (arguments.Count == 3)
                    {
                        result = CommonOps.Complain(arguments[2]);
                        code   = ReturnCode.Ok;
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1} string\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                case "append":
                {
                    if (arguments.Count == 3)
                    {
                        _Forms.TestForm form = _Shell.Test.mainForm;

                        if (form != null)
                        {
                            /* NO RESULT */
                            form.AsyncAppendStatusText(arguments[2],
                                                       true);

                            result = String.Empty;
                            code   = ReturnCode.Ok;
                        }
                        else
                        {
                            result = "invalid main form";
                            code   = ReturnCode.Error;
                        }
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1} string\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                case "clear":
                {
                    if (arguments.Count == 2)
                    {
                        _Forms.TestForm form = _Shell.Test.mainForm;

                        if (form != null)
                        {
                            /* NO RESULT */
                            form.AsyncClearTestItems();

                            /* NO RESULT */
                            form.AsyncClearStatusText();

                            result = String.Empty;
                            code   = ReturnCode.Ok;
                        }
                        else
                        {
                            result = "invalid main form";
                            code   = ReturnCode.Error;
                        }
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1}\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                case "progress":
                {
                    if (arguments.Count == 3)
                    {
                        int value = 0;

                        code = Value.GetInteger2(
                            (IGetValue)arguments[2], ValueFlags.AnyInteger,
                            interpreter.CultureInfo, ref value, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            _Forms.TestForm form = _Shell.Test.mainForm;

                            if (form != null)
                            {
                                /* NO RESULT */
                                form.AsyncSetProgressValue(value);

                                result = String.Empty;
                                code   = ReturnCode.Ok;
                            }
                            else
                            {
                                result = "invalid main form";
                                code   = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1} value\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                case "result":
                {
                    if (arguments.Count == 4)
                    {
                        object enumValue = Utility.TryParseEnum(
                            typeof(TestResult), arguments[3],
                            true, true);

                        if (enumValue is TestResult)
                        {
                            TestResult      testResult = (TestResult)enumValue;
                            _Forms.TestForm form       = _Shell.Test.mainForm;

                            if (form != null)
                            {
                                /* NO RESULT */
                                form.AsyncAddTestItem(String.Format(
                                                          "{0}{1}{2}", arguments[2],
                                                          Characters.HorizontalTab,
                                                          testResult.ToString().ToUpper()));

                                result = String.Empty;
                                code   = ReturnCode.Ok;
                            }
                            else
                            {
                                result = "invalid main form";
                                code   = ReturnCode.Error;
                            }
                        }
                        else
                        {
                            result = Utility.BadValue(
                                null, "test result", arguments[3],
                                Enum.GetNames(typeof(TestResult)),
                                null, null);

                            code = ReturnCode.Error;
                        }
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1} name result\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                case "send":
                {
                    if (arguments.Count >= 3)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                                new Option(null, OptionFlags.None, 1,
                                           Index.Invalid, "-asynchronous", null),
                                new Option(null,
                                           OptionFlags.MustHaveIntegerValue,
                                           1, Index.Invalid, "-timeout", null),
                                new Option(null, OptionFlags.None,
                                           Index.Invalid, Index.Invalid,
                                           Option.EndOfOptions, null)
                            });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(
                            options, arguments, 0, 2, Index.Invalid,
                            true, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex != Index.Invalid) &&
                                ((argumentIndex + 1) == arguments.Count))
                            {
                                bool asynchronous = false;

                                if (options.IsPresent("-asynchronous"))
                                {
                                    asynchronous = true;
                                }

                                Variant value   = null;
                                int     timeout = _Timeout.Infinite;

                                if (options.IsPresent("-timeout", ref value))
                                {
                                    timeout = (int)value.Value;
                                }

                                _Forms.TestForm form = _Shell.Test.mainForm;

                                if (form != null)
                                {
                                    if (asynchronous)
                                    {
                                        /* NO RESULT */
                                        form.AsyncEvaluateScript(
                                            arguments[argumentIndex], null);
                                    }
                                    else
                                    {
                                        result = Utility.CreateSynchronizedResult(null);

                                        /* NO RESULT */
                                        form.AsyncEvaluateScript(
                                            arguments[argumentIndex], result);

                                        if (Utility.WaitSynchronizedResult(result, timeout))
                                        {
                                            if (Utility.GetSynchronizedResult(result, ref code,
                                                                              ref result, ref result) != ReturnCode.Ok)
                                            {
                                                code = ReturnCode.Error;
                                            }
                                        }
                                        else
                                        {
                                            result = String.Format(
                                                "timeout, {0} milliseconds",
                                                timeout);

                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "invalid main form";
                                    code   = ReturnCode.Error;
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(
                                        options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = String.Format(
                                        "wrong # args: should be \"{0} {1} ?options? script\"",
                                        this.Name, subCommand);
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = String.Format(
                            "wrong # args: should be \"{0} {1} ?options? script\"",
                            this.Name, subCommand);

                        code = ReturnCode.Error;
                    }
                    break;
                }

                default:
                {
                    result = Utility.BadSubCommand(
                        interpreter, null, null, subCommand, this, null, null);

                    code = ReturnCode.Error;
                    break;
                }
                }
            }

            return(code);
        }
コード例 #10
0
ファイル: Lsearch.cs プロジェクト: jdruin/F5Eagle
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.None, 1, Index.Invalid, "-ascii", null),
                            new Option(null, OptionFlags.None, 1, Index.Invalid, "-dictionary", null),
                            new Option(null, OptionFlags.None, 1, Index.Invalid, "-integer", null),
                            new Option(null, OptionFlags.None, 1, Index.Invalid, "-real", null),
                            new Option(null, OptionFlags.None, 2, Index.Invalid, "-decreasing", null),
                            new Option(null, OptionFlags.None, 2, Index.Invalid, "-increasing", null),
                            new Option(null, OptionFlags.None, 3, Index.Invalid, "-exact", null),
                            new Option(null, OptionFlags.None, 3, Index.Invalid, "-substring", null),
                            new Option(null, OptionFlags.None, 3, Index.Invalid, "-glob", null),
                            new Option(null, OptionFlags.None, 3, Index.Invalid, "-regexp", null),
                            new Option(null, OptionFlags.None, 3, Index.Invalid, "-sorted", null), // NOTE: Implies "-exact"
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-variable", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-inverse", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-subindices", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-all", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-inline", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nocase", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-not", null),
                            new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-start", null),
                            new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-index", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                        });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex != Index.Invalid) && ((argumentIndex + 2) == arguments.Count))
                            {
                                StringList list = null;

                                ///////////////////////////////////////////////////////////////////////
                                //
                                // HACK: *PERF* This option enables an optimization that allows us to
                                //       use the cached list representation for a particular script
                                //       variable, if any, instead of re-parsing the string.  If this
                                //       option is enabled, the first non-option argument is NOT the
                                //       list to search; rather, it is the variable name containing
                                //       the list to search.
                                //
                                bool isVariable = false;

                                if (options.IsPresent("-variable"))
                                {
                                    isVariable = true;
                                }

                                if (isVariable)
                                {
                                    /* IGNORED */
                                    interpreter.GetListVariableValue(
                                        VariableFlags.None, arguments[argumentIndex], false, true,
                                        true, ref list);
                                }

                                ///////////////////////////////////////////////////////////////////////
                                //
                                // NOTE: If no list representation is available, then parse the first
                                //       non-option argument string into a list.
                                //
                                if (list == null)
                                {
                                    code = Parser.SplitList(
                                        interpreter, arguments[argumentIndex], 0,
                                        Length.Invalid, true, ref list, ref result);
                                }

                                if (code == ReturnCode.Ok)
                                {
                                    Variant value     = null;
                                    string  indexText = null;

                                    if (options.IsPresent("-index", ref value))
                                    {
                                        indexText = value.ToString();
                                    }

                                    bool inverse = false;

                                    if (options.IsPresent("-inverse"))
                                    {
                                        inverse = true;
                                    }

                                    bool subIndexes = false;

                                    if (options.IsPresent("-subindices"))
                                    {
                                        subIndexes = true;
                                    }

                                    if ((indexText != null) || !subIndexes)
                                    {
                                        string start = null;

                                        if (options.IsPresent("-start", ref value))
                                        {
                                            start = value.ToString();
                                        }

                                        int startIndex = Index.Invalid;

                                        if (start != null)
                                        {
                                            code = Value.GetIndex(
                                                start, list.Count, ValueFlags.AnyIndex,
                                                interpreter.CultureInfo, ref startIndex,
                                                ref result);
                                        }

                                        if (code == ReturnCode.Ok)
                                        {
                                            bool all = false;

                                            if (options.IsPresent("-all"))
                                            {
                                                all = true;
                                            }

                                            bool inline = false;

                                            if (options.IsPresent("-inline"))
                                            {
                                                inline = true;
                                            }

                                            if (startIndex < list.Count)
                                            {
                                                if (startIndex < 0)
                                                {
                                                    startIndex = 0;
                                                }

                                                bool ascending = true; // FIXME: PRI 5: Default handling.

                                                if (options.IsPresent("-decreasing"))
                                                {
                                                    ascending = false;
                                                }
                                                else if (options.IsPresent("-increasing"))
                                                {
                                                    ascending = true;
                                                }

                                                MatchMode mode   = StringOps.DefaultMatchMode;
                                                bool      sorted = false;

                                                if (options.IsPresent("-sorted"))
                                                {
                                                    mode   = MatchMode.Exact;
                                                    sorted = true;
                                                }
                                                else if (options.IsPresent("-exact"))
                                                {
                                                    mode = MatchMode.Exact;
                                                }
                                                else if (options.IsPresent("-substring"))
                                                {
                                                    mode = MatchMode.SubString;
                                                }
                                                else if (options.IsPresent("-regexp"))
                                                {
                                                    mode = MatchMode.RegExp;
                                                }
                                                else if (options.IsPresent("-glob"))
                                                {
                                                    mode = MatchMode.Glob;
                                                }

                                                bool noCase = false;

                                                if (options.IsPresent("-nocase"))
                                                {
                                                    noCase = true;
                                                }

                                                bool not = false;

                                                if (options.IsPresent("-not"))
                                                {
                                                    not = true;
                                                }

                                                IntDictionary      duplicates = null;
                                                IComparer <string> comparer   = null;

                                                if (options.IsPresent("-exact") || options.IsPresent("-sorted"))
                                                {
                                                    if (options.IsPresent("-dictionary"))
                                                    {
                                                        comparer = new _Comparers.StringDictionaryComparer(
                                                            interpreter, ascending, indexText, true, false,
                                                            interpreter.CultureInfo, ref duplicates);
                                                    }
                                                    else if (options.IsPresent("-integer"))
                                                    {
                                                        comparer = new _Comparers.StringIntegerComparer(
                                                            interpreter, ascending, indexText, true, false,
                                                            interpreter.CultureInfo, ref duplicates);
                                                    }
                                                    else if (options.IsPresent("-real"))
                                                    {
                                                        comparer = new _Comparers.StringRealComparer(
                                                            interpreter, ascending, indexText, true, false,
                                                            interpreter.CultureInfo, ref duplicates);
                                                    }
                                                    else if (options.IsPresent("-ascii") || true) // FIXME: PRI 5: Default handling.
                                                    {
                                                        //
                                                        // NOTE: Check for things that the .NET Framework will not do by
                                                        //       default (via String.Compare).
                                                        //
                                                        if (!ascending || (indexText != null) || noCase)
                                                        {
                                                            comparer = new _Comparers.StringAsciiComparer(
                                                                interpreter, ascending, indexText, true, noCase,
                                                                false, interpreter.CultureInfo, ref duplicates);
                                                        }
                                                    }
                                                }
                                                else if (options.IsPresent("-regexp"))
                                                {
                                                    comparer = new _Comparers.StringRegexpComparer(
                                                        interpreter, ascending, indexText, true, noCase,
                                                        false, interpreter.CultureInfo, ref duplicates);
                                                }
                                                else if (options.IsPresent("-substring"))
                                                {
                                                    comparer = new _Comparers.StringSubStringComparer(
                                                        interpreter, ascending, indexText, true, noCase,
                                                        false, interpreter.CultureInfo, ref duplicates);
                                                }
                                                else if (options.IsPresent("-glob") || true) // FIXME: PRI 5: Default handling.
                                                {
                                                    comparer = new _Comparers.StringGlobComparer(
                                                        interpreter, ascending, indexText, true, noCase,
                                                        false, interpreter.CultureInfo, ref duplicates);
                                                }

                                                try
                                                {
                                                    string     pattern   = arguments[argumentIndex + 1];
                                                    int        listIndex = Index.Invalid;
                                                    StringList matches   = all ? new StringList() : null;

                                                    if (sorted && ((indexText == null) || (comparer != null)) && !all && !not && !inverse)
                                                    {
                                                        //
                                                        // NOTE: Use the built-in binary search with the selected comparer.
                                                        //
                                                        listIndex = list.BinarySearch(startIndex, list.Count - startIndex, pattern, comparer);

                                                        if (listIndex < 0)
                                                        {
                                                            listIndex = Index.Invalid;
                                                        }
                                                    }
                                                    else if ((comparer != null) || all || not || inverse)
                                                    {
                                                        //
                                                        // NOTE: Some custom handling is required, use the selected comparer
                                                        //       and options.
                                                        //
                                                        for (int searchIndex = startIndex; searchIndex < list.Count; searchIndex++)
                                                        {
                                                            //
                                                            // NOTE: If we have a comparer object, use it; otherwise, use our
                                                            //       fallback matching routine.
                                                            //
                                                            bool match;

                                                            if (inverse)
                                                            {
                                                                if (comparer != null)
                                                                {
                                                                    match = (comparer.Compare(pattern, list[searchIndex]) == 0);
                                                                }
                                                                else
                                                                {
                                                                    match = StringOps.Match(interpreter, mode, pattern, list[searchIndex], noCase);
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (comparer != null)
                                                                {
                                                                    match = (comparer.Compare(list[searchIndex], pattern) == 0);
                                                                }
                                                                else
                                                                {
                                                                    match = StringOps.Match(interpreter, mode, list[searchIndex], pattern, noCase);
                                                                }
                                                            }

                                                            //
                                                            // NOTE: Do we want to consider this to be a match?
                                                            //
                                                            if ((match && !not) || (!match && not))
                                                            {
                                                                if (all)
                                                                {
                                                                    if (inline)
                                                                    {
                                                                        if (subIndexes)
                                                                        {
                                                                            string subValue = null;

                                                                            code = ListOps.SelectFromSubList(
                                                                                interpreter, list[searchIndex], indexText, false,
                                                                                interpreter.CultureInfo, ref subValue, ref result);

                                                                            if (code != ReturnCode.Ok)
                                                                            {
                                                                                break;
                                                                            }

                                                                            matches.Add(subValue);
                                                                        }
                                                                        else
                                                                        {
                                                                            matches.Add(list[searchIndex]);
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        if (subIndexes)
                                                                        {
                                                                            IntList indexList = new IntList(new int[] { searchIndex });

                                                                            code = ListOps.SelectFromSubList(
                                                                                interpreter, list[searchIndex], indexText, false,
                                                                                interpreter.CultureInfo, ref indexList, ref result);

                                                                            if (code != ReturnCode.Ok)
                                                                            {
                                                                                break;
                                                                            }

                                                                            matches.Add(indexList.ToString());
                                                                        }
                                                                        else
                                                                        {
                                                                            matches.Add(searchIndex.ToString());
                                                                        }
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    listIndex = searchIndex;
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                    else
                                                    {
                                                        //
                                                        // NOTE: No special handling required, use built-in find routine.
                                                        //
                                                        listIndex = list.IndexOf(pattern, startIndex);
                                                    }

                                                    //
                                                    // NOTE: Make sure nothing in the search loop failed.
                                                    //
                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        //
                                                        // NOTE: Handle the result(s) of the search and build the result.
                                                        //
                                                        if (all)
                                                        {
                                                            //
                                                            // NOTE: This may be an empty list.
                                                            //
                                                            result = matches;
                                                        }
                                                        else
                                                        {
                                                            if (listIndex != Index.Invalid)
                                                            {
                                                                //
                                                                // NOTE: Match found, returning index or value, based on
                                                                //       "-inline" option.
                                                                //
                                                                if (inline)
                                                                {
                                                                    result = list[listIndex];
                                                                }
                                                                else
                                                                {
                                                                    if (subIndexes)
                                                                    {
                                                                        IntList indexList = new IntList(new int[] { listIndex });

                                                                        code = ListOps.SelectFromSubList(
                                                                            interpreter, list[listIndex], indexText, false,
                                                                            interpreter.CultureInfo, ref indexList, ref result);

                                                                        if (code == ReturnCode.Ok)
                                                                        {
                                                                            result = indexList.ToString();
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        result = listIndex;
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                //
                                                                // NOTE: Match not found, returning invalid index or empty
                                                                //       value, based on "-inline" option.
                                                                //
                                                                if (inline)
                                                                {
                                                                    result = String.Empty;
                                                                }
                                                                else
                                                                {
                                                                    result = Index.Invalid;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    Engine.SetExceptionErrorCode(interpreter, e);

                                                    if (e.InnerException != null)
                                                    {
                                                        result = e.InnerException.Message;
                                                    }
                                                    else if (e is ScriptException)
                                                    {
                                                        result = e.Message;
                                                    }
                                                    else
                                                    {
                                                        result = e;
                                                    }

                                                    code = ReturnCode.Error;
                                                }
                                            }
                                            else
                                            {
                                                if (all || inline)
                                                {
                                                    result = String.Empty;
                                                }
                                                else
                                                {
                                                    result = Index.Invalid;
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        result = "-subindices cannot be used without -index option";
                                        code   = ReturnCode.Error;
                                    }
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"lsearch ?options? list pattern\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"lsearch ?options? list pattern\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #11
0
ファイル: Puts.cs プロジェクト: jdruin/F5Eagle
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nonewline", null)
                        });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex != Index.Invalid) && ((argumentIndex + 2) >= arguments.Count))
                            {
                                bool newLine = true;

                                if (options.IsPresent("-nonewline"))
                                {
                                    newLine = false;
                                }

                                string channelId = Channel.StdOut;

                                if ((argumentIndex + 1) < arguments.Count)
                                {
                                    channelId = arguments[argumentIndex];
                                }

                                Channel channel = interpreter.GetChannel(channelId, ref result);

                                if (channel != null)
                                {
                                    Encoding encoding = null;

                                    if (interpreter.GetChannelEncoding(channel, ref encoding) == ReturnCode.Ok)
                                    {
                                        string output = arguments[arguments.Count - 1];

                                        try
                                        {
                                            if (channel.IsVirtualOutput)
                                            {
                                                //
                                                // NOTE: The encoding is ignored, because this is directly
                                                //       from the input string, which is already Unicode.
                                                //
                                                channel.AppendVirtualOutput(output);

                                                if (newLine)
                                                {
                                                    channel.AppendVirtualOutput(Channel.NewLine);
                                                }

                                                result = String.Empty;
                                            }
                                            else
                                            {
                                                BinaryWriter binaryWriter = interpreter.GetChannelBinaryWriter(channel);

                                                if (binaryWriter != null)
                                                {
                                                    byte[] bytes = null;

                                                    code = StringOps.GetBytes(
                                                        encoding, output, EncodingType.Binary, ref bytes,
                                                        ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        channel.CheckAppend();

#if CONSOLE
                                                        if (channel.IsConsoleStream)
                                                        {
                                                            int offset = 0;
                                                            int count  = bytes.Length;

                                                            while (count > 0)
                                                            {
                                                                int writeCount = Math.Min(
                                                                    count, _Hosts.Console.SafeWriteSize);

                                                                binaryWriter.Write(bytes, offset, writeCount);

                                                                offset += writeCount;
                                                                count  -= writeCount;
                                                            }
                                                        }
                                                        else
#endif
                                                        {
                                                            binaryWriter.Write(bytes);
                                                        }

                                                        if (newLine)
                                                        {
                                                            binaryWriter.Write(Channel.NewLine);
                                                        }

#if MONO || MONO_HACKS
                                                        //
                                                        // HACK: *MONO* As of Mono 2.8.0, it seems that Mono "loses"
                                                        //       output unless a flush is performed right after a
                                                        //       write.  So far, this has only been observed for the
                                                        //       console channels; however, always using flush here
                                                        //       on Mono shouldn't cause too many problems, except a
                                                        //       slight loss in performance.
                                                        //       https://bugzilla.novell.com/show_bug.cgi?id=645193
                                                        //
                                                        if (CommonOps.Runtime.IsMono())
                                                        {
                                                            binaryWriter.Flush(); /* throw */
                                                        }
                                                        else
#endif
                                                        {
                                                            //
                                                            // NOTE: Check if we should automatically flush the channel
                                                            //       after each "logical" write done by this command.
                                                            //
                                                            /* IGNORED */
                                                            channel.CheckAutoFlush();
                                                        }

                                                        result = String.Empty;
                                                    }
                                                }
                                                else
                                                {
                                                    result = String.Format(
                                                        "failed to get binary writer for channel \"{0}\"",
                                                        channelId);

                                                    code = ReturnCode.Error;
                                                }
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            Engine.SetExceptionErrorCode(interpreter, e);

                                            result = e;
                                            code   = ReturnCode.Error;
                                        }
                                    }
                                    else
                                    {
                                        result = String.Format(
                                            "failed to get encoding for channel \"{0}\"",
                                            channelId);

                                        code = ReturnCode.Error;
                                    }
                                }
                                else
                                {
                                    code = ReturnCode.Error;
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"puts ?-nonewline? ?channelId? string\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"puts ?-nonewline? ?channelId? string\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #12
0
ファイル: Rename.cs プロジェクト: jdruin/F5Eagle
        ///////////////////////////////////////////////////////////////////////

        #region IExecute Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            if (interpreter == null)
            {
                result = "invalid interpreter";
                return(ReturnCode.Error);
            }

            if (arguments == null)
            {
                result = "invalid argument list";
                return(ReturnCode.Error);
            }

            if (arguments.Count < 3)
            {
                result = WrongNumArgs;
                return(ReturnCode.Error);
            }

            ///////////////////////////////////////////////////////////////////

            OptionDictionary options = new OptionDictionary(new IOption[] {
                new Option(null, OptionFlags.None, Index.Invalid,
                           Index.Invalid, "-nodelete", null),
                new Option(null, OptionFlags.Unsafe, Index.Invalid,
                           Index.Invalid, "-hidden", null),
                new Option(typeof(IdentifierKind),
                           OptionFlags.MustHaveEnumValue | OptionFlags.Unsafe,
                           Index.Invalid, Index.Invalid, "-kind",
                           new Variant(IdentifierKind.None)),
                new Option(null, OptionFlags.MustHaveValue, Index.Invalid,
                           Index.Invalid, "-newnamevar", null),
                new Option(null, OptionFlags.None, Index.Invalid,
                           Index.Invalid, Option.EndOfOptions, null)
            });

            int argumentIndex = Index.Invalid;

            if (interpreter.GetOptions(
                    options, arguments, 0, 1, Index.Invalid, false,
                    ref argumentIndex, ref result) != ReturnCode.Ok)
            {
                return(ReturnCode.Error);
            }

            if ((argumentIndex == Index.Invalid) ||
                ((argumentIndex + 1) >= arguments.Count) ||
                ((argumentIndex + 2) < arguments.Count))
            {
                if ((argumentIndex != Index.Invalid) &&
                    Option.LooksLikeOption(arguments[argumentIndex]))
                {
                    result = OptionDictionary.BadOption(
                        options, arguments[argumentIndex]);
                }
                else
                {
                    result = WrongNumArgs;
                }

                return(ReturnCode.Error);
            }

            ///////////////////////////////////////////////////////////////////

            Variant        value = null;
            IdentifierKind kind  = IdentifierKind.None;

            if (options.IsPresent("-kind", ref value))
            {
                kind = (IdentifierKind)value.Value;
            }

            bool delete = true;

            if (options.IsPresent("-nodelete"))
            {
                delete = false;
            }

            bool hidden = false;

            if (options.IsPresent("-hidden"))
            {
                hidden = true;
            }

            string varName = null;

            if (options.IsPresent("-newnamevar", ref value))
            {
                varName = value.ToString();
            }

            ///////////////////////////////////////////////////////////////////

            string oldName     = arguments[argumentIndex];
            string newName     = arguments[argumentIndex + 1];
            Result localResult = null;

            if (interpreter.RenameAnyIExecute(
                    oldName, newName, varName, kind, false,
                    delete, hidden, ref localResult) == ReturnCode.Ok)
            {
                result = String.Empty;
                return(ReturnCode.Ok);
            }
            else
            {
                result = localResult;
                return(ReturnCode.Error);
            }
        }
コード例 #13
0
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-all", null),   // simple switch
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-force", null), // simple switch
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                        });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex != Index.Invalid) && ((argumentIndex + 1) == arguments.Count))
                            {
                                bool all = false;

                                if (options.IsPresent("-all"))
                                {
                                    all = true;
                                }

                                bool force = false;

                                if (options.IsPresent("-force"))
                                {
                                    force = true;
                                }

                                code = ProcessOps.KillProcess(interpreter, arguments[argumentIndex], all, force, ref result);
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"kill ?-all? ?-force? ?--? process\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"kill ?-all? ?-force? ?--? process\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #14
0
ファイル: Vwait.cs プロジェクト: jdruin/F5Eagle
        ///////////////////////////////////////////////////////////////////////////////////////////////

        #region IExecute Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        //
                        // NOTE: Grab the variable and event wait flags from the
                        //       interpreter and use them as the defaults for the
                        //       associated options.
                        //
                        EventWaitFlags eventWaitFlags = interpreter.EventWaitFlags;
                        VariableFlags  variableFlags  = interpreter.EventVariableFlags;

                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null,
                                       OptionFlags.MustHaveObjectValue | OptionFlags.Unsafe,
                                       Index.Invalid, Index.Invalid, "-handle", null),
                            new Option(typeof(EventWaitFlags),
                                       OptionFlags.MustHaveEnumValue | OptionFlags.Unsafe,
                                       Index.Invalid, Index.Invalid, "-eventwaitflags",
                                       new Variant(eventWaitFlags)),
                            new Option(typeof(VariableFlags),
                                       OptionFlags.MustHaveEnumValue | OptionFlags.Unsafe,
                                       Index.Invalid, Index.Invalid, "-variableflags",
                                       new Variant(variableFlags)),
                            new Option(null,
                                       OptionFlags.MustHaveIntegerValue | OptionFlags.Unsafe,
                                       Index.Invalid, Index.Invalid, "-limit", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid,
                                       Index.Invalid, "-force", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid,
                                       Index.Invalid, "-nocomplain", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid,
                                       Index.Invalid, "-leaveresult", null),
                            new Option(null, OptionFlags.None, Index.Invalid,
                                       Index.Invalid, Option.EndOfOptions, null)
                        });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(
                            options, arguments, 0, 1, Index.Invalid, false,
                            ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex != Index.Invalid) &&
                                ((argumentIndex + 1) == arguments.Count))
                            {
                                Variant         value  = null;
                                EventWaitHandle @event = null;

                                if (options.IsPresent("-handle", ref value))
                                {
                                    IObject @object = (IObject)value.Value;

                                    if ((@object.Value == null) ||
                                        (@object.Value is EventWaitHandle))
                                    {
                                        @event = (EventWaitHandle)@object.Value;
                                    }
                                    else
                                    {
                                        result = "option value has invalid EventWaitHandle";
                                        code   = ReturnCode.Error;
                                    }
                                }

                                if (code == ReturnCode.Ok)
                                {
                                    int limit = 0;

                                    if (options.IsPresent("-limit", ref value))
                                    {
                                        limit = (int)value.Value;
                                    }

                                    if (options.IsPresent("-eventwaitflags", ref value))
                                    {
                                        eventWaitFlags = (EventWaitFlags)value.Value;
                                    }

                                    if (options.IsPresent("-variableflags", ref value))
                                    {
                                        variableFlags = (VariableFlags)value.Value;
                                    }

                                    bool force = false;

                                    if (options.IsPresent("-force"))
                                    {
                                        force = true;
                                    }

                                    bool noComplain = false;

                                    if (options.IsPresent("-nocomplain"))
                                    {
                                        noComplain = true;
                                    }

                                    bool leaveResult = false;

                                    if (options.IsPresent("-leaveresult"))
                                    {
                                        leaveResult = true;
                                    }

                                    //
                                    // NOTE: Typically, we do not want to enter a wait state if
                                    //       there are no events queued because there would be
                                    //       no possible way to ever (gracefully) exit the wait;
                                    //       however, there are exceptions to this.
                                    //
                                    if (force || interpreter.ShouldWaitVariable())
                                    {
                                        //
                                        // HACK: The call to ThreadOps.IsStaThread here is made
                                        //       under the assumption that no user-interface
                                        //       thread can exist without also being an STA
                                        //       thread.  This may eventually prove to be false;
                                        //       however, currently WinForms, WPF, et al require
                                        //       this (i.e. an STA thread).
                                        //
                                        if (!FlagOps.HasFlags(
                                                eventWaitFlags, EventWaitFlags.UserInterface,
                                                true) && ThreadOps.IsStaThread())
                                        {
                                            eventWaitFlags |= EventWaitFlags.UserInterface;
                                        }

                                        code = interpreter.WaitVariable(eventWaitFlags,
                                                                        variableFlags, arguments[argumentIndex], limit,
                                                                        @event, ref result);

                                        if ((code != ReturnCode.Ok) && noComplain)
                                        {
                                            code = ReturnCode.Ok;
                                        }

                                        if ((code == ReturnCode.Ok) && !leaveResult)
                                        {
                                            Engine.ResetResult(interpreter, ref result);
                                        }
                                    }
                                    else
                                    {
                                        result = String.Format(
                                            "can't wait for variable \"{0}\": would wait forever",
                                            arguments[1]);

                                        code = ReturnCode.Error;
                                    }
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(
                                        options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"vwait ?options? varName\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"vwait ?options? varName\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #15
0
ファイル: Switch.cs プロジェクト: jdruin/F5Eagle
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    OptionDictionary options = new OptionDictionary(
                        new IOption[] {
                        new Option(null, OptionFlags.None, 1, Index.Invalid, "-exact", null),
                        new Option(null, OptionFlags.None, 3, Index.Invalid, "-integer", null),
                        new Option(null, OptionFlags.None, 3, Index.Invalid, "-substring", null),
                        new Option(null, OptionFlags.None, 1, Index.Invalid, "-glob", null),
                        new Option(null, OptionFlags.None, 1, Index.Invalid, "-regexp", null),
                        new Option(null, OptionFlags.None, 2, Index.Invalid, "-subst", null),
                        new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nocase", null),
                        new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                    });

                    int argumentIndex = Index.Invalid;

                    if (arguments.Count > 1)
                    {
                        code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);
                    }
                    else
                    {
                        code = ReturnCode.Ok;
                    }

                    if (code == ReturnCode.Ok)
                    {
                        if ((argumentIndex != Index.Invalid) && ((argumentIndex + 1) < arguments.Count))
                        {
                            MatchMode mode = StringOps.DefaultSwitchMatchMode;

                            if (options.IsPresent("-integer"))
                            {
                                mode = MatchMode.Integer;
                            }
                            else if (options.IsPresent("-regexp"))
                            {
                                mode = MatchMode.RegExp;
                            }
                            else if (options.IsPresent("-glob"))
                            {
                                mode = MatchMode.Glob;
                            }
                            else if (options.IsPresent("-substring"))
                            {
                                mode = MatchMode.SubString;
                            }
                            else if (options.IsPresent("-exact"))
                            {
                                mode = MatchMode.Exact;
                            }

                            if (options.IsPresent("-subst"))
                            {
                                mode |= MatchMode.Substitute;
                            }

                            bool noCase = false;

                            if (options.IsPresent("-nocase"))
                            {
                                noCase = true;
                            }

                            bool            splitList = false;
                            StringList      list      = null;
                            IScriptLocation location  = null;

                            //
                            // NOTE: Is there only one argument following the string to match?
                            //
                            if ((argumentIndex + 2) == arguments.Count)
                            {
                                code = Parser.SplitList(
                                    interpreter, arguments[argumentIndex + 1], 0,
                                    Length.Invalid, true, ref list);

                                if (code == ReturnCode.Ok)
                                {
                                    if (list.Count > 0)
                                    {
                                        location  = arguments[argumentIndex + 1];
                                        splitList = true;
                                    }
                                    else
                                    {
                                        result = "wrong # args: should be \"switch ?switches? string {pattern body ... ?default body?}\"";
                                        code   = ReturnCode.Error;
                                    }
                                }
                            }
                            else
                            {
                                //
                                // TODO: Make sure this is always accurate.
                                //
                                code = ScriptOps.GetLocation(
                                    interpreter, arguments, argumentIndex + 1, ref location,
                                    ref result);

                                if (code == ReturnCode.Ok)
                                {
                                    list = ArgumentList.GetRangeAsStringList(arguments, argumentIndex + 1);
                                }
                            }

                            //
                            // NOTE: Ok, now we should have a list of patterns and bodies
                            //       if everything went Ok above.
                            //
                            if (code == ReturnCode.Ok)
                            {
                                //
                                // NOTE: Complain if there is an odd number of words in the
                                //       list of patterns and bodies.
                                //
                                if ((list.Count % 2) == 0)
                                {
                                    //
                                    // NOTE: Complain if the last body is a continuation.
                                    //
                                    if (String.Compare(list[list.Count - 1], Characters.MinusSign.ToString(),
                                                       StringOps.SystemStringComparisonType) != 0)
                                    {
                                        //
                                        // NOTE: Get the text to match against.
                                        //
                                        string input = arguments[argumentIndex];

                                        //
                                        // NOTE: We need to return an empty string if we do not
                                        //       match anything.
                                        //
                                        result = String.Empty;

                                        //
                                        // NOTE: Search the patterns for a match.
                                        //
                                        for (int index = 0; index < list.Count; index += 2)
                                        {
                                            Result pattern = list[index];

                                            bool match = false;

                                            if ((index == (list.Count - 2)) &&
                                                (String.Compare(pattern, Switch.Default, StringOps.SystemStringComparisonType) == 0))
                                            {
                                                //
                                                // NOTE: Default pattern at end always matches.
                                                //
                                                match = true;
                                            }
                                            else
                                            {
                                                if ((mode & MatchMode.Substitute) == MatchMode.Substitute)
                                                {
                                                    code = interpreter.SubstituteString(pattern, ref pattern);
                                                }

                                                if (code != ReturnCode.Ok)
                                                {
                                                    result = pattern;
                                                    break;
                                                }

                                                code = StringOps.Match(
                                                    interpreter, mode, input, pattern, noCase, ref match,
                                                    ref result);

                                                if (code != ReturnCode.Ok)
                                                {
                                                    break;
                                                }
                                            }

                                            if (!match)
                                            {
                                                continue;
                                            }

                                            //
                                            // NOTE: We've got a match. Find a body to execute, skipping
                                            //       bodies that are "-".
                                            //
                                            for (int index2 = index + 1; ; index2 += 2)
                                            {
                                                if (index2 >= list.Count)
                                                {
                                                    result = "fall-out when searching for body to match pattern";
                                                    code   = ReturnCode.Error;

                                                    goto switch_done;
                                                }

                                                if (String.Compare(list[index2], Characters.MinusSign.ToString(),
                                                                   StringOps.SystemStringComparisonType) != 0)
                                                {
                                                    code = interpreter.EvaluateScript(list[index2], location, ref result);

                                                    if (code == ReturnCode.Error)
                                                    {
                                                        Engine.AddErrorInformation(interpreter, result,
                                                                                   String.Format("{0}    (\"{1}\" arm line {2})",
                                                                                                 Environment.NewLine, FormatOps.Ellipsis(pattern),
                                                                                                 Interpreter.GetErrorLine(interpreter)));
                                                    }

                                                    goto switch_done;
                                                }
                                            }
                                        }

switch_done:
                                        ;
                                    }
                                    else
                                    {
                                        result = String.Format(
                                            "no body specified for pattern \"{0}\"",
                                            list[list.Count - 2]);

                                        code = ReturnCode.Error;
                                    }
                                }
                                else
                                {
                                    result = "extra switch pattern with no body";
                                    code   = ReturnCode.Error;

                                    if (splitList)
                                    {
                                        /*
                                         * Check if this can be due to a badly placed comment
                                         * in the switch block.
                                         *
                                         * The following is an heuristic to detect the infamous
                                         * "comment in switch" error: just check if a pattern
                                         * begins with '#'.
                                         */

                                        for (int index = 0; index < list.Count; index++)
                                        {
                                            if (!String.IsNullOrEmpty(list[index]) &&
                                                (list[index][0] == Characters.NumberSign))
                                            {
                                                result += ", this may be due to a comment incorrectly placed " +
                                                          "outside of a switch body - see the \"switch\" documentation";

                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            if ((argumentIndex != Index.Invalid) &&
                                Option.LooksLikeOption(arguments[argumentIndex]))
                            {
                                result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                            }
                            else
                            {
                                result = "wrong # args: should be \"switch ?switches? string pattern body ... ?default body?\"";
                            }

                            code = ReturnCode.Error;
                        }
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #16
0
ファイル: Unload.cs プロジェクト: jdruin/F5Eagle
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.MustHaveObjectValue, Index.Invalid, Index.Invalid, "-clientdata", null),
                            new Option(null, OptionFlags.MustHaveObjectValue, Index.Invalid, Index.Invalid, "-data", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nocase", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-keeplibrary", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nocomplain", null),
                            new Option(null, OptionFlags.MustHaveMatchModeValue, Index.Invalid, Index.Invalid, "-match",
                                       new Variant(StringOps.DefaultUnloadMatchMode)),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                        });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            //
                            // NOTE: There should be a minimum of one and a maximum
                            //       of three arguments after the final option.
                            //
                            if ((argumentIndex != Index.Invalid) &&
                                ((argumentIndex + 3) >= arguments.Count))
                            {
                                string path = ((argumentIndex + 2) < arguments.Count) ?
                                              (string)arguments[argumentIndex + 2] : String.Empty;

                                Interpreter slaveInterpreter = null;

                                code = interpreter.GetNestedSlaveInterpreter(
                                    path, LookupFlags.Interpreter, false,
                                    ref slaveInterpreter, ref result);

                                if (code == ReturnCode.Ok)
                                {
                                    Variant     value           = null;
                                    IClientData localClientData = clientData;

                                    if (options.IsPresent("-clientdata", ref value))
                                    {
                                        IObject @object = (IObject)value.Value;

                                        if ((@object.Value == null) ||
                                            (@object.Value is IClientData))
                                        {
                                            localClientData = (IClientData)@object.Value;
                                        }
                                        else
                                        {
                                            result = "option value has invalid clientData";
                                            code   = ReturnCode.Error;
                                        }
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        if (options.IsPresent("-data", ref value))
                                        {
                                            IObject @object = (IObject)value.Value;

                                            localClientData = _Public.ClientData.WrapOrReplace(
                                                localClientData, @object.Value);
                                        }

                                        MatchMode mode = StringOps.DefaultUnloadMatchMode;

                                        if (options.IsPresent("-match", ref value))
                                        {
                                            mode = (MatchMode)value.Value;
                                        }

                                        bool noCase = false;

                                        if (options.IsPresent("-nocase"))
                                        {
                                            noCase = true;
                                        }

                                        if (slaveInterpreter.HasPlugins(ref result))
                                        {
                                            string fileName = PathOps.ResolveFullPath(
                                                interpreter, arguments[argumentIndex]);

                                            if (!String.IsNullOrEmpty(fileName))
                                            {
                                                string typeName = null;

                                                if ((argumentIndex + 1) < arguments.Count)
                                                {
                                                    typeName = arguments[argumentIndex + 1];
                                                }

                                                //
                                                // NOTE: Grab the plugin flags to match from the target
                                                //       interpreter and add the Demand flag to them.
                                                //
                                                PluginFlags pluginFlags =
                                                    slaveInterpreter.PluginFlags | PluginFlags.Demand;

                                                //
                                                // FIXME: PRI 4: Threading.
                                                //
                                                bool       unload = false;
                                                StringList list   = slaveInterpreter.CopyPluginKeys();

                                                foreach (string name in list)
                                                {
                                                    IPluginData pluginData = slaveInterpreter.GetPluginData(name);

                                                    //
                                                    // NOTE: Check that this plugin represents a loaded
                                                    //       assembly.
                                                    //
                                                    if (pluginData != null)
                                                    {
                                                        if ((pluginData.FileName != null) &&
                                                            PathOps.IsSameFile(interpreter,
                                                                               pluginData.FileName, fileName))
                                                        {
                                                            if (String.IsNullOrEmpty(typeName) ||
                                                                StringOps.Match(interpreter, mode,
                                                                                pluginData.TypeName, typeName, noCase) ||
                                                                StringOps.Match(interpreter, mode,
                                                                                pluginData.Name, typeName, noCase))
                                                            {
                                                                code = slaveInterpreter.UnloadPlugin(
                                                                    name, localClientData, pluginFlags,
                                                                    ref result);

                                                                if (code == ReturnCode.Ok)
                                                                {
                                                                    unload = true;
                                                                }

                                                                //
                                                                // NOTE: Stop as soon as we match and
                                                                //       attempt to unload a plugin,
                                                                //       whether or not we actually
                                                                //       unloaded it.  We always halt
                                                                //       on errors and since we only
                                                                //       support unloading a single
                                                                //       plugin at a time (even if
                                                                //       there are multiple plugins
                                                                //       contained in a particular
                                                                //       assembly file), we know it
                                                                //       is safe to stop now.
                                                                //
                                                                break;
                                                            }
                                                        }
                                                    }
                                                }

                                                if ((code == ReturnCode.Ok) && !unload)
                                                {
                                                    if (typeName != null)
                                                    {
                                                        result = String.Format(
                                                            "type \"{0}\" and file \"{1}\" have never been loaded",
                                                            typeName, fileName);
                                                    }
                                                    else
                                                    {
                                                        result = String.Format(
                                                            "file \"{0}\" has never been loaded",
                                                            fileName);
                                                    }

                                                    code = ReturnCode.Error;
                                                }
                                            }
                                            else
                                            {
                                                result = "invalid file name";
                                                code   = ReturnCode.Error;
                                            }
                                        }
                                        else
                                        {
                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"unload ?options? fileName ?packageName? ?interp?\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"unload ?options? fileName ?packageName? ?interp?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #17
0
ファイル: Subst.cs プロジェクト: jdruin/F5Eagle
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nobackslashes", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nocommands", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-novariables", null) /*,
                                                                                                                    * new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null) */
                        });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex != Index.Invalid) && ((argumentIndex + 1) == arguments.Count))
                            {
                                SubstitutionFlags substitutionFlags = SubstitutionFlags.Default;

                                if (options.IsPresent("-nobackslashes"))
                                {
                                    substitutionFlags &= ~SubstitutionFlags.Backslashes;
                                }

                                if (options.IsPresent("-nocommands"))
                                {
                                    substitutionFlags &= ~SubstitutionFlags.Commands;
                                }

                                if (options.IsPresent("-novariables"))
                                {
                                    substitutionFlags &= ~SubstitutionFlags.Variables;
                                }

                                string name = StringList.MakeList("subst");

                                ICallFrame frame = interpreter.NewTrackingCallFrame(name,
                                                                                    CallFrameFlags.Substitute);

                                interpreter.PushAutomaticCallFrame(frame);

                                code = interpreter.SubstituteString(
                                    arguments[argumentIndex], substitutionFlags, ref result);

                                if (code == ReturnCode.Error)
                                {
                                    Engine.AddErrorInformation(interpreter, result,
                                                               String.Format("{0}    (\"subst\" body line {1})",
                                                                             Environment.NewLine, Interpreter.GetErrorLine(interpreter)));
                                }

                                //
                                // NOTE: Pop the original call frame that we pushed above and
                                //       any intervening scope call frames that may be leftover
                                //       (i.e. they were not explicitly closed).
                                //
                                /* IGNORED */
                                interpreter.PopScopeCallFramesAndOneMore();
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"subst ?-nobackslashes? ?-nocommands? ?-novariables? string\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"subst ?-nobackslashes? ?-nocommands? ?-novariables? string\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #18
0
ファイル: Namespace1.cs プロジェクト: jdruin/F5Eagle
        ///////////////////////////////////////////////////////////////////////

        #region IExecute Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code;

            //
            // NOTE: *WARNING* We do NOT actually support namespaces.  This
            //       command exists for the sole purpose of improving source
            //       code compatibility with simple stand alone scripts that
            //       may simply wrap themselves in a "namespace eval" block,
            //       etc.  Any other (more involved) use may not work at all
            //       or may cause undesired and/or unpredictable results.
            //
            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        string subCommand = arguments[1];
                        bool   tried      = false;

                        code = ScriptOps.TryExecuteSubCommandFromEnsemble(
                            interpreter, this, clientData, arguments, true,
                            false, ref subCommand, ref tried, ref result);

                        if ((code == ReturnCode.Ok) && !tried)
                        {
                            switch (subCommand)
                            {
                            case "children":
                            {
                                if ((arguments.Count >= 2) && (arguments.Count <= 4))
                                {
                                    string name = null;

                                    if (arguments.Count >= 3)
                                    {
                                        name = arguments[2];
                                    }

                                    if ((arguments.Count < 3) ||
                                        NamespaceOps.IsGlobalName(name))
                                    {
                                        result = String.Empty;
                                        code   = ReturnCode.Ok;
                                    }
                                    else
                                    {
                                        if (NamespaceOps.IsAbsoluteName(name))
                                        {
                                            result = String.Format("namespace \"{0}\" not found", name);
                                        }
                                        else
                                        {
                                            result = String.Format("namespace \"{0}\" not found in \"{1}\"",
                                                                   name, TclVars.GlobalNamespace);
                                        }

                                        code = ReturnCode.Error;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace children ?name? ?pattern?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "code":
                            {
                                if (arguments.Count == 3)
                                {
                                    //
                                    // NOTE: We are always in the global namespace, fake it.
                                    //
                                    result = new StringList(
                                        TclVars.GlobalNamespace + this.Name, "inscope",
                                        TclVars.GlobalNamespace, arguments[2]);

                                    code = ReturnCode.Ok;
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace code script\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "current":
                            {
                                if (arguments.Count == 2)
                                {
                                    result = TclVars.GlobalNamespace;
                                    code   = ReturnCode.Ok;
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace current\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "delete":
                            {
                                if (arguments.Count >= 2)
                                {
                                    code = ReturnCode.Ok;

                                    for (int index = 2; index < arguments.Count; index++)
                                    {
                                        if (!NamespaceOps.IsGlobalName(arguments[index]))
                                        {
                                            //
                                            // NOTE: We only know about the global namespace; an attempt
                                            //       to delete any other namespace is an error.
                                            //
                                            result = String.Format(
                                                "unknown namespace \"{0}\" in namespace delete command",
                                                arguments[index]);

                                            code = ReturnCode.Error;
                                            break;
                                        }
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        for (int index = 2; index < arguments.Count; /* index++ */)
                                        {
                                            //
                                            // NOTE: Delete the one-and-only namespace (global) now (actually,
                                            //       as soon as the evaluation stack unwinds).
                                            //
                                            code = interpreter.DeleteNamespace(
                                                VariableFlags.None, arguments[index], false, ref result);

                                            //
                                            // NOTE: Since we only know about the global namespace there
                                            //       is no point in attempting to delete it multiple times.
                                            //
                                            break;
                                        }
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        result = String.Empty;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace delete ?name name ...?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "enable":
                            {
                                if ((arguments.Count >= 2) && (arguments.Count <= 4))
                                {
                                    if (arguments.Count >= 3)
                                    {
                                        bool enabled = false;

                                        code = Value.GetBoolean2(
                                            arguments[2], ValueFlags.AnyBoolean,
                                            interpreter.CultureInfo, ref enabled, ref result);

                                        bool force = false;

                                        if ((code == ReturnCode.Ok) && (arguments.Count >= 4))
                                        {
                                            code = Value.GetBoolean2(
                                                arguments[3], ValueFlags.AnyBoolean,
                                                interpreter.CultureInfo, ref force, ref result);
                                        }

                                        if (code == ReturnCode.Ok)
                                        {
                                            code = NamespaceOps.Enable(
                                                interpreter, enabled, force, ref result);
                                        }
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        result = interpreter.AreNamespacesEnabled();
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace enable ?enabled? ?force?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "eval":
                            {
                                if (arguments.Count >= 4)
                                {
                                    string name = StringList.MakeList("namespace eval", arguments[2]);

                                    ICallFrame frame = interpreter.NewTrackingCallFrame(name,
                                                                                        CallFrameFlags.Namespace | CallFrameFlags.Evaluate);

                                    interpreter.PushAutomaticCallFrame(frame);

                                    if (arguments.Count == 4)
                                    {
                                        code = interpreter.EvaluateScript(arguments[3], ref result);
                                    }
                                    else
                                    {
                                        code = interpreter.EvaluateScript(arguments, 3, ref result);
                                    }

                                    if (code == ReturnCode.Error)
                                    {
                                        Engine.AddErrorInformation(interpreter, result,
                                                                   String.Format("{0}    (in namespace eval \"{1}\" script line {2})",
                                                                                 Environment.NewLine, arguments[2], Interpreter.GetErrorLine(interpreter)));
                                    }

                                    //
                                    // NOTE: Pop the original call frame that we pushed above and
                                    //       any intervening scope call frames that may be leftover
                                    //       (i.e. they were not explicitly closed).
                                    //
                                    /* IGNORED */
                                    interpreter.PopScopeCallFramesAndOneMore();
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace eval name arg ?arg ...?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "exists":
                            {
                                if (arguments.Count == 3)
                                {
                                    if (NamespaceOps.IsGlobalName(arguments[2]))
                                    {
                                        result = true;
                                    }
                                    else
                                    {
                                        result = false;
                                    }

                                    code = ReturnCode.Ok;
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace exists name\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "export":
                            {
                                if (arguments.Count >= 2)
                                {
                                    OptionDictionary options = new OptionDictionary(
                                        new IOption[] {
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-clear", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                        });

                                    int argumentIndex = Index.Invalid;

                                    if (arguments.Count > 2)
                                    {
                                        code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, false, ref argumentIndex, ref result);
                                    }
                                    else
                                    {
                                        code = ReturnCode.Ok;
                                    }

                                    //
                                    // NOTE: We do not support importing or exporting of namespace commands
                                    //       because we do not really support namespaces; therefore, we do
                                    //       nothing.
                                    //
                                    if (code == ReturnCode.Ok)
                                    {
                                        result = String.Empty;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace export ?-clear? ?pattern pattern ...?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "forget":
                            {
                                if (arguments.Count >= 2)
                                {
                                    //
                                    // NOTE: We do not support importing or exporting of namespace commands
                                    //       because we do not really support namespaces; therefore, we do
                                    //       nothing.
                                    //
                                    result = String.Empty;
                                    code   = ReturnCode.Ok;
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace forget ?pattern pattern ...?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "import":
                            {
                                if (arguments.Count >= 2)
                                {
                                    OptionDictionary options = new OptionDictionary(
                                        new IOption[] {
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-force", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                        });

                                    int argumentIndex = Index.Invalid;

                                    if (arguments.Count > 2)
                                    {
                                        code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, false, ref argumentIndex, ref result);
                                    }
                                    else
                                    {
                                        code = ReturnCode.Ok;
                                    }

                                    //
                                    // NOTE: We do not support importing or exporting of namespace commands
                                    //       because we do not really support namespaces; therefore, we do
                                    //       nothing.
                                    //
                                    if (code == ReturnCode.Ok)
                                    {
                                        result = String.Empty;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace import ?-force? ?pattern pattern ...?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "info":
                            {
                                if (arguments.Count == 3)
                                {
                                    code = NamespaceOps.InfoSubCommand(
                                        interpreter, arguments[2], ref result);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace info name\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "inscope":
                            {
                                if (arguments.Count >= 4)
                                {
                                    if (NamespaceOps.IsGlobalName(arguments[2]))
                                    {
                                        if (arguments.Count > 4)
                                        {
                                            IScriptLocation location = null;

#if DEBUGGER && BREAKPOINTS
                                            code = ScriptOps.GetLocation(
                                                interpreter, arguments, 3, ref location,
                                                ref result);

                                            if (code == ReturnCode.Ok)
#endif
                                            {
                                                string name = StringList.MakeList("namespace inscope", arguments[2]);

                                                ICallFrame frame = interpreter.NewTrackingCallFrame(name,
                                                                                                    CallFrameFlags.Namespace | CallFrameFlags.InScope);

                                                interpreter.PushAutomaticCallFrame(frame);

                                                StringList list = new StringList(arguments, 4);

                                                code = interpreter.EvaluateScript(
                                                    ListOps.Concat(arguments[3], list.ToString()),
                                                    location, ref result);

                                                if (code == ReturnCode.Error)
                                                {
                                                    Engine.AddErrorInformation(interpreter, result,
                                                                               String.Format("{0}    (in namespace inscope \"{1}\" script line {2})",
                                                                                             Environment.NewLine, arguments[2], Interpreter.GetErrorLine(interpreter)));
                                                }

                                                //
                                                // NOTE: Pop the original call frame that we pushed above and
                                                //       any intervening scope call frames that may be leftover
                                                //       (i.e. they were not explicitly closed).
                                                //
                                                /* IGNORED */
                                                interpreter.PopScopeCallFramesAndOneMore();
                                            }
                                        }
                                        else
                                        {
                                            string name = StringList.MakeList("namespace inscope", arguments[2]);

                                            ICallFrame frame = interpreter.NewTrackingCallFrame(name,
                                                                                                CallFrameFlags.Namespace | CallFrameFlags.InScope);

                                            interpreter.PushAutomaticCallFrame(frame);

                                            code = interpreter.EvaluateScript(arguments[3], ref result);

                                            if (code == ReturnCode.Error)
                                            {
                                                Engine.AddErrorInformation(interpreter, result,
                                                                           String.Format("{0}    (in namespace inscope \"{1}\" script line {2})",
                                                                                         Environment.NewLine, arguments[2], Interpreter.GetErrorLine(interpreter)));
                                            }

                                            //
                                            // NOTE: Pop the original call frame that we pushed above and
                                            //       any intervening scope call frames that may be leftover
                                            //       (i.e. they were not explicitly closed).
                                            //
                                            /* IGNORED */
                                            interpreter.PopScopeCallFramesAndOneMore();
                                        }
                                    }
                                    else
                                    {
                                        result = String.Format(
                                            "unknown namespace \"{0}\" in inscope namespace command",
                                            arguments[2]);

                                        code = ReturnCode.Error;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace inscope name arg ?arg...?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "mappings":
                            {
                                if (arguments.Count == 2)
                                {
                                    lock (interpreter.SyncRoot)         /* TRANSACTIONAL */
                                    {
                                        StringDictionary namespaceMappings = interpreter.NamespaceMappings;

                                        if (namespaceMappings != null)
                                        {
                                            result = namespaceMappings.KeysAndValuesToString(null, false);
                                            code   = ReturnCode.Ok;
                                        }
                                        else
                                        {
                                            result = "namespace mappings not available";
                                            code   = ReturnCode.Error;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace mappings\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "name":
                            {
                                if (arguments.Count == 3)
                                {
                                    //
                                    // NOTE: We are always in the global namespace, fake it.
                                    //
                                    string name = arguments[2];

                                    if (!NamespaceOps.IsQualifiedName(name))
                                    {
                                        result = NamespaceOps.MakeAbsoluteName(name);
                                        code   = ReturnCode.Ok;
                                    }
                                    else
                                    {
                                        result = "only non-qualified names are allowed";
                                        code   = ReturnCode.Error;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace name name\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "origin":
                            {
                                if (arguments.Count == 3)
                                {
                                    string   executeName = arguments[2];
                                    IExecute execute     = null;

                                    code = interpreter.GetIExecuteViaResolvers(
                                        interpreter.GetResolveEngineFlags(true),
                                        executeName, null, LookupFlags.Default,
                                        ref execute, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        result = TclVars.GlobalNamespace + executeName;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace origin name\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "parent":
                            {
                                if ((arguments.Count == 2) || (arguments.Count == 3))
                                {
                                    //
                                    // NOTE: Either they did not specify a namespace argument (use current
                                    //       namespace, which is always global and always exists) or they
                                    //       specified a namespace which should be the global one; otherwise,
                                    //       an error is reported because we do not really support namespaces.
                                    //
                                    if ((arguments.Count == 2) ||
                                        NamespaceOps.IsGlobalName(arguments[2]))
                                    {
                                        result = String.Empty;
                                        code   = ReturnCode.Ok;
                                    }
                                    else
                                    {
                                        //
                                        // NOTE: See if they prefixed the argument with "::" to figure out
                                        //       the appropriate error message (Tcl emulation).
                                        //
                                        if (NamespaceOps.IsAbsoluteName(arguments[2]))
                                        {
                                            result = String.Format("namespace \"{0}\" not found", arguments[2]);
                                        }
                                        else
                                        {
                                            result = String.Format("namespace \"{0}\" not found in \"{1}\"",
                                                                   arguments[2], TclVars.GlobalNamespace);
                                        }

                                        code = ReturnCode.Error;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace parent ?name?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "qualifiers":
                            {
                                if (arguments.Count == 3)
                                {
                                    string qualifiers = null;
                                    string tail       = null;

                                    code = NamespaceOps.SplitName(
                                        arguments[2], ref qualifiers, ref tail, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        result = qualifiers;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace qualifiers string\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "rename":
                            {
                                if (arguments.Count == 4)
                                {
                                    result = "not implemented";
                                    code   = ReturnCode.Error;
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace rename oldName newName\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "tail":
                            {
                                if (arguments.Count == 3)
                                {
                                    string qualifiers = null;
                                    string tail       = null;

                                    code = NamespaceOps.SplitName(
                                        arguments[2], ref qualifiers, ref tail, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        result = tail;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace tail string\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "unknown":
                            {
                                //
                                // NOTE: The string is currently used as the name of the command
                                //       or procedure to execute when an unknown command is
                                //       encountered by the engine.
                                //
                                if ((arguments.Count == 2) || (arguments.Count == 3))
                                {
                                    if (arguments.Count == 3)
                                    {
                                        string unknown = StringOps.NullIfEmpty(arguments[2]);

                                        if (String.IsNullOrEmpty(unknown))
                                        {
                                            interpreter.NamespaceUnknown = interpreter.GlobalUnknown;
                                        }
                                        else
                                        {
                                            interpreter.NamespaceUnknown = unknown;
                                        }

                                        result = unknown;
                                    }
                                    else
                                    {
                                        result = interpreter.NamespaceUnknown;
                                    }

                                    code = ReturnCode.Ok;
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace unknown ?script?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "which":
                            {
                                //
                                // TODO: *FIXME* Only the global namespace is supported here.
                                //
                                if (arguments.Count >= 2)
                                {
                                    OptionDictionary options = new OptionDictionary(
                                        new IOption[] {
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-command", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-variable", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                        });

                                    int argumentIndex = Index.Invalid;

                                    if (arguments.Count > 2)
                                    {
                                        code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, OptionBehaviorFlags.LastIsNonOption, false, ref argumentIndex, ref result);
                                    }
                                    else
                                    {
                                        code = ReturnCode.Ok;
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        if ((argumentIndex != Index.Invalid) &&
                                            ((argumentIndex + 1) == arguments.Count))
                                        {
                                            string name = arguments[argumentIndex];

                                            bool isCommand = false;

                                            if (options.IsPresent("-command"))
                                            {
                                                isCommand = true;
                                            }

                                            bool isVariable = false;

                                            if (options.IsPresent("-variable"))
                                            {
                                                isVariable = true;
                                            }

                                            if (!isCommand || !isVariable)
                                            {
                                                if (!isCommand && !isVariable)
                                                {
                                                    isCommand = true;
                                                }

                                                if (isCommand)
                                                {
                                                    IExecute execute = null;

                                                    code = interpreter.GetIExecuteViaResolvers(
                                                        interpreter.GetResolveEngineFlags(true),
                                                        name, null, LookupFlags.Default, ref execute,
                                                        ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        result = TclVars.GlobalNamespace +
                                                                 ScriptOps.MakeCommandName(name);
                                                    }
                                                    else
                                                    {
                                                        result = String.Empty;
                                                        code   = ReturnCode.Ok;
                                                    }
                                                }
                                                else
                                                {
                                                    VariableFlags flags    = VariableFlags.NamespaceWhichMask;
                                                    IVariable     variable = null;

                                                    code = interpreter.GetVariableViaResolversWithSplit(
                                                        name, ref flags, ref variable, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        result = TclVars.GlobalNamespace +
                                                                 ScriptOps.MakeVariableName(name);
                                                    }
                                                    else
                                                    {
                                                        result = String.Empty;
                                                        code   = ReturnCode.Ok;
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                result = "wrong # args: should be \"namespace which ?-command? ?-variable? name\"";         /* COMPAT: Tcl */
                                                code   = ReturnCode.Error;
                                            }
                                        }
                                        else
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                Option.LooksLikeOption(arguments[argumentIndex]))
                                            {
                                                result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                            }
                                            else
                                            {
                                                result = "wrong # args: should be \"namespace which ?-command? ?-variable? name\"";
                                            }

                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace which ?-command? ?-variable? name\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            default:
                            {
                                result = ScriptOps.BadSubCommand(
                                    interpreter, null, null, subCommand, this, null, null);

                                code = ReturnCode.Error;
                                break;
                            }
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"namespace subcommand ?arg ...?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #19
0
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        string subCommand = arguments[1];
                        bool   tried      = false;

                        code = ScriptOps.TryExecuteSubCommandFromEnsemble(
                            interpreter, this, clientData, arguments, true,
                            false, ref subCommand, ref tried, ref result);

                        if ((code == ReturnCode.Ok) && !tried)
                        {
                            switch (subCommand)
                            {
                            case "deserialize":
                            {
                                if (arguments.Count >= 4)
                                {
#if SERIALIZATION
                                    OptionDictionary options = ObjectOps.GetDeserializeOptions();

                                    int argumentIndex = Index.Invalid;

                                    code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        if ((argumentIndex != Index.Invalid) && ((argumentIndex + 2) == arguments.Count))
                                        {
                                            bool verbose;
                                            bool strictType;
                                            bool noCase;

                                            ObjectOps.ProcessGetTypeOptions(
                                                options, out verbose, out strictType, out noCase);

                                            Type        returnType;
                                            ObjectFlags objectFlags;
                                            string      objectName;
                                            string      interpName;
                                            bool        create;
                                            bool        dispose;
                                            bool        alias;
                                            bool        aliasRaw;
                                            bool        aliasAll;
                                            bool        aliasReference;
                                            bool        toString;

                                            ObjectOps.ProcessFixupReturnValueOptions(
                                                options, null, out returnType, out objectFlags,
                                                out objectName, out interpName, out create,
                                                out dispose, out alias, out aliasRaw, out aliasAll,
                                                out aliasReference, out toString);

                                            if (noCase)
                                            {
                                                objectFlags |= ObjectFlags.NoCase;
                                            }

                                            Variant  value    = null;
                                            Encoding encoding = null;

                                            if (options.IsPresent("-encoding", ref value))
                                            {
                                                encoding = (Encoding)value.Value;
                                            }

                                            if (code == ReturnCode.Ok)
                                            {
                                                Type       objectType = null;
                                                ResultList errors     = null;

                                                code = Value.GetType(interpreter,
                                                                     arguments[argumentIndex], null, interpreter.GetAppDomain(),
                                                                     Value.GetTypeValueFlags(strictType, verbose, noCase),
                                                                     interpreter.CultureInfo, ref objectType, ref errors);

                                                if (code == ReturnCode.Ok)
                                                {
                                                    byte[] bytes = null;

                                                    code = StringOps.GetBytes(
                                                        encoding, arguments[argumentIndex + 1],
                                                        EncodingType.Default, ref bytes, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        object @object = null;

                                                        code = XmlOps.Deserialize(
                                                            objectType, bytes, ref @object, ref result);

                                                        if (code == ReturnCode.Ok)
                                                        {
                                                            ObjectOptionType objectOptionType =
                                                                ObjectOptionType.Deserialize |
                                                                ObjectOps.GetOptionType(aliasRaw, aliasAll);

                                                            code = MarshalOps.FixupReturnValue(
                                                                interpreter, interpreter.Binder, interpreter.CultureInfo,
                                                                returnType, objectFlags, ObjectOps.GetInvokeOptions(
                                                                    objectOptionType), objectOptionType, objectName, interpName,
                                                                @object, create, dispose, alias, aliasReference, toString,
                                                                ref result);
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    errors.Insert(0, String.Format(
                                                                      "type \"{0}\" not found",
                                                                      arguments[argumentIndex]));

                                                    result = errors;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                Option.LooksLikeOption(arguments[argumentIndex]))
                                            {
                                                result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                            }
                                            else
                                            {
                                                result = "wrong # args: should be \"xml deserialize ?options? type xml\"";
                                            }

                                            code = ReturnCode.Error;
                                        }
                                    }
#else
                                    result = "not implemented";
                                    code   = ReturnCode.Error;
#endif
                                }
                                else
                                {
                                    result = "wrong # args: should be \"xml deserialize ?options? type xml\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "serialize":
                            {
                                if (arguments.Count >= 4)
                                {
#if SERIALIZATION
                                    OptionDictionary options = ObjectOps.GetSerializeOptions();

                                    int argumentIndex = Index.Invalid;

                                    code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        if ((argumentIndex != Index.Invalid) && ((argumentIndex + 2) == arguments.Count))
                                        {
                                            bool noCase = false;

                                            if (options.IsPresent("-nocase"))
                                            {
                                                noCase = true;
                                            }

                                            bool strictType = false;

                                            if (options.IsPresent("-stricttype"))
                                            {
                                                strictType = true;
                                            }

                                            bool verbose = false;

                                            if (options.IsPresent("-verbose"))
                                            {
                                                verbose = true;
                                            }

                                            Variant  value    = null;
                                            Encoding encoding = null;

                                            if (options.IsPresent("-encoding", ref value))
                                            {
                                                encoding = (Encoding)value.Value;
                                            }

                                            if (code == ReturnCode.Ok)
                                            {
                                                Type       objectType = null;
                                                ResultList errors     = null;

                                                code = Value.GetType(interpreter,
                                                                     arguments[argumentIndex], null, interpreter.GetAppDomain(),
                                                                     Value.GetTypeValueFlags(strictType, verbose, noCase),
                                                                     interpreter.CultureInfo, ref objectType, ref errors);

                                                if (code == ReturnCode.Ok)
                                                {
                                                    IObject @object = null;

                                                    code = interpreter.GetObject(
                                                        arguments[argumentIndex + 1], LookupFlags.Default,
                                                        ref @object, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        byte[] bytes = null;

                                                        code = XmlOps.Serialize(
                                                            (@object != null) ? @object.Value : null,
                                                            objectType, null, ref bytes, ref result);

                                                        if (code == ReturnCode.Ok)
                                                        {
                                                            string stringValue = null;

                                                            code = StringOps.GetString(
                                                                encoding, bytes, EncodingType.Default,
                                                                ref stringValue, ref result);

                                                            if (code == ReturnCode.Ok)
                                                            {
                                                                result = stringValue;
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    errors.Insert(0, String.Format(
                                                                      "type \"{0}\" not found",
                                                                      arguments[argumentIndex]));

                                                    result = errors;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                Option.LooksLikeOption(arguments[argumentIndex]))
                                            {
                                                result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                            }
                                            else
                                            {
                                                result = "wrong # args: should be \"xml serialize ?options? type object\"";
                                            }

                                            code = ReturnCode.Error;
                                        }
                                    }
#else
                                    result = "not implemented";
                                    code   = ReturnCode.Error;
#endif
                                }
                                else
                                {
                                    result = "wrong # args: should be \"xml serialize ?options? type object\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "validate":
                            {
                                if (arguments.Count == 4)
                                {
                                    XmlDocument document = null;

                                    code = XmlOps.LoadString(
                                        arguments[3], ref document, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        code = XmlOps.Validate(
                                            arguments[2], document, ref result);

                                        if (code == ReturnCode.Ok)
                                        {
                                            result = String.Empty;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"xml validate schemaXml documentXml\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            default:
                            {
                                result = ScriptOps.BadSubCommand(
                                    interpreter, null, null, subCommand, this, null, null);

                                code = ReturnCode.Error;
                                break;
                            }
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"xml option ?arg ...?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #20
0
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nonewline", null)
                        });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex != Index.Invalid) && ((argumentIndex + 2) >= arguments.Count))
                            {
                                bool newLine = true;

                                if (options.IsPresent("-nonewline"))
                                {
                                    newLine = false;
                                }

                                int count = Count.Invalid;

                                if ((argumentIndex + 1) < arguments.Count)
                                {
                                    code = Value.GetInteger2(
                                        (IGetValue)arguments[argumentIndex + 1], ValueFlags.AnyInteger,
                                        interpreter.CultureInfo, ref count, ref result);
                                }

                                if (code == ReturnCode.Ok)
                                {
                                    string  channelId = arguments[argumentIndex];
                                    Channel channel   = interpreter.GetChannel(channelId, ref result);

                                    if (channel != null)
                                    {
                                        CharList endOfLine = channel.GetInputEndOfLine();
                                        Encoding encoding  = null;

                                        if (interpreter.GetChannelEncoding(channel, ref encoding) == ReturnCode.Ok)
                                        {
                                            //
                                            // NOTE: If they do not specify a count we simply read
                                            //       until end-of-file.
                                            //
                                            try
                                            {
                                                ByteList buffer = null;

                                                code = channel.Read(count, null, false, ref buffer, ref result);

                                                if (code == ReturnCode.Ok)
                                                {
                                                    //
                                                    // BUGFIX: Remove trailing end-of-line character even
                                                    //         when reading the entire stream.
                                                    //
                                                    if (!newLine)
                                                    {
                                                        channel.RemoveTrailingEndOfLine(buffer, endOfLine);
                                                    }

                                                    string stringValue = null;

                                                    code = StringOps.GetString(
                                                        encoding, buffer.ToArray(), EncodingType.Binary,
                                                        ref stringValue, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        result = stringValue;
                                                    }
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                Engine.SetExceptionErrorCode(interpreter, e);

                                                result = e;
                                                code   = ReturnCode.Error;
                                            }
                                        }
                                        else
                                        {
                                            result = String.Format(
                                                "failed to get encoding for channel \"{0}\"",
                                                channelId);

                                            code = ReturnCode.Error;
                                        }
                                    }
                                    else
                                    {
                                        code = ReturnCode.Error;
                                    }
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"read channelId ?numChars?\" or \"read ?-nonewline? channelId\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"read channelId ?numChars?\" or \"read ?-nonewline? channelId\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #21
0
ファイル: Namespace2.cs プロジェクト: jdruin/F5Eagle
        ///////////////////////////////////////////////////////////////////////

        #region IExecute Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code;

            //
            // NOTE: *WARNING* We do NOT actually support namespaces.  This
            //       command exists for the sole purpose of improving source
            //       code compatibility with simple stand alone scripts that
            //       may simply wrap themselves in a "namespace eval" block,
            //       etc.  Any other (more involved) use may not work at all
            //       or may cause undesired and/or unpredictable results.
            //
            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        string subCommand = arguments[1];
                        bool   tried      = false;

                        code = ScriptOps.TryExecuteSubCommandFromEnsemble(
                            interpreter, this, clientData, arguments, true,
                            false, ref subCommand, ref tried, ref result);

                        if ((code == ReturnCode.Ok) && !tried)
                        {
                            switch (subCommand)
                            {
                            case "children":
                            {
                                if ((arguments.Count >= 2) && (arguments.Count <= 4))
                                {
                                    string name = null;

                                    if (arguments.Count >= 3)
                                    {
                                        name = arguments[2];
                                    }

                                    string pattern = null;

                                    if (arguments.Count >= 4)
                                    {
                                        pattern = arguments[3];
                                    }

                                    IEnumerable <INamespace> children = NamespaceOps.Children(
                                        interpreter, name, pattern, false, ref result);

                                    if (children != null)
                                    {
                                        StringList list = new StringList();

                                        foreach (INamespace child in children)
                                        {
                                            list.Add(child.QualifiedName);
                                        }

                                        result = list;
                                    }
                                    else
                                    {
                                        code = ReturnCode.Error;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace children ?name? ?pattern?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "code":
                            {
                                if (arguments.Count == 3)
                                {
                                    string text = arguments[2];

                                    if (!NamespaceOps.IsSubCommand(interpreter, text, "inscope"))
                                    {
                                        INamespace currentNamespace = null;

                                        code = interpreter.GetCurrentNamespaceViaResolvers(
                                            null, LookupFlags.Default, ref currentNamespace,
                                            ref result);

                                        if (code == ReturnCode.Ok)
                                        {
                                            StringList list = new StringList();

                                            list.Add(NamespaceOps.MakeAbsoluteName(
                                                         this.Name));

                                            list.Add("inscope");

                                            list.Add(NamespaceOps.MakeAbsoluteName(
                                                         currentNamespace.QualifiedName));

                                            list.Add(text);

                                            result = list;
                                        }
                                    }
                                    else
                                    {
                                        result = text;         /* COMPAT: Tcl. */
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace code script\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "current":
                            {
                                if (arguments.Count == 2)
                                {
                                    INamespace currentNamespace = null;

                                    code = interpreter.GetCurrentNamespaceViaResolvers(
                                        null, LookupFlags.Default, ref currentNamespace,
                                        ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        if (currentNamespace != null)
                                        {
                                            result = currentNamespace.QualifiedName;
                                        }
                                        else
                                        {
                                            result = "current namespace is invalid";
                                            code   = ReturnCode.Error;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace current\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "delete":
                            {
                                if (arguments.Count >= 2)
                                {
                                    for (int index = 2; index < arguments.Count; index++)
                                    {
                                        code = interpreter.DeleteNamespace(
                                            VariableFlags.None, arguments[index], false,
                                            ref result);

                                        if (code != ReturnCode.Ok)
                                        {
                                            break;
                                        }
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        result = String.Empty;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace delete ?name name ...?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "enable":
                            {
                                if ((arguments.Count >= 2) && (arguments.Count <= 4))
                                {
                                    if (arguments.Count >= 3)
                                    {
                                        bool enabled = false;

                                        code = Value.GetBoolean2(
                                            arguments[2], ValueFlags.AnyBoolean,
                                            interpreter.CultureInfo, ref enabled, ref result);

                                        bool force = false;

                                        if ((code == ReturnCode.Ok) && (arguments.Count >= 4))
                                        {
                                            code = Value.GetBoolean2(
                                                arguments[3], ValueFlags.AnyBoolean,
                                                interpreter.CultureInfo, ref force, ref result);
                                        }

                                        if (code == ReturnCode.Ok)
                                        {
                                            code = NamespaceOps.Enable(
                                                interpreter, enabled, force, ref result);
                                        }
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        result = interpreter.AreNamespacesEnabled();
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace enable ?enabled? ?force?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "eval":
                            {
                                if (arguments.Count >= 4)
                                {
                                    string namespaceName = NamespaceOps.MapName(
                                        interpreter, arguments[2]);

                                    INamespace @namespace = NamespaceOps.Lookup(
                                        interpreter, namespaceName, false, true,
                                        ref result);

                                    if (@namespace != null)
                                    {
                                        string name = StringList.MakeList("namespace eval", @namespace.QualifiedName);

                                        ICallFrame frame = interpreter.NewNamespaceCallFrame(
                                            name, CallFrameFlags.Evaluate | CallFrameFlags.UseNamespace,
                                            arguments, @namespace, false);

                                        interpreter.PushNamespaceCallFrame(frame);

                                        if (arguments.Count == 4)
                                        {
                                            code = interpreter.EvaluateScript(arguments[3], ref result);
                                        }
                                        else
                                        {
                                            code = interpreter.EvaluateScript(arguments, 3, ref result);
                                        }

                                        if (code == ReturnCode.Error)
                                        {
                                            Engine.AddErrorInformation(interpreter, result,
                                                                       String.Format("{0}    (in namespace eval \"{1}\" script line {2})",
                                                                                     Environment.NewLine, NamespaceOps.MaybeQualifiedName(@namespace,
                                                                                                                                          true), Interpreter.GetErrorLine(interpreter)));
                                        }

                                        /* IGNORED */
                                        interpreter.PopNamespaceCallFrame(frame);

                                        /* NO RESULT */
                                        Engine.CleanupNamespacesOrComplain(interpreter);
                                    }
                                    else
                                    {
                                        code = ReturnCode.Error;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace eval name arg ?arg ...?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "exists":
                            {
                                if (arguments.Count == 3)
                                {
                                    result = ConversionOps.ToInt(NamespaceOps.Lookup(
                                                                     interpreter, arguments[2], false, false) != null);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace exists name\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "export":
                            {
                                if (arguments.Count >= 2)
                                {
                                    OptionDictionary options = new OptionDictionary(
                                        new IOption[] {
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-clear", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                        });

                                    int argumentIndex = Index.Invalid;

                                    if (arguments.Count > 2)
                                    {
                                        code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, false, ref argumentIndex, ref result);
                                    }
                                    else
                                    {
                                        code = ReturnCode.Ok;
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        bool clear = false;

                                        if (options.IsPresent("-clear"))
                                        {
                                            clear = true;
                                        }

                                        StringList patterns = new StringList();

                                        if (argumentIndex != Index.Invalid)
                                        {
                                            patterns.AddObjects(ArgumentList.GetRange(arguments, argumentIndex));
                                        }

                                        code = NamespaceOps.Export(interpreter, null, patterns, clear, ref result);
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace export ?-clear? ?pattern pattern ...?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "forget":
                            {
                                if (arguments.Count >= 2)
                                {
                                    if (arguments.Count >= 3)
                                    {
                                        code = NamespaceOps.Forget(interpreter,
                                                                   new StringList(ArgumentList.GetRange(arguments, 2)),
                                                                   ref result);

                                        if (code == ReturnCode.Ok)
                                        {
                                            result = String.Empty;
                                        }
                                    }
                                    else
                                    {
                                        result = String.Empty;
                                        code   = ReturnCode.Ok;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace forget ?pattern pattern ...?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "import":
                            {
                                if (arguments.Count >= 2)
                                {
                                    OptionDictionary options = new OptionDictionary(
                                        new IOption[] {
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-force", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                        });

                                    int argumentIndex = Index.Invalid;

                                    if (arguments.Count > 2)
                                    {
                                        code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, false, ref argumentIndex, ref result);
                                    }
                                    else
                                    {
                                        code = ReturnCode.Ok;
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        bool force = false;

                                        if (options.IsPresent("-force"))
                                        {
                                            force = true;
                                        }

                                        StringList patterns = new StringList();

                                        if (argumentIndex != Index.Invalid)
                                        {
                                            patterns.AddObjects(ArgumentList.GetRange(arguments, argumentIndex));
                                        }

                                        code = NamespaceOps.Import(interpreter, patterns, force, ref result);
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace import ?-force? ?pattern pattern ...?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "info":
                            {
                                if (arguments.Count == 3)
                                {
                                    code = NamespaceOps.InfoSubCommand(
                                        interpreter, arguments[2], ref result);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace info name\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "inscope":
                            {
                                if (arguments.Count >= 4)
                                {
                                    string namespaceName = NamespaceOps.MapName(
                                        interpreter, arguments[2]);

                                    INamespace @namespace = NamespaceOps.Lookup(
                                        interpreter, namespaceName, false, false,
                                        ref result);

                                    if (@namespace != null)
                                    {
                                        if (arguments.Count > 4)
                                        {
                                            IScriptLocation location = null;

#if DEBUGGER && BREAKPOINTS
                                            code = ScriptOps.GetLocation(
                                                interpreter, arguments, 3, ref location,
                                                ref result);

                                            if (code == ReturnCode.Ok)
#endif
                                            {
                                                string name = StringList.MakeList("namespace inscope", @namespace.QualifiedName);

                                                ICallFrame frame = interpreter.NewNamespaceCallFrame(
                                                    name, CallFrameFlags.InScope | CallFrameFlags.UseNamespace,
                                                    arguments, @namespace, false);

                                                interpreter.PushNamespaceCallFrame(frame);

                                                StringList list = new StringList(arguments, 4);

                                                code = interpreter.EvaluateScript(
                                                    ListOps.Concat(arguments[3], list.ToString()),
                                                    location, ref result);

                                                if (code == ReturnCode.Error)
                                                {
                                                    Engine.AddErrorInformation(interpreter, result,
                                                                               String.Format("{0}    (in namespace inscope \"{1}\" script line {2})",
                                                                                             Environment.NewLine, NamespaceOps.MaybeQualifiedName(@namespace,
                                                                                                                                                  true), Interpreter.GetErrorLine(interpreter)));
                                                }

                                                /* IGNORED */
                                                interpreter.PopNamespaceCallFrame(frame);

                                                /* NO RESULT */
                                                Engine.CleanupNamespacesOrComplain(interpreter);
                                            }
                                        }
                                        else
                                        {
                                            string name = StringList.MakeList("namespace inscope", @namespace.QualifiedName);

                                            ICallFrame frame = interpreter.NewNamespaceCallFrame(
                                                name, CallFrameFlags.InScope | CallFrameFlags.UseNamespace,
                                                arguments, @namespace, false);

                                            interpreter.PushNamespaceCallFrame(frame);

                                            code = interpreter.EvaluateScript(arguments[3], ref result);

                                            if (code == ReturnCode.Error)
                                            {
                                                Engine.AddErrorInformation(interpreter, result,
                                                                           String.Format("{0}    (in namespace inscope \"{1}\" script line {2})",
                                                                                         Environment.NewLine, NamespaceOps.MaybeQualifiedName(@namespace,
                                                                                                                                              true), Interpreter.GetErrorLine(interpreter)));
                                            }

                                            /* IGNORED */
                                            interpreter.PopNamespaceCallFrame(frame);

                                            /* NO RESULT */
                                            Engine.CleanupNamespacesOrComplain(interpreter);
                                        }
                                    }
                                    else
                                    {
                                        code = ReturnCode.Error;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace inscope name arg ?arg...?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "mappings":
                            {
                                if (arguments.Count == 2)
                                {
                                    lock (interpreter.SyncRoot)         /* TRANSACTIONAL */
                                    {
                                        StringDictionary namespaceMappings = interpreter.NamespaceMappings;

                                        if (namespaceMappings != null)
                                        {
                                            result = namespaceMappings.KeysAndValuesToString(null, false);
                                            code   = ReturnCode.Ok;
                                        }
                                        else
                                        {
                                            result = "namespace mappings not available";
                                            code   = ReturnCode.Error;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace mappings\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "name":
                            {
                                if (arguments.Count == 3)
                                {
                                    string name = arguments[2];

                                    if (!NamespaceOps.IsQualifiedName(name))
                                    {
                                        result = NamespaceOps.MakeQualifiedName(interpreter, name, true);
                                        code   = ReturnCode.Ok;
                                    }
                                    else
                                    {
                                        result = "only non-qualified names are allowed";
                                        code   = ReturnCode.Error;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace name name\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "origin":
                            {
                                if (arguments.Count == 3)
                                {
                                    code = NamespaceOps.Origin(interpreter, null, arguments[2], ref result);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace origin name\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "parent":
                            {
                                if ((arguments.Count == 2) || (arguments.Count == 3))
                                {
                                    code = NamespaceOps.Parent(
                                        interpreter, (arguments.Count == 3) ? arguments[2] : null,
                                        ref result);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace parent ?name?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "qualifiers":
                            {
                                if (arguments.Count == 3)
                                {
                                    string qualifiers = null;
                                    string tail       = null;

                                    code = NamespaceOps.SplitName(
                                        arguments[2], ref qualifiers, ref tail, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        result = qualifiers;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace qualifiers string\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "rename":
                            {
                                if (arguments.Count == 4)
                                {
                                    code = interpreter.RenameNamespace(
                                        arguments[2], arguments[3], RenameGlobalOk,
                                        RenameInUseOk, ref result);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace rename oldName newName\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "tail":
                            {
                                if (arguments.Count == 3)
                                {
                                    string qualifiers = null;
                                    string tail       = null;

                                    code = NamespaceOps.SplitName(
                                        arguments[2], ref qualifiers, ref tail, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        result = tail;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace tail string\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "unknown":
                            {
                                if ((arguments.Count == 2) || (arguments.Count == 3))
                                {
                                    INamespace currentNamespace = null;

                                    code = interpreter.GetCurrentNamespaceViaResolvers(
                                        null, LookupFlags.Default, ref currentNamespace,
                                        ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        if (currentNamespace != null)
                                        {
                                            if (arguments.Count == 3)
                                            {
                                                string unknown = StringOps.NullIfEmpty(arguments[2]);

                                                if (String.IsNullOrEmpty(unknown) &&
                                                    NamespaceOps.IsGlobal(interpreter, currentNamespace))
                                                {
                                                    currentNamespace.Unknown = interpreter.GlobalUnknown;
                                                }
                                                else
                                                {
                                                    currentNamespace.Unknown = unknown;
                                                }

                                                result = unknown;
                                            }
                                            else
                                            {
                                                result = currentNamespace.Unknown;
                                            }
                                        }
                                        else
                                        {
                                            if (arguments.Count == 3)
                                            {
                                                string unknown = StringOps.NullIfEmpty(arguments[2]);

                                                if (String.IsNullOrEmpty(unknown))
                                                {
                                                    interpreter.GlobalUnknown = TclVars.Unknown;
                                                }
                                                else
                                                {
                                                    interpreter.GlobalUnknown = unknown;
                                                }

                                                result = unknown;
                                            }
                                            else
                                            {
                                                result = interpreter.GlobalUnknown;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace unknown ?script?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "which":
                            {
                                if (arguments.Count >= 2)
                                {
                                    OptionDictionary options = new OptionDictionary(
                                        new IOption[] {
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-command", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-variable", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                        });

                                    int argumentIndex = Index.Invalid;

                                    if (arguments.Count > 2)
                                    {
                                        code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, OptionBehaviorFlags.LastIsNonOption, false, ref argumentIndex, ref result);
                                    }
                                    else
                                    {
                                        code = ReturnCode.Ok;
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        if ((argumentIndex != Index.Invalid) &&
                                            ((argumentIndex + 1) == arguments.Count))
                                        {
                                            string name = arguments[argumentIndex];

                                            bool isCommand = false;

                                            if (options.IsPresent("-command"))
                                            {
                                                isCommand = true;
                                            }

                                            bool isVariable = false;

                                            if (options.IsPresent("-variable"))
                                            {
                                                isVariable = true;
                                            }

                                            if (!isCommand || !isVariable)
                                            {
                                                NamespaceFlags flags = NamespaceFlags.None;

                                                if (isCommand)
                                                {
                                                    flags |= NamespaceFlags.Command;
                                                }
                                                else if (isVariable)
                                                {
                                                    flags |= NamespaceFlags.Variable;
                                                }
                                                else
                                                {
                                                    flags |= NamespaceFlags.Command;
                                                }

                                                code = NamespaceOps.Which(interpreter, null, name, flags, ref result);
                                            }
                                            else
                                            {
                                                result = "wrong # args: should be \"namespace which ?-command? ?-variable? name\"";
                                                code   = ReturnCode.Error;
                                            }
                                        }
                                        else
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                Option.LooksLikeOption(arguments[argumentIndex]))
                                            {
                                                result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                            }
                                            else
                                            {
                                                result = "wrong # args: should be \"namespace which ?-command? ?-variable? name\"";
                                            }

                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"namespace which ?-command? ?-variable? name\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            default:
                            {
                                result = ScriptOps.BadSubCommand(
                                    interpreter, null, null, subCommand, this, null, null);

                                code = ReturnCode.Error;
                                break;
                            }
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"namespace subcommand ?arg ...?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #22
0
ファイル: Exit.cs プロジェクト: jdruin/F5Eagle
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 1)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-force", null),
                            new Option(null, OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-fail", null),
                            new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-message", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-current", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                        });

                        int argumentIndex = Index.Invalid;

                        if (arguments.Count > 1)
                        {
                            code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);
                        }
                        else
                        {
                            code = ReturnCode.Ok;
                        }

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex == Index.Invalid) ||
                                ((argumentIndex + 1) == arguments.Count))
                            {
                                bool force = false;

                                if (options.IsPresent("-force"))
                                {
                                    force = true;
                                }

                                bool fail = false;

                                if (options.IsPresent("-fail"))
                                {
                                    fail = true;
                                }

                                Variant value   = null;
                                string  message = null;

                                if (options.IsPresent("-message", ref value))
                                {
                                    message = value.ToString();
                                }

                                //
                                // NOTE: The default exit code is "success" (i.e. zero).
                                //
                                ExitCode exitCode = ResultOps.SuccessExitCode();

                                if (options.IsPresent("-current"))
                                {
                                    exitCode = interpreter.ExitCode;
                                }

                                //
                                // NOTE: Was an exit code specified in the command?
                                //
                                if (argumentIndex != Index.Invalid)
                                {
                                    object enumValue = EnumOps.TryParseEnum(
                                        typeof(ExitCode), arguments[argumentIndex],
                                        true, true, ref result);

                                    if (enumValue is ExitCode)
                                    {
                                        exitCode = (ExitCode)enumValue;
                                    }
                                    else
                                    {
                                        result = ScriptOps.BadValue(
                                            null, "exit code", arguments[argumentIndex],
                                            Enum.GetNames(typeof(ExitCode)), null, ", or an integer");

                                        code = ReturnCode.Error;
                                    }
                                }

                                //
                                // NOTE: Make sure we succeeded at coverting the exit code to an integer.
                                //
                                if (code == ReturnCode.Ok)
                                {
                                    //
                                    // NOTE: Make sure the interpreter host, if any, agrees to exit (i.e. it may deny the
                                    //       request if the application is doing something that should not be interrupted).
                                    //
                                    code = interpreter.CanExit(exitCode, force, fail, message, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        //
                                        // NOTE: Exit the application (either by marking the current interpreter as "exited"
                                        //       or physically exiting the containing process).
                                        //
                                        TraceOps.DebugTrace(String.Format(
                                                                "Execute: {0}, interpreter = {1}, message = {2}",
                                                                force && fail ?
                                                                "forcibly failing" : force ? "forcibly exiting" : "exiting",
                                                                FormatOps.InterpreterNoThrow(interpreter), FormatOps.WrapOrNull(message)),
                                                            typeof(Exit).Name, TracePriority.Command);

                                        if (force)
                                        {
#if !MONO
                                            if (fail && !CommonOps.Runtime.IsMono())
                                            {
                                                try
                                                {
                                                    //
                                                    // NOTE: Using this method to exit a script is NOT recommended unless
                                                    //       you are trying to prevent damaging another part of the system.
                                                    //
                                                    // MONO: This method is not supported by the Mono runtime.
                                                    //
                                                    Environment.FailFast(message);

                                                    /* NOT REACHED */
                                                    result = "failed to exit process";
                                                    code   = ReturnCode.Error;
                                                }
                                                catch (Exception e)
                                                {
                                                    result = e;
                                                    code   = ReturnCode.Error;
                                                }
                                            }
                                            else
#endif
                                            {
                                                //
                                                // BUGFIX: Try to dispose our containing interpreter now.  We must do
                                                //         this to prevent it from being disposed on a random GC thread.
                                                //
                                                try
                                                {
                                                    interpreter.Dispose();
                                                    interpreter = null;
                                                }
                                                catch (Exception e)
                                                {
                                                    result = e;
                                                    code   = ReturnCode.Error;
                                                }

                                                //
                                                // NOTE: If we could not dispose the interpreter properly, complain;
                                                //       however, keep exiting anyway.
                                                //
                                                if (code != ReturnCode.Ok)
                                                {
                                                    DebugOps.Complain(interpreter, code, result);
                                                }

                                                try
                                                {
                                                    //
                                                    // NOTE: Using this method to exit a script is NOT recommended unless
                                                    //       you are running a standalone script in the Eagle Shell (i.e.
                                                    //       you are not hosted within another application).
                                                    //
                                                    Environment.Exit((int)exitCode);

                                                    /* NOT REACHED */
                                                    result = "failed to exit process";
                                                    code   = ReturnCode.Error;
                                                }
                                                catch (Exception e)
                                                {
                                                    result = e;
                                                    code   = ReturnCode.Error;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            interpreter.ExitCode = exitCode;
                                            interpreter.Exit     = true;

                                            result = String.Empty;
                                            code   = ReturnCode.Ok;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"exit ?options? ?returnCode?\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"exit ?options? ?returnCode?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #23
0
ファイル: Package.cs プロジェクト: jdruin/F5Eagle
        ///////////////////////////////////////////////////////////////////////////////////////////////

        #region IExecute Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        string subCommand = arguments[1];
                        bool   tried      = false;

                        code = ScriptOps.TryExecuteSubCommandFromEnsemble(
                            interpreter, this, clientData, arguments, true,
                            false, ref subCommand, ref tried, ref result);

                        if ((code == ReturnCode.Ok) && !tried)
                        {
                            switch (subCommand)
                            {
                            case "forget":
                            {
                                if (arguments.Count >= 2)
                                {
                                    code = interpreter.PkgForget(
                                        new StringList(arguments, 2), ref result);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package forget ?package package ...?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "ifneeded":
                            {
                                if ((arguments.Count == 4) || (arguments.Count == 5))
                                {
                                    Version version = null;

                                    code = Value.GetVersion(
                                        arguments[3], interpreter.CultureInfo,
                                        ref version, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        string text = null;

                                        if (arguments.Count == 5)
                                        {
                                            text = arguments[4];
                                        }

                                        code = interpreter.PkgIfNeeded(
                                            arguments[2], version, text, interpreter.PackageFlags,
                                            ref result);
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package ifneeded package version ?script?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "indexes":
                            {
                                if ((arguments.Count == 2) || (arguments.Count == 3))
                                {
                                    string pattern = null;

                                    if (arguments.Count == 3)
                                    {
                                        pattern = arguments[2];
                                    }

                                    code = interpreter.PkgIndexes(
                                        pattern, false, ref result);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package indexes ?pattern?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "info":
                            {
                                if (arguments.Count == 3)
                                {
                                    IPackage package = null;

                                    code = interpreter.GetPackage(
                                        arguments[2], LookupFlags.Default,
                                        ref package, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        bool         scrub = interpreter.IsSafe();
                                        PackageFlags flags = package.Flags;
                                        Guid         id    = AttributeOps.GetObjectId(package);

                                        result = StringList.MakeList(
                                            "kind", package.Kind,
                                            "id", package.Id.Equals(Guid.Empty) ? id : package.Id,
                                            "name", package.Name,
                                            "description", package.Description,
                                            "indexFileName", scrub ? PathOps.ScrubPath(
                                                GlobalState.GetBasePath(), package.IndexFileName) :
                                            package.IndexFileName,
                                            "provideFileName", scrub ? PathOps.ScrubPath(
                                                GlobalState.GetBasePath(), package.ProvideFileName) :
                                            package.ProvideFileName,
                                            "flags", flags,
                                            "loaded", (package.Loaded != null) ? package.Loaded : null,
                                            "ifNeeded", (!scrub && (package.IfNeeded != null)) ?
                                            package.IfNeeded.KeysAndValuesToString(null, false) :
                                            null);
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package info name\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "loaded":
                            {
                                if ((arguments.Count == 2) || (arguments.Count == 3))
                                {
                                    string pattern = null;

                                    if (arguments.Count == 3)
                                    {
                                        pattern = arguments[2];
                                    }

                                    code = interpreter.PkgLoaded(
                                        pattern, false, false, ref result);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package loaded ?pattern?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "names":
                            {
                                if ((arguments.Count == 2) || (arguments.Count == 3))
                                {
                                    string pattern = null;

                                    if (arguments.Count == 3)
                                    {
                                        pattern = arguments[2];
                                    }

                                    code = interpreter.PkgNames(
                                        pattern, false, ref result);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package names ?pattern?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "present":
                            {
                                if (arguments.Count >= 3)
                                {
                                    OptionDictionary options = new OptionDictionary(
                                        new IOption[] {
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-exact", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                        });

                                    int argumentIndex = Index.Invalid;

                                    code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, false, ref argumentIndex, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        if ((argumentIndex != Index.Invalid) &&
                                            ((argumentIndex + 2) >= arguments.Count))
                                        {
                                            bool exact = false;

                                            if (options.IsPresent("-exact"))
                                            {
                                                exact = true;
                                            }

                                            Version version = null;

                                            if ((argumentIndex + 1) < arguments.Count)
                                            {
                                                code = Value.GetVersion(
                                                    arguments[argumentIndex + 1], interpreter.CultureInfo,
                                                    ref version, ref result);
                                            }

                                            if (code == ReturnCode.Ok)
                                            {
                                                code = interpreter.PresentPackage(
                                                    arguments[argumentIndex], version, exact, ref result);
                                            }
                                        }
                                        else
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                Option.LooksLikeOption(arguments[argumentIndex]))
                                            {
                                                result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                            }
                                            else
                                            {
                                                result = "wrong # args: should be \"package present ?-exact? package ?version?\"";
                                            }

                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package present ?-exact? package ?version?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "provide":
                            {
                                if ((arguments.Count == 3) || (arguments.Count == 4))
                                {
                                    PackageFlags flags = interpreter.PackageFlags;

                                    if (!FlagOps.HasFlags(flags, PackageFlags.NoProvide, true))
                                    {
                                        Version version = null;

                                        if (arguments.Count == 4)
                                        {
                                            code = Value.GetVersion(arguments[3], interpreter.CultureInfo, ref version, ref result);
                                        }

                                        if (code == ReturnCode.Ok)
                                        {
                                            code = interpreter.PkgProvide(arguments[2], version, flags, ref result);
                                        }
                                    }
                                    else
                                    {
                                        //
                                        // HACK: Do nothing, provide no package, and return nothing.
                                        //
                                        result = String.Empty;
                                        code   = ReturnCode.Ok;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package provide package ?version?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "relativefilename":
                            {
                                if ((arguments.Count == 3) || (arguments.Count == 4))
                                {
                                    PathComparisonType pathComparisonType = PathComparisonType.Default;

                                    if (arguments.Count == 4)
                                    {
                                        object enumValue = EnumOps.TryParseFlagsEnum(
                                            interpreter, typeof(PathComparisonType),
                                            pathComparisonType.ToString(), arguments[3],
                                            interpreter.CultureInfo, true, true, true,
                                            ref result);

                                        if (enumValue is EventFlags)
                                        {
                                            pathComparisonType = (PathComparisonType)enumValue;
                                        }
                                        else
                                        {
                                            code = ReturnCode.Error;
                                        }
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        string fileName = null;

                                        code = PackageOps.GetRelativeFileName(
                                            interpreter, arguments[2], pathComparisonType,
                                            ref fileName, ref result);

                                        if (code == ReturnCode.Ok)
                                        {
                                            result = fileName;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package relativefilename fileName ?type?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "require":
                            {
                                if (arguments.Count >= 3)
                                {
                                    OptionDictionary options = new OptionDictionary(
                                        new IOption[] {
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-exact", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                        });

                                    int argumentIndex = Index.Invalid;

                                    code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, false, ref argumentIndex, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        if ((argumentIndex != Index.Invalid) && ((argumentIndex + 2) >= arguments.Count))
                                        {
                                            bool exact = false;

                                            if (options.IsPresent("-exact"))
                                            {
                                                exact = true;
                                            }

                                            Version version = null;

                                            if ((argumentIndex + 1) < arguments.Count)
                                            {
                                                code = Value.GetVersion(
                                                    arguments[argumentIndex + 1], interpreter.CultureInfo,
                                                    ref version, ref result);
                                            }

                                            if (code == ReturnCode.Ok)
                                            {
                                                code = interpreter.RequirePackage(
                                                    arguments[argumentIndex], version, exact, ref result);
                                            }

                                            //
                                            // NOTE: This is a new feature.  If the initial attempt to
                                            //       require a package fails, call the package fallback
                                            //       delegate for the interpreter and then try requiring
                                            //       the package again.
                                            //
                                            if ((code != ReturnCode.Ok) && !ScriptOps.HasFlags(
                                                    interpreter, InterpreterFlags.NoPackageFallback, true))
                                            {
                                                PackageCallback packageFallback = interpreter.PackageFallback;

                                                if (packageFallback != null)
                                                {
                                                    code = packageFallback(
                                                        interpreter, arguments[argumentIndex], version, null,
                                                        interpreter.PackageFlags, exact, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        code = interpreter.RequirePackage(
                                                            arguments[argumentIndex], version, exact, ref result);
                                                    }
                                                }
                                            }

                                            //
                                            // BUGFIX: This is really a new feature.  In the event of a failure
                                            //         here, we now fallback to the "unknown package handler",
                                            //         just like Tcl does.
                                            //
                                            if ((code != ReturnCode.Ok) && !ScriptOps.HasFlags(
                                                    interpreter, InterpreterFlags.NoPackageUnknown, true))
                                            {
                                                string text = interpreter.PackageUnknown + Characters.Space +
                                                              Parser.Quote(arguments[argumentIndex]);

                                                if (version != null)
                                                {
                                                    text += Characters.Space + Parser.Quote(version.ToString());
                                                }

                                                code = interpreter.EvaluateScript(text, ref result);         /* EXEMPT */

                                                if (code == ReturnCode.Ok)
                                                {
                                                    code = interpreter.RequirePackage(
                                                        arguments[argumentIndex], version, exact, ref result);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                Option.LooksLikeOption(arguments[argumentIndex]))
                                            {
                                                result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                            }
                                            else
                                            {
                                                result = "wrong # args: should be \"package require ?-exact? package ?version?\"";
                                            }

                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package require ?-exact? package ?version?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "reset":
                            {
                                if (arguments.Count == 2)
                                {
                                    code = interpreter.ResetPkgIndexes(ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        result = String.Empty;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package reset\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "scan":
                            {
                                if (arguments.Count >= 2)
                                {
                                    OptionDictionary options = new OptionDictionary(
                                        new IOption[] {
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-interpreter", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-preferfilesystem", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-preferhost", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-host", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-normal", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nonormal", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-recursive", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-resolve", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-refresh", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-autopath", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                        });

                                    int argumentIndex = Index.Invalid;

                                    if (arguments.Count > 2)
                                    {
                                        code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result);
                                    }
                                    else
                                    {
                                        code = ReturnCode.Ok;
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        lock (interpreter.SyncRoot)         /* TRANSACTIONAL */
                                        {
                                            PackageIndexFlags flags;

                                            if (options.IsPresent("-interpreter"))
                                            {
                                                flags = interpreter.PackageIndexFlags;
                                            }
                                            else
                                            {
                                                flags = PackageIndexFlags.Default;
                                            }

                                            if (options.IsPresent("-preferfilesystem"))
                                            {
                                                flags |= PackageIndexFlags.PreferFileSystem;
                                            }

                                            if (options.IsPresent("-preferhost"))
                                            {
                                                flags |= PackageIndexFlags.PreferHost;
                                            }

                                            if (options.IsPresent("-host"))
                                            {
                                                flags |= PackageIndexFlags.Host;
                                            }

                                            if (options.IsPresent("-normal"))
                                            {
                                                flags |= PackageIndexFlags.Normal;
                                            }

                                            if (options.IsPresent("-nonormal"))
                                            {
                                                flags |= PackageIndexFlags.NoNormal;
                                            }

                                            if (options.IsPresent("-recursive"))
                                            {
                                                flags |= PackageIndexFlags.Recursive;
                                            }

                                            if (options.IsPresent("-refresh"))
                                            {
                                                flags |= PackageIndexFlags.Refresh;
                                            }

                                            if (options.IsPresent("-resolve"))
                                            {
                                                flags |= PackageIndexFlags.Resolve;
                                            }

                                            bool autoPath = false;

                                            if (options.IsPresent("-autopath"))
                                            {
                                                autoPath = true;
                                            }

                                            StringList paths;

                                            if (argumentIndex != Index.Invalid)
                                            {
                                                //
                                                // NOTE: Refresh the specified path list.
                                                //
                                                paths = new StringList(arguments, argumentIndex);
                                            }
                                            else
                                            {
                                                //
                                                // NOTE: Refresh the default path list.
                                                //
                                                paths = GlobalState.GetAutoPathList(interpreter, autoPath);

                                                //
                                                // NOTE: Did they request the auto-path be rebuilt?
                                                //
                                                if (autoPath)
                                                {
                                                    //
                                                    // NOTE: Since the actual auto-path may have changed,
                                                    //       update the variable now.  We disable traces
                                                    //       here because we manually rescan, if necessary,
                                                    //       below.
                                                    //
                                                    code = interpreter.SetLibraryVariableValue(
                                                        VariableFlags.SkipTrace, TclVars.AutoPath,
                                                        (paths != null) ? paths.ToString() : null,
                                                        ref result);
                                                }
                                            }

                                            if (code == ReturnCode.Ok)
                                            {
                                                PackageIndexDictionary packageIndexes = interpreter.CopyPackageIndexes();

                                                if (code == ReturnCode.Ok)
                                                {
                                                    code = PackageOps.FindAll(
                                                        interpreter, paths, flags, ref packageIndexes,
                                                        ref result);
                                                }

                                                if (code == ReturnCode.Ok)
                                                {
                                                    interpreter.PackageIndexes = packageIndexes;
                                                    result = String.Empty;
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package scan ?options? ?dir dir ...?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "unknown":
                            {
                                if ((arguments.Count == 2) || (arguments.Count == 3))
                                {
                                    if (arguments.Count == 3)
                                    {
                                        interpreter.PackageUnknown = arguments[2];
                                        result = String.Empty;
                                    }
                                    else
                                    {
                                        result = interpreter.PackageUnknown;
                                    }

                                    code = ReturnCode.Ok;
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package unknown ?command?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "vcompare":
                            {
                                if (arguments.Count == 4)
                                {
                                    Version version1 = null;

                                    code = Value.GetVersion(
                                        arguments[2], interpreter.CultureInfo,
                                        ref version1, ref result);

                                    Version version2 = null;

                                    if (code == ReturnCode.Ok)
                                    {
                                        code = Value.GetVersion(
                                            arguments[3], interpreter.CultureInfo,
                                            ref version2, ref result);
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        result = PackageOps.VersionCompare(version1, version2);
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package vcompare version1 version2\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "versions":
                            {
                                if (arguments.Count == 3)
                                {
                                    code = interpreter.PkgVersions(
                                        arguments[2], ref result);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package versions package\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "vloaded":
                            {
                                if ((arguments.Count == 2) || (arguments.Count == 3))
                                {
                                    string pattern = null;

                                    if (arguments.Count == 3)
                                    {
                                        pattern = arguments[2];
                                    }

                                    code = interpreter.PkgLoaded(
                                        pattern, false, true, ref result);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package vloaded ?pattern?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "vsatisfies":
                            {
                                if (arguments.Count == 4)
                                {
                                    PackageFlags flags = interpreter.PackageFlags;

                                    if (!FlagOps.HasFlags(flags, PackageFlags.AlwaysSatisfy, true))
                                    {
                                        Version version1 = null;

                                        code = Value.GetVersion(arguments[2], interpreter.CultureInfo, ref version1, ref result);

                                        Version version2 = null;

                                        if (code == ReturnCode.Ok)
                                        {
                                            code = Value.GetVersion(
                                                arguments[3], interpreter.CultureInfo,
                                                ref version2, ref result);
                                        }

                                        if (code == ReturnCode.Ok)
                                        {
                                            result = PackageOps.VersionSatisfies(
                                                version1, version2, false);
                                        }
                                    }
                                    else
                                    {
                                        //
                                        // HACK: Always fake that this was a satisfied package request.
                                        //
                                        result = true;
                                        code   = ReturnCode.Ok;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package vsatisfies version1 version2\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "withdraw":
                            {
                                if ((arguments.Count == 3) || (arguments.Count == 4))
                                {
                                    Version version = null;

                                    if (arguments.Count == 4)
                                    {
                                        code = Value.GetVersion(
                                            arguments[3], interpreter.CultureInfo,
                                            ref version, ref result);
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        code = interpreter.WithdrawPackage(
                                            arguments[2], version, ref result);
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"package withdraw package ?version?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            default:
                            {
                                result = ScriptOps.BadSubCommand(
                                    interpreter, null, null, subCommand, this, null, null);

                                code = ReturnCode.Error;
                                break;
                            }
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"package arg ?arg ...?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #24
0
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 4)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-all", null),
                            new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-count", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-ecma", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-compiled", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-explicit", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-quote", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nostrict", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-reverse", null),
                            new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-eval", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-command", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-verbatim", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-extra", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-expanded", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-line", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-lineanchor", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-linestop", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nocase", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-noculture", null),
                            new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-start", null),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                        });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, true, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex != Index.Invalid) &&
                                ((argumentIndex + 2) < arguments.Count) &&
                                ((argumentIndex + 4) >= arguments.Count))
                            {
                                string          pattern             = arguments[argumentIndex];
                                string          input               = arguments[argumentIndex + 1];
                                string          replacement         = arguments[argumentIndex + 2];
                                IScriptLocation replacementLocation = arguments[argumentIndex + 2];
                                string          varName             = null;

                                if ((argumentIndex + 3) < arguments.Count)
                                {
                                    varName = arguments[argumentIndex + 3];
                                }

                                RegexOptions regExOptions = StringOps.DefaultRegExOptions;

                                Variant value      = null;
                                int     valueIndex = Index.Invalid;
                                int     length     = input.Length;
                                int     startIndex = 0;

                                if (options.IsPresent("-start", ref value))
                                {
                                    //
                                    // NOTE: Handle "end-X", etc.
                                    //
                                    code = Value.GetIndex(
                                        value.ToString(), length,
                                        ValueFlags.AnyIndex,
                                        interpreter.CultureInfo,
                                        ref startIndex, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        if (startIndex < 0)
                                        {
                                            startIndex = 0;
                                        }

                                        if (startIndex > length)
                                        {
                                            startIndex = length;
                                        }
                                    }
                                }

                                if (code == ReturnCode.Ok)
                                {
                                    int count = 1;

                                    if (options.IsPresent("-count", ref value))
                                    {
                                        count = (int)value.Value;
                                    }

                                    string          text         = null;
                                    IScriptLocation textLocation = null;

                                    if (options.IsPresent("-eval", ref value, ref valueIndex))
                                    {
                                        text         = value.ToString();
                                        textLocation = arguments[valueIndex + 1];
                                    }

                                    bool command = false;

                                    if (options.IsPresent("-command"))
                                    {
                                        command = true;
                                    }

                                    if ((text == null) || !command)
                                    {
                                        bool verbatim = false;

                                        if (options.IsPresent("-verbatim"))
                                        {
                                            verbatim = true;
                                        }

                                        bool all = false;

                                        if (options.IsPresent("-all"))
                                        {
                                            all = true;
                                        }

                                        bool quote = false;

                                        if (options.IsPresent("-quote"))
                                        {
                                            quote = true;
                                        }

                                        bool strict = true; // COMPAT: Tcl.

                                        if (options.IsPresent("-nostrict"))
                                        {
                                            strict = false;
                                        }

                                        bool extra = false; // COMPAT: Tcl.

                                        if (options.IsPresent("-extra"))
                                        {
                                            extra = true;
                                        }

                                        if (options.IsPresent("-ecma"))
                                        {
                                            regExOptions |= RegexOptions.ECMAScript;
                                        }

                                        if (options.IsPresent("-compiled"))
                                        {
                                            regExOptions |= RegexOptions.Compiled;
                                        }

                                        if (options.IsPresent("-explicit"))
                                        {
                                            regExOptions |= RegexOptions.ExplicitCapture;
                                        }

                                        if (options.IsPresent("-reverse"))
                                        {
                                            regExOptions |= RegexOptions.RightToLeft;
                                        }

                                        if (options.IsPresent("-expanded"))
                                        {
                                            regExOptions |= RegexOptions.IgnorePatternWhitespace;
                                        }

                                        if (options.IsPresent("-line"))
                                        {
                                            regExOptions &= ~RegexOptions.Singleline;
                                            regExOptions |= RegexOptions.Multiline;
                                        }

                                        if (options.IsPresent("-lineanchor"))
                                        {
                                            regExOptions |= RegexOptions.Multiline;
                                        }

                                        if (options.IsPresent("-linestop"))
                                        {
                                            regExOptions &= ~RegexOptions.Singleline;
                                        }

                                        if (options.IsPresent("-nocase"))
                                        {
                                            regExOptions |= RegexOptions.IgnoreCase;
                                        }

                                        if (options.IsPresent("-noculture"))
                                        {
                                            regExOptions |= RegexOptions.CultureInvariant;
                                        }

                                        Regex regEx = null;

                                        try
                                        {
                                            regEx = new Regex(pattern, regExOptions);
                                        }
                                        catch (Exception e)
                                        {
                                            Engine.SetExceptionErrorCode(interpreter, e);

                                            result = String.Format(
                                                "couldn't compile regular expression pattern: {0}",
                                                e.Message);

                                            code = ReturnCode.Error;
                                        }

                                        //
                                        // NOTE: If the result is still Ok, then we know that the regular
                                        //       expression pattern was compiled and the regEx object was
                                        //       created successfully.
                                        //
                                        if (code == ReturnCode.Ok)
                                        {
                                            int matchCount = 0; // no matches yet.

                                            //
                                            // NOTE: Place the script to evaluate in the callback into a
                                            //       ClientData object for use by the callback itself.
                                            //
                                            RegsubClientData regsubClientData = new RegsubClientData(null,
                                                                                                     regEx, pattern, input, replacement, replacementLocation,
                                                                                                     text, textLocation, 0, quote, extra, strict, verbatim);

                                            //
                                            // NOTE: Push our interpreter and the necessary RegsubClientData
                                            //       instance onto the stack to guarantee that they can be
                                            //       fetched from inside the callback.  Technically, it
                                            //       should not be necessary to push the interpreter itself
                                            //       because the engine should push it for us; however,
                                            //       better safe than sorry (i.e. in case we are called
                                            //       outside the scope of a script being evaluated, etc).
                                            //       Also, the RegsubClientData instance is now passed using
                                            //       this method, so we always need to push something anyhow.
                                            //
                                            GlobalState.PushActiveInterpreter(interpreter, regsubClientData);

                                            try
                                            {
                                                if (text != null)
                                                {
                                                    //
                                                    // NOTE: Perform the replacements using our custom match
                                                    //       evaluator which will then evaluate the provided
                                                    //       script to obtain the final results.
                                                    //
                                                    if (all)
                                                    {
                                                        result = regEx.Replace(
                                                            input, RegExOps.RegsubEvaluateMatchCallback);
                                                    }
                                                    else
                                                    {
                                                        result = regEx.Replace(
                                                            input, RegExOps.RegsubEvaluateMatchCallback,
                                                            count, startIndex);
                                                    }
                                                }
                                                else if (command)
                                                {
                                                    //
                                                    // NOTE: Perform the replacements using the command match
                                                    //       evaluator which will then evaluate the provided
                                                    //       replacement script fragment, with the match text
                                                    //       appended to it in order to obtain the results.
                                                    //       This is designed to conform with TIP #463.
                                                    //
                                                    if (all)
                                                    {
                                                        result = regEx.Replace(
                                                            input, RegExOps.RegsubCommandMatchCallback);
                                                    }
                                                    else
                                                    {
                                                        result = regEx.Replace(
                                                            input, RegExOps.RegsubCommandMatchCallback,
                                                            count, startIndex);
                                                    }
                                                }
                                                else
                                                {
                                                    //
                                                    // NOTE: Perform the replacements using our custom match
                                                    //       evaluator which will simply count the number of
                                                    //       matches and return them verbatim to obtain the
                                                    //       final results.
                                                    //
                                                    if (all)
                                                    {
                                                        result = regEx.Replace(
                                                            input, RegExOps.RegsubNormalMatchCallback);
                                                    }
                                                    else
                                                    {
                                                        result = regEx.Replace(
                                                            input, RegExOps.RegsubNormalMatchCallback,
                                                            count, startIndex);
                                                    }
                                                }

                                                //
                                                // NOTE: Extract the match count from the regsub clientData so that,
                                                //       if necessary, it can be used for the command result (below).
                                                //
                                                matchCount = regsubClientData.Count;
                                            }
                                            catch (ScriptException e)
                                            {
                                                //
                                                // NOTE: Our callback threw an error (it wanted to
                                                //       halt processing of the matches and/or return
                                                //       an error).
                                                //
                                                if (e.ReturnCode == ReturnCode.Break)
                                                {
                                                    //
                                                    // NOTE: This is considered success.
                                                    //
                                                    result = String.Empty;
                                                    code   = ReturnCode.Ok;
                                                }
                                                else if (e.ReturnCode != ReturnCode.Ok)
                                                {
                                                    //
                                                    // NOTE: This is considered failure.
                                                    //
                                                    Engine.SetExceptionErrorCode(interpreter, e);

                                                    result = e.Message;
                                                    code   = ReturnCode.Error;
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                Engine.SetExceptionErrorCode(interpreter, e);

                                                //
                                                // NOTE: Other (non-script) exceptions are always
                                                //       considered failures in this context.
                                                //
                                                result = e;
                                                code   = ReturnCode.Error;
                                            }
                                            finally
                                            {
                                                //
                                                // NOTE: Pop our interpreter from the stack if we
                                                //       pushed it previously.
                                                //
                                                /* IGNORED */
                                                GlobalState.PopActiveInterpreter();
                                            }

                                            //
                                            // NOTE: Did we succeed thus far?
                                            //
                                            if (code == ReturnCode.Ok)
                                            {
                                                //
                                                // NOTE: If they provided a variable name to store the
                                                //       results into we return the match count as the
                                                //       command result.
                                                //
                                                if (varName != null)
                                                {
                                                    code = interpreter.SetVariableValue(
                                                        VariableFlags.None, varName, result, null,
                                                        ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        result = matchCount;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        result = "-command cannot be used with -eval option";
                                        code   = ReturnCode.Error;
                                    }
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(
                                        options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"regsub ?switches? exp string subSpec ?varName?\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"regsub ?switches? exp string subSpec ?varName?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #25
0
ファイル: Base64.cs プロジェクト: jdruin/F5Eagle
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        string subCommand = arguments[1];
                        bool   tried      = false;

                        code = ScriptOps.TryExecuteSubCommandFromEnsemble(
                            interpreter, this, clientData, arguments, true,
                            false, ref subCommand, ref tried, ref result);

                        if ((code == ReturnCode.Ok) && !tried)
                        {
                            switch (subCommand)
                            {
                            case "decode":
                            {
                                if (arguments.Count >= 3)
                                {
                                    OptionDictionary options = new OptionDictionary(
                                        new IOption[] {
                                            new Option(null, OptionFlags.MustHaveEncodingValue, Index.Invalid, Index.Invalid, "-encoding", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                        });

                                    int argumentIndex = Index.Invalid;

                                    code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        if ((argumentIndex != Index.Invalid) &&
                                            ((argumentIndex + 1) == arguments.Count))
                                        {
                                            Variant  value    = null;
                                            Encoding encoding = null;

                                            if (options.IsPresent("-encoding", ref value))
                                            {
                                                encoding = (Encoding)value.Value;
                                            }

                                            if (code == ReturnCode.Ok)
                                            {
                                                try
                                                {
                                                    string stringValue = null;

                                                    code = StringOps.GetString(encoding,
                                                                               Convert.FromBase64String(arguments[argumentIndex]),
                                                                               EncodingType.Binary, ref stringValue, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        result = stringValue;
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    Engine.SetExceptionErrorCode(interpreter, e);

                                                    result = e;
                                                    code   = ReturnCode.Error;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                Option.LooksLikeOption(arguments[argumentIndex]))
                                            {
                                                result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                            }
                                            else
                                            {
                                                result = "wrong # args: should be \"base64 decode ?options? string\"";
                                            }

                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"base64 decode ?options? string\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "encode":
                            {
                                if (arguments.Count >= 3)
                                {
                                    OptionDictionary options = new OptionDictionary(
                                        new IOption[] {
                                            new Option(null, OptionFlags.MustHaveEncodingValue, Index.Invalid, Index.Invalid, "-encoding", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                        });

                                    int argumentIndex = Index.Invalid;

                                    code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        if ((argumentIndex != Index.Invalid) && ((argumentIndex + 1) == arguments.Count))
                                        {
                                            Variant  value    = null;
                                            Encoding encoding = null;

                                            if (options.IsPresent("-encoding", ref value))
                                            {
                                                encoding = (Encoding)value.Value;
                                            }

                                            if (code == ReturnCode.Ok)
                                            {
                                                try
                                                {
                                                    byte[] bytes = null;

                                                    code = StringOps.GetBytes(
                                                        encoding, arguments[argumentIndex],
                                                        EncodingType.Binary, ref bytes, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        result = Convert.ToBase64String(bytes,
                                                                                        Base64FormattingOptions.InsertLineBreaks);
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    Engine.SetExceptionErrorCode(interpreter, e);

                                                    result = e;
                                                    code   = ReturnCode.Error;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                Option.LooksLikeOption(arguments[argumentIndex]))
                                            {
                                                result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                            }
                                            else
                                            {
                                                result = "wrong # args: should be \"base64 encode ?options? string\"";
                                            }

                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"base64 encode ?options? string\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            default:
                            {
                                result = ScriptOps.BadSubCommand(
                                    interpreter, null, null, subCommand, this, null, null);

                                code = ReturnCode.Error;
                                break;
                            }
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"base64 option ?arg ...?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
コード例 #26
0
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 3)
                    {
                        if (interpreter.HasChannels(ref result))
                        {
                            OptionDictionary options = new OptionDictionary(
                                new IOption[] {
                                new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-server", null),                // server only
                                new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-buffer", null),         // client & server
                                new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-timeout", null),        // client & server
                                new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-sendtimeout", null),    // client & server
                                new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-receivetimeout", null), // client & server
                                new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-myaddr", null),                // client & server
                                new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-myport", null),                // client only
                                new Option(null, OptionFlags.Unsupported, Index.Invalid, Index.Invalid, "-async", null),                   // client only
                                new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-channelid", null),             // client & server
                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nodelay", null),                        // client only
                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-noexclusive", null),                    // server only
                                new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                            });

                            int argumentIndex = Index.Invalid;

                            code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, true, ref argumentIndex, ref result);

                            if (code == ReturnCode.Ok)
                            {
                                //
                                // NOTE: There must be at least one argument after the options
                                //       and there can never be more than two.
                                //
                                if ((argumentIndex != Index.Invalid) && ((argumentIndex + 2) >= arguments.Count))
                                {
                                    Variant value   = null;
                                    string  command = null;

                                    if (options.IsPresent("-server", ref value))
                                    {
                                        command = value.ToString();
                                    }

                                    string myAddress = null;

                                    if (options.IsPresent("-myaddr", ref value))
                                    {
                                        myAddress = value.ToString();
                                    }

                                    string myPort = null;

                                    if (options.IsPresent("-myport", ref value))
                                    {
                                        myPort = value.ToString();
                                    }

                                    int buffer = 0;

                                    if (options.IsPresent("-buffer", ref value))
                                    {
                                        buffer = (int)value.Value;
                                    }

                                    int sendTimeout = _Timeout.None;

                                    if (options.IsPresent("-sendtimeout", ref value))
                                    {
                                        sendTimeout = (int)value.Value;
                                    }

                                    int receiveTimeout = _Timeout.None;

                                    if (options.IsPresent("-receivetimeout", ref value))
                                    {
                                        receiveTimeout = (int)value.Value;
                                    }

                                    int timeout = _Timeout.None;

                                    if (options.IsPresent("-timeout", ref value))
                                    {
                                        timeout = (int)value.Value;
                                    }

                                    bool asynchronous = false;

                                    if (options.IsPresent("-async"))
                                    {
                                        asynchronous = true; /* NOT YET IMPLEMENTED */
                                    }
                                    bool noDelay = false;

                                    if (options.IsPresent("-nodelay"))
                                    {
                                        noDelay = true;
                                    }

                                    bool exclusive = true; /* TODO: Good default? */

                                    if (options.IsPresent("-noexclusive"))
                                    {
                                        exclusive = false;
                                    }

                                    string channelId = null;

                                    if (options.IsPresent("-channelid", ref value))
                                    {
                                        channelId = value.ToString();
                                    }

                                    if ((channelId == null) ||
                                        (interpreter.DoesChannelExist(channelId) != ReturnCode.Ok))
                                    {
                                        if (command != null)
                                        {
                                            if ((argumentIndex + 1) == arguments.Count)
                                            {
                                                if (myPort == null)
                                                {
                                                    code = interpreter.StartServerSocket(
                                                        options, timeout, myAddress,
                                                        arguments[argumentIndex], exclusive,
                                                        command, ref result);
                                                }
                                                else
                                                {
                                                    goto wrongNumArgs;
                                                }
                                            }
                                            else
                                            {
                                                goto wrongNumArgs;
                                            }
                                        }
                                        else
                                        {
                                            if ((argumentIndex + 2) == arguments.Count)
                                            {
                                                if (!asynchronous)
                                                {
                                                    TcpClient client = SocketOps.NewTcpClient(myAddress, myPort,
                                                                                              interpreter.CultureInfo, ref result);

                                                    if (client != null)
                                                    {
                                                        try
                                                        {
                                                            client.NoDelay = noDelay;

                                                            if (timeout != _Timeout.None)
                                                            {
                                                                client.SendTimeout    = timeout;
                                                                client.ReceiveTimeout = timeout;
                                                            }

                                                            if (sendTimeout != _Timeout.None)
                                                            {
                                                                client.SendTimeout = sendTimeout;
                                                            }

                                                            if (receiveTimeout != _Timeout.None)
                                                            {
                                                                client.ReceiveTimeout = receiveTimeout;
                                                            }

                                                            if (buffer != 0)
                                                            {
                                                                client.SendBufferSize    = buffer;
                                                                client.ReceiveBufferSize = buffer;
                                                            }

                                                            code = ReturnCode.Ok;
                                                        }
                                                        catch (Exception e)
                                                        {
                                                            Engine.SetExceptionErrorCode(interpreter, e);

                                                            result = e;
                                                            code   = ReturnCode.Error;
                                                        }

                                                        if (code == ReturnCode.Ok)
                                                        {
                                                            code = SocketOps.Connect(client, arguments[argumentIndex],
                                                                                     arguments[argumentIndex + 1], interpreter.CultureInfo,
                                                                                     ref result);

                                                            if (code == ReturnCode.Ok)
                                                            {
                                                                StreamFlags flags = StreamFlags.PreventClose |
                                                                                    StreamFlags.Socket | StreamFlags.Client;

                                                                if (channelId == null)
                                                                {
                                                                    channelId = FormatOps.Id("clientSocket", null, interpreter.NextId());
                                                                }

                                                                code = interpreter.AddFileOrSocketChannel(
                                                                    channelId, client.GetStream(), options, flags, false,
                                                                    false, new ClientData(client), ref result);

                                                                if (code == ReturnCode.Ok)
                                                                {
                                                                    result = channelId;
                                                                }
                                                            }
                                                        }
                                                    }
                                                    else
                                                    {
                                                        code = ReturnCode.Error;
                                                    }
                                                }
                                                else
                                                {
                                                    result = "asynchronous sockets are not implemented";
                                                    code   = ReturnCode.Error;
                                                }
                                            }
                                            else
                                            {
                                                goto wrongNumArgs;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        result = String.Format(
                                            "can't add \"{0}\": channel already exists",
                                            channelId);

                                        code = ReturnCode.Error;
                                    }
                                }
                                else
                                {
                                    if ((argumentIndex != Index.Invalid) &&
                                        Option.LooksLikeOption(arguments[argumentIndex]))
                                    {
                                        result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                        code   = ReturnCode.Error;
                                    }
                                    else
                                    {
                                        goto wrongNumArgs;
                                    }
                                }
                            }
                        }
                        else
                        {
                            code = ReturnCode.Error;
                        }
                    }
                    else
                    {
                        goto wrongNumArgs;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);

wrongNumArgs:
            result = "wrong # args: should be \"socket ?-myaddr addr? ?-myport myport? ?-async? host port\" " +
                     "or \"socket -server command ?-myaddr addr? port\"";

            return(ReturnCode.Error);
        }