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