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