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