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 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_charset.h>
40 #include <vlc_services_discovery.h>
41 #include <vlc_stream.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 #define INTF_TEXT N_("Lua interface")
49 #define INTF_LONGTEXT N_("Lua interface module to load")
51 #define CONFIG_TEXT N_("Lua interface configuration")
52 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
53 #define SRC_TEXT N_( "Source directory" )
54 #define SRC_LONGTEXT N_( "Source directory" )
55 #define INDEX_TEXT N_( "Directory index" )
56 #define INDEX_LONGTEXT N_( "Allow to build directory index" )
58 #define TELNETHOST_TEXT N_( "Host" )
59 #define TELNETHOST_LONGTEXT N_( "This is the host on which the " \
60 "interface will listen. It defaults to all network interfaces (0.0.0.0)." \
61 " If you want this interface to be available only on the local " \
62 "machine, enter \"127.0.0.1\"." )
63 #define TELNETPORT_TEXT N_( "Port" )
64 #define TELNETPORT_LONGTEXT N_( "This is the TCP port on which this " \
65 "interface will listen. It defaults to 4212." )
66 #define TELNETPWD_TEXT N_( "Password" )
67 #define TELNETPWD_LONGTEXT N_( "A single password restricts access " \
68 "to this interface." )
69 #define RCHOST_TEXT N_("TCP command input")
70 #define RCHOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
71 "You can set the address and port the interface will bind to." )
72 #define CLIHOST_TEXT N_("CLI input")
73 #define CLIHOST_LONGTEXT N_( "Accept commands from this source. " \
74 "The CLI defaults to stdin (\"*console\"), but can also bind to a " \
75 "plain TCP socket (\"localhost:4212\") or use the telnet protocol " \
76 "(\"telnet://0.0.0.0:4212\")" )
78 static int vlc_sd_probe_Open( vlc_object_t * );
81 set_shortname( N_("Lua") )
82 set_description( N_("Lua interpreter") )
83 set_category( CAT_INTERFACE )
84 set_subcategory( SUBCAT_INTERFACE_MAIN )
86 add_string( "lua-intf", "dummy", INTF_TEXT, INTF_LONGTEXT, false )
87 add_string( "lua-config", "", CONFIG_TEXT, CONFIG_LONGTEXT, false )
88 set_capability( "interface", 0 )
89 set_callbacks( Open_LuaIntf, Close_LuaIntf )
90 add_shortcut( "luaintf" )
93 set_section( N_("Lua HTTP"), 0 )
94 add_string ( "http-src", NULL, SRC_TEXT, SRC_LONGTEXT, true )
95 add_bool ( "http-index", false, INDEX_TEXT, INDEX_LONGTEXT, true )
96 set_capability( "interface", 0 )
97 set_callbacks( Open_LuaHTTP, Close_LuaIntf )
98 add_shortcut( "luahttp", "http" )
99 set_description( N_("Lua HTTP") )
102 set_section( N_("Lua CLI"), 0 )
103 add_string( "rc-host", NULL, RCHOST_TEXT, RCHOST_LONGTEXT, true )
104 add_string( "cli-host", NULL, CLIHOST_TEXT, CLIHOST_LONGTEXT, true )
105 set_capability( "interface", 25 )
106 set_description( N_("Command-line interface") )
107 set_callbacks( Open_LuaCLI, Close_LuaIntf )
109 add_shortcut( "luacli", "luarc", "cli", "rc" )
111 add_shortcut( "luacli", "luarc" )
115 set_section( N_("Lua Telnet"), 0 )
116 add_string( "telnet-host", "localhost", TELNETHOST_TEXT,
117 TELNETHOST_LONGTEXT, true )
118 add_integer( "telnet-port", TELNETPORT_DEFAULT, TELNETPORT_TEXT,
119 TELNETPORT_LONGTEXT, true )
120 change_integer_range( 1, 65535 )
121 add_password( "telnet-password", NULL, TELNETPWD_TEXT,
123 TELNETPWD_LONGTEXT, true )
124 set_capability( "interface", 0 )
125 set_callbacks( Open_LuaTelnet, Close_LuaIntf )
126 set_description( N_("Lua Telnet") )
127 add_shortcut( "luatelnet", "telnet" )
130 set_shortname( N_( "Lua Meta Fetcher" ) )
131 set_description( N_("Fetch meta data using lua scripts") )
132 set_capability( "meta fetcher", 10 )
133 set_callbacks( FetchMeta, NULL )
136 set_shortname( N_( "Lua Meta Reader" ) )
137 set_description( N_("Read meta data using lua scripts") )
138 set_capability( "meta reader", 10 )
139 set_callbacks( ReadMeta, NULL )
142 add_shortcut( "luaplaylist" )
143 set_shortname( N_("Lua Playlist") )
144 set_description( N_("Lua Playlist Parser Interface") )
145 set_capability( "demux", 2 )
146 set_callbacks( Import_LuaPlaylist, Close_LuaPlaylist )
149 set_shortname( N_( "Lua Art" ) )
150 set_description( N_("Fetch artwork using lua scripts") )
151 set_capability( "art finder", 10 )
152 set_callbacks( FindArt, NULL )
155 set_shortname( N_("Lua Extension") )
156 set_description( N_("Lua Extension") )
157 add_shortcut( "luaextension" )
158 set_capability( "extension", 1 )
159 set_callbacks( Open_Extension, Close_Extension )
162 set_description( N_("Lua SD Module") )
163 add_shortcut( "luasd" )
164 set_capability( "services_discovery", 0 )
165 add_string( "lua-sd", "", NULL, NULL, false )
167 add_string( "lua-longname", "", NULL, NULL, false )
169 set_callbacks( Open_LuaSD, Close_LuaSD )
171 VLC_SD_PROBE_SUBMODULE
175 /*****************************************************************************
177 *****************************************************************************/
178 static const char *ppsz_lua_exts[] = { ".luac", ".lua", ".vle", NULL };
179 static int file_select( const char *file )
181 int i = strlen( file );
183 for( j = 0; ppsz_lua_exts[j]; j++ )
185 int l = strlen( ppsz_lua_exts[j] );
186 if( i >= l && !strcmp( file+i-l, ppsz_lua_exts[j] ) )
192 static int file_compare( const char **a, const char **b )
194 return strcmp( *a, *b );
197 int vlclua_dir_list( const char *luadirname, char ***pppsz_dir_list )
199 #define MAX_DIR_LIST_SIZE 5
200 *pppsz_dir_list = malloc(MAX_DIR_LIST_SIZE*sizeof(char *));
201 if (!*pppsz_dir_list)
203 char **ppsz_dir_list = *pppsz_dir_list;
206 char *datadir = config_GetUserDir( VLC_DATA_DIR );
208 if( likely(datadir != NULL)
209 && likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
210 datadir, luadirname ) != -1) )
214 #if !(defined(__APPLE__) || defined(WIN32) || defined(__OS2__))
215 char *psz_libpath = config_GetLibDir();
216 if( likely(psz_libpath != NULL) )
218 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
219 psz_libpath, luadirname ) != -1) )
225 char *psz_datapath = config_GetDataDir();
226 if( likely(psz_datapath != NULL) )
228 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
229 psz_datapath, luadirname ) != -1) )
232 #if defined(__APPLE__)
233 if( likely(asprintf( &ppsz_dir_list[i],
234 "%s"DIR_SEP"share"DIR_SEP"lua"DIR_SEP"%s",
235 psz_datapath, luadirname ) != -1) )
238 free( psz_datapath );
241 ppsz_dir_list[i] = NULL;
243 assert( i < MAX_DIR_LIST_SIZE);
248 void vlclua_dir_list_free( char **ppsz_dir_list )
250 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
252 free( ppsz_dir_list );
255 /*****************************************************************************
256 * Will execute func on all scripts in luadirname, and stop if func returns
258 *****************************************************************************/
259 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
260 const char * luadirname,
261 int (*func)(vlc_object_t *, const char *, void *),
264 char **ppsz_dir_list = NULL;
267 if( (i_ret = vlclua_dir_list( luadirname, &ppsz_dir_list )) != VLC_SUCCESS)
270 i_ret = VLC_EGENERIC;
271 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
273 char **ppsz_filelist;
275 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
276 int i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
281 char **ppsz_file = ppsz_filelist;
282 char **ppsz_fileend = ppsz_filelist + i_files;
284 while( ppsz_file < ppsz_fileend )
288 if( asprintf( &psz_filename,
289 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
291 free( *(ppsz_file++) );
293 if( likely(psz_filename != NULL) )
295 msg_Dbg( p_this, "Trying Lua playlist script %s",
297 i_ret = func( p_this, psz_filename, user_data );
298 free( psz_filename );
299 if( i_ret == VLC_SUCCESS )
304 while( ppsz_file < ppsz_fileend )
305 free( *(ppsz_file++) );
306 free( ppsz_filelist );
308 if( i_ret == VLC_SUCCESS )
311 vlclua_dir_list_free( ppsz_dir_list );
315 char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
317 char **ppsz_dir_list = NULL;
318 vlclua_dir_list( psz_luadirname, &ppsz_dir_list );
320 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
322 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
327 if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
328 psz_name, *ppsz_ext ) < 0 )
330 vlclua_dir_list_free( ppsz_dir_list );
334 if( vlc_stat( psz_filename, &st ) == 0
335 && S_ISREG( st.st_mode ) )
337 vlclua_dir_list_free( ppsz_dir_list );
340 free( psz_filename );
343 vlclua_dir_list_free( ppsz_dir_list );
347 /*****************************************************************************
348 * Meta data setters utility.
349 * Playlist item table should be on top of the stack when these are called
350 *****************************************************************************/
351 #undef vlclua_read_meta_data
352 void vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
353 input_item_t *p_input )
355 #define TRY_META( a, b ) \
356 lua_getfield( L, -1, a ); \
357 if( lua_isstring( L, -1 ) && \
358 strcmp( lua_tostring( L, -1 ), "" ) ) \
360 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
361 EnsureUTF8( psz_value ); \
362 msg_Dbg( p_this, #b ": %s", psz_value ); \
363 input_item_Set ## b ( p_input, psz_value ); \
366 lua_pop( L, 1 ); /* pop a */
367 TRY_META( "title", Title );
368 TRY_META( "artist", Artist );
369 TRY_META( "genre", Genre );
370 TRY_META( "copyright", Copyright );
371 TRY_META( "album", Album );
372 TRY_META( "tracknum", TrackNum );
373 TRY_META( "description", Description );
374 TRY_META( "rating", Rating );
375 TRY_META( "date", Date );
376 TRY_META( "setting", Setting );
377 TRY_META( "url", URL );
378 TRY_META( "language", Language );
379 TRY_META( "nowplaying", NowPlaying );
380 TRY_META( "publisher", Publisher );
381 TRY_META( "encodedby", EncodedBy );
382 TRY_META( "arturl", ArtURL );
383 TRY_META( "trackid", TrackID );
386 #undef vlclua_read_custom_meta_data
387 void vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
388 input_item_t *p_input )
390 /* Lock the input item and create the meta table if needed */
391 vlc_mutex_lock( &p_input->lock );
393 if( !p_input->p_meta )
394 p_input->p_meta = vlc_meta_New();
397 lua_getfield( L, -1, "meta" );
399 if( lua_istable( L, -1 ) )
402 /* ... item meta nil */
403 while( lua_next( L, -2 ) )
405 /* ... item meta key value */
406 if( !lua_isstring( L, -2 ) || !lua_isstring( L, -1 ) )
408 msg_Err( p_this, "'meta' keys and values must be strings");
409 lua_pop( L, 1 ); /* pop "value" */
412 const char *psz_key = lua_tostring( L, -2 );
413 const char *psz_value = lua_tostring( L, -1 );
415 vlc_meta_AddExtra( p_input->p_meta, psz_key, psz_value );
417 lua_pop( L, 1 ); /* pop "value" */
420 lua_pop( L, 1 ); /* pop "meta" */
421 /* ... item -> back to original stack */
423 vlc_mutex_unlock( &p_input->lock );
426 /*****************************************************************************
428 ****************************************************************************/
430 * Playlist item table should be on top of the stack when this is called
432 #undef vlclua_read_options
433 void vlclua_read_options( vlc_object_t *p_this, lua_State *L,
434 int *pi_options, char ***pppsz_options )
436 lua_getfield( L, -1, "options" );
437 if( lua_istable( L, -1 ) )
440 while( lua_next( L, -2 ) )
442 if( lua_isstring( L, -1 ) )
444 char *psz_option = strdup( lua_tostring( L, -1 ) );
445 msg_Dbg( p_this, "Option: %s", psz_option );
446 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
451 msg_Warn( p_this, "Option should be a string" );
453 lua_pop( L, 1 ); /* pop option */
456 lua_pop( L, 1 ); /* pop "options" */
459 #undef vlclua_playlist_add_internal
460 int vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
461 playlist_t *p_playlist,
462 input_item_t *p_parent, bool b_play )
465 input_item_node_t *p_parent_node = NULL;
467 assert( p_parent || p_playlist );
470 if( lua_istable( L, -1 ) )
472 if( p_parent ) p_parent_node = input_item_node_Create( p_parent );
475 while( lua_next( L, -2 ) )
477 /* playlist key item */
478 /* <Parse playlist item> */
479 if( lua_istable( L, -1 ) )
481 lua_getfield( L, -1, "path" );
482 /* playlist key item path */
483 if( lua_isstring( L, -1 ) )
485 const char *psz_path = NULL;
486 char *psz_u8path = NULL;
487 const char *psz_name = NULL;
488 char **ppsz_options = NULL;
490 mtime_t i_duration = -1;
491 input_item_t *p_input;
493 /* Read path and name */
494 psz_path = lua_tostring( L, -1 );
495 msg_Dbg( p_this, "Path: %s", psz_path );
496 lua_getfield( L, -2, "name" );
497 /* playlist key item path name */
498 if( lua_isstring( L, -1 ) )
500 psz_name = lua_tostring( L, -1 );
501 msg_Dbg( p_this, "Name: %s", psz_name );
505 if( !lua_isnil( L, -1 ) )
506 msg_Warn( p_this, "Playlist item name should be a string." );
511 lua_getfield( L, -3, "duration" );
512 /* playlist key item path name duration */
513 if( lua_isnumber( L, -1 ) )
515 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
517 else if( !lua_isnil( L, -1 ) )
519 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
521 lua_pop( L, 1 ); /* pop "duration" */
523 /* playlist key item path name */
525 /* Read options: item must be on top of stack */
526 lua_pushvalue( L, -3 );
527 /* playlist key item path name item */
528 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
530 /* Create input item */
531 p_input = input_item_NewExt( psz_path, psz_name, i_options,
532 (const char **)ppsz_options,
533 VLC_INPUT_OPTION_TRUSTED,
535 lua_pop( L, 3 ); /* pop "path name item" */
536 /* playlist key item */
538 /* Read meta data: item must be on top of stack */
539 vlclua_read_meta_data( p_this, L, p_input );
541 /* Read custom meta data: item must be on top of stack*/
542 vlclua_read_custom_meta_data( p_this, L, p_input );
544 /* Append item to playlist */
545 if( p_parent ) /* Add to node */
547 input_item_CopyOptions( p_parent, p_input );
548 input_item_node_AppendItem( p_parent_node, p_input );
550 else /* Play or Enqueue (preparse) */
551 /* FIXME: playlist_AddInput() can fail */
552 playlist_AddInput( p_playlist, p_input,
554 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
555 PLAYLIST_END, true, false );
556 i_count ++; /* increment counter */
557 vlc_gc_decref( p_input );
558 while( i_options > 0 )
559 free( ppsz_options[--i_options] );
560 free( ppsz_options );
565 lua_pop( L, 1 ); /* pop "path" */
567 "Playlist item's path should be a string" );
569 /* playlist key item */
573 msg_Warn( p_this, "Playlist item should be a table" );
575 /* <Parse playlist item> */
576 lua_pop( L, 1 ); /* pop the value, keep the key for
577 * the next lua_next() call */
583 if( i_count ) input_item_node_PostAndDelete( p_parent_node );
584 else input_item_node_Delete( p_parent_node );
589 msg_Warn( p_this, "Playlist should be a table." );
594 static int vlc_sd_probe_Open( vlc_object_t *obj )
596 vlc_probe_t *probe = (vlc_probe_t *)obj;
597 char **ppsz_filelist = NULL;
598 char **ppsz_fileend = NULL;
601 char **ppsz_dir_list = NULL;
604 vlclua_dir_list( "sd", &ppsz_dir_list );
605 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
610 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
613 free( ppsz_filelist );
614 ppsz_filelist = NULL;
616 i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
618 if( i_files < 1 ) continue;
619 ppsz_fileend = ppsz_filelist + i_files;
620 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
623 if( asprintf( &psz_filename,
624 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
631 msg_Err( probe, "Could not create new Lua State" );
632 free( psz_filename );
636 if( vlclua_add_modules_path( probe, L, psz_filename ) )
638 msg_Err( probe, "Error while setting the module search path for %s",
640 free( psz_filename );
643 if( luaL_dofile( L, psz_filename ) )
646 msg_Err( probe, "Error loading script %s: %s", psz_filename,
647 lua_tostring( L, lua_gettop( L ) ) );
649 free( psz_filename );
654 char *temp = strchr( *ppsz_file, '.' );
657 lua_getglobal( L, "descriptor" );
658 if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
660 msg_Warn( probe, "No 'descriptor' function in '%s'", psz_filename );
662 if( !( psz_longname = strdup( *ppsz_file ) ) )
664 free( psz_filename );
670 lua_getfield( L, -1, "title" );
671 if( !lua_isstring( L, -1 ) ||
672 !( psz_longname = strdup( lua_tostring( L, -1 ) ) ) )
674 free( psz_filename );
679 char *psz_file_esc = config_StringEscape( *ppsz_file );
680 char *psz_longname_esc = config_StringEscape( psz_longname );
681 if( asprintf( &psz_name, "lua{sd='%s',longname='%s'}",
682 psz_file_esc, psz_longname_esc ) < 0 )
684 free( psz_file_esc );
685 free( psz_longname_esc );
686 free( psz_filename );
687 free( psz_longname );
690 free( psz_file_esc );
691 free( psz_longname_esc );
692 vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
694 free( psz_longname );
695 free( psz_filename );
701 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
704 free( ppsz_filelist );
706 vlclua_dir_list_free( ppsz_dir_list );
707 return VLC_PROBE_CONTINUE;
711 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
714 free( ppsz_filelist );
718 vlclua_dir_list_free( ppsz_dir_list );
722 static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
725 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
727 lua_pushfstring( L, "%s"DIR_SEP"modules"DIR_SEP"?%s;",
728 psz_path, *ppsz_ext );
735 #undef vlclua_add_modules_path
736 int vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_filename )
738 /* Setup the module search path:
739 * * "The script's directory"/modules
740 * * "The script's parent directory"/modules
741 * * and so on for all the next directories in the directory list
743 char *psz_path = strdup( psz_filename );
747 char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
755 /* psz_path now holds the file's directory */
756 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
764 /* Push package on stack */
766 lua_getglobal( L, "package" );
768 /* psz_path now holds the file's parent directory */
769 count += vlclua_add_modules_path_inner( L, psz_path );
770 *psz_char = DIR_SEP_CHAR;
772 /* psz_path now holds the file's directory */
773 count += vlclua_add_modules_path_inner( L, psz_path );
775 char **ppsz_dir_list = NULL;
776 vlclua_dir_list( psz_char+1/* gruik? */, &ppsz_dir_list );
777 char **ppsz_dir = ppsz_dir_list;
779 for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
782 for( ; *ppsz_dir; ppsz_dir++ )
784 psz_path = *ppsz_dir;
785 /* FIXME: doesn't work well with meta/... modules due to the double
787 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
790 vlclua_dir_list_free( ppsz_dir_list );
795 count += vlclua_add_modules_path_inner( L, psz_path );
796 *psz_char = DIR_SEP_CHAR;
797 count += vlclua_add_modules_path_inner( L, psz_path );
800 lua_getfield( L, -(count+1), "path" ); /* Get package.path */
801 lua_concat( L, count+1 ); /* Concat vlc module paths and package.path */
802 lua_setfield( L, -2, "path"); /* Set package.path */
803 lua_pop( L, 1 ); /* Pop the package module */
805 vlclua_dir_list_free( ppsz_dir_list );
809 /** Replacement for luaL_dofile, using VLC's input capabilities */
810 int vlclua_dofile( vlc_object_t *p_this, lua_State *L, const char *uri )
812 if( !strstr( uri, "://" ) )
813 return luaL_dofile( L, uri );
814 if( !strncasecmp( uri, "file://", 7 ) )
815 return luaL_dofile( L, uri + 7 );
816 stream_t *s = stream_UrlNew( p_this, uri );
821 int64_t i_size = stream_Size( s );
822 char *p_buffer = ( i_size > 0 ) ? malloc( i_size ) : NULL;
825 // FIXME: read the whole stream until we reach the end (if no size)
829 int64_t i_read = stream_Read( s, p_buffer, (int) i_size );
830 int i_ret = ( i_read == i_size ) ? 0 : 1;
832 i_ret = luaL_loadbuffer( L, p_buffer, (size_t) i_size, uri );
834 i_ret = lua_pcall( L, 0, LUA_MULTRET, 0 );