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