]> git.xonotic.org Git - xonotic/darkplaces.git/blob - cvar.c
added description string to all cvars and commands
[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 cvar_t *cvar_vars = NULL;
25 char *cvar_null_string = "";
26
27 /*
28 ============
29 Cvar_FindVar
30 ============
31 */
32 cvar_t *Cvar_FindVar (const char *var_name)
33 {
34         cvar_t *var;
35
36         for (var = cvar_vars;var;var = var->next)
37                 if (!strcasecmp (var_name, var->name))
38                         return var;
39
40         return NULL;
41 }
42
43 cvar_t *Cvar_FindVarAfter (const char *prev_var_name, int neededflags)
44 {
45         cvar_t *var;
46
47         if (*prev_var_name)
48         {
49                 var = Cvar_FindVar (prev_var_name);
50                 if (!var)
51                         return NULL;
52                 var = var->next;
53         }
54         else
55                 var = cvar_vars;
56
57         // search for the next cvar matching the needed flags
58         while (var)
59         {
60                 if ((var->flags & neededflags) || !neededflags)
61                         break;
62                 var = var->next;
63         }
64         return var;
65 }
66
67 /*
68 ============
69 Cvar_VariableValue
70 ============
71 */
72 float Cvar_VariableValue (const char *var_name)
73 {
74         cvar_t *var;
75
76         var = Cvar_FindVar (var_name);
77         if (!var)
78                 return 0;
79         return atof (var->string);
80 }
81
82
83 /*
84 ============
85 Cvar_VariableString
86 ============
87 */
88 const char *Cvar_VariableString (const char *var_name)
89 {
90         cvar_t *var;
91
92         var = Cvar_FindVar (var_name);
93         if (!var)
94                 return cvar_null_string;
95         return var->string;
96 }
97
98 /*
99 ============
100 Cvar_VariableDefString
101 ============
102 */
103 const char *Cvar_VariableDefString (const char *var_name)
104 {
105         cvar_t *var;
106
107         var = Cvar_FindVar (var_name);
108         if (!var)
109                 return cvar_null_string;
110         return var->defstring;
111 }
112
113
114 /*
115 ============
116 Cvar_CompleteVariable
117 ============
118 */
119 const char *Cvar_CompleteVariable (const char *partial)
120 {
121         cvar_t          *cvar;
122         size_t          len;
123
124         len = strlen(partial);
125
126         if (!len)
127                 return NULL;
128
129 // check functions
130         for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
131                 if (!strncasecmp (partial,cvar->name, len))
132                         return cvar->name;
133
134         return NULL;
135 }
136
137
138 /*
139         CVar_CompleteCountPossible
140
141         New function for tab-completion system
142         Added by EvilTypeGuy
143         Thanks to Fett erich@heintz.com
144
145 */
146 int Cvar_CompleteCountPossible (const char *partial)
147 {
148         cvar_t  *cvar;
149         size_t  len;
150         int             h;
151
152         h = 0;
153         len = strlen(partial);
154
155         if (!len)
156                 return  0;
157
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))
161                         h++;
162
163         return h;
164 }
165
166 /*
167         CVar_CompleteBuildList
168
169         New function for tab-completion system
170         Added by EvilTypeGuy
171         Thanks to Fett erich@heintz.com
172         Thanks to taniwha
173
174 */
175 const char **Cvar_CompleteBuildList (const char *partial)
176 {
177         const cvar_t *cvar;
178         size_t len = 0;
179         size_t bpos = 0;
180         size_t sizeofbuf = (Cvar_CompleteCountPossible (partial) + 1) * sizeof (const char *);
181         const char **buf;
182
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;
189
190         buf[bpos] = NULL;
191         return buf;
192 }
193
194 // written by LordHavoc
195 void Cvar_CompleteCvarPrint (const char *partial)
196 {
197         cvar_t *cvar;
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);
203 }
204
205
206 /*
207 ============
208 Cvar_Set
209 ============
210 */
211 void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
212 {
213         qboolean changed;
214
215         changed = strcmp(var->string, value);
216         // LordHavoc: don't reallocate when there is no change
217         if (!changed)
218                 return;
219
220         // LordHavoc: don't reallocate when the buffer is the same size
221         if (!var->string || strlen(var->string) != strlen(value))
222         {
223                 Z_Free (var->string);   // free the old value string
224
225                 var->string = (char *)Z_Malloc (strlen(value)+1);
226         }
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);
232 }
233
234 void Cvar_SetQuick (cvar_t *var, const char *value)
235 {
236         if (var == NULL)
237         {
238                 Con_Print("Cvar_SetQuick: var == NULL\n");
239                 return;
240         }
241
242         if (developer.integer)
243                 Con_Printf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
244
245         Cvar_SetQuick_Internal(var, value);
246 }
247
248 void Cvar_Set (const char *var_name, const char *value)
249 {
250         cvar_t *var;
251         var = Cvar_FindVar (var_name);
252         if (var == NULL)
253         {
254                 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
255                 return;
256         }
257         Cvar_SetQuick(var, value);
258 }
259
260 /*
261 ============
262 Cvar_SetValue
263 ============
264 */
265 void Cvar_SetValueQuick(cvar_t *var, float value)
266 {
267         char val[MAX_INPUTLINE];
268
269         if ((float)((int)value) == value)
270                 sprintf(val, "%i", (int)value);
271         else
272                 sprintf(val, "%f", value);
273         Cvar_SetQuick(var, val);
274 }
275
276 void Cvar_SetValue(const char *var_name, float value)
277 {
278         char val[MAX_INPUTLINE];
279
280         if ((float)((int)value) == value)
281                 sprintf(val, "%i", (int)value);
282         else
283                 sprintf(val, "%f", value);
284         Cvar_Set(var_name, val);
285 }
286
287 /*
288 ============
289 Cvar_RegisterVariable
290
291 Adds a freestanding variable to the variable list.
292 ============
293 */
294 void Cvar_RegisterVariable (cvar_t *variable)
295 {
296         cvar_t *current, *next, *cvar;
297         char *oldstr;
298
299         if (developer.integer)
300                 Con_Printf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
301
302 // first check to see if it has already been defined
303         cvar = Cvar_FindVar (variable->name);
304         if (cvar)
305         {
306                 if (cvar->flags & CVAR_ALLOCATED)
307                 {
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)
323                         {
324                                 // head of the list is easy to change
325                                 cvar_vars = variable;
326                         }
327                         else
328                         {
329                                 // otherwise find it somewhere in the list
330                                 for (current = cvar_vars;current->next != cvar;current = current->next)
331                                         ;
332                                 current->next = variable;
333                         }
334
335                         // get rid of old allocated cvar
336                         // (but not cvar->string and cvar->defstring, because we kept those)
337                         Z_Free(cvar->name);
338                         Z_Free(cvar);
339                 }
340                 else
341                         Con_Printf("Can't register variable %s, already defined\n", variable->name);
342                 return;
343         }
344
345 // check for overlap with a command
346         if (Cmd_Exists (variable->name))
347         {
348                 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
349                 return;
350         }
351
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;
360
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 )
364                 ;
365         if( current ) {
366                 current->next = variable;
367         } else {
368                 cvar_vars = variable;
369         }
370         variable->next = next;
371 }
372
373 /*
374 ============
375 Cvar_Get
376
377 Adds a newly allocated variable to the variable list or sets its value.
378 ============
379 */
380 cvar_t *Cvar_Get (const char *name, const char *value, int flags)
381 {
382         cvar_t *cvar;
383
384         if (developer.integer)
385                 Con_Printf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
386
387 // first check to see if it has already been defined
388         cvar = Cvar_FindVar (name);
389         if (cvar)
390         {
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)
395                 {
396                         cvar->flags |= CVAR_DEFAULTSET;
397
398                         Z_Free(cvar->defstring);
399                         cvar->defstring = (char *)Z_Malloc(strlen(value) + 1);
400                         strcpy(cvar->defstring, value);
401                 }
402                 return cvar;
403         }
404
405 // check for overlap with a command
406         if (Cmd_Exists (name))
407         {
408                 Con_Printf("Cvar_Get: %s is a command\n", name);
409                 return NULL;
410         }
411
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
425 // link the variable in
426         cvar->next = cvar_vars;
427         cvar_vars = cvar;
428         return cvar;
429 }
430
431
432 /*
433 ============
434 Cvar_Command
435
436 Handles variable inspection and changing from the console
437 ============
438 */
439 qboolean        Cvar_Command (void)
440 {
441         cvar_t                  *v;
442
443 // check variables
444         v = Cvar_FindVar (Cmd_Argv(0));
445         if (!v)
446                 return false;
447
448 // perform a variable print or set
449         if (Cmd_Argc() == 1)
450         {
451                 Con_Printf("\"%s\" is \"%s\" [\"%s\"]\n", v->name, v->string, v->defstring);
452                 return true;
453         }
454
455         Con_DPrint("Cvar_Command: ");
456
457         if (v->flags & CVAR_READONLY)
458         {
459                 Con_Printf("%s is read-only\n", v->name);
460                 return true;
461         }
462         Cvar_Set (v->name, Cmd_Argv(1));
463         return true;
464 }
465
466
467 /*
468 ============
469 Cvar_WriteVariables
470
471 Writes lines containing "set variable value" for all variables
472 with the archive flag set to true.
473 ============
474 */
475 void Cvar_WriteVariables (qfile_t *f)
476 {
477         cvar_t  *var;
478
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);
482 }
483
484
485 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
486 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
487 /*
488 =========
489 Cvar_List
490 =========
491 */
492 void Cvar_List_f (void)
493 {
494         cvar_t *cvar;
495         const char *partial;
496         size_t len;
497         int count;
498
499         if (Cmd_Argc() > 1)
500         {
501                 partial = Cmd_Argv (1);
502                 len = strlen(partial);
503         }
504         else
505         {
506                 partial = NULL;
507                 len = 0;
508         }
509
510         count = 0;
511         for (cvar = cvar_vars; cvar; cvar = cvar->next)
512         {
513                 if (partial && strncasecmp (partial,cvar->name,len))
514                         continue;
515
516                 Con_Printf("%s is \"%s\" [\"%s\"]\n", cvar->name, cvar->string, cvar->defstring);
517                 count++;
518         }
519
520         if (partial)
521                 Con_Printf("%i cvar(s) beginning with \"%s\"\n", count, partial);
522         else
523                 Con_Printf("%i cvar(s)\n", count);
524 }
525 // 2000-01-09 CvarList command by Maddes
526
527 void Cvar_Set_f (void)
528 {
529         cvar_t *cvar;
530
531         // make sure it's the right number of parameters
532         if (Cmd_Argc() < 3)
533         {
534                 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value>\n");
535                 return;
536         }
537
538         // check if it's read-only
539         cvar = Cvar_FindVar(Cmd_Argv(1));
540         if (cvar && cvar->flags & CVAR_READONLY)
541         {
542                 Con_Printf("Set: %s is read-only\n", cvar->name);
543                 return;
544         }
545
546         Con_DPrint("Set: ");
547
548         // all looks ok, create/modify the cvar
549         Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), 0);
550 }
551
552 void Cvar_SetA_f (void)
553 {
554         cvar_t *cvar;
555
556         // make sure it's the right number of parameters
557         if (Cmd_Argc() < 3)
558         {
559                 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value>\n");
560                 return;
561         }
562
563         // check if it's read-only
564         cvar = Cvar_FindVar(Cmd_Argv(1));
565         if (cvar && cvar->flags & CVAR_READONLY)
566         {
567                 Con_Printf("SetA: %s is read-only\n", cvar->name);
568                 return;
569         }
570
571         Con_DPrint("SetA: ");
572
573         // all looks ok, create/modify the cvar
574         Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), CVAR_SAVE);
575 }
576
577