2002-08-03 06:04:00 +00:00
|
|
|
/*
|
|
|
|
#FILENAME#
|
|
|
|
|
|
|
|
#DESCRIPTION#
|
|
|
|
|
|
|
|
Copyright (C) 2002 #AUTHOR#
|
|
|
|
|
|
|
|
Author: #AUTHOR#
|
|
|
|
Date: #DATE#
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2002-08-03 06:04:00 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2002-08-27 04:47:49 +00:00
|
|
|
#include "QF/sys.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
#include "QF/dstring.h"
|
|
|
|
#include "QF/hash.h"
|
2002-08-05 00:01:06 +00:00
|
|
|
#include "QF/cbuf.h"
|
|
|
|
#include "QF/gib_parse.h"
|
|
|
|
#include "QF/gib_buffer.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
#include "QF/gib_function.h"
|
2002-08-08 09:20:00 +00:00
|
|
|
#include "QF/gib_vars.h"
|
2002-08-05 00:01:06 +00:00
|
|
|
#include "QF/va.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
|
|
|
|
hashtab_t *gib_functions = 0;
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Function_New
|
|
|
|
|
|
|
|
Builds a new function struct and returns
|
|
|
|
a pointer to it.
|
|
|
|
*/
|
2002-08-03 06:04:00 +00:00
|
|
|
gib_function_t *
|
|
|
|
GIB_Function_New (void)
|
|
|
|
{
|
|
|
|
gib_function_t *new = calloc (1, sizeof (gib_function_t));
|
|
|
|
new->name = dstring_newstr();
|
|
|
|
new->program = dstring_newstr();
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
Hashtable callbacks
|
|
|
|
*/
|
2002-08-03 06:04:00 +00:00
|
|
|
const char *
|
|
|
|
GIB_Function_Get_Key (void *ele, void *ptr)
|
|
|
|
{
|
|
|
|
return ((gib_function_t *)ele)->name->str;
|
|
|
|
}
|
|
|
|
void
|
|
|
|
GIB_Function_Free (void *ele, void *ptr)
|
|
|
|
{
|
|
|
|
gib_function_t *func = (gib_function_t *)ele;
|
|
|
|
dstring_delete (func->name);
|
|
|
|
dstring_delete (func->program);
|
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Function_Define
|
|
|
|
|
|
|
|
Sets the program text of a GIB function,
|
|
|
|
allocating one and adding it to the functions
|
|
|
|
hash if needed.
|
|
|
|
*/
|
2002-08-03 06:04:00 +00:00
|
|
|
void
|
|
|
|
GIB_Function_Define (const char *name, const char *program)
|
|
|
|
{
|
|
|
|
gib_function_t *func;
|
|
|
|
|
|
|
|
if (!gib_functions)
|
|
|
|
gib_functions = Hash_NewTable (1024, GIB_Function_Get_Key, GIB_Function_Free, 0);
|
|
|
|
func = Hash_Find(gib_functions, name);
|
|
|
|
if (func)
|
|
|
|
dstring_clearstr (func->program);
|
|
|
|
else {
|
|
|
|
func = GIB_Function_New ();
|
|
|
|
dstring_appendstr (func->name, name);
|
|
|
|
Hash_Add (gib_functions, func);
|
|
|
|
}
|
|
|
|
dstring_appendstr (func->program, program);
|
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Function_Find
|
|
|
|
|
|
|
|
Looks up a function in the function hash
|
|
|
|
and returns a pointer to it on success,
|
|
|
|
0 otherwise
|
|
|
|
*/
|
2002-08-03 06:04:00 +00:00
|
|
|
gib_function_t *
|
|
|
|
GIB_Function_Find (const char *name)
|
|
|
|
{
|
|
|
|
if (!gib_functions)
|
|
|
|
return 0;
|
|
|
|
return (gib_function_t *) Hash_Find (gib_functions, name);
|
|
|
|
}
|
2002-08-05 00:01:06 +00:00
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Function_Execute
|
|
|
|
|
2002-08-27 04:47:49 +00:00
|
|
|
Prepares a buffer to execute
|
|
|
|
a GIB function with certain arguments
|
2002-08-08 09:20:00 +00:00
|
|
|
*/
|
2002-08-05 00:01:06 +00:00
|
|
|
void
|
2002-08-27 04:47:49 +00:00
|
|
|
GIB_Function_Execute (cbuf_t *cbuf, gib_function_t *func, cbuf_args_t *args)
|
2002-08-05 00:01:06 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2002-08-27 04:47:49 +00:00
|
|
|
Cbuf_AddText (cbuf, func->program->str);
|
|
|
|
for (i = 0; i < args->argc; i++)
|
|
|
|
GIB_Var_Set_Local (cbuf, va("%i", i), args->argv[i]->str);
|
|
|
|
GIB_Var_Set_Local (cbuf, "argc", va("%i", args->argc));
|
2002-08-05 00:01:06 +00:00
|
|
|
}
|