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
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2002-08-03 06:04:00 +00:00
|
|
|
#include <stdlib.h>
|
2003-01-28 21:16:21 +00:00
|
|
|
#include <string.h>
|
2002-08-03 06:04:00 +00:00
|
|
|
|
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"
|
2022-05-26 07:11:50 +00:00
|
|
|
#include "QF/llist.h"
|
2002-08-05 00:01:06 +00:00
|
|
|
#include "QF/cbuf.h"
|
|
|
|
#include "QF/va.h"
|
2003-04-13 22:07:58 +00:00
|
|
|
#include "QF/gib.h"
|
|
|
|
|
|
|
|
#include "gib_buffer.h"
|
|
|
|
#include "gib_tree.h"
|
|
|
|
#include "gib_function.h"
|
|
|
|
#include "gib_vars.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2003-02-14 22:42:11 +00:00
|
|
|
hashtab_t *gib_functions = 0;
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2012-05-21 23:23:22 +00:00
|
|
|
/*
|
2002-08-08 09:20:00 +00:00
|
|
|
Hashtable callbacks
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
GIB_Function_Get_Key (const void *ele, void *ptr)
|
2002-08-03 06:04:00 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
return ((gib_function_t *) ele)->name;
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|
2003-02-14 08:06:01 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-08-03 06:04:00 +00:00
|
|
|
GIB_Function_Free (void *ele, void *ptr)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
gib_function_t *func = (gib_function_t *) ele;
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
dstring_delete (func->text);
|
2003-02-14 22:42:11 +00:00
|
|
|
free ((void *) func->name);
|
2003-01-28 21:16:21 +00:00
|
|
|
if (func->program)
|
2003-01-30 23:26:43 +00:00
|
|
|
GIB_Tree_Free_Recursive (func->program);
|
2003-02-14 08:06:01 +00:00
|
|
|
if (func->script && !(--func->script->refs)) {
|
2003-02-14 22:42:11 +00:00
|
|
|
free ((void *) func->script->text);
|
|
|
|
free ((void *) func->script->file);
|
2003-02-14 08:06:01 +00:00
|
|
|
free (func->script);
|
|
|
|
}
|
2003-01-03 04:15:32 +00:00
|
|
|
free (func);
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|
|
|
|
|
2003-10-19 00:51:47 +00:00
|
|
|
/*
|
|
|
|
GIB_Function_New
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-10-19 00:51:47 +00:00
|
|
|
Builds a new function struct and returns
|
|
|
|
a pointer to it.
|
|
|
|
*/
|
2004-04-09 18:18:19 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
afree (void *data, void *unused)
|
|
|
|
{
|
|
|
|
free (data);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-10-19 00:51:47 +00:00
|
|
|
static gib_function_t *
|
|
|
|
GIB_Function_New (const char *name)
|
|
|
|
{
|
|
|
|
gib_function_t *new = calloc (1, sizeof (gib_function_t));
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-10-19 00:51:47 +00:00
|
|
|
new->text = dstring_newstr ();
|
|
|
|
new->name = strdup (name);
|
|
|
|
new->arglist = llist_new (afree, NULL, NULL);
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Function_Define
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
Sets the program and text of a GIB function,
|
2002-08-08 09:20:00 +00:00
|
|
|
allocating one and adding it to the functions
|
|
|
|
hash if needed.
|
|
|
|
*/
|
2003-09-11 06:03:13 +00:00
|
|
|
gib_function_t *
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Function_Define (const char *name, const char *text, gib_tree_t * program,
|
|
|
|
gib_script_t * script, hashtab_t * globals)
|
2002-08-03 06:04:00 +00:00
|
|
|
{
|
|
|
|
gib_function_t *func;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_Tree_Ref (&program);
|
|
|
|
if (script)
|
|
|
|
script->refs++;
|
2002-08-03 06:04:00 +00:00
|
|
|
if (!gib_functions)
|
2020-03-25 06:43:16 +00:00
|
|
|
gib_functions = Hash_NewTable (1024, GIB_Function_Get_Key,
|
|
|
|
GIB_Function_Free, 0, 0);
|
2003-01-30 23:26:43 +00:00
|
|
|
|
2003-02-14 22:42:11 +00:00
|
|
|
func = Hash_Find (gib_functions, name);
|
2003-01-28 21:16:21 +00:00
|
|
|
if (func) {
|
|
|
|
dstring_clearstr (func->text);
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_Tree_Unref (&func->program);
|
|
|
|
if (func->script && !(--func->script->refs)) {
|
2003-02-14 22:42:11 +00:00
|
|
|
free ((void *) func->script->text);
|
|
|
|
free ((void *) func->script->file);
|
2003-02-14 08:06:01 +00:00
|
|
|
free (func->script);
|
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
} else {
|
|
|
|
func = GIB_Function_New (name);
|
2002-08-03 06:04:00 +00:00
|
|
|
Hash_Add (gib_functions, func);
|
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
dstring_appendstr (func->text, text);
|
|
|
|
func->program = program;
|
|
|
|
func->globals = globals;
|
2003-02-14 08:06:01 +00:00
|
|
|
func->script = script;
|
2003-09-11 06:03:13 +00:00
|
|
|
|
|
|
|
return func;
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Function_Find
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
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
|
|
|
|
2007-03-22 23:20:57 +00:00
|
|
|
static cbuf_t *g_fpa_cbuf;
|
|
|
|
static const char **g_fpa_args;
|
|
|
|
static unsigned int g_fpa_argc;
|
|
|
|
static hashtab_t *g_fpa_zero = 0;
|
|
|
|
static unsigned int g_fpa_i, g_fpa_ind;
|
|
|
|
|
2023-06-13 09:06:11 +00:00
|
|
|
static bool fpa_iterate (char *arg, llist_node_t *node)
|
2007-03-22 23:20:57 +00:00
|
|
|
{
|
|
|
|
gib_var_t *var = GIB_Var_Get_Complex (&GIB_DATA(g_fpa_cbuf)->locals, &g_fpa_zero,
|
|
|
|
arg, &g_fpa_ind, true);
|
|
|
|
|
|
|
|
if (!var->array[0].value)
|
|
|
|
var->array[0].value = dstring_newstr ();
|
|
|
|
dstring_copystr (var->array[0].value, g_fpa_args[g_fpa_i]);
|
|
|
|
g_fpa_i++;
|
|
|
|
return g_fpa_i < g_fpa_argc;
|
|
|
|
}
|
|
|
|
|
2003-10-19 00:51:47 +00:00
|
|
|
static void
|
|
|
|
GIB_Function_Prepare_Args (cbuf_t * cbuf, const char **args, unsigned int
|
|
|
|
argc, llist_t *arglist)
|
2003-04-11 02:57:11 +00:00
|
|
|
{
|
2007-03-22 23:20:57 +00:00
|
|
|
gib_var_t *var;
|
|
|
|
unsigned int i;
|
2003-04-11 02:57:11 +00:00
|
|
|
static char argss[] = "args";
|
|
|
|
|
2007-03-22 23:20:57 +00:00
|
|
|
g_fpa_cbuf = cbuf;
|
|
|
|
g_fpa_args = args;
|
|
|
|
g_fpa_argc = argc;
|
|
|
|
|
|
|
|
g_fpa_i = 1; llist_iterate (arglist, LLIST_ICAST (fpa_iterate));
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-04-11 02:57:11 +00:00
|
|
|
var =
|
2007-03-22 23:20:57 +00:00
|
|
|
GIB_Var_Get_Complex (&GIB_DATA (cbuf)->locals, &g_fpa_zero, argss,
|
|
|
|
&g_fpa_ind, true);
|
2003-04-11 02:57:11 +00:00
|
|
|
var->array = realloc (var->array, sizeof (struct gib_varray_s) * argc);
|
|
|
|
memset (var->array + 1, 0, (argc - 1) * sizeof (struct gib_varray_s));
|
|
|
|
var->size = argc;
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (var->array[i].value)
|
|
|
|
dstring_clearstr (var->array[i].value);
|
|
|
|
else
|
|
|
|
var->array[i].value = dstring_newstr ();
|
|
|
|
dstring_appendstr (var->array[i].value, args[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 23:20:57 +00:00
|
|
|
static cbuf_t *g_fpad_cbuf;
|
|
|
|
static dstring_t **g_fpad_args;
|
|
|
|
static unsigned int g_fpad_argc;
|
|
|
|
static hashtab_t *g_fpad_zero = 0;
|
|
|
|
static unsigned int g_fpad_i, g_fpad_ind;
|
|
|
|
|
2023-06-13 09:06:11 +00:00
|
|
|
static bool fpad_iterate (char *arg, llist_node_t *node)
|
2007-03-22 23:20:57 +00:00
|
|
|
{
|
|
|
|
gib_var_t *var;
|
|
|
|
|
|
|
|
var = GIB_Var_Get_Complex (&GIB_DATA(g_fpad_cbuf)->locals, &g_fpad_zero,
|
|
|
|
arg, &g_fpad_ind, true);
|
|
|
|
if (!var->array[0].value)
|
|
|
|
var->array[0].value = dstring_newstr ();
|
|
|
|
dstring_copystr (var->array[0].value, g_fpad_args[g_fpad_i]->str);
|
|
|
|
g_fpad_i++;
|
|
|
|
return g_fpad_i < g_fpad_argc;
|
|
|
|
}
|
|
|
|
|
2003-10-19 00:51:47 +00:00
|
|
|
static void
|
|
|
|
GIB_Function_Prepare_Args_D (cbuf_t * cbuf, dstring_t **args, unsigned int
|
|
|
|
argc, llist_t *arglist)
|
2002-11-14 02:10:55 +00:00
|
|
|
{
|
2007-03-22 23:20:57 +00:00
|
|
|
unsigned int i;
|
2003-02-14 22:42:11 +00:00
|
|
|
gib_var_t *var;
|
2003-01-30 23:26:43 +00:00
|
|
|
static char argss[] = "args";
|
2003-01-28 21:16:21 +00:00
|
|
|
|
2007-03-22 23:20:57 +00:00
|
|
|
g_fpad_cbuf = cbuf;
|
|
|
|
g_fpad_args = args;
|
|
|
|
g_fpad_argc = argc;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2018-09-08 13:23:57 +00:00
|
|
|
llist_iterate (arglist, LLIST_ICAST (fpad_iterate));
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2018-09-08 13:23:57 +00:00
|
|
|
var = GIB_Var_Get_Complex (&GIB_DATA (cbuf)->locals, &g_fpad_zero, argss,
|
|
|
|
&g_fpad_ind, true);
|
2003-02-16 19:46:34 +00:00
|
|
|
var->array = realloc (var->array, sizeof (struct gib_varray_s) * argc);
|
|
|
|
memset (var->array + 1, 0, (argc - 1) * sizeof (struct gib_varray_s));
|
2003-01-30 23:26:43 +00:00
|
|
|
var->size = argc;
|
|
|
|
for (i = 0; i < argc; i++) {
|
2003-02-16 02:44:24 +00:00
|
|
|
if (var->array[i].value)
|
|
|
|
dstring_clearstr (var->array[i].value);
|
2003-01-28 21:16:21 +00:00
|
|
|
else
|
2003-02-16 02:44:24 +00:00
|
|
|
var->array[i].value = dstring_newstr ();
|
|
|
|
dstring_appendstr (var->array[i].value, args[i]->str);
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
2002-11-14 02:10:55 +00:00
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Function_Execute
|
2012-05-21 23:23:22 +00:00
|
|
|
|
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-11-14 02:10:55 +00:00
|
|
|
|
2003-10-19 00:51:47 +00:00
|
|
|
int
|
2003-04-11 02:57:11 +00:00
|
|
|
GIB_Function_Execute (cbuf_t * cbuf, gib_function_t * func, const char ** args,
|
2003-02-14 22:42:11 +00:00
|
|
|
unsigned int argc)
|
2002-08-05 00:01:06 +00:00
|
|
|
{
|
2003-10-19 00:51:47 +00:00
|
|
|
if (argc < func->minargs)
|
|
|
|
return -1;
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_Tree_Ref (&func->program);
|
|
|
|
if (func->script)
|
|
|
|
func->script->refs++;
|
2003-01-30 23:26:43 +00:00
|
|
|
GIB_Buffer_Set_Program (cbuf, func->program);
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_DATA (cbuf)->script = func->script;
|
|
|
|
GIB_DATA (cbuf)->globals = func->globals;
|
2003-10-19 00:51:47 +00:00
|
|
|
GIB_Function_Prepare_Args (cbuf, args, argc, func->arglist);
|
|
|
|
return 0;
|
2002-08-05 00:01:06 +00:00
|
|
|
}
|
2003-04-11 02:57:11 +00:00
|
|
|
|
2003-10-19 00:51:47 +00:00
|
|
|
int
|
2003-04-11 02:57:11 +00:00
|
|
|
GIB_Function_Execute_D (cbuf_t * cbuf, gib_function_t * func, dstring_t ** args,
|
|
|
|
unsigned int argc)
|
|
|
|
{
|
2003-10-19 00:51:47 +00:00
|
|
|
if (argc < func->minargs)
|
|
|
|
return -1;
|
2003-04-11 02:57:11 +00:00
|
|
|
GIB_Tree_Ref (&func->program);
|
|
|
|
if (func->script)
|
|
|
|
func->script->refs++;
|
|
|
|
GIB_Buffer_Set_Program (cbuf, func->program);
|
|
|
|
GIB_DATA (cbuf)->script = func->script;
|
|
|
|
GIB_DATA (cbuf)->globals = func->globals;
|
2003-10-19 00:51:47 +00:00
|
|
|
GIB_Function_Prepare_Args_D (cbuf, args, argc, func->arglist);
|
|
|
|
return 0;
|
2003-04-11 02:57:11 +00:00
|
|
|
}
|