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