2003-01-28 21:16:21 +00:00
|
|
|
/*
|
|
|
|
gib_execute.c
|
|
|
|
|
|
|
|
GIB runtime execution functions
|
|
|
|
|
|
|
|
Copyright (C) 2002 Brian Koropoff
|
|
|
|
|
|
|
|
Author: Brian Koropoff
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used))
|
2003-02-14 22:42:11 +00:00
|
|
|
const char rcsid[] =
|
|
|
|
"$Id$";
|
2003-01-28 21:16:21 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "QF/cbuf.h"
|
|
|
|
#include "QF/cmd.h"
|
2003-04-13 22:07:58 +00:00
|
|
|
#include "QF/gib.h"
|
|
|
|
|
|
|
|
#include "gib_buffer.h"
|
|
|
|
#include "gib_builtin.h"
|
|
|
|
#include "gib_function.h"
|
|
|
|
#include "gib_vars.h"
|
|
|
|
#include "gib_tree.h"
|
|
|
|
#include "gib_process.h"
|
|
|
|
#include "gib_execute.h"
|
2003-01-28 21:16:21 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
GIB_Execute_Generate_Composite (struct cbuf_s *cbuf)
|
|
|
|
{
|
|
|
|
cbuf_args_t *args = cbuf->args;
|
2003-02-14 22:42:11 +00:00
|
|
|
int i;
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
dstring_clearstr (GIB_DATA (cbuf)->arg_composite);
|
|
|
|
for (i = 0; i < args->argc; i++) {
|
|
|
|
// ->str could be moved by realloc when a dstring is resized
|
|
|
|
// so save the offset instead of the pointer
|
2003-04-11 02:57:11 +00:00
|
|
|
args->args[i] = (const char *) strlen (GIB_DATA (cbuf)->arg_composite->str);
|
2003-01-28 21:16:21 +00:00
|
|
|
dstring_appendstr (GIB_DATA (cbuf)->arg_composite, args->argv[i]->str);
|
|
|
|
dstring_appendstr (GIB_DATA (cbuf)->arg_composite, " ");
|
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
// Get rid of trailing space
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_DATA (cbuf)->arg_composite->
|
|
|
|
str[strlen (GIB_DATA (cbuf)->arg_composite->str) - 1] = 0;
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
for (i = 0; i < args->argc; i++)
|
|
|
|
// now that arg_composite is done we can add the pointer to the stored
|
|
|
|
// offsets and get valid pointers. This *should* be portable.
|
2007-04-04 07:48:14 +00:00
|
|
|
args->args[i] += (intptr_t) GIB_DATA (cbuf)->arg_composite->str;
|
2003-02-14 22:42:11 +00:00
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
|
|
|
|
static void
|
2003-02-16 19:46:34 +00:00
|
|
|
GIB_Execute_Split_Var (cbuf_t * cbuf)
|
2003-01-28 21:16:21 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
gib_var_t *var;
|
2003-01-28 21:16:21 +00:00
|
|
|
unsigned int i;
|
2003-04-17 00:01:48 +00:00
|
|
|
unsigned int start = 0, end = ((unsigned int) ~0 >> 1);
|
2003-02-14 22:42:11 +00:00
|
|
|
char *c, *str = cbuf->args->argv[cbuf->args->argc - 1]->str + 1;
|
|
|
|
void *m = cbuf->args->argm[cbuf->args->argc - 1];
|
2003-01-28 21:16:21 +00:00
|
|
|
|
2003-02-25 08:04:48 +00:00
|
|
|
i = strlen (str);
|
|
|
|
if (i)
|
|
|
|
i--;
|
2003-02-16 19:46:34 +00:00
|
|
|
if (str[-1] == '@') {
|
|
|
|
if (str[i] == ']')
|
|
|
|
for (; i; i--)
|
|
|
|
if (str[i] == '[') {
|
|
|
|
str[i] = 0;
|
|
|
|
start = atoi (str + i + 1);
|
|
|
|
if ((c = strchr (str + i + 1, ':'))) {
|
|
|
|
if (c[1] != ']')
|
|
|
|
end = atoi (c + 1);
|
|
|
|
} else
|
|
|
|
end = start + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cbuf->args->argc--;
|
2003-04-11 02:57:11 +00:00
|
|
|
if (!(var = GIB_Var_Get_Complex (
|
|
|
|
&GIB_DATA (cbuf)->locals,
|
|
|
|
&GIB_DATA (cbuf)->globals,
|
|
|
|
str, &i, false)))
|
|
|
|
return;
|
2003-02-16 19:46:34 +00:00
|
|
|
if (end < 0)
|
|
|
|
end += var->size;
|
|
|
|
else if (end > var->size)
|
|
|
|
end = var->size;
|
|
|
|
if (start < 0) {
|
|
|
|
start += var->size;
|
|
|
|
if (start < 0)
|
|
|
|
start = 0;
|
|
|
|
} else if (start >= var->size || start >= end)
|
|
|
|
return;
|
|
|
|
for (i = start; i < end; i++) {
|
|
|
|
if (var->array[i].value)
|
|
|
|
Cbuf_ArgsAdd (cbuf->args, var->array[i].value->str);
|
|
|
|
else
|
|
|
|
Cbuf_ArgsAdd (cbuf->args, "");
|
|
|
|
cbuf->args->argm[cbuf->args->argc - 1] = m;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gib_var_t **vlist, **v;
|
2003-04-11 02:57:11 +00:00
|
|
|
|
2003-02-16 19:46:34 +00:00
|
|
|
cbuf->args->argc--;
|
2003-04-11 02:57:11 +00:00
|
|
|
if (!(var = GIB_Var_Get_Complex (
|
|
|
|
&GIB_DATA (cbuf)->locals,
|
|
|
|
&GIB_DATA (cbuf)->globals,
|
|
|
|
str, &i, false)))
|
|
|
|
return;
|
2003-02-16 19:46:34 +00:00
|
|
|
if (!var->array[i].leaves)
|
|
|
|
return;
|
|
|
|
vlist = (gib_var_t **) Hash_GetList (var->array[i].leaves);
|
|
|
|
for (v = vlist; *v; v++)
|
|
|
|
Cbuf_ArgsAdd (cbuf->args, (*v)->key);
|
2003-04-11 02:57:11 +00:00
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Execute_Prepare_Line (cbuf_t * cbuf, gib_tree_t * line)
|
2003-01-28 21:16:21 +00:00
|
|
|
{
|
|
|
|
gib_tree_t *cur;
|
|
|
|
cbuf_args_t *args = cbuf->args;
|
|
|
|
unsigned int pos;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
args->argc = 0;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
for (cur = line->children; cur; cur = cur->next) {
|
2003-02-25 06:52:27 +00:00
|
|
|
if (cur->flags & TREE_A_CONCAT) {
|
2003-02-14 22:42:11 +00:00
|
|
|
pos = args->argv[args->argc - 1]->size - 1;
|
2003-02-25 06:52:27 +00:00
|
|
|
if (cur->flags & TREE_A_EMBED) {
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Process_Embedded (cur, cbuf->args);
|
|
|
|
} else
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_appendstr (args->argv[args->argc - 1], cur->str);
|
2003-01-28 21:16:21 +00:00
|
|
|
} else {
|
|
|
|
pos = 0;
|
2003-02-25 06:52:27 +00:00
|
|
|
if (cur->flags & TREE_A_EMBED) {
|
2003-01-28 21:16:21 +00:00
|
|
|
Cbuf_ArgsAdd (args, "");
|
|
|
|
GIB_Process_Embedded (cur, cbuf->args);
|
2003-09-11 08:51:44 +00:00
|
|
|
} else {
|
2003-01-28 21:16:21 +00:00
|
|
|
Cbuf_ArgsAdd (args, cur->str);
|
2003-09-11 08:51:44 +00:00
|
|
|
args->argm[args->argc - 1] = cur;
|
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
if (cur->delim == '('
|
|
|
|
&& GIB_Process_Math (args->argv[args->argc - 1], pos))
|
2003-01-28 21:16:21 +00:00
|
|
|
return -1;
|
2003-02-25 06:52:27 +00:00
|
|
|
if (cur->flags & TREE_A_EXPAND)
|
2003-02-16 19:46:34 +00:00
|
|
|
GIB_Execute_Split_Var (cbuf);
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-04-13 22:07:58 +00:00
|
|
|
static int
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Execute_For_Next (cbuf_t * cbuf)
|
2003-01-28 21:16:21 +00:00
|
|
|
{
|
|
|
|
unsigned int index;
|
2003-02-14 22:42:11 +00:00
|
|
|
gib_var_t *var;
|
2003-04-11 02:57:11 +00:00
|
|
|
struct gib_dsarray_s *array = GIB_DATA (cbuf)->stack.values + GIB_DATA (cbuf)->stack.p - 1;
|
2003-01-28 21:16:21 +00:00
|
|
|
if (array->size == 1) {
|
|
|
|
GIB_Buffer_Pop_Sstack (cbuf);
|
2003-02-25 06:52:27 +00:00
|
|
|
return -1;
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
|
|
|
array->size--;
|
2003-04-11 02:57:11 +00:00
|
|
|
var = GIB_Var_Get_Complex (
|
|
|
|
&GIB_DATA (cbuf)->locals,
|
|
|
|
&GIB_DATA (cbuf)->globals, array->dstrs[0]->str,
|
|
|
|
&index, true
|
|
|
|
);
|
2003-02-16 02:44:24 +00:00
|
|
|
dstring_clearstr (var->array[index].value);
|
|
|
|
dstring_appendstr (var->array[index].value, array->dstrs[array->size]->str);
|
2003-02-25 06:52:27 +00:00
|
|
|
return 0;
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Execute (cbuf_t * cbuf)
|
2003-01-28 21:16:21 +00:00
|
|
|
{
|
|
|
|
gib_buffer_data_t *g = GIB_DATA (cbuf);
|
|
|
|
gib_builtin_t *b;
|
|
|
|
gib_function_t *f;
|
2003-09-11 06:03:13 +00:00
|
|
|
gib_object_t *obj;
|
2003-02-25 06:52:27 +00:00
|
|
|
unsigned int index;
|
|
|
|
gib_var_t *var;
|
2003-04-17 00:01:48 +00:00
|
|
|
int i;
|
2003-09-11 06:03:13 +00:00
|
|
|
qboolean super;
|
|
|
|
|
|
|
|
static const char **mesg = NULL;
|
|
|
|
static int maxmesg = 0;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-02-26 07:44:34 +00:00
|
|
|
if (!g->program)
|
|
|
|
return;
|
2003-02-25 06:52:27 +00:00
|
|
|
g->ip = g->ip ? g->ip->next : g->program;
|
|
|
|
while (g->ip) {
|
|
|
|
switch (g->ip->type) {
|
2003-09-11 06:03:13 +00:00
|
|
|
case TREE_T_LABEL: // Move to next instruction
|
2003-04-11 02:57:11 +00:00
|
|
|
g->ip = g->ip->next;
|
2003-01-28 21:16:21 +00:00
|
|
|
continue;
|
2003-04-11 02:57:11 +00:00
|
|
|
case TREE_T_JUMP: // Absolute jump
|
|
|
|
g->ip = g->ip->jump;
|
2003-02-25 06:52:27 +00:00
|
|
|
continue;
|
2003-04-11 02:57:11 +00:00
|
|
|
case TREE_T_FORNEXT: // Fetch next value in a for loop
|
2003-02-25 06:52:27 +00:00
|
|
|
if (GIB_Execute_For_Next (cbuf))
|
|
|
|
g->ip = g->ip->jump->next;
|
|
|
|
else
|
|
|
|
g->ip = g->ip->next;
|
|
|
|
continue;
|
2003-04-11 02:57:11 +00:00
|
|
|
case TREE_T_COND: // Conditional jump, move to next instruction
|
2003-02-25 06:52:27 +00:00
|
|
|
if (GIB_Execute_Prepare_Line (cbuf, g->ip))
|
|
|
|
return;
|
2003-04-11 02:57:11 +00:00
|
|
|
if (g->ip->flags & TREE_L_NOT ?
|
|
|
|
atof (cbuf->args->argv[1]->str) :
|
|
|
|
!atof (cbuf->args->argv[1]->str))
|
|
|
|
g->ip = g->ip->jump->next;
|
2003-02-25 06:52:27 +00:00
|
|
|
else
|
|
|
|
g->ip = g->ip->next;
|
|
|
|
continue;
|
2003-04-11 02:57:11 +00:00
|
|
|
case TREE_T_ASSIGN: // Assignment
|
2003-02-25 06:52:27 +00:00
|
|
|
if (GIB_Execute_Prepare_Line (cbuf, g->ip))
|
|
|
|
return;
|
2003-04-11 02:57:11 +00:00
|
|
|
var = GIB_Var_Get_Complex (
|
|
|
|
&g->locals,
|
|
|
|
&g->globals,
|
|
|
|
cbuf->args->argv[0]->str, &index, true);
|
2003-05-14 21:13:41 +00:00
|
|
|
GIB_Var_Assign (var, index, cbuf->args->argv + 2, cbuf->args->argc - 2, cbuf->args->argv[0]->str[strlen (cbuf->args->argv[0]->str) - 1] != ']');
|
2003-04-11 02:57:11 +00:00
|
|
|
if (g->ip->flags & TREE_L_EMBED) {
|
|
|
|
GIB_Buffer_Push_Sstack (cbuf);
|
|
|
|
g->waitret = true;
|
2003-09-11 06:03:13 +00:00
|
|
|
if (GIB_CanReturn ())
|
|
|
|
for (i = 2; i < cbuf->args->argc; i++)
|
|
|
|
GIB_Return (cbuf->args->argv[i]->str);
|
2003-04-11 02:57:11 +00:00
|
|
|
} else
|
|
|
|
g->waitret = false;
|
2003-02-25 06:52:27 +00:00
|
|
|
g->ip = g->ip->next;
|
|
|
|
continue;
|
2003-09-11 06:03:13 +00:00
|
|
|
case TREE_T_SEND: // Message sending
|
|
|
|
if (GIB_Execute_Prepare_Line (cbuf, g->ip))
|
|
|
|
return;
|
|
|
|
if (cbuf->args->argc - 2 > maxmesg) {
|
|
|
|
maxmesg += 32;
|
2007-03-22 23:20:57 +00:00
|
|
|
mesg = realloc ((void*)mesg, sizeof (char *) * maxmesg);
|
2003-09-11 06:03:13 +00:00
|
|
|
}
|
|
|
|
for (i = 2; i < cbuf->args->argc; i++)
|
|
|
|
mesg[i-2] = cbuf->args->argv[i]->str;
|
|
|
|
|
|
|
|
super = false;
|
|
|
|
if (!strcmp (cbuf->args->argv[0]->str,
|
|
|
|
"super")) {
|
|
|
|
if (!(obj = g->reply.obj)) {
|
|
|
|
GIB_Error (
|
|
|
|
"send",
|
|
|
|
"Sending "
|
|
|
|
"message to "
|
|
|
|
"super not "
|
|
|
|
"possible in "
|
|
|
|
"this context."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
super = true;
|
|
|
|
} else if (!(obj = GIB_Object_Get (cbuf->args->argv[0]->str))) {
|
|
|
|
GIB_Error (
|
|
|
|
"send",
|
|
|
|
"No such object or class: %s",
|
|
|
|
cbuf->args->argv[0]->str
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (g->ip->flags & TREE_L_EMBED) {
|
|
|
|
// Get ready for return values
|
|
|
|
g->waitret = true;
|
|
|
|
GIB_Buffer_Push_Sstack (cbuf);
|
|
|
|
cbuf->state = CBUF_STATE_BLOCKED;
|
|
|
|
i = super ? GIB_SendToMethod (obj,
|
|
|
|
g->reply.method->parent,
|
2003-09-11 08:51:44 +00:00
|
|
|
g->reply.obj, i - 2, mesg,
|
2003-09-11 06:03:13 +00:00
|
|
|
GIB_Buffer_Reply_Callback,
|
|
|
|
cbuf) : GIB_Send (obj,
|
2003-09-11 08:51:44 +00:00
|
|
|
g->reply.obj, i - 2, mesg,
|
2003-09-11 06:03:13 +00:00
|
|
|
GIB_Buffer_Reply_Callback,
|
|
|
|
cbuf);
|
|
|
|
} else {
|
|
|
|
g->waitret = false;
|
|
|
|
i = super ? GIB_SendToMethod (obj,
|
|
|
|
g->reply.method->parent,
|
2003-09-11 08:51:44 +00:00
|
|
|
g->reply.obj, i - 2, mesg,
|
2003-09-11 06:03:13 +00:00
|
|
|
NULL, NULL) :
|
2003-09-11 08:51:44 +00:00
|
|
|
GIB_Send (obj,g->reply.obj,
|
|
|
|
i - 2, mesg, NULL,
|
|
|
|
NULL);
|
2003-09-11 06:03:13 +00:00
|
|
|
}
|
|
|
|
if (i < 0) {
|
|
|
|
GIB_Error (
|
|
|
|
"send",
|
|
|
|
"Object %s (%s) could not handle message %s",
|
|
|
|
cbuf->args->argv[0]->str,
|
|
|
|
obj->class->name,
|
|
|
|
cbuf->args->argv[2]->str
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
g->ip = g->ip->next;
|
|
|
|
continue;
|
2003-04-11 02:57:11 +00:00
|
|
|
case TREE_T_CMD: // Normal command
|
2003-02-25 06:52:27 +00:00
|
|
|
if (GIB_Execute_Prepare_Line (cbuf, g->ip))
|
|
|
|
return;
|
2003-02-28 05:07:15 +00:00
|
|
|
if (g->ip->flags & TREE_L_EMBED) {
|
|
|
|
// Get ready for return values
|
|
|
|
g->waitret = true;
|
|
|
|
GIB_Buffer_Push_Sstack (cbuf);
|
|
|
|
} else
|
|
|
|
g->waitret = false;
|
|
|
|
if (cbuf->args->argc) {
|
2003-02-25 06:52:27 +00:00
|
|
|
if ((b = GIB_Builtin_Find (cbuf->args->argv[0]->str)))
|
|
|
|
b->func ();
|
|
|
|
else if ((f = GIB_Function_Find (cbuf->args->argv[0]->str))) {
|
|
|
|
cbuf_t *new = Cbuf_PushStack (&gib_interp);
|
2003-10-19 00:51:47 +00:00
|
|
|
if (GIB_Function_Execute_D
|
|
|
|
(new, f,
|
|
|
|
cbuf->args->argv,
|
|
|
|
cbuf->args->argc))
|
|
|
|
GIB_Error ("syntax", "not "
|
|
|
|
"enough "
|
|
|
|
"arguments to "
|
|
|
|
"function '%s'",
|
|
|
|
cbuf->args->argv[0]->str);
|
2003-02-25 06:52:27 +00:00
|
|
|
} else {
|
|
|
|
GIB_Execute_Generate_Composite (cbuf);
|
|
|
|
if (Cmd_Command (cbuf->args))
|
|
|
|
GIB_Error (
|
2003-11-29 02:01:31 +00:00
|
|
|
"UnknownCommandError",
|
2003-04-11 02:57:11 +00:00
|
|
|
"No builtin, function, or console command "
|
|
|
|
"named '%s' was found.",
|
2003-02-25 06:52:27 +00:00
|
|
|
cbuf->args->argv[0]->str
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2003-09-11 06:03:13 +00:00
|
|
|
if (cbuf->state)
|
|
|
|
return;
|
2003-02-25 08:04:48 +00:00
|
|
|
g->ip = g->ip->next;
|
2003-02-25 06:52:27 +00:00
|
|
|
continue;
|
2003-04-11 02:57:11 +00:00
|
|
|
default: // We should never get here
|
|
|
|
GIB_Error (
|
|
|
|
"QUAKEFORGE-BUG-PLEASE-REPORT",
|
|
|
|
"Unknown instruction type; tastes like chicken."
|
|
|
|
);
|
2003-02-25 06:52:27 +00:00
|
|
|
return;
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
|
|
|
}
|
2003-02-26 07:44:34 +00:00
|
|
|
g->ip = 0;
|
|
|
|
GIB_Tree_Unref (&g->program);
|
2003-02-28 04:03:45 +00:00
|
|
|
g->program = 0;
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|