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