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