private TclVoiceInterpreter(Interpreter interpreter) { _interpreter = interpreter; Result result = null; var customCommands = TclCommandProvider.GetCustomCommands(); Logger.Info("Adding custom TCL commands."); foreach (var customCommand in customCommands) { long token = 0; Logger.Debug($"Adding {customCommand.Name} command."); var code = interpreter.AddCommand(customCommand, null, ref token, ref result); if (code != ReturnCode.Ok) { Logger.Warn($"Failed to add {customCommand.Name} command. Error: {result}"); } SubscribeToInputCommand(customCommand as CiscoTclCommand); SubscribeToBreakpointCommand(customCommand as Breakpoint); SubscribeToFsmCommand(customCommand as Fsm); } void SubscribeToFsmCommand(Fsm fsmCommand) { if (fsmCommand == null) { return; } fsmCommand.FsmGenerated += (sender, args) => RaiseFsmGeneratedEvent(args); fsmCommand.StateChanged += (sender, args) => RaiseFsmStateChangedEvent(args); } void SubscribeToBreakpointCommand(Breakpoint breakpoint) { if (breakpoint == null) { return; } breakpoint.BreakpointHit += OnBreakpointHit; } void SubscribeToInputCommand(CiscoTclCommand command) { if (command == null) { return; } if (command is IInputRequestingCommand <DigitsInputData> digitInput) { digitInput.OnInputRequested += DigitInputOnOnInputRequested; } foreach (var subCommand in command.TclSubCommands.OfType <IInputRequestingCommand <DigitsInputData> >()) { subCommand.OnInputRequested += DigitInputOnOnInputRequested; } } }
/// <summary> /// Links a set of object files to a program or a single object file. /// </summary> /// <param name="args">The object files to link and the destination file path.</param> /// <returns>The exit code of the program.</returns> private static int Main(string[] args) { Interpreter interpreter = new Interpreter(); interpreter.AddCommand <LinkCommand>(); return(interpreter.Run(args)); }
private int ExecuteThread(string commands) { Result result = null; var c = new ConsoleRedirect(); var old_out = Console.Out; Console.SetOut(c); c.Output += c_Output; _interpreter = Interpreter.Create(ref result); ICommand command = new DccCommand(new CommandData( "dcc", null, null, null, typeof(DccCommand).FullName, CommandFlags.None, null, 0)); ReturnCode code; long token = 0; code = _interpreter.AddCommand( command, null, ref token, ref result); code = _interpreter.AddCommand(new EchoCommand(new CommandData( "echo", null, null, null, typeof(EchoCommand).FullName, CommandFlags.None, null, 0)), null, ref token, ref result); if (code == ReturnCode.Ok) { int errorLine = 0; code = _interpreter.EvaluateScript(commands, ref result, ref errorLine); _interpreter.Host.WriteResult(code, result, errorLine, true); return((int)_interpreter.ExitCode); } else { _interpreter.Host.WriteResult(code, result, true); } _interpreter.Dispose(); _interpreter = null; Console.SetOut(old_out); return(0); }
private int ExecuteThread(string commands) { Result result = null; var c = new ConsoleRedirect(); var old_out = Console.Out; Console.SetOut(c); c.Output += c_Output; _interpreter = Interpreter.Create(ref result); ICommand command = new DccCommand(new CommandData( "dcc", null, null, null, typeof(DccCommand).FullName, CommandFlags.None, null, 0)); ReturnCode code; long token = 0; code = _interpreter.AddCommand( command, null, ref token, ref result); code = _interpreter.AddCommand(new EchoCommand(new CommandData( "echo", null, null, null, typeof(EchoCommand).FullName, CommandFlags.None, null, 0)), null, ref token, ref result); if (code == ReturnCode.Ok) { int errorLine = 0; code = _interpreter.EvaluateScript(commands, ref result, ref errorLine); _interpreter.Host.WriteResult(code, result, errorLine, true); return (int) _interpreter.ExitCode; } else { _interpreter.Host.WriteResult(code, result, true); } _interpreter.Dispose(); _interpreter = null; Console.SetOut(old_out); return 0; }
/////////////////////////////////////////////////////////////////////// #region IState Members public override ReturnCode Initialize( Interpreter interpreter, IClientData clientData, ref Result result ) { // // NOTE: This method cannot rely on automatic command handling // provided by the default plugin because it does not own // the core command set. This is very useful for testing // "custom" plugin handling that does not involve relying // on the default plugin. // if (interpreter != null) { // // NOTE: The test plugin command is "non-standard". Create // and add it only if the interpreter matches. // ReturnCode code = interpreter.IsStandard() ? ReturnCode.Ok : interpreter.AddCommand(CreateCommand(clientData), null, ref result); if (code == ReturnCode.Ok) { Version version = this.Version; code = interpreter.PkgProvide( this.GetType().FullName, version, GetPackageFlags(), ref result); if (code == ReturnCode.Ok) { result = StringList.MakeList(this.Name, version); } } } /////////////////////////////////////////////////////////////////// this.Flags = GetIStatePluginFlags(); return(base.Initialize(interpreter, clientData, ref result)); }
/////////////////////////////////////////////////////////////////////// #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); }
/////////////////////////////////////////////////////////////////////// /// <summary> /// This method creates and initializes an interpreter, uses it to /// perform several tests, and then returns. The created interpreter /// is disposed automatically before returning. /// </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 ExitCode Test( IEnumerable <string> args /* in */ ) { // // NOTE: This variable will contain the interpreter exit code // (either via exiting the interactive loop or from an // explicit call to the [exit] command). // ExitCode exitCode; // // NOTE: The "interpreter result" that is passed to various // methods. The interpreter itself does *NOT* store this // result internally and that behavior is a significant // difference from native Tcl. // Result result = null; // // NOTE: Do we want to intercept all variable accesses for the // interpreter? // TraceList traces = new TraceList(new TraceCallback[] { TraceCallback }); // // NOTE: First, we create a new interpreter, using some reasonable // flags and passing in the command line arguments provided // to this method. // using (Interpreter interpreter = Interpreter.Create( args, CreateFlags.Default & ~CreateFlags.ThrowOnError, traces, ref result)) { // // NOTE: Was the interpreter created successfully? // if (interpreter != null) { // // NOTE: Override the default script binder with the one // implemented by this sample application. // interpreter.Binder = new Class9( interpreter, interpreter.Binder as IScriptBinder); // // NOTE: Create instances of our custom command classes. // These instances will be used when registering the // custom commands in the interpreter. // Class2 class2 = new Class2(new CommandData( "class2", null, "This would describe the command.", new ClientData(typeof(Class1)), typeof(Class2).FullName, CommandFlags.None, null, 0)); Class12 class12 = new Class12(new CommandData( "class12", null, "This would describe the command.", null, typeof(Class12).FullName, CommandFlags.None, new Class3(null), 0)); // // NOTE: These tokens will represent the custom command // registered with the interpreter. These tokens can // later be used to remove the commands, even if they // have been renamed. // long token2 = 0; long token12 = 0; // // NOTE: Next, we [optionally] register our custom commands // in the interpreter. Normally, the return codes // should be checked here; however, in this example, // they are ignored. // /* IGNORED */ interpreter.AddCommand( class2, null, ref token2, ref result); /* IGNORED */ interpreter.AddCommand( class12, null, ref token12, ref result); // // NOTE: Next, we can evaluate one or more scripts of our // choosing, which may or may not refer to custom // commands registered in the interpreter by us or // other parties. // // NOTE: To test the custom script binder, use the script: // // object invoke Sample.Class2 TestMethod class2 // ReturnCode code2; Result result2 = null; int errorLine2 = 0; code2 = Engine.EvaluateScript(interpreter, "set x 1; unset x; class2 test; # <-- script text", ref result2, ref errorLine2); ReturnCode code12; Result result12 = null; int errorLine12 = 0; code12 = Engine.EvaluateScript(interpreter, "list [class12 example1] [class12 example2] " + "[class12 example3] [class12 options]; # ensemble", ref result12, ref errorLine12); // // NOTE: Fetch the interpreter exit code (typically zero // for "success" and non-zero for "failure"). In the // event this indicates success and the return code // indicates failure, the return code will override // this value to produce the final exit code for this // method. This is an example of what could be done // and is entirely optional. // exitCode = interpreter.ExitCode; // // NOTE: Grab the interpreter host now since we will need // it several times below to display various pieces // of information. // IHost host = interpreter.Host; // // NOTE: Show number of variable traces that were fired. // if ((getTraces > 0) || (setTraces > 0) || (unsetTraces > 0)) { if (host != null) { host.Clear(); host.WriteLine(String.Format( "read = {0}", getTraces)); host.WriteLine(String.Format( "write = {0}", setTraces)); host.WriteLine(String.Format( "delete = {0}", unsetTraces)); host.WriteLine(); host.WriteLine("Press any key to continue..."); host.Pause(); } else { // // NOTE: Do something else here... Such as log the // trace counts. // } } // // NOTE: This section of code attempts to demonstrate how // script errors *could* be handled. // if (Utility.IsSuccess(code2, false) && Utility.IsSuccess(code12, false)) { // // NOTE: Always check for a valid interpreter hosting // environment before using it as it is not // guaranteed to always be available. // if (host != null) { // // NOTE: Display the script evaluation result along // with its return code using the interpreter // host. This is an example of what could be // done and is entirely optional. // host.Clear(); host.WriteLine(String.Format( "SUCCESS, code2 = {0}", code2)); host.WriteResultLine(code2, result2); host.WriteLine(String.Format( "SUCCESS, code12 = {0}", code12)); host.WriteResultLine(code12, result12); host.WriteLine(); host.WriteLine("Press any key to continue..."); host.Pause(); } else { // // NOTE: Do something else here... Such as log the // success. // } } else { // // NOTE: Always check for a valid interpreter hosting // environment before using it as it is not // guaranteed to always be available. // if (host != null) { // // NOTE: Display the script evaluation result along // with its return code using the interpreter // host. This is an example of what could be // done and is entirely optional. // host.Clear(); host.WriteLine(String.Format( "{0}, code2 = {1}", Utility.IsSuccess(code2, false) ? "SUCCESS" : "FAILURE", code2)); host.WriteResultLine(code2, result2, errorLine2); host.WriteLine(String.Format( "{0}, code12 = {1}", Utility.IsSuccess(code12, false) ? "SUCCESS" : "FAILURE", code12)); host.WriteResultLine(code12, result12, errorLine12); host.WriteLine(); host.WriteLine("Press any key to continue..."); host.Pause(); } else { // // NOTE: Do something else here... Such as log the // failure. // } // // NOTE: If the exit code is success and the return code // indicates an error, allow the return code to // override the exit code. This is an example of // what could be done and is entirely optional. // if (exitCode == Utility.SuccessExitCode()) { if (Utility.IsSuccess(code2, false)) { exitCode = Utility.ReturnCodeToExitCode(code12); } else { exitCode = Utility.ReturnCodeToExitCode(code2); } } } // // NOTE: Finally, if applicable, remove the custom commands // we previously registered in the interpreter. // if (token12 != 0) { /* IGNORED */ interpreter.RemoveCommand(token12, null, ref result); } if (token2 != 0) { /* IGNORED */ interpreter.RemoveCommand(token2, null, ref result); } } else { #if CONSOLE // // NOTE: Creation of the interpreter failed. Attempt to // display the reason why to the system console. // Console.WriteLine(Utility.FormatResult( ReturnCode.Error, result)); #endif exitCode = Utility.FailureExitCode(); } } return(exitCode); }