]> git.sesse.net Git - vlc/blob - modules/control/http/rpn.c
Separate socket/resolv stuff (network.h) from url stuff (vlc_url.h)
[vlc] / modules / control / http / rpn.c
1 /*****************************************************************************
2  * rpn.c : RPN evaluator for the HTTP Interface
3  *****************************************************************************
4  * Copyright (C) 2001-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #include "http.h"
27 #include "vlc_url.h"
28
29 static vlc_object_t *GetVLCObject( intf_thread_t *p_intf,
30                                    const char *psz_object,
31                                    vlc_bool_t *pb_need_release )
32 {
33     intf_sys_t    *p_sys = p_intf->p_sys;
34     int i_object_type = 0;
35     vlc_object_t *p_object = NULL;
36     *pb_need_release = VLC_FALSE;
37
38     if( !strcmp( psz_object, "VLC_OBJECT_ROOT" ) )
39         i_object_type = VLC_OBJECT_ROOT;
40     else if( !strcmp( psz_object, "VLC_OBJECT_VLC" ) )
41         p_object = VLC_OBJECT(p_intf->p_vlc);
42     else if( !strcmp( psz_object, "VLC_OBJECT_INTF" ) )
43         p_object = VLC_OBJECT(p_intf);
44     else if( !strcmp( psz_object, "VLC_OBJECT_PLAYLIST" ) )
45         p_object = VLC_OBJECT(p_sys->p_playlist);
46     else if( !strcmp( psz_object, "VLC_OBJECT_INPUT" ) )
47         p_object = VLC_OBJECT(p_sys->p_input);
48     else if( !strcmp( psz_object, "VLC_OBJECT_VOUT" ) )
49         i_object_type = VLC_OBJECT_VOUT;
50     else if( !strcmp( psz_object, "VLC_OBJECT_AOUT" ) )
51         i_object_type = VLC_OBJECT_AOUT;
52     else if( !strcmp( psz_object, "VLC_OBJECT_SOUT" ) )
53         i_object_type = VLC_OBJECT_SOUT;
54     else
55         msg_Warn( p_intf, "unknown object type (%s)", psz_object );
56
57     if( p_object == NULL && i_object_type )
58     {
59         *pb_need_release = VLC_TRUE;
60         p_object = vlc_object_find( p_intf, i_object_type, FIND_ANYWHERE );
61     }
62
63     return p_object;
64 }
65
66 void E_(SSInit)( rpn_stack_t *st )
67 {
68     st->i_stack = 0;
69 }
70
71 void E_(SSClean)( rpn_stack_t *st )
72 {
73     while( st->i_stack > 0 )
74     {
75         free( st->stack[--st->i_stack] );
76     }
77 }
78
79 void E_(SSPush)( rpn_stack_t *st, const char *s )
80 {
81     if( st->i_stack < STACK_MAX )
82     {
83         st->stack[st->i_stack++] = strdup( s );
84     }
85 }
86
87 char *E_(SSPop)( rpn_stack_t *st )
88 {
89     if( st->i_stack <= 0 )
90     {
91         return strdup( "" );
92     }
93     else
94     {
95         return st->stack[--st->i_stack];
96     }
97 }
98
99 int E_(SSPopN)( rpn_stack_t *st, mvar_t  *vars )
100 {
101     char *name;
102     char *value;
103
104     char *end;
105     int  i;
106
107     name = E_(SSPop)( st );
108     i = strtol( name, &end, 0 );
109     if( end == name )
110     {
111         value = E_(mvar_GetValue)( vars, name );
112         i = atoi( value );
113     }
114     free( name );
115
116     return( i );
117 }
118
119 void E_(SSPushN)( rpn_stack_t *st, int i )
120 {
121     char v[512];
122
123     sprintf( v, "%d", i );
124     E_(SSPush)( st, v );
125 }
126
127 void E_(EvaluateRPN)( intf_thread_t *p_intf, mvar_t  *vars,
128                       rpn_stack_t *st, char *exp )
129 {
130     intf_sys_t    *p_sys = p_intf->p_sys;
131
132     while( exp != NULL && *exp != '\0' )
133     {
134         char *p, *s;
135
136         /* skip space */
137         while( *exp == ' ' )
138         {
139             exp++;
140         }
141
142         if( *exp == '\'' )
143         {
144             /* extract string */
145             p = E_(FirstWord)( exp, exp );
146             E_(SSPush)( st, exp );
147             exp = p;
148             continue;
149         }
150
151         /* extract token */
152         p = E_(FirstWord)( exp, exp );
153         s = exp;
154         if( p == NULL )
155         {
156             exp += strlen( exp );
157         }
158         else
159         {
160             exp = p;
161         }
162
163         if( *s == '\0' )
164         {
165             break;
166         }
167
168         /* 1. Integer function */
169         if( !strcmp( s, "!" ) )
170         {
171             E_(SSPushN)( st, ~E_(SSPopN)( st, vars ) );
172         }
173         else if( !strcmp( s, "^" ) )
174         {
175             E_(SSPushN)( st, E_(SSPopN)( st, vars ) ^ E_(SSPopN)( st, vars ) );
176         }
177         else if( !strcmp( s, "&" ) )
178         {
179             E_(SSPushN)( st, E_(SSPopN)( st, vars ) & E_(SSPopN)( st, vars ) );
180         }
181         else if( !strcmp( s, "|" ) )
182         {
183             E_(SSPushN)( st, E_(SSPopN)( st, vars ) | E_(SSPopN)( st, vars ) );
184         }
185         else if( !strcmp( s, "+" ) )
186         {
187             E_(SSPushN)( st, E_(SSPopN)( st, vars ) + E_(SSPopN)( st, vars ) );
188         }
189         else if( !strcmp( s, "-" ) )
190         {
191             int j = E_(SSPopN)( st, vars );
192             int i = E_(SSPopN)( st, vars );
193             E_(SSPushN)( st, i - j );
194         }
195         else if( !strcmp( s, "*" ) )
196         {
197             E_(SSPushN)( st, E_(SSPopN)( st, vars ) * E_(SSPopN)( st, vars ) );
198         }
199         else if( !strcmp( s, "/" ) )
200         {
201             int i, j;
202
203             j = E_(SSPopN)( st, vars );
204             i = E_(SSPopN)( st, vars );
205
206             E_(SSPushN)( st, j != 0 ? i / j : 0 );
207         }
208         else if( !strcmp( s, "%" ) )
209         {
210             int i, j;
211
212             j = E_(SSPopN)( st, vars );
213             i = E_(SSPopN)( st, vars );
214
215             E_(SSPushN)( st, j != 0 ? i % j : 0 );
216         }
217         /* 2. integer tests */
218         else if( !strcmp( s, "=" ) )
219         {
220             E_(SSPushN)( st, E_(SSPopN)( st, vars ) == E_(SSPopN)( st, vars ) ? -1 : 0 );
221         }
222         else if( !strcmp( s, "!=" ) )
223         {
224             E_(SSPushN)( st, E_(SSPopN)( st, vars ) != E_(SSPopN)( st, vars ) ? -1 : 0 );
225         }
226         else if( !strcmp( s, "<" ) )
227         {
228             int j = E_(SSPopN)( st, vars );
229             int i = E_(SSPopN)( st, vars );
230
231             E_(SSPushN)( st, i < j ? -1 : 0 );
232         }
233         else if( !strcmp( s, ">" ) )
234         {
235             int j = E_(SSPopN)( st, vars );
236             int i = E_(SSPopN)( st, vars );
237
238             E_(SSPushN)( st, i > j ? -1 : 0 );
239         }
240         else if( !strcmp( s, "<=" ) )
241         {
242             int j = E_(SSPopN)( st, vars );
243             int i = E_(SSPopN)( st, vars );
244
245             E_(SSPushN)( st, i <= j ? -1 : 0 );
246         }
247         else if( !strcmp( s, ">=" ) )
248         {
249             int j = E_(SSPopN)( st, vars );
250             int i = E_(SSPopN)( st, vars );
251
252             E_(SSPushN)( st, i >= j ? -1 : 0 );
253         }
254         /* 3. string functions */
255         else if( !strcmp( s, "strcat" ) )
256         {
257             char *s2 = E_(SSPop)( st );
258             char *s1 = E_(SSPop)( st );
259             char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
260
261             strcpy( str, s1 );
262             strcat( str, s2 );
263
264             E_(SSPush)( st, str );
265             free( s1 );
266             free( s2 );
267             free( str );
268         }
269         else if( !strcmp( s, "strcmp" ) )
270         {
271             char *s2 = E_(SSPop)( st );
272             char *s1 = E_(SSPop)( st );
273
274             E_(SSPushN)( st, strcmp( s1, s2 ) );
275             free( s1 );
276             free( s2 );
277         }
278         else if( !strcmp( s, "strncmp" ) )
279         {
280             int n = E_(SSPopN)( st, vars );
281             char *s2 = E_(SSPop)( st );
282             char *s1 = E_(SSPop)( st );
283
284             E_(SSPushN)( st, strncmp( s1, s2 , n ) );
285             free( s1 );
286             free( s2 );
287         }
288         else if( !strcmp( s, "strsub" ) )
289         {
290             int n = E_(SSPopN)( st, vars );
291             int m = E_(SSPopN)( st, vars );
292             int i_len;
293             char *s = E_(SSPop)( st );
294             char *str;
295
296             if( n >= m )
297             {
298                 i_len = n - m + 1;
299             }
300             else
301             {
302                 i_len = 0;
303             }
304
305             str = malloc( i_len + 1 );
306
307             memcpy( str, s + m - 1, i_len );
308             str[ i_len ] = '\0';
309
310             E_(SSPush)( st, str );
311             free( s );
312             free( str );
313         }
314         else if( !strcmp( s, "strlen" ) )
315         {
316             char *str = E_(SSPop)( st );
317
318             E_(SSPushN)( st, strlen( str ) );
319             free( str );
320         }
321         else if( !strcmp( s, "str_replace" ) )
322         {
323             char *psz_to = E_(SSPop)( st );
324             char *psz_from = E_(SSPop)( st );
325             char *psz_in = E_(SSPop)( st );
326             char *psz_in_current = psz_in;
327             char *psz_out = malloc( strlen(psz_in) * strlen(psz_to) + 1 );
328             char *psz_out_current = psz_out;
329
330             while( (p = strstr( psz_in_current, psz_from )) != NULL )
331             {
332                 memcpy( psz_out_current, psz_in_current, p - psz_in_current );
333                 psz_out_current += p - psz_in_current;
334                 strcpy( psz_out_current, psz_to );
335                 psz_out_current += strlen(psz_to);
336                 psz_in_current = p + strlen(psz_from);
337             }
338             strcpy( psz_out_current, psz_in_current );
339             psz_out_current += strlen(psz_in_current);
340             *psz_out_current = '\0';
341
342             E_(SSPush)( st, psz_out );
343             free( psz_to );
344             free( psz_from );
345             free( psz_in );
346             free( psz_out );
347         }
348         else if( !strcmp( s, "url_extract" ) )
349         {
350             char *url = E_(mvar_GetValue)( vars, "url_value" );
351             char *name = E_(SSPop)( st );
352             char value[512];
353             char *tmp;
354
355             E_(ExtractURIValue)( url, name, value, 512 );
356             E_(DecodeEncodedURI)( value );
357             tmp = E_(FromUTF8)( p_intf, value );
358             E_(SSPush)( st, tmp );
359             free( tmp );
360             free( name );
361         }
362         else if( !strcmp( s, "url_encode" ) )
363         {
364             char *url = E_(SSPop)( st );
365             char *value;
366
367             value = E_(ToUTF8)( p_intf, url );
368             free( url );
369             url = value;
370             value = vlc_UrlEncode( url );
371             free( url );
372             E_(SSPush)( st, value );
373             free( value );
374         }
375         else if( !strcmp( s, "addslashes" ) )
376         {
377             char *psz_src = E_(SSPop)( st );
378             char *psz_dest;
379             char *str = psz_src;
380
381             p = psz_dest = malloc( strlen( str ) * 2 + 1 );
382
383             while( *str != '\0' )
384             {
385                 if( *str == '"' || *str == '\'' || *str == '\\' )
386                 {
387                     *p++ = '\\';
388                 }
389                 *p++ = *str;
390                 str++;
391             }
392             *p = '\0';
393
394             E_(SSPush)( st, psz_dest );
395             free( psz_src );
396             free( psz_dest );
397         }
398         else if( !strcmp( s, "stripslashes" ) )
399         {
400             char *psz_src = E_(SSPop)( st );
401             char *psz_dest;
402
403             p = psz_dest = strdup( psz_src );
404
405             while( *psz_src )
406             {
407                 if( *psz_src == '\\' && *(psz_src + 1) )
408                 {
409                     psz_src++;
410                 }
411                 *p++ = *psz_src++;
412             }
413             *p = '\0';
414
415             E_(SSPush)( st, psz_dest );
416             free( psz_src );
417             free( psz_dest );
418         }
419         else if( !strcmp( s, "htmlspecialchars" ) )
420         {
421             char *psz_src = E_(SSPop)( st );
422             char *psz_dest;
423             char *str = psz_src;
424
425             p = psz_dest = malloc( strlen( str ) * 6 + 1 );
426
427             while( *str != '\0' )
428             {
429                 if( *str == '&' )
430                 {
431                     strcpy( p, "&amp;" );
432                     p += 5;
433                 }
434                 else if( *str == '\"' )
435                 {
436                     strcpy( p, "&quot;" );
437                     p += 6;
438                 }
439                 else if( *str == '\'' )
440                 {
441                     strcpy( p, "&#039;" );
442                     p += 6;
443                 }
444                 else if( *str == '<' )
445                 {
446                     strcpy( p, "&lt;" );
447                     p += 4;
448                 }
449                 else if( *str == '>' )
450                 {
451                     strcpy( p, "&gt;" );
452                     p += 4;
453                 }
454                 else
455                 {
456                     *p++ = *str;
457                 }
458                 str++;
459             }
460             *p = '\0';
461
462             E_(SSPush)( st, psz_dest );
463             free( psz_src );
464             free( psz_dest );
465         }
466         else if( !strcmp( s, "realpath" ) )
467         {
468             char *psz_src = E_(SSPop)( st );
469             char *psz_dir = E_(RealPath)( p_intf, psz_src );
470
471             E_(SSPush)( st, psz_dir );
472             free( psz_src );
473             free( psz_dir );
474         }
475         /* 4. stack functions */
476         else if( !strcmp( s, "dup" ) )
477         {
478             char *str = E_(SSPop)( st );
479             E_(SSPush)( st, str );
480             E_(SSPush)( st, str );
481             free( str );
482         }
483         else if( !strcmp( s, "drop" ) )
484         {
485             char *str = E_(SSPop)( st );
486             free( str );
487         }
488         else if( !strcmp( s, "swap" ) )
489         {
490             char *s1 = E_(SSPop)( st );
491             char *s2 = E_(SSPop)( st );
492
493             E_(SSPush)( st, s1 );
494             E_(SSPush)( st, s2 );
495             free( s1 );
496             free( s2 );
497         }
498         else if( !strcmp( s, "flush" ) )
499         {
500             E_(SSClean)( st );
501             E_(SSInit)( st );
502         }
503         else if( !strcmp( s, "store" ) )
504         {
505             char *value = E_(SSPop)( st );
506             char *name  = E_(SSPop)( st );
507
508             E_(mvar_PushNewVar)( vars, name, value );
509             free( name );
510             free( value );
511         }
512         else if( !strcmp( s, "value" ) )
513         {
514             char *name  = E_(SSPop)( st );
515             char *value = E_(mvar_GetValue)( vars, name );
516
517             E_(SSPush)( st, value );
518
519             free( name );
520         }
521         /* 5. player control */
522         else if( !strcmp( s, "vlc_play" ) )
523         {
524             int i_id = E_(SSPopN)( st, vars );
525             int i_ret;
526
527             i_ret = playlist_Control( p_sys->p_playlist, PLAYLIST_ITEMPLAY,
528                                       playlist_ItemGetById( p_sys->p_playlist,
529                                       i_id ) );
530             msg_Dbg( p_intf, "requested playlist item: %i", i_id );
531             E_(SSPushN)( st, i_ret );
532         }
533         else if( !strcmp( s, "vlc_stop" ) )
534         {
535             playlist_Control( p_sys->p_playlist, PLAYLIST_STOP );
536             msg_Dbg( p_intf, "requested playlist stop" );
537         }
538         else if( !strcmp( s, "vlc_pause" ) )
539         {
540             playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
541             msg_Dbg( p_intf, "requested playlist pause" );
542         }
543         else if( !strcmp( s, "vlc_next" ) )
544         {
545             playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
546             msg_Dbg( p_intf, "requested playlist next" );
547         }
548         else if( !strcmp( s, "vlc_previous" ) )
549         {
550             playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1 );
551             msg_Dbg( p_intf, "requested playlist previous" );
552         }
553         else if( !strcmp( s, "vlc_seek" ) )
554         {
555             char *psz_value = E_(SSPop)( st );
556             E_(HandleSeek)( p_intf, psz_value );
557             msg_Dbg( p_intf, "requested playlist seek: %s", psz_value );
558             free( psz_value );
559         }
560         else if( !strcmp( s, "vlc_var_type" )
561                   || !strcmp( s, "vlc_config_type" ) )
562         {
563             vlc_object_t *p_object;
564             const char *psz_type = NULL;
565             int i_type = 0;
566
567             if( !strcmp( s, "vlc_var_type" ) )
568             {
569                 char *psz_object = E_(SSPop)( st );
570                 char *psz_variable = E_(SSPop)( st );
571                 vlc_bool_t b_need_release;
572
573                 p_object = GetVLCObject( p_intf, psz_object, &b_need_release );
574
575                 if( p_object != NULL )
576                     i_type = var_Type( p_object, psz_variable );
577                 free( psz_variable );
578                 free( psz_object );
579                 if( b_need_release && p_object != NULL )
580                     vlc_object_release( p_object );
581             }
582             else
583             {
584                 char *psz_variable = E_(SSPop)( st );
585                 p_object = VLC_OBJECT(p_intf);
586                 i_type = config_GetType( p_object, psz_variable );
587                 free( psz_variable );
588             }
589
590             if( p_object != NULL )
591             {
592                 switch( i_type & VLC_VAR_TYPE )
593                 {
594                 case VLC_VAR_BOOL:
595                     psz_type = "VLC_VAR_BOOL";
596                     break;
597                 case VLC_VAR_INTEGER:
598                     psz_type = "VLC_VAR_INTEGER";
599                     break;
600                 case VLC_VAR_HOTKEY:
601                     psz_type = "VLC_VAR_HOTKEY";
602                     break;
603                 case VLC_VAR_STRING:
604                     psz_type = "VLC_VAR_STRING";
605                     break;
606                 case VLC_VAR_MODULE:
607                     psz_type = "VLC_VAR_MODULE";
608                     break;
609                 case VLC_VAR_FILE:
610                     psz_type = "VLC_VAR_FILE";
611                     break;
612                 case VLC_VAR_DIRECTORY:
613                     psz_type = "VLC_VAR_DIRECTORY";
614                     break;
615                 case VLC_VAR_VARIABLE:
616                     psz_type = "VLC_VAR_VARIABLE";
617                     break;
618                 case VLC_VAR_FLOAT:
619                     psz_type = "VLC_VAR_FLOAT";
620                     break;
621                 default:
622                     psz_type = "UNDEFINED";
623                 }
624             }
625             else
626                 psz_type = "INVALID";
627
628             E_(SSPush)( st, psz_type );
629         }
630         else if( !strcmp( s, "vlc_var_set" ) )
631         {
632             char *psz_object = E_(SSPop)( st );
633             char *psz_variable = E_(SSPop)( st );
634             vlc_bool_t b_need_release;
635
636             vlc_object_t *p_object = GetVLCObject( p_intf, psz_object,
637                                                    &b_need_release );
638
639             if( p_object != NULL )
640             {
641                 vlc_bool_t b_error = VLC_FALSE;
642                 char *psz_value = NULL;
643                 vlc_value_t val;
644                 int i_type;
645
646                 i_type = var_Type( p_object, psz_variable );
647
648                 switch( i_type & VLC_VAR_TYPE )
649                 {
650                 case VLC_VAR_BOOL:
651                     val.b_bool = E_(SSPopN)( st, vars );
652                     msg_Dbg( p_intf, "requested %s var change: %s->%d",
653                              psz_object, psz_variable, val.b_bool );
654                     break;
655                 case VLC_VAR_INTEGER:
656                 case VLC_VAR_HOTKEY:
657                     val.i_int = E_(SSPopN)( st, vars );
658                     msg_Dbg( p_intf, "requested %s var change: %s->%d",
659                              psz_object, psz_variable, val.i_int );
660                     break;
661                 case VLC_VAR_STRING:
662                 case VLC_VAR_MODULE:
663                 case VLC_VAR_FILE:
664                 case VLC_VAR_DIRECTORY:
665                 case VLC_VAR_VARIABLE:
666                     val.psz_string = psz_value = E_(SSPop)( st );
667                     msg_Dbg( p_intf, "requested %s var change: %s->%s",
668                              psz_object, psz_variable, psz_value );
669                     break;
670                 case VLC_VAR_FLOAT:
671                     psz_value = E_(SSPop)( st );
672                     val.f_float = atof( psz_value );
673                     msg_Dbg( p_intf, "requested %s var change: %s->%f",
674                              psz_object, psz_variable, val.f_float );
675                     break;
676                 default:
677                     E_(SSPopN)( st, vars );
678                     msg_Warn( p_intf, "invalid %s variable type %d (%s)",
679                               psz_object, i_type & VLC_VAR_TYPE, psz_variable );
680                     b_error = VLC_TRUE;
681                 }
682
683                 if( !b_error )
684                     var_Set( p_object, psz_variable, val );
685                 if( psz_value != NULL )
686                     free( psz_value );
687             }
688             else
689                 msg_Warn( p_intf, "vlc_var_set called without an object" );
690             free( psz_variable );
691             free( psz_object );
692
693             if( b_need_release && p_object != NULL )
694                 vlc_object_release( p_object );
695         }
696         else if( !strcmp( s, "vlc_var_get" ) )
697         {
698             char *psz_object = E_(SSPop)( st );
699             char *psz_variable = E_(SSPop)( st );
700             vlc_bool_t b_need_release;
701
702             vlc_object_t *p_object = GetVLCObject( p_intf, psz_object,
703                                                    &b_need_release );
704
705             if( p_object != NULL )
706             {
707                 vlc_value_t val;
708                 int i_type;
709
710                 i_type = var_Type( p_object, psz_variable );
711                 var_Get( p_object, psz_variable, &val );
712
713                 switch( i_type & VLC_VAR_TYPE )
714                 {
715                 case VLC_VAR_BOOL:
716                     E_(SSPushN)( st, val.b_bool );
717                     break;
718                 case VLC_VAR_INTEGER:
719                 case VLC_VAR_HOTKEY:
720                     E_(SSPushN)( st, val.i_int );
721                     break;
722                 case VLC_VAR_STRING:
723                 case VLC_VAR_MODULE:
724                 case VLC_VAR_FILE:
725                 case VLC_VAR_DIRECTORY:
726                 case VLC_VAR_VARIABLE:
727                     E_(SSPush)( st, val.psz_string );
728                     free( val.psz_string );
729                     break;
730                 case VLC_VAR_FLOAT:
731                 {
732                     char psz_value[20];
733                     snprintf( psz_value, sizeof(psz_value), "%f", val.f_float );
734                     E_(SSPush)( st, psz_value );
735                     break;
736                 }
737                 default:
738                     msg_Warn( p_intf, "invalid %s variable type %d (%s)",
739                               psz_object, i_type & VLC_VAR_TYPE, psz_variable );
740                     E_(SSPush)( st, "" );
741                 }
742             }
743             else
744             {
745                 msg_Warn( p_intf, "vlc_var_get called without an object" );
746                 E_(SSPush)( st, "" );
747             }
748             free( psz_variable );
749             free( psz_object );
750
751             if( b_need_release && p_object != NULL )
752                 vlc_object_release( p_object );
753         }
754         else if( !strcmp( s, "vlc_object_exists" ) )
755         {
756             char *psz_object = E_(SSPop)( st );
757             vlc_bool_t b_need_release;
758
759             vlc_object_t *p_object = GetVLCObject( p_intf, psz_object,
760                                                    &b_need_release );
761             if( b_need_release && p_object != NULL )
762                 vlc_object_release( p_object );
763
764             if( p_object != NULL )
765                 E_(SSPush)( st, "1" );
766             else
767                 E_(SSPush)( st, "0" );
768         }
769         else if( !strcmp( s, "vlc_config_set" ) )
770         {
771             char *psz_variable = E_(SSPop)( st );
772             int i_type = config_GetType( p_intf, psz_variable );
773
774             switch( i_type & VLC_VAR_TYPE )
775             {
776             case VLC_VAR_BOOL:
777             case VLC_VAR_INTEGER:
778                 config_PutInt( p_intf, psz_variable, E_(SSPopN)( st, vars ) );
779                 break;
780             case VLC_VAR_STRING:
781             case VLC_VAR_MODULE:
782             case VLC_VAR_FILE:
783             case VLC_VAR_DIRECTORY:
784             {
785                 char *psz_string = E_(SSPop)( st );
786                 config_PutPsz( p_intf, psz_variable, psz_string );
787                 free( psz_string );
788                 break;
789             }
790             case VLC_VAR_FLOAT:
791             {
792                 char *psz_string = E_(SSPop)( st );
793                 config_PutFloat( p_intf, psz_variable, atof(psz_string) );
794                 free( psz_string );
795                 break;
796             }
797             default:
798                 msg_Warn( p_intf, "vlc_config_set called on unknown var (%s)",
799                           psz_variable );
800             }
801             free( psz_variable );
802         }
803         else if( !strcmp( s, "vlc_config_get" ) )
804         {
805             char *psz_variable = E_(SSPop)( st );
806             int i_type = config_GetType( p_intf, psz_variable );
807
808             switch( i_type & VLC_VAR_TYPE )
809             {
810             case VLC_VAR_BOOL:
811             case VLC_VAR_INTEGER:
812                 E_(SSPushN)( st, config_GetInt( p_intf, psz_variable ) );
813                 break;
814             case VLC_VAR_STRING:
815             case VLC_VAR_MODULE:
816             case VLC_VAR_FILE:
817             case VLC_VAR_DIRECTORY:
818             {
819                 char *psz_string = config_GetPsz( p_intf, psz_variable );
820                 E_(SSPush)( st, psz_string );
821                 free( psz_string );
822                 break;
823             }
824             case VLC_VAR_FLOAT:
825             {
826                 char psz_string[20];
827                 snprintf( psz_string, sizeof(psz_string), "%f",
828                           config_GetFloat( p_intf, psz_variable ) );
829                 E_(SSPush)( st, psz_string );
830                 break;
831             }
832             default:
833                 msg_Warn( p_intf, "vlc_config_get called on unknown var (%s)",
834                           psz_variable );
835             }
836             free( psz_variable );
837         }
838         else if( !strcmp( s, "vlc_config_save" ) )
839         {
840             char *psz_module = E_(SSPop)( st );
841             int i_result;
842
843             if( !*psz_module )
844             {
845                 free( psz_module );
846                 psz_module = NULL;
847             }
848             i_result = config_SaveConfigFile( p_intf, psz_module );
849
850             if( psz_module != NULL )
851                 free( psz_module );
852             E_(SSPushN)( st, i_result );
853         }
854         else if( !strcmp( s, "vlc_config_reset" ) )
855         {
856             config_ResetAll( p_intf );
857         }
858         /* 6. playlist functions */
859         else if( !strcmp( s, "playlist_add" ) )
860         {
861             char *psz_name = E_(SSPop)( st );
862             char *mrl = E_(SSPop)( st );
863             char *tmp;
864             playlist_item_t *p_item;
865             int i_id;
866
867             tmp = E_(ToUTF8)( p_intf, psz_name );
868             free( psz_name );
869             psz_name = tmp;
870             tmp = E_(ToUTF8)( p_intf, mrl );
871             free( mrl );
872             mrl = tmp;
873
874             if( !*psz_name )
875             {
876                 p_item = E_(MRLParse)( p_intf, mrl, mrl );
877             }
878             else
879             {
880                 p_item = E_(MRLParse)( p_intf, mrl, psz_name );
881             }
882
883             if( p_item == NULL || p_item->input.psz_uri == NULL ||
884                  !*p_item->input.psz_uri )
885             {
886                 i_id = VLC_EGENERIC;
887                 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
888             }
889             else
890             {
891                 i_id = playlist_AddItem( p_sys->p_playlist, p_item,
892                                          PLAYLIST_APPEND, PLAYLIST_END );
893                 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
894             }
895             E_(SSPushN)( st, i_id );
896
897             free( mrl );
898             free( psz_name );
899         }
900         else if( !strcmp( s, "playlist_empty" ) )
901         {
902             playlist_LockClear( p_sys->p_playlist );
903             msg_Dbg( p_intf, "requested playlist empty" );
904         }
905         else if( !strcmp( s, "playlist_delete" ) )
906         {
907             int i_id = E_(SSPopN)( st, vars );
908             playlist_LockDelete( p_sys->p_playlist, i_id );
909             msg_Dbg( p_intf, "requested playlist delete: %d", i_id );
910         }
911         else if( !strcmp( s, "playlist_move" ) )
912         {
913             int i_newpos = E_(SSPopN)( st, vars );
914             int i_pos = E_(SSPopN)( st, vars );
915             if ( i_pos < i_newpos )
916             {
917                 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
918             }
919             else
920             {
921                 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
922             }
923             msg_Dbg( p_intf, "requested to move playlist item %d to %d",
924                      i_pos, i_newpos);
925         }
926         else if( !strcmp( s, "vlc_volume_set" ) )
927         {
928             char *psz_vol = E_(SSPop)( st );
929             int i_value;
930             audio_volume_t i_volume;
931             aout_VolumeGet( p_intf, &i_volume );
932             if( psz_vol[0] == '+' )
933             {
934                 i_value = atoi( psz_vol );
935                 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
936                     aout_VolumeSet( p_intf, AOUT_VOLUME_MAX );
937                 else
938                     aout_VolumeSet( p_intf, i_volume + i_value );
939             }
940             else if( psz_vol[0] == '-' )
941             {
942                 i_value = atoi( psz_vol );
943                 if( (i_volume + i_value) < AOUT_VOLUME_MIN )
944                     aout_VolumeSet( p_intf, AOUT_VOLUME_MIN );
945                 else
946                     aout_VolumeSet( p_intf, i_volume + i_value );
947             }
948             else if( strstr( psz_vol, "%") != NULL )
949             {
950                 i_value = atoi( psz_vol );
951                 if( i_value < 0 ) i_value = 0;
952                 if( i_value > 400 ) i_value = 400;
953                 aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/400+AOUT_VOLUME_MIN);
954             }
955             else
956             {
957                 i_value = atoi( psz_vol );
958                 if( i_value > AOUT_VOLUME_MAX ) i_value = AOUT_VOLUME_MAX;
959                 if( i_value < AOUT_VOLUME_MIN ) i_value = AOUT_VOLUME_MIN;
960                 aout_VolumeSet( p_intf, i_value );
961             }
962             aout_VolumeGet( p_intf, &i_volume );
963         }
964         else if( !strcmp( s, "vlm_command" ) || !strcmp( s, "vlm_cmd" ) )
965         {
966             char *psz_elt;
967             char *psz_cmd = strdup( "" );
968             char *psz_error;
969             vlm_message_t *vlm_answer;
970
971             /* make sure that we have a vlm object */
972             if( p_intf->p_sys->p_vlm == NULL )
973                 p_intf->p_sys->p_vlm = vlm_New( p_intf );
974
975
976             /* vlm command uses the ';' delimiter
977              * (else we can't know when to stop) */
978             while( strcmp( psz_elt = E_(SSPop)( st ), "" )
979                    && strcmp( psz_elt, ";" ) )
980             {
981                 char *psz_buf =
982                     (char *)malloc( strlen( psz_cmd ) + strlen( psz_elt ) + 2 );
983                 sprintf( psz_buf, "%s %s", psz_cmd, psz_elt );
984                 free( psz_cmd );
985                 free( psz_elt );
986                 psz_cmd = psz_buf;
987             }
988
989             msg_Dbg( p_intf, "executing vlm command: %s", psz_cmd );
990             vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz_cmd, &vlm_answer );
991
992             if( vlm_answer->psz_value == NULL )
993             {
994                 psz_error = strdup( "" );
995             }
996             else
997             {
998                 psz_error = malloc( strlen(vlm_answer->psz_name) +
999                                     strlen(vlm_answer->psz_value) +
1000                                     strlen( " : ") + 1 );
1001                 sprintf( psz_error , "%s : %s" , vlm_answer->psz_name,
1002                                                  vlm_answer->psz_value );
1003             }
1004
1005             E_(mvar_AppendNewVar)( vars, "vlm_error", psz_error );
1006             /* this is kind of a duplicate but we need to have the message
1007              * without the command name for the "export" command */
1008             E_(mvar_AppendNewVar)( vars, "vlm_value", vlm_answer->psz_value );
1009             vlm_MessageDelete( vlm_answer );
1010
1011             free( psz_cmd );
1012             free( psz_error );
1013         }
1014         else
1015         {
1016             E_(SSPush)( st, s );
1017         }
1018     }
1019 }