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