Link libcompat with a library with math functions
[vlc.git] / modules / lua / vlc.c
blob837923c2cf837f70450ed8334e10542a89d41b03
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", 302 )
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 static char **vlclua_dir_list_append( char **restrict list, char *basedir,
201 const char *luadirname )
203 if (unlikely(basedir == NULL))
204 return list;
206 if (likely(asprintf(list, "%s"DIR_SEP"lua"DIR_SEP"%s",
207 basedir, luadirname) != -1))
208 list++;
210 free(basedir);
211 return list;
214 int vlclua_dir_list(const char *luadirname, char ***restrict listp)
216 char **list = malloc(4 * sizeof(char *));
217 if (unlikely(list == NULL))
218 return VLC_EGENERIC;
220 *listp = list;
222 /* Lua scripts in user-specific data directory */
223 list = vlclua_dir_list_append(list, config_GetUserDir(VLC_USERDATA_DIR),
224 luadirname);
226 char *libdir = config_GetSysPath(VLC_PKG_LIBEXEC_DIR, NULL);
227 char *datadir = config_GetSysPath(VLC_PKG_DATA_DIR, NULL);
228 bool both = libdir != NULL && datadir != NULL && strcmp(libdir, datadir);
230 /* Tokenized Lua scripts in architecture-specific data directory */
231 list = vlclua_dir_list_append(list, libdir, luadirname);
233 /* Source Lua Scripts in architecture-independent data directory */
234 if (both || libdir == NULL)
235 list = vlclua_dir_list_append(list, datadir, luadirname);
237 *list = NULL;
238 return VLC_SUCCESS;
241 void vlclua_dir_list_free( char **ppsz_dir_list )
243 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
244 free( *ppsz_dir );
245 free( ppsz_dir_list );
248 /*****************************************************************************
249 * Will execute func on all scripts in luadirname, and stop if func returns
250 * success.
251 *****************************************************************************/
252 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
253 const char * luadirname,
254 int (*func)(vlc_object_t *, const char *, const luabatch_context_t *),
255 void * user_data)
257 char **ppsz_dir_list = NULL;
258 int i_ret;
260 if( (i_ret = vlclua_dir_list( luadirname, &ppsz_dir_list )) != VLC_SUCCESS)
261 return i_ret;
263 i_ret = VLC_EGENERIC;
264 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
266 char **ppsz_filelist;
268 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
269 int i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
270 file_compare );
271 if( i_files < 0 )
272 continue;
274 char **ppsz_file = ppsz_filelist;
275 char **ppsz_fileend = ppsz_filelist + i_files;
277 while( ppsz_file < ppsz_fileend )
279 char *psz_filename;
281 if( asprintf( &psz_filename,
282 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
283 psz_filename = NULL;
284 free( *(ppsz_file++) );
286 if( likely(psz_filename != NULL) )
288 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
289 i_ret = func( p_this, psz_filename, user_data );
290 free( psz_filename );
291 if( i_ret == VLC_SUCCESS )
292 break;
296 while( ppsz_file < ppsz_fileend )
297 free( *(ppsz_file++) );
298 free( ppsz_filelist );
300 if( i_ret == VLC_SUCCESS )
301 break;
303 vlclua_dir_list_free( ppsz_dir_list );
304 return i_ret;
307 char *vlclua_find_file( const char *psz_luadirname, const char *psz_name )
309 char **ppsz_dir_list = NULL;
310 vlclua_dir_list( psz_luadirname, &ppsz_dir_list );
312 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
314 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
316 char *psz_filename;
317 struct stat st;
319 if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
320 psz_name, *ppsz_ext ) < 0 )
322 vlclua_dir_list_free( ppsz_dir_list );
323 return NULL;
326 if( vlc_stat( psz_filename, &st ) == 0
327 && S_ISREG( st.st_mode ) )
329 vlclua_dir_list_free( ppsz_dir_list );
330 return psz_filename;
332 free( psz_filename );
335 vlclua_dir_list_free( ppsz_dir_list );
336 return NULL;
339 /*****************************************************************************
340 * Meta data setters utility.
341 * Playlist item table should be on top of the stack when these are called
342 *****************************************************************************/
343 #undef vlclua_read_meta_data
344 void vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
345 input_item_t *p_input )
347 #define TRY_META( a, b ) \
348 lua_getfield( L, -1, a ); \
349 if( lua_isstring( L, -1 ) && \
350 strcmp( lua_tostring( L, -1 ), "" ) ) \
352 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
353 EnsureUTF8( psz_value ); \
354 msg_Dbg( p_this, #b ": %s", psz_value ); \
355 input_item_Set ## b ( p_input, psz_value ); \
356 free( psz_value ); \
358 lua_pop( L, 1 ); /* pop a */
359 TRY_META( "title", Title );
360 TRY_META( "artist", Artist );
361 TRY_META( "genre", Genre );
362 TRY_META( "copyright", Copyright );
363 TRY_META( "album", Album );
364 TRY_META( "tracknum", TrackNum );
365 TRY_META( "description", Description );
366 TRY_META( "rating", Rating );
367 TRY_META( "date", Date );
368 TRY_META( "setting", Setting );
369 TRY_META( "url", URL );
370 TRY_META( "language", Language );
371 TRY_META( "nowplaying", NowPlaying );
372 TRY_META( "publisher", Publisher );
373 TRY_META( "encodedby", EncodedBy );
374 TRY_META( "arturl", ArtURL );
375 TRY_META( "trackid", TrackID );
376 TRY_META( "director", Director );
377 TRY_META( "season", Season );
378 TRY_META( "episode", Episode );
379 TRY_META( "show_name", ShowName );
380 TRY_META( "actors", Actors );
383 #undef vlclua_read_custom_meta_data
384 void vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
385 input_item_t *p_input )
387 /* Lock the input item and create the meta table if needed */
388 vlc_mutex_lock( &p_input->lock );
390 if( !p_input->p_meta )
391 p_input->p_meta = vlc_meta_New();
393 /* ... item */
394 lua_getfield( L, -1, "meta" );
395 /* ... item meta */
396 if( lua_istable( L, -1 ) )
398 lua_pushnil( L );
399 /* ... item meta nil */
400 while( lua_next( L, -2 ) )
402 /* ... item meta key value */
403 if( !lua_isstring( L, -2 ) || !lua_isstring( L, -1 ) )
405 msg_Err( p_this, "'meta' keys and values must be strings");
406 lua_pop( L, 1 ); /* pop "value" */
407 continue;
409 const char *psz_key = lua_tostring( L, -2 );
410 const char *psz_value = lua_tostring( L, -1 );
412 vlc_meta_AddExtra( p_input->p_meta, psz_key, psz_value );
414 lua_pop( L, 1 ); /* pop "value" */
417 lua_pop( L, 1 ); /* pop "meta" */
418 /* ... item -> back to original stack */
420 vlc_mutex_unlock( &p_input->lock );
423 /*****************************************************************************
424 * Playlist utilities
425 ****************************************************************************/
427 * Playlist item table should be on top of the stack when this is called
429 #undef vlclua_read_options
430 void vlclua_read_options( vlc_object_t *p_this, lua_State *L,
431 int *pi_options, char ***pppsz_options )
433 lua_getfield( L, -1, "options" );
434 if( lua_istable( L, -1 ) )
436 lua_pushnil( L );
437 while( lua_next( L, -2 ) )
439 if( lua_isstring( L, -1 ) )
441 char *psz_option = strdup( lua_tostring( L, -1 ) );
442 msg_Dbg( p_this, "Option: %s", psz_option );
443 TAB_APPEND( *pi_options, *pppsz_options, psz_option );
445 else
447 msg_Warn( p_this, "Option should be a string" );
449 lua_pop( L, 1 ); /* pop option */
452 lua_pop( L, 1 ); /* pop "options" */
455 input_item_t *vlclua_read_input_item(vlc_object_t *obj, lua_State *L)
457 if (!lua_istable(L, -1))
459 msg_Warn(obj, "Playlist item should be a table" );
460 return NULL;
463 lua_getfield(L, -1, "path");
465 /* playlist key item path */
466 if (!lua_isstring(L, -1))
468 lua_pop(L, 1); /* pop "path" */
469 msg_Warn(obj, "Playlist item's path should be a string");
470 return NULL;
473 /* Read path and name */
474 const char *path = lua_tostring(L, -1);
475 msg_Dbg(obj, "Path: %s", path);
477 const char *name = NULL;
478 lua_getfield(L, -2, "name");
479 if (lua_isstring(L, -1))
481 name = lua_tostring(L, -1);
482 msg_Dbg(obj, "Name: %s", name);
484 else if (!lua_isnil(L, -1))
485 msg_Warn(obj, "Playlist item name should be a string" );
487 /* Read duration */
488 mtime_t duration = -1;
490 lua_getfield( L, -3, "duration" );
491 if (lua_isnumber(L, -1))
492 duration = (mtime_t)(lua_tonumber(L, -1) * (CLOCK_FREQ * 1.));
493 else if (!lua_isnil(L, -1))
494 msg_Warn(obj, "Playlist item duration should be a number (seconds)");
495 lua_pop( L, 1 ); /* pop "duration" */
497 /* Read options: item must be on top of stack */
498 char **optv = NULL;
499 int optc = 0;
501 lua_pushvalue(L, -3);
502 vlclua_read_options(obj, L, &optc, &optv);
504 /* Create input item */
505 input_item_t *item = input_item_NewExt(path, name, duration,
506 ITEM_TYPE_UNKNOWN,
507 ITEM_NET_UNKNOWN);
508 if (unlikely(item == NULL))
509 goto out;
511 input_item_AddOptions(item, optc, (const char **)optv,
512 VLC_INPUT_OPTION_TRUSTED);
513 lua_pop(L, 3); /* pop "path name item" */
514 /* playlist key item */
516 /* Read meta data: item must be on top of stack */
517 vlclua_read_meta_data(obj, L, item);
519 /* copy the psz_name to the meta data, if "Title" is still empty */
520 char* title = input_item_GetTitle(item);
521 if (title == NULL)
522 input_item_SetTitle(item, name);
523 free(title);
525 /* Read custom meta data: item must be on top of stack*/
526 vlclua_read_custom_meta_data(obj, L, item);
528 out:
529 while (optc > 0)
530 free(optv[--optc]);
531 free(optv);
532 return item;
535 static int vlc_sd_probe_Open( vlc_object_t *obj )
537 if( lua_Disabled( obj ) )
538 return VLC_EGENERIC;
540 vlc_dictionary_t name_d;
542 char **ppsz_dir_list;
543 if( vlclua_dir_list( "sd", &ppsz_dir_list ) )
544 return VLC_ENOMEM;
546 vlc_dictionary_init( &name_d, 32 );
547 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
549 char **ppsz_filelist;
550 int i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
551 file_compare );
552 if( i_files < 1 ) continue;
554 for( char **ppsz_file = ppsz_filelist;
555 ppsz_file < ppsz_filelist + i_files; ppsz_file++ )
557 char *temp = strchr( *ppsz_file, '.' );
558 if( temp )
559 *temp = '\0';
561 if( !vlc_dictionary_has_key( &name_d, *ppsz_file ) )
562 vlc_dictionary_insert( &name_d, *ppsz_file, &name_d );
563 free( *ppsz_file );
565 free( ppsz_filelist );
567 vlclua_dir_list_free( ppsz_dir_list );
569 int r = VLC_PROBE_CONTINUE;
570 char **names = vlc_dictionary_all_keys( &name_d );
571 if( names != NULL )
573 for( char **name = names; *name; ++name )
575 r = vlclua_probe_sd( obj, *name );
576 if( r != VLC_PROBE_CONTINUE )
577 break;
580 for( char **name = names; *name; ++name )
581 free( *name );
583 free( names );
585 vlc_dictionary_clear( &name_d, NULL, NULL );
587 return r;
590 static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
592 int count = 0;
593 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
595 lua_pushfstring( L, "%s"DIR_SEP"modules"DIR_SEP"?%s;",
596 psz_path, *ppsz_ext );
597 count ++;
600 return count;
603 int vlclua_add_modules_path( lua_State *L, const char *psz_filename )
605 /* Setup the module search path:
606 * * "The script's directory"/modules
607 * * "The script's parent directory"/modules
608 * * and so on for all the next directories in the directory list
610 char *psz_path = strdup( psz_filename );
611 if( !psz_path )
612 return 1;
614 char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
615 if( !psz_char )
617 free( psz_path );
618 return 1;
620 *psz_char = '\0';
622 /* psz_path now holds the file's directory */
623 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
624 if( !psz_char )
626 free( psz_path );
627 return 1;
629 *psz_char = '\0';
631 /* Push package on stack */
632 int count = 0;
633 lua_getglobal( L, "package" );
635 /* psz_path now holds the file's parent directory */
636 count += vlclua_add_modules_path_inner( L, psz_path );
637 *psz_char = DIR_SEP_CHAR;
639 /* psz_path now holds the file's directory */
640 count += vlclua_add_modules_path_inner( L, psz_path );
642 char **ppsz_dir_list = NULL;
643 vlclua_dir_list( psz_char+1/* gruik? */, &ppsz_dir_list );
644 char **ppsz_dir = ppsz_dir_list;
646 for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
647 free( psz_path );
649 for( ; *ppsz_dir; ppsz_dir++ )
651 psz_path = *ppsz_dir;
652 /* FIXME: doesn't work well with meta/... modules due to the double
653 * directory depth */
654 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
655 if( !psz_char )
657 vlclua_dir_list_free( ppsz_dir_list );
658 return 1;
661 *psz_char = '\0';
662 count += vlclua_add_modules_path_inner( L, psz_path );
663 *psz_char = DIR_SEP_CHAR;
664 count += vlclua_add_modules_path_inner( L, psz_path );
667 lua_getfield( L, -(count+1), "path" ); /* Get package.path */
668 lua_concat( L, count+1 ); /* Concat vlc module paths and package.path */
669 lua_setfield( L, -2, "path"); /* Set package.path */
670 lua_pop( L, 1 ); /* Pop the package module */
672 vlclua_dir_list_free( ppsz_dir_list );
673 return 0;
676 /** Replacement for luaL_dofile, using VLC's input capabilities */
677 int vlclua_dofile( vlc_object_t *p_this, lua_State *L, const char *curi )
679 char *uri = ToLocaleDup( curi );
680 if( !strstr( uri, "://" ) ) {
681 int ret = luaL_dofile( L, uri );
682 free( uri );
683 return ret;
685 if( !strncasecmp( uri, "file://", 7 ) ) {
686 int ret = luaL_dofile( L, uri + 7 );
687 free( uri );
688 return ret;
690 stream_t *s = vlc_stream_NewURL( p_this, uri );
691 if( !s )
693 free( uri );
694 return 1;
696 int64_t i_size = stream_Size( s );
697 char *p_buffer = ( i_size > 0 ) ? malloc( i_size ) : NULL;
698 if( !p_buffer )
700 // FIXME: read the whole stream until we reach the end (if no size)
701 vlc_stream_Delete( s );
702 free( uri );
703 return 1;
705 int64_t i_read = vlc_stream_Read( s, p_buffer, (int) i_size );
706 int i_ret = ( i_read == i_size ) ? 0 : 1;
707 if( !i_ret )
708 i_ret = luaL_loadbuffer( L, p_buffer, (size_t) i_size, uri );
709 if( !i_ret )
710 i_ret = lua_pcall( L, 0, LUA_MULTRET, 0 );
711 vlc_stream_Delete( s );
712 free( p_buffer );
713 free( uri );
714 return i_ret;