]> git.sesse.net Git - vlc/blob - modules/control/http/macro.c
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / control / http / macro.c
1 /*****************************************************************************
2  * macro.c : Custom <vlc> macro handling
3  *****************************************************************************
4  * Copyright (C) 2001-2005 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 "macros.h"
31 #include <vlc_url.h>
32
33 static int MacroParse( macro_t *m, char *psz_src )
34 {
35     char *dup = strdup( (char *)psz_src );
36     char *src = dup;
37     char *p;
38     int     i_skip;
39
40 #define EXTRACT( name, l ) \
41         src += l;    \
42         p = strchr( src, '"' );             \
43         if( p )                             \
44         {                                   \
45             *p++ = '\0';                    \
46         }                                   \
47         m->name = strdup( src );            \
48         if( !p )                            \
49         {                                   \
50             break;                          \
51         }                                   \
52         src = p;
53
54     /* init m */
55     m->id = NULL;
56     m->param1 = NULL;
57     m->param2 = NULL;
58
59     /* parse */
60     src += 4;
61
62     while( *src )
63     {
64         while( *src == ' ')
65         {
66             src++;
67         }
68         if( !strncmp( src, "id=\"", 4 ) )
69         {
70             EXTRACT( id, 4 );
71         }
72         else if( !strncmp( src, "param1=\"", 8 ) )
73         {
74             EXTRACT( param1, 8 );
75         }
76         else if( !strncmp( src, "param2=\"", 8 ) )
77         {
78             EXTRACT( param2, 8 );
79         }
80         else
81         {
82             break;
83         }
84     }
85     if( strstr( src, "/>" ) )
86     {
87         src = strstr( src, "/>" ) + 2;
88     }
89     else
90     {
91         src += strlen( src );
92     }
93
94     if( m->id == NULL )
95     {
96         m->id = strdup( "" );
97     }
98     if( m->param1 == NULL )
99     {
100         m->param1 = strdup( "" );
101     }
102     if( m->param2 == NULL )
103     {
104         m->param2 = strdup( "" );
105     }
106     i_skip = src - dup;
107
108     free( dup );
109     return i_skip;
110 #undef EXTRACT
111 }
112
113 static void MacroClean( macro_t *m )
114 {
115     free( m->id );
116     free( m->param1 );
117     free( m->param2 );
118 }
119
120 static int StrToMacroType( const char *name )
121 {
122     int i;
123
124     if( !name || *name == '\0')
125     {
126         return MVLC_UNKNOWN;
127     }
128     for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
129     {
130         if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
131         {
132             return StrToMacroTypeTab[i].i_type;
133         }
134     }
135     return MVLC_UNKNOWN;
136 }
137
138 static void MacroDo( httpd_file_sys_t *p_args,
139                      macro_t *m,
140                      char *p_request, int i_request,
141                      char **pp_data,  int *pi_data,
142                      char **pp_dst )
143 {
144     intf_thread_t  *p_intf = p_args->p_intf;
145     intf_sys_t     *p_sys = p_args->p_intf->p_sys;
146     playlist_t     *p_playlist = p_sys->p_playlist;
147     char control[512];
148
149 #define ALLOC( l ) \
150     {               \
151         int __i__ = *pp_dst - *pp_data; \
152         *pi_data += (l);                  \
153         *pp_data = xrealloc( *pp_data, *pi_data );   \
154         *pp_dst = (*pp_data) + __i__;   \
155     }
156 #define PRINT( str ) \
157     ALLOC( strlen( str ) + 1 ); \
158     *pp_dst += sprintf( *pp_dst, "%s", str );
159
160 #define PRINTS( str, s ) \
161     ALLOC( strlen( str ) + strlen( s ) + 1 ); \
162     { \
163         char * psz_cur = *pp_dst; \
164         *pp_dst += sprintf( *pp_dst, str, s ); \
165         while( psz_cur && *psz_cur ) \
166         {  \
167             /* Prevent script injection */ \
168             if( *psz_cur == '<' ) *psz_cur = '*'; \
169             if( *psz_cur == '>' ) *psz_cur = '*'; \
170             psz_cur++ ; \
171         } \
172     }
173
174     switch( StrToMacroType( m->id ) )
175     {
176         case MVLC_CONTROL:
177             if( i_request <= 0 )
178             {
179                 break;
180             }
181             ExtractURIValue( p_request, "control", control, 512 );
182             if( *m->param1 && !strstr( m->param1, control ) )
183             {
184                 msg_Warn( p_intf, "unauthorized control=%s", control );
185                 break;
186             }
187             switch( StrToMacroType( control ) )
188             {
189                 case MVLC_PLAY:
190                 {
191                     int i_item;
192                     char item[512];
193
194                     ExtractURIValue( p_request, "item", item, 512 );
195                     i_item = atoi( item );
196                     /* id = 0 : simply ask playlist to play */
197                     if( i_item == 0 )
198                     {
199                         playlist_Play( p_sys->p_playlist );
200                         msg_Dbg( p_intf, "requested playlist play" );
201                         break;
202                     }
203                     playlist_Control( p_sys->p_playlist, PLAYLIST_VIEWPLAY,
204                                       pl_Unlocked, NULL,
205                                       playlist_ItemGetById( p_sys->p_playlist,
206                                       i_item ) );
207                     msg_Dbg( p_intf, "requested playlist item: %i", i_item );
208                     break;
209                 }
210                 case MVLC_STOP:
211                     playlist_Control( p_sys->p_playlist, PLAYLIST_STOP,
212                                       pl_Unlocked );
213                     msg_Dbg( p_intf, "requested playlist stop" );
214                     break;
215                 case MVLC_PAUSE:
216                     playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE,
217                                       pl_Unlocked );
218                     msg_Dbg( p_intf, "requested playlist pause" );
219                     break;
220                 case MVLC_NEXT:
221                     playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP,
222                                       pl_Unlocked, 1 );
223                     msg_Dbg( p_intf, "requested playlist next" );
224                     break;
225                 case MVLC_PREVIOUS:
226                     playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP,
227                                       pl_Unlocked, -1 );
228                     msg_Dbg( p_intf, "requested playlist previous" );
229                     break;
230                 case MVLC_FULLSCREEN:
231                     if( p_sys->p_input )
232                     {
233                         bool fs = var_ToggleBool( p_sys->p_playlist,
234                                                   "fullscreen" );
235                         vout_thread_t *p_vout = input_GetVout( p_sys->p_input );
236                         if( p_vout )
237                         {
238                             var_SetBool( p_vout, "fullscreen", fs );
239                             vlc_object_release( p_vout );
240                             msg_Dbg( p_intf, "requested fullscreen toggle" );
241                         }
242                     }
243                     break;
244                 case MVLC_SEEK:
245                 {
246                     char value[30];
247                     ExtractURIValue( p_request, "seek_value", value, 30 );
248                     decode_URI( value );
249                     HandleSeek( p_intf, value );
250                     break;
251                 }
252                 case MVLC_VOLUME:
253                 {
254                     char vol[8];
255                     audio_volume_t i_volume;
256                     int i_value;
257
258                     ExtractURIValue( p_request, "value", vol, 8 );
259                     aout_VolumeGet( p_sys->p_playlist, &i_volume );
260                     decode_URI( vol );
261
262                     if( vol[0] == '+' )
263                     {
264                         i_value = atoi( vol + 1 );
265                         if( (i_volume + i_value) > AOUT_VOLUME_MAX )
266                         {
267                             aout_VolumeSet( p_sys->p_playlist, AOUT_VOLUME_MAX );
268                             msg_Dbg( p_intf, "requested volume set: max" );
269                         }
270                         else
271                         {
272                             aout_VolumeSet( p_sys->p_playlist, (i_volume + i_value) );
273                             msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
274                         }
275                     }
276                     else if( vol[0] == '-' )
277                     {
278                         i_value = atoi( vol + 1 );
279                         if( (i_volume - i_value) < AOUT_VOLUME_MIN )
280                         {
281                             aout_VolumeSet( p_sys->p_playlist, AOUT_VOLUME_MIN );
282                             msg_Dbg( p_intf, "requested volume set: min" );
283                         }
284                         else
285                         {
286                             aout_VolumeSet( p_sys->p_playlist, (i_volume - i_value) );
287                             msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
288                         }
289                     }
290                     else if( strstr(vol, "%") != NULL )
291                     {
292                         i_value = atoi( vol );
293                         if( (i_value <= 400) && (i_value>=0) ){
294                             aout_VolumeSet( p_sys->p_playlist, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/400+AOUT_VOLUME_MIN);
295                             msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
296                         }
297                     }
298                     else
299                     {
300                         i_value = atoi( vol );
301                         if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
302                         {
303                             aout_VolumeSet( p_sys->p_playlist, atoi( vol ) );
304                             msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
305                         }
306                     }
307                     break;
308                 }
309
310                 /* playlist management */
311                 case MVLC_ADD:
312                 {
313                     char mrl[1024], psz_name[1024], tmp[1024];
314                     char *p, *str;
315                     input_item_t *p_input;
316
317                     ExtractURIValue( p_request, "mrl", tmp, 1024 );
318                     decode_URI( tmp );
319                     ExtractURIValue( p_request, "name", psz_name, 1024 );
320                     decode_URI( psz_name );
321                     if( !*psz_name )
322                     {
323                         memcpy( psz_name, tmp, 1024 );
324                     }
325                     /* addslashes for backward compatibility with the old
326                      * http intf */
327                     p = mrl; str = tmp;
328                     while( *str != '\0' )
329                     {
330                         if( *str == '"' || *str == '\'' || *str == '\\' )
331                         {
332                             *p++ = '\\';
333                         }
334                         *p++ = *str;
335                         str++;
336                     }
337                     *p = '\0';
338
339                     p_input = MRLParse( p_intf, mrl, psz_name );
340
341                     char *psz_uri = p_input ? input_item_GetURI( p_input ) : NULL;
342                     if( psz_uri && *psz_uri &&
343                         playlist_AddInput( p_sys->p_playlist, p_input,
344                                            PLAYLIST_APPEND, PLAYLIST_END,
345                                            true, pl_Unlocked) == VLC_SUCCESS )
346                         msg_Dbg( p_intf, "requested mrl add: %s", mrl );
347                     else
348                         msg_Warn( p_intf, "adding mrl failed: %s", mrl );
349                     free( psz_uri );
350                     if( p_input )
351                         vlc_gc_decref( p_input );
352                     break;
353                 }
354                 case MVLC_DEL:
355                 {
356                     int *p_items = NULL;
357                     size_t i_nb_items = 0;
358                     char item[512];
359                     const char *p_parser = p_request;
360
361                     /* Get the list of items to delete */
362                     while( (p_parser =
363                             ExtractURIValue( p_parser, "item", item, 512 )) )
364                     {
365                         if( !*item ) continue;
366
367                         int i_item = atoi( item );
368                         p_items = xrealloc( p_items,
369                                         (i_nb_items + 1) * sizeof(*p_items) );
370                         p_items[i_nb_items] = i_item;
371                         i_nb_items++;
372                     }
373
374                     for( size_t i = 0; i < i_nb_items; i++ )
375                     {
376                         PL_LOCK;
377                         playlist_item_t *p_item;
378
379                         msg_Dbg( p_intf, "requested playlist delete: %d",
380                                  p_items[i] );
381                         p_item = playlist_ItemGetById( p_sys->p_playlist,
382                                                        p_items[i] );
383                         if( p_item )
384                             playlist_DeleteFromInput( p_sys->p_playlist,
385                                                       p_item->p_input,
386                                                       false );
387                         PL_UNLOCK;
388                     }
389
390                     free( p_items );
391                     break;
392                 }
393                 case MVLC_KEEP:
394                 {
395                     int *p_items = NULL;
396                     size_t i_nb_items = 0, i;
397                     char item[512];
398                     const char *p_parser = p_request;
399
400                     /* Get the list of items to keep */
401                     while( (p_parser =
402                        ExtractURIValue( p_parser, "item", item, 512 )) )
403                     {
404                         if( !*item ) continue;
405
406                         int i_item = atoi( item );
407                         p_items = xrealloc( p_items,
408                                         (i_nb_items + 1) * sizeof(*p_items) );
409                         p_items[i_nb_items] = i_item;
410                         i_nb_items++;
411                     }
412
413                     PL_LOCK;
414                     size_t size = p_sys->p_playlist->items.i_size;
415                     for( i = 0; i < size; i++ )
416                     {
417                         size_t j;
418
419                         /* Check if the item is in the keep list */
420                         for( j = 0 ; j < i_nb_items ; j++ )
421                         {
422                             if( p_items[j] ==
423                                 ARRAY_VAL(p_sys->p_playlist->items,i)->i_id)
424                                 break;
425                         }
426                         if( j == i_nb_items )
427                         {
428                             msg_Dbg( p_intf, "requested playlist delete: %d",
429                                    p_sys->p_playlist->items.p_elems[i]->i_id );
430                             playlist_DeleteFromInput( p_sys->p_playlist,
431                                 p_sys->p_playlist->items.p_elems[i]->p_input,
432                                                       false );
433                         }
434                     }
435                     PL_UNLOCK;
436
437                     free( p_items );
438                     break;
439                 }
440                 case MVLC_EMPTY:
441                 {
442                     playlist_Clear( p_sys->p_playlist, pl_Unlocked );
443                     msg_Dbg( p_intf, "requested playlist empty" );
444                     break;
445                 }
446                 case MVLC_SORT:
447                 {
448                     char type[12];
449                     char order[2];
450                     char item[512];
451                     int i_order;
452                     int i_item;
453
454                     ExtractURIValue( p_request, "type", type, 12 );
455                     ExtractURIValue( p_request, "order", order, 2 );
456                     ExtractURIValue( p_request, "item", item, 512 );
457                     i_item = atoi( item );
458
459                     if( order[0] == '0' ) i_order = ORDER_NORMAL;
460                     else i_order = ORDER_REVERSE;
461
462                     if( !strcmp( type , "title" ) )
463                     {
464                         PL_LOCK;
465                         playlist_RecursiveNodeSort( p_sys->p_playlist,
466                                                     /* Ugly hack,but not worse than before ... */
467                                                     p_sys->p_playlist->p_root_onelevel,
468                                                     SORT_TITLE_NODES_FIRST,
469                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
470                         PL_UNLOCK;
471                         msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
472                     }
473                     else if( !strcmp( type , "author" ) )
474                     {
475                         PL_LOCK;
476                         playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
477                                                     p_sys->p_playlist->p_root_onelevel,
478                                                     SORT_ARTIST,
479                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
480                         PL_UNLOCK;
481                         msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
482                     }
483                     else if( !strcmp( type , "shuffle" ) )
484                     {
485                         PL_LOCK;
486                         playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
487                                                     p_sys->p_playlist->p_root_onelevel,
488                                                     SORT_RANDOM,
489                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
490                         PL_UNLOCK;
491                         msg_Dbg( p_intf, "requested playlist shuffle");
492                     }
493
494                     break;
495                 }
496                 case MVLC_MOVE:
497                 {
498                     char psz_pos[6];
499                     char psz_newpos[6];
500                     int i_pos;
501                     int i_newpos;
502                     ExtractURIValue( p_request, "psz_pos", psz_pos, 6 );
503                     ExtractURIValue( p_request, "psz_newpos", psz_newpos, 6 );
504                     i_pos = atoi( psz_pos );
505                     i_newpos = atoi( psz_newpos );
506                     /* FIXME FIXME TODO TODO XXX XXX
507                     ( duplicate from rpn.c )
508                     if ( i_pos < i_newpos )
509                     {
510                         playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
511                     }
512                     else
513                     {
514                         playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
515                     }
516                     msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
517                     FIXME FIXME TODO TODO XXX XXX */
518                     break;
519                 }
520
521                 /* admin function */
522                 case MVLC_CLOSE:
523                 {
524                     char id[512];
525                     ExtractURIValue( p_request, "id", id, 512 );
526                     msg_Dbg( p_intf, "requested close id=%s", id );
527 #if 0
528                     if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
529                     {
530                         msg_Warn( p_intf, "close failed for id=%s", id );
531                     }
532 #endif
533                     break;
534                 }
535                 case MVLC_SHUTDOWN:
536                 {
537                     msg_Dbg( p_intf, "requested shutdown" );
538                     libvlc_Quit( p_intf->p_libvlc );
539                     break;
540                 }
541 #ifdef ENABLE_VLM
542                 /* vlm */
543                 case MVLC_VLM_NEW:
544                 case MVLC_VLM_SETUP:
545                 {
546                     static const char vlm_properties[][9] =
547                     {
548                         /* no args */
549                         "enabled", "disabled", "loop", "unloop",
550                         /* args required */
551                         "input", "output", "option", "date", "period",
552                         "repeat", "append", "",
553                     };
554                     vlm_message_t *vlm_answer;
555                     char name[512];
556                     char *psz = xmalloc( strlen( p_request ) + 1000 );
557                     char *p = psz;
558                     char *vlm_error;
559                     int i;
560
561                     if( p_intf->p_sys->p_vlm == NULL )
562                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
563
564                     if( p_intf->p_sys->p_vlm == NULL )
565                     {
566                         free( psz );
567                         break;
568                     }
569
570                     ExtractURIValue( p_request, "name", name, 512 );
571                     if( StrToMacroType( control ) == MVLC_VLM_NEW )
572                     {
573                         char type[20];
574                         ExtractURIValue( p_request, "type", type, 20 );
575                         p += sprintf( psz, "new %s %s", name, type );
576                     }
577                     else
578                     {
579                         p += sprintf( psz, "setup %s", name );
580                     }
581                     /* Parse the request */
582                     for( i = 0; vlm_properties[i][0]; i++ )
583                     {
584                         char val[512];
585                         ExtractURIValue( p_request,
586                                                vlm_properties[i], val, 512 );
587                         decode_URI( val );
588                         if( strlen( val ) > 0 && i >= 4 )
589                         {
590                             p += sprintf( p, " %s %s", vlm_properties[i], val );
591                         }
592                         else if( TestURIParam( p_request, vlm_properties[i] ) && i < 4 )
593                         {
594                             p += sprintf( p, " %s", vlm_properties[i] );
595                         }
596                     }
597                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
598                     if( vlm_answer->psz_value == NULL ) /* there is no error */
599                     {
600                         vlm_error = strdup( "" );
601                     }
602                     else
603                     {
604                         if( asprintf( &vlm_error , "%s : %s" ,
605                                       vlm_answer->psz_name,
606                                       vlm_answer->psz_value ) == -1 )
607                             vlm_error = NULL;
608                     }
609
610                     mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
611
612                     vlm_MessageDelete( vlm_answer );
613                     free( vlm_error );
614                     free( psz );
615                     break;
616                 }
617
618                 case MVLC_VLM_DEL:
619                 {
620                     vlm_message_t *vlm_answer;
621                     char name[512];
622                     char psz[512+10];
623                     if( p_intf->p_sys->p_vlm == NULL )
624                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
625
626                     if( p_intf->p_sys->p_vlm == NULL ) break;
627
628                     ExtractURIValue( p_request, "name", name, 512 );
629                     sprintf( psz, "del %s", name );
630
631                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
632                     /* FIXME do a vlm_answer -> var stack conversion */
633                     vlm_MessageDelete( vlm_answer );
634                     break;
635                 }
636
637                 case MVLC_VLM_PLAY:
638                 case MVLC_VLM_PAUSE:
639                 case MVLC_VLM_STOP:
640                 case MVLC_VLM_SEEK:
641                 {
642                     vlm_message_t *vlm_answer;
643                     char name[512];
644                     char psz[512+10];
645                     if( p_intf->p_sys->p_vlm == NULL )
646                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
647
648                     if( p_intf->p_sys->p_vlm == NULL ) break;
649
650                     ExtractURIValue( p_request, "name", name, 512 );
651                     if( StrToMacroType( control ) == MVLC_VLM_PLAY )
652                         sprintf( psz, "control %s play", name );
653                     else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
654                         sprintf( psz, "control %s pause", name );
655                     else if( StrToMacroType( control ) == MVLC_VLM_STOP )
656                         sprintf( psz, "control %s stop", name );
657                     else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
658                     {
659                         char percent[20];
660                         ExtractURIValue( p_request, "percent", percent, 512 );
661                         sprintf( psz, "control %s seek %s", name, percent );
662                     }
663
664                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
665                     /* FIXME do a vlm_answer -> var stack conversion */
666                     vlm_MessageDelete( vlm_answer );
667                     break;
668                 }
669                 case MVLC_VLM_LOAD:
670                 case MVLC_VLM_SAVE:
671                 {
672                     vlm_message_t *vlm_answer;
673                     char file[512];
674                     char psz[512];
675
676                     if( p_intf->p_sys->p_vlm == NULL )
677                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
678
679                     if( p_intf->p_sys->p_vlm == NULL ) break;
680
681                     ExtractURIValue( p_request, "file", file, 512 );
682                     decode_URI( file );
683
684                     if( StrToMacroType( control ) == MVLC_VLM_LOAD )
685                         sprintf( psz, "load %s", file );
686                     else
687                         sprintf( psz, "save %s", file );
688
689                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
690                     /* FIXME do a vlm_answer -> var stack conversion */
691                     vlm_MessageDelete( vlm_answer );
692                     break;
693                 }
694 #endif /* ENABLE_VLM */
695                 default:
696                     if( *control )
697                     {
698                         PRINTS( "<!-- control param(%s) unsupported -->", control );
699                     }
700                     break;
701             }
702             break;
703
704         case MVLC_SET:
705         {
706             char    value[512];
707             int     i;
708             float   f;
709
710             if( i_request <= 0 ||
711                 *m->param1  == '\0' ||
712                 strstr( p_request, m->param1 ) == NULL )
713             {
714                 break;
715             }
716             ExtractURIValue( p_request, m->param1,  value, 512 );
717             decode_URI( value );
718
719             switch( StrToMacroType( m->param2 ) )
720             {
721                 case MVLC_INT:
722                     i = atoi( value );
723                     config_PutInt( p_intf, m->param1, i );
724                     break;
725                 case MVLC_FLOAT:
726                     f = atof( value );
727                     config_PutFloat( p_intf, m->param1, f );
728                     break;
729                 case MVLC_STRING:
730                     config_PutPsz( p_intf, m->param1, value );
731                     break;
732                 default:
733                     PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
734             }
735             break;
736         }
737         case MVLC_GET:
738         {
739             char    value[512];
740             int     i;
741             float   f;
742             char    *psz;
743             lldiv_t div;
744
745             if( *m->param1  == '\0' )
746             {
747                 break;
748             }
749
750             switch( StrToMacroType( m->param2 ) )
751             {
752                 case MVLC_INT:
753                     i = config_GetInt( p_intf, m->param1 );
754                     sprintf( value, "%d", i );
755                     break;
756                 case MVLC_FLOAT:
757                     f = config_GetFloat( p_intf, m->param1 );
758                     div = lldiv( f * 1000000 , 1000000 );
759                     sprintf( value, "%lld.%06u", div.quot,
760                             (unsigned int)div.rem );
761                     break;
762                 case MVLC_STRING:
763                     psz = config_GetPsz( p_intf, m->param1 );
764                     if( psz != NULL )
765                     {
766                         strlcpy( value, psz,sizeof( value ) );
767                         free( psz );
768                     }
769                     else
770                         *value = '\0';
771                     msg_Dbg( p_intf, "%d: value = \"%s\"", __LINE__, value );
772                     break;
773                 default:
774                     snprintf( value, sizeof( value ),
775                               "invalid type(%s) in set", m->param2 );
776                     break;
777             }
778             PRINTS( "%s", value );
779             break;
780         }
781         case MVLC_VALUE:
782         {
783             char *s;
784             const char *v;
785
786             if( m->param1 )
787             {
788                 EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m->param1 );
789                 s = SSPop( &p_args->stack );
790                 v = mvar_GetValue( p_args->vars, s );
791             }
792             else
793             {
794                 v = s = SSPop( &p_args->stack );
795             }
796
797             PRINTS( "%s", v );
798             free( s );
799             break;
800         }
801         case MVLC_RPN:
802             EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m->param1 );
803             break;
804
805         /* Useful to learn stack management */
806         case MVLC_STACK:
807         {
808             int i;
809             msg_Dbg( p_intf, "stack" );
810             for (i=0;i<(&p_args->stack)->i_stack;i++)
811                 msg_Dbg( p_intf, "%d -> %s", i, (&p_args->stack)->stack[i] );
812             break;
813         }
814
815         case MVLC_UNKNOWN:
816         default:
817             PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
818             msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
819             break;
820     }
821 #undef PRINTS
822 #undef PRINT
823 #undef ALLOC
824 }
825
826 static
827 char *MacroSearch( char *src, char *end, int i_mvlc, bool b_after )
828 {
829     int     i_id;
830     int     i_level = 0;
831
832     while( src < end )
833     {
834         if( src + 4 < end  && !strncmp( (char *)src, "<vlc", 4 ) )
835         {
836             int i_skip;
837             macro_t m;
838
839             i_skip = MacroParse( &m, src );
840
841             i_id = StrToMacroType( m.id );
842
843             switch( i_id )
844             {
845                 case MVLC_IF:
846                 case MVLC_FOREACH:
847                     i_level++;
848                     break;
849                 case MVLC_END:
850                     i_level--;
851                     break;
852                 default:
853                     break;
854             }
855
856             MacroClean( &m );
857
858             if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
859                 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
860             {
861                 return src + ( b_after ? i_skip : 0 );
862             }
863             else if( i_level < 0 )
864             {
865                 return NULL;
866             }
867
868             src += i_skip;
869         }
870         else
871         {
872             src++;
873         }
874     }
875
876     return NULL;
877 }
878
879 void Execute( httpd_file_sys_t *p_args,
880                      char *p_request, int i_request,
881                      char **pp_data, int *pi_data,
882                      char **pp_dst,
883                      const char *_src, const char *_end )
884 {
885     intf_thread_t  *p_intf = p_args->p_intf;
886
887     char *src, *dup, *end;
888     char *dst = *pp_dst;
889
890     src = dup = xmalloc( _end - _src + 1 );
891     end = src +( _end - _src );
892
893     memcpy( src, _src, _end - _src );
894     *end = '\0';
895
896     /* we parse searching <vlc */
897     while( src < end )
898     {
899         char *p;
900         int i_copy;
901
902         p = strstr( src, "<vlc" );
903         if( p < end && p == src )
904         {
905             macro_t m;
906
907             src += MacroParse( &m, src );
908
909             //msg_Dbg( p_intf, "macro_id=%s", m.id );
910
911             switch( StrToMacroType( m.id ) )
912             {
913                 case MVLC_INCLUDE:
914                 {
915                     FILE *f;
916                     int  i_buffer;
917                     char *p_buffer;
918                     char psz_file[MAX_DIR_SIZE];
919                     char *p;
920                     char sep;
921
922 #if defined( WIN32 )
923                     sep = '\\';
924 #else
925                     sep = '/';
926 #endif
927
928                     if( m.param1[0] != sep )
929                     {
930                         strcpy( psz_file, p_args->file );
931                         p = strrchr( psz_file, sep );
932                         if( p != NULL )
933                             strcpy( p + 1, m.param1 );
934                         else
935                             strcpy( psz_file, m.param1 );
936                     }
937                     else
938                     {
939                         strcpy( psz_file, m.param1 );
940                     }
941
942                     /* We hereby assume that psz_file is in the
943                      * local character encoding */
944                     if( ( f = fopen( psz_file, "r" ) ) == NULL )
945                     {
946                         msg_Warn( p_args->p_intf,
947                                   "unable to include file %s (%m)",
948                                   psz_file );
949                         break;
950                     }
951
952                     /* first we load in a temporary buffer */
953                     FileLoad( f, &p_buffer, &i_buffer );
954
955                     /* we parse executing all  <vlc /> macros */
956                     Execute( p_args, p_request, i_request, pp_data, pi_data,
957                              &dst, &p_buffer[0], &p_buffer[i_buffer] );
958                     free( p_buffer );
959                     fclose(f);
960                     break;
961                 }
962                 case MVLC_IF:
963                 {
964                     bool i_test;
965                     char    *endif;
966
967                     EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m.param1 );
968                     if( SSPopN( &p_args->stack, p_args->vars ) )
969                     {
970                         i_test = 1;
971                     }
972                     else
973                     {
974                         i_test = 0;
975                     }
976                     endif = MacroSearch( src, end, MVLC_END, true );
977
978                     if( i_test == 0 )
979                     {
980                         char *start = MacroSearch( src, endif, MVLC_ELSE, true );
981
982                         if( start )
983                         {
984                             char *stop  = MacroSearch( start, endif, MVLC_END, false );
985                             if( stop )
986                             {
987                                 Execute( p_args, p_request, i_request,
988                                          pp_data, pi_data, &dst, start, stop );
989                             }
990                         }
991                     }
992                     else if( i_test == 1 )
993                     {
994                         char *stop;
995                         if( ( stop = MacroSearch( src, endif, MVLC_ELSE, false ) ) == NULL )
996                         {
997                             stop = MacroSearch( src, endif, MVLC_END, false );
998                         }
999                         if( stop )
1000                         {
1001                             Execute( p_args, p_request, i_request,
1002                                      pp_data, pi_data, &dst, src, stop );
1003                         }
1004                     }
1005
1006                     src = endif;
1007                     break;
1008                 }
1009                 case MVLC_FOREACH:
1010                 {
1011                     char *endfor = MacroSearch( src, end, MVLC_END, true );
1012                     char *start = src;
1013                     char *stop = MacroSearch( src, end, MVLC_END, false );
1014
1015                     if( stop )
1016                     {
1017                         mvar_t *index;
1018                         int    i_idx;
1019                         mvar_t *v;
1020                         if( !strcmp( m.param2, "integer" ) )
1021                         {
1022                             char *arg = SSPop( &p_args->stack );
1023                             index = mvar_IntegerSetNew( m.param1, arg );
1024                             free( arg );
1025                         }
1026                         else if( !strcmp( m.param2, "directory" ) )
1027                         {
1028                             char *arg = SSPop( &p_args->stack );
1029                             index = mvar_FileSetNew( p_intf, m.param1, arg );
1030                             free( arg );
1031                         }
1032                         else if( !strcmp( m.param2, "services" ) )
1033                         {
1034                             index = mvar_ServicesSetNew( p_intf, m.param1 );
1035                         }
1036                         else if( !strcmp( m.param2, "playlist" ) )
1037                         {
1038                             index = mvar_PlaylistSetNew( p_intf, m.param1,
1039                                                     p_intf->p_sys->p_playlist );
1040                         }
1041                         else if( !strcmp( m.param2, "information" ) )
1042                         {
1043                             index = mvar_InfoSetNew( m.param1,
1044                                                      p_intf->p_sys->p_input );
1045                         }
1046                         else if( !strcmp( m.param2, "program" )
1047                                   || !strcmp( m.param2, "title" )
1048                                   || !strcmp( m.param2, "chapter" )
1049                                   || !strcmp( m.param2, "audio-es" )
1050                                   || !strcmp( m.param2, "video-es" )
1051                                   || !strcmp( m.param2, "spu-es" ) )
1052                         {
1053                             index = mvar_InputVarSetNew( p_intf, m.param1,
1054                                                          p_intf->p_sys->p_input,
1055                                                          m.param2 );
1056                         }
1057 #ifdef ENABLE_VLM
1058                         else if( !strcmp( m.param2, "vlm" ) )
1059                         {
1060                             if( p_intf->p_sys->p_vlm == NULL )
1061                                 p_intf->p_sys->p_vlm = vlm_New( p_intf );
1062                             index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
1063                         }
1064 #endif
1065 #if 0
1066                         else if( !strcmp( m.param2, "hosts" ) )
1067                         {
1068                             index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
1069                         }
1070                         else if( !strcmp( m.param2, "urls" ) )
1071                         {
1072                             index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
1073                         }
1074                         else if( !strcmp( m.param2, "connections" ) )
1075                         {
1076                             index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1077                         }
1078 #endif
1079                         else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
1080                         {
1081                             index = mvar_Duplicate( v );
1082                         }
1083                         else
1084                         {
1085                             msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
1086                             src = endfor;
1087                             break;
1088                         }
1089
1090                         for( i_idx = 0; i_idx < index->i_field; i_idx++ )
1091                         {
1092                             mvar_t *f = mvar_Duplicate( index->field[i_idx] );
1093
1094                             //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
1095
1096                             free( f->name );
1097                             f->name = strdup( m.param1 );
1098
1099
1100                             mvar_PushVar( p_args->vars, f );
1101                             Execute( p_args, p_request, i_request,
1102                                      pp_data, pi_data, &dst, start, stop );
1103                             mvar_RemoveVar( p_args->vars, f );
1104
1105                             mvar_Delete( f );
1106                         }
1107                         mvar_Delete( index );
1108
1109                         src = endfor;
1110                     }
1111                     break;
1112                 }
1113                 default:
1114                     MacroDo( p_args, &m, p_request, i_request,
1115                              pp_data, pi_data, &dst );
1116                     break;
1117             }
1118
1119             MacroClean( &m );
1120             continue;
1121         }
1122
1123         i_copy =   ( (p == NULL || p > end ) ? end : p  ) - src;
1124         if( i_copy > 0 )
1125         {
1126             int i_index = dst - *pp_data;
1127
1128             *pi_data += i_copy;
1129             *pp_data = xrealloc( *pp_data, *pi_data );
1130             dst = (*pp_data) + i_index;
1131
1132             memcpy( dst, src, i_copy );
1133             dst += i_copy;
1134             src += i_copy;
1135         }
1136     }
1137
1138     *pp_dst = dst;
1139     free( dup );
1140 }