/quakeforge/branches/release_0_5_5/libs/gib/gib_function.c

# · C · 276 lines · 188 code · 30 blank · 58 comment · 20 complexity · dd0462d770ae72a99bcf5103f915b099 MD5 · raw file

  1. /*
  2. #FILENAME#
  3. #DESCRIPTION#
  4. Copyright (C) 2002 #AUTHOR#
  5. Author: #AUTHOR#
  6. Date: #DATE#
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. See the GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to:
  17. Free Software Foundation, Inc.
  18. 59 Temple Place - Suite 330
  19. Boston, MA 02111-1307, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. # include "config.h"
  23. #endif
  24. static __attribute__ ((unused))
  25. const char rcsid[] =
  26. "$Id: gib_function.c 10836 2004-04-09 18:18:19Z snax $";
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "QF/sys.h"
  30. #include "QF/dstring.h"
  31. #include "QF/hash.h"
  32. #include "QF/cbuf.h"
  33. #include "QF/va.h"
  34. #include "QF/gib.h"
  35. #include "gib_buffer.h"
  36. #include "gib_tree.h"
  37. #include "gib_function.h"
  38. #include "gib_vars.h"
  39. hashtab_t *gib_functions = 0;
  40. /*
  41. Hashtable callbacks
  42. */
  43. static const char *
  44. GIB_Function_Get_Key (void *ele, void *ptr)
  45. {
  46. return ((gib_function_t *) ele)->name;
  47. }
  48. static void
  49. GIB_Function_Free (void *ele, void *ptr)
  50. {
  51. gib_function_t *func = (gib_function_t *) ele;
  52. dstring_delete (func->text);
  53. free ((void *) func->name);
  54. if (func->program)
  55. GIB_Tree_Free_Recursive (func->program);
  56. if (func->script && !(--func->script->refs)) {
  57. free ((void *) func->script->text);
  58. free ((void *) func->script->file);
  59. free (func->script);
  60. }
  61. free (func);
  62. }
  63. /*
  64. GIB_Function_New
  65. Builds a new function struct and returns
  66. a pointer to it.
  67. */
  68. static void
  69. afree (void *data, void *unused)
  70. {
  71. free (data);
  72. };
  73. static gib_function_t *
  74. GIB_Function_New (const char *name)
  75. {
  76. gib_function_t *new = calloc (1, sizeof (gib_function_t));
  77. new->text = dstring_newstr ();
  78. new->name = strdup (name);
  79. new->arglist = llist_new (afree, NULL, NULL);
  80. return new;
  81. }
  82. /*
  83. GIB_Function_Define
  84. Sets the program and text of a GIB function,
  85. allocating one and adding it to the functions
  86. hash if needed.
  87. */
  88. gib_function_t *
  89. GIB_Function_Define (const char *name, const char *text, gib_tree_t * program,
  90. gib_script_t * script, hashtab_t * globals)
  91. {
  92. gib_function_t *func;
  93. GIB_Tree_Ref (&program);
  94. if (script)
  95. script->refs++;
  96. if (!gib_functions)
  97. gib_functions =
  98. Hash_NewTable (1024, GIB_Function_Get_Key, GIB_Function_Free, 0);
  99. func = Hash_Find (gib_functions, name);
  100. if (func) {
  101. dstring_clearstr (func->text);
  102. GIB_Tree_Unref (&func->program);
  103. if (func->script && !(--func->script->refs)) {
  104. free ((void *) func->script->text);
  105. free ((void *) func->script->file);
  106. free (func->script);
  107. }
  108. } else {
  109. func = GIB_Function_New (name);
  110. Hash_Add (gib_functions, func);
  111. }
  112. dstring_appendstr (func->text, text);
  113. func->program = program;
  114. func->globals = globals;
  115. func->script = script;
  116. return func;
  117. }
  118. /*
  119. GIB_Function_Find
  120. Looks up a function in the function hash
  121. and returns a pointer to it on success,
  122. 0 otherwise
  123. */
  124. gib_function_t *
  125. GIB_Function_Find (const char *name)
  126. {
  127. if (!gib_functions)
  128. return 0;
  129. return (gib_function_t *) Hash_Find (gib_functions, name);
  130. }
  131. static void
  132. GIB_Function_Prepare_Args (cbuf_t * cbuf, const char **args, unsigned int
  133. argc, llist_t *arglist)
  134. {
  135. static hashtab_t *zero = 0;
  136. unsigned int i, ind;
  137. gib_var_t *var;
  138. static char argss[] = "args";
  139. static qboolean
  140. iterate (char *arg, llist_node_t *node)
  141. {
  142. var = GIB_Var_Get_Complex (&GIB_DATA(cbuf)->locals, &zero,
  143. arg, &ind, true);
  144. if (!var->array[0].value)
  145. var->array[0].value = dstring_newstr ();
  146. dstring_copystr (var->array[0].value, args[i]);
  147. i++;
  148. return i < argc;
  149. }
  150. i = 1; llist_iterate (arglist, LLIST_ICAST (iterate));
  151. var =
  152. GIB_Var_Get_Complex (&GIB_DATA (cbuf)->locals, &zero, argss,
  153. &ind, true);
  154. var->array = realloc (var->array, sizeof (struct gib_varray_s) * argc);
  155. memset (var->array + 1, 0, (argc - 1) * sizeof (struct gib_varray_s));
  156. var->size = argc;
  157. for (i = 0; i < argc; i++) {
  158. if (var->array[i].value)
  159. dstring_clearstr (var->array[i].value);
  160. else
  161. var->array[i].value = dstring_newstr ();
  162. dstring_appendstr (var->array[i].value, args[i]);
  163. }
  164. }
  165. static void
  166. GIB_Function_Prepare_Args_D (cbuf_t * cbuf, dstring_t **args, unsigned int
  167. argc, llist_t *arglist)
  168. {
  169. static hashtab_t *zero = 0;
  170. unsigned int i, ind;
  171. gib_var_t *var;
  172. static char argss[] = "args";
  173. static qboolean
  174. iterate (char *arg, llist_node_t *node)
  175. {
  176. var = GIB_Var_Get_Complex (&GIB_DATA(cbuf)->locals, &zero,
  177. arg, &ind, true);
  178. if (!var->array[0].value)
  179. var->array[0].value = dstring_newstr ();
  180. dstring_copystr (var->array[0].value, args[i]->str);
  181. i++;
  182. return i < argc;
  183. }
  184. i = 1; llist_iterate (arglist, LLIST_ICAST (iterate));
  185. var =
  186. GIB_Var_Get_Complex (&GIB_DATA (cbuf)->locals, &zero, argss,
  187. &ind, true);
  188. var->array = realloc (var->array, sizeof (struct gib_varray_s) * argc);
  189. memset (var->array + 1, 0, (argc - 1) * sizeof (struct gib_varray_s));
  190. var->size = argc;
  191. for (i = 0; i < argc; i++) {
  192. if (var->array[i].value)
  193. dstring_clearstr (var->array[i].value);
  194. else
  195. var->array[i].value = dstring_newstr ();
  196. dstring_appendstr (var->array[i].value, args[i]->str);
  197. }
  198. }
  199. /*
  200. GIB_Function_Execute
  201. Prepares a buffer to execute
  202. a GIB function with certain arguments
  203. */
  204. int
  205. GIB_Function_Execute (cbuf_t * cbuf, gib_function_t * func, const char ** args,
  206. unsigned int argc)
  207. {
  208. if (argc < func->minargs)
  209. return -1;
  210. GIB_Tree_Ref (&func->program);
  211. if (func->script)
  212. func->script->refs++;
  213. GIB_Buffer_Set_Program (cbuf, func->program);
  214. GIB_DATA (cbuf)->script = func->script;
  215. GIB_DATA (cbuf)->globals = func->globals;
  216. GIB_Function_Prepare_Args (cbuf, args, argc, func->arglist);
  217. return 0;
  218. }
  219. int
  220. GIB_Function_Execute_D (cbuf_t * cbuf, gib_function_t * func, dstring_t ** args,
  221. unsigned int argc)
  222. {
  223. if (argc < func->minargs)
  224. return -1;
  225. GIB_Tree_Ref (&func->program);
  226. if (func->script)
  227. func->script->refs++;
  228. GIB_Buffer_Set_Program (cbuf, func->program);
  229. GIB_DATA (cbuf)->script = func->script;
  230. GIB_DATA (cbuf)->globals = func->globals;
  231. GIB_Function_Prepare_Args_D (cbuf, args, argc, func->arglist);
  232. return 0;
  233. }