]> git.sesse.net Git - vlc/blob - src/config/core.c
ConfigKeyToString: simplify and fix potential leak
[vlc] / src / config / core.c
1 /*****************************************************************************
2  * core.c management of the modules configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_keys.h>
30 #include <vlc_modules.h>
31
32 #include "vlc_configuration.h"
33
34 #include <assert.h>
35
36 #include "configuration.h"
37 #include "modules/modules.h"
38
39 vlc_rwlock_t config_lock;
40
41 static inline char *strdupnull (const char *src)
42 {
43     return src ? strdup (src) : NULL;
44 }
45
46 /* Item types that use a string value (i.e. serialized in the module cache) */
47 int IsConfigStringType (int type)
48 {
49     static const unsigned char config_types[] =
50     {
51         CONFIG_ITEM_STRING, CONFIG_ITEM_MODULE,
52         CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_MODULE_CAT, CONFIG_ITEM_PASSWORD,
53         CONFIG_ITEM_MODULE_LIST, CONFIG_ITEM_MODULE_LIST_CAT,
54         CONFIG_ITEM_FONT, CONFIG_ITEM_LOADFILE, CONFIG_ITEM_SAVEFILE,
55     };
56
57     /* NOTE: this needs to be changed if we ever get more than 255 types */
58     return memchr (config_types, type, sizeof (config_types)) != NULL;
59 }
60
61
62 int IsConfigIntegerType (int type)
63 {
64     static const unsigned char config_types[] =
65     {
66         CONFIG_ITEM_INTEGER, CONFIG_ITEM_KEY, CONFIG_ITEM_BOOL,
67         CONFIG_CATEGORY, CONFIG_SUBCATEGORY
68     };
69
70     return memchr (config_types, type, sizeof (config_types)) != NULL;
71 }
72
73 #undef config_GetType
74 /*****************************************************************************
75  * config_GetType: get the type of a variable (bool, int, float, string)
76  *****************************************************************************
77  * This function is used to get the type of a variable from its name.
78  * Beware, this is quite slow.
79  *****************************************************************************/
80 int config_GetType( vlc_object_t *p_this, const char *psz_name )
81 {
82     module_config_t *p_config;
83     int i_type;
84
85     p_config = config_FindConfig( p_this, psz_name );
86
87     /* sanity checks */
88     if( !p_config )
89     {
90         return 0;
91     }
92
93     switch( p_config->i_type )
94     {
95     case CONFIG_ITEM_BOOL:
96         i_type = VLC_VAR_BOOL;
97         break;
98
99     case CONFIG_ITEM_INTEGER:
100     case CONFIG_ITEM_KEY:
101         i_type = VLC_VAR_INTEGER;
102         break;
103
104     case CONFIG_ITEM_FLOAT:
105         i_type = VLC_VAR_FLOAT;
106         break;
107
108     case CONFIG_ITEM_MODULE:
109     case CONFIG_ITEM_MODULE_CAT:
110     case CONFIG_ITEM_MODULE_LIST:
111     case CONFIG_ITEM_MODULE_LIST_CAT:
112     case CONFIG_ITEM_STRING:
113     case CONFIG_ITEM_PASSWORD:
114     case CONFIG_ITEM_LOADFILE:
115     case CONFIG_ITEM_SAVEFILE:
116     case CONFIG_ITEM_DIRECTORY:
117         i_type = VLC_VAR_STRING;
118         break;
119
120     default:
121         i_type = 0;
122         break;
123     }
124
125     return i_type;
126 }
127
128 #undef config_GetInt
129 /*****************************************************************************
130  * config_GetInt: get the value of an int variable
131  *****************************************************************************
132  * This function is used to get the value of variables which are internally
133  * represented by an integer (CONFIG_ITEM_INTEGER and
134  * CONFIG_ITEM_BOOL).
135  *****************************************************************************/
136 int64_t config_GetInt( vlc_object_t *p_this, const char *psz_name )
137 {
138     module_config_t *p_config;
139
140     p_config = config_FindConfig( p_this, psz_name );
141
142     /* sanity checks */
143     if( !p_config )
144     {
145         msg_Err( p_this, "option %s does not exist", psz_name );
146         return -1;
147     }
148
149     if (!IsConfigIntegerType (p_config->i_type))
150     {
151         msg_Err( p_this, "option %s does not refer to an int", psz_name );
152         return -1;
153     }
154
155     int64_t val;
156
157     vlc_rwlock_rdlock (&config_lock);
158     val = p_config->value.i;
159     vlc_rwlock_unlock (&config_lock);
160     return val;
161 }
162
163 #undef config_GetFloat
164 /*****************************************************************************
165  * config_GetFloat: get the value of a float variable
166  *****************************************************************************
167  * This function is used to get the value of variables which are internally
168  * represented by a float (CONFIG_ITEM_FLOAT).
169  *****************************************************************************/
170 float config_GetFloat( vlc_object_t *p_this, const char *psz_name )
171 {
172     module_config_t *p_config;
173
174     p_config = config_FindConfig( p_this, psz_name );
175
176     /* sanity checks */
177     if( !p_config )
178     {
179         msg_Err( p_this, "option %s does not exist", psz_name );
180         return -1;
181     }
182
183     if (!IsConfigFloatType (p_config->i_type))
184     {
185         msg_Err( p_this, "option %s does not refer to a float", psz_name );
186         return -1;
187     }
188
189     float val;
190
191     vlc_rwlock_rdlock (&config_lock);
192     val = p_config->value.f;
193     vlc_rwlock_unlock (&config_lock);
194     return val;
195 }
196
197 #undef config_GetPsz
198 /*****************************************************************************
199  * config_GetPsz: get the string value of a string variable
200  *****************************************************************************
201  * This function is used to get the value of variables which are internally
202  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_*FILE,
203  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
204  *
205  * Important note: remember to free() the returned char* because it's a
206  *   duplicate of the actual value. It isn't safe to return a pointer to the
207  *   actual value as it can be modified at any time.
208  *****************************************************************************/
209 char * config_GetPsz( vlc_object_t *p_this, const char *psz_name )
210 {
211     module_config_t *p_config;
212
213     p_config = config_FindConfig( p_this, psz_name );
214
215     /* sanity checks */
216     if( !p_config )
217     {
218         msg_Err( p_this, "option %s does not exist", psz_name );
219         return NULL;
220     }
221
222     if (!IsConfigStringType (p_config->i_type))
223     {
224         msg_Err( p_this, "option %s does not refer to a string", psz_name );
225         return NULL;
226     }
227
228     /* return a copy of the string */
229     vlc_rwlock_rdlock (&config_lock);
230     char *psz_value = strdupnull (p_config->value.psz);
231     vlc_rwlock_unlock (&config_lock);
232
233     return psz_value;
234 }
235
236 #undef config_PutPsz
237 /*****************************************************************************
238  * config_PutPsz: set the string value of a string variable
239  *****************************************************************************
240  * This function is used to set the value of variables which are internally
241  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_*FILE,
242  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
243  *****************************************************************************/
244 void config_PutPsz( vlc_object_t *p_this,
245                       const char *psz_name, const char *psz_value )
246 {
247     module_config_t *p_config;
248
249     p_config = config_FindConfig( p_this, psz_name );
250
251
252     /* sanity checks */
253     if( !p_config )
254     {
255         msg_Warn( p_this, "option %s does not exist", psz_name );
256         return;
257     }
258
259     if (!IsConfigStringType (p_config->i_type))
260     {
261         msg_Err( p_this, "option %s does not refer to a string", psz_name );
262         return;
263     }
264
265     char *str, *oldstr;
266     if ((psz_value != NULL) && *psz_value)
267         str = strdup (psz_value);
268     else
269         str = NULL;
270
271     vlc_rwlock_wrlock (&config_lock);
272     oldstr = (char *)p_config->value.psz;
273     p_config->value.psz = str;
274     p_config->b_dirty = true;
275     vlc_rwlock_unlock (&config_lock);
276
277     free (oldstr);
278 }
279
280 #undef config_PutInt
281 /*****************************************************************************
282  * config_PutInt: set the integer value of an int variable
283  *****************************************************************************
284  * This function is used to set the value of variables which are internally
285  * represented by an integer (CONFIG_ITEM_INTEGER and
286  * CONFIG_ITEM_BOOL).
287  *****************************************************************************/
288 void config_PutInt( vlc_object_t *p_this, const char *psz_name,
289                     int64_t i_value )
290 {
291     module_config_t *p_config;
292
293     p_config = config_FindConfig( p_this, psz_name );
294
295     /* sanity checks */
296     if( !p_config )
297     {
298         msg_Warn( p_this, "option %s does not exist", psz_name );
299         return;
300     }
301
302     if (!IsConfigIntegerType (p_config->i_type))
303     {
304         msg_Err( p_this, "option %s does not refer to an int", psz_name );
305         return;
306     }
307
308     if (i_value < p_config->min.i)
309         i_value = p_config->min.i;
310     if (i_value > p_config->max.i)
311         i_value = p_config->max.i;
312
313     vlc_rwlock_wrlock (&config_lock);
314     p_config->value.i = i_value;
315     p_config->b_dirty = true;
316     vlc_rwlock_unlock (&config_lock);
317 }
318
319 #undef config_PutFloat
320 /*****************************************************************************
321  * config_PutFloat: set the value of a float variable
322  *****************************************************************************
323  * This function is used to set the value of variables which are internally
324  * represented by a float (CONFIG_ITEM_FLOAT).
325  *****************************************************************************/
326 void config_PutFloat( vlc_object_t *p_this,
327                       const char *psz_name, float f_value )
328 {
329     module_config_t *p_config;
330
331     p_config = config_FindConfig( p_this, psz_name );
332
333     /* sanity checks */
334     if( !p_config )
335     {
336         msg_Warn( p_this, "option %s does not exist", psz_name );
337         return;
338     }
339
340     if (!IsConfigFloatType (p_config->i_type))
341     {
342         msg_Err( p_this, "option %s does not refer to a float", psz_name );
343         return;
344     }
345
346     /* if f_min == f_max == 0, then do not use them */
347     if ((p_config->min.f == 0) && (p_config->max.f == 0))
348         ;
349     else if (f_value < p_config->min.f)
350         f_value = p_config->min.f;
351     else if (f_value > p_config->max.f)
352         f_value = p_config->max.f;
353
354     vlc_rwlock_wrlock (&config_lock);
355     p_config->value.f = f_value;
356     p_config->b_dirty = true;
357     vlc_rwlock_unlock (&config_lock);
358 }
359
360 static int confcmp (const void *a, const void *b)
361 {
362     const module_config_t *const *ca = a, *const *cb = b;
363
364     return strcmp ((*ca)->psz_name, (*cb)->psz_name);
365 }
366
367 static int confnamecmp (const void *key, const void *elem)
368 {
369     const module_config_t *const *conf = elem;
370
371     return strcmp (key, (*conf)->psz_name);
372 }
373
374 static struct
375 {
376     module_config_t **list;
377     size_t count;
378 } config = { NULL, 0 };
379
380 /**
381  * Index the configuration items by name for faster lookups.
382  */
383 int config_SortConfig (void)
384 {
385     size_t nmod;
386     module_t **mlist = module_list_get (&nmod);
387     if (unlikely(mlist == NULL))
388         return VLC_ENOMEM;
389
390     size_t nconf = 0;
391     for (size_t i = 0; i < nmod; i++)
392          nconf  += mlist[i]->confsize;
393
394     module_config_t **clist = malloc (sizeof (*clist) * nconf);
395     if (unlikely(clist == NULL))
396     {
397         module_list_free (mlist);
398         return VLC_ENOMEM;
399     }
400
401     nconf = 0;
402     for (size_t i = 0; i < nmod; i++)
403     {
404         module_t *parser = mlist[i];
405         module_config_t *item, *end;
406
407         for (item = parser->p_config, end = item + parser->confsize;
408              item < end;
409              item++)
410         {
411             if (item->i_type & CONFIG_HINT)
412                 continue; /* ignore hints */
413             clist[nconf++] = item;
414         }
415     }
416     module_list_free (mlist);
417
418     qsort (clist, nconf, sizeof (*clist), confcmp);
419
420     config.list = clist;
421     config.count = nconf;
422     return VLC_SUCCESS;
423 }
424
425 void config_UnsortConfig (void)
426 {
427     module_config_t **clist;
428
429     clist = config.list;
430     config.list = NULL;
431     config.count = 0;
432
433     free (clist);
434 }
435
436 /*****************************************************************************
437  * config_FindConfig: find the config structure associated with an option.
438  *****************************************************************************
439  * FIXME: remove p_this pointer parameter (or use it)
440  *****************************************************************************/
441 module_config_t *config_FindConfig (vlc_object_t *p_this, const char *name)
442 {
443     VLC_UNUSED(p_this);
444
445     if (unlikely(name == NULL))
446         return NULL;
447
448     module_config_t *const *p;
449     p = bsearch (name, config.list, config.count, sizeof (*p), confnamecmp);
450     return p ? *p : NULL;
451 }
452
453 /*****************************************************************************
454  * config_Free: frees a duplicated module's configuration data.
455  *****************************************************************************
456  * This function frees all the data duplicated by config_Duplicate.
457  *****************************************************************************/
458 void config_Free( module_t *p_module )
459 {
460     int i;
461
462     for (size_t j = 0; j < p_module->confsize; j++)
463     {
464         module_config_t *p_item = p_module->p_config + j;
465
466         free( p_item->psz_type );
467         free( p_item->psz_name );
468         free( p_item->psz_text );
469         free( p_item->psz_longtext );
470         free( p_item->psz_oldname );
471
472         if (IsConfigStringType (p_item->i_type))
473         {
474             free (p_item->value.psz);
475             free (p_item->orig.psz);
476             free (p_item->saved.psz);
477         }
478
479         if( p_item->ppsz_list )
480             for( i = 0; i < p_item->i_list; i++ )
481                 free( p_item->ppsz_list[i] );
482         if( p_item->ppsz_list_text )
483             for( i = 0; i < p_item->i_list; i++ )
484                 free( p_item->ppsz_list_text[i] );
485         free( p_item->ppsz_list );
486         free( p_item->ppsz_list_text );
487         free( p_item->pi_list );
488
489         if( p_item->i_action )
490         {
491             for( i = 0; i < p_item->i_action; i++ )
492             {
493                 free( p_item->ppsz_action_text[i] );
494             }
495             free( p_item->ppf_action );
496             free( p_item->ppsz_action_text );
497         }
498     }
499
500     free (p_module->p_config);
501     p_module->p_config = NULL;
502 }
503
504 #undef config_ResetAll
505 /*****************************************************************************
506  * config_ResetAll: reset the configuration data for all the modules.
507  *****************************************************************************/
508 void config_ResetAll( vlc_object_t *p_this )
509 {
510     VLC_UNUSED(p_this);
511     module_t *p_module;
512     module_t **list = module_list_get (NULL);
513
514     vlc_rwlock_wrlock (&config_lock);
515     for (size_t j = 0; (p_module = list[j]) != NULL; j++)
516     {
517         if( p_module->b_submodule ) continue;
518
519         for (size_t i = 0; i < p_module->confsize; i++ )
520         {
521             module_config_t *p_config = p_module->p_config + i;
522
523             if (IsConfigIntegerType (p_config->i_type))
524                 p_config->value.i = p_config->orig.i;
525             else
526             if (IsConfigFloatType (p_config->i_type))
527                 p_config->value.f = p_config->orig.f;
528             else
529             if (IsConfigStringType (p_config->i_type))
530             {
531                 free ((char *)p_config->value.psz);
532                 p_config->value.psz =
533                         strdupnull (p_config->orig.psz);
534             }
535         }
536     }
537     vlc_rwlock_unlock (&config_lock);
538
539     module_list_free (list);
540 }