diff --git a/modules/lua/services_discovery.c b/modules/lua/services_discovery.c index 923f602b261f62c92a1bf93ebd897d115a2f1590..51d9d9f585f6d01d54f492fb8007e35eeeb9e871 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 59eeb2f588afa129ea77638073652327cfe6b809..bae806df6dcc84f83972953932bc232283df6bcf 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 0325edd86dc9f6e14bb0a16cdd51442420f852ec..6d55b3d6ca864f25a8a875a4ff2671edfb57772e 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 *****************************************************************************/