aboutsummaryrefslogtreecommitdiff
path: root/jim.c
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2010-10-26 10:09:57 +1000
committerSteve Bennett <steveb@workware.net.au>2010-12-02 09:04:54 +1000
commit064ec299a9f7b1016ed7c24e5a014d94271281b3 (patch)
tree65925898533b35a1e730f0dfb5eee57de7c5328a /jim.c
parentd160ac7e0cdd776e46c502ab1ad33aa5366acc99 (diff)
downloadjimtcl-064ec299a9f7b1016ed7c24e5a014d94271281b3.zip
jimtcl-064ec299a9f7b1016ed7c24e5a014d94271281b3.tar.gz
jimtcl-064ec299a9f7b1016ed7c24e5a014d94271281b3.tar.bz2
Add the [exists] command
Especially simplifies checking for the existence of procs. Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'jim.c')
-rw-r--r--jim.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/jim.c b/jim.c
index 9a9342c..accb9d5 100644
--- a/jim.c
+++ b/jim.c
@@ -12995,6 +12995,51 @@ static int Jim_InfoCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *arg
return JIM_OK;
}
+/* [exists] */
+static int Jim_ExistsCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *objPtr;
+
+ static const char * const options[] = {
+ "-command", "-proc", "-var", NULL
+ };
+ enum
+ {
+ OPT_COMMAND, OPT_PROC, OPT_VAR
+ };
+ int option;
+
+ if (argc == 2) {
+ option = OPT_VAR;
+ objPtr = argv[1];
+ }
+ else if (argc == 3) {
+ if (Jim_GetEnum(interp, argv[1], options, &option, NULL, JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) {
+ return JIM_ERR;
+ }
+ objPtr = argv[2];
+ }
+ else {
+ Jim_WrongNumArgs(interp, 1, argv, "?option? name");
+ return JIM_ERR;
+ }
+
+ /* Test for the the most common commands first, just in case it makes a difference */
+ switch (option) {
+ case OPT_VAR:
+ Jim_SetResultBool(interp, Jim_GetVariable(interp, objPtr, 0) != NULL);
+ break;
+
+ case OPT_COMMAND:
+ case OPT_PROC: {
+ Jim_Cmd *cmd = Jim_GetCommand(interp, objPtr, JIM_NONE);
+ Jim_SetResultBool(interp, cmd != NULL && (option == OPT_COMMAND || !cmd->cmdProc));
+ break;
+ }
+ }
+ return JIM_OK;
+}
+
/* [split] */
static int Jim_SplitCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
@@ -13501,6 +13546,7 @@ static const struct {
{"dict", Jim_DictCoreCommand},
{"subst", Jim_SubstCoreCommand},
{"info", Jim_InfoCoreCommand},
+ {"exists", Jim_ExistsCoreCommand},
{"split", Jim_SplitCoreCommand},
{"join", Jim_JoinCoreCommand},
{"format", Jim_FormatCoreCommand},