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