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