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