]> git.sesse.net Git - vlc/blobdiff - modules/lua/intf.c
lua: raise an erro from net.poll() when quitting
[vlc] / modules / lua / intf.c
index 3971b61b52f6632d141dab0a549fd9391b0e92a4..6e34caf70aa5e26824b3c01035b01a6e8650426a 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#ifndef  _GNU_SOURCE
-#   define  _GNU_SOURCE
-#endif
-
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
-#include <vlc_common.h>
-#include <vlc_meta.h>
-
-#include <vlc_interface.h>
-#include <vlc_aout.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#include <lua.h>        /* Low level lua C API */
-#include <lauxlib.h>    /* Higher level C API */
-#include <lualib.h>     /* Lua libs */
+#include <vlc_common.h>
+#include <vlc_interface.h>
+#include <vlc_fs.h>
 
 #include "vlc.h"
 #include "libs.h"
@@ -65,68 +56,89 @@ static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
     lua_setfield( L, -2, psz_name );
 }
 
-static const struct
-{
-    const char *psz_shortcut;
-    const char *psz_name;
-} pp_shortcuts[] = {
-    { "luacli", "cli" },
-    { "luarc", "cli" },
-#ifndef WIN32
-    { "cli", "cli" },
-    { "rc", "cli" },
-#endif
-    { "luahotkeys", "hotkeys" },
-    /* { "hotkeys", "hotkeys" }, */
-    { "luatelnet", "telnet" },
-    { "telnet", "telnet" },
-    { "luahttp", "http" },
-    /* { "http", "http" }, */
-    { NULL, NULL } };
-
-static const char *WordInList( const char *psz_list, const char *psz_word )
+static char *MakeConfig( intf_thread_t *p_intf, const char *name )
 {
-    for( ;; )
+    char *psz_config = NULL;
+
+    if( !strcmp( name, "http" ) )
     {
-        const char *end = strchr( psz_list, ',' );
-        if( end == NULL )
-            break;
+        char *psz_http_src = var_InheritString( p_intf, "http-src" );
+        bool b_http_index = var_InheritBool( p_intf, "http-index" );
+        if( psz_http_src )
+        {
+            char *psz_esc = config_StringEscape( psz_http_src );
 
-        if( !strncmp( psz_list, psz_word, end - psz_list ) )
-            return psz_list;
-        psz_list = end + 1;
+            if( asprintf( &psz_config, "http={dir='%s',no_index=%s}", psz_esc,
+                          b_http_index ? "true" : "false" ) == -1 )
+                psz_config = NULL;
+            free( psz_esc );
+            free( psz_http_src );
+        }
+        else
+        {
+            if( asprintf( &psz_config, "http={no_index=%s}",
+                          b_http_index ? "true" : "false" ) == -1 )
+                psz_config = NULL;
+        }
     }
-    return !strcmp( psz_list, psz_word ) ? psz_list : NULL;
-}
-
-static char *GetModuleName( intf_thread_t *p_intf )
-{
-    int i;
-    const char *psz_intf;
-    /*if( *p_intf->psz_intf == '$' )
-        psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
-    else*/
-        psz_intf = p_intf->psz_intf;
-
-    int i_candidate = -1;
-    const char *psz_candidate = NULL;
-    for( i = 0; pp_shortcuts[i].psz_name; i++ )
+    else if( !strcmp( name, "telnet" ) )
     {
-        const char *psz_match;
-        if( ( psz_match = WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) ) )
+        char *psz_host = var_InheritString( p_intf, "telnet-host" );
+        if( !strcmp( psz_host, "*console" ) )
+            ;
+        else
         {
-            if( !psz_candidate || psz_match < psz_candidate )
+            vlc_url_t url;
+            vlc_UrlParse( &url, psz_host, 0 );
+            unsigned i_port = var_InheritInteger( p_intf, "telnet-port" );
+            if ( url.i_port != 0 )
             {
-                psz_candidate = psz_match;
-                i_candidate = i;
+                if ( i_port == TELNETPORT_DEFAULT )
+                    i_port = url.i_port;
+                else if ( url.i_port != i_port )
+                    msg_Warn( p_intf, "ignoring port %d (using %d)",
+                              url.i_port, i_port );
             }
+
+            char *psz_esc_host = config_StringEscape( url.psz_host );
+            free( psz_host );
+            vlc_UrlClean( &url );
+
+            if( asprintf( &psz_host, "telnet://%s:%d",
+                          psz_esc_host ? psz_esc_host : "", i_port ) == -1 )
+                psz_host = NULL;
+            free( psz_esc_host );
         }
+
+        char *psz_passwd = var_InheritString( p_intf, "telnet-password" );
+
+        char *psz_esc_passwd = config_StringEscape( psz_passwd );
+
+        if( asprintf( &psz_config, "telnet={host='%s',password='%s'}",
+                      psz_host, psz_esc_passwd ) == -1 )
+            psz_config = NULL;
+
+        free( psz_esc_passwd );
+        free( psz_passwd );
+        free( psz_host );
     }
