From 3c3844d2ac1dafb580bc2a6c580d4aba691fbce4 Mon Sep 17 00:00:00 2001 From: Brian Koropoff Date: Fri, 2 Aug 2002 04:07:25 +0000 Subject: [PATCH] Checked in the beginnings of GIB mark III and modified qw-server to use it for its console. --- include/QF/cbuf.h | 4 + include/QF/gib_buffer.h | 38 ++++++++ include/QF/gib_parse.h | 32 +++++++ include/QF/gib_process.h | 31 +++++++ libs/util/Makefile.am | 7 +- libs/util/cbuf.c | 24 +++++ libs/util/gib_buffer.c | 47 ++++++++++ libs/util/gib_parse.c | 191 +++++++++++++++++++++++++++++++++++++++ libs/util/gib_process.c | 49 ++++++++++ qw/source/sv_main.c | 5 +- 10 files changed, 424 insertions(+), 4 deletions(-) create mode 100644 include/QF/gib_buffer.h create mode 100644 include/QF/gib_parse.h create mode 100644 include/QF/gib_process.h create mode 100644 libs/util/gib_buffer.c create mode 100644 libs/util/gib_parse.c create mode 100644 libs/util/gib_process.c diff --git a/include/QF/cbuf.h b/include/QF/cbuf.h index 8eb433197..e846ce210 100644 --- a/include/QF/cbuf.h +++ b/include/QF/cbuf.h @@ -32,6 +32,8 @@ #ifndef __QF_cbuf_h #define __QF_cbuf_h +#include + #include "QF/qtypes.h" typedef struct cbuf_args_s { @@ -73,10 +75,12 @@ cbuf_t * Cbuf_New ( 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); void Cbuf_Execute (cbuf_t *cbuf); void Cbuf_Execute_Sets (cbuf_t *cbuf); +void Cbuf_Error (const char *class, const char *fmt, ...); #endif//__QF_cbuf_h diff --git a/include/QF/gib_buffer.h b/include/QF/gib_buffer.h new file mode 100644 index 000000000..0b56068d6 --- /dev/null +++ b/include/QF/gib_buffer.h @@ -0,0 +1,38 @@ +/* + #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 + +*/ + +#define GIB_DATA(buffer) ((gib_buffer_data_t *)(buffer->data)) + +typedef struct gib_buffer_data_s { + struct dstring_s *arg_composite; +} gib_buffer_data_t; + +void GIB_Buffer_Construct (struct cbuf_s *cbuf); +void GIB_Buffer_Destruct (struct cbuf_s *cbuf); diff --git a/include/QF/gib_parse.h b/include/QF/gib_parse.h new file mode 100644 index 000000000..05cb26099 --- /dev/null +++ b/include/QF/gib_parse.h @@ -0,0 +1,32 @@ +/* + #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 + +*/ + +void GIB_Parse_Extract_Line (struct cbuf_s *cbuf); +void GIB_Parse_Tokenize_Line (struct cbuf_s *cbuf); diff --git a/include/QF/gib_process.h b/include/QF/gib_process.h new file mode 100644 index 000000000..f8b96437d --- /dev/null +++ b/include/QF/gib_process.h @@ -0,0 +1,31 @@ +/* + #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 + +*/ + +void GIB_Process_Variable (struct dstring_s *token); diff --git a/libs/util/Makefile.am b/libs/util/Makefile.am index 38211cd0e..dc273f007 100644 --- a/libs/util/Makefile.am +++ b/libs/util/Makefile.am @@ -27,8 +27,9 @@ 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 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) + getopt.c getopt1.c gib_buffer.c gib_parse.c gib_process.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) diff --git a/libs/util/cbuf.c b/libs/util/cbuf.c index d2fedd138..2331bdb6a 100644 --- a/libs/util/cbuf.c +++ b/libs/util/cbuf.c @@ -42,6 +42,7 @@ static const char rcsid[] = #include +#include "QF/sys.h" #include "QF/cbuf.h" #include "QF/cmd.h" #include "QF/dstring.h" @@ -200,3 +201,26 @@ Cbuf_Execute_Sets (cbuf_t *cbuf) Cmd_Command (args); } } + +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 ( + "Error in command buffer execution\n" + "---------------------------------\n" + "Type: %s\n" + "Description: %s\n", + class, + message->str + ); + cbuf_active->state = CBUF_STATE_ERROR; + dstring_clearstr (cbuf_active->buf); + dstring_delete (message); +} + diff --git a/libs/util/gib_buffer.c b/libs/util/gib_buffer.c new file mode 100644 index 000000000..ce06dfa6e --- /dev/null +++ b/libs/util/gib_buffer.c @@ -0,0 +1,47 @@ +/* + #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 + +#include "QF/dstring.h" +#include "QF/cbuf.h" +#include "QF/gib_buffer.h" + +void GIB_Buffer_Construct (struct cbuf_s *cbuf) +{ + cbuf->data = calloc (1, sizeof (gib_buffer_data_t)); + GIB_DATA (cbuf)->arg_composite = dstring_newstr (); +} + +void GIB_Buffer_Destruct (struct cbuf_s *cbuf) +{ + dstring_delete (GIB_DATA (cbuf)->arg_composite); + free (cbuf->data); +} diff --git a/libs/util/gib_parse.c b/libs/util/gib_parse.c new file mode 100644 index 000000000..4307542cf --- /dev/null +++ b/libs/util/gib_parse.c @@ -0,0 +1,191 @@ +/* + #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 +#include + +#include "QF/sys.h" +#include "QF/dstring.h" +#include "QF/cbuf.h" +#include "QF/gib_buffer.h" +#include "QF/gib_process.h" + +/* Co-recursive parsing functions */ + +inline char +GIB_Parse_Match_Dquote (const char *str, unsigned int *i) +{ + for ((*i)++; str[*i]; (*i)++) { + if (str[*i] == '\"') + return 0; + } + return '\"'; +} + +inline char +GIB_Parse_Match_Brace (const char *str, unsigned int *i) +{ + char c; + for ((*i)++; str[*i]; (*i)++) { + if (str[*i] == '\"') { + if ((c = GIB_Parse_Match_Dquote (str, i))) + return c; + } + if (str[*i] == '{') { + if ((c = GIB_Parse_Match_Brace (str, i))) + return c; + } + if (str[*i] == '}') + return 0; + } + return '{'; +} + +void +GIB_Parse_Extract_Line (struct cbuf_s *cbuf) +{ + int i; + char c; + dstring_t *dstr = cbuf->buf; + + dstring_clearstr (cbuf->line); + + for (i = 0; dstr->str[i]; i++) { + if (dstr->str[i] == '{') { + if ((c = GIB_Parse_Match_Brace (dstr->str, &i))) { + Cbuf_Error ("parse", "Could not find match for character %c", c); + return; + } + } + else if (dstr->str[i] == '\n' || dstr->str[i] == ';') + break; + } + + if (i) { + dstring_insert (cbuf->line, dstr->str, i, 0); + Sys_DPrintf ("extracted line: %s\n", cbuf->line->str); + if (dstr->str[0]) + dstring_snip (dstr, 0, i); + } + else + dstring_clearstr (dstr); + return; + +} + +inline static char +GIB_Parse_Get_Token (const char *str, unsigned int *i, dstring_t *dstr) +{ + int n; + char c; + + n = *i; // Save start position + if (str[*i] == '\"') { + if ((c = GIB_Parse_Match_Dquote (str, i))) { + Cbuf_Error ("parse", "Could not find match for character %c", c); + return 0; // Parse error + } else { + dstring_insert (dstr, str+n+1, *i-n-1, 0); + return '\"'; + } + } else if (str[*i] == '{') { + if ((c = GIB_Parse_Match_Brace (str, i))) { + Cbuf_Error ("parse", "Could not find match for character %c", c); + return 0; // Parse error + } else { + dstring_insert (dstr, str+n+1, *i-n-1, 0); + return '{'; + } + } else { + while (str[*i] && !isspace(str[*i]) && str[*i] != ',') // find end of token + (*i)++; + dstring_insert (dstr, str+n, *i-n, 0); + return ' '; + } + return 0; // We should never get here +} + +void +GIB_Parse_Generate_Composite (struct cbuf_s *cbuf) +{ + cbuf_args_t *args = cbuf->args; + int i; + + dstring_clearstr (GIB_DATA (cbuf)->arg_composite); + for (i = 0; i < args->argc; i++) { + args->args[i] = (const char *) strlen (GIB_DATA (cbuf)->arg_composite->str); + dstring_appendstr (GIB_DATA (cbuf)->arg_composite, args->argv[i]->str); + dstring_appendstr (GIB_DATA (cbuf)->arg_composite, " "); + } + GIB_DATA (cbuf)->arg_composite->str[strlen(GIB_DATA (cbuf)->arg_composite->str)-1] = 0; + for (i = 0; i < args->argc; i++) + args->args[i] += (unsigned long int) GIB_DATA (cbuf)->arg_composite->str; +} + +void +GIB_Parse_Tokenize_Line (struct cbuf_s *cbuf) +{ + dstring_t *arg = dstring_newstr (); + const char *str = cbuf->line->str; + cbuf_args_t *args = cbuf->args; + qboolean cat = false; + char delim; + int i; + + cbuf->args->argc = 0; + + for (i = 0;str[i];) { + while (isspace(str[i])) // Eliminate whitespace + i++; + if (!str[i]) // Blank token + break; + if (str[i] == ',') { // Concatenation + cat = true; + i++; + continue; + } + delim = GIB_Parse_Get_Token (str, &i, arg); + if (!delim) + break; + Sys_DPrintf("Got token: %s\n", arg->str); + if (*arg->str == '$' && delim == ' ') + GIB_Process_Variable (arg); + if (cat) { + dstring_appendstr (args->argv[args->argc-1], arg->str); + cat = false; + } else + Cbuf_ArgsAdd (args, arg->str); + if (delim != ' ') + i++; + dstring_clearstr (arg); + } + dstring_delete (arg); + GIB_Parse_Generate_Composite (cbuf); +} diff --git a/libs/util/gib_process.c b/libs/util/gib_process.c new file mode 100644 index 000000000..ca98559de --- /dev/null +++ b/libs/util/gib_process.c @@ -0,0 +1,49 @@ +/* + #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 +#include + +void +GIB_Process_Variable (struct dstring_s *token) +{ + int i; + cvar_t *var; + + for (i = 0; token->str[i] == '$'; i++); + i--; + for (; i >= 0; i--) { + var = Cvar_FindVar (token->str+i+1); + if (!var) + return; + token->str[i] = 0; + dstring_appendstr (token, var->string); + } +} diff --git a/qw/source/sv_main.c b/qw/source/sv_main.c index eec955434..3915e0333 100644 --- a/qw/source/sv_main.c +++ b/qw/source/sv_main.c @@ -68,6 +68,8 @@ static const char rcsid[] = #include "QF/cbuf.h" #include "QF/idparse.h" +#include "QF/gib_parse.h" +#include "QF/gib_buffer.h" #include "QF/cmd.h" #include "QF/console.h" #include "QF/cvar.h" @@ -2406,7 +2408,8 @@ SV_Init (void) // COM_AddParm ("-game"); // COM_AddParm ("qw"); - sv_cbuf = Cbuf_New (COM_extract_line, COM_parse_line, NULL, NULL); + sv_cbuf = Cbuf_New (GIB_Parse_Extract_Line, GIB_Parse_Tokenize_Line, + GIB_Buffer_Construct, GIB_Buffer_Destruct); sv_args = Cbuf_ArgsNew (); Sys_RegisterShutdown (SV_Shutdown);