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