]> git.sesse.net Git - vlc/blob - modules/control/http/macro.c
60c5454248d60e518859e406abe7653cc9bf0daf
[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                     playlist_item_t *p_item;
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_item = E_(MRLParse)( p_intf, mrl, psz_name );
331
332                     if( !p_item || !p_item->input.psz_uri ||
333                         !*p_item->input.psz_uri )
334                     {
335                         msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
336                     }
337                     else
338                     {
339                         playlist_AddItem( p_sys->p_playlist, p_item,
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]->input.i_id ) break;
405                         }
406                         if( j == i_nb_items )
407                         {
408                             playlist_LockDelete( p_sys->p_playlist, p_sys->p_playlist->pp_items[i]->input.i_id );
409                             msg_Dbg( p_intf, "requested playlist delete: %d",
410                                      i );
411                         }
412                     }
413
414                     if( p_items ) free( p_items );
415                     break;
416                 }
417                 case MVLC_EMPTY:
418                 {
419                     playlist_LockClear( p_sys->p_playlist );
420                     msg_Dbg( p_intf, "requested playlist empty" );
421                     break;
422                 }
423                 case MVLC_SORT:
424                 {
425                     char type[12];
426                     char order[2];
427                     char item[512];
428                     int i_order;
429                     int i_item;
430
431                     E_(ExtractURIValue)( p_request, "type", type, 12 );
432                     E_(ExtractURIValue)( p_request, "order", order, 2 );
433                     E_(ExtractURIValue)( p_request, "item", item, 512 );
434                     i_item = atoi( item );
435
436                     if( order[0] == '0' ) i_order = ORDER_NORMAL;
437                     else i_order = ORDER_REVERSE;
438
439                     if( !strcmp( type , "title" ) )
440                     {
441                         playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
442                                                     p_sys->p_playlist->pp_views[0]->p_root,
443                                                     SORT_TITLE_NODES_FIRST,
444                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
445                         msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
446                     }
447                     else if( !strcmp( type , "author" ) )
448                     {
449                         playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
450                                                     p_sys->p_playlist->pp_views[0]->p_root,
451                                                     SORT_AUTHOR,
452                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
453                         msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
454                     }
455                     else if( !strcmp( type , "shuffle" ) )
456                     {
457                         playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
458                                                     p_sys->p_playlist->pp_views[0]->p_root,
459                                                     SORT_RANDOM,
460                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
461                         msg_Dbg( p_intf, "requested playlist shuffle");
462                     }
463
464                     break;
465                 }
466                 case MVLC_MOVE:
467                 {
468                     char psz_pos[6];
469                     char psz_newpos[6];
470                     int i_pos;
471                     int i_newpos;
472                     E_(ExtractURIValue)( p_request, "psz_pos", psz_pos, 6 );
473                     E_(ExtractURIValue)( p_request, "psz_newpos", psz_newpos, 6 );
474                     i_pos = atoi( psz_pos );
475                     i_newpos = atoi( psz_newpos );
476                     if ( i_pos < i_newpos )
477                     {
478                         playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
479                     }
480                     else
481                     {
482                         playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
483                     }
484                     msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
485                     break;
486                 }
487
488                 /* admin function */
489                 case MVLC_CLOSE:
490                 {
491                     char id[512];
492                     E_(ExtractURIValue)( p_request, "id", id, 512 );
493                     msg_Dbg( p_intf, "requested close id=%s", id );
494 #if 0
495                     if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
496                     {
497                         msg_Warn( p_intf, "close failed for id=%s", id );
498                     }
499 #endif
500                     break;
501                 }
502                 case MVLC_SHUTDOWN:
503                 {
504                     msg_Dbg( p_intf, "requested shutdown" );
505                     p_intf->p_vlc->b_die = VLC_TRUE;
506                     break;
507                 }
508                 /* vlm */
509                 case MVLC_VLM_NEW:
510                 case MVLC_VLM_SETUP:
511                 {
512                     static const char *vlm_properties[11] =
513                     {
514                         /* no args */
515                         "enabled", "disabled", "loop", "unloop",
516                         /* args required */
517                         "input", "output", "option", "date", "period", "repeat", "append",
518                     };
519                     vlm_message_t *vlm_answer;
520                     char name[512];
521                     char *psz = malloc( strlen( p_request ) + 1000 );
522                     char *p = psz;
523                     char *vlm_error;
524                     int i;
525
526                     if( p_intf->p_sys->p_vlm == NULL )
527                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
528
529                     if( p_intf->p_sys->p_vlm == NULL ) break;
530
531                     E_(ExtractURIValue)( p_request, "name", name, 512 );
532                     if( E_(StrToMacroType)( control ) == MVLC_VLM_NEW )
533                     {
534                         char type[20];
535                         E_(ExtractURIValue)( p_request, "type", type, 20 );
536                         p += sprintf( psz, "new %s %s", name, type );
537                     }
538                     else
539                     {
540                         p += sprintf( psz, "setup %s", name );
541                     }
542                     /* Parse the request */
543                     for( i = 0; i < 11; i++ )
544                     {
545                         char val[512];
546                         E_(ExtractURIValue)( p_request,
547                                                vlm_properties[i], val, 512 );
548                         decode_URI( val );
549                         if( strlen( val ) > 0 && i >= 4 )
550                         {
551                             p += sprintf( p, " %s %s", vlm_properties[i], val );
552                         }
553                         else if( E_(TestURIParam)( p_request, vlm_properties[i] ) && i < 4 )
554                         {
555                             p += sprintf( p, " %s", vlm_properties[i] );
556                         }
557                     }
558                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
559                     if( vlm_answer->psz_value == NULL ) /* there is no error */
560                     {
561                         vlm_error = strdup( "" );
562                     }
563                     else
564                     {
565                         vlm_error = malloc( strlen(vlm_answer->psz_name) +
566                                             strlen(vlm_answer->psz_value) +
567                                             strlen( " : ") + 1 );
568                         sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
569                                                          vlm_answer->psz_value );
570                     }
571
572                     E_(mvar_AppendNewVar)( p_args->vars, "vlm_error", vlm_error );
573
574                     vlm_MessageDelete( vlm_answer );
575                     free( vlm_error );
576                     free( psz );
577                     break;
578                 }
579
580                 case MVLC_VLM_DEL:
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_(ExtractURIValue)( p_request, "name", name, 512 );
591                     sprintf( psz, "del %s", name );
592
593                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
594                     /* FIXME do a vlm_answer -> var stack conversion */
595                     vlm_MessageDelete( vlm_answer );
596                     break;
597                 }
598
599                 case MVLC_VLM_PLAY:
600                 case MVLC_VLM_PAUSE:
601                 case MVLC_VLM_STOP:
602                 case MVLC_VLM_SEEK:
603                 {
604                     vlm_message_t *vlm_answer;
605                     char name[512];
606                     char psz[512+10];
607                     if( p_intf->p_sys->p_vlm == NULL )
608                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
609
610                     if( p_intf->p_sys->p_vlm == NULL ) break;
611
612                     E_(ExtractURIValue)( p_request, "name", name, 512 );
613                     if( E_(StrToMacroType)( control ) == MVLC_VLM_PLAY )
614                         sprintf( psz, "control %s play", name );
615                     else if( E_(StrToMacroType)( control ) == MVLC_VLM_PAUSE )
616                         sprintf( psz, "control %s pause", name );
617                     else if( E_(StrToMacroType)( control ) == MVLC_VLM_STOP )
618                         sprintf( psz, "control %s stop", name );
619                     else if( E_(StrToMacroType)( control ) == MVLC_VLM_SEEK )
620                     {
621                         char percent[20];
622                         E_(ExtractURIValue)( p_request, "percent", percent, 512 );
623                         sprintf( psz, "control %s seek %s", name, percent );
624                     }
625
626                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
627                     /* FIXME do a vlm_answer -> var stack conversion */
628                     vlm_MessageDelete( vlm_answer );
629                     break;
630                 }
631                 case MVLC_VLM_LOAD:
632                 case MVLC_VLM_SAVE:
633                 {
634                     vlm_message_t *vlm_answer;
635                     char file[512];
636                     char psz[512];
637
638                     if( p_intf->p_sys->p_vlm == NULL )
639                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
640
641                     if( p_intf->p_sys->p_vlm == NULL ) break;
642
643                     E_(ExtractURIValue)( p_request, "file", file, 512 );
644                     decode_URI( file );
645
646                     if( E_(StrToMacroType)( control ) == MVLC_VLM_LOAD )
647                         sprintf( psz, "load %s", file );
648                     else
649                         sprintf( psz, "save %s", file );
650
651                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
652                     /* FIXME do a vlm_answer -> var stack conversion */
653                     vlm_MessageDelete( vlm_answer );
654                     break;
655                 }
656
657                 default:
658                     if( *control )
659                     {
660                         PRINTS( "<!-- control param(%s) unsupported -->", control );
661                     }
662                     break;
663             }
664             break;
665
666         case MVLC_SET:
667         {
668             char    value[512];
669             int     i;
670             float   f;
671
672             if( i_request <= 0 ||
673                 *m->param1  == '\0' ||
674                 strstr( p_request, m->param1 ) == NULL )
675             {
676                 break;
677             }
678             E_(ExtractURIValue)( p_request, m->param1,  value, 512 );
679             decode_URI( value );
680
681             switch( E_(StrToMacroType)( m->param2 ) )
682             {
683                 case MVLC_INT:
684                     i = atoi( value );
685                     config_PutInt( p_intf, m->param1, i );
686                     break;
687                 case MVLC_FLOAT:
688                     f = atof( value );
689                     config_PutFloat( p_intf, m->param1, f );
690                     break;
691                 case MVLC_STRING:
692                     config_PutPsz( p_intf, m->param1, value );
693                     break;
694                 default:
695                     PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
696             }
697             break;
698         }
699         case MVLC_GET:
700         {
701             char    value[512];
702             int     i;
703             float   f;
704             char    *psz;
705             lldiv_t div;
706
707             if( *m->param1  == '\0' )
708             {
709                 break;
710             }
711
712             switch( E_(StrToMacroType)( m->param2 ) )
713             {
714                 case MVLC_INT:
715                     i = config_GetInt( p_intf, m->param1 );
716                     sprintf( value, "%d", i );
717                     break;
718                 case MVLC_FLOAT:
719                     f = config_GetFloat( p_intf, m->param1 );
720                     div = lldiv( f * 1000000 , 1000000 );
721                     sprintf( value, I64Fd".%06u", div.quot,
722                             (unsigned int)div.rem );
723                     break;
724                 case MVLC_STRING:
725                     psz = config_GetPsz( p_intf, m->param1 );
726                     if( psz != NULL )
727                     {
728                         strlcpy( value, psz,sizeof( value ) );
729                         free( psz );
730                     }
731                     else
732                         *value = '\0';
733                     msg_Dbg( p_intf, "%d: value = \"%s\"", __LINE__, value );
734                     break;
735                 default:
736                     snprintf( value, sizeof( value ),
737                               "invalid type(%s) in set", m->param2 );
738                     break;
739             }
740             PRINTS( "%s", value );
741             break;
742         }
743         case MVLC_VALUE:
744         {
745             char *s, *v;
746
747             if( m->param1 )
748             {
749                 E_(EvaluateRPN)( p_intf, p_args->vars, &p_args->stack, m->param1 );
750                 s = E_(SSPop)( &p_args->stack );
751                 v = E_(mvar_GetValue)( p_args->vars, s );
752             }
753             else
754             {
755                 v = s = E_(SSPop)( &p_args->stack );
756             }
757
758             PRINTS( "%s", v );
759             free( s );
760             break;
761         }
762         case MVLC_RPN:
763             E_(EvaluateRPN)( p_intf, p_args->vars, &p_args->stack, m->param1 );
764             break;
765
766         /* Useful to learn stack management */
767         case MVLC_STACK:
768         {
769             int i;
770             msg_Dbg( p_intf, "stack" );
771             for (i=0;i<(&p_args->stack)->i_stack;i++)
772                 msg_Dbg( p_intf, "%d -> %s", i, (&p_args->stack)->stack[i] );
773             break;
774         }
775
776         case MVLC_UNKNOWN:
777         default:
778             PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
779             msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
780             break;
781     }
782 #undef PRINTS
783 #undef PRINT
784 #undef ALLOC
785 }
786
787 char *E_(MacroSearch)( char *src, char *end, int i_mvlc, vlc_bool_t b_after )
788 {
789     int     i_id;
790     int     i_level = 0;
791
792     while( src < end )
793     {
794         if( src + 4 < end  && !strncmp( (char *)src, "<vlc", 4 ) )
795         {
796             int i_skip;
797             macro_t m;
798
799             i_skip = E_(MacroParse)( &m, src );
800
801             i_id = E_(StrToMacroType)( m.id );
802
803             switch( i_id )
804             {
805                 case MVLC_IF:
806                 case MVLC_FOREACH:
807                     i_level++;
808                     break;
809                 case MVLC_END:
810                     i_level--;
811                     break;
812                 default:
813                     break;
814             }
815
816             E_(MacroClean)( &m );
817
818             if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
819                 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
820             {
821                 return src + ( b_after ? i_skip : 0 );
822             }
823             else if( i_level < 0 )
824             {
825                 return NULL;
826             }
827
828             src += i_skip;
829         }
830         else
831         {
832             src++;
833         }
834     }
835
836     return NULL;
837 }
838
839 void E_(Execute)( httpd_file_sys_t *p_args,
840                      char *p_request, int i_request,
841                      char **pp_data, int *pi_data,
842                      char **pp_dst,
843                      char *_src, char *_end )
844 {
845     intf_thread_t  *p_intf = p_args->p_intf;
846
847     char *src, *dup, *end;
848     char *dst = *pp_dst;
849
850     src = dup = malloc( _end - _src + 1 );
851     end = src +( _end - _src );
852
853     memcpy( src, _src, _end - _src );
854     *end = '\0';
855
856     /* we parse searching <vlc */
857     while( src < end )
858     {
859         char *p;
860         int i_copy;
861
862         p = (char *)strstr( (char *)src, "<vlc" );
863         if( p < end && p == src )
864         {
865             macro_t m;
866
867             src += E_(MacroParse)( &m, src );
868
869             //msg_Dbg( p_intf, "macro_id=%s", m.id );
870
871             switch( E_(StrToMacroType)( m.id ) )
872             {
873                 case MVLC_INCLUDE:
874                 {
875                     FILE *f;
876                     int  i_buffer;
877                     char *p_buffer;
878                     char psz_file[MAX_DIR_SIZE];
879                     char *p;
880                     char sep;
881
882 #if defined( WIN32 )
883                     sep = '\\';
884 #else
885                     sep = '/';
886 #endif
887
888                     if( m.param1[0] != sep )
889                     {
890                         strcpy( psz_file, p_args->file );
891                         p = strrchr( psz_file, sep );
892                         if( p != NULL )
893                             strcpy( p + 1, m.param1 );
894                         else
895                             strcpy( psz_file, m.param1 );
896                     }
897                     else
898                     {
899                         strcpy( psz_file, m.param1 );
900                     }
901
902                     /* We hereby assume that psz_file is in the
903                      * local character encoding */
904                     if( ( f = fopen( psz_file, "r" ) ) == NULL )
905                     {
906                         msg_Warn( p_args->p_intf,
907                                   "unable to include file %s (%s)",
908                                   psz_file, strerror(errno) );
909                         break;
910                     }
911
912                     /* first we load in a temporary buffer */
913                     E_(FileLoad)( f, &p_buffer, &i_buffer );
914
915                     /* we parse executing all  <vlc /> macros */
916                     E_(Execute)( p_args, p_request, i_request, pp_data, pi_data,
917                              &dst, &p_buffer[0], &p_buffer[i_buffer] );
918                     free( p_buffer );
919                     fclose(f);
920                     break;
921                 }
922                 case MVLC_IF:
923                 {
924                     vlc_bool_t i_test;
925                     char    *endif;
926
927                     E_(EvaluateRPN)( p_intf, p_args->vars, &p_args->stack, m.param1 );
928                     if( E_(SSPopN)( &p_args->stack, p_args->vars ) )
929                     {
930                         i_test = 1;
931                     }
932                     else
933                     {
934                         i_test = 0;
935                     }
936                     endif = E_(MacroSearch)( src, end, MVLC_END, VLC_TRUE );
937
938                     if( i_test == 0 )
939                     {
940                         char *start = E_(MacroSearch)( src, endif, MVLC_ELSE, VLC_TRUE );
941
942                         if( start )
943                         {
944                             char *stop  = E_(MacroSearch)( start, endif, MVLC_END, VLC_FALSE );
945                             if( stop )
946                             {
947                                 E_(Execute)( p_args, p_request, i_request,
948                                          pp_data, pi_data, &dst, start, stop );
949                             }
950                         }
951                     }
952                     else if( i_test == 1 )
953                     {
954                         char *stop;
955                         if( ( stop = E_(MacroSearch)( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
956                         {
957                             stop = E_(MacroSearch)( src, endif, MVLC_END, VLC_FALSE );
958                         }
959                         if( stop )
960                         {
961                             E_(Execute)( p_args, p_request, i_request,
962                                      pp_data, pi_data, &dst, src, stop );
963                         }
964                     }
965
966                     src = endif;
967                     break;
968                 }
969                 case MVLC_FOREACH:
970                 {
971                     char *endfor = E_(MacroSearch)( src, end, MVLC_END, VLC_TRUE );
972                     char *start = src;
973                     char *stop = E_(MacroSearch)( src, end, MVLC_END, VLC_FALSE );
974
975                     if( stop )
976                     {
977                         mvar_t *index;
978                         int    i_idx;
979                         mvar_t *v;
980                         if( !strcmp( m.param2, "integer" ) )
981                         {
982                             char *arg = E_(SSPop)( &p_args->stack );
983                             index = E_(mvar_IntegerSetNew)( m.param1, arg );
984                             free( arg );
985                         }
986                         else if( !strcmp( m.param2, "directory" ) )
987                         {
988                             char *arg = E_(SSPop)( &p_args->stack );
989                             index = E_(mvar_FileSetNew)( p_intf, m.param1, arg );
990                             free( arg );
991                         }
992                         else if( !strcmp( m.param2, "object" ) )
993                         {
994                             char *arg = E_(SSPop)( &p_args->stack );
995                             index = E_(mvar_ObjectSetNew)( p_intf, m.param1, arg );
996                             free( arg );
997                         }
998                         else if( !strcmp( m.param2, "playlist" ) )
999                         {
1000                             index = E_(mvar_PlaylistSetNew)( p_intf, m.param1,
1001                                                     p_intf->p_sys->p_playlist );
1002                         }
1003                         else if( !strcmp( m.param2, "information" ) )
1004                         {
1005                             index = E_(mvar_InfoSetNew)( p_intf, m.param1,
1006                                                      p_intf->p_sys->p_input );
1007                         }
1008                         else if( !strcmp( m.param2, "program" )
1009                                   || !strcmp( m.param2, "title" )
1010                                   || !strcmp( m.param2, "chapter" )
1011                                   || !strcmp( m.param2, "audio-es" )
1012                                   || !strcmp( m.param2, "video-es" )
1013                                   || !strcmp( m.param2, "spu-es" ) )
1014                         {
1015                             index = E_(mvar_InputVarSetNew)( p_intf, m.param1,
1016                                                          p_intf->p_sys->p_input,
1017                                                          m.param2 );
1018                         }
1019                         else if( !strcmp( m.param2, "vlm" ) )
1020                         {
1021                             if( p_intf->p_sys->p_vlm == NULL )
1022                                 p_intf->p_sys->p_vlm = vlm_New( p_intf );
1023                             index = E_(mvar_VlmSetNew)( m.param1, p_intf->p_sys->p_vlm );
1024                         }
1025 #if 0
1026                         else if( !strcmp( m.param2, "hosts" ) )
1027                         {
1028                             index = E_(mvar_HttpdInfoSetNew)( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
1029                         }
1030                         else if( !strcmp( m.param2, "urls" ) )
1031                         {
1032                             index = E_(mvar_HttpdInfoSetNew)( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
1033                         }
1034                         else if( !strcmp( m.param2, "connections" ) )
1035                         {
1036                             index = E_(mvar_HttpdInfoSetNew)(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1037                         }
1038 #endif
1039                         else if( ( v = E_(mvar_GetVar)( p_args->vars, m.param2 ) ) )
1040                         {
1041                             index = E_(mvar_Duplicate)( v );
1042                         }
1043                         else
1044                         {
1045                             msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
1046                             src = endfor;
1047                             break;
1048                         }
1049
1050                         for( i_idx = 0; i_idx < index->i_field; i_idx++ )
1051                         {
1052                             mvar_t *f = E_(mvar_Duplicate)( index->field[i_idx] );
1053
1054                             //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
1055
1056                             free( f->name );
1057                             f->name = strdup( m.param1 );
1058
1059
1060                             E_(mvar_PushVar)( p_args->vars, f );
1061                             E_(Execute)( p_args, p_request, i_request,
1062                                      pp_data, pi_data, &dst, start, stop );
1063                             E_(mvar_RemoveVar)( p_args->vars, f );
1064
1065                             E_(mvar_Delete)( f );
1066                         }
1067                         E_(mvar_Delete)( index );
1068
1069                         src = endfor;
1070                     }
1071                     break;
1072                 }
1073                 default:
1074                     E_(MacroDo)( p_args, &m, p_request, i_request,
1075                                  pp_data, pi_data, &dst );
1076                     break;
1077             }
1078
1079             E_(MacroClean)( &m );
1080             continue;
1081         }
1082
1083         i_copy =   ( (p == NULL || p > end ) ? end : p  ) - src;
1084         if( i_copy > 0 )
1085         {
1086             int i_index = dst - *pp_data;
1087
1088             *pi_data += i_copy;
1089             *pp_data = realloc( *pp_data, *pi_data );
1090             dst = (*pp_data) + i_index;
1091
1092             memcpy( dst, src, i_copy );
1093             dst += i_copy;
1094             src += i_copy;
1095         }
1096     }
1097
1098     *pp_dst = dst;
1099     free( dup );
1100 }