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