2002-07-31 22:03: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
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
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-31 22:03:53 +00:00
|
|
|
#include <ctype.h>
|
2003-01-28 21:16:21 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2002-07-31 22:03:53 +00:00
|
|
|
|
|
|
|
#include "QF/dstring.h"
|
|
|
|
#include "QF/cbuf.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
#include "QF/cmd.h"
|
2003-01-06 18:28:13 +00:00
|
|
|
#include "QF/idparse.h"
|
2002-07-31 22:03:53 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
typedef struct idbuf_s {
|
|
|
|
dstring_t *buf, *line;
|
|
|
|
} idbuf_t;
|
|
|
|
|
|
|
|
#define DATA(x) ((idbuf_t *)(x)->data)
|
|
|
|
|
|
|
|
static void
|
|
|
|
COM_construct (cbuf_t *cbuf)
|
|
|
|
{
|
|
|
|
idbuf_t *new = calloc (1, sizeof (idbuf_t));
|
|
|
|
|
|
|
|
new->buf = dstring_newstr();
|
|
|
|
new->line = dstring_newstr();
|
|
|
|
cbuf->data = new;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
COM_destruct (cbuf_t *cbuf)
|
|
|
|
{
|
|
|
|
dstring_delete(DATA(cbuf)->buf);
|
|
|
|
dstring_delete(DATA(cbuf)->line);
|
|
|
|
free(cbuf->data);
|
|
|
|
}
|
|
|
|
|
2003-02-14 08:06:01 +00:00
|
|
|
static void
|
|
|
|
COM_reset (cbuf_t *cbuf)
|
|
|
|
{
|
|
|
|
dstring_clearstr (DATA(cbuf)->buf);
|
|
|
|
dstring_clearstr (DATA(cbuf)->line);
|
|
|
|
}
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
static void
|
|
|
|
COM_add (cbuf_t *cbuf, const char *str)
|
|
|
|
{
|
|
|
|
dstring_appendstr (DATA(cbuf)->buf, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
COM_insert (cbuf_t *cbuf, const char *str)
|
|
|
|
{
|
|
|
|
dstring_insertstr (DATA(cbuf)->buf, 0, "\n");
|
|
|
|
dstring_insertstr (DATA(cbuf)->buf, 0, str);
|
|
|
|
}
|
|
|
|
|
2002-07-31 22:03:53 +00:00
|
|
|
static dstring_t *_com_token;
|
|
|
|
const char *com_token;
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE const char *
|
2002-07-31 22:03:53 +00:00
|
|
|
COM_Parse (const char *data)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
int i;
|
|
|
|
|
2003-05-26 19:14:18 +00:00
|
|
|
if (!_com_token)
|
2002-07-31 22:03:53 +00:00
|
|
|
_com_token = dstring_newstr ();
|
|
|
|
com_token = _com_token->str;
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
skipwhite:
|
|
|
|
while (isspace ((byte) *data))
|
|
|
|
data++;
|
2003-07-27 21:45:53 +00:00
|
|
|
if (!*data) {
|
|
|
|
dstring_clearstr (_com_token);
|
|
|
|
com_token = _com_token->str;
|
2002-07-31 22:03:53 +00:00
|
|
|
return 0;
|
2003-07-27 21:45:53 +00:00
|
|
|
}
|
2002-07-31 22:03:53 +00:00
|
|
|
if (data[0] == '/' && data[1] == '/') {
|
|
|
|
while (*data && *data != '\n')
|
|
|
|
data++;
|
|
|
|
goto skipwhite;
|
|
|
|
}
|
|
|
|
if (*data == '"') {
|
|
|
|
data++;
|
|
|
|
i = 0;
|
|
|
|
while (1) {
|
|
|
|
c = data[i++];
|
|
|
|
if (c == '"' || !c) {
|
2003-05-26 19:14:18 +00:00
|
|
|
dstring_copysubstr (_com_token, data, i - 1);
|
2002-07-31 22:03:53 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
i++;
|
|
|
|
} while (data[i] && !isspace ((byte) data[i]));
|
2003-05-26 19:14:18 +00:00
|
|
|
dstring_copysubstr (_com_token, data, i);
|
2002-07-31 22:03:53 +00:00
|
|
|
done:
|
|
|
|
com_token = _com_token->str;
|
|
|
|
return data + i;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-07-31 22:03:53 +00:00
|
|
|
COM_TokenizeString (const char *str, cbuf_args_t *args)
|
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
args->argc = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
while (isspace ((byte)*str) && *str != '\n')
|
|
|
|
str++;
|
|
|
|
if (*str == '\n') {
|
|
|
|
str++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*str)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s = COM_Parse (str);
|
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Cbuf_ArgsAdd (args, com_token);
|
|
|
|
args->args[args->argc - 1] = str;
|
|
|
|
str = s;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-07-31 22:03:53 +00:00
|
|
|
COM_extract_line (cbuf_t *cbuf)
|
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
int i;
|
|
|
|
dstring_t *buf = DATA(cbuf)->buf;
|
|
|
|
dstring_t *line = DATA(cbuf)->line;
|
|
|
|
int len = buf->size - 1;
|
|
|
|
char *text = buf->str;
|
|
|
|
int quotes = 0;
|
|
|
|
|
|
|
|
dstring_clearstr (line);
|
2002-07-31 22:03:53 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (text[i] == '"')
|
|
|
|
quotes++;
|
2002-08-21 22:52:29 +00:00
|
|
|
if (!(quotes & 1)) { // don't break if inside a quoted string
|
2002-07-31 22:03:53 +00:00
|
|
|
if (text[i] == ';')
|
|
|
|
break;
|
|
|
|
if (text[i] == '/' && text[i + 1] == '/') {
|
|
|
|
int j = i;
|
2002-11-09 07:58:34 +00:00
|
|
|
while (j < len && text[j] != '\n'
|
|
|
|
&& (text[j] != '\r'
|
|
|
|
|| (j < len - 1 && text[j + 1] != '\n')))
|
2002-07-31 22:03:53 +00:00
|
|
|
j++;
|
2003-01-28 21:16:21 +00:00
|
|
|
dstring_snip (buf, i, j - i);
|
2002-08-21 22:52:29 +00:00
|
|
|
break;
|
2002-07-31 22:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
2002-11-09 07:58:34 +00:00
|
|
|
if (text[i] == '\n'
|
|
|
|
|| (text[i] == '\r' && (i == len - 1 || text[ i + 1] == '\n')))
|
2002-07-31 22:03:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i)
|
2003-01-28 21:16:21 +00:00
|
|
|
dstring_insert (line, 0, text, i);
|
2002-07-31 22:03:53 +00:00
|
|
|
if (text[i]) {
|
2003-01-28 21:16:21 +00:00
|
|
|
dstring_snip (buf, 0, i + 1);
|
2002-07-31 22:03:53 +00:00
|
|
|
} else {
|
|
|
|
// We've hit the end of the buffer, just clear it
|
2003-01-28 21:16:21 +00:00
|
|
|
dstring_clearstr (buf);
|
2002-07-31 22:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2003-01-28 21:16:21 +00:00
|
|
|
COM_execute (cbuf_t *cbuf)
|
2002-07-31 22:03:53 +00:00
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
dstring_t *buf = DATA(cbuf)->buf;
|
|
|
|
dstring_t *line = DATA(cbuf)->line;
|
2003-02-03 16:05:53 +00:00
|
|
|
while (cbuf->state == CBUF_STATE_NORMAL && *buf->str) {
|
2003-01-28 21:16:21 +00:00
|
|
|
COM_extract_line (cbuf);
|
|
|
|
COM_TokenizeString (line->str, cbuf->args);
|
|
|
|
if (cbuf->args->argc)
|
|
|
|
Cmd_Command (cbuf->args);
|
|
|
|
}
|
2002-07-31 22:03:53 +00:00
|
|
|
}
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2003-01-28 21:16:21 +00:00
|
|
|
COM_execute_sets (cbuf_t *cbuf)
|
2002-08-03 06:04:00 +00:00
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
dstring_t *buf = DATA(cbuf)->buf;
|
2003-01-29 22:46:24 +00:00
|
|
|
dstring_t *line = DATA(cbuf)->line;
|
2003-01-28 21:16:21 +00:00
|
|
|
while (*buf->str) {
|
|
|
|
COM_extract_line (cbuf);
|
|
|
|
COM_TokenizeString (line->str, cbuf->args);
|
|
|
|
if (cbuf->args->argc &&
|
|
|
|
(!strcmp (cbuf->args->argv[0]->str, "set") ||
|
|
|
|
!strcmp (cbuf->args->argv[0]->str, "setrom")))
|
|
|
|
Cmd_Command (cbuf->args);
|
|
|
|
}
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE cbuf_interpreter_t id_interp = {
|
2003-01-28 21:16:21 +00:00
|
|
|
COM_construct,
|
|
|
|
COM_destruct,
|
2003-02-14 08:06:01 +00:00
|
|
|
COM_reset,
|
2003-01-28 21:16:21 +00:00
|
|
|
COM_add,
|
|
|
|
COM_insert,
|
|
|
|
COM_execute,
|
|
|
|
COM_execute_sets,
|
2002-08-03 06:04:00 +00:00
|
|
|
};
|