]> git.sesse.net Git - vlc/blob - modules/control/http/macro.c
Don't include config.h from the headers - refs #297.
[vlc] / modules / control / http / macro.c
1 /*****************************************************************************
2  * macro.c : Custom <vlc> macro handling
3  *****************************************************************************
4  * Copyright (C) 2001-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25 #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             E_(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                     E_(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                                       VLC_TRUE, NULL,
204                                       playlist_ItemGetById( p_sys->p_playlist,
205                                       i_item, VLC_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                                       VLC_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                                       VLC_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                                       VLC_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                                       VLC_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                     E_(ExtractURIValue)( p_request, "seek_value", value, 30 );
248                     decode_URI( value );
249                     E_(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                     E_(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                     E_(ExtractURIValue)( p_request, "mrl", tmp, 1024 );
318                     decode_URI( tmp );
319                     E_(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 = E_(MRLParse)( p_intf, mrl, psz_name );
340
341                     char *psz_uri = input_item_GetURI( p_input );
342                     if( !p_input || !psz_uri || !*psz_uri )
343                     {
344                         msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
345                     }
346                     else
347                     {
348                         playlist_AddInput( p_sys->p_playlist, p_input,
349                                      PLAYLIST_APPEND, PLAYLIST_END, VLC_TRUE,
350                                      VLC_FALSE);
351                         vlc_gc_decref( p_input );
352                         msg_Dbg( p_intf, "requested mrl add: %s", mrl );
353                     }
354                     free( psz_uri );
355
356                     break;
357                 }
358                 case MVLC_DEL:
359                 {
360                     int i_item, *p_items = NULL, i_nb_items = 0;
361                     char item[512], *p_parser = p_request;
362
363                     /* Get the list of items to delete */
364                     while( (p_parser =
365                             E_(ExtractURIValue)( p_parser, "item", item, 512 )) )
366                     {
367                         if( !*item ) continue;
368
369                         i_item = atoi( item );
370                         p_items = realloc( p_items, (i_nb_items + 1) *
371                                            sizeof(int) );
372                         p_items[i_nb_items] = i_item;
373                         i_nb_items++;
374                     }
375
376                     if( i_nb_items )
377                     {
378                         int i;
379                         for( i = 0; i < i_nb_items; i++ )
380                         {
381                             playlist_DeleteFromInput( p_sys->p_playlist,
382                                                       p_items[i], VLC_FALSE );
383                             msg_Dbg( p_intf, "requested playlist delete: %d",
384                                      p_items[i] );
385                             p_items[i] = -1;
386                         }
387                     }
388
389                     if( p_items ) free( p_items );
390                     break;
391                 }
392                 case MVLC_KEEP:
393                 {
394                     int i_item, *p_items = NULL, i_nb_items = 0;
395                     char item[512], *p_parser = p_request;
396                     int i,j;
397
398                     /* Get the list of items to keep */
399                     while( (p_parser =
400                        E_(ExtractURIValue)( p_parser, "item", item, 512 )) )
401                     {
402                         if( !*item ) continue;
403
404                         i_item = atoi( item );
405                         p_items = realloc( p_items, (i_nb_items + 1) *
406                                            sizeof(int) );
407                         p_items[i_nb_items] = i_item;
408                         i_nb_items++;
409                     }
410
411                     for( i = p_sys->p_playlist->items.i_size - 1 ; i >= 0; i-- )
412                     {
413                         /* Check if the item is in the keep list */
414                         for( j = 0 ; j < i_nb_items ; j++ )
415                         {
416                             if( p_items[j] ==
417                                  ARRAY_VAL(p_sys->p_playlist->items,i)
418                                                 ->i_id)
419                                 break;
420                         }
421                         if( j == i_nb_items )
422                         {
423                             playlist_DeleteFromInput( p_sys->p_playlist,
424                                      p_sys->p_playlist->items.p_elems[i]->i_id,
425                                                       VLC_FALSE );
426                             msg_Dbg( p_intf, "requested playlist delete: %d",
427                                      i );
428                         }
429                     }
430
431                     if( p_items ) free( p_items );
432                     break;
433                 }
434                 case MVLC_EMPTY:
435                 {
436                     playlist_Clear( p_sys->p_playlist, VLC_FALSE );
437                     msg_Dbg( p_intf, "requested playlist empty" );
438                     break;
439                 }
440                 case MVLC_SORT:
441                 {
442                     char type[12];
443                     char order[2];
444                     char item[512];
445                     int i_order;
446                     int i_item;
447
448                     E_(ExtractURIValue)( p_request, "type", type, 12 );
449                     E_(ExtractURIValue)( p_request, "order", order, 2 );
450                     E_(ExtractURIValue)( p_request, "item", item, 512 );
451                     i_item = atoi( item );
452
453                     if( order[0] == '0' ) i_order = ORDER_NORMAL;
454                     else i_order = ORDER_REVERSE;
455
456                     if( !strcmp( type , "title" ) )
457                     {
458                         playlist_RecursiveNodeSort( p_sys->p_playlist,
459                                                     /* Ugly hack,but not worse than before ... */
460                                                     p_sys->p_playlist->p_root_onelevel,
461                                                     SORT_TITLE_NODES_FIRST,
462                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
463                         msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
464                     }
465                     else if( !strcmp( type , "author" ) )
466                     {
467                         playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
468                                                     p_sys->p_playlist->p_root_onelevel,
469                                                     SORT_ARTIST,
470                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
471                         msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
472                     }
473                     else if( !strcmp( type , "shuffle" ) )
474                     {
475                         playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
476                                                     p_sys->p_playlist->p_root_onelevel,
477                                                     SORT_RANDOM,
478                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
479                         msg_Dbg( p_intf, "requested playlist shuffle");
480                     }
481
482                     break;
483                 }
484                 case MVLC_MOVE:
485                 {
486                     char psz_pos[6];
487                     char psz_newpos[6];
488                     int i_pos;
489                     int i_newpos;
490                     E_(ExtractURIValue)( p_request, "psz_pos", psz_pos, 6 );
491                     E_(ExtractURIValue)( p_request, "psz_newpos", psz_newpos, 6 );
492                     i_pos = atoi( psz_pos );
493                     i_newpos = atoi( psz_newpos );
494                     /* FIXME FIXME TODO TODO XXX XXX
495                     ( duplicate from rpn.c )
496                     if ( i_pos < i_newpos )
497                     {
498                         playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
499                     }
500                     else
501                     {
502                         playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
503                     }
504                     msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
505                     FIXME FIXME TODO TODO XXX XXX */
506                     break;
507                 }
508
509                 /* admin function */
510                 case MVLC_CLOSE:
511                 {
512                     char id[512];
513                     E_(ExtractURIValue)( p_request, "id", id, 512 );
514                     msg_Dbg( p_intf, "requested close id=%s", id );
515 #if 0
516                     if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
517                     {
518                         msg_Warn( p_intf, "close failed for id=%s", id );
519                     }
520 #endif
521                     break;
522                 }
523                 case MVLC_SHUTDOWN:
524                 {
525                     msg_Dbg( p_intf, "requested shutdown" );
526                     vlc_object_kill( p_intf->p_libvlc );
527                     break;
528                 }
529                 /* vlm */
530                 case MVLC_VLM_NEW:
531                 case MVLC_VLM_SETUP:
532                 {
533                     static const char *vlm_properties[11] =
534                     {
535                         /* no args */
536                         "enabled", "disabled", "loop", "unloop",
537                         /* args required */
538                         "input", "output", "option", "date", "period", "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 ) break;
551
552                     E_(ExtractURIValue)( p_request, "name", name, 512 );
553                     if( StrToMacroType( control ) == MVLC_VLM_NEW )
554                     {
555                         char type[20];
556                         E_(ExtractURIValue)( p_request, "type", type, 20 );
557                         p += sprintf( psz, "new %s %s", name, type );
558                     }
559                     else
560                     {
561                         p += sprintf( psz, "setup %s", name );
562                     }
563                     /* Parse the request */
564                     for( i = 0; i < 11; i++ )
565                     {
566                         char val[512];
567                         E_(ExtractURIValue)( p_request,
568                                                vlm_properties[i], val, 512 );
569                         decode_URI( val );
570                         if( strlen( val ) > 0 && i >= 4 )
571                         {
572                             p += sprintf( p, " %s %s", vlm_properties[i], val );
573                         }
574                         else if( E_(TestURIParam)( p_request, vlm_properties[i] ) && i < 4 )
575                         {
576                             p += sprintf( p, " %s", vlm_properties[i] );
577                         }
578                     }
579                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
580                     if( vlm_answer->psz_value == NULL ) /* there is no error */
581                     {
582                         vlm_error = strdup( "" );
583                     }
584                     else
585                     {
586                         vlm_error = malloc( strlen(vlm_answer->psz_name) +
587                                             strlen(vlm_answer->psz_value) +
588                                             strlen( " : ") + 1 );
589                         sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
590                                                          vlm_answer->psz_value );
591                     }
592
593                     E_(mvar_AppendNewVar)( p_args->vars, "vlm_error", vlm_error );
594
595                     vlm_MessageDelete( vlm_answer );
596                     free( vlm_error );
597                     free( psz );
598                     break;
599                 }
600
601                 case MVLC_VLM_DEL:
602                 {
603                     vlm_message_t *vlm_answer;
604                     char name[512];
605                     char psz[512+10];
606                     if( p_intf->p_sys->p_vlm == NULL )
607                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
608
609                     if( p_intf->p_sys->p_vlm == NULL ) break;
610
611                     E_(ExtractURIValue)( p_request, "name", name, 512 );
612                     sprintf( psz, "del %s", name );
613
614                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
615                     /* FIXME do a vlm_answer -> var stack conversion */
616                     vlm_MessageDelete( vlm_answer );
617                     break;
618                 }
619
620                 case MVLC_VLM_PLAY:
621                 case MVLC_VLM_PAUSE:
622                 case MVLC_VLM_STOP:
623                 case MVLC_VLM_SEEK:
624                 {
625                     vlm_message_t *vlm_answer;
626                     char name[512];
627                     char psz[512+10];
628                     if( p_intf->p_sys->p_vlm == NULL )
629                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
630
631                     if( p_intf->p_sys->p_vlm == NULL ) break;
632
633                     E_(ExtractURIValue)( p_request, "name", name, 512 );
634                     if( StrToMacroType( control ) == MVLC_VLM_PLAY )
635                         sprintf( psz, "control %s play", name );
636                     else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
637                         sprintf( psz, "control %s pause", name );
638                     else if( StrToMacroType( control ) == MVLC_VLM_STOP )
639                         sprintf( psz, "control %s stop", name );
640                     else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
641                     {
642                         char percent[20];
643                         E_(ExtractURIValue)( p_request, "percent", percent, 512 );
644                         sprintf( psz, "control %s seek %s", name, percent );
645                     }
646
647                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
648                     /* FIXME do a vlm_answer -> var stack conversion */
649                     vlm_MessageDelete( vlm_answer );
650                     break;
651                 }
652                 case MVLC_VLM_LOAD:
653                 case MVLC_VLM_SAVE:
654                 {
655                     vlm_message_t *vlm_answer;
656                     char file[512];
657                     char psz[512];
658
659                     if( p_intf->p_sys->p_vlm == NULL )
660                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
661
662                     if( p_intf->p_sys->p_vlm == NULL ) break;
663
664                     E_(ExtractURIValue)( p_request, "file", file, 512 );
665                     decode_URI( file );
666
667                     if( StrToMacroType( control ) == MVLC_VLM_LOAD )
668                         sprintf( psz, "load %s", file );
669                     else
670                         sprintf( psz, "save %s", file );
671
672                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
673                     /* FIXME do a vlm_answer -> var stack conversion */
674                     vlm_MessageDelete( vlm_answer );
675                     break;
676                 }
677
678                 default:
679                     if( *control )
680                     {
681                         PRINTS( "<!-- control param(%s) unsupported -->", control );
682                     }
683                     break;
684             }
685             break;
686
687         case MVLC_SET:
688         {
689             char    value[512];
690             int     i;
691             float   f;
692
693             if( i_request <= 0 ||
694                 *m->param1  == '\0' ||
695                 strstr( p_request, m->param1 ) == NULL )
696             {
697                 break;
698             }
699             E_(ExtractURIValue)( p_request, m->param1,  value, 512 );
700             decode_URI( value );
701
702             switch( StrToMacroType( m->param2 ) )
703             {
704                 case MVLC_INT:
705                     i = atoi( value );
706                     config_PutInt( p_intf, m->param1, i );
707                     break;
708                 case MVLC_FLOAT:
709                     f = atof( value );
710                     config_PutFloat( p_intf, m->param1, f );
711                     break;
712                 case MVLC_STRING:
713                     config_PutPsz( p_intf, m->param1, value );
714                     break;
715                 default:
716                     PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
717             }
718             break;
719         }
720         case MVLC_GET:
721         {
722             char    value[512];
723             int     i;
724             float   f;
725             char    *psz;
726             lldiv_t div;
727
728             if( *m->param1  == '\0' )
729             {
730                 break;
731             }
732
733             switch( StrToMacroType( m->param2 ) )
734             {
735                 case MVLC_INT:
736                     i = config_GetInt( p_intf, m->param1 );
737                     sprintf( value, "%d", i );
738                     break;
739                 case MVLC_FLOAT:
740                     f = config_GetFloat( p_intf, m->param1 );
741                     div = lldiv( f * 1000000 , 1000000 );
742                     sprintf( value, "%lld.%06u", div.quot,
743                             (unsigned int)div.rem );
744                     break;
745                 case MVLC_STRING:
746                     psz = config_GetPsz( p_intf, m->param1 );
747                     if( psz != NULL )
748                     {
749                         strlcpy( value, psz,sizeof( value ) );
750                         free( psz );
751                     }
752                     else
753                         *value = '\0';
754                     msg_Dbg( p_intf, "%d: value = \"%s\"", __LINE__, value );
755                     break;
756                 default:
757                     snprintf( value, sizeof( value ),
758                               "invalid type(%s) in set", m->param2 );
759                     break;
760             }
761             PRINTS( "%s", value );
762             break;
763         }
764         case MVLC_VALUE:
765         {
766             char *s, *v;
767
768             if( m->param1 )
769             {
770                 E_(EvaluateRPN)( p_intf, p_args->vars, &p_args->stack, m->param1 );
771                 s = E_(SSPop)( &p_args->stack );
772                 v = E_(mvar_GetValue)( p_args->vars, s );
773             }
774             else
775             {
776                 v = s = E_(SSPop)( &p_args->stack );
777             }
778
779             PRINTS( "%s", v );
780             free( s );
781             break;
782         }
783         case MVLC_RPN:
784             E_(EvaluateRPN)( p_intf, p_args->vars, &p_args->stack, m->param1 );
785             break;
786
787         /* Useful to learn stack management */
788         case MVLC_STACK:
789         {
790             int i;
791             msg_Dbg( p_intf, "stack" );
792             for (i=0;i<(&p_args->stack)->i_stack;i++)
793                 msg_Dbg( p_intf, "%d -> %s", i, (&p_args->stack)->stack[i] );
794             break;
795         }
796
797         case MVLC_UNKNOWN:
798         default:
799             PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
800             msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
801             break;
802     }
803 #undef PRINTS
804 #undef PRINT
805 #undef ALLOC
806 }
807
808 static
809 char *MacroSearch( char *src, char *end, int i_mvlc, vlc_bool_t b_after )
810 {
811     int     i_id;
812     int     i_level = 0;
813
814     while( src < end )
815     {
816         if( src + 4 < end  && !strncmp( (char *)src, "<vlc", 4 ) )
817         {
818             int i_skip;
819             macro_t m;
820
821             i_skip = MacroParse( &m, src );
822
823             i_id = StrToMacroType( m.id );
824
825             switch( i_id )
826             {
827                 case MVLC_IF:
828                 case MVLC_FOREACH:
829                     i_level++;
830                     break;
831                 case MVLC_END:
832                     i_level--;
833                     break;
834                 default:
835                     break;
836             }
837
838             MacroClean( &m );
839
840             if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
841                 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
842             {
843                 return src + ( b_after ? i_skip : 0 );
844             }
845             else if( i_level < 0 )
846             {
847                 return NULL;
848             }
849
850             src += i_skip;
851         }
852         else
853         {
854             src++;
855         }
856     }
857
858     return NULL;
859 }
860
861 void E_(Execute)( httpd_file_sys_t *p_args,
862                      char *p_request, int i_request,
863                      char **pp_data, int *pi_data,
864                      char **pp_dst,
865                      char *_src, char *_end )
866 {
867     intf_thread_t  *p_intf = p_args->p_intf;
868
869     char *src, *dup, *end;
870     char *dst = *pp_dst;
871
872     src = dup = malloc( _end - _src + 1 );
873     end = src +( _end - _src );
874
875     memcpy( src, _src, _end - _src );
876     *end = '\0';
877
878     /* we parse searching <vlc */
879     while( src < end )
880     {
881         char *p;
882         int i_copy;
883
884         p = (char *)strstr( (char *)src, "<vlc" );
885         if( p < end && p == src )
886         {
887             macro_t m;
888
889             src += MacroParse( &m, src );
890
891             //msg_Dbg( p_intf, "macro_id=%s", m.id );
892
893             switch( StrToMacroType( m.id ) )
894             {
895                 case MVLC_INCLUDE:
896                 {
897                     FILE *f;
898                     int  i_buffer;
899                     char *p_buffer;
900                     char psz_file[MAX_DIR_SIZE];
901                     char *p;
902                     char sep;
903
904 #if defined( WIN32 )
905                     sep = '\\';
906 #else
907                     sep = '/';
908 #endif
909
910                     if( m.param1[0] != sep )
911                     {
912                         strcpy( psz_file, p_args->file );
913                         p = strrchr( psz_file, sep );
914                         if( p != NULL )
915                             strcpy( p + 1, m.param1 );
916                         else
917                             strcpy( psz_file, m.param1 );
918                     }
919                     else
920                     {
921                         strcpy( psz_file, m.param1 );
922                     }
923
924                     /* We hereby assume that psz_file is in the
925                      * local character encoding */
926                     if( ( f = fopen( psz_file, "r" ) ) == NULL )
927                     {
928                         msg_Warn( p_args->p_intf,
929                                   "unable to include file %s (%m)",
930                                   psz_file );
931                         break;
932                     }
933
934                     /* first we load in a temporary buffer */
935                     E_(FileLoad)( f, &p_buffer, &i_buffer );
936
937                     /* we parse executing all  <vlc /> macros */
938                     E_(Execute)( p_args, p_request, i_request, pp_data, pi_data,
939                              &dst, &p_buffer[0], &p_buffer[i_buffer] );
940                     free( p_buffer );
941                     fclose(f);
942                     break;
943                 }
944                 case MVLC_IF:
945                 {
946                     vlc_bool_t i_test;
947                     char    *endif;
948
949                     E_(EvaluateRPN)( p_intf, p_args->vars, &p_args->stack, m.param1 );
950                     if( E_(SSPopN)( &p_args->stack, p_args->vars ) )
951                     {
952                         i_test = 1;
953                     }
954                     else
955                     {
956                         i_test = 0;
957                     }
958                     endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
959
960                     if( i_test == 0 )
961                     {
962                         char *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
963
964                         if( start )
965                         {
966                             char *stop  = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
967                             if( stop )
968                             {
969                                 E_(Execute)( p_args, p_request, i_request,
970                                          pp_data, pi_data, &dst, start, stop );
971                             }
972                         }
973                     }
974                     else if( i_test == 1 )
975                     {
976                         char *stop;
977                         if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
978                         {
979                             stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
980                         }
981                         if( stop )
982                         {
983                             E_(Execute)( p_args, p_request, i_request,
984                                      pp_data, pi_data, &dst, src, stop );
985                         }
986                     }
987
988                     src = endif;
989                     break;
990                 }
991                 case MVLC_FOREACH:
992                 {
993                     char *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
994                     char *start = src;
995                     char *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
996
997                     if( stop )
998                     {
999                         mvar_t *index;
1000                         int    i_idx;
1001                         mvar_t *v;
1002                         if( !strcmp( m.param2, "integer" ) )
1003                         {
1004                             char *arg = E_(SSPop)( &p_args->stack );
1005                             index = E_(mvar_IntegerSetNew)( m.param1, arg );
1006                             free( arg );
1007                         }
1008                         else if( !strcmp( m.param2, "directory" ) )
1009                         {
1010                             char *arg = E_(SSPop)( &p_args->stack );
1011                             index = E_(mvar_FileSetNew)( p_intf, m.param1, arg );
1012                             free( arg );
1013                         }
1014                         else if( !strcmp( m.param2, "object" ) )
1015                         {
1016                             char *arg = E_(SSPop)( &p_args->stack );
1017                             index = E_(mvar_ObjectSetNew)( p_intf, m.param1, arg );
1018                             free( arg );
1019                         }
1020                         else if( !strcmp( m.param2, "playlist" ) )
1021                         {
1022                             index = E_(mvar_PlaylistSetNew)( p_intf, m.param1,
1023                                                     p_intf->p_sys->p_playlist );
1024                         }
1025                         else if( !strcmp( m.param2, "information" ) )
1026                         {
1027                             index = E_(mvar_InfoSetNew)( p_intf, m.param1,
1028                                                      p_intf->p_sys->p_input );
1029                         }
1030                         else if( !strcmp( m.param2, "program" )
1031                                   || !strcmp( m.param2, "title" )
1032                                   || !strcmp( m.param2, "chapter" )
1033                                   || !strcmp( m.param2, "audio-es" )
1034                                   || !strcmp( m.param2, "video-es" )
1035                                   || !strcmp( m.param2, "spu-es" ) )
1036                         {
1037                             index = E_(mvar_InputVarSetNew)( p_intf, m.param1,
1038                                                          p_intf->p_sys->p_input,
1039                                                          m.param2 );
1040                         }
1041                         else if( !strcmp( m.param2, "vlm" ) )
1042                         {
1043                             if( p_intf->p_sys->p_vlm == NULL )
1044                                 p_intf->p_sys->p_vlm = vlm_New( p_intf );
1045                             index = E_(mvar_VlmSetNew)( m.param1, p_intf->p_sys->p_vlm );
1046                         }
1047 #if 0
1048                         else if( !strcmp( m.param2, "hosts" ) )
1049                         {
1050                             index = E_(mvar_HttpdInfoSetNew)( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
1051                         }
1052                         else if( !strcmp( m.param2, "urls" ) )
1053                         {
1054                             index = E_(mvar_HttpdInfoSetNew)( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
1055                         }
1056                         else if( !strcmp( m.param2, "connections" ) )
1057                         {
1058                             index = E_(mvar_HttpdInfoSetNew)(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1059                         }
1060 #endif
1061                         else if( ( v = E_(mvar_GetVar)( p_args->vars, m.param2 ) ) )
1062                         {
1063                             index = E_(mvar_Duplicate)( v );
1064                         }
1065                         else
1066                         {
1067                             msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
1068                             src = endfor;
1069                             break;
1070                         }
1071
1072                         for( i_idx = 0; i_idx < index->i_field; i_idx++ )
1073                         {
1074                             mvar_t *f = E_(mvar_Duplicate)( index->field[i_idx] );
1075
1076                             //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
1077
1078                             free( f->name );
1079                             f->name = strdup( m.param1 );
1080
1081
1082                             E_(mvar_PushVar)( p_args->vars, f );
1083                             E_(Execute)( p_args, p_request, i_request,
1084                                      pp_data, pi_data, &dst, start, stop );
1085                             E_(mvar_RemoveVar)( p_args->vars, f );
1086
1087                             E_(mvar_Delete)( f );
1088                         }
1089                         E_(mvar_Delete)( index );
1090
1091                         src = endfor;
1092                     }
1093                     break;
1094                 }
1095                 default:
1096                     MacroDo( p_args, &m, p_request, i_request,
1097                              pp_data, pi_data, &dst );
1098                     break;
1099             }
1100
1101             MacroClean( &m );
1102             continue;
1103         }
1104
1105         i_copy =   ( (p == NULL || p > end ) ? end : p  ) - src;
1106         if( i_copy > 0 )
1107         {
1108             int i_index = dst - *pp_data;
1109
1110             *pi_data += i_copy;
1111             *pp_data = realloc( *pp_data, *pi_data );
1112             dst = (*pp_data) + i_index;
1113
1114             memcpy( dst, src, i_copy );
1115             dst += i_copy;
1116             src += i_copy;
1117         }
1118     }
1119
1120     *pp_dst = dst;
1121     free( dup );
1122 }