2002-08-02 04:07:25 +00:00
|
|
|
/*
|
2002-08-08 09:20:00 +00:00
|
|
|
gib_parse.c
|
2002-08-02 04:07:25 +00:00
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
GIB parser functions
|
2002-08-02 04:07:25 +00:00
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
Copyright (C) 2002 Brian Koropoff
|
2002-08-02 04:07:25 +00:00
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
Author: Brian Koropoff
|
2002-08-02 04:07:25 +00:00
|
|
|
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
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2002-08-02 04:07:25 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
2002-12-10 00:04:15 +00:00
|
|
|
#include <stdlib.h>
|
2002-08-02 04:07:25 +00:00
|
|
|
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/dstring.h"
|
2002-12-10 00:04:15 +00:00
|
|
|
#include "QF/va.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
#include "QF/cmd.h"
|
2002-08-02 04:07:25 +00:00
|
|
|
#include "QF/cbuf.h"
|
|
|
|
#include "QF/gib_buffer.h"
|
|
|
|
#include "QF/gib_process.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
#include "QF/gib_builtin.h"
|
|
|
|
#include "QF/gib_function.h"
|
2002-08-08 09:20:00 +00:00
|
|
|
#include "QF/gib_vars.h"
|
2002-08-22 22:43:47 +00:00
|
|
|
#include "QF/gib_parse.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
|
|
|
|
// Interpreter structure and prototypes
|
|
|
|
|
2002-11-05 19:12:51 +00:00
|
|
|
static void GIB_Parse_Execute_Line (cbuf_t *cbuf);
|
2002-08-03 06:04:00 +00:00
|
|
|
|
|
|
|
cbuf_interpreter_t gib_interp = {
|
|
|
|
GIB_Parse_Extract_Line,
|
|
|
|
GIB_Parse_Tokenize_Line,
|
|
|
|
GIB_Parse_Execute_Line,
|
|
|
|
GIB_Buffer_Construct,
|
|
|
|
GIB_Buffer_Destruct,
|
2002-11-16 20:56:04 +00:00
|
|
|
GIB_Buffer_Reset
|
2002-08-03 06:04:00 +00:00
|
|
|
};
|
|
|
|
|
2002-10-16 04:59:34 +00:00
|
|
|
/*
|
|
|
|
GIB_Escaped
|
|
|
|
|
|
|
|
Returns true if character i in str is
|
|
|
|
escaped with a backslash (and the backslash
|
|
|
|
is not itself escaped).
|
|
|
|
*/
|
2002-08-22 22:43:47 +00:00
|
|
|
inline qboolean
|
|
|
|
GIB_Escaped (const char *str, int i)
|
|
|
|
{
|
|
|
|
int n, c;
|
|
|
|
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
|
|
|
for (n = i - 1, c = 0; n >= 0 && str[n] == '\\'; n--, c++);
|
|
|
|
return c & 1;
|
|
|
|
}
|
|
|
|
|
2002-08-02 04:07:25 +00:00
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Parse_Match_Dquote
|
|
|
|
|
|
|
|
Progresses an index variable through a string
|
|
|
|
until a double quote is reached. Returns
|
|
|
|
0 on success, or the character it could not
|
|
|
|
match otherwise
|
|
|
|
*/
|
2002-08-05 00:01:06 +00:00
|
|
|
char
|
2002-08-02 04:07:25 +00:00
|
|
|
GIB_Parse_Match_Dquote (const char *str, unsigned int *i)
|
|
|
|
{
|
2002-08-29 22:12:51 +00:00
|
|
|
unsigned int n = *i;
|
2002-08-02 04:07:25 +00:00
|
|
|
for ((*i)++; str[*i]; (*i)++) {
|
2002-08-03 06:04:00 +00:00
|
|
|
if (str[*i] == '\n')
|
2002-11-19 04:15:36 +00:00
|
|
|
break;
|
2002-08-22 22:43:47 +00:00
|
|
|
else if (str[*i] == '\"' && !GIB_Escaped (str, *i))
|
2002-08-02 04:07:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-08-29 22:12:51 +00:00
|
|
|
*i = n;
|
2002-08-02 04:07:25 +00:00
|
|
|
return '\"';
|
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Parse_Match_Brace
|
|
|
|
|
|
|
|
Progresses an index variable through a string
|
|
|
|
until a closing brace (}) is reached. Calls
|
|
|
|
other matching functions to skip areas of a
|
|
|
|
string it does not want to handle. Returns
|
|
|
|
0 on success, or the character it could not
|
|
|
|
match otherwise.
|
|
|
|
*/
|
2002-08-05 00:01:06 +00:00
|
|
|
char
|
2002-08-02 04:07:25 +00:00
|
|
|
GIB_Parse_Match_Brace (const char *str, unsigned int *i)
|
|
|
|
{
|
|
|
|
char c;
|
2002-08-29 22:12:51 +00:00
|
|
|
unsigned int n = *i;
|
2002-08-02 04:07:25 +00:00
|
|
|
for ((*i)++; str[*i]; (*i)++) {
|
|
|
|
if (str[*i] == '\"') {
|
|
|
|
if ((c = GIB_Parse_Match_Dquote (str, i)))
|
|
|
|
return c;
|
2002-08-06 08:15:20 +00:00
|
|
|
} else if (str[*i] == '{') {
|
2002-08-02 04:07:25 +00:00
|
|
|
if ((c = GIB_Parse_Match_Brace (str, i)))
|
|
|
|
return c;
|
2002-08-06 08:15:20 +00:00
|
|
|
} else if (str[*i] == '}')
|
2002-08-02 04:07:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-08-29 22:12:51 +00:00
|
|
|
*i = n;
|
2002-08-02 04:07:25 +00:00
|
|
|
return '{';
|
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Parse_Match_Paren
|
|
|
|
|
|
|
|
Progresses an index variable through a string
|
|
|
|
until a closing parenthesis is reached. Calls
|
|
|
|
other matching functions to skip areas of a
|
|
|
|
string it does not want to handle. Returns
|
|
|
|
0 on success, or the character it could not
|
|
|
|
match otherwise.
|
|
|
|
*/
|
2002-08-05 00:01:06 +00:00
|
|
|
char
|
2002-08-03 06:04:00 +00:00
|
|
|
GIB_Parse_Match_Paren (const char *str, unsigned int *i)
|
|
|
|
{
|
|
|
|
char c;
|
2002-08-29 22:12:51 +00:00
|
|
|
unsigned int n = *i;
|
2002-08-03 06:04:00 +00:00
|
|
|
for ((*i)++; str[*i]; (*i)++) {
|
|
|
|
if (str[*i] == '(') {
|
|
|
|
if ((c = GIB_Parse_Match_Paren (str, i)))
|
|
|
|
return c;
|
2002-11-19 04:15:36 +00:00
|
|
|
} else if (str[*i] == '\"') {
|
|
|
|
if ((c = GIB_Parse_Match_Dquote (str, i)))
|
|
|
|
return c;
|
2002-08-06 08:15:20 +00:00
|
|
|
} else if (str[*i] == ')')
|
2002-08-03 06:04:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-08-29 22:12:51 +00:00
|
|
|
*i = n;
|
2002-08-03 06:04:00 +00:00
|
|
|
return '(';
|
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Parse_Match_Backtick
|
|
|
|
|
|
|
|
Progresses an index variable through a string
|
|
|
|
until a backtick (`) is reached. Calls
|
|
|
|
other matching functions to skip areas of a
|
|
|
|
string it does not want to handle. Returns
|
|
|
|
0 on success, or the character it could not
|
|
|
|
match otherwise.
|
|
|
|
*/
|
2002-08-05 00:01:06 +00:00
|
|
|
char
|
2002-08-07 06:17:50 +00:00
|
|
|
GIB_Parse_Match_Backtick (const char *str, unsigned int *i)
|
2002-08-05 00:01:06 +00:00
|
|
|
{
|
|
|
|
char c;
|
2002-08-29 22:12:51 +00:00
|
|
|
unsigned int n = *i;
|
2002-08-05 00:01:06 +00:00
|
|
|
for ((*i)++; str[*i]; (*i)++) {
|
2002-08-07 06:17:50 +00:00
|
|
|
if (str[*i] == '`')
|
2002-08-05 00:01:06 +00:00
|
|
|
return 0;
|
2002-08-08 09:20:00 +00:00
|
|
|
else if (str[*i] == '\"') { // Skip over strings as usual
|
2002-08-05 00:01:06 +00:00
|
|
|
if ((c = GIB_Parse_Match_Dquote (str, i)))
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
2002-08-29 22:12:51 +00:00
|
|
|
*i = n;
|
2002-08-07 06:17:50 +00:00
|
|
|
return '`';
|
2002-08-05 00:01:06 +00:00
|
|
|
}
|
|
|
|
|
2002-10-16 04:59:34 +00:00
|
|
|
/*
|
|
|
|
GIB_Parse_Match_Index
|
|
|
|
|
|
|
|
Progresses an index variable through a string
|
|
|
|
until a normal brace (]) is reached. Calls
|
|
|
|
other matching functions to skip areas of a
|
|
|
|
string it does not want to handle. Returns
|
|
|
|
0 on success, or the character it could not
|
|
|
|
match otherwise.
|
|
|
|
*/
|
2002-08-14 05:28:07 +00:00
|
|
|
char
|
|
|
|
GIB_Parse_Match_Index (const char *str, unsigned int *i)
|
|
|
|
{
|
2002-08-29 22:12:51 +00:00
|
|
|
unsigned int n = *i;
|
2002-08-14 05:28:07 +00:00
|
|
|
for ((*i)++; str[*i]; (*i)++) {
|
|
|
|
if (str[*i] == ']')
|
|
|
|
return 0;
|
|
|
|
}
|
2002-08-29 22:12:51 +00:00
|
|
|
*i = n;
|
2002-08-14 05:28:07 +00:00
|
|
|
return '[';
|
|
|
|
}
|
|
|
|
|
2002-10-16 04:59:34 +00:00
|
|
|
/*
|
|
|
|
GIB_Parse_Strip_Comments
|
|
|
|
|
|
|
|
Finds instances of // comments in a cbuf
|
|
|
|
outside of double quotes and snips between
|
|
|
|
the // and the next newline or the end of
|
|
|
|
the string.
|
|
|
|
*/
|
2002-09-10 01:26:02 +00:00
|
|
|
void
|
|
|
|
GIB_Parse_Strip_Comments (struct cbuf_s *cbuf)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
dstring_t *dstr = cbuf->buf;
|
|
|
|
char c, *n;
|
|
|
|
|
|
|
|
for (i = 0; dstr->str[i]; i++) {
|
|
|
|
if (dstr->str[i] == '\"') {
|
|
|
|
if ((c = GIB_Parse_Match_Dquote (dstr->str, &i)))
|
|
|
|
// We don't care about parse errors here.
|
|
|
|
// Let the parser sort it out later.
|
|
|
|
return;
|
|
|
|
} else if (dstr->str[i] == '/' && dstr->str[i+1] == '/') {
|
|
|
|
if ((n = strchr (dstr->str+i, '\n'))) {
|
|
|
|
dstring_snip (dstr, i, n-dstr->str-i);
|
|
|
|
i--;
|
|
|
|
} else {
|
|
|
|
dstring_snip (dstr, i, strlen(dstr->str+i));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Parse_Extract_Line
|
|
|
|
|
|
|
|
Extracts the next command from a cbuf and
|
|
|
|
deposits it in cbuf->line, removing the
|
|
|
|
portion used from the buffer
|
|
|
|
*/
|
2002-08-02 04:07:25 +00:00
|
|
|
void
|
|
|
|
GIB_Parse_Extract_Line (struct cbuf_s *cbuf)
|
|
|
|
{
|
2002-08-29 22:12:51 +00:00
|
|
|
unsigned int i;
|
2002-08-02 04:07:25 +00:00
|
|
|
char c;
|
|
|
|
dstring_t *dstr = cbuf->buf;
|
|
|
|
|
2002-08-05 05:58:17 +00:00
|
|
|
if (GIB_DATA(cbuf)->ret.waiting ) // Not ready for the next line
|
|
|
|
return;
|
|
|
|
|
2002-08-02 04:07:25 +00:00
|
|
|
dstring_clearstr (cbuf->line);
|
2002-08-05 05:58:17 +00:00
|
|
|
|
2002-08-02 04:07:25 +00:00
|
|
|
for (i = 0; dstr->str[i]; i++) {
|
|
|
|
if (dstr->str[i] == '{') {
|
2002-08-29 22:12:51 +00:00
|
|
|
if ((c = GIB_Parse_Match_Brace (dstr->str, &i)))
|
|
|
|
goto PARSE_ERROR;
|
2002-08-03 06:04:00 +00:00
|
|
|
} else if (dstr->str[i] == '\"') {
|
2002-08-29 22:12:51 +00:00
|
|
|
if ((c = GIB_Parse_Match_Dquote (dstr->str, &i)))
|
|
|
|
goto PARSE_ERROR;
|
2002-08-03 06:04:00 +00:00
|
|
|
} else if (dstr->str[i] == '\n' || dstr->str[i] == ';')
|
2002-08-02 04:07:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-12-13 23:36:05 +00:00
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
if (dstr->str[0]) { // If something is left in the buffer
|
2002-10-16 04:59:34 +00:00
|
|
|
if (i)// If we used any of it
|
2002-08-03 06:04:00 +00:00
|
|
|
dstring_insert (cbuf->line, 0, dstr->str, i);
|
2002-12-13 23:36:05 +00:00
|
|
|
// Clip out what we used
|
|
|
|
while (isspace ((byte) dstr->str[i]) || dstr->str[i] == ';') // Eliminate white space/null statements
|
|
|
|
i++;
|
|
|
|
dstring_snip (dstr, 0, i);
|
2002-08-02 04:07:25 +00:00
|
|
|
}
|
2002-08-28 08:28:05 +00:00
|
|
|
return;
|
2002-08-29 22:12:51 +00:00
|
|
|
|
|
|
|
PARSE_ERROR: // Extract out the line where the parse error occurred
|
|
|
|
|
|
|
|
for (; i && dstr->str[i] != '\n'; i--);
|
|
|
|
if (dstr->str[i] == '\n')
|
|
|
|
i++;
|
|
|
|
dstring_clearstr (cbuf->line);
|
|
|
|
dstring_appendstr (cbuf->line, dstr->str+i);
|
|
|
|
Cbuf_Error ("parse", "Could not find match for %c.", c);
|
2002-08-02 04:07:25 +00:00
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Parse_Get_Token
|
|
|
|
|
|
|
|
Progresses an index variable through a string
|
|
|
|
until the end of the next token is found.
|
|
|
|
Stores the token in dstr with or without
|
|
|
|
wrapping characters as specified by include_delim.
|
|
|
|
Returns 0 on error or the wrapping character of
|
|
|
|
the token otherwise
|
|
|
|
*/
|
2002-08-02 04:07:25 +00:00
|
|
|
inline static char
|
2002-08-07 06:17:50 +00:00
|
|
|
GIB_Parse_Get_Token (const char *str, unsigned int *i, dstring_t *dstr, qboolean include_delim)
|
2002-08-02 04:07:25 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
n = *i; // Save start position
|
|
|
|
if (str[*i] == '\"') {
|
|
|
|
if ((c = GIB_Parse_Match_Dquote (str, i))) {
|
2002-08-03 06:04:00 +00:00
|
|
|
Cbuf_Error ("parse", "Could not find matching %c", c);
|
2002-08-02 04:07:25 +00:00
|
|
|
return 0; // Parse error
|
|
|
|
} else {
|
2002-08-07 06:17:50 +00:00
|
|
|
dstring_insert (dstr, 0, str+n+!include_delim, *i-n+1-!include_delim-!include_delim);
|
2002-08-02 04:07:25 +00:00
|
|
|
return '\"';
|
|
|
|
}
|
|
|
|
} else if (str[*i] == '{') {
|
|
|
|
if ((c = GIB_Parse_Match_Brace (str, i))) {
|
2002-08-03 06:04:00 +00:00
|
|
|
Cbuf_Error ("parse", "Could not find matching %c", c);
|
2002-08-02 04:07:25 +00:00
|
|
|
return 0; // Parse error
|
|
|
|
} else {
|
2002-08-07 06:17:50 +00:00
|
|
|
dstring_insert (dstr, 0, str+n+!include_delim, *i-n+1-!include_delim-!include_delim);
|
2002-08-02 04:07:25 +00:00
|
|
|
return '{';
|
|
|
|
}
|
2002-08-03 06:04:00 +00:00
|
|
|
} else if (str[*i] == '(') {
|
|
|
|
if ((c = GIB_Parse_Match_Paren (str, i))) {
|
|
|
|
Cbuf_Error ("parse", "Could not find matching %c", c);
|
|
|
|
return 0; // Parse error
|
|
|
|
} else {
|
2002-08-07 06:17:50 +00:00
|
|
|
dstring_insert (dstr, 0, str+n+!include_delim, *i-n+1-!include_delim-!include_delim);
|
2002-08-03 06:04:00 +00:00
|
|
|
return '\(';
|
|
|
|
}
|
2002-08-02 04:07:25 +00:00
|
|
|
} else {
|
2002-08-09 04:06:17 +00:00
|
|
|
while (str[*i] && !isspace((byte)str[*i]) && str[*i] != ',') { // find end of token
|
2002-08-07 06:17:50 +00:00
|
|
|
if (str[*i] == '`') {
|
|
|
|
if ((c = GIB_Parse_Match_Backtick (str, i))) {
|
2002-11-19 04:15:36 +00:00
|
|
|
Cbuf_Error ("parse", "Could not find matching %c", c);
|
|
|
|
return 0; // Parse error
|
2002-08-05 00:01:06 +00:00
|
|
|
}
|
2002-11-19 04:15:36 +00:00
|
|
|
} else if (str[*i] == '(') {
|
|
|
|
if ((c = GIB_Parse_Match_Paren (str, i))) {
|
|
|
|
Cbuf_Error ("parse", "Could not find matching %c", c);
|
|
|
|
return 0; // Parse error
|
|
|
|
}
|
|
|
|
} else if (str[*i] == '{') {
|
2002-08-08 09:20:00 +00:00
|
|
|
if ((c = GIB_Parse_Match_Brace (str, i))) {
|
2002-11-19 04:15:36 +00:00
|
|
|
Cbuf_Error ("parse", "Could not find matching %c", c);
|
|
|
|
return 0; // Parse error
|
2002-08-08 09:20:00 +00:00
|
|
|
}
|
|
|
|
}
|
2002-08-02 04:07:25 +00:00
|
|
|
(*i)++;
|
2002-08-05 00:01:06 +00:00
|
|
|
}
|
2002-08-03 06:04:00 +00:00
|
|
|
dstring_insert (dstr, 0, str+n, *i-n);
|
2002-08-02 04:07:25 +00:00
|
|
|
return ' ';
|
|
|
|
}
|
|
|
|
return 0; // We should never get here
|
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Parse_Generate_Composite
|
|
|
|
|
|
|
|
Concatenates all parsed arguments in cbuf
|
|
|
|
into a single string and creates an array
|
|
|
|
of pointers to the beginnings of tokens
|
|
|
|
within it.
|
|
|
|
*/
|
2002-10-16 04:59:34 +00:00
|
|
|
inline static void
|
2002-08-02 04:07:25 +00:00
|
|
|
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++) {
|
2002-08-03 06:04:00 +00:00
|
|
|
// ->str could be moved by realloc when a dstring is resized
|
|
|
|
// so save the offset instead of the pointer
|
2002-08-02 04:07:25 +00:00
|
|
|
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, " ");
|
|
|
|
}
|
2002-08-03 06:04:00 +00:00
|
|
|
|
|
|
|
// Get rid of trailing space
|
2002-08-02 04:07:25 +00:00
|
|
|
GIB_DATA (cbuf)->arg_composite->str[strlen(GIB_DATA (cbuf)->arg_composite->str)-1] = 0;
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2002-08-02 04:07:25 +00:00
|
|
|
for (i = 0; i < args->argc; i++)
|
2002-08-03 06:04:00 +00:00
|
|
|
// now that arg_composite is done we can add the pointer to the stored
|
|
|
|
// offsets and get valid pointers. This *should* be portable.
|
2002-08-02 04:07:25 +00:00
|
|
|
args->args[i] += (unsigned long int) GIB_DATA (cbuf)->arg_composite->str;
|
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Parse_Add_Token
|
|
|
|
|
|
|
|
Adds a new token to the argument list args
|
|
|
|
or concatenates it to the end of the last
|
|
|
|
according to cat.
|
|
|
|
*/
|
2002-10-16 04:59:34 +00:00
|
|
|
inline static void
|
2002-08-03 06:04:00 +00:00
|
|
|
GIB_Parse_Add_Token (struct cbuf_args_s *args, qboolean cat, dstring_t *token)
|
|
|
|
{
|
|
|
|
if (cat) {
|
|
|
|
dstring_appendstr (args->argv[args->argc-1], token->str);
|
|
|
|
} else
|
|
|
|
Cbuf_ArgsAdd (args, token->str);
|
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Parse_Tokenize_Line
|
|
|
|
|
|
|
|
Tokenizes and processes an extracted command,
|
|
|
|
producing an argument list suitable for executing
|
|
|
|
the command. This function can be interrupted
|
|
|
|
to call a GIB subroutine, in which case it will
|
|
|
|
save important variables and start where it left
|
|
|
|
off when called again.
|
|
|
|
*/
|
2002-08-02 04:07:25 +00:00
|
|
|
void
|
|
|
|
GIB_Parse_Tokenize_Line (struct cbuf_s *cbuf)
|
|
|
|
{
|
2002-08-03 06:04:00 +00:00
|
|
|
dstring_t *arg = GIB_DATA(cbuf)->current_token;
|
2002-08-02 04:07:25 +00:00
|
|
|
const char *str = cbuf->line->str;
|
|
|
|
cbuf_args_t *args = cbuf->args;
|
2002-08-06 08:15:20 +00:00
|
|
|
qboolean cat = false;
|
2002-08-07 06:17:50 +00:00
|
|
|
int noprocess;
|
2002-08-02 04:07:25 +00:00
|
|
|
char delim;
|
2002-08-06 08:15:20 +00:00
|
|
|
int i;
|
2002-08-02 04:07:25 +00:00
|
|
|
|
2002-08-03 06:04:00 +00:00
|
|
|
// This function can be interrupted to call a GIB
|
|
|
|
// subroutine. First we need to clean up anything
|
|
|
|
// we left unfinished
|
|
|
|
|
|
|
|
// Do we have a left-over token that needs processing?
|
2002-08-02 04:07:25 +00:00
|
|
|
|
2002-08-03 06:04:00 +00:00
|
|
|
if (GIB_DATA(cbuf)->ret.waiting) {
|
2002-08-06 08:15:20 +00:00
|
|
|
if (GIB_Process_Token (arg, GIB_DATA(cbuf)->ret.delim))
|
|
|
|
return; // Still not done, or error
|
|
|
|
GIB_Parse_Add_Token (cbuf->args, GIB_DATA(cbuf)->ret.cat, arg);
|
|
|
|
i = GIB_DATA(cbuf)->ret.line_pos; // Start tokenizing where we left off
|
2002-08-07 06:17:50 +00:00
|
|
|
noprocess = GIB_DATA(cbuf)->ret.noprocess ? 1 : 0; // Past second token, no sense in having it be 2
|
2002-08-03 06:04:00 +00:00
|
|
|
} else {
|
|
|
|
args->argc = 0; // Start from scratch
|
|
|
|
i = 0;
|
2002-08-07 06:17:50 +00:00
|
|
|
noprocess = 0;
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|
2002-08-06 08:15:20 +00:00
|
|
|
|
2002-08-05 00:01:06 +00:00
|
|
|
while (str[i]) {
|
2002-08-09 04:06:17 +00:00
|
|
|
while (isspace((byte)str[i])) // Eliminate whitespace
|
2002-08-02 04:07:25 +00:00
|
|
|
i++;
|
|
|
|
if (!str[i]) // Blank token
|
|
|
|
break;
|
|
|
|
if (str[i] == ',') { // Concatenation
|
|
|
|
cat = true;
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
2002-08-06 08:15:20 +00:00
|
|
|
dstring_clearstr (arg);
|
2002-08-07 06:17:50 +00:00
|
|
|
delim = GIB_Parse_Get_Token (str, &i, arg, noprocess == 1);
|
2002-08-02 04:07:25 +00:00
|
|
|
if (!delim)
|
|
|
|
break;
|
|
|
|
Sys_DPrintf("Got token: %s\n", arg->str);
|
2002-08-07 06:17:50 +00:00
|
|
|
if (delim != ' ') // Move into whitespace if we haven't already
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (noprocess != 1)
|
|
|
|
if (GIB_Process_Token (arg, delim))
|
|
|
|
goto FILTER_ERROR; // Error or GIB subroutine needs to be called
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2002-08-07 06:17:50 +00:00
|
|
|
if (noprocess > 1)
|
|
|
|
noprocess--;
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2002-08-05 00:01:06 +00:00
|
|
|
GIB_Parse_Add_Token (cbuf->args, cat, arg);
|
2002-08-03 06:04:00 +00:00
|
|
|
if (cat)
|
2002-08-02 04:07:25 +00:00
|
|
|
cat = false;
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2002-08-07 06:17:50 +00:00
|
|
|
|
|
|
|
if (cbuf->args->argc == 1) {
|
|
|
|
gib_builtin_t *b;
|
|
|
|
if ((b = GIB_Builtin_Find (cbuf->args->argv[0]->str)))
|
|
|
|
noprocess = b->type;
|
|
|
|
}
|
2002-08-02 04:07:25 +00:00
|
|
|
}
|
|
|
|
GIB_Parse_Generate_Composite (cbuf);
|
2002-08-03 06:04:00 +00:00
|
|
|
return;
|
2002-08-06 08:15:20 +00:00
|
|
|
FILTER_ERROR:
|
|
|
|
GIB_DATA(cbuf)->ret.line_pos = i; // save our information in case
|
|
|
|
GIB_DATA(cbuf)->ret.cat = cat; // error is not fatal
|
|
|
|
GIB_DATA(cbuf)->ret.delim = delim;
|
2002-08-07 06:17:50 +00:00
|
|
|
GIB_DATA(cbuf)->ret.noprocess = noprocess;
|
2002-08-03 06:04:00 +00:00
|
|
|
return;
|
2002-08-02 04:07:25 +00:00
|
|
|
}
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2002-12-10 00:04:15 +00:00
|
|
|
inline qboolean
|
|
|
|
GIB_Parse_Execute_Varexp (cbuf_t *cbuf)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
const char *v;
|
|
|
|
double newval, other;
|
|
|
|
cbuf_args_t *args = cbuf->args;
|
|
|
|
if (args->argc == 1) {
|
|
|
|
s = args->argv[0]->str + strlen(args->argv[0]->str) - 2;
|
|
|
|
if (!strcmp (s, "++") || !strcmp (s, "--")) {
|
|
|
|
*s = 0;
|
|
|
|
if ((v = GIB_Var_Get (cbuf, args->argv[0]->str)))
|
|
|
|
newval = atof (v);
|
|
|
|
else
|
|
|
|
newval = 0.0;
|
|
|
|
switch (s[1]) {
|
|
|
|
case '+':
|
|
|
|
newval++;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
newval--;
|
|
|
|
}
|
|
|
|
GIB_Var_Set (cbuf, args->argv[0]->str, va("%.10g", newval));
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
} else if (args->argc == 3 && strlen(args->argv[1]->str) <= 2 && strchr(args->argv[1]->str, '=')) {
|
|
|
|
s = args->argv[1]->str;
|
|
|
|
if (*s == '=') {
|
|
|
|
GIB_Var_Set (cbuf, args->argv[0]->str, args->argv[2]->str);
|
|
|
|
return true;
|
|
|
|
} else if (s[1] == '=') {
|
|
|
|
if ((v = GIB_Var_Get (cbuf, args->argv[0]->str)))
|
|
|
|
newval = atof (v);
|
|
|
|
else
|
|
|
|
newval = 0.0;
|
|
|
|
other = atof (args->argv[2]->str);
|
|
|
|
switch (*s) {
|
|
|
|
case '+':
|
|
|
|
newval += other;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
newval -= other;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
newval *= other;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
newval /= other;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
GIB_Var_Set (cbuf, args->argv[0]->str, va("%.10g", newval));
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
GIB_Parse_Execute_Line
|
|
|
|
|
|
|
|
After an argument list has been created,
|
|
|
|
this function executes the appropriate command,
|
|
|
|
searching in this order:
|
|
|
|
|
|
|
|
GIB builtins
|
|
|
|
GIB functions
|
2002-08-14 05:28:07 +00:00
|
|
|
Assignment to a local/global variable
|
2002-08-08 09:20:00 +00:00
|
|
|
Normal quake console commands
|
|
|
|
*/
|
2002-12-10 00:04:15 +00:00
|
|
|
void
|
2002-11-05 19:12:51 +00:00
|
|
|
GIB_Parse_Execute_Line (cbuf_t *cbuf)
|
2002-08-03 06:04:00 +00:00
|
|
|
{
|
2002-08-07 06:17:50 +00:00
|
|
|
cbuf_args_t *args = cbuf->args;
|
2002-08-03 06:04:00 +00:00
|
|
|
gib_builtin_t *b;
|
|
|
|
gib_function_t *f;
|
|
|
|
|
2002-08-07 06:17:50 +00:00
|
|
|
if ((b = GIB_Builtin_Find (args->argv[0]->str)))
|
2002-08-03 06:04:00 +00:00
|
|
|
b->func ();
|
2002-08-27 04:47:49 +00:00
|
|
|
else if ((f = GIB_Function_Find (args->argv[0]->str))) {
|
|
|
|
cbuf_t *sub = Cbuf_New (&gib_interp);
|
|
|
|
GIB_Function_Execute (sub, f, cbuf_active->args);
|
|
|
|
cbuf_active->down = sub;
|
|
|
|
sub->up = cbuf_active;
|
|
|
|
cbuf_active->state = CBUF_STATE_STACK;
|
2002-12-10 00:04:15 +00:00
|
|
|
} else if (!GIB_Parse_Execute_Varexp(cbuf))
|
2002-08-03 06:04:00 +00:00
|
|
|
Cmd_Command (cbuf->args);
|
2002-08-07 06:17:50 +00:00
|
|
|
dstring_clearstr (cbuf->line);
|
2002-12-08 20:55:40 +00:00
|
|
|
// If this is a looping buffer and it is now empty,
|
|
|
|
// copy the loop program back in
|
|
|
|
if (GIB_DATA(cbuf)->type == GIB_BUFFER_LOOP && !cbuf->buf->str[0])
|
|
|
|
Cbuf_AddText (cbuf, GIB_DATA(cbuf)->loop_program->str);
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|
|
|
|
|