Commit 20c42664 authored by Pierre Ynard's avatar Pierre Ynard

luasd: probe scripts by name in separate helper

Ref #3353
parent d584df8d
......@@ -42,9 +42,8 @@ static int Control( services_discovery_t *p_sd, int i_command, va_list args );
// When successful, the returned string is stored on top of the lua
// stack and remains valid as long as it is kept in the stack.
#undef vlclua_sd_description
const char *vlclua_sd_description( vlc_object_t *obj, lua_State *L,
const char *filename )
static const char *vlclua_sd_description( vlc_object_t *obj, lua_State *L,
const char *filename )
{
lua_getglobal( L, "descriptor" );
if( !lua_isfunction( L, -1 ) )
......@@ -74,7 +73,62 @@ const char *vlclua_sd_description( vlc_object_t *obj, lua_State *L,
return lua_tostring( L, -1 );
}
#define vlclua_sd_description(a, b, c) vlclua_sd_description(VLC_OBJECT(a), b, c)
int vlclua_probe_sd( vlc_object_t *obj, const char *name )
{
vlc_probe_t *probe = (vlc_probe_t *)obj;
char *filename = vlclua_find_file( "sd", name );
if( filename == NULL )
{
// File suddenly disappeared - maybe a race condition, no problem
msg_Err( probe, "Couldn't probe lua services discovery script \"%s\".",
name );
return VLC_PROBE_CONTINUE;
}
lua_State *L = luaL_newstate();
if( !L )
{
msg_Err( probe, "Could not create new Lua State" );
free( filename );
return VLC_ENOMEM;
}
luaL_openlibs( L );
if( vlclua_add_modules_path( L, filename ) )
{
msg_Err( probe, "Error while setting the module search path for %s",
filename );
lua_close( L );
free( filename );
return VLC_ENOMEM;
}
if( vlclua_dofile( obj, L, filename ) )
{
msg_Err( probe, "Error loading script %s: %s", filename,
lua_tostring( L, -1 ) );
lua_close( L );
free( filename );
return VLC_PROBE_CONTINUE;
}
const char *description = vlclua_sd_description( obj, L, filename );
if( description == NULL )
description = name;
int r = VLC_ENOMEM;
char *name_esc = config_StringEscape( name );
char *chain;
if( asprintf( &chain, "lua{sd='%s'}", name_esc ) != -1 )
{
r = vlc_sd_probe_Add( probe, chain, description, SD_CAT_INTERNET );
free( chain );
}
free( name_esc );
lua_close( L );
free( filename );
return r;
}
static const char * const ppsz_sd_options[] = { "sd", NULL };
......@@ -169,7 +223,8 @@ int Open_LuaSD( vlc_object_t *p_this )
}
// No strdup(), just don't remove the string from the lua stack
p_sd->description = vlclua_sd_description( p_sd, L, p_sys->psz_filename );
p_sd->description = vlclua_sd_description( VLC_OBJECT(p_sd), L,
p_sys->psz_filename );
if( p_sd->description == NULL )
p_sd->description = p_sd->psz_name;
......
......@@ -619,105 +619,35 @@ int vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
static int vlc_sd_probe_Open( vlc_object_t *obj )
{
vlc_probe_t *probe = (vlc_probe_t *)obj;
char **ppsz_filelist = NULL;
char **ppsz_fileend = NULL;
char **ppsz_file;
char *psz_name;
char **ppsz_dir_list = NULL;
char **ppsz_dir;
lua_State *L = NULL;
int r = VLC_PROBE_CONTINUE;
char **ppsz_dir_list;
vlclua_dir_list( "sd", &ppsz_dir_list );
for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
{
int i_files;
if( ppsz_filelist )
{
for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
ppsz_file++ )
free( *ppsz_file );
free( ppsz_filelist );
ppsz_filelist = NULL;
}
i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
file_compare );
char **ppsz_filelist;
int i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
file_compare );
if( i_files < 1 ) continue;
ppsz_fileend = ppsz_filelist + i_files;
for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
{
char *psz_filename;
if( asprintf( &psz_filename,
"%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
{
goto error;
}
L = luaL_newstate();
if( !L )
{
msg_Err( probe, "Could not create new Lua State" );
free( psz_filename );
goto error;
}
luaL_openlibs( L );
if( vlclua_add_modules_path( L, psz_filename ) )
{
msg_Err( probe, "Error while setting the module search path for %s",
psz_filename );
free( psz_filename );
goto error;
}
if( vlclua_dofile( VLC_OBJECT(probe), L, psz_filename ) )
{
msg_Err( probe, "Error loading script %s: %s", psz_filename,
lua_tostring( L, lua_gettop( L ) ) );
lua_pop( L, 1 );
free( psz_filename );
lua_close( L );
continue;
}
for( char **ppsz_file = ppsz_filelist;
ppsz_file < ppsz_filelist + i_files; ppsz_file++ )
{
char *temp = strchr( *ppsz_file, '.' );
if( temp )
*temp = '\0';
const char *psz_longname = vlclua_sd_description( probe, L, psz_filename );
if( psz_longname == NULL )
psz_longname = *ppsz_file;
char *psz_file_esc = config_StringEscape( *ppsz_file );
if( asprintf( &psz_name, "lua{sd='%s'}", psz_file_esc ) < 0 )
{
free( psz_file_esc );
free( psz_filename );
goto error;
}
free( psz_file_esc );
vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
free( psz_name );
free( psz_filename );
lua_close( L );
r = vlclua_probe_sd( obj, *ppsz_file );
if( r != VLC_PROBE_CONTINUE )
break;
}
}
if( ppsz_filelist )
{
for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
ppsz_file++ )
free( *ppsz_file );
free( ppsz_filelist );
}
vlclua_dir_list_free( ppsz_dir_list );
return VLC_PROBE_CONTINUE;
error:
if( ppsz_filelist )
{
for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
ppsz_file++ )
for( char **ppsz_file = ppsz_filelist;
ppsz_file < ppsz_filelist + i_files; ppsz_file++ )
free( *ppsz_file );
free( ppsz_filelist );
if( r != VLC_PROBE_CONTINUE )
break;
}
if( L )
lua_close( L );
vlclua_dir_list_free( ppsz_dir_list );
return VLC_ENOMEM;
return r;
}
static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
......
......@@ -91,9 +91,8 @@ void Close_Extension( vlc_object_t * );
int Open_LuaSD( vlc_object_t * );
void Close_LuaSD( vlc_object_t * );
// Helper
const char *vlclua_sd_description( vlc_object_t *, lua_State *, const char * );
#define vlclua_sd_description(a, b, c) vlclua_sd_description(VLC_OBJECT(a), b, c)
// Script probe
int vlclua_probe_sd( vlc_object_t *, const char *name );
/*****************************************************************************
* Lua debug
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment