PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/quakeforge/trunk/libs/gib/gib_buffer.c

#
C | 338 lines | 267 code | 33 blank | 38 comment | 43 complexity | 75c97e8d25e71855c05c510df444bd54 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-3.0, AGPL-1.0, Unlicense
  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__ ((used))
  25. const char rcsid[] = "$Id: gib_buffer.c 11862 2010-01-13 06:42:26Z taniwha $";
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include "QF/sys.h"
  30. #include "QF/dstring.h"
  31. #include "QF/cbuf.h"
  32. #include "QF/hash.h"
  33. #include "QF/gib.h"
  34. #include "QF/idparse.h"
  35. #include "gib_tree.h"
  36. #include "gib_parse.h"
  37. #include "gib_vars.h"
  38. #include "gib_execute.h"
  39. #include "gib_buffer.h"
  40. static void
  41. GIB_Buffer_Construct (struct cbuf_s *cbuf)
  42. {
  43. cbuf->data = calloc (1, sizeof (gib_buffer_data_t));
  44. GIB_DATA (cbuf)->arg_composite = dstring_newstr ();
  45. GIB_DATA (cbuf)->globals = gib_globals;
  46. cbuf->strict = true;
  47. }
  48. static void
  49. GIB_Buffer_Destruct (struct cbuf_s *cbuf)
  50. {
  51. gib_buffer_data_t *g = GIB_DATA (cbuf);
  52. unsigned int i, j;
  53. if (g->dnotify)
  54. g->dnotify (cbuf, g->ddata);
  55. dstring_delete (g->arg_composite);
  56. if (g->locals)
  57. Hash_DelTable (g->locals);
  58. if (g->program)
  59. GIB_Tree_Unref (&g->program);
  60. if (g->script && !(--g->script->refs)) {
  61. free ((void *) g->script->text);
  62. free ((void *) g->script->file);
  63. free (g->script);
  64. }
  65. for (i = 0; i < g->stack.size; i++) {
  66. for (j = 0; j < g->stack.values[i].realsize; j++)
  67. dstring_delete (g->stack.values[i].dstrs[j]);
  68. if (g->stack.values[i].dstrs)
  69. free (g->stack.values[i].dstrs);
  70. }
  71. if (g->stack.values)
  72. free (g->stack.values);
  73. free (cbuf->data);
  74. }
  75. static void
  76. GIB_Buffer_Reset (struct cbuf_s *cbuf)
  77. {
  78. gib_buffer_data_t *g = GIB_DATA (cbuf);
  79. // Being reset is nearly the same as being destroyed.
  80. // It just means the buffer is going to be reused.
  81. if (g->dnotify)
  82. g->dnotify (cbuf, g->ddata);
  83. if (g->locals)
  84. Hash_FlushTable (g->locals);
  85. g->globals = gib_globals;
  86. if (g->program)
  87. GIB_Tree_Unref (&g->program);
  88. if (g->script && !(--g->script->refs)) {
  89. free ((void *) g->script->text);
  90. free ((void *) g->script->file);
  91. free (g->script);
  92. }
  93. g->script = 0;
  94. g->program = g->ip = 0;
  95. g->stack.p = 0;
  96. g->waitret = false;
  97. g->dnotify = NULL;
  98. g->reply.obj = NULL;
  99. }
  100. void
  101. GIB_Buffer_Set_Program (cbuf_t * cbuf, gib_tree_t * program)
  102. {
  103. GIB_DATA (cbuf)->program = program;
  104. }
  105. static unsigned int
  106. GIB_Buffer_Get_Line_Num (const char *program, unsigned int pos)
  107. {
  108. unsigned int i, line;
  109. for (i = 0, line = 1; i < pos; i++)
  110. if (program[i] == '\n')
  111. line++;
  112. return line;
  113. }
  114. static void
  115. GIB_Buffer_Add (cbuf_t * cbuf, const char *str)
  116. {
  117. gib_buffer_data_t *g = GIB_DATA (cbuf);
  118. gib_tree_t **save, *cur;
  119. // AddText should be used only to populate a buffer before
  120. // executing it and shouldn't happen to a running GIB buffer,
  121. // but if it does, try to find somewhere else to put the text.
  122. if (g->ip) {
  123. for (;cbuf; cbuf = cbuf->up)
  124. if (cbuf->interpreter == &id_interp) {
  125. Cbuf_AddText (cbuf, str);
  126. return;
  127. }
  128. Sys_Printf (
  129. "-------------\n"
  130. "|GIB Warning|\n"
  131. "-------------\n"
  132. "Text added to running GIB buffer discarded.\n"
  133. "Text: %s\n",
  134. str
  135. );
  136. return;
  137. } else if (g->program) {
  138. for (cur = g->program; cur->next; cur = cur->next);
  139. save = &cur->next;
  140. } else
  141. save = &g->program;
  142. if (!(*save = GIB_Parse_Lines (str, 0)))
  143. Sys_Printf (
  144. "-----------------\n"
  145. "|GIB Parse Error|\n"
  146. "-----------------\n"
  147. "Parse error while adding text to GIB buffer.\n"
  148. "Line %u: %s\n",
  149. GIB_Buffer_Get_Line_Num (str, GIB_Parse_ErrorPos ()),
  150. GIB_Parse_ErrorMsg ()
  151. );
  152. }
  153. static void
  154. GIB_Buffer_Insert (cbuf_t * cbuf, const char *str)
  155. {
  156. gib_buffer_data_t *g = GIB_DATA (cbuf);
  157. gib_tree_t *lines, *cur;
  158. // No GIB builtin should ever insert text into a running
  159. // GIB buffer, so create an idparse buffer to handle the
  160. // legacy console code.
  161. if (g->ip) {
  162. cbuf_t *new = Cbuf_New (&id_interp);
  163. new->up = cbuf;
  164. cbuf->down = new;
  165. cbuf->state = CBUF_STATE_STACK;
  166. Cbuf_InsertText (new, str);
  167. return;
  168. } else if ((lines = GIB_Parse_Lines (str, 0))) {
  169. for (cur = lines; cur; cur = cur->next);
  170. cur->next = g->program;
  171. g->program = lines;
  172. } else
  173. Sys_Printf (
  174. "-----------------\n"
  175. "|GIB Parse Error|\n"
  176. "-----------------\n"
  177. "Parse error while inserting text into GIB buffer.\n"
  178. "Line %u: %s\n",
  179. GIB_Buffer_Get_Line_Num (str, GIB_Parse_ErrorPos ()),
  180. GIB_Parse_ErrorMsg ()
  181. );
  182. }
  183. void
  184. GIB_Buffer_Push_Sstack (struct cbuf_s *cbuf)
  185. {
  186. gib_buffer_data_t *g = GIB_DATA (cbuf);
  187. if (++g->stack.p > g->stack.size) {
  188. g->stack.values = realloc (g->stack.values,
  189. sizeof (struct gib_dsarray_s) * g->stack.p);
  190. g->stack.values[g->stack.p - 1].dstrs = 0;
  191. g->stack.values[g->stack.p - 1].size =
  192. g->stack.values[g->stack.p - 1].realsize = 0;
  193. g->stack.size = g->stack.p;
  194. }
  195. g->stack.values[g->stack.p - 1].size = 0;
  196. }
  197. void
  198. GIB_Buffer_Pop_Sstack (struct cbuf_s *cbuf)
  199. {
  200. GIB_DATA (cbuf)->stack.p--;
  201. }
  202. dstring_t *
  203. GIB_Buffer_Dsarray_Get (struct cbuf_s *cbuf)
  204. {
  205. struct gib_dsarray_s *vals =
  206. GIB_DATA (cbuf)->stack.values + GIB_DATA (cbuf)->stack.p - 1;
  207. if (++vals->size > vals->realsize) {
  208. vals->dstrs = realloc (vals->dstrs, sizeof (dstring_t *) * vals->size);
  209. vals->dstrs[vals->size - 1] = dstring_newstr ();
  210. vals->realsize = vals->size;
  211. } else
  212. dstring_clearstr (vals->dstrs[vals->size - 1]);
  213. return vals->dstrs[vals->size - 1];
  214. }
  215. static int
  216. GIB_Buffer_Get_Line_Info (cbuf_t * cbuf, char **line)
  217. {
  218. const char *text;
  219. unsigned int ofs, i, start, linenum;
  220. // Do we have a copy of the original program this buffer comes from?
  221. if (GIB_DATA (cbuf)->script) {
  222. text = GIB_DATA (cbuf)->script->text;
  223. for (ofs = GIB_DATA (cbuf)->ip->start, start = 0, i = 0, linenum = 1;
  224. i <= ofs; i++)
  225. if (text[i] == '\n') {
  226. start = i + 1;
  227. linenum++;
  228. }
  229. while (text[i] != '\n')
  230. i++;
  231. *line = malloc (i - start + 1);
  232. memcpy (*line, text + start, i - start);
  233. (*line)[i - start] = 0;
  234. return linenum;
  235. } else {
  236. *line = strdup (GIB_DATA (cbuf)->ip->str);
  237. return -1;
  238. }
  239. }
  240. void
  241. GIB_Buffer_Reply_Callback (int argc, const char **argv, void *data)
  242. {
  243. cbuf_t *cbuf = (cbuf_t *) data;
  244. int i;
  245. for (i = 0; i < argc; i++)
  246. dstring_copystr (GIB_Buffer_Dsarray_Get (cbuf), argv[i]);
  247. if (cbuf->state == CBUF_STATE_BLOCKED)
  248. cbuf->state = CBUF_STATE_NORMAL;
  249. }
  250. void
  251. GIB_Buffer_Error (cbuf_t * cbuf, const char *type, const char *fmt,
  252. va_list args)
  253. {
  254. char *line;
  255. int linenum;
  256. dstring_t *message = dstring_newstr ();
  257. dvsprintf (message, fmt, args);
  258. va_end (args);
  259. Sys_Printf (
  260. "---------------------\n"
  261. "|GIB Execution Error|\n"
  262. "---------------------\n"
  263. "Type: %s\n",
  264. type
  265. );
  266. if ((linenum = GIB_Buffer_Get_Line_Info (cbuf, &line)) != -1)
  267. Sys_Printf (
  268. "%s:%i: %s\n"
  269. "-->%s\n",
  270. GIB_DATA (cbuf)->script->file, linenum, message->str, line
  271. );
  272. else
  273. Sys_Printf (
  274. "%s\n"
  275. "-->%s\n",
  276. message->str,
  277. line
  278. );
  279. cbuf->state = CBUF_STATE_ERROR;
  280. dstring_delete (message);
  281. free (line);
  282. }
  283. cbuf_interpreter_t gib_interp = {
  284. GIB_Buffer_Construct,
  285. GIB_Buffer_Destruct,
  286. GIB_Buffer_Reset,
  287. GIB_Buffer_Add,
  288. GIB_Buffer_Insert,
  289. GIB_Execute,
  290. GIB_Execute,
  291. };
  292. VISIBLE cbuf_interpreter_t *
  293. GIB_Interpreter (void)
  294. {
  295. return &gib_interp;
  296. }