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