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