Decklink: Fix swapped format arguments
[vlc.git] / modules / lua / intf.c
blob782b2c7708aba6d8d2d77227a2ee9d1025112644
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 luaopen_vlcio( L );
281 luaopen_errno( L );
282 luaopen_rand( L );
283 #if defined(_WIN32) && !VLC_WINSTORE_APP
284 luaopen_win( L );
285 #endif
287 /* clean up */
288 lua_pop( L, 1 );
290 /* Setup the module search path */
291 if( vlclua_add_modules_path( L, p_sys->psz_filename ) )
293 msg_Warn( p_intf, "Error while setting the module search path for %s",
294 p_sys->psz_filename );
295 lua_close( L );
296 goto error;
300 * Get the lua-config string.
301 * If the string is empty, try with the old http-* or telnet-* options
302 * and build the right configuration line
304 bool b_config_set = false;
305 char *psz_config = var_InheritString( p_intf, "lua-config" );
306 if( !psz_config )
307 psz_config = MakeConfig( p_intf, name );
309 if( psz_config )
311 char *psz_buffer;
312 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
314 char *psz_log = StripPasswords( psz_buffer );
315 if( psz_log != NULL )
317 msg_Dbg( p_intf, "Setting config variable: %s", psz_log );
318 free( psz_log );
321 if( luaL_dostring( L, psz_buffer ) == 1 )
322 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
323 free( psz_buffer );
324 lua_getglobal( L, "config" );
325 if( lua_istable( L, -1 ) )
327 if( !strcmp( name, "cli" ) )
329 lua_getfield( L, -1, "rc" );
330 if( lua_istable( L, -1 ) )
332 /* msg_Warn( p_intf, "The `rc' lua interface script "
333 "was renamed `cli', please update "
334 "your configuration!" ); */
335 lua_setfield( L, -2, "cli" );
337 else
338 lua_pop( L, 1 );
340 lua_getfield( L, -1, name );
341 if( lua_istable( L, -1 ) )
343 lua_setglobal( L, "config" );
344 b_config_set = true;
348 free( psz_config );
351 if( !b_config_set )
353 lua_newtable( L );
354 lua_setglobal( L, "config" );
357 /* Wrapper for legacy telnet config */
358 if ( !strcmp( name, "telnet" ) )
360 /* msg_Warn( p_intf, "The `telnet' lua interface script was replaced "
361 "by `cli', please update your configuration!" ); */
363 char *wrapped_file = vlclua_find_file( "intf", "cli" );
364 if( !wrapped_file )
366 msg_Err( p_intf, "Couldn't find lua interface script \"cli\", "
367 "needed by telnet wrapper" );
368 lua_close( p_sys->L );
369 goto error;
371 lua_pushstring( L, wrapped_file );
372 lua_setglobal( L, "wrapped_file" );
373 free( wrapped_file );
376 p_sys->L = L;
378 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
380 vlclua_fd_cleanup( &p_sys->dtable );
381 lua_close( p_sys->L );
382 goto error;
385 return VLC_SUCCESS;
386 error:
387 free( p_sys->psz_filename );
388 free( p_sys );
389 free( p_intf->obj.header );
390 p_intf->obj.header = NULL;
391 return VLC_EGENERIC;
394 void Close_LuaIntf( vlc_object_t *p_this )
396 intf_thread_t *p_intf = (intf_thread_t*)p_this;
397 intf_sys_t *p_sys = p_intf->p_sys;
399 vlclua_fd_interrupt( &p_sys->dtable );
400 vlc_join( p_sys->thread, NULL );
402 lua_close( p_sys->L );
403 vlclua_fd_cleanup( &p_sys->dtable );
404 free( p_sys->psz_filename );
405 free( p_sys );
408 static void *Run( void *data )
410 intf_thread_t *p_intf = data;
411 intf_sys_t *p_sys = p_intf->p_sys;
412 lua_State *L = p_sys->L;
414 if( vlclua_dofile( VLC_OBJECT(p_intf), L, p_sys->psz_filename ) )
416 msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
417 lua_tostring( L, lua_gettop( L ) ) );
418 lua_pop( L, 1 );
420 return NULL;
423 int Open_LuaIntf( vlc_object_t *p_this )
425 return Start_LuaIntf( p_this, NULL );
428 int Open_LuaHTTP( vlc_object_t *p_this )
430 return Start_LuaIntf( p_this, "http" );
433 int Open_LuaCLI( vlc_object_t *p_this )
435 return Start_LuaIntf( p_this, "cli" );
438 int Open_LuaTelnet( vlc_object_t *p_this )
440 char *pw = var_CreateGetNonEmptyString( p_this, "telnet-password" );
441 if( pw == NULL )
443 msg_Err( p_this, "password not configured" );
444 msg_Info( p_this, "Please specify the password in the preferences." );
445 return VLC_EGENERIC;
447 free( pw );
448 return Start_LuaIntf( p_this, "telnet" );