/* *---------------------------------------------------------------------- * * 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; }
/// <summary> /// Tcl_ProcObjCmd -> ProcCmd.cmdProc /// /// Creates a new Tcl procedure. /// /// </summary> /// <param name="interp">the current interpreter. /// </param> /// <param name="objv">command arguments. /// </param> /// <exception cref=""> TclException If incorrect number of arguments. /// </exception> public TCL.CompletionCode cmdProc(Interp interp, TclObject[] objv) { Procedure proc; string fullName, procName; NamespaceCmd.Namespace ns, altNs, cxtNs; Command cmd; StringBuilder ds; if (objv.Length != 4) { throw new TclNumArgsException(interp, 1, objv, "name args body"); } // Determine the namespace where the procedure should reside. Unless // the command name includes namespace qualifiers, this will be the // current namespace. fullName = objv[1].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[] altNsArr = new NamespaceCmd.Namespace[1]; NamespaceCmd.Namespace[] cxtNsArr = new NamespaceCmd.Namespace[1]; string[] procNameArr = new string[1]; NamespaceCmd.getNamespaceForQualName(interp, fullName, null, 0, nsArr, altNsArr, cxtNsArr, procNameArr); // Get the values out of the arrays ns = nsArr[0]; altNs = altNsArr[0]; cxtNs = cxtNsArr[0]; procName = procNameArr[0]; if (ns == null) { throw new TclException(interp, "can't create procedure \"" + fullName + "\": unknown namespace"); } if ((System.Object)procName == null) { throw new TclException(interp, "can't create procedure \"" + fullName + "\": bad procedure name"); } // FIXME : could there be a problem with a command named ":command" ? if ((ns != NamespaceCmd.getGlobalNamespace(interp)) && ((System.Object)procName != null) && ((procName.Length > 0) && (procName[0] == ':'))) { throw new TclException(interp, "can't create procedure \"" + procName + "\" in non-global namespace with name starting with \":\""); } // Create the data structure to represent the procedure. proc = new Procedure(interp, ns, procName, objv[2], objv[3], interp.ScriptFile, interp.getArgLineNumber(3)); // Now create a command for the procedure. This will initially be in // the current namespace unless the procedure's name included namespace // qualifiers. To create the new command in the right namespace, we // generate a fully qualified name for it. ds = new StringBuilder(); if (ns != NamespaceCmd.getGlobalNamespace(interp)) { ds.Append(ns.fullName); ds.Append("::"); } ds.Append(procName); interp.createCommand(ds.ToString(), proc); // Now initialize the new procedure's cmdPtr field. This will be used // later when the procedure is called to determine what namespace the // procedure will run in. This will be different than the current // namespace if the proc was renamed into a different namespace. // FIXME : we do not handle renaming into another namespace correctly yet! //procPtr->cmdPtr = (Command *) cmd; return(TCL.CompletionCode.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; }