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
|
|
|
|
|
|
|
|
*/
|
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
#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 *));
|
|
|
|
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->buf = dstring_newstr ();
|
|
|
|
cbuf->line = dstring_newstr ();
|
|
|
|
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;
|
|
|
|
dstring_delete (cbuf->buf);
|
|
|
|
dstring_delete (cbuf->line);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-30 17:08:53 +00:00
|
|
|
void
|
|
|
|
Cbuf_AddText (cbuf_t *cbuf, const char *text)
|
|
|
|
{
|
|
|
|
dstring_appendstr (cbuf->buf, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cbuf_InsertText (cbuf_t *cbuf, const char *text)
|
|
|
|
{
|
2002-08-03 06:04:00 +00:00
|
|
|
dstring_insertstr (cbuf->buf, 0, "\n");
|
|
|
|
dstring_insertstr (cbuf->buf, 0, 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;
|
2002-08-06 08:15:20 +00:00
|
|
|
while (cbuf->buf->str[0] || cbuf->line->str[0]) {
|
2002-08-03 06:04:00 +00:00
|
|
|
cbuf->interpreter->extract_line (cbuf);
|
2002-07-31 22:03:53 +00:00
|
|
|
if (cbuf->state)
|
|
|
|
break;
|
2002-08-03 06:04:00 +00:00
|
|
|
cbuf->interpreter->parse_line (cbuf);
|
2002-07-31 22:03:53 +00:00
|
|
|
if (cbuf->state) // Merging extract and parse
|
|
|
|
break; // will get rid of extra checks
|
2002-08-03 06:04:00 +00:00
|
|
|
if (!cbuf->args->argc)
|
2002-07-30 17:08:53 +00:00
|
|
|
continue;
|
2002-08-03 06:04:00 +00:00
|
|
|
cbuf->interpreter->execute_line (cbuf);
|
2002-07-31 22:03:53 +00:00
|
|
|
if (cbuf->state)
|
2002-07-31 05:19:03 +00:00
|
|
|
break;
|
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)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
return;
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|
2002-08-01 02:42:19 +00:00
|
|
|
sp = sp->up;
|
|
|
|
}
|
|
|
|
dstring_clearstr (cbuf->buf);
|
2002-08-06 08:15:20 +00:00
|
|
|
dstring_clearstr (cbuf->line);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-30 17:08:53 +00:00
|
|
|
void
|
|
|
|
Cbuf_Execute_Sets (cbuf_t *cbuf)
|
|
|
|
{
|
2002-07-31 05:19:03 +00:00
|
|
|
cbuf_args_t *args = cbuf->args;
|
|
|
|
|
|
|
|
cbuf_active = cbuf;
|
2002-08-06 08:15:20 +00:00
|
|
|
while (cbuf->buf->str[0] || cbuf->line->str[0]) {
|
2002-08-03 06:04:00 +00:00
|
|
|
cbuf->interpreter->extract_line (cbuf);
|
|
|
|
cbuf->interpreter->parse_line (cbuf);
|
2002-07-31 05:19:03 +00:00
|
|
|
if (!args->argc)
|
2002-07-30 17:08:53 +00:00
|
|
|
continue;
|
2002-07-31 05:19:03 +00:00
|
|
|
if (strequal (args->argv[0]->str, "set")
|
|
|
|
|| strequal (args->argv[0]->str, "setrom"))
|
|
|
|
Cmd_Command (args);
|
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();
|
2002-08-29 22:12:51 +00:00
|
|
|
char *n;
|
2002-08-02 04:07:25 +00:00
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
dvsprintf (message, fmt, args);
|
|
|
|
va_end (args);
|
2002-08-29 22:12:51 +00:00
|
|
|
if ((n = strchr (cbuf_active->line->str, '\n')))
|
|
|
|
*n = 0;
|
2002-08-02 04:07:25 +00:00
|
|
|
Sys_Printf (
|
2002-08-29 22:12:51 +00:00
|
|
|
"-----------------------------------\n"
|
|
|
|
"|Error in command buffer execution|\n"
|
|
|
|
"-----------------------------------\n"
|
2002-08-02 04:07:25 +00:00
|
|
|
"Type: %s\n"
|
2002-08-29 22:12:51 +00:00
|
|
|
"%s\n"
|
|
|
|
"Near/on line: %s\n",
|
2002-08-02 04:07:25 +00:00
|
|
|
class,
|
2002-08-29 22:12:51 +00:00
|
|
|
message->str,
|
|
|
|
cbuf_active->line->str
|
2002-08-02 04:07:25 +00:00
|
|
|
);
|
|
|
|
cbuf_active->state = CBUF_STATE_ERROR;
|
|
|
|
dstring_clearstr (cbuf_active->buf);
|
|
|
|
dstring_delete (message);
|
|
|
|
}
|