]> git.sesse.net Git - vlc/blob - modules/misc/lua/vlc.c
lua: copy input options to result of playlist script
[vlc] / modules / misc / lua / vlc.c
1 /*****************************************************************************
2  * vlc.c: Generic lua interface functions
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *          Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifndef  _GNU_SOURCE
29 #   define  _GNU_SOURCE
30 #endif
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <assert.h>
37
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
40 #include <vlc_meta.h>
41 #include <vlc_charset.h>
42 #include <vlc_fs.h>
43 #include <vlc_aout.h>
44 #include <vlc_services_discovery.h>
45 #include <sys/stat.h>
46
47 #include <lua.h>        /* Low level lua C API */
48 #include <lauxlib.h>    /* Higher level C API */
49 #include <lualib.h>     /* Lua libs */
50
51 #include "vlc.h"
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 #define INTF_TEXT N_("Lua interface")
57 #define INTF_LONGTEXT N_("Lua interface module to load")
58
59 #define CONFIG_TEXT N_("Lua interface configuration")
60 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
61 #define HOST_TEXT N_( "Host address" )
62 #define HOST_LONGTEXT N_( \
63     "Address and port the HTTP interface will listen on. It defaults to " \
64     "all network interfaces (0.0.0.0)." \
65     " If you want the HTTP interface to be available only on the local " \
66     "machine, enter 127.0.0.1" )
67 #define SRC_TEXT N_( "Source directory" )
68 #define SRC_LONGTEXT N_( "Source directory" )
69 #define INDEX_TEXT N_( "Directory index" )
70 #define INDEX_LONGTEXT N_( "Allow to build directory index" )
71
72 #define TELNETHOST_TEXT N_( "Host" )
73 #define TELNETHOST_LONGTEXT N_( "This is the host on which the " \
74     "interface will listen. It defaults to all network interfaces (0.0.0.0)." \
75     " If you want this interface to be available only on the local " \
76     "machine, enter \"127.0.0.1\"." )
77 #define TELNETPORT_TEXT N_( "Port" )
78 #define TELNETPORT_LONGTEXT N_( "This is the TCP port on which this " \
79     "interface will listen. It defaults to 4212." )
80 #define TELNETPORT_DEFAULT 4212
81 #define TELNETPWD_TEXT N_( "Password" )
82 #define TELNETPWD_LONGTEXT N_( "A single administration password is used " \
83     "to protect this interface. The default value is \"admin\"." )
84 #define TELNETPWD_DEFAULT "admin"
85 #define RCHOST_TEXT N_("TCP command input")
86 #define RCHOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
87             "You can set the address and port the interface will bind to." )
88
89 static int vlc_sd_probe_Open( vlc_object_t * );
90
91 vlc_module_begin ()
92         set_shortname( N_("Lua Interface Module") )
93         set_description( N_("Interfaces implemented using lua scripts") )
94         add_shortcut( "luaintf" )
95         add_shortcut( "luahttp" )
96         /* add_shortcut( "http" ) */
97         add_shortcut( "luatelnet" )
98         add_shortcut( "telnet" )
99         add_shortcut( "luahotkeys" )
100         /* add_shortcut( "hotkeys" ) */
101         set_capability( "interface", 0 )
102         set_category( CAT_INTERFACE )
103         set_subcategory( SUBCAT_INTERFACE_CONTROL )
104         add_string( "lua-intf", "dummy",
105                     INTF_TEXT, INTF_LONGTEXT, false )
106         add_string( "lua-config", "",
107                     CONFIG_TEXT, CONFIG_LONGTEXT, false )
108         set_section( N_("Lua HTTP"), 0 )
109             add_string ( "http-host", NULL, HOST_TEXT, HOST_LONGTEXT, true )
110             add_string ( "http-src",  NULL, SRC_TEXT,  SRC_LONGTEXT,  true )
111             add_bool   ( "http-index", false, INDEX_TEXT, INDEX_LONGTEXT, true )
112         set_section( N_("Lua RC"), 0 )
113             add_string( "rc-host", NULL, RCHOST_TEXT, RCHOST_LONGTEXT, true )
114         set_section( N_("Lua Telnet"), 0 )
115             add_string( "telnet-host", "localhost", TELNETHOST_TEXT,
116                         TELNETHOST_LONGTEXT, true )
117             add_integer( "telnet-port", TELNETPORT_DEFAULT, TELNETPORT_TEXT,
118                          TELNETPORT_LONGTEXT, true )
119             add_password( "telnet-password", TELNETPWD_DEFAULT, TELNETPWD_TEXT,
120                           TELNETPWD_LONGTEXT, true )
121
122         set_callbacks( Open_LuaIntf, Close_LuaIntf )
123
124     add_submodule ()
125         set_shortname( N_( "Lua Meta Fetcher" ) )
126         set_description( N_("Fetch meta data using lua scripts") )
127         set_capability( "meta fetcher", 10 )
128         set_callbacks( FetchMeta, NULL )
129
130     add_submodule ()
131         set_shortname( N_( "Lua Meta Reader" ) )
132         set_description( N_("Read meta data using lua scripts") )
133         set_capability( "meta reader", 10 )
134         set_callbacks( ReadMeta, NULL )
135
136     add_submodule ()
137         add_shortcut( "luaplaylist" )
138         set_shortname( N_("Lua Playlist") )
139         set_description( N_("Lua Playlist Parser Interface") )
140         set_capability( "demux", 2 )
141         set_callbacks( Import_LuaPlaylist, Close_LuaPlaylist )
142
143     add_submodule ()
144         set_description( N_("Lua Interface Module (shortcuts)") )
145         add_shortcut( "luarc" )
146         add_shortcut( "rc" )
147         set_capability( "interface", 25 )
148         set_callbacks( Open_LuaIntf, Close_LuaIntf )
149
150     add_submodule ()
151         set_shortname( N_( "Lua Art" ) )
152         set_description( N_("Fetch artwork using lua scripts") )
153         set_capability( "art finder", 10 )
154         set_callbacks( FindArt, NULL )
155
156     add_submodule ()
157         set_shortname( N_("Lua Extension") )
158         add_shortcut( "luaextension" )
159         set_capability( "extension", 1 )
160         set_callbacks( Open_Extension, Close_Extension )
161
162     add_submodule ()
163         set_description( N_("Lua SD Module") )
164         add_shortcut( "luasd" )
165         set_capability( "services_discovery", 0 )
166         add_string( "lua-sd", "", NULL, NULL, false )
167             change_volatile()
168         add_string( "lua-longname", "", NULL, NULL, false )
169             change_volatile()
170         set_callbacks( Open_LuaSD, Close_LuaSD )
171
172     add_submodule ()
173         set_description( N_("Freebox TV") )
174         add_shortcut( "freebox" )
175         set_capability( "services_discovery", 0 )
176         set_callbacks( Open_LuaSD, Close_LuaSD )
177
178     add_submodule ()
179         set_description( N_("French TV") )
180         add_shortcut( "frenchtv" )
181         set_capability( "services_discovery", 0 )
182         set_callbacks( Open_LuaSD, Close_LuaSD )
183
184     VLC_SD_PROBE_SUBMODULE
185
186 vlc_module_end ()
187
188 /*****************************************************************************
189  *
190  *****************************************************************************/
191 static const char *ppsz_lua_exts[] = { ".luac", ".lua", NULL };
192 static int file_select( const char *file )
193 {
194     int i = strlen( file );
195     int j;
196     for( j = 0; ppsz_lua_exts[j]; j++ )
197     {
198         int l = strlen( ppsz_lua_exts[j] );
199         if( i >= l && !strcmp( file+i-l, ppsz_lua_exts[j] ) )
200             return 1;
201     }
202     return 0;
203 }
204
205 static int file_compare( const char **a, const char **b )
206 {
207     return strcmp( *a, *b );
208 }
209
210 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
211                      char ***pppsz_dir_list )
212 {
213 #define MAX_DIR_LIST_SIZE 5
214     *pppsz_dir_list = malloc(MAX_DIR_LIST_SIZE*sizeof(char *));
215     if (!*pppsz_dir_list)
216         return VLC_EGENERIC;
217     char **ppsz_dir_list = *pppsz_dir_list;
218
219     int i = 0;
220     char *datadir = config_GetUserDir( VLC_DATA_DIR );
221
222     if( likely(datadir != NULL)
223      && likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
224                          datadir, luadirname ) != -1) )
225         i++;
226     free( datadir );
227
228 #if !(defined(__APPLE__) || defined(WIN32))
229     if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
230                          config_GetLibDir(), luadirname ) != -1) )
231             i++;
232 #endif
233
234     char *psz_datapath = config_GetDataDir( p_this );
235     if( likely(psz_datapath != NULL) )
236     {
237         if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
238                               psz_datapath, luadirname ) != -1) )
239             i++;
240
241 #if defined(__APPLE__)
242         if( likely(asprintf( &ppsz_dir_list[i],
243                              "%s"DIR_SEP"share"DIR_SEP"lua"DIR_SEP"%s",
244                              psz_datapath, luadirname ) != -1) )
245             i++;
246 #endif
247         free( psz_datapath );
248     }
249
250     ppsz_dir_list[i] = NULL;
251
252     assert( i < MAX_DIR_LIST_SIZE);
253
254     return VLC_SUCCESS;
255 }
256
257 void vlclua_dir_list_free( char **ppsz_dir_list )
258 {
259     for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
260         free( *ppsz_dir );
261     free( ppsz_dir_list );
262 }
263
264 /*****************************************************************************
265  * Will execute func on all scripts in luadirname, and stop if func returns
266  * success.
267  *****************************************************************************/
268 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
269                                   const char * luadirname,
270                                   int (*func)(vlc_object_t *, const char *, void *),
271                                   void * user_data)
272 {
273     char **ppsz_dir_list = NULL;
274     int i_ret;
275
276     if((i_ret = vlclua_dir_list( p_this, luadirname, &ppsz_dir_list ) != VLC_SUCCESS))
277         return i_ret;
278
279     i_ret = VLC_EGENERIC;
280     for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
281     {
282         char **ppsz_filelist;
283
284         msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
285         int i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
286                                    file_compare );
287         if( i_files < 0 )
288             continue;
289
290         char **ppsz_file = ppsz_filelist;
291         char **ppsz_fileend = ppsz_filelist + i_files;
292
293         while( ppsz_file < ppsz_fileend )
294         {
295             char *psz_filename;
296
297             if( asprintf( &psz_filename,
298                           "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
299                 psz_filename = NULL;
300             free( *(ppsz_file++) );
301
302             if( likely(psz_filename != NULL) )
303             {
304                 msg_Dbg( p_this, "Trying Lua playlist script %s",
305                          psz_filename );
306                 i_ret = func( p_this, psz_filename, user_data );
307                 free( psz_filename );
308                 if( i_ret == VLC_SUCCESS )
309                     break;
310             }
311         }
312
313         while( ppsz_file < ppsz_fileend )
314             free( *(ppsz_file++) );
315         free( ppsz_filelist );
316
317         if( i_ret == VLC_SUCCESS )
318             break;
319     }
320     vlclua_dir_list_free( ppsz_dir_list );
321     return i_ret;
322 }
323
324 char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
325 {
326     char **ppsz_dir_list = NULL;
327     vlclua_dir_list( p_this, psz_luadirname, &ppsz_dir_list );
328
329     for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
330     {
331         for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
332         {
333             char *psz_filename;
334             struct stat st;
335
336             if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
337                           psz_name, *ppsz_ext ) < 0 )
338             {
339                 vlclua_dir_list_free( ppsz_dir_list );
340                 return NULL;
341             }
342
343             if( vlc_stat( psz_filename, &st ) == 0
344                 && S_ISREG( st.st_mode ) )
345             {
346                 vlclua_dir_list_free( ppsz_dir_list );
347                 return psz_filename;
348             }
349             free( psz_filename );
350         }
351     }
352     vlclua_dir_list_free( ppsz_dir_list );
353     return NULL;
354 }
355
356 /*****************************************************************************
357  * Meta data setters utility.
358  * Playlist item table should be on top of the stack when these are called
359  *****************************************************************************/
360 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
361                               input_item_t *p_input )
362 {
363 #define TRY_META( a, b )                                        \
364     lua_getfield( L, -1, a );                                   \
365     if( lua_isstring( L, -1 ) &&                                \
366         strcmp( lua_tostring( L, -1 ), "" ) )                   \
367     {                                                           \
368         char *psz_value = strdup( lua_tostring( L, -1 ) );      \
369         EnsureUTF8( psz_value );                                \
370         msg_Dbg( p_this, #b ": %s", psz_value );                \
371         input_item_Set ## b ( p_input, psz_value );             \
372         free( psz_value );                                      \
373     }                                                           \
374     lua_pop( L, 1 ); /* pop a */
375     TRY_META( "title", Title );
376     TRY_META( "artist", Artist );
377     TRY_META( "genre", Genre );
378     TRY_META( "copyright", Copyright );
379     TRY_META( "album", Album );
380     TRY_META( "tracknum", TrackNum );
381     TRY_META( "description", Description );
382     TRY_META( "rating", Rating );
383     TRY_META( "date", Date );
384     TRY_META( "setting", Setting );
385     TRY_META( "url", URL );
386     TRY_META( "language", Language );
387     TRY_META( "nowplaying", NowPlaying );
388     TRY_META( "publisher", Publisher );
389     TRY_META( "encodedby", EncodedBy );
390     TRY_META( "arturl", ArtURL );
391     TRY_META( "trackid", TrackID );
392 }
393
394 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
395                                      input_item_t *p_input )
396 {
397     /* ... item */
398     lua_getfield( L, -1, "meta" );
399     /* ... item meta */
400     if( lua_istable( L, -1 ) )
401     {
402         lua_pushnil( L );
403         /* ... item meta nil */
404         while( lua_next( L, -2 ) )
405         {
406             /* ... item meta key value */
407             if( !lua_isstring( L, -2 ) )
408             {
409                 msg_Warn( p_this, "Custom meta data category name must be "
410                                    "a string" );
411             }
412             else if( !lua_istable( L, -1 ) )
413             {
414                 msg_Warn( p_this, "Custom meta data category contents "
415                                    "must be a table" );
416             }
417             else
418             {
419                 const char *psz_meta_category = lua_tostring( L, -2 );
420                 msg_Dbg( p_this, "Found custom meta data category: %s",
421                          psz_meta_category );
422                 lua_pushnil( L );
423                 /* ... item meta key value nil */
424                 while( lua_next( L, -2 ) )
425                 {
426                     /* ... item meta key value key2 value2 */
427                     if( !lua_isstring( L, -2 ) )
428                     {
429                         msg_Warn( p_this, "Custom meta category item name "
430                                            "must be a string." );
431                     }
432                     else if( !lua_isstring( L, -1 ) )
433                     {
434                         msg_Warn( p_this, "Custom meta category item value "
435                                            "must be a string." );
436                     }
437                     else
438                     {
439                         const char *psz_meta_name =
440                             lua_tostring( L, -2 );
441                         const char *psz_meta_value =
442                             lua_tostring( L, -1 );
443                         msg_Dbg( p_this, "Custom meta %s, %s: %s",
444                                  psz_meta_category, psz_meta_name,
445                                  psz_meta_value );
446                         input_item_AddInfo( p_input, psz_meta_category,
447                                            psz_meta_name, "%s", psz_meta_value );
448                     }
449                     lua_pop( L, 1 ); /* pop item */
450                     /* ... item meta key value key2 */
451                 }
452                 /* ... item meta key value */
453             }
454             lua_pop( L, 1 ); /* pop category */
455             /* ... item meta key */
456         }
457         /* ... item meta */
458     }
459     lua_pop( L, 1 ); /* pop "meta" */
460     /* ... item -> back to original stack */
461 }
462
463 /*****************************************************************************
464  * Playlist utilities
465  ****************************************************************************/
466 /**
467  * Playlist item table should be on top of the stack when this is called
468  */
469 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
470                             int *pi_options, char ***pppsz_options )
471 {
472     lua_getfield( L, -1, "options" );
473     if( lua_istable( L, -1 ) )
474     {
475         lua_pushnil( L );
476         while( lua_next( L, -2 ) )
477         {
478             if( lua_isstring( L, -1 ) )
479             {
480                 char *psz_option = strdup( lua_tostring( L, -1 ) );
481                 msg_Dbg( p_this, "Option: %s", psz_option );
482                 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
483                              psz_option );
484             }
485             else
486             {
487                 msg_Warn( p_this, "Option should be a string" );
488             }
489             lua_pop( L, 1 ); /* pop option */
490         }
491     }
492     lua_pop( L, 1 ); /* pop "options" */
493 }
494
495 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
496                                     playlist_t *p_playlist,
497                                     input_item_t *p_parent, bool b_play )
498 {
499     int i_count = 0;
500     input_item_node_t *p_parent_node = NULL;
501
502     assert( p_parent || p_playlist );
503
504     /* playlist */
505     if( lua_istable( L, -1 ) )
506     {
507         if( p_parent ) p_parent_node = input_item_node_Create( p_parent );
508         lua_pushnil( L );
509         /* playlist nil */
510         while( lua_next( L, -2 ) )
511         {
512             /* playlist key item */
513             /* <Parse playlist item> */
514             if( lua_istable( L, -1 ) )
515             {
516                 lua_getfield( L, -1, "path" );
517                 /* playlist key item path */
518                 if( lua_isstring( L, -1 ) )
519                 {
520                     const char   *psz_path     = NULL;
521                     const char   *psz_name     = NULL;
522                     char        **ppsz_options = NULL;
523                     int           i_options    = 0;
524                     mtime_t       i_duration   = -1;
525                     input_item_t *p_input;
526
527                     /* Read path and name */
528                     psz_path = lua_tostring( L, -1 );
529                     msg_Dbg( p_this, "Path: %s", psz_path );
530                     lua_getfield( L, -2, "name" );
531                     /* playlist key item path name */
532                     if( lua_isstring( L, -1 ) )
533                     {
534                         psz_name = lua_tostring( L, -1 );
535                         msg_Dbg( p_this, "Name: %s", psz_name );
536                     }
537                     else
538                     {
539                         if( !lua_isnil( L, -1 ) )
540                             msg_Warn( p_this, "Playlist item name should be a string." );
541                         psz_name = psz_path;
542                     }
543
544                     /* Read duration */
545                     lua_getfield( L, -3, "duration" );
546                     /* playlist key item path name duration */
547                     if( lua_isnumber( L, -1 ) )
548                     {
549                         i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
550                     }
551                     else if( !lua_isnil( L, -1 ) )
552                     {
553                         msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
554                     }
555                     lua_pop( L, 1 ); /* pop "duration" */
556
557                     /* playlist key item path name */
558
559                     /* Read options: item must be on top of stack */
560                     lua_pushvalue( L, -3 );
561                     /* playlist key item path name item */
562                     vlclua_read_options( p_this, L, &i_options, &ppsz_options );
563
564                     /* Create input item */
565                     p_input = input_item_NewExt( p_playlist, psz_path,
566                                                 psz_name, i_options,
567                                                 (const char **)ppsz_options,
568                                                 VLC_INPUT_OPTION_TRUSTED,
569                                                 i_duration );
570                     lua_pop( L, 3 ); /* pop "path name item" */
571                     /* playlist key item */
572
573                     /* Read meta data: item must be on top of stack */
574                     vlclua_read_meta_data( p_this, L, p_input );
575
576                     /* Read custom meta data: item must be on top of stack*/
577                     vlclua_read_custom_meta_data( p_this, L, p_input );
578
579                     /* Append item to playlist */
580                     if( p_parent ) /* Add to node */
581                     {
582                         input_item_CopyOptions( p_parent, p_input );
583                         input_item_node_AppendItem( p_parent_node, p_input );
584                     }
585                     else /* Play or Enqueue (preparse) */
586                         /* FIXME: playlist_AddInput() can fail */
587                         playlist_AddInput( p_playlist, p_input,
588                                PLAYLIST_APPEND |
589                                ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
590                                PLAYLIST_END, true, false );
591                     i_count ++; /* increment counter */
592                     vlc_gc_decref( p_input );
593                     while( i_options > 0 )
594                         free( ppsz_options[--i_options] );
595                     free( ppsz_options );
596                 }
597                 else
598                 {
599                     lua_pop( L, 1 ); /* pop "path" */
600                     msg_Warn( p_this,
601                              "Playlist item's path should be a string" );
602                 }
603                 /* playlist key item */
604             }
605             else
606             {
607                 msg_Warn( p_this, "Playlist item should be a table" );
608             }
609             /* <Parse playlist item> */
610             lua_pop( L, 1 ); /* pop the value, keep the key for
611                               * the next lua_next() call */
612             /* playlist key */
613         }
614         /* playlist */
615         if( p_parent )
616         {
617             if( i_count ) input_item_node_PostAndDelete( p_parent_node );
618             else input_item_node_Delete( p_parent_node );
619         }
620     }
621     else
622     {
623         msg_Warn( p_this, "Playlist should be a table." );
624     }
625     return i_count;
626 }
627
628 static int vlc_sd_probe_Open( vlc_object_t *obj )
629 {
630     vlc_probe_t *probe = (vlc_probe_t *)obj;
631     char **ppsz_filelist = NULL;
632     char **ppsz_fileend  = NULL;
633     char **ppsz_file;
634     char *psz_name;
635     char **ppsz_dir_list = NULL;
636     char **ppsz_dir;
637     lua_State *L = NULL;
638     vlclua_dir_list( obj, "sd", &ppsz_dir_list );
639     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
640     {
641         int i_files;
642         if( ppsz_filelist )
643         {
644             for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
645                  ppsz_file++ )
646                 free( *ppsz_file );
647             free( ppsz_filelist );
648             ppsz_filelist = NULL;
649         }
650         i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
651                                 file_compare );
652         if( i_files < 1 ) continue;
653         ppsz_fileend = ppsz_filelist + i_files;
654         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
655         {
656             char  *psz_filename;
657             if( asprintf( &psz_filename,
658                           "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
659             {
660                 goto error;
661             }
662             L = luaL_newstate();
663             if( !L )
664             {
665                 msg_Err( probe, "Could not create new Lua State" );
666                 free( psz_filename );
667                 goto error;
668             }
669             luaL_openlibs( L );
670             if( vlclua_add_modules_path( probe, L, psz_filename ) )
671             {
672                 msg_Err( probe, "Error while setting the module search path for %s",
673                           psz_filename );
674                 free( psz_filename );
675                 goto error;
676             }
677             if( luaL_dofile( L, psz_filename ) )
678             {
679
680                 msg_Err( probe, "Error loading script %s: %s", psz_filename,
681                           lua_tostring( L, lua_gettop( L ) ) );
682                 lua_pop( L, 1 );
683                 free( psz_filename );
684                 lua_close( L );
685                 continue;
686             }
687             char *psz_longname;
688             char *temp = strchr( *ppsz_file, '.' );
689             if( temp )
690                 *temp = '\0';
691             lua_getglobal( L, "descriptor" );
692             if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
693             {
694                 msg_Warn( probe, "No 'descriptor' function in '%s'", psz_filename );
695                 lua_pop( L, 1 );
696                 if( !( psz_longname = strdup( *ppsz_file ) ) )
697                 {
698                     free( psz_filename );
699                     goto error;
700                 }
701             }
702             else
703             {
704                 lua_getfield( L, -1, "title" );
705                 if( !lua_isstring( L, -1 ) ||
706                     !( psz_longname = strdup( lua_tostring( L, -1 ) ) ) )
707                 {
708                     free( psz_filename );
709                     goto error;
710                 }
711             }
712
713             char *psz_file_esc = config_StringEscape( *ppsz_file );
714             char *psz_longname_esc = config_StringEscape( psz_longname );
715             if( asprintf( &psz_name, "lua{sd='%s',longname='%s'}",
716                           psz_file_esc, psz_longname_esc ) < 0 )
717             {
718                 free( psz_file_esc );
719                 free( psz_longname_esc );
720                 free( psz_filename );
721                 free( psz_longname );
722                 goto error;
723             }
724             free( psz_file_esc );
725             free( psz_longname_esc );
726             vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
727             free( psz_name );
728             free( psz_longname );
729             free( psz_filename );
730             lua_close( L );
731         }
732     }
733     if( ppsz_filelist )
734     {
735         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
736              ppsz_file++ )
737             free( *ppsz_file );
738         free( ppsz_filelist );
739     }
740     vlclua_dir_list_free( ppsz_dir_list );
741     return VLC_PROBE_CONTINUE;
742 error:
743     if( ppsz_filelist )
744     {
745         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
746              ppsz_file++ )
747             free( *ppsz_file );
748         free( ppsz_filelist );
749     }
750     if( L )
751         lua_close( L );
752     vlclua_dir_list_free( ppsz_dir_list );
753     return VLC_ENOMEM;
754 }
755
756 static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
757 {
758     int count = 0;
759     for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
760     {
761         lua_pushfstring( L, "%s"DIR_SEP"modules"DIR_SEP"?%s;",
762                          psz_path, *ppsz_ext );
763         count ++;
764     }
765
766     return count;
767 }
768
769 int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_filename )
770 {
771     /* Setup the module search path:
772      *   * "The script's directory"/modules
773      *   * "The script's parent directory"/modules
774      *   * and so on for all the next directories in the directory list
775      */
776     char *psz_path = strdup( psz_filename );
777     if( !psz_path )
778         return 1;
779
780     char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
781     if( !psz_char )
782     {
783         free( psz_path );
784         return 1;
785     }
786     *psz_char = '\0';
787
788     /* psz_path now holds the file's directory */
789     psz_char = strrchr( psz_path, DIR_SEP_CHAR );
790     if( !psz_char )
791     {
792         free( psz_path );
793         return 1;
794     }
795     *psz_char = '\0';
796
797     /* Push package on stack */
798     int count = 0;
799     lua_getglobal( L, "package" );
800
801     /* psz_path now holds the file's parent directory */
802     count += vlclua_add_modules_path_inner( L, psz_path );
803     *psz_char = DIR_SEP_CHAR;
804
805     /* psz_path now holds the file's directory */
806     count += vlclua_add_modules_path_inner( L, psz_path );
807
808     char **ppsz_dir_list = NULL;
809     vlclua_dir_list( obj, psz_char+1/* gruik? */, &ppsz_dir_list );
810     char **ppsz_dir = ppsz_dir_list;
811
812     for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
813     free( psz_path );
814
815     for( ; *ppsz_dir; ppsz_dir++ )
816     {
817         psz_path = *ppsz_dir;
818         /* FIXME: doesn't work well with meta/... modules due to the double
819          * directory depth */
820         psz_char = strrchr( psz_path, DIR_SEP_CHAR );
821         if( !psz_char )
822         {
823             vlclua_dir_list_free( ppsz_dir_list );
824             return 1;
825         }
826
827         *psz_char = '\0';
828         count += vlclua_add_modules_path_inner( L, psz_path );
829         *psz_char = DIR_SEP_CHAR;
830         count += vlclua_add_modules_path_inner( L, psz_path );
831     }
832
833     lua_getfield( L, -(count+1), "path" ); /* Get package.path */
834     lua_concat( L, count+1 ); /* Concat vlc module paths and package.path */
835     lua_setfield( L, -2, "path"); /* Set package.path */
836     lua_pop( L, 1 ); /* Pop the package module */
837
838     vlclua_dir_list_free( ppsz_dir_list );
839     return 0;
840 }
841