/* *---------------------------------------------------------------------- * * 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 ArrayList Errors = new ArrayList(10); // A list of the pending background error handlers. internal BgErrorMgr(Interp i) { Interp = i; BgerrorCmdObj = TclString.NewInstance("bgerror"); BgerrorCmdObj.Preserve(); Errors = new ArrayList(10); }
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; }
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)); } } }
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.Ext_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); }; }
/// <summary> This procedure is invoked to process the "gets" 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 writeToVar = false; // If true write to var passes as arg string varName = ""; // The variable to write value to Channel chan; // The channel being operated on int lineLen; TclObject line; if ((argv.Length < 2) || (argv.Length > 3)) { throw new TclNumArgsException(interp, 1, argv, "channelId ?varName?"); } if (argv.Length == 3) { writeToVar = true; varName = argv[2].ToString(); } chan = TclIO.getChannel(interp, argv[1].ToString()); if (chan == null) { throw new TclException(interp, "can not find channel named \"" + argv[1].ToString() + "\""); } try { line = TclString.NewInstance(new StringBuilder(64)); lineLen = chan.read(interp, line, TclIO.READ_LINE, 0); if (lineLen < 0) { // FIXME: Need more specific posix error codes! if (!chan.eof() && !chan.isBlocked(interp)) { throw new TclPosixException(interp, TclPosixException.EIO, true, "error reading \"" + argv[1].ToString() + "\""); } lineLen = -1; } if (writeToVar) { interp.SetVar(varName, line, 0); interp.SetResult(lineLen); } else { interp.SetResult(line); } } catch (IOException e) { throw new TclRuntimeError("GetsCmd.cmdProc() Error: IOException when getting " + chan.ChanName + ": " + e.Message); } return(TCL.CompletionCode.RETURN); }
/* *---------------------------------------------------------------------- * * 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_NewStringObj(byte[] value, int iLength) { if (iLength > 0 && iLength < value.Length) { return(TclString.NewInstance(Encoding.UTF8.GetString(value, 0, iLength))); } else { return(TclString.NewInstance(Encoding.UTF8.GetString(value, 0, value.Length))); } }
private TclObject GetCmdObject(TclObject[] argv) // Argument list passed to the "after" command. { if (argv.Length == 3) { return(argv[2]); } else { TclObject cmd = TclString.NewInstance(Util.concat(2, argv.Length - 1, argv)); return(cmd); } }
/* *---------------------------------------------------------------------- * * 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; }
public static TclObject Tcl_NewStringObj(string value, int iLength) { if (value == null) { value = ""; } else { value = value.Split('\0')[0]; } if (iLength <= 0) { iLength = value.Length; } return(TclString.NewInstance(value.Substring(0, iLength))); }
public static string translateFileName(Interp interp, string path) { string fileName = ""; if ((path.Length == 0) || (path[0] != '~')) { // fileName = path; TclObject[] joinArrayObj = new TclObject[1]; joinArrayObj[0] = TclString.NewInstance(path); fileName = joinPath(interp, joinArrayObj, 0, 1); } else { TclObject[] splitArrayObj = TclList.getElements(interp, splitPath(interp, path)); string user = splitArrayObj[0].ToString().Substring(1); // Strip the trailing ':' off of a Mac path // before passing the user name to DoTildeSubst. if ((JACL.PLATFORM == JACL.PLATFORM_MAC) && (user.EndsWith(":"))) { user = user.Substring(0, (user.Length - 1) - (0)); } user = doTildeSubst(interp, user); // if (splitArrayObj.length < 2) { // fileName = user; // } else { splitArrayObj[0] = TclString.NewInstance(user); fileName = joinPath(interp, splitArrayObj, 0, splitArrayObj.Length); // } } // Convert forward slashes to backslashes in Windows paths because // some system interfaces don't accept forward slashes. if (JACL.PLATFORM == JACL.PLATFORM_WINDOWS) { fileName = fileName.Replace('/', '\\'); } return(fileName); }
/* *---------------------------------------------------------------------- * * 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; }
/* *---------------------------------------------------------------------- * * initOptStr -- * * This static method is called when the TraceCmd class is loaded * into the VM. It initializes the opStr array. * * Results: * Initial value for opStr. * * Side effects: * The TclObjects stored in opStr are preserve()'ed. * *---------------------------------------------------------------------- */ private static TclObject[] initOptStr() { TclObject[] strings = new TclObject[8]; strings[0] = TclString.NewInstance("error"); strings[1] = TclString.NewInstance("r"); strings[2] = TclString.NewInstance("w"); strings[3] = TclString.NewInstance("rw"); strings[4] = TclString.NewInstance("u"); strings[5] = TclString.NewInstance("ru"); strings[6] = TclString.NewInstance("wu"); strings[7] = TclString.NewInstance("rwu"); for (int i = 0; i < 8; i++) { strings[i].Preserve(); } return(strings); }
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)); } }
/// <summary> Splits a list (in string rep) up into its constituent fields. /// /// </summary> /// <param name="interp">current interpreter. /// </param> /// <param name="v">store the list elements in this vector. /// </param> /// <param name="s">the string to convert into a list. /// </param> /// <exception cref=""> TclException if the object doesn't contain a valid list. /// </exception> private static void splitList(Interp interp, ArrayList v, string s) { int len = s.Length; int i = 0; while (i < len) { FindElemResult res = Util.findElement(interp, s, i, len); if (res == null) { break; } else { TclObject tobj = TclString.NewInstance(res.Elem); tobj.Preserve(); v.Add(tobj); } i = res.ElemEnd; } }
/* *---------------------------------------------------------------------- * * 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; }
/// <summary> See Tcl user documentation for details.</summary> public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv) { if ((argv.Length < 2) || (argv.Length > 3)) { throw new TclNumArgsException(interp, 1, argv, "script ?count?"); } int count; if (argv.Length == 2) { count = 1; } else { count = TclInteger.Get(interp, argv[2]); } long startTime = System.DateTime.Now.Ticks; for (int i = 0; i < count; i++) { interp.Eval(argv[1], 0); } long endTime = System.DateTime.Now.Ticks; long uSecs = (((endTime - startTime) / 10) / count); if (uSecs == 1) { interp.SetResult(TclString.NewInstance("1 microsecond per iteration")); } else { interp.SetResult(TclString.NewInstance(uSecs + " microseconds per iteration")); } return(TCL.CompletionCode.RETURN); }
/// <summary> Tcl_WriteChars -> write /// /// Write string data to the Channel. /// /// </summary> /// <param name="interp">is used for TclExceptions. /// </param> /// <param name="outStr">the String object to write. /// </param> public void Write(Interp interp, string outStr) { Write(interp, TclString.NewInstance(outStr)); }
/// <summary> This procedure is invoked to process the "read" 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 int i = 1; // Index to the next arg in argv int toRead = 0; // Number of bytes or chars to read from channel int charactersRead; // Number of bytes or chars read from channel bool readAll = true; // If true read-all else toRead bool noNewline = false; // If true, strip the newline if there TclObject result; if ((argv.Length != 2) && (argv.Length != 3)) { errorWrongNumArgs(interp, argv[0].ToString()); } if (argv[i].ToString().Equals("-nonewline")) { noNewline = true; i++; } if (i == argv.Length) { errorWrongNumArgs(interp, argv[0].ToString()); } chan = TclIO.getChannel(interp, argv[i].ToString()); if (chan == null) { throw new TclException(interp, "can not find channel named \"" + argv[i].ToString() + "\""); } // Consumed channel name. i++; // Compute how many bytes or chars to read, and see whether the final // noNewline should be dropped. if (i < argv.Length) { string arg = argv[i].ToString(); if (System.Char.IsDigit(arg[0])) { toRead = TclInteger.Get(interp, argv[i]); readAll = false; } else if (arg.Equals("nonewline")) { noNewline = true; } else { throw new TclException(interp, "bad argument \"" + arg + "\": should be \"nonewline\""); } } try { if ((System.Object)chan.Encoding == null) { result = TclByteArray.NewInstance(); } else { result = TclString.NewInstance(new StringBuilder(64)); } if (readAll) { charactersRead = chan.read(interp, result, TclIO.READ_ALL, 0); // If -nonewline was specified, and we have not hit EOF // and the last char is a "\n", then remove it and return. if (noNewline) { string inStr = result.ToString(); if ((charactersRead > 0) && (inStr[charactersRead - 1] == '\n')) { interp.SetResult(inStr.Substring(0, ((charactersRead - 1)) - (0))); return(TCL.CompletionCode.RETURN); } } } else { // FIXME: Bug here, the -nonewline flag must be respected // when reading a set number of bytes charactersRead = chan.read(interp, result, TclIO.READ_N_BYTES, toRead); } /* * // FIXME: Port this -nonewline logic from the C code. * if (charactersRead < 0) { * Tcl_ResetResult(interp); * Tcl_AppendResult(interp, "error reading \"", name, "\": ", * Tcl_PosixError(interp), (char *) NULL); * Tcl_DecrRefCount(resultPtr); * return TCL_ERROR; * } * * // If requested, remove the last newline in the channel if at EOF. * * if ((charactersRead > 0) && (newline != 0)) { * char *result; * int length; * * result = Tcl_GetStringFromObj(resultPtr, length); * if (result[length - 1] == '\n') { * Tcl_SetObjLength(resultPtr, length - 1); * } * } * */ interp.SetResult(result); } catch (IOException e) { throw new TclRuntimeError("ReadCmd.cmdProc() Error: IOException when reading " + chan.ChanName); } 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); }
/// <summary> This procedure is invoked to process the "split" Tcl /// command. 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) { char[] splitChars = null; string inString; if (argv.Length == 2) { splitChars = defSplitChars; } else if (argv.Length == 3) { splitChars = argv[2].ToString().ToCharArray(); } else { throw new TclNumArgsException(interp, 1, argv, "string ?splitChars?"); } inString = argv[1].ToString(); int len = inString.Length; int num = splitChars.Length; /* * Handle the special case of splitting on every character. */ if (num == 0) { TclObject list = TclList.NewInstance(); list.Preserve(); try { for (int i = 0; i < len; i++) { TclList.Append(interp, list, TclString.NewInstance(inString[i])); } interp.SetResult(list); } finally { list.Release(); } return(TCL.CompletionCode.RETURN); } /* * Normal case: split on any of a given set of characters. * Discard instances of the split characters. */ TclObject list2 = TclList.NewInstance(); int elemStart = 0; list2.Preserve(); try { int i, j; for (i = 0; i < len; i++) { char c = inString[i]; for (j = 0; j < num; j++) { if (c == splitChars[j]) { TclList.Append(interp, list2, TclString.NewInstance(inString.Substring(elemStart, (i) - (elemStart)))); elemStart = i + 1; break; } } } if (i != 0) { TclList.Append(interp, list2, TclString.NewInstance(inString.Substring(elemStart))); } interp.SetResult(list2); } finally { list2.Release(); } return(TCL.CompletionCode.RETURN); }
public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv) { bool nocase = false; bool indices = false; try { int i = 1; while (argv[i].ToString().StartsWith("-")) { int index = TclIndex.Get(interp, argv[i], validOpts, "switch", 0); i++; switch (index) { case OPT_INDICES: { indices = true; break; } case OPT_NOCASE: { nocase = true; break; } case OPT_LAST: { goto opts_brk; } } } opts_brk: ; TclObject exp = TclString.NewInstance(argv[i++].ToString().Replace("\\d", "[0-9]")); string inString = argv[i++].ToString(); int matches = argv.Length - i; Regexp r = TclRegexp.compile(interp, exp, nocase); int[] args = new int[matches * 2]; bool matched = r.match(inString, args); if (matched) { for (int match = 0; i < argv.Length; i++) { TclObject obj; int start = args[match++]; int end = args[match++]; if (indices) { if (end >= 0) { end--; } obj = TclList.NewInstance(); TclList.Append(interp, obj, TclInteger.NewInstance(start)); TclList.Append(interp, obj, TclInteger.NewInstance(end)); } else { string range = (start >= 0) ? inString.Substring(start, (end) - (start)) : ""; obj = TclString.NewInstance(range); } try { interp.SetVar(argv[i].ToString(), obj, 0); } catch (TclException e) { throw new TclException(interp, "couldn't set variable \"" + argv[i] + "\""); } } } interp.SetResult(matched); } catch (System.IndexOutOfRangeException e) { throw new TclNumArgsException(interp, 1, argv, "?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"); } return(TCL.CompletionCode.RETURN); }
private static void deleteOneFile(Interp interp, string fileName, bool force) { if (fileName == ":memory:") { return; } bool isDeleted = true; FileInfo fileObj = FileUtil.getNewFileObj(interp, fileName); // Trying to delete a file that does not exist is not // considered an error, just a no-op bool tmpBool; if (File.Exists(fileObj.FullName)) { tmpBool = true; } else { tmpBool = Directory.Exists(fileObj.FullName); } if ((!tmpBool) || (fileName.Length == 0)) { return; } // If the file is a non-empty directory, recursively delete its children if // the -force option was chosen. Otherwise, throw an error. if (Directory.Exists(fileObj.FullName) && (Directory.GetFileSystemEntries(fileObj.FullName).Length > 0)) { if (force) { string[] fileList = Directory.GetFileSystemEntries(fileObj.FullName); for (int i = 0; i < fileList.Length; i++) { TclObject[] joinArrayObj = new TclObject[2]; joinArrayObj[0] = TclString.NewInstance(fileName); joinArrayObj[1] = TclString.NewInstance(fileList[i]); string child = FileUtil.joinPath(interp, joinArrayObj, 0, 2); deleteOneFile(interp, child, force); } } else { throw new TclPosixException(interp, TclPosixException.ENOTEMPTY, "error deleting \"" + fileName + "\": directory not empty"); } } try { bool tmpBool2; if (File.Exists(fileObj.FullName)) { fileObj.Attributes = FileAttributes.Normal; File.Delete(fileObj.FullName); tmpBool2 = true; } else if (Directory.Exists(fileObj.FullName)) { Directory.Delete(fileObj.FullName); tmpBool2 = true; } else { tmpBool2 = false; } isDeleted = tmpBool2; } catch (IOException e) { throw new TclException(interp, e.Message); } catch (System.Security.SecurityException e) { throw new TclException(interp, e.Message); } if (!isDeleted) { throw new TclPosixException(interp, TclPosixException.EACCES, true, "error deleting \"" + fileName + "\": best guess at reason"); } }
private static void getAndStoreStatData(Interp interp, string fileName, string varName) { FileInfo fileObj = FileUtil.getNewFileObj(interp, fileName); bool tmpBool; if (File.Exists(fileObj.FullName)) { tmpBool = true; } else { tmpBool = Directory.Exists(fileObj.FullName); } if (!tmpBool) { throw new TclPosixException(interp, TclPosixException.ENOENT, true, "could not read \"" + fileName + "\""); } try { int mtime = getMtime(interp, fileName, fileObj); TclObject mtimeObj = TclInteger.NewInstance(mtime); TclObject atimeObj = TclInteger.NewInstance(mtime); TclObject ctimeObj = TclInteger.NewInstance(mtime); interp.SetVar(varName, "atime", atimeObj, 0); interp.SetVar(varName, "ctime", ctimeObj, 0); interp.SetVar(varName, "mtime", mtimeObj, 0); } catch (System.Security.SecurityException e) { throw new TclException(interp, e.Message); } catch (TclException e) { throw new TclException(interp, "can't set \"" + varName + "(dev)\": variable isn't array"); } try { TclObject sizeObj = TclInteger.NewInstance((int)SupportClass.FileLength(fileObj)); interp.SetVar(varName, "size", sizeObj, 0); } catch (System.Exception e) { // Do nothing. } try { TclObject typeObj = TclString.NewInstance(getType(interp, fileName, fileObj)); interp.SetVar(varName, "type", typeObj, 0); } catch (System.Exception e) { } try { TclObject uidObj = TclBoolean.newInstance(isOwner(interp, fileObj)); interp.SetVar(varName, "uid", uidObj, 0); } catch (TclException e) { // Do nothing. } }
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; 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 (File.Exists(fileObj.FullName)) { tmpBool = true; } else { tmpBool = Directory.Exists(fileObj.FullName); } if (tmpBool) { isExe = 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 (File.Exists(fileObj.FullName)) { tmpBool2 = true; } else { tmpBool2 = 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(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(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 (File.Exists(fileObj.FullName)) { tmpBool3 = true; } else { tmpBool3 = 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 { FileInfo[] roots = (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"); } }
internal ParseResult(string s, int ni) { Value = TclString.NewInstance(s); Value.Preserve(); nextIndex = ni; }
private static void fileCopyRename(Interp interp, TclObject[] argv, bool copyFlag) { int firstSource = 2; bool force = false; for (bool last = false; (firstSource < argv.Length) && (!last); firstSource++) { if (!argv[firstSource].ToString().StartsWith("-")) { break; } int opt = TclIndex.Get(interp, argv[firstSource], validOptions, "option", 1); switch (opt) { case OPT_FORCE: force = true; break; case OPT_LAST: last = true; break; default: throw new TclRuntimeError("FileCmd.cmdProc: bad option " + opt + " index to validOptions"); } } if (firstSource >= (argv.Length - 1)) { throw new TclNumArgsException(interp, firstSource, argv, "?options? source ?source ...? target"); } // WARNING: ignoring links because Java does not support them. int target = argv.Length - 1; string targetName = argv[target].ToString(); FileInfo targetObj = FileUtil.getNewFileObj(interp, targetName); if (Directory.Exists(targetObj.FullName)) { // If the target is a directory, move each source file into target // directory. Extract the tailname from each source, and append it to // the end of the target path. for (int source = firstSource; source < target; source++) { string sourceName = argv[source].ToString(); if (targetName.Length == 0) { copyRenameOneFile(interp, sourceName, targetName, copyFlag, force); } else { string tailName = getTail(interp, sourceName); TclObject[] joinArrayObj = new TclObject[2]; joinArrayObj[0] = TclString.NewInstance(targetName); joinArrayObj[1] = TclString.NewInstance(tailName); string fullTargetName = FileUtil.joinPath(interp, joinArrayObj, 0, 2); copyRenameOneFile(interp, sourceName, fullTargetName, copyFlag, force); } } } else { // If there is more than 1 source file and the target is not a // directory, then throw an exception. if (firstSource + 1 != target) { string action; if (copyFlag) { action = "copying"; } else { action = "renaming"; } throw new TclPosixException(interp, TclPosixException.ENOTDIR, "error " + action + ": target \"" + argv[target].ToString() + "\" is not a directory"); } string sourceName = argv[firstSource].ToString(); copyRenameOneFile(interp, sourceName, targetName, copyFlag, force); } }
internal ParseResult(StringBuilder sbuf, int ni) { Value = TclString.NewInstance(sbuf.ToString()); Value.Preserve(); nextIndex = ni; }