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