mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2024-11-26 14:20:59 +00:00
225 lines
4.3 KiB
C
225 lines
4.3 KiB
C
/*
|
|
#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
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
"$Id$";
|
|
|
|
#ifdef HAVE_STRING_H
|
|
# include <string.h>
|
|
#endif
|
|
#ifdef HAVE_STRINGS_H
|
|
# include <strings.h>
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "QF/sys.h"
|
|
#include "QF/cbuf.h"
|
|
#include "QF/cmd.h"
|
|
#include "QF/dstring.h"
|
|
#include "QF/qtypes.h"
|
|
|
|
#include "compat.h"
|
|
|
|
cbuf_t *cbuf_active;
|
|
|
|
cbuf_args_t *
|
|
Cbuf_ArgsNew (void)
|
|
{
|
|
return calloc (1, sizeof (cbuf_args_t));
|
|
}
|
|
|
|
void
|
|
Cbuf_ArgsDelete (cbuf_args_t *args)
|
|
{
|
|
int i;
|
|
|
|
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)
|
|
{
|
|
int i;
|
|
|
|
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 *));
|
|
args->argm = realloc (args->argm, args->argv_size * sizeof (void *));
|
|
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 *
|
|
Cbuf_New (cbuf_interpreter_t *interp)
|
|
{
|
|
cbuf_t *cbuf = calloc (1, sizeof (cbuf_t));
|
|
|
|
cbuf->args = Cbuf_ArgsNew ();
|
|
cbuf->interpreter = interp;
|
|
if (interp->construct)
|
|
interp->construct (cbuf);
|
|
return cbuf;
|
|
}
|
|
|
|
void
|
|
Cbuf_Delete (cbuf_t *cbuf)
|
|
{
|
|
if (!cbuf)
|
|
return;
|
|
Cbuf_ArgsDelete (cbuf->args);
|
|
if (cbuf->interpreter->destruct)
|
|
cbuf->interpreter->destruct (cbuf);
|
|
free (cbuf);
|
|
}
|
|
|
|
void
|
|
Cbuf_DeleteStack (cbuf_t *stack)
|
|
{
|
|
cbuf_t *next;
|
|
|
|
for (; stack; stack = next) {
|
|
next = stack->down;
|
|
Cbuf_Delete (stack);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
void
|
|
Cbuf_AddText (cbuf_t *cbuf, const char *text)
|
|
{
|
|
cbuf->interpreter->add (cbuf, text);
|
|
}
|
|
|
|
void
|
|
Cbuf_InsertText (cbuf_t *cbuf, const char *text)
|
|
{
|
|
cbuf->interpreter->insert (cbuf, text);
|
|
}
|
|
|
|
void
|
|
Cbuf_Execute (cbuf_t *cbuf)
|
|
{
|
|
cbuf_active = cbuf;
|
|
cbuf->state = CBUF_STATE_NORMAL;
|
|
cbuf->interpreter->execute (cbuf);
|
|
}
|
|
|
|
void
|
|
Cbuf_Execute_Stack (cbuf_t *cbuf)
|
|
{
|
|
cbuf_t *sp;
|
|
|
|
if (cbuf->resumetime) {
|
|
if (cbuf->resumetime < Sys_DoubleTime())
|
|
cbuf->resumetime = 0;
|
|
else
|
|
return;
|
|
}
|
|
for (sp = cbuf; sp->down; sp = sp->down);
|
|
while (sp) {
|
|
if (sp->down) {
|
|
Cbuf_Delete (sp->down);
|
|
sp->down = 0;
|
|
}
|
|
Cbuf_Execute (sp);
|
|
if (sp->state) {
|
|
if (sp->state == CBUF_STATE_STACK) {
|
|
sp = sp->down;
|
|
continue;
|
|
} else if (sp->state == CBUF_STATE_ERROR)
|
|
goto ERROR;
|
|
else
|
|
return;
|
|
}
|
|
sp = sp->up;
|
|
}
|
|
return;
|
|
ERROR:
|
|
if (cbuf->down) {
|
|
Cbuf_DeleteStack (cbuf->down);
|
|
cbuf->down = 0;
|
|
}
|
|
// Tear it down and build it back up
|
|
cbuf->interpreter->destruct (cbuf);
|
|
cbuf->interpreter->construct (cbuf);
|
|
}
|
|
|
|
void
|
|
Cbuf_Execute_Sets (cbuf_t *cbuf)
|
|
{
|
|
cbuf->interpreter->execute_sets (cbuf);
|
|
}
|
|
|
|
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 (
|
|
"-----------------------------------\n"
|
|
"|Error in command buffer execution|\n"
|
|
"-----------------------------------\n"
|
|
"Type: %s\n\n"
|
|
"%s\n\n",
|
|
class,
|
|
message->str
|
|
);
|
|
cbuf_active->state = CBUF_STATE_ERROR;
|
|
dstring_delete (message);
|
|
}
|