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