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