Added the global.delete builtin to clean up old global variables. Updated

cb.gib to use it.
This commit is contained in:
Brian Koropoff 2002-10-23 03:56:57 +00:00
parent cfffd05c71
commit 20d47cde2f
4 changed files with 38 additions and 1 deletions

View file

@ -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

View file

@ -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);

View file

@ -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);

View file

@ -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);
}