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