]> git.sesse.net Git - vlc/blobdiff - modules/control/http/mvar.c
Make vlc_value_t.i_int 64-bits
[vlc] / modules / control / http / mvar.c
index 79b7eebac7e8f09577003815812dc195a442428a..946501985f339bcc6f1a85c7d471aabec311cf25 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * mvar.c : Variables handling for the HTTP Interface
  *****************************************************************************
- * Copyright (C) 2001-2005 the VideoLAN team
- * $Id: http.c 12225 2005-08-18 10:01:30Z massiot $
+ * Copyright (C) 2001-2007 the VideoLAN team
+ * $Id$
  *
  * Authors: Gildas Bazin <gbazin@netcourrier.com>
  *          Laurent Aimar <fenrir@via.ecp.fr>
  *
  * 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.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
 #include "http.h"
+#include <limits.h>
+#include <errno.h>
+#include <ctype.h>
+#include <fcntl.h>
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+#include <vlc_fs.h>
+#include <vlc_services_discovery.h>
 
 /* Utility function for scandir */
-static int Filter( const struct dirent *foo )
+static int Filter( const char *foo )
 {
-    return VLC_TRUE;
+    return strcmp( foo, "." );
 };
 
-static int InsensitiveAlphasort( const struct dirent **foo1,
-                                 const struct dirent **foo2 )
+static int InsensitiveAlphasort( const char **foo1,
+                                 const char **foo2 )
 {
-    return strcasecmp( (*foo1)->d_name, (*foo2)->d_name );
+    return strcasecmp( *foo1, *foo2 );
 };
 
 
@@ -48,7 +60,7 @@ mvar_t *mvar_New( const char *name, const char *value )
     v->value = strdup( value ? value : "" );
 
     v->i_field = 0;
-    v->field = malloc( sizeof( mvar_t * ) );
+    v->field = xmalloc( sizeof( mvar_t * ) );
     v->field[0] = NULL;
 
     return v;
@@ -71,7 +83,7 @@ void mvar_Delete( mvar_t *v )
 
 void mvar_AppendVar( mvar_t *v, mvar_t *f )
 {
-    v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
+    v->field = xrealloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
     v->field[v->i_field] = f;
     v->i_field++;
 }
