2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // cvar.c -- dynamic variable tracking
24 cvar_t *cvar_vars = NULL;
25 char *cvar_null_string = "";
32 cvar_t *Cvar_FindVar (const char *var_name)
36 for (var = cvar_vars;var;var = var->next)
37 if (!strcasecmp (var_name, var->name))
43 cvar_t *Cvar_FindVarAfter (const char *prev_var_name, int neededflags)
49 var = Cvar_FindVar (prev_var_name);
57 // search for the next cvar matching the needed flags
60 if ((var->flags & neededflags) || !neededflags)
72 float Cvar_VariableValue (const char *var_name)
76 var = Cvar_FindVar (var_name);
79 return atof (var->string);
88 const char *Cvar_VariableString (const char *var_name)
92 var = Cvar_FindVar (var_name);
94 return cvar_null_string;
100 Cvar_VariableDefString
103 const char *Cvar_VariableDefString (const char *var_name)
107 var = Cvar_FindVar (var_name);
109 return cvar_null_string;
110 return var->defstring;
116 Cvar_CompleteVariable
119 const char *Cvar_CompleteVariable (const char *partial)
124 len = strlen(partial);
130 for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
131 if (!strncasecmp (partial,cvar->name, len))
139 CVar_CompleteCountPossible
141 New function for tab-completion system
143 Thanks to Fett erich@heintz.com
146 int Cvar_CompleteCountPossible (const char *partial)
153 len = strlen(partial);
158 // Loop through the cvars and count all possible matches
159 for (cvar = cvar_vars; cvar; cvar = cvar->next)
160 if (!strncasecmp(partial, cvar->name, len))
167 CVar_CompleteBuildList
169 New function for tab-completion system
171 Thanks to Fett erich@heintz.com
175 const char **Cvar_CompleteBuildList (const char *partial)
180 size_t sizeofbuf = (Cvar_CompleteCountPossible (partial) + 1) * sizeof (const char *);
183 len = strlen(partial);
184 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
185 // Loop through the alias list and print all matches
186 for (cvar = cvar_vars; cvar; cvar = cvar->next)
187 if (!strncasecmp(partial, cvar->name, len))
188 buf[bpos++] = cvar->name;
194 // written by LordHavoc
195 void Cvar_CompleteCvarPrint (const char *partial)
198 size_t len = strlen(partial);
199 // Loop through the command list and print all matches
200 for (cvar = cvar_vars; cvar; cvar = cvar->next)
201 if (!strncasecmp(partial, cvar->name, len))
202 Con_Printf("%s : \"%s\" (\"%s\") : %s\n", cvar->name, cvar->string, cvar->defstring, cvar->description);
211 void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
215 changed = strcmp(var->string, value);
216 // LordHavoc: don't reallocate when there is no change
220 // LordHavoc: don't reallocate when the buffer is the same size
221 if (!var->string || strlen(var->string) != strlen(value))
223 Z_Free (var->string); // free the old value string
225 var->string = (char *)Z_Malloc (strlen(value)+1);
227 strcpy (var->string, value);
228 var->value = atof (var->string);
229 var->integer = (int) var->value;
230 if ((var->flags & CVAR_NOTIFY) && changed && sv.active)
231 SV_BroadcastPrintf("\"%s\" changed to \"%s\"\n", var->name, var->string);
234 void Cvar_SetQuick (cvar_t *var, const char *value)
238 Con_Print("Cvar_SetQuick: var == NULL\n");
242 if (developer.integer)
243 Con_Printf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
245 Cvar_SetQuick_Internal(var, value);
248 void Cvar_Set (const char *var_name, const char *value)
251 var = Cvar_FindVar (var_name);
254 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
257 Cvar_SetQuick(var, value);
265 void Cvar_SetValueQuick(cvar_t *var, float value)
267 char val[MAX_INPUTLINE];
269 if ((float)((int)value) == value)
270 sprintf(val, "%i", (int)value);
272 sprintf(val, "%f", value);
273 Cvar_SetQuick(var, val);
276 void Cvar_SetValue(const char *var_name, float value)
278 char val[MAX_INPUTLINE];
280 if ((float)((int)value) == value)
281 sprintf(val, "%i", (int)value);
283 sprintf(val, "%f", value);
284 Cvar_Set(var_name, val);
289 Cvar_RegisterVariable
291 Adds a freestanding variable to the variable list.
294 void Cvar_RegisterVariable (cvar_t *variable)
296 cvar_t *current, *next, *cvar;
299 if (developer.integer)
300 Con_Printf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
302 // first check to see if it has already been defined
303 cvar = Cvar_FindVar (variable->name);
306 if (cvar->flags & CVAR_ALLOCATED)
308 if (developer.integer)
309 Con_Printf("... replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
310 // fixed variables replace allocated ones
311 // (because the engine directly accesses fixed variables)
312 // NOTE: this isn't actually used currently
313 // (all cvars are registered before config parsing)
314 variable->flags |= (cvar->flags & ~CVAR_ALLOCATED);
315 // cvar->string is now owned by variable instead
316 variable->string = cvar->string;
317 variable->defstring = cvar->defstring;
318 variable->value = atof (variable->string);
319 variable->integer = (int) variable->value;
320 // replace cvar with this one...
321 variable->next = cvar->next;
322 if (cvar_vars == cvar)
324 // head of the list is easy to change
325 cvar_vars = variable;
329 // otherwise find it somewhere in the list
330 for (current = cvar_vars;current->next != cvar;current = current->next)
332 current->next = variable;
335 // get rid of old allocated cvar
336 // (but not cvar->string and cvar->defstring, because we kept those)
341 Con_Printf("Can't register variable %s, already defined\n", variable->name);
345 // check for overlap with a command
346 if (Cmd_Exists (variable->name))
348 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
352 // copy the value off, because future sets will Z_Free it
353 oldstr = variable->string;
354 variable->string = (char *)Z_Malloc (strlen(variable->string)+1);
355 strcpy (variable->string, oldstr);
356 variable->defstring = (char *)Z_Malloc (strlen(variable->string)+1);
357 strcpy (variable->defstring, oldstr);
358 variable->value = atof (variable->string);
359 variable->integer = (int) variable->value;
361 // link the variable in
362 // alphanumerical order
363 for( current = NULL, next = cvar_vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
366 current->next = variable;
368 cvar_vars = variable;
370 variable->next = next;
377 Adds a newly allocated variable to the variable list or sets its value.
380 cvar_t *Cvar_Get (const char *name, const char *value, int flags)
384 if (developer.integer)
385 Con_Printf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
387 // first check to see if it has already been defined
388 cvar = Cvar_FindVar (name);
391 cvar->flags |= flags;
392 Cvar_SetQuick_Internal (cvar, value);
393 // also set the default value (but only once)
394 if (~cvar->flags & CVAR_DEFAULTSET)
396 cvar->flags |= CVAR_DEFAULTSET;
398 Z_Free(cvar->defstring);
399 cvar->defstring = (char *)Z_Malloc(strlen(value) + 1);
400 strcpy(cvar->defstring, value);
405 // check for overlap with a command
406 if (Cmd_Exists (name))
408 Con_Printf("Cvar_Get: %s is a command\n", name);
412 // allocate a new cvar, cvar name, and cvar string
413 // FIXME: these never get Z_Free'd
414 cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
415 cvar->flags = flags | CVAR_ALLOCATED | CVAR_DEFAULTSET;
416 cvar->name = (char *)Z_Malloc(strlen(name)+1);
417 strcpy(cvar->name, name);
418 cvar->string = (char *)Z_Malloc(strlen(value)+1);
419 strcpy(cvar->string, value);
420 cvar->defstring = (char *)Z_Malloc(strlen(value)+1);
421 strcpy(cvar->defstring, value);
422 cvar->value = atof (cvar->string);
423 cvar->integer = (int) cvar->value;
424 cvar->description = "custom cvar";
426 // link the variable in
427 cvar->next = cvar_vars;
437 Handles variable inspection and changing from the console
440 qboolean Cvar_Command (void)
445 v = Cvar_FindVar (Cmd_Argv(0));
449 // perform a variable print or set
452 Con_Printf("\"%s\" is \"%s\" [\"%s\"]\n", v->name, v->string, v->defstring);
456 Con_DPrint("Cvar_Command: ");
458 if (v->flags & CVAR_READONLY)
460 Con_Printf("%s is read-only\n", v->name);
463 Cvar_Set (v->name, Cmd_Argv(1));
472 Writes lines containing "set variable value" for all variables
473 with the archive flag set to true.
476 void Cvar_WriteVariables (qfile_t *f)
480 for (var = cvar_vars ; var ; var = var->next)
481 if (var->flags & CVAR_SAVE)
482 FS_Printf(f, "%s%s \"%s\"\n", var->flags & CVAR_ALLOCATED ? "seta " : "", var->name, var->string);
486 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
487 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
493 void Cvar_List_f (void)
502 partial = Cmd_Argv (1);
503 len = strlen(partial);
512 for (cvar = cvar_vars; cvar; cvar = cvar->next)
514 if (partial && strncasecmp (partial,cvar->name,len))
517 Con_Printf("%s is \"%s\" [\"%s\"] %s\n", cvar->name, cvar->string, cvar->defstring, cvar->description);
522 Con_Printf("%i cvar(s) beginning with \"%s\"\n", count, partial);
524 Con_Printf("%i cvar(s)\n", count);
526 // 2000-01-09 CvarList command by Maddes
528 void Cvar_Set_f (void)
532 // make sure it's the right number of parameters
535 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value>\n");
539 // check if it's read-only
540 cvar = Cvar_FindVar(Cmd_Argv(1));
541 if (cvar && cvar->flags & CVAR_READONLY)
543 Con_Printf("Set: %s is read-only\n", cvar->name);
549 // all looks ok, create/modify the cvar
550 Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), 0);
553 void Cvar_SetA_f (void)
557 // make sure it's the right number of parameters
560 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value>\n");
564 // check if it's read-only
565 cvar = Cvar_FindVar(Cmd_Argv(1));
566 if (cvar && cvar->flags & CVAR_READONLY)
568 Con_Printf("SetA: %s is read-only\n", cvar->name);
572 Con_DPrint("SetA: ");
574 // all looks ok, create/modify the cvar
575 Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), CVAR_SAVE);