/////////////////////////////////////////////////////////////////////// #region Public Constructors public TestForm( _Forms.TestForm form, IPluginData pluginData ) : base(pluginData) { this.Flags |= Utility.GetPluginFlags(GetType().BaseType) | Utility.GetPluginFlags(this); this.form = form; }
/////////////////////////////////////////////////////////////////////// #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); }
[STAThread()] /* WinForms */ private static int Main(string[] args) { #region Shell Debugging Support (Optional) // // NOTE: Pause for them to attach a debugger, if requested. // This cannot be done inside the Interpreter class // because they may want to debug its initializers. // if (Environment.GetEnvironmentVariable(EnvVars.Break) != null) { // // NOTE: Prevent further breaks into the debugger. // Environment.SetEnvironmentVariable(EnvVars.Break, null); #if CONSOLE // // NOTE: Display the prompt and then wait for the user to // press a key. // Console.WriteLine(String.Format( _Constants.Prompt.Debugger, GetProcessId())); try { Console.ReadKey(true); /* throw */ } catch (InvalidOperationException) // Console.ReadKey { // do nothing. } #endif Debugger.Break(); /* throw */ } #endregion /////////////////////////////////////////////////////////////////// #region Interpreter Creation Flags (Optional) // // NOTE: Start with default shell flags. // CreateFlags flags = CreateFlags.ShellUse; #if CONSOLE // // NOTE: The console is enabled and we are going to use our custom // host which inherits from it; therefore, prevent a default // host from being created for the interpreter. // flags |= CreateFlags.NoHost; #endif // // NOTE: Get the effective interpreter creation flags for the shell // from the environment, etc. // flags = Interpreter.GetStartupCreateFlags( args, flags, OptionOriginFlags.Shell, true, true); #endregion /////////////////////////////////////////////////////////////////// // // NOTE: We need a return code and result variable now (in case // querying the interpreter library path fails). // ReturnCode code = ReturnCode.Ok; Result result = null; /////////////////////////////////////////////////////////////////// #region Interpreter Pre-Initialize Text (Optional) // // NOTE: Start with the default pre-initialize text. // string text = null; // // NOTE: Get the effective interpreter pre-initialize text for the // shell from the environment, etc. // if (code == ReturnCode.Ok) { code = Interpreter.GetStartupPreInitializeText( args, flags, OptionOriginFlags.Shell, true, true, ref text, ref result); } #endregion /////////////////////////////////////////////////////////////////// #region Interpreter Library Path (Optional) // // NOTE: Start with the default library path. // string libraryPath = null; // // NOTE: Get the effective interpreter library path for the shell // from the environment, etc. // if (code == ReturnCode.Ok) { code = Interpreter.GetStartupLibraryPath( args, flags, OptionOriginFlags.Shell, true, true, ref libraryPath, ref result); } #endregion /////////////////////////////////////////////////////////////////// if (code == ReturnCode.Ok) { // // NOTE: Create an interpreter now inside of a using block so // that we can be relatively sure it will be finalized // on this thread. // using (interpreter = Interpreter.Create( args, flags, text, libraryPath, ref result)) { // // NOTE: Make sure the interpreter was actually created. // This can, in theory, be a problem if the // ThrowOnError flag ends up getting removed somehow // prior to the call to create the interpreter. // if (interpreter != null) { // // NOTE: Fetch the interpreter host now for error // reporting purposes. // IHost host = interpreter.Host; /////////////////////////////////////////////////////// #region Interpreter Startup Options (Optional) // // NOTE: By default, initialize the script library for // the interpreter. // bool initialize = true; // // NOTE: Process all the remaining startup options // (i.e. the ones that do not modify the // interpreter creation flags) now. // code = Interpreter.ProcessStartupOptions( interpreter, args, flags, OptionOriginFlags.Shell, true, true, ref initialize, ref result); #endregion /////////////////////////////////////////////////////// if (code == ReturnCode.Ok) { #region Command Line Arguments (Optional) #if CONSOLE // // NOTE: In debug mode, show the command line // arguments just as we received them. // if (interpreter.Debug) { Console.WriteLine("The command line is: {0}", Utility.BuildCommandLine(args, true)); } #endif // // NOTE: Save the intial arguments for later use. // mainArguments = args; #endregion /////////////////////////////////////////////////// #region Host - Window Preference (Optional) // // NOTE: By default, show the host window? // // TODO: Make this an argument? // bool console = DefaultConsole; // // NOTE: Do we want the initial (auto-created) host // window to be visible? // if (console) { if (Utility.GetEnvironmentVariable( EnvVars.NoConsole, true, false) != null) { #if CONSOLE Console.WriteLine( _Constants.Prompt.NoConsole); #endif console = false; } } else { if (Utility.GetEnvironmentVariable( EnvVars.Console, true, false) != null) { #if CONSOLE Console.WriteLine( _Constants.Prompt.Console); #endif console = true; } } #endregion /////////////////////////////////////////////////// #region Resource Manager Creation // // NOTE: Create our resource manager (the host and // the form will both use this). // ResourceManager resourceManager = new ResourceManager(resourceBaseName, packageAssembly); #endregion /////////////////////////////////////////////////// #region Host - Custom Creation (Optional) #if CONSOLE // // NOTE: Create a custom IHost bound to the // interpreter. // host = new _Hosts.Custom(new HostData(null, null, null, ClientData.Empty, typeof(_Hosts.Custom).Name, interpreter, resourceManager, null, Utility.HasFlags(flags, CreateFlags.UseAttach, true), Utility.HasFlags(flags, CreateFlags.NoColor, true), Utility.HasFlags(flags, CreateFlags.NoTitle, true), Utility.HasFlags(flags, CreateFlags.NoIcon, true), Utility.HasFlags(flags, CreateFlags.NoProfile, true), Utility.HasFlags(flags, CreateFlags.NoCancel, true))); /////////////////////////////////////////////////// interpreter.Host = host; #endif #endregion /////////////////////////////////////////////////// #region Interpreter Initialization // // NOTE: Attempt to initialize the interpreter. // if (initialize) { code = interpreter.Initialize(false, ref result); } else { code = ReturnCode.Ok; } #endregion /////////////////////////////////////////////////// #region Application-Specific Startup // // NOTE: If initialization failed, no point in // continuing. // if (code == ReturnCode.Ok) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); mainForm = new _Forms.TestForm(interpreter, args); #if NOTIFY || NOTIFY_OBJECT Assembly assembly = Assembly.GetExecutingAssembly(); AssemblyName assemblyName = assembly.GetName(); string fileName = assembly.Location; string typeName = typeof(_Plugins.TestForm).FullName; Uri uri = Utility.GetAssemblyUri(assembly); IPlugin plugin = new _Plugins.TestForm(mainForm, 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, resourceManager, null, 0)); code = Utility.PrepareStaticPlugin( plugin, ref result); if (code == ReturnCode.Ok) { code = interpreter.AddPlugin( plugin, null, ref pluginToken, ref result); } #endif } #endregion /////////////////////////////////////////////////// #region Host - Window Startup if (code == ReturnCode.Ok) { if (console) { // // NOTE: Create and start the interpreter // loop thread. // code = StartupInteractiveLoopThread( ref result); } else if (host.IsOpen()) { // // NOTE: Close the initial host window. // code = host.Close(ref result); } } #endregion /////////////////////////////////////////////////// if (code == ReturnCode.Ok) { #region WinForms Specific // // NOTE: Show the primary user interface form // for the application. // Application.Run(mainForm); #endregion /////////////////////////////////////////////// #region Host - Window Shutdown // // NOTE: If there is an interactive loop thread, // we do not want to exit until it is no // longer running. // if (interactiveLoopThread != null) { interactiveLoopThread.Join(); } #endregion } /////////////////////////////////////////////////// #region Startup Error Handling // // NOTE: Was there any kind of failure above? // if (code != ReturnCode.Ok) { #region WinForms Specific Code if (mainForm != null) { mainForm.Dispose(); mainForm = null; } #endregion /////////////////////////////////////////////// if (host != null) { host.WriteResultLine( code, result, interpreter.ErrorLine); } CommonOps.Complain(code, result); exitCode = Utility.ReturnCodeToExitCode( code, true); } #endregion } else { if (host != null) { host.WriteResultLine(code, result); } 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(); } } } else { #if CONSOLE // // NOTE: Querying the interpreter library path failed. // Console.WriteLine(Utility.FormatResult(code, result)); #endif exitCode = Utility.ReturnCodeToExitCode(code, true); } return((int)exitCode); }