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