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
|
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used))
|
2003-02-14 22:42:11 +00:00
|
|
|
const char rcsid[] = "$Id$";
|
2003-01-15 15:31:36 +00:00
|
|
|
|
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"
|
2003-04-13 22:07:58 +00:00
|
|
|
#include "QF/gib.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
|
|
|
|
#include "exp.h"
|
2003-04-13 22:07:58 +00:00
|
|
|
#include "gib_buffer.h"
|
|
|
|
#include "gib_vars.h"
|
|
|
|
#include "gib_process.h"
|
2002-08-02 04:07:25 +00:00
|
|
|
|
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
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
double value;
|
|
|
|
|
|
|
|
value = EXP_Evaluate (token->str + i);
|
2002-10-13 05:52:06 +00:00
|
|
|
if (EXP_ERROR) {
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_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;
|
2003-02-14 22:42:11 +00:00
|
|
|
token->size = i + 1;
|
2003-01-28 21:16:21 +00:00
|
|
|
dasprintf (token, "%.10g", value);
|
2002-10-13 05:52:06 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
int
|
2003-02-14 22:42:11 +00:00
|
|
|
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;
|
2003-02-14 22:42:11 +00:00
|
|
|
gib_var_t *var;
|
|
|
|
cvar_t *cvar;
|
2003-01-28 21:16:21 +00:00
|
|
|
gib_tree_t *cur;
|
|
|
|
unsigned int index, prev = 0;
|
|
|
|
const char *str = node->str;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
for (cur = node->children; cur; cur = cur->next) {
|
|
|
|
if (cur->start > prev)
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_appendsubstr (args->argv[args->argc - 1], str + prev,
|
|
|
|
cur->start - prev);
|
2003-01-28 21:16:21 +00:00
|
|
|
prev = cur->end;
|
|
|
|
if (!cur->str) {
|
2003-02-14 22:42:11 +00:00
|
|
|
struct gib_dsarray_s *retvals =
|
|
|
|
GIB_DATA (cbuf_active)->stack.values +
|
|
|
|
GIB_DATA (cbuf_active)->stack.p - 1;
|
2003-01-28 21:16:21 +00:00
|
|
|
if (retvals->size != 1) {
|
2003-02-14 22:42:11 +00:00
|
|
|
if (!*args->argv[args->argc - 1]->str)
|
2003-01-28 21:16:21 +00:00
|
|
|
args->argc--;
|
|
|
|
for (j = 0; j < retvals->size; j++) {
|
|
|
|
Cbuf_ArgsAdd (args, retvals->dstrs[j]->str);
|
2003-02-14 22:42:11 +00:00
|
|
|
args->argm[args->argc - 1] = node;
|
2002-08-23 07:45:46 +00:00
|
|
|
}
|
2003-04-11 02:57:11 +00:00
|
|
|
if (str[prev] && retvals->size) // Still more stuff left?
|
2003-01-28 21:16:21 +00:00
|
|
|
Cbuf_ArgsAdd (args, "");
|
|
|
|
} else
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_appendstr (args->argv[args->argc - 1],
|
|
|
|
retvals->dstrs[0]->str);
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Buffer_Pop_Sstack (cbuf_active);
|
2003-02-25 06:52:27 +00:00
|
|
|
} else if (cur->flags & TREE_A_EMBED) {
|
2003-02-14 22:42:11 +00:00
|
|
|
n = args->argv[args->argc - 1]->size - 1;
|
|
|
|
dstring_appendstr (args->argv[args->argc - 1], cur->str);
|
2003-02-16 02:44:24 +00:00
|
|
|
var = GIB_Var_Get_Very_Complex (&GIB_DATA (cbuf_active)->locals,
|
|
|
|
&GIB_DATA (cbuf_active)->globals,
|
|
|
|
args->argv[args->argc - 1], n, &index, false);
|
|
|
|
cvar = Cvar_FindVar (args->argv[args->argc - 1]->str);
|
|
|
|
args->argv[args->argc - 1]->size = n+1;
|
|
|
|
args->argv[args->argc - 1]->str[n] = 0;
|
|
|
|
if (var) {
|
|
|
|
if (cur->delim == '#')
|
|
|
|
dasprintf (args->argv[args->argc - 1], "%u", var->size);
|
|
|
|
else
|
|
|
|
dstring_appendstr (args->argv[args->argc - 1], var->array[index].value->str);
|
|
|
|
} else if (cur->delim == '#')
|
|
|
|
dstring_appendstr (args->argv[args->argc - 1], "0");
|
|
|
|
else if (cvar)
|
|
|
|
dstring_appendstr (args->argv[args->argc - 1], cvar->string);
|
|
|
|
} else if ((var = GIB_Var_Get_Complex (&GIB_DATA (cbuf_active)->locals,
|
2003-02-14 22:42:11 +00:00
|
|
|
&GIB_DATA (cbuf_active)->globals,
|
|
|
|
(char *) cur->str, &index, false))) {
|
2003-01-28 21:16:21 +00:00
|
|
|
if (cur->delim == '$')
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_appendstr (args->argv[args->argc - 1],
|
2003-02-16 02:44:24 +00:00
|
|
|
var->array[index].value->str);
|
2003-01-28 21:16:21 +00:00
|
|
|
else
|
2003-02-14 22:42:11 +00:00
|
|
|
dasprintf (args->argv[args->argc - 1], "%u", var->size - index);
|
2003-01-30 20:43:57 +00:00
|
|
|
} else if (cur->delim == '#')
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_appendstr (args->argv[args->argc - 1], "0");
|
2003-01-30 20:43:57 +00:00
|
|
|
else if ((cvar = Cvar_FindVar (cur->str)))
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_appendstr (args->argv[args->argc - 1], cvar->string);
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
if (str[prev])
|
2003-02-14 22:42:11 +00:00
|
|
|
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-02-14 22:42:11 +00:00
|
|
|
int i, j;
|
|
|
|
char c;
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
for (i = 0, j = 0; str[i]; j++) {
|
|
|
|
if (str[i] == '\\') {
|
|
|
|
i++;
|
|
|
|
if (isdigit ((byte) str[i]) &&
|
2003-02-14 22:42:11 +00:00
|
|
|
isdigit ((byte) str[i + 1]) && isdigit ((byte) str[i + 2])) {
|
|
|
|
unsigned int num;
|
|
|
|
|
|
|
|
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++]) {
|
2003-01-28 21:16:21 +00:00
|
|
|
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;
|
2003-02-14 22:42:11 +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
|
|
|
}
|