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 *****************************************************************************/
39 #include <vlc/vout.h> /* for fullscreen */
41 #include "vlc_httpd.h"
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
58 #elif defined( WIN32 ) && !defined( UNDER_CE )
66 /* stat() support for large files on win32 */
67 #if defined( WIN32 ) && !defined( UNDER_CE )
68 # define stat _stati64
71 /*****************************************************************************
73 *****************************************************************************/
74 static int Open ( vlc_object_t * );
75 static void Close( vlc_object_t * );
77 #define HOST_TEXT N_( "Host address" )
78 #define HOST_LONGTEXT N_( \
79 "You can set the address and port the http interface will bind to." )
80 #define SRC_TEXT N_( "Source directory" )
81 #define SRC_LONGTEXT N_( "Source directory" )
82 #define CERT_TEXT N_( "Certificate file" )
83 #define CERT_LONGTEXT N_( "HTTP interface x509 PEM certificate file " \
85 #define KEY_TEXT N_( "Private key file" )
86 #define KEY_LONGTEXT N_( "HTTP interface x509 PEM private key file" )
87 #define CA_TEXT N_( "Root CA file" )
88 #define CA_LONGTEXT N_( "HTTP interface x509 PEM trusted root CA " \
90 #define CRL_TEXT N_( "CRL file" )
91 #define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file" )
94 set_shortname( _("HTTP"));
95 set_description( _("HTTP remote control interface") );
96 set_category( CAT_INTERFACE );
97 set_subcategory( SUBCAT_INTERFACE_GENERAL );
98 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
99 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
100 set_section( N_("HTTP SSL" ), 0 );
101 add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
102 add_string ( "http-intf-key", NULL, NULL, KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
103 add_string ( "http-intf-ca", NULL, NULL, CA_TEXT, CA_LONGTEXT, VLC_TRUE );
104 add_string ( "http-intf-crl", NULL, NULL, CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
105 set_capability( "interface", 0 );
106 set_callbacks( Open, Close );
110 /*****************************************************************************
112 *****************************************************************************/
113 static void Run ( intf_thread_t *p_intf );
115 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
118 static int DirectoryCheck( char *psz_dir )
122 #ifdef HAVE_SYS_STAT_H
123 struct stat stat_info;
125 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
131 if( ( p_dir = opendir( psz_dir ) ) == NULL )
140 static int HttpCallback( httpd_file_sys_t *p_args,
143 uint8_t **pp_data, int *pi_data );
145 static char *uri_extract_value( char *psz_uri, const char *psz_name,
146 char *psz_value, int i_value_max );
147 static int uri_test_param( char *psz_uri, const char *psz_name );
149 static void uri_decode_url_encoded( char *psz );
151 static char *Find_end_MRL( char *psz );
152 static playlist_item_t *parse_MRL( intf_thread_t * , char *psz );
154 /*****************************************************************************
156 *****************************************************************************/
157 typedef struct mvar_s
163 struct mvar_s **field;
166 #define STACK_MAX 100
169 char *stack[STACK_MAX];
173 struct httpd_file_sys_t
175 intf_thread_t *p_intf;
176 httpd_file_t *p_file;
177 httpd_redirect_t *p_redir;
178 httpd_redirect_t *p_redir2;
185 /* inited for each access */
192 httpd_host_t *p_httpd_host;
195 httpd_file_sys_t **pp_files;
197 playlist_t *p_playlist;
198 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 SSL configuration */
242 psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
243 if ( psz_cert != NULL )
245 msg_Dbg( p_intf, "enablind TLS for HTTP interface (cert file: %s)",
247 psz_key = config_GetPsz( p_intf, "http-intf-key" );
248 psz_ca = config_GetPsz( p_intf, "http-intf-ca" );
249 psz_crl = config_GetPsz( p_intf, "http-intf-crl" );
260 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
262 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
263 i_port, psz_cert, psz_key, psz_ca,
265 if( p_sys->p_httpd_host == NULL )
267 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
278 p_sys->pp_files = NULL;
280 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || defined(WIN32)
281 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
283 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
284 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
285 if( !psz_src ) return VLC_ENOMEM;
287 sprintf( psz_src, "%s/http", psz_vlcpath);
289 sprintf( psz_src, "%s/share/http", psz_vlcpath);
293 psz_src = config_GetPsz( p_intf, "http-src" );
295 if( !psz_src || *psz_src == '\0' )
297 if( !DirectoryCheck( "share/http" ) )
299 psz_src = strdup( "share/http" );
301 else if( !DirectoryCheck( DATA_PATH "/http" ) )
303 psz_src = strdup( DATA_PATH "/http" );
308 if( !psz_src || *psz_src == '\0' )
310 msg_Err( p_intf, "invalid src dir" );
314 /* remove trainling \ or / */
315 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
316 psz_src[strlen( psz_src ) - 1] == '/' )
318 psz_src[strlen( psz_src ) - 1] = '\0';
321 ParseDirectory( p_intf, psz_src, psz_src );
324 if( p_sys->i_files <= 0 )
326 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
329 p_intf->pf_run = Run;
335 if( psz_src ) free( psz_src );
336 if( p_sys->pp_files )
338 free( p_sys->pp_files );
340 httpd_HostDelete( p_sys->p_httpd_host );
345 /*****************************************************************************
346 * CloseIntf: destroy interface
347 *****************************************************************************/
348 void Close ( vlc_object_t *p_this )
350 intf_thread_t *p_intf = (intf_thread_t *)p_this;
351 intf_sys_t *p_sys = p_intf->p_sys;
357 vlm_Delete( p_sys->p_vlm );
359 for( i = 0; i < p_sys->i_files; i++ )
361 httpd_FileDelete( p_sys->pp_files[i]->p_file );
362 if( p_sys->pp_files[i]->p_redir )
363 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
364 if( p_sys->pp_files[i]->p_redir2 )
365 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir2 );
367 free( p_sys->pp_files[i]->file );
368 free( p_sys->pp_files[i]->name );
369 free( p_sys->pp_files[i] );
371 if( p_sys->pp_files )
373 free( p_sys->pp_files );
375 httpd_HostDelete( p_sys->p_httpd_host );
380 /*****************************************************************************
381 * Run: http interface thread
382 *****************************************************************************/
383 static void Run( intf_thread_t *p_intf )
385 intf_sys_t *p_sys = p_intf->p_sys;
387 while( !p_intf->b_die )
389 /* get the playlist */
390 if( p_sys->p_playlist == NULL )
392 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
395 /* Manage the input part */
396 if( p_sys->p_input == NULL )
398 if( p_sys->p_playlist )
401 vlc_object_find( p_sys->p_playlist,
406 else if( p_sys->p_input->b_dead )
408 vlc_object_release( p_sys->p_input );
409 p_sys->p_input = NULL;
414 msleep( INTF_IDLE_SLEEP );
419 vlc_object_release( p_sys->p_input );
420 p_sys->p_input = NULL;
423 if( p_sys->p_playlist )
425 vlc_object_release( p_sys->p_playlist );
426 p_sys->p_playlist = NULL;
431 /*****************************************************************************
433 *****************************************************************************/
434 #define MAX_DIR_SIZE 10240
436 /****************************************************************************
437 * FileToUrl: create a good name for an url from filename
438 ****************************************************************************/
439 static char *FileToUrl( char *name, vlc_bool_t *pb_index )
443 url = p = malloc( strlen( name ) + 1 );
445 *pb_index = VLC_FALSE;
452 while( *name == '\\' || *name == '/' )
454 while( *name == '/' )
464 /* convert '\\' into '/' */
477 if( ( p = strrchr( url, '/' ) ) != NULL )
479 if( !strncmp( p, "/index.", 7 ) )
482 *pb_index = VLC_TRUE;
488 /****************************************************************************
489 * ParseDirectory: parse recursively a directory, adding each file
490 ****************************************************************************/
491 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
494 static const char mimetype[] = "text/html; charset=UTF-8";
495 intf_sys_t *p_sys = p_intf->p_sys;
496 char dir[MAX_DIR_SIZE];
497 #ifdef HAVE_SYS_STAT_H
498 struct stat stat_info;
501 struct dirent *p_dir_content;
506 char *password = NULL;
510 #ifdef HAVE_SYS_STAT_H
511 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
517 if( ( p_dir = opendir( psz_dir ) ) == NULL )
519 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
523 i_dirlen = strlen( psz_dir );
524 if( i_dirlen + 10 > MAX_DIR_SIZE )
526 msg_Warn( p_intf, "skipping too deep dir (%s)", psz_dir );
530 msg_Dbg( p_intf, "dir=%s", psz_dir );
532 sprintf( dir, "%s/.access", psz_dir );
533 if( ( file = fopen( dir, "r" ) ) != NULL )
538 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
540 i_size = fread( line, 1, 1023, file );
544 while( i_size > 0 && ( line[i_size-1] == '\n' ||
545 line[i_size-1] == '\r' ) )
552 p = strchr( line, ':' );
556 user = strdup( line );
557 password = strdup( p );
560 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
561 user, password, i_size );
566 sprintf( dir, "%s/.hosts", psz_dir );
567 p_acl = ACL_Create( p_intf, VLC_FALSE );
568 if( ACL_LoadFile( p_acl, dir ) )
570 ACL_Destroy( p_acl );
576 /* parse psz_src dir */
577 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
582 if( ( p_dir_content->d_name[0] == '.' )
583 || ( i_dirlen + strlen( p_dir_content->d_name ) > MAX_DIR_SIZE ) )
586 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
587 if( ParseDirectory( p_intf, psz_root, dir ) )
589 httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
596 f->file = strdup( dir );
597 f->name = FileToUrl( &dir[strlen( psz_root )], &b_index );
598 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
602 msg_Err( p_intf , "unable to parse directory" );
605 return( VLC_ENOMEM );
607 msg_Dbg( p_intf, "file=%s (url=%s)",
610 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
612 f->b_html ? mimetype : NULL,
613 user, password, p_acl,
618 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
620 /* for url that ends by / add
621 * - a redirect from rep to rep/
622 * - in case of index.* rep/index.html to rep/ */
623 if( f && f->name[strlen(f->name) - 1] == '/' )
625 char *psz_redir = strdup( f->name );
627 psz_redir[strlen( psz_redir ) - 1] = '\0';
629 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
630 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
633 if( b_index && ( p = strstr( f->file, "index." ) ) )
635 asprintf( &psz_redir, "%s%s", f->name, p );
637 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
638 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
639 f->name, psz_redir );
656 ACL_Destroy( p_acl );
662 /****************************************************************************
663 * var and set handling
664 ****************************************************************************/
666 static mvar_t *mvar_New( char *name, char *value )
668 mvar_t *v = malloc( sizeof( mvar_t ) );
670 if( !v ) return NULL;
671 v->name = strdup( name );
672 v->value = strdup( value ? value : "" );
675 v->field = malloc( sizeof( mvar_t * ) );
681 static void mvar_Delete( mvar_t *v )
688 for( i = 0; i < v->i_field; i++ )
690 mvar_Delete( v->field[i] );
696 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
698 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
699 v->field[v->i_field] = f;
703 static mvar_t *mvar_Duplicate( mvar_t *v )
708 n = mvar_New( v->name, v->value );
709 for( i = 0; i < v->i_field; i++ )
711 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
717 static void mvar_PushVar( mvar_t *v, mvar_t *f )
719 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
722 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
728 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
731 for( i = 0; i < v->i_field; i++ )
733 if( v->field[i] == f )
738 if( i >= v->i_field )
743 if( i + 1 < v->i_field )
745 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
748 /* FIXME should do a realloc */
751 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
754 char base[512], *field, *p;
757 /* format: name[index].field */
759 field = strchr( name, '.' );
762 int i = field - name;
763 strncpy( base, name, i );
769 strcpy( base, name );
772 if( ( p = strchr( base, '[' ) ) )
775 sscanf( p, "%d]", &i_index );
786 for( i = 0; i < s->i_field; i++ )
788 if( !strcmp( s->field[i]->name, base ) )
798 return mvar_GetVar( s->field[i], field );
812 static char *mvar_GetValue( mvar_t *v, char *field )
820 mvar_t *f = mvar_GetVar( v, field );
832 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
834 mvar_t *f = mvar_New( name, value );
835 mvar_PushVar( vars, f );
838 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
840 mvar_t *f = mvar_New( name, value );
841 mvar_AppendVar( vars, f );
845 /* arg= start[:stop[:step]],.. */
846 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
848 char *dup = strdup( arg );
850 mvar_t *s = mvar_New( name, "set" );
856 int i_start,i_stop,i_step;
859 p = strchr( str, ',' );
866 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
873 else if( i_match == 2 )
875 i_step = i_start < i_stop ? 1 : -1;
882 if( ( i_start <= i_stop && i_step > 0 ) ||
883 ( i_start >= i_stop && i_step < 0 ) )
885 for( i = i_start; ; i += i_step )
889 if( ( i_step > 0 && i > i_stop ) ||
890 ( i_step < 0 && i < i_stop ) )
895 sprintf( value, "%d", i );
897 mvar_PushNewVar( s, name, value );
908 void PlaylistListNode( playlist_t *p_pl, playlist_item_t *p_node,
909 char *name, mvar_t *s, int i_depth )
913 if (p_node->i_children == -1)
916 mvar_t *itm = mvar_New( name, "set" );
918 sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
919 mvar_AppendNewVar( itm, "current", value );
921 sprintf( value, "%d", p_node->input.i_id );
922 mvar_AppendNewVar( itm, "index", value );
924 mvar_AppendNewVar( itm, "name", p_node->input.psz_name );
926 mvar_AppendNewVar( itm, "uri", p_node->input.psz_uri );
928 sprintf( value, "Item");
929 mvar_AppendNewVar( itm, "type", value );
931 sprintf( value, "%d", i_depth );
932 mvar_AppendNewVar( itm, "depth", value );
934 mvar_AppendVar( s, itm );
940 mvar_t *itm = mvar_New( name, "set" );
942 mvar_AppendNewVar( itm, "name", p_node->input.psz_name );
943 mvar_AppendNewVar( itm, "uri", p_node->input.psz_name );
945 sprintf( value, "Node" );
946 mvar_AppendNewVar( itm, "type", value );
948 sprintf( value, "%d", p_node->input.i_id );
949 mvar_AppendNewVar( itm, "index", value );
951 sprintf( value, "%d", p_node->i_children);
952 mvar_AppendNewVar( itm, "i_children", value );
954 sprintf( value, "%d", i_depth );
955 mvar_AppendNewVar( itm, "depth", value );
957 mvar_AppendVar( s, itm );
959 for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
960 PlaylistListNode( p_pl, p_node->pp_children[i_child], name, s, i_depth + 1);
966 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
968 playlist_view_t *p_view;
969 mvar_t *s = mvar_New( name, "set" );
972 vlc_mutex_lock( &p_pl->object_lock );
974 p_view = playlist_ViewFind( p_pl, VIEW_CATEGORY ); /* FIXME */
977 PlaylistListNode( p_pl, p_view->p_root, name, s, 0 );
979 vlc_mutex_unlock( &p_pl->object_lock );
984 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
986 mvar_t *s = mvar_New( name, "set" );
989 if( p_input == NULL )
994 vlc_mutex_lock( &p_input->input.p_item->lock );
995 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
997 info_category_t *p_category = p_input->input.p_item->pp_categories[i];
998 mvar_t *cat = mvar_New( name, "set" );
999 mvar_t *iset = mvar_New( "info", "set" );
1001 mvar_AppendNewVar( cat, "name", p_category->psz_name );
1002 mvar_AppendVar( cat, iset );
1004 for ( j = 0; j < p_category->i_infos; j++ )
1006 info_t *p_info = p_category->pp_infos[j];
1007 mvar_t *info = mvar_New( "info", "" );
1009 msg_Dbg( p_input, "adding info name=%s value=%s",
1010 p_info->psz_name, p_info->psz_value );
1011 mvar_AppendNewVar( info, "name", p_info->psz_name );
1012 mvar_AppendNewVar( info, "value", p_info->psz_value );
1013 mvar_AppendVar( iset, info );
1015 mvar_AppendVar( s, cat );
1017 vlc_mutex_unlock( &p_input->input.p_item->lock );
1022 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
1024 mvar_t *s = mvar_New( name, "set" );
1028 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
1030 for( i= 0; i < info.i_count; )
1034 inf = mvar_New( name, "set" );
1037 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
1038 info.info[i].psz_name, info.info[i].psz_value ); */
1039 mvar_AppendNewVar( inf,
1040 info.info[i].psz_name,
1041 info.info[i].psz_value );
1043 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
1044 mvar_AppendVar( s, inf );
1049 for( i = 0; i < info.i_count; i++ )
1051 free( info.info[i].psz_name );
1052 free( info.info[i].psz_value );
1054 if( info.i_count > 0 )
1063 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1065 mvar_t *s = mvar_New( name, "set" );
1066 char tmp[MAX_DIR_SIZE], *p, *src;
1067 #ifdef HAVE_SYS_STAT_H
1068 struct stat stat_info;
1071 struct dirent *p_dir_content;
1074 /* convert all / to native separator */
1075 #if defined( WIN32 )
1076 while( (p = strchr( psz_dir, '/' )) )
1085 /* remove trailling separator */
1086 while( strlen( psz_dir ) > 1 &&
1087 #if defined( WIN32 )
1088 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1090 psz_dir[strlen( psz_dir ) -1 ] == sep )
1092 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1094 /* remove double separator */
1095 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1097 if( src[0] == sep && src[1] == sep )
1105 if( *psz_dir == '\0' )
1109 /* first fix all .. dir */
1113 if( src[0] == '.' && src[1] == '.' )
1116 if( p <= &psz_dir[1] )
1123 while( p > &psz_dir[1] && *p != sep )
1128 else if( *src == sep )
1130 if( p > psz_dir && p[-1] == sep )
1144 } while( *src && *src != sep );
1150 #ifdef HAVE_SYS_STAT_H
1151 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1157 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1159 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1163 /* remove traling / or \ */
1164 for( p = &psz_dir[strlen( psz_dir) - 1];
1165 p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1173 const char *psz_ext;
1175 /* parse psz_src dir */
1176 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1180 if( !strcmp( p_dir_content->d_name, "." ) )
1185 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1187 #ifdef HAVE_SYS_STAT_H
1188 if( stat( tmp, &stat_info ) == -1 )
1193 f = mvar_New( name, "set" );
1194 mvar_AppendNewVar( f, "name", tmp );
1195 mvar_AppendNewVar( f, "basename", p_dir_content->d_name );
1197 /* put file extension in 'ext' */
1198 psz_ext = strrchr( p_dir_content->d_name, '.' );
1199 mvar_AppendNewVar( f, "ext", psz_ext != NULL ? psz_ext + 1 : "" );
1201 #ifdef HAVE_SYS_STAT_H
1202 if( S_ISDIR( stat_info.st_mode ) )
1204 mvar_AppendNewVar( f, "type", "directory" );
1206 else if( S_ISREG( stat_info.st_mode ) )
1208 mvar_AppendNewVar( f, "type", "file" );
1212 mvar_AppendNewVar( f, "type", "unknown" );
1215 sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
1216 mvar_AppendNewVar( f, "size", tmp );
1218 /* FIXME memory leak FIXME */
1220 ctime_r( &stat_info.st_mtime, tmp );
1221 mvar_AppendNewVar( f, "date", tmp );
1223 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1227 mvar_AppendNewVar( f, "type", "unknown" );
1228 mvar_AppendNewVar( f, "size", "unknown" );
1229 mvar_AppendNewVar( f, "date", "unknown" );
1231 mvar_AppendVar( s, f );
1237 static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
1239 mvar_t *s = mvar_New( name, "set" );
1243 if( vlm == NULL ) return s;
1245 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
1250 for( i = 0; i < msg->i_child; i++ )
1252 /* Over media, schedule */
1253 vlm_message_t *ch = msg->child[i];
1256 for( j = 0; j < ch->i_child; j++ )
1259 vlm_message_t *el = ch->child[j];
1260 vlm_message_t *inf, *desc;
1265 sprintf( psz, "show %s", el->psz_name );
1266 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
1268 desc = inf->child[0];
1270 /* Add a node with name and info */
1271 set = mvar_New( name, "set" );
1272 mvar_AppendNewVar( set, "name", el->psz_name );
1274 for( k = 0; k < desc->i_child; k++ )
1276 vlm_message_t *ch = desc->child[k];
1277 if( ch->i_child > 0 )
1280 mvar_t *n = mvar_New( ch->psz_name, "set" );
1282 for( c = 0; c < ch->i_child; c++ )
1284 if( ch->child[c]->psz_value )
1286 mvar_AppendNewVar( n, ch->child[c]->psz_name, ch->child[c]->psz_value );
1290 mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
1291 mvar_AppendVar( n, in );
1294 mvar_AppendVar( set, n );
1298 mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
1301 vlm_MessageDelete( inf );
1303 mvar_AppendVar( s, set );
1306 vlm_MessageDelete( msg );
1312 static void SSInit( rpn_stack_t * );
1313 static void SSClean( rpn_stack_t * );
1314 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1316 static void SSPush ( rpn_stack_t *, char * );
1317 static char *SSPop ( rpn_stack_t * );
1319 static void SSPushN ( rpn_stack_t *, int );
1320 static int SSPopN ( rpn_stack_t *, mvar_t * );
1323 /****************************************************************************
1325 ****************************************************************************/
1333 static int FileLoad( FILE *f, char **pp_data, int *pi_data )
1337 /* just load the file */
1339 *pp_data = malloc( 1025 ); /* +1 for \0 */
1340 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1343 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1349 (*pp_data)[*pi_data] = '\0';
1354 static int MacroParse( macro_t *m, char *psz_src )
1356 char *dup = strdup( (char *)psz_src );
1361 #define EXTRACT( name, l ) \
1363 p = strchr( src, '"' ); \
1368 m->name = strdup( src ); \
1389 if( !strncmp( src, "id=\"", 4 ) )
1393 else if( !strncmp( src, "param1=\"", 8 ) )
1395 EXTRACT( param1, 8 );
1397 else if( !strncmp( src, "param2=\"", 8 ) )
1399 EXTRACT( param2, 8 );
1406 if( strstr( src, "/>" ) )
1408 src = strstr( src, "/>" ) + 2;
1412 src += strlen( src );
1417 m->id = strdup( "" );
1419 if( m->param1 == NULL )
1421 m->param1 = strdup( "" );
1423 if( m->param2 == NULL )
1425 m->param2 = strdup( "" );
1434 static void MacroClean( macro_t *m )
1493 StrToMacroTypeTab [] =
1495 { "control", MVLC_CONTROL },
1496 /* player control */
1497 { "play", MVLC_PLAY },
1498 { "stop", MVLC_STOP },
1499 { "pause", MVLC_PAUSE },
1500 { "next", MVLC_NEXT },
1501 { "previous", MVLC_PREVIOUS },
1502 { "seek", MVLC_SEEK },
1503 { "keep", MVLC_KEEP },
1504 { "fullscreen", MVLC_FULLSCREEN },
1505 { "volume", MVLC_VOLUME },
1507 /* playlist management */
1508 { "add", MVLC_ADD },
1509 { "delete", MVLC_DEL },
1510 { "empty", MVLC_EMPTY },
1511 { "sort", MVLC_SORT },
1512 { "move", MVLC_MOVE },
1515 { "close", MVLC_CLOSE },
1516 { "shutdown", MVLC_SHUTDOWN },
1519 { "vlm_new", MVLC_VLM_NEW },
1520 { "vlm_setup", MVLC_VLM_SETUP },
1521 { "vlm_del", MVLC_VLM_DEL },
1522 { "vlm_play", MVLC_VLM_PLAY },
1523 { "vlm_pause", MVLC_VLM_PAUSE },
1524 { "vlm_stop", MVLC_VLM_STOP },
1525 { "vlm_seek", MVLC_VLM_SEEK },
1526 { "vlm_load", MVLC_VLM_LOAD },
1527 { "vlm_save", MVLC_VLM_SAVE },
1529 { "rpn", MVLC_RPN },
1530 { "stack", MVLC_STACK },
1532 { "foreach", MVLC_FOREACH },
1533 { "value", MVLC_VALUE },
1536 { "else", MVLC_ELSE },
1537 { "end", MVLC_END },
1538 { "get", MVLC_GET },
1539 { "set", MVLC_SET },
1540 { "int", MVLC_INT },
1541 { "float", MVLC_FLOAT },
1542 { "string", MVLC_STRING },
1545 { NULL, MVLC_UNKNOWN }
1548 static int StrToMacroType( char *name )
1552 if( !name || *name == '\0')
1554 return MVLC_UNKNOWN;
1556 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1558 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1560 return StrToMacroTypeTab[i].i_type;
1563 return MVLC_UNKNOWN;
1566 static void MacroDo( httpd_file_sys_t *p_args,
1568 char *p_request, int i_request,
1569 char **pp_data, int *pi_data,
1572 intf_thread_t *p_intf = p_args->p_intf;
1573 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1576 #define ALLOC( l ) \
1578 int __i__ = *pp_dst - *pp_data; \
1580 *pp_data = realloc( *pp_data, *pi_data ); \
1581 *pp_dst = (*pp_data) + __i__; \
1583 #define PRINT( str ) \
1584 ALLOC( strlen( str ) + 1 ); \
1585 *pp_dst += sprintf( *pp_dst, str );
1587 #define PRINTS( str, s ) \
1588 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1590 char * psz_cur = *pp_dst; \
1591 *pp_dst += sprintf( *pp_dst, str, s ); \
1592 while( psz_cur && *psz_cur ) \
1594 /* Prevent script injection */ \
1595 if( *psz_cur == '<' ) *psz_cur = '*'; \
1596 if( *psz_cur == '>' ) *psz_cur = '*'; \
1601 switch( StrToMacroType( m->id ) )
1604 if( i_request <= 0 )
1608 uri_extract_value( p_request, "control", control, 512 );
1609 if( *m->param1 && !strstr( m->param1, control ) )
1611 msg_Warn( p_intf, "unauthorized control=%s", control );
1614 switch( StrToMacroType( control ) )
1621 uri_extract_value( p_request, "item", item, 512 );
1622 i_item = atoi( item );
1623 playlist_Control( p_sys->p_playlist, PLAYLIST_ITEMPLAY,
1624 playlist_ItemGetById( p_sys->p_playlist,
1626 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1630 playlist_Control( p_sys->p_playlist, PLAYLIST_STOP);
1631 msg_Dbg( p_intf, "requested playlist stop" );
1634 playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
1635 msg_Dbg( p_intf, "requested playlist pause" );
1638 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
1639 msg_Dbg( p_intf, "requested playlist next" );
1642 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1);
1643 msg_Dbg( p_intf, "requested playlist next" );
1645 case MVLC_FULLSCREEN:
1647 if( p_sys->p_input )
1649 vout_thread_t *p_vout;
1650 p_vout = vlc_object_find( p_sys->p_input,
1651 VLC_OBJECT_VOUT, FIND_CHILD );
1655 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1656 vlc_object_release( p_vout );
1657 msg_Dbg( p_intf, "requested fullscreen toggle" );
1671 #define POSITION_ABSOLUTE 12
1672 #define POSITION_REL_FOR 13
1673 #define POSITION_REL_BACK 11
1674 #define VL_TIME_ABSOLUTE 0
1675 #define VL_TIME_REL_FOR 1
1676 #define VL_TIME_REL_BACK -1
1677 if( p_sys->p_input )
1679 uri_extract_value( p_request, "seek_value", value, 20 );
1680 uri_decode_url_encoded( value );
1682 var_Get( p_sys->p_input, "length", &val);
1683 i_length = val.i_time;
1685 while( p_value[0] != '\0' )
1691 i_relative = VL_TIME_REL_FOR;
1697 i_relative = VL_TIME_REL_BACK;
1701 case '0': case '1': case '2': case '3': case '4':
1702 case '5': case '6': case '7': case '8': case '9':
1704 i_stock = strtol( p_value , &p_value , 10 );
1707 case '%': /* for percentage ie position */
1709 i_relative += POSITION_ABSOLUTE;
1717 i_value = 60 * (i_value + i_stock) ;
1722 case 'h': case 'H': /* hours */
1724 i_value += 3600 * i_stock;
1726 /* other characters which are not numbers are not important */
1727 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1733 case 'm': case 'M': case '\'': /* minutes */
1735 i_value += 60 * i_stock;
1738 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1744 case 's': case 'S': case '"': /* seconds */
1748 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1762 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1767 case VL_TIME_ABSOLUTE:
1769 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1770 val.i_time = (uint64_t)( i_value ) * 1000000;
1772 val.i_time = i_length;
1774 var_Set( p_sys->p_input, "time", val );
1775 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1778 case VL_TIME_REL_FOR:
1780 var_Get( p_sys->p_input, "time", &val );
1781 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1783 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1786 val.i_time = i_length;
1788 var_Set( p_sys->p_input, "time", val );
1789 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1792 case VL_TIME_REL_BACK:
1794 var_Get( p_sys->p_input, "time", &val );
1795 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1800 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1802 var_Set( p_sys->p_input, "time", val );
1803 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1806 case POSITION_ABSOLUTE:
1808 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1809 var_Set( p_sys->p_input, "position", val );
1810 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1813 case POSITION_REL_FOR:
1815 var_Get( p_sys->p_input, "position", &val );
1816 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1817 var_Set( p_sys->p_input, "position", val );
1818 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1821 case POSITION_REL_BACK:
1823 var_Get( p_sys->p_input, "position", &val );
1824 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1825 var_Set( p_sys->p_input, "position", val );
1826 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1831 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1836 #undef POSITION_ABSOLUTE
1837 #undef POSITION_REL_FOR
1838 #undef POSITION_REL_BACK
1839 #undef VL_TIME_ABSOLUTE
1840 #undef VL_TIME_REL_FOR
1841 #undef VL_TIME_REL_BACK
1847 audio_volume_t i_volume;
1850 uri_extract_value( p_request, "value", vol, 8 );
1851 aout_VolumeGet( p_intf, &i_volume );
1852 uri_decode_url_encoded( vol );
1856 i_value = atoi( vol + 1 );
1857 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1859 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1860 msg_Dbg( p_intf, "requested volume set: max" );
1863 aout_VolumeSet( p_intf , (i_volume + i_value) );
1864 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1869 i_value = atoi( vol + 1 );
1870 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1872 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1873 msg_Dbg( p_intf, "requested volume set: min" );
1876 aout_VolumeSet( p_intf , (i_volume - i_value) );
1877 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1880 if( strstr(vol, "%") != NULL )
1882 i_value = atoi( vol );
1883 if( (i_value <= 400) && (i_value>=0) ){
1884 aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/400+AOUT_VOLUME_MIN);
1885 msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
1889 i_value = atoi( vol );
1890 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1892 aout_VolumeSet( p_intf , atoi( vol ) );
1893 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1899 /* playlist management */
1903 playlist_item_t * p_item;
1905 uri_extract_value( p_request, "mrl", mrl, 512 );
1906 uri_decode_url_encoded( mrl );
1907 p_item = parse_MRL( p_intf, mrl );
1909 if( !p_item || !p_item->input.psz_uri ||
1910 !*p_item->input.psz_uri )
1912 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1915 playlist_AddItem( p_sys->p_playlist , p_item ,
1916 PLAYLIST_APPEND, PLAYLIST_END );
1917 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1924 int i_item, *p_items = NULL, i_nb_items = 0;
1925 char item[512], *p_parser = p_request;
1927 /* Get the list of items to delete */
1929 uri_extract_value( p_parser, "item", item, 512 )) )
1931 if( !*item ) continue;
1933 i_item = atoi( item );
1934 p_items = realloc( p_items, (i_nb_items + 1) *
1936 p_items[i_nb_items] = i_item;
1943 for( i = 0; i < i_nb_items; i++ )
1945 playlist_LockDelete( p_sys->p_playlist, p_items[i] );
1946 msg_Dbg( p_intf, "requested playlist delete: %d",
1952 if( p_items ) free( p_items );
1957 int i_item, *p_items = NULL, i_nb_items = 0;
1958 char item[512], *p_parser = p_request;
1961 /* Get the list of items to keep */
1963 uri_extract_value( p_parser, "item", item, 512 )) )
1965 if( !*item ) continue;
1967 i_item = atoi( item );
1968 p_items = realloc( p_items, (i_nb_items + 1) *
1970 p_items[i_nb_items] = i_item;
1974 for( i = p_sys->p_playlist->i_size - 1 ; i >= 0; i-- )
1976 /* Check if the item is in the keep list */
1977 for( j = 0 ; j < i_nb_items ; j++ )
1980 p_sys->p_playlist->pp_items[i]->input.i_id ) break;
1982 if( j == i_nb_items )
1984 playlist_LockDelete( p_sys->p_playlist, p_sys->p_playlist->pp_items[i]->input.i_id );
1985 msg_Dbg( p_intf, "requested playlist delete: %d",
1990 if( p_items ) free( p_items );
1995 playlist_LockClear( p_sys->p_playlist );
1996 msg_Dbg( p_intf, "requested playlist empty" );
2007 uri_extract_value( p_request, "type", type, 12 );
2008 uri_extract_value( p_request, "order", order, 2 );
2009 uri_extract_value( p_request, "item", item, 512 );
2010 i_item = atoi( item );
2012 if( order[0] == '0' ) i_order = ORDER_NORMAL;
2013 else i_order = ORDER_REVERSE;
2015 if( !strcmp( type , "title" ) )
2017 playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
2018 p_sys->p_playlist->pp_views[0]->p_root,
2019 SORT_TITLE_NODES_FIRST,
2020 ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
2021 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
2022 } else if( !strcmp( type , "author" ) )
2024 playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
2025 p_sys->p_playlist->pp_views[0]->p_root,
2027 ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
2028 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
2029 } else if( !strcmp( type , "shuffle" ) )
2031 playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
2032 p_sys->p_playlist->pp_views[0]->p_root,
2034 ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
2035 msg_Dbg( p_intf, "requested playlist shuffle");
2046 uri_extract_value( p_request, "psz_pos", psz_pos, 6 );
2047 uri_extract_value( p_request, "psz_newpos", psz_newpos, 6 );
2048 i_pos = atoi( psz_pos );
2049 i_newpos = atoi( psz_newpos );
2050 if ( i_pos < i_newpos )
2052 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
2054 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
2056 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
2060 /* admin function */
2064 uri_extract_value( p_request, "id", id, 512 );
2065 msg_Dbg( p_intf, "requested close id=%s", id );
2067 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
2069 msg_Warn( p_intf, "close failed for id=%s", id );
2076 msg_Dbg( p_intf, "requested shutdown" );
2077 p_intf->p_vlc->b_die = VLC_TRUE;
2082 case MVLC_VLM_SETUP:
2084 static const char *vlm_properties[11] =
2087 "enabled", "disabled", "loop", "unloop",
2089 "input", "output", "option", "date", "period", "repeat", "append",
2091 vlm_message_t *vlm_answer;
2093 char *psz = malloc( strlen( p_request ) + 1000 );
2098 if( p_intf->p_sys->p_vlm == NULL )
2099 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2101 if( p_intf->p_sys->p_vlm == NULL ) break;
2103 uri_extract_value( p_request, "name", name, 512 );
2104 if( StrToMacroType( control ) == MVLC_VLM_NEW )
2107 uri_extract_value( p_request, "type", type, 20 );
2108 p += sprintf( psz, "new %s %s", name, type );
2112 p += sprintf( psz, "setup %s", name );
2114 /* Parse the request */
2115 for( i = 0; i < 11; i++ )
2118 uri_extract_value( p_request, vlm_properties[i], val, 512 );
2119 uri_decode_url_encoded( val );
2120 if( strlen( val ) > 0 && i >= 4 )
2122 p += sprintf( p, " %s %s", vlm_properties[i], val );
2124 else if( uri_test_param( p_request, vlm_properties[i] ) && i < 4 )
2126 p += sprintf( p, " %s", vlm_properties[i] );
2129 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2130 if( vlm_answer->psz_value == NULL ) /* there is no error */
2132 vlm_error = strdup( "" );
2136 vlm_error = malloc( strlen(vlm_answer->psz_name) +
2137 strlen(vlm_answer->psz_value) +
2138 strlen( " : ") + 1 );
2139 sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
2140 vlm_answer->psz_value );
2143 mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
2145 vlm_MessageDelete( vlm_answer );
2153 vlm_message_t *vlm_answer;
2156 if( p_intf->p_sys->p_vlm == NULL )
2157 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2159 if( p_intf->p_sys->p_vlm == NULL ) break;
2161 uri_extract_value( p_request, "name", name, 512 );
2162 sprintf( psz, "del %s", name );
2164 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2165 /* FIXME do a vlm_answer -> var stack conversion */
2166 vlm_MessageDelete( vlm_answer );
2171 case MVLC_VLM_PAUSE:
2175 vlm_message_t *vlm_answer;
2178 if( p_intf->p_sys->p_vlm == NULL )
2179 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2181 if( p_intf->p_sys->p_vlm == NULL ) break;
2183 uri_extract_value( p_request, "name", name, 512 );
2184 if( StrToMacroType( control ) == MVLC_VLM_PLAY )
2185 sprintf( psz, "control %s play", name );
2186 else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
2187 sprintf( psz, "control %s pause", name );
2188 else if( StrToMacroType( control ) == MVLC_VLM_STOP )
2189 sprintf( psz, "control %s stop", name );
2190 else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
2193 uri_extract_value( p_request, "percent", percent, 512 );
2194 sprintf( psz, "control %s seek %s", name, percent );
2197 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2198 /* FIXME do a vlm_answer -> var stack conversion */
2199 vlm_MessageDelete( vlm_answer );
2205 vlm_message_t *vlm_answer;
2209 if( p_intf->p_sys->p_vlm == NULL )
2210 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2212 if( p_intf->p_sys->p_vlm == NULL ) break;
2214 uri_extract_value( p_request, "file", file, 512 );
2215 uri_decode_url_encoded( file );
2217 if( StrToMacroType( control ) == MVLC_VLM_LOAD )
2218 sprintf( psz, "load %s", file );
2220 sprintf( psz, "save %s", file );
2222 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2223 /* FIXME do a vlm_answer -> var stack conversion */
2224 vlm_MessageDelete( vlm_answer );
2231 PRINTS( "<!-- control param(%s) unsuported -->", control );
2243 if( i_request <= 0 ||
2244 *m->param1 == '\0' ||
2245 strstr( p_request, m->param1 ) == NULL )
2249 uri_extract_value( p_request, m->param1, value, 512 );
2250 uri_decode_url_encoded( value );
2252 switch( StrToMacroType( m->param2 ) )
2256 config_PutInt( p_intf, m->param1, i );
2260 config_PutFloat( p_intf, m->param1, f );
2263 config_PutPsz( p_intf, m->param1, value );
2266 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
2277 if( *m->param1 == '\0' )
2282 switch( StrToMacroType( m->param2 ) )
2285 i = config_GetInt( p_intf, m->param1 );
2286 sprintf( value, "%d", i );
2289 f = config_GetFloat( p_intf, m->param1 );
2290 sprintf( value, "%f", f );
2293 psz = config_GetPsz( p_intf, m->param1 );
2296 strncpy( value, psz,sizeof( value ) );
2298 value[sizeof( value ) - 1] = '\0';
2302 msg_Dbg( p_intf, "%d: value = \"%s\"", __LINE__, value );
2305 snprintf( value, sizeof( value ),
2306 "invalid type(%s) in set", m->param2 );
2307 value[sizeof( value ) - 1] = '\0';
2310 PRINTS( "%s", value );
2319 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2320 s = SSPop( &p_args->stack );
2321 v = mvar_GetValue( p_args->vars, s );
2325 v = s = SSPop( &p_args->stack );
2333 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2336 /* Usefull for learning stack management */
2340 msg_Dbg( p_intf, "stack" );
2341 for (i=0;i<(&p_args->stack)->i_stack;i++)
2342 msg_Dbg( p_intf, "%d -> %s", i, (&p_args->stack)->stack[i] );
2348 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2349 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2357 static char *MacroSearch( char *src, char *end, int i_mvlc, vlc_bool_t b_after )
2364 if( src + 4 < end && !strncmp( (char *)src, "<vlc", 4 ) )
2369 i_skip = MacroParse( &m, src );
2371 i_id = StrToMacroType( m.id );
2388 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2389 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2391 return src + ( b_after ? i_skip : 0 );
2393 else if( i_level < 0 )
2409 static void Execute( httpd_file_sys_t *p_args,
2410 char *p_request, int i_request,
2411 char **pp_data, int *pi_data,
2413 char *_src, char *_end )
2415 intf_thread_t *p_intf = p_args->p_intf;
2417 char *src, *dup, *end;
2418 char *dst = *pp_dst;
2420 src = dup = malloc( _end - _src + 1 );
2421 end = src +( _end - _src );
2423 memcpy( src, _src, _end - _src );
2426 /* we parse searching <vlc */
2432 p = (char *)strstr( (char *)src, "<vlc" );
2433 if( p < end && p == src )
2437 src += MacroParse( &m, src );
2439 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2441 switch( StrToMacroType( m.id ) )
2448 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2449 if( SSPopN( &p_args->stack, p_args->vars ) )
2457 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2461 char *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2465 char *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2468 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2472 else if( i_test == 1 )
2475 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2477 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2481 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2490 char *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2492 char *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2499 if( !strcmp( m.param2, "integer" ) )
2501 char *arg = SSPop( &p_args->stack );
2502 index = mvar_IntegerSetNew( m.param1, arg );
2505 else if( !strcmp( m.param2, "directory" ) )
2507 char *arg = SSPop( &p_args->stack );
2508 index = mvar_FileSetNew( m.param1, arg );
2511 else if( !strcmp( m.param2, "playlist" ) )
2513 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2515 else if( !strcmp( m.param2, "information" ) )
2517 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2519 else if( !strcmp( m.param2, "vlm" ) )
2521 if( p_intf->p_sys->p_vlm == NULL )
2522 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2523 index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
2526 else if( !strcmp( m.param2, "hosts" ) )
2528 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2530 else if( !strcmp( m.param2, "urls" ) )
2532 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2534 else if( !strcmp( m.param2, "connections" ) )
2536 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2539 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2541 index = mvar_Duplicate( v );
2545 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2550 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2552 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2554 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2557 f->name = strdup( m.param1 );
2560 mvar_PushVar( p_args->vars, f );
2561 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2562 mvar_RemoveVar( p_args->vars, f );
2566 mvar_Delete( index );
2573 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2581 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2584 int i_index = dst - *pp_data;
2587 *pp_data = realloc( *pp_data, *pi_data );
2588 dst = (*pp_data) + i_index;
2590 memcpy( dst, src, i_copy );
2600 /****************************************************************************
2602 ****************************************************************************
2603 * a file with b_html is parsed and all "macro" replaced
2604 * <vlc id="macro name" [param1="" [param2=""]] />
2607 ****************************************************************************/
2608 static int HttpCallback( httpd_file_sys_t *p_args,
2609 httpd_file_t *p_file,
2610 uint8_t *_p_request,
2611 uint8_t **_pp_data, int *pi_data )
2613 char *p_request = (char *)_p_request;
2614 char **pp_data = (char **)_pp_data;
2615 int i_request = p_request ? strlen( p_request ) : 0;
2619 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2621 p = *pp_data = malloc( 10240 );
2624 return VLC_EGENERIC;
2626 p += sprintf( p, "<html>\n" );
2627 p += sprintf( p, "<head>\n" );
2628 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2629 p += sprintf( p, "</head>\n" );
2630 p += sprintf( p, "<body>\n" );
2631 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2632 p += sprintf( p, "<hr />\n" );
2633 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2634 p += sprintf( p, "</body>\n" );
2635 p += sprintf( p, "</html>\n" );
2637 *pi_data = strlen( *pp_data );
2642 if( !p_args->b_html )
2644 FileLoad( f, pp_data, pi_data );
2652 char position[4]; /* percentage */
2653 char time[12]; /* in seconds */
2654 char length[12]; /* in seconds */
2655 audio_volume_t i_volume;
2659 #define p_sys p_args->p_intf->p_sys
2660 if( p_sys->p_input )
2662 var_Get( p_sys->p_input, "position", &val);
2663 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2664 var_Get( p_sys->p_input, "time", &val);
2665 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2666 var_Get( p_sys->p_input, "length", &val);
2667 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2669 var_Get( p_sys->p_input, "state", &val );
2670 if( val.i_int == PLAYING_S )
2672 sprintf( state, "playing" );
2673 } else if( val.i_int == PAUSE_S )
2675 sprintf( state, "paused" );
2678 sprintf( state, "stop" );
2682 sprintf( position, "%d", 0 );
2683 sprintf( time, "%d", 0 );
2684 sprintf( length, "%d", 0 );
2685 sprintf( state, "stop" );
2689 aout_VolumeGet( p_args->p_intf , &i_volume );
2690 sprintf( volume , "%d" , (int)i_volume );
2692 p_args->vars = mvar_New( "variables", "" );
2693 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2694 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2695 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2696 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2697 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2698 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2699 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2700 mvar_AppendNewVar( p_args->vars, "volume", volume );
2701 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2703 SSInit( &p_args->stack );
2705 /* first we load in a temporary buffer */
2706 FileLoad( f, &p_buffer, &i_buffer );
2708 /* allocate output */
2709 *pi_data = i_buffer + 1000;
2710 dst = *pp_data = malloc( *pi_data );
2712 /* we parse executing all <vlc /> macros */
2713 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2716 *pi_data = dst - *pp_data;
2718 SSClean( &p_args->stack );
2719 mvar_Delete( p_args->vars );
2728 /****************************************************************************
2730 ****************************************************************************/
2731 static int uri_test_param( char *psz_uri, const char *psz_name )
2735 while( (p = strstr( p, psz_name )) )
2737 /* Verify that we are dealing with a post/get argument */
2738 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2747 static char *uri_extract_value( char *psz_uri, const char *psz_name,
2748 char *psz_value, int i_value_max )
2752 while( (p = strstr( p, psz_name )) )
2754 /* Verify that we are dealing with a post/get argument */
2755 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2764 p += strlen( psz_name );
2765 if( *p == '=' ) p++;
2767 if( strchr( p, '&' ) )
2769 i_len = strchr( p, '&' ) - p;
2773 /* for POST method */
2774 if( strchr( p, '\n' ) )
2776 i_len = strchr( p, '\n' ) - p;
2777 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2781 i_len = strlen( p );
2784 i_len = __MIN( i_value_max - 1, i_len );
2787 strncpy( psz_value, p, i_len );
2788 psz_value[i_len] = '\0';
2792 strncpy( psz_value, "", i_value_max );
2798 strncpy( psz_value, "", i_value_max );
2804 static void uri_decode_url_encoded( char *psz )
2806 char *dup = strdup( psz );
2824 *psz++ = strtol( val, NULL, 16 );
2826 else if( *p == '+' )
2840 /****************************************************************************
2841 * Light RPN evaluator
2842 ****************************************************************************/
2843 static void SSInit( rpn_stack_t *st )
2848 static void SSClean( rpn_stack_t *st )
2850 while( st->i_stack > 0 )
2852 free( st->stack[--st->i_stack] );
2856 static void SSPush( rpn_stack_t *st, char *s )
2858 if( st->i_stack < STACK_MAX )
2860 st->stack[st->i_stack++] = strdup( s );
2864 static char * SSPop( rpn_stack_t *st )
2866 if( st->i_stack <= 0 )
2868 return strdup( "" );
2872 return st->stack[--st->i_stack];
2876 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2885 i = strtol( name, &end, 0 );
2888 value = mvar_GetValue( vars, name );
2896 static void SSPushN( rpn_stack_t *st, int i )
2900 sprintf( v, "%d", i );
2904 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2911 while( *exp == ' ' )
2918 /* extract string */
2921 while( *exp && *exp != '\'' )
2932 p = strchr( exp, ' ' );
2937 exp += strlen( exp );
2942 strncpy( s, exp, i );
2953 /* 1. Integer function */
2954 if( !strcmp( s, "!" ) )
2956 SSPushN( st, ~SSPopN( st, vars ) );
2958 else if( !strcmp( s, "^" ) )
2960 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2962 else if( !strcmp( s, "&" ) )
2964 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2966 else if( !strcmp( s, "|" ) )
2968 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2970 else if( !strcmp( s, "+" ) )
2972 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2974 else if( !strcmp( s, "-" ) )
2976 int j = SSPopN( st, vars );
2977 int i = SSPopN( st, vars );
2978 SSPushN( st, i - j );
2980 else if( !strcmp( s, "*" ) )
2982 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2984 else if( !strcmp( s, "/" ) )
2988 j = SSPopN( st, vars );
2989 i = SSPopN( st, vars );
2991 SSPushN( st, j != 0 ? i / j : 0 );
2993 else if( !strcmp( s, "%" ) )
2997 j = SSPopN( st, vars );
2998 i = SSPopN( st, vars );
3000 SSPushN( st, j != 0 ? i % j : 0 );
3002 /* 2. integer tests */
3003 else if( !strcmp( s, "=" ) )
3005 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
3007 else if( !strcmp( s, "!=" ) )
3009 SSPushN( st, SSPopN( st, vars ) != SSPopN( st, vars ) ? -1 : 0 );
3011 else if( !strcmp( s, "<" ) )
3013 int j = SSPopN( st, vars );
3014 int i = SSPopN( st, vars );
3016 SSPushN( st, i < j ? -1 : 0 );
3018 else if( !strcmp( s, ">" ) )
3020 int j = SSPopN( st, vars );
3021 int i = SSPopN( st, vars );
3023 SSPushN( st, i > j ? -1 : 0 );
3025 else if( !strcmp( s, "<=" ) )
3027 int j = SSPopN( st, vars );
3028 int i = SSPopN( st, vars );
3030 SSPushN( st, i <= j ? -1 : 0 );
3032 else if( !strcmp( s, ">=" ) )
3034 int j = SSPopN( st, vars );
3035 int i = SSPopN( st, vars );
3037 SSPushN( st, i >= j ? -1 : 0 );
3039 /* 3. string functions */
3040 else if( !strcmp( s, "strcat" ) )
3042 char *s2 = SSPop( st );
3043 char *s1 = SSPop( st );
3044 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
3054 else if( !strcmp( s, "strcmp" ) )
3056 char *s2 = SSPop( st );
3057 char *s1 = SSPop( st );
3059 SSPushN( st, strcmp( s1, s2 ) );
3063 else if( !strcmp( s, "strncmp" ) )
3065 int n = SSPopN( st, vars );
3066 char *s2 = SSPop( st );
3067 char *s1 = SSPop( st );
3069 SSPushN( st, strncmp( s1, s2 , n ) );
3073 else if( !strcmp( s, "strsub" ) )
3075 int n = SSPopN( st, vars );
3076 int m = SSPopN( st, vars );
3078 char *s = SSPop( st );
3090 str = malloc( i_len + 1 );
3092 memcpy( str, s + m - 1, i_len );
3093 str[ i_len ] = '\0';
3099 else if( !strcmp( s, "strlen" ) )
3101 char *str = SSPop( st );
3103 SSPushN( st, strlen( str ) );
3106 /* 4. stack functions */
3107 else if( !strcmp( s, "dup" ) )
3109 char *str = SSPop( st );
3114 else if( !strcmp( s, "drop" ) )
3116 char *str = SSPop( st );
3119 else if( !strcmp( s, "swap" ) )
3121 char *s1 = SSPop( st );
3122 char *s2 = SSPop( st );
3129 else if( !strcmp( s, "flush" ) )
3134 else if( !strcmp( s, "store" ) )
3136 char *value = SSPop( st );
3137 char *name = SSPop( st );
3139 mvar_PushNewVar( vars, name, value );
3143 else if( !strcmp( s, "value" ) )
3145 char *name = SSPop( st );
3146 char *value = mvar_GetValue( vars, name );
3148 SSPush( st, value );
3152 else if( !strcmp( s, "url_extract" ) )
3154 char *url = mvar_GetValue( vars, "url_value" );
3155 char *name = SSPop( st );
3158 uri_extract_value( url, name, value, 512 );
3159 uri_decode_url_encoded( value );
3160 SSPush( st, value );
3162 else if( !strcmp( s, "url_encode" ) )
3164 char *url = SSPop( st );
3167 value = vlc_UrlEncode( url );
3168 SSPush( st, value );
3178 /**********************************************************************
3179 * Find_end_MRL: Find the end of the sentence :
3180 * this function parses the string psz and find the end of the item
3181 * and/or option with detecting the " and ' problems.
3182 * returns NULL if an error is detected, otherwise, returns a pointer
3183 * of the end of the sentence (after the last character)
3184 **********************************************************************/
3185 static char *Find_end_MRL( char *psz )
3195 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
3197 if( *s_sent == '\'' )
3199 s_sent = Find_end_MRL( s_sent );
3201 if( s_sent == NULL )
3211 if( *s_sent == '\"' )
3215 } else /* *s_sent == '\0' , which means the number of " is incorrect */
3225 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
3227 if( *s_sent == '\"' )
3229 s_sent = Find_end_MRL( s_sent );
3231 if( s_sent == NULL )
3241 if( *s_sent == '\'' )
3245 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
3251 default: /* now we can look for spaces */
3253 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
3255 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
3257 s_sent = Find_end_MRL( s_sent );
3268 /**********************************************************************
3269 * parse_MRL: parse the MRL, find the mrl string and the options,
3270 * create an item with all information in it, and return the item.
3271 * return NULL if there is an error.
3272 **********************************************************************/
3273 static playlist_item_t *parse_MRL( intf_thread_t *p_intf, char *psz )
3275 char **ppsz_options = NULL;
3282 playlist_item_t * p_item = NULL;
3284 /* In case there is spaces before the mrl */
3285 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
3290 /* extract the mrl */
3291 s_temp = strstr( s_mrl , " :" );
3292 if( s_temp == NULL )
3294 s_temp = s_mrl + strlen( s_mrl );
3297 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
3304 /* if the mrl is between " or ', we must remove them */
3305 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
3307 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
3308 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
3309 mrl[ s_temp - s_mrl - 2 ] = '\0';
3312 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
3313 strncpy( mrl , s_mrl , s_temp - s_mrl );
3314 mrl[ s_temp - s_mrl ] = '\0';
3319 /* now we can take care of the options */
3320 while( (*s_mrl != '\0') && (i_error == 0) )
3329 case ':': /* an option */
3331 s_temp = Find_end_MRL( s_mrl );
3333 if( s_temp == NULL )
3340 ppsz_options = realloc( ppsz_options , i_options *
3342 ppsz_options[ i_options - 1 ] =
3343 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
3345 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
3348 /* don't forget to finish the string with a '\0' */
3349 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
3369 /* now create an item */
3370 p_item = playlist_ItemNew( p_intf, mrl, mrl);
3371 for( i = 0 ; i< i_options ; i++ )
3373 playlist_ItemAddOption( p_item, ppsz_options[i] );
3377 for( i = 0; i < i_options; i++ ) free( ppsz_options[i] );
3378 if( i_options ) free( ppsz_options );