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