Don't use %f in HTTP interface (Closes:#584)
[vlc.git] / 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                     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( E_(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                     E_(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( E_(StrToMacroType)( control ) == MVLC_VLM_PLAY )
599                         sprintf( psz, "control %s play", name );
600                     else if( E_(StrToMacroType)( control ) == MVLC_VLM_PAUSE )
601                         sprintf( psz, "control %s pause", name );
602                     else if( E_(StrToMacroType)( control ) == MVLC_VLM_STOP )
603                         sprintf( psz, "control %s stop", name );
604                     else if( E_(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( E_(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( E_(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             lldiv_t div;
691
692             if( *m->param1  == '\0' )
693             {
694                 break;
695             }
696
697             switch( E_(StrToMacroType)( m->param2 ) )
698             {
699                 case MVLC_INT:
700                     i = config_GetInt( p_intf, m->param1 );
701                     sprintf( value, "%d", i );
702                     break;
703                 case MVLC_FLOAT:
704                     f = config_GetFloat( p_intf, m->param1 );
705                     div = lldiv( f * 1000000 , 1000000 );
706                     sprintf( value, I64Fd".%06u", div.quot,
707                             (unsigned int)div.rem );
708                     break;
709                 case MVLC_STRING:
710                     psz = config_GetPsz( p_intf, m->param1 );
711                     if( psz != NULL )
712                     {
713                         strncpy( value, psz,sizeof( value ) );
714                         free( psz );
715                         value[sizeof( value ) - 1] = '\0';
716                     }
717                     else
718                         *value = '\0';
719                     msg_Dbg( p_intf, "%d: value = \"%s\"", __LINE__, value );
720                     break;
721                 default:
722                     snprintf( value, sizeof( value ),
723                               "invalid type(%s) in set", m->param2 );
724                     value[sizeof( value ) - 1] = '\0';
725                     break;
726             }
727             PRINTS( "%s", value );
728             break;
729         }
730         case MVLC_VALUE:
731         {
732             char *s, *v;
733
734             if( m->param1 )
735             {
736                 E_(EvaluateRPN)( p_intf, p_args->vars, &p_args->stack, m->param1 );
737                 s = E_(SSPop)( &p_args->stack );
738                 v = E_(mvar_GetValue)( p_args->vars, s );
739             }
740             else
741             {
742                 v = s = E_(SSPop)( &p_args->stack );
743             }
744
745             PRINTS( "%s", v );
746             free( s );
747             break;
748         }
749         case MVLC_RPN:
750             E_(EvaluateRPN)( p_intf, p_args->vars, &p_args->stack, m->param1 );
751             break;
752
753         /* Useful to learn stack management */
754         case MVLC_STACK:
755         {
756             int i;
757             msg_Dbg( p_intf, "stack" );
758             for (i=0;i<(&p_args->stack)->i_stack;i++)
759                 msg_Dbg( p_intf, "%d -> %s", i, (&p_args->stack)->stack[i] );
760             break;
761         }
762
763         case MVLC_UNKNOWN:
764         default:
765             PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
766             msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
767             break;
768     }
769 #undef PRINTS
770 #undef PRINT
771 #undef ALLOC
772 }
773
774 char *E_(MacroSearch)( char *src, char *end, int i_mvlc, vlc_bool_t b_after )
775 {
776     int     i_id;
777     int     i_level = 0;
778
779     while( src < end )
780     {
781         if( src + 4 < end  && !strncmp( (char *)src, "<vlc", 4 ) )
782         {
783             int i_skip;
784             macro_t m;
785
786             i_skip = E_(MacroParse)( &m, src );
787
788             i_id = E_(StrToMacroType)( m.id );
789
790             switch( i_id )
791             {
792                 case MVLC_IF:
793                 case MVLC_FOREACH:
794                     i_level++;
795                     break;
796                 case MVLC_END:
797                     i_level--;
798                     break;
799                 default:
800                     break;
801             }
802
803             E_(MacroClean)( &m );
804
805             if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
806                 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
807             {
808                 return src + ( b_after ? i_skip : 0 );
809             }
810             else if( i_level < 0 )
811             {
812                 return NULL;
813             }
814
815             src += i_skip;
816         }
817         else
818         {
819             src++;
820         }
821     }
822
823     return NULL;
824 }
825
826 void E_(Execute)( httpd_file_sys_t *p_args,
827                      char *p_request, int i_request,
828                      char **pp_data, int *pi_data,
829                      char **pp_dst,
830                      char *_src, char *_end )
831 {
832     intf_thread_t  *p_intf = p_args->p_intf;
833
834     char *src, *dup, *end;
835     char *dst = *pp_dst;
836
837     src = dup = malloc( _end - _src + 1 );
838     end = src +( _end - _src );
839
840     memcpy( src, _src, _end - _src );
841     *end = '\0';
842
843     /* we parse searching <vlc */
844     while( src < end )
845     {
846         char *p;
847         int i_copy;
848
849         p = (char *)strstr( (char *)src, "<vlc" );
850         if( p < end && p == src )
851         {
852             macro_t m;
853
854             src += E_(MacroParse)( &m, src );
855
856             //msg_Dbg( p_intf, "macro_id=%s", m.id );
857
858             switch( E_(StrToMacroType)( m.id ) )
859             {
860                 case MVLC_INCLUDE:
861                 {
862                     FILE *f;
863                     int  i_buffer;
864                     char *p_buffer;
865                     char psz_file[MAX_DIR_SIZE];
866                     char *p;
867                     char sep;
868
869 #if defined( WIN32 )
870                     sep = '\\';
871 #else
872                     sep = '/';
873 #endif
874
875                     if( m.param1[0] != sep )
876                     {
877                         strcpy( psz_file, p_args->file );
878                         p = strrchr( psz_file, sep );
879                         if( p != NULL )
880                             strcpy( p + 1, m.param1 );
881                         else
882                             strcpy( psz_file, m.param1 );
883                     }
884                     else
885                     {
886                         strcpy( psz_file, m.param1 );
887                     }
888
889                     /* We hereby assume that psz_file is in the
890                      * local character encoding */
891                     if( ( f = fopen( psz_file, "r" ) ) == NULL )
892                     {
893                         msg_Warn( p_args->p_intf,
894                                   "unable to include file %s (%s)",
895                                   psz_file, strerror(errno) );
896                         break;
897                     }
898
899                     /* first we load in a temporary buffer */
900                     E_(FileLoad)( f, &p_buffer, &i_buffer );
901
902                     /* we parse executing all  <vlc /> macros */
903                     E_(Execute)( p_args, p_request, i_request, pp_data, pi_data,
904                              &dst, &p_buffer[0], &p_buffer[i_buffer] );
905                     free( p_buffer );
906                     fclose(f);
907                     break;
908                 }
909                 case MVLC_IF:
910                 {
911                     vlc_bool_t i_test;
912                     char    *endif;
913
914                     E_(EvaluateRPN)( p_intf, p_args->vars, &p_args->stack, m.param1 );
915                     if( E_(SSPopN)( &p_args->stack, p_args->vars ) )
916                     {
917                         i_test = 1;
918                     }
919                     else
920                     {
921                         i_test = 0;
922                     }
923                     endif = E_(MacroSearch)( src, end, MVLC_END, VLC_TRUE );
924
925                     if( i_test == 0 )
926                     {
927                         char *start = E_(MacroSearch)( src, endif, MVLC_ELSE, VLC_TRUE );
928
929                         if( start )
930                         {
931                             char *stop  = E_(MacroSearch)( start, endif, MVLC_END, VLC_FALSE );
932                             if( stop )
933                             {
934                                 E_(Execute)( p_args, p_request, i_request,
935                                          pp_data, pi_data, &dst, start, stop );
936                             }
937                         }
938                     }
939                     else if( i_test == 1 )
940                     {
941                         char *stop;
942                         if( ( stop = E_(MacroSearch)( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
943                         {
944                             stop = E_(MacroSearch)( src, endif, MVLC_END, VLC_FALSE );
945                         }
946                         if( stop )
947                         {
948                             E_(Execute)( p_args, p_request, i_request,
949                                      pp_data, pi_data, &dst, src, stop );
950                         }
951                     }
952
953                     src = endif;
954                     break;
955                 }
956                 case MVLC_FOREACH:
957                 {
958                     char *endfor = E_(MacroSearch)( src, end, MVLC_END, VLC_TRUE );
959                     char *start = src;
960                     char *stop = E_(MacroSearch)( src, end, MVLC_END, VLC_FALSE );
961
962                     if( stop )
963                     {
964                         mvar_t *index;
965                         int    i_idx;
966                         mvar_t *v;
967                         if( !strcmp( m.param2, "integer" ) )
968                         {
969                             char *arg = E_(SSPop)( &p_args->stack );
970                             index = E_(mvar_IntegerSetNew)( m.param1, arg );
971                             free( arg );
972                         }
973                         else if( !strcmp( m.param2, "directory" ) )
974                         {
975                             char *arg = E_(SSPop)( &p_args->stack );
976                             index = E_(mvar_FileSetNew)( p_intf, m.param1, arg );
977                             free( arg );
978                         }
979                         else if( !strcmp( m.param2, "object" ) )
980                         {
981                             char *arg = E_(SSPop)( &p_args->stack );
982                             index = E_(mvar_ObjectSetNew)( p_intf, m.param1, arg );
983                             free( arg );
984                         }
985                         else if( !strcmp( m.param2, "playlist" ) )
986                         {
987                             index = E_(mvar_PlaylistSetNew)( p_intf, m.param1,
988                                                     p_intf->p_sys->p_playlist );
989                         }
990                         else if( !strcmp( m.param2, "information" ) )
991                         {
992                             index = E_(mvar_InfoSetNew)( p_intf, m.param1,
993                                                      p_intf->p_sys->p_input );
994                         }
995                         else if( !strcmp( m.param2, "program" )
996                                   || !strcmp( m.param2, "title" )
997                                   || !strcmp( m.param2, "chapter" )
998                                   || !strcmp( m.param2, "audio-es" )
999                                   || !strcmp( m.param2, "video-es" )
1000                                   || !strcmp( m.param2, "spu-es" ) )
1001                         {
1002                             index = E_(mvar_InputVarSetNew)( p_intf, m.param1,
1003                                                          p_intf->p_sys->p_input,
1004                                                          m.param2 );
1005                         }
1006                         else if( !strcmp( m.param2, "vlm" ) )
1007                         {
1008                             if( p_intf->p_sys->p_vlm == NULL )
1009                                 p_intf->p_sys->p_vlm = vlm_New( p_intf );
1010                             index = E_(mvar_VlmSetNew)( m.param1, p_intf->p_sys->p_vlm );
1011                         }
1012 #if 0
1013                         else if( !strcmp( m.param2, "hosts" ) )
1014                         {
1015                             index = E_(mvar_HttpdInfoSetNew)( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
1016                         }
1017                         else if( !strcmp( m.param2, "urls" ) )
1018                         {
1019                             index = E_(mvar_HttpdInfoSetNew)( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
1020                         }
1021                         else if( !strcmp( m.param2, "connections" ) )
1022                         {
1023                             index = E_(mvar_HttpdInfoSetNew)(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1024                         }
1025 #endif
1026                         else if( ( v = E_(mvar_GetVar)( p_args->vars, m.param2 ) ) )
1027                         {
1028                             index = E_(mvar_Duplicate)( v );
1029                         }
1030                         else
1031                         {
1032                             msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
1033                             src = endfor;
1034                             break;
1035                         }
1036
1037                         for( i_idx = 0; i_idx < index->i_field; i_idx++ )
1038                         {
1039                             mvar_t *f = E_(mvar_Duplicate)( index->field[i_idx] );
1040
1041                             //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
1042
1043                             free( f->name );
1044                             f->name = strdup( m.param1 );
1045
1046
1047                             E_(mvar_PushVar)( p_args->vars, f );
1048                             E_(Execute)( p_args, p_request, i_request,
1049                                      pp_data, pi_data, &dst, start, stop );
1050                             E_(mvar_RemoveVar)( p_args->vars, f );
1051
1052                             E_(mvar_Delete)( f );
1053                         }
1054                         E_(mvar_Delete)( index );
1055
1056                         src = endfor;
1057                     }
1058                     break;
1059                 }
1060                 default:
1061                     E_(MacroDo)( p_args, &m, p_request, i_request,
1062                                  pp_data, pi_data, &dst );
1063                     break;
1064             }
1065
1066             E_(MacroClean)( &m );
1067             continue;
1068         }
1069
1070         i_copy =   ( (p == NULL || p > end ) ? end : p  ) - src;
1071         if( i_copy > 0 )
1072         {
1073             int i_index = dst - *pp_data;
1074
1075             *pi_data += i_copy;
1076             *pp_data = realloc( *pp_data, *pi_data );
1077             dst = (*pp_data) + i_index;
1078
1079             memcpy( dst, src, i_copy );
1080             dst += i_copy;
1081             src += i_copy;
1082         }
1083     }
1084
1085     *pp_dst = dst;
1086     free( dup );
1087 }