1 /*****************************************************************************
2 * mvar.c : Variables handling for the HTTP Interface
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Christophe Massiot <massiot@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
34 #include <vlc_memory.h>
36 /* Utility function for scandir */
37 static int Filter( const char *foo )
39 return strcmp( foo, "." );
42 static int InsensitiveAlphasort( const char **foo1,
45 return strcasecmp( *foo1, *foo2 );
50 mvar_t *mvar_New( const char *name, const char *value )
52 mvar_t *v = malloc( sizeof( mvar_t ) );
55 v->name = strdup( name );
56 v->value = strdup( value ? value : "" );
59 v->field = malloc( sizeof( mvar_t * ) );
66 void mvar_Delete( mvar_t *v )
73 for( i = 0; i < v->i_field; i++ )
75 mvar_Delete( v->field[i] );
81 void mvar_AppendVar( mvar_t *v, mvar_t *f )
83 v->field = realloc_or_free( v->field,
84 sizeof( mvar_t * ) * ( v->i_field + 2 ) );
86 v->field[v->i_field] = f;
90 mvar_t *mvar_Duplicate( const mvar_t *v )
95 n = mvar_New( v->name, v->value );
96 for( i = 0; i < v->i_field; i++ )
98 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
104 void mvar_PushVar( mvar_t *v, mvar_t *f )
106 v->field = realloc_or_free( v->field,
107 sizeof( mvar_t * ) * ( v->i_field + 2 ) );
111 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
117 void mvar_RemoveVar( mvar_t *v, mvar_t *f )
120 for( i = 0; i < v->i_field; i++ )
122 if( v->field[i] == f )
127 if( i >= v->i_field )
132 if( i + 1 < v->i_field )
134 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
137 /* FIXME should do a realloc */
140 mvar_t *mvar_GetVar( mvar_t *s, const char *name )
142 /* format: name[index].field */
143 const char *field = strchr( name, '.' );
144 char base[1 + (field ? (size_t)(field - name) : strlen( name ))];
148 strlcpy( base, name, sizeof (base) );
152 if( ( p = strchr( base, '[' ) ) != NULL )
155 unsigned long l = strtoul( p, &end, 0 );
157 if( ( l > INT_MAX ) || strcmp( "]", end ) )
168 for( i = 0; i < s->i_field; i++ )
170 if( !strcmp( s->field[i]->name, base ) )
180 return mvar_GetVar( s->field[i], field );
192 const char *mvar_GetValue( mvar_t *v, const char *field )
200 mvar_t *f = mvar_GetVar( v, field );
212 void mvar_PushNewVar( mvar_t *vars, const char *name,
215 mvar_t *f = mvar_New( name, value );
216 mvar_PushVar( vars, f );
219 void mvar_AppendNewVar( mvar_t *vars, const char *name,
222 mvar_t *f = mvar_New( name, value );
223 mvar_AppendVar( vars, f );
227 /* arg= start[:stop[:step]],.. */
228 mvar_t *mvar_IntegerSetNew( const char *name, const char *arg )
230 char *dup = strdup( arg );
232 mvar_t *s = mvar_New( name, "set" );
237 int i_start,i_stop,i_step;
240 p = strchr( str, ',' );
247 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
254 else if( i_match == 2 )
256 i_step = i_start < i_stop ? 1 : -1;
263 if( ( i_start <= i_stop && i_step > 0 ) ||
264 ( i_start >= i_stop && i_step < 0 ) )
266 for( i = i_start; ; i += i_step )
270 if( ( i_step > 0 && i > i_stop ) ||
271 ( i_step < 0 && i < i_stop ) )
276 sprintf( value, "%d", i );
278 mvar_PushNewVar( s, name, value );
289 /********************************************************************
290 * Special sets handling
291 ********************************************************************/
293 mvar_t *mvar_PlaylistSetNew( intf_thread_t *p_intf, char *name,
296 mvar_t *s = mvar_New( name, "set" );
297 playlist_Lock( p_pl );
298 PlaylistListNode( p_intf, p_pl, p_pl->p_root_category , name, s, 0 );
299 playlist_Unlock( p_pl );
303 mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
305 mvar_t *s = mvar_New( name, "set" );
308 if( p_input == NULL || p_input->p == NULL /* workarround assert in input_GetItem */ )
313 vlc_mutex_lock( &input_GetItem(p_input)->lock );
314 for ( i = 0; i < input_GetItem(p_input)->i_categories; i++ )
316 info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
318 mvar_t *cat = mvar_New( name, "set" );
319 mvar_t *iset = mvar_New( "info", "set" );
321 mvar_AppendNewVar( cat, "name", p_category->psz_name );
322 mvar_AppendVar( cat, iset );
324 for ( j = 0; j < p_category->i_infos; j++ )
326 info_t *p_info = p_category->pp_infos[j];
327 mvar_t *info = mvar_New( "info", "" );
329 /* msg_Dbg( p_input, "adding info name=%s value=%s",
330 psz_name, psz_value ); */
331 mvar_AppendNewVar( info, "name", p_info->psz_name );
332 mvar_AppendNewVar( info, "value", p_info->psz_value );
333 mvar_AppendVar( iset, info );
335 mvar_AppendVar( s, cat );
337 vlc_mutex_unlock( &input_GetItem(p_input)->lock );
342 mvar_t *mvar_ObjectSetNew( intf_thread_t *p_intf, char *psz_name,
343 const char *psz_capability )
346 mvar_t *s = mvar_New( psz_name, "set" );
349 module_t **p_list = module_list_get( NULL );
351 for( i = 0; p_list[i]; i++ )
353 module_t *p_parser = p_list[i];
354 if( module_provides( p_parser, psz_capability ) )
356 mvar_t *sd = mvar_New( "sd", module_get_object( p_parser ) );
357 mvar_AppendNewVar( sd, "name",
358 module_get_name( p_parser, true ) );
359 mvar_AppendVar( s, sd );
363 module_list_free( p_list );
368 mvar_t *mvar_InputVarSetNew( intf_thread_t *p_intf, char *name,
369 input_thread_t *p_input,
370 const char *psz_variable )
372 intf_sys_t *p_sys = p_intf->p_sys;
373 mvar_t *s = mvar_New( name, "set" );
374 vlc_value_t val, val_list, text_list;
377 if( p_input == NULL )
382 /* Check the type of the object variable */
383 i_type = var_Type( p_sys->p_input, psz_variable );
385 /* Make sure we want to display the variable */
386 if( i_type & VLC_VAR_HASCHOICE )
388 var_Change( p_sys->p_input, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
389 if( val.i_int == 0 ) return s;
390 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
398 switch( i_type & VLC_VAR_TYPE )
402 case VLC_VAR_VARIABLE:
404 case VLC_VAR_INTEGER:
407 /* Variable doesn't exist or isn't handled */
411 if( var_Get( p_sys->p_input, psz_variable, &val ) < 0 )
416 if( var_Change( p_sys->p_input, psz_variable, VLC_VAR_GETLIST,
417 &val_list, &text_list ) < 0 )
419 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
423 for( i = 0; i < val_list.p_list->i_count; i++ )
428 switch( i_type & VLC_VAR_TYPE )
431 itm = mvar_New( name, "set" );
432 mvar_AppendNewVar( itm, "name", text_list.p_list->p_values[i].psz_string );
433 mvar_AppendNewVar( itm, "id", val_list.p_list->p_values[i].psz_string );
434 snprintf( psz_int, sizeof(psz_int), "%d",
435 ( !strcmp( val.psz_string,
436 val_list.p_list->p_values[i].psz_string )
437 && !( i_type & VLC_VAR_ISCOMMAND ) ) );
438 mvar_AppendNewVar( itm, "selected", psz_int );
439 mvar_AppendVar( s, itm );
442 case VLC_VAR_INTEGER:
443 itm = mvar_New( name, "set" );
444 mvar_AppendNewVar( itm, "name", text_list.p_list->p_values[i].psz_string );
445 snprintf( psz_int, sizeof(psz_int), "%d",
446 val_list.p_list->p_values[i].i_int );
447 mvar_AppendNewVar( itm, "id", psz_int );
448 snprintf( psz_int, sizeof(psz_int), "%d",
449 ( val.i_int == val_list.p_list->p_values[i].i_int )
450 && !( i_type & VLC_VAR_ISCOMMAND ) );
451 mvar_AppendNewVar( itm, "selected", psz_int );
452 mvar_AppendVar( s, itm );
459 /* clean up everything */
460 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
461 var_FreeList( &val_list, &text_list );
466 mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
468 mvar_t *s = mvar_New( name, "set" );
472 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
474 for( i= 0; i < info.i_count; )
478 inf = mvar_New( name, "set" );
481 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
482 info.info[i].psz_name, info.info[i].psz_value ); */
483 mvar_AppendNewVar( inf,
484 info.info[i].psz_name,
485 info.info[i].psz_value );
487 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
488 mvar_AppendVar( s, inf );
493 for( i = 0; i < info.i_count; i++ )
495 free( info.info[i].psz_name );
496 free( info.info[i].psz_value );
498 if( info.i_count > 0 )
507 mvar_t *mvar_FileSetNew( intf_thread_t *p_intf, char *name,
510 mvar_t *s = mvar_New( name, "set" );
511 char **ppsz_dir_content;
512 int i_dir_content, i;
513 psz_dir = RealPath( psz_dir );
515 /* parse psz_src dir */
516 if( ( i_dir_content = utf8_scandir( psz_dir, &ppsz_dir_content, Filter,
517 InsensitiveAlphasort ) ) == -1 )
519 if( errno != ENOENT && errno != ENOTDIR )
520 msg_Warn( p_intf, "error while scanning dir %s (%m)", psz_dir );
525 for( i = 0; i < i_dir_content; i++ )
527 #ifdef HAVE_SYS_STAT_H
528 struct stat stat_info;
530 char *psz_name = ppsz_dir_content[i], *psz_ext, *psz_dummy;
531 char psz_tmp[strlen( psz_dir ) + 1 + strlen( psz_name ) + 1];
535 if( psz_dir[0] == '\0' || (psz_dir[0] == '\\' && psz_dir[1] == '\0') )
537 strcpy( psz_tmp, psz_name );
542 sprintf( psz_tmp, "%s"DIR_SEP"%s", psz_dir, psz_name );
544 #ifdef HAVE_SYS_STAT_H
545 if( utf8_stat( psz_tmp, &stat_info ) == -1 )
552 f = mvar_New( name, "set" );
554 /* put lower-case file extension in 'ext' */
555 psz_ext = strrchr( psz_name, '.' );
556 psz_ext = strdup( psz_ext != NULL ? psz_ext + 1 : "" );
557 for( psz_dummy = psz_ext; *psz_dummy != '\0'; psz_dummy++ )
558 *psz_dummy = tolower( *psz_dummy );
560 mvar_AppendNewVar( f, "ext", psz_ext );
564 if( psz_dir[0] == '\0' || (psz_dir[0] == '\\' && psz_dir[1] == '\0') )
567 sprintf( psz_tmp, "%c:", psz_name[0] );
568 mvar_AppendNewVar( f, "name", psz_name );
569 mvar_AppendNewVar( f, "basename", psz_tmp );
570 mvar_AppendNewVar( f, "type", "directory" );
571 mvar_AppendNewVar( f, "size", "unknown" );
572 mvar_AppendNewVar( f, "date", "unknown" );
578 char psz_tmp[strlen( psz_dir ) + 1 + strlen( psz_name ) + 1];
580 sprintf( psz_tmp, "%s"DIR_SEP"%s", psz_dir, psz_name );
581 mvar_AppendNewVar( f, "name", psz_tmp );
582 mvar_AppendNewVar( f, "basename", psz_name );
584 #ifdef HAVE_SYS_STAT_H
585 if( S_ISDIR( stat_info.st_mode ) )
587 mvar_AppendNewVar( f, "type", "directory" );
589 else if( S_ISREG( stat_info.st_mode ) )
591 mvar_AppendNewVar( f, "type", "file" );
595 mvar_AppendNewVar( f, "type", "unknown" );
598 snprintf( psz_buf, sizeof( psz_buf ), "%"PRId64,
599 (int64_t)stat_info.st_size );
600 mvar_AppendNewVar( f, "size", psz_buf );
602 /* FIXME memory leak FIXME */
604 ctime_r( &stat_info.st_mtime, psz_buf );
605 mvar_AppendNewVar( f, "date", psz_buf );
607 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
611 mvar_AppendNewVar( f, "type", "unknown" );
612 mvar_AppendNewVar( f, "size", "unknown" );
613 mvar_AppendNewVar( f, "date", "unknown" );
617 mvar_AppendVar( s, f );
623 free( ppsz_dir_content );
627 static void mvar_VlmSetNewLoop( char *name, vlm_t *vlm, mvar_t *s,
628 vlm_message_t *el, bool b_name )
634 /* Add a node with name and info */
635 set = mvar_New( name, "set" );
638 mvar_AppendNewVar( set, "name", el->psz_name );
641 for( k = 0; k < el->i_child; k++ )
643 vlm_message_t *ch = el->child[k];
644 if( ch->i_child > 0 )
646 mvar_VlmSetNewLoop( ch->psz_name, vlm, set, ch, false );
652 mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
656 mvar_AppendNewVar( set, el->psz_name, ch->psz_name );
661 mvar_AppendVar( s, set );
664 mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
666 mvar_t *s = mvar_New( name, "set" );
671 if( vlm == NULL ) return s;
673 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
676 for( i = 0; i < msg->i_child; i++ )
678 /* Over media, schedule */
679 vlm_message_t *ch = msg->child[i];
682 for( j = 0; j < ch->i_child; j++ )
685 vlm_message_t *el = ch->child[j];
686 vlm_message_t *inf, *desc;
687 char psz[6 + strlen(el->psz_name)];
689 sprintf( psz, "show %s", el->psz_name );
690 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
692 desc = inf->child[0];
694 mvar_VlmSetNewLoop( el->psz_name, vlm, s, desc, true );
696 vlm_MessageDelete( inf );
699 vlm_MessageDelete( msg );
700 #endif /* ENABLE_VLM */