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 *****************************************************************************/
35 /* Utility function for scandir */
36 static int Filter( const char *foo )
38 return strcmp( foo, "." );
41 static int InsensitiveAlphasort( const char **foo1,
44 return strcasecmp( *foo1, *foo2 );
49 mvar_t *mvar_New( const char *name, const char *value )
51 mvar_t *v = malloc( sizeof( mvar_t ) );
54 v->name = strdup( name );
55 v->value = strdup( value ? value : "" );
58 v->field = xmalloc( sizeof( mvar_t * ) );
64 void mvar_Delete( mvar_t *v )
71 for( i = 0; i < v->i_field; i++ )
73 mvar_Delete( v->field[i] );
79 void mvar_AppendVar( mvar_t *v, mvar_t *f )
81 v->field = xrealloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
82 v->field[v->i_field] = f;
86 mvar_t *mvar_Duplicate( const mvar_t *v )
91 n = mvar_New( v->name, v->value );
92 for( i = 0; i < v->i_field; i++ )
94 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
100 void mvar_PushVar( mvar_t *v, mvar_t *f )
102 v->field = xrealloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
105 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
111 void mvar_RemoveVar( mvar_t *v, mvar_t *f )
114 for( i = 0; i < v->i_field; i++ )
116 if( v->field[i] == f )
121 if( i >= v->i_field )
126 if( i + 1 < v->i_field )
128 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
131 /* FIXME should do a realloc */
134 mvar_t *mvar_GetVar( mvar_t *s, const char *name )
136 /* format: name[index].field */
137 const char *field = strchr( name, '.' );
138 char base[1 + (field ? (size_t)(field - name) : strlen( name ))];
142 strlcpy( base, name, sizeof (base) );
146 if( ( p = strchr( base, '[' ) ) != NULL )
149 unsigned long l = strtoul( p, &end, 0 );
151 if( ( l > INT_MAX ) || strcmp( "]", end ) )
162 for( i = 0; i < s->i_field; i++ )
164 if( !strcmp( s->field[i]->name, base ) )
174 return mvar_GetVar( s->field[i], field );
186 const char *mvar_GetValue( mvar_t *v, const char *field )
194 mvar_t *f = mvar_GetVar( v, field );
206 void mvar_PushNewVar( mvar_t *vars, const char *name,
209 mvar_t *f = mvar_New( name, value );
210 mvar_PushVar( vars, f );
213 void mvar_AppendNewVar( mvar_t *vars, const char *name,
216 mvar_t *f = mvar_New( name, value );
217 mvar_AppendVar( vars, f );
221 /* arg= start[:stop[:step]],.. */
222 mvar_t *mvar_IntegerSetNew( const char *name, const char *arg )
224 char *dup = strdup( arg );
226 mvar_t *s = mvar_New( name, "set" );
231 int i_start,i_stop,i_step;
234 p = strchr( str, ',' );
241 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
248 else if( i_match == 2 )
250 i_step = i_start < i_stop ? 1 : -1;
257 if( ( i_start <= i_stop && i_step > 0 ) ||
258 ( i_start >= i_stop && i_step < 0 ) )
260 for( i = i_start; ; i += i_step )
264 if( ( i_step > 0 && i > i_stop ) ||
265 ( i_step < 0 && i < i_stop ) )
270 sprintf( value, "%d", i );
272 mvar_PushNewVar( s, name, value );
283 /********************************************************************
284 * Special sets handling
285 ********************************************************************/
287 mvar_t *mvar_PlaylistSetNew( intf_thread_t *p_intf, char *name,
290 mvar_t *s = mvar_New( name, "set" );
291 playlist_Lock( p_pl );
292 PlaylistListNode( p_intf, p_pl, p_pl->p_root_category , name, s, 0 );
293 playlist_Unlock( p_pl );
297 mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
299 mvar_t *s = mvar_New( name, "set" );
302 if( p_input == NULL || p_input->p == NULL /* workarround assert in input_GetItem */ )
307 vlc_mutex_lock( &input_GetItem(p_input)->lock );
308 for ( i = 0; i < input_GetItem(p_input)->i_categories; i++ )
310 info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
312 mvar_t *cat = mvar_New( name, "set" );
313 mvar_t *iset = mvar_New( "info", "set" );
315 mvar_AppendNewVar( cat, "name", p_category->psz_name );
316 mvar_AppendVar( cat, iset );
318 for ( j = 0; j < p_category->i_infos; j++ )
320 info_t *p_info = p_category->pp_infos[j];
321 mvar_t *info = mvar_New( "info", "" );
323 /* msg_Dbg( p_input, "adding info name=%s value=%s",
324 psz_name, psz_value ); */
325 mvar_AppendNewVar( info, "name", p_info->psz_name );
326 mvar_AppendNewVar( info, "value", p_info->psz_value );
327 mvar_AppendVar( iset, info );
329 mvar_AppendVar( s, cat );
331 vlc_mutex_unlock( &input_GetItem(p_input)->lock );
336 mvar_t *mvar_ObjectSetNew( intf_thread_t *p_intf, char *psz_name,
337 const char *psz_capability )
340 mvar_t *s = mvar_New( psz_name, "set" );
343 module_t **p_list = module_list_get( NULL );
345 for( i = 0; p_list[i]; i++ )
347 module_t *p_parser = p_list[i];
348 if( module_provides( p_parser, psz_capability ) )
350 mvar_t *sd = mvar_New( "sd", module_get_object( p_parser ) );
351 mvar_AppendNewVar( sd, "name",
352 module_get_name( p_parser, true ) );
353 mvar_AppendVar( s, sd );
357 module_list_free( p_list );
362 mvar_t *mvar_InputVarSetNew( intf_thread_t *p_intf, char *name,
363 input_thread_t *p_input,
364 const char *psz_variable )
366 intf_sys_t *p_sys = p_intf->p_sys;
367 mvar_t *s = mvar_New( name, "set" );
368 vlc_value_t val, val_list, text_list;
371 if( p_input == NULL )
376 /* Check the type of the object variable */
377 i_type = var_Type( p_sys->p_input, psz_variable );
379 /* Make sure we want to display the variable */
380 if( i_type & VLC_VAR_HASCHOICE )
382 var_Change( p_sys->p_input, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
383 if( val.i_int == 0 ) return s;
384 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
392 switch( i_type & VLC_VAR_TYPE )
396 case VLC_VAR_VARIABLE:
398 case VLC_VAR_INTEGER:
401 /* Variable doesn't exist or isn't handled */
405 if( var_Get( p_sys->p_input, psz_variable, &val ) < 0 )
410 if( var_Change( p_sys->p_input, psz_variable, VLC_VAR_GETLIST,
411 &val_list, &text_list ) < 0 )
413 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
417 for( i = 0; i < val_list.p_list->i_count; i++ )
422 switch( i_type & VLC_VAR_TYPE )
425 itm = mvar_New( name, "set" );
426 mvar_AppendNewVar( itm, "name", text_list.p_list->p_values[i].psz_string );
427 mvar_AppendNewVar( itm, "id", val_list.p_list->p_values[i].psz_string );
428 snprintf( psz_int, sizeof(psz_int), "%d",
429 ( !strcmp( val.psz_string,
430 val_list.p_list->p_values[i].psz_string )
431 && !( i_type & VLC_VAR_ISCOMMAND ) ) );
432 mvar_AppendNewVar( itm, "selected", psz_int );
433 mvar_AppendVar( s, itm );
436 case VLC_VAR_INTEGER:
437 itm = mvar_New( name, "set" );
438 mvar_AppendNewVar( itm, "name", text_list.p_list->p_values[i].psz_string );
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_FreeList( &val_list, &text_list );
460 mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
462 mvar_t *s = mvar_New( name, "set" );
466 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
468 for( i= 0; i < info.i_count; )
472 inf = mvar_New( name, "set" );
475 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
476 info.info[i].psz_name, info.info[i].psz_value ); */
477 mvar_AppendNewVar( inf,
478 info.info[i].psz_name,
479 info.info[i].psz_value );
481 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
482 mvar_AppendVar( s, inf );
487 for( i = 0; i < info.i_count; i++ )
489 free( info.info[i].psz_name );
490 free( info.info[i].psz_value );
492 if( info.i_count > 0 )
501 mvar_t *mvar_FileSetNew( intf_thread_t *p_intf, char *name,
504 mvar_t *s = mvar_New( name, "set" );
505 char **ppsz_dir_content;
506 int i_dir_content, i;
507 psz_dir = RealPath( psz_dir );
509 /* parse psz_src dir */
510 if( ( i_dir_content = utf8_scandir( psz_dir, &ppsz_dir_content, Filter,
511 InsensitiveAlphasort ) ) == -1 )
513 if( errno != ENOENT && errno != ENOTDIR )
514 msg_Warn( p_intf, "error while scanning dir %s (%m)", psz_dir );
519 for( i = 0; i < i_dir_content; i++ )
521 #ifdef HAVE_SYS_STAT_H
522 struct stat stat_info;
524 char *psz_name = ppsz_dir_content[i], *psz_ext, *psz_dummy;
525 char psz_tmp[strlen( psz_dir ) + 1 + strlen( psz_name ) + 1];
529 if( psz_dir[0] == '\0' || (psz_dir[0] == '\\' && psz_dir[1] == '\0') )
531 strcpy( psz_tmp, psz_name );
536 sprintf( psz_tmp, "%s"DIR_SEP"%s", psz_dir, psz_name );
538 #ifdef HAVE_SYS_STAT_H
539 if( utf8_stat( psz_tmp, &stat_info ) == -1 )
546 f = mvar_New( name, "set" );
548 /* put lower-case file extension in 'ext' */
549 psz_ext = strrchr( psz_name, '.' );
550 psz_ext = strdup( psz_ext != NULL ? psz_ext + 1 : "" );
551 for( psz_dummy = psz_ext; *psz_dummy != '\0'; psz_dummy++ )
552 *psz_dummy = tolower( *psz_dummy );
554 mvar_AppendNewVar( f, "ext", psz_ext );
558 if( psz_dir[0] == '\0' || (psz_dir[0] == '\\' && psz_dir[1] == '\0') )
561 sprintf( psz_tmp, "%c:", psz_name[0] );
562 mvar_AppendNewVar( f, "name", psz_name );
563 mvar_AppendNewVar( f, "basename", psz_tmp );
564 mvar_AppendNewVar( f, "type", "directory" );
565 mvar_AppendNewVar( f, "size", "unknown" );
566 mvar_AppendNewVar( f, "date", "unknown" );
572 char psz_tmp[strlen( psz_dir ) + 1 + strlen( psz_name ) + 1];
574 sprintf( psz_tmp, "%s"DIR_SEP"%s", psz_dir, psz_name );
575 mvar_AppendNewVar( f, "name", psz_tmp );
576 mvar_AppendNewVar( f, "basename", psz_name );
578 #ifdef HAVE_SYS_STAT_H
579 if( S_ISDIR( stat_info.st_mode ) )
581 mvar_AppendNewVar( f, "type", "directory" );
583 else if( S_ISREG( stat_info.st_mode ) )
585 mvar_AppendNewVar( f, "type", "file" );
589 mvar_AppendNewVar( f, "type", "unknown" );
592 snprintf( psz_buf, sizeof( psz_buf ), "%"PRId64,
593 (int64_t)stat_info.st_size );
594 mvar_AppendNewVar( f, "size", psz_buf );
596 /* FIXME memory leak FIXME */
598 ctime_r( &stat_info.st_mtime, psz_buf );
599 mvar_AppendNewVar( f, "date", psz_buf );
601 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
605 mvar_AppendNewVar( f, "type", "unknown" );
606 mvar_AppendNewVar( f, "size", "unknown" );
607 mvar_AppendNewVar( f, "date", "unknown" );
611 mvar_AppendVar( s, f );
617 free( ppsz_dir_content );
621 static void mvar_VlmSetNewLoop( char *name, vlm_t *vlm, mvar_t *s,
622 vlm_message_t *el, bool b_name )
628 /* Add a node with name and info */
629 set = mvar_New( name, "set" );
632 mvar_AppendNewVar( set, "name", el->psz_name );
635 for( k = 0; k < el->i_child; k++ )
637 vlm_message_t *ch = el->child[k];
638 if( ch->i_child > 0 )
640 mvar_VlmSetNewLoop( ch->psz_name, vlm, set, ch, false );
646 mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
650 mvar_AppendNewVar( set, el->psz_name, ch->psz_name );
655 mvar_AppendVar( s, set );
658 mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
660 mvar_t *s = mvar_New( name, "set" );
665 if( vlm == NULL ) return s;
667 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
670 for( i = 0; i < msg->i_child; i++ )
672 /* Over media, schedule */
673 vlm_message_t *ch = msg->child[i];
676 for( j = 0; j < ch->i_child; j++ )
679 vlm_message_t *el = ch->child[j];
680 vlm_message_t *inf, *desc;
681 char psz[6 + strlen(el->psz_name)];
683 sprintf( psz, "show %s", el->psz_name );
684 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
686 desc = inf->child[0];
688 mvar_VlmSetNewLoop( el->psz_name, vlm, s, desc, true );
690 vlm_MessageDelete( inf );
693 vlm_MessageDelete( msg );
694 #endif /* ENABLE_VLM */