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 *****************************************************************************/
32 /* Utility function for scandir */
33 static int Filter( const char *foo )
35 return strcmp( foo, "." );
38 static int InsensitiveAlphasort( const char **foo1,
41 return strcasecmp( *foo1, *foo2 );
46 mvar_t *mvar_New( const char *name, const char *value )
48 mvar_t *v = malloc( sizeof( mvar_t ) );
51 v->name = strdup( name );
52 v->value = strdup( value ? value : "" );
55 v->field = malloc( sizeof( mvar_t * ) );
61 void mvar_Delete( mvar_t *v )
68 for( i = 0; i < v->i_field; i++ )
70 mvar_Delete( v->field[i] );
76 void mvar_AppendVar( mvar_t *v, mvar_t *f )
78 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
79 v->field[v->i_field] = f;
83 mvar_t *mvar_Duplicate( const mvar_t *v )
88 n = mvar_New( v->name, v->value );
89 for( i = 0; i < v->i_field; i++ )
91 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
97 void mvar_PushVar( mvar_t *v, mvar_t *f )
99 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
102 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
108 void mvar_RemoveVar( mvar_t *v, mvar_t *f )
111 for( i = 0; i < v->i_field; i++ )
113 if( v->field[i] == f )
118 if( i >= v->i_field )
123 if( i + 1 < v->i_field )
125 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
128 /* FIXME should do a realloc */
131 mvar_t *mvar_GetVar( mvar_t *s, const char *name )
133 /* format: name[index].field */
134 const char *field = strchr( name, '.' );
135 char base[1 + (field ? (size_t)(field - name) : strlen( name ))];
139 strlcpy( base, name, sizeof (base) );
143 if( ( p = strchr( base, '[' ) ) != NULL )
146 unsigned long l = strtoul( p, &end, 0 );
148 if( ( l > INT_MAX ) || strcmp( "]", end ) )
159 for( i = 0; i < s->i_field; i++ )
161 if( !strcmp( s->field[i]->name, base ) )
171 return mvar_GetVar( s->field[i], field );
183 char *mvar_GetValue( mvar_t *v, char *field )
191 mvar_t *f = mvar_GetVar( v, field );
203 void mvar_PushNewVar( mvar_t *vars, const char *name,
206 mvar_t *f = mvar_New( name, value );
207 mvar_PushVar( vars, f );
210 void mvar_AppendNewVar( mvar_t *vars, const char *name,
213 mvar_t *f = mvar_New( name, value );
214 mvar_AppendVar( vars, f );
218 /* arg= start[:stop[:step]],.. */
219 mvar_t *mvar_IntegerSetNew( const char *name, const char *arg )
221 char *dup = strdup( arg );
223 mvar_t *s = mvar_New( name, "set" );
228 int i_start,i_stop,i_step;
231 p = strchr( str, ',' );
238 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
245 else if( i_match == 2 )
247 i_step = i_start < i_stop ? 1 : -1;
254 if( ( i_start <= i_stop && i_step > 0 ) ||
255 ( i_start >= i_stop && i_step < 0 ) )
257 for( i = i_start; ; i += i_step )
261 if( ( i_step > 0 && i > i_stop ) ||
262 ( i_step < 0 && i < i_stop ) )
267 sprintf( value, "%d", i );
269 mvar_PushNewVar( s, name, value );
280 /********************************************************************
281 * Special sets handling
282 ********************************************************************/
284 mvar_t *mvar_PlaylistSetNew( intf_thread_t *p_intf, char *name,
287 mvar_t *s = mvar_New( name, "set" );
288 vlc_mutex_lock( &p_pl->object_lock );
289 PlaylistListNode( p_intf, p_pl, p_pl->p_root_category , name, s, 0 );
290 vlc_mutex_unlock( &p_pl->object_lock );
294 mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
296 mvar_t *s = mvar_New( name, "set" );
299 if( p_input == NULL || p_input->p == NULL /* workarround assert in input_GetItem */ )
304 vlc_mutex_lock( &input_GetItem(p_input)->lock );
305 for ( i = 0; i < input_GetItem(p_input)->i_categories; i++ )
307 info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
309 mvar_t *cat = mvar_New( name, "set" );
310 mvar_t *iset = mvar_New( "info", "set" );
312 mvar_AppendNewVar( cat, "name", p_category->psz_name );
313 mvar_AppendVar( cat, iset );
315 for ( j = 0; j < p_category->i_infos; j++ )
317 info_t *p_info = p_category->pp_infos[j];
318 mvar_t *info = mvar_New( "info", "" );
320 /* msg_Dbg( p_input, "adding info name=%s value=%s",
321 psz_name, psz_value ); */
322 mvar_AppendNewVar( info, "name", p_info->psz_name );
323 mvar_AppendNewVar( info, "value", p_info->psz_value );
324 mvar_AppendVar( iset, info );
326 mvar_AppendVar( s, cat );
328 vlc_mutex_unlock( &input_GetItem(p_input)->lock );
333 mvar_t *mvar_ObjectSetNew( intf_thread_t *p_intf, char *psz_name,
334 const char *psz_capability )
336 mvar_t *s = mvar_New( psz_name, "set" );
339 vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
342 for( i = 0; i < p_list->i_count; i++ )
344 module_t *p_parser = (module_t *)p_list->p_values[i].p_object;
345 if( module_IsCapable( p_parser, psz_capability ) )
347 mvar_t *sd = mvar_New( "sd", module_GetObjName( p_parser ) );
348 mvar_AppendNewVar( sd, "name",
349 module_GetName( p_parser, true ) );
350 mvar_AppendVar( s, sd );
354 vlc_list_release( p_list );
359 mvar_t *mvar_InputVarSetNew( intf_thread_t *p_intf, char *name,
360 input_thread_t *p_input,
361 const char *psz_variable )
363 intf_sys_t *p_sys = p_intf->p_sys;
364 mvar_t *s = mvar_New( name, "set" );
365 vlc_value_t val, val_list, text_list;
368 if( p_input == NULL )
373 /* Check the type of the object variable */
374 i_type = var_Type( p_sys->p_input, psz_variable );
376 /* Make sure we want to display the variable */
377 if( i_type & VLC_VAR_HASCHOICE )
379 var_Change( p_sys->p_input, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
380 if( val.i_int == 0 ) return s;
381 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
389 switch( i_type & VLC_VAR_TYPE )
393 case VLC_VAR_VARIABLE:
395 case VLC_VAR_INTEGER:
398 /* Variable doesn't exist or isn't handled */
402 if( var_Get( p_sys->p_input, psz_variable, &val ) < 0 )
407 if( var_Change( p_sys->p_input, psz_variable, VLC_VAR_GETLIST,
408 &val_list, &text_list ) < 0 )
410 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
414 for( i = 0; i < val_list.p_list->i_count; i++ )
416 char *psz, psz_int[16];
419 switch( i_type & VLC_VAR_TYPE )
422 itm = mvar_New( name, "set" );
423 /* FIXME: Memory leak here?? (remove strdup?) */
424 psz = strdup( text_list.p_list->p_values[i].psz_string );
425 mvar_AppendNewVar( itm, "name", psz );
426 mvar_AppendNewVar( itm, "id", val_list.p_list->p_values[i].psz_string );
427 snprintf( psz_int, sizeof(psz_int), "%d",
428 ( !strcmp( val.psz_string,
429 val_list.p_list->p_values[i].psz_string )
430 && !( i_type & VLC_VAR_ISCOMMAND ) ) );
431 mvar_AppendNewVar( itm, "selected", psz_int );
432 mvar_AppendVar( s, itm );
435 case VLC_VAR_INTEGER:
436 itm = mvar_New( name, "set" );
437 psz = strdup( text_list.p_list->p_values[i].psz_string );
438 mvar_AppendNewVar( itm, "name", psz );
439 snprintf( psz_int, sizeof(psz_int), "%d",
440 val_list.p_list->p_values[i].i_int );
441 mvar_AppendNewVar( itm, "id", psz_int );
442 snprintf( psz_int, sizeof(psz_int), "%d",
443 ( val.i_int == val_list.p_list->p_values[i].i_int )
444 && !( i_type & VLC_VAR_ISCOMMAND ) );
445 mvar_AppendNewVar( itm, "selected", psz_int );
446 mvar_AppendVar( s, itm );
453 /* clean up everything */
454 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
455 var_Change( p_sys->p_input, psz_variable, VLC_VAR_FREELIST, &val_list,
461 mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
463 mvar_t *s = mvar_New( name, "set" );
467 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
469 for( i= 0; i < info.i_count; )
473 inf = mvar_New( name, "set" );
476 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
477 info.info[i].psz_name, info.info[i].psz_value ); */
478 mvar_AppendNewVar( inf,
479 info.info[i].psz_name,
480 info.info[i].psz_value );
482 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
483 mvar_AppendVar( s, inf );
488 for( i = 0; i < info.i_count; i++ )
490 free( info.info[i].psz_name );
491 free( info.info[i].psz_value );
493 if( info.i_count > 0 )
502 mvar_t *mvar_FileSetNew( intf_thread_t *p_intf, char *name,
505 mvar_t *s = mvar_New( name, "set" );
506 #ifdef HAVE_SYS_STAT_H
507 struct stat stat_info;
509 char **ppsz_dir_content;
510 int i_dir_content, i;
511 psz_dir = RealPath( p_intf, psz_dir );
513 #ifdef HAVE_SYS_STAT_H
514 if( (utf8_stat( psz_dir, &stat_info ) == -1 )
515 || !S_ISDIR( stat_info.st_mode )
516 # if defined( WIN32 )
517 && psz_dir[0] != '\0' && (psz_dir[0] != '\\' || psz_dir[1] != '\0')
526 /* parse psz_src dir */
527 if( ( i_dir_content = utf8_scandir( psz_dir, &ppsz_dir_content, Filter,
528 InsensitiveAlphasort ) ) == -1 )
530 msg_Warn( p_intf, "error while scanning dir %s (%m)", psz_dir );
535 for( i = 0; i < i_dir_content; i++ )
537 char *psz_name = ppsz_dir_content[i], *psz_ext, *psz_dummy;
538 char psz_tmp[strlen( psz_dir ) + 1 + strlen( psz_name ) + 1];
542 if( psz_dir[0] == '\0' || (psz_dir[0] == '\\' && psz_dir[1] == '\0') )
544 strcpy( psz_tmp, psz_name );
549 sprintf( psz_tmp, "%s"DIR_SEP"%s", psz_dir, psz_name );
551 #ifdef HAVE_SYS_STAT_H
552 if( utf8_stat( psz_tmp, &stat_info ) == -1 )
559 f = mvar_New( name, "set" );
561 /* put lower-case file extension in 'ext' */
562 psz_ext = strrchr( psz_name, '.' );
563 psz_ext = strdup( psz_ext != NULL ? psz_ext + 1 : "" );
564 for( psz_dummy = psz_ext; *psz_dummy != '\0'; psz_dummy++ )
565 *psz_dummy = tolower( *psz_dummy );
567 mvar_AppendNewVar( f, "ext", psz_ext );
571 if( psz_dir[0] == '\0' || (psz_dir[0] == '\\' && psz_dir[1] == '\0') )
574 sprintf( psz_tmp, "%c:", psz_name[0] );
575 mvar_AppendNewVar( f, "name", psz_name );
576 mvar_AppendNewVar( f, "basename", psz_tmp );
577 mvar_AppendNewVar( f, "type", "directory" );
578 mvar_AppendNewVar( f, "size", "unknown" );
579 mvar_AppendNewVar( f, "date", "unknown" );
585 char psz_tmp[strlen( psz_dir ) + 1 + strlen( psz_name ) + 1];
587 sprintf( psz_tmp, "%s"DIR_SEP"%s", psz_dir, psz_name );
588 mvar_AppendNewVar( f, "name", psz_tmp );
589 mvar_AppendNewVar( f, "basename", psz_name );
591 #ifdef HAVE_SYS_STAT_H
592 if( S_ISDIR( stat_info.st_mode ) )
594 mvar_AppendNewVar( f, "type", "directory" );
596 else if( S_ISREG( stat_info.st_mode ) )
598 mvar_AppendNewVar( f, "type", "file" );
602 mvar_AppendNewVar( f, "type", "unknown" );
605 sprintf( psz_ctime, "%"PRId64, (int64_t)stat_info.st_size );
606 mvar_AppendNewVar( f, "size", psz_ctime );
608 /* FIXME memory leak FIXME */
610 ctime_r( &stat_info.st_mtime, psz_ctime );
611 mvar_AppendNewVar( f, "date", psz_ctime );
613 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
617 mvar_AppendNewVar( f, "type", "unknown" );
618 mvar_AppendNewVar( f, "size", "unknown" );
619 mvar_AppendNewVar( f, "date", "unknown" );
623 mvar_AppendVar( s, f );
629 free( ppsz_dir_content );
633 void mvar_VlmSetNewLoop( char *name, vlm_t *vlm, mvar_t *s, vlm_message_t *el, bool b_name );
634 void mvar_VlmSetNewLoop( char *name, vlm_t *vlm, mvar_t *s, vlm_message_t *el, bool b_name )
640 /* Add a node with name and info */
641 set = mvar_New( name, "set" );
644 mvar_AppendNewVar( set, "name", el->psz_name );
647 for( k = 0; k < el->i_child; k++ )
649 vlm_message_t *ch = el->child[k];
650 if( ch->i_child > 0 )
652 mvar_VlmSetNewLoop( ch->psz_name, vlm, set, ch, false );
658 mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
662 mvar_AppendNewVar( set, el->psz_name, ch->psz_name );
667 mvar_AppendVar( s, set );
670 mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
672 mvar_t *s = mvar_New( name, "set" );
676 if( vlm == NULL ) return s;
678 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
683 for( i = 0; i < msg->i_child; i++ )
685 /* Over media, schedule */
686 vlm_message_t *ch = msg->child[i];
689 for( j = 0; j < ch->i_child; j++ )
692 vlm_message_t *el = ch->child[j];
693 vlm_message_t *inf, *desc;
694 char psz[6 + strlen(el->psz_name)];
696 sprintf( psz, "show %s", el->psz_name );
697 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
699 desc = inf->child[0];
701 mvar_VlmSetNewLoop( el->psz_name, vlm, s, desc, true );
703 vlm_MessageDelete( inf );
706 vlm_MessageDelete( msg );