2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "cvar.h"
|
|
|
|
#include "console.h"
|
|
|
|
#include "qargs.h"
|
|
|
|
#include "cmd.h"
|
|
|
|
#include "zone.h"
|
|
|
|
#include "quakefs.h"
|
|
|
|
#include "gib.h"
|
|
|
|
#include "gib_instructions.h"
|
|
|
|
#include "gib_interpret.h"
|
|
|
|
#include "gib_modules.h"
|
|
|
|
#include "gib_parse.h"
|
|
|
|
#include "gib_vars.h"
|
|
|
|
#include "gib_error.h"
|
|
|
|
#include "gib_stack.h"
|
|
|
|
|
|
|
|
static gib_inst_t *gibinstructions;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
char *gib_subret;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
GIB_AddInstruction (char *name, gib_func_t func)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
gib_inst_t *new;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
|
|
|
new = malloc (sizeof (gib_inst_t));
|
|
|
|
new->name = malloc (strlen (name) + 1);
|
2001-02-19 21:15:25 +00:00
|
|
|
new->func = func;
|
2001-02-26 06:48:02 +00:00
|
|
|
strcpy (new->name, name);
|
2001-02-19 21:15:25 +00:00
|
|
|
new->next = gibinstructions;
|
|
|
|
gibinstructions = new;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
gib_inst_t *
|
|
|
|
GIB_Find_Instruction (char *name)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
gib_inst_t *inst;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!(gibinstructions))
|
|
|
|
return 0;
|
2001-02-26 06:48:02 +00:00
|
|
|
for (inst = gibinstructions; strcmp (inst->name, name); inst = inst->next)
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!(inst->next))
|
|
|
|
return 0;
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
GIB_Init_Instructions (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
GIB_AddInstruction ("echo", GIB_Echo_f);
|
|
|
|
GIB_AddInstruction ("call", GIB_Call_f);
|
|
|
|
GIB_AddInstruction ("return", GIB_Return_f);
|
|
|
|
GIB_AddInstruction ("con", GIB_Con_f);
|
|
|
|
GIB_AddInstruction ("listfetch", GIB_ListFetch_f);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
GIB_Echo_f (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
Con_Printf (GIB_Argv (1));
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
GIB_Call_f (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
gib_module_t *mod;
|
2001-02-26 06:48:02 +00:00
|
|
|
gib_sub_t *sub;
|
|
|
|
int i, ret;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
mod = GIB_Get_ModSub_Mod (GIB_Argv (1));
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!mod)
|
|
|
|
return GIB_E_NOSUB;
|
2001-02-26 06:48:02 +00:00
|
|
|
sub = GIB_Get_ModSub_Sub (GIB_Argv (1));
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!sub)
|
|
|
|
return GIB_E_NOSUB;
|
2001-02-26 06:48:02 +00:00
|
|
|
gib_subargc = GIB_Argc () - 1;
|
2001-02-19 21:15:25 +00:00
|
|
|
gib_subargv[0] = sub->name;
|
|
|
|
for (i = 1; i <= gib_subargc; i++)
|
2001-02-26 06:48:02 +00:00
|
|
|
gib_subargv[i] = GIB_Argv (i + 1);
|
2001-02-19 21:15:25 +00:00
|
|
|
ret = GIB_Run_Sub (mod, sub);
|
2001-02-26 06:48:02 +00:00
|
|
|
if (gib_subret) {
|
|
|
|
GIB_Var_Set ("retval", gib_subret);
|
2001-02-19 21:15:25 +00:00
|
|
|
free (gib_subret);
|
|
|
|
}
|
|
|
|
gib_subret = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
GIB_VarPrint_f (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
gib_var_t *var;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 1; i <= GIB_Argc (); i++) {
|
|
|
|
var = GIB_Var_FindLocal (GIB_Argv (i));
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!var)
|
|
|
|
return GIB_E_NOVAR;
|
2001-02-26 06:48:02 +00:00
|
|
|
Con_Printf ("%s", var->value);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
Con_Printf ("\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
GIB_Return_f (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
if (GIB_Argc () != 1)
|
2001-02-19 21:15:25 +00:00
|
|
|
return GIB_E_NUMARGS;
|
2001-02-26 06:48:02 +00:00
|
|
|
gib_subret = malloc (strlen (GIB_Argv (1)) + 1);
|
|
|
|
strcpy (gib_subret, GIB_Argv (1));
|
|
|
|
return GIB_E_RETURN; // Signal to block executor to return
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// immediately
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
GIB_ListFetch_f (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
char *element;
|
|
|
|
gib_var_t *var;
|
|
|
|
int i, n, m;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (GIB_Argc () != 2)
|
2001-02-19 21:15:25 +00:00
|
|
|
return GIB_E_NUMARGS;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
|
|
|
if (!(var = GIB_Var_FindLocal (GIB_Argv (1))))
|
2001-02-19 21:15:25 +00:00
|
|
|
return GIB_E_NOVAR;
|
2001-02-26 06:48:02 +00:00
|
|
|
for (i = 0; isspace (var->value[i]); i++);
|
|
|
|
for (n = 1; n < atoi (GIB_Argv (2)); n++) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if ((m = GIB_Get_Arg (var->value + i)) < 1)
|
|
|
|
return GIB_E_ULIMIT;
|
|
|
|
i += m;
|
2001-02-26 06:48:02 +00:00
|
|
|
for (; isspace (var->value[i]); i++);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
if ((m = GIB_Get_Arg (var->value + i)) < 1)
|
2001-02-26 06:48:02 +00:00
|
|
|
return GIB_E_ULIMIT;
|
|
|
|
element = malloc (m + 1);
|
|
|
|
strncpy (element, var->value + i, m);
|
2001-02-19 21:15:25 +00:00
|
|
|
element[m] = 0;
|
|
|
|
GIB_Var_Set ("retval", element);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
GIB_Con_f (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
if (GIB_Argc () != 1)
|
2001-02-19 21:15:25 +00:00
|
|
|
return GIB_E_NUMARGS;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
|
|
|
Cmd_ExecuteString (GIB_Argv (1), src_command);
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
GIB_ExpandVars (char *source, char *buffer, int buffersize)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int i, n, m;
|
|
|
|
|
|
|
|
char varname[256];
|
|
|
|
gib_var_t *var;
|
|
|
|
|
|
|
|
for (i = 0, n = 0; i <= strlen (source); i++) {
|
|
|
|
if (source[i] == '$') {
|
2001-02-19 21:15:25 +00:00
|
|
|
m = 0;
|
2001-02-26 06:48:02 +00:00
|
|
|
while (isalnum (source[++i]))
|
|
|
|
varname[m++] = source[i];
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
varname[m++] = 0;
|
|
|
|
if (!(var = GIB_Var_FindLocal (varname)))
|
|
|
|
return GIB_E_NOVAR;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (n + strlen (var->value) >= buffersize)
|
2001-02-19 21:15:25 +00:00
|
|
|
return GIB_E_BUFFER;
|
2001-02-26 06:48:02 +00:00
|
|
|
memcpy (buffer + n, var->value, strlen (var->value));
|
|
|
|
|
|
|
|
n += strlen (var->value);
|
2001-02-19 21:15:25 +00:00
|
|
|
i--;
|
2001-02-26 06:48:02 +00:00
|
|
|
} else {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (n >= buffersize + 1)
|
|
|
|
return GIB_E_BUFFER;
|
|
|
|
buffer[n++] = source[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
GIB_ExpandBackticks (char *source, char *buffer, int buffersize)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int i, n, m, ret;
|
|
|
|
|
|
|
|
char tick[256];
|
|
|
|
gib_var_t *var;
|
|
|
|
|
|
|
|
for (i = 0, n = 0; i <= strlen (source); i++) {
|
|
|
|
if (source[i] == '`') {
|
2001-02-19 21:15:25 +00:00
|
|
|
m = 0;
|
2001-02-26 06:48:02 +00:00
|
|
|
while (source[++i] != '`')
|
|
|
|
tick[m++] = source[i];
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
tick[m++] = 0;
|
|
|
|
ret = GIB_Run_Inst (tick);
|
2001-02-26 06:48:02 +00:00
|
|
|
if (ret) {
|
2001-02-19 21:15:25 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (!(var = GIB_Var_FindLocal ("retval")))
|
|
|
|
return GIB_E_NOVAR;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (n + strlen (var->value) >= buffersize)
|
2001-02-19 21:15:25 +00:00
|
|
|
return GIB_E_BUFFER;
|
2001-02-26 06:48:02 +00:00
|
|
|
memcpy (buffer + n, var->value, strlen (var->value));
|
|
|
|
|
|
|
|
n += strlen (var->value);
|
|
|
|
} else {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (n >= buffersize + 1)
|
|
|
|
return GIB_E_BUFFER;
|
|
|
|
buffer[n++] = source[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2001-02-22 04:46:59 +00:00
|
|
|
}
|