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
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2003-01-15 15:31:36 +00:00
|
|
|
"$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"
|
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
cbuf_t *cbuf_active = NULL;
|
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);
|
2003-02-26 07:44:34 +00:00
|
|
|
free (args->argm);
|
2002-07-30 17:08:53 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2003-05-26 19:14:18 +00:00
|
|
|
dstring_copystr (args->argv[args->argc], arg);
|
2002-07-30 17:08:53 +00:00
|
|
|
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
|
2003-02-14 08:06:01 +00:00
|
|
|
Cbuf_Reset (cbuf_t *cbuf)
|
2003-01-28 21:16:21 +00:00
|
|
|
{
|
2003-02-14 08:06:01 +00:00
|
|
|
cbuf->resumetime = 0.0;
|
|
|
|
cbuf->args->argc = 0;
|
2003-04-11 05:58:58 +00:00
|
|
|
cbuf->state = CBUF_STATE_NORMAL;
|
2003-02-14 08:06:01 +00:00
|
|
|
if (cbuf->interpreter->reset)
|
|
|
|
cbuf->interpreter->reset (cbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
cbuf_t *
|
|
|
|
Cbuf_PushStack (cbuf_interpreter_t *interp)
|
|
|
|
{
|
|
|
|
cbuf_t *new;
|
|
|
|
if (cbuf_active->down) {
|
|
|
|
new = cbuf_active->down;
|
2003-05-26 05:07:21 +00:00
|
|
|
if (new->interpreter != interp) {
|
|
|
|
new->interpreter->destruct (new);
|
|
|
|
new->interpreter = interp;
|
|
|
|
new->interpreter->construct (new);
|
|
|
|
}
|
2003-02-14 08:06:01 +00:00
|
|
|
Cbuf_Reset (new);
|
|
|
|
} else
|
|
|
|
new = Cbuf_New (interp);
|
2003-01-28 21:16:21 +00:00
|
|
|
cbuf_active->down = new;
|
|
|
|
new->up = cbuf_active;
|
|
|
|
cbuf_active->state = CBUF_STATE_STACK;
|
2003-02-14 08:06:01 +00:00
|
|
|
return new;
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
|
|
|
|
2002-07-30 17:08:53 +00:00
|
|
|
void
|
|
|
|
Cbuf_AddText (cbuf_t *cbuf, const char *text)
|
|
|
|
{
|
2003-04-15 19:40:46 +00:00
|
|
|
if (cbuf->state == CBUF_STATE_JUNK)
|
|
|
|
cbuf->state = CBUF_STATE_NORMAL;
|
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-04-15 19:40:46 +00:00
|
|
|
if (cbuf->state == CBUF_STATE_JUNK)
|
|
|
|
cbuf->state = CBUF_STATE_NORMAL;
|
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)
|
|
|
|
{
|
2003-09-11 06:03:13 +00:00
|
|
|
cbuf_t *old = cbuf_active;
|
2002-07-31 05:19:03 +00:00
|
|
|
cbuf_active = cbuf;
|
2003-01-28 21:16:21 +00:00
|
|
|
cbuf->interpreter->execute (cbuf);
|
2003-09-11 06:03:13 +00:00
|
|
|
cbuf_active = old;
|
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;
|
|
|
|
}
|
2003-02-14 08:06:01 +00:00
|
|
|
for (sp = cbuf; sp->down && sp->down->state != CBUF_STATE_JUNK; sp = sp->down);
|
2003-09-11 06:03:13 +00:00
|
|
|
if (sp->state == CBUF_STATE_BLOCKED)
|
|
|
|
return;
|
2002-08-01 02:42:19 +00:00
|
|
|
while (sp) {
|
|
|
|
Cbuf_Execute (sp);
|
|
|
|
if (sp->state) {
|
|
|
|
if (sp->state == CBUF_STATE_STACK) {
|
2003-04-11 05:58:58 +00:00
|
|
|
sp->state = CBUF_STATE_NORMAL;
|
2002-08-01 02:42:19 +00:00
|
|
|
sp = sp->down;
|
|
|
|
continue;
|
|
|
|
} else if (sp->state == CBUF_STATE_ERROR)
|
2003-02-14 08:06:01 +00:00
|
|
|
break;
|
2003-09-11 06:03:13 +00:00
|
|
|
else if (sp->state == CBUF_STATE_BLOCKED)
|
|
|
|
return;
|
2003-04-11 05:58:58 +00:00
|
|
|
else {
|
|
|
|
sp->state = CBUF_STATE_NORMAL;
|
2002-08-01 02:42:19 +00:00
|
|
|
return;
|
2003-04-11 05:58:58 +00:00
|
|
|
}
|
2003-02-14 08:06:01 +00:00
|
|
|
}
|
|
|
|
sp->state = CBUF_STATE_JUNK;
|
2002-08-01 02:42:19 +00:00
|
|
|
sp = sp->up;
|
|
|
|
}
|
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-02-26 07:44:34 +00:00
|
|
|
if (sp)
|
2003-02-25 06:52:27 +00:00
|
|
|
Cbuf_Reset (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
|
|
|
|