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 SSInit( rpn_stack_t *st )
70 void SSClean( rpn_stack_t *st )
72 while( st->i_stack > 0 )
74 free( st->stack[--st->i_stack] );
78 void 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 * SSPop( rpn_stack_t *st )
88 if( st->i_stack <= 0 )
94 return st->stack[--st->i_stack];
98 int SSPopN( rpn_stack_t *st, mvar_t *vars )
107 i = strtol( name, &end, 0 );
110 value = mvar_GetValue( vars, name );
118 void SSPushN( rpn_stack_t *st, int i )
122 sprintf( v, "%d", i );
126 void 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 );
151 p = E_(FirstWord)( exp, exp );
155 exp += strlen( exp );
167 /* 1. Integer function */
168 if( !strcmp( s, "!" ) )
170 SSPushN( st, ~SSPopN( st, vars ) );
172 else if( !strcmp( s, "^" ) )
174 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
176 else if( !strcmp( s, "&" ) )
178 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
180 else if( !strcmp( s, "|" ) )
182 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
184 else if( !strcmp( s, "+" ) )
186 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
188 else if( !strcmp( s, "-" ) )
190 int j = SSPopN( st, vars );
191 int i = SSPopN( st, vars );
192 SSPushN( st, i - j );
194 else if( !strcmp( s, "*" ) )
196 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
198 else if( !strcmp( s, "/" ) )
202 j = SSPopN( st, vars );
203 i = SSPopN( st, vars );
205 SSPushN( st, j != 0 ? i / j : 0 );
207 else if( !strcmp( s, "%" ) )
211 j = SSPopN( st, vars );
212 i = SSPopN( st, vars );
214 SSPushN( st, j != 0 ? i % j : 0 );
216 /* 2. integer tests */
217 else if( !strcmp( s, "=" ) )
219 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
221 else if( !strcmp( s, "!=" ) )
223 SSPushN( st, SSPopN( st, vars ) != SSPopN( st, vars ) ? -1 : 0 );
225 else if( !strcmp( s, "<" ) )
227 int j = SSPopN( st, vars );
228 int i = SSPopN( st, vars );
230 SSPushN( st, i < j ? -1 : 0 );
232 else if( !strcmp( s, ">" ) )
234 int j = SSPopN( st, vars );
235 int i = SSPopN( st, vars );
237 SSPushN( st, i > j ? -1 : 0 );
239 else if( !strcmp( s, "<=" ) )
241 int j = SSPopN( st, vars );
242 int i = SSPopN( st, vars );
244 SSPushN( st, i <= j ? -1 : 0 );
246 else if( !strcmp( s, ">=" ) )
248 int j = SSPopN( st, vars );
249 int i = SSPopN( st, vars );
251 SSPushN( st, i >= j ? -1 : 0 );
253 /* 3. string functions */
254 else if( !strcmp( s, "strcat" ) )
256 char *s2 = SSPop( st );
257 char *s1 = SSPop( st );
258 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
268 else if( !strcmp( s, "strcmp" ) )
270 char *s2 = SSPop( st );
271 char *s1 = SSPop( st );
273 SSPushN( st, strcmp( s1, s2 ) );
277 else if( !strcmp( s, "strncmp" ) )
279 int n = SSPopN( st, vars );
280 char *s2 = SSPop( st );
281 char *s1 = SSPop( st );
283 SSPushN( st, strncmp( s1, s2 , n ) );
287 else if( !strcmp( s, "strsub" ) )
289 int n = SSPopN( st, vars );
290 int m = SSPopN( st, vars );
292 char *s = SSPop( st );
304 str = malloc( i_len + 1 );
306 memcpy( str, s + m - 1, i_len );
313 else if( !strcmp( s, "strlen" ) )
315 char *str = SSPop( st );
317 SSPushN( st, strlen( str ) );
320 else if( !strcmp( s, "str_replace" ) )
322 char *psz_to = SSPop( st );
323 char *psz_from = SSPop( st );
324 char *psz_in = 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 SSPush( st, psz_out );
347 else if( !strcmp( s, "url_extract" ) )
349 char *url = mvar_GetValue( vars, "url_value" );
350 char *name = SSPop( st );
354 E_(ExtractURIValue)( url, name, value, 512 );
355 E_(DecodeEncodedURI)( value );
356 tmp = E_(FromUTF8)( p_intf, value );
361 else if( !strcmp( s, "url_encode" ) )
363 char *url = SSPop( st );
366 value = E_(ToUTF8)( p_intf, url );
369 value = vlc_UrlEncode( url );
374 else if( !strcmp( s, "addslashes" ) )
376 char *psz_src = SSPop( st );
380 p = psz_dest = malloc( strlen( str ) * 2 + 1 );
382 while( *str != '\0' )
384 if( *str == '"' || *str == '\'' || *str == '\\' )
393 SSPush( st, psz_dest );
397 else if( !strcmp( s, "stripslashes" ) )
399 char *psz_src = SSPop( st );
402 p = psz_dest = strdup( psz_src );
404 while( *psz_src != '\0' )
406 if( *psz_src == '\\' )
415 SSPush( st, psz_dest );
419 else if( !strcmp( s, "htmlspecialchars" ) )
421 char *psz_src = SSPop( st );
425 p = psz_dest = malloc( strlen( str ) * 6 + 1 );
427 while( *str != '\0' )
431 strcpy( p, "&" );
434 else if( *str == '\"' )
436 strcpy( p, """ );
439 else if( *str == '\'' )
441 strcpy( p, "'" );
444 else if( *str == '<' )
449 else if( *str == '>' )
462 SSPush( st, psz_dest );
466 else if( !strcmp( s, "realpath" ) )
468 char *psz_src = SSPop( st );
469 char *psz_dir = E_(RealPath)( p_intf, psz_src );
472 SSPush( st, psz_dir );
476 /* 4. stack functions */
477 else if( !strcmp( s, "dup" ) )
479 char *str = SSPop( st );
484 else if( !strcmp( s, "drop" ) )
486 char *str = SSPop( st );
489 else if( !strcmp( s, "swap" ) )
491 char *s1 = SSPop( st );
492 char *s2 = SSPop( st );
499 else if( !strcmp( s, "flush" ) )
504 else if( !strcmp( s, "store" ) )
506 char *value = SSPop( st );
507 char *name = SSPop( st );
509 mvar_PushNewVar( vars, name, value );
513 else if( !strcmp( s, "value" ) )
515 char *name = SSPop( st );
516 char *value = mvar_GetValue( vars, name );
522 /* 5. player control */
523 else if( !strcmp( s, "vlc_play" ) )
525 int i_id = SSPopN( st, vars );
528 i_ret = playlist_Control( p_sys->p_playlist, PLAYLIST_ITEMPLAY,
529 playlist_ItemGetById( p_sys->p_playlist,
531 msg_Dbg( p_intf, "requested playlist item: %i", i_id );
532 SSPushN( st, i_ret );
534 else if( !strcmp( s, "vlc_stop" ) )
536 playlist_Control( p_sys->p_playlist, PLAYLIST_STOP );
537 msg_Dbg( p_intf, "requested playlist stop" );
539 else if( !strcmp( s, "vlc_pause" ) )
541 playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
542 msg_Dbg( p_intf, "requested playlist pause" );
544 else if( !strcmp( s, "vlc_next" ) )
546 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
547 msg_Dbg( p_intf, "requested playlist next" );
549 else if( !strcmp( s, "vlc_previous" ) )
551 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1 );
552 msg_Dbg( p_intf, "requested playlist previous" );
554 else if( !strcmp( s, "vlc_seek" ) )
556 char *psz_value = SSPop( st );
557 E_(HandleSeek)( p_intf, psz_value );
558 msg_Dbg( p_intf, "requested playlist seek: %s", psz_value );
561 else if( !strcmp( s, "vlc_var_type" )
562 || !strcmp( s, "vlc_config_type" ) )
564 vlc_object_t *p_object;
565 const char *psz_type = NULL;
568 if( !strcmp( s, "vlc_var_type" ) )
570 char *psz_object = SSPop( st );
571 char *psz_variable = SSPop( st );
572 vlc_bool_t b_need_release;
574 p_object = GetVLCObject( p_intf, psz_object, &b_need_release );
576 if( p_object != NULL )
577 i_type = var_Type( p_object, psz_variable );
578 free( psz_variable );
580 if( b_need_release && p_object != NULL )
581 vlc_object_release( p_object );
585 char *psz_variable = SSPop( st );
586 p_object = VLC_OBJECT(p_intf);
587 i_type = config_GetType( p_object, psz_variable );
588 free( psz_variable );
591 if( p_object != NULL )
593 switch( i_type & VLC_VAR_TYPE )
596 psz_type = "VLC_VAR_BOOL";
598 case VLC_VAR_INTEGER:
599 psz_type = "VLC_VAR_INTEGER";
602 psz_type = "VLC_VAR_HOTKEY";
605 psz_type = "VLC_VAR_STRING";
608 psz_type = "VLC_VAR_MODULE";
611 psz_type = "VLC_VAR_FILE";
613 case VLC_VAR_DIRECTORY:
614 psz_type = "VLC_VAR_DIRECTORY";
616 case VLC_VAR_VARIABLE:
617 psz_type = "VLC_VAR_VARIABLE";
620 psz_type = "VLC_VAR_FLOAT";
623 psz_type = "UNDEFINED";
627 psz_type = "INVALID";
629 SSPush( st, psz_type );
631 else if( !strcmp( s, "vlc_var_set" ) )
633 char *psz_object = SSPop( st );
634 char *psz_variable = SSPop( st );
635 vlc_bool_t b_need_release;
637 vlc_object_t *p_object = GetVLCObject( p_intf, psz_object,
640 if( p_object != NULL )
642 vlc_bool_t b_error = VLC_FALSE;
643 char *psz_value = NULL;
647 i_type = var_Type( p_object, psz_variable );
649 switch( i_type & VLC_VAR_TYPE )
652 val.b_bool = SSPopN( st, vars );
653 msg_Dbg( p_intf, "requested %s var change: %s->%d",
654 psz_object, psz_variable, val.b_bool );
656 case VLC_VAR_INTEGER:
658 val.i_int = SSPopN( st, vars );
659 msg_Dbg( p_intf, "requested %s var change: %s->%d",
660 psz_object, psz_variable, val.i_int );
665 case VLC_VAR_DIRECTORY:
666 case VLC_VAR_VARIABLE:
667 val.psz_string = psz_value = SSPop( st );
668 msg_Dbg( p_intf, "requested %s var change: %s->%s",
669 psz_object, psz_variable, psz_value );
672 psz_value = SSPop( st );
673 val.f_float = atof( psz_value );
674 msg_Dbg( p_intf, "requested %s var change: %s->%f",
675 psz_object, psz_variable, val.f_float );
679 msg_Warn( p_intf, "invalid %s variable type %d (%s)",
680 psz_object, i_type & VLC_VAR_TYPE, psz_variable );
685 var_Set( p_object, psz_variable, val );
686 if( psz_value != NULL )
690 msg_Warn( p_intf, "vlc_var_set called without an object" );
691 free( psz_variable );
694 if( b_need_release && p_object != NULL )
695 vlc_object_release( p_object );
697 else if( !strcmp( s, "vlc_var_get" ) )
699 char *psz_object = SSPop( st );
700 char *psz_variable = SSPop( st );
701 vlc_bool_t b_need_release;
703 vlc_object_t *p_object = GetVLCObject( p_intf, psz_object,
706 if( p_object != NULL )
711 i_type = var_Type( p_object, psz_variable );
712 var_Get( p_object, psz_variable, &val );
714 switch( i_type & VLC_VAR_TYPE )
717 SSPushN( st, val.b_bool );
719 case VLC_VAR_INTEGER:
721 SSPushN( st, val.i_int );
726 case VLC_VAR_DIRECTORY:
727 case VLC_VAR_VARIABLE:
728 SSPush( st, val.psz_string );
729 free( val.psz_string );
734 snprintf( psz_value, sizeof(psz_value), "%f", val.f_float );
735 SSPush( st, psz_value );
739 msg_Warn( p_intf, "invalid %s variable type %d (%s)",
740 psz_object, i_type & VLC_VAR_TYPE, psz_variable );
746 msg_Warn( p_intf, "vlc_var_get called without an object" );
749 free( psz_variable );
752 if( b_need_release && p_object != NULL )
753 vlc_object_release( p_object );
755 else if( !strcmp( s, "vlc_config_set" ) )
757 char *psz_variable = SSPop( st );
758 int i_type = config_GetType( p_intf, psz_variable );
760 switch( i_type & VLC_VAR_TYPE )
763 case VLC_VAR_INTEGER:
764 config_PutInt( p_intf, psz_variable, SSPopN( st, vars ) );
769 case VLC_VAR_DIRECTORY:
771 char *psz_string = SSPop( st );
772 config_PutPsz( p_intf, psz_variable, psz_string );
778 char *psz_string = SSPop( st );
779 config_PutFloat( p_intf, psz_variable, atof(psz_string) );
784 msg_Warn( p_intf, "vlc_config_set called on unknown var (%s)",
787 free( psz_variable );
789 else if( !strcmp( s, "vlc_config_get" ) )
791 char *psz_variable = SSPop( st );
792 int i_type = config_GetType( p_intf, psz_variable );
794 switch( i_type & VLC_VAR_TYPE )
797 case VLC_VAR_INTEGER:
798 SSPushN( st, config_GetInt( p_intf, psz_variable ) );
803 case VLC_VAR_DIRECTORY:
805 char *psz_string = config_GetPsz( p_intf, psz_variable );
806 SSPush( st, psz_string );
813 snprintf( psz_string, sizeof(psz_string), "%f",
814 config_GetFloat( p_intf, psz_variable ) );
815 SSPush( st, psz_string );
819 msg_Warn( p_intf, "vlc_config_get called on unknown var (%s)",
822 free( psz_variable );
824 else if( !strcmp( s, "vlc_config_save" ) )
826 char *psz_module = SSPop( st );
834 i_result = config_SaveConfigFile( p_intf, psz_module );
836 if( psz_module != NULL )
838 SSPushN( st, i_result );
840 else if( !strcmp( s, "vlc_config_reset" ) )
842 config_ResetAll( p_intf );
844 /* 6. playlist functions */
845 else if( !strcmp( s, "playlist_add" ) )
847 char *psz_name = SSPop( st );
848 char *mrl = SSPop( st );
850 playlist_item_t *p_item;
853 tmp = E_(ToUTF8)( p_intf, psz_name );
856 tmp = E_(ToUTF8)( p_intf, mrl );
862 p_item = E_(MRLParse)( p_intf, mrl, mrl );
866 p_item = E_(MRLParse)( p_intf, mrl, psz_name );
869 if( p_item == NULL || p_item->input.psz_uri == NULL ||
870 !*p_item->input.psz_uri )
873 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
877 i_id = playlist_AddItem( p_sys->p_playlist, p_item,
878 PLAYLIST_APPEND, PLAYLIST_END );
879 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
886 else if( !strcmp( s, "playlist_empty" ) )
888 playlist_LockClear( p_sys->p_playlist );
889 msg_Dbg( p_intf, "requested playlist empty" );
891 else if( !strcmp( s, "playlist_delete" ) )
893 int i_id = SSPopN( st, vars );
894 playlist_LockDelete( p_sys->p_playlist, i_id );
895 msg_Dbg( p_intf, "requested playlist delete: %d", i_id );
897 else if( !strcmp( s, "playlist_move" ) )
899 int i_newpos = SSPopN( st, vars );
900 int i_pos = SSPopN( st, vars );
901 if ( i_pos < i_newpos )
903 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
907 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
909 msg_Dbg( p_intf, "requested to move playlist item %d to %d",