From 20d47cde2f6ca548094bb53325980d082c59903f Mon Sep 17 00:00:00 2001 From: Brian Koropoff Date: Wed, 23 Oct 2002 03:56:57 +0000 Subject: [PATCH] Added the global.delete builtin to clean up old global variables. Updated cb.gib to use it. --- doc/config/gib/cb.gib | 3 +++ include/QF/gib_vars.h | 2 +- libs/util/gib_builtin.c | 11 +++++++++++ libs/util/gib_vars.c | 23 +++++++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/doc/config/gib/cb.gib b/doc/config/gib/cb.gib index 4ef021c0e..fcb7aac75 100644 --- a/doc/config/gib/cb.gib +++ b/doc/config/gib/cb.gib @@ -90,6 +90,9 @@ function custom.load.menu { oldi = `function.get item` function "menu" ${custom.load.menu_f} function "item" ${custom.load.item_f} + // Clear out old junk + global.delete custom.menu + global.delete custom.item custom.menu.size = 0 exec "custom/", $1, ".menu" function "menu" $oldm diff --git a/include/QF/gib_vars.h b/include/QF/gib_vars.h index 7e90f9be5..9566a322b 100644 --- a/include/QF/gib_vars.h +++ b/include/QF/gib_vars.h @@ -45,4 +45,4 @@ const char *GIB_Var_Get_Local (cbuf_t *cbuf, const char *key); const char *GIB_Var_Get_Global (const char *key); const char *GIB_Var_Get_Key (void *ele, void *ptr); void GIB_Var_Free (void *ele, void *ptr); - +void GIB_Var_Free_Global (const char *key); diff --git a/libs/util/gib_builtin.c b/libs/util/gib_builtin.c index 20ee33534..134f58c2e 100644 --- a/libs/util/gib_builtin.c +++ b/libs/util/gib_builtin.c @@ -198,6 +198,16 @@ GIB_Global_f (void) } } +void +GIB_Global_Delete_f (void) +{ + if (GIB_Argc () != 2) + Cbuf_Error ("syntax", + "global.delete: invalid syntax\n" + "usage: global.delete variable"); + GIB_Var_Free_Global (GIB_Argv(1)); +} + void GIB_Return_f (void) { @@ -767,6 +777,7 @@ GIB_Builtin_Init (void) GIB_Builtin_Add ("export", GIB_Export_f, GIB_BUILTIN_NORMAL); GIB_Builtin_Add ("local", GIB_Local_f, GIB_BUILTIN_NORMAL); GIB_Builtin_Add ("global", GIB_Global_f, GIB_BUILTIN_NORMAL); + GIB_Builtin_Add ("global.delete", GIB_Global_Delete_f, GIB_BUILTIN_NORMAL); GIB_Builtin_Add ("return", GIB_Return_f, GIB_BUILTIN_NORMAL); GIB_Builtin_Add ("if", GIB_If_f, GIB_BUILTIN_FIRSTONLY); GIB_Builtin_Add ("ifnot", GIB_If_f, GIB_BUILTIN_FIRSTONLY); diff --git a/libs/util/gib_vars.c b/libs/util/gib_vars.c index d0a7bb980..5749511ea 100644 --- a/libs/util/gib_vars.c +++ b/libs/util/gib_vars.c @@ -161,3 +161,26 @@ GIB_Var_Get_Global (const char *key) else return 0; } + +void +GIB_Var_Free_Global (const char *key) +{ + char *p, *k; + gib_var_t *root; + void *del; + k = strdup (key); + if ((p = strrchr (k, '.'))) { + *p = 0; + if ((root = GIB_Var_Get_R (gib_globals, k))) { + del = Hash_Del (root->subvars, p+1); + if (del) + GIB_Var_Free (del, 0); + } + } else { + del = Hash_Del (gib_globals, k); + if (del) + GIB_Var_Free (del, 0); + } + free (k); +} +