]> git.sesse.net Git - vlc/blob - modules/control/http/rpn.c
lavc implements emu-edge in VP8 now
[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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "http.h"
30 #include <vlc_url.h>
31 #include <vlc_meta.h>
32 #include <vlc_strings.h>
33
34 static vlc_object_t *GetVLCObject( intf_thread_t *p_intf,
35                                    const char *psz_object,
36                                    bool *pb_need_release )
37 {
38     intf_sys_t    *p_sys = p_intf->p_sys;
39     vlc_object_t *p_object = NULL;
40     *pb_need_release = false;
41
42     if( !strcmp( psz_object, "VLC_OBJECT_LIBVLC" ) )
43         p_object = VLC_OBJECT(p_intf->p_libvlc);
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( p_sys->p_input )
49     {
50         if( !strcmp( psz_object, "VLC_OBJECT_VOUT" ) )
51             p_object = VLC_OBJECT( input_GetVout( p_sys->p_input ) );
52         else if( !strcmp( psz_object, "VLC_OBJECT_AOUT" ) )
53             p_object = VLC_OBJECT( input_GetAout( p_sys->p_input ) );
54         if( p_object )
55             *pb_need_release = true;
56     }
57     else
58         msg_Warn( p_intf, "unknown object type (%s)", psz_object );
59
60     return p_object;
61 }
62
63 void SSInit( rpn_stack_t *st )
64 {
65     st->i_stack = 0;
66 }
67
68 void SSClean( rpn_stack_t *st )
69 {
70     while( st->i_stack > 0 )
71     {
72         free( st->stack[--st->i_stack] );
73     }
74 }
75
76 void SSPush( rpn_stack_t *st, const char *s )
77 {
78     if( st->i_stack < STACK_MAX )
79     {
80         st->stack[st->i_stack++] = strdup( s );
81     }
82 }
83
84 char *SSPop( rpn_stack_t *st )
85 {
86     if( st->i_stack <= 0 )
87     {
88         return strdup( "" );
89     }
90     else
91     {
92         return st->stack[--st->i_stack];
93     }
94 }
95
96 int SSPopN( rpn_stack_t *st, mvar_t  *vars )
97 {
98     char *name;
99
100     char *end;
101     int  i;
102
103     name = SSPop( st );
104     i = strtol( name, &end, 0 );
105     if( end == name )
106     {
107         const char *value = mvar_GetValue( vars, name );
108         i = atoi( value );
109     }
110     free( name );
111
112     return( i );
113 }
114
115 void SSPushN( rpn_stack_t *st, int i )
116 {
117     char v[12];
118
119     snprintf( v, sizeof (v), "%d", i );
120     SSPush( st, v );
121 }
122
123 void EvaluateRPN( intf_thread_t *p_intf, mvar_t  *vars,
124                       rpn_stack_t *st, char *exp )
125 {
126     intf_sys_t    *p_sys = p_intf->p_sys;
127
128     while( exp != NULL && *exp != '\0' )
129     {
130         char *p, *s;
131
132         /* skip space */
133         while( *exp == ' ' )
134         {
135             exp++;
136         }
137
138         if( *exp == '\'' )
139         {
140             /* extract string */
141             p = FirstWord( exp, exp );
142             SSPush( st, exp );
143             exp = p;
144             continue;
145         }
146
147         /* extract token */
148         p = FirstWord( exp, exp );
149         s = exp;
150         if( p == NULL )
151         {
152             exp += strlen( exp );
153         }
154         else
155         {
156             exp = p;
157         }
158
159         if( *s == '\0' )
160         {
161             break;
162         }
163
164         /* 1. Integer function */
165         if( !strcmp( s, "!" ) )
166         {
167             SSPushN( st, !SSPopN( st, vars ) );
168         }
169         else if( !strcmp( s, "^" ) )
170         {
171             SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
172         }
173         else if( !strcmp( s, "&" ) )
174         {
175             SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
176         }
177         else if( !strcmp( s, "|" ) )
178         {
179             SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
180         }
181         else if( !strcmp( s, "+" ) )
182         {
183             SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
184         }
185         else if( !strcmp( s, "-" ) )
186         {
187             int j = SSPopN( st, vars );
188             int i = SSPopN( st, vars );
189             SSPushN( st, i - j );
190         }
191         else if( !strcmp( s, "*" ) )
192         {
193             SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
194         }
195         else if( !strcmp( s, "/" ) )
196         {
197             int i, j;
198
199             j = SSPopN( st, vars );
200             i = SSPopN( st, vars );
201
202             SSPushN( st, j != 0 ? i / j : 0 );
203         }
204         else if( !strcmp( s, "%" ) )
205         {
206             int i, j;
207
208             j = SSPopN( st, vars );
209             i = SSPopN( st, vars );
210
211             SSPushN( st, j != 0 ? i % j : 0 );
212         }
213         /* 2. integer tests */
214         else if( !strcmp( s, "=" ) )
215         {
216             SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
217         }
218         else if( !strcmp( s, "!=" ) )
219         {
220             SSPushN( st, SSPopN( st, vars ) != SSPopN( st, vars ) ? -1 : 0 );
221         }
222         else if( !strcmp( s, "<" ) )
223         {
224             int j = SSPopN( st, vars );
225             int i = SSPopN( st, vars );
226
227             SSPushN( st, i < j ? -1 : 0 );
228         }
229         else if( !strcmp( s, ">" ) )
230         {
231             int j = SSPopN( st, vars );
232             int i = SSPopN( st, vars );
233
234             SSPushN( st, i > j ? -1 : 0 );
235         }
236         else if( !strcmp( s, "<=" ) )
237         {
238             int j = SSPopN( st, vars );
239             int i = SSPopN( st, vars );
240
241             SSPushN( st, i <= j ? -1 : 0 );
242         }
243         else if( !strcmp( s, ">=" ) )
244         {
245             int j = SSPopN( st, vars );
246             int i = SSPopN( st, vars );
247
248             SSPushN( st, i >= j ? -1 : 0 );
249         }
250         /* 3. string functions */
251         else if( !strcmp( s, "strcat" ) )
252         {
253             char *s2 = SSPop( st );
254             char *s1 = SSPop( st );
255             char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
256
257             strcpy( str, s1 );
258             strcat( str, s2 );
259
260             SSPush( st, str );
261             free( s1 );
262             free( s2 );
263             free( str );
264         }
265         else if( !strcmp( s, "strcmp" ) )
266         {
267             char *s2 = SSPop( st );
268             char *s1 = SSPop( st );
269
270             SSPushN( st, strcmp( s1, s2 ) );
271             free( s1 );
272             free( s2 );
273         }
274         else if( !strcmp( s, "strncmp" ) )
275         {
276             int n = SSPopN( st, vars );
277             char *s2 = SSPop( st );
278             char *s1 = SSPop( st );
279
280             SSPushN( st, strncmp( s1, s2 , n ) );
281             free( s1 );
282             free( s2 );
283         }
284         else if( !strcmp( s, "strsub" ) )
285         {
286             int n = SSPopN( st, vars );
287             int m = SSPopN( st, vars );
288             int i_len;
289             char *s = SSPop( st );
290             char *str;
291
292             if( n >= m )
293             {
294                 i_len = n - m + 1;
295             }
296             else
297             {
298                 i_len = 0;
299             }
300
301             str = malloc( i_len + 1 );
302
303             memcpy( str, s + m - 1, i_len );
304             str[ i_len ] = '\0';
305
306             SSPush( st, str );
307             free( s );
308             free( str );
309         }
310         else if( !strcmp( s, "strlen" ) )
311         {
312             char *str = SSPop( st );
313
314             SSPushN( st, strlen( str ) );
315             free( str );
316         }
317         else if( !strcmp( s, "str_replace" ) )
318         {
319             char *psz_to = SSPop( st );
320             char *psz_from = SSPop( st );
321             char *psz_in = SSPop( st );
322             char *psz_in_current = psz_in;
323             char *psz_out = malloc( strlen(psz_in) * strlen(psz_to) + 1 );
324             char *psz_out_current = psz_out;
325
326             while( (p = strstr( psz_in_current, psz_from )) != NULL )
327             {
328                 memcpy( psz_out_current, psz_in_current, p - psz_in_current );
329                 psz_out_current += p - psz_in_current;
330                 strcpy( psz_out_current, psz_to );
331                 psz_out_current += strlen(psz_to);
332                 psz_in_current = p + strlen(psz_from);
333             }
334             strcpy( psz_out_current, psz_in_current );
335             psz_out_current += strlen(psz_in_current);
336             *psz_out_current = '\0';
337
338             SSPush( st, psz_out );
339             free( psz_to );
340             free( psz_from );
341             free( psz_in );
342             free( psz_out );
343         }
344         else if( !strcmp( s, "url_extract" ) )
345         {
346             const char *url = mvar_GetValue( vars, "url_value" );
347             char *name = SSPop( st );
348             char *value = ExtractURIString( url, name );
349             if( value != NULL )
350             {
351                 decode_URI( value );
352                 SSPush( st, value );
353                 free( value );
354             }
355             else
356                 SSPush( st, "" );
357
358             free( name );
359         }
360         else if( !strcmp( s, "url_encode" ) )
361         {
362             char *url = SSPop( st );
363             char *value = encode_URI_component( url );
364             free( url );
365             SSPush( st, value );
366             free( value );
367         }
368         else if( !strcmp( s, "xml_encode" )
369               || !strcmp( s, "htmlspecialchars" ) )
370         {
371             char *url = SSPop( st );
372             char *value = convert_xml_special_chars( url );
373             free( url );
374             SSPush( st, value );
375             free( value );
376         }
377         else if( !strcmp( s, "addslashes" ) )
378         {
379             char *psz_src = SSPop( st );
380             char *psz_dest;
381             char *str = psz_src;
382
383             p = psz_dest = malloc( strlen( str ) * 2 + 1 );
384
385             while( *str != '\0' )
386             {
387                 if( *str == '"' || *str == '\'' || *str == '\\' )
388                 {
389                     *p++ = '\\';
390                 }
391                 *p++ = *str;
392                 str++;
393             }
394             *p = '\0';
395
396             SSPush( st, psz_dest );
397             free( psz_src );
398             free( psz_dest );
399         }
400         else if( !strcmp( s, "stripslashes" ) )
401         {
402             char *psz_src = SSPop( st );
403             char *psz_dest;
404             char *str = psz_src;
405
406             p = psz_dest = strdup( psz_src );
407
408             while( *str )
409             {
410                 if( *str == '\\' && *(str + 1) )
411                 {
412                     str++;
413                 }
414                 *p++ = *str++;
415             }
416             *p = '\0';
417
418             SSPush( st, psz_dest );
419             free( psz_src );
420             free( psz_dest );
421         }
422         else if( !strcmp( s, "realpath" ) )
423         {
424             char *psz_src = SSPop( st );
425             char *psz_dir = RealPath( psz_src );
426
427             SSPush( st, psz_dir );
428             free( psz_src );
429             free( psz_dir );
430         }
431         /* 4. stack functions */
432         else if( !strcmp( s, "dup" ) )
433         {
434             char *str = SSPop( st );
435             SSPush( st, str );
436             SSPush( st, str );
437             free( str );
438         }
439         else if( !strcmp( s, "drop" ) )
440         {
441             char *str = SSPop( st );
442             free( str );
443         }
444         else if( !strcmp( s, "swap" ) )
445         {
446             char *s1 = SSPop( st );
447             char *s2 = SSPop( st );
448
449             SSPush( st, s1 );
450             SSPush( st, s2 );
451             free( s1 );
452             free( s2 );
453         }
454         else if( !strcmp( s, "flush" ) )
455         {
456             SSClean( st );
457             SSInit( st );
458         }
459         else if( !strcmp( s, "store" ) )
460         {
461             char *value = SSPop( st );
462             char *name  = SSPop( st );
463
464             mvar_PushNewVar( vars, name, value );
465             free( name );
466             free( value );
467         }
468         else if( !strcmp( s, "value" ) )
469         {
470             char *name  = SSPop( st );
471             const char *value = mvar_GetValue( vars, name );
472
473             SSPush( st, value );
474
475             free( name );
476         }
477         /* 5. player control */
478         else if( !strcmp( s, "vlc_play" ) )
479         {
480             int i_id = SSPopN( st, vars );
481             int i_ret;
482
483             playlist_Lock( p_sys->p_playlist );
484             i_ret = playlist_Control( p_sys->p_playlist, PLAYLIST_VIEWPLAY,
485                                       pl_Locked, NULL,
486                                       playlist_ItemGetById( p_sys->p_playlist,
487                                       i_id ) );
488             playlist_Unlock( p_sys->p_playlist );
489             msg_Dbg( p_intf, "requested playlist item: %i", i_id );
490             SSPushN( st, i_ret );
491         }
492         else if( !strcmp( s, "vlc_stop" ) )
493         {
494             playlist_Control( p_sys->p_playlist, PLAYLIST_STOP, pl_Unlocked );
495             msg_Dbg( p_intf, "requested playlist stop" );
496         }
497         else if( !strcmp( s, "vlc_pause" ) )
498         {
499             playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE, pl_Unlocked );
500             msg_Dbg( p_intf, "requested playlist pause" );
501         }
502         else if( !strcmp( s, "vlc_next" ) )
503         {
504             playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, pl_Unlocked, 1 );
505             msg_Dbg( p_intf, "requested playlist next" );
506         }
507         else if( !strcmp( s, "vlc_previous" ) )
508         {
509             playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, pl_Unlocked, -1 );
510             msg_Dbg( p_intf, "requested playlist previous" );
511         }
512         else if( !strcmp( s, "vlc_seek" ) )
513         {
514             char *psz_value = SSPop( st );
515             HandleSeek( p_intf, psz_value );
516             msg_Dbg( p_intf, "requested playlist seek: %s", psz_value );
517             free( psz_value );
518         }
519         else if( !strcmp( s, "vlc_var_type" )
520                   || !strcmp( s, "vlc_config_type" ) )
521         {
522             vlc_object_t *p_object;
523             const char *psz_type = NULL;
524             int i_type = 0;
525
526             if( !strcmp( s, "vlc_var_type" ) )
527             {
528                 char *psz_object = SSPop( st );
529                 char *psz_variable = SSPop( st );
530                 bool b_need_release;
531
532                 p_object = GetVLCObject( p_intf, psz_object, &b_need_release );
533
534                 if( p_object != NULL )
535                     i_type = var_Type( p_object, psz_variable );
536                 free( psz_variable );
537                 free( psz_object );
538                 if( b_need_release && p_object != NULL )
539                     vlc_object_release( p_object );
540             }
541             else
542             {
543                 char *psz_variable = SSPop( st );
544                 p_object = VLC_OBJECT(p_intf);
545                 i_type = config_GetType( p_object, psz_variable );
546                 free( psz_variable );
547             }
548
549             if( p_object != NULL )
550             {
551                 switch( i_type & VLC_VAR_TYPE )
552                 {
553                 case VLC_VAR_BOOL:
554                     psz_type = "VLC_VAR_BOOL";
555                     break;
556                 case VLC_VAR_INTEGER:
557                     psz_type = "VLC_VAR_INTEGER";
558                     break;
559                 case VLC_VAR_HOTKEY:
560                     psz_type = "VLC_VAR_HOTKEY";
561                     break;
562                 case VLC_VAR_STRING:
563                     psz_type = "VLC_VAR_STRING";
564                     break;
565                 case VLC_VAR_VARIABLE:
566                     psz_type = "VLC_VAR_VARIABLE";
567                     break;
568                 case VLC_VAR_FLOAT:
569                     psz_type = "VLC_VAR_FLOAT";
570                     break;
571                 default:
572                     psz_type = "UNDEFINED";
573                 }
574             }
575             else
576                 psz_type = "INVALID";
577
578             SSPush( st, psz_type );
579         }
580         else if( !strcmp( s, "vlc_var_set" ) )
581         {
582             char *psz_object = SSPop( st );
583             char *psz_variable = SSPop( st );
584             bool b_need_release;
585
586             vlc_object_t *p_object = GetVLCObject( p_intf, psz_object,
587                                                    &b_need_release );
588
589             if( p_object != NULL )
590             {
591                 bool b_error = false;
592                 char *psz_value = NULL;
593                 vlc_value_t val;
594                 int i_type;
595
596                 i_type = var_Type( p_object, psz_variable );
597
598                 switch( i_type & VLC_VAR_TYPE )
599                 {
600                 case VLC_VAR_BOOL:
601                     val.b_bool = SSPopN( st, vars );
602                     msg_Dbg( p_intf, "requested %s var change: %s->%d",
603                              psz_object, psz_variable, val.b_bool );
604                     break;
605                 case VLC_VAR_INTEGER:
606                 case VLC_VAR_HOTKEY:
607                     val.i_int = SSPopN( st, vars );
608                     msg_Dbg( p_intf, "requested %s var change: %s->%"PRIu64,
609                              psz_object, psz_variable, val.i_int );
610                     break;
611                 case VLC_VAR_STRING:
612                 case VLC_VAR_VARIABLE:
613                     val.psz_string = psz_value = SSPop( st );
614                     msg_Dbg( p_intf, "requested %s var change: %s->%s",
615                              psz_object, psz_variable, psz_value );
616                     break;
617                 case VLC_VAR_FLOAT:
618                     psz_value = SSPop( st );
619                     val.f_float = atof( psz_value );
620                     msg_Dbg( p_intf, "requested %s var change: %s->%f",
621                              psz_object, psz_variable, val.f_float );
622                     break;
623                 default:
624                     SSPopN( st, vars );
625                     msg_Warn( p_intf, "invalid %s variable type %d (%s)",
626                               psz_object, i_type & VLC_VAR_TYPE, psz_variable );
627                     b_error = true;
628                 }
629
630                 if( !b_error )
631                     var_Set( p_object, psz_variable, val );
632                 free( psz_value );
633             }
634             else
635                 msg_Warn( p_intf, "vlc_var_set called without an object" );
636             free( psz_variable );
637             free( psz_object );
638
639             if( b_need_release && p_object != NULL )
640                 vlc_object_release( p_object );
641         }
642         else if( !strcmp( s, "vlc_var_get" ) )
643         {
644             char *psz_object = SSPop( st );
645             char *psz_variable = SSPop( st );
646             bool b_need_release;
647
648             vlc_object_t *p_object = GetVLCObject( p_intf, psz_object,
649                                                    &b_need_release );
650
651             if( p_object != NULL )
652             {
653                 vlc_value_t val;
654                 int i_type;
655
656                 i_type = var_Type( p_object, psz_variable );
657                 var_Get( p_object, psz_variable, &val );
658
659                 switch( i_type & VLC_VAR_TYPE )
660                 {
661                 case VLC_VAR_BOOL:
662                     SSPushN( st, val.b_bool );
663                     break;
664                 case VLC_VAR_INTEGER:
665                 case VLC_VAR_HOTKEY:
666                     SSPushN( st, val.i_int );
667                     break;
668                 case VLC_VAR_STRING:
669                 case VLC_VAR_VARIABLE:
670                     SSPush( st, val.psz_string );
671                     free( val.psz_string );
672                     break;
673                 case VLC_VAR_FLOAT:
674                 {
675                     char psz_value[20];
676                     lldiv_t value = lldiv( val.f_float * 1000000, 1000000 );
677                     snprintf( psz_value, sizeof(psz_value), "%lld.%06u",
678                                     value.quot, (unsigned int)value.rem );
679                     SSPush( st, psz_value );
680                     break;
681                 }
682                 default:
683                     msg_Warn( p_intf, "invalid %s variable type %d (%s)",
684                               psz_object, i_type & VLC_VAR_TYPE, psz_variable );
685                     SSPush( st, "" );
686                 }
687             }
688             else
689             {
690                 msg_Warn( p_intf, "vlc_var_get called without an object" );
691                 SSPush( st, "" );
692             }
693             free( psz_variable );
694             free( psz_object );
695
696             if( b_need_release && p_object != NULL )
697                 vlc_object_release( p_object );
698         }
699         else if( !strcmp( s, "vlc_object_exists" ) )
700         {
701             char *psz_object = SSPop( st );
702             bool b_need_release;
703
704             vlc_object_t *p_object = GetVLCObject( p_intf, psz_object,
705                                                    &b_need_release );
706             if( b_need_release && p_object != NULL )
707                 vlc_object_release( p_object );
708
709             if( p_object != NULL )
710                 SSPush( st, "1" );
711             else
712                 SSPush( st, "0" );
713         }
714         else if( !strcmp( s, "vlc_config_set" ) )
715         {
716             char *psz_variable = SSPop( st );
717             int i_type = config_GetType( p_intf, psz_variable );
718
719             switch( i_type & VLC_VAR_TYPE )
720             {
721             case VLC_VAR_BOOL:
722             case VLC_VAR_INTEGER:
723                 config_PutInt( p_intf, psz_variable, SSPopN( st, vars ) );
724                 break;
725             case VLC_VAR_STRING:
726             {
727                 char *psz_string = SSPop( st );
728                 config_PutPsz( p_intf, psz_variable, psz_string );
729                 free( psz_string );
730                 break;
731             }
732             case VLC_VAR_FLOAT:
733             {
734                 char *psz_string = SSPop( st );
735                 config_PutFloat( p_intf, psz_variable, atof(psz_string) );
736                 free( psz_string );
737                 break;
738             }
739             default:
740                 msg_Warn( p_intf, "vlc_config_set called on unknown var (%s)",
741                           psz_variable );
742             }
743             free( psz_variable );
744         }
745         else if( !strcmp( s, "vlc_config_get" ) )
746         {
747             char *psz_variable = SSPop( st );
748             int i_type = config_GetType( p_intf, psz_variable );
749
750             switch( i_type & VLC_VAR_TYPE )
751             {
752             case VLC_VAR_BOOL:
753             case VLC_VAR_INTEGER:
754                 SSPushN( st, config_GetInt( p_intf, psz_variable ) );
755                 break;
756             case VLC_VAR_STRING:
757             {
758                 char *psz_string = config_GetPsz( p_intf, psz_variable );
759                 SSPush( st, psz_string );
760                 free( psz_string );
761                 break;
762             }
763             case VLC_VAR_FLOAT:
764             {
765                 char psz_string[20];
766                 lldiv_t value = lldiv( config_GetFloat( p_intf, psz_variable )
767                                        * 1000000, 1000000 );
768                 snprintf( psz_string, sizeof(psz_string), "%lld.%06u",
769                           value.quot, (unsigned int)value.rem );
770                 SSPush( st, psz_string );
771                 break;
772             }
773             default:
774                 msg_Warn( p_intf, "vlc_config_get called on unknown var (%s)",
775                           psz_variable );
776                 SSPush( st, "" );
777             }
778             free( psz_variable );
779         }
780         else if( !strcmp( s, "vlc_config_save" ) )
781         {
782             char *psz_module = SSPop( st );
783             int i_result;
784
785             if( !*psz_module )
786             {
787                 free( psz_module );
788                 psz_module = NULL;
789             }
790             i_result = config_SaveConfigFile( p_intf, psz_module );
791
792             free( psz_module );
793             SSPushN( st, i_result );
794         }
795         else if( !strcmp( s, "vlc_config_reset" ) )
796         {
797             config_ResetAll( p_intf );
798         }
799         /* 6. playlist functions */
800         else if( !strcmp( s, "playlist_add" ) )
801         {
802             char *psz_name = SSPop( st );
803             char *mrl = SSPop( st );
804             input_item_t *p_input;
805             int i_ret;
806
807             p_input = MRLParse( p_intf, mrl, psz_name );
808
809             char *psz_uri = input_item_GetURI( p_input );
810             if( !p_input || !psz_uri || !*psz_uri )
811             {
812                 i_ret = VLC_EGENERIC;
813                 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
814             }
815             else
816             {
817                 i_ret = playlist_AddInput( p_sys->p_playlist, p_input,
818                                    PLAYLIST_APPEND, PLAYLIST_END, true,
819                                    pl_Unlocked );
820                 if( i_ret == VLC_SUCCESS )
821                 {
822                     playlist_item_t *p_item;
823                     msg_Dbg( p_intf, "requested mrl add: %s", mrl );
824                     playlist_Lock( p_sys->p_playlist );
825                     p_item = playlist_ItemGetByInput( p_sys->p_playlist,
826                                                       p_input );
827                     if( p_item )
828                         i_ret = p_item->i_id;
829                     playlist_Unlock( p_sys->p_playlist );
830                 }
831                 else
832                     msg_Warn( p_intf, "adding mrl %s failed", mrl );
833                 vlc_gc_decref( p_input );
834             }
835             free( psz_uri );
836             SSPushN( st, i_ret );
837
838             free( mrl );
839             free( psz_name );
840         }
841         else if( !strcmp( s, "playlist_empty" ) )
842         {
843             playlist_Clear( p_sys->p_playlist, pl_Unlocked );
844             msg_Dbg( p_intf, "requested playlist empty" );
845         }
846         else if( !strcmp( s, "playlist_delete" ) )
847         {
848             int i_id = SSPopN( st, vars );
849             playlist_Lock( p_sys->p_playlist );
850             playlist_item_t *p_item = playlist_ItemGetById( p_sys->p_playlist,
851                                                             i_id );
852             if( p_item )
853             {
854                 playlist_DeleteFromInput( p_sys->p_playlist,
855                                           p_item->p_input, pl_Locked );
856                 msg_Dbg( p_intf, "requested playlist delete: %d", i_id );
857             }
858             else
859             {
860                 msg_Dbg( p_intf, "couldn't find playlist item to delete (%d)",
861                          i_id );
862             }
863             playlist_Unlock( p_sys->p_playlist );
864         }
865         else if( !strcmp( s, "playlist_move" ) )
866         {
867             /*int i_newpos =*/ SSPopN( st, vars );
868             /*int i_pos =*/ SSPopN( st, vars );
869             /* FIXME FIXME TODO TODO XXX XXX
870             do not release before fixing this
871             if ( i_pos < i_newpos )
872             {
873                 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
874             }
875             else
876             {
877                 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
878             }
879             msg_Dbg( p_intf, "requested to move playlist item %d to %d",
880                      i_pos, i_newpos);
881                FIXME FIXME TODO TODO XXX XXX */
882             msg_Err( p_intf, "moving using indexes is obsolete. We need to update this function" );
883         }
884         else if( !strcmp( s, "playlist_sort" ) )
885         {
886             int i_order = SSPopN( st, vars );
887             int i_sort = SSPopN( st, vars );
888             i_order = i_order % 2;
889             i_sort = i_sort % 9;
890             /* FIXME FIXME TODO TODO XXX XXX
891             do not release before fixing this
892             playlist_RecursiveNodeSort(  p_sys->p_playlist,
893                                          p_sys->p_playlist->p_general,
894                                          i_sort, i_order );
895             msg_Dbg( p_intf, "requested sort playlist by : %d in order : %d",
896                      i_sort, i_order );
897                FIXME FIXME TODO TODO XXX XXX */
898             msg_Err( p_intf, "this needs to be fixed to use the new playlist framework" );
899         }
900         else if( !strcmp( s, "services_discovery_add" ) )
901         {
902             char *psz_sd = SSPop( st );
903             playlist_ServicesDiscoveryAdd( p_sys->p_playlist, psz_sd );
904             free( psz_sd );
905         }
906         else if( !strcmp( s, "services_discovery_remove" ) )
907         {
908             char *psz_sd = SSPop( st );
909             playlist_ServicesDiscoveryRemove( p_sys->p_playlist, psz_sd );
910             free( psz_sd );
911         }
912         else if( !strcmp( s, "services_discovery_is_loaded" ) )
913         {
914             char *psz_sd = SSPop( st );
915             SSPushN( st,
916             playlist_IsServicesDiscoveryLoaded( p_sys->p_playlist, psz_sd ) );
917             free( psz_sd );
918         }
919         else if( !strcmp( s, "vlc_volume_set" ) )
920         {
921             char *psz_vol = SSPop( st );
922             int i_value;
923             audio_volume_t i_volume;
924             aout_VolumeGet( p_sys->p_playlist, &i_volume );
925             if( psz_vol[0] == '+' )
926             {
927                 i_value = atoi( psz_vol );
928                 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
929                     aout_VolumeSet( p_sys->p_playlist, AOUT_VOLUME_MAX );
930                 else
931                     aout_VolumeSet( p_sys->p_playlist, i_volume + i_value );
932             }
933             else if( psz_vol[0] == '-' )
934             {
935                 i_value = atoi( psz_vol );
936                 if( (i_volume + i_value) < AOUT_VOLUME_MIN )
937                     aout_VolumeSet( p_sys->p_playlist, AOUT_VOLUME_MIN );
938                 else
939                     aout_VolumeSet( p_sys->p_playlist, i_volume + i_value );
940             }
941             else if( strstr( psz_vol, "%") != NULL )
942             {
943                 i_value = atoi( psz_vol );
944                 if( i_value < 0 ) i_value = 0;
945                 if( i_value > 400 ) i_value = 400;
946                 aout_VolumeSet( p_sys->p_playlist, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/400+AOUT_VOLUME_MIN);
947             }
948             else
949             {
950                 i_value = atoi( psz_vol );
951                 if( i_value > AOUT_VOLUME_MAX ) i_value = AOUT_VOLUME_MAX;
952                 if( i_value < AOUT_VOLUME_MIN ) i_value = AOUT_VOLUME_MIN;
953                 aout_VolumeSet( p_sys->p_playlist, i_value );
954             }
955             aout_VolumeGet( p_sys->p_playlist, &i_volume );
956             free( psz_vol );
957         }
958         else if( !strcmp( s, "vlc_get_meta" ) )
959         {
960             char *psz_meta = SSPop( st );
961             char *psz_val = NULL;
962             if( p_sys->p_input && input_GetItem(p_sys->p_input) )
963             {
964 #define p_item input_GetItem( p_sys->p_input )
965                 if( !strcmp( psz_meta, "ARTIST" ) )
966                 {
967                     psz_val = input_item_GetArtist( p_item );
968                 }
969                 else if( !strcmp( psz_meta, "TITLE" ) )
970                 {
971                     psz_val = input_item_GetTitle( p_item );
972                     if( !psz_val )
973                         psz_val = input_item_GetName( p_item );
974                 }
975                 else if( !strcmp( psz_meta, "ALBUM" ) )
976                 {
977                     psz_val = input_item_GetAlbum( p_item );
978                 }
979                 else if( !strcmp( psz_meta, "GENRE" ) )
980                 {
981                     psz_val = input_item_GetGenre( p_item );
982                 }
983                 else if( !strcmp( psz_meta, "COPYRIGHT" ) )
984                 {
985                      psz_val = input_item_GetCopyright( p_item );
986                 }
987                 else if( !strcmp( psz_meta, "TRACK_NUMBER" ) )
988                 {
989                     psz_val = input_item_GetTrackNum( p_item );
990                 }
991                 else if( !strcmp( psz_meta, "DESCRIPTION" ) )
992                 {
993                     psz_val = input_item_GetDescription( p_item );
994                 }
995                 else if( !strcmp( psz_meta, "RATING" ) )
996                 {
997                     psz_val = input_item_GetRating( p_item );
998                 }
999                 else if( !strcmp( psz_meta, "DATE" ) )
1000                 {
1001                     psz_val = input_item_GetDate( p_item );
1002                 }
1003                 else if( !strcmp( psz_meta, "URL" ) )
1004                 {
1005                     psz_val = input_item_GetURL( p_item );
1006                 }
1007                 else if( !strcmp( psz_meta, "LANGUAGE" ) )
1008                 {
1009                     psz_val = input_item_GetLanguage( p_item );
1010                 }
1011                 else if( !strcmp( psz_meta, "NOW_PLAYING" ) )
1012                 {
1013                     psz_val = input_item_GetNowPlaying( p_item );
1014                 }
1015                 else if( !strcmp( psz_meta, "PUBLISHER" ) )
1016                 {
1017                     psz_val = input_item_GetPublisher( p_item );
1018                 }
1019                 else if( !strcmp( psz_meta, "ENCODED_BY" ) )
1020                 {
1021                     psz_val = input_item_GetEncodedBy( p_item );
1022                 }
1023                 else if( !strcmp( psz_meta, "ART_URL" ) )
1024                 {
1025                     psz_val = input_item_GetEncodedBy( p_item );
1026                 }
1027                 else if( !strcmp( psz_meta, "TRACK_ID" ) )
1028                 {
1029                     psz_val = input_item_GetTrackID( p_item );
1030                 }
1031 #undef p_item
1032             }
1033             if( psz_val == NULL ) psz_val = strdup( "" );
1034             SSPush( st, psz_val );
1035             free( psz_meta );
1036             free( psz_val );
1037         }
1038 #ifdef ENABLE_VLM
1039         else if( !strcmp( s, "vlm_command" ) || !strcmp( s, "vlm_cmd" ) )
1040         {
1041             char *psz_elt;
1042             char *psz_cmd = strdup( "" );
1043             char *psz_error;
1044             vlm_message_t *vlm_answer;
1045
1046             /* make sure that we have a vlm object */
1047             if( p_intf->p_sys->p_vlm == NULL )
1048                 p_intf->p_sys->p_vlm = vlm_New( p_intf );
1049
1050
1051             /* vlm command uses the ';' delimiter
1052              * (else we can't know when to stop) */
1053             while( strcmp( psz_elt = SSPop( st ), "" )
1054                    && strcmp( psz_elt, ";" ) )
1055             {
1056                 char* psz_buf;
1057                 if( asprintf( &psz_buf, "%s %s", psz_cmd, psz_elt ) == -1 )
1058                     psz_buf = NULL;
1059                 free( psz_cmd );
1060                 free( psz_elt );
1061                 psz_cmd = psz_buf;
1062             }
1063
1064             msg_Dbg( p_intf, "executing vlm command: %s", psz_cmd );
1065             vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz_cmd, &vlm_answer );
1066
1067             if( vlm_answer->psz_value == NULL )
1068             {
1069                 psz_error = strdup( "" );
1070             }
1071             else
1072             {
1073                 if( asprintf( &psz_error , "%s : %s" , vlm_answer->psz_name,
1074                               vlm_answer->psz_value ) == -1 )
1075                     psz_error = NULL;
1076             }
1077
1078             mvar_AppendNewVar( vars, "vlm_error", psz_error );
1079             /* this is kind of a duplicate but we need to have the message
1080              * without the command name for the "export" command */
1081             mvar_AppendNewVar( vars, "vlm_value", vlm_answer->psz_value );
1082             vlm_MessageDelete( vlm_answer );
1083
1084             free( psz_cmd );
1085             free( psz_error );
1086         }
1087 #endif /* ENABLE_VLM */
1088         else if( !strcmp( s, "snapshot" ) )
1089         {
1090             if( p_sys->p_input )
1091             {
1092                 vout_thread_t *p_vout = input_GetVout( p_sys->p_input );
1093                 if( p_vout )
1094                 {
1095                     var_TriggerCallback( p_vout, "video-snapshot" );
1096                     vlc_object_release( p_vout );
1097                     msg_Dbg( p_intf, "requested snapshot" );
1098                 }
1099             }
1100             break;
1101
1102         }
1103         else
1104         {
1105             SSPush( st, s );
1106         }
1107     }
1108 }