Split off the old console parser into idparse.[ch] and prepared the cbuf

struct and support functions for GIB mark III.

Oh yeah, I'm back.
This commit is contained in:
Brian Koropoff 2002-07-31 22:03:53 +00:00
parent ad8176d68c
commit a93b221272
11 changed files with 242 additions and 137 deletions

View file

@ -47,7 +47,18 @@ typedef struct cbuf_s {
cbuf_args_t *args;
void (*extract_line) (struct cbuf_s *cbuf);
void (*parse_line) (struct cbuf_s *cbuf);
qboolean wait;
void (*destructor) (struct cbuf_s *cbuf);
struct cbuf_s *up, *down; // The stack
enum {
CBUF_STATE_NORMAL = 0, // Normal condition
CBUF_STATE_WAIT, // Buffer is stalled until next frame
CBUF_STATE_ERROR, // An unrecoverable error occured
CBUF_STATE_STACK, // A buffer has been added to the stack
} state;
void *data; // Pointer to a custom structure if needed
} cbuf_t;
extern cbuf_t *cbuf_active;
@ -60,7 +71,12 @@ cbuf_args_t *Cbuf_ArgsNew (void);
void Cbuf_ArgsDelete (cbuf_args_t *);
void Cbuf_ArgsAdd (cbuf_args_t *args, const char *arg);
cbuf_t *Cbuf_New (void);
cbuf_t * Cbuf_New (
void (*extract) (struct cbuf_s *cbuf),
void (*parse) (struct cbuf_s *cbuf),
void (*construct) (struct cbuf_s *cbuf),
void (*destruct) (struct cbuf_s *cbuf)
);
void CBuf_Delete (cbuf_t *cbuf);
void Cbuf_AddText (cbuf_t *cbuf, const char *text);
void Cbuf_InsertText (cbuf_t *cbuf, const char *text);

34
include/QF/idparse.h Normal file
View file

@ -0,0 +1,34 @@
/*
#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
*/
const char *COM_Parse (const char *data);
void COM_TokenizeString (const char *str, cbuf_args_t *args);
void COM_extract_line (cbuf_t *cbuf);
void COM_parse_line (cbuf_t *cbuf);

View file

@ -31,6 +31,7 @@ static const char rcsid[] =
# include "config.h"
#endif
#include "QF/cbuf.h"
#include "QF/idparse.h" // For now, use the id console parser
#include "QF/progs.h"
static cbuf_t *cbuf; //FIXME use a properly allocated cbuf rather than this hack
@ -39,7 +40,7 @@ static inline void
check_cbuf (void)
{
if (!cbuf)
cbuf = Cbuf_New ();
cbuf = Cbuf_New (COM_extract_line, COM_parse_line, NULL, NULL);
}
static void

View file

