cvar: Rename aliasindex to aliases_size and only use it for size of the aliases array...
authorCloudwalk <cloudwalk009@gmail.com>
Tue, 25 May 2021 23:16:07 +0000 (19:16 -0400)
committerCloudwalk <cloudwalk009@gmail.com>
Tue, 25 May 2021 23:16:07 +0000 (19:16 -0400)
cmd.c
cvar.c
cvar.h

diff --git a/cmd.c b/cmd.c
index 525be466796515ca4ae98f96e183aebe5b4afbd0..8fef16f446aa45c44d7d7c4272617a5b44d43a4d 100644 (file)
--- a/cmd.c
+++ b/cmd.c
@@ -1594,12 +1594,12 @@ static void Cmd_Apropos_f(cmd_state_t *cmd)
                        Cvar_PrintHelp(cvar, cvar->name, true);
                        count++;
                }
-               for (int i = 0; i < cvar->aliasindex; i++)
+               for (char **cvar_alias = cvar->aliases; cvar_alias && *cvar_alias; cvar_alias++)
                {
-                       if (matchpattern_with_separator(cvar->aliases[i], partial, true, "", false))
+                       if (matchpattern_with_separator(*cvar_alias, partial, true, "", false))
                        {
                                Con_Printf ("cvar ");
-                               Cvar_PrintHelp(cvar, cvar->aliases[i], true);
+                               Cvar_PrintHelp(cvar, *cvar_alias, true);
                                count++;
                        }
                }
diff --git a/cvar.c b/cvar.c
index 6b6ddde23fa60eac731cfd655de1336defde588c..ce1e065254ac1bba166c1b34b7fc201813e7ef1d 100644 (file)
--- a/cvar.c
+++ b/cvar.c
@@ -44,8 +44,8 @@ cvar_t *Cvar_FindVar(cvar_state_t *cvars, const char *var_name, int neededflags)
                if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags))
                        return hash->cvar;
                else
-                       for (int i = 0; i < hash->cvar->aliasindex; i++)
-                               if (!strcmp (var_name, hash->cvar->aliases[i]) && (hash->cvar->flags & neededflags))
+                       for (char **alias = hash->cvar->aliases; alias && *alias; alias++)
+                               if (!strcmp (var_name, *alias) && (hash->cvar->flags & neededflags))
                                        return hash->cvar;
        return NULL;
 }
@@ -89,8 +89,8 @@ static cvar_t *Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name, cvar_
                if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags))
                        goto match;
                else
-                       for (int i = 0; i < hash->cvar->aliasindex; i++)
-                               if (!strcmp (var_name, hash->cvar->aliases[i]) && (hash->cvar->flags & neededflags))
+                       for (char **alias = hash->cvar->aliases; alias && *alias; alias++)
+                               if (!strcmp (var_name, *alias) && (hash->cvar->flags & neededflags))
                                        goto match;
                if(parent) *parent = hash->cvar;
        }
@@ -227,8 +227,8 @@ int Cvar_CompleteCountPossible(cvar_state_t *cvars, const char *partial, int nee
                if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
                        h++;
                else
-                       for(int i = 0; i < cvar->aliasindex; i++)
-                               if (!strncasecmp(partial, cvar->aliases[i], len) && (cvar->flags & neededflags))
+                       for (char **alias = cvar->aliases; alias && *alias; alias++)
+                               if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags))
                                        h++;
                
        return h;
@@ -258,9 +258,9 @@ const char **Cvar_CompleteBuildList(cvar_state_t *cvars, const char *partial, in
                if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
                        buf[bpos++] = cvar->name;
                else
-                       for(int i = 0; i < cvar->aliasindex; i++)
-                               if (!strncasecmp(partial, cvar->aliases[i], len) && (cvar->flags & neededflags))
-                                       buf[bpos++] = cvar->aliases[i];
+                       for (char **alias = cvar->aliases; alias && *alias; alias++)
+                               if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags))
+                                       buf[bpos++] = *alias;
                
 
        buf[bpos] = NULL;