@@ -92,7 +104,7 @@ mvar_t *mvar_Duplicate( const mvar_t *v )
 
 void mvar_PushVar( mvar_t *v, mvar_t *f )
 {
-    v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
+    v->field = xrealloc( 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 );
@@ -126,33 +138,26 @@ void mvar_RemoveVar( mvar_t *v, mvar_t *f )
 
 mvar_t *mvar_GetVar( mvar_t *s, const char *name )
 {
-    int i;
-    char base[512], *field, *p;
-    int  i_index;
-
     /* format: name[index].field */
+    const char *field = strchr( name, '.' );
+    char base[1 + (field ? (size_t)(field - name) : strlen( name ))];
+    char *p;
+    int i_index, i;
 
-    field = strchr( name, '.' );
-    if( field )
-    {
-        int i = field - name;
-        strncpy( base, name, i );
-        base[i] = '\0';
+    strlcpy( base, name, sizeof (base) );
+    if( field != NULL )
         field++;
-    }
-    else
-    {
-        strcpy( base, name );
-    }
 
-    if( ( p = strchr( base, '[' ) ) )
+    if( ( p = strchr( base, '[' ) ) != NULL )
     {
-        *p++ = '\0';
-        sscanf( p, "%d]", &i_index );
-        if( i_index < 0 )
-        {
+        char *end;
+        unsigned long l = strtoul( p, &end, 0 );
+
+        if( ( l > INT_MAX ) || strcmp( "]", end ) )
             return NULL;
-        }
+
+        *p++ = '\0';
+        i_index = (int)l;
     }
     else
     {
@@ -183,7 +188,7 @@ mvar_t *mvar_GetVar( mvar_t *s, const char *name )
     return NULL;
 }
 
-char *mvar_GetValue( mvar_t *v, char *field )
+const char *mvar_GetValue( mvar_t *v, const char *field )
 {
     if( *field == '\0' )
     {
@@ -204,14 +209,14 @@ char *mvar_GetValue( mvar_t *v, char *field )
 }
 
 void mvar_PushNewVar( mvar_t *vars, const char *name,
-                             const char *value )
+                          const char *value )
 {
     mvar_t *f = mvar_New( name, value );
     mvar_PushVar( vars, f );
 }
 
 void mvar_AppendNewVar( mvar_t *vars, const char *name,
-                               const char *value )
+                            const char *value )
 {
     mvar_t *f = mvar_New( name, value );
     mvar_AppendVar( vars, f );
@@ -285,74 +290,80 @@ mvar_t *mvar_IntegerSetNew( const char *name, const char *arg )
  ********************************************************************/
 
 mvar_t *mvar_PlaylistSetNew( intf_thread_t *p_intf, char *name,
-                                    playlist_t *p_pl )
+                                 playlist_t *p_pl )
 {
-    playlist_view_t *p_view;
     mvar_t *s = mvar_New( name, "set" );
-
-
-    vlc_mutex_lock( &p_pl->object_lock );
-
-    p_view = playlist_ViewFind( p_pl, VIEW_CATEGORY ); /* FIXME */
-
-    if( p_view != NULL )
-        E_(PlaylistListNode)( p_intf, p_pl, p_view->p_root, name, s, 0 );
-
-    vlc_mutex_unlock( &p_pl->object_lock );
-
+    playlist_Lock( p_pl );
+    PlaylistListNode( p_intf, p_pl, p_pl->p_root_category , name, s, 0 );
+    playlist_Unlock( p_pl );
     return s;
 }
 
-mvar_t *mvar_InfoSetNew( intf_thread_t *p_intf, char *name,
-                                input_thread_t *p_input )
+mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
 {
     mvar_t *s = mvar_New( name, "set" );
     int i, j;
 
-    if( p_input == NULL )
+    if( p_input == NULL || p_input->p == NULL /* workarround assert in input_GetItem */ )
     {
         return s;
     }
 
-    vlc_mutex_lock( &p_input->input.p_item->lock );
-    for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
+    vlc_mutex_lock( &input_GetItem(p_input)->lock );
+    for ( i = 0; i < input_GetItem(p_input)->i_categories; i++ )
     {
-        info_category_t *p_category = p_input->input.p_item->pp_categories[i];
-        char *psz;
+        info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
 
         mvar_t *cat  = mvar_New( name, "set" );
         mvar_t *iset = mvar_New( "info", "set" );
 
-        psz = E_(FromUTF8)( p_intf, p_category->psz_name );
-        mvar_AppendNewVar( cat, "name", psz );
-        free( psz );
+        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", "" );
-            char *psz_name = E_(FromUTF8)( p_intf, p_info->psz_name );
-            char *psz_value = E_(FromUTF8)( p_intf, p_info->psz_value );
-
-            msg_Dbg( p_input, "adding info name=%s value=%s",
-                     psz_name, psz_value );
-            mvar_AppendNewVar( info, "name",  psz_name );
-            mvar_AppendNewVar( info, "value", psz_value );
-            free( psz_name );
-            free( psz_value );
+
+            /* msg_Dbg( p_input, "adding info name=%s value=%s",
+                     psz_name, 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 );
+    vlc_mutex_unlock( &input_GetItem(p_input)->lock );
+
+    return s;
+}
+
+mvar_t *mvar_ServicesSetNew( intf_thread_t *p_intf, char *psz_name )
+{
+    mvar_t *s = mvar_New( psz_name, "set" );
+    char **longnames;
+    char **names = vlc_sd_GetNames( p_intf, &longnames, NULL );
+    if( names == NULL )
+        goto out;
+
+    for( size_t i = 0; names[i]; i++ )
+    {
+        mvar_t *sd = mvar_New( "sd", names[i] );
+        mvar_AppendNewVar( sd, "name", longnames[i] );
+        mvar_AppendVar( s, sd );
+        free( names[i] );
+        free( longnames[i] );
+    }
 
+    free( longnames );
+    free( names );
+out:
     return s;
 }
 
 mvar_t *mvar_InputVarSetNew( intf_thread_t *p_intf, char *name,
-                                    input_thread_t *p_input,
-                                    const char *psz_variable )
+                                 input_thread_t *p_input,
+                                 const char *psz_variable )
 {
     intf_sys_t     *p_sys = p_intf->p_sys;
     mvar_t *s = mvar_New( name, "set" );
@@ -407,18 +418,15 @@ mvar_t *mvar_InputVarSetNew( intf_thread_t *p_intf, char *name,
 
     for( i = 0; i < val_list.p_list->i_count; i++ )
     {
-        char *psz, psz_int[16];
+        char psz_int[21];
         mvar_t *itm;
 
         switch( i_type & VLC_VAR_TYPE )
         {
         case VLC_VAR_STRING:
             itm = mvar_New( name, "set" );
-            psz = E_(FromUTF8)( p_intf, text_list.p_list->p_values[i].psz_string );
-            mvar_AppendNewVar( itm, "name", psz );
-            psz = E_(FromUTF8)( p_intf, val_list.p_list->p_values[i].psz_string );
-            mvar_AppendNewVar( itm, "id", psz );
-            free( psz );
+            mvar_AppendNewVar( itm, "name", text_list.p_list->p_values[i].psz_string );
+            mvar_AppendNewVar( itm, "id", val_list.p_list->p_values[i].psz_string );
             snprintf( psz_int, sizeof(psz_int), "%d",
                       ( !strcmp( val.psz_string,
                                    val_list.p_list->p_values[i].psz_string )
@@ -429,9 +437,8 @@ mvar_t *mvar_InputVarSetNew( intf_thread_t *p_intf, char *name,
 
         case VLC_VAR_INTEGER:
             itm = mvar_New( name, "set" );
-            psz = E_(FromUTF8)( p_intf, text_list.p_list->p_values[i].psz_string );
-            mvar_AppendNewVar( itm, "name", psz );
-            snprintf( psz_int, sizeof(psz_int), "%d",
+            mvar_AppendNewVar( itm, "name", text_list.p_list->p_values[i].psz_string );
+            snprintf( psz_int, sizeof(psz_int), "%"PRId64,
                       val_list.p_list->p_values[i].i_int );
             mvar_AppendNewVar( itm, "id", psz_int );
             snprintf( psz_int, sizeof(psz_int), "%d",
@@ -447,8 +454,7 @@ mvar_t *mvar_InputVarSetNew( intf_thread_t *p_intf, char *name,
     }
     /* clean up everything */
     if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
-    var_Change( p_sys->p_input, psz_variable, VLC_VAR_FREELIST, &val_list,
-                &text_list );
+    var_FreeList( &val_list, &text_list );
     return s;
 }
 
@@ -495,129 +501,173 @@ mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
 #endif
 
 mvar_t *mvar_FileSetNew( intf_thread_t *p_intf, char *name,
-                         char *psz_dir )
+                             char *psz_dir )
 {
     mvar_t *s = mvar_New( name, "set" );
-    char          tmp[MAX_DIR_SIZE];
-#ifdef HAVE_SYS_STAT_H
-    struct stat   stat_info;
-#endif
-    struct dirent **pp_dir_content;
+    char        **ppsz_dir_content;
     int           i_dir_content, i;
-    char          sep;
-
-    /* convert all / to native separator */
-#if defined( WIN32 )
-    sep = '\\';
-#else
-    sep = '/';
-#endif
-
-    psz_dir = E_(RealPath)( p_intf, psz_dir );
-
-#ifdef HAVE_SYS_STAT_H
-    if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
-    {
-        free( psz_dir );
-        return s;
-    }
-#endif
+    psz_dir = RealPath( psz_dir );
 
     /* parse psz_src dir */
-    if( ( i_dir_content = scandir( psz_dir, &pp_dir_content, Filter,
-                                   InsensitiveAlphasort ) ) == -1 )
+    if( ( i_dir_content = vlc_scandir( psz_dir, &ppsz_dir_content, Filter,
+                                        InsensitiveAlphasort ) ) == -1 )
     {
-        msg_Warn( p_intf, "scandir error on %s (%s)", psz_dir,
-                  strerror(errno) );
+        if( errno != ENOENT && errno != ENOTDIR )
+            msg_Warn( p_intf, "error while scanning dir %s (%m)", psz_dir );
         free( psz_dir );
         return s;
     }
 
     for( i = 0; i < i_dir_content; i++ )
     {
-        struct dirent *p_dir_content = pp_dir_content[i];
+#ifdef HAVE_SYS_STAT_H
+        struct stat stat_info;
+#endif
+        char *psz_name = ppsz_dir_content[i], *psz_ext, *psz_dummy;
+        char psz_tmp[strlen( psz_dir ) + 1 + strlen( psz_name ) + 1];
         mvar_t *f;
-        const char *psz_ext;
-        char *psz_name, *psz_tmp;
 
-        if( !strcmp( p_dir_content->d_name, "." ) )
+#if defined( WIN32 )
+        if( psz_dir[0] == '\0' || (psz_dir[0] == '\\' && psz_dir[1] == '\0') )
         {
-            continue;
+            strcpy( psz_tmp, psz_name );
         }
-
-        snprintf( tmp, sizeof(tmp), "%s%c%s", psz_dir, sep,
-                  p_dir_content->d_name );
+        else
+#endif
+        {
+            sprintf( psz_tmp, "%s"DIR_SEP"%s", psz_dir, psz_name );
 
 #ifdef HAVE_SYS_STAT_H
-        if( stat( tmp, &stat_info ) == -1 )
-        {
-            continue;
-        }
+            if( vlc_stat( psz_tmp, &stat_info ) == -1 )
+            {
+                free( psz_name );
+                continue;
+            }
 #endif
+        }
         f = mvar_New( name, "set" );
 
-        psz_tmp = vlc_fix_readdir_charset( p_intf, p_dir_content->d_name );
-        psz_name = E_(FromUTF8)( p_intf, psz_tmp );
-        free( psz_tmp );
-        snprintf( tmp, sizeof(tmp), "%s%c%s", psz_dir, sep, psz_name );
-        mvar_AppendNewVar( f, "name", tmp );
-        mvar_AppendNewVar( f, "basename", psz_name );
-
-        /* put file extension in 'ext' */
+        /* put lower-case file extension in 'ext' */
         psz_ext = strrchr( psz_name, '.' );
-        mvar_AppendNewVar( f, "ext", psz_ext != NULL ? psz_ext + 1 : "" );
+        psz_ext = strdup( psz_ext != NULL ? psz_ext + 1 : "" );
+        for( psz_dummy = psz_ext; *psz_dummy != '\0'; psz_dummy++ )
+            *psz_dummy = tolower( *psz_dummy );
 
-        free( psz_name );
+        mvar_AppendNewVar( f, "ext", psz_ext );
+        free( psz_ext );
 
-#ifdef HAVE_SYS_STAT_H
-        if( S_ISDIR( stat_info.st_mode ) )
+#if defined( WIN32 )
+        if( psz_dir[0] == '\0' || (psz_dir[0] == '\\' && psz_dir[1] == '\0') )
         {
+            char psz_tmp[3];
+            sprintf( psz_tmp, "%c:", psz_name[0] );
+            mvar_AppendNewVar( f, "name", psz_name );
+            mvar_AppendNewVar( f, "basename", psz_tmp );
             mvar_AppendNewVar( f, "type", "directory" );
-        }
-        else if( S_ISREG( stat_info.st_mode ) )
-        {
-            mvar_AppendNewVar( f, "type", "file" );
+            mvar_AppendNewVar( f, "size", "unknown" );
+            mvar_AppendNewVar( f, "date", "unknown" );
         }
         else
+#endif
         {
-            mvar_AppendNewVar( f, "type", "unknown" );
-        }
+            char psz_buf[26];
+            char psz_tmp[strlen( psz_dir ) + 1 + strlen( psz_name ) + 1];
 
-        sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
-        mvar_AppendNewVar( f, "size", tmp );
+            sprintf( psz_tmp, "%s"DIR_SEP"%s", psz_dir, psz_name );
+            mvar_AppendNewVar( f, "name", psz_tmp );
+            mvar_AppendNewVar( f, "basename", psz_name );
 
-        /* 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
+#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" );
+            }
+
+            snprintf( psz_buf, sizeof( psz_buf ), "%"PRId64,
+                      (int64_t)stat_info.st_size );
+            mvar_AppendNewVar( f, "size", psz_buf );
+
+            /* FIXME memory leak FIXME */
+#   ifdef HAVE_CTIME_R
+            ctime_r( &stat_info.st_mtime, psz_buf );
+            mvar_AppendNewVar( f, "date", psz_buf );
+#   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" );
+            mvar_AppendNewVar( f, "type", "unknown" );
+            mvar_AppendNewVar( f, "size", "unknown" );
+            mvar_AppendNewVar( f, "date", "unknown" );
 #endif
+        }
+
         mvar_AppendVar( s, f );
+
+        free( psz_name );
     }
 
     free( psz_dir );
+    free( ppsz_dir_content );
     return s;
 }
 
