From 513b5737f921dd62e525bfcae71738bfe49401ea Mon Sep 17 00:00:00 2001 From: Brian Koropoff Date: Wed, 14 Aug 2002 05:28:07 +0000 Subject: [PATCH] Got indexing into variables working and cleaned up variables a bit. Partially translated zoom.gib into the new language, but it isn't done yet. --- doc/config/zoom.gib | 46 +++++++++++-------------- include/QF/gib_parse.h | 1 + libs/util/gib_builtin.c | 44 +++++++++++++++++++++++- libs/util/gib_parse.c | 12 ++++++- libs/util/gib_process.c | 75 ++++++++++++++++++++++++++++++++++++++++- 5 files changed, 148 insertions(+), 30 deletions(-) diff --git a/doc/config/zoom.gib b/doc/config/zoom.gib index 94bf14aa9..a31c85c5f 100644 --- a/doc/config/zoom.gib +++ b/doc/config/zoom.gib @@ -21,39 +21,31 @@ // Free Software Foundation, Inc. // 59 Temple Place - Suite 330 // Boston, MA 02111-1307, USA +// +// $Id$ -set zoom_version {$Id$} -set zoom_base_amp 1 -set zoom_base_fov 90 -set zoom_mult 1.25 -set zoom_zoomed 0 +global zoom -set zoom_mult_lower 1.25 -set zoom_mult_upper 90 // This is more or less the hard limit -set zoom_mult_step .25 - -alias zoom_init { // Initialize basic options - if #{$argn != 6} { - echo "Usage: $0 base_sensitivity base_fov min_zoom max_zoom zoom_step" +function "zoom.init" { // Initialize basic options + if ($argc != 6) { + echo "Usage: $0 base_amp base_fov min_zoom max_zoom zoom_step" return } - set zoom_base_amp $1 - set zoom_base_fov $2 - set zoom_mult_lower $3 - set zoom_mult $3 - set zoom_mult_upper $4 - set zoom_mult_step $5 + zoom.amp = $1 + zoom.fov = $2 + zoom.mult.lower = $3 + zoom.mult = $3 + zoom.mult.upper = $4 + zoom.mult.step = $5 + zoom.zoomed = 0 + set fov ${zoom.fov} + set in_amp ${zoom.amp} } -alias zoom_mult { // "Accessor" for zoom_mult - if #{!$1} return // Prevent division by zero - set zoom_mult $1 -} - -alias zoom_adjust { // Adjust fov and sensitivity to match zoom factor - if $zoom_zoomed { - set fov #{$zoom_base_fov/$zoom_mult} - set in_amp #{$zoom_base_amp/$zoom_mult} +function "zoom.adjust" { // Adjust fov and sensitivity to match zoom factor + if ${zoom.zoomed} { + set fov (${zoom.fov}/${zoom.mult}) + set in_amp (${zoom.amp}/${zoom.mult}) return } else { set fov $zoom_base_fov diff --git a/include/QF/gib_parse.h b/include/QF/gib_parse.h index 1f27d3f36..f49f72aae 100644 --- a/include/QF/gib_parse.h +++ b/include/QF/gib_parse.h @@ -31,6 +31,7 @@ char GIB_Parse_Match_Brace (const char *str, unsigned int *i); char GIB_Parse_Match_Backtick (const char *str, unsigned int *i); +char GIB_Parse_Match_Index (const char *str, unsigned int *i); void GIB_Parse_Extract_Line (struct cbuf_s *cbuf); void GIB_Parse_Tokenize_Line (struct cbuf_s *cbuf); diff --git a/libs/util/gib_builtin.c b/libs/util/gib_builtin.c index 5aaee8b76..f316c84eb 100644 --- a/libs/util/gib_builtin.c +++ b/libs/util/gib_builtin.c @@ -52,12 +52,14 @@ static const char rcsid[] = hashtab_t *gib_builtins; +/* + Hashtable callbacks +*/ const char * GIB_Builtin_Get_Key (void *ele, void *ptr) { return ((gib_builtin_t *)ele)->name->str; } - void GIB_Builtin_Free (void *ele, void *ptr) { @@ -67,6 +69,11 @@ GIB_Builtin_Free (void *ele, void *ptr) free (b); } +/* + GIB_Builtin_Add + + Registers a new builtin GIB command. +*/ void GIB_Builtin_Add (const char *name, void (*func) (void), enum gib_builtin_type_e type) { @@ -83,6 +90,13 @@ GIB_Builtin_Add (const char *name, void (*func) (void), enum gib_builtin_type_e Hash_Add (gib_builtins, new); } +/* + GIB_Builtin_Find + + Looks up the builtin name in the builtin hash, + returning a pointer to the struct on success, + zero otherwise. +*/ gib_builtin_t * GIB_Builtin_Find (const char *name) { @@ -91,12 +105,22 @@ GIB_Builtin_Find (const char *name) return (gib_builtin_t *) Hash_Find (gib_builtins, name); } +/* + GIB_Argc + + Returns the number of arguments available + in the current buffer. +*/ unsigned int GIB_Argc (void) { return cbuf_active->args->argc; } +/* + Returns a specific argument in the current + buffer. +*/ const char * GIB_Argv (unsigned int arg) { @@ -106,6 +130,12 @@ GIB_Argv (unsigned int arg) return ""; } +/* + GIB_Args + + Returns a pointer to the composite command + line starting at token arg. +*/ const char * GIB_Args (unsigned int arg) { @@ -115,6 +145,13 @@ GIB_Args (unsigned int arg) return ""; } +/* + GIB_Arg_Strip_Delim + + Strips any wrapping characters off of the + specified argument. Useful for GIB_BUILTIN_NOPROCESS + or GIB_BUILTIN_FIRSTONLY builtins. +*/ void GIB_Arg_Strip_Delim (unsigned int arg) { @@ -125,6 +162,11 @@ GIB_Arg_Strip_Delim (unsigned int arg) } } +/* + GIB Builtin functions + + See GIB docs for information. +*/ void GIB_Function_f (void) { diff --git a/libs/util/gib_parse.c b/libs/util/gib_parse.c index 64d6b1830..11cc74ded 100644 --- a/libs/util/gib_parse.c +++ b/libs/util/gib_parse.c @@ -161,6 +161,16 @@ GIB_Parse_Match_Backtick (const char *str, unsigned int *i) return '`'; } +char +GIB_Parse_Match_Index (const char *str, unsigned int *i) +{ + for ((*i)++; str[*i]; (*i)++) { + if (str[*i] == ']') + return 0; + } + return '['; +} + /* GIB_Parse_Extract_Line @@ -427,7 +437,7 @@ FILTER_ERROR: GIB builtins GIB functions - Assignment to a local variable + Assignment to a local/global variable Normal quake console commands */ void GIB_Parse_Execute_Line (cbuf_t *cbuf) diff --git a/libs/util/gib_process.c b/libs/util/gib_process.c index cb356c7d0..5335a1e98 100644 --- a/libs/util/gib_process.c +++ b/libs/util/gib_process.c @@ -37,6 +37,7 @@ static const char rcsid[] = #include #include +#include #include "QF/dstring.h" #include "QF/cbuf.h" @@ -47,6 +48,44 @@ static const char rcsid[] = #include "exp.h" +int GIB_Process_Variables_All (struct dstring_s *token); + +int +GIB_Process_Index (dstring_t *index, unsigned int pos, int *i1, int *i2) +{ + int i, v1, v2; + char *p; + + + for (i = pos; index->str[i] != ']'; i++) + if (!index->str[i]) { + Cbuf_Error ("parse", "Could not find matching ["); + return -1; + } + v1 = atoi (index->str+pos+1); + if (v1 < 0) { + Cbuf_Error ("index", "Negative index found in sub-string expression"); + return -1; + } + if ((p = strchr (index->str+pos, ':'))) { + v2 = atoi (p+1); + if (v2 < 0) { + Cbuf_Error ("index", "Negative index found in sub-string expression"); + return -1; + } + } else + v2 = v1; + dstring_snip (index, pos, i - pos + 1); + if (v2 < v1) { + v1 ^= v2; + v2 ^= v1; + v1 ^= v2; + } + *i1 = v1; + *i2 = v2; + return 0; +} + unsigned int GIB_Process_Variable (struct dstring_s *dstr, unsigned int pos, qboolean tolerant) { @@ -80,6 +119,8 @@ GIB_Process_Variables_All (struct dstring_s *token) int i, n, m; dstring_t *var = dstring_newstr (); char c = 0; + char *p; + int i1, i2; for (i = 0; token->str[i]; i++) { if (token->str[i] == '$') { @@ -89,19 +130,50 @@ GIB_Process_Variables_All (struct dstring_s *token) Cbuf_Error ("parse", "Could not find match for %c", c); goto ERROR; } + if (token->str[n+1] == '[') { + // Cut index out and put it with the variable + m = n+1; + if ((c = GIB_Parse_Match_Index (token->str, &m))) { + Cbuf_Error ("parse", "Could not find match for %c", c); + goto ERROR; + } + dstring_insert (var, 0, token->str+n+1, m-n); + dstring_snip (token, n+1, m-n); + } n -= i; dstring_insert (var, 0, token->str+i+2, n-2); dstring_insertstr (var, 0, "$"); n++; } else { - for (n = 1; isalnum((byte)token->str[i+n]) || token->str[i+n] == '$' || token->str[i+n] == '_'; n++); // find end of var + for (n = 1; isalnum((byte)token->str[i+n]) || + token->str[i+n] == '$' || + token->str[i+n] == '_' || + token->str[i+n] == '[' || + token->str[i+n] == ']' || + token->str[i+n] == ':'; n++); // find end of var dstring_insert (var, 0, token->str+i, n); // extract it } for (m = 1; var->str[m]; m++) { if (var->str[m] == '$') m += GIB_Process_Variable (var, m, false) - 1; } + i1 = -1; + if (var->str[strlen(var->str)-1] == ']' && (p = strrchr (var->str, '['))) + if (GIB_Process_Index(var, p-var->str, &i1, &i2)) { + c = '['; + goto ERROR; + } GIB_Process_Variable (var, 0, true); + if (i1 >= 0) { + if (i1 >= strlen (var->str)) + i1 = strlen(var->str)-1; + if (i2 >= strlen (var->str)) + i2 = strlen(var->str)-1; + if (i2 < strlen(var->str)-1) // Snip everthing after index 2 + dstring_snip (var, i2+1, strlen(var->str)-i2-1); + if (i1 > 0) // Snip everything before index 1 + dstring_snip (var, 0, i1); + } dstring_replace (token, i, n, var->str, strlen(var->str)); i += strlen (var->str) - 1; dstring_clearstr (var); @@ -187,6 +259,7 @@ GIB_Process_Token (struct dstring_s *token, char delim) if (GIB_Process_Variables_All (token)) return -1; } + if (delim == '(') if (GIB_Process_Math (token)) return -1;