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