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