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