+    else if( !strcmp( name, "cli" ) )
+    {
+        char *psz_rc_host = var_InheritString( p_intf, "rc-host" );
+        if( !psz_rc_host )
+            psz_rc_host = var_InheritString( p_intf, "cli-host" );
+        if( psz_rc_host )
+        {
+            char *psz_esc_host = config_StringEscape( psz_rc_host );
 
-    if( i_candidate >= 0 )
-        return strdup( pp_shortcuts[i_candidate].psz_name );
+            if( asprintf( &psz_config, "cli={host='%s'}", psz_esc_host ) == -1 )
+                psz_config = NULL;
+            free( psz_esc_host );
+            free( psz_rc_host );
+        }
+    }
 
-    return var_CreateGetString( p_intf, "lua-intf" );
+    return psz_config;
 }
 
 static char *StripPasswords( const char *psz_config )
@@ -183,37 +195,38 @@ static char *StripPasswords( const char *psz_config )
 
 static const luaL_Reg p_reg[] = { { NULL, NULL } };
 
-int Open_LuaIntf( vlc_object_t *p_this )
+static int Start_LuaIntf( vlc_object_t *p_this, const char *name )
 {
     intf_thread_t *p_intf = (intf_thread_t*)p_this;
     intf_sys_t *p_sys;
     lua_State *L;
 
     config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
-    char *psz_name = NULL;
 
-    if( !p_intf->psz_intf || !*p_intf->psz_intf )
-        psz_name = strdup( "rc" );
+    if( name == NULL )
+    {
+        char *n = var_InheritString( p_this, "lua-intf" );
+        if( unlikely(n == NULL) )
+            return VLC_EGENERIC;
+        name = p_intf->psz_header = n;
+    }
     else
-        psz_name = GetModuleName( p_intf );
-
-    if( !psz_name ) psz_name = strdup( "dummy" );
-
-    char *psz_config;
-    bool b_config_set = false;
+        /* Cleaned up by vlc_object_release() */
+        p_intf->psz_header = strdup( name );
 
     p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t) );
     if( !p_intf->p_sys )
     {
-        free( psz_name );
+        free( p_intf->psz_header );
+        p_intf->psz_header = NULL;
         return VLC_ENOMEM;
     }
     p_sys = p_intf->p_sys;
-    p_sys->psz_filename = vlclua_find_file( p_this, "intf", psz_name );
+    p_sys->psz_filename = vlclua_find_file( "intf", name );
     if( !p_sys->psz_filename )
     {
         msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
-                 psz_name );
+                 name );
         goto error;
     }
     msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
@@ -226,7 +239,6 @@ int Open_LuaIntf( vlc_object_t *p_this )
     }
 
     vlclua_set_this( L, p_intf );
-    vlclua_set_intf( L, p_sys );
 
     luaL_openlibs( L );
 
@@ -234,7 +246,6 @@ int Open_LuaIntf( vlc_object_t *p_this )
     luaL_register( L, "vlc", p_reg );
 
     /* register submodules */
-    luaopen_acl( L );
     luaopen_config( L );
     luaopen_volume( L );
     luaopen_httpd( L );
@@ -254,13 +265,16 @@ int Open_LuaIntf( vlc_object_t *p_this )
     luaopen_volume( L );
     luaopen_gettext( L );
     luaopen_xml( L );
-    luaopen_md5( L );
+    luaopen_equalizer( L );
+#if defined(_WIN32) && !VLC_WINSTORE_APP
+    luaopen_win( L );
+#endif
 
     /* clean up */
     lua_pop( L, 1 );
 
     /* Setup the module search path */
