]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cvar.c
cvar: Don't memset cvar->aliases before it's even allocated
[xonotic/darkplaces.git] / cvar.c
diff --git a/cvar.c b/cvar.c
index fcd5062b3f408dc4238e60d779336fd5e1552e9f..58562d6b642742e911d879ddc0fdc55dd50af0c4 100644 (file)
--- a/cvar.c
+++ b/cvar.c
@@ -1,5 +1,6 @@
 /*
 Copyright (C) 1996-1997 Id Software, Inc.
+Copyright (C) 2000-2021 DarkPlaces contributors
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
@@ -43,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;
 }
@@ -73,7 +74,7 @@ cvar_t *Cvar_FindVarAfter(cvar_state_t *cvars, const char *prev_var_name, int ne
        return var;
 }
 
-static cvar_hash_t *Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name, cvar_hash_t **parent, cvar_hash_t ***link, cvar_t **prev_alpha, int neededflags)
+static cvar_t *Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name, cvar_t **parent, cvar_t ***link, cvar_t **prev_alpha, int neededflags)
 {
        int hashindex;
        cvar_hash_t *hash;
@@ -82,28 +83,28 @@ static cvar_hash_t *Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name,
        hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name)) % CVAR_HASHSIZE;
        if(parent) *parent = NULL;
        if(prev_alpha) *prev_alpha = NULL;
-       if(link) *link = &cvars->hashtable[hashindex];
+       if(link) *link = &cvars->hashtable[hashindex]->cvar;
        for (hash = cvars->hashtable[hashindex];hash;hash = hash->next)
        {
                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;
+               if(parent) *parent = hash->cvar;
        }
        return NULL;
 match:
        if(!prev_alpha || hash->cvar == cvars->vars)
-               return hash;
+               return hash->cvar;
 
        *prev_alpha = cvars->vars;
        // if prev_alpha happens to become NULL then there has been some inconsistency elsewhere
        // already - should I still insert '*prev_alpha &&' in the loop?
        while((*prev_alpha)->next != hash->cvar)
                *prev_alpha = (*prev_alpha)->next;
-       return hash;
+       return hash->cvar;
 }
 
 /*
@@ -226,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;
@@ -257,23 +258,23 @@ 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;
        return buf;
 }
 
-void Cvar_PrintHelp(cvar_t *cvar, const char *name, qboolean full)
+void Cvar_PrintHelp(cvar_t *cvar, const char *name, qbool full)
 {
        // Aliases are purple, cvars are yellow
        if (strcmp(cvar->name, name))
                Con_Printf("^6");
        else
                Con_Printf("^3");
-       Con_Printf("%s^7 is \"%s\" [\"%s\"]", name, ((cvar->flags & CVAR_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring);
+       Con_Printf("%s^7 is \"%s\" [\"%s\"]", name, ((cvar->flags & CF_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring);
        if (strcmp(cvar->name, name))
                Con_Printf(" (also ^3%s^7)", cvar->name);
        if (full)
@@ -291,15 +292,15 @@ 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);
 
                
 }
 
 // check if a cvar is held by some progs
-static qboolean Cvar_IsAutoCvar(cvar_t *var)
+static qbool Cvar_IsAutoCvar(cvar_t *var)
 {
        int i;
        prvm_prog_t *prog;
@@ -365,6 +366,18 @@ void Cvar_UpdateAllAutoCvars(cvar_state_t *cvars)
                Cvar_UpdateAutoCvar(var);
 }
 
+void Cvar_Callback(cvar_t *var)
+{
+       if (var == NULL)
+       {
+               Con_Print("Cvar_Callback: var == NULL\n");
+               return;
+       }
+
+       if(var->callback)
+               var->callback(var);
+}
+
 /*
 ============
 Cvar_Set
@@ -373,39 +386,30 @@ Cvar_Set
 extern cvar_t sv_disablenotify;
 static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
 {
-       cvar_state_t *cvars = &cvars_all;
-       qboolean changed;
+       qbool changed;
        size_t valuelen;
-       char vabuf[1024];
-       char new_value[MAX_INPUTLINE];
 
        changed = strcmp(var->string, value) != 0;
        // LadyHavoc: don't reallocate when there is no change
        if (!changed)
                return;
 
-       memcpy(new_value,value,MAX_INPUTLINE);
-
-       // Call the function stored in the cvar for bounds checking, cleanup, etc
-       if (var->callback)
-               var->callback(new_value);
-
        // LadyHavoc: don't reallocate when the buffer is the same size
-       valuelen = strlen(new_value);
+       valuelen = strlen(value);
        if (!var->string || strlen(var->string) != valuelen)
        {
                Z_Free ((char *)var->string);   // free the old value string
 
                var->string = (char *)Z_Malloc (valuelen + 1);
        }
-       memcpy ((char *)var->string, new_value, valuelen + 1);
+       memcpy ((char *)var->string, value, valuelen + 1);
        var->value = atof (var->string);
        var->integer = (int) var->value;
-       if ((var->flags & CVAR_NOTIFY) && changed && sv.active && !sv_disablenotify.integer)
-               SV_BroadcastPrintf("\001^3Server cvar \"%s\" changed to \"%s\"\n", var->name, var->string);
+       if ((var->flags & CF_NOTIFY) && sv.active && !sv_disablenotify.integer)
+               SV_BroadcastPrintf("\003^3Server cvar \"%s\" changed to \"%s\"\n", var->name, var->string);
 #if 0
        // TODO: add infostring support to the server?
-       if ((var->flags & CVAR_SERVERINFO) && changed && sv.active)
+       if ((var->flags & CF_SERVERINFO) && changed && sv.active)
        {
                InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
                if (sv.active)
@@ -416,52 +420,13 @@ static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
                }
        }
 #endif
-       if ((var->flags & CVAR_USERINFO) && cls.state != ca_dedicated)
+       if (var->flags & CF_USERINFO)
                CL_SetInfo(var->name, var->string, true, false, false, false);
-       else if ((var->flags & CVAR_NQUSERINFOHACK) && cls.state != ca_dedicated)
-       {
-               // update the cls.userinfo to have proper values for the
-               // silly nq config variables.
-               //
-               // this is done when these variables are changed rather than at
-               // connect time because if the user or code checks the userinfo and it
-               // holds weird values it may cause confusion...
-               if (!strcmp(var->name, "_cl_color"))
-               {
-                       int top = (var->integer >> 4) & 15, bottom = var->integer & 15;
-                       CL_SetInfo("topcolor", va(vabuf, sizeof(vabuf), "%i", top), true, false, false, false);
-                       CL_SetInfo("bottomcolor", va(vabuf, sizeof(vabuf), "%i", bottom), true, false, false, false);
-                       if (cls.protocol != PROTOCOL_QUAKEWORLD && cls.netcon)
-                       {
-                               MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
-                               MSG_WriteString(&cls.netcon->message, va(vabuf, sizeof(vabuf), "color %i %i", top, bottom));
-                       }
-               }
-               else if (!strcmp(var->name, "_cl_rate"))
-                       CL_SetInfo("rate", va(vabuf, sizeof(vabuf), "%i", var->integer), true, false, false, false);
-               else if (!strcmp(var->name, "_cl_rate_burstsize"))
-                       CL_SetInfo("rate_burstsize", va(vabuf, sizeof(vabuf), "%i", var->integer), true, false, false, false);
-               else if (!strcmp(var->name, "_cl_playerskin"))
-                       CL_SetInfo("playerskin", var->string, true, false, false, false);
-               else if (!strcmp(var->name, "_cl_playermodel"))
-                       CL_SetInfo("playermodel", var->string, true, false, false, false);
-               else if (!strcmp(var->name, "_cl_name"))
-                       CL_SetInfo("name", var->string, true, false, false, false);
-               else if (!strcmp(var->name, "rcon_secure"))
-               {
-                       // whenever rcon_secure is changed to 0, clear rcon_password for
-                       // security reasons (prevents a send-rcon-password-as-plaintext
-                       // attack based on NQ protocol session takeover and svc_stufftext)
-                       if(var->integer <= 0)
-                               Cvar_Set(cvars, "rcon_password", "");
-               }
-#ifdef CONFIG_MENU
-               else if (!strcmp(var->name, "net_slist_favorites"))
-                       NetConn_UpdateFavorites();
-#endif
-       }
 
        Cvar_UpdateAutoCvar(var);
+
+       // Call the function stored in the cvar for bounds checking, cleanup, etc
+       Cvar_Callback(var);
 }
 
 void Cvar_SetQuick (cvar_t *var, const char *value)
@@ -517,8 +482,13 @@ void Cvar_SetValue(cvar_state_t *cvars, const char *var_name, float value)
        Cvar_Set(cvars, var_name, val);
 }
 
-void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(char *))
+void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(cvar_t *))
 {
+       if (variable == NULL)
+       {
+               Con_Print("Cvar_RegisterCallback: var == NULL\n");
+               return;
+       }
        variable->callback = callback;
 }
 
@@ -528,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));
@@ -542,6 +537,38 @@ void Cvar_RegisterAlias(cvar_t *variable, const char *alias )
        hash->cvar = variable;
 }
 
+/*
+============
+Cvar_Link
+
+Links a variable to the variable list and hashtable
+============
+*/
+static void Cvar_Link(cvar_t *variable, cvar_state_t *cvars)
+{
+       cvar_t *current, *next;
+       cvar_hash_t *hash;
+       int hashindex;
+       /*
+        * Link the variable in
+        * alphanumerical order
+        */
+       for( current = NULL, next = cvars->vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
+               ;
+       if(current)
+               current->next = variable;
+       else
+               cvars->vars = variable;
+       variable->next = next;
+
+       // link to head of list in this hash table index
+       hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
+       hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
+       hash->next = cvars->hashtable[hashindex];
+       hash->cvar = variable;
+       cvars->hashtable[hashindex] = hash;
+}
+
 /*
 ============
 Cvar_RegisterVariable
@@ -552,36 +579,32 @@ Adds a freestanding variable to the variable list.
 void Cvar_RegisterVariable (cvar_t *variable)
 {
        cvar_state_t *cvars = NULL;
-       int hashindex;
-       cvar_hash_t *hash;
-       cvar_t *current, *next, *cvar;
-       char *oldstr;
-       size_t alloclen;
+       cvar_t *current, *cvar;
        int i;
 
-       switch (variable->flags & (CVAR_CLIENT | CVAR_SERVER))
+       switch (variable->flags & (CF_CLIENT | CF_SERVER))
        {
-       case CVAR_CLIENT:
-       case CVAR_SERVER:
-       case CVAR_CLIENT | CVAR_SERVER:
+       case CF_CLIENT:
+       case CF_SERVER:
+       case CF_CLIENT | CF_SERVER:
                cvars = &cvars_all;
                break;
        case 0:
-               Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with no CVAR_CLIENT | CVAR_SERVER flags\n", variable->name, variable->string, variable->flags);
+               Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with no CF_CLIENT | CF_SERVER flags\n", variable->name, variable->string, variable->flags);
                break;
        default:
-               Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with weird CVAR_CLIENT | CVAR_SERVER flags\n", variable->name, variable->string, variable->flags);
+               Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with weird CF_CLIENT | CF_SERVER flags\n", variable->name, variable->string, variable->flags);
                break;
        }
 
        if (developer_extra.integer)
                Con_DPrintf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
 
-// first check to see if it has already been defined
+       // first check to see if it has already been defined
        cvar = Cvar_FindVar(cvars, variable->name, ~0);
        if (cvar)
        {
-               if (cvar->flags & CVAR_ALLOCATED)
+               if (cvar->flags & CF_ALLOCATED)
                {
                        if (developer_extra.integer)
                                Con_DPrintf("...  replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
@@ -589,7 +612,7 @@ void Cvar_RegisterVariable (cvar_t *variable)
                        // (because the engine directly accesses fixed variables)
                        // NOTE: this isn't actually used currently
                        // (all cvars are registered before config parsing)
-                       variable->flags |= (cvar->flags & ~CVAR_ALLOCATED);
+                       variable->flags |= (cvar->flags & ~CF_ALLOCATED);
                        // cvar->string is now owned by variable instead
                        variable->string = cvar->string;
                        variable->defstring = cvar->defstring;
@@ -623,45 +646,28 @@ void Cvar_RegisterVariable (cvar_t *variable)
                return;
        }
 
-// check for overlap with a command
-       if (Cmd_Exists(&cmd_client, variable->name) || Cmd_Exists(&cmd_server, variable->name))
+       // check for overlap with a command
+       if (Cmd_Exists(cmd_local, variable->name))
        {
                Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
                return;
        }
 
-// copy the value off, because future sets will Z_Free it
-       oldstr = (char *)variable->string;
-       alloclen = strlen(variable->string) + 1;
-       variable->string = (char *)Z_Malloc (alloclen);
-       memcpy ((char *)variable->string, oldstr, alloclen);
-       variable->defstring = (char *)Z_Malloc (alloclen);
-       memcpy ((char *)variable->defstring, oldstr, alloclen);
+       // copy the value off, because future sets will Z_Free it
+       variable->name = (char *)Mem_strdup(zonemempool, variable->name);
+       variable->string = (char *)Mem_strdup(zonemempool, variable->string);
+       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.
        for (i = 0;i < PRVM_PROG_MAX;i++)
                variable->globaldefindex[i] = -1;
 
-// link the variable in
-// alphanumerical order
-       for( current = NULL, next = cvars->vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
-               ;
-       if( current ) {
-               current->next = variable;
-       } else {
-               cvars->vars = variable;
-       }
-       variable->next = next;
-
-       // link to head of list in this hash table index
-       hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
-       hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
-       hash->next = cvars->hashtable[hashindex];
-       hash->cvar = variable;
-       cvars->hashtable[hashindex] = hash;
+       Cvar_Link(variable, cvars);
 }
 
 /*
@@ -673,21 +679,19 @@ Adds a newly allocated variable to the variable list or sets its value.
 */
 cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int flags, const char *newdescription)
 {
-       int hashindex;
-       cvar_hash_t *hash;
-       cvar_t *current, *next, *cvar;
+       cvar_t *cvar;
        int i;
 
        if (developer_extra.integer)
                Con_DPrintf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
 
-// first check to see if it has already been defined
+       // first check to see if it has already been defined
        cvar = Cvar_FindVar(cvars, name, ~0);
        if (cvar)
        {
                cvar->flags |= flags;
                Cvar_SetQuick_Internal (cvar, value);
-               if(newdescription && (cvar->flags & CVAR_ALLOCATED))
+               if(newdescription && (cvar->flags & CF_ALLOCATED))
                {
                        if(cvar->description != cvar_dummy_description)
                                Z_Free((char *)cvar->description);
@@ -700,32 +704,33 @@ cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int f
                return cvar;
        }
 
-// check for pure evil
+       // check for pure evil
        if (!*name)
        {
                Con_Printf("Cvar_Get: invalid variable name\n");
                return NULL;
        }
 
-// check for overlap with a command
-       if (Cmd_Exists(&cmd_client, name) || Cmd_Exists(&cmd_server, name))
+       // check for overlap with a command
+       if (Cmd_Exists(cmd_local, name))
        {
                Con_Printf("Cvar_Get: %s is a command\n", name);
                return NULL;
        }
 
-// allocate a new cvar, cvar name, and cvar string
-// TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
-// FIXME: these never get Z_Free'd
+       // allocate a new cvar, cvar name, and cvar string
+       // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
+       // FIXME: these never get Z_Free'd
        cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
-       cvar->flags = flags | CVAR_ALLOCATED;
+       cvar->flags = flags | CF_ALLOCATED;
        cvar->name = (char *)Mem_strdup(zonemempool, name);
        cvar->string = (char *)Mem_strdup(zonemempool, value);
        cvar->defstring = (char *)Mem_strdup(zonemempool, value);
        cvar->value = atof (cvar->string);
        cvar->integer = (int) cvar->value;
-       cvar->aliases = (char **)Z_Malloc(sizeof(char **));
-       memset(cvar->aliases, 0, sizeof(char *));
+       cvar->aliases = NULL;
+       cvar->aliases_size = 0;
+       cvar->initstate = NULL;
 
        if(newdescription && *newdescription)
                cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
@@ -736,29 +741,14 @@ cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int f
        for (i = 0;i < PRVM_PROG_MAX;i++)
                cvar->globaldefindex[i] = -1;
 
-// link the variable in
-// alphanumerical order
-       for( current = NULL, next = cvars->vars ; next && strcmp( next->name, cvar->name ) < 0 ; current = next, next = next->next )
-               ;
-       if( current )
-               current->next = cvar;
-       else
-               cvars->vars = cvar;
-       cvar->next = next;
-
-       // link to head of list in this hash table index
-       hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
-       hashindex = CRC_Block((const unsigned char *)cvar->name, strlen(cvar->name)) % CVAR_HASHSIZE;
-       hash->next = cvars->hashtable[hashindex];
-       cvars->hashtable[hashindex] = hash;
-       hash->cvar = cvar;
+       Cvar_Link(cvar, cvars);
 
        return cvar;
 }
 
-qboolean Cvar_Readonly (cvar_t *var, const char *cmd_name)
+qbool Cvar_Readonly (cvar_t *var, const char *cmd_name)
 {
-       if (var->flags & CVAR_READONLY)
+       if (var->flags & CF_READONLY)
        {
                if(cmd_name)
                        Con_Printf("%s: ",cmd_name);
@@ -776,17 +766,17 @@ Cvar_Command
 Handles variable inspection and changing from the console
 ============
 */
-qboolean       Cvar_Command (cmd_state_t *cmd)
+qbool  Cvar_Command (cmd_state_t *cmd)
 {
        cvar_state_t    *cvars = cmd->cvars;
        cvar_t                  *v;
 
-// check variables
+       // check variables
        v = Cvar_FindVar(cvars, Cmd_Argv(cmd, 0), (cmd->cvars_flagsmask));
        if (!v)
                return false;
 
-// perform a variable print or set
+       // perform a variable print or set
        if (Cmd_Argc(cmd) == 1)
        {
                Cvar_PrintHelp(v, Cmd_Argv(cmd, 0), true);
@@ -795,27 +785,25 @@ qboolean  Cvar_Command (cmd_state_t *cmd)
 
        if (developer_extra.integer)
                Con_DPrint("Cvar_Command: ");
-       
+
        if(Cvar_Readonly(v, NULL))
                return true;
-       
+
        Cvar_SetQuick(v, Cmd_Argv(cmd, 1));
        if (developer_extra.integer)
                Con_DPrint("\n");
        return true;
 }
 
-
 void Cvar_UnlockDefaults(cmd_state_t *cmd)
 {
        cvar_state_t *cvars = cmd->cvars;
        cvar_t *var;
        // unlock the default values of all cvars
        for (var = cvars->vars ; var ; var = var->next)
-               var->flags &= ~CVAR_DEFAULTSET;
+               var->flags &= ~CF_DEFAULTSET;
 }
 
-
 void Cvar_LockDefaults_f(cmd_state_t *cmd)
 {
        cvar_state_t *cvars = cmd->cvars;
@@ -823,12 +811,12 @@ void Cvar_LockDefaults_f(cmd_state_t *cmd)
        // lock in the default values of all cvars
        for (var = cvars->vars ; var ; var = var->next)
        {
-               if (!(var->flags & CVAR_DEFAULTSET))
+               if (!(var->flags & CF_DEFAULTSET))
                {
                        size_t alloclen;
 
                        //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
-                       var->flags |= CVAR_DEFAULTSET;
+                       var->flags |= CF_DEFAULTSET;
                        Z_Free((char *)var->defstring);
                        alloclen = strlen(var->string) + 1;
                        var->defstring = (char *)Z_Malloc(alloclen);
@@ -842,13 +830,8 @@ void Cvar_SaveInitState(cvar_state_t *cvars)
        cvar_t *c;
        for (c = cvars->vars;c;c = c->next)
        {
-               c->initstate = true;
-               c->initflags = c->flags;
-               c->initdefstring = Mem_strdup(zonemempool, c->defstring);
-               c->initstring = Mem_strdup(zonemempool, c->string);
-               c->initvalue = c->value;
-               c->initinteger = c->integer;
-               VectorCopy(c->vector, c->initvector);
+               c->initstate = (cvar_t *)Z_Malloc(sizeof(cvar_t));
+               memcpy(c->initstate, c, sizeof(cvar_t));
        }
 }
 
@@ -862,31 +845,31 @@ void Cvar_RestoreInitState(cvar_state_t *cvars)
                if (c->initstate)
                {
                        // restore this cvar, it existed at init
-                       if (((c->flags ^ c->initflags) & CVAR_MAXFLAGSVAL)
-                        || strcmp(c->defstring ? c->defstring : "", c->initdefstring ? c->initdefstring : "")
-                        || strcmp(c->string ? c->string : "", c->initstring ? c->initstring : ""))
+                       if (((c->flags ^ c->initstate->flags) & CF_MAXFLAGSVAL)
+                        || strcmp(c->defstring ? c->defstring : "", c->initstate->defstring ? c->initstate->defstring : "")
+                        || strcmp(c->string ? c->string : "", c->initstate->string ? c->initstate->string : ""))
                        {
                                Con_DPrintf("Cvar_RestoreInitState: Restoring cvar \"%s\"\n", c->name);
                                if (c->defstring)
                                        Z_Free((char *)c->defstring);
-                               c->defstring = Mem_strdup(zonemempool, c->initdefstring);
+                               c->defstring = Mem_strdup(zonemempool, c->initstate->defstring);
                                if (c->string)
                                        Z_Free((char *)c->string);
-                               c->string = Mem_strdup(zonemempool, c->initstring);
+                               c->string = Mem_strdup(zonemempool, c->initstate->string);
                        }
-                       c->flags = c->initflags;
-                       c->value = c->initvalue;
-                       c->integer = c->initinteger;
-                       VectorCopy(c->initvector, c->vector);
+                       c->flags = c->initstate->flags;
+                       c->value = c->initstate->value;
+                       c->integer = c->initstate->integer;
+                       VectorCopy(c->initstate->vector, c->vector);
                        cp = &c->next;
                }
                else
                {
-                       if (!(c->flags & CVAR_ALLOCATED))
+                       if (!(c->flags & CF_ALLOCATED))
                        {
                                Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it was registered after init!\n", c->name);
                                // In this case, at least reset it to the default.
-                               if((c->flags & CVAR_NORESETTODEFAULTS) == 0)
+                               if((c->flags & CF_PERSISTENT) == 0)
                                        Cvar_SetQuick(c, c->defstring);
                                cp = &c->next;
                                continue;
@@ -895,7 +878,7 @@ void Cvar_RestoreInitState(cvar_state_t *cvars)
                        {
                                Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it is an autocvar used by running progs!\n", c->name);
                                // In this case, at least reset it to the default.
-                               if((c->flags & CVAR_NORESETTODEFAULTS) == 0)
+                               if((c->flags & CF_PERSISTENT) == 0)
                                        Cvar_SetQuick(c, c->defstring);
                                cp = &c->next;
                                continue;
@@ -936,12 +919,11 @@ void Cvar_ResetToDefaults_All_f(cmd_state_t *cmd)
        // restore the default values of all cvars
        for (var = cvars->vars ; var ; var = var->next)
        {
-               if((var->flags & CVAR_NORESETTODEFAULTS) == 0)
+               if((var->flags & CF_PERSISTENT) == 0)
                        Cvar_SetQuick(var, var->defstring);
        }
 }
 
-
 void Cvar_ResetToDefaults_NoSaveOnly_f(cmd_state_t *cmd)
 {
        cvar_state_t *cvars = cmd->cvars;
@@ -949,7 +931,7 @@ void Cvar_ResetToDefaults_NoSaveOnly_f(cmd_state_t *cmd)
        // restore the default values of all cvars
        for (var = cvars->vars ; var ; var = var->next)
        {
-               if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == 0)
+               if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == 0)
                        Cvar_SetQuick(var, var->defstring);
        }
 }
@@ -962,12 +944,11 @@ void Cvar_ResetToDefaults_SaveOnly_f(cmd_state_t *cmd)
        // restore the default values of all cvars
        for (var = cvars->vars ; var ; var = var->next)
        {
-               if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == CVAR_SAVE)
+               if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == CF_ARCHIVE)
                        Cvar_SetQuick(var, var->defstring);
        }
 }
 
-
 /*
 ============
 Cvar_WriteVariables
@@ -983,16 +964,15 @@ void Cvar_WriteVariables (cvar_state_t *cvars, qfile_t *f)
 
        // don't save cvars that match their default value
        for (var = cvars->vars ; var ; var = var->next) {
-               if ((var->flags & CVAR_SAVE) && (strcmp(var->string, var->defstring) || ((var->flags & CVAR_ALLOCATED) && !(var->flags & CVAR_DEFAULTSET))))
+               if ((var->flags & CF_ARCHIVE) && (strcmp(var->string, var->defstring) || ((var->flags & CF_ALLOCATED) && !(var->flags & CF_DEFAULTSET))))
                {
                        Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$", false);
                        Cmd_QuoteString(buf2, sizeof(buf2), var->string, "\"\\$", false);
-                       FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CVAR_ALLOCATED ? "seta " : "", buf1, buf2);
+                       FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CF_ALLOCATED ? "seta " : "", buf1, buf2);
                }
        }
 }
 
-
 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
 /*
@@ -1006,7 +986,7 @@ void Cvar_List_f(cmd_state_t *cmd)
        cvar_t *cvar;
        const char *partial;
        int count;
-       qboolean ispattern;
+       qbool ispattern;
        char vabuf[1024];
 
        if (Cmd_Argc(cmd) > 1)
@@ -1030,11 +1010,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++;
                        }
                }
@@ -1099,7 +1079,7 @@ void Cvar_SetA_f(cmd_state_t *cmd)
                Con_DPrint("SetA: ");
 
        // all looks ok, create/modify the cvar
-       Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask | CVAR_SAVE, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
+       Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask | CF_ARCHIVE, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
 }
 
 void Cvar_Del_f(cmd_state_t *cmd)
@@ -1107,7 +1087,7 @@ void Cvar_Del_f(cmd_state_t *cmd)
        cvar_state_t *cvars = cmd->cvars;
        int neededflags = ~0;
        int i;
-       cvar_hash_t *hash, *parent, **link;
+       cvar_t *parent, **link;
        cvar_t *cvar, *prev;
 
        if(Cmd_Argc(cmd) < 2)
@@ -1117,8 +1097,7 @@ void Cvar_Del_f(cmd_state_t *cmd)
        }
        for(i = 1; i < Cmd_Argc(cmd); ++i)
        {
-               hash = Cvar_FindVarLink(cvars, Cmd_Argv(cmd, i), &parent, &link, &prev, neededflags);
-               cvar = hash->cvar;
+               cvar = Cvar_FindVarLink(cvars, Cmd_Argv(cmd, i), &parent, &link, &prev, neededflags);
 
                if(!cvar)
                {
@@ -1127,7 +1106,7 @@ void Cvar_Del_f(cmd_state_t *cmd)
                }
                if(Cvar_Readonly(cvar, Cmd_Argv(cmd, 0)))
                        continue;
-               if(!(cvar->flags & CVAR_ALLOCATED))
+               if(!(cvar->flags & CF_ALLOCATED))
                {
                        Con_Printf("%s: %s is static and cannot be deleted\n", Cmd_Argv(cmd, 0), cvar->name);
                        continue;
@@ -1144,9 +1123,9 @@ void Cvar_Del_f(cmd_state_t *cmd)
                }
 
                if(parent)
-                       parent->next = hash->next;
+                       parent->next = cvar->next;
                else if(link)
-                       *link = hash->next;
+                       *link = cvar->next;
                if(cvar->description != cvar_dummy_description)
                        Z_Free((char *)cvar->description);
 
@@ -1163,7 +1142,7 @@ void Cvar_FillAll_f(cmd_state_t *cmd)
        char *buf, *p, *q;
        int n, i;
        cvar_t *var;
-       qboolean verify;
+       qbool verify;
        if(Cmd_Argc(cmd) != 2)
        {
                Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(cmd, 0));