From 8a57fce912d9b3cc44b3f0124722db48475a5ee7 Mon Sep 17 00:00:00 2001 From: Pierre Ynard Date: Wed, 16 Nov 2016 08:25:56 +0100 Subject: [PATCH] luasd: helper function to fetch longname --- modules/lua/services_discovery.c | 35 ++++++++++++++++++++++++++++++++ modules/lua/vlc.c | 17 ++-------------- modules/lua/vlc.h | 4 ++++ 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/modules/lua/services_discovery.c b/modules/lua/services_discovery.c index 923f602b26..51d9d9f585 100644 --- a/modules/lua/services_discovery.c +++ b/modules/lua/services_discovery.c @@ -40,6 +40,41 @@ static int DoSearch( services_discovery_t *p_sd, const char *psz_query ); static int FillDescriptor( services_discovery_t *, services_discovery_descriptor_t * ); 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 ) +{ + lua_getglobal( L, "descriptor" ); + if( !lua_isfunction( L, -1 ) ) + { + msg_Warn( obj, "No 'descriptor' function in '%s'", filename ); + lua_pop( L, 1 ); + return NULL; + } + + if( lua_pcall( L, 0, 1, 0 ) ) + { + msg_Warn( obj, "Error while running script %s, " + "function descriptor(): %s", filename, + lua_tostring( L, -1 ) ); + lua_pop( L, 1 ); + return NULL; + } + + lua_getfield( L, -1, "title" ); + if ( !lua_isstring( L, -1 ) ) + { + msg_Warn( obj, "'descriptor' function in '%s' returned no title", + filename ); + lua_pop( L, 2 ); + return NULL; + } + + return lua_tostring( L, -1 ); +} + static const char * const ppsz_sd_options[] = { "sd", "longname", NULL }; /***************************************************************************** diff --git a/modules/lua/vlc.c b/modules/lua/vlc.c index 59eeb2f588..bae806df6d 100644 --- a/modules/lua/vlc.c +++ b/modules/lua/vlc.c @@ -678,25 +678,12 @@ static int vlc_sd_probe_Open( vlc_object_t *obj ) lua_close( L ); continue; } - const char *psz_longname; char *temp = strchr( *ppsz_file, '.' ); if( temp ) *temp = '\0'; - lua_getglobal( L, "descriptor" ); - if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) ) - { - msg_Warn( probe, "No 'descriptor' function in '%s'", psz_filename ); - lua_pop( L, 1 ); + const char *psz_longname = vlclua_sd_description( probe, L, psz_filename ); + if( psz_longname == NULL ) psz_longname = *ppsz_file; - } - else - { - lua_getfield( L, -1, "title" ); - if( lua_isstring( L, -1 ) ) - psz_longname = lua_tostring( L, -1 ); - else - psz_longname = *ppsz_file; - } char *psz_file_esc = config_StringEscape( *ppsz_file ); char *psz_longname_esc = config_StringEscape( psz_longname ); diff --git a/modules/lua/vlc.h b/modules/lua/vlc.h index 0325edd86d..6d55b3d6ca 100644 --- a/modules/lua/vlc.h +++ b/modules/lua/vlc.h @@ -91,6 +91,10 @@ 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) + /***************************************************************************** * Lua debug *****************************************************************************/ -- GitLab