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