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