lib: vlm: privatize libvlc_vlm_t
[vlc.git] / modules / lua / vlc.c
blob441113fe618f2f0c0a913be8ff8233473789eb24
1 /*****************************************************************************
2 * vlc.c: Generic lua interface functions
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <assert.h>
33 #include <sys/stat.h>
35 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
37 #include "vlc.h"
39 #include <vlc_plugin.h>
40 #include <vlc_arrays.h>
41 #include <vlc_charset.h>
42 #include <vlc_fs.h>
43 #include <vlc_services_discovery.h>
44 #include <vlc_stream.h>
46 /*****************************************************************************
47 * Module descriptor
48 *****************************************************************************/
49 #define INTF_TEXT N_("Lua interface")
50 #define INTF_LONGTEXT N_("Lua interface module to load")
52 #define CONFIG_TEXT N_("Lua interface configuration")
53 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
54 #define PASS_TEXT N_( "Password" )
55 #define PASS_LONGTEXT N_( "A single password restricts access " \
56 "to this interface." )
57 #define SRC_TEXT N_( "Source directory" )
58 #define SRC_LONGTEXT N_( "Source directory" )
59 #define INDEX_TEXT N_( "Directory index" )
60 #define INDEX_LONGTEXT N_( "Allow to build directory index" )
62 #define TELNETHOST_TEXT N_( "Host" )
63 #define TELNETHOST_LONGTEXT N_( "This is the host on which the " \
64 "interface will listen. It defaults to all network interfaces (0.0.0.0)." \
65 " If you want this interface to be available only on the local " \
66 "machine, enter \"127.0.0.1\"." )
67 #define TELNETPORT_TEXT N_( "Port" )
68 #define TELNETPORT_LONGTEXT N_( "This is the TCP port on which this " \
69 "interface will listen. It defaults to 4212." )
70 #define TELNETPWD_TEXT N_( "Password" )
71 #define TELNETPWD_LONGTEXT N_( "A single password restricts access " \
72 "to this interface." )
73 #define RCHOST_TEXT N_("TCP command input")
74 #define RCHOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
75 "You can set the address and port the interface will bind to." )
76 #define CLIHOST_TEXT N_("CLI input")
77 #define CLIHOST_LONGTEXT N_( "Accept commands from this source. " \
78 "The CLI defaults to stdin (\"*console\"), but can also bind to a " \
79 "plain TCP socket (\"localhost:4212\") or use the telnet protocol " \
80 "(\"telnet://0.0.0.0:4212\")" )
82 static int vlc_sd_probe_Open( vlc_object_t * );
84 vlc_module_begin ()
85 set_shortname( N_("Lua") )
86 set_description( N_("Lua interpreter") )
87 set_category( CAT_INTERFACE )
88 set_subcategory( SUBCAT_INTERFACE_MAIN )
90 add_string( "lua-intf", "dummy", INTF_TEXT, INTF_LONGTEXT, false )
91 add_string( "lua-config", "", CONFIG_TEXT, CONFIG_LONGTEXT, false )
92 set_capability( "interface", 0 )
93 set_callbacks( Open_LuaIntf, Close_LuaIntf )
94 add_shortcut( "luaintf" )
96 add_submodule ()
97 set_section( N_("Lua HTTP"), 0 )
98 add_password ( "http-password", NULL, PASS_TEXT, PASS_LONGTEXT, false )
99 add_string ( "http-src", NULL, SRC_TEXT, SRC_LONGTEXT, true )
100 add_bool ( "http-index", false, INDEX_TEXT, INDEX_LONGTEXT, true )
101 set_capability( "interface", 0 )
102 set_callbacks( Open_LuaHTTP, Close_LuaIntf )
103 add_shortcut( "luahttp", "http" )
104 set_description( N_("Lua HTTP") )
106 add_submodule ()
107 set_section( N_("Lua CLI"), 0 )
108 add_string( "rc-host", NULL, RCHOST_TEXT, RCHOST_LONGTEXT, true )
109 add_string( "cli-host", NULL, CLIHOST_TEXT, CLIHOST_LONGTEXT, true )
110 set_capability( "interface", 25 )
111 set_description( N_("Command-line interface") )
112 set_callbacks( Open_LuaCLI, Close_LuaIntf )
113 #ifndef _WIN32
114 add_shortcut( "luacli", "luarc", "cli", "rc" )
115 #else
116 add_shortcut( "luacli", "luarc" )
117 #endif
119 add_submodule ()
120 set_section( N_("Lua Telnet"), 0 )
121 add_string( "telnet-host", "localhost", TELNETHOST_TEXT,
122 TELNETHOST_LONGTEXT, true )
123 add_integer( "telnet-port", TELNETPORT_DEFAULT, TELNETPORT_TEXT,
124 TELNETPORT_LONGTEXT, true )
125 change_integer_range( 1, 65535 )
126 add_password( "telnet-password", NULL, TELNETPWD_TEXT,
128 TELNETPWD_LONGTEXT, true )
129 set_capability( "interface", 0 )
130 set_callbacks( Open_LuaTelnet, Close_LuaIntf )
131 set_description( N_("Lua Telnet") )
132 add_shortcut( "luatelnet", "telnet" )
134 add_submodule ()
135 set_shortname( N_( "Lua Meta Fetcher" ) )
136 set_description( N_("Fetch meta data using lua scripts") )
137 set_capability( "meta fetcher", 10 )
138 set_callbacks( FetchMeta, NULL )
140 add_submodule ()
141 set_shortname( N_( "Lua Meta Reader" ) )
142 set_description( N_("Read meta data using lua scripts") )
143 set_capability( "meta reader", 10 )
144 set_callbacks( ReadMeta, NULL )
146 add_submodule ()
147 add_shortcut( "luaplaylist" )
148 set_shortname( N_("Lua Playlist") )
149 set_description( N_("Lua Playlist Parser Interface") )
150 set_capability( "stream_filter", 2 )
151 set_callbacks( Import_LuaPlaylist, Close_LuaPlaylist )
153 add_submodule ()
154 set_shortname( N_( "Lua Art" ) )
155 set_description( N_("Fetch artwork using lua scripts") )
156 set_capability( "art finder", 10 )
157 set_callbacks( FindArt, NULL )
159 add_submodule ()
160 set_shortname( N_("Lua Extension") )
161 set_description( N_("Lua Extension") )
162 add_shortcut( "luaextension" )
163 set_capability( "extension", 1 )
164 set_callbacks( Open_Extension, Close_Extension )
166 add_submodule ()
167 set_description( N_("Lua SD Module") )
168 add_shortcut( "luasd" )
169 set_capability( "services_discovery", 0 )
170 add_string( "lua-sd", "", NULL, NULL, false )
171 change_volatile()
172 set_callbacks( Open_LuaSD, Close_LuaSD )
174 VLC_SD_PROBE_SUBMODULE
176 vlc_module_end ()
178 /*****************************************************************************
180 *****************************************************************************/
181 static const char *ppsz_lua_exts[] = { ".luac", ".lua", ".vle", NULL };
182 static int file_select( const char *file )
184 int i = strlen( file );
185 int j;
186 for( j = 0; ppsz_lua_exts[j]; j++ )
188 int l = strlen( ppsz_lua_exts[j] );
189 if( i >= l && !strcmp( file+i-l, ppsz_lua_exts[j] ) )
190 return 1;
192 return 0;
195 static int file_compare( const char **a, const char **b )
197 return strcmp( *a, *b );
200 int vlclua_dir_list( const char *luadirname, char ***pppsz_dir_list )
202 #define MAX_DIR_LIST_SIZE 5
203 *pppsz_dir_list = malloc(MAX_DIR_LIST_SIZE*sizeof(char *));
204 if (!*pppsz_dir_list)
205 return VLC_EGENERIC;
206 char **ppsz_dir_list = *pppsz_dir_list;
208 int i = 0;
209 char *datadir = config_GetUserDir( VLC_DATA_DIR );
211 if( likely(datadir != NULL)
212 && likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
213 datadir, luadirname ) != -1) )
214 i++;
215 free( datadir );
217 #if !(defined(__APPLE__) || defined(_WIN32))
218 char *psz_libpath = config_GetLibDir();
219 if( likely(psz_libpath != NULL) )
221 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
222 psz_libpath, luadirname ) != -1) )
223 i++;
224 free( psz_libpath );
226 #endif
228 char *psz_datapath = config_GetDataDir();
229 if( likely(psz_datapath != NULL) )
231 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
232 psz_datapath, luadirname ) != -1) )
233 i++;
235 #if defined(__APPLE__)
236 if( likely(asprintf( &ppsz_dir_list[i],
237 "%s"DIR_SEP"share"DIR_SEP"lua"DIR_SEP"%s",
238 psz_datapath, luadirname ) != -1) )
239 i++;
240 #endif
241 free( psz_datapath );
244 ppsz_dir_list[i] = NULL;
246 assert( i < MAX_DIR_LIST_SIZE);
248 return VLC_SUCCESS;
251 void vlclua_dir_list_free( char **ppsz_dir_list )
253 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
254 free( *ppsz_dir );
255 free( ppsz_dir_list );
258 /*****************************************************************************
259 * Will execute func on all scripts in luadirname, and stop if func returns
260 * success.
261 *****************************************************************************/
262 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
263 const char * luadirname,
264 int (*func)(vlc_object_t *, const char *, const luabatch_context_t *),
265 void * user_data)
267 char **ppsz_dir_list = NULL;
268 int i_ret;
270 if( (i_ret = vlclua_dir_list( luadirname, &ppsz_dir_list )) != VLC_SUCCESS)
271 return i_ret;
273 i_ret = VLC_EGENERIC;
274 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
276 char **ppsz_filelist;
278 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
279 int i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
280 file_compare );
281 if( i_files < 0 )
282 continue;
284 char **ppsz_file = ppsz_filelist;
285 char **ppsz_fileend = ppsz_filelist + i_files;
287 while( ppsz_file < ppsz_fileend )
289 char *psz_filename;
291 if( asprintf( &psz_filename,
292 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
293 psz_filename = NULL;
294 free( *(ppsz_file++) );
296 if( likely(psz_filename != NULL) )
298 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
299 i_ret = func( p_this, psz_filename, user_data );
300 free( psz_filename );
301 if( i_ret == VLC_SUCCESS )
302 break;
306 while( ppsz_file < ppsz_fileend )
307 free( *(ppsz_file++) );
308 free( ppsz_filelist );
310 if( i_ret == VLC_SUCCESS )
311 break;
313 vlclua_dir_list_free( ppsz_dir_list );
314 return i_ret;
317 char *vlclua_find_file( const char *psz_luadirname, const char *psz_name )
319 char **ppsz_dir_list = NULL;
320 vlclua_dir_list( psz_luadirname, &ppsz_dir_list );
322 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
324 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
326 char *psz_filename;
327 struct stat st;
329 if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
330 psz_name, *ppsz_ext ) < 0 )
332 vlclua_dir_list_free( ppsz_dir_list );
333 return NULL;
336 if( vlc_stat( psz_filename, &st ) == 0
337 && S_ISREG( st.st_mode ) )
339 vlclua_dir_list_free( ppsz_dir_list );
340 return psz_filename;
342 free( psz_filename );
345 vlclua_dir_list_free( ppsz_dir_list );
346 return NULL;
349 /*****************************************************************************
350 * Meta data setters utility.
351 * Playlist item table should be on top of the stack when these are called
352 *****************************************************************************/
353 #undef vlclua_read_meta_data
354 void vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
355 input_item_t *p_input )
357 #define TRY_META( a, b ) \
358 lua_getfield( L, -1, a ); \
359 if( lua_isstring( L, -1 ) && \
360 strcmp( lua_tostring( L, -1 ), "" ) ) \
362 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
363 EnsureUTF8( psz_value ); \
364 msg_Dbg( p_this, #b ": %s", psz_value ); \
365 input_item_Set ## b ( p_input, psz_value ); \
366 free( psz_value ); \
368 lua_pop( L, 1 ); /* pop a */
369 TRY_META( "title", Title );
370 TRY_META( "artist", Artist );
371 TRY_META( "genre", Genre );
372 TRY_META( "copyright", Copyright );
373 TRY_META( "album", Album );
374 TRY_META( "tracknum", TrackNum );
375 TRY_META( "description", Description );
376 TRY_META( "rating", Rating );
377 TRY_META( "date", Date );
378 TRY_META( "setting", Setting );
379 TRY_META( "url", URL );
380 TRY_META( "language", Language );
381 TRY_META( "nowplaying", NowPlaying );
382 TRY_META( "publisher", Publisher );
383 TRY_META( "encodedby", EncodedBy );
384 TRY_META( "arturl", ArtURL );
385 TRY_META( "trackid", TrackID );
386 TRY_META( "director", Director );
387 TRY_META( "season", Season );
388 TRY_META( "episode", Episode );
389 TRY_META( "show_name", ShowName );
390 TRY_META( "actors", Actors );
393 #undef vlclua_read_custom_meta_data
394 void vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
395 input_item_t *p_input )
397 /* Lock the input item and create the meta table if needed */
398 vlc_mutex_lock( &p_input->lock );
400 if( !p_input->p_meta )
401 p_input->p_meta = vlc_meta_New();
403 /* ... item */
404 lua_getfield( L, -1, "meta" );
405 /* ... item meta */
406 if( lua_istable( L, -1 ) )
408 lua_pushnil( L );
409 /* ... item meta nil */
410 while( lua_next( L, -2 ) )
412 /* ... item meta key value */
413 if( !lua_isstring( L, -2 ) || !lua_isstring( L, -1 ) )
415 msg_Err( p_this, "'meta' keys and values must be strings");
416 lua_pop( L, 1 ); /* pop "value" */
417 continue;
419 const char *psz_key = lua_tostring( L, -2 );
420 const char *psz_value = lua_tostring( L, -1 );
422 vlc_meta_AddExtra( p_input->p_meta, psz_key, psz_value );
424 lua_pop( L, 1 ); /* pop "value" */
427 lua_pop( L, 1 ); /* pop "meta" */
428 /* ... item -> back to original stack */
430 vlc_mutex_unlock( &p_input->lock );
433 /*****************************************************************************
434 * Playlist utilities
435 ****************************************************************************/
437 * Playlist item table should be on top of the stack when this is called
439 #undef vlclua_read_options
440 void vlclua_read_options( vlc_object_t *p_this, lua_State *L,
441 int *pi_options, char ***pppsz_options )
443 lua_getfield( L, -1, "options" );
444 if( lua_istable( L, -1 ) )
446 lua_pushnil( L );
447 while( lua_next( L, -2 ) )
449 if( lua_isstring( L, -1 ) )
451 char *psz_option = strdup( lua_tostring( L, -1 ) );
452 msg_Dbg( p_this, "Option: %s", psz_option );
453 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
454 psz_option );
456 else
458 msg_Warn( p_this, "Option should be a string" );
460 lua_pop( L, 1 ); /* pop option */
463 lua_pop( L, 1 ); /* pop "options" */
466 input_item_t *vlclua_read_input_item(vlc_object_t *obj, lua_State *L)
468 if (!lua_istable(L, -1))
470 msg_Warn(obj, "Playlist item should be a table" );
471 return NULL;
474 lua_getfield(L, -1, "path");
476 /* playlist key item path */
477 if (!lua_isstring(L, -1))
479 lua_pop(L, 1); /* pop "path" */
480 msg_Warn(obj, "Playlist item's path should be a string");
481 return NULL;
484 /* Read path and name */
485 const char *path = lua_tostring(L, -1);
486 msg_Dbg(obj, "Path: %s", path);
488 const char *name = NULL;
489 lua_getfield(L, -2, "name");
490 if (lua_isstring(L, -1))
492 name = lua_tostring(L, -1);
493 msg_Dbg(obj, "Name: %s", name);
495 else if (!lua_isnil(L, -1))
496 msg_Warn(obj, "Playlist item name should be a string" );
498 /* Read duration */
499 mtime_t duration = -1;
501 lua_getfield( L, -3, "duration" );
502 if (lua_isnumber(L, -1))
503 duration = (mtime_t)(lua_tonumber(L, -1) * (CLOCK_FREQ * 1.));
504 else if (!lua_isnil(L, -1))
505 msg_Warn(obj, "Playlist item duration should be a number (seconds)");
506 lua_pop( L, 1 ); /* pop "duration" */
508 /* Read options: item must be on top of stack */
509 char **optv = NULL;
510 int optc = 0;
512 lua_pushvalue(L, -3);
513 vlclua_read_options(obj, L, &optc, &optv);
515 /* Create input item */
516 input_item_t *item = input_item_NewExt(path, name, duration,
517 ITEM_TYPE_UNKNOWN,
518 ITEM_NET_UNKNOWN);
519 if (unlikely(item == NULL))
520 goto out;
522 input_item_AddOptions(item, optc, (const char **)optv,
523 VLC_INPUT_OPTION_TRUSTED);
524 lua_pop(L, 3); /* pop "path name item" */
525 /* playlist key item */
527 /* Read meta data: item must be on top of stack */
528 vlclua_read_meta_data(obj, L, item);
530 /* copy the psz_name to the meta data, if "Title" is still empty */
531 char* title = input_item_GetTitle(item);
532 if (title == NULL)
533 input_item_SetTitle(item, name);
534 free(title);
536 /* Read custom meta data: item must be on top of stack*/
537 vlclua_read_custom_meta_data(obj, L, item);
539 out:
540 while (optc > 0)
541 free(optv[--optc]);
542 free(optv);
543 return item;
546 static int vlc_sd_probe_Open( vlc_object_t *obj )
548 vlc_dictionary_t name_d;
550 char **ppsz_dir_list;
551 if( vlclua_dir_list( "sd", &ppsz_dir_list ) )
552 return VLC_ENOMEM;
554 vlc_dictionary_init( &name_d, 32 );
555 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
557 char **ppsz_filelist;
558 int i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
559 file_compare );
560 if( i_files < 1 ) continue;
562 for( char **ppsz_file = ppsz_filelist;
563 ppsz_file < ppsz_filelist + i_files; ppsz_file++ )
565 char *temp = strchr( *ppsz_file, '.' );
566 if( temp )
567 *temp = '\0';
569 if( vlc_dictionary_value_for_key( &name_d, *ppsz_file ) ==
570 kVLCDictionaryNotFound )
571 vlc_dictionary_insert( &name_d, *ppsz_file, &name_d );
572 free( *ppsz_file );
574 free( ppsz_filelist );
576 vlclua_dir_list_free( ppsz_dir_list );
578 int r = VLC_PROBE_CONTINUE;
579 char **names = vlc_dictionary_all_keys( &name_d );
580 if( names != NULL )
582 for( char **name = names; *name; ++name )
584 r = vlclua_probe_sd( obj, *name );
585 if( r != VLC_PROBE_CONTINUE )
586 break;
589 for( char **name = names; *name; ++name )
590 free( *name );
592 free( names );
594 vlc_dictionary_clear( &name_d, NULL, NULL );
596 return r;
599 static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
601 int count = 0;
602 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
604 lua_pushfstring( L, "%s"DIR_SEP"modules"DIR_SEP"?%s;",
605 psz_path, *ppsz_ext );
606 count ++;
609 return count;
612 int vlclua_add_modules_path( lua_State *L, const char *psz_filename )
614 /* Setup the module search path:
615 * * "The script's directory"/modules
616 * * "The script's parent directory"/modules
617 * * and so on for all the next directories in the directory list
619 char *psz_path = strdup( psz_filename );
620 if( !psz_path )
621 return 1;
623 char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
624 if( !psz_char )
626 free( psz_path );
627 return 1;
629 *psz_char = '\0';
631 /* psz_path now holds the file's directory */
632 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
633 if( !psz_char )
635 free( psz_path );
636 return 1;
638 *psz_char = '\0';
640 /* Push package on stack */
641 int count = 0;
642 lua_getglobal( L, "package" );
644 /* psz_path now holds the file's parent directory */
645 count += vlclua_add_modules_path_inner( L, psz_path );
646 *psz_char = DIR_SEP_CHAR;
648 /* psz_path now holds the file's directory */
649 count += vlclua_add_modules_path_inner( L, psz_path );
651 char **ppsz_dir_list = NULL;
652 vlclua_dir_list( psz_char+1/* gruik? */, &ppsz_dir_list );
653 char **ppsz_dir = ppsz_dir_list;
655 for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
656 free( psz_path );
658 for( ; *ppsz_dir; ppsz_dir++ )
660 psz_path = *ppsz_dir;
661 /* FIXME: doesn't work well with meta/... modules due to the double
662 * directory depth */
663 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
664 if( !psz_char )
666 vlclua_dir_list_free( ppsz_dir_list );
667 return 1;
670 *psz_char = '\0';
671 count += vlclua_add_modules_path_inner( L, psz_path );
672 *psz_char = DIR_SEP_CHAR;
673 count += vlclua_add_modules_path_inner( L, psz_path );
676 lua_getfield( L, -(count+1), "path" ); /* Get package.path */
677 lua_concat( L, count+1 ); /* Concat vlc module paths and package.path */
678 lua_setfield( L, -2, "path"); /* Set package.path */
679 lua_pop( L, 1 ); /* Pop the package module */
681 vlclua_dir_list_free( ppsz_dir_list );
682 return 0;
685 /** Replacement for luaL_dofile, using VLC's input capabilities */
686 int vlclua_dofile( vlc_object_t *p_this, lua_State *L, const char *curi )
688 char *uri = ToLocaleDup( curi );
689 if( !strstr( uri, "://" ) ) {
690 int ret = luaL_dofile( L, uri );
691 free( uri );
692 return ret;
694 if( !strncasecmp( uri, "file://", 7 ) ) {
695 int ret = luaL_dofile( L, uri + 7 );
696 free( uri );
697 return ret;
699 stream_t *s = vlc_stream_NewURL( p_this, uri );
700 if( !s )
702 free( uri );
703 return 1;
705 int64_t i_size = stream_Size( s );
706 char *p_buffer = ( i_size > 0 ) ? malloc( i_size ) : NULL;
707 if( !p_buffer )
709 // FIXME: read the whole stream until we reach the end (if no size)
710 vlc_stream_Delete( s );
711 free( uri );
712 return 1;
714 int64_t i_read = vlc_stream_Read( s, p_buffer, (int) i_size );
715 int i_ret = ( i_read == i_size ) ? 0 : 1;
716 if( !i_ret )
717 i_ret = luaL_loadbuffer( L, p_buffer, (size_t) i_size, uri );
718 if( !i_ret )
719 i_ret = lua_pcall( L, 0, LUA_MULTRET, 0 );
720 vlc_stream_Delete( s );
721 free( p_buffer );
722 free( uri );
723 return i_ret;