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