1 /*****************************************************************************
2 * vlc.c: Generic lua interface functions
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
8 * Pierre d'Herbemont <pdherbemont # videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
41 #include <vlc_charset.h>
44 #include <vlc_services_discovery.h>
45 #include <vlc_stream.h>
48 #include <lua.h> /* Low level lua C API */
49 #include <lauxlib.h> /* Higher level C API */
50 #include <lualib.h> /* Lua libs */
54 /*****************************************************************************
56 *****************************************************************************/
57 #define INTF_TEXT N_("Lua interface")
58 #define INTF_LONGTEXT N_("Lua interface module to load")
60 #define CONFIG_TEXT N_("Lua interface configuration")
61 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
62 #define HOST_TEXT N_( "Host address" )
63 #define HOST_LONGTEXT N_( \
64 "Address and port the HTTP interface will listen on. It defaults to " \
65 "all network interfaces (0.0.0.0)." \
66 " If you want the HTTP interface to be available only on the local " \
67 "machine, enter 127.0.0.1" )
68 #define SRC_TEXT N_( "Source directory" )
69 #define SRC_LONGTEXT N_( "Source directory" )
70 #define INDEX_TEXT N_( "Directory index" )
71 #define INDEX_LONGTEXT N_( "Allow to build directory index" )
73 #define TELNETHOST_TEXT N_( "Host" )
74 #define TELNETHOST_LONGTEXT N_( "This is the host on which the " \
75 "interface will listen. It defaults to all network interfaces (0.0.0.0)." \
76 " If you want this interface to be available only on the local " \
77 "machine, enter \"127.0.0.1\"." )
78 #define TELNETPORT_TEXT N_( "Port" )
79 #define TELNETPORT_LONGTEXT N_( "This is the TCP port on which this " \
80 "interface will listen. It defaults to 4212." )
81 #define TELNETPWD_TEXT N_( "Password" )
82 #define TELNETPWD_LONGTEXT N_( "A single administration password is used " \
83 "to protect this interface. The default value is \"admin\"." )
84 #define TELNETPWD_DEFAULT "admin"
85 #define RCHOST_TEXT N_("TCP command input")
86 #define RCHOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
87 "You can set the address and port the interface will bind to." )
89 static int vlc_sd_probe_Open( vlc_object_t * );
92 set_shortname( N_("Lua Interface Module") )
93 set_description( N_("Interfaces implemented using lua scripts") )
94 add_shortcut( "luaintf" )
95 add_shortcut( "luahttp" )
96 /* add_shortcut( "http" ) */
97 add_shortcut( "luatelnet" )
98 add_shortcut( "telnet" )
99 add_shortcut( "luahotkeys" )
100 /* add_shortcut( "hotkeys" ) */
101 set_capability( "interface", 0 )
102 set_category( CAT_INTERFACE )
103 set_subcategory( SUBCAT_INTERFACE_CONTROL )
104 add_string( "lua-intf", "dummy",
105 INTF_TEXT, INTF_LONGTEXT, false )
106 add_string( "lua-config", "",
107 CONFIG_TEXT, CONFIG_LONGTEXT, false )
108 set_section( N_("Lua HTTP"), 0 )
109 add_string ( "http-host", NULL, HOST_TEXT, HOST_LONGTEXT, true )
110 add_string ( "http-src", NULL, SRC_TEXT, SRC_LONGTEXT, true )
111 add_bool ( "http-index", false, INDEX_TEXT, INDEX_LONGTEXT, true )
112 set_section( N_("Lua RC"), 0 )
113 add_string( "rc-host", NULL, RCHOST_TEXT, RCHOST_LONGTEXT, true )
114 set_section( N_("Lua Telnet"), 0 )
115 add_string( "telnet-host", "localhost", TELNETHOST_TEXT,
116 TELNETHOST_LONGTEXT, true )
117 add_integer( "telnet-port", TELNETPORT_DEFAULT, TELNETPORT_TEXT,
118 TELNETPORT_LONGTEXT, true )
119 add_password( "telnet-password", TELNETPWD_DEFAULT, TELNETPWD_TEXT,
120 TELNETPWD_LONGTEXT, true )
122 set_callbacks( Open_LuaIntf, Close_LuaIntf )
125 set_shortname( N_( "Lua Meta Fetcher" ) )
126 set_description( N_("Fetch meta data using lua scripts") )
127 set_capability( "meta fetcher", 10 )
128 set_callbacks( FetchMeta, NULL )
131 set_shortname( N_( "Lua Meta Reader" ) )
132 set_description( N_("Read meta data using lua scripts") )
133 set_capability( "meta reader", 10 )
134 set_callbacks( ReadMeta, NULL )
137 add_shortcut( "luaplaylist" )
138 set_shortname( N_("Lua Playlist") )
139 set_description( N_("Lua Playlist Parser Interface") )
140 set_capability( "demux", 2 )
141 set_callbacks( Import_LuaPlaylist, Close_LuaPlaylist )
144 set_description( N_("Lua Interface Module (shortcuts)") )
145 add_shortcut( "luarc" )
149 set_capability( "interface", 25 )
150 set_callbacks( Open_LuaIntf, Close_LuaIntf )
153 set_shortname( N_( "Lua Art" ) )
154 set_description( N_("Fetch artwork using lua scripts") )
155 set_capability( "art finder", 10 )
156 set_callbacks( FindArt, NULL )
159 set_shortname( N_("Lua Extension") )
160 add_shortcut( "luaextension" )
161 set_capability( "extension", 1 )
162 set_callbacks( Open_Extension, Close_Extension )
165 set_description( N_("Lua SD Module") )
166 add_shortcut( "luasd" )
167 set_capability( "services_discovery", 0 )
168 add_string( "lua-sd", "", NULL, NULL, false )
170 add_string( "lua-longname", "", NULL, NULL, false )
172 set_callbacks( Open_LuaSD, Close_LuaSD )
175 set_description( N_("Freebox TV") )
176 add_shortcut( "freebox" )
177 set_capability( "services_discovery", 0 )
178 set_callbacks( Open_LuaSD, Close_LuaSD )
181 set_description( N_("French TV") )
182 add_shortcut( "frenchtv" )
183 set_capability( "services_discovery", 0 )
184 set_callbacks( Open_LuaSD, Close_LuaSD )
186 VLC_SD_PROBE_SUBMODULE
190 /*****************************************************************************
192 *****************************************************************************/
193 static const char *ppsz_lua_exts[] = { ".luac", ".lua", ".vle", NULL };
194 static int file_select( const char *file )
196 int i = strlen( file );
198 for( j = 0; ppsz_lua_exts[j]; j++ )
200 int l = strlen( ppsz_lua_exts[j] );
201 if( i >= l && !strcmp( file+i-l, ppsz_lua_exts[j] ) )
207 static int file_compare( const char **a, const char **b )
209 return strcmp( *a, *b );
212 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
213 char ***pppsz_dir_list )
215 #define MAX_DIR_LIST_SIZE 5
216 *pppsz_dir_list = malloc(MAX_DIR_LIST_SIZE*sizeof(char *));
217 if (!*pppsz_dir_list)
219 char **ppsz_dir_list = *pppsz_dir_list;
222 char *datadir = config_GetUserDir( VLC_DATA_DIR );
224 if( likely(datadir != NULL)
225 && likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
226 datadir, luadirname ) != -1) )
230 #if !(defined(__APPLE__) || defined(WIN32))
231 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
232 config_GetLibDir(), luadirname ) != -1) )
236 char *psz_datapath = config_GetDataDir( p_this );
237 if( likely(psz_datapath != NULL) )
239 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
240 psz_datapath, luadirname ) != -1) )
243 #if defined(__APPLE__)
244 if( likely(asprintf( &ppsz_dir_list[i],
245 "%s"DIR_SEP"share"DIR_SEP"lua"DIR_SEP"%s",
246 psz_datapath, luadirname ) != -1) )
249 free( psz_datapath );
252 ppsz_dir_list[i] = NULL;
254 assert( i < MAX_DIR_LIST_SIZE);
259 void vlclua_dir_list_free( char **ppsz_dir_list )
261 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
263 free( ppsz_dir_list );
266 /*****************************************************************************
267 * Will execute func on all scripts in luadirname, and stop if func returns
269 *****************************************************************************/
270 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
271 const char * luadirname,
272 int (*func)(vlc_object_t *, const char *, void *),
275 char **ppsz_dir_list = NULL;
278 if((i_ret = vlclua_dir_list( p_this, luadirname, &ppsz_dir_list ) != VLC_SUCCESS))
281 i_ret = VLC_EGENERIC;
282 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
284 char **ppsz_filelist;
286 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
287 int i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
292 char **ppsz_file = ppsz_filelist;
293 char **ppsz_fileend = ppsz_filelist + i_files;
295 while( ppsz_file < ppsz_fileend )
299 if( asprintf( &psz_filename,
300 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
302 free( *(ppsz_file++) );
304 if( likely(psz_filename != NULL) )
306 msg_Dbg( p_this, "Trying Lua playlist script %s",
308 i_ret = func( p_this, psz_filename, user_data );
309 free( psz_filename );
310 if( i_ret == VLC_SUCCESS )
315 while( ppsz_file < ppsz_fileend )
316 free( *(ppsz_file++) );
317 free( ppsz_filelist );
319 if( i_ret == VLC_SUCCESS )
322 vlclua_dir_list_free( ppsz_dir_list );
326 char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
328 char **ppsz_dir_list = NULL;
329 vlclua_dir_list( p_this, psz_luadirname, &ppsz_dir_list );
331 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
333 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
338 if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
339 psz_name, *ppsz_ext ) < 0 )
341 vlclua_dir_list_free( ppsz_dir_list );
345 if( vlc_stat( psz_filename, &st ) == 0
346 && S_ISREG( st.st_mode ) )
348 vlclua_dir_list_free( ppsz_dir_list );
351 free( psz_filename );
354 vlclua_dir_list_free( ppsz_dir_list );
358 /*****************************************************************************
359 * Meta data setters utility.
360 * Playlist item table should be on top of the stack when these are called
361 *****************************************************************************/
362 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
363 input_item_t *p_input )
365 #define TRY_META( a, b ) \
366 lua_getfield( L, -1, a ); \
367 if( lua_isstring( L, -1 ) && \
368 strcmp( lua_tostring( L, -1 ), "" ) ) \
370 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
371 EnsureUTF8( psz_value ); \
372 msg_Dbg( p_this, #b ": %s", psz_value ); \
373 input_item_Set ## b ( p_input, psz_value ); \
376 lua_pop( L, 1 ); /* pop a */
377 TRY_META( "title", Title );
378 TRY_META( "artist", Artist );
379 TRY_META( "genre", Genre );
380 TRY_META( "copyright", Copyright );
381 TRY_META( "album", Album );
382 TRY_META( "tracknum", TrackNum );
383 TRY_META( "description", Description );
384 TRY_META( "rating", Rating );
385 TRY_META( "date", Date );
386 TRY_META( "setting", Setting );
387 TRY_META( "url", URL );
388 TRY_META( "language", Language );
389 TRY_META( "nowplaying", NowPlaying );
390 TRY_META( "publisher", Publisher );
391 TRY_META( "encodedby", EncodedBy );
392 TRY_META( "arturl", ArtURL );
393 TRY_META( "trackid", TrackID );
396 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
397 input_item_t *p_input )
400 lua_getfield( L, -1, "meta" );
402 if( lua_istable( L, -1 ) )
405 /* ... item meta nil */
406 while( lua_next( L, -2 ) )
408 /* ... item meta key value */
409 if( !lua_isstring( L, -2 ) )
411 msg_Warn( p_this, "Custom meta data category name must be "
414 else if( !lua_istable( L, -1 ) )
416 msg_Warn( p_this, "Custom meta data category contents "
421 const char *psz_meta_category = lua_tostring( L, -2 );
422 msg_Dbg( p_this, "Found custom meta data category: %s",
425 /* ... item meta key value nil */
426 while( lua_next( L, -2 ) )
428 /* ... item meta key value key2 value2 */
429 if( !lua_isstring( L, -2 ) )
431 msg_Warn( p_this, "Custom meta category item name "
432 "must be a string." );
434 else if( !lua_isstring( L, -1 ) )
436 msg_Warn( p_this, "Custom meta category item value "
437 "must be a string." );
441 const char *psz_meta_name =
442 lua_tostring( L, -2 );
443 const char *psz_meta_value =
444 lua_tostring( L, -1 );
445 msg_Dbg( p_this, "Custom meta %s, %s: %s",
446 psz_meta_category, psz_meta_name,
448 input_item_AddInfo( p_input, psz_meta_category,
449 psz_meta_name, "%s", psz_meta_value );
451 lua_pop( L, 1 ); /* pop item */
452 /* ... item meta key value key2 */
454 /* ... item meta key value */
456 lua_pop( L, 1 ); /* pop category */
457 /* ... item meta key */
461 lua_pop( L, 1 ); /* pop "meta" */
462 /* ... item -> back to original stack */
465 /*****************************************************************************
467 ****************************************************************************/
469 * Playlist item table should be on top of the stack when this is called
471 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
472 int *pi_options, char ***pppsz_options )
474 lua_getfield( L, -1, "options" );
475 if( lua_istable( L, -1 ) )
478 while( lua_next( L, -2 ) )
480 if( lua_isstring( L, -1 ) )
482 char *psz_option = strdup( lua_tostring( L, -1 ) );
483 msg_Dbg( p_this, "Option: %s", psz_option );
484 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
489 msg_Warn( p_this, "Option should be a string" );
491 lua_pop( L, 1 ); /* pop option */
494 lua_pop( L, 1 ); /* pop "options" */
497 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
498 playlist_t *p_playlist,
499 input_item_t *p_parent, bool b_play )
502 input_item_node_t *p_parent_node = NULL;
504 assert( p_parent || p_playlist );
507 if( lua_istable( L, -1 ) )
509 if( p_parent ) p_parent_node = input_item_node_Create( p_parent );
512 while( lua_next( L, -2 ) )
514 /* playlist key item */
515 /* <Parse playlist item> */
516 if( lua_istable( L, -1 ) )
518 lua_getfield( L, -1, "path" );
519 /* playlist key item path */
520 if( lua_isstring( L, -1 ) )
522 const char *psz_path = NULL;
523 const char *psz_name = NULL;
524 char **ppsz_options = NULL;
526 mtime_t i_duration = -1;
527 input_item_t *p_input;
529 /* Read path and name */
530 psz_path = lua_tostring( L, -1 );
531 msg_Dbg( p_this, "Path: %s", psz_path );
532 lua_getfield( L, -2, "name" );
533 /* playlist key item path name */
534 if( lua_isstring( L, -1 ) )
536 psz_name = lua_tostring( L, -1 );
537 msg_Dbg( p_this, "Name: %s", psz_name );
541 if( !lua_isnil( L, -1 ) )
542 msg_Warn( p_this, "Playlist item name should be a string." );
547 lua_getfield( L, -3, "duration" );
548 /* playlist key item path name duration */
549 if( lua_isnumber( L, -1 ) )
551 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
553 else if( !lua_isnil( L, -1 ) )
555 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
557 lua_pop( L, 1 ); /* pop "duration" */
559 /* playlist key item path name */
561 /* Read options: item must be on top of stack */
562 lua_pushvalue( L, -3 );
563 /* playlist key item path name item */
564 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
566 /* Create input item */
567 p_input = input_item_NewExt( p_playlist, psz_path,
569 (const char **)ppsz_options,
570 VLC_INPUT_OPTION_TRUSTED,
572 lua_pop( L, 3 ); /* pop "path name item" */
573 /* playlist key item */
575 /* Read meta data: item must be on top of stack */
576 vlclua_read_meta_data( p_this, L, p_input );
578 /* Read custom meta data: item must be on top of stack*/
579 vlclua_read_custom_meta_data( p_this, L, p_input );
581 /* Append item to playlist */
582 if( p_parent ) /* Add to node */
584 input_item_CopyOptions( p_parent, p_input );
585 input_item_node_AppendItem( p_parent_node, p_input );
587 else /* Play or Enqueue (preparse) */
588 /* FIXME: playlist_AddInput() can fail */
589 playlist_AddInput( p_playlist, p_input,
591 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
592 PLAYLIST_END, true, false );
593 i_count ++; /* increment counter */
594 vlc_gc_decref( p_input );
595 while( i_options > 0 )
596 free( ppsz_options[--i_options] );
597 free( ppsz_options );
601 lua_pop( L, 1 ); /* pop "path" */
603 "Playlist item's path should be a string" );
605 /* playlist key item */
609 msg_Warn( p_this, "Playlist item should be a table" );
611 /* <Parse playlist item> */
612 lua_pop( L, 1 ); /* pop the value, keep the key for
613 * the next lua_next() call */
619 if( i_count ) input_item_node_PostAndDelete( p_parent_node );
620 else input_item_node_Delete( p_parent_node );
625 msg_Warn( p_this, "Playlist should be a table." );
630 static int vlc_sd_probe_Open( vlc_object_t *obj )
632 vlc_probe_t *probe = (vlc_probe_t *)obj;
633 char **ppsz_filelist = NULL;
634 char **ppsz_fileend = NULL;
637 char **ppsz_dir_list = NULL;
640 vlclua_dir_list( obj, "sd", &ppsz_dir_list );
641 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
646 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
649 free( ppsz_filelist );
650 ppsz_filelist = NULL;
652 i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
654 if( i_files < 1 ) continue;
655 ppsz_fileend = ppsz_filelist + i_files;
656 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
659 if( asprintf( &psz_filename,
660 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
667 msg_Err( probe, "Could not create new Lua State" );
668 free( psz_filename );
672 if( vlclua_add_modules_path( probe, L, psz_filename ) )
674 msg_Err( probe, "Error while setting the module search path for %s",
676 free( psz_filename );
679 if( luaL_dofile( L, psz_filename ) )
682 msg_Err( probe, "Error loading script %s: %s", psz_filename,
683 lua_tostring( L, lua_gettop( L ) ) );
685 free( psz_filename );
690 char *temp = strchr( *ppsz_file, '.' );
693 lua_getglobal( L, "descriptor" );
694 if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
696 msg_Warn( probe, "No 'descriptor' function in '%s'", psz_filename );
698 if( !( psz_longname = strdup( *ppsz_file ) ) )
700 free( psz_filename );
706 lua_getfield( L, -1, "title" );
707 if( !lua_isstring( L, -1 ) ||
708 !( psz_longname = strdup( lua_tostring( L, -1 ) ) ) )
710 free( psz_filename );
715 char *psz_file_esc = config_StringEscape( *ppsz_file );
716 char *psz_longname_esc = config_StringEscape( psz_longname );
717 if( asprintf( &psz_name, "lua{sd='%s',longname='%s'}",
718 psz_file_esc, psz_longname_esc ) < 0 )
720 free( psz_file_esc );
721 free( psz_longname_esc );
722 free( psz_filename );
723 free( psz_longname );
726 free( psz_file_esc );
727 free( psz_longname_esc );
728 vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
730 free( psz_longname );
731 free( psz_filename );
737 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
740 free( ppsz_filelist );
742 vlclua_dir_list_free( ppsz_dir_list );
743 return VLC_PROBE_CONTINUE;
747 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
750 free( ppsz_filelist );
754 vlclua_dir_list_free( ppsz_dir_list );
758 static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
761 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
763 lua_pushfstring( L, "%s"DIR_SEP"modules"DIR_SEP"?%s;",
764 psz_path, *ppsz_ext );
771 int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_filename )
773 /* Setup the module search path:
774 * * "The script's directory"/modules
775 * * "The script's parent directory"/modules
776 * * and so on for all the next directories in the directory list
778 char *psz_path = strdup( psz_filename );
782 char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
790 /* psz_path now holds the file's directory */
791 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
799 /* Push package on stack */
801 lua_getglobal( L, "package" );
803 /* psz_path now holds the file's parent directory */
804 count += vlclua_add_modules_path_inner( L, psz_path );
805 *psz_char = DIR_SEP_CHAR;
807 /* psz_path now holds the file's directory */
808 count += vlclua_add_modules_path_inner( L, psz_path );
810 char **ppsz_dir_list = NULL;
811 vlclua_dir_list( obj, psz_char+1/* gruik? */, &ppsz_dir_list );
812 char **ppsz_dir = ppsz_dir_list;
814 for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
817 for( ; *ppsz_dir; ppsz_dir++ )
819 psz_path = *ppsz_dir;
820 /* FIXME: doesn't work well with meta/... modules due to the double
822 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
825 vlclua_dir_list_free( ppsz_dir_list );
830 count += vlclua_add_modules_path_inner( L, psz_path );
831 *psz_char = DIR_SEP_CHAR;
832 count += vlclua_add_modules_path_inner( L, psz_path );
835 lua_getfield( L, -(count+1), "path" ); /* Get package.path */
836 lua_concat( L, count+1 ); /* Concat vlc module paths and package.path */
837 lua_setfield( L, -2, "path"); /* Set package.path */
838 lua_pop( L, 1 ); /* Pop the package module */
840 vlclua_dir_list_free( ppsz_dir_list );
844 /** Replacement for luaL_dofile, using VLC's input capabilities */
845 int vlclua_dofile( vlc_object_t *p_this, lua_State *L, const char *uri )
847 if( !strstr( uri, "://" ) )
848 return luaL_dofile( L, uri );
849 if( !strncasecmp( uri, "file://", 7 ) )
850 return luaL_dofile( L, uri + 7 );
851 stream_t *s = stream_UrlNew( p_this, uri );
856 int64_t i_size = stream_Size( s );
857 char *p_buffer = ( i_size > 0 ) ? malloc( i_size ) : NULL;
860 // FIXME: read the whole stream until we reach the end (if no size)
864 int64_t i_read = stream_Read( s, p_buffer, (int) i_size );
865 int i_ret = ( i_read == i_size ) ? 0 : 1;
867 i_ret = luaL_loadbuffer( L, p_buffer, (size_t) i_size, uri );
869 i_ret = lua_pcall( L, 0, LUA_MULTRET, 0 );