/* *---------------------------------------------------------------------- * * AppendLocals -- * * Append the local variables for the current frame to the * specified list object. * * Results: * None. * * Side effects: * None. * *---------------------------------------------------------------------- */ private static void AppendLocals(Interp interp, TclObject list, string pattern, bool includeLinks) { Var var; string varName; Hashtable localVarTable; IDictionaryEnumerator search; localVarTable = interp.varFrame.varTable; // Compiled locals do not exist in Jacl if (localVarTable != null) { for (search = localVarTable.GetEnumerator(); search.MoveNext();) { var = (Var)search.Value; varName = (string)search.Key; if (!var.isVarUndefined() && (includeLinks || !var.isVarLink())) { if (((System.Object)pattern == null) || Util.stringMatch(varName, pattern)) { TclList.append(interp, list, TclString.newInstance(varName)); } } } } }
private static void addFileToResult(Interp interp, string fileName, string separators, TclObject resultList) { string prettyFileName = fileName; int prettyLen = fileName.Length; // Java IO reuqires Windows volumes [A-Za-z]: to be followed by '\\'. if ((JACL.PLATFORM == JACL.PLATFORM_WINDOWS) && (prettyLen >= 2) && (fileName[1] == ':')) { if (prettyLen == 2) { fileName = fileName + '\\'; } else if (fileName[2] != '\\') { fileName = fileName.Substring(0, (2) - (0)) + '\\' + fileName.Substring(2); } } TclObject[] arrayObj = TclList.getElements(interp, FileUtil.splitAndTranslate(interp, fileName)); fileName = FileUtil.joinPath(interp, arrayObj, 0, arrayObj.Length); FileInfo f; if (FileUtil.getPathType(fileName) == FileUtil.PATH_ABSOLUTE) { f = FileUtil.getNewFileObj(interp, fileName); } else { f = new FileInfo(interp.getWorkingDir().FullName + "\\" + fileName); } // If the last character is a spearator, make sure the file is an // existing directory, otherwise check that the file exists. if ((prettyLen > 0) && (separators.IndexOf((System.Char)prettyFileName[prettyLen - 1]) != -1)) { if (Directory.Exists(f.FullName)) { TclList.append(interp, resultList, TclString.newInstance(prettyFileName)); } } else { bool tmpBool; if (File.Exists(f.FullName)) { tmpBool = true; } else { tmpBool = Directory.Exists(f.FullName); } if (tmpBool) { TclList.append(interp, resultList, TclString.newInstance(prettyFileName)); } } }
internal static void eval(Interp interp, Interp slaveInterp, int objIx, TclObject[] objv) { TCL.CompletionCode result; slaveInterp.preserve(); slaveInterp.allowExceptions(); try { if (objIx + 1 == objv.Length) { slaveInterp.eval(objv[objIx], 0); } else { TclObject obj = TclList.newInstance(); for (int ix = objIx; ix < objv.Length; ix++) { TclList.append(interp, obj, objv[ix]); } obj.preserve(); slaveInterp.eval(obj, 0); obj.release(); } result = slaveInterp.returnCode; } catch (TclException e) { result = e.getCompletionCode(); } slaveInterp.release(); interp.transferResult(slaveInterp, result); }
/// <summary> Chain this frame into the call frame stack and binds the parameters /// values to the formal parameters of the procedure. /// /// </summary> /// <param name="proc">the procedure. /// </param> /// <param name="proc">argv the parameter values. /// </param> /// <exception cref=""> TclException if wrong number of arguments. /// </exception> internal void chain( Procedure proc, TclObject[] objv ) { // FIXME: double check this ns thing in case where proc is renamed to different ns. this.ns = proc.ns; this.objv = objv; // FIXME : quick level hack : fix later level = ( interp.varFrame == null ) ? 1 : ( interp.varFrame.level + 1 ); caller = interp.frame; callerVar = interp.varFrame; interp.frame = this; interp.varFrame = this; // parameter bindings int numArgs = proc.argList.Length; if ( ( !proc.isVarArgs ) && ( objv.Length - 1 > numArgs ) ) { wrongNumProcArgs( objv[0], proc ); } int i, j; for ( i = 0, j = 1; i < numArgs; i++, j++ ) { // Handle the special case of the last formal being // "args". When it occurs, assign it a list consisting of // all the remaining actual arguments. TclObject varName = proc.argList[i][0]; TclObject value = null; if ( ( i == ( numArgs - 1 ) ) && proc.isVarArgs ) { value = TclList.newInstance(); value.preserve(); for ( int k = j; k < objv.Length; k++ ) { TclList.append( interp, value, objv[k] ); } interp.setVar( varName, value, 0 ); value.release(); } else { if ( j < objv.Length ) { value = objv[j]; } else if ( proc.argList[i][1] != null ) { value = proc.argList[i][1]; } else { wrongNumProcArgs( objv[0], proc ); } interp.setVar( varName, value, 0 ); } } }
public TclPosixException(Interp interp, int errno, bool appendPosixMsg, string errorMsg) : base(TCL.CompletionCode.ERROR) { string msg = getPosixMsg(errno); TclObject threeEltListObj = TclList.newInstance(); TclList.append(interp, threeEltListObj, TclString.newInstance("POSIX")); TclList.append(interp, threeEltListObj, TclString.newInstance(getPosixId(errno))); TclList.append(interp, threeEltListObj, TclString.newInstance(msg)); interp.setErrorCode(threeEltListObj); if (interp != null) { if (appendPosixMsg) { interp.setResult(errorMsg + ": " + msg); } else { interp.setResult(errorMsg); } } }
/* *---------------------------------------------------------------------- * * InfoArgsCmd -- * * Called to implement the "info args" command that returns the * argument list for a procedure. Handles the following syntax: * * info args procName * * Results: * Returns if successful, raises TclException otherwise. * * Side effects: * Returns a result in the interpreter's result object. * *---------------------------------------------------------------------- */ private static void InfoArgsCmd(Interp interp, TclObject[] objv) { string name; Procedure proc; TclObject listObj; if (objv.Length != 3) { throw new TclNumArgsException(interp, 2, objv, "procname"); } name = objv[2].ToString(); proc = Procedure.findProc(interp, name); if (proc == null) { throw new TclException(interp, "\"" + name + "\" isn't a procedure"); } // Build a return list containing the arguments. listObj = TclList.newInstance(); for (int i = 0; i < proc.argList.Length; i++) { TclObject s = TclString.newInstance(proc.argList[i][0]); TclList.append(interp, listObj, s); } interp.setResult(listObj); return; }
internal static void create(Interp interp, Interp slaveInterp, Interp masterInterp, TclObject name, TclObject targetName, int objIx, TclObject[] objv) { string inString = name.ToString(); InterpAliasCmd alias = new InterpAliasCmd(); alias.name = name; name.preserve(); alias.slaveInterp = slaveInterp; alias.targetInterp = masterInterp; alias.prefix = TclList.newInstance(); alias.prefix.preserve(); TclList.append(interp, alias.prefix, targetName); TclList.insert(interp, alias.prefix, 1, objv, objIx, objv.Length - 1); slaveInterp.createCommand(inString, alias); alias.slaveCmd = NamespaceCmd.findCommand(slaveInterp, inString, null, 0); try { interp.preventAliasLoop(slaveInterp, alias.slaveCmd); } catch (TclException e) { // Found an alias loop! The last call to Tcl_CreateObjCommand made // the alias point to itself. Delete the command and its alias // record. Be careful to wipe out its client data first, so the // command doesn't try to delete itself. slaveInterp.deleteCommandFromToken(alias.slaveCmd); throw; } // Make an entry in the alias table. If it already exists delete // the alias command. Then retry. if (slaveInterp.aliasTable.ContainsKey(inString)) { InterpAliasCmd oldAlias = (InterpAliasCmd)slaveInterp.aliasTable[inString]; slaveInterp.deleteCommandFromToken(oldAlias.slaveCmd); } alias.aliasEntry = inString; SupportClass.PutElement(slaveInterp.aliasTable, inString, alias); // Create the new command. We must do it after deleting any old command, // because the alias may be pointing at a renamed alias, as in: // // interp alias {} foo {} bar # Create an alias "foo" // rename foo zop # Now rename the alias // interp alias {} foo {} zop # Now recreate "foo"... SupportClass.PutElement(masterInterp.targetTable, alias.slaveCmd, slaveInterp); interp.setResult(name); }
public static bool Tcl_ListObjAppendElement(Interp interp, TclObject to, TclObject elemObj) { try { TclList.append(interp, to, elemObj); return(false); } catch { return(true); } }
public static TclObject Tcl_GetVar2Ex(Interp interp, string part1, string part2, VarFlag flags) { try { Var[] result = Var.lookupVar(interp, part1, part2, flags, "read", false, true); if (result == null) { // lookupVar() returns null only if VarFlag.LEAVE_ERR_MSG is // not part of the flags argument, return null in this case. return(null); } Var var = result[0]; Var array = result[1]; TclObject to = null; if (var.isVarScalar() && !var.isVarUndefined()) { to = (TclObject)var.value; //if ( to.typePtr != "String" ) //{ // double D = 0; // if ( !Double.TryParse( to.ToString(), out D ) ) { if ( String.IsNullOrEmpty( to.typePtr ) ) to.typePtr = "string"; } // else if ( to.typePtr == "ByteArray" ) // to.typePtr = "bytearray"; // else if ( to.ToString().Contains( "." ) ) // to.typePtr = "double"; // else // to.typePtr = "int"; //} return(to); } else if (var.isSQLITE3_Link()) { to = (TclObject)var.sqlite3_get(); } else { to = TclList.newInstance(); foreach (string key in ((Hashtable)array.value).Keys) { Var s = (Var)((Hashtable)array.value)[key]; if (s.value != null) { TclList.append(null, to, TclString.newInstance(s.value.ToString())); } } } return(to); } catch (Exception e) { return(null); }; }
/* *---------------------------------------------------------------------- * * InfoLevelCmd -- * * Called to implement the "info level" command that returns * information about the call stack. Handles the following syntax: * * info level ?number? * * Results: * Returns if successful, raises TclException otherwise. * * Side effects: * Returns a result in the interpreter's result object. * *---------------------------------------------------------------------- */ private static void InfoLevelCmd(Interp interp, TclObject[] objv) { int level; CallFrame frame; TclObject list; if (objv.Length == 2) { // just "info level" if (interp.varFrame == null) { interp.setResult(0); } else { interp.setResult(interp.varFrame.level); } return; } else if (objv.Length == 3) { level = TclInteger.get(interp, objv[2]); if (level <= 0) { if (interp.varFrame == null) { throw new TclException(interp, "bad level \"" + objv[2].ToString() + "\""); } level += interp.varFrame.level; } for (frame = interp.varFrame; frame != null; frame = frame.callerVar) { if (frame.level == level) { break; } } if ((frame == null) || frame.objv == null) { throw new TclException(interp, "bad level \"" + objv[2].ToString() + "\""); } list = TclList.newInstance(); for (int i = 0; i < frame.objv.Length; i++) { TclList.append(interp, list, TclString.newInstance(frame.objv[i])); } interp.setResult(list); return; } throw new TclNumArgsException(interp, 2, objv, "?number?"); }
public static TclObject Tcl_NewListObj(int nArg, TclObject[] aArg) { TclObject to = TclList.newInstance(); for (int i = 0; i < nArg; i++) { TclList.append(null, to, aArg[i]); } return(to); }
internal static void list(Interp interp, Interp slaveInterp) { TclObject result = TclList.newInstance(); interp.setResult(result); IEnumerator aliases = slaveInterp.aliasTable.Values.GetEnumerator(); while (aliases.MoveNext()) { InterpAliasCmd alias = (InterpAliasCmd)aliases.Current; TclList.append(interp, result, alias.name); } }
/* *---------------------------------------------------------------------- * * InfoProcsCmd -- * * Called to implement the "info procs" command that returns the * procedures in the current namespace that match an optional pattern. * Handles the following syntax: * * info procs ?pattern? * * Results: * Returns if successful, raises TclException otherwise. * * Side effects: * Returns a result in the interpreter's result object. * *---------------------------------------------------------------------- */ private static void InfoProcsCmd(Interp interp, TclObject[] objv) { string cmdName, pattern; NamespaceCmd.Namespace currNs = NamespaceCmd.getCurrentNamespace(interp); IDictionaryEnumerator search; WrappedCommand cmd, realCmd; TclObject list; if (objv.Length == 2) { pattern = null; } else if (objv.Length == 3) { pattern = objv[2].ToString(); } else { throw new TclNumArgsException(interp, 2, objv, "?pattern?"); } // Scan through the current namespace's command table and return a list // of all procs that match the pattern. list = TclList.newInstance(); for (search = currNs.cmdTable.GetEnumerator(); search.MoveNext();) { cmdName = ((string)search.Key); cmd = (WrappedCommand)search.Value; // If the command isn't itself a proc, it still might be an // imported command that points to a "real" proc in a different // namespace. realCmd = NamespaceCmd.getOriginalCommand(cmd); if (Procedure.isProc(cmd) || ((realCmd != null) && Procedure.isProc(realCmd))) { if (((System.Object)pattern == null) || Util.stringMatch(cmdName, pattern)) { TclList.append(interp, list, TclString.newInstance(cmdName)); } } } interp.setResult(list); return; }
/* *---------------------------------------------------------------------- * * InfoGlobalsCmd -- * * Called to implement the "info globals" command that returns the list * of global variables matching an optional pattern. Handles the * following syntax: * * info globals ?pattern?* * * Results: * Returns if successful, raises TclException otherwise. * * Side effects: * Returns a result in the interpreter's result object. * *---------------------------------------------------------------------- */ private static void InfoGlobalsCmd(Interp interp, TclObject[] objv) { string varName, pattern; NamespaceCmd.Namespace globalNs = NamespaceCmd.getGlobalNamespace(interp); IDictionaryEnumerator search; Var var; TclObject list; if (objv.Length == 2) { pattern = null; } else if (objv.Length == 3) { pattern = objv[2].ToString(); } else { throw new TclNumArgsException(interp, 2, objv, "?pattern?"); } // Scan through the global :: namespace's variable table and create a // list of all global variables that match the pattern. list = TclList.newInstance(); for (search = globalNs.varTable.GetEnumerator(); search.MoveNext();) { varName = ((string)search.Key); var = (Var)search.Value; if (var.isVarUndefined()) { continue; } if (((System.Object)pattern == null) || Util.stringMatch(varName, pattern)) { TclList.append(interp, list, TclString.newInstance(varName)); } } interp.setResult(list); return; }
/// <summary> See Tcl user documentation for details.</summary> public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv) { TclObject list = TclList.newInstance(); list.preserve(); try { for (int i = 1; i < argv.Length; i++) { TclList.append(interp, list, argv[i]); } interp.setResult(list); } finally { list.release(); } return(TCL.CompletionCode.RETURN); }
internal static void hidden(Interp interp, Interp slaveInterp) { if (slaveInterp.hiddenCmdTable == null) { return; } TclObject result = TclList.newInstance(); interp.setResult(result); IEnumerator hiddenCmds = slaveInterp.hiddenCmdTable.Keys.GetEnumerator(); while (hiddenCmds.MoveNext()) { string cmdName = (string)hiddenCmds.Current; TclList.append(interp, result, TclString.newInstance(cmdName)); } }
/* *---------------------------------------------------------------------- * * InfoNameOfExecutableCmd -- * * Called to implement the "info nameofexecutable" command that returns * the name of the binary file running this application. Handles the * following syntax: * * info nameofexecutable * * Results: * Returns if successful, raises TclException otherwise. * * Side effects: * Returns a result in the interpreter's result object. * *---------------------------------------------------------------------- */ private static void InfoNameOfExecutableCmd(Interp interp, TclObject[] objv) { if (objv.Length != 2) { throw new TclNumArgsException(interp, 2, objv, null); } // We depend on a user defined property named "JAVA" since // the JDK provides no means to learn the name of the executable // that launched the application. string nameOfExecutable = "nacl"; if ((System.Object)nameOfExecutable != null) { TclObject result = TclList.newInstance(); TclList.append(interp, result, TclString.newInstance(nameOfExecutable)); TclList.append(interp, result, TclString.newInstance("tcl.lang.Shell")); interp.setResult(result); } return; }
/* *---------------------------------------------------------------------- * * InfoCommandsCmd -- * * Called to implement the "info commands" command that returns the * list of commands in the interpreter that match an optional pattern. * The pattern, if any, consists of an optional sequence of namespace * names separated by "::" qualifiers, which is followed by a * glob-style pattern that restricts which commands are returned. * Handles the following syntax: * * info commands ?pattern? * * Results: * Returns if successful, raises TclException otherwise. * * Side effects: * Returns a result in the interpreter's result object. * *---------------------------------------------------------------------- */ private static void InfoCommandsCmd(Interp interp, TclObject[] objv) { string cmdName, pattern, simplePattern; IDictionaryEnumerator search; NamespaceCmd.Namespace ns; NamespaceCmd.Namespace globalNs = NamespaceCmd.getGlobalNamespace(interp); NamespaceCmd.Namespace currNs = NamespaceCmd.getCurrentNamespace(interp); TclObject list, elemObj; bool specificNsInPattern = false; // Init. to avoid compiler warning. WrappedCommand cmd; // Get the pattern and find the "effective namespace" in which to // list commands. if (objv.Length == 2) { simplePattern = null; ns = currNs; specificNsInPattern = false; } else if (objv.Length == 3) { // From the pattern, get the effective namespace and the simple // pattern (no namespace qualifiers or ::'s) at the end. If an // error was found while parsing the pattern, return it. Otherwise, // if the namespace wasn't found, just leave ns NULL: we will // return an empty list since no commands there can be found. pattern = objv[2].ToString(); // Java does not support passing an address so we pass // an array of size 1 and then assign arr[0] to the value NamespaceCmd.Namespace[] nsArr = new NamespaceCmd.Namespace[1]; NamespaceCmd.Namespace[] dummy1Arr = new NamespaceCmd.Namespace[1]; NamespaceCmd.Namespace[] dummy2Arr = new NamespaceCmd.Namespace[1]; string[] simplePatternArr = new string[1]; NamespaceCmd.getNamespaceForQualName(interp, pattern, null, 0, nsArr, dummy1Arr, dummy2Arr, simplePatternArr); // Get the values out of the arrays! ns = nsArr[0]; simplePattern = simplePatternArr[0]; if (ns != null) { // we successfully found the pattern's ns specificNsInPattern = (simplePattern.CompareTo(pattern) != 0); } } else { throw new TclNumArgsException(interp, 2, objv, "?pattern?"); } // Scan through the effective namespace's command table and create a // list with all commands that match the pattern. If a specific // namespace was requested in the pattern, qualify the command names // with the namespace name. list = TclList.newInstance(); if (ns != null) { search = ns.cmdTable.GetEnumerator(); while (search.MoveNext()) { cmdName = ((string)search.Key); if (((System.Object)simplePattern == null) || Util.stringMatch(cmdName, simplePattern)) { if (specificNsInPattern) { cmd = (WrappedCommand)search.Value; elemObj = TclString.newInstance(interp.getCommandFullName(cmd)); } else { elemObj = TclString.newInstance(cmdName); } TclList.append(interp, list, elemObj); } } // If the effective namespace isn't the global :: namespace, and a // specific namespace wasn't requested in the pattern, then add in // all global :: commands that match the simple pattern. Of course, // we add in only those commands that aren't hidden by a command in // the effective namespace. if ((ns != globalNs) && !specificNsInPattern) { search = globalNs.cmdTable.GetEnumerator(); while (search.MoveNext()) { cmdName = ((string)search.Key); if (((System.Object)simplePattern == null) || Util.stringMatch(cmdName, simplePattern)) { if (ns.cmdTable[cmdName] == null) { TclList.append(interp, list, TclString.newInstance(cmdName)); } } } } } interp.setResult(list); return; }
/* *---------------------------------------------------------------------- * * InfoVarsCmd -- * * Called to implement the "info vars" command that returns the * list of variables in the interpreter that match an optional pattern. * The pattern, if any, consists of an optional sequence of namespace * names separated by "::" qualifiers, which is followed by a * glob-style pattern that restricts which variables are returned. * Handles the following syntax: * * info vars ?pattern? * * Results: * Returns if successful, raises TclException otherwise. * * Side effects: * Returns a result in the interpreter's result object. * *---------------------------------------------------------------------- */ private static void InfoVarsCmd(Interp interp, TclObject[] objv) { string varName, pattern, simplePattern; IDictionaryEnumerator search; Var var; NamespaceCmd.Namespace ns; NamespaceCmd.Namespace globalNs = NamespaceCmd.getGlobalNamespace(interp); NamespaceCmd.Namespace currNs = NamespaceCmd.getCurrentNamespace(interp); TclObject list, elemObj; bool specificNsInPattern = false; // Init. to avoid compiler warning. // Get the pattern and find the "effective namespace" in which to // list variables. We only use this effective namespace if there's // no active Tcl procedure frame. if (objv.Length == 2) { simplePattern = null; ns = currNs; specificNsInPattern = false; } else if (objv.Length == 3) { // From the pattern, get the effective namespace and the simple // pattern (no namespace qualifiers or ::'s) at the end. If an // error was found while parsing the pattern, return it. Otherwise, // if the namespace wasn't found, just leave ns = null: we will // return an empty list since no variables there can be found. pattern = objv[2].ToString(); // Java does not support passing an address so we pass // an array of size 1 and then assign arr[0] to the value NamespaceCmd.Namespace[] nsArr = new NamespaceCmd.Namespace[1]; NamespaceCmd.Namespace[] dummy1Arr = new NamespaceCmd.Namespace[1]; NamespaceCmd.Namespace[] dummy2Arr = new NamespaceCmd.Namespace[1]; string[] simplePatternArr = new string[1]; NamespaceCmd.getNamespaceForQualName(interp, pattern, null, 0, nsArr, dummy1Arr, dummy2Arr, simplePatternArr); // Get the values out of the arrays! ns = nsArr[0]; simplePattern = simplePatternArr[0]; if (ns != null) { // we successfully found the pattern's ns specificNsInPattern = (simplePattern.CompareTo(pattern) != 0); } } else { throw new TclNumArgsException(interp, 2, objv, "?pattern?"); } // If the namespace specified in the pattern wasn't found, just return. if (ns == null) { return; } list = TclList.newInstance(); if ((interp.varFrame == null) || !interp.varFrame.isProcCallFrame || specificNsInPattern) { // There is no frame pointer, the frame pointer was pushed only // to activate a namespace, or we are in a procedure call frame // but a specific namespace was specified. Create a list containing // only the variables in the effective namespace's variable table. search = ns.varTable.GetEnumerator(); while (search.MoveNext()) { varName = ((string)search.Key); var = (Var)search.Value; if (!var.isVarUndefined() || ((var.flags & VarFlags.NAMESPACE_VAR) != 0)) { if (((System.Object)simplePattern == null) || Util.stringMatch(varName, simplePattern)) { if (specificNsInPattern) { elemObj = TclString.newInstance(Var.getVariableFullName(interp, var)); } else { elemObj = TclString.newInstance(varName); } TclList.append(interp, list, elemObj); } } } // If the effective namespace isn't the global :: namespace, and a // specific namespace wasn't requested in the pattern (i.e., the // pattern only specifies variable names), then add in all global :: // variables that match the simple pattern. Of course, add in only // those variables that aren't hidden by a variable in the effective // namespace. if ((ns != globalNs) && !specificNsInPattern) { search = globalNs.varTable.GetEnumerator(); while (search.MoveNext()) { varName = ((string)search.Key); var = (Var)search.Value; if (!var.isVarUndefined() || ((var.flags & VarFlags.NAMESPACE_VAR) != 0)) { if (((System.Object)simplePattern == null) || Util.stringMatch(varName, simplePattern)) { // Skip vars defined in current namespace if (ns.varTable[varName] == null) { TclList.append(interp, list, TclString.newInstance(varName)); } } } } } } else { AppendLocals(interp, list, simplePattern, true); } interp.setResult(list); return; }
public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv) { int arg; // Index of next argument to consume. char[] format = null; // User specified format string. char cmd; // Current format character. int cursor; // Current position within result buffer. int maxPos; // Greatest position within result buffer that // cursor has visited. int value = 0; // Current integer value to be packed. // Initialized to avoid compiler warning. int offset, size = 0, length; //, index; if (argv.Length < 2) { throw new TclNumArgsException(interp, 1, argv, "option ?arg arg ...?"); } int cmdIndex = TclIndex.get(interp, argv[1], validCmds, "option", 0); switch (cmdIndex) { case CMD_FORMAT: { if (argv.Length < 3) { throw new TclNumArgsException(interp, 2, argv, "formatString ?arg arg ...?"); } // To avoid copying the data, we format the string in two passes. // The first pass computes the size of the output buffer. The // second pass places the formatted data into the buffer. format = argv[2].ToString().ToCharArray(); arg = 3; length = 0; offset = 0; System.Int32 parsePos = 0; while ((cmd = GetFormatSpec(format, ref parsePos)) != FORMAT_END) { int count = GetFormatCount(format, ref parsePos); switch (cmd) { case 'a': case 'A': case 'b': case 'B': case 'h': case 'H': { // For string-type specifiers, the count corresponds // to the number of bytes in a single argument. if (arg >= argv.Length) { missingArg(interp); } if (count == BINARY_ALL) { count = TclByteArray.getLength(interp, argv[arg]); } else if (count == BINARY_NOCOUNT) { count = 1; } arg++; switch (cmd) { case 'a': case 'A': offset += count; break; case 'b': case 'B': offset += (count + 7) / 8; break; case 'h': case 'H': offset += (count + 1) / 2; break; } break; } case 'c': case 's': case 'S': case 'i': case 'I': case 'f': case 'd': { if (arg >= argv.Length) { missingArg(interp); } switch (cmd) { case 'c': size = 1; break; case 's': case 'S': size = 2; break; case 'i': case 'I': size = 4; break; case 'f': size = 4; break; case 'd': size = 8; break; } // For number-type specifiers, the count corresponds // to the number of elements in the list stored in // a single argument. If no count is specified, then // the argument is taken as a single non-list value. if (count == BINARY_NOCOUNT) { arg++; count = 1; } else { int listc = TclList.getLength(interp, argv[arg++]); if (count == BINARY_ALL) { count = listc; } else if (count > listc) { throw new TclException(interp, "number of elements in list" + " does not match count"); } } offset += count * size; break; } case 'x': { if (count == BINARY_ALL) { throw new TclException(interp, "cannot use \"*\"" + " in format string with \"x\""); } if (count == BINARY_NOCOUNT) { count = 1; } offset += count; break; } case 'X': { if (count == BINARY_NOCOUNT) { count = 1; } if ((count > offset) || (count == BINARY_ALL)) { count = offset; } if (offset > length) { length = offset; } offset -= count; break; } case '@': { if (offset > length) { length = offset; } if (count == BINARY_ALL) { offset = length; } else if (count == BINARY_NOCOUNT) { alephWithoutCount(interp); } else { offset = count; } break; } default: { badField(interp, cmd); } break; } } if (offset > length) { length = offset; } if (length == 0) { return(TCL.CompletionCode.RETURN); } // Prepare the result object by preallocating the calculated // number of bytes and filling with nulls. TclObject resultObj = TclByteArray.newInstance(); resultObj._typePtr = "bytearray"; byte[] resultBytes = TclByteArray.setLength(interp, resultObj, length); interp.setResult(resultObj); // Pack the data into the result object. Note that we can skip // the error checking during this pass, since we have already // parsed the string once. arg = 3; cursor = 0; maxPos = cursor; parsePos = 0; while ((cmd = GetFormatSpec(format, ref parsePos)) != FORMAT_END) { int count = GetFormatCount(format, ref parsePos); if ((count == 0) && (cmd != '@')) { arg++; continue; } switch (cmd) { case 'a': case 'A': { byte pad = (cmd == 'a') ? (byte)0 : (byte)SupportClass.Identity(' '); byte[] bytes = TclByteArray.getBytes(interp, argv[arg++]); length = bytes.Length; if (count == BINARY_ALL) { count = length; } else if (count == BINARY_NOCOUNT) { count = 1; } if (length >= count) { Array.Copy(bytes, 0, resultBytes, cursor, count); } else { Array.Copy(bytes, 0, resultBytes, cursor, length); for (int ix = 0; ix < count - length; ix++) { resultBytes[cursor + length + ix] = pad; } } cursor += count; break; } case 'b': case 'B': { char[] str = argv[arg++].ToString().ToCharArray(); if (count == BINARY_ALL) { count = str.Length; } else if (count == BINARY_NOCOUNT) { count = 1; } int last = cursor + ((count + 7) / 8); if (count > str.Length) { count = str.Length; } if (cmd == 'B') { for (offset = 0; offset < count; offset++) { value <<= 1; if (str[offset] == '1') { value |= 1; } else if (str[offset] != '0') { expectedButGot(interp, "binary", new string( str )); } if (((offset + 1) % 8) == 0) { resultBytes[cursor++] = (byte)value; value = 0; } } } else { for (offset = 0; offset < count; offset++) { value >>= 1; if (str[offset] == '1') { value |= 128; } else if (str[offset] != '0') { expectedButGot(interp, "binary", new string( str )); } if (((offset + 1) % 8) == 0) { resultBytes[cursor++] = (byte)value; value = 0; } } } if ((offset % 8) != 0) { if (cmd == 'B') { value <<= 8 - (offset % 8); } else { value >>= 8 - (offset % 8); } resultBytes[cursor++] = (byte)value; } while (cursor < last) { resultBytes[cursor++] = 0; } break; } case 'h': case 'H': { char[] str = argv[arg++].ToString().ToCharArray(); if (count == BINARY_ALL) { count = str.Length; } else if (count == BINARY_NOCOUNT) { count = 1; } int last = cursor + ((count + 1) / 2); if (count > str.Length) { count = str.Length; } if (cmd == 'H') { for (offset = 0; offset < count; offset++) { value <<= 4; int c = HEXDIGITS.IndexOf(Char.ToLower(str[offset])); if (c < 0) { expectedButGot(interp, "hexadecimal", new string( str )); } value |= (c & 0xf); if ((offset % 2) != 0) { resultBytes[cursor++] = (byte)value; value = 0; } } } else { for (offset = 0; offset < count; offset++) { value >>= 4; int c = HEXDIGITS.IndexOf(Char.ToLower(str[offset])); if (c < 0) { expectedButGot(interp, "hexadecimal", new string( str )); } value |= ((c << 4) & 0xf0); if ((offset % 2) != 0) { resultBytes[cursor++] = (byte)value; value = 0; } } } if ((offset % 2) != 0) { if (cmd == 'H') { value <<= 4; } else { value >>= 4; } resultBytes[cursor++] = (byte)value; } while (cursor < last) { resultBytes[cursor++] = 0; } break; } case 'c': case 's': case 'S': case 'i': case 'I': case 'f': case 'd': { TclObject[] listv; if (count == BINARY_NOCOUNT) { listv = new TclObject[1]; listv[0] = argv[arg++]; count = 1; } else { listv = TclList.getElements(interp, argv[arg++]); if (count == BINARY_ALL) { count = listv.Length; } } for (int ix = 0; ix < count; ix++) { cursor = FormatNumber(interp, cmd, listv[ix], resultBytes, cursor); } break; } case 'x': { if (count == BINARY_NOCOUNT) { count = 1; } for (int ix = 0; ix < count; ix++) { resultBytes[cursor++] = 0; } break; } case 'X': { if (cursor > maxPos) { maxPos = cursor; } if (count == BINARY_NOCOUNT) { count = 1; } if (count == BINARY_ALL || count > cursor) { cursor = 0; } else { cursor -= count; } break; } case '@': { if (cursor > maxPos) { maxPos = cursor; } if (count == BINARY_ALL) { cursor = maxPos; } else { cursor = count; } break; } } } break; } case CMD_SCAN: { if (argv.Length < 4) { throw new TclNumArgsException(interp, 2, argv, "value formatString ?varName varName ...?"); } byte[] src = TclByteArray.getBytes(interp, argv[2]); length = src.Length; format = argv[3].ToString().ToCharArray(); arg = 4; cursor = 0; offset = 0; System.Int32 parsePos = 0; while ((cmd = GetFormatSpec(format, ref parsePos)) != FORMAT_END) { int count = GetFormatCount(format, ref parsePos); switch (cmd) { case 'a': case 'A': { if (arg >= argv.Length) { missingArg(interp); } if (count == BINARY_ALL) { count = length - offset; } else { if (count == BINARY_NOCOUNT) { count = 1; } if (count > length - offset) { break; } } size = count; // Trim trailing nulls and spaces, if necessary. if (cmd == 'A') { while (size > 0) { if (src[offset + size - 1] != '\x0000' && src[offset + size - 1] != ' ') { break; } size--; } } interp.setVar(argv[arg++], TclByteArray.newInstance(src, offset, size), 0); offset += count; break; } case 'b': case 'B': { if (arg >= argv.Length) { missingArg(interp); } if (count == BINARY_ALL) { count = (length - offset) * 8; } else { if (count == BINARY_NOCOUNT) { count = 1; } if (count > (length - offset) * 8) { break; } } System.Text.StringBuilder s = new System.Text.StringBuilder(count); int thisOffset = offset; if (cmd == 'b') { for (int ix = 0; ix < count; ix++) { if ((ix % 8) != 0) { value >>= 1; } else { value = src[thisOffset++]; } s.Append((value & 1) != 0 ? '1' : '0'); } } else { for (int ix = 0; ix < count; ix++) { if ((ix % 8) != 0) { value <<= 1; } else { value = src[thisOffset++]; } s.Append((value & 0x80) != 0 ? '1' : '0'); } } interp.setVar(argv[arg++], TclString.newInstance(s.ToString()), 0); offset += (count + 7) / 8; break; } case 'h': case 'H': { if (arg >= argv.Length) { missingArg(interp); } if (count == BINARY_ALL) { count = (length - offset) * 2; } else { if (count == BINARY_NOCOUNT) { count = 1; } if (count > (length - offset) * 2) { break; } } System.Text.StringBuilder s = new System.Text.StringBuilder(count); int thisOffset = offset; if (cmd == 'h') { for (int ix = 0; ix < count; ix++) { if ((ix % 2) != 0) { value >>= 4; } else { value = src[thisOffset++]; } s.Append(HEXDIGITS[value & 0xf]); } } else { for (int ix = 0; ix < count; ix++) { if ((ix % 2) != 0) { value <<= 4; } else { value = src[thisOffset++]; } s.Append(HEXDIGITS[value >> 4 & 0xf]); } } interp.setVar(argv[arg++], TclString.newInstance(s.ToString()), 0); offset += (count + 1) / 2; break; } case 'c': case 's': case 'S': case 'i': case 'I': case 'f': case 'd': { if (arg >= argv.Length) { missingArg(interp); } switch (cmd) { case 'c': size = 1; break; case 's': case 'S': size = 2; break; case 'i': case 'I': size = 4; break; case 'f': size = 4; break; case 'd': size = 8; break; } TclObject valueObj; if (count == BINARY_NOCOUNT) { if (length - offset < size) { break; } valueObj = ScanNumber(src, offset, cmd); offset += size; } else { if (count == BINARY_ALL) { count = (length - offset) / size; } if (length - offset < count * size) { break; } valueObj = TclList.newInstance(); int thisOffset = offset; for (int ix = 0; ix < count; ix++) { TclList.append(null, valueObj, ScanNumber(src, thisOffset, cmd)); thisOffset += size; } offset += count * size; } interp.setVar(argv[arg++], valueObj, 0); break; } case 'x': { if (count == BINARY_NOCOUNT) { count = 1; } if (count == BINARY_ALL || count > length - offset) { offset = length; } else { offset += count; } break; } case 'X': { if (count == BINARY_NOCOUNT) { count = 1; } if (count == BINARY_ALL || count > offset) { offset = 0; } else { offset -= count; } break; } case '@': { if (count == BINARY_NOCOUNT) { alephWithoutCount(interp); } if (count == BINARY_ALL || count > length) { offset = length; } else { offset = count; } break; } default: { badField(interp, cmd); } break; } } // Set the result to the last position of the cursor. interp.setResult(arg - 4); } break; } return(TCL.CompletionCode.RETURN); }
/// <summary> This procedure is invoked to process the "fconfigure" 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> public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv) { Channel chan; // The channel being operated on this method if ((argv.Length < 2) || (((argv.Length % 2) == 1) && (argv.Length != 3))) { throw new TclNumArgsException(interp, 1, argv, "channelId ?optionName? ?value? ?optionName value?..."); } chan = TclIO.getChannel(interp, argv[1].ToString()); if (chan == null) { throw new TclException(interp, "can not find channel named \"" + argv[1].ToString() + "\""); } if (argv.Length == 2) { // return list of all name/value pairs for this channelId TclObject list = TclList.newInstance(); TclList.append(interp, list, TclString.newInstance("-blocking")); TclList.append(interp, list, TclBoolean.newInstance(chan.Blocking)); TclList.append(interp, list, TclString.newInstance("-buffering")); TclList.append(interp, list, TclString.newInstance(TclIO.getBufferingString(chan.Buffering))); TclList.append(interp, list, TclString.newInstance("-buffersize")); TclList.append(interp, list, TclInteger.newInstance(chan.BufferSize)); // -encoding TclList.append(interp, list, TclString.newInstance("-encoding")); System.Text.Encoding javaEncoding = chan.Encoding; string tclEncoding; if ((System.Object)javaEncoding == null) { tclEncoding = "binary"; } else { tclEncoding = EncodingCmd.getTclName(javaEncoding); } TclList.append(interp, list, TclString.newInstance(tclEncoding)); // -eofchar TclList.append(interp, list, TclString.newInstance("-eofchar")); if (chan.ReadOnly) { char eofChar = chan.InputEofChar; TclList.append(interp, list, (eofChar == 0) ? TclString.newInstance("") : TclString.newInstance(eofChar)); } else if (chan.WriteOnly) { char eofChar = chan.OutputEofChar; TclList.append(interp, list, (eofChar == 0) ? TclString.newInstance("") : TclString.newInstance(eofChar)); } else if (chan.ReadWrite) { char inEofChar = chan.InputEofChar; char outEofChar = chan.OutputEofChar; TclObject eofchar_pair = TclList.newInstance(); TclList.append(interp, eofchar_pair, (inEofChar == 0) ? TclString.newInstance("") : TclString.newInstance(inEofChar)); TclList.append(interp, eofchar_pair, (outEofChar == 0) ? TclString.newInstance("") : TclString.newInstance(outEofChar)); TclList.append(interp, list, eofchar_pair); } else { // Not readable or writeable, do nothing } // -translation TclList.append(interp, list, TclString.newInstance("-translation")); if (chan.ReadOnly) { TclList.append(interp, list, TclString.newInstance(TclIO.getTranslationString(chan.InputTranslation))); } else if (chan.WriteOnly) { TclList.append(interp, list, TclString.newInstance(TclIO.getTranslationString(chan.OutputTranslation))); } else if (chan.ReadWrite) { TclObject translation_pair = TclList.newInstance(); TclList.append(interp, translation_pair, TclString.newInstance(TclIO.getTranslationString(chan.InputTranslation))); TclList.append(interp, translation_pair, TclString.newInstance(TclIO.getTranslationString(chan.OutputTranslation))); TclList.append(interp, list, translation_pair); } else { // Not readable or writeable, do nothing } interp.setResult(list); } if (argv.Length == 3) { // return value for supplied name int index = TclIndex.get(interp, argv[2], validCmds, "option", 0); switch (index) { case OPT_BLOCKING: { // -blocking interp.setResult(chan.Blocking); break; } case OPT_BUFFERING: { // -buffering interp.setResult(TclIO.getBufferingString(chan.Buffering)); break; } case OPT_BUFFERSIZE: { // -buffersize interp.setResult(chan.BufferSize); break; } case OPT_ENCODING: { // -encoding System.Text.Encoding javaEncoding = chan.Encoding; if ((System.Object)javaEncoding == null) { interp.setResult("binary"); } else { interp.setResult(EncodingCmd.getTclName(javaEncoding)); } break; } case OPT_EOFCHAR: { // -eofchar if (chan.ReadOnly) { char eofChar = chan.InputEofChar; interp.setResult((eofChar == 0) ? TclString.newInstance("") : TclString.newInstance(eofChar)); } else if (chan.WriteOnly) { char eofChar = chan.OutputEofChar; interp.setResult((eofChar == 0) ? TclString.newInstance("") : TclString.newInstance(eofChar)); } else if (chan.ReadWrite) { char inEofChar = chan.InputEofChar; char outEofChar = chan.OutputEofChar; TclObject eofchar_pair = TclList.newInstance(); TclList.append(interp, eofchar_pair, (inEofChar == 0) ? TclString.newInstance("") : TclString.newInstance(inEofChar)); TclList.append(interp, eofchar_pair, (outEofChar == 0) ? TclString.newInstance("") : TclString.newInstance(outEofChar)); interp.setResult(eofchar_pair); } else { // Not readable or writeable, do nothing } break; } case OPT_TRANSLATION: { // -translation if (chan.ReadOnly) { interp.setResult(TclIO.getTranslationString(chan.InputTranslation)); } else if (chan.WriteOnly) { interp.setResult(TclIO.getTranslationString(chan.OutputTranslation)); } else if (chan.ReadWrite) { TclObject translation_pair = TclList.newInstance(); TclList.append(interp, translation_pair, TclString.newInstance(TclIO.getTranslationString(chan.InputTranslation))); TclList.append(interp, translation_pair, TclString.newInstance(TclIO.getTranslationString(chan.OutputTranslation))); interp.setResult(translation_pair); } else { // Not readable or writeable, do nothing } break; } default: { throw new TclRuntimeError("Fconfigure.cmdProc() error: " + "incorrect index returned from TclIndex.get()"); } } } for (int i = 3; i < argv.Length; i += 2) { // Iterate through the list setting the name with the // corresponding value. int index = TclIndex.get(interp, argv[i - 1], validCmds, "option", 0); switch (index) { case OPT_BLOCKING: { // -blocking chan.Blocking = TclBoolean.get(interp, argv[i]); break; } case OPT_BUFFERING: { // -buffering int id = TclIO.getBufferingID(argv[i].ToString()); if (id == -1) { throw new TclException(interp, "bad value for -buffering: must be " + "one of full, line, or none"); } chan.Buffering = id; break; } case OPT_BUFFERSIZE: { // -buffersize chan.BufferSize = TclInteger.get(interp, argv[i]); break; } case OPT_ENCODING: { // -encoding string tclEncoding = argv[i].ToString(); if (tclEncoding.Equals("") || tclEncoding.Equals("binary")) { chan.Encoding = null; } else { System.Text.Encoding javaEncoding = EncodingCmd.getJavaName(tclEncoding); if ((System.Object)javaEncoding == null) { throw new TclException(interp, "unknown encoding \"" + tclEncoding + "\""); } chan.Encoding = javaEncoding; } break; } case OPT_EOFCHAR: { // -eofchar TclList.setListFromAny(interp, argv[i]); int length = TclList.getLength(interp, argv[i]); if (length > 2) { throw new TclException(interp, "bad value for -eofchar: " + "should be a list of zero, one, or two elements"); } char inputEofChar, outputEofChar; string s; if (length == 0) { inputEofChar = outputEofChar = (char)(0); } else if (length == 1) { s = TclList.index(interp, argv[i], 0).ToString(); inputEofChar = outputEofChar = s[0]; } else { s = TclList.index(interp, argv[i], 0).ToString(); inputEofChar = s[0]; s = TclList.index(interp, argv[i], 1).ToString(); outputEofChar = s[0]; } chan.InputEofChar = inputEofChar; chan.OutputEofChar = outputEofChar; break; } case OPT_TRANSLATION: { // -translation TclList.setListFromAny(interp, argv[i]); int length = TclList.getLength(interp, argv[i]); if (length < 1 || length > 2) { throw new TclException(interp, "bad value for -translation: " + "must be a one or two element list"); } string inputTranslationArg, outputTranslationArg; int inputTranslation, outputTranslation; if (length == 2) { inputTranslationArg = TclList.index(interp, argv[i], 0).ToString(); inputTranslation = TclIO.getTranslationID(inputTranslationArg); outputTranslationArg = TclList.index(interp, argv[i], 1).ToString(); outputTranslation = TclIO.getTranslationID(outputTranslationArg); } else { outputTranslationArg = inputTranslationArg = argv[i].ToString(); outputTranslation = inputTranslation = TclIO.getTranslationID(outputTranslationArg); } if ((inputTranslation == -1) || (outputTranslation == -1)) { throw new TclException(interp, "bad value for -translation: " + "must be one of auto, binary, cr, lf, " + "crlf, or platform"); } if (outputTranslation == TclIO.TRANS_AUTO) { outputTranslation = TclIO.TRANS_PLATFORM; } if (chan.ReadOnly) { chan.InputTranslation = inputTranslation; if (inputTranslationArg.Equals("binary")) { chan.Encoding = null; } } else if (chan.WriteOnly) { chan.OutputTranslation = outputTranslation; if (outputTranslationArg.Equals("binary")) { chan.Encoding = null; } } else if (chan.ReadWrite) { chan.InputTranslation = inputTranslation; chan.OutputTranslation = outputTranslation; if (inputTranslationArg.Equals("binary") || outputTranslationArg.Equals("binary")) { chan.Encoding = null; } } else { // Not readable or writeable, do nothing } break; } default: { throw new TclRuntimeError("Fconfigure.cmdProc() error: " + "incorrect index returned from TclIndex.get()"); } } } return(TCL.CompletionCode.RETURN); }
public TclObject get() { TclObject obj; TclToken token; string typeString; int nextIndex; string cmd; int i; System.Diagnostics.Debug.WriteLine("Entered TclParse.get()"); System.Diagnostics.Debug.WriteLine("numTokens is " + numTokens); obj = TclList.newInstance(); try { if (commentSize > 0) { TclList.append(interp, obj, TclString.newInstance(new string( inString, commentStart, commentSize ))); } else { TclList.append(interp, obj, TclString.newInstance("-")); } if (commandStart >= (endIndex + 1)) { commandStart = endIndex; } cmd = new string( inString, commandStart, commandSize ); TclList.append(interp, obj, TclString.newInstance(cmd)); TclList.append(interp, obj, TclInteger.newInstance(numWords)); for (i = 0; i < numTokens; i++) { System.Diagnostics.Debug.WriteLine("processing token " + i); token = tokenList[i]; switch (token.type) { case Parser.TCL_TOKEN_WORD: typeString = "word"; break; case Parser.TCL_TOKEN_SIMPLE_WORD: typeString = "simple"; break; case Parser.TCL_TOKEN_EXPAND_WORD: typeString = "expand"; break; case Parser.TCL_TOKEN_TEXT: typeString = "text"; break; case Parser.TCL_TOKEN_BS: typeString = "backslash"; break; case Parser.TCL_TOKEN_COMMAND: typeString = "command"; break; case Parser.TCL_TOKEN_VARIABLE: typeString = "variable"; break; default: typeString = "??"; break; } System.Diagnostics.Debug.WriteLine("typeString is " + typeString); TclList.append(interp, obj, TclString.newInstance(typeString)); TclList.append(interp, obj, TclString.newInstance(token.TokenString)); TclList.append(interp, obj, TclInteger.newInstance(token.numComponents)); } nextIndex = commandStart + commandSize; TclList.append(interp, obj, TclString.newInstance(new string( inString, nextIndex, (endIndex - nextIndex)))); } catch (TclException e) { // Do Nothing. } return(obj); }
/// <summary> This procedure is invoked to process the "encoding" 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> public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv) { if (argv.Length < 2) { throw new TclNumArgsException(interp, 1, argv, "option ?arg ...?"); } int index = TclIndex.get(interp, argv[1], validCmds, "option", 0); switch (index) { case OPT_CONVERTTO: case OPT_CONVERTFROM: { string tclEncoding; Encoding javaEncoding; TclObject data; if (argv.Length == 3) { tclEncoding = systemTclEncoding; data = argv[2]; } else if (argv.Length == 4) { tclEncoding = argv[2].ToString(); data = argv[3]; } else { throw new TclNumArgsException(interp, 2, argv, "?encoding? data"); } javaEncoding = getJavaName(tclEncoding); if ((System.Object)javaEncoding == null) { throw new TclException(interp, "unknown encoding \"" + tclEncoding + "\""); } try { if (index == OPT_CONVERTFROM) { // Treat the string as binary data byte[] bytes = TclByteArray.getBytes(interp, data); // ATK interp.setResult(System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length)); } else { // Store the result as binary data // ATK byte[] bytes = data.ToString().getBytes(javaEncoding); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data.ToString()); interp.setResult(TclByteArray.newInstance(bytes)); } } catch (IOException ex) { throw new TclRuntimeError("Encoding.cmdProc() error: " + "unsupported java encoding \"" + javaEncoding + "\""); } break; } case OPT_NAMES: { if (argv.Length > 2) { throw new TclNumArgsException(interp, 2, argv, null); } TclObject list = TclList.newInstance(); for (int i = 0; i < tclNames.Length; i++) { TclList.append(interp, list, TclString.newInstance(tclNames[i])); } interp.setResult(list); break; } case OPT_SYSTEM: { if (argv.Length > 3) { throw new TclNumArgsException(interp, 2, argv, "?encoding?"); } if (argv.Length == 2) { interp.setResult(systemTclEncoding); } else { string tclEncoding = argv[2].ToString(); Encoding javaEncoding = getJavaName(tclEncoding); if (javaEncoding == null) { throw new TclException(interp, "unknown encoding \"" + tclEncoding + "\""); } systemTclEncoding = tclEncoding; systemJavaEncoding = javaEncoding; } break; } default: { throw new TclRuntimeError("Encoding.cmdProc() error: " + "incorrect index returned from TclIndex.get()"); } } return(TCL.CompletionCode.RETURN); }
public TCL.CompletionCode cmdProc(Interp interp, TclObject[] objv) { int len; if (objv.Length < 2) { throw new TclNumArgsException(interp, 1, objv, "option [arg arg ...]"); } int opt = TclIndex.get(interp, objv[1], validCmds, "option", 0); switch (opt) { case OPT_VARIABLE: case OPT_VDELETE: if (objv.Length != 5) { if (opt == OPT_VARIABLE) { throw new TclNumArgsException(interp, 1, objv, "variable name ops command"); } else { throw new TclNumArgsException(interp, 1, objv, "vdelete name ops command"); } } TCL.VarFlag flags = 0; string ops = objv[3].ToString(); len = ops.Length; { for (int i = 0; i < len; i++) { switch (ops[i]) { case 'r': flags |= TCL.VarFlag.TRACE_READS; break; case 'w': flags |= TCL.VarFlag.TRACE_WRITES; break; case 'u': flags |= TCL.VarFlag.TRACE_UNSETS; break; default: flags = 0; goto check_ops_brk; } } } check_ops_brk: ; if (flags == 0) { throw new TclException(interp, "bad operations \"" + objv[3] + "\": should be one or more of rwu"); } if (opt == OPT_VARIABLE) { CmdTraceProc trace = new CmdTraceProc(objv[4].ToString(), flags); Var.traceVar(interp, objv[2], flags, trace); } else { // Search through all of our traces on this variable to // see if there's one with the given command. If so, then // delete the first one that matches. ArrayList traces = Var.getTraces(interp, objv[2].ToString(), 0); if (traces != null) { len = traces.Count; for (int i = 0; i < len; i++) { TraceRecord rec = (TraceRecord)traces[i]; if (rec.trace is CmdTraceProc) { CmdTraceProc proc = (CmdTraceProc)rec.trace; if (proc.flags == flags && proc.command.ToString().Equals(objv[4].ToString())) { Var.untraceVar(interp, objv[2], flags, proc); break; } } } } } break; case OPT_VINFO: if (objv.Length != 3) { throw new TclNumArgsException(interp, 2, objv, "name"); } ArrayList traces2 = Var.getTraces(interp, objv[2].ToString(), 0); if (traces2 != null) { len = traces2.Count; TclObject list = TclList.newInstance(); TclObject cmd = null; list.preserve(); try { for (int i = 0; i < len; i++) { TraceRecord rec = (TraceRecord)traces2[i]; if (rec.trace is CmdTraceProc) { CmdTraceProc proc = (CmdTraceProc)rec.trace; TCL.VarFlag mode = proc.flags; mode &= (TCL.VarFlag.TRACE_READS | TCL.VarFlag.TRACE_WRITES | TCL.VarFlag.TRACE_UNSETS); int modeInt = (int)mode; modeInt /= ((int)TCL.VarFlag.TRACE_READS); cmd = TclList.newInstance(); TclList.append(interp, cmd, opStr[modeInt]); TclList.append(interp, cmd, TclString.newInstance(proc.command)); TclList.append(interp, list, cmd); } } interp.setResult(list); } finally { list.release(); } } break; } return(TCL.CompletionCode.RETURN); }
public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv) { int i; Notifier notifier = (Notifier)interp.getNotifier(); Object info; if (assocData == null) { /* * Create the "after" information associated for this * interpreter, if it doesn't already exist. */ assocData = (AfterAssocData)interp.getAssocData("tclAfter"); if (assocData == null) { assocData = new AfterAssocData(this); interp.setAssocData("tclAfter", assocData); } } if (argv.Length < 2) { throw new TclNumArgsException(interp, 1, argv, "option ?arg arg ...?"); } /* * First lets see if the command was passed a number as the first argument. */ bool isNumber = false; int ms = 0; if (argv[1].InternalRep is TclInteger) { ms = TclInteger.get(interp, argv[1]); isNumber = true; } else { string s = argv[1].ToString(); if ((s.Length > 0) && (System.Char.IsDigit(s[0]))) { ms = TclInteger.get(interp, argv[1]); isNumber = true; } } if (isNumber) { if (ms < 0) { ms = 0; } if (argv.Length == 2) { /* * Sleep for at least the given milliseconds and return. */ long endTime = System.DateTime.Now.Ticks / 10000 + ms; while (true) { try { System.Threading.Thread.Sleep(ms); return(TCL.CompletionCode.RETURN); } catch (System.Threading.ThreadInterruptedException e) { /* * We got interrupted. Sleep again if we havn't slept * long enough yet. */ long sysTime = System.DateTime.Now.Ticks / 10000; if (sysTime >= endTime) { return(TCL.CompletionCode.RETURN); } ms = (int)(endTime - sysTime); continue; } } } TclObject cmd = getCmdObject(argv); cmd.preserve(); assocData.lastAfterId++; TimerInfo timerInfo = new TimerInfo(this, notifier, ms); timerInfo.interp = interp; timerInfo.command = cmd; timerInfo.id = assocData.lastAfterId; assocData.handlers.Add(timerInfo); interp.setResult("after#" + timerInfo.id); return(TCL.CompletionCode.RETURN); } /* * If it's not a number it must be a subcommand. */ int index; try { index = TclIndex.get(interp, argv[1], validOpts, "option", 0); } catch (TclException e) { throw new TclException(interp, "bad argument \"" + argv[1] + "\": must be cancel, idle, info, or a number"); } switch (index) { case OPT_CANCEL: if (argv.Length < 3) { throw new TclNumArgsException(interp, 2, argv, "id|command"); } TclObject arg = getCmdObject(argv); arg.preserve(); /* * Search the timer/idle handler by id or by command. */ info = null; for (i = 0; i < assocData.handlers.Count; i++) { Object obj = assocData.handlers[i]; if (obj is TimerInfo) { TclObject cmd = ((TimerInfo)obj).command; if ((cmd == arg) || cmd.ToString().Equals(arg.ToString())) { info = obj; break; } } else { TclObject cmd = ((IdleInfo)obj).command; if ((cmd == arg) || cmd.ToString().Equals(arg.ToString())) { info = obj; break; } } } if (info == null) { info = getAfterEvent(arg.ToString()); } arg.release(); /* * Cancel the handler. */ if (info != null) { if (info is TimerInfo) { ((TimerInfo)info).cancel(); ((TimerInfo)info).command.release(); } else { ((IdleInfo)info).cancel(); ((IdleInfo)info).command.release(); } SupportClass.VectorRemoveElement(assocData.handlers, info); } break; case OPT_IDLE: if (argv.Length < 3) { throw new TclNumArgsException(interp, 2, argv, "script script ..."); } TclObject cmd2 = getCmdObject(argv); cmd2.preserve(); assocData.lastAfterId++; IdleInfo idleInfo = new IdleInfo(this, notifier); idleInfo.interp = interp; idleInfo.command = cmd2; idleInfo.id = assocData.lastAfterId; assocData.handlers.Add(idleInfo); interp.setResult("after#" + idleInfo.id); break; case OPT_INFO: if (argv.Length == 2) { /* * No id is given. Return a list of current after id's. */ TclObject list = TclList.newInstance(); for (i = 0; i < assocData.handlers.Count; i++) { int id; Object obj = assocData.handlers[i]; if (obj is TimerInfo) { id = ((TimerInfo)obj).id; } else { id = ((IdleInfo)obj).id; } TclList.append(interp, list, TclString.newInstance("after#" + id)); } interp.resetResult(); interp.setResult(list); return(TCL.CompletionCode.RETURN); } if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "?id?"); } /* * Return command and type of the given after id. */ info = getAfterEvent(argv[2].ToString()); if (info == null) { throw new TclException(interp, "event \"" + argv[2] + "\" doesn't exist"); } TclObject list2 = TclList.newInstance(); TclList.append(interp, list2, ((info is TimerInfo)?((TimerInfo)info).command:((IdleInfo)info).command)); TclList.append(interp, list2, TclString.newInstance((info is TimerInfo)?"timer":"idle")); interp.resetResult(); interp.setResult(list2); break; } return(TCL.CompletionCode.RETURN); }
/// <summary> See Tcl user documentation for details.</summary> /// <exception cref=""> TclException If incorrect number of arguments. /// </exception> public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv) { if (argv.Length != 4) { throw new TclNumArgsException(interp, 1, argv, "list first last"); } int size = TclList.getLength(interp, argv[1]); int first; int last; first = Util.getIntForIndex(interp, argv[2], size - 1); last = Util.getIntForIndex(interp, argv[3], size - 1); if (last < 0) { interp.resetResult(); return(TCL.CompletionCode.RETURN); } if (first >= size) { interp.resetResult(); return(TCL.CompletionCode.RETURN); } if (first <= 0 && last >= size) { interp.setResult(argv[1]); return(TCL.CompletionCode.RETURN); } if (first < 0) { first = 0; } if (first >= size) { first = size - 1; } if (last < 0) { last = 0; } if (last >= size) { last = size - 1; } if (first > last) { interp.resetResult(); return(TCL.CompletionCode.RETURN); } TclObject list = TclList.newInstance(); list.preserve(); try { for (int i = first; i <= last; i++) { TclList.append(interp, list, TclList.index(interp, argv[1], i)); } interp.setResult(list); } finally { list.release(); } return(TCL.CompletionCode.RETURN); }
/// <summary> /// Tcl_LappendObjCmd -> LappendCmd.cmdProc /// /// This procedure is invoked to process the "lappend" Tcl command. /// See the user documentation for details on what it does. /// </summary> public TCL.CompletionCode cmdProc(Interp interp, TclObject[] objv) { TclObject varValue, newValue = null; int i;//int numElems, i, j; bool createdNewObj, createVar; if (objv.Length < 2) { throw new TclNumArgsException(interp, 1, objv, "varName ?value value ...?"); } if (objv.Length == 2) { try { newValue = interp.getVar(objv[1], 0); } catch (TclException e) { // The variable doesn't exist yet. Just create it with an empty // initial value. varValue = TclList.newInstance(); try { newValue = interp.setVar(objv[1], varValue, 0); } finally { if (newValue == null) { varValue.release(); // free unneeded object } } interp.resetResult(); return(TCL.CompletionCode.RETURN); } } else { // We have arguments to append. We used to call Tcl_SetVar2 to // append each argument one at a time to ensure that traces were run // for each append step. We now append the arguments all at once // because it's faster. Note that a read trace and a write trace for // the variable will now each only be called once. Also, if the // variable's old value is unshared we modify it directly, otherwise // we create a new copy to modify: this is "copy on write". createdNewObj = false; createVar = true; try { varValue = interp.getVar(objv[1], 0); } catch (TclException e) { // We couldn't read the old value: either the var doesn't yet // exist or it's an array element. If it's new, we will try to // create it with Tcl_ObjSetVar2 below. // FIXME : not sure we even need this parse for anything! // If we do not need to parse could we at least speed it up a bit string varName; int nameBytes; varName = objv[1].ToString(); nameBytes = varName.Length; // Number of Unicode chars in string for (i = 0; i < nameBytes; i++) { if (varName[i] == '(') { i = nameBytes - 1; if (varName[i] == ')') { // last char is ')' => array ref createVar = false; } break; } } varValue = TclList.newInstance(); createdNewObj = true; } // We only take this branch when the catch branch was not run if (createdNewObj == false && varValue.Shared) { varValue = varValue.duplicate(); createdNewObj = true; } // Insert the new elements at the end of the list. for (i = 2; i < objv.Length; i++) { TclList.append(interp, varValue, objv[i]); } // No need to call varValue.invalidateStringRep() since it // is called during the TclList.append operation. // Now store the list object back into the variable. If there is an // error setting the new value, decrement its ref count if it // was new and we didn't create the variable. try { newValue = interp.setVar(objv[1].ToString(), varValue, 0); } catch (TclException e) { if (createdNewObj && !createVar) { varValue.release(); // free unneeded obj } throw; } } // Set the interpreter's object result to refer to the variable's value // object. interp.setResult(newValue); return(TCL.CompletionCode.RETURN); }
public TCL.CompletionCode cmdProc(Interp interp, TclObject[] objv) { if (objv.Length < 2) { throw new TclNumArgsException(interp, 1, objv, "cmd ?arg ...?"); } int cmd = TclIndex.get(interp, objv[1], options, "option", 0); switch (cmd) { case OPT_ALIAS: { if (objv.Length >= 4) { Interp slaveInterp = getInterp(interp, objv[2]); if (objv.Length == 4) { InterpAliasCmd.describe(interp, slaveInterp, objv[3]); return(TCL.CompletionCode.RETURN); } if ((objv.Length == 5) && ("".Equals(objv[4].ToString()))) { InterpAliasCmd.delete(interp, slaveInterp, objv[3]); return(TCL.CompletionCode.RETURN); } if (objv.Length > 5) { Interp masterInterp = getInterp(interp, objv[4]); if ("".Equals(objv[5].ToString())) { if (objv.Length == 6) { InterpAliasCmd.delete(interp, slaveInterp, objv[3]); return(TCL.CompletionCode.RETURN); } } else { InterpAliasCmd.create(interp, slaveInterp, masterInterp, objv[3], objv[5], 6, objv); return(TCL.CompletionCode.RETURN); } } } throw new TclNumArgsException(interp, 2, objv, "slavePath slaveCmd ?masterPath masterCmd? ?args ..?"); } case OPT_ALIASES: { Interp slaveInterp = getInterp(interp, objv); InterpAliasCmd.list(interp, slaveInterp); break; } case OPT_CREATE: { // Weird historical rules: "-safe" is accepted at the end, too. bool safe = interp.isSafe; TclObject slaveNameObj = null; bool last = false; for (int i = 2; i < objv.Length; i++) { if ((!last) && (objv[i].ToString()[0] == '-')) { int index = TclIndex.get(interp, objv[i], createOptions, "option", 0); if (index == OPT_CREATE_SAFE) { safe = true; continue; } i++; last = true; } if (slaveNameObj != null) { throw new TclNumArgsException(interp, 2, objv, "?-safe? ?--? ?path?"); } slaveNameObj = objv[i]; } if (slaveNameObj == null) { // Create an anonymous interpreter -- we choose its name and // the name of the command. We check that the command name // that we use for the interpreter does not collide with an // existing command in the master interpreter. int i = 0; while (interp.getCommand("interp" + i) != null) { i++; } slaveNameObj = TclString.newInstance("interp" + i); } InterpSlaveCmd.create(interp, slaveNameObj, safe); interp.setResult(slaveNameObj); break; } case OPT_DELETE: { for (int i = 2; i < objv.Length; i++) { Interp slaveInterp = getInterp(interp, objv[i]); if (slaveInterp == interp) { throw new TclException(interp, "cannot delete the current interpreter"); } InterpSlaveCmd slave = slaveInterp.slave; slave.masterInterp.deleteCommandFromToken(slave.interpCmd); } break; } case OPT_EVAL: { if (objv.Length < 4) { throw new TclNumArgsException(interp, 2, objv, "path arg ?arg ...?"); } Interp slaveInterp = getInterp(interp, objv[2]); InterpSlaveCmd.eval(interp, slaveInterp, 3, objv); break; } case OPT_EXISTS: { bool exists = true; try { getInterp(interp, objv); } catch (TclException e) { if (objv.Length > 3) { throw; } exists = false; } interp.setResult(exists); break; } case OPT_EXPOSE: { if (objv.Length < 4 || objv.Length > 5) { throw new TclNumArgsException(interp, 2, objv, "path hiddenCmdName ?cmdName?"); } Interp slaveInterp = getInterp(interp, objv[2]); InterpSlaveCmd.expose(interp, slaveInterp, 3, objv); break; } case OPT_HIDE: { if (objv.Length < 4 || objv.Length > 5) { throw new TclNumArgsException(interp, 2, objv, "path cmdName ?hiddenCmdName?"); } Interp slaveInterp = getInterp(interp, objv[2]); InterpSlaveCmd.hide(interp, slaveInterp, 3, objv); break; } case OPT_HIDDEN: { Interp slaveInterp = getInterp(interp, objv); InterpSlaveCmd.hidden(interp, slaveInterp); break; } case OPT_ISSAFE: { Interp slaveInterp = getInterp(interp, objv); interp.setResult(slaveInterp.isSafe); break; } case OPT_INVOKEHIDDEN: { bool global = false; int i; for (i = 3; i < objv.Length; i++) { if (objv[i].ToString()[0] != '-') { break; } int index = TclIndex.get(interp, objv[i], hiddenOptions, "option", 0); if (index == OPT_HIDDEN_GLOBAL) { global = true; } else { i++; break; } } if (objv.Length - i < 1) { throw new TclNumArgsException(interp, 2, objv, "path ?-global? ?--? cmd ?arg ..?"); } Interp slaveInterp = getInterp(interp, objv[2]); InterpSlaveCmd.invokeHidden(interp, slaveInterp, global, i, objv); break; } case OPT_MARKTRUSTED: { if (objv.Length != 3) { throw new TclNumArgsException(interp, 2, objv, "path"); } Interp slaveInterp = getInterp(interp, objv[2]); InterpSlaveCmd.markTrusted(interp, slaveInterp); break; } case OPT_SLAVES: { Interp slaveInterp = getInterp(interp, objv); TclObject result = TclList.newInstance(); interp.setResult(result); IEnumerator keys = slaveInterp.slaveTable.Keys.GetEnumerator(); while (keys.MoveNext()) { string inString = (string)keys.Current; TclList.append(interp, result, TclString.newInstance(inString)); } break; } case OPT_SHARE: { if (objv.Length != 5) { throw new TclNumArgsException(interp, 2, objv, "srcPath channelId destPath"); } Interp masterInterp = getInterp(interp, objv[2]); Channel chan = TclIO.getChannel(masterInterp, objv[3].ToString()); if (chan == null) { throw new TclException(interp, "can not find channel named \"" + objv[3].ToString() + "\""); } Interp slaveInterp = getInterp(interp, objv[4]); TclIO.registerChannel(slaveInterp, chan); break; } case OPT_TARGET: { if (objv.Length != 4) { throw new TclNumArgsException(interp, 2, objv, "path alias"); } Interp slaveInterp = getInterp(interp, objv[2]); string aliasName = objv[3].ToString(); Interp targetInterp = InterpAliasCmd.getTargetInterp(slaveInterp, aliasName); if (targetInterp == null) { throw new TclException(interp, "alias \"" + aliasName + "\" in path \"" + objv[2].ToString() + "\" not found"); } if (!getInterpPath(interp, targetInterp)) { throw new TclException(interp, "target interpreter for alias \"" + aliasName + "\" in path \"" + objv[2].ToString() + "\" is not my descendant"); } break; } case OPT_TRANSFER: { if (objv.Length != 5) { throw new TclNumArgsException(interp, 2, objv, "srcPath channelId destPath"); } Interp masterInterp = getInterp(interp, objv[2]); Channel chan = TclIO.getChannel(masterInterp, objv[3].ToString()); if (chan == null) { throw new TclException(interp, "can not find channel named \"" + objv[3].ToString() + "\""); } Interp slaveInterp = getInterp(interp, objv[4]); TclIO.registerChannel(slaveInterp, chan); TclIO.unregisterChannel(masterInterp, chan); break; } } return(TCL.CompletionCode.RETURN); }
public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv) { if (argv.Length < 2) { throw new TclNumArgsException(interp, 1, argv, "option ?arg ...?"); } int opt = TclIndex.get(interp, argv[1], validCmds, "option", 0); string path; System.IO.FileInfo fileObj = null; switch (opt) { case OPT_ATIME: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } // FIXME: Currently returns the same thing as MTIME. // Java does not support retrieval of access time. fileObj = FileUtil.getNewFileObj(interp, argv[2].ToString()); interp.setResult(getMtime(interp, argv[2].ToString(), fileObj)); return(TCL.CompletionCode.RETURN); case OPT_ATTRIBUTES: if (argv[3].ToString() == "-readonly") { fileSetReadOnly(interp, argv); } else { throw new TclException(interp, "sorry, \"file attributes\" is not implemented yet"); } return(TCL.CompletionCode.RETURN); case OPT_CHANNELS: throw new TclException(interp, "sorry, \"file channels\" is not implemented yet"); case OPT_COPY: fileCopyRename(interp, argv, true); return(TCL.CompletionCode.RETURN); case OPT_DELETE: fileDelete(interp, argv); return(TCL.CompletionCode.RETURN); case OPT_DIRNAME: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } path = argv[2].ToString(); // Return all but the last component. If there is only one // component, return it if the path was non-relative, otherwise // return the current directory. TclObject[] splitArrayObj = TclList.getElements(interp, FileUtil.splitAndTranslate(interp, path)); if (splitArrayObj.Length > 1) { interp.setResult(FileUtil.joinPath(interp, splitArrayObj, 0, splitArrayObj.Length - 1)); } else if ((splitArrayObj.Length == 0) || (FileUtil.getPathType(path) == FileUtil.PATH_RELATIVE)) { if (JACL.PLATFORM == JACL.PLATFORM_MAC) { interp.setResult(":"); } else { interp.setResult("."); } } else { interp.setResult(splitArrayObj[0].ToString()); } return(TCL.CompletionCode.RETURN); case OPT_EXECUTABLE: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } bool isExe = false; fileObj = FileUtil.getNewFileObj(interp, argv[2].ToString()); // A file must exist to be executable. Directories are always // executable. bool tmpBool; if (System.IO.File.Exists(fileObj.FullName)) { tmpBool = true; } else { tmpBool = System.IO.Directory.Exists(fileObj.FullName); } if (tmpBool) { isExe = System.IO.Directory.Exists(fileObj.FullName); if (isExe) { interp.setResult(isExe); return(TCL.CompletionCode.RETURN); } if (Util.Windows) { // File that ends with .exe, .com, or .bat is executable. string fileName = argv[2].ToString(); isExe = (fileName.EndsWith(".exe") || fileName.EndsWith(".com") || fileName.EndsWith(".bat")); } else if (Util.Mac) { // FIXME: Not yet implemented on Mac. For now, return true. // Java does not support executability checking. isExe = true; } else { // FIXME: Not yet implemented on Unix. For now, return true. // Java does not support executability checking. isExe = true; } } interp.setResult(isExe); return(TCL.CompletionCode.RETURN); case OPT_EXISTS: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } fileObj = FileUtil.getNewFileObj(interp, argv[2].ToString()); bool tmpBool2; if (System.IO.File.Exists(fileObj.FullName)) { tmpBool2 = true; } else { tmpBool2 = System.IO.Directory.Exists(fileObj.FullName); } interp.setResult(tmpBool2); return(TCL.CompletionCode.RETURN); case OPT_EXTENSION: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } interp.setResult(getExtension(argv[2].ToString())); return(TCL.CompletionCode.RETURN); case OPT_ISDIRECTORY: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } fileObj = FileUtil.getNewFileObj(interp, argv[2].ToString()); interp.setResult(System.IO.Directory.Exists(fileObj.FullName)); return(TCL.CompletionCode.RETURN); case OPT_ISFILE: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } fileObj = FileUtil.getNewFileObj(interp, argv[2].ToString()); interp.setResult(System.IO.File.Exists(fileObj.FullName)); return(TCL.CompletionCode.RETURN); case OPT_JOIN: if (argv.Length < 3) { throw new TclNumArgsException(interp, 2, argv, "name ?name ...?"); } interp.setResult(FileUtil.joinPath(interp, argv, 2, argv.Length)); return(TCL.CompletionCode.RETURN); case OPT_LINK: throw new TclException(interp, "sorry, \"file link\" is not implemented yet"); case OPT_LSTAT: if (argv.Length != 4) { throw new TclNumArgsException(interp, 2, argv, "name varName"); } // FIXME: Not yet implemented. // Java does not support link access. throw new TclException(interp, "file command with opt " + argv[1].ToString() + " is not yet implemented"); case OPT_MTIME: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } fileObj = FileUtil.getNewFileObj(interp, argv[2].ToString()); interp.setResult(getMtime(interp, argv[2].ToString(), fileObj)); return(TCL.CompletionCode.RETURN); case OPT_MKDIR: fileMakeDirs(interp, argv); return(TCL.CompletionCode.RETURN); case OPT_NATIVENAME: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } interp.setResult(FileUtil.translateFileName(interp, argv[2].ToString())); return(TCL.CompletionCode.RETURN); case OPT_NORMALIZE: throw new TclException(interp, "sorry, \"file normalize\" is not implemented yet"); case OPT_OWNED: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } fileObj = FileUtil.getNewFileObj(interp, argv[2].ToString()); interp.setResult(isOwner(interp, fileObj)); return(TCL.CompletionCode.RETURN); case OPT_PATHTYPE: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } switch (FileUtil.getPathType(argv[2].ToString())) { case FileUtil.PATH_RELATIVE: interp.setResult("relative"); return(TCL.CompletionCode.RETURN); case FileUtil.PATH_VOLUME_RELATIVE: interp.setResult("volumerelative"); return(TCL.CompletionCode.RETURN); case FileUtil.PATH_ABSOLUTE: interp.setResult("absolute"); break; } return(TCL.CompletionCode.RETURN); case OPT_READABLE: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } fileObj = FileUtil.getNewFileObj(interp, argv[2].ToString()); // interp.setResult(fileObj.canRead()); // HACK interp.setResult(true); return(TCL.CompletionCode.RETURN); case OPT_READLINK: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } // FIXME: Not yet implemented. // Java does not support link access. throw new TclException(interp, "file command with opt " + argv[1].ToString() + " is not yet implemented"); case OPT_RENAME: fileCopyRename(interp, argv, false); return(TCL.CompletionCode.RETURN); case OPT_ROOTNAME: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } string fileName2 = argv[2].ToString(); string extension = getExtension(fileName2); int diffLength = fileName2.Length - extension.Length; interp.setResult(fileName2.Substring(0, (diffLength) - (0))); return(TCL.CompletionCode.RETURN); case OPT_SEPARATOR: throw new TclException(interp, "sorry, \"file separator\" is not implemented yet"); case OPT_SIZE: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } fileObj = FileUtil.getNewFileObj(interp, argv[2].ToString()); bool tmpBool3; if (System.IO.File.Exists(fileObj.FullName)) { tmpBool3 = true; } else { tmpBool3 = System.IO.Directory.Exists(fileObj.FullName); } if (!tmpBool3) { throw new TclPosixException(interp, TclPosixException.ENOENT, true, "could not read \"" + argv[2].ToString() + "\""); } interp.setResult((int)SupportClass.FileLength(fileObj)); return(TCL.CompletionCode.RETURN); case OPT_SPLIT: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } interp.setResult(FileUtil.splitPath(interp, argv[2].ToString())); return(TCL.CompletionCode.RETURN); case OPT_STAT: if (argv.Length != 4) { throw new TclNumArgsException(interp, 2, argv, "name varName"); } getAndStoreStatData(interp, argv[2].ToString(), argv[3].ToString()); return(TCL.CompletionCode.RETURN); case OPT_SYSTEM: throw new TclException(interp, "sorry, \"file system\" is not implemented yet"); case OPT_TAIL: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } interp.setResult(getTail(interp, argv[2].ToString())); return(TCL.CompletionCode.RETURN); case OPT_TYPE: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } fileObj = FileUtil.getNewFileObj(interp, argv[2].ToString()); interp.setResult(getType(interp, argv[2].ToString(), fileObj)); return(TCL.CompletionCode.RETURN); case OPT_VOLUMES: if (argv.Length != 2) { throw new TclNumArgsException(interp, 2, argv, null); } // use Java 1.2's File.listRoots() method if available if (listRootsMethod == null) { throw new TclException(interp, "\"file volumes\" is not supported"); } try { System.IO.FileInfo[] roots = (System.IO.FileInfo[])listRootsMethod.Invoke(null, (System.Object[]) new System.Object[0]); if (roots != null) { TclObject list = TclList.newInstance(); for (int i = 0; i < roots.Length; i++) { string root = roots[i].FullName; TclList.append(interp, list, TclString.newInstance(root)); } interp.setResult(list); } } catch (System.UnauthorizedAccessException ex) { throw new TclRuntimeError("IllegalAccessException in volumes cmd"); } catch (System.ArgumentException ex) { throw new TclRuntimeError("IllegalArgumentException in volumes cmd"); } catch (System.Reflection.TargetInvocationException ex) { System.Exception t = ex.GetBaseException(); if (t is System.ApplicationException) { throw (System.ApplicationException)t; } else { throw new TclRuntimeError("unexected exception in volumes cmd"); } } return(TCL.CompletionCode.RETURN); case OPT_WRITABLE: if (argv.Length != 3) { throw new TclNumArgsException(interp, 2, argv, "name"); } fileObj = FileUtil.getNewFileObj(interp, argv[2].ToString()); interp.setResult(SupportClass.FileCanWrite(fileObj)); return(TCL.CompletionCode.RETURN); default: throw new TclRuntimeError("file command with opt " + argv[1].ToString() + " is not implemented"); } }
/// <summary> This procedure is invoked to process the "array" Tcl command. /// See the user documentation for details on what it does. /// </summary> public TCL.CompletionCode cmdProc(Interp interp, TclObject[] objv) { Var var = null, array = null; bool notArray = false; string varName, msg; int index;//, result; if (objv.Length < 3) { throw new TclNumArgsException(interp, 1, objv, "option arrayName ?arg ...?"); } index = TclIndex.get(interp, objv[1], validCmds, "option", 0); // Locate the array variable (and it better be an array). varName = objv[2].ToString(); Var[] retArray = Var.lookupVar(interp, varName, null, 0, null, false, false); // Assign the values returned in the array if (retArray != null) { var = retArray[0]; array = retArray[1]; } if ((var == null) || !var.isVarArray() || var.isVarUndefined()) { notArray = true; } // Special array trace used to keep the env array in sync for // array names, array get, etc. if (var != null && var.traces != null) { msg = Var.callTraces(interp, array, var, varName, null, (TCL.VarFlag.LEAVE_ERR_MSG | TCL.VarFlag.NAMESPACE_ONLY | TCL.VarFlag.GLOBAL_ONLY | TCL.VarFlag.TRACE_ARRAY)); if ((System.Object)msg != null) { throw new TclVarException(interp, varName, null, "trace array", msg); } } switch (index) { case OPT_ANYMORE: { if (objv.Length != 4) { throw new TclNumArgsException(interp, 2, objv, "arrayName searchId"); } if (notArray) { errorNotArray(interp, objv[2].ToString()); } if (var.sidVec == null) { errorIllegalSearchId(interp, objv[2].ToString(), objv[3].ToString()); } SearchId e = var.getSearch(objv[3].ToString()); if (e == null) { errorIllegalSearchId(interp, objv[2].ToString(), objv[3].ToString()); } if (e.HasMore) { interp.setResult("1"); } else { interp.setResult("0"); } break; } case OPT_DONESEARCH: { if (objv.Length != 4) { throw new TclNumArgsException(interp, 2, objv, "arrayName searchId"); } if (notArray) { errorNotArray(interp, objv[2].ToString()); } bool rmOK = true; if (var.sidVec != null) { rmOK = (var.removeSearch(objv[3].ToString())); } if ((var.sidVec == null) || !rmOK) { errorIllegalSearchId(interp, objv[2].ToString(), objv[3].ToString()); } break; } case OPT_EXISTS: { if (objv.Length != 3) { throw new TclNumArgsException(interp, 2, objv, "arrayName"); } interp.setResult(!notArray); break; } case OPT_GET: { // Due to the differences in the hashtable implementation // from the Tcl core and Java, the output will be rearranged. // This is not a negative side effect, however, test results // will differ. if ((objv.Length != 3) && (objv.Length != 4)) { throw new TclNumArgsException(interp, 2, objv, "arrayName ?pattern?"); } if (notArray) { return(TCL.CompletionCode.RETURN); } string pattern = null; if (objv.Length == 4) { pattern = objv[3].ToString(); } Hashtable table = (Hashtable)var.value; TclObject tobj = TclList.newInstance(); string arrayName = objv[2].ToString(); string key, strValue; Var var2; // Go through each key in the hash table. If there is a // pattern, test for a match. Each valid key and its value // is written into sbuf, which is returned. // FIXME : do we need to port over the 8.1 code for this loop? for (IDictionaryEnumerator e = table.GetEnumerator(); e.MoveNext();) { key = ((string)e.Key); var2 = (Var)e.Value; if (var2.isVarUndefined()) { continue; } if ((System.Object)pattern != null && !Util.stringMatch(key, pattern)) { continue; } strValue = interp.getVar(arrayName, key, 0).ToString(); TclList.append(interp, tobj, TclString.newInstance(key)); TclList.append(interp, tobj, TclString.newInstance(strValue)); } interp.setResult(tobj); break; } case OPT_NAMES: { if ((objv.Length != 3) && (objv.Length != 4)) { throw new TclNumArgsException(interp, 2, objv, "arrayName ?pattern?"); } if (notArray) { return(TCL.CompletionCode.RETURN); } string pattern = null; if (objv.Length == 4) { pattern = objv[3].ToString(); } Hashtable table = (Hashtable)var.value; TclObject tobj = TclList.newInstance(); string key; // Go through each key in the hash table. If there is a // pattern, test for a match. Each valid key and its value // is written into sbuf, which is returned. for (IDictionaryEnumerator e = table.GetEnumerator(); e.MoveNext();) { key = (string)e.Key; Var elem = (Var)e.Value; if (!elem.isVarUndefined()) { if ((System.Object)pattern != null) { if (!Util.stringMatch(key, pattern)) { continue; } } TclList.append(interp, tobj, TclString.newInstance(key)); } } interp.setResult(tobj); break; } case OPT_NEXTELEMENT: { if (objv.Length != 4) { throw new TclNumArgsException(interp, 2, objv, "arrayName searchId"); } if (notArray) { errorNotArray(interp, objv[2].ToString()); } if (var.sidVec == null) { errorIllegalSearchId(interp, objv[2].ToString(), objv[3].ToString()); } SearchId e = var.getSearch(objv[3].ToString()); if (e == null) { errorIllegalSearchId(interp, objv[2].ToString(), objv[3].ToString()); } if (e.HasMore) { Hashtable table = (Hashtable)var.value; DictionaryEntry entry = e.nextEntry(); string key = (string)entry.Key; Var elem = (Var)entry.Value; if ((elem.flags & VarFlags.UNDEFINED) == 0) { interp.setResult(key); } else { interp.setResult(""); } } break; } case OPT_SET: { if (objv.Length != 4) { throw new TclNumArgsException(interp, 2, objv, "arrayName list"); } int size = TclList.getLength(interp, objv[3]); if (size % 2 != 0) { throw new TclException(interp, "list must have an even number of elements"); } int i; string name1 = objv[2].ToString(); string name2, strValue; // Set each of the array variable names in the interp for (i = 0; i < size; i++) { name2 = TclList.index(interp, objv[3], i++).ToString(); strValue = TclList.index(interp, objv[3], i).ToString(); interp.setVar(name1, name2, TclString.newInstance(strValue), 0); } break; } case OPT_SIZE: { if (objv.Length != 3) { throw new TclNumArgsException(interp, 2, objv, "arrayName"); } if (notArray) { interp.setResult(0); } else { Hashtable table = (Hashtable)var.value; int size = 0; for (IDictionaryEnumerator e = table.GetEnumerator(); e.MoveNext();) { Var elem = (Var)e.Value; if ((elem.flags & VarFlags.UNDEFINED) == 0) { size++; } } interp.setResult(size); } break; } case OPT_STARTSEARCH: { if (objv.Length != 3) { throw new TclNumArgsException(interp, 2, objv, "arrayName"); } if (notArray) { errorNotArray(interp, objv[2].ToString()); } if (var.sidVec == null) { var.sidVec = new ArrayList(10); } // Create a SearchId Object: // To create a new SearchId object, a unique string // identifier needs to be composed and we need to // create an Enumeration of the array keys. The // unique string identifier is created from three // strings: // // "s-" is the default prefix // "i" is a unique number that is 1+ the greatest // SearchId index currently on the ArrayVar. // "name" is the name of the array // // Once the SearchId string is created we construct a // new SearchId object using the string and the // Enumeration. From now on the string is used to // uniquely identify the SearchId object. int i = var.NextIndex; string s = "s-" + i + "-" + objv[2].ToString(); IDictionaryEnumerator e = ((Hashtable)var.value).GetEnumerator(); var.sidVec.Add(new SearchId(e, s, i)); interp.setResult(s); break; } case OPT_UNSET: { string pattern; string name; if ((objv.Length != 3) && (objv.Length != 4)) { throw new TclNumArgsException(interp, 2, objv, "arrayName ?pattern?"); } if (notArray) { //Ignot this error -- errorNotArray(interp, objv[2].ToString()); break; } if (objv.Length == 3) { // When no pattern is given, just unset the whole array interp.unsetVar(objv[2], 0); } else { pattern = objv[3].ToString(); Hashtable table = (Hashtable)(((Hashtable)var.value).Clone()); for (IDictionaryEnumerator e = table.GetEnumerator(); e.MoveNext();) { name = (string)e.Key; Var elem = (Var)e.Value; if (var.isVarUndefined()) { continue; } if (Util.stringMatch(name, pattern)) { interp.unsetVar(varName, name, 0); } } } break; } } return(TCL.CompletionCode.RETURN); }