internal Procedure(Interp interp, NamespaceCmd.Namespace ns, string name, TclObject args, TclObject b, string sFileName, int sLineNumber) { this.NS = ns; srcFileName = sFileName; srcLineNumber = sLineNumber; // Break up the argument list into argument specifiers, then process // each argument specifier. int numArgs = TclList.getLength(interp, args); ArgList = new TclObject[numArgs][]; for (int i = 0; i < numArgs; i++) { ArgList[i] = new TclObject[2]; } for (int i = 0; i < numArgs; i++) { // Now divide the specifier up into name and default. TclObject argSpec = TclList.index(interp, args, i); int specLen = TclList.getLength(interp, argSpec); if (specLen == 0) { throw new TclException(interp, "procedure \"" + name + "\" has argument with no name"); } if (specLen > 2) { throw new TclException(interp, "too many fields in argument " + "specifier \"" + argSpec + "\""); } ArgList[i][0] = TclList.index(interp, argSpec, 0); ArgList[i][0].Preserve(); if (specLen == 2) { ArgList[i][1] = TclList.index(interp, argSpec, 1); ArgList[i][1].Preserve(); } else { ArgList[i][1] = null; } } if (numArgs > 0 && (ArgList[numArgs - 1][0].ToString().Equals("args"))) { isVarArgs = true; } else { isVarArgs = false; } body = new CharPointer(b.ToString()); body_length = body.Length(); }
public static void Tcl_ListObjIndex(Interp interp, TclObject to, int nItem, out TclObject elmObj) { try { elmObj = TclList.index(interp, to, nItem); } catch { elmObj = null; } }
internal static TclObject splitAndTranslate(Interp interp, string path) { TclObject splitResult = splitPath(interp, path); int len = TclList.getLength(interp, splitResult); if (len == 1) { string fileName = TclList.index(interp, splitResult, 0).ToString(); if (fileName[0] == '~') { string user = translateFileName(interp, fileName); splitResult = splitPath(interp, user); } } return(splitResult); }
/// <summary> See Tcl user documentation for details.</summary> public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv) { string sep = null; if (argv.Length == 2) { sep = null; } else if (argv.Length == 3) { sep = argv[2].ToString(); } else { throw new TclNumArgsException(interp, 1, argv, "list ?joinString?"); } TclObject list = argv[1]; int size = TclList.getLength(interp, list); if (size == 0) { interp.ResetResult(); return(TCL.CompletionCode.RETURN); } StringBuilder sbuf = new StringBuilder(TclList.index(interp, list, 0).ToString()); for (int i = 1; i < size; i++) { if ((System.Object)sep == null) { sbuf.Append(' '); } else { sbuf.Append(sep); } sbuf.Append(TclList.index(interp, list, i).ToString()); } interp.SetResult(sbuf.ToString()); return(TCL.CompletionCode.RETURN); }
private static string getTail(Interp interp, string path) { // Split the path and return the string form of the last component, // unless there is only one component which is the root or an absolute // path. TclObject splitResult = FileUtil.splitAndTranslate(interp, path); int last = TclList.getLength(interp, splitResult) - 1; if (last >= 0) { if ((last > 0) || (FileUtil.getPathType(path) == FileUtil.PATH_RELATIVE)) { TclObject tailObj = TclList.index(interp, splitResult, last); return(tailObj.ToString()); } } return(""); }
public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv) { if (argv.Length < 3) { throw new TclNumArgsException(interp, 1, argv, "list index"); } int size = TclList.getLength(interp, argv[1]); int index = Util.getIntForIndex(interp, argv[2], size - 1); TclObject element = TclList.index(interp, argv[1], index); if (element != null) { interp.SetResult(element); } else { interp.ResetResult(); } return(TCL.CompletionCode.RETURN); }
/// <summary> Compares the order of two items in the array. /// /// </summary> /// <param name="obj1">first item. /// </param> /// <param name="obj2">second item. /// </param> /// <returns> 0 if they are equal, 1 if obj1 > obj2, -1 otherwise. /// /// </returns> /// <exception cref=""> TclException if an error occurs during sorting. /// </exception> private int compare(TclObject obj1, TclObject obj2) { int index; int code = 0; if (sortIndex != -1) { // The "-index" option was specified. Treat each object as a // list, extract the requested element from each list, and // compare the elements, not the lists. The special index "end" // is signaled here with a negative index (other than -1). TclObject obj; if (sortIndex < -1) { index = TclList.getLength(sortInterp, obj1) - 1; } else { index = sortIndex; } obj = TclList.index(sortInterp, obj1, index); if (obj == null) { throw new TclException(sortInterp, "element " + index + " missing from sublist \"" + obj1 + "\""); } obj1 = obj; if (sortIndex < -1) { index = TclList.getLength(sortInterp, obj2) - 1; } else { index = sortIndex; } obj = TclList.index(sortInterp, obj2, index); if (obj == null) { throw new TclException(sortInterp, "element " + index + " missing from sublist \"" + obj2 + "\""); } obj2 = obj; } switch (sortMode) { case ASCII: // ATK C# CompareTo use option // similar to -dictionary but a > A code = System.Globalization.CultureInfo.InvariantCulture.CompareInfo.Compare(obj1.ToString(), obj2.ToString(), System.Globalization.CompareOptions.Ordinal); // code = obj1.ToString().CompareTo(obj2.ToString()); break; case DICTIONARY: code = doDictionary(obj1.ToString(), obj2.ToString()); break; case INTEGER: try { int int1 = TclInteger.Get(sortInterp, obj1); int int2 = TclInteger.Get(sortInterp, obj2); if (int1 > int2) { code = 1; } else if (int2 > int1) { code = -1; } } catch (TclException e1) { sortInterp.AddErrorInfo("\n (converting list element from string to integer)"); throw e1; } break; case REAL: try { double f1 = TclDouble.Get(sortInterp, obj1); double f2 = TclDouble.Get(sortInterp, obj2); if (f1 > f2) { code = 1; } else if (f2 > f1) { code = -1; } } catch (TclException e2) { sortInterp.AddErrorInfo("\n (converting list element from string to real)"); throw e2; } break; case COMMAND: StringBuilder sbuf = new StringBuilder(sortCommand); Util.appendElement(sortInterp, sbuf, obj1.ToString()); Util.appendElement(sortInterp, sbuf, obj2.ToString()); try { sortInterp.Eval(sbuf.ToString(), 0); } catch (TclException e3) { sortInterp.AddErrorInfo("\n (user-defined comparison command)"); throw e3; } try { code = TclInteger.Get(sortInterp, sortInterp.GetResult()); } catch (TclException e) { sortInterp.ResetResult(); TclException e4 = new TclException(sortInterp, "comparison command returned non-numeric result"); throw e4; } break; default: throw new TclRuntimeError("Unknown sortMode " + sortMode); } if (sortIncreasing) { return(code); } else { return(-code); } }
/// <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> /// 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; 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 ((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 ((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(string.Empty); } } 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"); } string name1 = objv[2].ToString(); string name2, strValue; // Set each of the array variable names in the interp for (int 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); }
/// <summary> Executes a "case" statement. See Tcl user /// documentation for details. /// /// </summary> /// <param name="interp">the current interpreter. /// </param> /// <param name="argv">command arguments. /// </param> /// <exception cref=""> TclException If incorrect number of arguments. /// </exception> public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv) { if (argv.Length < 3) { throw new TclNumArgsException(interp, 1, argv, "string ?in? patList body ... ?default body?"); } int i; int body; TclObject[] caseArgv; string inString; inString = argv[1].ToString(); caseArgv = argv; body = -1; if (argv[2].ToString().Equals("in")) { i = 3; } else { i = 2; } /* * If all of the pattern/command pairs are lumped into a single * argument, split them out again. */ if (argv.Length - i == 1) { caseArgv = TclList.getElements(interp, argv[i]); i = 0; } { for (; i < caseArgv.Length; i += 2) { int j; if (i == (caseArgv.Length - 1)) { throw new TclException(interp, "extra case pattern with no body"); } /* * Check for special case of single pattern (no list) with * no backslash sequences. */ string caseString = caseArgv[i].ToString(); int len = caseString.Length; for (j = 0; j < len; j++) { char c = caseString[j]; if (System.Char.IsWhiteSpace(c) || (c == '\\')) { break; } } if (j == len) { if (caseString.Equals("default")) { body = i + 1; } if (Util.StringMatch(inString, caseString)) { body = i + 1; goto match_loop_brk; } continue; } /* * Break up pattern lists, then check each of the patterns * in the list. */ int numPats = TclList.getLength(interp, caseArgv[i]); for (j = 0; j < numPats; j++) { if (Util.StringMatch(inString, TclList.index(interp, caseArgv[i], j).ToString())) { body = i + 1; goto match_loop_brk; } } } } match_loop_brk: ; if (body != -1) { try { interp.Eval(caseArgv[body], 0); } catch (TclException e) { if (e.GetCompletionCode() == TCL.CompletionCode.ERROR) { interp.AddErrorInfo("\n (\"" + caseArgv[body - 1] + "\" arm line " + interp._errorLine + ")"); } throw; } } 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); }
/// <summary> Tcl_ForeachObjCmd -> ForeachCmd.cmdProc /// /// This procedure is invoked to process the "foreach" Tcl command. /// See the user documentation for details on what it does. /// /// </summary> /// <param name="interp">the current interpreter. /// </param> /// <param name="objv">command arguments. /// </param> /// <exception cref=""> TclException if script causes error. /// </exception> public TCL.CompletionCode CmdProc(Interp interp, TclObject[] objv) { if (objv.Length < 4 || (objv.Length % 2) != 0) { throw new TclNumArgsException(interp, 1, objv, "varList list ?varList list ...? command"); } // foreach {n1 n2} {1 2 3 4} {n3} {1 2} {puts $n1-$n2-$n3} // name[0] = {n1 n2} value[0] = {1 2 3 4} // name[1] = {n3} value[0] = {1 2} TclObject[] name = new TclObject[(objv.Length - 2) / 2]; TclObject[] value = new TclObject[(objv.Length - 2) / 2]; int c, i, j, base_; int maxIter = 0; TclObject command = objv[objv.Length - 1]; bool done = false; for (i = 0; i < objv.Length - 2; i += 2) { int x = i / 2; name[x] = objv[i + 1]; value[x] = objv[i + 2]; int nSize = TclList.getLength(interp, name[x]); int vSize = TclList.getLength(interp, value[x]); if (nSize == 0) { throw new TclException(interp, "foreach varlist is empty"); } int iter = (vSize + nSize - 1) / nSize; if (maxIter < iter) { maxIter = iter; } } for (c = 0; !done && c < maxIter; c++) { // Set up the variables for (i = 0; i < objv.Length - 2; i += 2) { int x = i / 2; int nSize = TclList.getLength(interp, name[x]); base_ = nSize * c; for (j = 0; j < nSize; j++) { // Test and see if the name variable is an array. Var[] result = Var.LookupVar(interp, name[x].ToString(), null, 0, null, false, false); Var var = null; if (result != null) { if (result[1] != null) { var = result[1]; } else { var = result[0]; } } try { if (base_ + j >= TclList.getLength(interp, value[x])) { interp.SetVar(TclList.index(interp, name[x], j), TclString.NewInstance(""), 0); } else { interp.SetVar(TclList.index(interp, name[x], j), TclList.index(interp, value[x], base_ + j), 0); } } catch (TclException e) { throw new TclException(interp, "couldn't set loop variable: \"" + TclList.index(interp, name[x], j) + "\""); } } } // Execute the script try { interp.Eval(command, 0); } catch (TclException e) { switch (e.GetCompletionCode()) { case TCL.CompletionCode.BREAK: done = true; break; case TCL.CompletionCode.CONTINUE: continue; case TCL.CompletionCode.ERROR: interp.AddErrorInfo("\n (\"foreach\" body line " + interp._errorLine + ")"); throw; default: throw; } } } interp.ResetResult(); return(TCL.CompletionCode.RETURN); }
/// <summary> This procedure is invoked to process the "open" 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) { bool pipeline = false; /* True if opening pipeline chan */ int prot = 438; /* Final rdwr permissions of file */ int modeFlags = TclIO.RDONLY; /* Rdwr mode for the file. See the * TclIO class for more info on the * valid modes */ if ((argv.Length < 2) || (argv.Length > 4)) { throw new TclNumArgsException(interp, 1, argv, "fileName ?access? ?permissions?"); } if (argv.Length > 2) { TclObject mode = argv[2]; string modeStr = mode.ToString(); int len = modeStr.Length; // This "r+1" hack is just to get a test case to pass if ((len == 0) || (modeStr.StartsWith("r+") && len >= 3)) { throw new TclException(interp, "illegal access mode \"" + modeStr + "\""); } if (len < 3) { switch (modeStr[0]) { case 'r': { if (len == 1) { modeFlags = TclIO.RDONLY; break; } else if (modeStr[1] == '+') { modeFlags = TclIO.RDWR; break; } } goto case 'w'; case 'w': { FileInfo f = FileUtil.getNewFileObj(interp, argv[1].ToString()); bool tmpBool; if (File.Exists(f.FullName)) { tmpBool = true; } else { tmpBool = Directory.Exists(f.FullName); } if (tmpBool) { bool tmpBool2; try { if (File.Exists(f.FullName)) { File.SetAttributes(f.FullName, FileAttributes.Normal); File.Delete(f.FullName); tmpBool2 = true; } else if (Directory.Exists(f.FullName)) { Directory.Delete(f.FullName); tmpBool2 = true; } else { tmpBool2 = false; } } // ATK added because .NET do not allow often to delete // files used by another process catch (IOException e) { throw new TclException(interp, "cannot open file: " + argv[1].ToString()); } bool generatedAux = tmpBool2; } if (len == 1) { modeFlags = (TclIO.WRONLY | TclIO.CREAT); break; } else if (modeStr[1] == '+') { modeFlags = (TclIO.RDWR | TclIO.CREAT); break; } } goto case 'a'; case 'a': { if (len == 1) { modeFlags = (TclIO.WRONLY | TclIO.APPEND); break; } else if (modeStr[1] == '+') { modeFlags = (TclIO.RDWR | TclIO.CREAT | TclIO.APPEND); break; } } goto default; default: { throw new TclException(interp, "illegal access mode \"" + modeStr + "\""); } } } else { modeFlags = 0; bool gotRorWflag = false; int mlen = TclList.getLength(interp, mode); for (int i = 0; i < mlen; i++) { TclObject marg = TclList.index(interp, mode, i); if (marg.ToString().Equals("RDONLY")) { modeFlags |= TclIO.RDONLY; gotRorWflag = true; } else { if (marg.ToString().Equals("WRONLY")) { modeFlags |= TclIO.WRONLY; gotRorWflag = true; } else { if (marg.ToString().Equals("RDWR")) { modeFlags |= TclIO.RDWR; gotRorWflag = true; } else { if (marg.ToString().Equals("APPEND")) { modeFlags |= TclIO.APPEND; } else { if (marg.ToString().Equals("CREAT")) { modeFlags |= TclIO.CREAT; } else { if (marg.ToString().Equals("EXCL")) { modeFlags |= TclIO.EXCL; } else { if (marg.ToString().Equals("TRUNC")) { modeFlags |= TclIO.TRUNC; } else { throw new TclException(interp, "invalid access mode \"" + marg.ToString() + "\": must be RDONLY, WRONLY, RDWR, APPEND, " + "CREAT EXCL, NOCTTY, NONBLOCK, or TRUNC"); } } } } } } } } if (!gotRorWflag) { throw new TclException(interp, "access mode must include either RDONLY, WRONLY, or RDWR"); } } } if (argv.Length == 4) { prot = TclInteger.Get(interp, argv[3]); throw new TclException(interp, "setting permissions not implemented yet"); } if ((argv[1].ToString().Length > 0) && (argv[1].ToString()[0] == '|')) { pipeline = true; throw new TclException(interp, "pipes not implemented yet"); } /* * Open the file or create a process pipeline. */ if (!pipeline) { try { FileChannel file = new FileChannel(); file.open(interp, argv[1].ToString(), modeFlags); TclIO.registerChannel(interp, file); interp.SetResult(file.ChanName); } catch (IOException e) { throw new TclException(interp, "cannot open file: " + argv[1].ToString()); } } else { /* * Pipeline code here... */ } return(TCL.CompletionCode.RETURN); }