]> git.sesse.net Git - vlc/blob - modules/misc/lua/vlc.c
lua_intf: also provide the --rc-host option for backward compatibility.
[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", NULL,
105                     INTF_TEXT, INTF_LONGTEXT, false )
106         add_string( "lua-config", "", NULL,
107                     CONFIG_TEXT, CONFIG_LONGTEXT, false )
108         set_section( N_("Lua HTTP"), 0 )
109             add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, true )
110             add_string ( "http-src",  NULL, NULL, SRC_TEXT,  SRC_LONGTEXT,  true )
111             add_bool   ( "http-index", false, NULL, INDEX_TEXT, INDEX_LONGTEXT, true )
112         set_section( N_("Lua RC"), 0 )
113             add_string( "rc-host", NULL, NULL, RCHOST_TEXT, RCHOST_LONGTEXT, true )
114         set_section( N_("Lua Telnet"), 0 )
115             add_string( "telnet-host", "localhost", NULL, TELNETHOST_TEXT,
116                         TELNETHOST_LONGTEXT, true )
117             add_integer( "telnet-port", TELNETPORT_DEFAULT, NULL, TELNETPORT_TEXT,
118                          TELNETPORT_LONGTEXT, true )
119             add_password( "telnet-password", TELNETPWD_DEFAULT, NULL, 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, NULL, false )
167             change_volatile()
168         add_string( "lua-longname", "", NULL, 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(SYS_BEOS) || 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__) || defined(SYS_BEOS)
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     char **ppsz_dir;
260     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
261         free( *ppsz_dir );
262     free( ppsz_dir_list );
263 }
264
265 /*****************************************************************************
266  * Will execute func on all scripts in luadirname, and stop if func returns
267  * success.
268  *****************************************************************************/
269 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
270                                   const char * luadirname,
271                                   int (*func)(vlc_object_t *, const char *, void *),
272                                   void * user_data)
273 {
274     char **ppsz_dir_list = NULL;
275
276     int i_ret = vlclua_dir_list( p_this, luadirname, &ppsz_dir_list );
277     if( i_ret != VLC_SUCCESS )
278         return i_ret;
279     i_ret = VLC_EGENERIC;
280
281     for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
282     {
283         char **ppsz_filelist;
284         int i_files;
285
286         msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
287         i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
288                                 file_compare );
289         if( i_files < 0 )
290             continue;
291
292         char **ppsz_file = ppsz_filelist;
293         char **ppsz_fileend = ppsz_filelist + i_files;
294
295         while( ppsz_file < ppsz_fileend )
296         {
297             char *psz_filename;
298
299             if( asprintf( &psz_filename,
300                           "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
301                 psz_filename = NULL;
302             free( *(ppsz_file++) );
303
304             if( likely(psz_filename != NULL) )
305             {
306                 msg_Dbg( p_this, "Trying Lua playlist script %s",
307                          psz_filename );
308                 i_ret = func( p_this, psz_filename, user_data );
309                 free( psz_filename );
310                 if( i_ret == VLC_SUCCESS )
311                     break;
312             }
313         }
314
315         while( ppsz_file < ppsz_fileend )
316             free( *(ppsz_file++) );
317         free( ppsz_filelist );
318
319         if( i_ret == VLC_SUCCESS )
320             break;
321     }
322     vlclua_dir_list_free( ppsz_dir_list );
323     return i_ret;
324 }
325
326 char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
327 {
328     char **ppsz_dir_list = NULL;
329     char **ppsz_dir;
330     vlclua_dir_list( p_this, psz_luadirname, &ppsz_dir_list );
331     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
332     {
333         for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
334         {
335             char *psz_filename;
336             struct stat st;
337
338             if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
339                           psz_name, *ppsz_ext ) < 0 )
340             {
341                 vlclua_dir_list_free( ppsz_dir_list );
342                 return NULL;
343             }
344
345             if( vlc_stat( psz_filename, &st ) == 0
346                 && S_ISREG( st.st_mode ) )
347             {
348                 vlclua_dir_list_free( ppsz_dir_list );
349                 return psz_filename;
350             }
351             free( psz_filename );
352         }
353     }
354     vlclua_dir_list_free( ppsz_dir_list );
355     return NULL;
356 }
357
358 /*****************************************************************************
359  * Meta data setters utility.
360  * Playlist item table should be on top of the stack when these are called
361  *****************************************************************************/
362 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
363                               input_item_t *p_input )
364 {
365 #define TRY_META( a, b )                                        \
366     lua_getfield( L, -1, a );                                   \
367     if( lua_isstring( L, -1 ) &&                                \
368         strcmp( lua_tostring( L, -1 ), "" ) )                   \
369     {                                                           \
370         char *psz_value = strdup( lua_tostring( L, -1 ) );      \
371         EnsureUTF8( psz_value );                                \
372         msg_Dbg( p_this, #b ": %s", psz_value );                \
373         input_item_Set ## b ( p_input, psz_value );             \
374         free( psz_value );                                      \
375     }                                                           \
376     lua_pop( L, 1 ); /* pop a */
377     TRY_META( "title", Title );
378     TRY_META( "artist", Artist );
379     TRY_META( "genre", Genre );
380     TRY_META( "copyright", Copyright );
381     TRY_META( "album", Album );
382     TRY_META( "tracknum", TrackNum );
383     TRY_META( "description", Description );
384     TRY_META( "rating", Rating );
385     TRY_META( "date", Date );
386     TRY_META( "setting", Setting );
387     TRY_META( "url", URL );
388     TRY_META( "language", Language );
389     TRY_META( "nowplaying", NowPlaying );
390     TRY_META( "publisher", Publisher );
391     TRY_META( "encodedby", EncodedBy );
392     TRY_META( "arturl", ArtURL );
393     TRY_META( "trackid", TrackID );
394 }
395
396 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
397                                      input_item_t *p_input )
398 {
399     /* ... item */
400     lua_getfield( L, -1, "meta" );
401     /* ... item meta */
402     if( lua_istable( L, -1 ) )
403     {
404         lua_pushnil( L );
405         /* ... item meta nil */
406         while( lua_next( L, -2 ) )
407         {
408             /* ... item meta key value */
409             if( !lua_isstring( L, -2 ) )
410             {
411                 msg_Warn( p_this, "Custom meta data category name must be "
412                                    "a string" );
413             }
414             else if( !lua_istable( L, -1 ) )
415             {
416                 msg_Warn( p_this, "Custom meta data category contents "
417                                    "must be a table" );
418             }
419             else
420             {
421                 const char *psz_meta_category = lua_tostring( L, -2 );
422                 msg_Dbg( p_this, "Found custom meta data category: %s",
423                          psz_meta_category );
424                 lua_pushnil( L );
425                 /* ... item meta key value nil */
426                 while( lua_next( L, -2 ) )
427                 {
428                     /* ... item meta key value key2 value2 */
429                     if( !lua_isstring( L, -2 ) )
430                     {
431                         msg_Warn( p_this, "Custom meta category item name "
432                                            "must be a string." );
433                     }
434                     else if( !lua_isstring( L, -1 ) )
435                     {
436                         msg_Warn( p_this, "Custom meta category item value "
437                                            "must be a string." );
438                     }
439                     else
440                     {
441                         const char *psz_meta_name =
442                             lua_tostring( L, -2 );
443                         const char *psz_meta_value =
444                             lua_tostring( L, -1 );
445                         msg_Dbg( p_this, "Custom meta %s, %s: %s",
446                                  psz_meta_category, psz_meta_name,
447                                  psz_meta_value );
448                         input_item_AddInfo( p_input, psz_meta_category,
449                                            psz_meta_name, "%s", psz_meta_value );
450                     }
451                     lua_pop( L, 1 ); /* pop item */
452                     /* ... item meta key value key2 */
453                 }
454                 /* ... item meta key value */
455             }
456             lua_pop( L, 1 ); /* pop category */
457             /* ... item meta key */
458         }
459         /* ... item meta */
460     }
461     lua_pop( L, 1 ); /* pop "meta" */
462     /* ... item -> back to original stack */
463 }
464
465 /*****************************************************************************
466  * Playlist utilities
467  ****************************************************************************/
468 /**
469  * Playlist item table should be on top of the stack when this is called
470  */
471 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
472                             int *pi_options, char ***pppsz_options )
473 {
474     lua_getfield( L, -1, "options" );
475     if( lua_istable( L, -1 ) )
476     {
477         lua_pushnil( L );
478         while( lua_next( L, -2 ) )
479         {
480             if( lua_isstring( L, -1 ) )
481             {
482                 char *psz_option = strdup( lua_tostring( L, -1 ) );
483                 msg_Dbg( p_this, "Option: %s", psz_option );
484                 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
485                              psz_option );
486             }
487             else
488             {
489                 msg_Warn( p_this, "Option should be a string" );
490             }
491             lua_pop( L, 1 ); /* pop option */
492         }
493     }
494     lua_pop( L, 1 ); /* pop "options" */
495 }
496
497 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
498                                     playlist_t *p_playlist,
499                                     input_item_t *p_parent, bool b_play )
500 {
501     int i_count = 0;
502     input_item_node_t *p_parent_node = NULL;
503
504     assert( p_parent || p_playlist );
505
506     /* playlist */
507     if( lua_istable( L, -1 ) )
508     {
509         if( p_parent ) p_parent_node = input_item_node_Create( p_parent );
510         lua_pushnil( L );
511         /* playlist nil */
512         while( lua_next( L, -2 ) )
513         {
514             /* playlist key item */
515             /* <Parse playlist item> */
516             if( lua_istable( L, -1 ) )
517             {
518                 lua_getfield( L, -1, "path" );
519                 /* playlist key item path */
520                 if( lua_isstring( L, -1 ) )
521                 {
522                     const char   *psz_path     = NULL;
523                     const char   *psz_name     = NULL;
524                     char        **ppsz_options = NULL;
525                     int           i_options    = 0;
526                     mtime_t       i_duration   = -1;
527                     input_item_t *p_input;
528
529                     /* Read path and name */
530                     psz_path = lua_tostring( L, -1 );
531                     msg_Dbg( p_this, "Path: %s", psz_path );
532                     lua_getfield( L, -2, "name" );
533                     /* playlist key item path name */
534                     if( lua_isstring( L, -1 ) )
535                     {
536                         psz_name = lua_tostring( L, -1 );
537                         msg_Dbg( p_this, "Name: %s", psz_name );
538                     }
539                     else
540                     {
541                         if( !lua_isnil( L, -1 ) )
542                             msg_Warn( p_this, "Playlist item name should be a string." );
543                         psz_name = psz_path;
544                     }
545
546                     /* Read duration */
547                     lua_getfield( L, -3, "duration" );
548                     /* playlist key item path name duration */
549                     if( lua_isnumber( L, -1 ) )
550                     {
551                         i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
552                     }
553                     else if( !lua_isnil( L, -1 ) )
554                     {
555                         msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
556                     }
557                     lua_pop( L, 1 ); /* pop "duration" */
558
559                     /* playlist key item path name */
560
561                     /* Read options: item must be on top of stack */
562                     lua_pushvalue( L, -3 );
563                     /* playlist key item path name item */
564                     vlclua_read_options( p_this, L, &i_options, &ppsz_options );
565
566                     /* Create input item */
567                     p_input = input_item_NewExt( p_playlist, psz_path,
568                                                 psz_name, i_options,
569                                                 (const char **)ppsz_options,
570                                                 VLC_INPUT_OPTION_TRUSTED,
571                                                 i_duration );
572                     lua_pop( L, 3 ); /* pop "path name item" */
573                     /* playlist key item */
574
575                     /* Read meta data: item must be on top of stack */
576                     vlclua_read_meta_data( p_this, L, p_input );
577
578                     /* Read custom meta data: item must be on top of stack*/
579                     vlclua_read_custom_meta_data( p_this, L, p_input );
580
581                     /* Append item to playlist */
582                     if( p_parent ) /* Add to node */
583                     {
584                         input_item_node_AppendItem( p_parent_node, p_input );
585                     }
586                     else /* Play or Enqueue (preparse) */
587                         /* FIXME: playlist_AddInput() can fail */
588                         playlist_AddInput( p_playlist, p_input,
589                                PLAYLIST_APPEND |
590                                ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
591                                PLAYLIST_END, true, false );
592                     i_count ++; /* increment counter */
593                     vlc_gc_decref( p_input );
594                     while( i_options > 0 )
595                         free( ppsz_options[--i_options] );
596                     free( ppsz_options );
597                 }
598                 else
599                 {
600                     lua_pop( L, 1 ); /* pop "path" */
601                     msg_Warn( p_this,
602                              "Playlist item's path should be a string" );
603                 }
604                 /* playlist key item */
605             }
606             else
607             {
608                 msg_Warn( p_this, "Playlist item should be a table" );
609             }
610             /* <Parse playlist item> */
611             lua_pop( L, 1 ); /* pop the value, keep the key for
612                               * the next lua_next() call */
613             /* playlist key */
614         }
615         /* playlist */
616         if( p_parent )
617         {
618             if( i_count ) input_item_node_PostAndDelete( p_parent_node );
619             else input_item_node_Delete( p_parent_node );
620         }
621     }
622     else
623     {
624         msg_Warn( p_this, "Playlist should be a table." );
625     }
626     return i_count;
627 }
628
629 static int vlc_sd_probe_Open( vlc_object_t *obj )
630 {
631     vlc_probe_t *probe = (vlc_probe_t *)obj;
632     char **ppsz_filelist = NULL;
633     char **ppsz_fileend  = NULL;
634     char **ppsz_file;
635     char *psz_name;
636     char **ppsz_dir_list = NULL;
637     char **ppsz_dir;
638     lua_State *L = NULL;
639     vlclua_dir_list( obj, "sd", &ppsz_dir_list );
640     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
641     {
642         int i_files;
643         if( ppsz_filelist )
644         {
645             for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
646                  ppsz_file++ )
647                 free( *ppsz_file );
648             free( ppsz_filelist );
649             ppsz_filelist = NULL;
650         }
651         i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
652                                 file_compare );
653         if( i_files < 1 ) continue;
654         ppsz_fileend = ppsz_filelist + i_files;
655         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
656         {
657             char  *psz_filename;
658             if( asprintf( &psz_filename,
659                           "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
660             {
661                 goto error;
662             }
663             L = luaL_newstate();
664             if( !L )
665             {
666                 msg_Err( probe, "Could not create new Lua State" );
667                 free( psz_filename );
668                 goto error;
669             }
670             luaL_openlibs( L );
671             if( vlclua_add_modules_path( probe, L, psz_filename ) )
672             {
673                 msg_Err( probe, "Error while setting the module search path for %s",
674                           psz_filename );
675                 free( psz_filename );
676                 goto error;
677             }
678             if( luaL_dofile( L, psz_filename ) )
679             {
680
681                 msg_Err( probe, "Error loading script %s: %s", psz_filename,
682                           lua_tostring( L, lua_gettop( L ) ) );
683                 lua_pop( L, 1 );
684                 free( psz_filename );
685                 lua_close( L );
686                 continue;
687             }
688             char *psz_longname;
689             char *temp = strchr( *ppsz_file, '.' );
690             if( temp )
691                 *temp = '\0';
692             lua_getglobal( L, "descriptor" );
693             if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
694             {
695                 msg_Warn( probe, "No 'descriptor' function in '%s'", psz_filename );
696                 lua_pop( L, 1 );
697                 if( !( psz_longname = strdup( *ppsz_file ) ) )
698                 {
699                     free( psz_filename );
700                     goto error;
701                 }
702             }
703             else
704             {
705                 lua_getfield( L, -1, "title" );
706                 if( !lua_isstring( L, -1 ) ||
707                     !( psz_longname = strdup( lua_tostring( L, -1 ) ) ) )
708                 {
709                     free( psz_filename );
710                     goto error;
711                 }
712             }
713
714             char *psz_file_esc = config_StringEscape( *ppsz_file );
715             char *psz_longname_esc = config_StringEscape( psz_longname );
716             if( asprintf( &psz_name, "lua{sd='%s',longname='%s'}",
717                           psz_file_esc, psz_longname_esc ) < 0 )
718             {
719                 free( psz_file_esc );
720                 free( psz_longname_esc );
721                 free( psz_filename );
722                 free( psz_longname );
723                 goto error;
724             }
725             free( psz_file_esc );
726             free( psz_longname_esc );
727             vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
728             free( psz_name );
729             free( psz_longname );
730             free( psz_filename );
731             lua_close( L );
732         }
733     }
734     if( ppsz_filelist )
735     {
736         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
737              ppsz_file++ )
738             free( *ppsz_file );
739         free( ppsz_filelist );
740     }
741     vlclua_dir_list_free( ppsz_dir_list );
742     return VLC_PROBE_CONTINUE;
743 error:
744     if( ppsz_filelist )
745     {
746         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
747              ppsz_file++ )
748             free( *ppsz_file );
749         free( ppsz_filelist );
750     }
751     if( L )
752         lua_close( L );
753     vlclua_dir_list_free( ppsz_dir_list );
754     return VLC_ENOMEM;
755 }
756
757 static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
758 {
759     int count = 0;
760     for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
761     {
762         lua_pushfstring( L, "%s"DIR_SEP"modules"DIR_SEP"?%s;",
763                          psz_path, *ppsz_ext );
764         count ++;
765     }
766
767     return count;
768 }
769
770 int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_filename )
771 {
772     /* Setup the module search path:
773      *   * "The script's directory"/modules
774      *   * "The script's parent directory"/modules
775      *   * and so on for all the next directories in the directory list
776      */
777     char *psz_path = strdup( psz_filename );
778     if( !psz_path )
779         return 1;
780
781     char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
782     if( !psz_char )
783     {
784         free( psz_path );
785         return 1;
786     }
787     *psz_char = '\0';
788
789     /* psz_path now holds the file's directory */
790     psz_char = strrchr( psz_path, DIR_SEP_CHAR );
791     if( !psz_char )
792     {
793         free( psz_path );
794         return 1;
795     }
796     *psz_char = '\0';
797
798     /* Push package on stack */
799     int count = 0;
800     lua_getglobal( L, "package" );
801
802     /* psz_path now holds the file's parent directory */
803     count += vlclua_add_modules_path_inner( L, psz_path );
804     *psz_char = DIR_SEP_CHAR;
805
806     /* psz_path now holds the file's directory */
807     count += vlclua_add_modules_path_inner( L, psz_path );
808
809     char **ppsz_dir_list = NULL;
810     vlclua_dir_list( obj, psz_char+1/* gruik? */, &ppsz_dir_list );
811     char **ppsz_dir = ppsz_dir_list;
812
813     for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
814     free( psz_path );
815
816     for( ; *ppsz_dir; ppsz_dir++ )
817     {
818         psz_path = *ppsz_dir;
819         /* FIXME: doesn't work well with meta/... modules due to the double
820          * directory depth */
821         psz_char = strrchr( psz_path, DIR_SEP_CHAR );
822         if( !psz_char )
823         {
824             vlclua_dir_list_free( ppsz_dir_list );
825             return 1;
826         }
827
828         *psz_char = '\0';
829         count += vlclua_add_modules_path_inner( L, psz_path );
830         *psz_char = DIR_SEP_CHAR;
831         count += vlclua_add_modules_path_inner( L, psz_path );
832     }
833
834     lua_getfield( L, -(count+1), "path" ); /* Get package.path */
835     lua_concat( L, count+1 ); /* Concat vlc module paths and package.path */
836     lua_setfield( L, -2, "path"); /* Set package.path */
837     lua_pop( L, 1 ); /* Pop the package module */
838
839     vlclua_dir_list_free( ppsz_dir_list );
840     return 0;
841 }
842