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