]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cvar.c
Merge MR 'Fix inability to access non-ascii path under Windows with WTF-8'
[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 (char **alias = hash->cvar->aliases; alias && *alias; alias++)
48                                 if (!strcmp (var_name, *alias) && (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 (char **alias = hash->cvar->aliases; alias && *alias; alias++)
93                                 if (!strcmp (var_name, *alias) && (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 (char **alias = cvar->aliases; alias && *alias; alias++)
231                                 if (!strncasecmp(partial, *alias, 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 (char **alias = cvar->aliases; alias && *alias; alias++)
262                                 if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags))
263                                         buf[bpos++] = *alias;
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 (char **alias = cvar->aliases; alias && *alias; alias++)
296                                 if (!strncasecmp (partial, *alias, len) && (cvar->flags & neededflags))
297                                         Cvar_PrintHelp(cvar, *alias, 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 (!(var->flags & CF_REGISTERED) && !(var->flags & CF_ALLOCATED))
441         {
442                 Con_Printf(CON_WARN "Warning: Cvar_SetQuick() cannot set unregistered cvar \"%s\"\n", var->name);
443                 return; // setting an unregistered engine cvar crashes
444         }
445
446         if (developer_extra.integer)
447                 Con_DPrintf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
448
449         Cvar_SetQuick_Internal(var, value);
450 }
451
452 void Cvar_Set(cvar_state_t *cvars, const char *var_name, const char *value)
453 {
454         cvar_t *var;
455         var = Cvar_FindVar(cvars, var_name, ~0);
456         if (var == NULL)
457         {
458                 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
459                 return;
460         }
461         Cvar_SetQuick(var, value);
462 }
463
464 /*
465 ============
466 Cvar_SetValue
467 ============
468 */
469 void Cvar_SetValueQuick(cvar_t *var, float value)
470 {
471         char val[MAX_INPUTLINE];
472
473         if ((float)((int)value) == value)
474                 dpsnprintf(val, sizeof(val), "%i", (int)value);
475         else
476                 dpsnprintf(val, sizeof(val), "%f", value);
477         Cvar_SetQuick(var, val);
478 }
479
480 void Cvar_SetValue(cvar_state_t *cvars, const char *var_name, float value)
481 {
482         char val[MAX_INPUTLINE];
483
484         if ((float)((int)value) == value)
485                 dpsnprintf(val, sizeof(val), "%i", (int)value);
486         else
487                 dpsnprintf(val, sizeof(val), "%f", value);
488         Cvar_Set(cvars, var_name, val);
489 }
490
491 void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(cvar_t *))
492 {
493         if (variable == NULL)
494         {
495                 Con_Print("Cvar_RegisterCallback: var == NULL\n");
496                 return;
497         }
498         variable->callback = callback;
499 }
500
501 void Cvar_RegisterVirtual(cvar_t *variable, const char *name )
502 {
503         cvar_state_t *cvars = &cvars_all;
504         cvar_hash_t *hash;
505         int hashindex;
506
507         if (cls.state == ca_dedicated && !(variable->flags & CF_SERVER))
508                 return;
509
510         if(!*name)
511         {
512                 Con_Printf(CON_WARN "Cvar_RegisterVirtual: invalid virtual cvar name\n");
513                 return;
514         }
515
516         // check for overlap with a command
517         if (Cmd_Exists(cmd_local, name))
518         {
519                 Con_Printf(CON_WARN "Cvar_RegisterVirtual: %s is a command\n", name);
520                 return;
521         }
522
523         if(Cvar_FindVar(&cvars_all, name, 0))
524         {
525                 Con_Printf(CON_WARN "Cvar_RegisterVirtual: %s is a cvar\n", name);
526                 return;
527         }
528
529         // Resize the variable->aliases list to have room for another entry and a null terminator.
530         // This zero-pads when resizing, so we don't need to write the NULL terminator manually here.
531         // Also if aliases is NULL this allocates fresh for the correct size, so it's fine to just do this.
532         variable->aliases = (char **)Z_Realloc(variable->aliases, sizeof(char *) * (variable->aliases_size + 2));
533         // Add the new alias, and increment the number of aliases in the list
534         variable->aliases[variable->aliases_size++] = (char *)Z_strdup(name);
535
536         // link to head of list in this hash table index
537         hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
538         hashindex = CRC_Block((const unsigned char *)name, strlen(name)) % CVAR_HASHSIZE;
539         hash->next = cvars->hashtable[hashindex];
540         cvars->hashtable[hashindex] = hash;
541         hash->cvar = variable;
542 }
543
544 /*
545 ============
546 Cvar_Link
547
548 Links a variable to the variable list and hashtable
549 ============
550 */
551 static void Cvar_Link(cvar_t *variable, cvar_state_t *cvars)
552 {
553         cvar_t *current, *next;
554         cvar_hash_t *hash;
555         int hashindex;
556         /*
557          * Link the variable in
558          * alphanumerical order
559          */
560         for( current = NULL, next = cvars->vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
561                 ;
562         if(current)
563                 current->next = variable;
564         else
565                 cvars->vars = variable;
566         variable->next = next;
567
568         // link to head of list in this hash table index
569         hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
570         hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
571         hash->next = cvars->hashtable[hashindex];
572         hash->cvar = variable;
573         cvars->hashtable[hashindex] = hash;
574 }
575
576 /*
577 ============
578 Cvar_RegisterVariable
579
580 Adds a freestanding variable to the variable list.
581 ============
582 */
583 void Cvar_RegisterVariable (cvar_t *variable)
584 {
585         cvar_state_t *cvars = NULL;
586         cvar_t *current, *cvar;
587         int i;
588
589         switch (variable->flags & (CF_CLIENT | CF_SERVER))
590         {
591         case CF_CLIENT: // client-only cvar
592                 if (cls.state == ca_dedicated)
593                         return;
594         case CF_SERVER:
595         case CF_CLIENT | CF_SERVER:
596                 cvars = &cvars_all;
597                 break;
598         case 0:
599                 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with no CF_CLIENT | CF_SERVER flags\n", variable->name, variable->string, variable->flags);
600                 break;
601         default:
602                 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with weird CF_CLIENT | CF_SERVER flags\n", variable->name, variable->string, variable->flags);
603                 break;
604         }
605
606         if (developer_extra.integer)
607                 Con_DPrintf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
608
609         // first check to see if it has already been defined
610         cvar = Cvar_FindVar(cvars, variable->name, ~0);
611         if (cvar)
612         {
613                 if (cvar->flags & CF_ALLOCATED)
614                 {
615                         if (developer_extra.integer)
616                                 Con_DPrintf("...  replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
617                         // fixed variables replace allocated ones
618                         // (because the engine directly accesses fixed variables)
619                         // NOTE: this isn't actually used currently
620                         // (all cvars are registered before config parsing)
621                         variable->flags &= ~CF_ALLOCATED;
622                         // cvar->string is now owned by variable instead
623                         variable->string = cvar->string;
624                         variable->defstring = cvar->defstring;
625                         variable->value = atof (variable->string);
626                         variable->integer = (int) variable->value;
627                         // Preserve autocvar status.
628                         memcpy(variable->globaldefindex, cvar->globaldefindex, sizeof(variable->globaldefindex));
629                         memcpy(variable->globaldefindex_stringno, cvar->globaldefindex_stringno, sizeof(variable->globaldefindex_stringno));
630                         // replace cvar with this one...
631                         variable->next = cvar->next;
632                         if (cvars->vars == cvar)
633                         {
634                                 // head of the list is easy to change
635                                 cvars->vars = variable;
636                         }
637                         else
638                         {
639                                 // otherwise find it somewhere in the list
640                                 for (current = cvars->vars;current->next != cvar;current = current->next)
641                                         ;
642                                 current->next = variable;
643                         }
644
645                         // get rid of old allocated cvar
646                         // (but not cvar->string and cvar->defstring, because we kept those)
647                         Z_Free((char *)cvar->name);
648                         Z_Free(cvar);
649                 }
650                 else
651                         Con_DPrintf("Can't register variable %s, already defined\n", variable->name);
652                 return;
653         }
654
655         // check for overlap with a command
656         if (Cmd_Exists(cmd_local, variable->name))
657         {
658                 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
659                 return;
660         }
661
662         // copy the value off, because future sets will Z_Free it
663         variable->name = (char *)Mem_strdup(zonemempool, variable->name);
664         variable->string = (char *)Mem_strdup(zonemempool, variable->string);
665         variable->defstring = (char *)Mem_strdup(zonemempool, variable->string);
666         variable->value = atof (variable->string);
667         variable->integer = (int) variable->value;
668         variable->aliases = NULL;
669         variable->aliases_size = 0;
670         variable->initstate = NULL;
671
672         // Mark it as not an autocvar.
673         for (i = 0;i < PRVM_PROG_MAX;i++)
674                 variable->globaldefindex[i] = -1;
675
676         // Safe for Cvar_SetQuick()
677         variable->flags |= CF_REGISTERED;
678
679         Cvar_Link(variable, cvars);
680 }
681
682 /*
683 ============
684 Cvar_Get
685
686 Adds a newly allocated variable to the variable list or sets its value.
687 ============
688 */
689 cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int flags, const char *newdescription)
690 {
691         cvar_t *cvar;
692         int i;
693
694         if (developer_extra.integer)
695                 Con_DPrintf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
696
697         // first check to see if it has already been defined
698         cvar = Cvar_FindVar(cvars, name, ~0);
699         if (cvar)
700         {
701                 cvar->flags |= flags;
702                 Cvar_SetQuick_Internal (cvar, value);
703                 if(newdescription && (cvar->flags & CF_ALLOCATED))
704                 {
705                         if(cvar->description != cvar_dummy_description)
706                                 Z_Free((char *)cvar->description);
707
708                         if(*newdescription)
709                                 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
710                         else
711                                 cvar->description = cvar_dummy_description;
712                 }
713                 return cvar;
714         }
715
716         // check for pure evil
717         if (!*name)
718         {
719                 Con_Printf("Cvar_Get: invalid variable name\n");
720                 return NULL;
721         }
722
723         // check for overlap with a command
724         if (Cmd_Exists(cmd_local, name))
725         {
726                 Con_Printf("Cvar_Get: %s is a command\n", name);
727                 return NULL;
728         }
729
730         // allocate a new cvar, cvar name, and cvar string
731         // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
732         // FIXME: these never get Z_Free'd
733         cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
734         cvar->flags = flags | CF_ALLOCATED;
735         cvar->name = (char *)Mem_strdup(zonemempool, name);
736         cvar->string = (char *)Mem_strdup(zonemempool, value);
737         cvar->defstring = (char *)Mem_strdup(zonemempool, value);
738         cvar->value = atof (cvar->string);
739         cvar->integer = (int) cvar->value;
740         cvar->aliases = NULL;
741         cvar->aliases_size = 0;
742         cvar->initstate = NULL;
743
744         if(newdescription && *newdescription)
745                 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
746         else
747                 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
748
749         // Mark it as not an autocvar.
750         for (i = 0;i < PRVM_PROG_MAX;i++)
751                 cvar->globaldefindex[i] = -1;
752
753         Cvar_Link(cvar, cvars);
754
755         return cvar;
756 }
757
758 qbool Cvar_Readonly (cvar_t *var, const char *cmd_name)
759 {
760         if (var->flags & CF_READONLY)
761         {
762                 if(cmd_name)
763                         Con_Printf("%s: ",cmd_name);
764                 Con_Printf("%s", var->name);
765                 Con_Printf(" is read-only\n");
766                 return true;
767         }
768         return false;
769 }
770
771 /*
772 ============
773 Cvar_Command
774
775 Handles variable inspection and changing from the console
776 ============
777 */
778 qbool   Cvar_Command (cmd_state_t *cmd)
779 {
780         cvar_state_t    *cvars = cmd->cvars;
781         cvar_t                  *v;
782
783         // check variables
784         v = Cvar_FindVar(cvars, Cmd_Argv(cmd, 0), (cmd->cvars_flagsmask));
785         if (!v)
786                 return false;
787
788         // perform a variable print or set
789         if (Cmd_Argc(cmd) == 1)
790         {
791                 Cvar_PrintHelp(v, Cmd_Argv(cmd, 0), true);
792                 return true;
793         }
794
795         if (developer_extra.integer)
796                 Con_DPrint("Cvar_Command: ");
797
798         if(Cvar_Readonly(v, NULL))
799                 return true;
800
801         Cvar_SetQuick(v, Cmd_Argv(cmd, 1));
802         if (developer_extra.integer)
803                 Con_DPrint("\n");
804         return true;
805 }
806
807 void Cvar_UnlockDefaults(cmd_state_t *cmd)
808 {
809         cvar_state_t *cvars = cmd->cvars;
810         cvar_t *var;
811         // unlock the default values of all cvars
812         for (var = cvars->vars ; var ; var = var->next)
813                 var->flags &= ~CF_DEFAULTSET;
814 }
815
816 void Cvar_LockDefaults_f(cmd_state_t *cmd)
817 {
818         cvar_state_t *cvars = cmd->cvars;
819         cvar_t *var;
820         // lock in the default values of all cvars
821         for (var = cvars->vars ; var ; var = var->next)
822         {
823                 if (!(var->flags & CF_DEFAULTSET))
824                 {
825                         size_t alloclen;
826
827                         //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
828                         var->flags |= CF_DEFAULTSET;
829                         Z_Free((char *)var->defstring);
830                         alloclen = strlen(var->string) + 1;
831                         var->defstring = (char *)Z_Malloc(alloclen);
832                         memcpy((char *)var->defstring, var->string, alloclen);
833                 }
834         }
835 }
836
837 void Cvar_SaveInitState(cvar_state_t *cvars)
838 {
839         cvar_t *c;
840         for (c = cvars->vars;c;c = c->next)
841         {
842                 c->initstate = (cvar_t *)Z_Malloc(sizeof(cvar_t));
843                 memcpy(c->initstate, c, sizeof(cvar_t));
844         }
845 }
846
847 void Cvar_RestoreInitState(cvar_state_t *cvars)
848 {
849         int hashindex;
850         cvar_t *c, **cp;
851         cvar_t *c2, **cp2;
852         for (cp = &cvars->vars;(c = *cp);)
853         {
854                 if (c->initstate)
855                 {
856                         // restore this cvar, it existed at init
857                         if (((c->flags ^ c->initstate->flags) & CF_MAXFLAGSVAL)
858                          || strcmp(c->defstring ? c->defstring : "", c->initstate->defstring ? c->initstate->defstring : "")
859                          || strcmp(c->string ? c->string : "", c->initstate->string ? c->initstate->string : ""))
860                         {
861                                 Con_DPrintf("Cvar_RestoreInitState: Restoring cvar \"%s\"\n", c->name);
862                                 if (c->defstring)
863                                         Z_Free((char *)c->defstring);
864                                 c->defstring = Mem_strdup(zonemempool, c->initstate->defstring);
865                                 if (c->string)
866                                         Z_Free((char *)c->string);
867                                 c->string = Mem_strdup(zonemempool, c->initstate->string);
868                         }
869                         c->flags = c->initstate->flags;
870                         c->value = c->initstate->value;
871                         c->integer = c->initstate->integer;
872                         VectorCopy(c->initstate->vector, c->vector);
873                         cp = &c->next;
874                 }
875                 else
876                 {
877                         if (!(c->flags & CF_ALLOCATED))
878                         {
879                                 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it was registered after init!\n", c->name);
880                                 // In this case, at least reset it to the default.
881                                 if((c->flags & CF_PERSISTENT) == 0)
882                                         Cvar_SetQuick(c, c->defstring);
883                                 cp = &c->next;
884                                 continue;
885                         }
886                         if (Cvar_IsAutoCvar(c))
887                         {
888                                 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it is an autocvar used by running progs!\n", c->name);
889                                 // In this case, at least reset it to the default.
890                                 if((c->flags & CF_PERSISTENT) == 0)
891                                         Cvar_SetQuick(c, c->defstring);
892                                 cp = &c->next;
893                                 continue;
894                         }
895                         // remove this cvar, it did not exist at init
896                         Con_DPrintf("Cvar_RestoreInitState: Destroying cvar \"%s\"\n", c->name);
897                         // unlink struct from hash
898                         hashindex = CRC_Block((const unsigned char *)c->name, strlen(c->name)) % CVAR_HASHSIZE;
899                         for (cp2 = &cvars->hashtable[hashindex]->cvar;(c2 = *cp2);)
900                         {
901                                 if (c2 == c)
902                                 {
903                                         *cp2 = cvars->hashtable[hashindex]->next->cvar;
904                                         break;
905                                 }
906                                 else
907                                         cp2 = &cvars->hashtable[hashindex]->next->cvar;
908                         }
909                         // unlink struct from main list
910                         *cp = c->next;
911                         // free strings
912                         if (c->defstring)
913                                 Z_Free((char *)c->defstring);
914                         if (c->string)
915                                 Z_Free((char *)c->string);
916                         if (c->description && c->description != cvar_dummy_description)
917                                 Z_Free((char *)c->description);
918                         // free struct
919                         Z_Free(c);
920                 }
921         }
922 }
923
924 void Cvar_ResetToDefaults_All_f(cmd_state_t *cmd)
925 {
926         cvar_state_t *cvars = cmd->cvars;
927         cvar_t *var;
928         // restore the default values of all cvars
929         for (var = cvars->vars ; var ; var = var->next)
930         {
931                 if((var->flags & CF_PERSISTENT) == 0)
932                         Cvar_SetQuick(var, var->defstring);
933         }
934 }
935
936 void Cvar_ResetToDefaults_NoSaveOnly_f(cmd_state_t *cmd)
937 {
938         cvar_state_t *cvars = cmd->cvars;
939         cvar_t *var;
940         // restore the default values of all cvars
941         for (var = cvars->vars ; var ; var = var->next)
942         {
943                 if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == 0)
944                         Cvar_SetQuick(var, var->defstring);
945         }
946 }
947
948
949 void Cvar_ResetToDefaults_SaveOnly_f(cmd_state_t *cmd)
950 {
951         cvar_state_t *cvars = cmd->cvars;
952         cvar_t *var;
953         // restore the default values of all cvars
954         for (var = cvars->vars ; var ; var = var->next)
955         {
956                 if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == CF_ARCHIVE)
957                         Cvar_SetQuick(var, var->defstring);
958         }
959 }
960
961 /*
962 ============
963 Cvar_WriteVariables
964
965 Writes lines containing "set variable value" for all variables
966 with the archive flag set to true.
967 ============
968 */
969 void Cvar_WriteVariables (cvar_state_t *cvars, qfile_t *f)
970 {
971         cvar_t  *var;
972         char buf1[MAX_INPUTLINE], buf2[MAX_INPUTLINE];
973
974         // don't save cvars that match their default value
975         for (var = cvars->vars ; var ; var = var->next) {
976                 if ((var->flags & CF_ARCHIVE) && (strcmp(var->string, var->defstring) || ((var->flags & CF_ALLOCATED) && !(var->flags & CF_DEFAULTSET))))
977                 {
978                         Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$", false);
979                         Cmd_QuoteString(buf2, sizeof(buf2), var->string, "\"\\$", false);
980                         FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CF_ALLOCATED ? "seta " : "", buf1, buf2);
981                 }
982         }
983 }
984
985 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
986 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
987 /*
988 =========
989 Cvar_List
990 =========
991 */
992 void Cvar_List_f(cmd_state_t *cmd)
993 {
994         cvar_state_t *cvars = cmd->cvars;
995         cvar_t *cvar;
996         const char *partial;
997         int count;
998         qbool ispattern;
999         char vabuf[1024];
1000
1001         if (Cmd_Argc(cmd) > 1)
1002         {
1003                 partial = Cmd_Argv(cmd, 1);
1004                 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1005                 if(!ispattern)
1006                         partial = va(vabuf, sizeof(vabuf), "%s*", partial);
1007         }
1008         else
1009         {
1010                 partial = va(vabuf, sizeof(vabuf), "*");
1011                 ispattern = false;
1012         }
1013
1014         count = 0;
1015         for (cvar = cvars->vars; cvar; cvar = cvar->next)
1016         {
1017                 if (matchpattern_with_separator(cvar->name, partial, false, "", false))
1018                 {
1019                         Cvar_PrintHelp(cvar, cvar->name, true);
1020                         count++;
1021                 }
1022                 for (char **alias = cvar->aliases; alias && *alias; alias++)
1023                 {
1024                         if (matchpattern_with_separator(*alias, partial, false, "", false))
1025                         {
1026                                 Cvar_PrintHelp(cvar, *alias, true);
1027                                 count++;
1028                         }
1029                 }
1030         }
1031
1032         if (Cmd_Argc(cmd) > 1)
1033         {
1034                 if(ispattern)
1035                         Con_Printf("%i cvar%s matching \"%s\"\n", count, (count > 1) ? "s" : "", partial);
1036                 else
1037                         Con_Printf("%i cvar%s beginning with \"%s\"\n", count, (count > 1) ? "s" : "", Cmd_Argv(cmd,1));
1038         }
1039         else
1040                 Con_Printf("%i cvar(s)\n", count);
1041 }
1042 // 2000-01-09 CvarList command by Maddes
1043
1044 void Cvar_Set_f(cmd_state_t *cmd)
1045 {
1046         cvar_state_t *cvars = cmd->cvars;
1047         cvar_t *cvar;
1048
1049         // make sure it's the right number of parameters
1050         if (Cmd_Argc(cmd) < 3)
1051         {
1052                 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
1053                 return;
1054         }
1055
1056         // check if it's read-only
1057         cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1058         if (cvar)
1059                 if(Cvar_Readonly(cvar,"Set"))
1060                         return;
1061
1062         if (developer_extra.integer)
1063                 Con_DPrint("Set: ");
1064
1065         // all looks ok, create/modify the cvar
1066         Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
1067 }
1068
1069 void Cvar_SetA_f(cmd_state_t *cmd)
1070 {
1071         cvar_state_t *cvars = cmd->cvars;
1072         cvar_t *cvar;
1073
1074         // make sure it's the right number of parameters
1075         if (Cmd_Argc(cmd) < 3)
1076         {
1077                 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value> [<description>]\n");
1078                 return;
1079         }
1080
1081         // check if it's read-only
1082         cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1083         if (cvar)
1084                 if(Cvar_Readonly(cvar,"SetA"))
1085                         return;
1086
1087         if (developer_extra.integer)
1088                 Con_DPrint("SetA: ");
1089
1090         // all looks ok, create/modify the cvar
1091         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);
1092 }
1093
1094 void Cvar_Del_f(cmd_state_t *cmd)
1095 {
1096         cvar_state_t *cvars = cmd->cvars;
1097         int neededflags = ~0;
1098         int i;
1099         cvar_t *parent, **link;
1100         cvar_t *cvar, *prev;
1101
1102         if(Cmd_Argc(cmd) < 2)
1103         {
1104                 Con_Printf("%s: wrong number of parameters, usage: unset <variablename1> [<variablename2> ...]\n", Cmd_Argv(cmd, 0));
1105                 return;
1106         }
1107         for(i = 1; i < Cmd_Argc(cmd); ++i)
1108         {
1109                 cvar = Cvar_FindVarLink(cvars, Cmd_Argv(cmd, i), &parent, &link, &prev, neededflags);
1110
1111                 if(!cvar)
1112                 {
1113                         Con_Printf("%s: %s is not defined\n", Cmd_Argv(cmd, 0), Cmd_Argv(cmd, i));
1114                         continue;
1115                 }
1116                 if(Cvar_Readonly(cvar, Cmd_Argv(cmd, 0)))
1117                         continue;
1118                 if(!(cvar->flags & CF_ALLOCATED))
1119                 {
1120                         Con_Printf("%s: %s is static and cannot be deleted\n", Cmd_Argv(cmd, 0), cvar->name);
1121                         continue;
1122                 }
1123                 if(cvar == cvars->vars)
1124                 {
1125                         cvars->vars = cvar->next;
1126                 }
1127                 else
1128                 {
1129                         // in this case, prev must be set, otherwise there has been some inconsistensy
1130                         // elsewhere already... should I still check for prev != NULL?
1131                         prev->next = cvar->next;
1132                 }
1133
1134                 if(parent)
1135                         parent->next = cvar->next;
1136                 else if(link)
1137                         *link = cvar->next;
1138                 if(cvar->description != cvar_dummy_description)
1139                         Z_Free((char *)cvar->description);
1140
1141                 Z_Free((char *)cvar->name);
1142                 Z_Free((char *)cvar->string);
1143                 Z_Free((char *)cvar->defstring);
1144                 Z_Free(cvar);
1145         }
1146 }
1147
1148 #ifdef FILLALLCVARSWITHRUBBISH
1149 void Cvar_FillAll_f(cmd_state_t *cmd)
1150 {
1151         char *buf, *p, *q;
1152         int n, i;
1153         cvar_t *var;
1154         qbool verify;
1155         if(Cmd_Argc(cmd) != 2)
1156         {
1157                 Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(cmd, 0));
1158                 Con_Printf("Usage: %s -length to verify that the rubbish is still there\n", Cmd_Argv(cmd, 0));
1159                 return;
1160         }
1161         n = atoi(Cmd_Argv(cmd, 1));
1162         verify = (n < 0);
1163         if(verify)
1164                 n = -n;
1165         buf = Z_Malloc(n + 1);
1166         buf[n] = 0;
1167         for(var = cvars->vars; var; var = var->next)
1168         {
1169                 for(i = 0, p = buf, q = var->name; i < n; ++i)
1170                 {
1171                         *p++ = *q++;
1172                         if(!*q)
1173                                 q = var->name;
1174                 }
1175                 if(verify && strcmp(var->string, buf))
1176                 {
1177                         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);
1178                 }
1179                 Cvar_SetQuick(var, buf);
1180         }
1181         Z_Free(buf);
1182 }
1183 #endif /* FILLALLCVARSWITHRUBBISH */