http.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:96k
- /*****************************************************************************
- * http.c : http mini-server ;)
- *****************************************************************************
- * Copyright (C) 2001-2004 VideoLAN
- * $Id: http.c 9281 2004-11-11 12:45:53Z zorglub $
- *
- * Authors: Gildas Bazin <gbazin@netcourrier.com>
- * Laurent Aimar <fenrir@via.ecp.fr>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
- *****************************************************************************/
- /*****************************************************************************
- * Preamble
- *****************************************************************************/
- /* TODO:
- * - clean up ?
- * - doc ! (mouarf ;)
- */
- #include <stdlib.h>
- #include <vlc/vlc.h>
- #include <vlc/intf.h>
- #include <vlc/aout.h>
- #include <vlc/vout.h> /* for fullscreen */
- #include "vlc_httpd.h"
- #include "vlc_vlm.h"
- #include "vlc_tls.h"
- #ifdef HAVE_SYS_STAT_H
- # include <sys/stat.h>
- #endif
- #ifdef HAVE_ERRNO_H
- # include <errno.h>
- #endif
- #ifdef HAVE_FCNTL_H
- # include <fcntl.h>
- #endif
- #ifdef HAVE_UNISTD_H
- # include <unistd.h>
- #elif defined( WIN32 ) && !defined( UNDER_CE )
- # include <io.h>
- #endif
- #if (!defined( WIN32 ) || defined(__MINGW32__))
- /* Mingw has its own version of dirent */
- # include <dirent.h>
- #endif
- /* stat() support for large files on win32 */
- #if defined( WIN32 ) && !defined( UNDER_CE )
- # define stat _stati64
- #endif
- /*****************************************************************************
- * Module descriptor
- *****************************************************************************/
- static int Open ( vlc_object_t * );
- static void Close( vlc_object_t * );
- #define HOST_TEXT N_( "Host address" )
- #define HOST_LONGTEXT N_(
- "You can set the address and port the http interface will bind to." )
- #define SRC_TEXT N_( "Source directory" )
- #define SRC_LONGTEXT N_( "Source directory" )
- #define CERT_TEXT N_( "Certificate file" )
- #define CERT_LONGTEXT N_( "HTTP interface x509 PEM certificate file "
- "(enables SSL)" )
- #define KEY_TEXT N_( "Private key file" )
- #define KEY_LONGTEXT N_( "HTTP interface x509 PEM private key file" )
- #define CA_TEXT N_( "Root CA file" )
- #define CA_LONGTEXT N_( "HTTP interface x509 PEM trusted root CA "
- "certificates file" )
- #define CRL_TEXT N_( "CRL file" )
- #define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file" )
- vlc_module_begin();
- set_description( _("HTTP remote control interface") );
- add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
- add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
- add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
- add_string ( "http-intf-key", NULL, NULL, KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
- add_string ( "http-intf-ca", NULL, NULL, CA_TEXT, CA_LONGTEXT, VLC_TRUE );
- add_string ( "http-intf-crl", NULL, NULL, CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
- set_capability( "interface", 0 );
- set_callbacks( Open, Close );
- vlc_module_end();
- /*****************************************************************************
- * Local prototypes
- *****************************************************************************/
- static void Run ( intf_thread_t *p_intf );
- static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
- char *psz_dir );
- static int DirectoryCheck( char *psz_dir )
- {
- DIR *p_dir;
- #ifdef HAVE_SYS_STAT_H
- struct stat stat_info;
- if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
- {
- return VLC_EGENERIC;
- }
- #endif
- if( ( p_dir = opendir( psz_dir ) ) == NULL )
- {
- return VLC_EGENERIC;
- }
- closedir( p_dir );
- return VLC_SUCCESS;
- }
- static int HttpCallback( httpd_file_sys_t *p_args,
- httpd_file_t *,
- uint8_t *p_request,
- uint8_t **pp_data, int *pi_data );
- static char *uri_extract_value( char *psz_uri, const char *psz_name,
- char *psz_value, int i_value_max );
- static int uri_test_param( char *psz_uri, const char *psz_name );
- static void uri_decode_url_encoded( char *psz );
- static char *Find_end_MRL( char *psz );
- static playlist_item_t * parse_MRL( intf_thread_t * , char *psz );
- /*****************************************************************************
- *
- *****************************************************************************/
- typedef struct mvar_s
- {
- char *name;
- char *value;
- int i_field;
- struct mvar_s **field;
- } mvar_t;
- #define STACK_MAX 100
- typedef struct
- {
- char *stack[STACK_MAX];
- int i_stack;
- } rpn_stack_t;
- struct httpd_file_sys_t
- {
- intf_thread_t *p_intf;
- httpd_file_t *p_file;
- httpd_redirect_t *p_redir;
- char *file;
- char *name;
- vlc_bool_t b_html;
- /* inited for each access */
- rpn_stack_t stack;
- mvar_t *vars;
- };
- struct intf_sys_t
- {
- httpd_host_t *p_httpd_host;
- int i_files;
- httpd_file_sys_t **pp_files;
- playlist_t *p_playlist;
- input_thread_t *p_input;
- vlm_t *p_vlm;
- };
- /*****************************************************************************
- * Activate: initialize and create stuff
- *****************************************************************************/
- static int Open( vlc_object_t *p_this )
- {
- intf_thread_t *p_intf = (intf_thread_t*)p_this;
- intf_sys_t *p_sys;
- char *psz_host;
- char *psz_address = "";
- const char *psz_cert;
- int i_port = 0;
- char *psz_src;
- tls_server_t *p_tls;
- psz_host = config_GetPsz( p_intf, "http-host" );
- if( psz_host )
- {
- char *psz_parser;
- psz_address = psz_host;
- psz_parser = strchr( psz_host, ':' );
- if( psz_parser )
- {
- *psz_parser++ = ' ';
- i_port = atoi( psz_parser );
- }
- }
- p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
- if( !p_intf->p_sys )
- {
- return( VLC_ENOMEM );
- }
- p_sys->p_playlist = NULL;
- p_sys->p_input = NULL;
- p_sys->p_vlm = NULL;
- psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
- if ( psz_cert != NULL )
- {
- const char *psz_pem;
- msg_Dbg( p_intf, "enablind TLS for HTTP interface (cert file: %s)",
- psz_cert );
- psz_pem = config_GetPsz( p_intf, "http-intf-key" );
- p_tls = tls_ServerCreate( p_this, psz_cert, psz_pem );
- if ( p_tls == NULL )
- {
- msg_Err( p_intf, "TLS initialization error" );
- free( p_sys );
- return VLC_EGENERIC;
- }
- psz_pem = config_GetPsz( p_intf, "http-intf-ca" );
- if ( ( psz_pem != NULL) && tls_ServerAddCA( p_tls, psz_pem ) )
- {
- msg_Err( p_intf, "TLS CA error" );
- tls_ServerDelete( p_tls );
- free( p_sys );
- return VLC_EGENERIC;
- }
- psz_pem = config_GetPsz( p_intf, "http-intf-crl" );
- if ( ( psz_pem != NULL) && tls_ServerAddCRL( p_tls, psz_pem ) )
- {
- msg_Err( p_intf, "TLS CRL error" );
- tls_ServerDelete( p_tls );
- free( p_sys );
- return VLC_EGENERIC;
- }
- if( i_port <= 0 )
- i_port = 8443;
- }
- else
- {
- p_tls = NULL;
- if( i_port <= 0 )
- i_port= 8080;
- }
- msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
- p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
- i_port, p_tls );
- if( p_sys->p_httpd_host == NULL )
- {
- msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
- if ( p_tls != NULL )
- tls_ServerDelete( p_tls );
- free( p_sys );
- return VLC_EGENERIC;
- }
- if( psz_host )
- {
- free( psz_host );
- }
- p_sys->i_files = 0;
- p_sys->pp_files = NULL;
- #if defined(SYS_DARWIN) || defined(SYS_BEOS) ||
- ( defined(WIN32) && !defined(UNDER_CE ) )
- if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
- {
- char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
- psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
- if( !psz_src )
- {
- return VLC_ENOMEM;
- }
- #if defined(WIN32)
- sprintf( psz_src, "%s/http", psz_vlcpath);
- #else
- sprintf( psz_src, "%s/share/http", psz_vlcpath);
- #endif
- }
- #else
- psz_src = config_GetPsz( p_intf, "http-src" );
- if( !psz_src || *psz_src == ' ' )
- {
- if( !DirectoryCheck( "share/http" ) )
- {
- psz_src = strdup( "share/http" );
- }
- else if( !DirectoryCheck( DATA_PATH "/http" ) )
- {
- psz_src = strdup( DATA_PATH "/http" );
- }
- }
- #endif
- if( !psz_src || *psz_src == ' ' )
- {
- msg_Err( p_intf, "invalid src dir" );
- goto failed;
- }
- /* remove trainling or / */
- if( psz_src[strlen( psz_src ) - 1] == '\' ||
- psz_src[strlen( psz_src ) - 1] == '/' )
- {
- psz_src[strlen( psz_src ) - 1] = ' ';
- }
- ParseDirectory( p_intf, psz_src, psz_src );
- if( p_sys->i_files <= 0 )
- {
- msg_Err( p_intf, "cannot find any files (%s)", psz_src );
- goto failed;
- }
- p_intf->pf_run = Run;
- free( psz_src );
- return VLC_SUCCESS;
- failed:
- if( psz_src ) free( psz_src );
- if( p_sys->pp_files )
- {
- free( p_sys->pp_files );
- }
- httpd_HostDelete( p_sys->p_httpd_host );
- free( p_sys );
- return VLC_EGENERIC;
- }
- /*****************************************************************************
- * CloseIntf: destroy interface
- *****************************************************************************/
- void Close ( vlc_object_t *p_this )
- {
- intf_thread_t *p_intf = (intf_thread_t *)p_this;
- intf_sys_t *p_sys = p_intf->p_sys;
- int i;
- if( p_sys->p_vlm )
- {
- vlm_Delete( p_sys->p_vlm );
- }
- for( i = 0; i < p_sys->i_files; i++ )
- {
- httpd_FileDelete( p_sys->pp_files[i]->p_file );
- if( p_sys->pp_files[i]->p_redir )
- {
- httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
- }
- free( p_sys->pp_files[i]->file );
- free( p_sys->pp_files[i]->name );
- free( p_sys->pp_files[i] );
- }
- if( p_sys->pp_files )
- {
- free( p_sys->pp_files );
- }
- httpd_HostDelete( p_sys->p_httpd_host );
- free( p_sys );
- }
- /*****************************************************************************
- * Run: http interface thread
- *****************************************************************************/
- static void Run( intf_thread_t *p_intf )
- {
- intf_sys_t *p_sys = p_intf->p_sys;
- while( !p_intf->b_die )
- {
- /* get the playlist */
- if( p_sys->p_playlist == NULL )
- {
- p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
- }
- /* Manage the input part */
- if( p_sys->p_input == NULL )
- {
- if( p_sys->p_playlist )
- {
- p_sys->p_input =
- vlc_object_find( p_sys->p_playlist,
- VLC_OBJECT_INPUT,
- FIND_CHILD );
- }
- }
- else if( p_sys->p_input->b_dead )
- {
- vlc_object_release( p_sys->p_input );
- p_sys->p_input = NULL;
- }
- /* Wait a bit */
- msleep( INTF_IDLE_SLEEP );
- }
- if( p_sys->p_input )
- {
- vlc_object_release( p_sys->p_input );
- p_sys->p_input = NULL;
- }
- if( p_sys->p_playlist )
- {
- vlc_object_release( p_sys->p_playlist );
- p_sys->p_playlist = NULL;
- }
- }
- /*****************************************************************************
- * Local functions
- *****************************************************************************/
- #define MAX_DIR_SIZE 10240
- /****************************************************************************
- * FileToUrl: create a good name for an url from filename
- ****************************************************************************/
- static char *FileToUrl( char *name )
- {
- char *url, *p;
- url = p = malloc( strlen( name ) + 1 );
- if( !url || !p )
- {
- return NULL;
- }
- #ifdef WIN32
- while( *name == '\' || *name == '/' )
- #else
- while( *name == '/' )
- #endif
- {
- name++;
- }
- *p++ = '/';
- strcpy( p, name );
- #ifdef WIN32
- /* convert '\' into '/' */
- name = p;
- while( *name )
- {
- if( *name == '\' )
- {
- *p++ = '/';
- }
- name++;
- }
- #endif
- /* index.* -> / */
- if( ( p = strrchr( url, '/' ) ) != NULL )
- {
- if( !strncmp( p, "/index.", 7 ) )
- {
- p[1] = ' ';
- }
- }
- return url;
- }
- /****************************************************************************
- * ParseDirectory: parse recursively a directory, adding each file
- ****************************************************************************/
- static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
- char *psz_dir )
- {
- intf_sys_t *p_sys = p_intf->p_sys;
- char dir[MAX_DIR_SIZE];
- #ifdef HAVE_SYS_STAT_H
- struct stat stat_info;
- #endif
- DIR *p_dir;
- struct dirent *p_dir_content;
- FILE *file;
- char *user = NULL;
- char *password = NULL;
- #ifdef HAVE_SYS_STAT_H
- if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
- {
- return VLC_EGENERIC;
- }
- #endif
- if( ( p_dir = opendir( psz_dir ) ) == NULL )
- {
- msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
- return VLC_EGENERIC;
- }
- msg_Dbg( p_intf, "dir=%s", psz_dir );
- sprintf( dir, "%s/.access", psz_dir );
- if( ( file = fopen( dir, "r" ) ) != NULL )
- {
- char line[1024];
- int i_size;
- msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
- i_size = fread( line, 1, 1023, file );
- if( i_size > 0 )
- {
- char *p;
- while( i_size > 0 && ( line[i_size-1] == 'n' ||
- line[i_size-1] == 'r' ) )
- {
- i_size--;
- }
- line[i_size] = ' ';
- p = strchr( line, ':' );
- if( p )
- {
- *p++ = ' ';
- user = strdup( line );
- password = strdup( p );
- }
- }
- msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
- user, password, i_size );
- fclose( file );
- }
- for( ;; )
- {
- /* parse psz_src dir */
- if( ( p_dir_content = readdir( p_dir ) ) == NULL )
- {
- break;
- }
- if( p_dir_content->d_name[0] == '.' )
- {
- continue;
- }
- sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
- if( ParseDirectory( p_intf, psz_root, dir ) )
- {
- httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
- f->p_intf = p_intf;
- f->p_file = NULL;
- f->p_redir = NULL;
- f->file = strdup( dir );
- f->name = FileToUrl( &dir[strlen( psz_root )] );
- f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
- if( !f->name )
- {
- msg_Err( p_intf , "unable to parse directory" );
- closedir( p_dir );
- free( f );
- return( VLC_ENOMEM );
- }
- msg_Dbg( p_intf, "file=%s (url=%s)",
- f->file, f->name );
- f->p_file = httpd_FileNew( p_sys->p_httpd_host,
- f->name, f->b_html ? "text/html" : NULL,
- user, password,
- HttpCallback, f );
- if( f->p_file )
- {
- TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
- }
- /* For rep/ add a redir from rep to rep/ */
- if( f && f->name[strlen(f->name) - 1] == '/' )
- {
- char *psz_redir = strdup( f->name );
- psz_redir[strlen( psz_redir ) - 1] = ' ';
- msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
- f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
- free( psz_redir );
- }
- }
- }
- if( user )
- {
- free( user );
- }
- if( password )
- {
- free( password );
- }
- closedir( p_dir );
- return VLC_SUCCESS;
- }
- /****************************************************************************
- * var and set handling
- ****************************************************************************/
- static mvar_t *mvar_New( char *name, char *value )
- {
- mvar_t *v = malloc( sizeof( mvar_t ) );
- if( !v ) return NULL;
- v->name = strdup( name );
- v->value = strdup( value ? value : "" );
- v->i_field = 0;
- v->field = malloc( sizeof( mvar_t * ) );
- v->field[0] = NULL;
- return v;
- }
- static void mvar_Delete( mvar_t *v )
- {
- int i;
- free( v->name );
- free( v->value );
- for( i = 0; i < v->i_field; i++ )
- {
- mvar_Delete( v->field[i] );
- }
- free( v->field );
- free( v );
- }
- static void mvar_AppendVar( mvar_t *v, mvar_t *f )
- {
- v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
- v->field[v->i_field] = f;
- v->i_field++;
- }
- static mvar_t *mvar_Duplicate( mvar_t *v )
- {
- int i;
- mvar_t *n;
- n = mvar_New( v->name, v->value );
- for( i = 0; i < v->i_field; i++ )
- {
- mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
- }
- return n;
- }
- static void mvar_PushVar( mvar_t *v, mvar_t *f )
- {
- v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
- if( v->i_field > 0 )
- {
- memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
- }
- v->field[0] = f;
- v->i_field++;
- }
- static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
- {
- int i;
- for( i = 0; i < v->i_field; i++ )
- {
- if( v->field[i] == f )
- {
- break;
- }
- }
- if( i >= v->i_field )
- {
- return;
- }
- if( i + 1 < v->i_field )
- {
- memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
- }
- v->i_field--;
- /* FIXME should do a realloc */
- }
- static mvar_t *mvar_GetVar( mvar_t *s, char *name )
- {
- int i;
- char base[512], *field, *p;
- int i_index;
- /* format: name[index].field */
- field = strchr( name, '.' );
- if( field )
- {
- int i = field - name;
- strncpy( base, name, i );
- base[i] = ' ';
- field++;
- }
- else
- {
- strcpy( base, name );
- }
- if( ( p = strchr( base, '[' ) ) )
- {
- *p++ = ' ';
- sscanf( p, "%d]", &i_index );
- if( i_index < 0 )
- {
- return NULL;
- }
- }
- else
- {
- i_index = 0;
- }
- for( i = 0; i < s->i_field; i++ )
- {
- if( !strcmp( s->field[i]->name, base ) )
- {
- if( i_index > 0 )
- {
- i_index--;
- }
- else
- {
- if( field )
- {
- return mvar_GetVar( s->field[i], field );
- }
- else
- {
- return s->field[i];
- }
- }
- }
- }
- return NULL;
- }
- static char *mvar_GetValue( mvar_t *v, char *field )
- {
- if( *field == ' ' )
- {
- return v->value;
- }
- else
- {
- mvar_t *f = mvar_GetVar( v, field );
- if( f )
- {
- return f->value;
- }
- else
- {
- return field;
- }
- }
- }
- static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
- {
- mvar_t *f = mvar_New( name, value );
- mvar_PushVar( vars, f );
- }
- static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
- {
- mvar_t *f = mvar_New( name, value );
- mvar_AppendVar( vars, f );
- }
- /* arg= start[:stop[:step]],.. */
- static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
- {
- char *dup = strdup( arg );
- char *str = dup;
- mvar_t *s = mvar_New( name, "set" );
- fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'n", name, str );
- while( str )
- {
- char *p;
- int i_start,i_stop,i_step;
- int i_match;
- p = strchr( str, ',' );
- if( p )
- {
- *p++ = ' ';
- }
- i_step = 0;
- i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
- fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%dn", i_match, i_start, i_stop, i_step );
- if( i_match == 1 )
- {
- i_stop = i_start;
- i_step = 1;
- }
- else if( i_match == 2 )
- {
- i_step = i_start < i_stop ? 1 : -1;
- }
- if( i_match >= 1 )
- {
- int i;
- if( ( i_start < i_stop && i_step > 0 ) ||
- ( i_start > i_stop && i_step < 0 ) )
- {
- for( i = i_start; ; i += i_step )
- {
- char value[512];
- if( ( i_step > 0 && i > i_stop ) ||
- ( i_step < 0 && i < i_stop ) )
- {
- break;
- }
- fprintf( stderr," mvar_IntegerSetNew: adding %dn", i );
- sprintf( value, "%d", i );
- mvar_PushNewVar( s, name, value );
- }
- }
- }
- str = p;
- }
- free( dup );
- return s;
- }
- static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
- {
- mvar_t *s = mvar_New( name, "set" );
- int i;
- fprintf( stderr," mvar_PlaylistSetNew: name=`%s'n", name );
- vlc_mutex_lock( &p_pl->object_lock );
- for( i = 0; i < p_pl->i_size; i++ )
- {
- mvar_t *itm = mvar_New( name, "set" );
- char value[512];
- sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
- mvar_AppendNewVar( itm, "current", value );
- sprintf( value, "%d", i );
- mvar_AppendNewVar( itm, "index", value );
- mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->input.psz_name );
- mvar_AppendNewVar( itm, "uri", p_pl->pp_items[i]->input.psz_uri );
- sprintf( value, "%d", p_pl->pp_items[i]->i_group );
- mvar_AppendNewVar( itm, "group", value );
- mvar_AppendVar( s, itm );
- }
- vlc_mutex_unlock( &p_pl->object_lock );
- return s;
- }
- static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
- {
- mvar_t *s = mvar_New( name, "set" );
- int i, j;
- fprintf( stderr," mvar_InfoSetNew: name=`%s'n", name );
- if( p_input == NULL )
- {
- return s;
- }
- vlc_mutex_lock( &p_input->input.p_item->lock );
- for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
- {
- info_category_t *p_category = p_input->input.p_item->pp_categories[i];
- mvar_t *cat = mvar_New( name, "set" );
- mvar_t *iset = mvar_New( "info", "set" );
- mvar_AppendNewVar( cat, "name", p_category->psz_name );
- mvar_AppendVar( cat, iset );
- for ( j = 0; j < p_category->i_infos; j++ )
- {
- info_t *p_info = p_category->pp_infos[j];
- mvar_t *info = mvar_New( "info", "" );
- msg_Dbg( p_input, "adding info name=%s value=%s",
- p_info->psz_name, p_info->psz_value );
- mvar_AppendNewVar( info, "name", p_info->psz_name );
- mvar_AppendNewVar( info, "value", p_info->psz_value );
- mvar_AppendVar( iset, info );
- }
- mvar_AppendVar( s, cat );
- }
- vlc_mutex_unlock( &p_input->input.p_item->lock );
- return s;
- }
- #if 0
- static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
- {
- mvar_t *s = mvar_New( name, "set" );
- httpd_info_t info;
- int i;
- fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'n", name );
- if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
- {
- for( i= 0; i < info.i_count; )
- {
- mvar_t *inf;
- inf = mvar_New( name, "set" );
- do
- {
- /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'n",
- info.info[i].psz_name, info.info[i].psz_value ); */
- mvar_AppendNewVar( inf,
- info.info[i].psz_name,
- info.info[i].psz_value );
- i++;
- } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
- mvar_AppendVar( s, inf );
- }
- }
- /* free mem */
- for( i = 0; i < info.i_count; i++ )
- {
- free( info.info[i].psz_name );
- free( info.info[i].psz_value );
- }
- if( info.i_count > 0 )
- {
- free( info.info );
- }
- return s;
- }
- #endif
- static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
- {
- mvar_t *s = mvar_New( name, "set" );
- char tmp[MAX_DIR_SIZE], *p, *src;
- #ifdef HAVE_SYS_STAT_H
- struct stat stat_info;
- #endif
- DIR *p_dir;
- struct dirent *p_dir_content;
- char sep;
- /* convert all / to native separator */
- #if defined( WIN32 )
- while( (p = strchr( psz_dir, '/' )) )
- {
- *p = '\';
- }
- sep = '\';
- #else
- sep = '/';
- #endif
- /* remove trailling separator */
- while( strlen( psz_dir ) > 1 &&
- #if defined( WIN32 )
- !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
- #endif
- psz_dir[strlen( psz_dir ) -1 ] == sep )
- {
- psz_dir[strlen( psz_dir ) -1 ] =' ';
- }
- /* remove double separator */
- for( p = src = psz_dir; *src != ' '; src++, p++ )
- {
- if( src[0] == sep && src[1] == sep )
- {
- src++;
- }
- *p = *src;
- }
- *p = ' ';
- if( *psz_dir == ' ' )
- {
- return s;
- }
- /* first fix all .. dir */
- p = src = psz_dir;
- while( *src )
- {
- if( src[0] == '.' && src[1] == '.' )
- {
- src += 2;
- if( p <= &psz_dir[1] )
- {
- continue;
- }
- p -= 2;
- while( p > &psz_dir[1] && *p != sep )
- {
- p--;
- }
- }
- else if( *src == sep )
- {
- if( p > psz_dir && p[-1] == sep )
- {
- src++;
- }
- else
- {
- *p++ = *src++;
- }
- }
- else
- {
- do
- {
- *p++ = *src++;
- } while( *src && *src != sep );
- }
- }
- *p = ' ';
- fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'n", name, psz_dir );
- #ifdef HAVE_SYS_STAT_H
- if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
- {
- return s;
- }
- #endif
- if( ( p_dir = opendir( psz_dir ) ) == NULL )
- {
- fprintf( stderr, "cannot open dir (%s)", psz_dir );
- return s;
- }
- /* remove traling / or */
- for( p = &psz_dir[strlen( psz_dir) - 1];
- p >= psz_dir && ( *p =='/' || *p =='\' ); p-- )
- {
- *p = ' ';
- }
- for( ;; )
- {
- mvar_t *f;
- /* parse psz_src dir */
- if( ( p_dir_content = readdir( p_dir ) ) == NULL )
- {
- break;
- }
- if( !strcmp( p_dir_content->d_name, "." ) )
- {
- continue;
- }
- sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
- #ifdef HAVE_SYS_STAT_H
- if( stat( tmp, &stat_info ) == -1 )
- {
- continue;
- }
- #endif
- f = mvar_New( name, "set" );
- mvar_AppendNewVar( f, "name", tmp );
- #ifdef HAVE_SYS_STAT_H
- if( S_ISDIR( stat_info.st_mode ) )
- {
- mvar_AppendNewVar( f, "type", "directory" );
- }
- else if( S_ISREG( stat_info.st_mode ) )
- {
- mvar_AppendNewVar( f, "type", "file" );
- }
- else
- {
- mvar_AppendNewVar( f, "type", "unknown" );
- }
- sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
- mvar_AppendNewVar( f, "size", tmp );
- /* FIXME memory leak FIXME */
- #ifdef HAVE_CTIME_R
- ctime_r( &stat_info.st_mtime, tmp );
- mvar_AppendNewVar( f, "date", tmp );
- #else
- mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
- #endif
- #else
- mvar_AppendNewVar( f, "type", "unknown" );
- mvar_AppendNewVar( f, "size", "unknown" );
- mvar_AppendNewVar( f, "date", "unknown" );
- #endif
- mvar_AppendVar( s, f );
- }
- return s;
- }
- static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
- {
- mvar_t *s = mvar_New( name, "set" );
- vlm_message_t *msg;
- int i;
- /* fprintf( stderr," mvar_VlmSetNew: name=`%s'n", name ); */
- if( vlm == NULL ) return s;
- if( vlm_ExecuteCommand( vlm, "show", &msg ) )
- {
- return s;
- }
- for( i = 0; i < msg->i_child; i++ )
- {
- /* Over media, schedule */
- vlm_message_t *ch = msg->child[i];
- int j;
- for( j = 0; j < ch->i_child; j++ )
- {
- /* Over name */
- vlm_message_t *el = ch->child[j];
- vlm_message_t *inf, *desc;
- mvar_t *set;
- char psz[500];
- int k;
- sprintf( psz, "show %s", el->psz_name );
- if( vlm_ExecuteCommand( vlm, psz, &inf ) )
- continue;
- desc = inf->child[0];
- /* Add a node with name and info */
- set = mvar_New( name, "set" );
- mvar_AppendNewVar( set, "name", el->psz_name );
- /* fprintf( stderr, "#### name=%sn", el->psz_name ); */
- for( k = 0; k < desc->i_child; k++ )
- {
- vlm_message_t *ch = desc->child[k];
- if( ch->i_child > 0 )
- {
- int c;
- mvar_t *n = mvar_New( ch->psz_name, "set" );
- /* fprintf( stderr, " child=%s [%d]n", ch->psz_name, ch->i_child ); */
- for( c = 0; c < ch->i_child; c++ )
- {
- mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
- mvar_AppendVar( n, in );
- /* fprintf( stderr, " sub=%sn", ch->child[c]->psz_name );*/
- }
- mvar_AppendVar( set, n );
- }
- else
- {
- /* fprintf( stderr, " child=%s->%sn", ch->psz_name, ch->psz_value ); */
- mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
- }
- }
- vlm_MessageDelete( inf );
- mvar_AppendVar( s, set );
- }
- }
- vlm_MessageDelete( msg );
- return s;
- }
- static void SSInit( rpn_stack_t * );
- static void SSClean( rpn_stack_t * );
- static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
- static void SSPush ( rpn_stack_t *, char * );
- static char *SSPop ( rpn_stack_t * );
- static void SSPushN ( rpn_stack_t *, int );
- static int SSPopN ( rpn_stack_t *, mvar_t * );
- /****************************************************************************
- * Macro handling
- ****************************************************************************/
- typedef struct
- {
- char *id;
- char *param1;
- char *param2;
- } macro_t;
- static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
- {
- int i_read;
- /* just load the file */
- *pi_data = 0;
- *pp_data = malloc( 1025 ); /* +1 for