1 /*****************************************************************************
2 * rpn.c : RPN evaluator for the HTTP Interface
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
5 * $Id: http.c 12225 2005-08-18 10:01:30Z massiot $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
28 static vlc_object_t *GetVLCObject( intf_thread_t *p_intf,
29 const char *psz_object,
30 vlc_bool_t *pb_need_release )
32 intf_sys_t *p_sys = p_intf->p_sys;
33 int i_object_type = 0;
34 vlc_object_t *p_object = NULL;
35 *pb_need_release = VLC_FALSE;
37 if( !strcmp( psz_object, "VLC_OBJECT_ROOT" ) )
38 i_object_type = VLC_OBJECT_ROOT;
39 else if( !strcmp( psz_object, "VLC_OBJECT_VLC" ) )
40 p_object = VLC_OBJECT(p_intf->p_vlc);
41 else if( !strcmp( psz_object, "VLC_OBJECT_INTF" ) )
42 p_object = VLC_OBJECT(p_intf);
43 else if( !strcmp( psz_object, "VLC_OBJECT_PLAYLIST" ) )
44 p_object = VLC_OBJECT(p_sys->p_playlist);
45 else if( !strcmp( psz_object, "VLC_OBJECT_INPUT" ) )
46 p_object = VLC_OBJECT(p_sys->p_input);
47 else if( !strcmp( psz_object, "VLC_OBJECT_VOUT" ) )
48 i_object_type = VLC_OBJECT_VOUT;
49 else if( !strcmp( psz_object, "VLC_OBJECT_AOUT" ) )
50 i_object_type = VLC_OBJECT_AOUT;
51 else if( !strcmp( psz_object, "VLC_OBJECT_SOUT" ) )
52 i_object_type = VLC_OBJECT_SOUT;
54 msg_Warn( p_intf, "unknown object type (%s)", psz_object );
56 if( p_object == NULL && i_object_type )
58 *pb_need_release = VLC_TRUE;
59 p_object = vlc_object_find( p_intf, i_object_type, FIND_ANYWHERE );
65 void E_(SSInit)( rpn_stack_t *st )
70 void E_(SSClean)( rpn_stack_t *st )
72 while( st->i_stack > 0 )
74 free( st->stack[--st->i_stack] );
78 void E_(SSPush)( rpn_stack_t *st, const char *s )
80 if( st->i_stack < STACK_MAX )
82 st->stack[st->i_stack++] = strdup( s );
86 char *E_(SSPop)( rpn_stack_t *st )
88 if( st->i_stack <= 0 )
94 return st->stack[--st->i_stack];
98 int E_(SSPopN)( rpn_stack_t *st, mvar_t *vars )
106 name = E_(SSPop)( st );
107 i = strtol( name, &end, 0 );
110 value = E_(mvar_GetValue)( vars, name );
118 void E_(SSPushN)( rpn_stack_t *st, int i )
122 sprintf( v, "%d", i );
126 void E_(EvaluateRPN)( intf_thread_t *p_intf, mvar_t *vars,
127 rpn_stack_t *st, char *exp )
129 intf_sys_t *p_sys = p_intf->p_sys;
131 while( exp != NULL && *exp != '\0' )
144 p = E_(FirstWord)( exp, exp );
145 E_(SSPush)( st, exp );
151 p = E_(FirstWord)( exp, exp );
155 exp += strlen( exp );
167 /* 1. Integer function */
168 if( !strcmp( s, "!" ) )
170 E_(SSPushN)( st, ~E_(SSPopN)( st, vars ) );
172 else if( !strcmp( s, "^" ) )
174 E_(SSPushN)( st, E_(SSPopN)( st, vars ) ^ E_(SSPopN)( st, vars ) );
176 else if( !strcmp( s, "&" ) )
178 E_(SSPushN)( st, E_(SSPopN)( st, vars ) & E_(SSPopN)( st, vars ) );
180 else if( !strcmp( s, "|" ) )
182 E_(SSPushN)( st, E_(SSPopN)( st, vars ) | E_(SSPopN)( st, vars ) );
184 else if( !strcmp( s, "+" ) )
186 E_(SSPushN)( st, E_(SSPopN)( st, vars ) + E_(SSPopN)( st, vars ) );
188 else if( !strcmp( s, "-" ) )
190 int j = E_(SSPopN)( st, vars );
191 int i = E_(SSPopN)( st, vars );
192 E_(SSPushN)( st, i - j );
194 else if( !strcmp( s, "*" ) )
196 E_(SSPushN)( st, E_(SSPopN)( st, vars ) * E_(SSPopN)( st, vars ) );
198 else if( !strcmp( s, "/" ) )
202 j = E_(SSPopN)( st, vars );
203 i = E_(SSPopN)( st, vars );
205 E_(SSPushN)( st, j != 0 ? i / j : 0 );
207 else if( !strcmp( s, "%" ) )
211 j = E_(SSPopN)( st, vars );
212 i = E_(SSPopN)( st, vars );
214 E_(SSPushN)( st, j != 0 ? i % j : 0 );
216 /* 2. integer tests */
217 else if( !strcmp( s, "=" ) )
219 E_(SSPushN)( st, E_(SSPopN)( st, vars ) == E_(SSPopN)( st, vars ) ? -1 : 0 );
221 else if( !strcmp( s, "!=" ) )
223 E_(SSPushN)( st, E_(SSPopN)( st, vars ) != E_(SSPopN)( st, vars ) ? -1 : 0 );
225 else if( !strcmp( s, "<" ) )
227 int j = E_(SSPopN)( st, vars );
228 int i = E_(SSPopN)( st, vars );
230 E_(SSPushN)( st, i < j ? -1 : 0 );
232 else if( !strcmp( s, ">" ) )
234 int j = E_(SSPopN)( st, vars );
235 int i = E_(SSPopN)( st, vars );
237 E_(SSPushN)( st, i > j ? -1 : 0 );
239 else if( !strcmp( s, "<=" ) )
241 int j = E_(SSPopN)( st, vars );
242 int i = E_(SSPopN)( st, vars );
244 E_(SSPushN)( st, i <= j ? -1 : 0 );
246 else if( !strcmp( s, ">=" ) )
248 int j = E_(SSPopN)( st, vars );
249 int i = E_(SSPopN)( st, vars );
251 E_(SSPushN)( st, i >= j ? -1 : 0 );
253 /* 3. string functions */
254 else if( !strcmp( s, "strcat" ) )
256 char *s2 = E_(SSPop)( st );
257 char *s1 = E_(SSPop)( st );
258 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
263 E_(SSPush)( st, str );
268 else if( !strcmp( s, "strcmp" ) )
270 char *s2 = E_(SSPop)( st );
271 char *s1 = E_(SSPop)( st );
273 E_(SSPushN)( st, strcmp( s1, s2 ) );
277 else if( !strcmp( s, "strncmp" ) )
279 int n = E_(SSPopN)( st, vars );
280 char *s2 = E_(SSPop)( st );
281 char *s1 = E_(SSPop)( st );
283 E_(SSPushN)( st, strncmp( s1, s2 , n ) );
287 else if( !strcmp( s, "strsub" ) )
289 int n = E_(SSPopN)( st, vars );
290 int m = E_(SSPopN)( st, vars );
292 char *s = E_(SSPop)( st );
304 str = malloc( i_len + 1 );
306 memcpy( str, s + m - 1, i_len );
309 E_(SSPush)( st, str );
313 else if( !strcmp( s, "strlen" ) )
315 char *str = E_(SSPop)( st );
317 E_(SSPushN)( st, strlen( str ) );
320 else if( !strcmp( s, "str_replace" ) )
322 char *psz_to = E_(SSPop)( st );
323 char *psz_from = E_(SSPop)( st );
324 char *psz_in = E_(SSPop)( st );
325 char *psz_in_current = psz_in;
326 char *psz_out = malloc( strlen(psz_in) * strlen(psz_to) + 1 );
327 char *psz_out_current = psz_out;
329 while( (p = strstr( psz_in_current, psz_from )) != NULL )
331 memcpy( psz_out_current, psz_in_current, p - psz_in_current );
332 psz_out_current += p - psz_in_current;
333 strcpy( psz_out_current, psz_to );
334 psz_out_current += strlen(psz_to);
335 psz_in_current = p + strlen(psz_from);
337 strcpy( psz_out_current, psz_in_current );
338 psz_out_current += strlen(psz_in_current);
339 *psz_out_current = '\0';
341 E_(SSPush)( st, psz_out );
347 else if( !strcmp( s, "url_extract" ) )
349 char *url = E_(mvar_GetValue)( vars, "url_value" );
350 char *name = E_(SSPop)( st );
354 E_(ExtractURIValue)( url, name, value, 512 );
355 E_(DecodeEncodedURI)( value );
356 tmp = E_(FromUTF8)( p_intf, value );
357 E_(SSPush)( st, tmp );
361 else if( !strcmp( s, "url_encode" ) )
363 char *url = E_(SSPop)( st );
366 value = E_(ToUTF8)( p_intf, url );
369 value = vlc_UrlEncode( url );
371 E_(SSPush)( st, value );
374 else if( !strcmp( s, "addslashes" ) )
376 char *psz_src = E_(SSPop)( st );
380 p = psz_dest = malloc( strlen( str ) * 2 + 1 );
382 while( *str != '\0' )
384 if( *str == '"' || *str == '\'' || *str == '\\' )
393 E_(SSPush)( st, psz_dest );
397 else if( !strcmp( s, "stripslashes" ) )
399 char *psz_src = E_(SSPop)( st );
402 p = psz_dest = strdup( psz_src );
406 if( *psz_src == '\\' && *(psz_src + 1) )
414 E_(SSPush)( st, psz_dest );
418 else if( !strcmp( s, "htmlspecialchars" ) )
420 char *psz_src = E_(SSPop)( st );
424 p = psz_dest = malloc( strlen( str ) * 6 + 1 );
426 while( *str != '\0' )
430 strcpy( p, "&" );
433 else if( *str == '\"' )
435 strcpy( p, """ );
438 else if( *str == '\'' )
440 strcpy( p, "'" );
443 else if( *str == '<' )
448 else if( *str == '>' )
461 E_(SSPush)( st, psz_dest );
465 else if( !strcmp( s, "realpath" ) )
467 char *psz_src = E_(SSPop)( st );
468 char *psz_dir = E_(RealPath)( p_intf, psz_src );
470 E_(SSPush)( st, psz_dir );
474 /* 4. stack functions */
475 else if( !strcmp( s, "dup" ) )
477 char *str = E_(SSPop)( st );
478 E_(SSPush)( st, str );
479 E_(SSPush)( st, str );
482 else if( !strcmp( s, "drop" ) )
484 char *str = E_(SSPop)( st );
487 else if( !strcmp( s, "swap" ) )
489 char *s1 = E_(SSPop)( st );
490 char *s2 = E_(SSPop)( st );
492 E_(SSPush)( st, s1 );
493 E_(SSPush)( st, s2 );
497 else if( !strcmp( s, "flush" ) )
502 else if( !strcmp( s, "store" ) )
504 char *value = E_(SSPop)( st );
505 char *name = E_(SSPop)( st );
507 E_(mvar_PushNewVar)( vars, name, value );
511 else if( !strcmp( s, "value" ) )
513 char *name = E_(SSPop)( st );
514 char *value = E_(mvar_GetValue)( vars, name );
516 E_(SSPush)( st, value );
520 /* 5. player control */
521 else if( !strcmp( s, "vlc_play" ) )
523 int i_id = E_(SSPopN)( st, vars );
526 i_ret = playlist_Control( p_sys->p_playlist, PLAYLIST_ITEMPLAY,
527 playlist_ItemGetById( p_sys->p_playlist,
529 msg_Dbg( p_intf, "requested playlist item: %i", i_id );
530 E_(SSPushN)( st, i_ret );
532 else if( !strcmp( s, "vlc_stop" ) )
534 playlist_Control( p_sys->p_playlist, PLAYLIST_STOP );
535 msg_Dbg( p_intf, "requested playlist stop" );
537 else if( !strcmp( s, "vlc_pause" ) )
539 playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
540 msg_Dbg( p_intf, "requested playlist pause" );
542 else if( !strcmp( s, "vlc_next" ) )
544 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
545 msg_Dbg( p_intf, "requested playlist next" );
547 else if( !strcmp( s, "vlc_previous" ) )
549 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1 );
550 msg_Dbg( p_intf, "requested playlist previous" );
552 else if( !strcmp( s, "vlc_seek" ) )
554 char *psz_value = E_(SSPop)( st );
555 E_(HandleSeek)( p_intf, psz_value );
556 msg_Dbg( p_intf, "requested playlist seek: %s", psz_value );
559 else if( !strcmp( s, "vlc_var_type" )
560 || !strcmp( s, "vlc_config_type" ) )
562 vlc_object_t *p_object;
563 const char *psz_type = NULL;
566 if( !strcmp( s, "vlc_var_type" ) )
568 char *psz_object = E_(SSPop)( st );
569 char *psz_variable = E_(SSPop)( st );
570 vlc_bool_t b_need_release;
572 p_object = GetVLCObject( p_intf, psz_object, &b_need_release );
574 if( p_object != NULL )
575 i_type = var_Type( p_object, psz_variable );
576 free( psz_variable );
578 if( b_need_release && p_object != NULL )
579 vlc_object_release( p_object );
583 char *psz_variable = E_(SSPop)( st );
584 p_object = VLC_OBJECT(p_intf);
585 i_type = config_GetType( p_object, psz_variable );
586 free( psz_variable );
589 if( p_object != NULL )
591 switch( i_type & VLC_VAR_TYPE )
594 psz_type = "VLC_VAR_BOOL";
596 case VLC_VAR_INTEGER:
597 psz_type = "VLC_VAR_INTEGER";
600 psz_type = "VLC_VAR_HOTKEY";
603 psz_type = "VLC_VAR_STRING";
606 psz_type = "VLC_VAR_MODULE";
609 psz_type = "VLC_VAR_FILE";
611 case VLC_VAR_DIRECTORY:
612 psz_type = "VLC_VAR_DIRECTORY";
614 case VLC_VAR_VARIABLE:
615 psz_type = "VLC_VAR_VARIABLE";
618 psz_type = "VLC_VAR_FLOAT";
621 psz_type = "UNDEFINED";
625 psz_type = "INVALID";
627 E_(SSPush)( st, psz_type );
629 else if( !strcmp( s, "vlc_var_set" ) )
631 char *psz_object = E_(SSPop)( st );
632 char *psz_variable = E_(SSPop)( st );
633 vlc_bool_t b_need_release;
635 vlc_object_t *p_object = GetVLCObject( p_intf, psz_object,
638 if( p_object != NULL )
640 vlc_bool_t b_error = VLC_FALSE;
641 char *psz_value = NULL;
645 i_type = var_Type( p_object, psz_variable );
647 switch( i_type & VLC_VAR_TYPE )
650 val.b_bool = E_(SSPopN)( st, vars );
651 msg_Dbg( p_intf, "requested %s var change: %s->%d",
652 psz_object, psz_variable, val.b_bool );
654 case VLC_VAR_INTEGER:
656 val.i_int = E_(SSPopN)( st, vars );
657 msg_Dbg( p_intf, "requested %s var change: %s->%d",
658 psz_object, psz_variable, val.i_int );
663 case VLC_VAR_DIRECTORY:
664 case VLC_VAR_VARIABLE:
665 val.psz_string = psz_value = E_(SSPop)( st );
666 msg_Dbg( p_intf, "requested %s var change: %s->%s",
667 psz_object, psz_variable, psz_value );
670 psz_value = E_(SSPop)( st );
671 val.f_float = atof( psz_value );
672 msg_Dbg( p_intf, "requested %s var change: %s->%f",
673 psz_object, psz_variable, val.f_float );
676 E_(SSPopN)( st, vars );
677 msg_Warn( p_intf, "invalid %s variable type %d (%s)",
678 psz_object, i_type & VLC_VAR_TYPE, psz_variable );
683 var_Set( p_object, psz_variable, val );
684 if( psz_value != NULL )
688 msg_Warn( p_intf, "vlc_var_set called without an object" );
689 free( psz_variable );
692 if( b_need_release && p_object != NULL )
693 vlc_object_release( p_object );
695 else if( !strcmp( s, "vlc_var_get" ) )
697 char *psz_object = E_(SSPop)( st );
698 char *psz_variable = E_(SSPop)( st );
699 vlc_bool_t b_need_release;
701 vlc_object_t *p_object = GetVLCObject( p_intf, psz_object,
704 if( p_object != NULL )
709 i_type = var_Type( p_object, psz_variable );
710 var_Get( p_object, psz_variable, &val );
712 switch( i_type & VLC_VAR_TYPE )
715 E_(SSPushN)( st, val.b_bool );
717 case VLC_VAR_INTEGER:
719 E_(SSPushN)( st, val.i_int );
724 case VLC_VAR_DIRECTORY:
725 case VLC_VAR_VARIABLE:
726 E_(SSPush)( st, val.psz_string );
727 free( val.psz_string );
732 snprintf( psz_value, sizeof(psz_value), "%f", val.f_float );
733 E_(SSPush)( st, psz_value );
737 msg_Warn( p_intf, "invalid %s variable type %d (%s)",
738 psz_object, i_type & VLC_VAR_TYPE, psz_variable );
739 E_(SSPush)( st, "" );
744 msg_Warn( p_intf, "vlc_var_get called without an object" );
745 E_(SSPush)( st, "" );
747 free( psz_variable );
750 if( b_need_release && p_object != NULL )
751 vlc_object_release( p_object );
753 else if( !strcmp( s, "vlc_config_set" ) )
755 char *psz_variable = E_(SSPop)( st );
756 int i_type = config_GetType( p_intf, psz_variable );
758 switch( i_type & VLC_VAR_TYPE )
761 case VLC_VAR_INTEGER:
762 config_PutInt( p_intf, psz_variable, E_(SSPopN)( st, vars ) );
767 case VLC_VAR_DIRECTORY:
769 char *psz_string = E_(SSPop)( st );
770 config_PutPsz( p_intf, psz_variable, psz_string );
776 char *psz_string = E_(SSPop)( st );
777 config_PutFloat( p_intf, psz_variable, atof(psz_string) );
782 msg_Warn( p_intf, "vlc_config_set called on unknown var (%s)",
785 free( psz_variable );
787 else if( !strcmp( s, "vlc_config_get" ) )
789 char *psz_variable = E_(SSPop)( st );
790 int i_type = config_GetType( p_intf, psz_variable );
792 switch( i_type & VLC_VAR_TYPE )
795 case VLC_VAR_INTEGER:
796 E_(SSPushN)( st, config_GetInt( p_intf, psz_variable ) );
801 case VLC_VAR_DIRECTORY:
803 char *psz_string = config_GetPsz( p_intf, psz_variable );
804 E_(SSPush)( st, psz_string );
811 snprintf( psz_string, sizeof(psz_string), "%f",
812 config_GetFloat( p_intf, psz_variable ) );
813 E_(SSPush)( st, psz_string );
817 msg_Warn( p_intf, "vlc_config_get called on unknown var (%s)",
820 free( psz_variable );
822 else if( !strcmp( s, "vlc_config_save" ) )
824 char *psz_module = E_(SSPop)( st );
832 i_result = config_SaveConfigFile( p_intf, psz_module );
834 if( psz_module != NULL )
836 E_(SSPushN)( st, i_result );
838 else if( !strcmp( s, "vlc_config_reset" ) )
840 config_ResetAll( p_intf );
842 /* 6. playlist functions */
843 else if( !strcmp( s, "playlist_add" ) )
845 char *psz_name = E_(SSPop)( st );
846 char *mrl = E_(SSPop)( st );
848 playlist_item_t *p_item;
851 tmp = E_(ToUTF8)( p_intf, psz_name );
854 tmp = E_(ToUTF8)( p_intf, mrl );
860 p_item = E_(MRLParse)( p_intf, mrl, mrl );
864 p_item = E_(MRLParse)( p_intf, mrl, psz_name );
867 if( p_item == NULL || p_item->input.psz_uri == NULL ||
868 !*p_item->input.psz_uri )
871 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
875 i_id = playlist_AddItem( p_sys->p_playlist, p_item,
876 PLAYLIST_APPEND, PLAYLIST_END );
877 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
879 E_(SSPushN)( st, i_id );
884 else if( !strcmp( s, "playlist_empty" ) )
886 playlist_LockClear( p_sys->p_playlist );
887 msg_Dbg( p_intf, "requested playlist empty" );
889 else if( !strcmp( s, "playlist_delete" ) )
891 int i_id = E_(SSPopN)( st, vars );
892 playlist_LockDelete( p_sys->p_playlist, i_id );
893 msg_Dbg( p_intf, "requested playlist delete: %d", i_id );
895 else if( !strcmp( s, "playlist_move" ) )
897 int i_newpos = E_(SSPopN)( st, vars );
898 int i_pos = E_(SSPopN)( st, vars );
899 if ( i_pos < i_newpos )
901 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
905 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
907 msg_Dbg( p_intf, "requested to move playlist item %d to %d",