@ -39,6 +39,7 @@ static const char rcsid[] =
#endif
#include "QF/cbuf.h"
#include "QF/idparse.h"
#include "QF/keys.h"
#include "QF/progs.h"
#include "QF/zone.h"
@ -49,7 +50,7 @@ static inline void
check_cbuf (void)
{
if (!cbuf)
cbuf = Cbuf_New ();
cbuf = Cbuf_New (COM_extract_line, COM_parse_line, NULL, NULL);
}
/*

View file

@ -27,8 +27,8 @@ libQFutil_la_LIBADD= libasm.la $(Z_LIBS) $(DL_LIBS)
libQFutil_la_DEPENDENCIES= libasm.la
libQFutil_la_SOURCES= \
buildnum.c cbuf.c checksum.c cmd.c crc.c cvar.c dstring.c exp.c fendian.c \
getopt.c getopt1.c hash.c info.c link.c mathlib.c mdfour.c msg.c ops.c \
pakfile.c pcx.c plugin.c qargs.c qendian.c qfplist.c quakefs.c quakeio.c \
getopt.c getopt1.c hash.c idparse.c info.c link.c mathlib.c mdfour.c msg.c \
ops.c pakfile.c pcx.c plugin.c qargs.c qendian.c qfplist.c quakefs.c quakeio.c \
sizebuf.c string.c sys.c tga.c va.c ver_check.c wad.c zone.c $(fnmatch)
EXTRA_DIST= $(asm_src) $(fnmatch_src)

View file

@ -39,7 +39,7 @@ static const char rcsid[] =
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <ctype.h>
#include <stdlib.h>
#include "QF/cbuf.h"
@ -49,8 +49,6 @@ static const char rcsid[] =
#include "compat.h"
static dstring_t *_com_token;
const char *com_token;
cbuf_t *cbuf_active;
cbuf_args_t *
@ -91,135 +89,24 @@ Cbuf_ArgsAdd (cbuf_args_t *args, const char *arg)
args->argc++;
}
const char *
COM_Parse (const char *data)
{
int c;
int i;
if (!_com_token) {
_com_token = dstring_newstr ();
} else {
dstring_clearstr (_com_token);
}
com_token = _com_token->str;
if (!data)
return 0;
skipwhite:
while (isspace ((byte) *data))
data++;
if (!*data)
return 0;
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) {
dstring_insert (_com_token, data, i - 1, 0);
goto done;
}
}
}
i = 0;
do {
i++;
} while (data[i] && !isspace ((byte) data[i]));
dstring_insert (_com_token, data, i, 0);
done:
com_token = _com_token->str;
return data + i;
}
void
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;
}
static void
extract_line (cbuf_t *cbuf)
{
int i;
int len = cbuf->buf->size - 1;
char *text = cbuf->buf->str;
int quotes = 0;
dstring_clearstr (cbuf->line);
for (i = 0; i < len; i++) {
if (text[i] == '"')
quotes++;
if (!(quotes & 1)) {
if (text[i] == ';')
// don't break if inside a quoted string
break;
if (text[i] == '/' && text[i + 1] == '/') {
int j = i;
while (j < len && text[j] != '\n' && text[j] != '\r')
j++;
dstring_snip (cbuf->buf, i, j - i);
len -= j - i;
}
}
if (text[i] == '\n' || text[i] == '\r')
break;
}
if (i)
dstring_insert (cbuf->line, text, i, 0);
if (text[i]) {
dstring_snip (cbuf->buf, 0, i + 1);
} else {
// We've hit the end of the buffer, just clear it
dstring_clearstr (cbuf->buf);
}
}
static void
parse_line (cbuf_t *cbuf)
{
COM_TokenizeString (cbuf->line->str, cbuf->args);;
}
cbuf_t *
Cbuf_New (void)
Cbuf_New (
void (*extract) (struct cbuf_s *cbuf),
void (*parse) (struct cbuf_s *cbuf),
void (*construct) (struct cbuf_s *cbuf),
void (*destruct) (struct cbuf_s *cbuf)
)
{
cbuf_t *cbuf = calloc (1, sizeof (cbuf_t));
cbuf->buf = dstring_newstr ();
cbuf->line = dstring_newstr ();
cbuf->args = Cbuf_ArgsNew ();
cbuf->extract_line = extract_line;
cbuf->parse_line = parse_line;
cbuf->extract_line = extract;
cbuf->parse_line = parse;
cbuf->destructor = destruct;
if (construct)
construct (cbuf);
return cbuf;
}
@ -231,6 +118,8 @@ CBuf_Delete (cbuf_t *cbuf)
dstring_delete (cbuf->buf);
dstring_delete (cbuf->line);
Cbuf_ArgsDelete (cbuf->args);
if (cbuf->destructor)
cbuf->destructor (cbuf);
free (cbuf);
}
@ -255,11 +144,15 @@ Cbuf_Execute (cbuf_t *cbuf)
cbuf_active = cbuf;
while (cbuf->buf->str[0]) {
cbuf->extract_line (cbuf);
if (cbuf->state)
break;
cbuf->parse_line (cbuf);
if (cbuf->state) // Merging extract and parse
break; // will get rid of extra checks
if (!args->argc)
continue;
Cmd_Command (args);
if (cbuf->wait)
if (cbuf->state)
break;
}
}

View file

@ -43,6 +43,7 @@ static const char rcsid[] =
#include <dirent.h>
#include "QF/cbuf.h"
#include "QF/idparse.h"
#include "QF/cmd.h"
#include "QF/cvar.h"
#include "QF/dstring.h"
@ -511,7 +512,7 @@ next call of Cmd_Execute (usually next frame) */
void
Cmd_Wait_f (void)
{
cbuf_active->wait = true;
cbuf_active->state = CBUF_STATE_WAIT;
}
void
@ -588,7 +589,7 @@ Cmd_Init (void)
Cmd_AddCommand ("wait", Cmd_Wait_f, "Wait a game tic");
cmd_warncmd = Cvar_Get ("cmd_warncmd", "0", CVAR_NONE, NULL, "Toggles the "
"display of error messages for unknown commands");
cmd_cbuf = Cbuf_New ();
cmd_cbuf = Cbuf_New (COM_extract_line, COM_parse_line, NULL, NULL);
}
int

