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