]> git.sesse.net Git - vlc/blob - src/modules/entry.c
Fix use of uninitialized value.
[vlc] / src / modules / entry.c
1 /*****************************************************************************
2  * entry.c : Callbacks for module entry point
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * Copyright © 2007-2008 Rémi Denis-Courmont
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <vlc_common.h>
27 #include <vlc_plugin.h>
28 #include <vlc_memory.h>
29 #include <assert.h>
30 #include <stdarg.h>
31
32 #include "modules/modules.h"
33 #include "config/configuration.h"
34 #include "libvlc.h"
35
36 static void vlc_module_destruct (gc_object_t *obj)
37 {
38     module_t *module = vlc_priv (obj, module_t);
39
40     free (module->psz_object_name);
41     free (module);
42 }
43
44 static const char default_name[] = "unnamed";
45
46 module_t *vlc_module_create (vlc_object_t *obj)
47 {
48     module_t *module = malloc (sizeof (*module));
49     if (module == NULL)
50         return NULL;
51
52     module->psz_object_name = strdup( default_name );
53     module->next = NULL;
54     module->submodule = NULL;
55     module->parent = NULL;
56     module->submodule_count = 0;
57     vlc_gc_init (module, vlc_module_destruct);
58
59     module->psz_shortname = NULL;
60     module->psz_longname = (char*)default_name;
61     module->psz_help = NULL;
62     for (unsigned i = 0; i < MODULE_SHORTCUT_MAX; i++)
63         module->pp_shortcuts[i] = NULL;
64     module->psz_capability = (char*)"";
65     module->i_score = 1;
66     module->b_unloadable = true;
67     module->b_submodule = false;
68     module->pf_activate = NULL;
69     module->pf_deactivate = NULL;
70     module->p_config = NULL;
71     module->confsize = 0;
72     module->i_config_items = 0;
73     module->i_bool_items = 0;
74     /*module->handle = garbage */
75     module->psz_filename = NULL;
76     module->domain = NULL;
77     module->b_builtin = false;
78     module->b_loaded = false;
79
80     (void)obj;
81     return module;
82 }
83
84
85 static void vlc_submodule_destruct (gc_object_t *obj)
86 {
87     module_t *module = vlc_priv (obj, module_t);
88     free (module->psz_object_name);
89     free (module);
90 }
91
92 module_t *vlc_submodule_create (module_t *module)
93 {
94     assert (module != NULL);
95
96     module_t *submodule = calloc( 1, sizeof(*submodule) );
97     if( !submodule )
98         return NULL;
99
100     vlc_gc_init (submodule, vlc_submodule_destruct);
101
102     submodule->next = module->submodule;
103     submodule->parent = module;
104     module->submodule = submodule;
105     module->submodule_count++;
106
107     /* Muahahaha! Heritage! Polymorphism! Ugliness!! */
108     submodule->pp_shortcuts[0] = module->pp_shortcuts[0]; /* object name */
109     for (unsigned i = 1; i < MODULE_SHORTCUT_MAX; i++)
110         submodule->pp_shortcuts[i] = NULL;
111
112     submodule->psz_object_name = strdup( module->psz_object_name );
113     submodule->psz_shortname = module->psz_shortname;
114     submodule->psz_longname = module->psz_longname;
115     submodule->psz_capability = module->psz_capability;
116     submodule->i_score = module->i_score;
117     submodule->b_submodule = true;
118     submodule->domain = module->domain;
119     return submodule;
120 }
121
122 static module_config_t *vlc_config_create (module_t *module, int type)
123 {
124     unsigned confsize = module->confsize;
125     module_config_t *tab = module->p_config;
126
127     if ((confsize & 0xf) == 0)
128     {
129         tab = realloc_or_free (tab, (confsize + 17) * sizeof (*tab));
130         if (tab == NULL)
131             return NULL;
132
133         module->p_config = tab;
134     }
135
136     memset (tab + confsize, 0, sizeof (tab[confsize]));
137     tab[confsize].i_type = type;
138
139     if (type & CONFIG_ITEM)
140     {
141         module->i_config_items++;
142         if (type == CONFIG_ITEM_BOOL)
143             module->i_bool_items++;
144     }
145
146     module->confsize++;
147     return tab + confsize;
148 }
149
150
151 int vlc_plugin_set (module_t *module, module_config_t *item, int propid, ...)
152 {
153     va_list ap;
154     int ret = 0;
155
156     va_start (ap, propid);
157     switch (propid)
158     {
159         case VLC_SUBMODULE_CREATE:
160         {
161             module_t **pp = va_arg (ap, module_t **);
162             *pp = vlc_submodule_create (module);
163             if (*pp == NULL)
164                 ret = -1;
165             break;
166         }
167
168         case VLC_CONFIG_CREATE:
169         {
170             int type = va_arg (ap, int);
171             module_config_t **pp = va_arg (ap, module_config_t **);
172             *pp = vlc_config_create (module, type);
173             if (*pp == NULL)
174                 ret = -1;
175             break;
176         }
177
178         case VLC_MODULE_SHORTCUT:
179         {
180             unsigned i;
181             for (i = 0; module->pp_shortcuts[i] != NULL; i++);
182                 if (i >= (MODULE_SHORTCUT_MAX - 1))
183                     break;
184
185             module->pp_shortcuts[i] = va_arg (ap, char *);
186             break;
187         }
188
189         case VLC_MODULE_CAPABILITY:
190             module->psz_capability = va_arg (ap, char *);
191             break;
192
193         case VLC_MODULE_SCORE:
194             module->i_score = va_arg (ap, int);
195             break;
196
197         case VLC_MODULE_CB_OPEN:
198             module->pf_activate = va_arg (ap, int (*) (vlc_object_t *));
199             break;
200
201         case VLC_MODULE_CB_CLOSE:
202             module->pf_deactivate = va_arg (ap, void (*) (vlc_object_t *));
203             break;
204
205         case VLC_MODULE_NO_UNLOAD:
206             module->b_unloadable = false;
207             break;
208
209         case VLC_MODULE_NAME:
210         {
211             const char *value = va_arg (ap, const char *);
212             free( module->psz_object_name );
213             module->psz_object_name = strdup( value );
214             module->pp_shortcuts[0] = (char*)value; /* dooh! */
215             if (module->psz_longname == default_name)
216                 module->psz_longname = (char*)value; /* dooh! */
217             break;
218         }
219
220         case VLC_MODULE_SHORTNAME:
221             module->psz_shortname = va_arg (ap, char *);
222             break;
223
224         case VLC_MODULE_DESCRIPTION:
225             module->psz_longname = va_arg (ap, char *);
226             break;
227
228         case VLC_MODULE_HELP:
229             module->psz_help = va_arg (ap, char *);
230             break;
231
232         case VLC_MODULE_TEXTDOMAIN:
233             module->domain = va_arg (ap, char *);
234             break;
235
236         case VLC_CONFIG_NAME:
237         {
238             const char *name = va_arg (ap, const char *);
239             vlc_callback_t cb = va_arg (ap, vlc_callback_t);
240
241             assert (name != NULL);
242             item->psz_name = strdup (name);
243             item->pf_callback = cb;
244             break;
245         }
246
247         case VLC_CONFIG_VALUE:
248         {
249             if (IsConfigIntegerType (item->i_type))
250             {
251                 item->orig.i = item->saved.i =
252                 item->value.i = va_arg (ap, int);
253             }
254             else
255             if (IsConfigFloatType (item->i_type))
256             {
257                 item->orig.f = item->saved.f =
258                 item->value.f = va_arg (ap, double);
259             }
260             else
261             if (IsConfigStringType (item->i_type))
262             {
263                 const char *value = va_arg (ap, const char *);
264                 item->value.psz = value ? strdup (value) : NULL;
265                 item->orig.psz = value ? strdup (value) : NULL;
266                 item->saved.psz = value ? strdup (value) : NULL;
267             }
268             break;
269         }
270
271         case VLC_CONFIG_RANGE:
272         {
273             if (IsConfigIntegerType (item->i_type)
274              || item->i_type == CONFIG_ITEM_MODULE_LIST_CAT
275              || item->i_type == CONFIG_ITEM_MODULE_CAT)
276             {
277                 item->min.i = va_arg (ap, int);
278                 item->max.i = va_arg (ap, int);
279             }
280             else
281             if (IsConfigFloatType (item->i_type))
282             {
283                 item->min.f = va_arg (ap, double);
284                 item->max.f = va_arg (ap, double);
285             }
286             break;
287         }
288
289         case VLC_CONFIG_ADVANCED:
290             item->b_advanced = true;
291             break;
292
293         case VLC_CONFIG_VOLATILE:
294             item->b_unsaveable = true;
295             break;
296
297         case VLC_CONFIG_PERSISTENT:
298             item->b_autosave = true;
299             break;
300
301         case VLC_CONFIG_RESTART:
302             item->b_restart = true;
303             break;
304
305         case VLC_CONFIG_PRIVATE:
306             item->b_internal = true;
307             break;
308
309         case VLC_CONFIG_REMOVED:
310             item->b_removed = true;
311             break;
312
313         case VLC_CONFIG_CAPABILITY:
314         {
315             const char *cap = va_arg (ap, const char *);
316             item->psz_type = cap ? strdup (cap) : NULL;
317             break;
318         }
319
320         case VLC_CONFIG_SHORTCUT:
321             item->i_short = va_arg (ap, int);
322             break;
323
324         case VLC_CONFIG_OLDNAME:
325         {
326             const char *oldname = va_arg (ap, const char *);
327             item->psz_oldname = oldname ? strdup (oldname) : NULL;
328             break;
329         }
330
331         case VLC_CONFIG_SAFE:
332             item->b_safe = true;
333             break;
334
335         case VLC_CONFIG_DESC:
336         {
337             const char *text = va_arg (ap, const char *);
338             const char *longtext = va_arg (ap, const char *);
339
340             item->psz_text = text ? strdup (text) : NULL;
341             item->psz_longtext = longtext ? strdup (longtext) : NULL;
342             break;
343         }
344
345         case VLC_CONFIG_LIST:
346         {
347             size_t len = va_arg (ap, size_t);
348
349             /* Copy values */
350             if (IsConfigIntegerType (item->i_type))
351             {
352                 const int *src = va_arg (ap, const int *);
353                 int *dst = malloc (sizeof (int) * (len + 1));
354
355                 if (dst != NULL)
356                 {
357                     memcpy (dst, src, sizeof (int) * len);
358                     dst[len] = 0;
359                 }
360                 item->pi_list = dst;
361             }
362             else
363             if (IsConfigStringType (item->i_type))
364             {
365                 const char *const *src = va_arg (ap, const char *const *);
366                 char **dst = malloc (sizeof (char *) * (len + 1));
367
368                 if (dst != NULL)
369                 {
370                     for (size_t i = 0; i < len; i++)
371                         dst[i] = src[i] ? strdup (src[i]) : NULL;
372                     dst[len] = NULL;
373                 }
374                 item->ppsz_list = dst;
375             }
376             else
377                 break;
378
379             /* Copy textual descriptions */
380             const char *const *text = va_arg (ap, const char *const *);
381             if (text != NULL)
382             {
383                 char **dtext = malloc (sizeof (char *) * (len + 1));
384                 if( dtext != NULL )
385                 {
386                     for (size_t i = 0; i < len; i++)
387                         dtext[i] = text[i] ? strdup (text[i]) : NULL;
388                     dtext[len] = NULL;
389                 }
390                 item->ppsz_list_text = dtext;
391             }
392             else
393                 item->ppsz_list_text = NULL;
394
395             item->i_list = len;
396             item->pf_update_list = va_arg (ap, vlc_callback_t);
397             break;
398         }
399
400         case VLC_CONFIG_ADD_ACTION:
401         {
402             vlc_callback_t cb = va_arg (ap, vlc_callback_t), *tabcb;
403             const char *name = va_arg (ap, const char *);
404             char **tabtext;
405
406             tabcb = realloc (item->ppf_action,
407                              (item->i_action + 2) * sizeof (cb));
408             if (tabcb == NULL)
409                 break;
410             item->ppf_action = tabcb;
411             tabcb[item->i_action] = cb;
412             tabcb[item->i_action + 1] = NULL;
413
414             tabtext = realloc (item->ppsz_action_text,
415                                (item->i_action + 2) * sizeof (name));
416             if (tabtext == NULL)
417                 break;
418             item->ppsz_action_text = tabtext;
419
420             if (name)
421                 tabtext[item->i_action] = strdup (name);
422             else
423                 tabtext[item->i_action] = NULL;
424             tabtext[item->i_action + 1] = NULL;
425
426             item->i_action++;
427             break;
428         }
429
430         default:
431             fprintf (stderr, "LibVLC: unknown module property %d\n", propid);
432             fprintf (stderr, "LibVLC: too old to use this module?\n");
433             ret = -1;
434             break;
435     }
436
437     va_end (ap);
438     return ret;
439 }