modules.c 42.7 KB
Newer Older
Sam Hocevar's avatar
   
Sam Hocevar committed
1
/*****************************************************************************
2
 * modules.c : Builtin and plugin modules management functions
Sam Hocevar's avatar
   
Sam Hocevar committed
3
 *****************************************************************************
4
 * Copyright (C) 2001-2007 the VideoLAN team
5
 * $Id$
Sam Hocevar's avatar
   
Sam Hocevar committed
6
 *
7
 * Authors: Sam Hocevar <sam@zoy.org>
Sam Hocevar's avatar
   
Sam Hocevar committed
8
 *          Ethan C. Baldridge <BaldridgeE@cadmus.com>
Sam Hocevar's avatar
   
Sam Hocevar committed
9
 *          Hans-Peter Jansen <hpj@urpla.net>
10
 *          Gildas Bazin <gbazin@videolan.org>
Sam Hocevar's avatar
   
Sam Hocevar committed
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Sam Hocevar's avatar
   
Sam Hocevar committed
25
26
 *****************************************************************************/

27
28
29
30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
33
#include "libvlc.h"
34

Sam Hocevar's avatar
   
Sam Hocevar committed
35
36
37
38
39
40
/* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
 * is set to 64. Don't try to be cleverer. */
#ifdef _FILE_OFFSET_BITS
#undef _FILE_OFFSET_BITS
#endif

Sam Hocevar's avatar
   
Sam Hocevar committed
41
42
#include <stdlib.h>                                      /* free(), strtol() */
#include <stdio.h>                                              /* sprintf() */
Sam Hocevar's avatar
   
Sam Hocevar committed
43
#include <string.h>                                              /* strdup() */
44
#include <assert.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
45

46
47
48
49
#ifdef HAVE_DIRENT_H
#   include <dirent.h>
#endif

50
51
52
53
54
55
#ifdef HAVE_SYS_TYPES_H
#   include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
56
57
58
#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
59

60
61
62
#if !defined(HAVE_DYNAMIC_PLUGINS)
    /* no support for plugins */
#elif defined(HAVE_DL_DYLD)
Sam Hocevar's avatar
Sam Hocevar committed
63
64
65
66
67
68
69
70
#   if defined(HAVE_MACH_O_DYLD_H)
#       include <mach-o/dyld.h>
#   endif
#elif defined(HAVE_DL_BEOS)
#   if defined(HAVE_IMAGE_H)
#       include <image.h>
#   endif
#elif defined(HAVE_DL_WINDOWS)
Sam Hocevar's avatar
Sam Hocevar committed
71
#   include <windows.h>
Sam Hocevar's avatar
Sam Hocevar committed
72
73
#elif defined(HAVE_DL_DLOPEN)
#   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
Sam Hocevar's avatar
Sam Hocevar committed
74
75
#       include <dlfcn.h>
#   endif
Sam Hocevar's avatar
Sam Hocevar committed
76
#   if defined(HAVE_SYS_DL_H)
Sam Hocevar's avatar
Sam Hocevar committed
77
78
#       include <sys/dl.h>
#   endif
Sam Hocevar's avatar
Sam Hocevar committed
79
80
#elif defined(HAVE_DL_SHL_LOAD)
#   if defined(HAVE_DL_H)
Sam Hocevar's avatar
Sam Hocevar committed
81
82
#       include <dl.h>
#   endif
Sam Hocevar's avatar
   
Sam Hocevar committed
83
84
#endif

85
#include "config/configuration.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
86

zorglub's avatar
zorglub committed
87
#include "vlc_charset.h"
88
#include "vlc_arrays.h"
gbazin's avatar
   
gbazin committed
89

90
91
#include "modules/modules.h"
#include "modules/builtin.h"
zorglub's avatar
zorglub committed
92

93
94
module_bank_t *p_module_bank;

Sam Hocevar's avatar
   
Sam Hocevar committed
95
96
97
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
98
#ifdef HAVE_DYNAMIC_PLUGINS
99
100
101
102
static void AllocateAllPlugins  ( vlc_object_t * );
static void AllocatePluginDir   ( vlc_object_t *, const char *, int );
static int  AllocatePluginFile  ( vlc_object_t *, char *, int64_t, int64_t );
static module_t * AllocatePlugin( vlc_object_t *, char * );
Sam Hocevar's avatar
   
