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