]> icculus.org git repositories - divverent/darkplaces.git/blob - cvar.c
The BSD audio module had the same bug as the OSS module (incorrectly reporting "audio...
[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 cvar_t *cvar_vars = NULL;
25 cvar_t *cvar_hashtable[65536];
26 char *cvar_null_string = "";
27
28 /*
29 ============
30 Cvar_FindVar
31 ============
32 */
33 cvar_t *Cvar_FindVar (const char *var_name)
34 {
35         int hashindex;
36         cvar_t *var;
37
38         // use hash lookup to minimize search time
39         hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name));
40         for (var = cvar_hashtable[hashindex];var;var = var->nextonhashchain)
41                 if (!strcasecmp (var_name, var->name))
42                         return var;
43
44         return NULL;
45 }
46
47 cvar_t *Cvar_FindVarAfter (const char *prev_var_name, int neededflags)
48 {
49         cvar_t *var;
50
51         if (*prev_var_name)
52         {
53                 var = Cvar_FindVar (prev_var_name);
54                 if (!var)
55                         return NULL;
56                 var = var->next;
57         }
58         else
59                 var = cvar_vars;
60
61         // search for the next cvar matching the needed flags
62         while (var)
63         {
64                 if ((var->flags & neededflags) || !neededflags)
65                         break;
66                 var = var->next;
67         }
68         return var;
69 }
70
71 /*
72 ============
73 Cvar_VariableValue
74 ============
75 */
76 float Cvar_VariableValue (const char *var_name)
77 {
78         cvar_t *var;
79
80         var = Cvar_FindVar (var_name);
81         if (!var)
82                 return 0;
83         return atof (var->string);
84 }
85
86
87 /*
88 ============
89 Cvar_VariableString
90 ============
91 */
92 const char *Cvar_VariableString (const char *var_name)
93 {
94         cvar_t *var;
95
96         var = Cvar_FindVar (var_name);
97         if (!var)
98                 return cvar_null_string;
99         return var->string;
100 }
101
102 /*
103 ============
104 Cvar_VariableDefString
105 ============
106 */
107 const char *Cvar_VariableDefString (const char *var_name)
108 {
109         cvar_t *var;
110
111         var = Cvar_FindVar (var_name);
112         if (!var)
113                 return cvar_null_string;
114         return var->defstring;
115 }
116
117
118 /*
119 ============
120 Cvar_CompleteVariable
121 ============
122 */
123 const char *Cvar_CompleteVariable (const char *partial)
124 {
125         cvar_t          *cvar;
126         size_t          len;
127
128         len = strlen(partial);
129
130         if (!len)
131                 return NULL;
132
133 // check functions
134         for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
135                 if (!strncasecmp (partial,cvar->name, len))
136                         return cvar->name;
137
138         return NULL;
139 }
140
141
142 /*
143         CVar_CompleteCountPossible
144
145         New function for tab-completion system
146         Added by EvilTypeGuy
147         Thanks to Fett erich@heintz.com
148
149 */
150 int Cvar_CompleteCountPossible (const char *partial)
151 {
152         cvar_t  *cvar;
153         size_t  len;
154         int             h;
155
156         h = 0;
157         len = strlen(partial);
158
159         if (!len)
160                 return  0;
161
162         // Loop through the cvars and count all possible matches
163         for (cvar = cvar_vars; cvar; cvar = cvar->next)
164                 if (!strncasecmp(partial, cvar->name, len))
165                         h++;
166
167         return h;
168 }
169
170 /*
171         CVar_CompleteBuildList
172
173         New function for tab-completion system
174         Added by EvilTypeGuy
175         Thanks to Fett erich@heintz.com
176         Thanks to taniwha
177
178 */
179 const char **Cvar_CompleteBuildList (const char *partial)
180 {
181         const cvar_t *cvar;
182         size_t len = 0;
183         size_t bpos = 0;
184         size_t sizeofbuf = (Cvar_CompleteCountPossible (partial) + 1) * sizeof (const char *);
185         const char **buf;
186
187         len = strlen(partial);
188         buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
189         // Loop through the alias list and print all matches
190         for (cvar = cvar_vars; cvar; cvar = cvar->next)
191                 if (!strncasecmp(partial, cvar->name, len))
192                         buf[bpos++] = cvar->name;
193
194         buf[bpos] = NULL;
195         return buf;
196 }
197
198 // written by LordHavoc
199 void Cvar_CompleteCvarPrint (const char *partial)
200 {
201         cvar_t *cvar;
202         size_t len = strlen(partial);
203         // Loop through the command list and print all matches
204         for (cvar = cvar_vars; cvar; cvar = cvar->next)
205                 if (!strncasecmp(partial, cvar->name, len))
206                         Con_Printf ("%c3%s%s : \"%s\" (\"%s\") : %s\n", STRING_COLOR_TAG, cvar->name, STRING_COLOR_DEFAULT_STR, cvar->string, cvar->defstring, cvar->description);
207 }
208
209
210 /*
211 ============
212 Cvar_Set
213 ============
214 */
215 void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
216 {
217         qboolean changed;
218
219         changed = strcmp(var->string, value);
220         // LordHavoc: don't reallocate when there is no change
221         if (!changed)
222                 return;
223
224         // LordHavoc: don't reallocate when the buffer is the same size
225         if (!var->string || strlen(var->string) != strlen(value))
226         {
227                 Z_Free (var->string);   // free the old value string
228
229                 var->string = (char *)Z_Malloc (strlen(value)+1);
230         }
231         strcpy (var->string, value);
232         var->value = atof (var->string);
233         var->integer = (int) var->value;
234         if ((var->flags & CVAR_NOTIFY) && changed && sv.active)
235                 SV_BroadcastPrintf("\"%s\" changed to \"%s\"\n", var->name, var->string);
236 #if 0
237         // TODO: add infostring support to the server?
238         if ((var->flags & CVAR_SERVERINFO) && changed && sv.active)
239         {
240                 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
241                 if (sv.active)
242                 {
243                         MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
244                         MSG_WriteString (&sv.reliable_datagram, var->name);
245                         MSG_WriteString (&sv.reliable_datagram, var->string);
246                 }
247         }
248 #endif
249         if ((var->flags & CVAR_USERINFO) && cls.state != ca_dedicated)
250                 CL_SetInfo(var->name, var->string, true, false, false, false);
251 }
252
253 void Cvar_SetQuick (cvar_t *var, const char *value)
254 {
255         if (var == NULL)
256         {
257                 Con_Print("Cvar_SetQuick: var == NULL\n");
258                 return;
259         }
260
261         if (developer.integer >= 100)
262                 Con_Printf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
263
264         Cvar_SetQuick_Internal(var, value);
265 }
266
267 void Cvar_Set (const char *var_name, const char *value)
268 {
269         cvar_t *var;
270         var = Cvar_FindVar (var_name);
271         if (var == NULL)
272         {
273                 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
274                 return;
275         }
276         Cvar_SetQuick(var, value);
277 }
278
279 /*
280 ============
281 Cvar_SetValue
282 ============
283 */
284 void Cvar_SetValueQuick(cvar_t *var, float value)
285 {
286         char val[MAX_INPUTLINE];
287
288         if ((float)((int)value) == value)
289                 sprintf(val, "%i", (int)value);
290         else
291                 sprintf(val, "%f", value);
292         Cvar_SetQuick(var, val);
293 }
294
295 void Cvar_SetValue(const char *var_name, float value)
296 {
297         char val[MAX_INPUTLINE];
298
299         if ((float)((int)value) == value)
300                 sprintf(val, "%i", (int)value);
301         else
302                 sprintf(val, "%f", value);
303         Cvar_Set(var_name, val);
304 }
305
306 /*
307 ============
308 Cvar_RegisterVariable
309
310 Adds a freestanding variable to the variable list.
311 ============
312 */
313 void Cvar_RegisterVariable (cvar_t *variable)
314 {
315         int hashindex;
316         cvar_t *current, *next, *cvar;
317         char *oldstr;
318
319         if (developer.integer >= 100)
320                 Con_Printf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
321
322 // first check to see if it has already been defined
323         cvar = Cvar_FindVar (variable->name);
324         if (cvar)
325         {
326                 if (cvar->flags & CVAR_ALLOCATED)
327                 {
328                         if (developer.integer >= 100)
329                                 Con_Printf("...  replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
330                         // fixed variables replace allocated ones
331                         // (because the engine directly accesses fixed variables)
332                         // NOTE: this isn't actually used currently
333                         // (all cvars are registered before config parsing)
334                         variable->flags |= (cvar->flags & ~CVAR_ALLOCATED);
335                         // cvar->string is now owned by variable instead
336                         variable->string = cvar->string;
337                         variable->defstring = cvar->defstring;
338                         variable->value = atof (variable->string);
339                         variable->integer = (int) variable->value;
340                         // replace cvar with this one...
341                         variable->next = cvar->next;
342                         if (cvar_vars == cvar)
343                         {
344                                 // head of the list is easy to change
345                                 cvar_vars = variable;
346                         }
347                         else
348                         {
349                                 // otherwise find it somewhere in the list
350                                 for (current = cvar_vars;current->next != cvar;current = current->next)
351                                         ;
352                                 current->next = variable;
353                         }
354
355                         // get rid of old allocated cvar
356                         // (but not cvar->string and cvar->defstring, because we kept those)
357                         Z_Free(cvar->name);
358                         Z_Free(cvar);
359                 }
360                 else
361                         Con_Printf("Can't register variable %s, already defined\n", variable->name);
362                 return;
363         }
364
365 // check for overlap with a command
366         if (Cmd_Exists (variable->name))
367         {
368                 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
369                 return;
370         }
371
372 // copy the value off, because future sets will Z_Free it
373         oldstr = variable->string;
374         variable->string = (char *)Z_Malloc (strlen(variable->string)+1);
375         strcpy (variable->string, oldstr);
376         variable->defstring = (char *)Z_Malloc (strlen(variable->string)+1);
377         strcpy (variable->defstring, oldstr);
378         variable->value = atof (variable->string);
379         variable->integer = (int) variable->value;
380
381 // link the variable in
382 // alphanumerical order
383         for( current = NULL, next = cvar_vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
384                 ;
385         if( current ) {
386                 current->next = variable;
387         } else {
388                 cvar_vars = variable;
389         }
390         variable->next = next;
391
392         // link to head of list in this hash table index
393         hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name));
394         variable->nextonhashchain = cvar_hashtable[hashindex];
395         cvar_hashtable[hashindex] = variable;
396 }
397
398 /*
399 ============
400 Cvar_Get
401
402 Adds a newly allocated variable to the variable list or sets its value.
403 ============
404 */
405 cvar_t *Cvar_Get (const char *name, const char *value, int flags)
406 {
407         int hashindex;
408         cvar_t *current, *next, *cvar;
409
410         if (developer.integer >= 100)
411                 Con_Printf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
412
413 // first check to see if it has already been defined
414         cvar = Cvar_FindVar (name);
415         if (cvar)
416         {
417                 cvar->flags |= flags;
418                 Cvar_SetQuick_Internal (cvar, value);
419                 // also set the default value (but only once)
420                 if (~cvar->flags & CVAR_DEFAULTSET)
421                 {
422                         cvar->flags |= CVAR_DEFAULTSET;
423
424                         Z_Free(cvar->defstring);
425                         cvar->defstring = (char *)Z_Malloc(strlen(value) + 1);
426                         strcpy(cvar->defstring, value);
427                 }
428                 return cvar;
429         }
430
431 // check for overlap with a command
432         if (Cmd_Exists (name))
433         {
434                 Con_Printf("Cvar_Get: %s is a command\n", name);
435                 return NULL;
436         }
437
438 // allocate a new cvar, cvar name, and cvar string
439 // FIXME: these never get Z_Free'd
440         cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
441         cvar->flags = flags | CVAR_ALLOCATED | CVAR_DEFAULTSET;
442         cvar->name = (char *)Z_Malloc(strlen(name)+1);
443         strcpy(cvar->name, name);
444         cvar->string = (char *)Z_Malloc(strlen(value)+1);
445         strcpy(cvar->string, value);
446         cvar->defstring = (char *)Z_Malloc(strlen(value)+1);
447         strcpy(cvar->defstring, value);
448         cvar->value = atof (cvar->string);
449         cvar->integer = (int) cvar->value;
450         cvar->description = "custom cvar";
451
452 // link the variable in
453 // alphanumerical order
454         for( current = NULL, next = cvar_vars ; next && strcmp( next->name, cvar->name ) < 0 ; current = next, next = next->next )
455                 ;
456         if( current )
457                 current->next = cvar;
458         else
459                 cvar_vars = cvar;
460         cvar->next = next;
461
462         // link to head of list in this hash table index
463         hashindex = CRC_Block((const unsigned char *)cvar->name, strlen(cvar->name));
464         cvar->nextonhashchain = cvar_hashtable[hashindex];
465         cvar_hashtable[hashindex] = cvar;
466
467         return cvar;
468 }
469
470
471 /*
472 ============
473 Cvar_Command
474
475 Handles variable inspection and changing from the console
476 ============
477 */
478 qboolean        Cvar_Command (void)
479 {
480         cvar_t                  *v;
481
482 // check variables
483         v = Cvar_FindVar (Cmd_Argv(0));
484         if (!v)
485                 return false;
486
487 // perform a variable print or set
488         if (Cmd_Argc() == 1)
489         {
490                 Con_Printf("\"%s\" is \"%s\" [\"%s\"]\n", v->name, v->string, v->defstring);
491                 return true;
492         }
493
494         if (developer.integer >= 100)
495                 Con_DPrint("Cvar_Command: ");
496
497         if (v->flags & CVAR_READONLY)
498         {
499                 Con_Printf("%s is read-only\n", v->name);
500                 return true;
501         }
502         Cvar_Set (v->name, Cmd_Argv(1));
503         if (developer.integer >= 100)
504                 Con_DPrint("\n");
505         return true;
506 }
507
508
509 /*
510 ============
511 Cvar_WriteVariables
512
513 Writes lines containing "set variable value" for all variables
514 with the archive flag set to true.
515 ============
516 */
517 void Cvar_WriteVariables (qfile_t *f)
518 {
519         cvar_t  *var;
520
521         // don't save cvars that match their default value
522         for (var = cvar_vars ; var ; var = var->next)
523                 if (var->flags & CVAR_SAVE && strcmp(var->string, var->defstring))
524                         FS_Printf(f, "%s%s \"%s\"\n", var->flags & CVAR_ALLOCATED ? "seta " : "", var->name, var->string);
525 }
526
527
528 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
529 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
530 /*
531 =========
532 Cvar_List
533 =========
534 */
535 void Cvar_List_f (void)
536 {
537         cvar_t *cvar;
538         const char *partial;
539         size_t len;
540         int count;
541
542         if (Cmd_Argc() > 1)
543         {
544                 partial = Cmd_Argv (1);
545                 len = strlen(partial);
546         }
547         else
548         {
549                 partial = NULL;
550                 len = 0;
551         }
552
553         count = 0;
554         for (cvar = cvar_vars; cvar; cvar = cvar->next)
555         {
556                 if (partial && strncasecmp (partial,cvar->name,len))
557                         continue;
558
559                 Con_Printf("%s is \"%s\" [\"%s\"] %s\n", cvar->name, cvar->string, cvar->defstring, cvar->description);
560                 count++;
561         }
562
563         if (partial)
564                 Con_Printf("%i cvar(s) beginning with \"%s\"\n", count, partial);
565         else
566                 Con_Printf("%i cvar(s)\n", count);
567 }
568 // 2000-01-09 CvarList command by Maddes
569
570 void Cvar_Set_f (void)
571 {
572         cvar_t *cvar;
573
574         // make sure it's the right number of parameters
575         if (Cmd_Argc() < 3)
576         {
577                 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value>\n");
578                 return;
579         }
580
581         // check if it's read-only
582         cvar = Cvar_FindVar(Cmd_Argv(1));
583         if (cvar && cvar->flags & CVAR_READONLY)
584         {
585                 Con_Printf("Set: %s is read-only\n", cvar->name);
586                 return;
587         }
588
589         Con_DPrint("Set: ");
590
591         // all looks ok, create/modify the cvar
592         Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), 0);
593 }
594
595 void Cvar_SetA_f (void)
596 {
597         cvar_t *cvar;
598
599         // make sure it's the right number of parameters
600         if (Cmd_Argc() < 3)
601         {
602                 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value>\n");
603                 return;
604         }
605
606         // check if it's read-only
607         cvar = Cvar_FindVar(Cmd_Argv(1));
608         if (cvar && cvar->flags & CVAR_READONLY)
609         {
610                 Con_Printf("SetA: %s is read-only\n", cvar->name);
611                 return;
612         }
613
614         Con_DPrint("SetA: ");
615
616         // all looks ok, create/modify the cvar
617         Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), CVAR_SAVE);
618 }
619
620