From 064ec299a9f7b1016ed7c24e5a014d94271281b3 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Tue, 26 Oct 2010 10:09:57 +1000 Subject: Add the [exists] command Especially simplifies checking for the existence of procs. Signed-off-by: Steve Bennett --- jim.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'jim.c') 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}, -- cgit v1.1