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