]> git.sesse.net Git - vlc/blob - modules/control/http.c
* modules/control/http.c : specify UTF-8 as charset (closes #236)
[vlc] / modules / control / http.c
1 /*****************************************************************************
2  * http.c :  http mini-server ;)
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  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 /* TODO:
29  * - clean up ?
30  * - doc ! (mouarf ;)
31  */
32
33 #include <stdlib.h>
34 #include <ctype.h>
35 #include <vlc/vlc.h>
36 #include <vlc/intf.h>
37
38 #include <vlc/aout.h>
39 #include <vlc/vout.h> /* for fullscreen */
40
41 #include "vlc_httpd.h"
42 #include "vlc_vlm.h"
43 #include "vlc_tls.h"
44 #include "vlc_acl.h"
45
46 #ifdef HAVE_SYS_STAT_H
47 #   include <sys/stat.h>
48 #endif
49 #ifdef HAVE_ERRNO_H
50 #   include <errno.h>
51 #endif
52 #ifdef HAVE_FCNTL_H
53 #   include <fcntl.h>
54 #endif
55
56 #ifdef HAVE_UNISTD_H
57 #   include <unistd.h>
58 #elif defined( WIN32 ) && !defined( UNDER_CE )
59 #   include <io.h>
60 #endif
61
62 #ifdef HAVE_DIRENT_H
63 #   include <dirent.h>
64 #endif
65
66 /* stat() support for large files on win32 */
67 #if defined( WIN32 ) && !defined( UNDER_CE )
68 #   define stat _stati64
69 #endif
70
71 /*****************************************************************************
72  * Module descriptor
73  *****************************************************************************/
74 static int  Open ( vlc_object_t * );
75 static void Close( vlc_object_t * );
76
77 #define HOST_TEXT N_( "Host address" )
78 #define HOST_LONGTEXT N_( \
79     "You can set the address and port the http interface will bind to." )
80 #define SRC_TEXT N_( "Source directory" )
81 #define SRC_LONGTEXT N_( "Source directory" )
82 #define CERT_TEXT N_( "Certificate file" )
83 #define CERT_LONGTEXT N_( "HTTP interface x509 PEM certificate file " \
84                           "(enables SSL)" )
85 #define KEY_TEXT N_( "Private key file" )
86 #define KEY_LONGTEXT N_( "HTTP interface x509 PEM private key file" )
87 #define CA_TEXT N_( "Root CA file" )
88 #define CA_LONGTEXT N_( "HTTP interface x509 PEM trusted root CA " \
89                         "certificates file" )
90 #define CRL_TEXT N_( "CRL file" )
91 #define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file" )
92
93 vlc_module_begin();
94     set_shortname( _("HTTP"));
95     set_description( _("HTTP remote control interface") );
96     set_category( CAT_INTERFACE );
97     set_subcategory( SUBCAT_INTERFACE_GENERAL );
98         add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
99         add_string ( "http-src",  NULL, NULL, SRC_TEXT,  SRC_LONGTEXT,  VLC_TRUE );
100         set_section( N_("HTTP SSL" ), 0 );
101         add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
102         add_string ( "http-intf-key",  NULL, NULL, KEY_TEXT,  KEY_LONGTEXT,  VLC_TRUE );
103         add_string ( "http-intf-ca",   NULL, NULL, CA_TEXT,   CA_LONGTEXT,   VLC_TRUE );
104         add_string ( "http-intf-crl",  NULL, NULL, CRL_TEXT,  CRL_LONGTEXT,  VLC_TRUE );
105     set_capability( "interface", 0 );
106     set_callbacks( Open, Close );
107 vlc_module_end();
108
109
110 /*****************************************************************************
111  * Local prototypes
112  *****************************************************************************/
113 static void Run          ( intf_thread_t *p_intf );
114
115 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
116                            char *psz_dir );
117
118 static int DirectoryCheck( char *psz_dir )
119 {
120     DIR           *p_dir;
121
122 #ifdef HAVE_SYS_STAT_H
123     struct stat   stat_info;
124
125     if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
126     {
127         return VLC_EGENERIC;
128     }
129 #endif
130
131     if( ( p_dir = opendir( psz_dir ) ) == NULL )
132     {
133         return VLC_EGENERIC;
134     }
135     closedir( p_dir );
136
137     return VLC_SUCCESS;
138 }
139
140 static int  HttpCallback( httpd_file_sys_t *p_args,
141                           httpd_file_t *,
142                           uint8_t *p_request,
143                           uint8_t **pp_data, int *pi_data );
144
145 static char *uri_extract_value( char *psz_uri, const char *psz_name,
146                                 char *psz_value, int i_value_max );
147 static int uri_test_param( char *psz_uri, const char *psz_name );
148
149 static void uri_decode_url_encoded( char *psz );
150
151 static char *Find_end_MRL( char *psz );
152 static playlist_item_t *parse_MRL( intf_thread_t * , char *psz );
153
154 /*****************************************************************************
155  *
156  *****************************************************************************/
157 typedef struct mvar_s
158 {
159     char *name;
160     char *value;
161
162     int           i_field;
163     struct mvar_s **field;
164 } mvar_t;
165
166 #define STACK_MAX 100
167 typedef struct
168 {
169     char *stack[STACK_MAX];
170     int  i_stack;
171 } rpn_stack_t;
172
173 struct httpd_file_sys_t
174 {
175     intf_thread_t    *p_intf;
176     httpd_file_t     *p_file;
177     httpd_redirect_t *p_redir;
178     httpd_redirect_t *p_redir2;
179
180     char          *file;
181     char          *name;
182
183     vlc_bool_t    b_html;
184
185     /* inited for each access */
186     rpn_stack_t   stack;
187     mvar_t        *vars;
188 };
189
190 struct intf_sys_t
191 {
192     httpd_host_t        *p_httpd_host;
193
194     int                 i_files;
195     httpd_file_sys_t    **pp_files;
196
197     playlist_t          *p_playlist;
198     input_thread_t      *p_input;
199     vlm_t               *p_vlm;
200 };
201
202
203
204 /*****************************************************************************
205  * Activate: initialize and create stuff
206  *****************************************************************************/
207 static int Open( vlc_object_t *p_this )
208 {
209     intf_thread_t *p_intf = (intf_thread_t*)p_this;
210     intf_sys_t    *p_sys;
211     char          *psz_host;
212     char          *psz_address = "";
213     const char    *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
214                   *psz_crl = NULL;
215     int           i_port       = 0;
216     char          *psz_src;
217
218     psz_host = config_GetPsz( p_intf, "http-host" );
219     if( psz_host )
220     {
221         char *psz_parser;
222         psz_address = psz_host;
223
224         psz_parser = strchr( psz_host, ':' );
225         if( psz_parser )
226         {
227             *psz_parser++ = '\0';
228             i_port = atoi( psz_parser );
229         }
230     }
231
232     p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
233     if( !p_intf->p_sys )
234     {
235         return( VLC_ENOMEM );
236     }
237     p_sys->p_playlist = NULL;
238     p_sys->p_input    = NULL;
239     p_sys->p_vlm      = NULL;
240
241     /* determine SSL configuration */
242     psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
243     if ( psz_cert != NULL )
244     {
245         msg_Dbg( p_intf, "enablind TLS for HTTP interface (cert file: %s)",
246                  psz_cert );
247         psz_key = config_GetPsz( p_intf, "http-intf-key" );
248         psz_ca = config_GetPsz( p_intf, "http-intf-ca" );
249         psz_crl = config_GetPsz( p_intf, "http-intf-crl" );
250
251         if( i_port <= 0 )
252             i_port = 8443;
253     }
254     else
255     {
256         if( i_port <= 0 )
257             i_port= 8080;
258     }
259
260     msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
261
262     p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
263                                             i_port, psz_cert, psz_key, psz_ca,
264                                             psz_crl );
265     if( p_sys->p_httpd_host == NULL )
266     {
267         msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
268         free( p_sys );
269         return VLC_EGENERIC;
270     }
271
272     if( psz_host )
273     {
274         free( psz_host );
275     }
276
277     p_sys->i_files  = 0;
278     p_sys->pp_files = NULL;
279
280 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || defined(WIN32)
281     if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
282     {
283         char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
284         psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
285         if( !psz_src ) return VLC_ENOMEM;
286 #if defined(WIN32)
287         sprintf( psz_src, "%s/http", psz_vlcpath);
288 #else
289         sprintf( psz_src, "%s/share/http", psz_vlcpath);
290 #endif
291     }
292 #else
293     psz_src = config_GetPsz( p_intf, "http-src" );
294
295     if( !psz_src || *psz_src == '\0' )
296     {
297         if( !DirectoryCheck( "share/http" ) )
298         {
299             psz_src = strdup( "share/http" );
300         }
301         else if( !DirectoryCheck( DATA_PATH "/http" ) )
302         {
303             psz_src = strdup( DATA_PATH "/http" );
304         }
305     }
306 #endif
307
308     if( !psz_src || *psz_src == '\0' )
309     {
310         msg_Err( p_intf, "invalid src dir" );
311         goto failed;
312     }
313
314     /* remove trainling \ or / */
315     if( psz_src[strlen( psz_src ) - 1] == '\\' ||
316         psz_src[strlen( psz_src ) - 1] == '/' )
317     {
318         psz_src[strlen( psz_src ) - 1] = '\0';
319     }
320
321     ParseDirectory( p_intf, psz_src, psz_src );
322
323
324     if( p_sys->i_files <= 0 )
325     {
326         msg_Err( p_intf, "cannot find any files (%s)", psz_src );
327         goto failed;
328     }
329     p_intf->pf_run = Run;
330     free( psz_src );
331
332     return VLC_SUCCESS;
333
334 failed:
335     if( psz_src ) free( psz_src );
336     if( p_sys->pp_files )
337     {
338         free( p_sys->pp_files );
339     }
340     httpd_HostDelete( p_sys->p_httpd_host );
341     free( p_sys );
342     return VLC_EGENERIC;
343 }
344
345 /*****************************************************************************
346  * CloseIntf: destroy interface
347  *****************************************************************************/
348 void Close ( vlc_object_t *p_this )
349 {
350     intf_thread_t *p_intf = (intf_thread_t *)p_this;
351     intf_sys_t    *p_sys = p_intf->p_sys;
352
353     int i;
354
355     if( p_sys->p_vlm )
356     {
357         vlm_Delete( p_sys->p_vlm );
358     }
359     for( i = 0; i < p_sys->i_files; i++ )
360     {
361        httpd_FileDelete( p_sys->pp_files[i]->p_file );
362        if( p_sys->pp_files[i]->p_redir )
363            httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
364        if( p_sys->pp_files[i]->p_redir2 )
365            httpd_RedirectDelete( p_sys->pp_files[i]->p_redir2 );
366
367        free( p_sys->pp_files[i]->file );
368        free( p_sys->pp_files[i]->name );
369        free( p_sys->pp_files[i] );
370     }
371     if( p_sys->pp_files )
372     {
373         free( p_sys->pp_files );
374     }
375     httpd_HostDelete( p_sys->p_httpd_host );
376
377     free( p_sys );
378 }
379
380 /*****************************************************************************
381  * Run: http interface thread
382  *****************************************************************************/
383 static void Run( intf_thread_t *p_intf )
384 {
385     intf_sys_t     *p_sys = p_intf->p_sys;
386
387     while( !p_intf->b_die )
388     {
389         /* get the playlist */
390         if( p_sys->p_playlist == NULL )
391         {
392             p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
393         }
394
395         /* Manage the input part */
396         if( p_sys->p_input == NULL )
397         {
398             if( p_sys->p_playlist )
399             {
400                 p_sys->p_input =
401                     vlc_object_find( p_sys->p_playlist,
402                                      VLC_OBJECT_INPUT,
403                                      FIND_CHILD );
404             }
405         }
406         else if( p_sys->p_input->b_dead )
407         {
408             vlc_object_release( p_sys->p_input );
409             p_sys->p_input = NULL;
410         }
411
412
413         /* Wait a bit */
414         msleep( INTF_IDLE_SLEEP );
415     }
416
417     if( p_sys->p_input )
418     {
419         vlc_object_release( p_sys->p_input );
420         p_sys->p_input = NULL;
421     }
422
423     if( p_sys->p_playlist )
424     {
425         vlc_object_release( p_sys->p_playlist );
426         p_sys->p_playlist = NULL;
427     }
428 }
429
430
431 /*****************************************************************************
432  * Local functions
433  *****************************************************************************/
434 #define MAX_DIR_SIZE 10240
435
436 /****************************************************************************
437  * FileToUrl: create a good name for an url from filename
438  ****************************************************************************/
439 static char *FileToUrl( char *name, vlc_bool_t *pb_index )
440 {
441     char *url, *p;
442
443     url = p = malloc( strlen( name ) + 1 );
444
445     *pb_index = VLC_FALSE;
446     if( !url || !p )
447     {
448         return NULL;
449     }
450
451 #ifdef WIN32
452     while( *name == '\\' || *name == '/' )
453 #else
454     while( *name == '/' )
455 #endif
456     {
457         name++;
458     }
459
460     *p++ = '/';
461     strcpy( p, name );
462
463 #ifdef WIN32
464     /* convert '\\' into '/' */
465     name = p;
466     while( *name )
467     {
468         if( *name == '\\' )
469         {
470             *p++ = '/';
471         }
472         name++;
473     }
474 #endif
475
476     /* index.* -> / */
477     if( ( p = strrchr( url, '/' ) ) != NULL )
478     {
479         if( !strncmp( p, "/index.", 7 ) )
480         {
481             p[1] = '\0';
482             *pb_index = VLC_TRUE;
483         }
484     }
485     return url;
486 }
487
488 /****************************************************************************
489  * ParseDirectory: parse recursively a directory, adding each file
490  ****************************************************************************/
491 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
492                            char *psz_dir )
493 {
494     static const char mimetype[] = "text/html; charset=UTF-8";
495     intf_sys_t     *p_sys = p_intf->p_sys;
496     char           dir[MAX_DIR_SIZE];
497 #ifdef HAVE_SYS_STAT_H
498     struct stat   stat_info;
499 #endif
500     DIR           *p_dir;
501     struct dirent *p_dir_content;
502     vlc_acl_t     *p_acl;
503     FILE          *file;
504
505     char          *user = NULL;
506     char          *password = NULL;
507
508     int           i_dirlen;
509
510 #ifdef HAVE_SYS_STAT_H
511     if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
512     {
513         return VLC_EGENERIC;
514     }
515 #endif
516
517     if( ( p_dir = opendir( psz_dir ) ) == NULL )
518     {
519         msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
520         return VLC_EGENERIC;
521     }
522
523     i_dirlen = strlen( psz_dir );
524     if( i_dirlen + 10 > MAX_DIR_SIZE )
525     {
526         msg_Warn( p_intf, "skipping too deep dir (%s)", psz_dir );
527         return 0;
528     }
529
530     msg_Dbg( p_intf, "dir=%s", psz_dir );
531
532     sprintf( dir, "%s/.access", psz_dir );
533     if( ( file = fopen( dir, "r" ) ) != NULL )
534     {
535         char line[1024];
536         int  i_size;
537
538         msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
539
540         i_size = fread( line, 1, 1023, file );
541         if( i_size > 0 )
542         {
543             char *p;
544             while( i_size > 0 && ( line[i_size-1] == '\n' ||
545                    line[i_size-1] == '\r' ) )
546             {
547                 i_size--;
548             }
549
550             line[i_size] = '\0';
551
552             p = strchr( line, ':' );
553             if( p )
554             {
555                 *p++ = '\0';
556                 user = strdup( line );
557                 password = strdup( p );
558             }
559         }
560         msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
561                  user, password, i_size );
562
563         fclose( file );
564     }
565
566     sprintf( dir, "%s/.hosts", psz_dir );
567     p_acl = ACL_Create( p_intf, VLC_FALSE );
568     if( ACL_LoadFile( p_acl, dir ) )
569     {
570         ACL_Destroy( p_acl );
571         p_acl = NULL;
572     }
573
574     for( ;; )
575     {
576         /* parse psz_src dir */
577         if( ( p_dir_content = readdir( p_dir ) ) == NULL )
578         {
579             break;
580         }
581
582         if( ( p_dir_content->d_name[0] == '.' )
583          || ( i_dirlen + strlen( p_dir_content->d_name ) > MAX_DIR_SIZE ) )
584             continue;
585         
586         sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
587         if( ParseDirectory( p_intf, psz_root, dir ) )
588         {
589             httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
590             vlc_bool_t b_index;
591
592             f->p_intf  = p_intf;
593             f->p_file = NULL;
594             f->p_redir = NULL;
595             f->p_redir2 = NULL;
596             f->file = strdup( dir );
597             f->name = FileToUrl( &dir[strlen( psz_root )], &b_index );
598             f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
599
600             if( !f->name )
601             {
602                 msg_Err( p_intf , "unable to parse directory" );
603                 closedir( p_dir );
604                 free( f );
605                 return( VLC_ENOMEM );
606             }
607             msg_Dbg( p_intf, "file=%s (url=%s)",
608                      f->file, f->name );
609
610             f->p_file = httpd_FileNew( p_sys->p_httpd_host,
611                                        f->name,
612                                        f->b_html ? mimetype : NULL,
613                                        user, password, p_acl,
614                                        HttpCallback, f );
615
616             if( f->p_file )
617             {
618                 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
619             }
620             /* for url that ends by / add
621              *  - a redirect from rep to rep/
622              *  - in case of index.* rep/index.html to rep/ */
623             if( f && f->name[strlen(f->name) - 1] == '/' )
624             {
625                 char *psz_redir = strdup( f->name );
626                 char *p;
627                 psz_redir[strlen( psz_redir ) - 1] = '\0';
628
629                 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
630                 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
631                 free( psz_redir );
632
633                 if( b_index && ( p = strstr( f->file, "index." ) ) )
634                 {
635                     asprintf( &psz_redir, "%s%s", f->name, p );
636
637                     msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
638                     f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
639                                                      f->name, psz_redir );
640
641                     free( psz_redir );
642                 }
643             }
644         }
645     }
646
647     if( user )
648     {
649         free( user );
650     }
651     if( password )
652     {
653         free( password );
654     }
655
656     ACL_Destroy( p_acl );
657     closedir( p_dir );
658
659     return VLC_SUCCESS;
660 }
661
662 /****************************************************************************
663  * var and set handling
664  ****************************************************************************/
665
666 static mvar_t *mvar_New( char *name, char *value )
667 {
668     mvar_t *v = malloc( sizeof( mvar_t ) );
669
670     if( !v ) return NULL;
671     v->name = strdup( name );
672     v->value = strdup( value ? value : "" );
673
674     v->i_field = 0;
675     v->field = malloc( sizeof( mvar_t * ) );
676     v->field[0] = NULL;
677
678     return v;
679 }
680
681 static void mvar_Delete( mvar_t *v )
682 {
683     int i;
684
685     free( v->name );
686     free( v->value );
687
688     for( i = 0; i < v->i_field; i++ )
689     {
690         mvar_Delete( v->field[i] );
691     }
692     free( v->field );
693     free( v );
694 }
695
696 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
697 {
698     v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
699     v->field[v->i_field] = f;
700     v->i_field++;
701 }
702
703 static mvar_t *mvar_Duplicate( mvar_t *v )
704 {
705     int i;
706     mvar_t *n;
707
708     n = mvar_New( v->name, v->value );
709     for( i = 0; i < v->i_field; i++ )
710     {
711         mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
712     }
713
714     return n;
715 }
716
717 static void mvar_PushVar( mvar_t *v, mvar_t *f )
718 {
719     v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
720     if( v->i_field > 0 )
721     {
722         memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
723     }
724     v->field[0] = f;
725     v->i_field++;
726 }
727
728 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
729 {
730     int i;
731     for( i = 0; i < v->i_field; i++ )
732     {
733         if( v->field[i] == f )
734         {
735             break;
736         }
737     }
738     if( i >= v->i_field )
739     {
740         return;
741     }
742
743     if( i + 1 < v->i_field )
744     {
745         memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
746     }
747     v->i_field--;
748     /* FIXME should do a realloc */
749 }
750
751 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
752 {
753     int i;
754     char base[512], *field, *p;
755     int  i_index;
756
757     /* format: name[index].field */
758
759     field = strchr( name, '.' );
760     if( field )
761     {
762         int i = field - name;
763         strncpy( base, name, i );
764         base[i] = '\0';
765         field++;
766     }
767     else
768     {
769         strcpy( base, name );
770     }
771
772     if( ( p = strchr( base, '[' ) ) )
773     {
774         *p++ = '\0';
775         sscanf( p, "%d]", &i_index );
776         if( i_index < 0 )
777         {
778             return NULL;
779         }
780     }
781     else
782     {
783         i_index = 0;
784     }
785
786     for( i = 0; i < s->i_field; i++ )
787     {
788         if( !strcmp( s->field[i]->name, base ) )
789         {
790             if( i_index > 0 )
791             {
792                 i_index--;
793             }
794             else
795             {
796                 if( field )
797                 {
798                     return mvar_GetVar( s->field[i], field );
799                 }
800                 else
801                 {
802                     return s->field[i];
803                 }
804             }
805         }
806     }
807     return NULL;
808 }
809
810
811
812 static char *mvar_GetValue( mvar_t *v, char *field )
813 {
814     if( *field == '\0' )
815     {
816         return v->value;
817     }
818     else
819     {
820         mvar_t *f = mvar_GetVar( v, field );
821         if( f )
822         {
823             return f->value;
824         }
825         else
826         {
827             return field;
828         }
829     }
830 }
831
832 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
833 {
834     mvar_t *f = mvar_New( name, value );
835     mvar_PushVar( vars, f );
836 }
837
838 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
839 {
840     mvar_t *f = mvar_New( name, value );
841     mvar_AppendVar( vars, f );
842 }
843
844
845 /* arg= start[:stop[:step]],.. */
846 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
847 {
848     char *dup = strdup( arg );
849     char *str = dup;
850     mvar_t *s = mvar_New( name, "set" );
851
852
853     while( str )
854     {
855         char *p;
856         int  i_start,i_stop,i_step;
857         int  i_match;
858
859         p = strchr( str, ',' );
860         if( p )
861         {
862             *p++ = '\0';
863         }
864
865         i_step = 0;
866         i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
867
868         if( i_match == 1 )
869         {
870             i_stop = i_start;
871             i_step = 1;
872         }
873         else if( i_match == 2 )
874         {
875             i_step = i_start < i_stop ? 1 : -1;
876         }
877
878         if( i_match >= 1 )
879         {
880             int i;
881
882             if( ( i_start <= i_stop && i_step > 0 ) ||
883                 ( i_start >= i_stop && i_step < 0 ) )
884             {
885                 for( i = i_start; ; i += i_step )
886                 {
887                     char   value[79];
888
889                     if( ( i_step > 0 && i > i_stop ) ||
890                         ( i_step < 0 && i < i_stop ) )
891                     {
892                         break;
893                     }
894
895                     sprintf( value, "%d", i );
896
897                     mvar_PushNewVar( s, name, value );
898                 }
899             }
900         }
901         str = p;
902     }
903
904     free( dup );
905     return s;
906 }
907
908 void PlaylistListNode( playlist_t *p_pl, playlist_item_t *p_node,
909                        char *name, mvar_t *s, int i_depth )
910 {
911     if( p_node != NULL )
912     {
913         if (p_node->i_children == -1)
914         {
915             char value[512];
916             mvar_t *itm = mvar_New( name, "set" );
917
918             sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
919             mvar_AppendNewVar( itm, "current", value );
920
921             sprintf( value, "%d", p_node->input.i_id );
922             mvar_AppendNewVar( itm, "index", value );
923
924             mvar_AppendNewVar( itm, "name", p_node->input.psz_name );
925
926             mvar_AppendNewVar( itm, "uri", p_node->input.psz_uri );
927
928             sprintf( value, "Item");
929             mvar_AppendNewVar( itm, "type", value );
930
931             sprintf( value, "%d", i_depth );
932             mvar_AppendNewVar( itm, "depth", value );
933
934             mvar_AppendVar( s, itm );
935         }
936         else
937         {
938             char value[512];
939             int i_child;
940             mvar_t *itm = mvar_New( name, "set" );
941
942             mvar_AppendNewVar( itm, "name", p_node->input.psz_name );
943             mvar_AppendNewVar( itm, "uri", p_node->input.psz_name );
944
945             sprintf( value, "Node" );
946             mvar_AppendNewVar( itm, "type", value );
947
948             sprintf( value, "%d", p_node->input.i_id );
949             mvar_AppendNewVar( itm, "index", value );
950
951             sprintf( value, "%d", p_node->i_children);
952             mvar_AppendNewVar( itm, "i_children", value );
953
954             sprintf( value, "%d", i_depth );
955             mvar_AppendNewVar( itm, "depth", value );
956
957             mvar_AppendVar( s, itm );
958
959             for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
960                 PlaylistListNode( p_pl, p_node->pp_children[i_child], name, s, i_depth + 1);
961
962         }
963     }
964 }
965
966 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
967 {
968     playlist_view_t *p_view;
969     mvar_t *s = mvar_New( name, "set" );
970
971
972     vlc_mutex_lock( &p_pl->object_lock );
973
974     p_view = playlist_ViewFind( p_pl, VIEW_CATEGORY ); /* FIXME */
975
976     if( p_view != NULL )
977         PlaylistListNode( p_pl, p_view->p_root, name, s, 0 );
978
979     vlc_mutex_unlock( &p_pl->object_lock );
980
981     return s;
982 }
983
984 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
985 {
986     mvar_t *s = mvar_New( name, "set" );
987     int i, j;
988
989     if( p_input == NULL )
990     {
991         return s;
992     }
993
994     vlc_mutex_lock( &p_input->input.p_item->lock );
995     for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
996     {
997         info_category_t *p_category = p_input->input.p_item->pp_categories[i];
998         mvar_t *cat  = mvar_New( name, "set" );
999         mvar_t *iset = mvar_New( "info", "set" );
1000
1001         mvar_AppendNewVar( cat, "name", p_category->psz_name );
1002         mvar_AppendVar( cat, iset );
1003
1004         for ( j = 0; j < p_category->i_infos; j++ )
1005         {
1006             info_t *p_info = p_category->pp_infos[j];
1007             mvar_t *info = mvar_New( "info", "" );
1008
1009             msg_Dbg( p_input, "adding info name=%s value=%s",
1010                      p_info->psz_name, p_info->psz_value );
1011             mvar_AppendNewVar( info, "name",  p_info->psz_name );
1012             mvar_AppendNewVar( info, "value", p_info->psz_value );
1013             mvar_AppendVar( iset, info );
1014         }
1015         mvar_AppendVar( s, cat );
1016     }
1017     vlc_mutex_unlock( &p_input->input.p_item->lock );
1018
1019     return s;
1020 }
1021 #if 0
1022 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
1023 {
1024     mvar_t       *s = mvar_New( name, "set" );
1025     httpd_info_t info;
1026     int          i;
1027
1028     if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
1029     {
1030         for( i= 0; i < info.i_count; )
1031         {
1032             mvar_t *inf;
1033
1034             inf = mvar_New( name, "set" );
1035             do
1036             {
1037                 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
1038                             info.info[i].psz_name, info.info[i].psz_value ); */
1039                 mvar_AppendNewVar( inf,
1040                                    info.info[i].psz_name,
1041                                    info.info[i].psz_value );
1042                 i++;
1043             } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
1044             mvar_AppendVar( s, inf );
1045         }
1046     }
1047
1048     /* free mem */
1049     for( i = 0; i < info.i_count; i++ )
1050     {
1051         free( info.info[i].psz_name );
1052         free( info.info[i].psz_value );
1053     }
1054     if( info.i_count > 0 )
1055     {
1056         free( info.info );
1057     }
1058
1059     return s;
1060 }
1061 #endif
1062
1063 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1064 {
1065     mvar_t *s = mvar_New( name, "set" );
1066     char           tmp[MAX_DIR_SIZE], *p, *src;
1067 #ifdef HAVE_SYS_STAT_H
1068     struct stat   stat_info;
1069 #endif
1070     DIR           *p_dir;
1071     struct dirent *p_dir_content;
1072     char          sep;
1073
1074     /* convert all / to native separator */
1075 #if defined( WIN32 )
1076     while( (p = strchr( psz_dir, '/' )) )
1077     {
1078         *p = '\\';
1079     }
1080     sep = '\\';
1081 #else
1082     sep = '/';
1083 #endif
1084
1085     /* remove trailling separator */
1086     while( strlen( psz_dir ) > 1 &&
1087 #if defined( WIN32 )
1088            !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1089 #endif
1090            psz_dir[strlen( psz_dir ) -1 ] == sep )
1091     {
1092         psz_dir[strlen( psz_dir ) -1 ]  ='\0';
1093     }
1094     /* remove double separator */
1095     for( p = src = psz_dir; *src != '\0'; src++, p++ )
1096     {
1097         if( src[0] == sep && src[1] == sep )
1098         {
1099             src++;
1100         }
1101         *p = *src;
1102     }
1103     *p = '\0';
1104
1105     if( *psz_dir == '\0' )
1106     {
1107         return s;
1108     }
1109     /* first fix all .. dir */
1110     p = src = psz_dir;
1111     while( *src )
1112     {
1113         if( src[0] == '.' && src[1] == '.' )
1114         {
1115             src += 2;
1116             if( p <= &psz_dir[1] )
1117             {
1118                 continue;
1119             }
1120
1121             p -= 2;
1122
1123             while( p > &psz_dir[1] && *p != sep )
1124             {
1125                 p--;
1126             }
1127         }
1128         else if( *src == sep )
1129         {
1130             if( p > psz_dir && p[-1] == sep )
1131             {
1132                 src++;
1133             }
1134             else
1135             {
1136                 *p++ = *src++;
1137             }
1138         }
1139         else
1140         {
1141             do
1142             {
1143                 *p++ = *src++;
1144             } while( *src && *src != sep );
1145         }
1146     }
1147     *p = '\0';
1148
1149
1150 #ifdef HAVE_SYS_STAT_H
1151     if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1152     {
1153         return s;
1154     }
1155 #endif
1156
1157     if( ( p_dir = opendir( psz_dir ) ) == NULL )
1158     {
1159         fprintf( stderr, "cannot open dir (%s)", psz_dir );
1160         return s;
1161     }
1162
1163     /* remove traling / or \ */
1164     for( p = &psz_dir[strlen( psz_dir) - 1];
1165          p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1166     {
1167         *p = '\0';
1168     }
1169
1170     for( ;; )
1171     {
1172         mvar_t *f;
1173         const char *psz_ext;
1174
1175         /* parse psz_src dir */
1176         if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1177         {
1178             break;
1179         }
1180         if( !strcmp( p_dir_content->d_name, "." ) )
1181         {
1182             continue;
1183         }
1184
1185         sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1186
1187 #ifdef HAVE_SYS_STAT_H
1188         if( stat( tmp, &stat_info ) == -1 )
1189         {
1190             continue;
1191         }
1192 #endif
1193         f = mvar_New( name, "set" );
1194         mvar_AppendNewVar( f, "name", tmp );
1195         mvar_AppendNewVar( f, "basename", p_dir_content->d_name );
1196
1197         /* put file extension in 'ext' */
1198         psz_ext = strrchr( p_dir_content->d_name, '.' );
1199         mvar_AppendNewVar( f, "ext", psz_ext != NULL ? psz_ext + 1 : "" );
1200
1201 #ifdef HAVE_SYS_STAT_H
1202         if( S_ISDIR( stat_info.st_mode ) )
1203         {
1204             mvar_AppendNewVar( f, "type", "directory" );
1205         }
1206         else if( S_ISREG( stat_info.st_mode ) )
1207         {
1208             mvar_AppendNewVar( f, "type", "file" );
1209         }
1210         else
1211         {
1212             mvar_AppendNewVar( f, "type", "unknown" );
1213         }
1214
1215         sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
1216         mvar_AppendNewVar( f, "size", tmp );
1217
1218         /* FIXME memory leak FIXME */
1219 #ifdef HAVE_CTIME_R
1220         ctime_r( &stat_info.st_mtime, tmp );
1221         mvar_AppendNewVar( f, "date", tmp );
1222 #else
1223         mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1224 #endif
1225
1226 #else
1227         mvar_AppendNewVar( f, "type", "unknown" );
1228         mvar_AppendNewVar( f, "size", "unknown" );
1229         mvar_AppendNewVar( f, "date", "unknown" );
1230 #endif
1231         mvar_AppendVar( s, f );
1232     }
1233
1234     return s;
1235 }
1236
1237 static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
1238 {
1239     mvar_t        *s = mvar_New( name, "set" );
1240     vlm_message_t *msg;
1241     int    i;
1242
1243     if( vlm == NULL ) return s;
1244
1245     if( vlm_ExecuteCommand( vlm, "show", &msg ) )
1246     {
1247         return s;
1248     }
1249
1250     for( i = 0; i < msg->i_child; i++ )
1251     {
1252         /* Over media, schedule */
1253         vlm_message_t *ch = msg->child[i];
1254         int j;
1255
1256         for( j = 0; j < ch->i_child; j++ )
1257         {
1258             /* Over name */
1259             vlm_message_t *el = ch->child[j];
1260             vlm_message_t *inf, *desc;
1261             mvar_t        *set;
1262             char          psz[500];
1263             int k;
1264
1265             sprintf( psz, "show %s", el->psz_name );
1266             if( vlm_ExecuteCommand( vlm, psz, &inf ) )
1267                 continue;
1268             desc = inf->child[0];
1269
1270             /* Add a node with name and info */
1271             set = mvar_New( name, "set" );
1272             mvar_AppendNewVar( set, "name", el->psz_name );
1273
1274             for( k = 0; k < desc->i_child; k++ )
1275             {
1276                 vlm_message_t *ch = desc->child[k];
1277                 if( ch->i_child > 0 )
1278                 {
1279                     int c;
1280                     mvar_t *n = mvar_New( ch->psz_name, "set" );
1281
1282                     for( c = 0; c < ch->i_child; c++ )
1283                     {
1284                         if( ch->child[c]->psz_value )
1285                         {
1286                             mvar_AppendNewVar( n, ch->child[c]->psz_name, ch->child[c]->psz_value );
1287                         }
1288                         else
1289                         {
1290                             mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
1291                             mvar_AppendVar( n, in );
1292                         }
1293                     }
1294                     mvar_AppendVar( set, n );
1295                 }
1296                 else
1297                 {
1298                     mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
1299                 }
1300             }
1301             vlm_MessageDelete( inf );
1302
1303             mvar_AppendVar( s, set );
1304         }
1305     }
1306     vlm_MessageDelete( msg );
1307
1308     return s;
1309 }
1310
1311
1312 static void SSInit( rpn_stack_t * );
1313 static void SSClean( rpn_stack_t * );
1314 static void EvaluateRPN( mvar_t  *, rpn_stack_t *, char * );
1315
1316 static void SSPush  ( rpn_stack_t *, char * );
1317 static char *SSPop  ( rpn_stack_t * );
1318
1319 static void SSPushN ( rpn_stack_t *, int );
1320 static int  SSPopN  ( rpn_stack_t *, mvar_t  * );
1321
1322
1323 /****************************************************************************
1324  * Macro handling
1325  ****************************************************************************/
1326 typedef struct
1327 {
1328     char *id;
1329     char *param1;
1330     char *param2;
1331 } macro_t;
1332
1333 static int FileLoad( FILE *f, char **pp_data, int *pi_data )
1334 {
1335     int i_read;
1336
1337     /* just load the file */
1338     *pi_data = 0;
1339     *pp_data = malloc( 1025 );  /* +1 for \0 */
1340     while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1341     {
1342         *pi_data += 1024;
1343         *pp_data = realloc( *pp_data, *pi_data  + 1025 );
1344     }
1345     if( i_read > 0 )
1346     {
1347         *pi_data += i_read;
1348     }
1349     (*pp_data)[*pi_data] = '\0';
1350
1351     return VLC_SUCCESS;
1352 }
1353
1354 static int MacroParse( macro_t *m, char *psz_src )
1355 {
1356     char *dup = strdup( (char *)psz_src );
1357     char *src = dup;
1358     char *p;
1359     int     i_skip;
1360
1361 #define EXTRACT( name, l ) \
1362         src += l;    \
1363         p = strchr( src, '"' );             \
1364         if( p )                             \
1365         {                                   \
1366             *p++ = '\0';                    \
1367         }                                   \
1368         m->name = strdup( src );            \
1369         if( !p )                            \
1370         {                                   \
1371             break;                          \
1372         }                                   \
1373         src = p;
1374
1375     /* init m */
1376     m->id = NULL;
1377     m->param1 = NULL;
1378     m->param2 = NULL;
1379
1380     /* parse */
1381     src += 4;
1382
1383     while( *src )
1384     {
1385         while( *src == ' ')
1386         {
1387             src++;
1388         }
1389         if( !strncmp( src, "id=\"", 4 ) )
1390         {
1391             EXTRACT( id, 4 );
1392         }
1393         else if( !strncmp( src, "param1=\"", 8 ) )
1394         {
1395             EXTRACT( param1, 8 );
1396         }
1397         else if( !strncmp( src, "param2=\"", 8 ) )
1398         {
1399             EXTRACT( param2, 8 );
1400         }
1401         else
1402         {
1403             break;
1404         }
1405     }
1406     if( strstr( src, "/>" ) )
1407     {
1408         src = strstr( src, "/>" ) + 2;
1409     }
1410     else
1411     {
1412         src += strlen( src );
1413     }
1414
1415     if( m->id == NULL )
1416     {
1417         m->id = strdup( "" );
1418     }
1419     if( m->param1 == NULL )
1420     {
1421         m->param1 = strdup( "" );
1422     }
1423     if( m->param2 == NULL )
1424     {
1425         m->param2 = strdup( "" );
1426     }
1427     i_skip = src - dup;
1428
1429     free( dup );
1430     return i_skip;
1431 #undef EXTRACT
1432 }
1433
1434 static void MacroClean( macro_t *m )
1435 {
1436     free( m->id );
1437     free( m->param1 );
1438     free( m->param2 );
1439 }
1440
1441 enum macroType
1442 {
1443     MVLC_UNKNOWN = 0,
1444     MVLC_CONTROL,
1445         MVLC_PLAY,
1446         MVLC_STOP,
1447         MVLC_PAUSE,
1448         MVLC_NEXT,
1449         MVLC_PREVIOUS,
1450         MVLC_ADD,
1451         MVLC_DEL,
1452         MVLC_EMPTY,
1453         MVLC_SEEK,
1454         MVLC_KEEP,
1455         MVLC_SORT,
1456         MVLC_MOVE,
1457         MVLC_VOLUME,
1458         MVLC_FULLSCREEN,
1459
1460         MVLC_CLOSE,
1461         MVLC_SHUTDOWN,
1462
1463         MVLC_VLM_NEW,
1464         MVLC_VLM_SETUP,
1465         MVLC_VLM_DEL,
1466         MVLC_VLM_PLAY,
1467         MVLC_VLM_PAUSE,
1468         MVLC_VLM_STOP,
1469         MVLC_VLM_SEEK,
1470         MVLC_VLM_LOAD,
1471         MVLC_VLM_SAVE,
1472
1473     MVLC_FOREACH,
1474     MVLC_IF,
1475     MVLC_RPN,
1476     MVLC_STACK,
1477     MVLC_ELSE,
1478     MVLC_END,
1479     MVLC_GET,
1480     MVLC_SET,
1481         MVLC_INT,
1482         MVLC_FLOAT,
1483         MVLC_STRING,
1484
1485     MVLC_VALUE
1486 };
1487
1488 static struct
1489 {
1490     char *psz_name;
1491     int  i_type;
1492 }
1493 StrToMacroTypeTab [] =
1494 {
1495     { "control",    MVLC_CONTROL },
1496         /* player control */
1497         { "play",           MVLC_PLAY },
1498         { "stop",           MVLC_STOP },
1499         { "pause",          MVLC_PAUSE },
1500         { "next",           MVLC_NEXT },
1501         { "previous",       MVLC_PREVIOUS },
1502         { "seek",           MVLC_SEEK },
1503         { "keep",           MVLC_KEEP },
1504         { "fullscreen",     MVLC_FULLSCREEN },
1505         { "volume",         MVLC_VOLUME },
1506
1507         /* playlist management */
1508         { "add",            MVLC_ADD },
1509         { "delete",         MVLC_DEL },
1510         { "empty",          MVLC_EMPTY },
1511         { "sort",           MVLC_SORT },
1512         { "move",           MVLC_MOVE },
1513
1514         /* admin control */
1515         { "close",          MVLC_CLOSE },
1516         { "shutdown",       MVLC_SHUTDOWN },
1517
1518         /* vlm control */
1519         { "vlm_new",        MVLC_VLM_NEW },
1520         { "vlm_setup",      MVLC_VLM_SETUP },
1521         { "vlm_del",        MVLC_VLM_DEL },
1522         { "vlm_play",       MVLC_VLM_PLAY },
1523         { "vlm_pause",      MVLC_VLM_PAUSE },
1524         { "vlm_stop",       MVLC_VLM_STOP },
1525         { "vlm_seek",       MVLC_VLM_SEEK },
1526         { "vlm_load",       MVLC_VLM_LOAD },
1527         { "vlm_save",       MVLC_VLM_SAVE },
1528
1529     { "rpn",        MVLC_RPN },
1530     { "stack",        MVLC_STACK },
1531
1532     { "foreach",    MVLC_FOREACH },
1533     { "value",      MVLC_VALUE },
1534
1535     { "if",         MVLC_IF },
1536     { "else",       MVLC_ELSE },
1537     { "end",        MVLC_END },
1538     { "get",         MVLC_GET },
1539     { "set",         MVLC_SET },
1540         { "int",            MVLC_INT },
1541         { "float",          MVLC_FLOAT },
1542         { "string",         MVLC_STRING },
1543
1544     /* end */
1545     { NULL,         MVLC_UNKNOWN }
1546 };
1547
1548 static int StrToMacroType( char *name )
1549 {
1550     int i;
1551
1552     if( !name || *name == '\0')
1553     {
1554         return MVLC_UNKNOWN;
1555     }
1556     for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1557     {
1558         if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1559         {
1560             return StrToMacroTypeTab[i].i_type;
1561         }
1562     }
1563     return MVLC_UNKNOWN;
1564 }
1565
1566 static void MacroDo( httpd_file_sys_t *p_args,
1567                      macro_t *m,
1568                      char *p_request, int i_request,
1569                      char **pp_data,  int *pi_data,
1570                      char **pp_dst )
1571 {
1572     intf_thread_t  *p_intf = p_args->p_intf;
1573     intf_sys_t     *p_sys = p_args->p_intf->p_sys;
1574     char control[512];
1575
1576 #define ALLOC( l ) \
1577     {               \
1578         int __i__ = *pp_dst - *pp_data; \
1579         *pi_data += (l);                  \
1580         *pp_data = realloc( *pp_data, *pi_data );   \
1581         *pp_dst = (*pp_data) + __i__;   \
1582     }
1583 #define PRINT( str ) \
1584     ALLOC( strlen( str ) + 1 ); \
1585     *pp_dst += sprintf( *pp_dst, str );
1586
1587 #define PRINTS( str, s ) \
1588     ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1589     { \
1590         char * psz_cur = *pp_dst; \
1591         *pp_dst += sprintf( *pp_dst, str, s ); \
1592         while( psz_cur && *psz_cur ) \
1593         {  \
1594             /* Prevent script injection */ \
1595             if( *psz_cur == '<' ) *psz_cur = '*'; \
1596             if( *psz_cur == '>' ) *psz_cur = '*'; \
1597             psz_cur++ ; \
1598         } \
1599     }
1600
1601     switch( StrToMacroType( m->id ) )
1602     {
1603         case MVLC_CONTROL:
1604             if( i_request <= 0 )
1605             {
1606                 break;
1607             }
1608             uri_extract_value( p_request, "control", control, 512 );
1609             if( *m->param1 && !strstr( m->param1, control ) )
1610             {
1611                 msg_Warn( p_intf, "unauthorized control=%s", control );
1612                 break;
1613             }
1614             switch( StrToMacroType( control ) )
1615             {
1616                 case MVLC_PLAY:
1617                 {
1618                     int i_item;
1619                     char item[512];
1620
1621                     uri_extract_value( p_request, "item", item, 512 );
1622                     i_item = atoi( item );
1623                     playlist_Control( p_sys->p_playlist, PLAYLIST_ITEMPLAY,
1624                                       playlist_ItemGetById( p_sys->p_playlist,
1625                                       i_item ) );
1626                     msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1627                     break;
1628                 }
1629                 case MVLC_STOP:
1630                     playlist_Control( p_sys->p_playlist, PLAYLIST_STOP);
1631                     msg_Dbg( p_intf, "requested playlist stop" );
1632                     break;
1633                 case MVLC_PAUSE:
1634                     playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
1635                     msg_Dbg( p_intf, "requested playlist pause" );
1636                     break;
1637                 case MVLC_NEXT:
1638                     playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
1639                     msg_Dbg( p_intf, "requested playlist next" );
1640                     break;
1641                 case MVLC_PREVIOUS:
1642                     playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1);
1643                     msg_Dbg( p_intf, "requested playlist next" );
1644                     break;
1645                 case MVLC_FULLSCREEN:
1646                 {
1647                     if( p_sys->p_input )
1648                     {
1649                         vout_thread_t *p_vout;
1650                         p_vout = vlc_object_find( p_sys->p_input,
1651                                                   VLC_OBJECT_VOUT, FIND_CHILD );
1652
1653                         if( p_vout )
1654                         {
1655                             p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1656                             vlc_object_release( p_vout );
1657                             msg_Dbg( p_intf, "requested fullscreen toggle" );
1658                         }
1659                     }
1660                 }
1661                 break;
1662                 case MVLC_SEEK:
1663                 {
1664                     vlc_value_t val;
1665                     char value[30];
1666                     char * p_value;
1667                     int i_stock = 0;
1668                     uint64_t i_length;
1669                     int i_value = 0;
1670                     int i_relative = 0;
1671 #define POSITION_ABSOLUTE 12
1672 #define POSITION_REL_FOR 13
1673 #define POSITION_REL_BACK 11
1674 #define VL_TIME_ABSOLUTE 0
1675 #define VL_TIME_REL_FOR 1
1676 #define VL_TIME_REL_BACK -1
1677                     if( p_sys->p_input )
1678                     {
1679                         uri_extract_value( p_request, "seek_value", value, 20 );
1680                         uri_decode_url_encoded( value );
1681                         p_value = value;
1682                         var_Get( p_sys->p_input, "length", &val);
1683                         i_length = val.i_time;
1684
1685                         while( p_value[0] != '\0' )
1686                         {
1687                             switch(p_value[0])
1688                             {
1689                                 case '+':
1690                                 {
1691                                     i_relative = VL_TIME_REL_FOR;
1692                                     p_value++;
1693                                     break;
1694                                 }
1695                                 case '-':
1696                                 {
1697                                     i_relative = VL_TIME_REL_BACK;
1698                                     p_value++;
1699                                     break;
1700                                 }
1701                                 case '0': case '1': case '2': case '3': case '4':
1702                                 case '5': case '6': case '7': case '8': case '9':
1703                                 {
1704                                     i_stock = strtol( p_value , &p_value , 10 );
1705                                     break;
1706                                 }
1707                                 case '%': /* for percentage ie position */
1708                                 {
1709                                     i_relative += POSITION_ABSOLUTE;
1710                                     i_value = i_stock;
1711                                     i_stock = 0;
1712                                     p_value[0] = '\0';
1713                                     break;
1714                                 }
1715                                 case ':':
1716                                 {
1717                                     i_value = 60 * (i_value + i_stock) ;
1718                                     i_stock = 0;
1719                                     p_value++;
1720                                     break;
1721                                 }
1722                                 case 'h': case 'H': /* hours */
1723                                 {
1724                                     i_value += 3600 * i_stock;
1725                                     i_stock = 0;
1726                                     /* other characters which are not numbers are not important */
1727                                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1728                                     {
1729                                         p_value++;
1730                                     }
1731                                     break;
1732                                 }
1733                                 case 'm': case 'M': case '\'': /* minutes */
1734                                 {
1735                                     i_value += 60 * i_stock;
1736                                     i_stock = 0;
1737                                     p_value++;
1738                                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1739                                     {
1740                                         p_value++;
1741                                     }
1742                                     break;
1743                                 }
1744                                 case 's': case 'S': case '"':  /* seconds */
1745                                 {
1746                                     i_value += i_stock;
1747                                     i_stock = 0;
1748                                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1749                                     {
1750                                         p_value++;
1751                                     }
1752                                     break;
1753                                 }
1754                                 default:
1755                                 {
1756                                     p_value++;
1757                                     break;
1758                                 }
1759                             }
1760                         }
1761
1762                         /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1763                         i_value += i_stock;
1764
1765                         switch(i_relative)
1766                         {
1767                             case VL_TIME_ABSOLUTE:
1768                             {
1769                                 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1770                                     val.i_time = (uint64_t)( i_value ) * 1000000;
1771                                 else
1772                                     val.i_time = i_length;
1773
1774                                 var_Set( p_sys->p_input, "time", val );
1775                                 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1776                                 break;
1777                             }
1778                             case VL_TIME_REL_FOR:
1779                             {
1780                                 var_Get( p_sys->p_input, "time", &val );
1781                                 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1782                                 {
1783                                     val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1784                                 } else
1785                                 {
1786                                     val.i_time = i_length;
1787                                 }
1788                                 var_Set( p_sys->p_input, "time", val );
1789                                 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1790                                 break;
1791                             }
1792                             case VL_TIME_REL_BACK:
1793                             {
1794                                 var_Get( p_sys->p_input, "time", &val );
1795                                 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1796                                 {
1797                                     val.i_time = 0;
1798                                 } else
1799                                 {
1800                                     val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1801                                 }
1802                                 var_Set( p_sys->p_input, "time", val );
1803                                 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1804                                 break;
1805                             }
1806                             case POSITION_ABSOLUTE:
1807                             {
1808                                 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1809                                 var_Set( p_sys->p_input, "position", val );
1810                                 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1811                                 break;
1812                             }
1813                             case POSITION_REL_FOR:
1814                             {
1815                                 var_Get( p_sys->p_input, "position", &val );
1816                                 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1817                                 var_Set( p_sys->p_input, "position", val );
1818                                 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1819                                 break;
1820                             }
1821                             case POSITION_REL_BACK:
1822                             {
1823                                 var_Get( p_sys->p_input, "position", &val );
1824                                 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1825                                 var_Set( p_sys->p_input, "position", val );
1826                                 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1827                                 break;
1828                             }
1829                             default:
1830                             {
1831                                 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1832                                 break;
1833                             }
1834                         }
1835                     }
1836 #undef POSITION_ABSOLUTE
1837 #undef POSITION_REL_FOR
1838 #undef POSITION_REL_BACK
1839 #undef VL_TIME_ABSOLUTE
1840 #undef VL_TIME_REL_FOR
1841 #undef VL_TIME_REL_BACK
1842                     break;
1843                 }
1844                 case MVLC_VOLUME:
1845                 {
1846                     char vol[8];
1847                     audio_volume_t i_volume;
1848                     int i_value;
1849
1850                     uri_extract_value( p_request, "value", vol, 8 );
1851                     aout_VolumeGet( p_intf, &i_volume );
1852                     uri_decode_url_encoded( vol );
1853
1854                     if( vol[0] == '+' )
1855                     {
1856                         i_value = atoi( vol + 1 );
1857                         if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1858                         {
1859                             aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1860                             msg_Dbg( p_intf, "requested volume set: max" );
1861                         } else
1862                         {
1863                             aout_VolumeSet( p_intf , (i_volume + i_value) );
1864                             msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1865                         }
1866                     } else
1867                     if( vol[0] == '-' )
1868                     {
1869                         i_value = atoi( vol + 1 );
1870                         if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1871                         {
1872                             aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1873                             msg_Dbg( p_intf, "requested volume set: min" );
1874                         } else
1875                         {
1876                             aout_VolumeSet( p_intf , (i_volume - i_value) );
1877                             msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1878                         }
1879                     } else
1880                     if( strstr(vol, "%") != NULL )
1881                     {
1882                         i_value = atoi( vol );
1883                         if( (i_value <= 400) && (i_value>=0) ){
1884                             aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/400+AOUT_VOLUME_MIN);
1885                             msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
1886                         }
1887                     } else
1888                     {
1889                         i_value = atoi( vol );
1890                         if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1891                         {
1892                             aout_VolumeSet( p_intf , atoi( vol ) );
1893                             msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1894                         }
1895                     }
1896                     break;
1897                 }
1898
1899                 /* playlist management */
1900                 case MVLC_ADD:
1901                 {
1902                     char mrl[512];
1903                     playlist_item_t * p_item;
1904
1905                     uri_extract_value( p_request, "mrl", mrl, 512 );
1906                     uri_decode_url_encoded( mrl );
1907                     p_item = parse_MRL( p_intf, mrl );
1908
1909                     if( !p_item || !p_item->input.psz_uri ||
1910                         !*p_item->input.psz_uri )
1911                     {
1912                         msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1913                     } else
1914                     {
1915                         playlist_AddItem( p_sys->p_playlist , p_item ,
1916                                           PLAYLIST_APPEND, PLAYLIST_END );
1917                         msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1918                     }
1919
1920                     break;
1921                 }
1922                 case MVLC_DEL:
1923                 {
1924                     int i_item, *p_items = NULL, i_nb_items = 0;
1925                     char item[512], *p_parser = p_request;
1926
1927                     /* Get the list of items to delete */
1928                     while( (p_parser =
1929                             uri_extract_value( p_parser, "item", item, 512 )) )
1930                     {
1931                         if( !*item ) continue;
1932
1933                         i_item = atoi( item );
1934                         p_items = realloc( p_items, (i_nb_items + 1) *
1935                                            sizeof(int) );
1936                         p_items[i_nb_items] = i_item;
1937                         i_nb_items++;
1938                     }
1939
1940                     if( i_nb_items )
1941                     {
1942                         int i;
1943                         for( i = 0; i < i_nb_items; i++ )
1944                         {
1945                             playlist_LockDelete( p_sys->p_playlist, p_items[i] );
1946                             msg_Dbg( p_intf, "requested playlist delete: %d",
1947                                      p_items[i] );
1948                             p_items[i] = -1;
1949                         }
1950                     }
1951
1952                     if( p_items ) free( p_items );
1953                     break;
1954                 }
1955                 case MVLC_KEEP:
1956                 {
1957                     int i_item, *p_items = NULL, i_nb_items = 0;
1958                     char item[512], *p_parser = p_request;
1959                     int i,j;
1960
1961                     /* Get the list of items to keep */
1962                     while( (p_parser =
1963                             uri_extract_value( p_parser, "item", item, 512 )) )
1964                     {
1965                         if( !*item ) continue;
1966
1967                         i_item = atoi( item );
1968                         p_items = realloc( p_items, (i_nb_items + 1) *
1969                                            sizeof(int) );
1970                         p_items[i_nb_items] = i_item;
1971                         i_nb_items++;
1972                     }
1973
1974                     for( i = p_sys->p_playlist->i_size - 1 ; i >= 0; i-- )
1975                     {
1976                         /* Check if the item is in the keep list */
1977                         for( j = 0 ; j < i_nb_items ; j++ )
1978                         {
1979                             if( p_items[j] ==
1980                                 p_sys->p_playlist->pp_items[i]->input.i_id ) break;
1981                         }
1982                         if( j == i_nb_items )
1983                         {
1984                             playlist_LockDelete( p_sys->p_playlist, p_sys->p_playlist->pp_items[i]->input.i_id );
1985                             msg_Dbg( p_intf, "requested playlist delete: %d",
1986                                      i );
1987                         }
1988                     }
1989
1990                     if( p_items ) free( p_items );
1991                     break;
1992                 }
1993                 case MVLC_EMPTY:
1994                 {
1995                     playlist_LockClear( p_sys->p_playlist );
1996                     msg_Dbg( p_intf, "requested playlist empty" );
1997                     break;
1998                 }
1999                 case MVLC_SORT:
2000                 {
2001                     char type[12];
2002                     char order[2];
2003                     char item[512];
2004                     int i_order;
2005                     int i_item;
2006
2007                     uri_extract_value( p_request, "type", type, 12 );
2008                     uri_extract_value( p_request, "order", order, 2 );
2009                     uri_extract_value( p_request, "item", item, 512 );
2010                     i_item = atoi( item );
2011
2012                     if( order[0] == '0' ) i_order = ORDER_NORMAL;
2013                     else i_order = ORDER_REVERSE;
2014
2015                     if( !strcmp( type , "title" ) )
2016                     {
2017                         playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
2018                                                     p_sys->p_playlist->pp_views[0]->p_root,
2019                                                     SORT_TITLE_NODES_FIRST,
2020                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
2021                         msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
2022                     } else if( !strcmp( type , "author" ) )
2023                     {
2024                         playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
2025                                                     p_sys->p_playlist->pp_views[0]->p_root,
2026                                                     SORT_AUTHOR,
2027                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
2028                         msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
2029                     } else if( !strcmp( type , "shuffle" ) )
2030                     {
2031                         playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
2032                                                     p_sys->p_playlist->pp_views[0]->p_root,
2033                                                     SORT_RANDOM,
2034                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
2035                         msg_Dbg( p_intf, "requested playlist shuffle");
2036                     }
2037
2038                     break;
2039                 }
2040                 case MVLC_MOVE:
2041                 {
2042                     char psz_pos[6];
2043                     char psz_newpos[6];
2044                     int i_pos;
2045                     int i_newpos;
2046                     uri_extract_value( p_request, "psz_pos", psz_pos, 6 );
2047                     uri_extract_value( p_request, "psz_newpos", psz_newpos, 6 );
2048                     i_pos = atoi( psz_pos );
2049                     i_newpos = atoi( psz_newpos );
2050                     if ( i_pos < i_newpos )
2051                     {
2052                         playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
2053                     } else {
2054                         playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
2055                     }
2056                     msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
2057                     break;
2058                 }
2059
2060                 /* admin function */
2061                 case MVLC_CLOSE:
2062                 {
2063                     char id[512];
2064                     uri_extract_value( p_request, "id", id, 512 );
2065                     msg_Dbg( p_intf, "requested close id=%s", id );
2066 #if 0
2067                     if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
2068                     {
2069                         msg_Warn( p_intf, "close failed for id=%s", id );
2070                     }
2071 #endif
2072                     break;
2073                 }
2074                 case MVLC_SHUTDOWN:
2075                 {
2076                     msg_Dbg( p_intf, "requested shutdown" );
2077                     p_intf->p_vlc->b_die = VLC_TRUE;
2078                     break;
2079                 }
2080                 /* vlm */
2081                 case MVLC_VLM_NEW:
2082                 case MVLC_VLM_SETUP:
2083                 {
2084                     static const char *vlm_properties[11] =
2085                     {
2086                         /* no args */
2087                         "enabled", "disabled", "loop", "unloop",
2088                         /* args required */
2089                         "input", "output", "option", "date", "period", "repeat", "append",
2090                     };
2091                     vlm_message_t *vlm_answer;
2092                     char name[512];
2093                     char *psz = malloc( strlen( p_request ) + 1000 );
2094                     char *p = psz;
2095                     char *vlm_error;
2096                     int i;
2097
2098                     if( p_intf->p_sys->p_vlm == NULL )
2099                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
2100
2101                     if( p_intf->p_sys->p_vlm == NULL ) break;
2102
2103                     uri_extract_value( p_request, "name", name, 512 );
2104                     if( StrToMacroType( control ) == MVLC_VLM_NEW )
2105                     {
2106                         char type[20];
2107                         uri_extract_value( p_request, "type", type, 20 );
2108                         p += sprintf( psz, "new %s %s", name, type );
2109                     }
2110                     else
2111                     {
2112                         p += sprintf( psz, "setup %s", name );
2113                     }
2114                     /* Parse the request */
2115                     for( i = 0; i < 11; i++ )
2116                     {
2117                         char val[512];
2118                         uri_extract_value( p_request, vlm_properties[i], val, 512 );
2119                         uri_decode_url_encoded( val );
2120                         if( strlen( val ) > 0 && i >= 4 )
2121                         {
2122                             p += sprintf( p, " %s %s", vlm_properties[i], val );
2123                         }
2124                         else if( uri_test_param( p_request, vlm_properties[i] ) && i < 4 )
2125                         {
2126                             p += sprintf( p, " %s", vlm_properties[i] );
2127                         }
2128                     }
2129                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2130                     if( vlm_answer->psz_value == NULL ) /* there is no error */
2131                     {
2132                         vlm_error = strdup( "" );
2133                     }
2134                     else
2135                     {
2136                         vlm_error = malloc( strlen(vlm_answer->psz_name) +
2137                                             strlen(vlm_answer->psz_value) +
2138                                             strlen( " : ") + 1 );
2139                         sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
2140                                                          vlm_answer->psz_value );
2141                     }
2142
2143                     mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
2144
2145                     vlm_MessageDelete( vlm_answer );
2146                     free( vlm_error );
2147                     free( psz );
2148                     break;
2149                 }
2150
2151                 case MVLC_VLM_DEL:
2152                 {
2153                     vlm_message_t *vlm_answer;
2154                     char name[512];
2155                     char psz[512+10];
2156                     if( p_intf->p_sys->p_vlm == NULL )
2157                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
2158
2159                     if( p_intf->p_sys->p_vlm == NULL ) break;
2160
2161                     uri_extract_value( p_request, "name", name, 512 );
2162                     sprintf( psz, "del %s", name );
2163
2164                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2165                     /* FIXME do a vlm_answer -> var stack conversion */
2166                     vlm_MessageDelete( vlm_answer );
2167                     break;
2168                 }
2169
2170                 case MVLC_VLM_PLAY:
2171                 case MVLC_VLM_PAUSE:
2172                 case MVLC_VLM_STOP:
2173                 case MVLC_VLM_SEEK:
2174                 {
2175                     vlm_message_t *vlm_answer;
2176                     char name[512];
2177                     char psz[512+10];
2178                     if( p_intf->p_sys->p_vlm == NULL )
2179                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
2180
2181                     if( p_intf->p_sys->p_vlm == NULL ) break;
2182
2183                     uri_extract_value( p_request, "name", name, 512 );
2184                     if( StrToMacroType( control ) == MVLC_VLM_PLAY )
2185                         sprintf( psz, "control %s play", name );
2186                     else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
2187                         sprintf( psz, "control %s pause", name );
2188                     else if( StrToMacroType( control ) == MVLC_VLM_STOP )
2189                         sprintf( psz, "control %s stop", name );
2190                     else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
2191                     {
2192                         char percent[20];
2193                         uri_extract_value( p_request, "percent", percent, 512 );
2194                         sprintf( psz, "control %s seek %s", name, percent );
2195                     }
2196
2197                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2198                     /* FIXME do a vlm_answer -> var stack conversion */
2199                     vlm_MessageDelete( vlm_answer );
2200                     break;
2201                 }
2202                 case MVLC_VLM_LOAD:
2203                 case MVLC_VLM_SAVE:
2204                 {
2205                     vlm_message_t *vlm_answer;
2206                     char file[512];
2207                     char psz[512];
2208
2209                     if( p_intf->p_sys->p_vlm == NULL )
2210                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
2211
2212                     if( p_intf->p_sys->p_vlm == NULL ) break;
2213
2214                     uri_extract_value( p_request, "file", file, 512 );
2215                     uri_decode_url_encoded( file );
2216
2217                     if( StrToMacroType( control ) == MVLC_VLM_LOAD )
2218                         sprintf( psz, "load %s", file );
2219                     else
2220                         sprintf( psz, "save %s", file );
2221
2222                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2223                     /* FIXME do a vlm_answer -> var stack conversion */
2224                     vlm_MessageDelete( vlm_answer );
2225                     break;
2226                 }
2227
2228                 default:
2229                     if( *control )
2230                     {
2231                         PRINTS( "<!-- control param(%s) unsuported -->", control );
2232                     }
2233                     break;
2234             }
2235             break;
2236
2237         case MVLC_SET:
2238         {
2239             char    value[512];
2240             int     i;
2241             float   f;
2242
2243             if( i_request <= 0 ||
2244                 *m->param1  == '\0' ||
2245                 strstr( p_request, m->param1 ) == NULL )
2246             {
2247                 break;
2248             }
2249             uri_extract_value( p_request, m->param1,  value, 512 );
2250             uri_decode_url_encoded( value );
2251
2252             switch( StrToMacroType( m->param2 ) )
2253             {
2254                 case MVLC_INT:
2255                     i = atoi( value );
2256                     config_PutInt( p_intf, m->param1, i );
2257                     break;
2258                 case MVLC_FLOAT:
2259                     f = atof( value );
2260                     config_PutFloat( p_intf, m->param1, f );
2261                     break;
2262                 case MVLC_STRING:
2263                     config_PutPsz( p_intf, m->param1, value );
2264                     break;
2265                 default:
2266                     PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
2267             }
2268             break;
2269         }
2270         case MVLC_GET:
2271         {
2272             char    value[512];
2273             int     i;
2274             float   f;
2275             char    *psz;
2276
2277             if( *m->param1  == '\0' )
2278             {
2279                 break;
2280             }
2281
2282             switch( StrToMacroType( m->param2 ) )
2283             {
2284                 case MVLC_INT:
2285                     i = config_GetInt( p_intf, m->param1 );
2286                     sprintf( value, "%d", i );
2287                     break;
2288                 case MVLC_FLOAT:
2289                     f = config_GetFloat( p_intf, m->param1 );
2290                     sprintf( value, "%f", f );
2291                     break;
2292                 case MVLC_STRING:
2293                     psz = config_GetPsz( p_intf, m->param1 );
2294                     if( psz != NULL )
2295                     {
2296                         strncpy( value, psz,sizeof( value ) );
2297                         free( psz );
2298                         value[sizeof( value ) - 1] = '\0';
2299                     }
2300                     else
2301                         *value = '\0';
2302                     msg_Dbg( p_intf, "%d: value = \"%s\"", __LINE__, value );
2303                     break;
2304                 default:
2305                     snprintf( value, sizeof( value ),
2306                               "invalid type(%s) in set", m->param2 );
2307                     value[sizeof( value ) - 1] = '\0';
2308                     break;
2309             }
2310             PRINTS( "%s", value );
2311             break;
2312         }
2313         case MVLC_VALUE:
2314         {
2315             char *s, *v;
2316
2317             if( m->param1 )
2318             {
2319                 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2320                 s = SSPop( &p_args->stack );
2321                 v = mvar_GetValue( p_args->vars, s );
2322             }
2323             else
2324             {
2325                 v = s = SSPop( &p_args->stack );
2326             }
2327
2328             PRINTS( "%s", v );
2329             free( s );
2330             break;
2331         }
2332         case MVLC_RPN:
2333             EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2334             break;
2335
2336 /* Usefull for learning stack management */
2337         case MVLC_STACK:
2338         {
2339             int i;
2340             msg_Dbg( p_intf, "stack" );
2341             for (i=0;i<(&p_args->stack)->i_stack;i++)
2342                 msg_Dbg( p_intf, "%d -> %s", i, (&p_args->stack)->stack[i] );
2343             break;
2344         }
2345
2346         case MVLC_UNKNOWN:
2347         default:
2348             PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2349             msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2350             break;
2351     }
2352 #undef PRINTS
2353 #undef PRINT
2354 #undef ALLOC
2355 }
2356
2357 static char *MacroSearch( char *src, char *end, int i_mvlc, vlc_bool_t b_after )
2358 {
2359     int     i_id;
2360     int     i_level = 0;
2361
2362     while( src < end )
2363     {
2364         if( src + 4 < end  && !strncmp( (char *)src, "<vlc", 4 ) )
2365         {
2366             int i_skip;
2367             macro_t m;
2368
2369             i_skip = MacroParse( &m, src );
2370
2371             i_id = StrToMacroType( m.id );
2372
2373             switch( i_id )
2374             {
2375                 case MVLC_IF:
2376                 case MVLC_FOREACH:
2377                     i_level++;
2378                     break;
2379                 case MVLC_END:
2380                     i_level--;
2381                     break;
2382                 default:
2383                     break;
2384             }
2385
2386             MacroClean( &m );
2387
2388             if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2389                 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2390             {
2391                 return src + ( b_after ? i_skip : 0 );
2392             }
2393             else if( i_level < 0 )
2394             {
2395                 return NULL;
2396             }
2397
2398             src += i_skip;
2399         }
2400         else
2401         {
2402             src++;
2403         }
2404     }
2405
2406     return NULL;
2407 }
2408
2409 static void Execute( httpd_file_sys_t *p_args,
2410                      char *p_request, int i_request,
2411                      char **pp_data, int *pi_data,
2412                      char **pp_dst,
2413                      char *_src, char *_end )
2414 {
2415     intf_thread_t  *p_intf = p_args->p_intf;
2416
2417     char *src, *dup, *end;
2418     char *dst = *pp_dst;
2419
2420     src = dup = malloc( _end - _src + 1 );
2421     end = src +( _end - _src );
2422
2423     memcpy( src, _src, _end - _src );
2424     *end = '\0';
2425
2426     /* we parse searching <vlc */
2427     while( src < end )
2428     {
2429         char *p;
2430         int i_copy;
2431
2432         p = (char *)strstr( (char *)src, "<vlc" );
2433         if( p < end && p == src )
2434         {
2435             macro_t m;
2436
2437             src += MacroParse( &m, src );
2438
2439             //msg_Dbg( p_intf, "macro_id=%s", m.id );
2440
2441             switch( StrToMacroType( m.id ) )
2442             {
2443                 case MVLC_IF:
2444                 {
2445                     vlc_bool_t i_test;
2446                     char    *endif;
2447
2448                     EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2449                     if( SSPopN( &p_args->stack, p_args->vars ) )
2450                     {
2451                         i_test = 1;
2452                     }
2453                     else
2454                     {
2455                         i_test = 0;
2456                     }
2457                     endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2458
2459                     if( i_test == 0 )
2460                     {
2461                         char *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2462
2463                         if( start )
2464                         {
2465                             char *stop  = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2466                             if( stop )
2467                             {
2468                                 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2469                             }
2470                         }
2471                     }
2472                     else if( i_test == 1 )
2473                     {
2474                         char *stop;
2475                         if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2476                         {
2477                             stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2478                         }
2479                         if( stop )
2480                         {
2481                             Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2482                         }
2483                     }
2484
2485                     src = endif;
2486                     break;
2487                 }
2488                 case MVLC_FOREACH:
2489                 {
2490                     char *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2491                     char *start = src;
2492                     char *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2493
2494                     if( stop )
2495                     {
2496                         mvar_t *index;
2497                         int    i_idx;
2498                         mvar_t *v;
2499                         if( !strcmp( m.param2, "integer" ) )
2500                         {
2501                             char *arg = SSPop( &p_args->stack );
2502                             index = mvar_IntegerSetNew( m.param1, arg );
2503                             free( arg );
2504                         }
2505                         else if( !strcmp( m.param2, "directory" ) )
2506                         {
2507                             char *arg = SSPop( &p_args->stack );
2508                             index = mvar_FileSetNew( m.param1, arg );
2509                             free( arg );
2510                         }
2511                         else if( !strcmp( m.param2, "playlist" ) )
2512                         {
2513                             index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2514                         }
2515                         else if( !strcmp( m.param2, "information" ) )
2516                         {
2517                             index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2518                         }
2519                         else if( !strcmp( m.param2, "vlm" ) )
2520                         {
2521                             if( p_intf->p_sys->p_vlm == NULL )
2522                                 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2523                             index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
2524                         }
2525 #if 0
2526                         else if( !strcmp( m.param2, "hosts" ) )
2527                         {
2528                             index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2529                         }
2530                         else if( !strcmp( m.param2, "urls" ) )
2531                         {
2532                             index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2533                         }
2534                         else if( !strcmp( m.param2, "connections" ) )
2535                         {
2536                             index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2537                         }
2538 #endif
2539                         else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2540                         {
2541                             index = mvar_Duplicate( v );
2542                         }
2543                         else
2544                         {
2545                             msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2546                             src = endfor;
2547                             break;
2548                         }
2549
2550                         for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2551                         {
2552                             mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2553
2554                             //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2555
2556                             free( f->name );
2557                             f->name = strdup( m.param1 );
2558
2559
2560                             mvar_PushVar( p_args->vars, f );
2561                             Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2562                             mvar_RemoveVar( p_args->vars, f );
2563
2564                             mvar_Delete( f );
2565                         }
2566                         mvar_Delete( index );
2567
2568                         src = endfor;
2569                     }
2570                     break;
2571                 }
2572                 default:
2573                     MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2574                     break;
2575             }
2576
2577             MacroClean( &m );
2578             continue;
2579         }
2580
2581         i_copy =   ( (p == NULL || p > end ) ? end : p  ) - src;
2582         if( i_copy > 0 )
2583         {
2584             int i_index = dst - *pp_data;
2585
2586             *pi_data += i_copy;
2587             *pp_data = realloc( *pp_data, *pi_data );
2588             dst = (*pp_data) + i_index;
2589
2590             memcpy( dst, src, i_copy );
2591             dst += i_copy;
2592             src += i_copy;
2593         }
2594     }
2595
2596     *pp_dst = dst;
2597     free( dup );
2598 }
2599
2600 /****************************************************************************
2601  * HttpCallback:
2602  ****************************************************************************
2603  * a file with b_html is parsed and all "macro" replaced
2604  * <vlc id="macro name" [param1="" [param2=""]] />
2605  * valid id are
2606  *
2607  ****************************************************************************/
2608 static int  HttpCallback( httpd_file_sys_t *p_args,
2609                           httpd_file_t *p_file,
2610                           uint8_t *_p_request,
2611                           uint8_t **_pp_data, int *pi_data )
2612 {
2613     char *p_request = (char *)_p_request;
2614     char **pp_data = (char **)_pp_data;
2615     int i_request = p_request ? strlen( p_request ) : 0;
2616     char *p;
2617     FILE *f;
2618
2619     if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2620     {
2621         p = *pp_data = malloc( 10240 );
2622         if( !p )
2623         {
2624                 return VLC_EGENERIC;
2625         }
2626         p += sprintf( p, "<html>\n" );
2627         p += sprintf( p, "<head>\n" );
2628         p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2629         p += sprintf( p, "</head>\n" );
2630         p += sprintf( p, "<body>\n" );
2631         p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2632         p += sprintf( p, "<hr />\n" );
2633         p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2634         p += sprintf( p, "</body>\n" );
2635         p += sprintf( p, "</html>\n" );
2636
2637         *pi_data = strlen( *pp_data );
2638
2639         return VLC_SUCCESS;
2640     }
2641
2642     if( !p_args->b_html )
2643     {
2644         FileLoad( f, pp_data, pi_data );
2645     }
2646     else
2647     {
2648         int  i_buffer;
2649         char *p_buffer;
2650         char *dst;
2651         vlc_value_t val;
2652         char position[4]; /* percentage */
2653         char time[12]; /* in seconds */
2654         char length[12]; /* in seconds */
2655         audio_volume_t i_volume;
2656         char volume[5];
2657         char state[8];
2658  
2659 #define p_sys p_args->p_intf->p_sys
2660         if( p_sys->p_input )
2661         {
2662             var_Get( p_sys->p_input, "position", &val);
2663             sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2664             var_Get( p_sys->p_input, "time", &val);
2665             sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2666             var_Get( p_sys->p_input, "length", &val);
2667             sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2668
2669             var_Get( p_sys->p_input, "state", &val );
2670             if( val.i_int == PLAYING_S )
2671             {
2672                 sprintf( state, "playing" );
2673             } else if( val.i_int == PAUSE_S )
2674             {
2675                 sprintf( state, "paused" );
2676             } else
2677             {
2678                 sprintf( state, "stop" );
2679             }
2680         } else
2681         {
2682             sprintf( position, "%d", 0 );
2683             sprintf( time, "%d", 0 );
2684             sprintf( length, "%d", 0 );
2685             sprintf( state, "stop" );
2686         }
2687 #undef p_sys
2688
2689         aout_VolumeGet( p_args->p_intf , &i_volume );
2690         sprintf( volume , "%d" , (int)i_volume );
2691
2692         p_args->vars = mvar_New( "variables", "" );
2693         mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2694         mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2695         mvar_AppendNewVar( p_args->vars, "version",   VERSION_MESSAGE );
2696         mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2697         mvar_AppendNewVar( p_args->vars, "stream_position", position );
2698         mvar_AppendNewVar( p_args->vars, "stream_time", time );
2699         mvar_AppendNewVar( p_args->vars, "stream_length", length );
2700         mvar_AppendNewVar( p_args->vars, "volume", volume );
2701         mvar_AppendNewVar( p_args->vars, "stream_state", state );
2702
2703         SSInit( &p_args->stack );
2704
2705         /* first we load in a temporary buffer */
2706         FileLoad( f, &p_buffer, &i_buffer );
2707
2708         /* allocate output */
2709         *pi_data = i_buffer + 1000;
2710         dst = *pp_data = malloc( *pi_data );
2711
2712         /* we parse executing all  <vlc /> macros */
2713         Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2714
2715         *dst     = '\0';
2716         *pi_data = dst - *pp_data;
2717
2718         SSClean( &p_args->stack );
2719         mvar_Delete( p_args->vars );
2720         free( p_buffer );
2721     }
2722
2723     fclose( f );
2724
2725     return VLC_SUCCESS;
2726 }
2727
2728 /****************************************************************************
2729  * uri parser
2730  ****************************************************************************/
2731 static int uri_test_param( char *psz_uri, const char *psz_name )
2732 {
2733     char *p = psz_uri;
2734
2735     while( (p = strstr( p, psz_name )) )
2736     {
2737         /* Verify that we are dealing with a post/get argument */
2738         if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2739         {
2740             return VLC_TRUE;
2741         }
2742         p++;
2743     }
2744
2745     return VLC_FALSE;
2746 }
2747 static char *uri_extract_value( char *psz_uri, const char *psz_name,
2748                                 char *psz_value, int i_value_max )
2749 {
2750     char *p = psz_uri;
2751
2752     while( (p = strstr( p, psz_name )) )
2753     {
2754         /* Verify that we are dealing with a post/get argument */
2755         if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2756             break;
2757         p++;
2758     }
2759
2760     if( p )
2761     {
2762         int i_len;
2763
2764         p += strlen( psz_name );
2765         if( *p == '=' ) p++;
2766
2767         if( strchr( p, '&' ) )
2768         {
2769             i_len = strchr( p, '&' ) - p;
2770         }
2771         else
2772         {
2773             /* for POST method */
2774             if( strchr( p, '\n' ) )
2775             {
2776                 i_len = strchr( p, '\n' ) - p;
2777                 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2778             }
2779             else
2780             {
2781                 i_len = strlen( p );
2782             }
2783         }
2784         i_len = __MIN( i_value_max - 1, i_len );
2785         if( i_len > 0 )
2786         {
2787             strncpy( psz_value, p, i_len );
2788             psz_value[i_len] = '\0';
2789         }
2790         else
2791         {
2792             strncpy( psz_value, "", i_value_max );
2793         }
2794         p += i_len;
2795     }
2796     else
2797     {
2798         strncpy( psz_value, "", i_value_max );
2799     }
2800
2801     return p;
2802 }
2803
2804 static void uri_decode_url_encoded( char *psz )
2805 {
2806     char *dup = strdup( psz );
2807     char *p = dup;
2808
2809     while( *p )
2810     {
2811         if( *p == '%' )
2812         {
2813             char val[3];
2814             p++;
2815             if( !*p )
2816             {
2817                 break;
2818             }
2819
2820             val[0] = *p++;
2821             val[1] = *p++;
2822             val[2] = '\0';
2823
2824             *psz++ = strtol( val, NULL, 16 );
2825         }
2826         else if( *p == '+' )
2827         {
2828             *psz++ = ' ';
2829             p++;
2830         }
2831         else
2832         {
2833             *psz++ = *p++;
2834         }
2835     }
2836     *psz++ = '\0';
2837     free( dup );
2838 }
2839
2840 /****************************************************************************
2841  * Light RPN evaluator
2842  ****************************************************************************/
2843 static void SSInit( rpn_stack_t *st )
2844 {
2845     st->i_stack = 0;
2846 }
2847
2848 static void SSClean( rpn_stack_t *st )
2849 {
2850     while( st->i_stack > 0 )
2851     {
2852         free( st->stack[--st->i_stack] );
2853     }
2854 }
2855
2856 static void SSPush( rpn_stack_t *st, char *s )
2857 {
2858     if( st->i_stack < STACK_MAX )
2859     {
2860         st->stack[st->i_stack++] = strdup( s );
2861     }
2862 }
2863
2864 static char * SSPop( rpn_stack_t *st )
2865 {
2866     if( st->i_stack <= 0 )
2867     {
2868         return strdup( "" );
2869     }
2870     else
2871     {
2872         return st->stack[--st->i_stack];
2873     }
2874 }
2875
2876 static int SSPopN( rpn_stack_t *st, mvar_t  *vars )
2877 {
2878     char *name;
2879     char *value;
2880
2881     char *end;
2882     int  i;
2883
2884     name = SSPop( st );
2885     i = strtol( name, &end, 0 );
2886     if( end == name )
2887     {
2888         value = mvar_GetValue( vars, name );
2889         i = atoi( value );
2890     }
2891     free( name );
2892
2893     return( i );
2894 }
2895
2896 static void SSPushN( rpn_stack_t *st, int i )
2897 {
2898     char v[512];
2899
2900     sprintf( v, "%d", i );
2901     SSPush( st, v );
2902 }
2903
2904 static void  EvaluateRPN( mvar_t  *vars, rpn_stack_t *st, char *exp )
2905 {
2906     for( ;; )
2907     {
2908         char s[100], *p;
2909
2910         /* skip spcae */
2911         while( *exp == ' ' )
2912         {
2913             exp++;
2914         }
2915
2916         if( *exp == '\'' )
2917         {
2918             /* extract string */
2919             p = &s[0];
2920             exp++;
2921             while( *exp && *exp != '\'' )
2922             {
2923                 *p++ = *exp++;
2924             }
2925             *p = '\0';
2926             exp++;
2927             SSPush( st, s );
2928             continue;
2929         }
2930
2931         /* extract token */
2932         p = strchr( exp, ' ' );
2933         if( !p )
2934         {
2935             strcpy( s, exp );
2936
2937             exp += strlen( exp );
2938         }
2939         else
2940         {
2941             int i = p -exp;
2942             strncpy( s, exp, i );
2943             s[i] = '\0';
2944
2945             exp = p + 1;
2946         }
2947
2948         if( *s == '\0' )
2949         {
2950             break;
2951         }
2952
2953         /* 1. Integer function */
2954         if( !strcmp( s, "!" ) )
2955         {
2956             SSPushN( st, ~SSPopN( st, vars ) );
2957         }
2958         else if( !strcmp( s, "^" ) )
2959         {
2960             SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2961         }
2962         else if( !strcmp( s, "&" ) )
2963         {
2964             SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2965         }
2966         else if( !strcmp( s, "|" ) )
2967         {
2968             SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2969         }
2970         else if( !strcmp( s, "+" ) )
2971         {
2972             SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2973         }
2974         else if( !strcmp( s, "-" ) )
2975         {
2976             int j = SSPopN( st, vars );
2977             int i = SSPopN( st, vars );
2978             SSPushN( st, i - j );
2979         }
2980         else if( !strcmp( s, "*" ) )
2981         {
2982             SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2983         }
2984         else if( !strcmp( s, "/" ) )
2985         {
2986             int i, j;
2987
2988             j = SSPopN( st, vars );
2989             i = SSPopN( st, vars );
2990
2991             SSPushN( st, j != 0 ? i / j : 0 );
2992         }
2993         else if( !strcmp( s, "%" ) )
2994         {
2995             int i, j;
2996
2997             j = SSPopN( st, vars );
2998             i = SSPopN( st, vars );
2999
3000             SSPushN( st, j != 0 ? i % j : 0 );
3001         }
3002         /* 2. integer tests */
3003         else if( !strcmp( s, "=" ) )
3004         {
3005             SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
3006         }
3007         else if( !strcmp( s, "!=" ) )
3008         {
3009             SSPushN( st, SSPopN( st, vars ) != SSPopN( st, vars ) ? -1 : 0 );
3010         }
3011         else if( !strcmp( s, "<" ) )
3012         {
3013             int j = SSPopN( st, vars );
3014             int i = SSPopN( st, vars );
3015
3016             SSPushN( st, i < j ? -1 : 0 );
3017         }
3018         else if( !strcmp( s, ">" ) )
3019         {
3020             int j = SSPopN( st, vars );
3021             int i = SSPopN( st, vars );
3022
3023             SSPushN( st, i > j ? -1 : 0 );
3024         }
3025         else if( !strcmp( s, "<=" ) )
3026         {
3027             int j = SSPopN( st, vars );
3028             int i = SSPopN( st, vars );
3029
3030             SSPushN( st, i <= j ? -1 : 0 );
3031         }
3032         else if( !strcmp( s, ">=" ) )
3033         {
3034             int j = SSPopN( st, vars );
3035             int i = SSPopN( st, vars );
3036
3037             SSPushN( st, i >= j ? -1 : 0 );
3038         }
3039         /* 3. string functions */
3040         else if( !strcmp( s, "strcat" ) )
3041         {
3042             char *s2 = SSPop( st );
3043             char *s1 = SSPop( st );
3044             char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
3045
3046             strcpy( str, s1 );
3047             strcat( str, s2 );
3048
3049             SSPush( st, str );
3050             free( s1 );
3051             free( s2 );
3052             free( str );
3053         }
3054         else if( !strcmp( s, "strcmp" ) )
3055         {
3056             char *s2 = SSPop( st );
3057             char *s1 = SSPop( st );
3058
3059             SSPushN( st, strcmp( s1, s2 ) );
3060             free( s1 );
3061             free( s2 );
3062         }
3063         else if( !strcmp( s, "strncmp" ) )
3064         {
3065             int n = SSPopN( st, vars );
3066             char *s2 = SSPop( st );
3067             char *s1 = SSPop( st );
3068
3069             SSPushN( st, strncmp( s1, s2 , n ) );
3070             free( s1 );
3071             free( s2 );
3072         }
3073         else if( !strcmp( s, "strsub" ) )
3074         {
3075             int n = SSPopN( st, vars );
3076             int m = SSPopN( st, vars );
3077             int i_len;
3078             char *s = SSPop( st );
3079             char *str;
3080
3081             if( n >= m )
3082             {
3083                 i_len = n - m + 1;
3084             }
3085             else
3086             {
3087                 i_len = 0;
3088             }
3089
3090             str = malloc( i_len + 1 );
3091
3092             memcpy( str, s + m - 1, i_len );
3093             str[ i_len ] = '\0';
3094
3095             SSPush( st, str );
3096             free( s );
3097             free( str );
3098         }
3099        else if( !strcmp( s, "strlen" ) )
3100         {
3101             char *str = SSPop( st );
3102
3103             SSPushN( st, strlen( str ) );
3104             free( str );
3105         }
3106         /* 4. stack functions */
3107         else if( !strcmp( s, "dup" ) )
3108         {
3109             char *str = SSPop( st );
3110             SSPush( st, str );
3111             SSPush( st, str );
3112             free( str );
3113         }
3114         else if( !strcmp( s, "drop" ) )
3115         {
3116             char *str = SSPop( st );
3117             free( str );
3118         }
3119         else if( !strcmp( s, "swap" ) )
3120         {
3121             char *s1 = SSPop( st );
3122             char *s2 = SSPop( st );
3123
3124             SSPush( st, s1 );
3125             SSPush( st, s2 );
3126             free( s1 );
3127             free( s2 );
3128         }
3129         else if( !strcmp( s, "flush" ) )
3130         {
3131             SSClean( st );
3132             SSInit( st );
3133         }
3134         else if( !strcmp( s, "store" ) )
3135         {
3136             char *value = SSPop( st );
3137             char *name  = SSPop( st );
3138
3139             mvar_PushNewVar( vars, name, value );
3140             free( name );
3141             free( value );
3142         }
3143         else if( !strcmp( s, "value" ) )
3144         {
3145             char *name  = SSPop( st );
3146             char *value = mvar_GetValue( vars, name );
3147
3148             SSPush( st, value );
3149
3150             free( name );
3151         }
3152         else if( !strcmp( s, "url_extract" ) )
3153         {
3154             char *url = mvar_GetValue( vars, "url_value" );
3155             char *name = SSPop( st );
3156             char value[512];
3157
3158             uri_extract_value( url, name, value, 512 );
3159             uri_decode_url_encoded( value );
3160             SSPush( st, value );
3161         }
3162         else if( !strcmp( s, "url_encode" ) )
3163         {
3164             char *url = SSPop( st );
3165             char *value;
3166
3167             value = vlc_UrlEncode( url );
3168             SSPush( st, value );
3169             free( value );
3170         }
3171         else
3172         {
3173             SSPush( st, s );
3174         }
3175     }
3176 }
3177
3178 /**********************************************************************
3179  * Find_end_MRL: Find the end of the sentence :
3180  * this function parses the string psz and find the end of the item
3181  * and/or option with detecting the " and ' problems.
3182  * returns NULL if an error is detected, otherwise, returns a pointer
3183  * of the end of the sentence (after the last character)
3184  **********************************************************************/
3185 static char *Find_end_MRL( char *psz )
3186 {
3187     char *s_sent = psz;
3188
3189     switch( *s_sent )
3190     {
3191         case '\"':
3192         {
3193             s_sent++;
3194
3195             while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
3196             {
3197                 if( *s_sent == '\'' )
3198                 {
3199                     s_sent = Find_end_MRL( s_sent );
3200
3201                     if( s_sent == NULL )
3202                     {
3203                         return NULL;
3204                     }
3205                 } else
3206                 {
3207                     s_sent++;
3208                 }
3209             }
3210
3211             if( *s_sent == '\"' )
3212             {
3213                 s_sent++;
3214                 return s_sent;
3215             } else  /* *s_sent == '\0' , which means the number of " is incorrect */
3216             {
3217                 return NULL;
3218             }
3219             break;
3220         }
3221         case '\'':
3222         {
3223             s_sent++;
3224
3225             while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
3226             {
3227                 if( *s_sent == '\"' )
3228                 {
3229                     s_sent = Find_end_MRL( s_sent );
3230
3231                     if( s_sent == NULL )
3232                     {
3233                         return NULL;
3234                     }
3235                 } else
3236                 {
3237                     s_sent++;
3238                 }
3239             }
3240
3241             if( *s_sent == '\'' )
3242             {
3243                 s_sent++;
3244                 return s_sent;
3245             } else  /* *s_sent == '\0' , which means the number of ' is incorrect */
3246             {
3247                 return NULL;
3248             }
3249             break;
3250         }
3251         default: /* now we can look for spaces */
3252         {
3253             while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
3254             {
3255                 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
3256                 {
3257                     s_sent = Find_end_MRL( s_sent );
3258                 } else
3259                 {
3260                     s_sent++;
3261                 }
3262             }
3263             return s_sent;
3264         }
3265     }
3266 }
3267
3268 /**********************************************************************
3269  * parse_MRL: parse the MRL, find the mrl string and the options,
3270  * create an item with all information in it, and return the item.
3271  * return NULL if there is an error.
3272  **********************************************************************/
3273 static playlist_item_t *parse_MRL( intf_thread_t *p_intf, char *psz )
3274 {
3275     char **ppsz_options = NULL;
3276     char *mrl;
3277     char *s_mrl = psz;
3278     int i_error = 0;
3279     char *s_temp;
3280     int i = 0;
3281     int i_options = 0;
3282     playlist_item_t * p_item = NULL;
3283
3284     /* In case there is spaces before the mrl */
3285     while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
3286     {
3287         s_mrl++;
3288     }
3289
3290     /* extract the mrl */
3291     s_temp = strstr( s_mrl , " :" );
3292     if( s_temp == NULL )
3293     {
3294         s_temp = s_mrl + strlen( s_mrl );
3295     } else
3296     {
3297         while( (*s_temp == ' ') && (s_temp != s_mrl ) )
3298         {
3299             s_temp--;
3300         }
3301         s_temp++;
3302     }
3303
3304     /* if the mrl is between " or ', we must remove them */
3305     if( (*s_mrl == '\'') || (*s_mrl == '\"') )
3306     {
3307         mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
3308         strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
3309         mrl[ s_temp - s_mrl - 2 ] = '\0';
3310     } else
3311     {
3312         mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
3313         strncpy( mrl , s_mrl , s_temp - s_mrl );
3314         mrl[ s_temp - s_mrl ] = '\0';
3315     }
3316
3317     s_mrl = s_temp;
3318
3319     /* now we can take care of the options */
3320     while( (*s_mrl != '\0') && (i_error == 0) )
3321     {
3322         switch( *s_mrl )
3323         {
3324             case ' ':
3325             {
3326                 s_mrl++;
3327                 break;
3328             }
3329             case ':': /* an option */
3330             {
3331                 s_temp = Find_end_MRL( s_mrl );
3332
3333                 if( s_temp == NULL )
3334                 {
3335                     i_error = 1;
3336                 }
3337                 else
3338                 {
3339                     i_options++;
3340                     ppsz_options = realloc( ppsz_options , i_options *
3341                                             sizeof(char *) );
3342                     ppsz_options[ i_options - 1 ] =
3343                         malloc( (s_temp - s_mrl + 1) * sizeof(char) );
3344
3345                     strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
3346                              s_temp - s_mrl );
3347
3348                     /* don't forget to finish the string with a '\0' */
3349                     (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
3350
3351                     s_mrl = s_temp;
3352                 }
3353                 break;
3354             }
3355             default:
3356             {
3357                 i_error = 1;
3358                 break;
3359             }
3360         }
3361     }
3362
3363     if( i_error != 0 )
3364     {
3365         free( mrl );
3366     }
3367     else
3368     {
3369         /* now create an item */
3370         p_item = playlist_ItemNew( p_intf, mrl, mrl);
3371         for( i = 0 ; i< i_options ; i++ )
3372         {
3373             playlist_ItemAddOption( p_item, ppsz_options[i] );
3374         }
3375     }
3376
3377     for( i = 0; i < i_options; i++ ) free( ppsz_options[i] );
3378     if( i_options ) free( ppsz_options );
3379
3380     return p_item;
3381 }