コード例 #1
0
ファイル: Shell.cs プロジェクト: jdruin/F5Eagle
        [STAThread()] /* WinForms */
        private static int Main(string[] args)
        {
            ReturnCode code;
            Result     result = null;

            using (interpreter = Interpreter.Create(
                       args, CreateFlags.ShellUse, ref result))
            {
                if (interpreter != null)
                {
                    code = interpreter.Initialize(false, ref result);

                    if (code == ReturnCode.Ok)
                    {
                        Assembly     assembly     = Assembly.GetExecutingAssembly();
                        AssemblyName assemblyName = assembly.GetName();
                        string       fileName     = assembly.Location;
                        string       typeName     = typeof(_Plugins.Toolkit).FullName;
                        Uri          uri          = Utility.GetAssemblyUri(assembly);

                        IPlugin plugin = new _Plugins.Toolkit(new PluginData(
                                                                  Utility.FormatPluginName(assemblyName.FullName,
                                                                                           typeName), null, null, ClientData.Empty,
                                                                  PluginFlags.None, assemblyName.Version, uri,
                                                                  interpreter.GetAppDomain(), assembly, assemblyName,
                                                                  fileName, typeName, null, null, null, null, null,
                                                                  null, null, null, 0));

                        code = Utility.PrepareStaticPlugin(plugin, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            code = interpreter.AddPlugin(plugin, null,
                                                         ref toolkitPluginToken, ref result);
                        }
                    }

                    if (code == ReturnCode.Ok)
                    {
                        Thread thread = Engine.CreateThread(
                            interpreter, InteractiveLoopThreadStart, 0, true,
                            false);

                        if (thread != null)
                        {
                            thread.Name = String.Format(
                                "interactiveLoopThread: {0}", interpreter);

                            thread.Start(args);

                            Application.EnableVisualStyles();
                            Application.SetCompatibleTextRenderingDefault(false);

                            Toplevel toplevel = new Toplevel(interpreter, ".");

                            Application.Run(toplevel);

                            if (thread != null)
                            {
                                thread.Join();
                            }

                            exitCode = interpreter.ExitCode;
                        }
                        else
                        {
                            result = "could not create interactive loop thread";
                            code   = ReturnCode.Error;
                        }
                    }

                    if (code != ReturnCode.Ok)
                    {
                        IInteractiveHost interactiveHost = interpreter.Host;

                        if (interactiveHost != null)
                        {
                            interactiveHost.WriteResultLine(
                                code, result, interpreter.ErrorLine);
                        }

                        exitCode = Utility.ReturnCodeToExitCode(code, true);
                    }
                }
                else
                {
#if CONSOLE
                    //
                    // NOTE: Creation of the interpreter failed.
                    //
                    Console.WriteLine(Utility.FormatResult(
                                          ReturnCode.Error, result));
#endif

                    exitCode = Utility.FailureExitCode();
                }
            }

            return((int)exitCode);
        }
