]> git.xonotic.org Git - xonotic/darkplaces.git/blob - cvar.c
cmd: Merge cmd_client and cmd_server into cmd_local. Third time's a charm.
[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_t *Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name, cvar_t **parent, cvar_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]->cvar;
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->cvar;
95         }
96         return NULL;
97 match:
98         if(!prev_alpha || hash->cvar == cvars->vars)
99                 return hash->cvar;
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->cvar;
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, qbool full)
270 {
271         // Aliases are purple, cvars are yellow
272         if (strcmp(cvar->name, name))
273                 Con_Printf("^6");
274         else
275                 Con_Printf("^3");
276         Con_Printf("%s^7 is \"%s\" [\"%s\"]", name, ((cvar->flags & CF_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring);
277         if (strcmp(cvar->name, name))
278                 Con_Printf(" (also ^3%s^7)", cvar->name);
279         if (full)
280                 Con_Printf(" %s", cvar->description);
281         Con_Printf("\n");
282 }
283
284 // written by LadyHavoc
285 void Cvar_CompleteCvarPrint(cvar_state_t *cvars, const char *partial, int neededflags)
286 {
287         cvar_t *cvar;
288         size_t len = strlen(partial);
289         // Loop through the command list and print all matches
290         for (cvar = cvars->vars; cvar; cvar = cvar->next)
291                 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
292                         Cvar_PrintHelp(cvar, cvar->name, true);
293                 else
294                         for (int i = 0; i < cvar->aliasindex; i++)
295                                 if (!strncasecmp (partial, cvar->aliases[i], len) && (cvar->flags & neededflags))
296                                         Cvar_PrintHelp(cvar, cvar->aliases[i], true);
297
298                 
299 }
300
301 // check if a cvar is held by some progs
302 static qbool Cvar_IsAutoCvar(cvar_t *var)
303 {
304         int i;
305         prvm_prog_t *prog;
306         for (i = 0;i < PRVM_PROG_MAX;i++)
307         {
308                 prog = &prvm_prog_list[i];
309                 if (prog->loaded && var->globaldefindex[i] >= 0)
310                         return true;
311         }
312         return false;
313 }
314
315 // we assume that prog is already set to the target progs
316 static void Cvar_UpdateAutoCvar(cvar_t *var)
317 {
318         int i;
319         int j;
320         const char *s;
321         vec3_t v;
322         prvm_prog_t *prog;
323         for (i = 0;i < PRVM_PROG_MAX;i++)
324         {
325                 prog = &prvm_prog_list[i];
326                 if (prog->loaded && var->globaldefindex[i] >= 0)
327                 {
328                         // MUST BE SYNCED WITH prvm_edict.c PRVM_LoadProgs
329                         switch(prog->globaldefs[var->globaldefindex[i]].type & ~DEF_SAVEGLOBAL)
330                         {
331                         case ev_float:
332                                 PRVM_GLOBALFIELDFLOAT(prog->globaldefs[var->globaldefindex[i]].ofs) = var->value;
333                                 break;
334                         case ev_vector:
335                                 s = var->string;
336                                 VectorClear(v);
337                                 for (j = 0;j < 3;j++)
338                                 {
339                                         while (*s && ISWHITESPACE(*s))
340                                                 s++;
341                                         if (!*s)
342                                                 break;
343                                         v[j] = atof(s);
344                                         while (!ISWHITESPACE(*s))
345                                                 s++;
346                                         if (!*s)
347                                                 break;
348                                 }
349                                 VectorCopy(v, PRVM_GLOBALFIELDVECTOR(prog->globaldefs[var->globaldefindex[i]].ofs));
350                                 break;
351                         case ev_string:
352                                 PRVM_ChangeEngineString(prog, var->globaldefindex_stringno[i], var->string);
353                                 PRVM_GLOBALFIELDSTRING(prog->globaldefs[var->globaldefindex[i]].ofs) = var->globaldefindex_stringno[i];
354                                 break;
355                         }
356                 }
357         }
358 }
359
360 // called after loading a savegame
361 void Cvar_UpdateAllAutoCvars(cvar_state_t *cvars)
362 {
363         cvar_t *var;
364         for (var = cvars->vars ; var ; var = var->next)
365                 Cvar_UpdateAutoCvar(var);
366 }
367
368 void Cvar_Callback(cvar_t *var)
369 {
370         if (var == NULL)
371         {
372                 Con_Print("Cvar_Callback: var == NULL\n");
373                 return;
374         }
375
376         if(var->callback)
377                 var->callback(var);
378 }
379
380 /*
381 ============
382 Cvar_Set
383 ============
384 */
385 extern cvar_t sv_disablenotify;
386 static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
387 {
388         qbool changed;
389         size_t valuelen;
390
391         changed = strcmp(var->string, value) != 0;
392         // LadyHavoc: don't reallocate when there is no change
393         if (!changed)
394                 return;
395
396         // LadyHavoc: don't reallocate when the buffer is the same size
397         valuelen = strlen(value);
398         if (!var->string || strlen(var->string) != valuelen)
399         {
400                 Z_Free ((char *)var->string);   // free the old value string
401
402                 var->string = (char *)Z_Malloc (valuelen + 1);
403         }
404         memcpy ((char *)var->string, value, valuelen + 1);
405         var->value = atof (var->string);
406         var->integer = (int) var->value;
407         if ((var->flags & CF_NOTIFY) && sv.active && !sv_disablenotify.integer)
408                 SV_BroadcastPrintf("\003^3Server cvar \"%s\" changed to \"%s\"\n", var->name, var->string);
409 #if 0
410         // TODO: add infostring support to the server?
411         if ((var->flags & CF_SERVERINFO) && changed && sv.active)
412         {
413                 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
414                 if (sv.active)
415                 {
416                         MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
417                         MSG_WriteString (&sv.reliable_datagram, var->name);
418                         MSG_WriteString (&sv.reliable_datagram, var->string);
419                 }
420         }
421 #endif
422         if (var->flags & CF_USERINFO)
423                 CL_SetInfo(var->name, var->string, true, false, false, false);
424
425         Cvar_UpdateAutoCvar(var);
426
427         // Call the function stored in the cvar for bounds checking, cleanup, etc
428         Cvar_Callback(var);
429 }
430
431 void Cvar_SetQuick (cvar_t *var, const char *value)
432 {
433         if (var == NULL)
434         {
435                 Con_Print("Cvar_SetQuick: var == NULL\n");
436                 return;
437         }
438
439         if (developer_extra.integer)
440                 Con_DPrintf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
441
442         Cvar_SetQuick_Internal(var, value);
443 }
444
445 void Cvar_Set(cvar_state_t *cvars, const char *var_name, const char *value)
446 {
447         cvar_t *var;
448         var = Cvar_FindVar(cvars, var_name, ~0);
449         if (var == NULL)
450         {
451                 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
452                 return;
453         }
454         Cvar_SetQuick(var, value);
455 }
456
457 /*
458 ============
459 Cvar_SetValue
460 ============
461 */
462 void Cvar_SetValueQuick(cvar_t *var, float value)
463 {
464         char val[MAX_INPUTLINE];
465
466         if ((float)((int)value) == value)
467                 dpsnprintf(val, sizeof(val), "%i", (int)value);
468         else
469                 dpsnprintf(val, sizeof(val), "%f", value);
470         Cvar_SetQuick(var, val);
471 }
472
473 void Cvar_SetValue(cvar_state_t *cvars, const char *var_name, float value)
474 {
475         char val[MAX_INPUTLINE];
476
477         if ((float)((int)value) == value)
478                 dpsnprintf(val, sizeof(val), "%i", (int)value);
479         else
480                 dpsnprintf(val, sizeof(val), "%f", value);
481         Cvar_Set(cvars, var_name, val);
482 }
483
484 void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(cvar_t *))
485 {
486         if (variable == NULL)
487         {
488                 Con_Print("Cvar_RegisterCallback: var == NULL\n");
489                 return;
490         }
491         variable->callback = callback;
492 }
493
494 void Cvar_RegisterAlias(cvar_t *variable, const char *alias )
495 {
496         cvar_state_t *cvars = &cvars_all;
497         cvar_hash_t *hash;
498         int hashindex;
499
500         variable->aliases = (char **)Mem_Realloc(zonemempool, variable->aliases, sizeof(char *) * (variable->aliasindex + 1));
501         // Add to it
502         variable->aliases[variable->aliasindex] = (char *)Z_Malloc(strlen(alias) + 1);
503         memcpy(variable->aliases[variable->aliasindex], alias, strlen(alias) + 1);
504         variable->aliasindex++;
505
506         // link to head of list in this hash table index
507         hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
508         hashindex = CRC_Block((const unsigned char *)alias, strlen(alias)) % CVAR_HASHSIZE;
509         hash->next = cvars->hashtable[hashindex];
510         cvars->hashtable[hashindex] = hash;
511         hash->cvar = variable;
512 }
513
514 /*
515 ============
516 Cvar_Link
517
518 Links a variable to the variable list and hashtable
519 ============
520 */
521 static void Cvar_Link(cvar_t *variable, cvar_state_t *cvars)
522 {
523         cvar_t *current, *next;
524         cvar_hash_t *hash;
525         int hashindex;
526         /*
527          * Link the variable in
528          * alphanumerical order
529          */
530         for( current = NULL, next = cvars->vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
531                 ;
532         if(current)
533                 current->next = variable;
534         else
535                 cvars->vars = variable;
536         variable->next = next;
537
538         // link to head of list in this hash table index
539         hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
540         hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
541         hash->next = cvars->hashtable[hashindex];
542         hash->cvar = variable;
543         cvars->hashtable[hashindex] = hash;
544 }
545
546 /*
547 ============
548 Cvar_RegisterVariable
549
550 Adds a freestanding variable to the variable list.
551 ============
552 */
553 void Cvar_RegisterVariable (cvar_t *variable)
554 {
555         cvar_state_t *cvars = NULL;
556         cvar_t *current, *cvar;
557         int i;
558
559         switch (variable->flags & (CF_CLIENT | CF_SERVER))
560         {
561         case CF_CLIENT:
562         case CF_SERVER:
563         case CF_CLIENT | CF_SERVER:
564                 cvars = &cvars_all;
565                 break;
566         case 0:
567                 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with no CF_CLIENT | CF_SERVER flags\n", variable->name, variable->string, variable->flags);
568                 break;
569         default:
570                 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with weird CF_CLIENT | CF_SERVER flags\n", variable->name, variable->string, variable->flags);
571                 break;
572         }
573
574         if (developer_extra.integer)
575                 Con_DPrintf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
576
577         // first check to see if it has already been defined
578         cvar = Cvar_FindVar(cvars, variable->name, ~0);
579         if (cvar)
580         {
581                 if (cvar->flags & CF_ALLOCATED)
582                 {
583                         if (developer_extra.integer)
584                                 Con_DPrintf("...  replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
585                         // fixed variables replace allocated ones
586                         // (because the engine directly accesses fixed variables)
587                         // NOTE: this isn't actually used currently
588                         // (all cvars are registered before config parsing)
589                         variable->flags |= (cvar->flags & ~CF_ALLOCATED);
590                         // cvar->string is now owned by variable instead
591                         variable->string = cvar->string;
592                         variable->defstring = cvar->defstring;
593                         variable->value = atof (variable->string);
594                         variable->integer = (int) variable->value;
595                         // Preserve autocvar status.
596                         memcpy(variable->globaldefindex, cvar->globaldefindex, sizeof(variable->globaldefindex));
597                         memcpy(variable->globaldefindex_stringno, cvar->globaldefindex_stringno, sizeof(variable->globaldefindex_stringno));
598                         // replace cvar with this one...
599                         variable->next = cvar->next;
600                         if (cvars->vars == cvar)
601                         {
602                                 // head of the list is easy to change
603                                 cvars->vars = variable;
604                         }
605                         else
606                         {
607                                 // otherwise find it somewhere in the list
608                                 for (current = cvars->vars;current->next != cvar;current = current->next)
609                                         ;
610                                 current->next = variable;
611                         }
612
613                         // get rid of old allocated cvar
614                         // (but not cvar->string and cvar->defstring, because we kept those)
615                         Z_Free((char *)cvar->name);
616                         Z_Free(cvar);
617                 }
618                 else
619                         Con_DPrintf("Can't register variable %s, already defined\n", variable->name);
620                 return;
621         }
622
623         // check for overlap with a command
624         if (Cmd_Exists(cmd_local, variable->name) || Cmd_Exists(cmd_local, variable->name))
625         {
626                 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
627                 return;
628         }
629
630         // copy the value off, because future sets will Z_Free it
631         variable->name = (char *)Mem_strdup(zonemempool, variable->name);
632         variable->string = (char *)Mem_strdup(zonemempool, variable->string);
633         variable->defstring = (char *)Mem_strdup(zonemempool, variable->string);
634         variable->value = atof (variable->string);
635         variable->integer = (int) variable->value;
636         variable->aliasindex = 0;
637         variable->initstate = NULL;
638
639         // Mark it as not an autocvar.
640         for (i = 0;i < PRVM_PROG_MAX;i++)
641                 variable->globaldefindex[i] = -1;
642
643         Cvar_Link(variable, cvars);
644 }
645
646 /*
647 ============
648 Cvar_Get
649
650 Adds a newly allocated variable to the variable list or sets its value.
651 ============
652 */
653 cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int flags, const char *newdescription)
654 {
655         cvar_t *cvar;
656         int i;
657
658         if (developer_extra.integer)
659                 Con_DPrintf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
660
661         // first check to see if it has already been defined
662         cvar = Cvar_FindVar(cvars, name, ~0);
663         if (cvar)
664         {
665                 cvar->flags |= flags;
666                 Cvar_SetQuick_Internal (cvar, value);
667                 if(newdescription && (cvar->flags & CF_ALLOCATED))
668                 {
669                         if(cvar->description != cvar_dummy_description)
670                                 Z_Free((char *)cvar->description);
671
672                         if(*newdescription)
673                                 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
674                         else
675                                 cvar->description = cvar_dummy_description;
676                 }
677                 return cvar;
678         }
679
680         // check for pure evil
681         if (!*name)
682         {
683                 Con_Printf("Cvar_Get: invalid variable name\n");
684                 return NULL;
685         }
686
687         // check for overlap with a command
688         if (Cmd_Exists(cmd_local, name) || Cmd_Exists(cmd_local, name))
689         {
690                 Con_Printf("Cvar_Get: %s is a command\n", name);
691                 return NULL;
692         }
693
694         // allocate a new cvar, cvar name, and cvar string
695         // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
696         // FIXME: these never get Z_Free'd
697         cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
698         cvar->flags = flags | CF_ALLOCATED;
699         cvar->name = (char *)Mem_strdup(zonemempool, name);
700         cvar->string = (char *)Mem_strdup(zonemempool, value);
701         cvar->defstring = (char *)Mem_strdup(zonemempool, value);
702         cvar->value = atof (cvar->string);
703         cvar->integer = (int) cvar->value;
704         cvar->aliases = (char **)Z_Malloc(sizeof(char **));
705         cvar->initstate = NULL;
706         memset(cvar->aliases, 0, sizeof(char *));
707
708         if(newdescription && *newdescription)
709                 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
710         else
711                 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
712
713         // Mark it as not an autocvar.
714         for (i = 0;i < PRVM_PROG_MAX;i++)
715                 cvar->globaldefindex[i] = -1;
716
717         Cvar_Link(cvar, cvars);
718
719         return cvar;
720 }
721
722 qbool Cvar_Readonly (cvar_t *var, const char *cmd_name)
723 {
724         if (var->flags & CF_READONLY)
725         {
726                 if(cmd_name)
727                         Con_Printf("%s: ",cmd_name);
728                 Con_Printf("%s", var->name);
729                 Con_Printf(" is read-only\n");
730                 return true;
731         }
732         return false;
733 }
734
735 /*
736 ============
737 Cvar_Command
738
739 Handles variable inspection and changing from the console
740 ============
741 */
742 qbool   Cvar_Command (cmd_state_t *cmd)
743 {
744         cvar_state_t    *cvars = cmd->cvars;
745         cvar_t                  *v;
746
747         // check variables
748         v = Cvar_FindVar(cvars, Cmd_Argv(cmd, 0), (cmd->cvars_flagsmask));
749         if (!v)
750                 return false;
751
752         // perform a variable print or set
753         if (Cmd_Argc(cmd) == 1)
754         {
755                 Cvar_PrintHelp(v, Cmd_Argv(cmd, 0), true);
756                 return true;
757         }
758
759         if (developer_extra.integer)
760                 Con_DPrint("Cvar_Command: ");
761
762         if(Cvar_Readonly(v, NULL))
763                 return true;
764
765         Cvar_SetQuick(v, Cmd_Argv(cmd, 1));
766         if (developer_extra.integer)
767                 Con_DPrint("\n");
768         return true;
769 }
770
771 void Cvar_UnlockDefaults(cmd_state_t *cmd)
772 {
773         cvar_state_t *cvars = cmd->cvars;
774         cvar_t *var;
775         // unlock the default values of all cvars
776         for (var = cvars->vars ; var ; var = var->next)
777                 var->flags &= ~CF_DEFAULTSET;
778 }
779
780 void Cvar_LockDefaults_f(cmd_state_t *cmd)
781 {
782         cvar_state_t *cvars = cmd->cvars;
783         cvar_t *var;
784         // lock in the default values of all cvars
785         for (var = cvars->vars ; var ; var = var->next)
786         {
787                 if (!(var->flags & CF_DEFAULTSET))
788                 {
789                         size_t alloclen;
790
791                         //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
792                         var->flags |= CF_DEFAULTSET;
793                         Z_Free((char *)var->defstring);
794                         alloclen = strlen(var->string) + 1;
795                         var->defstring = (char *)Z_Malloc(alloclen);
796                         memcpy((char *)var->defstring, var->string, alloclen);
797                 }
798         }
799 }
800
801 void Cvar_SaveInitState(cvar_state_t *cvars)
802 {
803         cvar_t *c;
804         for (c = cvars->vars;c;c = c->next)
805         {
806                 c->initstate = (cvar_t *)Z_Malloc(sizeof(cvar_t));
807                 memcpy(c->initstate, c, sizeof(cvar_t));
808         }
809 }
810
811 void Cvar_RestoreInitState(cvar_state_t *cvars)
812 {
813         int hashindex;
814         cvar_t *c, **cp;
815         cvar_t *c2, **cp2;
816         for (cp = &cvars->vars;(c = *cp);)
817         {
818                 if (c->initstate)
819                 {
820                         // restore this cvar, it existed at init
821                         if (((c->flags ^ c->initstate->flags) & CF_MAXFLAGSVAL)
822                          || strcmp(c->defstring ? c->defstring : "", c->initstate->defstring ? c->initstate->defstring : "")
823                          || strcmp(c->string ? c->string : "", c->initstate->string ? c->initstate->string : ""))
824                         {
825                                 Con_DPrintf("Cvar_RestoreInitState: Restoring cvar \"%s\"\n", c->name);
826                                 if (c->defstring)
827                                         Z_Free((char *)c->defstring);
828                                 c->defstring = Mem_strdup(zonemempool, c->initstate->defstring);
829                                 if (c->string)
830                                         Z_Free((char *)c->string);
831                                 c->string = Mem_strdup(zonemempool, c->initstate->string);
832                         }
833                         c->flags = c->initstate->flags;
834                         c->value = c->initstate->value;
835                         c->integer = c->initstate->integer;
836                         VectorCopy(c->initstate->vector, c->vector);
837                         cp = &c->next;
838                 }
839                 else
840                 {
841                         if (!(c->flags & CF_ALLOCATED))
842                         {
843                                 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it was registered after init!\n", c->name);
844                                 // In this case, at least reset it to the default.
845                                 if((c->flags & CF_PERSISTENT) == 0)
846                                         Cvar_SetQuick(c, c->defstring);
847                                 cp = &c->next;
848                                 continue;
849                         }
850                         if (Cvar_IsAutoCvar(c))
851                         {
852                                 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it is an autocvar used by running progs!\n", c->name);
853                                 // In this case, at least reset it to the default.
854                                 if((c->flags & CF_PERSISTENT) == 0)
855                                         Cvar_SetQuick(c, c->defstring);
856                                 cp = &c->next;
857                                 continue;
858                         }
859                         // remove this cvar, it did not exist at init
860                         Con_DPrintf("Cvar_RestoreInitState: Destroying cvar \"%s\"\n", c->name);
861                         // unlink struct from hash
862                         hashindex = CRC_Block((const unsigned char *)c->name, strlen(c->name)) % CVAR_HASHSIZE;
863                         for (cp2 = &cvars->hashtable[hashindex]->cvar;(c2 = *cp2);)
864                         {
865                                 if (c2 == c)
866                                 {
867                                         *cp2 = cvars->hashtable[hashindex]->next->cvar;
868                                         break;
869                                 }
870                                 else
871                                         cp2 = &cvars->hashtable[hashindex]->next->cvar;
872                         }
873                         // unlink struct from main list
874                         *cp = c->next;
875                         // free strings
876                         if (c->defstring)
877                                 Z_Free((char *)c->defstring);
878                         if (c->string)
879                                 Z_Free((char *)c->string);
880                         if (c->description && c->description != cvar_dummy_description)
881                                 Z_Free((char *)c->description);
882                         // free struct
883                         Z_Free(c);
884                 }
885         }
886 }
887
888 void Cvar_ResetToDefaults_All_f(cmd_state_t *cmd)
889 {
890         cvar_state_t *cvars = cmd->cvars;
891         cvar_t *var;
892         // restore the default values of all cvars
893         for (var = cvars->vars ; var ; var = var->next)
894         {
895                 if((var->flags & CF_PERSISTENT) == 0)
896                         Cvar_SetQuick(var, var->defstring);
897         }
898 }
899
900 void Cvar_ResetToDefaults_NoSaveOnly_f(cmd_state_t *cmd)
901 {
902         cvar_state_t *cvars = cmd->cvars;
903         cvar_t *var;
904         // restore the default values of all cvars
905         for (var = cvars->vars ; var ; var = var->next)
906         {
907                 if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == 0)
908                         Cvar_SetQuick(var, var->defstring);
909         }
910 }
911
912
913 void Cvar_ResetToDefaults_SaveOnly_f(cmd_state_t *cmd)
914 {
915         cvar_state_t *cvars = cmd->cvars;
916         cvar_t *var;
917         // restore the default values of all cvars
918         for (var = cvars->vars ; var ; var = var->next)
919         {
920                 if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == CF_ARCHIVE)
921                         Cvar_SetQuick(var, var->defstring);
922         }
923 }
924
925 /*
926 ============
927 Cvar_WriteVariables
928
929 Writes lines containing "set variable value" for all variables
930 with the archive flag set to true.
931 ============
932 */
933 void Cvar_WriteVariables (cvar_state_t *cvars, qfile_t *f)
934 {
935         cvar_t  *var;
936         char buf1[MAX_INPUTLINE], buf2[MAX_INPUTLINE];
937
938         // don't save cvars that match their default value
939         for (var = cvars->vars ; var ; var = var->next) {
940                 if ((var->flags & CF_ARCHIVE) && (strcmp(var->string, var->defstring) || ((var->flags & CF_ALLOCATED) && !(var->flags & CF_DEFAULTSET))))
941                 {
942                         Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$", false);
943                         Cmd_QuoteString(buf2, sizeof(buf2), var->string, "\"\\$", false);
944                         FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CF_ALLOCATED ? "seta " : "", buf1, buf2);
945                 }
946         }
947 }
948
949 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
950 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
951 /*
952 =========
953 Cvar_List
954 =========
955 */
956 void Cvar_List_f(cmd_state_t *cmd)
957 {
958         cvar_state_t *cvars = cmd->cvars;
959         cvar_t *cvar;
960         const char *partial;
961         int count;
962         qbool ispattern;
963         char vabuf[1024];
964
965         if (Cmd_Argc(cmd) > 1)
966         {
967                 partial = Cmd_Argv(cmd, 1);
968                 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
969                 if(!ispattern)
970                         partial = va(vabuf, sizeof(vabuf), "%s*", partial);
971         }
972         else
973         {
974                 partial = va(vabuf, sizeof(vabuf), "*");
975                 ispattern = false;
976         }
977
978         count = 0;
979         for (cvar = cvars->vars; cvar; cvar = cvar->next)
980         {
981                 if (matchpattern_with_separator(cvar->name, partial, false, "", false))
982                 {
983                         Cvar_PrintHelp(cvar, cvar->name, true);
984                         count++;
985                 }
986                 for (int i = 0; i < cvar->aliasindex; i++)
987                 {
988                         if (matchpattern_with_separator(cvar->aliases[i], partial, false, "", false))
989                         {
990                                 Cvar_PrintHelp(cvar, cvar->aliases[i], true);
991                                 count++;
992                         }
993                 }
994         }
995
996         if (Cmd_Argc(cmd) > 1)
997         {
998                 if(ispattern)
999                         Con_Printf("%i cvar%s matching \"%s\"\n", count, (count > 1) ? "s" : "", partial);
1000                 else
1001                         Con_Printf("%i cvar%s beginning with \"%s\"\n", count, (count > 1) ? "s" : "", Cmd_Argv(cmd,1));
1002         }
1003         else
1004                 Con_Printf("%i cvar(s)\n", count);
1005 }
1006 // 2000-01-09 CvarList command by Maddes
1007
1008 void Cvar_Set_f(cmd_state_t *cmd)
1009 {
1010         cvar_state_t *cvars = cmd->cvars;
1011         cvar_t *cvar;
1012
1013         // make sure it's the right number of parameters
1014         if (Cmd_Argc(cmd) < 3)
1015         {
1016                 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
1017                 return;
1018         }
1019
1020         // check if it's read-only
1021         cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1022         if (cvar)
1023                 if(Cvar_Readonly(cvar,"Set"))
1024                         return;
1025
1026         if (developer_extra.integer)
1027                 Con_DPrint("Set: ");
1028
1029         // all looks ok, create/modify the cvar
1030         Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
1031 }
1032
1033 void Cvar_SetA_f(cmd_state_t *cmd)
1034 {
1035         cvar_state_t *cvars = cmd->cvars;
1036         cvar_t *cvar;
1037
1038         // make sure it's the right number of parameters
1039         if (Cmd_Argc(cmd) < 3)
1040         {
1041                 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value> [<description>]\n");
1042                 return;
1043         }
1044
1045         // check if it's read-only
1046         cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1047         if (cvar)
1048                 if(Cvar_Readonly(cvar,"SetA"))
1049                         return;
1050
1051         if (developer_extra.integer)
1052                 Con_DPrint("SetA: ");
1053
1054         // all looks ok, create/modify the cvar
1055         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);
1056 }
1057
1058 void Cvar_Del_f(cmd_state_t *cmd)
1059 {
1060         cvar_state_t *cvars = cmd->cvars;
1061         int neededflags = ~0;
1062         int i;
1063         cvar_t *parent, **link;
1064         cvar_t *cvar, *prev;
1065
1066         if(Cmd_Argc(cmd) < 2)
1067         {
1068                 Con_Printf("%s: wrong number of parameters, usage: unset <variablename1> [<variablename2> ...]\n", Cmd_Argv(cmd, 0));
1069                 return;
1070         }
1071         for(i = 1; i < Cmd_Argc(cmd); ++i)
1072         {
1073                 cvar = Cvar_FindVarLink(cvars, Cmd_Argv(cmd, i), &parent, &link, &prev, neededflags);
1074
1075                 if(!cvar)
1076                 {
1077                         Con_Printf("%s: %s is not defined\n", Cmd_Argv(cmd, 0), Cmd_Argv(cmd, i));
1078                         continue;
1079                 }
1080                 if(Cvar_Readonly(cvar, Cmd_Argv(cmd, 0)))
1081                         continue;
1082                 if(!(cvar->flags & CF_ALLOCATED))
1083                 {
1084                         Con_Printf("%s: %s is static and cannot be deleted\n", Cmd_Argv(cmd, 0), cvar->name);
1085                         continue;
1086                 }
1087                 if(cvar == cvars->vars)
1088                 {
1089                         cvars->vars = cvar->next;
1090                 }
1091                 else
1092                 {
1093                         // in this case, prev must be set, otherwise there has been some inconsistensy
1094                         // elsewhere already... should I still check for prev != NULL?
1095                         prev->next = cvar->next;
1096                 }
1097
1098                 if(parent)
1099                         parent->next = cvar->next;
1100                 else if(link)
1101                         *link = cvar->next;
1102                 if(cvar->description != cvar_dummy_description)
1103                         Z_Free((char *)cvar->description);
1104
1105                 Z_Free((char *)cvar->name);
1106                 Z_Free((char *)cvar->string);
1107                 Z_Free((char *)cvar->defstring);
1108                 Z_Free(cvar);
1109         }
1110 }
1111
1112 #ifdef FILLALLCVARSWITHRUBBISH
1113 void Cvar_FillAll_f(cmd_state_t *cmd)
1114 {
1115         char *buf, *p, *q;
1116         int n, i;
1117         cvar_t *var;
1118         qbool verify;
1119         if(Cmd_Argc(cmd) != 2)
1120         {
1121                 Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(cmd, 0));
1122                 Con_Printf("Usage: %s -length to verify that the rubbish is still there\n", Cmd_Argv(cmd, 0));
1123                 return;
1124         }
1125         n = atoi(Cmd_Argv(cmd, 1));
1126         verify = (n < 0);
1127         if(verify)
1128                 n = -n;
1129         buf = Z_Malloc(n + 1);
1130         buf[n] = 0;
1131         for(var = cvars->vars; var; var = var->next)
1132         {
1133                 for(i = 0, p = buf, q = var->name; i < n; ++i)
1134                 {
1135                         *p++ = *q++;
1136                         if(!*q)
1137                                 q = var->name;
1138                 }
1139                 if(verify && strcmp(var->string, buf))
1140                 {
1141                         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);
1142                 }
1143                 Cvar_SetQuick(var, buf);
1144         }
1145         Z_Free(buf);
1146 }
1147 #endif /* FILLALLCVARSWITHRUBBISH */