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