]> icculus.org git repositories - divverent/darkplaces.git/blob - cvar.c
cleaned up PF_fopen filename rejection, now rejects more unsafe names, including...
[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 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 (!strcmp (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 ============
101 Cvar_CompleteVariable
102 ============
103 */
104 const char *Cvar_CompleteVariable (const char *partial)
105 {
106         cvar_t          *cvar;
107         int                     len;
108
109         len = strlen(partial);
110
111         if (!len)
112                 return NULL;
113
114 // check functions
115         for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
116                 if (!strncmp (partial,cvar->name, len))
117                         return cvar->name;
118
119         return NULL;
120 }
121
122
123 /*
124         CVar_CompleteCountPossible
125
126         New function for tab-completion system
127         Added by EvilTypeGuy
128         Thanks to Fett erich@heintz.com
129
130 */
131 int Cvar_CompleteCountPossible (const char *partial)
132 {
133         cvar_t  *cvar;
134         int             len;
135         int             h;
136
137         h = 0;
138         len = strlen(partial);
139
140         if (!len)
141                 return  0;
142
143         // Loop through the cvars and count all possible matches
144         for (cvar = cvar_vars; cvar; cvar = cvar->next)
145                 if (!strncasecmp(partial, cvar->name, len))
146                         h++;
147
148         return h;
149 }
150
151 /*
152         CVar_CompleteBuildList
153
154         New function for tab-completion system
155         Added by EvilTypeGuy
156         Thanks to Fett erich@heintz.com
157         Thanks to taniwha
158
159 */
160 const char **Cvar_CompleteBuildList (const char *partial)
161 {
162         const cvar_t *cvar;
163         int len = 0;
164         int bpos = 0;
165         int sizeofbuf = (Cvar_CompleteCountPossible (partial) + 1) * sizeof (const char *);
166         const char **buf;
167
168         len = strlen(partial);
169         buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
170         // Loop through the alias list and print all matches
171         for (cvar = cvar_vars; cvar; cvar = cvar->next)
172                 if (!strncasecmp(partial, cvar->name, len))
173                         buf[bpos++] = cvar->name;
174
175         buf[bpos] = NULL;
176         return buf;
177 }
178
179 /*
180 ============
181 Cvar_Set
182 ============
183 */
184 void Cvar_SetQuick (cvar_t *var, const char *value)
185 {
186         qboolean changed;
187
188         if (var == NULL)
189         {
190                 Con_Print("Cvar_SetQuick: var == NULL\n");
191                 return;
192         }
193
194         changed = strcmp(var->string, value);
195         // LordHavoc: don't reallocate when there is no change
196         if (!changed)
197                 return;
198
199         // LordHavoc: don't reallocate when the buffer is the same size
200         if (!var->string || strlen(var->string) != strlen(value))
201         {
202                 Z_Free (var->string);   // free the old value string
203
204                 var->string = Z_Malloc (strlen(value)+1);
205         }
206         strcpy (var->string, value);
207         var->value = atof (var->string);
208         var->integer = (int) var->value;
209         if ((var->flags & CVAR_NOTIFY) && changed && sv.active)
210                 SV_BroadcastPrintf("\"%s\" changed to \"%s\"\n", var->name, var->string);
211 }
212
213 void Cvar_Set (const char *var_name, const char *value)
214 {
215         cvar_t *var;
216         var = Cvar_FindVar (var_name);
217         if (var == NULL)
218         {
219                 // there is an error in C code if this happens
220                 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
221                 return;
222         }
223         if (var->flags & CVAR_READONLY)
224         {
225                 Con_Printf("Cvar_Set: %s is read-only\n", var_name);
226                 return;
227         }
228
229         Cvar_SetQuick(var, value);
230 }
231
232 /*
233 ============
234 Cvar_SetValue
235 ============
236 */
237 void Cvar_SetValueQuick(cvar_t *var, float value)
238 {
239         char val[256];
240
241         if ((float)((int)value) == value)
242                 sprintf(val, "%i", (int)value);
243         else
244                 sprintf(val, "%f", value);
245         Cvar_SetQuick(var, val);
246 }
247
248 void Cvar_SetValue(const char *var_name, float value)
249 {
250         char val[256];
251
252         if ((float)((int)value) == value)
253                 sprintf(val, "%i", (int)value);
254         else
255                 sprintf(val, "%f", value);
256         Cvar_Set(var_name, val);
257 }
258
259 /*
260 ============
261 Cvar_RegisterVariable
262
263 Adds a freestanding variable to the variable list.
264 ============
265 */
266 void Cvar_RegisterVariable (cvar_t *variable)
267 {
268         char    *oldstr;
269
270 // first check to see if it has already been defined
271         if (Cvar_FindVar (variable->name))
272         {
273                 Con_Printf("Can't register variable %s, already defined\n", variable->name);
274                 return;
275         }
276
277 // check for overlap with a command
278         if (Cmd_Exists (variable->name))
279         {
280                 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
281                 return;
282         }
283
284 // copy the value off, because future sets will Z_Free it
285         oldstr = variable->string;
286         variable->string = Z_Malloc (strlen(variable->string)+1);
287         strcpy (variable->string, oldstr);
288         variable->value = atof (variable->string);
289         variable->integer = (int) variable->value;
290
291 // link the variable in
292         variable->next = cvar_vars;
293         cvar_vars = variable;
294 }
295
296 /*
297 ============
298 Cvar_Command
299
300 Handles variable inspection and changing from the console
301 ============
302 */
303 qboolean        Cvar_Command (void)
304 {
305         cvar_t                  *v;
306
307 // check variables
308         v = Cvar_FindVar (Cmd_Argv(0));
309         if (!v)
310                 return false;
311
312 // perform a variable print or set
313         if (Cmd_Argc() == 1)
314         {
315                 // only print if host_initialized (otherwise it could print twice if this is in a script)
316                 if (host_initialized)
317                         Con_Printf("\"%s\" is \"%s\"\n", v->name, v->string);
318                 return true;
319         }
320
321         Cvar_Set (v->name, Cmd_Argv(1));
322         return true;
323 }
324
325
326 /*
327 ============
328 Cvar_WriteVariables
329
330 Writes lines containing "set variable value" for all variables
331 with the archive flag set to true.
332 ============
333 */
334 void Cvar_WriteVariables (qfile_t *f)
335 {
336         cvar_t  *var;
337
338         for (var = cvar_vars ; var ; var = var->next)
339                 if (var->flags & CVAR_SAVE)
340                         FS_Printf(f, "%s \"%s\"\n", var->name, var->string);
341 }
342
343
344 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
345 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
346 /*
347 =========
348 Cvar_List
349 =========
350 */
351 void Cvar_List_f (void)
352 {
353         cvar_t *cvar;
354         const char *partial;
355         int len, count;
356
357         if (Cmd_Argc() > 1)
358         {
359                 partial = Cmd_Argv (1);
360                 len = strlen(partial);
361         }
362         else
363         {
364                 partial = NULL;
365                 len = 0;
366         }
367
368         count = 0;
369         for (cvar = cvar_vars; cvar; cvar = cvar->next)
370         {
371                 if (partial && strncmp (partial,cvar->name,len))
372                         continue;
373
374                 Con_Printf("%s is \"%s\"\n", cvar->name, cvar->string);
375                 count++;
376         }
377
378         Con_Printf("%i cvar(s)", count);
379         if (partial)
380                 Con_Printf(" beginning with \"%s\"", partial);
381         Con_Print("\n");
382 }
383 // 2000-01-09 CvarList command by Maddes
384