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->value, 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;
425 // link the variable in
426 cvar->next = cvar_vars;
436 Handles variable inspection and changing from the console
439 qboolean Cvar_Command (void)
444 v = Cvar_FindVar (Cmd_Argv(0));
448 // perform a variable print or set
451 Con_Printf("\"%s\" is \"%s\" [\"%s\"]\n", v->name, v->string, v->defstring);
455 Con_DPrint("Cvar_Command: ");
457 if (v->flags & CVAR_READONLY)
459 Con_Printf("%s is read-only\n", v->name);
462 Cvar_Set (v->name, Cmd_Argv(1));
471 Writes lines containing "set variable value" for all variables
472 with the archive flag set to true.
475 void Cvar_WriteVariables (qfile_t *f)
479 for (var = cvar_vars ; var ; var = var->next)
480 if (var->flags & CVAR_SAVE)
481 FS_Printf(f, "%s%s \"%s\"\n", var->flags & CVAR_ALLOCATED ? "seta " : "", var->name, var->string);
485 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
486 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
492 void Cvar_List_f (void)
501 partial = Cmd_Argv (1);
502 len = strlen(partial);
511 for (cvar = cvar_vars; cvar; cvar = cvar->next)
513 if (partial && strncasecmp (partial,cvar->name,len))
516 Con_Printf("%s is \"%s\" [\"%s\"]\n", cvar->name, cvar->string, cvar->defstring);
521 Con_Printf("%i cvar(s) beginning with \"%s\"\n", count, partial);
523 Con_Printf("%i cvar(s)\n", count);
525 // 2000-01-09 CvarList command by Maddes
527 void Cvar_Set_f (void)
531 // make sure it's the right number of parameters
534 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value>\n");
538 // check if it's read-only
539 cvar = Cvar_FindVar(Cmd_Argv(1));
540 if (cvar && cvar->flags & CVAR_READONLY)
542 Con_Printf("Set: %s is read-only\n", cvar->name);
548 // all looks ok, create/modify the cvar
549 Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), 0);
552 void Cvar_SetA_f (void)
556 // make sure it's the right number of parameters
559 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value>\n");
563 // check if it's read-only
564 cvar = Cvar_FindVar(Cmd_Argv(1));
565 if (cvar && cvar->flags & CVAR_READONLY)
567 Con_Printf("SetA: %s is read-only\n", cvar->name);
571 Con_DPrint("SetA: ");
573 // all looks ok, create/modify the cvar
574 Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), CVAR_SAVE);