Ejemplo n.º 1
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);
        }