2002-07-30 17:08:53 +00:00
|
|
|
/*
|
|
|
|
#FILENAME#
|
|
|
|
|
|
|
|
#DESCRIPTION#
|
|
|
|
|
|
|
|
Copyright (C) 2002 #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
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2002-07-30 17:08:53 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2002-07-31 22:03:53 +00:00
|
|
|
|
2002-07-30 17:08:53 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2002-08-02 04:07:25 +00:00
|
|
|
#include "QF/sys.h"
|
2002-07-30 17:08:53 +00:00
|
|
|
#include "QF/cbuf.h"
|
2002-07-31 05:19:03 +00:00
|
|
|
#include "QF/cmd.h"
|
2002-07-30 17:08:53 +00:00
|
|
|
#include "QF/dstring.h"
|
|
|
|
#include "QF/qtypes.h"
|
|
|
|
|
|
|
|
#include "compat.h"
|
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
cbuf_t *cbuf_active;
|
2002-07-30 17:08:53 +00:00
|
|
|
|
|
|
|
cbuf_args_t *
|
|
|
|
Cbuf_ArgsNew (void)
|
|
|
|
{
|
|
|
|
return calloc (1, sizeof (cbuf_args_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cbuf_ArgsDelete (cbuf_args_t *args)
|
|
|
|
{
|
2002-08-25 04:47:57 +00:00
|
|
|
int i;
|
2002-07-30 17:08:53 +00:00
|
|
|
|
|
|
|
for (i = 0; i < args->argv_size; i++)
|
|
|
|
dstring_delete (args->argv[i]);
|
|
|
|
free (args->argv);
|
|
|
|
free (args->args);
|
|
|
|
free (args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cbuf_ArgsAdd (cbuf_args_t *args, const char *arg)
|
|
|
|
{
|
2002-08-25 04:47:57 +00:00
|
|
|
int i;
|
2002-07-30 17:08:53 +00:00
|
|
|
|
|
|
|
if (args->argc == args->argv_size) {
|
|
|
|
args->argv_size += 4;
|
|
|
|
args->argv = realloc (args->argv,
|
|
|
|
args->argv_size * sizeof (dstring_t *));
|
|
|
|
args->args = realloc (args->args, args->argv_size * sizeof (char *));
|
2003-01-28 21:16:21 +00:00
|
|
|
args->argm = realloc (args->argm, args->argv_size * sizeof (void *));
|
2002-07-30 17:08:53 +00:00
|
|
|
for (i = args->argv_size - 4; i < args->argv_size; i++) {
|
|
|
|
args->argv[i] = dstring_newstr ();
|
|
|
|
args->args[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dstring_clearstr (args->argv[args->argc]);
|
|
|
|
dstring_appendstr (args->argv[args->argc], arg);
|
|
|
|
args->argc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
cbuf_t *
|
2002-08-03 06:04:00 +00:00
|
|
|
Cbuf_New (cbuf_interpreter_t *interp)
|
2002-07-30 17:08:53 +00:00
|
|
|
{
|
2002-08-25 04:47:57 +00:00
|
|
|
cbuf_t *cbuf = calloc (1, sizeof (cbuf_t));
|
2002-07-30 17:08:53 +00:00
|
|
|
|
|
|
|
cbuf->args = Cbuf_ArgsNew ();
|
2002-08-03 06:04:00 +00:00
|
|
|
cbuf->interpreter = interp;
|
|
|
|
if (interp->construct)
|
|
|
|
interp->construct (cbuf);
|
2002-07-30 17:08:53 +00:00
|
|
|
return cbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-08-01 02:42:19 +00:00
|
|
|
Cbuf_Delete (cbuf_t *cbuf)
|
2002-07-30 17:08:53 +00:00
|
|
|
{
|
|
|
|
if (!cbuf)
|
|
|
|
return;
|
|
|
|
Cbuf_ArgsDelete (cbuf->args);
|
2002-08-03 06:04:00 +00:00
|
|
|
if (cbuf->interpreter->destruct)
|
|
|
|
cbuf->interpreter->destruct (cbuf);
|
2002-07-30 17:08:53 +00:00
|
|
|
free (cbuf);
|
|
|
|
}
|
|
|
|
|
2002-08-03 06:04:00 +00:00
|
|
|
void
|
|
|
|
Cbuf_DeleteStack (cbuf_t *stack)
|
|
|
|
{
|
2002-08-25 04:47:57 +00:00
|
|
|
cbuf_t *next;
|
2002-08-03 06:04:00 +00:00
|
|
|
|
|
|
|
for (; stack; stack = next) {
|
|
|
|
next = stack->down;
|
|
|
|
Cbuf_Delete (stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
void
|
|
|
|
Cbuf_PushStack (cbuf_t *new)
|
|
|
|
{
|
|
|
|
if (cbuf_active->down)
|
|
|
|
Cbuf_DeleteStack (cbuf_active->down);
|
|
|
|
cbuf_active->down = new;
|
|
|
|
new->up = cbuf_active;
|
|
|
|
cbuf_active->state = CBUF_STATE_STACK;
|
|
|
|
}
|
|
|
|
|
2002-07-30 17:08:53 +00:00
|
|
|
void
|
|
|
|
Cbuf_AddText (cbuf_t *cbuf, const char *text)
|
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
cbuf->interpreter->add (cbuf, text);
|
2002-07-30 17:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cbuf_InsertText (cbuf_t *cbuf, const char *text)
|
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
cbuf->interpreter->insert (cbuf, text);
|
2002-07-30 17:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cbuf_Execute (cbuf_t *cbuf)
|
|
|
|
{
|
2002-07-31 05:19:03 +00:00
|
|
|
cbuf_active = cbuf;
|
2002-08-01 02:42:19 +00:00
|
|
|
cbuf->state = CBUF_STATE_NORMAL;
|
2003-01-28 21:16:21 +00:00
|
|
|
cbuf->interpreter->execute (cbuf);
|
2002-07-30 17:08:53 +00:00
|
|
|
}
|
|
|
|
|
2002-08-01 02:42:19 +00:00
|
|
|
void
|
|
|
|
Cbuf_Execute_Stack (cbuf_t *cbuf)
|
|
|
|
{
|
2002-08-25 04:47:57 +00:00
|
|
|
cbuf_t *sp;
|
2002-08-01 02:42:19 +00:00
|
|
|
|
2002-10-13 19:46:47 +00:00
|
|
|
if (cbuf->resumetime) {
|
|
|
|
if (cbuf->resumetime < Sys_DoubleTime())
|
|
|
|
cbuf->resumetime = 0;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
2002-08-01 02:42:19 +00:00
|
|
|
for (sp = cbuf; sp->down; sp = sp->down);
|
|
|
|
while (sp) {
|
2002-08-03 06:04:00 +00:00
|
|
|
if (sp->down) {
|
|
|
|
Cbuf_Delete (sp->down);
|
|
|
|
sp->down = 0;
|
|
|
|
}
|
2002-08-01 02:42:19 +00:00
|
|
|
Cbuf_Execute (sp);
|
|
|
|
if (sp->state) {
|
|
|
|
if (sp->state == CBUF_STATE_STACK) {
|
|
|
|
sp = sp->down;
|
|
|
|
continue;
|
|
|
|
} else if (sp->state == CBUF_STATE_ERROR)
|
2002-12-01 07:22:42 +00:00
|
|
|
goto ERROR;
|
2002-08-01 02:42:19 +00:00
|
|
|
else
|
|
|
|
return;
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|
2002-08-01 02:42:19 +00:00
|
|
|
sp = sp->up;
|
|
|
|
}
|
2002-12-01 07:22:42 +00:00
|
|
|
return;
|
|
|
|
ERROR:
|
2002-08-03 06:04:00 +00:00
|
|
|
if (cbuf->down) {
|
|
|
|
Cbuf_DeleteStack (cbuf->down);
|
|
|
|
cbuf->down = 0;
|
2002-08-01 02:42:19 +00:00
|
|
|
}
|
2003-01-29 04:34:23 +00:00
|
|
|
// Tear it down and build it back up
|
|
|
|
cbuf->interpreter->destruct (cbuf);
|
|
|
|
cbuf->interpreter->construct (cbuf);
|
2002-08-01 02:42:19 +00:00
|
|
|
}
|
|
|
|
|
2002-07-30 17:08:53 +00:00
|
|
|
void
|
|
|
|
Cbuf_Execute_Sets (cbuf_t *cbuf)
|
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
cbuf->interpreter->execute_sets (cbuf);
|
2002-07-30 17:08:53 +00:00
|
|
|
}
|
2002-08-02 04:07:25 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Cbuf_Error (const char *class, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
dstring_t *message = dstring_newstr();
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
dvsprintf (message, fmt, args);
|
|
|
|
va_end (args);
|
|
|
|
Sys_Printf (
|
2002-08-29 22:12:51 +00:00
|
|
|
"-----------------------------------\n"
|
|
|
|
"|Error in command buffer execution|\n"
|
|
|
|
"-----------------------------------\n"
|
2002-11-13 03:03:37 +00:00
|
|
|
"Type: %s\n\n"
|
2003-01-28 21:16:21 +00:00
|
|
|
"%s\n\n",
|
2002-08-02 04:07:25 +00:00
|
|
|
class,
|
2003-01-28 21:16:21 +00:00
|
|
|
message->str
|
2002-08-02 04:07:25 +00:00
|
|
|
);
|
|
|
|
cbuf_active->state = CBUF_STATE_ERROR;
|
|
|
|
dstring_delete (message);
|
|
|
|
}
|