/// <summary> Load the class that implements the given command and execute it. /// /// </summary> /// <param name="interp">the current interpreter. /// </param> /// <param name="argv">command arguments. /// </param> /// <exception cref=""> TclException if error happens inside the real command proc. /// </exception> public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv) { Type cmdClass = null; Command cmd; try { cmdClass = System.Type.GetType(className, true); } catch (System.Exception e) { throw new TclException(interp, "ClassNotFoundException for class \"" + className + "\""); } try { cmd = (Command)SupportClass.CreateNewInstance(cmdClass); } catch (System.UnauthorizedAccessException e1) { throw new TclException(interp, "IllegalAccessException for class \"" + cmdClass.FullName + "\""); } catch (System.InvalidCastException e3) { throw new TclException(interp, "ClassCastException for class \"" + cmdClass.FullName + "\""); } catch (System.Exception e2) { throw new TclException(interp, "InstantiationException for class \"" + cmdClass.FullName + "\""); } interp.createCommand(argv[0].ToString(), cmd); TCL.CompletionCode rc = cmd.cmdProc(interp, argv); return(rc == TCL.CompletionCode.EXIT ? TCL.CompletionCode.EXIT : TCL.CompletionCode.RETURN); }
internal void setCompletionCode(TCL.CompletionCode ccode) // New completion code. { if (ccode == TCL.CompletionCode.OK) { throw new TclRuntimeError("The reserved completion code TCL.CompletionCode.OK (0) cannot be used"); } compCode = ccode; }
public TclException(TCL.CompletionCode ccode):base() { if (ccode == TCL.CompletionCode.OK) { throw new TclRuntimeError("The reserved completion code TCL.CompletionCode.OK (0) cannot be used"); } compCode = ccode; errIndex = - 1; }
public TclException(TCL.CompletionCode ccode) : base() { if (ccode == TCL.CompletionCode.OK) { throw new TclRuntimeError("The reserved completion code TCL.CompletionCode.OK (0) cannot be used"); } compCode = ccode; errIndex = -1; }
protected internal TclException(Interp interp, string msg, TCL.CompletionCode ccode, int idx) : base(msg) { if (ccode == TCL.CompletionCode.OK) { throw new TclRuntimeError("The reserved completion code TCL.CompletionCode.OK (0) cannot be used " + "in TclException"); } compCode = ccode; errIndex = idx; if (interp != null && (System.Object)msg != null) { interp.setResult(msg); } }
protected internal TclException(Interp interp, string msg, TCL.CompletionCode ccode, int idx):base(msg) { if (ccode == TCL.CompletionCode.OK) { throw new TclRuntimeError("The reserved completion code TCL.CompletionCode.OK (0) cannot be used " + "in TclException"); } compCode = ccode; errIndex = idx; if (interp != null && (System.Object) msg != null) { interp.setResult(msg); } }
public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv) { targetInterp.preserve(); targetInterp._nestLevel++; targetInterp.ResetResult(); targetInterp.allowExceptions(); // Append the arguments to the command prefix and invoke the command // in the target interp's global namespace. TclObject[] prefv = TclList.getElements(interp, prefix); TclObject cmd = TclList.NewInstance(); cmd.Preserve(); TclList.replace(interp, cmd, 0, 0, prefv, 0, prefv.Length - 1); TclList.replace(interp, cmd, prefv.Length, 0, argv, 1, argv.Length - 1); TclObject[] cmdv = TclList.getElements(interp, cmd); TCL.CompletionCode result = targetInterp.invoke(cmdv, Interp.INVOKE_NO_TRACEBACK); cmd.Release(); targetInterp._nestLevel--; // Check if we are at the bottom of the stack for the target interpreter. // If so, check for special return codes. if (targetInterp._nestLevel == 0) { if (result == TCL.CompletionCode.RETURN) { result = targetInterp.updateReturnInfo(); } if (result != TCL.CompletionCode.OK && result != TCL.CompletionCode.ERROR) { try { targetInterp.processUnexpectedResult(result); } catch (TclException e) { result = e.GetCompletionCode(); } } } targetInterp.release(); interp.transferResult(targetInterp, result); return(TCL.CompletionCode.RETURN); }
/// <summary> This procedure is invoked to process the "catch" Tcl command. /// See the user documentation for details on what it does. /// /// </summary> /// <param name="interp">the current interpreter. /// </param> /// <param name="argv">command arguments. /// </param> /// <exception cref=""> TclException if wrong number of arguments. /// </exception> public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv) { if (argv.Length != 2 && argv.Length != 3) { throw new TclNumArgsException(interp, 1, argv, "command ?varName?"); } TclObject result; TCL.CompletionCode code = TCL.CompletionCode.OK; try { interp.eval(argv[1], 0); } catch (TclException e) { code = e.getCompletionCode(); } result = interp.getResult(); if (argv.Length == 3) { try { interp.setVar(argv[2], result, 0); } catch (TclException e) { throw new TclException(interp, "couldn't save command result in variable"); } } interp.resetResult(); interp.setResult(TclInteger.newInstance((int)code)); return(TCL.CompletionCode.RETURN); }
public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv) { string fileName = null; bool url = false; if (argv.Length == 2) { fileName = argv[1].ToString(); } else if (argv.Length == 3) { if (argv[1].ToString().Equals("-url")) { url = true; fileName = argv[2].ToString(); } } if ((System.Object)fileName == null) { throw new TclNumArgsException(interp, 1, argv, "?-url? fileName"); } try { if (url) { if (fileName.StartsWith("resource:/")) { interp.evalResource(fileName.Substring(9)); } else { interp.evalURL(null, fileName); } } else { interp.evalFile(fileName); } } catch (TclException e) { TCL.CompletionCode code = e.getCompletionCode(); if (code == TCL.CompletionCode.RETURN) { TCL.CompletionCode realCode = interp.updateReturnInfo(); if (realCode != TCL.CompletionCode.OK) { e.setCompletionCode(realCode); throw; } } else if (code == TCL.CompletionCode.ERROR) { /* * Record information telling where the error occurred. */ interp.addErrorInfo("\n (file line " + interp.errorLine + ")"); throw; } else { throw; } } return(TCL.CompletionCode.RETURN); }
public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv) { // Create the call frame and parameter bindings CallFrame frame = interp.newCallFrame(this, argv); // Execute the body interp.pushDebugStack(srcFileName, srcLineNumber); try { Parser.eval2(interp, body._array, body._index, body_length, 0); } catch (TclException e) { TCL.CompletionCode code = e.GetCompletionCode(); if (code == TCL.CompletionCode.RETURN) { TCL.CompletionCode realCode = interp.updateReturnInfo(); if (realCode != TCL.CompletionCode.OK) { e.setCompletionCode(realCode); throw; } } else if (code == TCL.CompletionCode.ERROR) { interp.AddErrorInfo("\n (procedure \"" + argv[0] + "\" line " + interp._errorLine + ")"); throw; } else if (code == TCL.CompletionCode.BREAK) { throw new TclException(interp, "invoked \"break\" outside of a loop"); } else if (code == TCL.CompletionCode.CONTINUE) { throw new TclException(interp, "invoked \"continue\" outside of a loop"); } else { throw; } } finally { interp.popDebugStack(); // The check below is a hack. The problem is that there // could be unset traces on the variables, which cause // scripts to be evaluated. This will clear the // errInProgress flag, losing stack trace information if // the procedure was exiting with an error. The code // below preserves the flag. Unfortunately, that isn't // really enough: we really should preserve the errorInfo // variable too (otherwise a nested error in the trace // script will trash errorInfo). What's really needed is // a general-purpose mechanism for saving and restoring // interpreter state. if (interp._errInProgress) { frame.Dispose(); interp._errInProgress = true; } else { frame.Dispose(); } } return(TCL.CompletionCode.RETURN); }
public override int processEvent(int flags) { // See if the command is a complete Tcl command if (Interp.commandComplete(command)) { if (tcl.lang.ConsoleThread.debug) { WriteLine("line was a complete command"); } bool eval_exception = true; TclObject commandObj = TclString.newInstance(command); try { commandObj.preserve(); Enclosing_Instance.interp.recordAndEval(commandObj, 0); eval_exception = false; } catch (TclException e) { if (tcl.lang.ConsoleThread.debug) { WriteLine("eval returned exceptional condition"); } TCL.CompletionCode code = e.getCompletionCode(); switch (code) { case TCL.CompletionCode.ERROR: Enclosing_Instance.putLine(Enclosing_Instance.err, Enclosing_Instance.interp.getResult().ToString()); break; case TCL.CompletionCode.BREAK: Enclosing_Instance.putLine(Enclosing_Instance.err, "invoked \"break\" outside of a loop"); break; case TCL.CompletionCode.CONTINUE: Enclosing_Instance.putLine(Enclosing_Instance.err, "invoked \"continue\" outside of a loop"); break; default: Enclosing_Instance.putLine(Enclosing_Instance.err, "command returned bad code: " + code); break; } } finally { commandObj.release(); } if (!eval_exception) { if (tcl.lang.ConsoleThread.debug) { WriteLine("eval returned normally"); } string evalResult = Enclosing_Instance.interp.getResult().ToString(); if (tcl.lang.ConsoleThread.debug) { WriteLine("eval result was \"" + evalResult + "\""); } if (evalResult.Length > 0) { Enclosing_Instance.putLine(Enclosing_Instance.out_Renamed, evalResult); } } // Empty out the incoming command buffer Enclosing_Instance.sbuf.Length = 0; // See if the user set a custom shell prompt for the next command TclObject prompt; try { prompt = Enclosing_Instance.interp.getVar("tcl_prompt1", TCL.VarFlag.GLOBAL_ONLY); } catch (TclException e) { prompt = null; } if (prompt != null) { try { Enclosing_Instance.interp.eval(prompt.ToString(), TCL.EVAL_GLOBAL); } catch (TclException e) { Enclosing_Instance.put(Enclosing_Instance.out_Renamed, "% "); } } else { Enclosing_Instance.put(Enclosing_Instance.out_Renamed, "% "); } return(1); } else { // Interp.commandComplete() returned false if (tcl.lang.ConsoleThread.debug) { WriteLine("line was not a complete command"); } // We don't have a complete command yet. Print out a level 2 // prompt message and wait for further inputs. TclObject prompt; try { prompt = Enclosing_Instance.interp.getVar("tcl_prompt2", TCL.VarFlag.GLOBAL_ONLY); } catch (TclException) { prompt = null; } if (prompt != null) { try { Enclosing_Instance.interp.eval(prompt.ToString(), TCL.EVAL_GLOBAL); } catch (TclException e) { Enclosing_Instance.put(Enclosing_Instance.out_Renamed, ""); } } else { Enclosing_Instance.put(Enclosing_Instance.out_Renamed, ""); } return(1); } } // end processEvent method
public Interp() { InitBlock(); //freeProc = null; errorLine = 0; // An empty result is used pretty often. We will use a shared // TclObject instance to represent the empty result so that we // don't need to create a new TclObject instance every time the // interpreter result is set to empty. m_nullResult = TclString.newInstance( "" ); m_nullResult.preserve(); // Increment refCount to 1 m_nullResult.preserve(); // Increment refCount to 2 (shared) m_result = TclString.newInstance( "" ); //m_nullResult; // correcponds to iPtr->objResultPtr m_result.preserve(); expr = new Expression(); nestLevel = 0; maxNestingDepth = 1000; frame = null; varFrame = null; returnCode = TCL.CompletionCode.OK; errorInfo = null; errorCode = null; packageTable = new Hashtable(); packageUnknown = null; cmdCount = 0; termOffset = 0; resolvers = null; evalFlags = 0; scriptFile = null; flags = 0; isSafe = false; assocData = null; globalNs = null; // force creation of global ns below globalNs = NamespaceCmd.createNamespace( this, null, null ); if ( globalNs == null ) { throw new TclRuntimeError( "Interp(): can't create global namespace" ); } // Init things that are specific to the Jacl implementation workingDir = new FileInfo( System.Environment.CurrentDirectory ); noEval = 0; notifier = Notifier.getNotifierForThread( System.Threading.Thread.CurrentThread ); notifier.preserve(); randSeedInit = false; deleted = false; errInProgress = false; errAlreadyLogged = false; errCodeSet = false; dbg = initDebugInfo(); slaveTable = new Hashtable(); targetTable = new Hashtable(); aliasTable = new Hashtable(); // init parser variables Parser.init( this ); TclParse.init( this ); // Initialize the Global (static) channel table and the local // interp channel table. interpChanTable = TclIO.getInterpChanTable( this ); // Sets up the variable trace for tcl_precision. Util.setupPrecisionTrace( this ); // Create the built-in commands. createCommands(); try { // Set up tcl_platform, tcl_version, tcl_library and other // global variables. setVar( "tcl_platform", "platform", "windows", TCL.VarFlag.GLOBAL_ONLY ); setVar( "tcl_platform", "byteOrder", "bigEndian", TCL.VarFlag.GLOBAL_ONLY ); setVar( "tcl_platform", "os", Environment.OSVersion.Platform.ToString(), TCL.VarFlag.GLOBAL_ONLY ); setVar( "tcl_platform", "osVersion", Environment.OSVersion.Version.ToString(), TCL.VarFlag.GLOBAL_ONLY ); setVar( "tcl_platform", "machine", Util.tryGetSystemProperty( "os.arch", "?" ), TCL.VarFlag.GLOBAL_ONLY ); setVar( "tcl_version", TCL_VERSION, TCL.VarFlag.GLOBAL_ONLY ); setVar( "tcl_patchLevel", TCL_PATCH_LEVEL, TCL.VarFlag.GLOBAL_ONLY ); setVar( "tcl_library", "resource:/tcl/lang/library", TCL.VarFlag.GLOBAL_ONLY ); if ( Util.Windows ) { setVar( "tcl_platform", "host_platform", "windows", TCL.VarFlag.GLOBAL_ONLY ); } else if ( Util.Mac ) { setVar( "tcl_platform", "host_platform", "macintosh", TCL.VarFlag.GLOBAL_ONLY ); } else { setVar( "tcl_platform", "host_platform", "unix", TCL.VarFlag.GLOBAL_ONLY ); } // Create the env array an populated it with proper // values. Env.initialize( this ); // Register Tcl's version number. Note: This MUST be // done before the call to evalResource, otherwise // calls to "package require tcl" will fail. pkgProvide( "Tcl", TCL_VERSION ); // Source the init.tcl script to initialize auto-loading. evalResource( "/tcl/lang/library/init.tcl" ); } catch ( TclException e ) { System.Diagnostics.Debug.WriteLine( getResult().ToString() ); SupportClass.WriteStackTrace( e, Console.Error ); throw new TclRuntimeError( "unexpected TclException: " + e.Message, e ); } }
internal void transferResult( Interp sourceInterp, TCL.CompletionCode result ) { if ( sourceInterp == this ) { return; } if ( result == TCL.CompletionCode.ERROR ) { TclObject obj; // An error occurred, so transfer error information from the source // interpreter to the target interpreter. Setting the flags tells // the target interp that it has inherited a partial traceback // chain, not just a simple error message. if ( !sourceInterp.errAlreadyLogged ) { sourceInterp.addErrorInfo( "" ); } sourceInterp.errAlreadyLogged = true; resetResult(); obj = sourceInterp.getVar( "errorInfo", TCL.VarFlag.GLOBAL_ONLY ); setVar( "errorInfo", obj, TCL.VarFlag.GLOBAL_ONLY ); obj = sourceInterp.getVar( "errorCode", TCL.VarFlag.GLOBAL_ONLY ); setVar( "errorCode", obj, TCL.VarFlag.GLOBAL_ONLY ); errInProgress = true; errCodeSet = true; } returnCode = result; setResult( sourceInterp.getResult() ); sourceInterp.resetResult(); if ( result != TCL.CompletionCode.OK ) { throw new TclException( this, getResult().ToString(), result ); } }
public TCL.CompletionCode updateReturnInfo() { TCL.CompletionCode code; code = returnCode; returnCode = TCL.CompletionCode.OK; if ( code == TCL.CompletionCode.ERROR ) { try { setVar( "errorCode", null, ( (System.Object)errorCode != null ) ? errorCode : "NONE", TCL.VarFlag.GLOBAL_ONLY ); } catch ( TclException e ) { // An error may happen during a trace to errorCode. We ignore it. // This may leave error messages inside Interp.result (which // is compatible with Tcl 8.0 behavior. } errCodeSet = true; if ( (System.Object)errorInfo != null ) { try { setVar( "errorInfo", null, errorInfo, TCL.VarFlag.GLOBAL_ONLY ); } catch ( TclException e ) { // An error may happen during a trace to errorInfo. We // ignore it. This may leave error messages inside // Interp.result (which is compatible with Tcl 8.0 // behavior. } errInProgress = true; } } return code; }
public void resetResult() { if ( m_result != m_nullResult ) { m_result.release(); m_result = TclString.newInstance( "" ); //m_nullResult; m_result.preserve(); if ( !m_nullResult.Shared ) { throw new TclRuntimeError( "m_nullResult is not shared" ); } } errAlreadyLogged = false; errInProgress = false; errCodeSet = false; returnCode = TCL.CompletionCode.OK; }
public TclException(Interp interp, string msg, TCL.CompletionCode ccode) : this(interp, msg, ccode, -1) { }
internal void setCompletionCode( TCL.CompletionCode ccode ) // New completion code. { if ( ccode == TCL.CompletionCode.OK ) { throw new TclRuntimeError( "The reserved completion code TCL.CompletionCode.OK (0) cannot be used" ); } compCode = ccode; }
/* ** 2009 July 17 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains code to implement the "sqlite" test harness ** which runs TCL commands for testing the C#-SQLite port. ** ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart ** C#-SQLite is an independent reimplementation of the SQLite software library ** ************************************************************************* */ public static void Main(string[] args) { // Array of command-line argument strings. { string fileName = null; // Create the interpreter. This will also create the built-in // Tcl commands. Interp interp = new Interp(); // Make command-line arguments available in the Tcl variables "argc" // and "argv". If the first argument doesn't start with a "-" then // strip it off and use it as the name of a script file to process. // We also set the argv0 and TCL.Tcl_interactive vars here. if ((args.Length > 0) && !(args[0].StartsWith("-"))) { fileName = args[0]; } TclObject argv = TclList.newInstance(); argv.preserve(); try { int i = 0; int argc = args.Length; if ((System.Object)fileName == null) { interp.setVar("argv0", "tcl.lang.Shell", TCL.VarFlag.GLOBAL_ONLY); interp.setVar("tcl_interactive", "1", TCL.VarFlag.GLOBAL_ONLY); } else { interp.setVar("argv0", fileName, TCL.VarFlag.GLOBAL_ONLY); interp.setVar("tcl_interactive", "0", TCL.VarFlag.GLOBAL_ONLY); i++; argc--; } for ( ; i < args.Length; i++) { TclList.append(interp, argv, TclString.newInstance(args[i])); } interp.setVar("argv", argv, TCL.VarFlag.GLOBAL_ONLY); interp.setVar("argc", System.Convert.ToString(argc), TCL.VarFlag.GLOBAL_ONLY); } catch (TclException e) { throw new TclRuntimeError("unexpected TclException: " + e.Message); } finally { argv.release(); } init_all(interp); TCL.Tcl_CreateObjCommand(interp, "load_testfixture_extensions", init_all_cmd, 0, null); // Normally we would do application specific initialization here. // However, that feature is not currently supported. // If a script file was specified then just source that file // and quit. Console.WriteLine(" C#-SQLite version " + Sqlite3.sqlite3_version); Console.WriteLine("An independent reimplementation of the SQLite software library"); Console.WriteLine("=============================================================="); Console.WriteLine(""); if ((System.Object)fileName != null) { try { interp.evalFile(fileName); } catch (TclException e) { TCL.CompletionCode code = e.getCompletionCode(); if (code == TCL.CompletionCode.RETURN) { code = interp.updateReturnInfo(); if (code != TCL.CompletionCode.OK) { System.Console.Error.WriteLine("command returned bad code: " + code); if (tcl.lang.ConsoleThread.debug) { System.Diagnostics.Debug.WriteLine("command returned bad code: " + code); } } } else if (code == TCL.CompletionCode.ERROR) { System.Console.Error.WriteLine(interp.getResult().ToString()); if (tcl.lang.ConsoleThread.debug) { System.Diagnostics.Debug.WriteLine(interp.getResult().ToString()); } System.Diagnostics.Debug.Assert(false, interp.getResult().ToString()); } else if (code != TCL.CompletionCode.EXIT) { System.Console.Error.WriteLine("command returned bad code: " + code); if (tcl.lang.ConsoleThread.debug) { System.Diagnostics.Debug.WriteLine("command returned bad code: " + code); } } } // Note that if the above interp.evalFile() returns the main // thread will exit. This may bring down the VM and stop // the execution of Tcl. // // If the script needs to handle events, it must call // vwait or do something similar. // // Note that the script can create AWT widgets. This will // start an AWT event handling thread and keep the VM up. However, // the interpreter thread (the same as the main thread) would // have exited and no Tcl scripts can be executed. interp.dispose(); Sqlite3.sqlite3_shutdown(); System.Environment.Exit(0); } if ((System.Object)fileName == null) { // We are running in interactive mode. Start the ConsoleThread // that loops, grabbing stdin and passing it to the interp. ConsoleThread consoleThread = new ConsoleThread(interp); consoleThread.IsBackground = true; consoleThread.Start(); // Loop forever to handle user input events in the command line. Notifier notifier = interp.getNotifier(); while (true) { // process events until "exit" is called. notifier.doOneEvent(TCL.ALL_EVENTS); } } } }
public void ResetResult() { if (_result != _nullResult) { _result.Release(); _result = TclString.NewInstance(""); //m_nullResult; _result.Preserve(); if (!_nullResult.Shared) { throw new TclRuntimeError("m_nullResult is not shared"); } } _errAlreadyLogged = false; _errInProgress = false; _errCodeSet = false; _returnCode = TCL.CompletionCode.OK; }