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