2002-08-02 04:07:25 +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
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/dstring.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"
|
|
|
|
|
|
|
|
// Interpreter structure and prototypes
|
|
|
|
|
|
|
|
void GIB_Parse_Extract_Line (cbuf_t *cbuf);
|
|
|
|
void GIB_Parse_Tokenize_Line (cbuf_t *cbuf);
|
|
|
|
void GIB_Parse_Execute_Line (cbuf_t *cbuf);
|
|
|
|
|
|
|
|
cbuf_interpreter_t gib_interp = {
|
|
|
|
GIB_Parse_Extract_Line,
|
|
|
|
GIB_Parse_Tokenize_Line,
|
|
|
|
GIB_Parse_Execute_Line,
|
|
|
|
GIB_Buffer_Construct,
|
|
|
|
GIB_Buffer_Destruct,
|
|
|
|
};
|
|
|
|
|
2002-08-02 04:07:25 +00:00
|
|
|
|
|
|
|
/* Co-recursive parsing functions */
|
|
|
|
|
|
|
|
inline char
|
|
|
|
GIB_Parse_Match_Dquote (const char *str, unsigned int *i)
|
|
|
|
{
|
|
|
|
for ((*i)++; str[*i]; (*i)++) {
|
2002-08-03 06:04:00 +00:00
|
|
|
if (str[*i] == '\n')
|
|
|
|
return '\"'; // Newlines should never occur inside quotes
|
2002-08-02 04:07:25 +00:00
|
|
|
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 '{';
|
|
|
|
}
|
|
|
|
|
2002-08-03 06:04:00 +00:00
|
|
|
inline char
|
|
|
|
GIB_Parse_Match_Paren (const char *str, unsigned int *i)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
for ((*i)++; str[*i]; (*i)++) {
|
|
|
|
if (str[*i] == '(') {
|
|
|
|
if ((c = GIB_Parse_Match_Paren (str, i)))
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
if (str[*i] == ')')
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return '(';
|
|
|
|
}
|
|
|
|
|
2002-08-02 04:07:25 +00:00
|
|
|
void
|
|
|
|
GIB_Parse_Extract_Line (struct cbuf_s *cbuf)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char c;
|
2002-08-03 06:04:00 +00:00
|
|
|
|
|
|
|
if (GIB_DATA(cbuf)->ret.waiting) // Not ready for next line
|
|
|
|
return;
|
|
|
|
|
2002-08-02 04:07:25 +00:00
|
|
|
dstring_t *dstr = cbuf->buf;
|
|
|
|
|
|
|
|
dstring_clearstr (cbuf->line);
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2002-08-02 04:07:25 +00:00
|
|
|
for (i = 0; dstr->str[i]; i++) {
|
|
|
|
if (dstr->str[i] == '{') {
|
|
|
|
if ((c = GIB_Parse_Match_Brace (dstr->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;
|
|
|
|
}
|
2002-08-03 06:04:00 +00:00
|
|
|
} else if (dstr->str[i] == '\"') {
|
|
|
|
if ((c = GIB_Parse_Match_Dquote (dstr->str, &i))) {
|
|
|
|
Cbuf_Error ("parse", "Could not find matching %c", c);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (dstr->str[i] == '\n' || dstr->str[i] == ';')
|
2002-08-02 04:07:25 +00:00
|
|
|
break;
|
2002-08-03 06:04:00 +00:00
|
|
|
else if (dstr->str[i] == '/' && dstr->str[i+1] == '/') {
|
|
|
|
char *n;
|
|
|
|
if ((n = strchr (dstr->str+i, '\n')))
|
|
|
|
dstring_snip (dstr, i, n-dstr->str);
|
|
|
|
else {
|
|
|
|
dstring_snip (dstr, i, strlen(dstr->str+i));
|
|
|
|
i--; // So we don't go past null
|
|
|
|
}
|
|
|
|
}
|
2002-08-02 04:07:25 +00:00
|
|
|
}
|
|
|
|
|
2002-08-03 06:04:00 +00:00
|
|
|
if (dstr->str[0]) {
|
|
|
|
if (i) {
|
|
|
|
dstring_insert (cbuf->line, 0, dstr->str, i);
|
|
|
|
Sys_DPrintf ("extracted line: %s\n", cbuf->line->str);
|
|
|
|
}
|
|
|
|
dstring_snip (dstr, 0, i + (dstr->str[i] == '\n' || dstr->str[i] == ';'));
|
2002-08-02 04:07:25 +00:00
|
|
|
}
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2002-08-02 04:07:25 +00:00
|
|
|
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))) {
|
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-03 06:04:00 +00:00
|
|
|
dstring_insert (dstr, 0, str+n+1, *i-n-1);
|
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-03 06:04:00 +00:00
|
|
|
dstring_insert (dstr, 0, str+n+1, *i-n-1);
|
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 {
|
|
|
|
dstring_insert (dstr, 0, str+n+1, *i-n-1);
|
|
|
|
return '\(';
|
|
|
|
}
|
2002-08-02 04:07:25 +00:00
|
|
|
} else {
|
|
|
|
while (str[*i] && !isspace(str[*i]) && str[*i] != ',') // find end of token
|
|
|
|
(*i)++;
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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++) {
|
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-03 06:04:00 +00:00
|
|
|
inline void
|
|
|
|
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-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-03 06:04:00 +00:00
|
|
|
static qboolean cat = false;
|
2002-08-02 04:07:25 +00:00
|
|
|
char delim;
|
|
|
|
int i;
|
|
|
|
|
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) {
|
|
|
|
// FIXME: Call processing function here
|
|
|
|
if (GIB_DATA(cbuf)->ret.waiting) // Still not done?
|
|
|
|
return;
|
|
|
|
i = GIB_DATA(cbuf)->ret.line_pos; // Start where we left off
|
|
|
|
} else {
|
|
|
|
args->argc = 0; // Start from scratch
|
|
|
|
i = 0;
|
|
|
|
cat = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; str[i];) {
|
2002-08-02 04:07:25 +00:00
|
|
|
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);
|
2002-08-03 06:04:00 +00:00
|
|
|
|
|
|
|
// Global filters -- command sub and var sub
|
|
|
|
|
|
|
|
// FIXME: Command sub goes here with subroutine handling
|
|
|
|
|
|
|
|
if (delim != '{' && delim != '\"')
|
|
|
|
GIB_Process_Variables_All (arg);
|
|
|
|
|
|
|
|
if (delim == '(')
|
|
|
|
if (GIB_Process_Math (arg))
|
|
|
|
goto FILTER_ERROR; // GOTO??!?!?!? AHHHH!
|
|
|
|
|
|
|
|
// Add token to list
|
|
|
|
GIB_Parse_Add_Token (args, cat, arg);
|
|
|
|
if (cat)
|
2002-08-02 04:07:25 +00:00
|
|
|
cat = false;
|
2002-08-03 06:04:00 +00:00
|
|
|
|
|
|
|
if (delim != ' ') // Move into whitespace if we haven't already
|
2002-08-02 04:07:25 +00:00
|
|
|
i++;
|
|
|
|
dstring_clearstr (arg);
|
|
|
|
}
|
|
|
|
GIB_Parse_Generate_Composite (cbuf);
|
2002-08-03 06:04:00 +00:00
|
|
|
return;
|
|
|
|
FILTER_ERROR: // Error during filtering, clean up mess
|
|
|
|
dstring_clearstr (arg);
|
|
|
|
args->argc = 0;
|
|
|
|
return;
|
2002-08-02 04:07:25 +00:00
|
|
|
}
|
2002-08-03 06:04:00 +00:00
|
|
|
|
|
|
|
void GIB_Parse_Execute_Line (cbuf_t *cbuf)
|
|
|
|
{
|
|
|
|
gib_builtin_t *b;
|
|
|
|
gib_function_t *f;
|
|
|
|
|
|
|
|
if ((b = GIB_Builtin_Find (cbuf->args->argv[0]->str)))
|
|
|
|
b->func ();
|
|
|
|
else if ((f = GIB_Function_Find (cbuf->args->argv[0]->str))) {
|
|
|
|
cbuf_t *sub = Cbuf_New (&gib_interp);
|
|
|
|
Cbuf_AddText (sub, f->program->str);
|
|
|
|
if (cbuf_active->down)
|
|
|
|
Cbuf_DeleteStack (cbuf_active->down);
|
|
|
|
cbuf_active->down = sub;
|
|
|
|
sub->up = cbuf_active;
|
|
|
|
cbuf_active->state = CBUF_STATE_STACK;
|
|
|
|
} else
|
|
|
|
Cmd_Command (cbuf->args);
|
|
|
|
}
|
|
|
|
|