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 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
39 #include <vlc_plugin.h>
40 #include <vlc_charset.h>
42 #include <vlc_services_discovery.h>
43 #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 PASS_TEXT N_( "Password" )
54 #define PASS_LONGTEXT N_( "A single password restricts access " \
55 "to this interface." )
56 #define SRC_TEXT N_( "Source directory" )
57 #define SRC_LONGTEXT N_( "Source directory" )
58 #define INDEX_TEXT N_( "Directory index" )
59 #define INDEX_LONGTEXT N_( "Allow to build directory index" )
61 #define TELNETHOST_TEXT N_( "Host" )
62 #define TELNETHOST_LONGTEXT N_( "This is the host on which the " \
63 "interface will listen. It defaults to all network interfaces (0.0.0.0)." \
64 " If you want this interface to be available only on the local " \
65 "machine, enter \"127.0.0.1\"." )
66 #define TELNETPORT_TEXT N_( "Port" )
67 #define TELNETPORT_LONGTEXT N_( "This is the TCP port on which this " \
68 "interface will listen. It defaults to 4212." )
69 #define TELNETPWD_TEXT N_( "Password" )
70 #define TELNETPWD_LONGTEXT N_( "A single password restricts access " \
71 "to this interface." )
72 #define RCHOST_TEXT N_("TCP command input")
73 #define RCHOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
74 "You can set the address and port the interface will bind to." )
75 #define CLIHOST_TEXT N_("CLI input")
76 #define CLIHOST_LONGTEXT N_( "Accept commands from this source. " \
77 "The CLI defaults to stdin (\"*console\"), but can also bind to a " \
78 "plain TCP socket (\"localhost:4212\") or use the telnet protocol " \
79 "(\"telnet://0.0.0.0:4212\")" )
81 static int vlc_sd_probe_Open( vlc_object_t
* );
84 set_shortname( N_("Lua") )
85 set_description( N_("Lua interpreter") )
86 set_category( CAT_INTERFACE
)
87 set_subcategory( SUBCAT_INTERFACE_MAIN
)
89 add_string( "lua-intf", "dummy", INTF_TEXT
, INTF_LONGTEXT
, false )
90 add_string( "lua-config", "", CONFIG_TEXT
, CONFIG_LONGTEXT
, false )
91 set_capability( "interface", 0 )
92 set_callbacks( Open_LuaIntf
, Close_LuaIntf
)
93 add_shortcut( "luaintf" )
96 set_section( N_("Lua HTTP"), 0 )
97 add_password ( "http-password", NULL
, PASS_TEXT
, PASS_LONGTEXT
, false )
98 add_string ( "http-src", NULL
, SRC_TEXT
, SRC_LONGTEXT
, true )
99 add_bool ( "http-index", false, INDEX_TEXT
, INDEX_LONGTEXT
, true )
100 set_capability( "interface", 0 )
101 set_callbacks( Open_LuaHTTP
, Close_LuaIntf
)
102 add_shortcut( "luahttp", "http" )
103 set_description( N_("Lua HTTP") )
106 set_section( N_("Lua CLI"), 0 )
107 add_string( "rc-host", NULL
, RCHOST_TEXT
, RCHOST_LONGTEXT
, true )
108 add_string( "cli-host", NULL
, CLIHOST_TEXT
, CLIHOST_LONGTEXT
, true )
109 set_capability( "interface", 25 )
110 set_description( N_("Command-line interface") )
111 set_callbacks( Open_LuaCLI
, Close_LuaIntf
)
113 add_shortcut( "luacli", "luarc", "cli", "rc" )
115 add_shortcut( "luacli", "luarc" )
119 set_section( N_("Lua Telnet"), 0 )
120 add_string( "telnet-host", "localhost", TELNETHOST_TEXT
,
121 TELNETHOST_LONGTEXT
, true )
122 add_integer( "telnet-port", TELNETPORT_DEFAULT
, TELNETPORT_TEXT
,
123 TELNETPORT_LONGTEXT
, true )
124 change_integer_range( 1, 65535 )
125 add_password( "telnet-password", NULL
, TELNETPWD_TEXT
,
127 TELNETPWD_LONGTEXT
, true )
128 set_capability( "interface", 0 )
129 set_callbacks( Open_LuaTelnet
, Close_LuaIntf
)
130 set_description( N_("Lua Telnet") )
131 add_shortcut( "luatelnet", "telnet" )
134 set_shortname( N_( "Lua Meta Fetcher" ) )
135 set_description( N_("Fetch meta data using lua scripts") )
136 set_capability( "meta fetcher", 10 )
137 set_callbacks( FetchMeta
, NULL
)
140 set_shortname( N_( "Lua Meta Reader" ) )
141 set_description( N_("Read meta data using lua scripts") )
142 set_capability( "meta reader", 10 )
143 set_callbacks( ReadMeta
, NULL
)
146 add_shortcut( "luaplaylist" )
147 set_shortname( N_("Lua Playlist") )
148 set_description( N_("Lua Playlist Parser Interface") )
149 set_capability( "demux", 2 )
150 set_callbacks( Import_LuaPlaylist
, Close_LuaPlaylist
)
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 set_description( N_("Lua Extension") )
161 add_shortcut( "luaextension" )
162 set_capability( "extension", 1 )
163 set_callbacks( Open_Extension
, Close_Extension
)
166 set_description( N_("Lua SD Module") )
167 add_shortcut( "luasd" )
168 set_capability( "services_discovery", 0 )
169 add_string( "lua-sd", "", NULL
, NULL
, false )
171 add_string( "lua-longname", "", NULL
, NULL
, false )
173 set_callbacks( Open_LuaSD
, Close_LuaSD
)
175 VLC_SD_PROBE_SUBMODULE
179 /*****************************************************************************
181 *****************************************************************************/
182 static const char *ppsz_lua_exts
[] = { ".luac", ".lua", ".vle", NULL
};
183 static int file_select( const char *file
)
185 int i
= strlen( file
);
187 for( j
= 0; ppsz_lua_exts
[j
]; j
++ )
189 int l
= strlen( ppsz_lua_exts
[j
] );
190 if( i
>= l
&& !strcmp( file
+i
-l
, ppsz_lua_exts
[j
] ) )
196 static int file_compare( const char **a
, const char **b
)
198 return strcmp( *a
, *b
);
201 int vlclua_dir_list( const char *luadirname
, char ***pppsz_dir_list
)
203 #define MAX_DIR_LIST_SIZE 5
204 *pppsz_dir_list
= malloc(MAX_DIR_LIST_SIZE
*sizeof(char *));
205 if (!*pppsz_dir_list
)
207 char **ppsz_dir_list
= *pppsz_dir_list
;
210 char *datadir
= config_GetUserDir( VLC_DATA_DIR
);
212 if( likely(datadir
!= NULL
)
213 && likely(asprintf( &ppsz_dir_list
[i
], "%s"DIR_SEP
"lua"DIR_SEP
"%s",
214 datadir
, luadirname
) != -1) )
218 #if !(defined(__APPLE__) || defined(_WIN32))
219 char *psz_libpath
= config_GetLibDir();
220 if( likely(psz_libpath
!= NULL
) )
222 if( likely(asprintf( &ppsz_dir_list
[i
], "%s"DIR_SEP
"lua"DIR_SEP
"%s",
223 psz_libpath
, luadirname
) != -1) )
229 char *psz_datapath
= config_GetDataDir();
230 if( likely(psz_datapath
!= NULL
) )
232 if( likely(asprintf( &ppsz_dir_list
[i
], "%s"DIR_SEP
"lua"DIR_SEP
"%s",
233 psz_datapath
, luadirname
) != -1) )
236 #if defined(__APPLE__)
237 if( likely(asprintf( &ppsz_dir_list
[i
],
238 "%s"DIR_SEP
"share"DIR_SEP
"lua"DIR_SEP
"%s",
239 psz_datapath
, luadirname
) != -1) )
242 free( psz_datapath
);
245 ppsz_dir_list
[i
] = NULL
;
247 assert( i
< MAX_DIR_LIST_SIZE
);
252 void vlclua_dir_list_free( char **ppsz_dir_list
)
254 for( char **ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
256 free( ppsz_dir_list
);
259 /*****************************************************************************
260 * Will execute func on all scripts in luadirname, and stop if func returns
262 *****************************************************************************/
263 int vlclua_scripts_batch_execute( vlc_object_t
*p_this
,
264 const char * luadirname
,
265 int (*func
)(vlc_object_t
*, const char *, const luabatch_context_t
*),
268 char **ppsz_dir_list
= NULL
;
271 if( (i_ret
= vlclua_dir_list( luadirname
, &ppsz_dir_list
)) != VLC_SUCCESS
)
274 i_ret
= VLC_EGENERIC
;
275 for( char **ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
277 char **ppsz_filelist
;
279 msg_Dbg( p_this
, "Trying Lua scripts in %s", *ppsz_dir
);
280 int i_files
= vlc_scandir( *ppsz_dir
, &ppsz_filelist
, file_select
,
285 char **ppsz_file
= ppsz_filelist
;
286 char **ppsz_fileend
= ppsz_filelist
+ i_files
;
288 while( ppsz_file
< ppsz_fileend
)
292 if( asprintf( &psz_filename
,
293 "%s" DIR_SEP
"%s", *ppsz_dir
, *ppsz_file
) == -1 )
295 free( *(ppsz_file
++) );
297 if( likely(psz_filename
!= NULL
) )
299 msg_Dbg( p_this
, "Trying Lua playlist script %s", psz_filename
);
300 i_ret
= func( p_this
, psz_filename
, user_data
);
301 free( psz_filename
);
302 if( i_ret
== VLC_SUCCESS
)
307 while( ppsz_file
< ppsz_fileend
)
308 free( *(ppsz_file
++) );
309 free( ppsz_filelist
);
311 if( i_ret
== VLC_SUCCESS
)
314 vlclua_dir_list_free( ppsz_dir_list
);
318 char *vlclua_find_file( const char *psz_luadirname
, const char *psz_name
)
320 char **ppsz_dir_list
= NULL
;
321 vlclua_dir_list( psz_luadirname
, &ppsz_dir_list
);
323 for( char **ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
325 for( const char **ppsz_ext
= ppsz_lua_exts
; *ppsz_ext
; ppsz_ext
++ )
330 if( asprintf( &psz_filename
, "%s"DIR_SEP
"%s%s", *ppsz_dir
,
331 psz_name
, *ppsz_ext
) < 0 )
333 vlclua_dir_list_free( ppsz_dir_list
);
337 if( vlc_stat( psz_filename
, &st
) == 0
338 && S_ISREG( st
.st_mode
) )
340 vlclua_dir_list_free( ppsz_dir_list
);
343 free( psz_filename
);
346 vlclua_dir_list_free( ppsz_dir_list
);
350 /*****************************************************************************
351 * Meta data setters utility.
352 * Playlist item table should be on top of the stack when these are called
353 *****************************************************************************/
354 #undef vlclua_read_meta_data
355 void vlclua_read_meta_data( vlc_object_t
*p_this
, lua_State
*L
,
356 input_item_t
*p_input
)
358 #define TRY_META( a, b ) \
359 lua_getfield( L, -1, a ); \
360 if( lua_isstring( L, -1 ) && \
361 strcmp( lua_tostring( L, -1 ), "" ) ) \
363 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
364 EnsureUTF8( psz_value ); \
365 msg_Dbg( p_this, #b ": %s", psz_value ); \
366 input_item_Set ## b ( p_input, psz_value ); \
369 lua_pop( L, 1 ); /* pop a */
370 TRY_META( "title", Title
);
371 TRY_META( "artist", Artist
);
372 TRY_META( "genre", Genre
);
373 TRY_META( "copyright", Copyright
);
374 TRY_META( "album", Album
);
375 TRY_META( "tracknum", TrackNum
);
376 TRY_META( "description", Description
);
377 TRY_META( "rating", Rating
);
378 TRY_META( "date", Date
);
379 TRY_META( "setting", Setting
);
380 TRY_META( "url", URL
);
381 TRY_META( "language", Language
);
382 TRY_META( "nowplaying", NowPlaying
);
383 TRY_META( "publisher", Publisher
);
384 TRY_META( "encodedby", EncodedBy
);
385 TRY_META( "arturl", ArtURL
);
386 TRY_META( "trackid", TrackID
);
387 TRY_META( "director", Director
);
388 TRY_META( "season", Season
);
389 TRY_META( "episode", Episode
);
390 TRY_META( "show_name", ShowName
);
391 TRY_META( "actors", Actors
);
394 #undef vlclua_read_custom_meta_data
395 void vlclua_read_custom_meta_data( vlc_object_t
*p_this
, lua_State
*L
,
396 input_item_t
*p_input
)
398 /* Lock the input item and create the meta table if needed */
399 vlc_mutex_lock( &p_input
->lock
);
401 if( !p_input
->p_meta
)
402 p_input
->p_meta
= vlc_meta_New();
405 lua_getfield( L
, -1, "meta" );
407 if( lua_istable( L
, -1 ) )
410 /* ... item meta nil */
411 while( lua_next( L
, -2 ) )
413 /* ... item meta key value */
414 if( !lua_isstring( L
, -2 ) || !lua_isstring( L
, -1 ) )
416 msg_Err( p_this
, "'meta' keys and values must be strings");
417 lua_pop( L
, 1 ); /* pop "value" */
420 const char *psz_key
= lua_tostring( L
, -2 );
421 const char *psz_value
= lua_tostring( L
, -1 );
423 vlc_meta_AddExtra( p_input
->p_meta
, psz_key
, psz_value
);
425 lua_pop( L
, 1 ); /* pop "value" */
428 lua_pop( L
, 1 ); /* pop "meta" */
429 /* ... item -> back to original stack */
431 vlc_mutex_unlock( &p_input
->lock
);
434 /*****************************************************************************
436 ****************************************************************************/
438 * Playlist item table should be on top of the stack when this is called
440 #undef vlclua_read_options
441 void vlclua_read_options( vlc_object_t
*p_this
, lua_State
*L
,
442 int *pi_options
, char ***pppsz_options
)
444 lua_getfield( L
, -1, "options" );
445 if( lua_istable( L
, -1 ) )
448 while( lua_next( L
, -2 ) )
450 if( lua_isstring( L
, -1 ) )
452 char *psz_option
= strdup( lua_tostring( L
, -1 ) );
453 msg_Dbg( p_this
, "Option: %s", psz_option
);
454 INSERT_ELEM( *pppsz_options
, *pi_options
, *pi_options
,
459 msg_Warn( p_this
, "Option should be a string" );
461 lua_pop( L
, 1 ); /* pop option */
464 lua_pop( L
, 1 ); /* pop "options" */
467 #undef vlclua_playlist_add_internal
468 int vlclua_playlist_add_internal( vlc_object_t
*p_this
, lua_State
*L
,
469 playlist_t
*p_playlist
,
470 input_item_t
*p_parent
, bool b_play
)
473 input_item_node_t
*p_parent_node
= NULL
;
475 assert( p_parent
|| p_playlist
);
478 if( lua_istable( L
, -1 ) )
480 if( p_parent
) p_parent_node
= input_item_node_Create( p_parent
);
483 while( lua_next( L
, -2 ) )
485 /* playlist key item */
486 /* <Parse playlist item> */
487 if( lua_istable( L
, -1 ) )
489 lua_getfield( L
, -1, "path" );
490 /* playlist key item path */
491 if( lua_isstring( L
, -1 ) )
493 char *psz_oldurl
= NULL
;
494 const char *psz_path
= NULL
;
495 char *psz_u8path
= NULL
;
496 const char *psz_name
= NULL
;
497 char **ppsz_options
= NULL
;
499 mtime_t i_duration
= -1;
500 input_item_t
*p_input
;
502 /* Read path and name */
504 psz_oldurl
= input_item_GetURI( p_parent
);
505 msg_Dbg( p_this
, "old path: %s", psz_oldurl
);
507 psz_path
= lua_tostring( L
, -1 );
508 msg_Dbg( p_this
, "Path: %s", psz_path
);
509 lua_getfield( L
, -2, "name" );
510 /* playlist key item path name */
511 if( lua_isstring( L
, -1 ) )
513 psz_name
= lua_tostring( L
, -1 );
514 msg_Dbg( p_this
, "Name: %s", psz_name
);
518 if( !lua_isnil( L
, -1 ) )
519 msg_Warn( p_this
, "Playlist item name should be a string." );
524 lua_getfield( L
, -3, "duration" );
525 /* playlist key item path name duration */
526 if( lua_isnumber( L
, -1 ) )
528 i_duration
= (mtime_t
)(lua_tonumber( L
, -1 )*1e6
);
530 else if( !lua_isnil( L
, -1 ) )
532 msg_Warn( p_this
, "Playlist item duration should be a number (in seconds)." );
534 lua_pop( L
, 1 ); /* pop "duration" */
536 /* playlist key item path name */
538 /* Read options: item must be on top of stack */
539 lua_pushvalue( L
, -3 );
540 /* playlist key item path name item */
541 vlclua_read_options( p_this
, L
, &i_options
, &ppsz_options
);
543 /* Create input item */
544 p_input
= input_item_NewExt( psz_path
, psz_name
, i_options
,
545 (const char **)ppsz_options
,
546 VLC_INPUT_OPTION_TRUSTED
,
548 lua_pop( L
, 3 ); /* pop "path name item" */
549 /* playlist key item */
551 /* Read meta data: item must be on top of stack */
552 vlclua_read_meta_data( p_this
, L
, p_input
);
554 /* copy the original URL to the meta data, if "URL" is still empty */
555 char* url
= input_item_GetURL( p_input
);
556 if( url
== NULL
&& p_parent
)
558 EnsureUTF8( psz_oldurl
);
559 msg_Dbg( p_this
, "meta-URL: %s", psz_oldurl
);
560 input_item_SetURL ( p_input
, psz_oldurl
);
565 /* copy the psz_name to the meta data, if "Title" is still empty */
566 char* title
= input_item_GetTitle( p_input
);
568 input_item_SetTitle ( p_input
, psz_name
);
571 /* Read custom meta data: item must be on top of stack*/
572 vlclua_read_custom_meta_data( p_this
, L
, p_input
);
574 /* Append item to playlist */
575 if( p_parent
) /* Add to node */
577 input_item_CopyOptions( p_parent
, p_input
);
578 input_item_node_AppendItem( p_parent_node
, p_input
);
580 else /* Play or Enqueue (preparse) */
581 /* FIXME: playlist_AddInput() can fail */
582 playlist_AddInput( p_playlist
, p_input
,
584 ( b_play
? PLAYLIST_GO
: PLAYLIST_PREPARSE
),
585 PLAYLIST_END
, true, false );
586 i_count
++; /* increment counter */
587 vlc_gc_decref( p_input
);
588 while( i_options
> 0 )
589 free( ppsz_options
[--i_options
] );
590 free( ppsz_options
);
595 lua_pop( L
, 1 ); /* pop "path" */
597 "Playlist item's path should be a string" );
599 /* playlist key item */
603 msg_Warn( p_this
, "Playlist item should be a table" );
605 /* <Parse playlist item> */
606 lua_pop( L
, 1 ); /* pop the value, keep the key for
607 * the next lua_next() call */
613 if( i_count
) input_item_node_PostAndDelete( p_parent_node
);
614 else input_item_node_Delete( p_parent_node
);
619 msg_Warn( p_this
, "Playlist should be a table." );
624 static int vlc_sd_probe_Open( vlc_object_t
*obj
)
626 vlc_probe_t
*probe
= (vlc_probe_t
*)obj
;
627 char **ppsz_filelist
= NULL
;
628 char **ppsz_fileend
= NULL
;
631 char **ppsz_dir_list
= NULL
;
634 vlclua_dir_list( "sd", &ppsz_dir_list
);
635 for( ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
640 for( ppsz_file
= ppsz_filelist
; ppsz_file
< ppsz_fileend
;
643 free( ppsz_filelist
);
644 ppsz_filelist
= NULL
;
646 i_files
= vlc_scandir( *ppsz_dir
, &ppsz_filelist
, file_select
,
648 if( i_files
< 1 ) continue;
649 ppsz_fileend
= ppsz_filelist
+ i_files
;
650 for( ppsz_file
= ppsz_filelist
; ppsz_file
< ppsz_fileend
; ppsz_file
++ )
653 if( asprintf( &psz_filename
,
654 "%s" DIR_SEP
"%s", *ppsz_dir
, *ppsz_file
) < 0 )
661 msg_Err( probe
, "Could not create new Lua State" );
662 free( psz_filename
);
666 if( vlclua_add_modules_path( L
, psz_filename
) )
668 msg_Err( probe
, "Error while setting the module search path for %s",
670 free( psz_filename
);
673 if( vlclua_dofile( VLC_OBJECT(probe
), L
, psz_filename
) )
676 msg_Err( probe
, "Error loading script %s: %s", psz_filename
,
677 lua_tostring( L
, lua_gettop( L
) ) );
679 free( psz_filename
);
684 char *temp
= strchr( *ppsz_file
, '.' );
687 lua_getglobal( L
, "descriptor" );
688 if( !lua_isfunction( L
, lua_gettop( L
) ) || lua_pcall( L
, 0, 1, 0 ) )
690 msg_Warn( probe
, "No 'descriptor' function in '%s'", psz_filename
);
692 if( !( psz_longname
= strdup( *ppsz_file
) ) )
694 free( psz_filename
);
700 lua_getfield( L
, -1, "title" );
701 if( !lua_isstring( L
, -1 ) ||
702 !( psz_longname
= strdup( lua_tostring( L
, -1 ) ) ) )
704 free( psz_filename
);
709 char *psz_file_esc
= config_StringEscape( *ppsz_file
);
710 char *psz_longname_esc
= config_StringEscape( psz_longname
);
711 if( asprintf( &psz_name
, "lua{sd='%s',longname='%s'}",
712 psz_file_esc
, psz_longname_esc
) < 0 )
714 free( psz_file_esc
);
715 free( psz_longname_esc
);
716 free( psz_filename
);
717 free( psz_longname
);
720 free( psz_file_esc
);
721 free( psz_longname_esc
);
722 vlc_sd_probe_Add( probe
, psz_name
, psz_longname
, SD_CAT_INTERNET
);
724 free( psz_longname
);
725 free( psz_filename
);
731 for( ppsz_file
= ppsz_filelist
; ppsz_file
< ppsz_fileend
;
734 free( ppsz_filelist
);
736 vlclua_dir_list_free( ppsz_dir_list
);
737 return VLC_PROBE_CONTINUE
;
741 for( ppsz_file
= ppsz_filelist
; ppsz_file
< ppsz_fileend
;
744 free( ppsz_filelist
);
748 vlclua_dir_list_free( ppsz_dir_list
);
752 static int vlclua_add_modules_path_inner( lua_State
*L
, const char *psz_path
)
755 for( const char **ppsz_ext
= ppsz_lua_exts
; *ppsz_ext
; ppsz_ext
++ )
757 lua_pushfstring( L
, "%s"DIR_SEP
"modules"DIR_SEP
"?%s;",
758 psz_path
, *ppsz_ext
);
765 int vlclua_add_modules_path( lua_State
*L
, const char *psz_filename
)
767 /* Setup the module search path:
768 * * "The script's directory"/modules
769 * * "The script's parent directory"/modules
770 * * and so on for all the next directories in the directory list
772 char *psz_path
= strdup( psz_filename
);
776 char *psz_char
= strrchr( psz_path
, DIR_SEP_CHAR
);
784 /* psz_path now holds the file's directory */
785 psz_char
= strrchr( psz_path
, DIR_SEP_CHAR
);
793 /* Push package on stack */
795 lua_getglobal( L
, "package" );
797 /* psz_path now holds the file's parent directory */
798 count
+= vlclua_add_modules_path_inner( L
, psz_path
);
799 *psz_char
= DIR_SEP_CHAR
;
801 /* psz_path now holds the file's directory */
802 count
+= vlclua_add_modules_path_inner( L
, psz_path
);
804 char **ppsz_dir_list
= NULL
;
805 vlclua_dir_list( psz_char
+1/* gruik? */, &ppsz_dir_list
);
806 char **ppsz_dir
= ppsz_dir_list
;
808 for( ; *ppsz_dir
&& strcmp( *ppsz_dir
, psz_path
); ppsz_dir
++ );
811 for( ; *ppsz_dir
; ppsz_dir
++ )
813 psz_path
= *ppsz_dir
;
814 /* FIXME: doesn't work well with meta/... modules due to the double
816 psz_char
= strrchr( psz_path
, DIR_SEP_CHAR
);
819 vlclua_dir_list_free( ppsz_dir_list
);
824 count
+= vlclua_add_modules_path_inner( L
, psz_path
);
825 *psz_char
= DIR_SEP_CHAR
;
826 count
+= vlclua_add_modules_path_inner( L
, psz_path
);
829 lua_getfield( L
, -(count
+1), "path" ); /* Get package.path */
830 lua_concat( L
, count
+1 ); /* Concat vlc module paths and package.path */
831 lua_setfield( L
, -2, "path"); /* Set package.path */
832 lua_pop( L
, 1 ); /* Pop the package module */
834 vlclua_dir_list_free( ppsz_dir_list
);
838 /** Replacement for luaL_dofile, using VLC's input capabilities */
839 int vlclua_dofile( vlc_object_t
*p_this
, lua_State
*L
, const char *curi
)
841 char *uri
= ToLocaleDup( curi
);
842 if( !strstr( uri
, "://" ) ) {
843 int ret
= luaL_dofile( L
, uri
);
847 if( !strncasecmp( uri
, "file://", 7 ) ) {
848 int ret
= luaL_dofile( L
, uri
+ 7 );
852 stream_t
*s
= stream_UrlNew( p_this
, uri
);
858 int64_t i_size
= stream_Size( s
);
859 char *p_buffer
= ( i_size
> 0 ) ? malloc( i_size
) : NULL
;
862 // FIXME: read the whole stream until we reach the end (if no size)
867 int64_t i_read
= stream_Read( s
, p_buffer
, (int) i_size
);
868 int i_ret
= ( i_read
== i_size
) ? 0 : 1;
870 i_ret
= luaL_loadbuffer( L
, p_buffer
, (size_t) i_size
, uri
);
872 i_ret
= lua_pcall( L
, 0, LUA_MULTRET
, 0 );