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