contrib: soxr: fix build on WIN32
[vlc.git] / modules / lua / intf.c
blob7218a99380415afd56aec5f0f93202dded4e640c
1 /*****************************************************************************
2 * intf.c: Generic lua interface functions
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
5 * $Id$
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
35 #include <vlc_common.h>
36 #include <vlc_interface.h>
37 #include <vlc_fs.h>
39 #include "vlc.h"
40 #include "libs.h"
42 /*****************************************************************************
43 * Prototypes
44 *****************************************************************************/
45 static void *Run( void * );
47 static const char * const ppsz_intf_options[] = { "intf", "config", NULL };
49 /*****************************************************************************
50 * Local structures
51 *****************************************************************************/
52 struct intf_sys_t
54 char *psz_filename;
55 lua_State *L;
56 vlc_thread_t thread;
57 vlclua_dtable_t dtable;
59 /*****************************************************************************
61 *****************************************************************************/
63 static char *MakeConfig( intf_thread_t *p_intf, const char *name )
65 char *psz_config = NULL;
67 if( !strcmp( name, "http" ) )
69 char *psz_http_src = var_InheritString( p_intf, "http-src" );
70 bool b_http_index = var_InheritBool( p_intf, "http-index" );
71 if( psz_http_src )
73 char *psz_esc = config_StringEscape( psz_http_src );
75 if( asprintf( &psz_config, "http={dir='%s',no_index=%s}", psz_esc,
76 b_http_index ? "true" : "false" ) == -1 )
77 psz_config = NULL;
78 free( psz_esc );
79 free( psz_http_src );
81 else
83 if( asprintf( &psz_config, "http={no_index=%s}",
84 b_http_index ? "true" : "false" ) == -1 )
85 psz_config = NULL;
88 else if( !strcmp( name, "telnet" ) )
90 char *psz_host = var_InheritString( p_intf, "telnet-host" );
91 if( !strcmp( psz_host, "*console" ) )
93 else
95 vlc_url_t url;
96 vlc_UrlParse( &url, psz_host );
97 unsigned i_port = var_InheritInteger( p_intf, "telnet-port" );
98 if ( url.i_port != 0 )
100 if ( i_port == TELNETPORT_DEFAULT )
101 i_port = url.i_port;
102 else if ( url.i_port != i_port )
103 msg_Warn( p_intf, "ignoring port %d (using %d)",
104 url.i_port, i_port );
107 char *psz_esc_host = config_StringEscape( url.psz_host );
108 free( psz_host );
109 vlc_UrlClean( &url );
111 if( asprintf( &psz_host, "telnet://%s:%d",
112 psz_esc_host ? psz_esc_host : "", i_port ) == -1 )
113 psz_host = NULL;
114 free( psz_esc_host );
117 char *psz_passwd = var_InheritString( p_intf, "telnet-password" );
119 char *psz_esc_passwd = config_StringEscape( psz_passwd );
121 if( asprintf( &psz_config, "telnet={host='%s',password='%s'}",
122 psz_host, psz_esc_passwd ) == -1 )
123 psz_config = NULL;
125 free( psz_esc_passwd );
126 free( psz_passwd );
127 free( psz_host );
129 else if( !strcmp( name, "cli" ) )
131 char *psz_rc_host = var_InheritString( p_intf, "rc-host" );
132 if( !psz_rc_host )
133 psz_rc_host = var_InheritString( p_intf, "cli-host" );
134 if( psz_rc_host )
136 char *psz_esc_host = config_StringEscape( psz_rc_host );
138 if( asprintf( &psz_config, "cli={host='%s'}", psz_esc_host ) == -1 )
139 psz_config = NULL;
140 free( psz_esc_host );
141 free( psz_rc_host );
145 return psz_config;
148 static char *StripPasswords( const char *psz_config )
150 unsigned n = 0;
151 const char *p = psz_config;
152 while ((p = strstr(p, "password=")) != NULL)
154 n++;
155 p++;
157 if (n == 0)
158 return strdup(psz_config);
160 char *psz_log = malloc(strlen(psz_config) + n * strlen("******") + 1);
161 if (psz_log == NULL)
162 return NULL;
163 psz_log[0] = '\0';
165 for (p = psz_config; ; )
167 const char *pwd = strstr(p, "password=");
168 if (pwd == NULL)
170 /* Copy the last, ending bit */
171 strcat(psz_log, p);
172 break;
174 pwd += strlen("password=");
176 char delim[3] = ",}";
177 if (*pwd == '\'' || *pwd == '"')
179 delim[0] = *pwd++;
180 delim[1] = '\0';
183 strncat(psz_log, p, pwd - p);
184 strcat(psz_log, "******");
186 /* Advance to the delimiter at the end of the password */
187 p = pwd - 1;
190 p = strpbrk(p + 1, delim);
191 if (p == NULL)
192 /* Oops, unbalanced quotes or brackets */
193 return psz_log;
195 while (*(p - 1) == '\\');
197 return psz_log;
200 static const luaL_Reg p_reg[] = { { NULL, NULL } };
202 static int Start_LuaIntf( vlc_object_t *p_this, const char *name )
204 if( lua_Disabled( p_this ) )
205 return VLC_EGENERIC;
207 intf_thread_t *p_intf = (intf_thread_t*)p_this;
208 lua_State *L;
210 config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
212 if( name == NULL )
214 char *n = var_InheritString( p_this, "lua-intf" );
215 if( unlikely(n == NULL) )
216 return VLC_EGENERIC;
217 name = p_intf->obj.header = n;
219 else
220 /* Cleaned up by vlc_object_release() */
221 p_intf->obj.header = strdup( name );
223 intf_sys_t *p_sys = malloc( sizeof(*p_sys) );
224 if( unlikely(p_sys == NULL) )
226 free( p_intf->obj.header );
227 p_intf->obj.header = NULL;
228 return VLC_ENOMEM;
230 p_intf->p_sys = p_sys;
232 p_sys->psz_filename = vlclua_find_file( "intf", name );
233 if( !p_sys->psz_filename )
235 msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
236 name );
237 goto error;
239 msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
241 L = luaL_newstate();
242 if( !L )
244 msg_Err( p_intf, "Could not create new Lua State" );
245 goto error;
248 vlclua_set_this( L, p_intf );
249 vlclua_set_playlist_internal( L, pl_Get(p_intf) );
251 luaL_openlibs( L );
253 /* register our functions */
254 luaL_register_namespace( L, "vlc", p_reg );
256 /* register submodules */
257 luaopen_config( L );
258 luaopen_httpd( L );
259 luaopen_input( L );
260 luaopen_msg( L );
261 luaopen_misc( L );
262 if( vlclua_fd_init( L, &p_sys->dtable ) )
264 lua_close( L );
265 goto error;
267 luaopen_object( L );
268 luaopen_osd( L );
269 luaopen_playlist( L );
270 luaopen_sd_intf( L );
271 luaopen_stream( L );
272 luaopen_strings( L );
273 luaopen_variables( L );
274 luaopen_video( L );
275 luaopen_vlm( L );
276 luaopen_volume( L );
277 luaopen_gettext( L );
278 luaopen_xml( L );
279 luaopen_equalizer( L );
280 #if defined(_WIN32) && !VLC_WINSTORE_APP
281 luaopen_win( L );
282 #endif
284 /* clean up */
285 lua_pop( L, 1 );
287 /* Setup the module search path */
288 if( vlclua_add_modules_path( L, p_sys->psz_filename ) )
290 msg_Warn( p_intf, "Error while setting the module search path for %s",
291 p_sys->psz_filename );
292 lua_close( L );
293 goto error;
297 * Get the lua-config string.
298 * If the string is empty, try with the old http-* or telnet-* options
299 * and build the right configuration line
301 bool b_config_set = false;
302 char *psz_config = var_InheritString( p_intf, "lua-config" );
303 if( !psz_config )
304 psz_config = MakeConfig( p_intf, name );
306 if( psz_config )
308 char *psz_buffer;
309 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
311 char *psz_log = StripPasswords( psz_buffer );
312 if( psz_log != NULL )
314 msg_Dbg( p_intf, "Setting config variable: %s", psz_log );
315 free( psz_log );
318 if( luaL_dostring( L, psz_buffer ) == 1 )
319 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
320 free( psz_buffer );
321 lua_getglobal( L, "config" );
322 if( lua_istable( L, -1 ) )
324 if( !strcmp( name, "cli" ) )
326 lua_getfield( L, -1, "rc" );
327 if( lua_istable( L, -1 ) )
329 /* msg_Warn( p_intf, "The `rc' lua interface script "
330 "was renamed `cli', please update "
331 "your configuration!" ); */
332 lua_setfield( L, -2, "cli" );
334 else
335 lua_pop( L, 1 );
337 lua_getfield( L, -1, name );
338 if( lua_istable( L, -1 ) )
340 lua_setglobal( L, "config" );
341 b_config_set = true;
345 free( psz_config );
348 if( !b_config_set )
350 lua_newtable( L );
351 lua_setglobal( L, "config" );
354 /* Wrapper for legacy telnet config */
355 if ( !strcmp( name, "telnet" ) )
357 /* msg_Warn( p_intf, "The `telnet' lua interface script was replaced "
358 "by `cli', please update your configuration!" ); */
360 char *wrapped_file = vlclua_find_file( "intf", "cli" );
361 if( !wrapped_file )
363 msg_Err( p_intf, "Couldn't find lua interface script \"cli\", "
364 "needed by telnet wrapper" );
365 lua_close( p_sys->L );
366 goto error;
368 lua_pushstring( L, wrapped_file );
369 lua_setglobal( L, "wrapped_file" );
370 free( wrapped_file );
373 p_sys->L = L;
375 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
377 vlclua_fd_cleanup( &p_sys->dtable );
378 lua_close( p_sys->L );
379 goto error;
382 return VLC_SUCCESS;
383 error:
384 free( p_sys->psz_filename );
385 free( p_sys );
386 free( p_intf->obj.header );
387 p_intf->obj.header = NULL;
388 return VLC_EGENERIC;
391 void Close_LuaIntf( vlc_object_t *p_this )
393 intf_thread_t *p_intf = (intf_thread_t*)p_this;
394 intf_sys_t *p_sys = p_intf->p_sys;
396 vlclua_fd_interrupt( &p_sys->dtable );
397 vlc_join( p_sys->thread, NULL );
399 lua_close( p_sys->L );
400 vlclua_fd_cleanup( &p_sys->dtable );
401 free( p_sys->psz_filename );
402 free( p_sys );
405 static void *Run( void *data )
407 intf_thread_t *p_intf = data;
408 intf_sys_t *p_sys = p_intf->p_sys;
409 lua_State *L = p_sys->L;
411 if( vlclua_dofile( VLC_OBJECT(p_intf), L, p_sys->psz_filename ) )
413 msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
414 lua_tostring( L, lua_gettop( L ) ) );
415 lua_pop( L, 1 );
417 return NULL;
420 int Open_LuaIntf( vlc_object_t *p_this )
422 return Start_LuaIntf( p_this, NULL );
425 int Open_LuaHTTP( vlc_object_t *p_this )
427 return Start_LuaIntf( p_this, "http" );
430 int Open_LuaCLI( vlc_object_t *p_this )
432 return Start_LuaIntf( p_this, "cli" );
435 int Open_LuaTelnet( vlc_object_t *p_this )
437 char *pw = var_CreateGetNonEmptyString( p_this, "telnet-password" );
438 if( pw == NULL )
440 msg_Err( p_this, "password not configured" );
441 msg_Info( p_this, "Please specify the password in the preferences." );
442 return VLC_EGENERIC;
444 free( pw );
445 return Start_LuaIntf( p_this, "telnet" );