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