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