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