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