/* *---------------------------------------------------------------------- * * NamespaceWhichCmd -> whichCmd * * Invoked to implement the "namespace which" command that returns the * fully-qualified name of a command or variable. If the specified * command or variable does not exist, it returns "". Handles the * following syntax: * * namespace which ?-command? ?-variable? name * * Results: * Returns if successful, raises TclException if something goes wrong. * * Side effects: * Returns a result in the interpreter's result object. If anything * goes wrong, the result is an error message. * *---------------------------------------------------------------------- */ private static void whichCmd(Interp interp, TclObject[] objv) { string arg; WrappedCommand cmd; Var variable; int argIndex, lookup; if (objv.Length < 3) { throw new TclNumArgsException(interp, 2, objv, "?-command? ?-variable? name"); } // Look for a flag controlling the lookup. argIndex = 2; lookup = 0; // assume command lookup by default arg = objv[2].ToString(); if ((arg.Length > 1) && (arg[0] == '-')) { if (arg.Equals("-command")) { lookup = 0; } else if (arg.Equals("-variable")) { lookup = 1; } else { throw new TclNumArgsException(interp, 2, objv, "?-command? ?-variable? name"); } argIndex = 3; } if (objv.Length != (argIndex + 1)) { throw new TclNumArgsException(interp, 2, objv, "?-command? ?-variable? name"); } // FIXME : check that this implementation works! switch (lookup) { case 0: arg = objv[argIndex].ToString(); // FIXME : is this the right way to lookup a Command token? //cmd = Tcl_GetCommandFromObj(interp, objv[argIndex]); cmd = NamespaceCmd.findCommand(interp, arg, null, 0); if (cmd == null) { return ; // cmd not found, just return (no error) } interp.setResult(interp.getCommandFullName(cmd)); return ; case 1: arg = objv[argIndex].ToString(); variable = NamespaceCmd.findNamespaceVar(interp, arg, null, 0); if (variable != null) { interp.setResult(Var.getVariableFullName(interp, variable)); } return ; } 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; }
/* *---------------------------------------------------------------------- * * NamespaceOriginCmd -> originCmd * * Invoked to implement the "namespace origin" command to return the * fully-qualified name of the "real" command to which the specified * "imported command" refers. Handles the following syntax: * * namespace origin name * * Results: * An imported command is created in an namespace when that namespace * imports a command from another namespace. If a command is imported * into a sequence of namespaces a, b,...,n where each successive * namespace just imports the command from the previous namespace, this * command returns the fully-qualified name of the original command in * the first namespace, a. If "name" does not refer to an alias, its * fully-qualified name is returned. The returned name is stored in the * interpreter's result object. This procedure returns TCL_OK if * successful, and TCL_ERROR if anything goes wrong. * * Side effects: * If anything goes wrong, this procedure returns an error message in * the interpreter's result object. * *---------------------------------------------------------------------- */ private static void originCmd(Interp interp, TclObject[] objv) { WrappedCommand command, origCommand; if (objv.Length != 3) { throw new TclNumArgsException(interp, 2, objv, "name"); } // FIXME : is this the right way to search for a command? //command = Tcl_GetCommandFromObj(interp, objv[2]); command = NamespaceCmd.findCommand(interp, objv[2].ToString(), null, 0); if (command == null) { throw new TclException(interp, "invalid command name \"" + objv[2].ToString() + "\""); } origCommand = getOriginalCommand(command); if (origCommand == null) { // The specified command isn't an imported command. Return the // command's name qualified by the full name of the namespace it // was defined in. interp.setResult(interp.getCommandFullName(command)); } else { interp.setResult(interp.getCommandFullName(origCommand)); } return ; }