+static void mvar_VlmSetNewLoop( char *name, vlm_t *vlm, mvar_t *s,
+                                vlm_message_t *el, bool b_name )
+{
+    /* Over name */
+    mvar_t        *set;
+    int k;
+
+    /* Add a node with name and info */
+    set = mvar_New( name, "set" );
+    if( b_name == true )
+    {
+        mvar_AppendNewVar( set, "name", el->psz_name );
+    }
+
+    for( k = 0; k < el->i_child; k++ )
+    {
+        vlm_message_t *ch = el->child[k];
+        if( ch->i_child > 0 )
+        {
+            mvar_VlmSetNewLoop( ch->psz_name, vlm, set, ch, false );
+        }
+        else
+        {
+            if( ch->psz_value )
+            {
+                mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
+            }
+            else
+            {
+                mvar_AppendNewVar( set, el->psz_name, ch->psz_name );
+            }
+        }
+    }
+
+    mvar_AppendVar( s, set );
+}
+
 mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
 {
     mvar_t        *s = mvar_New( name, "set" );
+#ifdef ENABLE_VLM
     vlm_message_t *msg;
     int    i;
 
     if( vlm == NULL ) return s;
 
     if( vlm_ExecuteCommand( vlm, "show", &msg ) )
-    {
         return s;
-    }
 
     for( i = 0; i < msg->i_child; i++ )
     {
@@ -630,52 +680,19 @@ mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
             /* Over name */
             vlm_message_t *el = ch->child[j];
             vlm_message_t *inf, *desc;
-            mvar_t        *set;
-            char          psz[500];
-            int k;
+            char          psz[6 + strlen(el->psz_name)];
 
             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 );
-
-            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" );
+            mvar_VlmSetNewLoop( el->psz_name, vlm, s, desc, true );
 
-                    for( c = 0; c < ch->i_child; c++ )
-                    {
-                        if( ch->child[c]->psz_value )
-                        {
-                            mvar_AppendNewVar( n, ch->child[c]->psz_name, ch->child[c]->psz_value );
-                        }
-                        else
-                        {
-                            mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
-                            mvar_AppendVar( n, in );
-                        }
-                    }
-                    mvar_AppendVar( set, n );
-                }
-                else
-                {
-                    mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
-                }
-            }
             vlm_MessageDelete( inf );
-
-            mvar_AppendVar( s, set );
         }
     }
     vlm_MessageDelete( msg );
-
+#endif /* ENABLE_VLM */
     return s;
 }