Sam Hocevar committed
103
#endif
104
static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
105
static void DeleteModule ( module_t *, bool );
Sam Hocevar's avatar
   
Sam Hocevar committed
106
#ifdef HAVE_DYNAMIC_PLUGINS
107
108
static void   DupModule        ( module_t * );
static void   UndupModule      ( module_t * );
109
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
110

111
112
113
114
/**
 * Init bank
 *
 * Creates a module bank structure which will be filled later
gbazin's avatar
   
gbazin committed
115
 * on with all the modules found.
116
117
118
 * \param p_this vlc object structure
 * \return nothing
 */
119
void __module_InitBank( vlc_object_t *p_this )
Sam Hocevar's avatar
   
Sam Hocevar committed
120
{
121
    module_bank_t *p_bank = NULL;
122

123
    vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
124

125
    if( p_module_bank == NULL )
126
    {
127
        p_bank = calloc (1, sizeof(*p_bank));
128
129
130
131
        p_bank->i_usage = 1;
        p_bank->i_cache = p_bank->i_loaded_cache = 0;
        p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
        p_bank->b_cache = p_bank->b_cache_dirty =
132
        p_bank->b_cache_delete = false;
133
        p_bank->head = NULL;
134
135

        /* Everything worked, attach the object */
136
        p_module_bank = p_bank;
137
138
139
140
141
142
143

        /* Fills the module bank structure with the main module infos.
         * This is very useful as it will allow us to consider the main
         * library just as another module, and for instance the configuration
         * options of main will be available in the module bank structure just
         * as for every other module. */
        AllocateBuiltinModule( p_this, vlc_entry__main );
144
    }
145
    else
146
        p_module_bank->i_usage++;
147

148
    vlc_mutex_unlock( lock );
gbazin's avatar
   
gbazin committed
149
150
}

Sam Hocevar's avatar
   
Sam Hocevar committed
151

152
153
154
155
/**
 * End bank
 *
 * Unloads all unused plugin modules and empties the module
Sam Hocevar's avatar
   
Sam Hocevar committed
156
 * bank in case of success.
157
158
159
 * \param p_this vlc object structure
 * \return nothing
 */
160
void __module_EndBank( vlc_object_t *p_this )
Sam Hocevar's avatar
   
