435232b722faf029f1e35ddd277fb6c403b46d07
[vlc.git] / 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     char control[512];
147
148 #define ALLOC( l ) \
149     {               \
150         int __i__ = *pp_dst - *pp_data; \
151         *pi_data += (l);                  \
152         *pp_data = realloc( *pp_data, *pi_data );   \
153         *pp_dst = (*pp_data) + __i__;   \
154     }
155 #define PRINT( str ) \
156     ALLOC( strlen( str ) + 1 ); \
157     *pp_dst += sprintf( *pp_dst, "%s", str );
158
159 #define PRINTS( str, s ) \
160     ALLOC( strlen( str ) + strlen( s ) + 1 ); \
161     { \
162         char * psz_cur = *pp_dst; \
163         *pp_dst += sprintf( *pp_dst, str, s ); \
164         while( psz_cur && *psz_cur ) \
165         {  \
166             /* Prevent script injection */ \
167             if( *psz_cur == '<' ) *psz_cur = '*'; \
168             if( *psz_cur == '>' ) *psz_cur = '*'; \
169             psz_cur++ ; \
170         } \
171     }
172
173     switch( StrToMacroType( m->id ) )
174     {
175         case MVLC_CONTROL:
176             if( i_request <= 0 )
177             {
178                 break;
179             }
180             ExtractURIValue( p_request, "control", control, 512 );
181             if( *m->param1 && !strstr( m->param1, control ) )
182             {
183                 msg_Warn( p_intf, "unauthorized control=%s", control );
184                 break;
185             }
186             switch( StrToMacroType( control ) )
187             {
188                 case MVLC_PLAY:
189                 {
190                     int i_item;
191                     char item[512];
192
193                     ExtractURIValue( p_request, "item", item, 512 );
194                     i_item = atoi( item );
195                     /* id = 0 : simply ask playlist to play */
196                     if( i_item == 0 )
197                     {
198                         playlist_Play( p_sys->p_playlist );
199                         msg_Dbg( p_intf, "requested playlist play" );
200                         break;
201                     }
202                     playlist_Control( p_sys->p_playlist, PLAYLIST_VIEWPLAY,
203                                       true, NULL,
204                                       playlist_ItemGetById( p_sys->p_playlist,
205                                       i_item, true ) );
206                     msg_Dbg( p_intf, "requested playlist item: %i", i_item );
207                     break;
208                 }
209                 case MVLC_STOP:
210                     playlist_Control( p_sys->p_playlist, PLAYLIST_STOP,
211                                       true );
212                     msg_Dbg( p_intf, "requested playlist stop" );
213                     break;
214                 case MVLC_PAUSE:
215                     playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE,
216                                       true );
217                     msg_Dbg( p_intf, "requested playlist pause" );
218                     break;
219                 case MVLC_NEXT:
220                     playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP,
221                                       true, 1 );
222                     msg_Dbg( p_intf, "requested playlist next" );
223                     break;
224                 case MVLC_PREVIOUS:
225                     playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP,
226                                       true, -1 );
227                     msg_Dbg( p_intf, "requested playlist previous" );
228                     break;
229                 case MVLC_FULLSCREEN:
230                     if( p_sys->p_input )
231                     {
232                         vout_thread_t *p_vout;
233                         p_vout = vlc_object_find( p_sys->p_input,
234                                                   VLC_OBJECT_VOUT, FIND_CHILD );
235
236                         if( p_vout )
237                         {
238                             p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
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_intf, &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_intf , AOUT_VOLUME_MAX );
268                             msg_Dbg( p_intf, "requested volume set: max" );
269                         }
270                         else
271                         {
272                             aout_VolumeSet( p_intf , (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_intf , AOUT_VOLUME_MIN );
282                             msg_Dbg( p_intf, "requested volume set: min" );
283                         }
284                         else
285                         {
286                             aout_VolumeSet( p_intf , (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_intf, (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_intf , 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 = input_item_GetURI( p_input );
342                     if( !p_input || !psz_uri || !*psz_uri )
343                     {
344                         msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
345                     }
346                     else
347                     {
348                         int i_ret = playlist_AddInput( p_sys->p_playlist,
349                                      p_input,
350                                      PLAYLIST_APPEND, PLAYLIST_END, true,
351                                      false);
352                         vlc_gc_decref( p_input );
353                         if( i_ret == VLC_SUCCESS )
354                             msg_Dbg( p_intf, "requested mrl add: %s", mrl );
355                         else
356                             msg_Warn( p_intf, "adding mrl %s failed", mrl );
357                     }
358                     free( psz_uri );
359
360                     break;
361                 }
362                 case MVLC_DEL:
363                 {
364                     int i_item, *p_items = NULL, i_nb_items = 0;
365                     char item[512];
366                     const char *p_parser = p_request;
367
368                     /* Get the list of items to delete */
369                     while( (p_parser =
370                             ExtractURIValue( p_parser, "item", item, 512 )) )
371                     {
372                         if( !*item ) continue;
373
374                         i_item = atoi( item );
375                         p_items = realloc( p_items, (i_nb_items + 1) *
376                                            sizeof(int) );
377                         p_items[i_nb_items] = i_item;
378                         i_nb_items++;
379                     }
380
381                     if( i_nb_items )
382                     {
383                         int i;
384                         for( i = 0; i < i_nb_items; i++ )
385                         {
386                             playlist_DeleteFromInput( p_sys->p_playlist,
387                                                       p_items[i], false );
388                             msg_Dbg( p_intf, "requested playlist delete: %d",
389                                      p_items[i] );
390                             p_items[i] = -1;
391                         }
392                     }
393
394                     free( p_items );
395                     break;
396                 }
397                 case MVLC_KEEP:
398                 {
399                     int i_item, *p_items = NULL, i_nb_items = 0;
400                     char item[512];
401                     const char *p_parser = p_request;
402                     int i,j;
403
404                     /* Get the list of items to keep */
405                     while( (p_parser =
406                        ExtractURIValue( p_parser, "item", item, 512 )) )
407                     {
408                         if( !*item ) continue;
409
410                         i_item = atoi( item );
411                         p_items = realloc( p_items, (i_nb_items + 1) *
412                                            sizeof(int) );
413                         p_items[i_nb_items] = i_item;
414                         i_nb_items++;
415                     }
416
417                     for( i = p_sys->p_playlist->items.i_size - 1 ; i >= 0; i-- )
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)
424                                                 ->i_id)
425                                 break;
426                         }
427                         if( j == i_nb_items )
428                         {
429                             playlist_DeleteFromInput( p_sys->p_playlist,
430                                      p_sys->p_playlist->items.p_elems[i]->i_id,
431                                                       false );
432                             msg_Dbg( p_intf, "requested playlist delete: %d",
433                                      i );
434                         }
435                     }
436
437                     free( p_items );
438                     break;
439                 }
440                 case MVLC_EMPTY:
441                 {
442                     playlist_Clear( p_sys->p_playlist, false );
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                         playlist_RecursiveNodeSort( p_sys->p_playlist,
465                                                     /* Ugly hack,but not worse than before ... */
466                                                     p_sys->p_playlist->p_root_onelevel,
467                                                     SORT_TITLE_NODES_FIRST,
468                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
469                         msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
470                     }
471                     else if( !strcmp( type , "author" ) )
472                     {
473                         playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
474                                                     p_sys->p_playlist->p_root_onelevel,
475                                                     SORT_ARTIST,
476                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
477                         msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
478                     }
479                     else if( !strcmp( type , "shuffle" ) )
480                     {
481                         playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
482                                                     p_sys->p_playlist->p_root_onelevel,
483                                                     SORT_RANDOM,
484                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
485                         msg_Dbg( p_intf, "requested playlist shuffle");
486                     }
487
488                     break;
489                 }
490                 case MVLC_MOVE:
491                 {
492                     char psz_pos[6];
493                     char psz_newpos[6];
494                     int i_pos;
495                     int i_newpos;
496                     ExtractURIValue( p_request, "psz_pos", psz_pos, 6 );
497                     ExtractURIValue( p_request, "psz_newpos", psz_newpos, 6 );
498                     i_pos = atoi( psz_pos );
499                     i_newpos = atoi( psz_newpos );
500                     /* FIXME FIXME TODO TODO XXX XXX
501                     ( duplicate from rpn.c )
502                     if ( i_pos < i_newpos )
503                     {
504                         playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
505                     }
506                     else
507                     {
508                         playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
509                     }
510                     msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
511                     FIXME FIXME TODO TODO XXX XXX */
512                     break;
513                 }
514
515                 /* admin function */
516                 case MVLC_CLOSE:
517                 {
518                     char id[512];
519                     ExtractURIValue( p_request, "id", id, 512 );
520                     msg_Dbg( p_intf, "requested close id=%s", id );
521 #if 0
522                     if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
523                     {
524                         msg_Warn( p_intf, "close failed for id=%s", id );
525                     }
526 #endif
527                     break;
528                 }
529                 case MVLC_SHUTDOWN:
530                 {
531                     msg_Dbg( p_intf, "requested shutdown" );
532                     vlc_object_kill( p_intf->p_libvlc );
533                     break;
534                 }
535 #ifdef ENABLE_VLM
536                 /* vlm */
537                 case MVLC_VLM_NEW:
538                 case MVLC_VLM_SETUP:
539                 {
540                     static const char vlm_properties[][9] =
541                     {
542                         /* no args */
543                         "enabled", "disabled", "loop", "unloop",
544                         /* args required */
545                         "input", "output", "option", "date", "period",
546                         "repeat", "append", "",
547                     };
548                     vlm_message_t *vlm_answer;
549                     char name[512];
550                     char *psz = malloc( strlen( p_request ) + 1000 );
551                     char *p = psz;
552                     char *vlm_error;
553                     int i;
554
555                     if( p_intf->p_sys->p_vlm == NULL )
556                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
557
558                     if( p_intf->p_sys->p_vlm == NULL )
559                     {
560                         free( psz );
561                         break;
562                     }
563
564                     ExtractURIValue( p_request, "name", name, 512 );
565                     if( StrToMacroType( control ) == MVLC_VLM_NEW )
566                     {
567                         char type[20];
568                         ExtractURIValue( p_request, "type", type, 20 );
569                         p += sprintf( psz, "new %s %s", name, type );
570                     }
571                     else
572                     {
573                         p += sprintf( psz, "setup %s", name );
574                     }
575                     /* Parse the request */
576                     for( i = 0; vlm_properties[i][0]; i++ )
577                     {
578                         char val[512];
579                         ExtractURIValue( p_request,
580                                                vlm_properties[i], val, 512 );
581                         decode_URI( val );
582                         if( strlen( val ) > 0 && i >= 4 )
583                         {
584                             p += sprintf( p, " %s %s", vlm_properties[i], val );
585                         }
586                         else if( TestURIParam( p_request, vlm_properties[i] ) && i < 4 )
587                         {
588                             p += sprintf( p, " %s", vlm_properties[i] );
589                         }
590                     }
591                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
592                     if( vlm_answer->psz_value == NULL ) /* there is no error */
593                     {
594                         vlm_error = strdup( "" );
595                     }
596                     else
597                     {
598                         vlm_error = malloc( strlen(vlm_answer->psz_name) +
599                                             strlen(vlm_answer->psz_value) +
600                                             strlen( " : ") + 1 );
601                         sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
602                                                          vlm_answer->psz_value );
603                     }
604
605                     mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
606
607                     vlm_MessageDelete( vlm_answer );
608                     free( vlm_error );
609                     free( psz );
610                     break;
611                 }
612
613                 case MVLC_VLM_DEL:
614                 {
615                     vlm_message_t *vlm_answer;
616                     char name[512];
617                     char psz[512+10];
618                     if( p_intf->p_sys->p_vlm == NULL )
619                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
620
621                     if( p_intf->p_sys->p_vlm == NULL ) break;
622
623                     ExtractURIValue( p_request, "name", name, 512 );
624                     sprintf( psz, "del %s", name );
625
626                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
627                     /* FIXME do a vlm_answer -> var stack conversion */
628                     vlm_MessageDelete( vlm_answer );
629                     break;
630                 }
631
632                 case MVLC_VLM_PLAY:
633                 case MVLC_VLM_PAUSE:
634                 case MVLC_VLM_STOP:
635                 case MVLC_VLM_SEEK:
636                 {
637                     vlm_message_t *vlm_answer;
638                     char name[512];
639                     char psz[512+10];
640                     if( p_intf->p_sys->p_vlm == NULL )
641                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
642
643                     if( p_intf->p_sys->p_vlm == NULL ) break;
644
645                     ExtractURIValue( p_request, "name", name, 512 );
646                     if( StrToMacroType( control ) == MVLC_VLM_PLAY )
647                         sprintf( psz, "control %s play", name );
648                     else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
649                         sprintf( psz, "control %s pause", name );
650                     else if( StrToMacroType( control ) == MVLC_VLM_STOP )
651                         sprintf( psz, "control %s stop", name );
652                     else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
653                     {
654                         char percent[20];
655                         ExtractURIValue( p_request, "percent", percent, 512 );
656                         sprintf( psz, "control %s seek %s", name, percent );
657                     }
658
659                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
660                     /* FIXME do a vlm_answer -> var stack conversion */
661                     vlm_MessageDelete( vlm_answer );
662                     break;
663                 }
664                 case MVLC_VLM_LOAD:
665                 case MVLC_VLM_SAVE:
666                 {
667                     vlm_message_t *vlm_answer;
668                     char file[512];
669                     char psz[512];
670
671                     if( p_intf->p_sys->p_vlm == NULL )
672                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
673
674                     if( p_intf->p_sys->p_vlm == NULL ) break;
675
676                     ExtractURIValue( p_request, "file", file, 512 );
677                     decode_URI( file );
678
679                     if( StrToMacroType( control ) == MVLC_VLM_LOAD )
680                         sprintf( psz, "load %s", file );
681                     else
682                         sprintf( psz, "save %s", file );
683
684                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
685                     /* FIXME do a vlm_answer -> var stack conversion */
686                     vlm_MessageDelete( vlm_answer );
687                     break;
688                 }
689 #endif /* ENABLE_VLM */
690                 default:
691                     if( *control )
692                     {
693                         PRINTS( "<!-- control param(%s) unsupported -->", control );
694                     }
695                     break;
696             }
697             break;
698
699         case MVLC_SET:
700         {
701             char    value[512];
702             int     i;
703             float   f;
704
705             if( i_request <= 0 ||
706                 *m->param1  == '\0' ||
707                 strstr( p_request, m->param1 ) == NULL )
708             {
709                 break;
710             }
711             ExtractURIValue( p_request, m->param1,  value, 512 );
712             decode_URI( value );
713
714             switch( StrToMacroType( m->param2 ) )
715             {
716                 case MVLC_INT:
717                     i = atoi( value );
718                     config_PutInt( p_intf, m->param1, i );
719                     break;
720                 case MVLC_FLOAT:
721                     f = atof( value );
722                     config_PutFloat( p_intf, m->param1, f );
723                     break;
724                 case MVLC_STRING:
725                     config_PutPsz( p_intf, m->param1, value );
726                     break;
727                 default:
728                     PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
729             }
730             break;
731         }
732         case MVLC_GET:
733         {
734             char    value[512];
735             int     i;
736             float   f;
737             char    *psz;
738             lldiv_t div;
739
740             if( *m->param1  == '\0' )
741             {
742                 break;
743             }
744
745             switch( StrToMacroType( m->param2 ) )
746             {
747                 case MVLC_INT:
748                     i = config_GetInt( p_intf, m->param1 );
749                     sprintf( value, "%d", i );
750                     break;
751                 case MVLC_FLOAT:
752                     f = config_GetFloat( p_intf, m->param1 );
753                     div = lldiv( f * 1000000 , 1000000 );
754                     sprintf( value, "%lld.%06u", div.quot,
755                             (unsigned int)div.rem );
756                     break;
757                 case MVLC_STRING:
758                     psz = config_GetPsz( p_intf, m->param1 );
759                     if( psz != NULL )
760                     {
761                         strlcpy( value, psz,sizeof( value ) );
762                         free( psz );
763                     }
764                     else
765                         *value = '\0';
766                     msg_Dbg( p_intf, "%d: value = \"%s\"", __LINE__, value );
767                     break;
768                 default:
769                     snprintf( value, sizeof( value ),
770                               "invalid type(%s) in set", m->param2 );
771                     break;
772             }
773             PRINTS( "%s", value );
774             break;
775         }
776         case MVLC_VALUE:
777         {
778             char *s;
779             const char *v;
780
781             if( m->param1 )
782             {
783                 EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m->param1 );
784                 s = SSPop( &p_args->stack );
785                 v = mvar_GetValue( p_args->vars, s );
786             }
787             else
788             {
789                 v = s = SSPop( &p_args->stack );
790             }
791
792             PRINTS( "%s", v );
793             free( s );
794             break;
795         }
796         case MVLC_RPN:
797             EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m->param1 );
798             break;
799
800         /* Useful to learn stack management */
801         case MVLC_STACK:
802         {
803             int i;
804             msg_Dbg( p_intf, "stack" );
805             for (i=0;i<(&p_args->stack)->i_stack;i++)
806                 msg_Dbg( p_intf, "%d -> %s", i, (&p_args->stack)->stack[i] );
807             break;
808         }
809
810         case MVLC_UNKNOWN:
811         default:
812             PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
813             msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
814             break;
815     }
816 #undef PRINTS
817 #undef PRINT
818 #undef ALLOC
819 }
820
821 static
822 char *MacroSearch( char *src, char *end, int i_mvlc, bool b_after )
823 {
824     int     i_id;
825     int     i_level = 0;
826
827     while( src < end )
828     {
829         if( src + 4 < end  && !strncmp( (char *)src, "<vlc", 4 ) )
830         {
831             int i_skip;
832             macro_t m;
833
834             i_skip = MacroParse( &m, src );
835
836             i_id = StrToMacroType( m.id );
837
838             switch( i_id )
839             {
840                 case MVLC_IF:
841                 case MVLC_FOREACH:
842                     i_level++;
843                     break;
844                 case MVLC_END:
845                     i_level--;
846                     break;
847                 default:
848                     break;
849             }
850
851             MacroClean( &m );
852
853             if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
854                 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
855             {
856                 return src + ( b_after ? i_skip : 0 );
857             }
858             else if( i_level < 0 )
859             {
860                 return NULL;
861             }
862
863             src += i_skip;
864         }
865         else
866         {
867             src++;
868         }
869     }
870
871     return NULL;
872 }
873
874 void Execute( httpd_file_sys_t *p_args,
875                      char *p_request, int i_request,
876                      char **pp_data, int *pi_data,
877                      char **pp_dst,
878                      char *_src, char *_end )
879 {
880     intf_thread_t  *p_intf = p_args->p_intf;
881
882     char *src, *dup, *end;
883     char *dst = *pp_dst;
884
885     src = dup = malloc( _end - _src + 1 );
886     end = src +( _end - _src );
887
888     memcpy( src, _src, _end - _src );
889     *end = '\0';
890
891     /* we parse searching <vlc */
892     while( src < end )
893     {
894         char *p;
895         int i_copy;
896
897         p = (char *)strstr( (char *)src, "<vlc" );
898         if( p < end && p == src )
899         {
900             macro_t m;
901
902             src += MacroParse( &m, src );
903
904             //msg_Dbg( p_intf, "macro_id=%s", m.id );
905
906             switch( StrToMacroType( m.id ) )
907             {
908                 case MVLC_INCLUDE:
909                 {
910                     FILE *f;
911                     int  i_buffer;
912                     char *p_buffer;
913                     char psz_file[MAX_DIR_SIZE];
914                     char *p;
915                     char sep;
916
917 #if defined( WIN32 )
918                     sep = '\\';
919 #else
920                     sep = '/';
921 #endif
922
923                     if( m.param1[0] != sep )
924                     {
925                         strcpy( psz_file, p_args->file );
926                         p = strrchr( psz_file, sep );
927                         if( p != NULL )
928                             strcpy( p + 1, m.param1 );
929                         else
930                             strcpy( psz_file, m.param1 );
931                     }
932                     else
933                     {
934                         strcpy( psz_file, m.param1 );
935                     }
936
937                     /* We hereby assume that psz_file is in the
938                      * local character encoding */
939                     if( ( f = fopen( psz_file, "r" ) ) == NULL )
940                     {
941                         msg_Warn( p_args->p_intf,
942                                   "unable to include file %s (%m)",
943                                   psz_file );
944                         break;
945                     }
946
947                     /* first we load in a temporary buffer */
948                     FileLoad( f, &p_buffer, &i_buffer );
949
950                     /* we parse executing all  <vlc /> macros */
951                     Execute( p_args, p_request, i_request, pp_data, pi_data,
952                              &dst, &p_buffer[0], &p_buffer[i_buffer] );
953                     free( p_buffer );
954                     fclose(f);
955                     break;
956                 }
957                 case MVLC_IF:
958                 {
959                     bool i_test;
960                     char    *endif;
961
962                     EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m.param1 );
963                     if( SSPopN( &p_args->stack, p_args->vars ) )
964                     {
965                         i_test = 1;
966                     }
967                     else
968                     {
969                         i_test = 0;
970                     }
971                     endif = MacroSearch( src, end, MVLC_END, true );
972
973                     if( i_test == 0 )
974                     {
975                         char *start = MacroSearch( src, endif, MVLC_ELSE, true );
976
977                         if( start )
978                         {
979                             char *stop  = MacroSearch( start, endif, MVLC_END, false );
980                             if( stop )
981                             {
982                                 Execute( p_args, p_request, i_request,
983                                          pp_data, pi_data, &dst, start, stop );
984                             }
985                         }
986                     }
987                     else if( i_test == 1 )
988                     {
989                         char *stop;
990                         if( ( stop = MacroSearch( src, endif, MVLC_ELSE, false ) ) == NULL )
991                         {
992                             stop = MacroSearch( src, endif, MVLC_END, false );
993                         }
994                         if( stop )
995                         {
996                             Execute( p_args, p_request, i_request,
997                                      pp_data, pi_data, &dst, src, stop );
998                         }
999                     }
1000
1001                     src = endif;
1002                     break;
1003                 }
1004                 case MVLC_FOREACH:
1005                 {
1006                     char *endfor = MacroSearch( src, end, MVLC_END, true );
1007                     char *start = src;
1008                     char *stop = MacroSearch( src, end, MVLC_END, false );
1009
1010                     if( stop )
1011                     {
1012                         mvar_t *index;
1013                         int    i_idx;
1014                         mvar_t *v;
1015                         if( !strcmp( m.param2, "integer" ) )
1016                         {
1017                             char *arg = SSPop( &p_args->stack );
1018                             index = mvar_IntegerSetNew( m.param1, arg );
1019                             free( arg );
1020                         }
1021                         else if( !strcmp( m.param2, "directory" ) )
1022                         {
1023                             char *arg = SSPop( &p_args->stack );
1024                             index = mvar_FileSetNew( p_intf, m.param1, arg );
1025                             free( arg );
1026                         }
1027                         else if( !strcmp( m.param2, "object" ) )
1028                         {
1029                             char *arg = SSPop( &p_args->stack );
1030                             index = mvar_ObjectSetNew( p_intf, m.param1, arg );
1031                             free( arg );
1032                         }
1033                         else if( !strcmp( m.param2, "playlist" ) )
1034                         {
1035                             index = mvar_PlaylistSetNew( p_intf, m.param1,
1036                                                     p_intf->p_sys->p_playlist );
1037                         }
1038                         else if( !strcmp( m.param2, "information" ) )
1039                         {
1040                             index = mvar_InfoSetNew( m.param1,
1041                                                      p_intf->p_sys->p_input );
1042                         }
1043                         else if( !strcmp( m.param2, "program" )
1044                                   || !strcmp( m.param2, "title" )
1045                                   || !strcmp( m.param2, "chapter" )
1046                                   || !strcmp( m.param2, "audio-es" )
1047                                   || !strcmp( m.param2, "video-es" )
1048                                   || !strcmp( m.param2, "spu-es" ) )
1049                         {
1050                             index = mvar_InputVarSetNew( p_intf, m.param1,
1051                                                          p_intf->p_sys->p_input,
1052                                                          m.param2 );
1053                         }
1054 #ifdef ENABLE_VLM
1055                         else if( !strcmp( m.param2, "vlm" ) )
1056                         {
1057                             if( p_intf->p_sys->p_vlm == NULL )
1058                                 p_intf->p_sys->p_vlm = vlm_New( p_intf );
1059                             index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
1060                         }
1061 #endif
1062 #if 0
1063                         else if( !strcmp( m.param2, "hosts" ) )
1064                         {
1065                             index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
1066                         }
1067                         else if( !strcmp( m.param2, "urls" ) )
1068                         {
1069                             index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
1070                         }
1071                         else if( !strcmp( m.param2, "connections" ) )
1072                         {
1073                             index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1074                         }
1075 #endif
1076                         else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
1077                         {
1078                             index = mvar_Duplicate( v );
1079                         }
1080                         else
1081                         {
1082                             msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
1083                             src = endfor;
1084                             break;
1085                         }
1086
1087                         for( i_idx = 0; i_idx < index->i_field; i_idx++ )
1088                         {
1089                             mvar_t *f = mvar_Duplicate( index->field[i_idx] );
1090
1091                             //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
1092
1093                             free( f->name );
1094                             f->name = strdup( m.param1 );
1095
1096
1097                             mvar_PushVar( p_args->vars, f );
1098                             Execute( p_args, p_request, i_request,
1099                                      pp_data, pi_data, &dst, start, stop );
1100                             mvar_RemoveVar( p_args->vars, f );
1101
1102                             mvar_Delete( f );
1103                         }
1104                         mvar_Delete( index );
1105
1106                         src = endfor;
1107                     }
1108                     break;
1109                 }
1110                 default:
1111                     MacroDo( p_args, &m, p_request, i_request,
1112                              pp_data, pi_data, &dst );
1113                     break;
1114             }
1115
1116             MacroClean( &m );
1117             continue;
1118         }
1119
1120         i_copy =   ( (p == NULL || p > end ) ? end : p  ) - src;
1121         if( i_copy > 0 )
1122         {
1123             int i_index = dst - *pp_data;
1124
1125             *pi_data += i_copy;
1126             *pp_data = realloc( *pp_data, *pi_data );
1127             dst = (*pp_data) + i_index;
1128
1129             memcpy( dst, src, i_copy );
1130             dst += i_copy;
1131             src += i_copy;
1132         }
1133     }
1134
1135     *pp_dst = dst;
1136     free( dup );
1137 }