1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
38 #include <vlc/vout.h> /* for fullscreen */
40 #include "vlc_httpd.h"
45 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
57 #elif defined( WIN32 ) && !defined( UNDER_CE )
65 /* stat() support for large files on win32 */
66 #if defined( WIN32 ) && !defined( UNDER_CE )
67 # define stat _stati64
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Open ( vlc_object_t * );
74 static void Close( vlc_object_t * );
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 " \
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 " \
89 #define CRL_TEXT N_( "CRL file" )
90 #define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file" )
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 );
109 /*****************************************************************************
111 *****************************************************************************/
112 static void Run ( intf_thread_t *p_intf );
114 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
117 static int DirectoryCheck( char *psz_dir )
121 #ifdef HAVE_SYS_STAT_H
122 struct stat stat_info;
124 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
130 if( ( p_dir = opendir( psz_dir ) ) == NULL )
139 static int HttpCallback( httpd_file_sys_t *p_args,
142 uint8_t **pp_data, int *pi_data );
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 );
148 static void uri_decode_url_encoded( char *psz );
150 static char *Find_end_MRL( char *psz );
151 static playlist_item_t *parse_MRL( intf_thread_t * , char *psz );
153 /*****************************************************************************
155 *****************************************************************************/
156 typedef struct mvar_s
162 struct mvar_s **field;
165 #define STACK_MAX 100
168 char *stack[STACK_MAX];
172 struct httpd_file_sys_t
174 intf_thread_t *p_intf;
175 httpd_file_t *p_file;
176 httpd_redirect_t *p_redir;
177 httpd_redirect_t *p_redir2;
184 /* inited for each access */
191 httpd_host_t *p_httpd_host;
194 httpd_file_sys_t **pp_files;
196 playlist_t *p_playlist;
197 input_thread_t *p_input;
204 /*****************************************************************************
205 * Activate: initialize and create stuff
206 *****************************************************************************/
207 static int Open( vlc_object_t *p_this )
209 intf_thread_t *p_intf = (intf_thread_t*)p_this;
212 char *psz_address = "";
213 const char *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
218 psz_host = config_GetPsz( p_intf, "http-host" );
222 psz_address = psz_host;
224 psz_parser = strchr( psz_host, ':' );
227 *psz_parser++ = '\0';
228 i_port = atoi( psz_parser );
232 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
235 return( VLC_ENOMEM );
237 p_sys->p_playlist = NULL;
238 p_sys->p_input = NULL;
241 /* determine Content-Type value for HTML pages */
242 vlc_current_charset(&psz_src);
243 if( psz_src == NULL )
248 p_sys->psz_html_type = malloc( 20 + strlen( psz_src ) );
249 if( p_sys->psz_html_type == NULL )
255 sprintf( p_sys->psz_html_type, "text/html; charset=%s", psz_src );
258 /* determine SSL configuration */
259 psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
260 if ( psz_cert != NULL )
262 msg_Dbg( p_intf, "enablind TLS for HTTP interface (cert file: %s)",
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" );
277 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
279 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
280 i_port, psz_cert, psz_key, psz_ca,
282 if( p_sys->p_httpd_host == NULL )
284 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
285 free( p_sys->psz_html_type );
296 p_sys->pp_files = NULL;
298 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || defined(WIN32)
299 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
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;
305 sprintf( psz_src, "%s/http", psz_vlcpath);
307 sprintf( psz_src, "%s/share/http", psz_vlcpath);
311 psz_src = config_GetPsz( p_intf, "http-src" );
313 if( !psz_src || *psz_src == '\0' )
315 if( !DirectoryCheck( "share/http" ) )
317 psz_src = strdup( "share/http" );
319 else if( !DirectoryCheck( DATA_PATH "/http" ) )
321 psz_src = strdup( DATA_PATH "/http" );
326 if( !psz_src || *psz_src == '\0' )
328 msg_Err( p_intf, "invalid src dir" );
332 /* remove trainling \ or / */
333 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
334 psz_src[strlen( psz_src ) - 1] == '/' )
336 psz_src[strlen( psz_src ) - 1] = '\0';
339 ParseDirectory( p_intf, psz_src, psz_src );
342 if( p_sys->i_files <= 0 )
344 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
347 p_intf->pf_run = Run;
353 if( psz_src ) free( psz_src );
354 if( p_sys->pp_files )
356 free( p_sys->pp_files );
358 httpd_HostDelete( p_sys->p_httpd_host );
359 free( p_sys->psz_html_type );
364 /*****************************************************************************
365 * CloseIntf: destroy interface
366 *****************************************************************************/
367 void Close ( vlc_object_t *p_this )
369 intf_thread_t *p_intf = (intf_thread_t *)p_this;
370 intf_sys_t *p_sys = p_intf->p_sys;
376 vlm_Delete( p_sys->p_vlm );
378 for( i = 0; i < p_sys->i_files; i++ )
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 );
386 free( p_sys->pp_files[i]->file );
387 free( p_sys->pp_files[i]->name );
388 free( p_sys->pp_files[i] );
390 if( p_sys->pp_files )
392 free( p_sys->pp_files );
394 httpd_HostDelete( p_sys->p_httpd_host );
396 free( p_sys->psz_html_type );
400 /*****************************************************************************
401 * Run: http interface thread
402 *****************************************************************************/
403 static void Run( intf_thread_t *p_intf )
405 intf_sys_t *p_sys = p_intf->p_sys;
407 while( !p_intf->b_die )
409 /* get the playlist */
410 if( p_sys->p_playlist == NULL )
412 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
415 /* Manage the input part */
416 if( p_sys->p_input == NULL )
418 if( p_sys->p_playlist )
421 vlc_object_find( p_sys->p_playlist,
426 else if( p_sys->p_input->b_dead )
428 vlc_object_release( p_sys->p_input );
429 p_sys->p_input = NULL;
434 msleep( INTF_IDLE_SLEEP );
439 vlc_object_release( p_sys->p_input );
440 p_sys->p_input = NULL;
443 if( p_sys->p_playlist )
445 vlc_object_release( p_sys->p_playlist );
446 p_sys->p_playlist = NULL;
451 /*****************************************************************************
453 *****************************************************************************/
454 #define MAX_DIR_SIZE 10240
456 /****************************************************************************
457 * FileToUrl: create a good name for an url from filename
458 ****************************************************************************/
459 static char *FileToUrl( char *name, vlc_bool_t *pb_index )
463 url = p = malloc( strlen( name ) + 1 );
465 *pb_index = VLC_FALSE;
472 while( *name == '\\' || *name == '/' )
474 while( *name == '/' )
484 /* convert '\\' into '/' */
497 if( ( p = strrchr( url, '/' ) ) != NULL )
499 if( !strncmp( p, "/index.", 7 ) )
502 *pb_index = VLC_TRUE;
508 /****************************************************************************
509 * ParseDirectory: parse recursively a directory, adding each file
510 ****************************************************************************/
511 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
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;
520 struct dirent *p_dir_content;
524 char *password = NULL;
525 char **ppsz_hosts = NULL;
530 #ifdef HAVE_SYS_STAT_H
531 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
537 if( ( p_dir = opendir( psz_dir ) ) == NULL )
539 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
543 i_dirlen = strlen( psz_dir );
544 if( i_dirlen + 10 > MAX_DIR_SIZE )
546 msg_Warn( p_intf, "skipping too deep dir (%s)", psz_dir );
550 msg_Dbg( p_intf, "dir=%s", psz_dir );
552 sprintf( dir, "%s/.access", psz_dir );
553 if( ( file = fopen( dir, "r" ) ) != NULL )
558 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
560 i_size = fread( line, 1, 1023, file );
564 while( i_size > 0 && ( line[i_size-1] == '\n' ||
565 line[i_size-1] == '\r' ) )
572 p = strchr( line, ':' );
576 user = strdup( line );
577 password = strdup( p );
580 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
581 user, password, i_size );
586 sprintf( dir, "%s/.hosts", psz_dir );
587 if( ( file = fopen( dir, "r" ) ) != NULL )
592 msg_Dbg( p_intf, "find .hosts in dir=%s", psz_dir );
594 while( !feof( file ) )
596 fgets( line, 1023, file );
597 i_size = strlen(line);
598 if( i_size > 0 && line[0] != '#' )
600 while( i_size > 0 && ( line[i_size-1] == '\n' ||
601 line[i_size-1] == '\r' ) )
605 if( !i_size ) continue;
609 msg_Dbg( p_intf, "restricted to %s (read=%d)",
611 TAB_APPEND( i_hosts, ppsz_hosts, strdup( line ) );
617 if( net_CheckIP( p_intf, "0.0.0.0", ppsz_hosts, i_hosts ) < 0 )
619 msg_Err( p_intf, ".hosts file is invalid in dir=%s", psz_dir );
625 /* parse psz_src dir */
626 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
631 if( ( p_dir_content->d_name[0] == '.' )
632 || ( i_dirlen + strlen( p_dir_content->d_name ) > MAX_DIR_SIZE ) )
635 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
636 if( ParseDirectory( p_intf, psz_root, dir ) )
638 httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
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;
651 msg_Err( p_intf , "unable to parse directory" );
654 return( VLC_ENOMEM );
656 msg_Dbg( p_intf, "file=%s (url=%s)",
659 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
661 f->b_html ? p_sys->psz_html_type : NULL,
662 user, password, ppsz_hosts, i_hosts,
667 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
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] == '/' )
674 char *psz_redir = strdup( f->name );
676 psz_redir[strlen( psz_redir ) - 1] = '\0';
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 );
682 if( b_index && ( p = strstr( f->file, "index." ) ) )
684 asprintf( &psz_redir, "%s%s", f->name, p );
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 );
704 for( i = 0; i < i_hosts; i++ )
706 TAB_REMOVE( i_hosts, ppsz_hosts, ppsz_hosts[0] );
714 /****************************************************************************
715 * var and set handling
716 ****************************************************************************/
718 static mvar_t *mvar_New( char *name, char *value )
720 mvar_t *v = malloc( sizeof( mvar_t ) );
722 if( !v ) return NULL;
723 v->name = strdup( name );
724 v->value = strdup( value ? value : "" );
727 v->field = malloc( sizeof( mvar_t * ) );
733 static void mvar_Delete( mvar_t *v )
740 for( i = 0; i < v->i_field; i++ )
742 mvar_Delete( v->field[i] );
748 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
750 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
751 v->field[v->i_field] = f;
755 static mvar_t *mvar_Duplicate( mvar_t *v )
760 n = mvar_New( v->name, v->value );
761 for( i = 0; i < v->i_field; i++ )
763 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
769 static void mvar_PushVar( mvar_t *v, mvar_t *f )
771 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
774 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
780 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
783 for( i = 0; i < v->i_field; i++ )
785 if( v->field[i] == f )
790 if( i >= v->i_field )
795 if( i + 1 < v->i_field )
797 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
800 /* FIXME should do a realloc */
803 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
806 char base[512], *field, *p;
809 /* format: name[index].field */
811 field = strchr( name, '.' );
814 int i = field - name;
815 strncpy( base, name, i );
821 strcpy( base, name );
824 if( ( p = strchr( base, '[' ) ) )
827 sscanf( p, "%d]", &i_index );
838 for( i = 0; i < s->i_field; i++ )
840 if( !strcmp( s->field[i]->name, base ) )
850 return mvar_GetVar( s->field[i], field );
864 static char *mvar_GetValue( mvar_t *v, char *field )
872 mvar_t *f = mvar_GetVar( v, field );
884 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
886 mvar_t *f = mvar_New( name, value );
887 mvar_PushVar( vars, f );
890 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
892 mvar_t *f = mvar_New( name, value );
893 mvar_AppendVar( vars, f );
897 /* arg= start[:stop[:step]],.. */
898 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
900 char *dup = strdup( arg );
902 mvar_t *s = mvar_New( name, "set" );
908 int i_start,i_stop,i_step;
911 p = strchr( str, ',' );
918 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
925 else if( i_match == 2 )
927 i_step = i_start < i_stop ? 1 : -1;
934 if( ( i_start <= i_stop && i_step > 0 ) ||
935 ( i_start >= i_stop && i_step < 0 ) )
937 for( i = i_start; ; i += i_step )
941 if( ( i_step > 0 && i > i_stop ) ||
942 ( i_step < 0 && i < i_stop ) )
947 sprintf( value, "%d", i );
949 mvar_PushNewVar( s, name, value );
960 void PlaylistListNode( playlist_t *p_pl, playlist_item_t *p_node,
961 char *name, mvar_t *s, int i_depth )
965 if (p_node->i_children == -1)
968 mvar_t *itm = mvar_New( name, "set" );
970 sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
971 mvar_AppendNewVar( itm, "current", value );
973 sprintf( value, "%d", p_node->input.i_id );
974 mvar_AppendNewVar( itm, "index", value );
976 mvar_AppendNewVar( itm, "name", p_node->input.psz_name );
978 mvar_AppendNewVar( itm, "uri", p_node->input.psz_uri );
980 sprintf( value, "Item");
981 mvar_AppendNewVar( itm, "type", value );
983 sprintf( value, "%d", i_depth );
984 mvar_AppendNewVar( itm, "depth", value );
986 mvar_AppendVar( s, itm );
992 mvar_t *itm = mvar_New( name, "set" );
994 mvar_AppendNewVar( itm, "name", p_node->input.psz_name );
995 mvar_AppendNewVar( itm, "uri", p_node->input.psz_name );
997 sprintf( value, "Node" );
998 mvar_AppendNewVar( itm, "type", value );
1000 sprintf( value, "%d", p_node->input.i_id );
1001 mvar_AppendNewVar( itm, "index", value );
1003 sprintf( value, "%d", p_node->i_children);
1004 mvar_AppendNewVar( itm, "i_children", value );
1006 sprintf( value, "%d", i_depth );
1007 mvar_AppendNewVar( itm, "depth", value );
1009 mvar_AppendVar( s, itm );
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);
1018 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
1020 playlist_view_t *p_view;
1021 mvar_t *s = mvar_New( name, "set" );
1024 vlc_mutex_lock( &p_pl->object_lock );
1026 p_view = playlist_ViewFind( p_pl, VIEW_CATEGORY ); /* FIXME */
1028 if( p_view != NULL )
1029 PlaylistListNode( p_pl, p_view->p_root, name, s, 0 );
1031 vlc_mutex_unlock( &p_pl->object_lock );
1036 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
1038 mvar_t *s = mvar_New( name, "set" );
1041 if( p_input == NULL )
1046 vlc_mutex_lock( &p_input->input.p_item->lock );
1047 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
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" );
1053 mvar_AppendNewVar( cat, "name", p_category->psz_name );
1054 mvar_AppendVar( cat, iset );
1056 for ( j = 0; j < p_category->i_infos; j++ )
1058 info_t *p_info = p_category->pp_infos[j];
1059 mvar_t *info = mvar_New( "info", "" );
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 );
1067 mvar_AppendVar( s, cat );
1069 vlc_mutex_unlock( &p_input->input.p_item->lock );
1074 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
1076 mvar_t *s = mvar_New( name, "set" );
1080 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
1082 for( i= 0; i < info.i_count; )
1086 inf = mvar_New( name, "set" );
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 );
1095 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
1096 mvar_AppendVar( s, inf );
1101 for( i = 0; i < info.i_count; i++ )
1103 free( info.info[i].psz_name );
1104 free( info.info[i].psz_value );
1106 if( info.i_count > 0 )
1115 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
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;
1123 struct dirent *p_dir_content;
1126 /* convert all / to native separator */
1127 #if defined( WIN32 )
1128 while( (p = strchr( psz_dir, '/' )) )
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 ) &&
1142 psz_dir[strlen( psz_dir ) -1 ] == sep )
1144 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1146 /* remove double separator */
1147 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1149 if( src[0] == sep && src[1] == sep )
1157 if( *psz_dir == '\0' )
1161 /* first fix all .. dir */
1165 if( src[0] == '.' && src[1] == '.' )
1168 if( p <= &psz_dir[1] )
1175 while( p > &psz_dir[1] && *p != sep )
1180 else if( *src == sep )
1182 if( p > psz_dir && p[-1] == sep )
1196 } while( *src && *src != sep );
1202 #ifdef HAVE_SYS_STAT_H
1203 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1209 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1211 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1215 /* remove traling / or \ */
1216 for( p = &psz_dir[strlen( psz_dir) - 1];
1217 p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1226 /* parse psz_src dir */
1227 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1231 if( !strcmp( p_dir_content->d_name, "." ) )
1236 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1238 #ifdef HAVE_SYS_STAT_H
1239 if( stat( tmp, &stat_info ) == -1 )
1244 f = mvar_New( name, "set" );
1245 mvar_AppendNewVar( f, "name", tmp );
1247 #ifdef HAVE_SYS_STAT_H
1248 if( S_ISDIR( stat_info.st_mode ) )
1250 mvar_AppendNewVar( f, "type", "directory" );
1252 else if( S_ISREG( stat_info.st_mode ) )
1254 mvar_AppendNewVar( f, "type", "file" );
1258 mvar_AppendNewVar( f, "type", "unknown" );
1261 sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
1262 mvar_AppendNewVar( f, "size", tmp );
1264 /* FIXME memory leak FIXME */
1266 ctime_r( &stat_info.st_mtime, tmp );
1267 mvar_AppendNewVar( f, "date", tmp );
1269 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1273 mvar_AppendNewVar( f, "type", "unknown" );
1274 mvar_AppendNewVar( f, "size", "unknown" );
1275 mvar_AppendNewVar( f, "date", "unknown" );
1277 mvar_AppendVar( s, f );
1283 static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
1285 mvar_t *s = mvar_New( name, "set" );
1289 if( vlm == NULL ) return s;
1291 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
1296 for( i = 0; i < msg->i_child; i++ )
1298 /* Over media, schedule */
1299 vlm_message_t *ch = msg->child[i];
1302 for( j = 0; j < ch->i_child; j++ )
1305 vlm_message_t *el = ch->child[j];
1306 vlm_message_t *inf, *desc;
1311 sprintf( psz, "show %s", el->psz_name );
1312 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
1314 desc = inf->child[0];
1316 /* Add a node with name and info */
1317 set = mvar_New( name, "set" );
1318 mvar_AppendNewVar( set, "name", el->psz_name );
1320 for( k = 0; k < desc->i_child; k++ )
1322 vlm_message_t *ch = desc->child[k];
1323 if( ch->i_child > 0 )
1326 mvar_t *n = mvar_New( ch->psz_name, "set" );
1328 for( c = 0; c < ch->i_child; c++ )
1330 if( ch->child[c]->psz_value )
1332 mvar_AppendNewVar( n, ch->child[c]->psz_name, ch->child[c]->psz_value );
1336 mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
1337 mvar_AppendVar( n, in );
1340 mvar_AppendVar( set, n );
1344 mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
1347 vlm_MessageDelete( inf );
1349 mvar_AppendVar( s, set );
1352 vlm_MessageDelete( msg );
1358 static void SSInit( rpn_stack_t * );
1359 static void SSClean( rpn_stack_t * );
1360 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1362 static void SSPush ( rpn_stack_t *, char * );
1363 static char *SSPop ( rpn_stack_t * );
1365 static void SSPushN ( rpn_stack_t *, int );
1366 static int SSPopN ( rpn_stack_t *, mvar_t * );
1369 /****************************************************************************
1371 ****************************************************************************/
1379 static int FileLoad( FILE *f, char **pp_data, int *pi_data )
1383 /* just load the file */
1385 *pp_data = malloc( 1025 ); /* +1 for \0 */
1386 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1389 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1395 (*pp_data)[*pi_data] = '\0';
1400 static int MacroParse( macro_t *m, char *psz_src )
1402 char *dup = strdup( (char *)psz_src );
1407 #define EXTRACT( name, l ) \
1409 p = strchr( src, '"' ); \
1414 m->name = strdup( src ); \
1435 if( !strncmp( src, "id=\"", 4 ) )
1439 else if( !strncmp( src, "param1=\"", 8 ) )
1441 EXTRACT( param1, 8 );
1443 else if( !strncmp( src, "param2=\"", 8 ) )
1445 EXTRACT( param2, 8 );
1452 if( strstr( src, "/>" ) )
1454 src = strstr( src, "/>" ) + 2;
1458 src += strlen( src );
1463 m->id = strdup( "" );
1465 if( m->param1 == NULL )
1467 m->param1 = strdup( "" );
1469 if( m->param2 == NULL )
1471 m->param2 = strdup( "" );
1480 static void MacroClean( macro_t *m )
1539 StrToMacroTypeTab [] =
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 },
1553 /* playlist management */
1554 { "add", MVLC_ADD },
1555 { "delete", MVLC_DEL },
1556 { "empty", MVLC_EMPTY },
1557 { "sort", MVLC_SORT },
1558 { "move", MVLC_MOVE },
1561 { "close", MVLC_CLOSE },
1562 { "shutdown", MVLC_SHUTDOWN },
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 },
1575 { "rpn", MVLC_RPN },
1576 { "stack", MVLC_STACK },
1578 { "foreach", MVLC_FOREACH },
1579 { "value", MVLC_VALUE },
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 },
1591 { NULL, MVLC_UNKNOWN }
1594 static int StrToMacroType( char *name )
1598 if( !name || *name == '\0')
1600 return MVLC_UNKNOWN;
1602 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1604 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1606 return StrToMacroTypeTab[i].i_type;
1609 return MVLC_UNKNOWN;
1612 static void MacroDo( httpd_file_sys_t *p_args,
1614 char *p_request, int i_request,
1615 char **pp_data, int *pi_data,
1618 intf_thread_t *p_intf = p_args->p_intf;
1619 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1622 #define ALLOC( l ) \
1624 int __i__ = *pp_dst - *pp_data; \
1626 *pp_data = realloc( *pp_data, *pi_data ); \
1627 *pp_dst = (*pp_data) + __i__; \
1629 #define PRINT( str ) \
1630 ALLOC( strlen( str ) + 1 ); \
1631 *pp_dst += sprintf( *pp_dst, str );
1633 #define PRINTS( str, s ) \
1634 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1636 char * psz_cur = *pp_dst; \
1637 *pp_dst += sprintf( *pp_dst, str, s ); \
1638 while( psz_cur && *psz_cur ) \
1640 /* Prevent script injection */ \
1641 if( *psz_cur == '<' ) *psz_cur = '*'; \
1642 if( *psz_cur == '>' ) *psz_cur = '*'; \
1647 switch( StrToMacroType( m->id ) )
1650 if( i_request <= 0 )
1654 uri_extract_value( p_request, "control", control, 512 );
1655 if( *m->param1 && !strstr( m->param1, control ) )
1657 msg_Warn( p_intf, "unauthorized control=%s", control );
1660 switch( StrToMacroType( control ) )
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,
1672 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1676 playlist_Control( p_sys->p_playlist, PLAYLIST_STOP);
1677 msg_Dbg( p_intf, "requested playlist stop" );
1680 playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
1681 msg_Dbg( p_intf, "requested playlist pause" );
1684 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
1685 msg_Dbg( p_intf, "requested playlist next" );
1688 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1);
1689 msg_Dbg( p_intf, "requested playlist next" );
1691 case MVLC_FULLSCREEN:
1693 if( p_sys->p_input )
1695 vout_thread_t *p_vout;
1696 p_vout = vlc_object_find( p_sys->p_input,
1697 VLC_OBJECT_VOUT, FIND_CHILD );
1701 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1702 vlc_object_release( p_vout );
1703 msg_Dbg( p_intf, "requested fullscreen toggle" );
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 )
1725 uri_extract_value( p_request, "seek_value", value, 20 );
1726 uri_decode_url_encoded( value );
1728 var_Get( p_sys->p_input, "length", &val);
1729 i_length = val.i_time;
1731 while( p_value[0] != '\0' )
1737 i_relative = VL_TIME_REL_FOR;
1743 i_relative = VL_TIME_REL_BACK;
1747 case '0': case '1': case '2': case '3': case '4':
1748 case '5': case '6': case '7': case '8': case '9':
1750 i_stock = strtol( p_value , &p_value , 10 );
1753 case '%': /* for percentage ie position */
1755 i_relative += POSITION_ABSOLUTE;
1763 i_value = 60 * (i_value + i_stock) ;
1768 case 'h': case 'H': /* hours */
1770 i_value += 3600 * i_stock;
1772 /* other characters which are not numbers are not important */
1773 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1779 case 'm': case 'M': case '\'': /* minutes */
1781 i_value += 60 * i_stock;
1784 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1790 case 's': case 'S': case '"': /* seconds */
1794 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1808 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1813 case VL_TIME_ABSOLUTE:
1815 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1816 val.i_time = (uint64_t)( i_value ) * 1000000;
1818 val.i_time = i_length;
1820 var_Set( p_sys->p_input, "time", val );
1821 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1824 case VL_TIME_REL_FOR:
1826 var_Get( p_sys->p_input, "time", &val );
1827 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1829 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1832 val.i_time = i_length;
1834 var_Set( p_sys->p_input, "time", val );
1835 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1838 case VL_TIME_REL_BACK:
1840 var_Get( p_sys->p_input, "time", &val );
1841 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1846 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1848 var_Set( p_sys->p_input, "time", val );
1849 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1852 case POSITION_ABSOLUTE:
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 );
1859 case POSITION_REL_FOR:
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 );
1867 case POSITION_REL_BACK:
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 );
1877 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
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
1893 audio_volume_t i_volume;
1896 uri_extract_value( p_request, "value", vol, 8 );
1897 aout_VolumeGet( p_intf, &i_volume );
1898 uri_decode_url_encoded( vol );
1902 i_value = atoi( vol + 1 );
1903 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1905 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1906 msg_Dbg( p_intf, "requested volume set: max" );
1909 aout_VolumeSet( p_intf , (i_volume + i_value) );
1910 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1915 i_value = atoi( vol + 1 );
1916 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1918 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1919 msg_Dbg( p_intf, "requested volume set: min" );
1922 aout_VolumeSet( p_intf , (i_volume - i_value) );
1923 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1926 if( strstr(vol, "%") != NULL )
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 ));
1935 i_value = atoi( vol );
1936 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1938 aout_VolumeSet( p_intf , atoi( vol ) );
1939 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1945 /* playlist management */
1949 playlist_item_t * p_item;
1951 uri_extract_value( p_request, "mrl", mrl, 512 );
1952 uri_decode_url_encoded( mrl );
1953 p_item = parse_MRL( p_intf, mrl );
1955 if( !p_item || !p_item->input.psz_uri ||
1956 !*p_item->input.psz_uri )
1958 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1961 playlist_AddItem( p_sys->p_playlist , p_item ,
1962 PLAYLIST_APPEND, PLAYLIST_END );
1963 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1970 int i_item, *p_items = NULL, i_nb_items = 0;
1971 char item[512], *p_parser = p_request;
1973 /* Get the list of items to delete */
1975 uri_extract_value( p_parser, "item", item, 512 )) )
1977 if( !*item ) continue;
1979 i_item = atoi( item );
1980 p_items = realloc( p_items, (i_nb_items + 1) *
1982 p_items[i_nb_items] = i_item;
1989 for( i = 0; i < i_nb_items; i++ )
1991 playlist_LockDelete( p_sys->p_playlist, p_items[i] );
1992 msg_Dbg( p_intf, "requested playlist delete: %d",
1998 if( p_items ) free( p_items );
2003 int i_item, *p_items = NULL, i_nb_items = 0;
2004 char item[512], *p_parser = p_request;
2007 /* Get the list of items to keep */
2009 uri_extract_value( p_parser, "item", item, 512 )) )
2011 if( !*item ) continue;
2013 i_item = atoi( item );
2014 p_items = realloc( p_items, (i_nb_items + 1) *
2016 p_items[i_nb_items] = i_item;
2020 for( i = p_sys->p_playlist->i_size - 1 ; i >= 0; i-- )
2022 /* Check if the item is in the keep list */
2023 for( j = 0 ; j < i_nb_items ; j++ )
2026 p_sys->p_playlist->pp_items[i]->input.i_id ) break;
2028 if( j == i_nb_items )
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",
2036 if( p_items ) free( p_items );
2041 playlist_LockClear( p_sys->p_playlist );
2042 msg_Dbg( p_intf, "requested playlist empty" );
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 );
2058 if( order[0] == '0' ) i_order = ORDER_NORMAL;
2059 else i_order = ORDER_REVERSE;
2061 if( !strcmp( type , "title" ) )
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" ) )
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,
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" ) )
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,
2080 ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
2081 msg_Dbg( p_intf, "requested playlist shuffle");
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 )
2098 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
2100 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
2102 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
2106 /* admin function */
2110 uri_extract_value( p_request, "id", id, 512 );
2111 msg_Dbg( p_intf, "requested close id=%s", id );
2113 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
2115 msg_Warn( p_intf, "close failed for id=%s", id );
2122 msg_Dbg( p_intf, "requested shutdown" );
2123 p_intf->p_vlc->b_die = VLC_TRUE;
2128 case MVLC_VLM_SETUP:
2130 static const char *vlm_properties[11] =
2133 "enabled", "disabled", "loop", "unloop",
2135 "input", "output", "option", "date", "period", "repeat", "append",
2137 vlm_message_t *vlm_answer;
2139 char *psz = malloc( strlen( p_request ) + 1000 );
2144 if( p_intf->p_sys->p_vlm == NULL )
2145 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2147 if( p_intf->p_sys->p_vlm == NULL ) break;
2149 uri_extract_value( p_request, "name", name, 512 );
2150 if( StrToMacroType( control ) == MVLC_VLM_NEW )
2153 uri_extract_value( p_request, "type", type, 20 );
2154 p += sprintf( psz, "new %s %s", name, type );
2158 p += sprintf( psz, "setup %s", name );
2160 /* Parse the request */
2161 for( i = 0; i < 11; i++ )
2164 uri_extract_value( p_request, vlm_properties[i], val, 512 );
2165 uri_decode_url_encoded( val );
2166 if( strlen( val ) > 0 && i >= 4 )
2168 p += sprintf( p, " %s %s", vlm_properties[i], val );
2170 else if( uri_test_param( p_request, vlm_properties[i] ) && i < 4 )
2172 p += sprintf( p, " %s", vlm_properties[i] );
2175 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2176 if( vlm_answer->psz_value == NULL ) /* there is no error */
2178 vlm_error = strdup( "" );
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 );
2189 mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
2191 vlm_MessageDelete( vlm_answer );
2199 vlm_message_t *vlm_answer;
2202 if( p_intf->p_sys->p_vlm == NULL )
2203 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2205 if( p_intf->p_sys->p_vlm == NULL ) break;
2207 uri_extract_value( p_request, "name", name, 512 );
2208 sprintf( psz, "del %s", name );
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 );
2217 case MVLC_VLM_PAUSE:
2221 vlm_message_t *vlm_answer;
2224 if( p_intf->p_sys->p_vlm == NULL )
2225 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2227 if( p_intf->p_sys->p_vlm == NULL ) break;
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 )
2239 uri_extract_value( p_request, "percent", percent, 512 );
2240 sprintf( psz, "control %s seek %s", name, percent );
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 );
2251 vlm_message_t *vlm_answer;
2255 if( p_intf->p_sys->p_vlm == NULL )
2256 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2258 if( p_intf->p_sys->p_vlm == NULL ) break;
2260 uri_extract_value( p_request, "file", file, 512 );
2261 uri_decode_url_encoded( file );
2263 if( StrToMacroType( control ) == MVLC_VLM_LOAD )
2264 sprintf( psz, "load %s", file );
2266 sprintf( psz, "save %s", file );
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 );
2277 PRINTS( "<!-- control param(%s) unsuported -->", control );
2289 if( i_request <= 0 ||
2290 *m->param1 == '\0' ||
2291 strstr( p_request, m->param1 ) == NULL )
2295 uri_extract_value( p_request, m->param1, value, 512 );
2296 uri_decode_url_encoded( value );
2298 switch( StrToMacroType( m->param2 ) )
2302 config_PutInt( p_intf, m->param1, i );
2306 config_PutFloat( p_intf, m->param1, f );
2309 config_PutPsz( p_intf, m->param1, value );
2312 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
2323 if( *m->param1 == '\0' )
2328 switch( StrToMacroType( m->param2 ) )
2331 i = config_GetInt( p_intf, m->param1 );
2332 sprintf( value, "%d", i );
2335 f = config_GetFloat( p_intf, m->param1 );
2336 sprintf( value, "%f", f );
2339 psz = config_GetPsz( p_intf, m->param1 );
2340 snprintf( value, sizeof( value ), "%s", psz ? psz : "" );
2341 if( psz ) free( psz );
2344 snprintf( value, sizeof( value ),
2345 "invalid type(%s) in set", m->param2 );
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 );
2359 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2360 s = SSPop( &p_args->stack );
2361 v = mvar_GetValue( p_args->vars, s );
2365 v = s = SSPop( &p_args->stack );
2373 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2376 /* Usefull for learning stack management */
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] );
2388 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2389 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2397 static char *MacroSearch( char *src, char *end, int i_mvlc, vlc_bool_t b_after )
2404 if( src + 4 < end && !strncmp( (char *)src, "<vlc", 4 ) )
2409 i_skip = MacroParse( &m, src );
2411 i_id = StrToMacroType( m.id );
2428 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2429 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2431 return src + ( b_after ? i_skip : 0 );
2433 else if( i_level < 0 )
2449 static void Execute( httpd_file_sys_t *p_args,
2450 char *p_request, int i_request,
2451 char **pp_data, int *pi_data,
2453 char *_src, char *_end )
2455 intf_thread_t *p_intf = p_args->p_intf;
2457 char *src, *dup, *end;
2458 char *dst = *pp_dst;
2460 src = dup = malloc( _end - _src + 1 );
2461 end = src +( _end - _src );
2463 memcpy( src, _src, _end - _src );
2466 /* we parse searching <vlc */
2472 p = (char *)strstr( (char *)src, "<vlc" );
2473 if( p < end && p == src )
2477 src += MacroParse( &m, src );
2479 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2481 switch( StrToMacroType( m.id ) )
2488 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2489 if( SSPopN( &p_args->stack, p_args->vars ) )
2497 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2501 char *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2505 char *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2508 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2512 else if( i_test == 1 )
2515 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2517 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2521 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2530 char *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2532 char *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2539 if( !strcmp( m.param2, "integer" ) )
2541 char *arg = SSPop( &p_args->stack );
2542 index = mvar_IntegerSetNew( m.param1, arg );
2545 else if( !strcmp( m.param2, "directory" ) )
2547 char *arg = SSPop( &p_args->stack );
2548 index = mvar_FileSetNew( m.param1, arg );
2551 else if( !strcmp( m.param2, "playlist" ) )
2553 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2555 else if( !strcmp( m.param2, "information" ) )
2557 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2559 else if( !strcmp( m.param2, "vlm" ) )
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 );
2566 else if( !strcmp( m.param2, "hosts" ) )
2568 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2570 else if( !strcmp( m.param2, "urls" ) )
2572 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2574 else if( !strcmp( m.param2, "connections" ) )
2576 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2579 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2581 index = mvar_Duplicate( v );
2585 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2590 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2592 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2594 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2597 f->name = strdup( m.param1 );
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 );
2606 mvar_Delete( index );
2613 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2621 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2624 int i_index = dst - *pp_data;
2627 *pp_data = realloc( *pp_data, *pi_data );
2628 dst = (*pp_data) + i_index;
2630 memcpy( dst, src, i_copy );
2640 /****************************************************************************
2642 ****************************************************************************
2643 * a file with b_html is parsed and all "macro" replaced
2644 * <vlc id="macro name" [param1="" [param2=""]] />
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 )
2653 char *p_request = (char *)_p_request;
2654 char **pp_data = (char **)_pp_data;
2655 int i_request = p_request ? strlen( p_request ) : 0;
2659 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2661 p = *pp_data = malloc( 10240 );
2664 return VLC_EGENERIC;
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" );
2677 *pi_data = strlen( *pp_data );
2682 if( !p_args->b_html )
2684 FileLoad( f, pp_data, pi_data );
2692 char position[4]; /* percentage */
2693 char time[12]; /* in seconds */
2694 char length[12]; /* in seconds */
2695 audio_volume_t i_volume;
2699 #define p_sys p_args->p_intf->p_sys
2700 if( p_sys->p_input )
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) );
2709 var_Get( p_sys->p_input, "state", &val );
2710 if( val.i_int == PLAYING_S )
2712 sprintf( state, "playing" );
2713 } else if( val.i_int == PAUSE_S )
2715 sprintf( state, "paused" );
2718 sprintf( state, "stop" );
2722 sprintf( position, "%d", 0 );
2723 sprintf( time, "%d", 0 );
2724 sprintf( length, "%d", 0 );
2725 sprintf( state, "stop" );
2729 aout_VolumeGet( p_args->p_intf , &i_volume );
2730 sprintf( volume , "%d" , (int)i_volume );
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 );
2743 SSInit( &p_args->stack );
2745 /* first we load in a temporary buffer */
2746 FileLoad( f, &p_buffer, &i_buffer );
2748 /* allocate output */
2749 *pi_data = i_buffer + 1000;
2750 dst = *pp_data = malloc( *pi_data );
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] );
2756 *pi_data = dst - *pp_data;
2758 SSClean( &p_args->stack );
2759 mvar_Delete( p_args->vars );
2768 /****************************************************************************
2770 ****************************************************************************/
2771 static int uri_test_param( char *psz_uri, const char *psz_name )
2775 while( (p = strstr( p, psz_name )) )
2777 /* Verify that we are dealing with a post/get argument */
2778 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2787 static char *uri_extract_value( char *psz_uri, const char *psz_name,
2788 char *psz_value, int i_value_max )
2792 while( (p = strstr( p, psz_name )) )
2794 /* Verify that we are dealing with a post/get argument */
2795 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2804 p += strlen( psz_name );
2805 if( *p == '=' ) p++;
2807 if( strchr( p, '&' ) )
2809 i_len = strchr( p, '&' ) - p;
2813 /* for POST method */
2814 if( strchr( p, '\n' ) )
2816 i_len = strchr( p, '\n' ) - p;
2817 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2821 i_len = strlen( p );
2824 i_len = __MIN( i_value_max - 1, i_len );
2827 strncpy( psz_value, p, i_len );
2828 psz_value[i_len] = '\0';
2832 strncpy( psz_value, "", i_value_max );
2838 strncpy( psz_value, "", i_value_max );
2844 static void uri_decode_url_encoded( char *psz )
2846 char *dup = strdup( psz );
2864 *psz++ = strtol( val, NULL, 16 );
2866 else if( *p == '+' )
2880 /****************************************************************************
2881 * Light RPN evaluator
2882 ****************************************************************************/
2883 static void SSInit( rpn_stack_t *st )
2888 static void SSClean( rpn_stack_t *st )
2890 while( st->i_stack > 0 )
2892 free( st->stack[--st->i_stack] );
2896 static void SSPush( rpn_stack_t *st, char *s )
2898 if( st->i_stack < STACK_MAX )
2900 st->stack[st->i_stack++] = strdup( s );
2904 static char * SSPop( rpn_stack_t *st )
2906 if( st->i_stack <= 0 )
2908 return strdup( "" );
2912 return st->stack[--st->i_stack];
2916 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2925 i = strtol( name, &end, 0 );
2928 value = mvar_GetValue( vars, name );
2936 static void SSPushN( rpn_stack_t *st, int i )
2940 sprintf( v, "%d", i );
2944 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2951 while( *exp == ' ' )
2958 /* extract string */
2961 while( *exp && *exp != '\'' )
2972 p = strchr( exp, ' ' );
2977 exp += strlen( exp );
2982 strncpy( s, exp, i );
2993 /* 1. Integer function */
2994 if( !strcmp( s, "!" ) )
2996 SSPushN( st, ~SSPopN( st, vars ) );
2998 else if( !strcmp( s, "^" ) )
3000 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
3002 else if( !strcmp( s, "&" ) )
3004 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
3006 else if( !strcmp( s, "|" ) )
3008 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
3010 else if( !strcmp( s, "+" ) )
3012 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
3014 else if( !strcmp( s, "-" ) )
3016 int j = SSPopN( st, vars );
3017 int i = SSPopN( st, vars );
3018 SSPushN( st, i - j );
3020 else if( !strcmp( s, "*" ) )
3022 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
3024 else if( !strcmp( s, "/" ) )
3028 j = SSPopN( st, vars );
3029 i = SSPopN( st, vars );
3031 SSPushN( st, j != 0 ? i / j : 0 );
3033 else if( !strcmp( s, "%" ) )
3037 j = SSPopN( st, vars );
3038 i = SSPopN( st, vars );
3040 SSPushN( st, j != 0 ? i % j : 0 );
3042 /* 2. integer tests */
3043 else if( !strcmp( s, "=" ) )
3045 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
3047 else if( !strcmp( s, "!=" ) )
3049 SSPushN( st, SSPopN( st, vars ) != SSPopN( st, vars ) ? -1 : 0 );
3051 else if( !strcmp( s, "<" ) )
3053 int j = SSPopN( st, vars );
3054 int i = SSPopN( st, vars );
3056 SSPushN( st, i < j ? -1 : 0 );
3058 else if( !strcmp( s, ">" ) )
3060 int j = SSPopN( st, vars );
3061 int i = SSPopN( st, vars );
3063 SSPushN( st, i > j ? -1 : 0 );
3065 else if( !strcmp( s, "<=" ) )
3067 int j = SSPopN( st, vars );
3068 int i = SSPopN( st, vars );
3070 SSPushN( st, i <= j ? -1 : 0 );
3072 else if( !strcmp( s, ">=" ) )
3074 int j = SSPopN( st, vars );
3075 int i = SSPopN( st, vars );
3077 SSPushN( st, i >= j ? -1 : 0 );
3079 /* 3. string functions */
3080 else if( !strcmp( s, "strcat" ) )
3082 char *s2 = SSPop( st );
3083 char *s1 = SSPop( st );
3084 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
3094 else if( !strcmp( s, "strcmp" ) )
3096 char *s2 = SSPop( st );
3097 char *s1 = SSPop( st );
3099 SSPushN( st, strcmp( s1, s2 ) );
3103 else if( !strcmp( s, "strncmp" ) )
3105 int n = SSPopN( st, vars );
3106 char *s2 = SSPop( st );
3107 char *s1 = SSPop( st );
3109 SSPushN( st, strncmp( s1, s2 , n ) );
3113 else if( !strcmp( s, "strsub" ) )
3115 int n = SSPopN( st, vars );
3116 int m = SSPopN( st, vars );
3118 char *s = SSPop( st );
3130 str = malloc( i_len + 1 );
3132 memcpy( str, s + m - 1, i_len );
3133 str[ i_len ] = '\0';
3139 else if( !strcmp( s, "strlen" ) )
3141 char *str = SSPop( st );
3143 SSPushN( st, strlen( str ) );
3146 /* 4. stack functions */
3147 else if( !strcmp( s, "dup" ) )
3149 char *str = SSPop( st );
3154 else if( !strcmp( s, "drop" ) )
3156 char *str = SSPop( st );
3159 else if( !strcmp( s, "swap" ) )
3161 char *s1 = SSPop( st );
3162 char *s2 = SSPop( st );
3169 else if( !strcmp( s, "flush" ) )
3174 else if( !strcmp( s, "store" ) )
3176 char *value = SSPop( st );
3177 char *name = SSPop( st );
3179 mvar_PushNewVar( vars, name, value );
3183 else if( !strcmp( s, "value" ) )
3185 char *name = SSPop( st );
3186 char *value = mvar_GetValue( vars, name );
3188 SSPush( st, value );
3192 else if( !strcmp( s, "url_extract" ) )
3194 char *url = mvar_GetValue( vars, "url_value" );
3195 char *name = SSPop( st );
3198 uri_extract_value( url, name, value, 512 );
3199 uri_decode_url_encoded( value );
3200 SSPush( st, value );
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 )
3226 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
3228 if( *s_sent == '\'' )
3230 s_sent = Find_end_MRL( s_sent );
3232 if( s_sent == NULL )
3242 if( *s_sent == '\"' )
3246 } else /* *s_sent == '\0' , which means the number of " is incorrect */
3256 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
3258 if( *s_sent == '\"' )
3260 s_sent = Find_end_MRL( s_sent );
3262 if( s_sent == NULL )
3272 if( *s_sent == '\'' )
3276 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
3282 default: /* now we can look for spaces */
3284 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
3286 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
3288 s_sent = Find_end_MRL( s_sent );
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 )
3306 char **ppsz_options = NULL;
3313 playlist_item_t * p_item = NULL;
3315 /* In case there is spaces before the mrl */
3316 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
3321 /* extract the mrl */
3322 s_temp = strstr( s_mrl , " :" );
3323 if( s_temp == NULL )
3325 s_temp = s_mrl + strlen( s_mrl );
3328 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
3335 /* if the mrl is between " or ', we must remove them */
3336 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
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';
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';
3350 /* now we can take care of the options */
3351 while( (*s_mrl != '\0') && (i_error == 0) )
3360 case ':': /* an option */
3362 s_temp = Find_end_MRL( s_mrl );
3364 if( s_temp == NULL )
3371 ppsz_options = realloc( ppsz_options , i_options *
3373 ppsz_options[ i_options - 1 ] =
3374 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
3376 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
3379 /* don't forget to finish the string with a '\0' */
3380 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
3400 /* now create an item */
3401 p_item = playlist_ItemNew( p_intf, mrl, mrl);
3402 for( i = 0 ; i< i_options ; i++ )
3404 playlist_ItemAddOption( p_item, ppsz_options[i] );
3408 for( i = 0; i < i_options; i++ ) free( ppsz_options[i] );
3409 if( i_options ) free( ppsz_options );