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