-    if( vlclua_add_modules_path( p_intf, L, p_sys->psz_filename ) )
+    if( vlclua_add_modules_path( L, p_sys->psz_filename ) )
     {
         msg_Warn( p_intf, "Error while setting the module search path for %s",
                   p_sys->psz_filename );
@@ -273,97 +287,10 @@ int Open_LuaIntf( vlc_object_t *p_this )
      * If the string is empty, try with the old http-* or telnet-* options
      * and build the right configuration line
      */
-    psz_config = var_CreateGetNonEmptyString( p_intf, "lua-config" );
+    bool b_config_set = false;
+    char *psz_config = var_InheritString( p_intf, "lua-config" );
     if( !psz_config )
-    {
-        if( !strcmp( psz_name, "http" ) )
-        {
-            char *psz_http_host = var_CreateGetNonEmptyString( p_intf, "http-host" );
-            char *psz_http_src = var_CreateGetNonEmptyString( p_intf, "http-src" );
-            bool b_http_index = var_CreateGetBool( p_intf, "http-index" );
-            if( psz_http_host )
-            {
-                char *psz_esc = config_StringEscape( psz_http_host );
-                asprintf( &psz_config, "http={host='%s'", psz_esc );
-                free( psz_esc );
-                free( psz_http_host );
-            }
-            if( psz_http_src )
-            {
-                char *psz_esc = config_StringEscape( psz_http_src );
-                if( psz_config )
-                {
-                    char *psz_tmp;
-                    asprintf( &psz_tmp, "%s,dir='%s'", psz_config, psz_esc );
-                    free( psz_config );
-                    psz_config = psz_tmp;
-                }
-                else
-                    asprintf( &psz_config, "http={dir='%s'", psz_esc );
-                free( psz_esc );
-                free( psz_http_src );
-            }
-            if( psz_config )
-            {
-                char *psz_tmp;
-                asprintf( &psz_tmp, "%s,no_index=%s}", psz_config, b_http_index ? "true" : "false" );
-                free( psz_config );
-                psz_config = psz_tmp;
-            }
-            else
-                asprintf( &psz_config, "http={no_index=%s}", b_http_index ? "true" : "false" );
-        }
-        else if( !strcmp( psz_name, "telnet" ) )
-        {
-            char *psz_telnet_host = var_CreateGetString( p_intf, "telnet-host" );
-            if( !strcmp( psz_telnet_host, "*console" ) )
-                ;
-            else
-            {
-                vlc_url_t url;
-                vlc_UrlParse( &url, psz_telnet_host, 0 );
-                int i_telnet_port = var_CreateGetInteger( p_intf, "telnet-port" );
-                if ( url.i_port != 0 )
-                {
-                    if ( i_telnet_port == TELNETPORT_DEFAULT )
-                        i_telnet_port = url.i_port;
-                    else if ( url.i_port != i_telnet_port )
-                        msg_Warn( p_intf, "ignoring port %d (using %d)", url.i_port, i_telnet_port );
-                }
-
-                char *psz_esc_host = config_StringEscape( url.psz_host );
-                free( psz_telnet_host );
-                vlc_UrlClean( &url );
-
-                asprintf( &psz_telnet_host, "telnet://%s:%d", psz_esc_host ? psz_esc_host : "", i_telnet_port );
-                free( psz_esc_host );
-            }
-
-            char *psz_telnet_passwd = var_CreateGetString( p_intf, "telnet-password" );
-
-            char *psz_esc_passwd = config_StringEscape( psz_telnet_passwd );
-
-            asprintf( &psz_config, "telnet={host='%s',password='%s'}", psz_telnet_host, psz_esc_passwd );
-
-            free( psz_esc_passwd );
-            free( psz_telnet_passwd );
-            free( psz_telnet_host );
-        }
-        else if( !strcmp( psz_name, "cli" ) )
-        {
-            char *psz_rc_host = var_CreateGetNonEmptyString( p_intf, "rc-host" );
-            if( !psz_rc_host )
-                psz_rc_host = var_CreateGetNonEmptyString( p_intf, "cli-host" );
-            if( psz_rc_host )
-            {
-                char *psz_esc_host = config_StringEscape( psz_rc_host );
-                asprintf( &psz_config, "cli={host='%s'}", psz_esc_host );
-
-                free( psz_esc_host );
-                free( psz_rc_host );
-            }
-        }
-    }
+        psz_config = MakeConfig( p_intf, name );
 
     if( psz_config )
     {
@@ -383,7 +310,7 @@ int Open_LuaIntf( vlc_object_t *p_this )
             lua_getglobal( L, "config" );
             if( lua_istable( L, -1 ) )
             {
-                if( !strcmp( psz_name, "cli" ) )
+                if( !strcmp( name, "cli" ) )
                 {
                     lua_getfield( L, -1, "rc" );
                     if( lua_istable( L, -1 ) )
@@ -396,7 +323,7 @@ int Open_LuaIntf( vlc_object_t *p_this )
                     else
                         lua_pop( L, 1 );
                 }
-                lua_getfield( L, -1, psz_name );
+                lua_getfield( L, -1, name );
                 if( lua_istable( L, -1 ) )
                 {
                     lua_setglobal( L, "config" );
@@ -414,17 +341,16 @@ int Open_LuaIntf( vlc_object_t *p_this )
     }
 
     /* Wrapper for legacy telnet config */
-    if ( !strcmp( psz_name, "telnet" ) )
+    if ( !strcmp( name, "telnet" ) )
     {
         /* msg_Warn( p_intf, "The `telnet' lua interface script was replaced "
                           "by `cli', please update your configuration!" ); */
 
-        char *wrapped_file = vlclua_find_file( p_this, "intf", "cli" );
+        char *wrapped_file = vlclua_find_file( "intf", "cli" );
         if( !wrapped_file )
         {
             msg_Err( p_intf, "Couldn't find lua interface script \"cli\", "
                              "needed by telnet wrapper" );
-            p_intf->psz_header = NULL;
             lua_close( p_sys->L );
             goto error;
         }
@@ -435,18 +361,16 @@ int Open_LuaIntf( vlc_object_t *p_this )
 
     p_sys->L = L;
 
-    p_intf->psz_header = psz_name;
-    /* ^^ Do I need to clean that up myself in Close_LuaIntf? */
-
-    vlc_mutex_init( &p_sys->lock );
-    vlc_cond_init( &p_sys->wait );
-    p_sys->exiting = false;
+    if( vlc_pipe( p_sys->fd ) )
+    {
+        lua_close( p_sys->L );
+        goto error;
+    }
 
     if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
     {
-        p_intf->psz_header = NULL;
-        vlc_cond_destroy( &p_sys->wait );
-        vlc_mutex_destroy( &p_sys->lock );
+        close( p_sys->fd[1] );
+        close( p_sys->fd[0] );
         lua_close( p_sys->L );
         goto error;
     }
@@ -455,7 +379,8 @@ int Open_LuaIntf( vlc_object_t *p_this )
 error:
     free( p_sys->psz_filename );
     free( p_sys );
-    free( psz_name );
+    free( p_intf->psz_header );
+    p_intf->psz_header = NULL;
     return VLC_EGENERIC;
 }
 
@@ -464,18 +389,11 @@ void Close_LuaIntf( vlc_object_t *p_this )
     intf_thread_t *p_intf = (intf_thread_t*)p_this;
     intf_sys_t *p_sys = p_intf->p_sys;
 
-    vlc_cancel( p_sys->thread );
-
-    vlc_mutex_lock( &p_sys->lock );
-    p_sys->exiting = true;
-    vlc_cond_signal( &p_sys->wait );
-    vlc_mutex_unlock( &p_sys->lock );
+    close( p_sys->fd[1] );
     vlc_join( p_sys->thread, NULL );
-    vlc_cond_destroy( &p_sys->wait );
-    vlc_mutex_destroy( &p_sys->lock );
 
     lua_close( p_sys->L );
-
+    close( p_sys->fd[0] );
     free( p_sys->psz_filename );
     free( p_sys );
 }
@@ -494,3 +412,31 @@ static void *Run( void *data )
     }
     return NULL;
 }
+
+int Open_LuaIntf( vlc_object_t *p_this )
+{
+    return Start_LuaIntf( p_this, NULL );
+}
+
+int Open_LuaHTTP( vlc_object_t *p_this )
+{
+    return Start_LuaIntf( p_this, "http" );
+}
+
+int Open_LuaCLI( vlc_object_t *p_this )
+{
+    return Start_LuaIntf( p_this, "cli" );
+}
+
+int Open_LuaTelnet( vlc_object_t *p_this )
+{
+    char *pw = var_CreateGetNonEmptyString( p_this, "telnet-password" );
+    if( pw == NULL )
+    {
+        msg_Err( p_this, "password not configured" );
+        msg_Info( p_this, "Please specify the password in the preferences." );
+        return VLC_EGENERIC;
+    }
+    free( pw );
+    return Start_LuaIntf( p_this, "telnet" );
+}