Sam Hocevar committed
161
{
162
    module_bank_t *p_bank;
163

164
    vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
165
166
167
    p_bank = p_module_bank;
    assert (p_bank != NULL);
    if( --p_bank->i_usage > 0 )
168
    {
169
        vlc_mutex_unlock( lock );
170
171
        return;
    }
172
173
    /*FIXME: For thread safety, we need to:
    p_module_bank = NULL; - immediately, but that will crash the cache */
174
    vlc_mutex_unlock( lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
175

176
    /* Save the configuration */
177
178
    config_AutoSaveConfigFile( p_this );

179
#ifdef HAVE_DYNAMIC_PLUGINS
180
181
182
    if( p_bank->b_cache ) CacheSave( p_this );
    while( p_bank->i_loaded_cache-- )
    {
183
184
        if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
        {
185
186
187
            DeleteModule(
                    p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module,
                    p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
188
189
190
191
            free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
            free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
            p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
        }
192
    }
193
    if( p_bank->pp_loaded_cache )
194
    {
195
        free( p_bank->pp_loaded_cache );
196
197
        p_bank->pp_loaded_cache = NULL;
    }
198
199
200
201
    while( p_bank->i_cache-- )
    {
        free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
        free( p_bank->pp_cache[p_bank->i_cache] );
202
        p_bank->pp_cache[p_bank->i_cache] = NULL;
203
    }
204
    if( p_bank->pp_cache )
205
    {
206
        free( p_bank->pp_cache );
207
208
        p_bank->pp_cache = NULL;
    }
209
210
#endif

211
212
    while( p_bank->head != NULL )
        DeleteModule( p_bank->head, true );
Sam Hocevar's avatar
   
Sam Hocevar committed
213

214
215
    p_module_bank = NULL; /* FIXME: do this inside the lock */
    free( p_bank );
Sam Hocevar's avatar
   
Sam Hocevar committed
216
217
}

218
219
220
221
222
223
224
/**
 * Load all modules which we built with.
 *
 * Fills the module bank structure with the builtin modules.
 * \param p_this vlc object structure
 * \return nothing
 */
225
void __module_LoadBuiltins( vlc_object_t * p_this )
226
{
227
    vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
228
    if( p_module_bank->b_builtins )
229
    {
230
        vlc_mutex_unlock( lock );
231
232
        return;
    }
233
    p_module_bank->b_builtins = true;
234
    vlc_mutex_unlock( lock );
235

236
237
238
239
    msg_Dbg( p_this, "checking builtin modules" );
    ALLOCATE_ALL_BUILTINS();
}

240
241
242
243
244
245
246
247
/**
 * Load all plugins
 *
 * Load all plugin modules we can find.
 * Fills the module bank structure with the plugin modules.
 * \param p_this vlc object structure
 * \return nothing
 */
248
void __module_LoadPlugins( vlc_object_t * p_this )
249
250
{
#ifdef HAVE_DYNAMIC_PLUGINS
251
    vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
252
    if( p_module_bank->b_plugins )
253
    {
254
        vlc_mutex_unlock( lock );
255
256
        return;
    }
257
    p_module_bank->b_plugins = true;
258
    vlc_mutex_unlock( lock );
259

260
    msg_Dbg( p_this, "checking plugin modules" );
261
262

    if( config_GetInt( p_this, "plugins-cache" ) )
263
        p_module_bank->b_cache = true;
264

265
266
    if( p_module_bank->b_cache ||
        p_module_bank->b_cache_delete ) CacheLoad( p_this );
267

268
269
    AllocateAllPlugins( p_this );
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
270
271
}

272
273
274
275
276
277
278
/**
 * Checks whether a module implements a capability.
 *
 * \param m the module
 * \param cap the capability to check
 * \return TRUE if the module have the capability
 */
279
bool module_provides( const module_t *m, const char *cap )
280
281
282
283
{
    return !strcmp( m->psz_capability, cap );
}

284
285
286
287
288
289
/**
 * Get the internal name of a module
 *
 * \param m the module
 * \return the module name
 */
290
const char *module_get_object( const module_t *m )
291
292
293
294
{
    return m->psz_object_name;
}

295
296
297
298
299
300
301
/**
 * Get the human-friendly name of a module.
 *
 * \param m the module
 * \param long_name TRUE to have the long name of the module
 * \return the short or long name of the module
 */
302
const char *module_get_name( const module_t *m, bool long_name )
303
304
305
{
    if( long_name && ( m->psz_longname != NULL) )
        return m->psz_longname;
306

307
308
309
    return m->psz_shortname ?: m->psz_object_name;
}

310
311
312
313
314
315
/**
 * Get the help for a module
 *
 * \param m the module
 * \return the help
 */
316
const char *module_get_help( const module_t *m )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
317
318
319
320
{
    return m->psz_help;
}

321
322
323
324
325
326
327
328
329
330
331
module_t *module_hold (module_t *m)
{
    vlc_hold (&m->vlc_gc_data);
    return m;
}

void module_release (module_t *m)
{
    vlc_release (&m->vlc_gc_data);
}

332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
 * Frees the flat list of VLC modules.
 * @param list list obtained by module_list_get
 * @param length number of items on the list
 * @return nothing.
 */
void module_list_free (module_t **list)
{
    if (list == NULL)
        return;

    for (size_t i = 0; list[i] != NULL; i++)
         module_release (list[i]);
    free (list);
}

/**
 * Gets the flat list of VLC modules.
 * @param n [OUT] pointer to the number of modules or NULL
 * @return NULL-terminated table of module pointers
 *         (release with module_list_free()), or NULL in case of error.
 */
module_t **module_list_get (size_t *n)
{
    /* TODO: this whole module lookup is quite inefficient */
357
    /* Remove this and improve module_need */
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    module_t **tab = NULL;
    size_t i = 0;

    for (module_t *mod = p_module_bank->head; mod; mod = mod->next)
    {
         module_t **nt;
         nt  = realloc (tab, (i + 2 + mod->submodule_count) * sizeof (*tab));
         if (nt == NULL)
         {
             module_list_free (tab);
             return NULL;
         }

         tab = nt;
         tab[i++] = module_hold (mod);
         for (module_t *subm = mod->submodule; subm; subm = subm->next)
             tab[i++] = module_hold (subm);
         tab[i] = NULL;
    }
    if (n != NULL)
        *n = i;
    return tab;
}

382
383
384
385
/**
 * module Need
 *
 * Return the best module function, given a capability list.
386
387
388
389
390
391
 *
 * If the p_this object doesn't have it's psz_object_name set, then
 * psz_object_name will be set to the module's name, unless the user
 * provided an alias using the "module name@alias" syntax in which case
 * psz_object_name will be set to the alias.
 *
392
393
394
395
396
397
 * \param p_this the vlc object
 * \param psz_capability list of capabilities needed
 * \param psz_name name of the module asked
 * \param b_strict TRUE yto use the strict mode
 * \return the module or NULL in case of a failure
 */
398
module_t * __module_need( vlc_object_t *p_this, const char *psz_capability,
399
                          const char *psz_name, bool b_strict )
Sam Hocevar's avatar
   
Sam Hocevar committed
400
{
401
    typedef struct module_list_t module_list_t;
402

403
    stats_TimerStart( p_this, "module_need()", STATS_TIMER_MODULE_NEED );
404

405
    struct module_list_t
Sam Hocevar's avatar
   
Sam Hocevar committed
406
    {
407
        module_t *p_module;
408
        int i_score;
409
        bool b_force;
410
411
412
413
        module_list_t *p_next;
    };

    module_list_t *p_list, *p_first, *p_tmp;
Sam Hocevar's avatar
   
Sam Hocevar committed
414

gbazin's avatar
   
gbazin committed
415
    int i_which_module, i_index = 0;
Sam Hocevar's avatar
   
Sam Hocevar committed
416
417

    module_t *p_module;
418

419
    int   i_shortcuts = 0;
420
    char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
421
    bool b_force_backup = p_this->b_force;
Sam Hocevar's avatar
   
Sam Hocevar committed
422

423

424
425
426
    /* Deal with variables */
    if( psz_name && psz_name[0] == '$' )
    {
dionoea's avatar
dionoea committed
427
        psz_name = psz_var = var_CreateGetString( p_this, psz_name + 1 );
428
429
    }

430
    /* Count how many different shortcuts were asked for */
431
    if( psz_name && *psz_name )
Sam Hocevar's avatar
   
Sam Hocevar committed
432
    {
gbazin's avatar
   
gbazin committed
433
        char *psz_parser, *psz_last_shortcut;
434

435
436
437
        /* If the user wants none, give him none. */
        if( !strcmp( psz_name, "none" ) )
        {
438
            free( psz_var );
439
440
441
            stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
            stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
            stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
442
443
444
            return NULL;
        }

445
        i_shortcuts++;
gbazin's avatar
   
gbazin committed
446
        psz_shortcuts = psz_last_shortcut = strdup( psz_name );
447
448

        for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
Sam Hocevar's avatar
   
Sam Hocevar committed
449
        {
450
            if( *psz_parser == ',' )
Sam Hocevar's avatar
   
Sam Hocevar committed
451
            {
452
453
                 *psz_parser = '\0';
                 i_shortcuts++;
gbazin's avatar
   
gbazin committed
454
                 psz_last_shortcut = psz_parser + 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
455
            }
Sam Hocevar's avatar
   
Sam Hocevar committed
456
        }
gbazin's avatar
   
gbazin committed
457
458
459
460

        /* Check if the user wants to override the "strict" mode */
        if( psz_last_shortcut )
        {
461
462
            if( !strcmp(psz_last_shortcut, "none") )
            {
463
                b_strict = true;
464
465
466
467
                i_shortcuts--;
            }
            else if( !strcmp(psz_last_shortcut, "any") )
            {
468
                b_strict = false;
469
470
                i_shortcuts--;
            }
gbazin's avatar
   
gbazin committed
471
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
472
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
473

Sam Hocevar's avatar
   
Sam Hocevar committed
474
    /* Sort the modules and test them */
475
476
477
    size_t count;
    module_t **p_all = module_list_get (&count);
    p_list = malloc( count * sizeof( module_list_t ) );
Sam Hocevar's avatar
   
Sam Hocevar committed
478
    p_first = NULL;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
479
    unsigned i_cpu = vlc_CPU();
Sam Hocevar's avatar
   
Sam Hocevar committed
480

Sam Hocevar's avatar
   
Sam Hocevar committed
481
    /* Parse the module list for capabilities and probe each of them */
482
    for( i_which_module = 0; i_which_module < count; i_which_module++ )
Sam Hocevar's avatar
   
Sam Hocevar committed
483
    {
484
        int i_shortcut_bonus = 0;
485
        p_module = p_all[i_which_module];
486

487
        /* Test that this module can do what we need */
488
        if( !module_provides( p_module, psz_capability ) )
Sam Hocevar's avatar
   
Sam Hocevar committed
489
        {
490
491
492
            /* Don't recurse through the sub-modules because vlc_list_find()
             * will list them anyway. */
            continue;
Sam Hocevar's avatar
   
Sam Hocevar committed
493
494
        }

Sam Hocevar's avatar
   
Sam Hocevar committed
495
        /* Test if we have the required CPU */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
496
        if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
Sam Hocevar's avatar
   
Sam Hocevar committed
497
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
498
            continue;
Sam Hocevar's avatar
   
Sam Hocevar committed
499
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
500

501
        /* If we required a shortcut, check this plugin provides it. */
502
        if( i_shortcuts > 0 )
Sam Hocevar's avatar
   
Sam Hocevar committed
503
        {
504
            bool b_trash;
505
            const char *psz_name = psz_shortcuts;
Sam Hocevar's avatar
   
Sam Hocevar committed
506

507
            /* Let's drop modules with a <= 0 score (unless they are
gbazin's avatar
   
gbazin committed
508
             * explicitly requested) */
509
            b_trash = p_module->i_score <= 0;
gbazin's avatar
   
gbazin committed
510

511
            for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
Sam Hocevar's avatar
   
Sam Hocevar committed
512
            {
513
                for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
514
                {
515
516
517
518
519
                    char *c;
                    if( ( c = strchr( psz_name, '@' ) )
                        ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
                                        c-psz_name )
                        : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
gbazin's avatar
   
gbazin committed
520
521
                    {
                        /* Found it */
522
523
                        if( c && c[1] )
                            psz_alias = c+1;
gbazin's avatar
   
gbazin committed
524
                        i_shortcut_bonus = i_short * 10000;
525
                        goto found_shortcut;
gbazin's avatar
   
gbazin committed
526
                    }
527
528
                }

529
                /* Go to the next shortcut... This is so lame! */
530
                psz_name += strlen( psz_name ) + 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
531
            }
Sam Hocevar's avatar
   
Sam Hocevar committed
532

533
534
535
            /* If we are in "strict" mode and we couldn't
             * find the module in the list of provided shortcuts,
             * then kick the bastard out of here!!! */
536
            if( b_strict )
Sam Hocevar's avatar
   
Sam Hocevar committed
537
                continue;
Sam Hocevar's avatar
   
Sam Hocevar committed
538
        }
539
540
        /* If we didn't require a shortcut, trash <= 0 scored plugins */
        else if( p_module->i_score <= 0 )
541
542
543
        {
            continue;
        }
544

545
546
found_shortcut:

Sam Hocevar's avatar
   
Sam Hocevar committed
547
548
        /* Store this new module */
        p_list[ i_index ].p_module = p_module;
549
        p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
550
        p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
Sam Hocevar's avatar
   
Sam Hocevar committed
551

Sam Hocevar's avatar
   
Sam Hocevar committed
552
        /* Add it to the modules-to-probe list */
Sam Hocevar's avatar
   
Sam Hocevar committed
553
554
        if( i_index == 0 )
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
555
            p_list[ 0 ].p_next = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
556
557
558
559
560
561
562
            p_first = p_list;
        }
        else
        {
            /* Ok, so at school you learned that quicksort is quick, and
             * bubble sort sucks raw eggs. But that's when dealing with
             * thousands of items. Here we have barely 50. */
563
            module_list_t *p_newlist = p_first;
Sam Hocevar's avatar
   
Sam Hocevar committed
564

565
            if( p_first->i_score < p_list[ i_index ].i_score )
Sam Hocevar's avatar
   
Sam Hocevar committed
566
            {
Sam Hocevar's avatar
   
Sam Hocevar committed
567
568
                p_list[ i_index ].p_next = p_first;
                p_first = &p_list[ i_index ];
Sam Hocevar's avatar
   
Sam Hocevar committed
569
570
            }
            else
571
            {
572
573
                while( p_newlist->p_next != NULL &&
                    p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
Sam Hocevar's avatar
   
Sam Hocevar committed
574
                {
Sam Hocevar's avatar
   
Sam Hocevar committed
575
                    p_newlist = p_newlist->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
576
577
                }

Sam Hocevar's avatar
   
Sam Hocevar committed
578
579
580
                p_list[ i_index ].p_next = p_newlist->p_next;
                p_newlist->p_next = &p_list[ i_index ];
            }
Sam Hocevar's avatar
   
Sam Hocevar committed
581
582
        }

Sam Hocevar's avatar
   
Sam Hocevar committed
583
584
        i_index++;
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
585

zorglub's avatar
zorglub committed
586
587
    msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
                                            i_index, i_index == 1 ? "" : "s" );
588

589
    /* Lock all candidate modules */
590
591
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
592
    {
593
        module_hold( p_tmp->p_module );
594
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
595
596
    }

597
    /* We can release the list, interesting modules were held */
598
    module_list_free (p_all);
Sam Hocevar's avatar
   
Sam Hocevar committed
599

Sam Hocevar's avatar
   
Sam Hocevar committed
600
    /* Parse the linked list and use the first successful module */
601
602
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
603
    {
604
605
#ifdef HAVE_DYNAMIC_PLUGINS
        /* Make sure the module is loaded in mem */
606
607
        module_t *p_module = p_tmp->p_module;
        if( p_module->b_submodule )
608
            p_module = p_module->parent;
609

610
611
612
613
614
615
616
        if( !p_module->b_builtin && !p_module->b_loaded )
        {
            module_t *p_new_module =
                AllocatePlugin( p_this, p_module->psz_filename );
            if( p_new_module )
            {
                CacheMerge( p_this, p_module, p_new_module );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
617
                DeleteModule( p_new_module, false );
618
619
620
621
            }
        }
#endif

622
        p_this->b_force = p_tmp->b_force;
623
624
        if( p_tmp->p_module->pf_activate
             && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
Sam Hocevar's avatar
   
Sam Hocevar committed
625
626
        {
            break;
627
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
628

629
        module_release( p_tmp->p_module );
630
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
631
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
632

Sam Hocevar's avatar
   
Sam Hocevar committed
633
    /* Store the locked module value */
634
    if( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
635
    {
636
637
        p_module = p_tmp->p_module;
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
638
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
639
640
641
642
    else
    {
        p_module = NULL;
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
643

Sam Hocevar's avatar
   
Sam Hocevar committed
644
    /* Unlock the remaining modules */
645
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
646
    {
647
        module_release( p_tmp->p_module );
648
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
649
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
650

Sam Hocevar's avatar
   
Sam Hocevar committed
651
    free( p_list );
652
    p_this->b_force = b_force_backup;
Sam Hocevar's avatar
   
Sam Hocevar committed
653

Sam Hocevar's avatar
   
Sam Hocevar committed
654
    if( p_module != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
655
    {
656
        msg_Dbg( p_this, "using %s module \"%s\"",
657
                 psz_capability, p_module->psz_object_name );
658
659
660
    }
    else if( p_first == NULL )
    {
661
662
663
        if( !strcmp( psz_capability, "access_demux" ) )
        {
            msg_Warn( p_this, "no %s module matched \"%s\"",
664
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
665
666
        }
        else
zorglub's avatar
zorglub committed
667
        {
668
669
            msg_Err( p_this, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
670
671
672

            msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
673
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
674
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
675
676
    else if( psz_name != NULL && *psz_name )
    {
Christophe Massiot's avatar
Christophe Massiot committed
677
678
        msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
Sam Hocevar's avatar
   
Sam Hocevar committed
679
    }
680
681
    else
        msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
Sam Hocevar's avatar
   
Sam Hocevar committed
682

683
684
    if( p_module && !p_this->psz_object_name )
    {
685
686
687
        /* This assumes that p_this is the object which will be using the
         * module. That's not always the case ... but it is in most cases.
         */
dionoea's avatar
dionoea committed
688
689
690
691
        if( psz_alias )
            p_this->psz_object_name = strdup( psz_alias );
        else
            p_this->psz_object_name = strdup( p_module->psz_object_name );
692
    }
693

694
695
    free( psz_shortcuts );
    free( psz_var );
696

697
698
699
700
    stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
    stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
    stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );

Sam Hocevar's avatar
   
Sam Hocevar committed
701
    /* Don't forget that the module is still locked */
702
    return p_module;
Sam Hocevar's avatar
   
Sam Hocevar committed
703
704
}

705
706
707
/**
 * Module unneed
 *
708
 * This function must be called by the thread that called module_need, to
709
 * decrease the reference count and allow for hiding of modules.
710
711
712
713
 * \param p_this vlc object structure
 * \param p_module the module structure
 * \return nothing
 */
714
void __module_unneed( vlc_object_t * p_this, module_t * p_module )
Sam Hocevar's avatar
   
Sam Hocevar committed
715
{
716
717
718
719
720
721
    /* Use the close method */
    if( p_module->pf_deactivate )
    {
        p_module->pf_deactivate( p_this );
    }

722
    msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
Sam Hocevar's avatar
   
Sam Hocevar committed
723

724
    module_release( p_module );
Sam Hocevar's avatar
   
Sam Hocevar committed
725
726
}

727
728
729
730
731
732
733
/**
 * Get a pointer to a module_t given it's name.
 *
 * \param p_this vlc object structure
 * \param psz_name the name of the module
 * \return a pointer to the module or NULL in case of a failure
 */
734
module_t *__module_find( vlc_object_t *p_this, const char * psz_name )
735
{
736
737
738
739
740
741
742
    module_t **list, *module;

    list = module_list_get (NULL);
    if (!list)
        return NULL;

    for (size_t i = 0; (module = list[i]) != NULL; i++)
743
    {
744
745
        const char *psz_module_name = module->psz_object_name;

746
        if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
747
        {
748
749
            module_hold (module);
            break;
750
751
        }
    }
752
753
    module_list_free (list);
    return module;
754
755
}

756
757
758
759
760
761
762
/**
 * Tell if a module exists and release it in thic case
 *
 * \param p_this vlc object structure
 * \param psz_name th name of the module
 * \return TRUE if the module exists
 */
763
bool __module_exists( vlc_object_t *p_this, const char * psz_name )
764
{
765
    module_t *p_module = __module_find( p_this, psz_name );
766
    if( p_module )
767
768
        module_release (p_module);
    return true != NULL;
769
770
}

771
772
773
774
775
/**
 * GetModuleNamesForCapability
 *
 * Return a NULL terminated array with the names of the modules
 * that have a certain capability.
776
 * Free after uses both the string and the table.
777
778
779
780
781
782
 * \param p_this vlc object structure
 * \param psz_capability the capability asked
 * \param pppsz_longname an pointer to an array of string to contain
    the long names of the modules. If set to NULL the function don't use it.
 * \return the NULL terminated array
 */
783
char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
784
                                               const char *psz_capability,
785
                                               char ***pppsz_longname )
786
{
787
    size_t count = 0;
788
    char **psz_ret;
789

790
791
    module_t **list = module_list_get (NULL);

792
    /* Do it in two passes : count the number of modules before */
793
    for (size_t i = 0; list[i]; i++)
794
    {
795
        module_t *p_module = list[i];
796
        const char *psz_module_capability = p_module->psz_capability;
797

798
799
800
        if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
            count++;
    }
801

802
    psz_ret = malloc( sizeof(char*) * (count+1) );
803
804
    if( pppsz_longname )
        *pppsz_longname = malloc( sizeof(char*) * (count+1) );
805
806
807
808
809
    if( !psz_ret || ( pppsz_longname && *pppsz_longname == NULL ) )
    {
        free( psz_ret );
        free( *pppsz_longname );
        *pppsz_longname = NULL;
810
        module_list_free (list);
811
812
813
        return NULL;
    }

814
    for (size_t i = 0, j = 0; list[i]; i++)
815
    {
816
        module_t *p_module = list[i];
817
        const char *psz_module_capability = p_module->psz_capability;
818

819
        if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
820
821
822
823
824
825
826
827
828
        {
            int k = -1; /* hack to handle submodules properly */
            if( p_module->b_submodule )
            {
                while( p_module->pp_shortcuts[++k] != NULL );
                k--;
            }
            psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
                                     :p_module->psz_object_name );
829
            if( pppsz_longname )
830
                (*pppsz_longname)[j] = strdup( module_get_name( p_module, true ) );
831
832
            j++;
        }
833
834
835
    }
    psz_ret[count] = NULL;

836
    module_list_free (list);
837

838
839
840
    return psz_ret;
}

841
842
843
844
845
846
847
/**
 * Get the configuration of a module
 *
 * \param module the module
 * \param psize the size of the configuration returned
 * \return the configuration as an array
 */
848
module_config_t *module_config_get( const module_t *module, unsigned *restrict psize )
849
{
850
    unsigned i,j;
851
    unsigned size = module->confsize;
852
    module_config_t *config = malloc( size * sizeof( *config ) );
853

854
    assert( psize != NULL );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
855
    *psize = 0;
856

857
858
859
    if( !config )
        return NULL;

860
    for( i = 0, j = 0; i < size; i++ )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
861
    {
862
        const module_config_t *item = module->p_config + i;
863
        if( item->b_internal /* internal option */
864
         || item->b_unsaveable /* non-modifiable option */
865
         || item->b_removed /* removed option */ )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
866
867
            continue;

868
        memcpy( config + j, item, sizeof( *config ) );
869
        j++;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
870
    }
871
    *psize = j;
872
873
874
875

    return config;
}

876
877
878
879
880
881
/**
 * Release the configuration
 *
 * \param the configuration
 * \return nothing
 */
882
void module_config_free( module_config_t *config )
883
{
884
    free( config );
885
886
}

Sam Hocevar's avatar
   
Sam Hocevar committed
887
888
889
890
/*****************************************************************************
 * Following functions are local.
 *****************************************************************************/

891
892
893
894
895
896
897
 /*****************************************************************************
 * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
 * return first path.
 *****************************************************************************/
static char * copy_next_paths_token( char * paths, char ** remaining_paths )
{
    char * path;
898
    int i, done;
899
900
901
902
903
    bool escaped = false;

    assert( paths );

    /* Alloc a buffer to store the path */
Rafaël Carré's avatar
Rafaël Carré committed
904
    path = malloc( strlen( paths ) + 1 );
905
906
907
    if( !path ) return NULL;

    /* Look for PATH_SEP_CHAR (a ':' or a ';') */
908
909
    for( i = 0, done = 0 ; paths[i]; i++ )
    {
910
        /* Take care of \\ and \: or \; escapement */
911
912
        if( escaped )
        {
913
            escaped = false;
914
            path[done++] = paths[i];
915
        }
916
917
918
919
#ifdef WIN32
        else if( paths[i] == '/' )
            escaped = true;
#else
920
921
        else if( paths[i] == '\\' )
            escaped = true;
922
#endif
923
924
925
        else if( paths[i] == PATH_SEP_CHAR )
            break;
        else
926
            path[done++] = paths[i];
927
    }
928
    path[done++] = 0;
929
930