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