@@ -292,9 +292,9 @@ void Cvar_CompleteCvarPrint(cvar_state_t *cvars, const char *partial, int needed
                if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
                        Cvar_PrintHelp(cvar, cvar->name, true);
                else
-                       for (int i = 0; i < cvar->aliasindex; i++)
-                               if (!strncasecmp (partial, cvar->aliases[i], len) && (cvar->flags & neededflags))
-                                       Cvar_PrintHelp(cvar, cvar->aliases[i], true);
+                       for (char **alias = cvar->aliases; alias && *alias; alias++)
+                               if (!strncasecmp (partial, *alias, len) && (cvar->flags & neededflags))
+                                       Cvar_PrintHelp(cvar, *alias, true);
 
                
 }
@@ -498,11 +498,36 @@ void Cvar_RegisterAlias(cvar_t *variable, const char *alias )
        cvar_hash_t *hash;
        int hashindex;
 
-       variable->aliases = (char **)Mem_Realloc(zonemempool, variable->aliases, sizeof(char *) * (variable->aliasindex + 1));
+       if(!*alias)
+       {
+               Con_Printf("Cvar_RegisterAlias: invalid alias name\n");
+               return;
+       }
+
+       // check for overlap with a command
+       if (Cmd_Exists(cmd_local, alias))
+       {
+               Con_Printf("Cvar_RegisterAlias: %s is a command\n", alias);
+               return;
+       }
+
+       if(Cvar_FindVar(&cvars_all, alias, 0))
+       {
+               Con_Printf("Cvar_RegisterAlias: %s is a cvar\n", alias);
+               return;
+       }
+
+       if(!variable->aliases)
+               variable->aliases = (char **)Z_Malloc(sizeof(char *) * 2); // For NULL terminator
+       else
+               variable->aliases = (char **)Mem_Realloc(zonemempool, variable->aliases, sizeof(char *) * (variable->aliases_size + 1));
+       
+       variable->aliases[variable->aliases_size + 1] = NULL;
+       
        // Add to it
-       variable->aliases[variable->aliasindex] = (char *)Z_Malloc(strlen(alias) + 1);
-       memcpy(variable->aliases[variable->aliasindex], alias, strlen(alias) + 1);
-       variable->aliasindex++;
+       variable->aliases[variable->aliases_size] = (char *)Z_Malloc(strlen(alias) + 1);
+       memcpy(variable->aliases[variable->aliases_size], alias, strlen(alias) + 1);
+       variable->aliases_size++;
 
        // link to head of list in this hash table index
        hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
@@ -634,7 +659,8 @@ void Cvar_RegisterVariable (cvar_t *variable)
        variable->defstring = (char *)Mem_strdup(zonemempool, variable->string);
        variable->value = atof (variable->string);
        variable->integer = (int) variable->value;
-       variable->aliasindex = 0;
+       variable->aliases = NULL;
+       variable->aliases_size = 0;
        variable->initstate = NULL;
 
        // Mark it as not an autocvar.
@@ -702,7 +728,8 @@ cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int f
        cvar->defstring = (char *)Mem_strdup(zonemempool, value);
        cvar->value = atof (cvar->string);
        cvar->integer = (int) cvar->value;
-       cvar->aliases = (char **)Z_Malloc(sizeof(char **));
+       cvar->aliases = NULL;
+       cvar->aliases_size = 0;
        cvar->initstate = NULL;
        memset(cvar->aliases, 0, sizeof(char *));
 
@@ -984,11 +1011,11 @@ void Cvar_List_f(cmd_state_t *cmd)
                        Cvar_PrintHelp(cvar, cvar->name, true);
                        count++;
                }
-               for (int i = 0; i < cvar->aliasindex; i++)
+               for (char **alias = cvar->aliases; alias && *alias; alias++)
                {
-                       if (matchpattern_with_separator(cvar->aliases[i], partial, false, "", false))
+                       if (matchpattern_with_separator(*alias, partial, false, "", false))
                        {
-                               Cvar_PrintHelp(cvar, cvar->aliases[i], true);
+                               Cvar_PrintHelp(cvar, *alias, true);
                                count++;
                        }
                }
diff --git a/cvar.h b/cvar.h
index dd7bc98b1c2065a62ab3dfbfc74de233638cbba6..660320390f99358d826e0f8f1fa0b12f790840c4 100644 (file)
--- a/cvar.h
+++ b/cvar.h
@@ -79,7 +79,7 @@ typedef struct cvar_s
        void (*callback)(struct cvar_s *var);
 
        char **aliases;
-       int aliasindex;
+       int aliases_size;
 
        struct cvar_s *initstate; // snapshot of cvar during init