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