[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); }
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); }
[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); }