コード例 #2
0
        ///////////////////////////////////////////////////////////////////////

        #region Application Entry Point
        /// <summary>
        /// This is the main entry point for this assembly.
        /// </summary>
        /// <param name="args">
        /// The command line arguments received from the calling assembly.
        /// </param>
        /// <returns>
        /// Zero for success, non-zero on error.
        /// </returns>
        private static int Main(
            string[] args /* in */
            )
        {
            //
            // NOTE: The integer exit code to return to the caller (parent
            //       process, etc).
            //
            ExitCode exitCode = Utility.SuccessExitCode();

            //
            // NOTE: Save the command line arguments for use by the
            //       interpreter via the linked variable (optional).
            //
            mainArgs = args;

            //
            // NOTE: The "interpreter result" that is passed to various
            //       methods.
            //
            Result result = null;

            //
            // NOTE: First, we create a new interpreter (with the default
            //       options).
            //
            using (Interpreter interpreter = Interpreter.Create(
                       args, ref result))
            {
                if (interpreter != null)
                {
                    ReturnCode code = interpreter.SetVariableLink(
                        VariableFlags.None, fieldName, typeof(Program).
                        GetField(fieldName), null, ref result);

                    if (code != ReturnCode.Ok)
                    {
                        //
                        // NOTE: Handle variable linking error.
                        //
                        exitCode = Utility.ReturnCodeToExitCode(code);
                    }

                    //
                    // NOTE: Create an instance of the example custom command.
                    //
                    Class0 class0 = new Class0(new CommandData("class0", null,
                                                               null, ClientData.Empty, typeof(Class0).FullName,
                                                               CommandFlags.None, null, 0));

                    //
                    // NOTE: The token that will represent the custom command
                    //       we add.
                    //
                    long commandToken = 0;

                    //
                    // NOTE: Next, we can optionally add one or more custom
                    //       commands.
                    //
                    if (code == ReturnCode.Ok)
                    {
                        code = interpreter.AddCommand(
                            class0, null, ref commandToken, ref result);
                    }

                    //
                    // NOTE: The token that will represent the custom command
                    //       policy we add.
                    //
                    long policyToken = 0;

                    //
                    // NOTE: Next, add our custom command execution policy (for
                    //       use in "safe" mode).
                    //
                    if (code == ReturnCode.Ok)
                    {
                        code = interpreter.AddPolicy(
                            Class0PolicyCallback, null, null, ref policyToken,
                            ref result);
                    }

                    //
                    // NOTE: The error line number that is passed to various
                    //       script evaluation methods.
                    //
                    int errorLine = 0;

                    //
                    // NOTE: Check for a successful return code.
                    //
                    if (code == ReturnCode.Ok) // OR: Utility.IsSuccess(code, true)
                    {
#if SHELL
                        result = null;

                        code = Interpreter.InteractiveLoop(
                            interpreter, args, ref result);
#else
                        //
                        // NOTE: Next, evaluate one or more scripts of your
                        //       choosing (which may or may not reference any
                        //       custom commands you may have added in the
                        //       previous step).
                        //
                        code = Engine.EvaluateScript(
                            interpreter, "class0 test; # <-- script text",
                            ref result, ref errorLine);
#endif

                        //
                        // NOTE: Check for an unsuccessful return code.
                        //
                        if (code != ReturnCode.Ok) // OR: !Utility.IsSuccess(code, true)
                        {
                            //
                            // NOTE: Handle script error.
                            //
                            exitCode = Utility.ReturnCodeToExitCode(code);
                        }

                        //
                        // NOTE: Next, we can optionally remove one or more of
                        //       the custom command policies we added earlier.
                        //
                        code = interpreter.RemovePolicy(policyToken, null,
                                                        ref result);

                        //
                        // NOTE: Check for an unsuccessful return code.
                        //
                        if (code != ReturnCode.Ok)
                        {
                            //
                            // NOTE: Handle policy removal error.
                            //
                            exitCode = Utility.ReturnCodeToExitCode(code);
                        }

                        //
                        // NOTE: Next, we can optionally remove one or more of
                        //       the custom commands we added earlier.
                        //
                        code = interpreter.RemoveCommand(commandToken, null,
                                                         ref result);

                        //
                        // NOTE: Check for an unsuccessful return code.
                        //
                        if (code != ReturnCode.Ok)
                        {
                            //
                            // NOTE: Handle command removal error.
                            //
                            exitCode = Utility.ReturnCodeToExitCode(code);
                        }

                        code = interpreter.UnsetVariable(VariableFlags.None,
                                                         fieldName, ref result);

                        if (code != ReturnCode.Ok)
                        {
                            //
                            // NOTE: Handle variable unlinking error.
                            //
                            exitCode = Utility.ReturnCodeToExitCode(code);
                        }
                    }
                    else
                    {
                        //
                        // NOTE: Handle failure to add the custom command
                        //       or policy.
                        //
                        exitCode = Utility.ReturnCodeToExitCode(code);
                    }

                    //
                    // NOTE: Always check for a valid interpreter hosting
                    //       environment before using it as it is not
                    //       guaranteed to always be available.
                    //
                    IInteractiveHost interactiveHost = interpreter.Host;

                    if (interactiveHost != null)
                    {
                        interactiveHost.WriteResultLine(
                            code, result, errorLine);
                    }
                    else
                    {
                        Console.WriteLine(Utility.FormatResult(
                                              code, result, errorLine));
                    }
                }
                else
                {
                    //
                    // NOTE: Creation of the interpreter failed.
                    //
                    Console.WriteLine(Utility.FormatResult(
                                          ReturnCode.Error, result));

                    exitCode = Utility.FailureExitCode();
                }
            }

            return((int)exitCode);
        }
コード例 #3
0
ファイル: Bgerror.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)
                    {
                        if (!interpreter.HasNoBackgroundError())
                        {
                            IInteractiveHost interactiveHost = interpreter.Host;

                            if (interactiveHost != null)
                            {
                                string message = arguments[1];

                                message = !String.IsNullOrEmpty(message) ?
                                          String.Format("{0}: {1}", this.Name, message) :
                                          this.Name;

                                interactiveHost.WriteResultLine(ReturnCode.Error,
                                                                message, Interpreter.GetErrorLine(interpreter));

                                result = String.Empty;
                            }
                            else
                            {
                                result = "interpreter host not available";
                                code   = ReturnCode.Error;
                            }
                        }
                        else
                        {
                            result = "background error handling disabled";
                            code   = ReturnCode.Error;
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"bgerror message\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }