2001-05-25 20:31:47 +00:00
|
|
|
/*
|
|
|
|
gib_interpret.c
|
|
|
|
|
|
|
|
#DESCRIPTION#
|
|
|
|
|
|
|
|
Copyright (C) 2000 #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
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
*/
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
2001-05-09 05:41:34 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2001-05-25 16:09:14 +00:00
|
|
|
#include <stdio.h>
|
2001-02-19 21:15:25 +00:00
|
|
|
#include <stdlib.h>
|
2001-05-25 16:09:14 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2001-05-09 20:50:38 +00:00
|
|
|
#include "QF/gib.h"
|
2001-05-25 16:09:14 +00:00
|
|
|
|
2001-06-01 00:39:31 +00:00
|
|
|
#include "compat.h"
|
2001-05-25 16:09:14 +00:00
|
|
|
#include "gib_error.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
#include "gib_instructions.h"
|
|
|
|
#include "gib_interpret.h"
|
|
|
|
#include "gib_parse.h"
|
|
|
|
#include "gib_stack.h"
|
2001-05-25 16:09:14 +00:00
|
|
|
#include "gib_vars.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
char *gib_subargv[256];
|
|
|
|
int gib_subargc;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
char *
|
|
|
|
GIB_Argv (int i)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
return gib_instack[gib_insp - 1].argv[i];
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
GIB_Argc (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
return gib_instack[gib_insp - 1].argc;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
GIB_Strip_Arg (char *arg)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
if (arg[0] == '{' || arg[0] == '\'' || arg[0] == '\"') {
|
|
|
|
arg[strlen (arg) - 1] = 0;
|
|
|
|
memmove (arg, arg + 1, strlen (arg));
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
GIB_Execute_Block (char *block, int retflag)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int len, i, ret;
|
|
|
|
char *code;
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
i = 0;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
while ((len = GIB_Get_Inst (block + i)) > 0) {
|
|
|
|
code = malloc (len + 1);
|
|
|
|
strncpy (code, block + i, len);
|
2001-02-19 21:15:25 +00:00
|
|
|
code[len] = 0;
|
2001-02-26 06:48:02 +00:00
|
|
|
if ((ret = GIB_Interpret_Inst (code))) {
|
2001-02-19 21:15:25 +00:00
|
|
|
free (code);
|
|
|
|
return ret;
|
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
if ((ret = GIB_Execute_Inst ())) {
|
2001-02-19 21:15:25 +00:00
|
|
|
free (code);
|
|
|
|
if (ret == GIB_E_RETURN && retflag)
|
|
|
|
return 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
i += len + 1;
|
|
|
|
free (code);
|
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
if (len == -1)
|
2001-02-19 21:15:25 +00:00
|
|
|
return GIB_E_PARSE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
GIB_Execute_Inst (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int ret;
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
ret = gib_instack[gib_insp - 1].instruction->func ();
|
2001-02-26 06:48:02 +00:00
|
|
|
GIB_InStack_Pop ();
|
2001-02-19 21:15:25 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
GIB_Interpret_Inst (char *inst)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
char *buffer;
|
|
|
|
char *buffer2;
|
|
|
|
char *buffer3;
|
|
|
|
int i, n, len, ret, gib_argc;
|
|
|
|
char *gib_argv[256];
|
2001-02-19 21:15:25 +00:00
|
|
|
gib_inst_t *ginst;
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
buffer = malloc (strlen (inst) + 1);
|
2001-02-19 21:15:25 +00:00
|
|
|
i = 0;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
while (isspace (inst[i]))
|
2001-02-19 21:15:25 +00:00
|
|
|
i++;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
|
|
|
for (n = 0; i <= strlen (inst); i++) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (inst[i] == '\n' || inst[i] == '\t')
|
|
|
|
buffer[n] = ' ';
|
|
|
|
else
|
|
|
|
buffer[n] = inst[i];
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
buffer2 = malloc (2048);
|
|
|
|
buffer3 = malloc (2048);
|
2001-02-19 21:15:25 +00:00
|
|
|
ret = GIB_ExpandVars (buffer, buffer2, 2048);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = GIB_ExpandBackticks (buffer2, buffer3, 2048);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
gib_argc = 0;
|
2001-05-22 15:11:54 +00:00
|
|
|
for (i = 0; buffer3[i] != ' '; i++)
|
|
|
|
if (buffer3[i] == '\0')
|
|
|
|
return GIB_E_PARSE;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
gib_argv[0] = malloc (i + 1);
|
|
|
|
strncpy (gib_argv[0], buffer3, i);
|
2001-02-19 21:15:25 +00:00
|
|
|
gib_argv[0][i] = 0;
|
2001-02-26 06:48:02 +00:00
|
|
|
for (n = 0;; n++) {
|
|
|
|
for (; isspace (buffer3[i]); i++);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (buffer3[i] == 0)
|
|
|
|
break;
|
2001-02-26 06:48:02 +00:00
|
|
|
if ((len = GIB_Get_Arg (buffer3 + i)) < 0)
|
2001-02-19 21:15:25 +00:00
|
|
|
return GIB_E_PARSE;
|
2001-02-26 06:48:02 +00:00
|
|
|
else {
|
|
|
|
gib_argv[n + 1] = malloc (len + 1);
|
|
|
|
strncpy (gib_argv[n + 1], buffer3 + i, len);
|
2001-02-19 21:15:25 +00:00
|
|
|
gib_argv[n + 1][len] = 0;
|
|
|
|
GIB_ExpandEscapes (gib_argv[n + 1]);
|
|
|
|
i += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gib_argc = n;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
|
|
|
free (buffer);
|
|
|
|
free (buffer2);
|
|
|
|
free (buffer3);
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
for (i = 1; i <= n; i++)
|
|
|
|
GIB_Strip_Arg (gib_argv[i]);
|
2001-02-26 06:48:02 +00:00
|
|
|
if (!(ginst = GIB_Find_Instruction (gib_argv[0])))
|
2001-02-19 21:15:25 +00:00
|
|
|
return GIB_E_ILLINST;
|
2001-02-26 06:48:02 +00:00
|
|
|
GIB_InStack_Push (ginst, gib_argc, gib_argv);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
for (i = 0; i <= n; i++)
|
2001-02-26 06:48:02 +00:00
|
|
|
free (gib_argv[i]);
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
GIB_Run_Sub (gib_module_t * mod, gib_sub_t * sub)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int ret, i;
|
|
|
|
char buf[256];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
GIB_SubStack_Push (mod, sub, 0);
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
for (i = 0; i <= gib_subargc; i++) {
|
|
|
|
snprintf (buf, sizeof (buf), "arg%i", i);
|
2001-02-19 21:15:25 +00:00
|
|
|
GIB_Var_Set (buf, gib_subargv[i]);
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
snprintf (buf, sizeof (buf), "%i", gib_subargc);
|
2001-02-19 21:15:25 +00:00
|
|
|
GIB_Var_Set ("argc", buf);
|
|
|
|
|
|
|
|
ret = GIB_Execute_Sub ();
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
if (GIB_LOCALS)
|
2001-02-26 06:48:02 +00:00
|
|
|
GIB_Var_FreeAll (GIB_LOCALS);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
GIB_SubStack_Pop ();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
GIB_Execute_Sub (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
return GIB_Execute_Block (GIB_CURRENTSUB->code, 1);
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
GIB_Run_Inst (char *inst)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int ret;
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
ret = GIB_Interpret_Inst (inst);
|
2001-02-26 06:48:02 +00:00
|
|
|
if (ret) {
|
2001-02-19 21:15:25 +00:00
|
|
|
GIB_InStack_Pop ();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return GIB_Execute_Inst ();
|
|
|
|
}
|