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