]> git.xonotic.org Git - xonotic/darkplaces.git/blob - cvar.c
Rearrange cvar help to fix compatibility with some regex parsing of cvar help
[xonotic/darkplaces.git] / cvar.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // cvar.c -- dynamic variable tracking
21
22 #include "quakedef.h"
23
24 const char *cvar_dummy_description = "custom cvar";
25 static const char *cvar_null_string = "";
26
27 cvar_state_t cvars_all;
28 cvar_state_t cvars_null;
29
30 /*
31 ============
32 Cvar_FindVar
33 ============
34 */
35 cvar_t *Cvar_FindVar(cvar_state_t *cvars, const char *var_name, int neededflags)
36 {
37         int hashindex;
38         cvar_hash_t *hash;
39
40         // use hash lookup to minimize search time
41         hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name)) % CVAR_HASHSIZE;
42         for (hash = cvars->hashtable[hashindex];hash;hash = hash->next)
43                 if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags))
44                         return hash->cvar;
45                 else
46                         for (int i = 0; i < hash->cvar->aliasindex; i++)
47                                 if (!strcmp (var_name, hash->cvar->aliases[i]) && (hash->cvar->flags & neededflags))
48                                         return hash->cvar;
49         return NULL;
50 }
51
52 cvar_t *Cvar_FindVarAfter(cvar_state_t *cvars, const char *prev_var_name, int neededflags)
53 {
54         cvar_t *var;
55
56         if (*prev_var_name)
57         {
58                 var = Cvar_FindVar(cvars, prev_var_name, neededflags);
59                 if (!var)
60                         return NULL;
61                 var = var->next;
62         }
63         else
64                 var = cvars->vars;
65
66         // search for the next cvar matching the needed flags
67         while (var)
68         {
69                 if (var->flags & neededflags)
70                         break;
71                 var = var->next;
72         }
73         return var;
74 }
75
76 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)
77 {
78         int hashindex;
79         cvar_hash_t *hash;
80
81         // use hash lookup to minimize search time
82         hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name)) % CVAR_HASHSIZE;
83         if(parent) *parent = NULL;
84         if(prev_alpha) *prev_alpha = NULL;
85         if(link) *link = &cvars->hashtable[hashindex];
86         for (hash = cvars->hashtable[hashindex];hash;hash = hash->next)
87         {
88                 if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags))
89                         goto match;
90                 else
91                         for (int i = 0; i < hash->cvar->aliasindex; i++)
92                                 if (!strcmp (var_name, hash->cvar->aliases[i]) && (hash->cvar->flags & neededflags))
93                                         goto match;
94                 if(parent) *parent = hash;
95         }
96         return NULL;
97 match:
98         if(!prev_alpha || hash->cvar == cvars->vars)
99                 return hash;
100
101         *prev_alpha = cvars->vars;
102         // if prev_alpha happens to become NULL then there has been some inconsistency elsewhere
103         // already - should I still insert '*prev_alpha &&' in the loop?
104         while((*prev_alpha)->next != hash->cvar)
105                 *prev_alpha = (*prev_alpha)->next;
106         return hash;
107 }
108
109 /*
110 ============
111 Cvar_VariableValue
112 ============
113 */
114 float Cvar_VariableValueOr(cvar_state_t *cvars, const char *var_name, float def, int neededflags)
115 {
116         cvar_t *var;
117
118         var = Cvar_FindVar(cvars, var_name, neededflags);
119         if (!var)
120                 return def;
121         return atof (var->string);
122 }
123
124 float Cvar_VariableValue(cvar_state_t *cvars, const char *var_name, int neededflags)
125 {
126         return Cvar_VariableValueOr(cvars, var_name, 0, neededflags);
127 }
128
129 /*
130 ============
131 Cvar_VariableString
132 ============
133 */
134 const char *Cvar_VariableStringOr(cvar_state_t *cvars, const char *var_name, const char *def, int neededflags)
135 {
136         cvar_t *var;
137
138         var = Cvar_FindVar(cvars, var_name, neededflags);
139         if (!var)
140                 return def;
141         return var->string;
142 }
143
144 const char *Cvar_VariableString(cvar_state_t *cvars, const char *var_name, int neededflags)
145 {
146         return Cvar_VariableStringOr(cvars, var_name, cvar_null_string, neededflags);
147 }
148
149 /*
150 ============
151 Cvar_VariableDefString
152 ============
153 */
154 const char *Cvar_VariableDefString(cvar_state_t *cvars, const char *var_name, int neededflags)
155 {
156         cvar_t *var;
157
158         var = Cvar_FindVar(cvars, var_name, neededflags);
159         if (!var)
160                 return cvar_null_string;
161         return var->defstring;
162 }
163
164 /*
165 ============
166 Cvar_VariableDescription
167 ============
168 */
169 const char *Cvar_VariableDescription(cvar_state_t *cvars, const char *var_name, int neededflags)
170 {
171         cvar_t *var;
172
173         var = Cvar_FindVar(cvars, var_name, neededflags);
174         if (!var)
175                 return cvar_null_string;
176         return var->description;
177 }
178
179
180 /*
181 ============
182 Cvar_CompleteVariable
183 ============
184 */
185 const char *Cvar_CompleteVariable(cvar_state_t *cvars, const char *partial, int neededflags)
186 {
187         cvar_t          *cvar;
188         size_t          len;
189
190         len = strlen(partial);
191
192         if (!len)
193                 return NULL;
194
195 // check functions
196         for (cvar=cvars->vars ; cvar ; cvar=cvar->next)
197                 if (!strncasecmp (partial,cvar->name, len) && (cvar->flags & neededflags))
198                         return cvar->name;
199
200         return NULL;
201 }
202
203
204 /*
205         CVar_CompleteCountPossible
206
207         New function for tab-completion system
208         Added by EvilTypeGuy
209         Thanks to Fett erich@heintz.com
210
211 */
212 int Cvar_CompleteCountPossible(cvar_state_t *cvars, const char *partial, int neededflags)
213 {
214         cvar_t  *cvar;
215         size_t  len;
216         int             h;
217
218         h = 0;
219         len = strlen(partial);
220
221         if (!len)
222                 return  0;
223
224         // Loop through the cvars and count all possible matches
225         for (cvar = cvars->vars; cvar; cvar = cvar->next)
226                 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
227                         h++;
228                 else
229                         for(int i = 0; i < cvar->aliasindex; i++)
230                                 if (!strncasecmp(partial, cvar->aliases[i], len) && (cvar->flags & neededflags))
231                                         h++;
232                 
233         return h;
234 }
235
236 /*
237         CVar_CompleteBuildList
238
239         New function for tab-completion system
240         Added by EvilTypeGuy
241         Thanks to Fett erich@heintz.com
242         Thanks to taniwha
243
244 */
245 const char **Cvar_CompleteBuildList(cvar_state_t *cvars, const char *partial, int neededflags)
246 {
247         const cvar_t *cvar;
248         size_t len = 0;
249         size_t bpos = 0;
250         size_t sizeofbuf = (Cvar_CompleteCountPossible(cvars, partial, neededflags) + 1) * sizeof(const char *);
251         const char **buf;
252
253         len = strlen(partial);
254         buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof(const char *));
255         // Loop through the alias list and print all matches
256         for (cvar = cvars->vars; cvar; cvar = cvar->next)
257                 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
258                         buf[bpos++] = cvar->name;
259                 else
260                         for(int i = 0; i < cvar->aliasindex; i++)
261                                 if (!strncasecmp(partial, cvar->aliases[i], len) && (cvar->flags & neededflags))
262                                         buf[bpos++] = cvar->aliases[i];
263                 
264
265         buf[bpos] = NULL;
266         return buf;
267 }
268
269 void Cvar_PrintHelp(cvar_t *cvar, const char *name, qboolean full)
270 {
271         Con_Printf("^3%s^7 is \"%s\" [\"%s\"]", name, ((cvar->flags & CVAR_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring);
272         if (strcmp(cvar->name, name))
273                 Con_Printf(" (also ^3%s^7)", cvar->name);
274         if (full)
275                 Con_Printf(" %s", cvar->description);
276         Con_Printf("\n");
277 }
278
279 // written by LadyHavoc
280 void Cvar_CompleteCvarPrint(cvar_state_t *cvars, const char *partial, int neededflags)
281 {
282         cvar_t *cvar;
283         size_t len = strlen(partial);
284         // Loop through the command list and print all matches
285         for (cvar = cvars->vars; cvar; cvar = cvar->next)
286                 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
287                         Cvar_PrintHelp(cvar, cvar->name, true);
288                 else
289                         for (int i = 0; i < cvar->aliasindex; i++)
290                                 if (!strncasecmp (partial, cvar->aliases[i], len) && (cvar->flags & neededflags))
291                                         Cvar_PrintHelp(cvar, cvar->aliases[i], true);
292
293                 
294 }
295
296 // check if a cvar is held by some progs
297 static qboolean Cvar_IsAutoCvar(cvar_t *var)
298 {
299         int i;
300         prvm_prog_t *prog;
301         for (i = 0;i < PRVM_PROG_MAX;i++)
302         {
303                 prog = &prvm_prog_list[i];
304                 if (prog->loaded && var->globaldefindex[i] >= 0)
305                         return true;
306         }
307         return false;
308 }
309
310 // we assume that prog is already set to the target progs
311 static void Cvar_UpdateAutoCvar(cvar_t *var)
312 {
313         int i;
314         int j;
315         const char *s;
316         vec3_t v;
317         prvm_prog_t *prog;
318         for (i = 0;i < PRVM_PROG_MAX;i++)
319         {
320                 prog = &prvm_prog_list[i];
321                 if (prog->loaded && var->globaldefindex[i] >= 0)
322                 {
323                         // MUST BE SYNCED WITH prvm_edict.c PRVM_LoadProgs
324                         switch(prog->globaldefs[var->globaldefindex[i]].type & ~DEF_SAVEGLOBAL)
325                         {
326                         case ev_float:
327                                 PRVM_GLOBALFIELDFLOAT(prog->globaldefs[var->globaldefindex[i]].ofs) = var->value;
328                                 break;
329                         case ev_vector:
330                                 s = var->string;
331                                 VectorClear(v);
332                                 for (j = 0;j < 3;j++)
333                                 {
334                                         while (*s && ISWHITESPACE(*s))
335                                                 s++;
336                                         if (!*s)
337                                                 break;
338                                         v[j] = atof(s);
339                                         while (!ISWHITESPACE(*s))
340                                                 s++;
341                                         if (!*s)
342                                                 break;
343                                 }
344                                 VectorCopy(v, PRVM_GLOBALFIELDVECTOR(prog->globaldefs[var->globaldefindex[i]].ofs));
345                                 break;
346                         case ev_string:
347                                 PRVM_ChangeEngineString(prog, var->globaldefindex_stringno[i], var->string);
348                                 PRVM_GLOBALFIELDSTRING(prog->globaldefs[var->globaldefindex[i]].ofs) = var->globaldefindex_stringno[i];
349                                 break;
350                         }
351                 }
352         }
353 }
354
355 // called after loading a savegame
356 void Cvar_UpdateAllAutoCvars(cvar_state_t *cvars)
357 {
358         cvar_t *var;
359         for (var = cvars->vars ; var ; var = var->next)
360                 Cvar_UpdateAutoCvar(var);
361 }
362
363 /*
364 ============
365 Cvar_Set
366 ============
367 */
368 extern cvar_t sv_disablenotify;
369 static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
370 {
371         cvar_state_t *cvars = &cvars_all;
372         qboolean changed;
373         size_t valuelen;
374         char vabuf[1024];
375         char new_value[MAX_INPUTLINE];
376
377         changed = strcmp(var->string, value) != 0;
378         // LadyHavoc: don't reallocate when there is no change
379         if (!changed)
380                 return;
381
382         memcpy(new_value,value,MAX_INPUTLINE);
383
384         // Call the function stored in the cvar for bounds checking, cleanup, etc
385         if (var->callback)
386                 var->callback(new_value);
387
388         // LadyHavoc: don't reallocate when the buffer is the same size
389         valuelen = strlen(new_value);
390         if (!var->string || strlen(var->string) != valuelen)
391         {
392                 Z_Free ((char *)var->string);   // free the old value string
393
394                 var->string = (char *)Z_Malloc (valuelen + 1);
395         }
396         memcpy ((char *)var->string, new_value, valuelen + 1);
397         var->value = atof (var->string);
398         var->integer = (int) var->value;
399         if ((var->flags & CVAR_NOTIFY) && changed && sv.active && !sv_disablenotify.integer)
400                 SV_BroadcastPrintf("\001^3Server cvar \"%s\" changed to \"%s\"\n", var->name, var->string);
401 #if 0
402         // TODO: add infostring support to the server?
403         if ((var->flags & CVAR_SERVERINFO) && changed && sv.active)
404         {
405                 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
406                 if (sv.active)
407                 {
408                         MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
409                         MSG_WriteString (&sv.reliable_datagram, var->name);
410                         MSG_WriteString (&sv.reliable_datagram, var->string);
411                 }
412         }
413 #endif
414         if ((var->flags & CVAR_USERINFO) && cls.state != ca_dedicated)
415                 CL_SetInfo(var->name, var->string, true, false, false, false);
416         else if ((var->flags & CVAR_NQUSERINFOHACK) && cls.state != ca_dedicated)
417         {
418                 // update the cls.userinfo to have proper values for the
419                 // silly nq config variables.
420                 //
421                 // this is done when these variables are changed rather than at
422                 // connect time because if the user or code checks the userinfo and it
423                 // holds weird values it may cause confusion...
424                 if (!strcmp(var->name, "_cl_color"))
425                 {
426                         int top = (var->integer >> 4) & 15, bottom = var->integer & 15;
427                         CL_SetInfo("topcolor", va(vabuf, sizeof(vabuf), "%i", top), true, false, false, false);
428                         CL_SetInfo("bottomcolor", va(vabuf, sizeof(vabuf), "%i", bottom), true, false, false, false);
429                         if (cls.protocol != PROTOCOL_QUAKEWORLD && cls.netcon)
430                         {
431                                 MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
432                                 MSG_WriteString(&cls.netcon->message, va(vabuf, sizeof(vabuf), "color %i %i", top, bottom));
433                         }
434                 }
435                 else if (!strcmp(var->name, "_cl_rate"))
436                         CL_SetInfo("rate", va(vabuf, sizeof(vabuf), "%i", var->integer), true, false, false, false);
437                 else if (!strcmp(var->name, "_cl_rate_burstsize"))
438                         CL_SetInfo("rate_burstsize", va(vabuf, sizeof(vabuf), "%i", var->integer), true, false, false, false);
439                 else if (!strcmp(var->name, "_cl_playerskin"))
440                         CL_SetInfo("playerskin", var->string, true, false, false, false);
441                 else if (!strcmp(var->name, "_cl_playermodel"))
442                         CL_SetInfo("playermodel", var->string, true, false, false, false);
443                 else if (!strcmp(var->name, "_cl_name"))
444                         CL_SetInfo("name", var->string, true, false, false, false);
445                 else if (!strcmp(var->name, "rcon_secure"))
446                 {
447                         // whenever rcon_secure is changed to 0, clear rcon_password for
448                         // security reasons (prevents a send-rcon-password-as-plaintext
449                         // attack based on NQ protocol session takeover and svc_stufftext)
450                         if(var->integer <= 0)
451                                 Cvar_Set(cvars, "rcon_password", "");
452                 }
453 #ifdef CONFIG_MENU
454                 else if (!strcmp(var->name, "net_slist_favorites"))
455                         NetConn_UpdateFavorites();
456 #endif
457         }
458
459         Cvar_UpdateAutoCvar(var);
460 }
461
462 void Cvar_SetQuick (cvar_t *var, const char *value)
463 {
464         if (var == NULL)
465         {
466                 Con_Print("Cvar_SetQuick: var == NULL\n");
467                 return;
468         }
469
470         if (developer_extra.integer)
471                 Con_DPrintf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
472
473         Cvar_SetQuick_Internal(var, value);
474 }
475
476 void Cvar_Set(cvar_state_t *cvars, const char *var_name, const char *value)
477 {
478         cvar_t *var;
479         var = Cvar_FindVar(cvars, var_name, ~0);
480         if (var == NULL)
481         {
482                 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
483                 return;
484         }
485         Cvar_SetQuick(var, value);
486 }
487
488 /*
489 ============
490 Cvar_SetValue
491 ============
492 */
493 void Cvar_SetValueQuick(cvar_t *var, float value)
494 {
495         char val[MAX_INPUTLINE];
496
497         if ((float)((int)value) == value)
498                 dpsnprintf(val, sizeof(val), "%i", (int)value);
499         else
500                 dpsnprintf(val, sizeof(val), "%f", value);
501         Cvar_SetQuick(var, val);
502 }
503
504 void Cvar_SetValue(cvar_state_t *cvars, const char *var_name, float value)
505 {
506         char val[MAX_INPUTLINE];
507
508         if ((float)((int)value) == value)
509                 dpsnprintf(val, sizeof(val), "%i", (int)value);
510         else
511                 dpsnprintf(val, sizeof(val), "%f", value);
512         Cvar_Set(cvars, var_name, val);
513 }
514
515 void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(char *))
516 {
517         variable->callback = callback;
518 }
519
520 void Cvar_RegisterAlias(cvar_t *variable, const char *alias )
521 {
522         cvar_state_t *cvars = &cvars_all;
523         cvar_hash_t *hash;
524         int hashindex;
525
526         variable->aliases = (char **)Mem_Realloc(zonemempool, variable->aliases, sizeof(char *) * (variable->aliasindex + 1));
527         // Add to it
528         variable->aliases[variable->aliasindex] = (char *)Z_Malloc(strlen(alias) + 1);
529         memcpy(variable->aliases[variable->aliasindex], alias, strlen(alias) + 1);
530         variable->aliasindex++;
531
532         // link to head of list in this hash table index
533         hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
534         hashindex = CRC_Block((const unsigned char *)alias, strlen(alias)) % CVAR_HASHSIZE;
535         hash->next = cvars->hashtable[hashindex];
536         cvars->hashtable[hashindex] = hash;
537         hash->cvar = variable;
538 }
539
540 /*
541 ============
542 Cvar_RegisterVariable
543
544 Adds a freestanding variable to the variable list.
545 ============
546 */
547 void Cvar_RegisterVariable (cvar_t *variable)
548 {
549         cvar_state_t *cvars = NULL;
550         int hashindex;
551         cvar_hash_t *hash;
552         cvar_t *current, *next, *cvar;
553         char *oldstr;
554         size_t alloclen;
555         int i;
556
557         switch (variable->flags & (CVAR_CLIENT | CVAR_SERVER))
558         {
559         case CVAR_CLIENT:
560         case CVAR_SERVER:
561         case CVAR_CLIENT | CVAR_SERVER:
562                 cvars = &cvars_all;
563                 break;
564         case 0:
565                 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with no CVAR_CLIENT | CVAR_SERVER flags\n", variable->name, variable->string, variable->flags);
566                 break;
567         default:
568                 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with weird CVAR_CLIENT | CVAR_SERVER flags\n", variable->name, variable->string, variable->flags);
569                 break;
570         }
571
572         if (developer_extra.integer)
573                 Con_DPrintf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
574
575 // first check to see if it has already been defined
576         cvar = Cvar_FindVar(cvars, variable->name, ~0);
577         if (cvar)
578         {
579                 if (cvar->flags & CVAR_ALLOCATED)
580                 {
581                         if (developer_extra.integer)
582                                 Con_DPrintf("...  replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
583                         // fixed variables replace allocated ones
584                         // (because the engine directly accesses fixed variables)
585                         // NOTE: this isn't actually used currently
586                         // (all cvars are registered before config parsing)
587                         variable->flags |= (cvar->flags & ~CVAR_ALLOCATED);
588                         // cvar->string is now owned by variable instead
589                         variable->string = cvar->string;
590                         variable->defstring = cvar->defstring;
591                         variable->value = atof (variable->string);
592                         variable->integer = (int) variable->value;
593                         // Preserve autocvar status.
594                         memcpy(variable->globaldefindex, cvar->globaldefindex, sizeof(variable->globaldefindex));
595                         memcpy(variable->globaldefindex_stringno, cvar->globaldefindex_stringno, sizeof(variable->globaldefindex_stringno));
596                         // replace cvar with this one...
597                         variable->next = cvar->next;
598                         if (cvars->vars == cvar)
599                         {
600                                 // head of the list is easy to change
601                                 cvars->vars = variable;
602                         }
603                         else
604                         {
605                                 // otherwise find it somewhere in the list
606                                 for (current = cvars->vars;current->next != cvar;current = current->next)
607                                         ;
608                                 current->next = variable;
609                         }
610
611                         // get rid of old allocated cvar
612                         // (but not cvar->string and cvar->defstring, because we kept those)
613                         Z_Free((char *)cvar->name);
614                         Z_Free(cvar);
615                 }
616                 else
617                         Con_DPrintf("Can't register variable %s, already defined\n", variable->name);
618                 return;
619         }
620
621 // check for overlap with a command
622         if (Cmd_Exists(&cmd_client, variable->name) || Cmd_Exists(&cmd_server, variable->name))
623         {
624                 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
625                 return;
626         }
627
628 // copy the value off, because future sets will Z_Free it
629         oldstr = (char *)variable->string;
630         alloclen = strlen(variable->string) + 1;
631         variable->string = (char *)Z_Malloc (alloclen);
632         memcpy ((char *)variable->string, oldstr, alloclen);
633         variable->defstring = (char *)Z_Malloc (alloclen);
634         memcpy ((char *)variable->defstring, oldstr, alloclen);
635         variable->value = atof (variable->string);
636         variable->integer = (int) variable->value;
637         variable->aliasindex = 0;
638
639         // Mark it as not an autocvar.
640         for (i = 0;i < PRVM_PROG_MAX;i++)
641                 variable->globaldefindex[i] = -1;
642
643 // link the variable in
644 // alphanumerical order
645         for( current = NULL, next = cvars->vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
646                 ;
647         if( current ) {
648                 current->next = variable;
649         } else {
650                 cvars->vars = variable;
651         }
652         variable->next = next;
653
654         // link to head of list in this hash table index
655         hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
656         hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
657         hash->next = cvars->hashtable[hashindex];
658         hash->cvar = variable;
659         cvars->hashtable[hashindex] = hash;
660 }
661
662 /*
663 ============
664 Cvar_Get
665
666 Adds a newly allocated variable to the variable list or sets its value.
667 ============
668 */
669 cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int flags, const char *newdescription)
670 {
671         int hashindex;
672         cvar_hash_t *hash;
673         cvar_t *current, *next, *cvar;
674         int i;
675
676         if (developer_extra.integer)
677                 Con_DPrintf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
678
679 // first check to see if it has already been defined
680         cvar = Cvar_FindVar(cvars, name, ~0);
681         if (cvar)
682         {
683                 cvar->flags |= flags;
684                 Cvar_SetQuick_Internal (cvar, value);
685                 if(newdescription && (cvar->flags & CVAR_ALLOCATED))
686                 {
687                         if(cvar->description != cvar_dummy_description)
688                                 Z_Free((char *)cvar->description);
689
690                         if(*newdescription)
691                                 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
692                         else
693                                 cvar->description = cvar_dummy_description;
694                 }
695                 return cvar;
696         }
697
698 // check for pure evil
699         if (!*name)
700         {
701                 Con_Printf("Cvar_Get: invalid variable name\n");
702                 return NULL;
703         }
704
705 // check for overlap with a command
706         if (Cmd_Exists(&cmd_client, name) || Cmd_Exists(&cmd_server, name))
707         {
708                 Con_Printf("Cvar_Get: %s is a command\n", name);
709                 return NULL;
710         }
711
712 // allocate a new cvar, cvar name, and cvar string
713 // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
714 // FIXME: these never get Z_Free'd
715         cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
716         cvar->flags = flags | CVAR_ALLOCATED;
717         cvar->name = (char *)Mem_strdup(zonemempool, name);
718         cvar->string = (char *)Mem_strdup(zonemempool, value);
719         cvar->defstring = (char *)Mem_strdup(zonemempool, value);
720         cvar->value = atof (cvar->string);
721         cvar->integer = (int) cvar->value;
722         cvar->aliases = (char **)Z_Malloc(sizeof(char **));
723         memset(cvar->aliases, 0, sizeof(char *));
724
725         if(newdescription && *newdescription)
726                 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
727         else
728                 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
729
730         // Mark it as not an autocvar.
731         for (i = 0;i < PRVM_PROG_MAX;i++)
732                 cvar->globaldefindex[i] = -1;
733
734 // link the variable in
735 // alphanumerical order
736         for( current = NULL, next = cvars->vars ; next && strcmp( next->name, cvar->name ) < 0 ; current = next, next = next->next )
737                 ;
738         if( current )
739                 current->next = cvar;
740         else
741                 cvars->vars = cvar;
742         cvar->next = next;
743
744         // link to head of list in this hash table index
745         hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
746         hashindex = CRC_Block((const unsigned char *)cvar->name, strlen(cvar->name)) % CVAR_HASHSIZE;
747         hash->next = cvars->hashtable[hashindex];
748         cvars->hashtable[hashindex] = hash;
749         hash->cvar = cvar;
750
751         return cvar;
752 }
753
754 qboolean Cvar_Readonly (cvar_t *var, const char *cmd_name)
755 {
756         if (var->flags & CVAR_READONLY)
757         {
758                 if(cmd_name)
759                         Con_Printf("%s: ",cmd_name);
760                 Con_Printf("%s", var->name);
761                 Con_Printf(" is read-only\n");
762                 return true;
763         }
764         return false;
765 }
766
767 /*
768 ============
769 Cvar_Command
770
771 Handles variable inspection and changing from the console
772 ============
773 */
774 qboolean        Cvar_Command (cmd_state_t *cmd)
775 {
776         cvar_state_t    *cvars = cmd->cvars;
777         cvar_t                  *v;
778
779 // check variables
780         v = Cvar_FindVar(cvars, Cmd_Argv(cmd, 0), (cmd->cvars_flagsmask));
781         if (!v)
782                 return false;
783
784 // perform a variable print or set
785         if (Cmd_Argc(cmd) == 1)
786         {
787                 Cvar_PrintHelp(v, Cmd_Argv(cmd, 0), true);
788                 return true;
789         }
790
791         if (developer_extra.integer)
792                 Con_DPrint("Cvar_Command: ");
793         
794         if(Cvar_Readonly(v, NULL))
795                 return true;
796         
797         Cvar_SetQuick(v, Cmd_Argv(cmd, 1));
798         if (developer_extra.integer)
799                 Con_DPrint("\n");
800         return true;
801 }
802
803
804 void Cvar_UnlockDefaults(cmd_state_t *cmd)
805 {
806         cvar_state_t *cvars = cmd->cvars;
807         cvar_t *var;
808         // unlock the default values of all cvars
809         for (var = cvars->vars ; var ; var = var->next)
810                 var->flags &= ~CVAR_DEFAULTSET;
811 }
812
813
814 void Cvar_LockDefaults_f(cmd_state_t *cmd)
815 {
816         cvar_state_t *cvars = cmd->cvars;
817         cvar_t *var;
818         // lock in the default values of all cvars
819         for (var = cvars->vars ; var ; var = var->next)
820         {
821                 if (!(var->flags & CVAR_DEFAULTSET))
822                 {
823                         size_t alloclen;
824
825                         //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
826                         var->flags |= CVAR_DEFAULTSET;
827                         Z_Free((char *)var->defstring);
828                         alloclen = strlen(var->string) + 1;
829                         var->defstring = (char *)Z_Malloc(alloclen);
830                         memcpy((char *)var->defstring, var->string, alloclen);
831                 }
832         }
833 }
834
835 void Cvar_SaveInitState(cvar_state_t *cvars)
836 {
837         cvar_t *c;
838         for (c = cvars->vars;c;c = c->next)
839         {
840                 c->initstate = true;
841                 c->initflags = c->flags;
842                 c->initdefstring = Mem_strdup(zonemempool, c->defstring);
843                 c->initstring = Mem_strdup(zonemempool, c->string);
844                 c->initvalue = c->value;
845                 c->initinteger = c->integer;
846                 VectorCopy(c->vector, c->initvector);
847         }
848 }
849
850 void Cvar_RestoreInitState(cvar_state_t *cvars)
851 {
852         int hashindex;
853         cvar_t *c, **cp;
854         cvar_t *c2, **cp2;
855         for (cp = &cvars->vars;(c = *cp);)
856         {
857                 if (c->initstate)
858                 {
859                         // restore this cvar, it existed at init
860                         if (((c->flags ^ c->initflags) & CVAR_MAXFLAGSVAL)
861                          || strcmp(c->defstring ? c->defstring : "", c->initdefstring ? c->initdefstring : "")
862                          || strcmp(c->string ? c->string : "", c->initstring ? c->initstring : ""))
863                         {
864                                 Con_DPrintf("Cvar_RestoreInitState: Restoring cvar \"%s\"\n", c->name);
865                                 if (c->defstring)
866                                         Z_Free((char *)c->defstring);
867                                 c->defstring = Mem_strdup(zonemempool, c->initdefstring);
868                                 if (c->string)
869                                         Z_Free((char *)c->string);
870                                 c->string = Mem_strdup(zonemempool, c->initstring);
871                         }
872                         c->flags = c->initflags;
873                         c->value = c->initvalue;
874                         c->integer = c->initinteger;
875                         VectorCopy(c->initvector, c->vector);
876                         cp = &c->next;
877                 }
878                 else
879                 {
880                         if (!(c->flags & CVAR_ALLOCATED))
881                         {
882                                 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it was registered after init!\n", c->name);
883                                 // In this case, at least reset it to the default.
884                                 if((c->flags & CVAR_NORESETTODEFAULTS) == 0)
885                                         Cvar_SetQuick(c, c->defstring);
886                                 cp = &c->next;
887                                 continue;
888                         }
889                         if (Cvar_IsAutoCvar(c))
890                         {
891                                 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it is an autocvar used by running progs!\n", c->name);
892                                 // In this case, at least reset it to the default.
893                                 if((c->flags & CVAR_NORESETTODEFAULTS) == 0)
894                                         Cvar_SetQuick(c, c->defstring);
895                                 cp = &c->next;
896                                 continue;
897                         }
898                         // remove this cvar, it did not exist at init
899                         Con_DPrintf("Cvar_RestoreInitState: Destroying cvar \"%s\"\n", c->name);
900                         // unlink struct from hash
901                         hashindex = CRC_Block((const unsigned char *)c->name, strlen(c->name)) % CVAR_HASHSIZE;
902                         for (cp2 = &cvars->hashtable[hashindex]->cvar;(c2 = *cp2);)
903                         {
904                                 if (c2 == c)
905                                 {
906                                         *cp2 = cvars->hashtable[hashindex]->next->cvar;
907                                         break;
908                                 }
909                                 else
910                                         cp2 = &cvars->hashtable[hashindex]->next->cvar;
911                         }
912                         // unlink struct from main list
913                         *cp = c->next;
914                         // free strings
915                         if (c->defstring)
916                                 Z_Free((char *)c->defstring);
917                         if (c->string)
918                                 Z_Free((char *)c->string);
919                         if (c->description && c->description != cvar_dummy_description)
920                                 Z_Free((char *)c->description);
921                         // free struct
922                         Z_Free(c);
923                 }
924         }
925 }
926
927 void Cvar_ResetToDefaults_All_f(cmd_state_t *cmd)
928 {
929         cvar_state_t *cvars = cmd->cvars;
930         cvar_t *var;
931         // restore the default values of all cvars
932         for (var = cvars->vars ; var ; var = var->next)
933         {
934                 if((var->flags & CVAR_NORESETTODEFAULTS) == 0)
935                         Cvar_SetQuick(var, var->defstring);
936         }
937 }
938
939
940 void Cvar_ResetToDefaults_NoSaveOnly_f(cmd_state_t *cmd)
941 {
942         cvar_state_t *cvars = cmd->cvars;
943         cvar_t *var;
944         // restore the default values of all cvars
945         for (var = cvars->vars ; var ; var = var->next)
946         {
947                 if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == 0)
948                         Cvar_SetQuick(var, var->defstring);
949         }
950 }
951
952
953 void Cvar_ResetToDefaults_SaveOnly_f(cmd_state_t *cmd)
954 {
955         cvar_state_t *cvars = cmd->cvars;
956         cvar_t *var;
957         // restore the default values of all cvars
958         for (var = cvars->vars ; var ; var = var->next)
959         {
960                 if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == CVAR_SAVE)
961                         Cvar_SetQuick(var, var->defstring);
962         }
963 }
964
965
966 /*
967 ============
968 Cvar_WriteVariables
969
970 Writes lines containing "set variable value" for all variables
971 with the archive flag set to true.
972 ============
973 */
974 void Cvar_WriteVariables (cvar_state_t *cvars, qfile_t *f)
975 {
976         cvar_t  *var;
977         char buf1[MAX_INPUTLINE], buf2[MAX_INPUTLINE];
978
979         // don't save cvars that match their default value
980         for (var = cvars->vars ; var ; var = var->next) {
981                 if ((var->flags & CVAR_SAVE) && (strcmp(var->string, var->defstring) || ((var->flags & CVAR_ALLOCATED) && !(var->flags & CVAR_DEFAULTSET))))
982                 {
983                         Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$", false);
984                         Cmd_QuoteString(buf2, sizeof(buf2), var->string, "\"\\$", false);
985                         FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CVAR_ALLOCATED ? "seta " : "", buf1, buf2);
986                 }
987         }
988 }
989
990
991 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
992 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
993 /*
994 =========
995 Cvar_List
996 =========
997 */
998 void Cvar_List_f(cmd_state_t *cmd)
999 {
1000         cvar_state_t *cvars = cmd->cvars;
1001         cvar_t *cvar;
1002         const char *partial;
1003         int count;
1004         qboolean ispattern;
1005         char vabuf[1024];
1006
1007         if (Cmd_Argc(cmd) > 1)
1008         {
1009                 partial = Cmd_Argv(cmd, 1);
1010                 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1011                 if(!ispattern)
1012                         partial = va(vabuf, sizeof(vabuf), "%s*", partial);
1013         }
1014         else
1015         {
1016                 partial = va(vabuf, sizeof(vabuf), "*");
1017                 ispattern = false;
1018         }
1019
1020         count = 0;
1021         for (cvar = cvars->vars; cvar; cvar = cvar->next)
1022         {
1023                 if (matchpattern_with_separator(cvar->name, partial, false, "", false))
1024                 {
1025                         Cvar_PrintHelp(cvar, cvar->name, true);
1026                         count++;
1027                 }
1028                 for (int i = 0; i < cvar->aliasindex; i++)
1029                 {
1030                         if (matchpattern_with_separator(cvar->aliases[i], partial, false, "", false))
1031                         {
1032                                 Cvar_PrintHelp(cvar, cvar->aliases[i], true);
1033                                 count++;
1034                         }
1035                 }
1036         }
1037
1038         if (Cmd_Argc(cmd) > 1)
1039         {
1040                 if(ispattern)
1041                         Con_Printf("%i cvar%s matching \"%s\"\n", count, (count > 1) ? "s" : "", partial);
1042                 else
1043                         Con_Printf("%i cvar%s beginning with \"%s\"\n", count, (count > 1) ? "s" : "", Cmd_Argv(cmd,1));
1044         }
1045         else
1046                 Con_Printf("%i cvar(s)\n", count);
1047 }
1048 // 2000-01-09 CvarList command by Maddes
1049
1050 void Cvar_Set_f(cmd_state_t *cmd)
1051 {
1052         cvar_state_t *cvars = cmd->cvars;
1053         cvar_t *cvar;
1054
1055         // make sure it's the right number of parameters
1056         if (Cmd_Argc(cmd) < 3)
1057         {
1058                 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
1059                 return;
1060         }
1061
1062         // check if it's read-only
1063         cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1064         if (cvar)
1065                 if(Cvar_Readonly(cvar,"Set"))
1066                         return;
1067
1068         if (developer_extra.integer)
1069                 Con_DPrint("Set: ");
1070
1071         // all looks ok, create/modify the cvar
1072         Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
1073 }
1074
1075 void Cvar_SetA_f(cmd_state_t *cmd)
1076 {
1077         cvar_state_t *cvars = cmd->cvars;
1078         cvar_t *cvar;
1079
1080         // make sure it's the right number of parameters
1081         if (Cmd_Argc(cmd) < 3)
1082         {
1083                 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value> [<description>]\n");
1084                 return;
1085         }
1086
1087         // check if it's read-only
1088         cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1089         if (cvar)
1090                 if(Cvar_Readonly(cvar,"SetA"))
1091                         return;
1092
1093         if (developer_extra.integer)
1094                 Con_DPrint("SetA: ");
1095
1096         // all looks ok, create/modify the cvar
1097         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);
1098 }
1099
1100 void Cvar_Del_f(cmd_state_t *cmd)
1101 {
1102         cvar_state_t *cvars = cmd->cvars;
1103         int neededflags = ~0;
1104         int i;
1105         cvar_hash_t *hash, *parent, **link;
1106         cvar_t *cvar, *prev;
1107
1108         if(Cmd_Argc(cmd) < 2)
1109         {
1110                 Con_Printf("%s: wrong number of parameters, usage: unset <variablename1> [<variablename2> ...]\n", Cmd_Argv(cmd, 0));
1111                 return;
1112         }
1113         for(i = 1; i < Cmd_Argc(cmd); ++i)
1114         {
1115                 hash = Cvar_FindVarLink(cvars, Cmd_Argv(cmd, i), &parent, &link, &prev, neededflags);
1116                 cvar = hash->cvar;
1117
1118                 if(!cvar)
1119                 {
1120                         Con_Printf("%s: %s is not defined\n", Cmd_Argv(cmd, 0), Cmd_Argv(cmd, i));
1121                         continue;
1122                 }
1123                 if(Cvar_Readonly(cvar, Cmd_Argv(cmd, 0)))
1124                         continue;
1125                 if(!(cvar->flags & CVAR_ALLOCATED))
1126                 {
1127                         Con_Printf("%s: %s is static and cannot be deleted\n", Cmd_Argv(cmd, 0), cvar->name);
1128                         continue;
1129                 }
1130                 if(cvar == cvars->vars)
1131                 {
1132                         cvars->vars = cvar->next;
1133                 }
1134                 else
1135                 {
1136                         // in this case, prev must be set, otherwise there has been some inconsistensy
1137                         // elsewhere already... should I still check for prev != NULL?
1138                         prev->next = cvar->next;
1139                 }
1140
1141                 if(parent)
1142                         parent->next = hash->next;
1143                 else if(link)
1144                         *link = hash->next;
1145                 if(cvar->description != cvar_dummy_description)
1146                         Z_Free((char *)cvar->description);
1147
1148                 Z_Free((char *)cvar->name);
1149                 Z_Free((char *)cvar->string);
1150                 Z_Free((char *)cvar->defstring);
1151                 Z_Free(cvar);
1152         }
1153 }
1154
1155 #ifdef FILLALLCVARSWITHRUBBISH
1156 void Cvar_FillAll_f(cmd_state_t *cmd)
1157 {
1158         char *buf, *p, *q;
1159         int n, i;
1160         cvar_t *var;
1161         qboolean verify;
1162         if(Cmd_Argc(cmd) != 2)
1163         {
1164                 Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(cmd, 0));
1165                 Con_Printf("Usage: %s -length to verify that the rubbish is still there\n", Cmd_Argv(cmd, 0));
1166                 return;
1167         }
1168         n = atoi(Cmd_Argv(cmd, 1));
1169         verify = (n < 0);
1170         if(verify)
1171                 n = -n;
1172         buf = Z_Malloc(n + 1);
1173         buf[n] = 0;
1174         for(var = cvars->vars; var; var = var->next)
1175         {
1176                 for(i = 0, p = buf, q = var->name; i < n; ++i)
1177                 {
1178                         *p++ = *q++;
1179                         if(!*q)
1180                                 q = var->name;
1181                 }
1182                 if(verify && strcmp(var->string, buf))
1183                 {
1184                         Con_Printf("\n%s does not contain the right rubbish, either this is the first run or a possible overrun was detected, or something changed it intentionally; it DOES contain: %s\n", var->name, var->string);
1185                 }
1186                 Cvar_SetQuick(var, buf);
1187         }
1188         Z_Free(buf);
1189 }
1190 #endif /* FILLALLCVARSWITHRUBBISH */