156
libs/util/idparse.c Normal file
View file

@ -0,0 +1,156 @@
/*
#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
*/
#include <ctype.h>
#include "QF/dstring.h"
#include "QF/cbuf.h"
static dstring_t *_com_token;
const char *com_token;
const char *
COM_Parse (const char *data)
{
int c;
int i;
if (!_com_token) {
_com_token = dstring_newstr ();
} else {
dstring_clearstr (_com_token);
}
com_token = _com_token->str;
if (!data)
return 0;
skipwhite:
while (isspace ((byte) *data))
data++;
if (!*data)
return 0;
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) {
dstring_insert (_com_token, data, i - 1, 0);
goto done;
}
}
}
i = 0;
do {
i++;
} while (data[i] && !isspace ((byte) data[i]));
dstring_insert (_com_token, data, i, 0);
done:
com_token = _com_token->str;
return data + i;
}
void
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;
}
void
COM_extract_line (cbuf_t *cbuf)
{
int i;
int len = cbuf->buf->size - 1;
char *text = cbuf->buf->str;
int quotes = 0;
dstring_clearstr (cbuf->line);
for (i = 0; i < len; i++) {
if (text[i] == '"')
quotes++;
if (!(quotes & 1)) {
if (text[i] == ';')
// don't break if inside a quoted string
break;
if (text[i] == '/' && text[i + 1] == '/') {
int j = i;
while (j < len && text[j] != '\n' && text[j] != '\r')
j++;
dstring_snip (cbuf->buf, i, j - i);
len -= j - i;
}
}
if (text[i] == '\n' || text[i] == '\r')
break;
}
if (i)
dstring_insert (cbuf->line, text, i, 0);
if (text[i]) {
dstring_snip (cbuf->buf, 0, i + 1);
} else {
// We've hit the end of the buffer, just clear it
dstring_clearstr (cbuf->buf);
}
}
void
COM_parse_line (cbuf_t *cbuf)
{
COM_TokenizeString (cbuf->line->str, cbuf->args);;
}

View file

@ -32,6 +32,7 @@ static const char rcsid[] =
#endif
#include "QF/cbuf.h"
#include "QF/idparse.h"
#include "QF/cdaudio.h"
#include "QF/cmd.h"
#include "QF/console.h"
@ -847,7 +848,7 @@ Host_Init (void)
{
Con_Printf ("Host_Init\n");
host_cbuf = Cbuf_New ();
host_cbuf = Cbuf_New (COM_extract_line, COM_parse_line, NULL, NULL);
cmd_source = src_command;
Cvar_Init_Hash ();

View file

@ -64,6 +64,7 @@ static const char rcsid[] =
#endif
#include "QF/cbuf.h"
#include "QF/idparse.h"
#include "QF/cdaudio.h"
#include "QF/cmd.h"
#include "QF/console.h"
@ -1662,7 +1663,7 @@ CL_Init_Memory (void)
void
Host_Init (void)
{
cl_cbuf = Cbuf_New ();
cl_cbuf = Cbuf_New (COM_extract_line, COM_parse_line, NULL, NULL);
Cvar_Init_Hash ();
Cmd_Init_Hash ();

View file

@ -67,6 +67,7 @@ static const char rcsid[] =
#endif
#include "QF/cbuf.h"
#include "QF/idparse.h"
#include "QF/cmd.h"
#include "QF/console.h"
#include "QF/cvar.h"
@ -2405,7 +2406,7 @@ SV_Init (void)
// COM_AddParm ("-game");
// COM_AddParm ("qw");
sv_cbuf = Cbuf_New ();
sv_cbuf = Cbuf_New (COM_extract_line, COM_parse_line, NULL, NULL);
sv_args = Cbuf_ArgsNew ();
Sys_RegisterShutdown (SV_Shutdown);