]> git.sesse.net Git - vlc/blob - modules/control/http.c
Merge string fixes
[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         mvar_AppendVar( s, itm );
922     }
923     vlc_mutex_unlock( &p_pl->object_lock );
924
925     return s;
926 }
927
928 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
929 {
930     mvar_t *s = mvar_New( name, "set" );
931     int i, j;
932
933     fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
934     if( p_input == NULL )
935     {
936         return s;
937     }
938
939     vlc_mutex_lock( &p_input->input.p_item->lock );
940     for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
941     {
942         info_category_t *p_category = p_input->input.p_item->pp_categories[i];
943         mvar_t *cat  = mvar_New( name, "set" );
944         mvar_t *iset = mvar_New( "info", "set" );
945
946         mvar_AppendNewVar( cat, "name", p_category->psz_name );
947         mvar_AppendVar( cat, iset );
948
949         for ( j = 0; j < p_category->i_infos; j++ )
950         {
951             info_t *p_info = p_category->pp_infos[j];
952             mvar_t *info = mvar_New( "info", "" );
953
954             msg_Dbg( p_input, "adding info name=%s value=%s",
955                      p_info->psz_name, p_info->psz_value );
956             mvar_AppendNewVar( info, "name",  p_info->psz_name );
957             mvar_AppendNewVar( info, "value", p_info->psz_value );
958             mvar_AppendVar( iset, info );
959         }
960         mvar_AppendVar( s, cat );
961     }
962     vlc_mutex_unlock( &p_input->input.p_item->lock );
963
964     return s;
965 }
966 #if 0
967 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
968 {
969     mvar_t       *s = mvar_New( name, "set" );
970     httpd_info_t info;
971     int          i;
972
973     fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
974     if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
975     {
976         for( i= 0; i < info.i_count; )
977         {
978             mvar_t *inf;
979
980             inf = mvar_New( name, "set" );
981             do
982             {
983                 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
984                             info.info[i].psz_name, info.info[i].psz_value ); */
985                 mvar_AppendNewVar( inf,
986                                    info.info[i].psz_name,
987                                    info.info[i].psz_value );
988                 i++;
989             } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
990             mvar_AppendVar( s, inf );
991         }
992     }
993
994     /* free mem */
995     for( i = 0; i < info.i_count; i++ )
996     {
997         free( info.info[i].psz_name );
998         free( info.info[i].psz_value );
999     }
1000     if( info.i_count > 0 )
1001     {
1002         free( info.info );
1003     }
1004
1005     return s;
1006 }
1007 #endif
1008
1009 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1010 {
1011     mvar_t *s = mvar_New( name, "set" );
1012     char           tmp[MAX_DIR_SIZE], *p, *src;
1013 #ifdef HAVE_SYS_STAT_H
1014     struct stat   stat_info;
1015 #endif
1016     DIR           *p_dir;
1017     struct dirent *p_dir_content;
1018     char          sep;
1019
1020     /* convert all / to native separator */
1021 #if defined( WIN32 )
1022     while( (p = strchr( psz_dir, '/' )) )
1023     {
1024         *p = '\\';
1025     }
1026     sep = '\\';
1027 #else
1028     sep = '/';
1029 #endif
1030
1031     /* remove trailling separator */
1032     while( strlen( psz_dir ) > 1 &&
1033 #if defined( WIN32 )
1034            !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1035 #endif
1036            psz_dir[strlen( psz_dir ) -1 ] == sep )
1037     {
1038         psz_dir[strlen( psz_dir ) -1 ]  ='\0';
1039     }
1040     /* remove double separator */
1041     for( p = src = psz_dir; *src != '\0'; src++, p++ )
1042     {
1043         if( src[0] == sep && src[1] == sep )
1044         {
1045             src++;
1046         }
1047         *p = *src;
1048     }
1049     *p = '\0';
1050
1051     if( *psz_dir == '\0' )
1052     {
1053         return s;
1054     }
1055     /* first fix all .. dir */
1056     p = src = psz_dir;
1057     while( *src )
1058     {
1059         if( src[0] == '.' && src[1] == '.' )
1060         {
1061             src += 2;
1062             if( p <= &psz_dir[1] )
1063             {
1064                 continue;
1065             }
1066
1067             p -= 2;
1068
1069             while( p > &psz_dir[1] && *p != sep )
1070             {
1071                 p--;
1072             }
1073         }
1074         else if( *src == sep )
1075         {
1076             if( p > psz_dir && p[-1] == sep )
1077             {
1078                 src++;
1079             }
1080             else
1081             {
1082                 *p++ = *src++;
1083             }
1084         }
1085         else
1086         {
1087             do
1088             {
1089                 *p++ = *src++;
1090             } while( *src && *src != sep );
1091         }
1092     }
1093     *p = '\0';
1094
1095     fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1096
1097 #ifdef HAVE_SYS_STAT_H
1098     if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1099     {
1100         return s;
1101     }
1102 #endif
1103
1104     if( ( p_dir = opendir( psz_dir ) ) == NULL )
1105     {
1106         fprintf( stderr, "cannot open dir (%s)", psz_dir );
1107         return s;
1108     }
1109
1110     /* remove traling / or \ */
1111     for( p = &psz_dir[strlen( psz_dir) - 1];
1112          p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1113     {
1114         *p = '\0';
1115     }
1116
1117     for( ;; )
1118     {
1119         mvar_t *f;
1120
1121         /* parse psz_src dir */
1122         if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1123         {
1124             break;
1125         }
1126         if( !strcmp( p_dir_content->d_name, "." ) )
1127         {
1128             continue;
1129         }
1130
1131         sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1132
1133 #ifdef HAVE_SYS_STAT_H
1134         if( stat( tmp, &stat_info ) == -1 )
1135         {
1136             continue;
1137         }
1138 #endif
1139         f = mvar_New( name, "set" );
1140         mvar_AppendNewVar( f, "name", tmp );
1141
1142 #ifdef HAVE_SYS_STAT_H
1143         if( S_ISDIR( stat_info.st_mode ) )
1144         {
1145             mvar_AppendNewVar( f, "type", "directory" );
1146         }
1147         else if( S_ISREG( stat_info.st_mode ) )
1148         {
1149             mvar_AppendNewVar( f, "type", "file" );
1150         }
1151         else
1152         {
1153             mvar_AppendNewVar( f, "type", "unknown" );
1154         }
1155
1156         sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
1157         mvar_AppendNewVar( f, "size", tmp );
1158
1159         /* FIXME memory leak FIXME */
1160 #ifdef HAVE_CTIME_R
1161         ctime_r( &stat_info.st_mtime, tmp );
1162         mvar_AppendNewVar( f, "date", tmp );
1163 #else
1164         mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1165 #endif
1166
1167 #else
1168         mvar_AppendNewVar( f, "type", "unknown" );
1169         mvar_AppendNewVar( f, "size", "unknown" );
1170         mvar_AppendNewVar( f, "date", "unknown" );
1171 #endif
1172         mvar_AppendVar( s, f );
1173     }
1174
1175     return s;
1176 }
1177
1178 static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
1179 {
1180     mvar_t        *s = mvar_New( name, "set" );
1181     vlm_message_t *msg;
1182     int    i;
1183
1184     /* fprintf( stderr," mvar_VlmSetNew: name=`%s'\n", name ); */
1185     if( vlm == NULL ) return s;
1186
1187     if( vlm_ExecuteCommand( vlm, "show", &msg ) )
1188     {
1189         return s;
1190     }
1191
1192     for( i = 0; i < msg->i_child; i++ )
1193     {
1194         /* Over media, schedule */
1195         vlm_message_t *ch = msg->child[i];
1196         int j;
1197
1198         for( j = 0; j < ch->i_child; j++ )
1199         {
1200             /* Over name */
1201             vlm_message_t *el = ch->child[j];
1202             vlm_message_t *inf, *desc;
1203             mvar_t        *set;
1204             char          psz[500];
1205             int k;
1206
1207             sprintf( psz, "show %s", el->psz_name );
1208             if( vlm_ExecuteCommand( vlm, psz, &inf ) )
1209                 continue;
1210             desc = inf->child[0];
1211
1212             /* Add a node with name and info */
1213             set = mvar_New( name, "set" );
1214             mvar_AppendNewVar( set, "name", el->psz_name );
1215
1216             /* fprintf( stderr, "#### name=%s\n", el->psz_name ); */
1217
1218             for( k = 0; k < desc->i_child; k++ )
1219             {
1220                 vlm_message_t *ch = desc->child[k];
1221                 if( ch->i_child > 0 )
1222                 {
1223                     int c;
1224                     mvar_t *n = mvar_New( ch->psz_name, "set" );
1225
1226                     /* fprintf( stderr, "        child=%s [%d]\n", ch->psz_name, ch->i_child ); */
1227                     for( c = 0; c < ch->i_child; c++ )
1228                     {
1229                         mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
1230                         mvar_AppendVar( n, in );
1231
1232                         /* fprintf( stderr, "            sub=%s\n", ch->child[c]->psz_name );*/
1233                     }
1234                     mvar_AppendVar( set, n );
1235                 }
1236                 else
1237                 {
1238                     /* fprintf( stderr, "        child=%s->%s\n", ch->psz_name, ch->psz_value ); */
1239                     mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
1240                 }
1241             }
1242             vlm_MessageDelete( inf );
1243
1244             mvar_AppendVar( s, set );
1245         }
1246     }
1247     vlm_MessageDelete( msg );
1248
1249     return s;
1250 }
1251
1252
1253 static void SSInit( rpn_stack_t * );
1254 static void SSClean( rpn_stack_t * );
1255 static void EvaluateRPN( mvar_t  *, rpn_stack_t *, char * );
1256
1257 static void SSPush  ( rpn_stack_t *, char * );
1258 static char *SSPop  ( rpn_stack_t * );
1259
1260 static void SSPushN ( rpn_stack_t *, int );
1261 static int  SSPopN  ( rpn_stack_t *, mvar_t  * );
1262
1263
1264 /****************************************************************************
1265  * Macro handling
1266  ****************************************************************************/
1267 typedef struct
1268 {
1269     char *id;
1270     char *param1;
1271     char *param2;
1272 } macro_t;
1273
1274 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1275 {
1276     int i_read;
1277
1278     /* just load the file */
1279     *pi_data = 0;
1280     *pp_data = malloc( 1025 );  /* +1 for \0 */
1281     while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1282     {
1283         *pi_data += 1024;
1284         *pp_data = realloc( *pp_data, *pi_data  + 1025 );
1285     }
1286     if( i_read > 0 )
1287     {
1288         *pi_data += i_read;
1289     }
1290     (*pp_data)[*pi_data] = '\0';
1291
1292     return VLC_SUCCESS;
1293 }
1294
1295 static int MacroParse( macro_t *m, uint8_t *psz_src )
1296 {
1297     uint8_t *dup = strdup( psz_src );
1298     uint8_t *src = dup;
1299     uint8_t *p;
1300     int     i_skip;
1301
1302 #define EXTRACT( name, l ) \
1303         src += l;    \
1304         p = strchr( src, '"' );             \
1305         if( p )                             \
1306         {                                   \
1307             *p++ = '\0';                    \
1308         }                                   \
1309         m->name = strdup( src );            \
1310         if( !p )                            \
1311         {                                   \
1312             break;                          \
1313         }                                   \
1314         src = p;
1315
1316     /* init m */
1317     m->id = NULL;
1318     m->param1 = NULL;
1319     m->param2 = NULL;
1320
1321     /* parse */
1322     src += 4;
1323
1324     while( *src )
1325     {
1326         while( *src == ' ')
1327         {
1328             src++;
1329         }
1330         if( !strncmp( src, "id=\"", 4 ) )
1331         {
1332             EXTRACT( id, 4 );
1333         }
1334         else if( !strncmp( src, "param1=\"", 8 ) )
1335         {
1336             EXTRACT( param1, 8 );
1337         }
1338         else if( !strncmp( src, "param2=\"", 8 ) )
1339         {
1340             EXTRACT( param2, 8 );
1341         }
1342         else
1343         {
1344             break;
1345         }
1346     }
1347     if( strstr( src, "/>" ) )
1348     {
1349         src = strstr( src, "/>" ) + 2;
1350     }
1351     else
1352     {
1353         src += strlen( src );
1354     }
1355
1356     if( m->id == NULL )
1357     {
1358         m->id = strdup( "" );
1359     }
1360     if( m->param1 == NULL )
1361     {
1362         m->param1 = strdup( "" );
1363     }
1364     if( m->param2 == NULL )
1365     {
1366         m->param2 = strdup( "" );
1367     }
1368     i_skip = src - dup;
1369
1370     free( dup );
1371     return i_skip;
1372 #undef EXTRACT
1373 }
1374
1375 static void MacroClean( macro_t *m )
1376 {
1377     free( m->id );
1378     free( m->param1 );
1379     free( m->param2 );
1380 }
1381
1382 enum macroType
1383 {
1384     MVLC_UNKNOWN = 0,
1385     MVLC_CONTROL,
1386         MVLC_PLAY,
1387         MVLC_STOP,
1388         MVLC_PAUSE,
1389         MVLC_NEXT,
1390         MVLC_PREVIOUS,
1391         MVLC_ADD,
1392         MVLC_DEL,
1393         MVLC_EMPTY,
1394         MVLC_SEEK,
1395         MVLC_KEEP,
1396         MVLC_SORT,
1397         MVLC_MOVE,
1398         MVLC_VOLUME,
1399         MVLC_FULLSCREEN,
1400
1401         MVLC_CLOSE,
1402         MVLC_SHUTDOWN,
1403
1404         MVLC_VLM_NEW,
1405         MVLC_VLM_SETUP,
1406         MVLC_VLM_DEL,
1407         MVLC_VLM_PLAY,
1408         MVLC_VLM_PAUSE,
1409         MVLC_VLM_STOP,
1410         MVLC_VLM_SEEK,
1411         MVLC_VLM_LOAD,
1412         MVLC_VLM_SAVE,
1413
1414     MVLC_FOREACH,
1415     MVLC_IF,
1416     MVLC_RPN,
1417     MVLC_ELSE,
1418     MVLC_END,
1419     MVLC_GET,
1420     MVLC_SET,
1421         MVLC_INT,
1422         MVLC_FLOAT,
1423         MVLC_STRING,
1424
1425     MVLC_VALUE
1426 };
1427
1428 static struct
1429 {
1430     char *psz_name;
1431     int  i_type;
1432 }
1433 StrToMacroTypeTab [] =
1434 {
1435     { "control",    MVLC_CONTROL },
1436         /* player control */
1437         { "play",           MVLC_PLAY },
1438         { "stop",           MVLC_STOP },
1439         { "pause",          MVLC_PAUSE },
1440         { "next",           MVLC_NEXT },
1441         { "previous",       MVLC_PREVIOUS },
1442         { "seek",           MVLC_SEEK },
1443         { "keep",           MVLC_KEEP },
1444         { "fullscreen",     MVLC_FULLSCREEN },
1445         { "volume",         MVLC_VOLUME },
1446
1447         /* playlist management */
1448         { "add",            MVLC_ADD },
1449         { "delete",         MVLC_DEL },
1450         { "empty",          MVLC_EMPTY },
1451         { "sort",           MVLC_SORT },
1452         { "move",           MVLC_MOVE },
1453
1454         /* admin control */
1455         { "close",          MVLC_CLOSE },
1456         { "shutdown",       MVLC_SHUTDOWN },
1457
1458         /* vlm control */
1459         { "vlm_new",        MVLC_VLM_NEW },
1460         { "vlm_setup",      MVLC_VLM_SETUP },
1461         { "vlm_del",        MVLC_VLM_DEL },
1462         { "vlm_play",       MVLC_VLM_PLAY },
1463         { "vlm_pause",      MVLC_VLM_PAUSE },
1464         { "vlm_stop",       MVLC_VLM_STOP },
1465         { "vlm_seek",       MVLC_VLM_SEEK },
1466         { "vlm_load",       MVLC_VLM_LOAD },
1467         { "vlm_save",       MVLC_VLM_SAVE },
1468
1469     { "rpn",        MVLC_RPN },
1470
1471     { "foreach",    MVLC_FOREACH },
1472     { "value",      MVLC_VALUE },
1473
1474     { "if",         MVLC_IF },
1475     { "else",       MVLC_ELSE },
1476     { "end",        MVLC_END },
1477     { "get",         MVLC_GET },
1478     { "set",         MVLC_SET },
1479         { "int",            MVLC_INT },
1480         { "float",          MVLC_FLOAT },
1481         { "string",         MVLC_STRING },
1482
1483     /* end */
1484     { NULL,         MVLC_UNKNOWN }
1485 };
1486
1487 static int StrToMacroType( char *name )
1488 {
1489     int i;
1490
1491     if( !name || *name == '\0')
1492     {
1493         return MVLC_UNKNOWN;
1494     }
1495     for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1496     {
1497         if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1498         {
1499             return StrToMacroTypeTab[i].i_type;
1500         }
1501     }
1502     return MVLC_UNKNOWN;
1503 }
1504
1505 static void MacroDo( httpd_file_sys_t *p_args,
1506                      macro_t *m,
1507                      uint8_t *p_request, int i_request,
1508                      uint8_t **pp_data,  int *pi_data,
1509                      uint8_t **pp_dst )
1510 {
1511     intf_thread_t  *p_intf = p_args->p_intf;
1512     intf_sys_t     *p_sys = p_args->p_intf->p_sys;
1513     char control[512];
1514
1515 #define ALLOC( l ) \
1516     {               \
1517         int __i__ = *pp_dst - *pp_data; \
1518         *pi_data += (l);                  \
1519         *pp_data = realloc( *pp_data, *pi_data );   \
1520         *pp_dst = (*pp_data) + __i__;   \
1521     }
1522 #define PRINT( str ) \
1523     ALLOC( strlen( str ) + 1 ); \
1524     *pp_dst += sprintf( *pp_dst, str );
1525
1526 #define PRINTS( str, s ) \
1527     ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1528     { \
1529         char * psz_cur = *pp_dst; \
1530         *pp_dst += sprintf( *pp_dst, str, s ); \
1531         while( psz_cur && *psz_cur ) \
1532         {  \
1533             /* Prevent script injection */ \
1534             if( *psz_cur == '<' ) *psz_cur = '*'; \
1535             if( *psz_cur == '>' ) *psz_cur = '*'; \
1536             psz_cur++ ; \
1537         } \
1538     }
1539
1540     switch( StrToMacroType( m->id ) )
1541     {
1542         case MVLC_CONTROL:
1543             if( i_request <= 0 )
1544             {
1545                 break;
1546             }
1547             uri_extract_value( p_request, "control", control, 512 );
1548             if( *m->param1 && !strstr( m->param1, control ) )
1549             {
1550                 msg_Warn( p_intf, "unauthorized control=%s", control );
1551                 break;
1552             }
1553             switch( StrToMacroType( control ) )
1554             {
1555                 case MVLC_PLAY:
1556                 {
1557                     int i_item;
1558                     char item[512];
1559
1560                     uri_extract_value( p_request, "item", item, 512 );
1561                     i_item = atoi( item );
1562                     playlist_Control( p_sys->p_playlist, PLAYLIST_GOTO,
1563                                                          i_item );
1564                     msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1565                     break;
1566                 }
1567                 case MVLC_STOP:
1568                     playlist_Control( p_sys->p_playlist, PLAYLIST_STOP);
1569                     msg_Dbg( p_intf, "requested playlist stop" );
1570                     break;
1571                 case MVLC_PAUSE:
1572                     playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
1573                     msg_Dbg( p_intf, "requested playlist pause" );
1574                     break;
1575                 case MVLC_NEXT:
1576                     playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
1577                     msg_Dbg( p_intf, "requested playlist next" );
1578                     break;
1579                 case MVLC_PREVIOUS:
1580                     playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1);
1581                     msg_Dbg( p_intf, "requested playlist next" );
1582                     break;
1583                 case MVLC_FULLSCREEN:
1584                 {
1585                     if( p_sys->p_input )
1586                     {
1587                         vout_thread_t *p_vout;
1588                         p_vout = vlc_object_find( p_sys->p_input,
1589                                                   VLC_OBJECT_VOUT, FIND_CHILD );
1590
1591                         if( p_vout )
1592                         {
1593                             p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1594                             vlc_object_release( p_vout );
1595                             msg_Dbg( p_intf, "requested fullscreen toggle" );
1596                         }
1597                     }
1598                 }
1599                 break;
1600                 case MVLC_SEEK:
1601                 {
1602                     vlc_value_t val;
1603                     char value[30];
1604                     char * p_value;
1605                     int i_stock = 0;
1606                     uint64_t i_length;
1607                     int i_value = 0;
1608                     int i_relative = 0;
1609 #define POSITION_ABSOLUTE 12
1610 #define POSITION_REL_FOR 13
1611 #define POSITION_REL_BACK 11
1612 #define TIME_ABSOLUTE 0
1613 #define TIME_REL_FOR 1
1614 #define TIME_REL_BACK -1
1615                     if( p_sys->p_input )
1616                     {
1617                         uri_extract_value( p_request, "seek_value", value, 20 );
1618                         uri_decode_url_encoded( value );
1619                         p_value = value;
1620                         var_Get( p_sys->p_input, "length", &val);
1621                         i_length = val.i_time;
1622
1623                         while( p_value[0] != '\0' )
1624                         {
1625                             switch(p_value[0])
1626                             {
1627                                 case '+':
1628                                 {
1629                                     i_relative = TIME_REL_FOR;
1630                                     p_value++;
1631                                     break;
1632                                 }
1633                                 case '-':
1634                                 {
1635                                     i_relative = TIME_REL_BACK;
1636                                     p_value++;
1637                                     break;
1638                                 }
1639                                 case '0': case '1': case '2': case '3': case '4':
1640                                 case '5': case '6': case '7': case '8': case '9':
1641                                 {
1642                                     i_stock = strtol( p_value , &p_value , 10 );
1643                                     break;
1644                                 }
1645                                 case '%': /* for percentage ie position */
1646                                 {
1647                                     i_relative += POSITION_ABSOLUTE;
1648                                     i_value = i_stock;
1649                                     i_stock = 0;
1650                                     p_value[0] = '\0';
1651                                     break;
1652                                 }
1653                                 case ':':
1654                                 {
1655                                     i_value = 60 * (i_value + i_stock) ;
1656                                     i_stock = 0;
1657                                     p_value++;
1658                                     break;
1659                                 }
1660                                 case 'h': case 'H': /* hours */
1661                                 {
1662                                     i_value += 3600 * i_stock;
1663                                     i_stock = 0;
1664                                     /* other characters which are not numbers are not important */
1665                                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1666                                     {
1667                                         p_value++;
1668                                     }
1669                                     break;
1670                                 }
1671                                 case 'm': case 'M': case '\'': /* minutes */
1672                                 {
1673                                     i_value += 60 * i_stock;
1674                                     i_stock = 0;
1675                                     p_value++;
1676                                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1677                                     {
1678                                         p_value++;
1679                                     }
1680                                     break;
1681                                 }
1682                                 case 's': case 'S': case '"':  /* seconds */
1683                                 {
1684                                     i_value += i_stock;
1685                                     i_stock = 0;
1686                                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1687                                     {
1688                                         p_value++;
1689                                     }
1690                                     break;
1691                                 }
1692                                 default:
1693                                 {
1694                                     p_value++;
1695                                     break;
1696                                 }
1697                             }
1698                         }
1699
1700                         /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1701                         i_value += i_stock;
1702
1703                         switch(i_relative)
1704                         {
1705                             case TIME_ABSOLUTE:
1706                             {
1707                                 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1708                                     val.i_time = (uint64_t)( i_value ) * 1000000;
1709                                 else
1710                                     val.i_time = i_length;
1711
1712                                 var_Set( p_sys->p_input, "time", val );
1713                                 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1714                                 break;
1715                             }
1716                             case TIME_REL_FOR:
1717                             {
1718                                 var_Get( p_sys->p_input, "time", &val );
1719                                 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1720                                 {
1721                                     val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1722                                 } else
1723                                 {
1724                                     val.i_time = i_length;
1725                                 }
1726                                 var_Set( p_sys->p_input, "time", val );
1727                                 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1728                                 break;
1729                             }
1730                             case TIME_REL_BACK:
1731                             {
1732                                 var_Get( p_sys->p_input, "time", &val );
1733                                 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1734                                 {
1735                                     val.i_time = 0;
1736                                 } else
1737                                 {
1738                                     val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1739                                 }
1740                                 var_Set( p_sys->p_input, "time", val );
1741                                 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1742                                 break;
1743                             }
1744                             case POSITION_ABSOLUTE:
1745                             {
1746                                 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1747                                 var_Set( p_sys->p_input, "position", val );
1748                                 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1749                                 break;
1750                             }
1751                             case POSITION_REL_FOR:
1752                             {
1753                                 var_Get( p_sys->p_input, "position", &val );
1754                                 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1755                                 var_Set( p_sys->p_input, "position", val );
1756                                 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1757                                 break;
1758                             }
1759                             case POSITION_REL_BACK:
1760                             {
1761                                 var_Get( p_sys->p_input, "position", &val );
1762                                 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1763                                 var_Set( p_sys->p_input, "position", val );
1764                                 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1765                                 break;
1766                             }
1767                             default:
1768                             {
1769                                 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1770                                 break;
1771                             }
1772                         }
1773                     }
1774 #undef POSITION_ABSOLUTE
1775 #undef POSITION_REL_FOR
1776 #undef POSITION_REL_BACK
1777 #undef TIME_ABSOLUTE
1778 #undef TIME_REL_FOR
1779 #undef TIME_REL_BACK
1780                     break;
1781                 }
1782                 case MVLC_VOLUME:
1783                 {
1784                     char vol[8];
1785                     audio_volume_t i_volume;
1786                     int i_value;
1787
1788                     uri_extract_value( p_request, "value", vol, 8 );
1789                     aout_VolumeGet( p_intf, &i_volume );
1790                     uri_decode_url_encoded( vol );
1791
1792                     if( vol[0] == '+' )
1793                     {
1794                         i_value = atoi( vol + 1 );
1795                         if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1796                         {
1797                             aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1798                             msg_Dbg( p_intf, "requested volume set: max" );
1799                         } else
1800                         {
1801                             aout_VolumeSet( p_intf , (i_volume + i_value) );
1802                             msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1803                         }
1804                     } else
1805                     if( vol[0] == '-' )
1806                     {
1807                         i_value = atoi( vol + 1 );
1808                         if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1809                         {
1810                             aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1811                             msg_Dbg( p_intf, "requested volume set: min" );
1812                         } else
1813                         {
1814                             aout_VolumeSet( p_intf , (i_volume - i_value) );
1815                             msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1816                         }
1817                     } else
1818                     if( strstr(vol, "%") != NULL )
1819                     {
1820                         i_value = atoi( vol );
1821                         if( (i_value <= 100) && (i_value>=0) ){
1822                             aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/100+AOUT_VOLUME_MIN);
1823                             msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
1824                         }
1825                     } else
1826                     {
1827                         i_value = atoi( vol );
1828                         if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1829                         {
1830                             aout_VolumeSet( p_intf , atoi( vol ) );
1831                             msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1832                         }
1833                     }
1834                     break;
1835                 }
1836
1837                 /* playlist management */
1838                 case MVLC_ADD:
1839                 {
1840                     char mrl[512];
1841                     playlist_item_t * p_item;
1842
1843                     uri_extract_value( p_request, "mrl", mrl, 512 );
1844                     uri_decode_url_encoded( mrl );
1845                     p_item = parse_MRL( p_intf, mrl );
1846
1847                     if( !p_item || !p_item->input.psz_uri ||
1848                         !*p_item->input.psz_uri )
1849                     {
1850                         msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1851                     } else
1852                     {
1853                         playlist_AddItem( p_sys->p_playlist , p_item ,
1854                                           PLAYLIST_APPEND, PLAYLIST_END );
1855                         msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1856                     }
1857
1858                     break;
1859                 }
1860                 case MVLC_DEL:
1861                 {
1862                     int i_item, *p_items = NULL, i_nb_items = 0;
1863                     char item[512], *p_parser = p_request;
1864
1865                     /* Get the list of items to delete */
1866                     while( (p_parser =
1867                             uri_extract_value( p_parser, "item", item, 512 )) )
1868                     {
1869                         if( !*item ) continue;
1870
1871                         i_item = atoi( item );
1872                         p_items = realloc( p_items, (i_nb_items + 1) *
1873                                            sizeof(int) );
1874                         p_items[i_nb_items] = i_item;
1875                         i_nb_items++;
1876                     }
1877
1878                     /* The items need to be deleted from in reversed order */
1879                     if( i_nb_items )
1880                     {
1881                         int i;
1882                         for( i = 0; i < i_nb_items; i++ )
1883                         {
1884                             int j, i_index = 0;
1885                             for( j = 0; j < i_nb_items; j++ )
1886                             {
1887                                 if( p_items[j] > p_items[i_index] )
1888                                     i_index = j;
1889                             }
1890
1891                             playlist_Delete( p_sys->p_playlist,
1892                                              p_items[i_index] );
1893                             msg_Dbg( p_intf, "requested playlist delete: %d",
1894                                      p_items[i_index] );
1895                             p_items[i_index] = -1;
1896                         }
1897                     }
1898
1899                     if( p_items ) free( p_items );
1900                     break;
1901                 }
1902                 case MVLC_KEEP:
1903                 {
1904                     int i_item, *p_items = NULL, i_nb_items = 0;
1905                     char item[512], *p_parser = p_request;
1906                     int i,j;
1907
1908                     /* Get the list of items to keep */
1909                     while( (p_parser =
1910                             uri_extract_value( p_parser, "item", item, 512 )) )
1911                     {
1912                         if( !*item ) continue;
1913
1914                         i_item = atoi( item );
1915                         p_items = realloc( p_items, (i_nb_items + 1) *
1916                                            sizeof(int) );
1917                         p_items[i_nb_items] = i_item;
1918                         i_nb_items++;
1919                     }
1920
1921                     /* The items need to be deleted from in reversed order */
1922                     for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
1923                     {
1924                         /* Check if the item is in the keep list */
1925                         for( j = 0 ; j < i_nb_items ; j++ )
1926                         {
1927                             if( p_items[j] == i ) break;
1928                         }
1929                         if( j == i_nb_items )
1930                         {
1931                             playlist_Delete( p_sys->p_playlist, i );
1932                             msg_Dbg( p_intf, "requested playlist delete: %d",
1933                                      i );
1934                         }
1935                     }
1936
1937                     if( p_items ) free( p_items );
1938                     break;
1939                 }
1940                 case MVLC_EMPTY:
1941                 {
1942                     while( p_sys->p_playlist->i_size > 0 )
1943                     {
1944                         playlist_Delete( p_sys->p_playlist, 0 );
1945                     }
1946                     msg_Dbg( p_intf, "requested playlist empty" );
1947                     break;
1948                 }
1949                 case MVLC_SORT:
1950                 {
1951                     char type[12];
1952                     char order[2];
1953                     int i_order;
1954
1955                     uri_extract_value( p_request, "type", type, 12 );
1956                     uri_extract_value( p_request, "order", order, 2 );
1957
1958                     if( order[0] == '0' ) i_order = ORDER_NORMAL;
1959                     else i_order = ORDER_REVERSE;
1960
1961                     if( !strcmp( type , "title" ) )
1962                     {
1963                         playlist_SortTitle( p_sys->p_playlist , i_order );
1964                         msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1965                     } else if( !strcmp( type , "author" ) )
1966                     {
1967                         playlist_SortAuthor( p_sys->p_playlist , i_order );
1968                         msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1969                     } else if( !strcmp( type , "shuffle" ) )
1970                     {
1971                         playlist_Sort( p_sys->p_playlist , SORT_RANDOM, ORDER_NORMAL );
1972                         msg_Dbg( p_intf, "requested playlist shuffle");
1973                     }
1974
1975                     break;
1976                 }
1977                 case MVLC_MOVE:
1978                 {
1979                     char psz_pos[6];
1980                     char psz_newpos[6];
1981                     int i_pos;
1982                     int i_newpos;
1983                     uri_extract_value( p_request, "psz_pos", psz_pos, 6 );
1984                     uri_extract_value( p_request, "psz_newpos", psz_newpos, 6 );
1985                     i_pos = atoi( psz_pos );
1986                     i_newpos = atoi( psz_newpos );
1987                     if ( i_pos < i_newpos )
1988                     {
1989                         playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
1990                     } else {
1991                         playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
1992                     }
1993                     msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
1994                     break;
1995                 }
1996
1997                 /* admin function */
1998                 case MVLC_CLOSE:
1999                 {
2000                     char id[512];
2001                     uri_extract_value( p_request, "id", id, 512 );
2002                     msg_Dbg( p_intf, "requested close id=%s", id );
2003 #if 0
2004                     if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
2005                     {
2006                         msg_Warn( p_intf, "close failed for id=%s", id );
2007                     }
2008 #endif
2009                     break;
2010                 }
2011                 case MVLC_SHUTDOWN:
2012                 {
2013                     msg_Dbg( p_intf, "requested shutdown" );
2014                     p_intf->p_vlc->b_die = VLC_TRUE;
2015                     break;
2016                 }
2017                 /* vlm */
2018                 case MVLC_VLM_NEW:
2019                 case MVLC_VLM_SETUP:
2020                 {
2021                     static const char *vlm_properties[11] =
2022                     {
2023                         /* no args */
2024                         "enabled", "disabled", "loop", "unloop",
2025                         /* args required */
2026                         "input", "output", "option", "date", "period", "repeat", "append",
2027                     };
2028                     vlm_message_t *vlm_answer;
2029                     char name[512];
2030                     char *psz = malloc( strlen( p_request ) + 1000 );
2031                     char *p = psz;
2032                     char *vlm_error;
2033                     int i;
2034
2035                     if( p_intf->p_sys->p_vlm == NULL )
2036                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
2037
2038                     if( p_intf->p_sys->p_vlm == NULL ) break;
2039
2040                     uri_extract_value( p_request, "name", name, 512 );
2041                     if( StrToMacroType( control ) == MVLC_VLM_NEW )
2042                     {
2043                         char type[20];
2044                         uri_extract_value( p_request, "type", type, 20 );
2045                         p += sprintf( psz, "new %s %s", name, type );
2046                     }
2047                     else
2048                     {
2049                         p += sprintf( psz, "setup %s", name );
2050                     }
2051                     /* Parse the request */
2052                     for( i = 0; i < 11; i++ )
2053                     {
2054                         char val[512];
2055                         uri_extract_value( p_request, vlm_properties[i], val, 512 );
2056                         uri_decode_url_encoded( val );
2057                         if( strlen( val ) > 0 && i >= 4 )
2058                         {
2059                             p += sprintf( p, " %s %s", vlm_properties[i], val );
2060                         }
2061                         else if( uri_test_param( p_request, vlm_properties[i] ) && i < 4 )
2062                         {
2063                             p += sprintf( p, " %s", vlm_properties[i] );
2064                         }
2065                     }
2066                     fprintf( stderr, "vlm_ExecuteCommand: %s\n", psz );
2067                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2068                     if( vlm_answer->psz_value == NULL ) /* there is no error */
2069                     {
2070                         vlm_error = strdup( "" );
2071                     }
2072                     else
2073                     {
2074                         vlm_error = malloc( strlen(vlm_answer->psz_name) +
2075                                             strlen(vlm_answer->psz_value) +
2076                                             strlen( " : ") + 1 );
2077                         sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
2078                                                          vlm_answer->psz_value );
2079                     }
2080
2081                     mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
2082
2083                     vlm_MessageDelete( vlm_answer );
2084                     free( vlm_error );
2085                     free( psz );
2086                     break;
2087                 }
2088
2089                 case MVLC_VLM_DEL:
2090                 {
2091                     vlm_message_t *vlm_answer;
2092                     char name[512];
2093                     char psz[512+10];
2094                     if( p_intf->p_sys->p_vlm == NULL )
2095                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
2096
2097                     if( p_intf->p_sys->p_vlm == NULL ) break;
2098
2099                     uri_extract_value( p_request, "name", name, 512 );
2100                     sprintf( psz, "del %s", name );
2101
2102                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2103                     /* FIXME do a vlm_answer -> var stack conversion */
2104                     vlm_MessageDelete( vlm_answer );
2105                     break;
2106                 }
2107
2108                 case MVLC_VLM_PLAY:
2109                 case MVLC_VLM_PAUSE:
2110                 case MVLC_VLM_STOP:
2111                 case MVLC_VLM_SEEK:
2112                 {
2113                     vlm_message_t *vlm_answer;
2114                     char name[512];
2115                     char psz[512+10];
2116                     if( p_intf->p_sys->p_vlm == NULL )
2117                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
2118
2119                     if( p_intf->p_sys->p_vlm == NULL ) break;
2120
2121                     uri_extract_value( p_request, "name", name, 512 );
2122                     if( StrToMacroType( control ) == MVLC_VLM_PLAY )
2123                         sprintf( psz, "control %s play", name );
2124                     else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
2125                         sprintf( psz, "control %s pause", name );
2126                     else if( StrToMacroType( control ) == MVLC_VLM_STOP )
2127                         sprintf( psz, "control %s stop", name );
2128                     else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
2129                     {
2130                         char percent[20];
2131                         uri_extract_value( p_request, "percent", percent, 512 );
2132                         sprintf( psz, "control %s seek %s", name, percent );
2133                     }
2134
2135                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2136                     /* FIXME do a vlm_answer -> var stack conversion */
2137                     vlm_MessageDelete( vlm_answer );
2138                     break;
2139                 }
2140                 case MVLC_VLM_LOAD:
2141                 case MVLC_VLM_SAVE:
2142                 {
2143                     vlm_message_t *vlm_answer;
2144                     char file[512];
2145                     char psz[512];
2146
2147                     if( p_intf->p_sys->p_vlm == NULL )
2148                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
2149
2150                     if( p_intf->p_sys->p_vlm == NULL ) break;
2151
2152                     uri_extract_value( p_request, "file", file, 512 );
2153                     uri_decode_url_encoded( file );
2154
2155                     if( StrToMacroType( control ) == MVLC_VLM_LOAD )
2156                         sprintf( psz, "load %s", file );
2157                     else
2158                         sprintf( psz, "save %s", file );
2159
2160                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2161                     /* FIXME do a vlm_answer -> var stack conversion */
2162                     vlm_MessageDelete( vlm_answer );
2163                     break;
2164                 }
2165
2166                 default:
2167                     if( *control )
2168                     {
2169                         PRINTS( "<!-- control param(%s) unsuported -->", control );
2170                     }
2171                     break;
2172             }
2173             break;
2174
2175         case MVLC_SET:
2176         {
2177             char    value[512];
2178             int     i;
2179             float   f;
2180
2181             if( i_request <= 0 ||
2182                 *m->param1  == '\0' ||
2183                 strstr( p_request, m->param1 ) == NULL )
2184             {
2185                 break;
2186             }
2187             uri_extract_value( p_request, m->param1,  value, 512 );
2188             uri_decode_url_encoded( value );
2189
2190             switch( StrToMacroType( m->param2 ) )
2191             {
2192                 case MVLC_INT:
2193                     i = atoi( value );
2194                     config_PutInt( p_intf, m->param1, i );
2195                     break;
2196                 case MVLC_FLOAT:
2197                     f = atof( value );
2198                     config_PutFloat( p_intf, m->param1, f );
2199                     break;
2200                 case MVLC_STRING:
2201                     config_PutPsz( p_intf, m->param1, value );
2202                     break;
2203                 default:
2204                     PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
2205             }
2206             break;
2207         }
2208         case MVLC_GET:
2209         {
2210             char    value[512];
2211             int     i;
2212             float   f;
2213             char    *psz;
2214
2215             if( *m->param1  == '\0' )
2216             {
2217                 break;
2218             }
2219
2220             switch( StrToMacroType( m->param2 ) )
2221             {
2222                 case MVLC_INT:
2223                     i = config_GetInt( p_intf, m->param1 );
2224                     sprintf( value, "%i", i );
2225                     break;
2226                 case MVLC_FLOAT:
2227                     f = config_GetFloat( p_intf, m->param1 );
2228                     sprintf( value, "%f", f );
2229                     break;
2230                 case MVLC_STRING:
2231                     psz = config_GetPsz( p_intf, m->param1 );
2232                     sprintf( value, "%s", psz ? psz : "" );
2233                     if( psz ) free( psz );
2234                     break;
2235                 default:
2236                     sprintf( value, "invalid type(%s) in set", m->param2 );
2237                     break;
2238             }
2239             msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
2240             PRINTS( "%s", value );
2241             break;
2242         }
2243         case MVLC_VALUE:
2244         {
2245             char *s, *v;
2246
2247             if( m->param1 )
2248             {
2249                 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2250                 s = SSPop( &p_args->stack );
2251                 v = mvar_GetValue( p_args->vars, s );
2252             }
2253             else
2254             {
2255                 v = s = SSPop( &p_args->stack );
2256             }
2257
2258             PRINTS( "%s", v );
2259             free( s );
2260             break;
2261         }
2262         case MVLC_RPN:
2263             EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2264             break;
2265
2266         case MVLC_UNKNOWN:
2267         default:
2268             PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2269             msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2270             break;
2271     }
2272 #undef PRINTS
2273 #undef PRINT
2274 #undef ALLOC
2275 }
2276
2277 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2278 {
2279     int     i_id;
2280     int     i_level = 0;
2281
2282     while( src < end )
2283     {
2284         if( src + 4 < end  && !strncmp( src, "<vlc", 4 ) )
2285         {
2286             int i_skip;
2287             macro_t m;
2288
2289             i_skip = MacroParse( &m, src );
2290
2291             i_id = StrToMacroType( m.id );
2292
2293             switch( i_id )
2294             {
2295                 case MVLC_IF:
2296                 case MVLC_FOREACH:
2297                     i_level++;
2298                     break;
2299                 case MVLC_END:
2300                     i_level--;
2301                     break;
2302                 default:
2303                     break;
2304             }
2305
2306             MacroClean( &m );
2307
2308             if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2309                 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2310             {
2311                 return src + ( b_after ? i_skip : 0 );
2312             }
2313             else if( i_level < 0 )
2314             {
2315                 return NULL;
2316             }
2317
2318             src += i_skip;
2319         }
2320         else
2321         {
2322             src++;
2323         }
2324     }
2325
2326     return NULL;
2327 }
2328
2329 static void Execute( httpd_file_sys_t *p_args,
2330                      uint8_t *p_request, int i_request,
2331                      uint8_t **pp_data, int *pi_data,
2332                      uint8_t **pp_dst,
2333                      uint8_t *_src, uint8_t *_end )
2334 {
2335     intf_thread_t  *p_intf = p_args->p_intf;
2336
2337     uint8_t *src, *dup, *end;
2338     uint8_t *dst = *pp_dst;
2339
2340     src = dup = malloc( _end - _src + 1 );
2341     end = src +( _end - _src );
2342
2343     memcpy( src, _src, _end - _src );
2344     *end = '\0';
2345
2346     /* we parse searching <vlc */
2347     while( src < end )
2348     {
2349         uint8_t *p;
2350         int i_copy;
2351
2352         p = strstr( src, "<vlc" );
2353         if( p < end && p == src )
2354         {
2355             macro_t m;
2356
2357             src += MacroParse( &m, src );
2358
2359             //msg_Dbg( p_intf, "macro_id=%s", m.id );
2360
2361             switch( StrToMacroType( m.id ) )
2362             {
2363                 case MVLC_IF:
2364                 {
2365                     vlc_bool_t i_test;
2366                     uint8_t    *endif;
2367
2368                     EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2369                     if( SSPopN( &p_args->stack, p_args->vars ) )
2370                     {
2371                         i_test = 1;
2372                     }
2373                     else
2374                     {
2375                         i_test = 0;
2376                     }
2377                     endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2378
2379                     if( i_test == 0 )
2380                     {
2381                         uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2382
2383                         if( start )
2384                         {
2385                             uint8_t *stop  = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2386                             if( stop )
2387                             {
2388                                 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2389                             }
2390                         }
2391                     }
2392                     else if( i_test == 1 )
2393                     {
2394                         uint8_t *stop;
2395                         if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2396                         {
2397                             stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2398                         }
2399                         if( stop )
2400                         {
2401                             Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2402                         }
2403                     }
2404
2405                     src = endif;
2406                     break;
2407                 }
2408                 case MVLC_FOREACH:
2409                 {
2410                     uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2411                     uint8_t *start = src;
2412                     uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2413
2414                     if( stop )
2415                     {
2416                         mvar_t *index;
2417                         int    i_idx;
2418                         mvar_t *v;
2419                         if( !strcmp( m.param2, "integer" ) )
2420                         {
2421                             char *arg = SSPop( &p_args->stack );
2422                             index = mvar_IntegerSetNew( m.param1, arg );
2423                             free( arg );
2424                         }
2425                         else if( !strcmp( m.param2, "directory" ) )
2426                         {
2427                             char *arg = SSPop( &p_args->stack );
2428                             index = mvar_FileSetNew( m.param1, arg );
2429                             free( arg );
2430                         }
2431                         else if( !strcmp( m.param2, "playlist" ) )
2432                         {
2433                             index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2434                         }
2435                         else if( !strcmp( m.param2, "information" ) )
2436                         {
2437                             index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2438                         }
2439                         else if( !strcmp( m.param2, "vlm" ) )
2440                         {
2441                             if( p_intf->p_sys->p_vlm == NULL )
2442                                 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2443                             index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
2444                         }
2445 #if 0
2446                         else if( !strcmp( m.param2, "hosts" ) )
2447                         {
2448                             index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2449                         }
2450                         else if( !strcmp( m.param2, "urls" ) )
2451                         {
2452                             index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2453                         }
2454                         else if( !strcmp( m.param2, "connections" ) )
2455                         {
2456                             index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2457                         }
2458 #endif
2459                         else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2460                         {
2461                             index = mvar_Duplicate( v );
2462                         }
2463                         else
2464                         {
2465                             msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2466                             src = endfor;
2467                             break;
2468                         }
2469
2470                         for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2471                         {
2472                             mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2473
2474                             //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2475
2476                             free( f->name );
2477                             f->name = strdup( m.param1 );
2478
2479
2480                             mvar_PushVar( p_args->vars, f );
2481                             Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2482                             mvar_RemoveVar( p_args->vars, f );
2483
2484                             mvar_Delete( f );
2485                         }
2486                         mvar_Delete( index );
2487
2488                         src = endfor;
2489                     }
2490                     break;
2491                 }
2492                 default:
2493                     MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2494                     break;
2495             }
2496
2497             MacroClean( &m );
2498             continue;
2499         }
2500
2501         i_copy =   ( (p == NULL || p > end ) ? end : p  ) - src;
2502         if( i_copy > 0 )
2503         {
2504             int i_index = dst - *pp_data;
2505
2506             *pi_data += i_copy;
2507             *pp_data = realloc( *pp_data, *pi_data );
2508             dst = (*pp_data) + i_index;
2509
2510             memcpy( dst, src, i_copy );
2511             dst += i_copy;
2512             src += i_copy;
2513         }
2514     }
2515
2516     *pp_dst = dst;
2517     free( dup );
2518 }
2519
2520 /****************************************************************************
2521  * HttpCallback:
2522  ****************************************************************************
2523  * a file with b_html is parsed and all "macro" replaced
2524  * <vlc id="macro name" [param1="" [param2=""]] />
2525  * valid id are
2526  *
2527  ****************************************************************************/
2528 static int  HttpCallback( httpd_file_sys_t *p_args,
2529                           httpd_file_t *p_file,
2530                           uint8_t *p_request,
2531                           uint8_t **pp_data, int *pi_data )
2532 {
2533     int i_request = p_request ? strlen( p_request ) : 0;
2534     char *p;
2535     FILE *f;
2536
2537     if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2538     {
2539         p = *pp_data = malloc( 10240 );
2540         if( !p )
2541         {
2542                 return VLC_EGENERIC;
2543         }
2544         p += sprintf( p, "<html>\n" );
2545         p += sprintf( p, "<head>\n" );
2546         p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2547         p += sprintf( p, "</head>\n" );
2548         p += sprintf( p, "<body>\n" );
2549         p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2550         p += sprintf( p, "<hr />\n" );
2551         p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2552         p += sprintf( p, "</body>\n" );
2553         p += sprintf( p, "</html>\n" );
2554
2555         *pi_data = strlen( *pp_data );
2556
2557         return VLC_SUCCESS;
2558     }
2559
2560     if( !p_args->b_html )
2561     {
2562         FileLoad( f, pp_data, pi_data );
2563     }
2564     else
2565     {
2566         int  i_buffer;
2567         uint8_t *p_buffer;
2568         uint8_t *dst;
2569         vlc_value_t val;
2570         char position[4]; /* percentage */
2571         char time[12]; /* in seconds */
2572         char length[12]; /* in seconds */
2573         audio_volume_t i_volume;
2574         char volume[5];
2575         char state[8];
2576  
2577 #define p_sys p_args->p_intf->p_sys
2578         if( p_sys->p_input )
2579         {
2580             var_Get( p_sys->p_input, "position", &val);
2581             sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2582             var_Get( p_sys->p_input, "time", &val);
2583             sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2584             var_Get( p_sys->p_input, "length", &val);
2585             sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2586
2587             var_Get( p_sys->p_input, "state", &val );
2588             if( val.i_int == PLAYING_S )
2589             {
2590                 sprintf( state, "playing" );
2591             } else if( val.i_int == PAUSE_S )
2592             {
2593                 sprintf( state, "paused" );
2594             } else
2595             {
2596                 sprintf( state, "stop" );
2597             }
2598         } else
2599         {
2600             sprintf( position, "%d", 0 );
2601             sprintf( time, "%d", 0 );
2602             sprintf( length, "%d", 0 );
2603             sprintf( state, "stop" );
2604         }
2605 #undef p_sys
2606
2607         aout_VolumeGet( p_args->p_intf , &i_volume );
2608         sprintf( volume , "%d" , (int)i_volume );
2609
2610         p_args->vars = mvar_New( "variables", "" );
2611         mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2612         mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2613         mvar_AppendNewVar( p_args->vars, "version",   VERSION_MESSAGE );
2614         mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2615         mvar_AppendNewVar( p_args->vars, "stream_position", position );
2616         mvar_AppendNewVar( p_args->vars, "stream_time", time );
2617         mvar_AppendNewVar( p_args->vars, "stream_length", length );
2618         mvar_AppendNewVar( p_args->vars, "volume", volume );
2619         mvar_AppendNewVar( p_args->vars, "stream_state", state );
2620
2621         SSInit( &p_args->stack );
2622
2623         /* first we load in a temporary buffer */
2624         FileLoad( f, &p_buffer, &i_buffer );
2625
2626         /* allocate output */
2627         *pi_data = i_buffer + 1000;
2628         dst = *pp_data = malloc( *pi_data );
2629
2630         /* we parse executing all  <vlc /> macros */
2631         Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2632
2633         *dst     = '\0';
2634         *pi_data = dst - *pp_data;
2635
2636         SSClean( &p_args->stack );
2637         mvar_Delete( p_args->vars );
2638         free( p_buffer );
2639     }
2640
2641     fclose( f );
2642
2643     return VLC_SUCCESS;
2644 }
2645
2646 /****************************************************************************
2647  * uri parser
2648  ****************************************************************************/
2649 static int uri_test_param( char *psz_uri, const char *psz_name )
2650 {
2651     char *p = psz_uri;
2652
2653     while( (p = strstr( p, psz_name )) )
2654     {
2655         /* Verify that we are dealing with a post/get argument */
2656         if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2657         {
2658             return VLC_TRUE;
2659         }
2660         p++;
2661     }
2662
2663     return VLC_FALSE;
2664 }
2665 static char *uri_extract_value( char *psz_uri, const char *psz_name,
2666                                 char *psz_value, int i_value_max )
2667 {
2668     char *p = psz_uri;
2669
2670     while( (p = strstr( p, psz_name )) )
2671     {
2672         /* Verify that we are dealing with a post/get argument */
2673         if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2674             break;
2675         p++;
2676     }
2677
2678     if( p )
2679     {
2680         int i_len;
2681
2682         p += strlen( psz_name );
2683         if( *p == '=' ) p++;
2684
2685         if( strchr( p, '&' ) )
2686         {
2687             i_len = strchr( p, '&' ) - p;
2688         }
2689         else
2690         {
2691             /* for POST method */
2692             if( strchr( p, '\n' ) )
2693             {
2694                 i_len = strchr( p, '\n' ) - p;
2695                 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2696             }
2697             else
2698             {
2699                 i_len = strlen( p );
2700             }
2701         }
2702         i_len = __MIN( i_value_max - 1, i_len );
2703         if( i_len > 0 )
2704         {
2705             strncpy( psz_value, p, i_len );
2706             psz_value[i_len] = '\0';
2707         }
2708         else
2709         {
2710             strncpy( psz_value, "", i_value_max );
2711         }
2712         p += i_len;
2713     }
2714     else
2715     {
2716         strncpy( psz_value, "", i_value_max );
2717     }
2718
2719     return p;
2720 }
2721
2722 static void uri_decode_url_encoded( char *psz )
2723 {
2724     char *dup = strdup( psz );
2725     char *p = dup;
2726
2727     while( *p )
2728     {
2729         if( *p == '%' )
2730         {
2731             char val[3];
2732             p++;
2733             if( !*p )
2734             {
2735                 break;
2736             }
2737
2738             val[0] = *p++;
2739             val[1] = *p++;
2740             val[2] = '\0';
2741
2742             *psz++ = strtol( val, NULL, 16 );
2743         }
2744         else if( *p == '+' )
2745         {
2746             *psz++ = ' ';
2747             p++;
2748         }
2749         else
2750         {
2751             *psz++ = *p++;
2752         }
2753     }
2754     *psz++  ='\0';
2755     free( dup );
2756 }
2757
2758 /****************************************************************************
2759  * Light RPN evaluator
2760  ****************************************************************************/
2761 static void SSInit( rpn_stack_t *st )
2762 {
2763     st->i_stack = 0;
2764 }
2765
2766 static void SSClean( rpn_stack_t *st )
2767 {
2768     while( st->i_stack > 0 )
2769     {
2770         free( st->stack[--st->i_stack] );
2771     }
2772 }
2773
2774 static void SSPush( rpn_stack_t *st, char *s )
2775 {
2776     if( st->i_stack < STACK_MAX )
2777     {
2778         st->stack[st->i_stack++] = strdup( s );
2779     }
2780 }
2781
2782 static char * SSPop( rpn_stack_t *st )
2783 {
2784     if( st->i_stack <= 0 )
2785     {
2786         return strdup( "" );
2787     }
2788     else
2789     {
2790         return st->stack[--st->i_stack];
2791     }
2792 }
2793
2794 static int SSPopN( rpn_stack_t *st, mvar_t  *vars )
2795 {
2796     char *name;
2797     char *value;
2798
2799     char *end;
2800     int  i;
2801
2802     name = SSPop( st );
2803     i = strtol( name, &end, 0 );
2804     if( end == name )
2805     {
2806         value = mvar_GetValue( vars, name );
2807         i = atoi( value );
2808     }
2809     free( name );
2810
2811     return( i );
2812 }
2813
2814 static void SSPushN( rpn_stack_t *st, int i )
2815 {
2816     char v[512];
2817
2818     sprintf( v, "%d", i );
2819     SSPush( st, v );
2820 }
2821
2822 static void  EvaluateRPN( mvar_t  *vars, rpn_stack_t *st, char *exp )
2823 {
2824     for( ;; )
2825     {
2826         char s[100], *p;
2827
2828         /* skip spcae */
2829         while( *exp == ' ' )
2830         {
2831             exp++;
2832         }
2833
2834         if( *exp == '\'' )
2835         {
2836             /* extract string */
2837             p = &s[0];
2838             exp++;
2839             while( *exp && *exp != '\'' )
2840             {
2841                 *p++ = *exp++;
2842             }
2843             *p = '\0';
2844             exp++;
2845             SSPush( st, s );
2846             continue;
2847         }
2848
2849         /* extract token */
2850         p = strchr( exp, ' ' );
2851         if( !p )
2852         {
2853             strcpy( s, exp );
2854
2855             exp += strlen( exp );
2856         }
2857         else
2858         {
2859             int i = p -exp;
2860             strncpy( s, exp, i );
2861             s[i] = '\0';
2862
2863             exp = p + 1;
2864         }
2865
2866         if( *s == '\0' )
2867         {
2868             break;
2869         }
2870
2871         /* 1. Integer function */
2872         if( !strcmp( s, "!" ) )
2873         {
2874             SSPushN( st, ~SSPopN( st, vars ) );
2875         }
2876         else if( !strcmp( s, "^" ) )
2877         {
2878             SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2879         }
2880         else if( !strcmp( s, "&" ) )
2881         {
2882             SSPushN( st, SSPopN( st, vars ) & 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             int j = SSPopN( st, vars );
2895             int i = SSPopN( st, vars );
2896             SSPushN( st, i - j );
2897         }
2898         else if( !strcmp( s, "*" ) )
2899         {
2900             SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2901         }
2902         else if( !strcmp( s, "/" ) )
2903         {
2904             int i, j;
2905
2906             j = SSPopN( st, vars );
2907             i = SSPopN( st, vars );
2908
2909             SSPushN( st, j != 0 ? i / j : 0 );
2910         }
2911         else if( !strcmp( s, "%" ) )
2912         {
2913             int i, j;
2914
2915             j = SSPopN( st, vars );
2916             i = SSPopN( st, vars );
2917
2918             SSPushN( st, j != 0 ? i % j : 0 );
2919         }
2920         /* 2. integer tests */
2921         else if( !strcmp( s, "=" ) )
2922         {
2923             SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2924         }
2925         else if( !strcmp( s, "<" ) )
2926         {
2927             int j = SSPopN( st, vars );
2928             int i = SSPopN( st, vars );
2929
2930             SSPushN( st, i < j ? -1 : 0 );
2931         }
2932         else if( !strcmp( s, ">" ) )
2933         {
2934             int j = SSPopN( st, vars );
2935             int i = SSPopN( st, vars );
2936
2937             SSPushN( st, i > j ? -1 : 0 );
2938         }
2939         else if( !strcmp( s, "<=" ) )
2940         {
2941             int j = SSPopN( st, vars );
2942             int i = SSPopN( st, vars );
2943
2944             SSPushN( st, i <= j ? -1 : 0 );
2945         }
2946         else if( !strcmp( s, ">=" ) )
2947         {
2948             int j = SSPopN( st, vars );
2949             int i = SSPopN( st, vars );
2950
2951             SSPushN( st, i >= j ? -1 : 0 );
2952         }
2953         /* 3. string functions */
2954         else if( !strcmp( s, "strcat" ) )
2955         {
2956             char *s2 = SSPop( st );
2957             char *s1 = SSPop( st );
2958             char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2959
2960             strcpy( str, s1 );
2961             strcat( str, s2 );
2962
2963             SSPush( st, str );
2964             free( s1 );
2965             free( s2 );
2966             free( str );
2967         }
2968         else if( !strcmp( s, "strcmp" ) )
2969         {
2970             char *s2 = SSPop( st );
2971             char *s1 = SSPop( st );
2972
2973             SSPushN( st, strcmp( s1, s2 ) );
2974             free( s1 );
2975             free( s2 );
2976         }
2977         else if( !strcmp( s, "strncmp" ) )
2978         {
2979             int n = SSPopN( st, vars );
2980             char *s2 = SSPop( st );
2981             char *s1 = SSPop( st );
2982
2983             SSPushN( st, strncmp( s1, s2 , n ) );
2984             free( s1 );
2985             free( s2 );
2986         }
2987         else if( !strcmp( s, "strsub" ) )
2988         {
2989             int n = SSPopN( st, vars );
2990             int m = SSPopN( st, vars );
2991             int i_len;
2992             char *s = SSPop( st );
2993             char *str;
2994
2995             if( n >= m )
2996             {
2997                 i_len = n - m + 1;
2998             }
2999             else
3000             {
3001                 i_len = 0;
3002             }
3003
3004             str = malloc( i_len + 1 );
3005
3006             memcpy( str, s + m - 1, i_len );
3007             str[ i_len ] = '\0';
3008
3009             SSPush( st, str );
3010             free( s );
3011             free( str );
3012         }
3013        else if( !strcmp( s, "strlen" ) )
3014         {
3015             char *str = SSPop( st );
3016
3017             SSPushN( st, strlen( str ) );
3018             free( str );
3019         }
3020         /* 4. stack functions */
3021         else if( !strcmp( s, "dup" ) )
3022         {
3023             char *str = SSPop( st );
3024             SSPush( st, str );
3025             SSPush( st, str );
3026             free( str );
3027         }
3028         else if( !strcmp( s, "drop" ) )
3029         {
3030             char *str = SSPop( st );
3031             free( str );
3032         }
3033         else if( !strcmp( s, "swap" ) )
3034         {
3035             char *s1 = SSPop( st );
3036             char *s2 = SSPop( st );
3037
3038             SSPush( st, s1 );
3039             SSPush( st, s2 );
3040             free( s1 );
3041             free( s2 );
3042         }
3043         else if( !strcmp( s, "flush" ) )
3044         {
3045             SSClean( st );
3046             SSInit( st );
3047         }
3048         else if( !strcmp( s, "store" ) )
3049         {
3050             char *value = SSPop( st );
3051             char *name  = SSPop( st );
3052
3053             mvar_PushNewVar( vars, name, value );
3054             free( name );
3055             free( value );
3056         }
3057         else if( !strcmp( s, "value" ) )
3058         {
3059             char *name  = SSPop( st );
3060             char *value = mvar_GetValue( vars, name );
3061
3062             SSPush( st, value );
3063
3064             free( name );
3065         }
3066         else if( !strcmp( s, "url_extract" ) )
3067         {
3068             char *url = mvar_GetValue( vars, "url_value" );
3069             char *name = SSPop( st );
3070             char value[512];
3071
3072             uri_extract_value( url, name, value, 512 );
3073             uri_decode_url_encoded( value );
3074             SSPush( st, value );
3075         }
3076         else
3077         {
3078             SSPush( st, s );
3079         }
3080     }
3081 }
3082
3083 /**********************************************************************
3084  * Find_end_MRL: Find the end of the sentence :
3085  * this function parses the string psz and find the end of the item
3086  * and/or option with detecting the " and ' problems.
3087  * returns NULL if an error is detected, otherwise, returns a pointer
3088  * of the end of the sentence (after the last character)
3089  **********************************************************************/
3090 static char *Find_end_MRL( char *psz )
3091 {
3092     char *s_sent = psz;
3093
3094     switch( *s_sent )
3095     {
3096         case '\"':
3097         {
3098             s_sent++;
3099
3100             while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
3101             {
3102                 if( *s_sent == '\'' )
3103                 {
3104                     s_sent = Find_end_MRL( s_sent );
3105
3106                     if( s_sent == NULL )
3107                     {
3108                         return NULL;
3109                     }
3110                 } else
3111                 {
3112                     s_sent++;
3113                 }
3114             }
3115
3116             if( *s_sent == '\"' )
3117             {
3118                 s_sent++;
3119                 return s_sent;
3120             } else  /* *s_sent == '\0' , which means the number of " is incorrect */
3121             {
3122                 return NULL;
3123             }
3124             break;
3125         }
3126         case '\'':
3127         {
3128             s_sent++;
3129
3130             while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
3131             {
3132                 if( *s_sent == '\"' )
3133                 {
3134                     s_sent = Find_end_MRL( s_sent );
3135
3136                     if( s_sent == NULL )
3137                     {
3138                         return NULL;
3139                     }
3140                 } else
3141                 {
3142                     s_sent++;
3143                 }
3144             }
3145
3146             if( *s_sent == '\'' )
3147             {
3148                 s_sent++;
3149                 return s_sent;
3150             } else  /* *s_sent == '\0' , which means the number of ' is incorrect */
3151             {
3152                 return NULL;
3153             }
3154             break;
3155         }
3156         default: /* now we can look for spaces */
3157         {
3158             while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
3159             {
3160                 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
3161                 {
3162                     s_sent = Find_end_MRL( s_sent );
3163                 } else
3164                 {
3165                     s_sent++;
3166                 }
3167             }
3168             return s_sent;
3169         }
3170     }
3171 }
3172
3173 /**********************************************************************
3174  * parse_MRL: parse the MRL, find the mrl string and the options,
3175  * create an item with all information in it, and return the item.
3176  * return NULL if there is an error.
3177  **********************************************************************/
3178 playlist_item_t * parse_MRL( intf_thread_t *p_intf, char *psz )
3179 {
3180     char **ppsz_options = NULL;
3181     char *mrl;
3182     char *s_mrl = psz;
3183     int i_error = 0;
3184     char *s_temp;
3185     int i = 0;
3186     int i_options = 0;
3187     playlist_item_t * p_item = NULL;
3188
3189     /* In case there is spaces before the mrl */
3190     while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
3191     {
3192         s_mrl++;
3193     }
3194
3195     /* extract the mrl */
3196     s_temp = strstr( s_mrl , " :" );
3197     if( s_temp == NULL )
3198     {
3199         s_temp = s_mrl + strlen( s_mrl );
3200     } else
3201     {
3202         while( (*s_temp == ' ') && (s_temp != s_mrl ) )
3203         {
3204             s_temp--;
3205         }
3206         s_temp++;
3207     }
3208
3209     /* if the mrl is between " or ', we must remove them */
3210     if( (*s_mrl == '\'') || (*s_mrl == '\"') )
3211     {
3212         mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
3213         strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
3214         mrl[ s_temp - s_mrl - 2 ] = '\0';
3215     } else
3216     {
3217         mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
3218         strncpy( mrl , s_mrl , s_temp - s_mrl );
3219         mrl[ s_temp - s_mrl ] = '\0';
3220     }
3221
3222     s_mrl = s_temp;
3223
3224     /* now we can take care of the options */
3225     while( (*s_mrl != '\0') && (i_error == 0) )
3226     {
3227         switch( *s_mrl )
3228         {
3229             case ' ':
3230             {
3231                 s_mrl++;
3232                 break;
3233             }
3234             case ':': /* an option */
3235             {
3236                 s_temp = Find_end_MRL( s_mrl );
3237
3238                 if( s_temp == NULL )
3239                 {
3240                     i_error = 1;
3241                 }
3242                 else
3243                 {
3244                     i_options++;
3245                     ppsz_options = realloc( ppsz_options , i_options *
3246                                             sizeof(char *) );
3247                     ppsz_options[ i_options - 1 ] =
3248                         malloc( (s_temp - s_mrl + 1) * sizeof(char) );
3249
3250                     strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
3251                              s_temp - s_mrl );
3252
3253                     /* don't forget to finish the string with a '\0' */
3254                     (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
3255
3256                     s_mrl = s_temp;
3257                 }
3258                 break;
3259             }
3260             default:
3261             {
3262                 i_error = 1;
3263                 break;
3264             }
3265         }
3266     }
3267
3268     if( i_error != 0 )
3269     {
3270         free( mrl );
3271     }
3272     else
3273     {
3274         /* now create an item */
3275         p_item = playlist_ItemNew( p_intf, mrl, mrl);
3276         for( i = 0 ; i< i_options ; i++ )
3277         {
3278             playlist_ItemAddOption( p_item, ppsz_options[i] );
3279         }
3280     }
3281
3282     for( i = 0 ; i < i_options ; i++ )
3283     {
3284         free( ppsz_options[i] );
3285     }
3286     free( ppsz_options );
3287
3288     return p_item;
3289 }