1 /*****************************************************************************
2 * macro.c : Custom <vlc> macro handling
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
33 static int MacroParse( macro_t *m, char *psz_src )
35 char *dup = strdup( (char *)psz_src );
40 #define EXTRACT( name, l ) \
42 p = strchr( src, '"' ); \
47 m->name = strdup( src ); \
68 if( !strncmp( src, "id=\"", 4 ) )
72 else if( !strncmp( src, "param1=\"", 8 ) )
76 else if( !strncmp( src, "param2=\"", 8 ) )
85 if( strstr( src, "/>" ) )
87 src = strstr( src, "/>" ) + 2;
98 if( m->param1 == NULL )
100 m->param1 = strdup( "" );
102 if( m->param2 == NULL )
104 m->param2 = strdup( "" );
113 static void MacroClean( macro_t *m )
120 static int StrToMacroType( const char *name )
124 if( !name || *name == '\0')
128 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
130 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
132 return StrToMacroTypeTab[i].i_type;
138 static void MacroDo( httpd_file_sys_t *p_args,
140 char *p_request, int i_request,
141 char **pp_data, int *pi_data,
144 intf_thread_t *p_intf = p_args->p_intf;
145 intf_sys_t *p_sys = p_args->p_intf->p_sys;
150 int __i__ = *pp_dst - *pp_data; \
152 *pp_data = realloc( *pp_data, *pi_data ); \
153 *pp_dst = (*pp_data) + __i__; \
155 #define PRINT( str ) \
156 ALLOC( strlen( str ) + 1 ); \
157 *pp_dst += sprintf( *pp_dst, "%s", str );
159 #define PRINTS( str, s ) \
160 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
162 char * psz_cur = *pp_dst; \
163 *pp_dst += sprintf( *pp_dst, str, s ); \
164 while( psz_cur && *psz_cur ) \
166 /* Prevent script injection */ \
167 if( *psz_cur == '<' ) *psz_cur = '*'; \
168 if( *psz_cur == '>' ) *psz_cur = '*'; \
173 switch( StrToMacroType( m->id ) )
180 ExtractURIValue( p_request, "control", control, 512 );
181 if( *m->param1 && !strstr( m->param1, control ) )
183 msg_Warn( p_intf, "unauthorized control=%s", control );
186 switch( StrToMacroType( control ) )
193 ExtractURIValue( p_request, "item", item, 512 );
194 i_item = atoi( item );
195 /* id = 0 : simply ask playlist to play */
198 playlist_Play( p_sys->p_playlist );
199 msg_Dbg( p_intf, "requested playlist play" );
202 playlist_Control( p_sys->p_playlist, PLAYLIST_VIEWPLAY,
204 playlist_ItemGetById( p_sys->p_playlist,
206 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
210 playlist_Control( p_sys->p_playlist, PLAYLIST_STOP,
212 msg_Dbg( p_intf, "requested playlist stop" );
215 playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE,
217 msg_Dbg( p_intf, "requested playlist pause" );
220 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP,
222 msg_Dbg( p_intf, "requested playlist next" );
225 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP,
227 msg_Dbg( p_intf, "requested playlist previous" );
229 case MVLC_FULLSCREEN:
232 vout_thread_t *p_vout;
234 p_vout = input_GetVout( p_sys->p_input );
237 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
238 vlc_object_release( p_vout );
239 msg_Dbg( p_intf, "requested fullscreen toggle" );
246 ExtractURIValue( p_request, "seek_value", value, 30 );
248 HandleSeek( p_intf, value );
254 audio_volume_t i_volume;
257 ExtractURIValue( p_request, "value", vol, 8 );
258 aout_VolumeGet( p_intf, &i_volume );
263 i_value = atoi( vol + 1 );
264 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
266 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
267 msg_Dbg( p_intf, "requested volume set: max" );
271 aout_VolumeSet( p_intf , (i_volume + i_value) );
272 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
275 else if( vol[0] == '-' )
277 i_value = atoi( vol + 1 );
278 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
280 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
281 msg_Dbg( p_intf, "requested volume set: min" );
285 aout_VolumeSet( p_intf , (i_volume - i_value) );
286 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
289 else if( strstr(vol, "%") != NULL )
291 i_value = atoi( vol );
292 if( (i_value <= 400) && (i_value>=0) ){
293 aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/400+AOUT_VOLUME_MIN);
294 msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
299 i_value = atoi( vol );
300 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
302 aout_VolumeSet( p_intf , atoi( vol ) );
303 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
309 /* playlist management */
312 char mrl[1024], psz_name[1024], tmp[1024];
314 input_item_t *p_input;
316 ExtractURIValue( p_request, "mrl", tmp, 1024 );
318 ExtractURIValue( p_request, "name", psz_name, 1024 );
319 decode_URI( psz_name );
322 memcpy( psz_name, tmp, 1024 );
324 /* addslashes for backward compatibility with the old
327 while( *str != '\0' )
329 if( *str == '"' || *str == '\'' || *str == '\\' )
338 p_input = MRLParse( p_intf, mrl, psz_name );
340 char *psz_uri = p_input ? input_item_GetURI( p_input ) : NULL;
341 if( psz_uri && *psz_uri &&
342 playlist_AddInput( p_sys->p_playlist, p_input,
343 PLAYLIST_APPEND, PLAYLIST_END,
344 true, false) == VLC_SUCCESS )
345 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
347 msg_Warn( p_intf, "adding mrl failed: %s", mrl );
350 vlc_gc_decref( p_input );
355 int i_item, *p_items = NULL, i_nb_items = 0;
357 const char *p_parser = p_request;
359 /* Get the list of items to delete */
361 ExtractURIValue( p_parser, "item", item, 512 )) )
363 if( !*item ) continue;
365 i_item = atoi( item );
366 p_items = realloc( p_items, (i_nb_items + 1) *
368 p_items[i_nb_items] = i_item;
375 for( i = 0; i < i_nb_items; i++ )
377 playlist_DeleteFromInput( p_sys->p_playlist,
379 msg_Dbg( p_intf, "requested playlist delete: %d",
390 int i_item, *p_items = NULL, i_nb_items = 0;
392 const char *p_parser = p_request;
395 /* Get the list of items to keep */
397 ExtractURIValue( p_parser, "item", item, 512 )) )
399 if( !*item ) continue;
401 i_item = atoi( item );
402 p_items = realloc( p_items, (i_nb_items + 1) *
404 p_items[i_nb_items] = i_item;
408 for( i = p_sys->p_playlist->items.i_size - 1 ; i >= 0; i-- )
410 /* Check if the item is in the keep list */
411 for( j = 0 ; j < i_nb_items ; j++ )
414 ARRAY_VAL(p_sys->p_playlist->items,i)
418 if( j == i_nb_items )
420 playlist_DeleteFromInput( p_sys->p_playlist,
421 p_sys->p_playlist->items.p_elems[i]->i_id,
423 msg_Dbg( p_intf, "requested playlist delete: %d",
433 playlist_Clear( p_sys->p_playlist, false );
434 msg_Dbg( p_intf, "requested playlist empty" );
445 ExtractURIValue( p_request, "type", type, 12 );
446 ExtractURIValue( p_request, "order", order, 2 );
447 ExtractURIValue( p_request, "item", item, 512 );
448 i_item = atoi( item );
450 if( order[0] == '0' ) i_order = ORDER_NORMAL;
451 else i_order = ORDER_REVERSE;
453 if( !strcmp( type , "title" ) )
455 playlist_RecursiveNodeSort( p_sys->p_playlist,
456 /* Ugly hack,but not worse than before ... */
457 p_sys->p_playlist->p_root_onelevel,
458 SORT_TITLE_NODES_FIRST,
459 ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
460 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
462 else if( !strcmp( type , "author" ) )
464 playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
465 p_sys->p_playlist->p_root_onelevel,
467 ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
468 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
470 else if( !strcmp( type , "shuffle" ) )
472 playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
473 p_sys->p_playlist->p_root_onelevel,
475 ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
476 msg_Dbg( p_intf, "requested playlist shuffle");
487 ExtractURIValue( p_request, "psz_pos", psz_pos, 6 );
488 ExtractURIValue( p_request, "psz_newpos", psz_newpos, 6 );
489 i_pos = atoi( psz_pos );
490 i_newpos = atoi( psz_newpos );
491 /* FIXME FIXME TODO TODO XXX XXX
492 ( duplicate from rpn.c )
493 if ( i_pos < i_newpos )
495 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
499 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
501 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
502 FIXME FIXME TODO TODO XXX XXX */
510 ExtractURIValue( p_request, "id", id, 512 );
511 msg_Dbg( p_intf, "requested close id=%s", id );
513 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
515 msg_Warn( p_intf, "close failed for id=%s", id );
522 msg_Dbg( p_intf, "requested shutdown" );
523 libvlc_Quit( p_intf->p_libvlc );
531 static const char vlm_properties[][9] =
534 "enabled", "disabled", "loop", "unloop",
536 "input", "output", "option", "date", "period",
537 "repeat", "append", "",
539 vlm_message_t *vlm_answer;
541 char *psz = malloc( strlen( p_request ) + 1000 );
546 if( p_intf->p_sys->p_vlm == NULL )
547 p_intf->p_sys->p_vlm = vlm_New( p_intf );
549 if( p_intf->p_sys->p_vlm == NULL )
555 ExtractURIValue( p_request, "name", name, 512 );
556 if( StrToMacroType( control ) == MVLC_VLM_NEW )
559 ExtractURIValue( p_request, "type", type, 20 );
560 p += sprintf( psz, "new %s %s", name, type );
564 p += sprintf( psz, "setup %s", name );
566 /* Parse the request */
567 for( i = 0; vlm_properties[i][0]; i++ )
570 ExtractURIValue( p_request,
571 vlm_properties[i], val, 512 );
573 if( strlen( val ) > 0 && i >= 4 )
575 p += sprintf( p, " %s %s", vlm_properties[i], val );
577 else if( TestURIParam( p_request, vlm_properties[i] ) && i < 4 )
579 p += sprintf( p, " %s", vlm_properties[i] );
582 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
583 if( vlm_answer->psz_value == NULL ) /* there is no error */
585 vlm_error = strdup( "" );
589 if( asprintf( &vlm_error , "%s : %s" ,
590 vlm_answer->psz_name,
591 vlm_answer->psz_value ) == -1 )
595 mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
597 vlm_MessageDelete( vlm_answer );
605 vlm_message_t *vlm_answer;
608 if( p_intf->p_sys->p_vlm == NULL )
609 p_intf->p_sys->p_vlm = vlm_New( p_intf );
611 if( p_intf->p_sys->p_vlm == NULL ) break;
613 ExtractURIValue( p_request, "name", name, 512 );
614 sprintf( psz, "del %s", name );
616 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
617 /* FIXME do a vlm_answer -> var stack conversion */
618 vlm_MessageDelete( vlm_answer );
627 vlm_message_t *vlm_answer;
630 if( p_intf->p_sys->p_vlm == NULL )
631 p_intf->p_sys->p_vlm = vlm_New( p_intf );
633 if( p_intf->p_sys->p_vlm == NULL ) break;
635 ExtractURIValue( p_request, "name", name, 512 );
636 if( StrToMacroType( control ) == MVLC_VLM_PLAY )
637 sprintf( psz, "control %s play", name );
638 else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
639 sprintf( psz, "control %s pause", name );
640 else if( StrToMacroType( control ) == MVLC_VLM_STOP )
641 sprintf( psz, "control %s stop", name );
642 else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
645 ExtractURIValue( p_request, "percent", percent, 512 );
646 sprintf( psz, "control %s seek %s", name, percent );
649 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
650 /* FIXME do a vlm_answer -> var stack conversion */
651 vlm_MessageDelete( vlm_answer );
657 vlm_message_t *vlm_answer;
661 if( p_intf->p_sys->p_vlm == NULL )
662 p_intf->p_sys->p_vlm = vlm_New( p_intf );
664 if( p_intf->p_sys->p_vlm == NULL ) break;
666 ExtractURIValue( p_request, "file", file, 512 );
669 if( StrToMacroType( control ) == MVLC_VLM_LOAD )
670 sprintf( psz, "load %s", file );
672 sprintf( psz, "save %s", file );
674 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
675 /* FIXME do a vlm_answer -> var stack conversion */
676 vlm_MessageDelete( vlm_answer );
679 #endif /* ENABLE_VLM */
683 PRINTS( "<!-- control param(%s) unsupported -->", control );
695 if( i_request <= 0 ||
696 *m->param1 == '\0' ||
697 strstr( p_request, m->param1 ) == NULL )
701 ExtractURIValue( p_request, m->param1, value, 512 );
704 switch( StrToMacroType( m->param2 ) )
708 config_PutInt( p_intf, m->param1, i );
712 config_PutFloat( p_intf, m->param1, f );
715 config_PutPsz( p_intf, m->param1, value );
718 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
730 if( *m->param1 == '\0' )
735 switch( StrToMacroType( m->param2 ) )
738 i = config_GetInt( p_intf, m->param1 );
739 sprintf( value, "%d", i );
742 f = config_GetFloat( p_intf, m->param1 );
743 div = lldiv( f * 1000000 , 1000000 );
744 sprintf( value, "%lld.%06u", div.quot,
745 (unsigned int)div.rem );
748 psz = config_GetPsz( p_intf, m->param1 );
751 strlcpy( value, psz,sizeof( value ) );
756 msg_Dbg( p_intf, "%d: value = \"%s\"", __LINE__, value );
759 snprintf( value, sizeof( value ),
760 "invalid type(%s) in set", m->param2 );
763 PRINTS( "%s", value );
773 EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m->param1 );
774 s = SSPop( &p_args->stack );
775 v = mvar_GetValue( p_args->vars, s );
779 v = s = SSPop( &p_args->stack );
787 EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m->param1 );
790 /* Useful to learn stack management */
794 msg_Dbg( p_intf, "stack" );
795 for (i=0;i<(&p_args->stack)->i_stack;i++)
796 msg_Dbg( p_intf, "%d -> %s", i, (&p_args->stack)->stack[i] );
802 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
803 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
812 char *MacroSearch( char *src, char *end, int i_mvlc, bool b_after )
819 if( src + 4 < end && !strncmp( (char *)src, "<vlc", 4 ) )
824 i_skip = MacroParse( &m, src );
826 i_id = StrToMacroType( m.id );
843 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
844 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
846 return src + ( b_after ? i_skip : 0 );
848 else if( i_level < 0 )
864 void Execute( httpd_file_sys_t *p_args,
865 char *p_request, int i_request,
866 char **pp_data, int *pi_data,
868 char *_src, char *_end )
870 intf_thread_t *p_intf = p_args->p_intf;
872 char *src, *dup, *end;
875 src = dup = malloc( _end - _src + 1 );
876 end = src +( _end - _src );
878 memcpy( src, _src, _end - _src );
881 /* we parse searching <vlc */
887 p = (char *)strstr( (char *)src, "<vlc" );
888 if( p < end && p == src )
892 src += MacroParse( &m, src );
894 //msg_Dbg( p_intf, "macro_id=%s", m.id );
896 switch( StrToMacroType( m.id ) )
903 char psz_file[MAX_DIR_SIZE];
913 if( m.param1[0] != sep )
915 strcpy( psz_file, p_args->file );
916 p = strrchr( psz_file, sep );
918 strcpy( p + 1, m.param1 );
920 strcpy( psz_file, m.param1 );
924 strcpy( psz_file, m.param1 );
927 /* We hereby assume that psz_file is in the
928 * local character encoding */
929 if( ( f = fopen( psz_file, "r" ) ) == NULL )
931 msg_Warn( p_args->p_intf,
932 "unable to include file %s (%m)",
937 /* first we load in a temporary buffer */
938 FileLoad( f, &p_buffer, &i_buffer );
940 /* we parse executing all <vlc /> macros */
941 Execute( p_args, p_request, i_request, pp_data, pi_data,
942 &dst, &p_buffer[0], &p_buffer[i_buffer] );
952 EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m.param1 );
953 if( SSPopN( &p_args->stack, p_args->vars ) )
961 endif = MacroSearch( src, end, MVLC_END, true );
965 char *start = MacroSearch( src, endif, MVLC_ELSE, true );
969 char *stop = MacroSearch( start, endif, MVLC_END, false );
972 Execute( p_args, p_request, i_request,
973 pp_data, pi_data, &dst, start, stop );
977 else if( i_test == 1 )
980 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, false ) ) == NULL )
982 stop = MacroSearch( src, endif, MVLC_END, false );
986 Execute( p_args, p_request, i_request,
987 pp_data, pi_data, &dst, src, stop );
996 char *endfor = MacroSearch( src, end, MVLC_END, true );
998 char *stop = MacroSearch( src, end, MVLC_END, false );
1005 if( !strcmp( m.param2, "integer" ) )
1007 char *arg = SSPop( &p_args->stack );
1008 index = mvar_IntegerSetNew( m.param1, arg );
1011 else if( !strcmp( m.param2, "directory" ) )
1013 char *arg = SSPop( &p_args->stack );
1014 index = mvar_FileSetNew( p_intf, m.param1, arg );
1017 else if( !strcmp( m.param2, "object" ) )
1019 char *arg = SSPop( &p_args->stack );
1020 index = mvar_ObjectSetNew( p_intf, m.param1, arg );
1023 else if( !strcmp( m.param2, "playlist" ) )
1025 index = mvar_PlaylistSetNew( p_intf, m.param1,
1026 p_intf->p_sys->p_playlist );
1028 else if( !strcmp( m.param2, "information" ) )
1030 index = mvar_InfoSetNew( m.param1,
1031 p_intf->p_sys->p_input );
1033 else if( !strcmp( m.param2, "program" )
1034 || !strcmp( m.param2, "title" )
1035 || !strcmp( m.param2, "chapter" )
1036 || !strcmp( m.param2, "audio-es" )
1037 || !strcmp( m.param2, "video-es" )
1038 || !strcmp( m.param2, "spu-es" ) )
1040 index = mvar_InputVarSetNew( p_intf, m.param1,
1041 p_intf->p_sys->p_input,
1045 else if( !strcmp( m.param2, "vlm" ) )
1047 if( p_intf->p_sys->p_vlm == NULL )
1048 p_intf->p_sys->p_vlm = vlm_New( p_intf );
1049 index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
1053 else if( !strcmp( m.param2, "hosts" ) )
1055 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
1057 else if( !strcmp( m.param2, "urls" ) )
1059 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
1061 else if( !strcmp( m.param2, "connections" ) )
1063 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1066 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
1068 index = mvar_Duplicate( v );
1072 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
1077 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
1079 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
1081 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
1084 f->name = strdup( m.param1 );
1087 mvar_PushVar( p_args->vars, f );
1088 Execute( p_args, p_request, i_request,
1089 pp_data, pi_data, &dst, start, stop );
1090 mvar_RemoveVar( p_args->vars, f );
1094 mvar_Delete( index );
1101 MacroDo( p_args, &m, p_request, i_request,
1102 pp_data, pi_data, &dst );
1110 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
1113 int i_index = dst - *pp_data;
1116 *pp_data = realloc( *pp_data, *pi_data );
1117 dst = (*pp_data) + i_index;
1119 memcpy( dst, src, i_copy );