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
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-01-15 15:31:36 +00:00
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2002-08-03 06:04:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2002-08-14 05:28:07 +00:00
|
|
|
#include <stdlib.h>
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
#include "QF/va.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
#include "QF/dstring.h"
|
|
|
|
#include "QF/cbuf.h"
|
|
|
|
#include "QF/cvar.h"
|
2002-08-05 00:01:06 +00:00
|
|
|
#include "QF/gib_buffer.h"
|
2002-08-06 08:15:20 +00:00
|
|
|
#include "QF/gib_parse.h"
|
2002-08-08 09:20:00 +00:00
|
|
|
#include "QF/gib_vars.h"
|
2003-01-28 21:16:21 +00:00
|
|
|
#include "QF/gib_process.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
|
|
|
|
#include "exp.h"
|
2002-08-02 04:07:25 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Process_Variable (dstring_t *token, unsigned int *i)
|
2002-08-14 05:28:07 +00:00
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
hashtab_t *one = GIB_DATA(cbuf_active)->locals, *two = GIB_DATA(cbuf_active)->globals;
|
|
|
|
unsigned int n = *i, j, k, start = *i, index = 0, len, len2;
|
|
|
|
gib_var_t *var = 0;
|
|
|
|
char c;
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
(*i)++;
|
|
|
|
if (token->str[*i] == '{') {
|
|
|
|
if ((c = GIB_Parse_Match_Brace (token->str, i))) {
|
|
|
|
Cbuf_Error ("Parse", "Could not find match for %c.", c);
|
2002-08-14 05:28:07 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
n += 2;
|
|
|
|
len = 1;
|
|
|
|
} else {
|
|
|
|
for (; isalnum((byte) token->str[*i]) || token->str[*i] == '_'; (*i)++);
|
|
|
|
if (token->str[*i] == '[') {
|
|
|
|
if ((c = GIB_Parse_Match_Index (token->str, i)))
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
(*i)++;
|
2002-08-23 21:18:00 +00:00
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
n++;
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
c = token->str[*i];
|
|
|
|
token->str[*i] = 0;
|
|
|
|
|
|
|
|
for (k = n; token->str[n]; n++)
|
|
|
|
if (token->str[n] == '$' || token->str[n] == '#')
|
|
|
|
if (GIB_Process_Variable (token, &n))
|
|
|
|
return -1;
|
|
|
|
index = 0;
|
|
|
|
if (n && token->str[n-1] == ']')
|
|
|
|
for (j = n-1; j; j--)
|
|
|
|
if (token->str[j] == '[') {
|
|
|
|
index = atoi (token->str+j+1);
|
|
|
|
token->str[j] = 0;
|
|
|
|
}
|
|
|
|
if ((var = GIB_Var_Get (one, two, token->str+k)) && index < var->size && var->array[index]) {
|
|
|
|
if (token->str[start] == '#')
|
|
|
|
str = va("%u", var->size - index);
|
|
|
|
else
|
|
|
|
str = var->array[index]->str;
|
2002-08-14 05:28:07 +00:00
|
|
|
} else
|
2003-01-28 21:16:21 +00:00
|
|
|
str = "";
|
|
|
|
token->str[n] = c;
|
|
|
|
len += n-start;
|
|
|
|
len2 = strlen (str);
|
|
|
|
dstring_replace (token, start, len, str, len2);
|
|
|
|
*i = start + len2 - 1;
|
2002-08-14 05:28:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
int
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Process_Math (struct dstring_s *token, unsigned int i)
|
2002-10-13 05:52:06 +00:00
|
|
|
{
|
|
|
|
double value;
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
value = EXP_Evaluate (token->str+i);
|
2002-10-13 05:52:06 +00:00
|
|
|
if (EXP_ERROR) {
|
2002-11-13 03:03:37 +00:00
|
|
|
Cbuf_Error ("math", "Expression \"%s\" caused an error:\n%s", token->str, EXP_GetErrorMsg());
|
2002-10-13 05:52:06 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
2003-01-28 21:16:21 +00:00
|
|
|
token->str[i] = 0;
|
|
|
|
token->size = i+1;
|
|
|
|
dasprintf (token, "%.10g", value);
|
2002-10-13 05:52:06 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
int
|
|
|
|
GIB_Process_Embedded (gib_tree_t *node, cbuf_args_t *args)
|
2002-08-03 06:04:00 +00:00
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
unsigned int n, j;
|
|
|
|
gib_var_t *var;
|
|
|
|
gib_tree_t *cur;
|
|
|
|
unsigned int index, prev = 0;
|
|
|
|
const char *str = node->str;
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
for (cur = node->children; cur; cur = cur->next) {
|
|
|
|
if (cur->start > prev)
|
|
|
|
dstring_appendsubstr (args->argv[args->argc-1], str+prev, cur->start - prev);
|
|
|
|
prev = cur->end;
|
|
|
|
if (!cur->str) {
|
|
|
|
struct gib_dsarray_s *retvals = GIB_DATA(cbuf_active)->stack.values+GIB_DATA(cbuf_active)->stack.p-1;
|
|
|
|
if (retvals->size != 1) {
|
|
|
|
if (!*args->argv[args->argc-1]->str)
|
|
|
|
args->argc--;
|
|
|
|
for (j = 0; j < retvals->size; j++) {
|
|
|
|
Cbuf_ArgsAdd (args, retvals->dstrs[j]->str);
|
|
|
|
args->argm[args->argc-1] = node;
|
2002-08-23 07:45:46 +00:00
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
if (str[prev] && retvals->size) // Still more stuff left?
|
|
|
|
Cbuf_ArgsAdd (args, "");
|
|
|
|
} else
|
|
|
|
dstring_appendstr (args->argv[args->argc-1], retvals->dstrs[0]->str);
|
|
|
|
GIB_Buffer_Pop_Sstack (cbuf_active);
|
|
|
|
} else if (cur->flags & TREE_P_EMBED) {
|
|
|
|
n = args->argv[args->argc-1]->size-1;
|
|
|
|
if (cur->delim == '$')
|
|
|
|
dstring_appendstr (args->argv[args->argc-1], "${");
|
|
|
|
else
|
|
|
|
dstring_appendstr (args->argv[args->argc-1], "#{");
|
|
|
|
dstring_appendstr (args->argv[args->argc-1], cur->str);
|
|
|
|
dstring_appendstr (args->argv[args->argc-1], "}");
|
|
|
|
if (GIB_Process_Variable (args->argv[args->argc-1], &n))
|
|
|
|
return -1;
|
|
|
|
} else if ((var = GIB_Var_Get_Complex (&GIB_DATA(cbuf_active)->locals, &GIB_DATA(cbuf_active)->globals, (char *)cur->str, &index, false))) {
|
|
|
|
if (cur->delim == '$')
|
|
|
|
dstring_appendstr (args->argv[args->argc-1], var->array[index]->str);
|
|
|
|
else
|
|
|
|
dasprintf (args->argv[args->argc-1], "%u", var->size - index);
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
if (str[prev])
|
|
|
|
dstring_appendstr (args->argv[args->argc-1], str+prev);
|
2002-10-13 05:52:06 +00:00
|
|
|
return 0;
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
void
|
|
|
|
GIB_Process_Escapes (char *str)
|
2002-08-22 22:43:47 +00:00
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
int i, j;
|
|
|
|
char c;
|
|
|
|
for (i = 0, j = 0; str[i]; j++) {
|
|
|
|
if (str[i] == '\\') {
|
|
|
|
i++;
|
|
|
|
if (isdigit ((byte) str[i]) &&
|
|
|
|
isdigit ((byte) str[i+1]) &&
|
|
|
|
isdigit ((byte) str[i+2])) {
|
2002-10-26 03:09:38 +00:00
|
|
|
unsigned int num;
|
2003-01-28 21:16:21 +00:00
|
|
|
if ((num = 100 * (str[i] - '0') + 10 * (str[i+1] - '0') + (str[i+2] - '0')) <= 255) {
|
|
|
|
c = (char) num;
|
|
|
|
i += 3;
|
|
|
|
} else
|
|
|
|
c = '\\';
|
|
|
|
} else switch (str[i++]) {
|
|
|
|
case 'n':
|
|
|
|
c = '\n';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
c = '\t';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
c = '\r';
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
c = '"';
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
c = '\\';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
c = '\\';
|
|
|
|
i--;
|
|
|
|
break;
|
2002-10-16 04:59:34 +00:00
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
str[j] = c;
|
|
|
|
} else
|
|
|
|
str[j] = str[i++];
|
2002-08-22 22:43:47 +00:00
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
str[j] = 0;
|
2002-08-05 00:01:06 +00:00
|
|
|
}
|