]> icculus.org git repositories - divverent/darkplaces.git/blob - cvar.c
unset command
[divverent/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 char *cvar_dummy_description = "custom cvar";
25
26 cvar_t *cvar_vars = NULL;
27 cvar_t *cvar_hashtable[CVAR_HASHSIZE];
28 char *cvar_null_string = "";
29
30 /*
31 ============
32 Cvar_FindVar
33 ============
34 */
35 cvar_t *Cvar_FindVar (const char *var_name)
36 {
37         int hashindex;
38         cvar_t *var;
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 (var = cvar_hashtable[hashindex];var;var = var->nextonhashchain)
43                 if (!strcmp (var_name, var->name))
44                         return var;
45
46         return NULL;
47 }
48
49 cvar_t *Cvar_FindVarAfter (const char *prev_var_name, int neededflags)
50 {
51         cvar_t *var;
52
53         if (*prev_var_name)
54         {
55                 var = Cvar_FindVar (prev_var_name);
56                 if (!var)
57                         return NULL;
58                 var = var->next;
59         }
60         else
61                 var = cvar_vars;
62
63         // search for the next cvar matching the needed flags
64         while (var)
65         {
66                 if ((var->flags & neededflags) || !neededflags)
67                         break;
68                 var = var->next;
69         }
70         return var;
71 }
72
73 cvar_t *Cvar_FindVarLink (const char *var_name, cvar_t **parent, cvar_t ***link, cvar_t **prev_alpha)
74 {
75         int hashindex;
76         cvar_t *var;
77
78         // use hash lookup to minimize search time
79         hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name));
80         if(parent) *parent = NULL;
81         if(prev_alpha) *prev_alpha = NULL;
82         if(link) *link = &cvar_hashtable[hashindex];
83         for (var = cvar_hashtable[hashindex];var;var = var->nextonhashchain)
84         {
85                 if (!strcmp (var_name, var->name))
86                 {
87                         if(!prev_alpha || var == cvar_vars)
88                                 return var;
89
90                         *prev_alpha = cvar_vars;
91                         // if prev_alpha happens to become NULL then there has been some inconsistency elsewhere
92                         // already - should I still insert '*prev_alpha &&' in the loop?
93                         while((*prev_alpha)->next != var)
94                                 *prev_alpha = (*prev_alpha)->next;
95                         return var;
96                 }
97                 if(parent) *parent = var;
98         }
99
100         return NULL;
101 }
102
103 /*
104 ============
105 Cvar_VariableValue
106 ============
107 */
108 float Cvar_VariableValue (const char *var_name)
109 {
110         cvar_t *var;
111
112         var = Cvar_FindVar (var_name);
113         if (!var)
114                 return 0;
115         return atof (var->string);
116 }
117
118
119 /*
120 ============
121 Cvar_VariableString
122 ============
123 */
124 const char *Cvar_VariableString (const char *var_name)
125 {
126         cvar_t *var;
127
128         var = Cvar_FindVar (var_name);
129         if (!var)
130                 return cvar_null_string;
131         return var->string;
132 }
133
134 /*
135 ============
136 Cvar_VariableDefString
137 ============
138 */
139 const char *Cvar_VariableDefString (const char *var_name)
140 {
141         cvar_t *var;
142
143         var = Cvar_FindVar (var_name);
144         if (!var)
145                 return cvar_null_string;
146         return var->defstring;
147 }
148
149 /*
150 ============
151 Cvar_VariableDescription
152 ============
153 */
154 const char *Cvar_VariableDescription (const char *var_name)
155 {
156         cvar_t *var;
157
158         var = Cvar_FindVar (var_name);
159         if (!var)
160                 return cvar_null_string;
161         return var->description;
162 }
163
164
165 /*
166 ============
167 Cvar_CompleteVariable
168 ============
169 */
170 const char *Cvar_CompleteVariable (const char *partial)
171 {
172         cvar_t          *cvar;
173         size_t          len;
174
175         len = strlen(partial);
176
177         if (!len)
178                 return NULL;
179
180 // check functions
181         for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
182                 if (!strncasecmp (partial,cvar->name, len))
183                         return cvar->name;
184
185         return NULL;
186 }
187
188
189 /*
190         CVar_CompleteCountPossible
191
192         New function for tab-completion system
193         Added by EvilTypeGuy
194         Thanks to Fett erich@heintz.com
195
196 */
197 int Cvar_CompleteCountPossible (const char *partial)
198 {
199         cvar_t  *cvar;
200         size_t  len;
201         int             h;
202
203         h = 0;
204         len = strlen(partial);
205
206         if (!len)
207                 return  0;
208
209         // Loop through the cvars and count all possible matches
210         for (cvar = cvar_vars; cvar; cvar = cvar->next)
211                 if (!strncasecmp(partial, cvar->name, len))
212                         h++;
213
214         return h;
215 }
216
217 /*
218         CVar_CompleteBuildList
219
220         New function for tab-completion system
221         Added by EvilTypeGuy
222         Thanks to Fett erich@heintz.com
223         Thanks to taniwha
224
225 */
226 const char **Cvar_CompleteBuildList (const char *partial)
227 {
228         const cvar_t *cvar;
229         size_t len = 0;
230         size_t bpos = 0;
231         size_t sizeofbuf = (Cvar_CompleteCountPossible (partial) + 1) * sizeof (const char *);
232         const char **buf;
233
234         len = strlen(partial);
235         buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
236         // Loop through the alias list and print all matches
237         for (cvar = cvar_vars; cvar; cvar = cvar->next)
238                 if (!strncasecmp(partial, cvar->name, len))
239                         buf[bpos++] = cvar->name;
240
241         buf[bpos] = NULL;
242         return buf;
243 }
244
245 // written by LordHavoc
246 void Cvar_CompleteCvarPrint (const char *partial)
247 {
248         cvar_t *cvar;
249         size_t len = strlen(partial);
250         // Loop through the command list and print all matches
251         for (cvar = cvar_vars; cvar; cvar = cvar->next)
252                 if (!strncasecmp(partial, cvar->name, len))
253                         Con_Printf ("^3%s^7 is \"%s\" [\"%s\"] %s\n", cvar->name, cvar->string, cvar->defstring, cvar->description);
254 }
255
256
257 /*
258 ============
259 Cvar_Set
260 ============
261 */
262 void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
263 {
264         qboolean changed;
265         size_t valuelen;
266         prvm_prog_t *tmpprog;
267         int i;
268
269         changed = strcmp(var->string, value) != 0;
270         // LordHavoc: don't reallocate when there is no change
271         if (!changed)
272                 return;
273
274         // LordHavoc: don't reallocate when the buffer is the same size
275         valuelen = strlen(value);
276         if (!var->string || strlen(var->string) != valuelen)
277         {
278                 Z_Free (var->string);   // free the old value string
279
280                 var->string = (char *)Z_Malloc (valuelen + 1);
281         }
282         memcpy (var->string, value, valuelen + 1);
283         var->value = atof (var->string);
284         var->integer = (int) var->value;
285         if ((var->flags & CVAR_NOTIFY) && changed && sv.active)
286                 SV_BroadcastPrintf("\"%s\" changed to \"%s\"\n", var->name, var->string);
287 #if 0
288         // TODO: add infostring support to the server?
289         if ((var->flags & CVAR_SERVERINFO) && changed && sv.active)
290         {
291                 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
292                 if (sv.active)
293                 {
294                         MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
295                         MSG_WriteString (&sv.reliable_datagram, var->name);
296                         MSG_WriteString (&sv.reliable_datagram, var->string);
297                 }
298         }
299 #endif
300         if ((var->flags & CVAR_USERINFO) && cls.state != ca_dedicated)
301                 CL_SetInfo(var->name, var->string, true, false, false, false);
302         else if ((var->flags & CVAR_NQUSERINFOHACK) && cls.state != ca_dedicated)
303         {
304                 // update the cls.userinfo to have proper values for the
305                 // silly nq config variables.
306                 //
307                 // this is done when these variables are changed rather than at
308                 // connect time because if the user or code checks the userinfo and it
309                 // holds weird values it may cause confusion...
310                 if (!strcmp(var->name, "_cl_color"))
311                 {
312                         int top = (var->integer >> 4) & 15, bottom = var->integer & 15;
313                         CL_SetInfo("topcolor", va("%i", top), true, false, false, false);
314                         CL_SetInfo("bottomcolor", va("%i", bottom), true, false, false, false);
315                         if (cls.protocol != PROTOCOL_QUAKEWORLD && cls.netcon)
316                         {
317                                 MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
318                                 MSG_WriteString(&cls.netcon->message, va("color %i %i", top, bottom));
319                         }
320                 }
321                 else if (!strcmp(var->name, "_cl_rate"))
322                         CL_SetInfo("rate", va("%i", var->integer), true, false, false, false);
323                 else if (!strcmp(var->name, "_cl_playerskin"))
324                         CL_SetInfo("playerskin", var->string, true, false, false, false);
325                 else if (!strcmp(var->name, "_cl_playermodel"))
326                         CL_SetInfo("playermodel", var->string, true, false, false, false);
327                 else if (!strcmp(var->name, "_cl_name"))
328                         CL_SetInfo("name", var->string, true, false, false, false);
329                 else if (!strcmp(var->name, "rcon_secure"))
330                 {
331                         // whenever rcon_secure is changed to 0, clear rcon_password for
332                         // security reasons (prevents a send-rcon-password-as-plaintext
333                         // attack based on NQ protocol session takeover and svc_stufftext)
334                         if(var->integer <= 0)
335                                 Cvar_Set("rcon_password", "");
336                 }
337                 else if (!strcmp(var->name, "net_slist_favorites"))
338                         NetConn_UpdateFavorites();
339         }
340
341         tmpprog = prog;
342         for(i = 0; i < PRVM_MAXPROGS; ++i)
343         {
344                 if(PRVM_ProgLoaded(i))
345                 {
346                         PRVM_SetProg(i);
347                         if(var->globaldefindex_progid[i] == prog->id)
348                         {
349                                 // MUST BE SYNCED WITH prvm_edict.c PRVM_LoadProgs
350                                 int j;
351                                 const char *s;
352                                 prvm_eval_t *val = (prvm_eval_t *)(prog->globals.generic + prog->globaldefs[var->globaldefindex[i]].ofs);
353                                 switch(prog->globaldefs[var->globaldefindex[i]].type & ~DEF_SAVEGLOBAL)
354                                 {
355                                         case ev_float:
356                                                 val->_float = var->value;
357                                                 break;
358                                         case ev_vector:
359                                                 s = var->string;
360                                                 VectorClear(val->vector);
361                                                 for (j = 0;j < 3;j++)
362                                                 {
363                                                         while (*s && ISWHITESPACE(*s))
364                                                                 s++;
365                                                         if (!*s)
366                                                                 break;
367                                                         val->vector[j] = atof(s);
368                                                         while (!ISWHITESPACE(*s))
369                                                                 s++;
370                                                         if (!*s)
371                                                                 break;
372                                                 }
373                                                 break;
374                                         case ev_string:
375                                                 PRVM_ChangeEngineString(var->globaldefindex_stringno[i], var->string);
376                                                 val->string = var->globaldefindex_stringno[i];
377                                                 break;
378                                 }
379                         }
380                 }
381         }
382         prog = tmpprog;
383 }
384
385 void Cvar_SetQuick (cvar_t *var, const char *value)
386 {
387         if (var == NULL)
388         {
389                 Con_Print("Cvar_SetQuick: var == NULL\n");
390                 return;
391         }
392
393         if (developer_extra.integer)
394                 Con_DPrintf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
395
396         Cvar_SetQuick_Internal(var, value);
397 }
398
399 void Cvar_Set (const char *var_name, const char *value)
400 {
401         cvar_t *var;
402         var = Cvar_FindVar (var_name);
403         if (var == NULL)
404         {
405                 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
406                 return;
407         }
408         Cvar_SetQuick(var, value);
409 }
410
411 /*
412 ============
413 Cvar_SetValue
414 ============
415 */
416 void Cvar_SetValueQuick(cvar_t *var, float value)
417 {
418         char val[MAX_INPUTLINE];
419
420         if ((float)((int)value) == value)
421                 dpsnprintf(val, sizeof(val), "%i", (int)value);
422         else
423                 dpsnprintf(val, sizeof(val), "%f", value);
424         Cvar_SetQuick(var, val);
425 }
426
427 void Cvar_SetValue(const char *var_name, float value)
428 {
429         char val[MAX_INPUTLINE];
430
431         if ((float)((int)value) == value)
432                 dpsnprintf(val, sizeof(val), "%i", (int)value);
433         else
434                 dpsnprintf(val, sizeof(val), "%f", value);
435         Cvar_Set(var_name, val);
436 }
437
438 /*
439 ============
440 Cvar_RegisterVariable
441
442 Adds a freestanding variable to the variable list.
443 ============
444 */
445 void Cvar_RegisterVariable (cvar_t *variable)
446 {
447         int hashindex;
448         cvar_t *current, *next, *cvar;
449         char *oldstr;
450         size_t alloclen;
451
452         if (developer_extra.integer)
453                 Con_DPrintf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
454
455 // first check to see if it has already been defined
456         cvar = Cvar_FindVar (variable->name);
457         if (cvar)
458         {
459                 if (cvar->flags & CVAR_ALLOCATED)
460                 {
461                         if (developer_extra.integer)
462                                 Con_DPrintf("...  replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
463                         // fixed variables replace allocated ones
464                         // (because the engine directly accesses fixed variables)
465                         // NOTE: this isn't actually used currently
466                         // (all cvars are registered before config parsing)
467                         variable->flags |= (cvar->flags & ~CVAR_ALLOCATED);
468                         // cvar->string is now owned by variable instead
469                         variable->string = cvar->string;
470                         variable->defstring = cvar->defstring;
471                         variable->value = atof (variable->string);
472                         variable->integer = (int) variable->value;
473                         // replace cvar with this one...
474                         variable->next = cvar->next;
475                         if (cvar_vars == cvar)
476                         {
477                                 // head of the list is easy to change
478                                 cvar_vars = variable;
479                         }
480                         else
481                         {
482                                 // otherwise find it somewhere in the list
483                                 for (current = cvar_vars;current->next != cvar;current = current->next)
484                                         ;
485                                 current->next = variable;
486                         }
487
488                         // get rid of old allocated cvar
489                         // (but not cvar->string and cvar->defstring, because we kept those)
490                         Z_Free(cvar->name);
491                         Z_Free(cvar);
492                 }
493                 else
494                         Con_DPrintf("Can't register variable %s, already defined\n", variable->name);
495                 return;
496         }
497
498 // check for overlap with a command
499         if (Cmd_Exists (variable->name))
500         {
501                 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
502                 return;
503         }
504
505 // copy the value off, because future sets will Z_Free it
506         oldstr = variable->string;
507         alloclen = strlen(variable->string) + 1;
508         variable->string = (char *)Z_Malloc (alloclen);
509         memcpy (variable->string, oldstr, alloclen);
510         variable->defstring = (char *)Z_Malloc (alloclen);
511         memcpy (variable->defstring, oldstr, alloclen);
512         variable->value = atof (variable->string);
513         variable->integer = (int) variable->value;
514
515 // link the variable in
516 // alphanumerical order
517         for( current = NULL, next = cvar_vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
518                 ;
519         if( current ) {
520                 current->next = variable;
521         } else {
522                 cvar_vars = variable;
523         }
524         variable->next = next;
525
526         // link to head of list in this hash table index
527         hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
528         variable->nextonhashchain = cvar_hashtable[hashindex];
529         cvar_hashtable[hashindex] = variable;
530 }
531
532 /*
533 ============
534 Cvar_Get
535
536 Adds a newly allocated variable to the variable list or sets its value.
537 ============
538 */
539 cvar_t *Cvar_Get (const char *name, const char *value, int flags, const char *newdescription)
540 {
541         int hashindex;
542         cvar_t *current, *next, *cvar;
543         size_t alloclen;
544
545         if (developer_extra.integer)
546                 Con_DPrintf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
547
548 // first check to see if it has already been defined
549         cvar = Cvar_FindVar (name);
550         if (cvar)
551         {
552                 cvar->flags |= flags;
553                 Cvar_SetQuick_Internal (cvar, value);
554                 if(newdescription && (cvar->flags & CVAR_ALLOCATED))
555                 {
556                         if(cvar->description != cvar_dummy_description)
557                                 Z_Free(cvar->description);
558
559                         if(*newdescription)
560                         {
561                                 alloclen = strlen(newdescription) + 1;
562                                 cvar->description = (char *)Z_Malloc(alloclen);
563                                 memcpy(cvar->description, newdescription, alloclen);
564                         }
565                         else
566                                 cvar->description = cvar_dummy_description;
567                 }
568                 return cvar;
569         }
570
571 // check for pure evil
572         if (!*name)
573         {
574                 Con_Printf("Cvar_Get: invalid variable name\n");
575                 return NULL;
576         }
577
578 // check for overlap with a command
579         if (Cmd_Exists (name))
580         {
581                 Con_Printf("Cvar_Get: %s is a command\n", name);
582                 return NULL;
583         }
584
585 // allocate a new cvar, cvar name, and cvar string
586 // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
587 // FIXME: these never get Z_Free'd
588         cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
589         cvar->flags = flags | CVAR_ALLOCATED;
590         alloclen = strlen(name) + 1;
591         cvar->name = (char *)Z_Malloc(alloclen);
592         memcpy(cvar->name, name, alloclen);
593         alloclen = strlen(value) + 1;
594         cvar->string = (char *)Z_Malloc(alloclen);
595         memcpy(cvar->string, value, alloclen);
596         cvar->defstring = (char *)Z_Malloc(alloclen);
597         memcpy(cvar->defstring, value, alloclen);
598         cvar->value = atof (cvar->string);
599         cvar->integer = (int) cvar->value;
600
601         if(newdescription && *newdescription)
602         {
603                 alloclen = strlen(newdescription) + 1;
604                 cvar->description = (char *)Z_Malloc(alloclen);
605                 memcpy(cvar->description, newdescription, alloclen);
606         }
607         else
608                 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
609
610 // link the variable in
611 // alphanumerical order
612         for( current = NULL, next = cvar_vars ; next && strcmp( next->name, cvar->name ) < 0 ; current = next, next = next->next )
613                 ;
614         if( current )
615                 current->next = cvar;
616         else
617                 cvar_vars = cvar;
618         cvar->next = next;
619
620         // link to head of list in this hash table index
621         hashindex = CRC_Block((const unsigned char *)cvar->name, strlen(cvar->name)) % CVAR_HASHSIZE;
622         cvar->nextonhashchain = cvar_hashtable[hashindex];
623         cvar_hashtable[hashindex] = cvar;
624
625         return cvar;
626 }
627
628
629 /*
630 ============
631 Cvar_Command
632
633 Handles variable inspection and changing from the console
634 ============
635 */
636 qboolean        Cvar_Command (void)
637 {
638         cvar_t                  *v;
639
640 // check variables
641         v = Cvar_FindVar (Cmd_Argv(0));
642         if (!v)
643                 return false;
644
645 // perform a variable print or set
646         if (Cmd_Argc() == 1)
647         {
648                 Con_Printf("\"%s\" is \"%s\" [\"%s\"]\n", v->name, ((v->flags & CVAR_PRIVATE) ? "********"/*hunter2*/ : v->string), v->defstring);
649                 return true;
650         }
651
652         if (developer_extra.integer)
653                 Con_DPrint("Cvar_Command: ");
654
655         if (v->flags & CVAR_READONLY)
656         {
657                 Con_Printf("%s is read-only\n", v->name);
658                 return true;
659         }
660         Cvar_Set (v->name, Cmd_Argv(1));
661         if (developer_extra.integer)
662                 Con_DPrint("\n");
663         return true;
664 }
665
666
667 void Cvar_UnlockDefaults (void)
668 {
669         cvar_t *var;
670         // unlock the default values of all cvars
671         for (var = cvar_vars ; var ; var = var->next)
672                 var->flags &= ~CVAR_DEFAULTSET;
673 }
674
675
676 void Cvar_LockDefaults_f (void)
677 {
678         cvar_t *var;
679         // lock in the default values of all cvars
680         for (var = cvar_vars ; var ; var = var->next)
681         {
682                 if (!(var->flags & CVAR_DEFAULTSET))
683                 {
684                         size_t alloclen;
685
686                         //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
687                         var->flags |= CVAR_DEFAULTSET;
688                         Z_Free(var->defstring);
689                         alloclen = strlen(var->string) + 1;
690                         var->defstring = (char *)Z_Malloc(alloclen);
691                         memcpy(var->defstring, var->string, alloclen);
692                 }
693         }
694 }
695
696
697 void Cvar_ResetToDefaults_All_f (void)
698 {
699         cvar_t *var;
700         // restore the default values of all cvars
701         for (var = cvar_vars ; var ; var = var->next)
702                 if((var->flags & CVAR_NORESETTODEFAULTS) == 0)
703                         Cvar_SetQuick(var, var->defstring);
704 }
705
706
707 void Cvar_ResetToDefaults_NoSaveOnly_f (void)
708 {
709         cvar_t *var;
710         // restore the default values of all cvars
711         for (var = cvar_vars ; var ; var = var->next)
712                 if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == 0)
713                         Cvar_SetQuick(var, var->defstring);
714 }
715
716
717 void Cvar_ResetToDefaults_SaveOnly_f (void)
718 {
719         cvar_t *var;
720         // restore the default values of all cvars
721         for (var = cvar_vars ; var ; var = var->next)
722                 if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == CVAR_SAVE)
723                         Cvar_SetQuick(var, var->defstring);
724 }
725
726
727 /*
728 ============
729 Cvar_WriteVariables
730
731 Writes lines containing "set variable value" for all variables
732 with the archive flag set to true.
733 ============
734 */
735 void Cvar_WriteVariables (qfile_t *f)
736 {
737         cvar_t  *var;
738         char buf1[MAX_INPUTLINE], buf2[MAX_INPUTLINE];
739
740         // don't save cvars that match their default value
741         for (var = cvar_vars ; var ; var = var->next)
742                 if ((var->flags & CVAR_SAVE) && (strcmp(var->string, var->defstring) || (var->flags & CVAR_ALLOCATED)))
743                 {
744                         Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$");
745                         Cmd_QuoteString(buf2, sizeof(buf2), var->string, "\"\\$");
746                         FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CVAR_ALLOCATED ? "seta " : "", buf1, buf2);
747                 }
748 }
749
750
751 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
752 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
753 /*
754 =========
755 Cvar_List
756 =========
757 */
758 void Cvar_List_f (void)
759 {
760         cvar_t *cvar;
761         const char *partial;
762         size_t len;
763         int count;
764         qboolean ispattern;
765
766         if (Cmd_Argc() > 1)
767         {
768                 partial = Cmd_Argv (1);
769                 len = strlen(partial);
770         }
771         else
772         {
773                 partial = NULL;
774                 len = 0;
775         }
776
777         ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
778
779         count = 0;
780         for (cvar = cvar_vars; cvar; cvar = cvar->next)
781         {
782                 if (len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp (partial,cvar->name,len)))
783                         continue;
784
785                 Con_Printf("%s is \"%s\" [\"%s\"] %s\n", cvar->name, ((cvar->flags & CVAR_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring, cvar->description);
786                 count++;
787         }
788
789         if (len)
790         {
791                 if(ispattern)
792                         Con_Printf("%i cvar%s matching \"%s\"\n", count, (count > 1) ? "s" : "", partial);
793                 else
794                         Con_Printf("%i cvar%s beginning with \"%s\"\n", count, (count > 1) ? "s" : "", partial);
795         }
796         else
797                 Con_Printf("%i cvar(s)\n", count);
798 }
799 // 2000-01-09 CvarList command by Maddes
800
801 void Cvar_Set_f (void)
802 {
803         cvar_t *cvar;
804
805         // make sure it's the right number of parameters
806         if (Cmd_Argc() < 3)
807         {
808                 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
809                 return;
810         }
811
812         // check if it's read-only
813         cvar = Cvar_FindVar(Cmd_Argv(1));
814         if (cvar && cvar->flags & CVAR_READONLY)
815         {
816                 Con_Printf("Set: %s is read-only\n", cvar->name);
817                 return;
818         }
819
820         if (developer_extra.integer)
821                 Con_DPrint("Set: ");
822
823         // all looks ok, create/modify the cvar
824         Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), 0, Cmd_Argc() > 3 ? Cmd_Argv(3) : NULL);
825 }
826
827 void Cvar_SetA_f (void)
828 {
829         cvar_t *cvar;
830
831         // make sure it's the right number of parameters
832         if (Cmd_Argc() < 3)
833         {
834                 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value> [<description>]\n");
835                 return;
836         }
837
838         // check if it's read-only
839         cvar = Cvar_FindVar(Cmd_Argv(1));
840         if (cvar && cvar->flags & CVAR_READONLY)
841         {
842                 Con_Printf("SetA: %s is read-only\n", cvar->name);
843                 return;
844         }
845
846         if (developer_extra.integer)
847                 Con_DPrint("SetA: ");
848
849         // all looks ok, create/modify the cvar
850         Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), CVAR_SAVE, Cmd_Argc() > 3 ? Cmd_Argv(3) : NULL);
851 }
852
853 void Cvar_Del_f (void)
854 {
855         int i;
856         cvar_t *cvar, *parent, **link, *prev;
857
858         if(Cmd_Argc() < 2)
859         {
860                 Con_Printf("Del: wrong number of parameters, useage: unset <variablename1> [<variablename2> ...]\n");
861                 return;
862         }
863         for(i = 1; i < Cmd_Argc(); ++i)
864         {
865                 cvar = Cvar_FindVarLink(Cmd_Argv(i), &parent, &link, &prev);
866                 if(!cvar)
867                 {
868                         Con_Printf("Del: %s is not defined\n", Cmd_Argv(i));
869                         continue;
870                 }
871                 if(cvar->flags & CVAR_READONLY)
872                 {
873                         Con_Printf("Del: %s is read-only\n", cvar->name);
874                         continue;
875                 }
876                 if(!(cvar->flags & CVAR_ALLOCATED))
877                 {
878                         Con_Printf("Del: %s is static and cannot be deleted\n", cvar->name);
879                         continue;
880                 }
881                 if(cvar == cvar_vars)
882                 {
883                         cvar_vars = cvar->next;
884                 }
885                 else
886                 {
887                         // in this case, prev must be set, otherwise there has been some inconsistensy
888                         // elsewhere already... should I still check for prev != NULL?
889                         prev->next = cvar->next;
890                 }
891
892                 if(parent)
893                         parent->nextonhashchain = cvar->nextonhashchain;
894                 else if(link)
895                         *link = cvar->nextonhashchain;
896
897                 if(cvar->description != cvar_dummy_description)
898                         Z_Free(cvar->description);
899
900                 Z_Free(cvar->name);
901                 Z_Free(cvar->string);
902                 Z_Free(cvar->defstring);
903                 Z_Free(cvar);
904         }
905 }
906
907 #ifdef FILLALLCVARSWITHRUBBISH
908 void Cvar_FillAll_f()
909 {
910         char *buf, *p, *q;
911         int n, i;
912         cvar_t *var;
913         qboolean verify;
914         if(Cmd_Argc() != 2)
915         {
916                 Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(0));
917                 Con_Printf("Usage: %s -length to verify that the rubbish is still there\n", Cmd_Argv(0));
918                 return;
919         }
920         n = atoi(Cmd_Argv(1));
921         verify = (n < 0);
922         if(verify)
923                 n = -n;
924         buf = Z_Malloc(n + 1);
925         buf[n] = 0;
926         for(var = cvar_vars; var; var = var->next)
927         {
928                 for(i = 0, p = buf, q = var->name; i < n; ++i)
929                 {
930                         *p++ = *q++;
931                         if(!*q)
932                                 q = var->name;
933                 }
934                 if(verify && strcmp(var->string, buf))
935                 {
936                         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);
937                 }
938                 Cvar_SetQuick(var, buf);
939         }
940         Z_Free(buf);
941 }
942 #endif /* FILLALLCVARSWITHRUBBISH */