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