access: http: only warn on deflate errors
[vlc.git] / src / input / vlmshell.c
blob97edd9032ebd86d6a3b4e445beffb1570c537794
1 /*****************************************************************************
2 * vlmshell.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000-2005 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Simon Latapie <garf@videolan.org>
8 * Laurent Aimar <fenrir@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
35 #include <stdio.h>
36 #include <ctype.h> /* tolower() */
37 #include <assert.h>
39 #include <vlc_vlm.h>
41 #ifdef ENABLE_VLM
43 #include <time.h> /* ctime() */
45 #include <vlc_input.h>
46 #include "input_internal.h"
47 #include <vlc_stream.h>
48 #include "vlm_internal.h"
49 #include <vlc_charset.h>
50 #include <vlc_fs.h>
51 #include <vlc_sout.h>
52 #include <vlc_url.h>
53 #include "../stream_output/stream_output.h"
54 #include "../libvlc.h"
56 /*****************************************************************************
57 * Local prototypes.
58 *****************************************************************************/
60 /* */
61 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_sys_t *, vlm_schedule_sys_t *, const char * );
63 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *, const char * );
65 static char *Save( vlm_t * );
66 static int Load( vlm_t *, char * );
68 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
69 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
70 const char *psz_value );
72 /* */
73 static vlm_media_sys_t *vlm_MediaSearch( vlm_t *, const char *);
75 static const char quotes[] = "\"'";
76 /**
77 * FindCommandEnd: look for the end of a possibly quoted string
78 * @return NULL on mal-formatted string,
79 * pointer past the last character otherwise.
81 static const char *FindCommandEnd( const char *psz_sent )
83 unsigned char c, quote = 0;
85 while( (c = *psz_sent) != '\0' )
87 if( !quote )
89 if( strchr(quotes,c) ) // opening quote
90 quote = c;
91 else if( isspace(c) ) // non-escaped space
92 return psz_sent;
93 else if( c == '\\' )
95 psz_sent++; // skip escaped character
96 if( *psz_sent == '\0' )
97 return psz_sent;
100 else
102 if( c == quote ) // non-escaped matching quote
103 quote = 0;
104 else if( (quote == '"') && (c == '\\') )
106 psz_sent++; // skip escaped character
107 if (*psz_sent == '\0')
108 return NULL; // error, closing quote missing
111 psz_sent++;
114 // error (NULL) if we could not find a matching quote
115 return quote ? NULL : psz_sent;
120 * Unescape a nul-terminated string.
121 * Note that in and out can be identical.
123 * @param out output buffer (at least <strlen (in) + 1> characters long)
124 * @param in nul-terminated string to be unescaped
126 * @return 0 on success, -1 on error.
128 static int Unescape( char *out, const char *in )
130 unsigned char c, quote = 0;
131 bool param = false;
133 while( (c = *in++) != '\0' )
135 // Don't escape the end of the string if we find a '#'
136 // that's the begining of a vlc command
137 // TODO: find a better solution
138 if( ( c == '#' && !quote ) || param )
140 param = true;
141 *out++ = c;
142 continue;
145 if( !quote )
147 if (strchr(quotes,c)) // opening quote
149 quote = c;
150 continue;
152 else if( c == '\\' )
154 switch (c = *in++)
156 case '"':
157 case '\'':
158 case '\\':
159 *out++ = c;
160 continue;
162 case '\0':
163 *out = '\0';
164 return 0;
166 if( isspace(c) )
168 *out++ = c;
169 continue;
171 /* None of the special cases - copy the backslash */
172 *out++ = '\\';
175 else
177 if( c == quote ) // non-escaped matching quote
179 quote = 0;
180 continue;
182 if( (quote == '"') && (c == '\\') )
184 switch( c = *in++ )
186 case '"':
187 case '\\':
188 *out++ = c;
189 continue;
191 case '\0': // should never happen
192 *out = '\0';
193 return -1;
195 /* None of the special cases - copy the backslash */
196 *out++ = '\\';
199 *out++ = c;
202 *out = '\0';
203 return 0;
207 /*****************************************************************************
208 * ExecuteCommand: The main state machine
209 *****************************************************************************
210 * Execute a command which ends with '\0' (string)
211 *****************************************************************************/
212 static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
214 *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
215 return VLC_EGENERIC;
218 static bool ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
220 int64_t id;
222 if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
223 return false;
224 return true;
226 static bool ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
228 if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
229 return false;
230 return true;
233 static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
235 vlm_media_sys_t *p_media;
236 vlm_schedule_sys_t *p_schedule;
238 p_media = vlm_MediaSearch( p_vlm, psz_name );
239 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
241 if( p_schedule != NULL )
243 vlm_ScheduleDelete( p_vlm, p_schedule );
245 else if( p_media != NULL )
247 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
249 else if( !strcmp(psz_name, "media") )
251 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
253 else if( !strcmp(psz_name, "schedule") )
255 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
257 else if( !strcmp(psz_name, "all") )
259 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
260 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
262 else
264 *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
265 return VLC_EGENERIC;
268 *pp_status = vlm_MessageSimpleNew( "del" );
269 return VLC_SUCCESS;
272 static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
274 vlm_media_sys_t *p_media;
275 vlm_schedule_sys_t *p_schedule;
277 if( !psz_name )
279 *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
280 return VLC_SUCCESS;
283 p_media = vlm_MediaSearch( p_vlm, psz_name );
284 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
286 if( p_schedule != NULL )
287 *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
288 else if( p_media != NULL )
289 *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
290 else
291 *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );
293 return VLC_SUCCESS;
296 static int ExecuteHelp( vlm_message_t **pp_status )
298 vlm_message_t *message_child;
300 #define MessageAdd( a ) \
301 vlm_MessageAdd( *pp_status, vlm_MessageSimpleNew( a ) );
302 #define MessageAddChild( a ) \
303 vlm_MessageAdd( message_child, vlm_MessageSimpleNew( a ) );
305 *pp_status = vlm_MessageSimpleNew( "help" );
307 message_child = MessageAdd( "Commands Syntax:" );
308 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
309 MessageAddChild( "setup (name) (properties)" );
310 MessageAddChild( "show [(name)|media|schedule]" );
311 MessageAddChild( "del (name)|all|media|schedule" );
312 MessageAddChild( "control (name) [instance_name] (command)" );
313 MessageAddChild( "save (config_file)" );
314 MessageAddChild( "export" );
315 MessageAddChild( "load (config_file)" );
317 message_child = MessageAdd( "Media Proprieties Syntax:" );
318 MessageAddChild( "input (input_name)" );
319 MessageAddChild( "inputdel (input_name)|all" );
320 MessageAddChild( "inputdeln input_number" );
321 MessageAddChild( "output (output_name)" );
322 MessageAddChild( "option (option_name)[=value]" );
323 MessageAddChild( "enabled|disabled" );
324 MessageAddChild( "loop|unloop (broadcast only)" );
325 MessageAddChild( "mux (mux_name)" );
327 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
328 MessageAddChild( "enabled|disabled" );
329 MessageAddChild( "append (command_until_rest_of_the_line)" );
330 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
331 "(seconds)|now" );
332 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
333 "(days)-(hours):(minutes):(seconds)" );
334 MessageAddChild( "repeat (number_of_repetitions)" );
336 message_child = MessageAdd( "Control Commands Syntax:" );
337 MessageAddChild( "play [input_number]" );
338 MessageAddChild( "pause" );
339 MessageAddChild( "stop" );
340 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](milliseconds)ms" );
342 return VLC_SUCCESS;
345 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
347 vlm_media_sys_t *p_media;
348 const char *psz_control = NULL;
349 const char *psz_instance = NULL;
350 const char *psz_argument = NULL;
351 int i_index;
352 int i_result;
354 if( !ExecuteIsMedia( p_vlm, psz_name ) )
356 *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
357 return VLC_EGENERIC;
360 assert( i_arg > 0 );
362 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
363 i_index = 0;
364 if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
366 i_index = 1;
367 psz_instance = ppsz_arg[0];
369 if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
370 return ExecuteSyntaxError( "control", pp_status );
372 #undef IS
373 psz_control = ppsz_arg[i_index];
375 if( i_index+1 < i_arg )
376 psz_argument = ppsz_arg[i_index+1];
378 p_media = vlm_MediaSearch( p_vlm, psz_name );
379 assert( p_media );
381 if( !strcmp( psz_control, "play" ) )
383 int i_input_index = 0;
384 int i;
386 if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
388 i_input_index = i-1;
390 else if( psz_argument )
392 int j;
393 vlm_media_t *p_cfg = &p_media->cfg;
394 for ( j=0; j < p_cfg->i_input; j++)
396 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
398 i_input_index = j;
399 break;
404 if( p_media->cfg.b_vod )
405 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_VOD_INSTANCE, p_media->cfg.id, psz_instance, i_input_index, NULL ); // we should get here now
406 else
407 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
409 else if( !strcmp( psz_control, "seek" ) )
411 if( psz_argument )
413 bool b_relative;
414 if( psz_argument[0] == '+' || psz_argument[0] == '-' )
415 b_relative = true;
416 else
417 b_relative = false;
419 if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
421 /* Time (ms or s) */
422 int64_t i_new_time;
424 if( strstr( psz_argument, "ms" ) )
425 i_new_time = 1000 * (int64_t)atoi( psz_argument );
426 else
427 i_new_time = 1000000 * (int64_t)atoi( psz_argument );
429 if( b_relative )
431 int64_t i_time = 0;
432 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
433 i_new_time += i_time;
435 if( i_new_time < 0 )
436 i_new_time = 0;
437 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
439 else
441 /* Percent */
442 double d_new_position = us_atof( psz_argument ) / 100.0;
444 if( b_relative )
446 double d_position = 0.0;
448 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
449 d_new_position += d_position;
451 if( d_new_position < 0.0 )
452 d_new_position = 0.0;
453 else if( d_new_position > 1.0 )
454 d_new_position = 1.0;
455 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
458 else
460 i_result = VLC_EGENERIC;
463 else if( !strcmp( psz_control, "stop" ) )
465 i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
467 else if( !strcmp( psz_control, "pause" ) )
469 i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
471 else
473 i_result = VLC_EGENERIC;
476 if( i_result )
478 *pp_status = vlm_MessageNew( "control", "unknown error" );
479 return VLC_SUCCESS;
481 *pp_status = vlm_MessageSimpleNew( "control" );
482 return VLC_SUCCESS;
485 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
487 char *psz_export = Save( p_vlm );
489 *pp_status = vlm_MessageNew( "export", "%s", psz_export );
490 free( psz_export );
491 return VLC_SUCCESS;
494 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
496 FILE *f = vlc_fopen( psz_file, "wt" );
497 char *psz_save = NULL;
499 if( !f )
500 goto error;
502 psz_save = Save( p_vlm );
503 if( psz_save == NULL )
504 goto error;
505 if( fputs( psz_save, f ) == EOF )
506 goto error;;
507 if( fclose( f ) )
509 f = NULL;
510 goto error;
513 free( psz_save );
515 *pp_status = vlm_MessageSimpleNew( "save" );
516 return VLC_SUCCESS;
518 error:
519 free( psz_save );
520 if( f )
521 fclose( f );
522 *pp_status = vlm_MessageNew( "save", "Unable to save to file");
523 return VLC_EGENERIC;
526 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_path, vlm_message_t **pp_status )
528 char *psz_url = vlc_path2uri( psz_path, NULL );
529 stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
530 free( psz_url );
531 uint64_t i_size;
532 char *psz_buffer;
534 if( !p_stream )
536 *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
537 return VLC_EGENERIC;
540 /* FIXME needed ? */
541 if( stream_Seek( p_stream, 0 ) != 0 )
543 stream_Delete( p_stream );
545 *pp_status = vlm_MessageNew( "load", "Read file error" );
546 return VLC_EGENERIC;
549 i_size = stream_Size( p_stream );
550 if( i_size > SIZE_MAX - 1 )
551 i_size = SIZE_MAX - 1;
553 psz_buffer = malloc( i_size + 1 );
554 if( !psz_buffer )
556 stream_Delete( p_stream );
558 *pp_status = vlm_MessageNew( "load", "Read file error" );
559 return VLC_EGENERIC;
562 stream_Read( p_stream, psz_buffer, i_size );
563 psz_buffer[i_size] = '\0';
565 stream_Delete( p_stream );
567 if( Load( p_vlm, psz_buffer ) )
569 free( psz_buffer );
571 *pp_status = vlm_MessageNew( "load", "Error while loading file" );
572 return VLC_EGENERIC;
575 free( psz_buffer );
577 *pp_status = vlm_MessageSimpleNew( "load" );
578 return VLC_SUCCESS;
581 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, bool b_new,
582 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
584 const char *psz_cmd = b_new ? "new" : "setup";
585 int i;
587 for( i = 0; i < i_property; i++ )
589 if( !strcmp( ppsz_property[i], "enabled" ) ||
590 !strcmp( ppsz_property[i], "disabled" ) )
592 if ( vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL ) )
593 goto error;
595 else if( !strcmp( ppsz_property[i], "append" ) )
597 char *psz_line;
598 int j;
599 /* Beware: everything behind append is considered as
600 * command line */
602 if( ++i >= i_property )
603 break;
605 psz_line = strdup( ppsz_property[i] );
606 for( j = i+1; j < i_property; j++ )
608 psz_line = xrealloc( psz_line,
609 strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
610 strcat( psz_line, " " );
611 strcat( psz_line, ppsz_property[j] );
614 if( vlm_ScheduleSetup( p_schedule, "append", psz_line ) )
615 goto error;
616 break;
618 else
620 if( i + 1 >= i_property )
622 if( b_new )
623 vlm_ScheduleDelete( p_vlm, p_schedule );
624 return ExecuteSyntaxError( psz_cmd, pp_status );
627 if( vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] ) )
628 goto error;
629 i++;
632 *pp_status = vlm_MessageSimpleNew( psz_cmd );
634 vlc_mutex_lock( &p_vlm->lock_manage );
635 p_vlm->input_state_changed = true;
636 vlc_cond_signal( &p_vlm->wait_manage );
637 vlc_mutex_unlock( &p_vlm->lock_manage );
639 return VLC_SUCCESS;
641 error:
642 *pp_status = vlm_MessageNew( psz_cmd, "Error while setting the property '%s' to the schedule",
643 ppsz_property[i] );
644 return VLC_EGENERIC;
647 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, bool b_new,
648 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
650 const char *psz_cmd = b_new ? "new" : "setup";
651 vlm_media_t *p_cfg = NULL;
652 int i_result;
653 int i;
655 #undef ERROR
656 #undef MISSING
657 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
658 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
659 ERROR( "unknown media" );
661 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
662 for( i = 0; i < i_property; i++ )
664 const char *psz_option = ppsz_property[i];
665 const char *psz_value = i+1 < i_property ? ppsz_property[i+1] : NULL;
667 if( !strcmp( psz_option, "enabled" ) )
669 p_cfg->b_enabled = true;
671 else if( !strcmp( psz_option, "disabled" ) )
673 p_cfg->b_enabled = false;
675 else if( !strcmp( psz_option, "input" ) )
677 MISSING( "input" );
678 TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
679 i++;
681 else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
683 while( p_cfg->i_input > 0 )
684 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
685 i++;
687 else if( !strcmp( psz_option, "inputdel" ) )
689 int j;
691 MISSING( "inputdel" );
693 for( j = 0; j < p_cfg->i_input; j++ )
695 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
697 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
698 break;
701 i++;
703 else if( !strcmp( psz_option, "inputdeln" ) )
705 MISSING( "inputdeln" );
707 int idx = atoi( psz_value );
708 if( idx > 0 && idx <= p_cfg->i_input )
709 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[idx-1] );
710 i++;
712 else if( !strcmp( psz_option, "output" ) )
714 MISSING( "output" );
716 free( p_cfg->psz_output );
717 p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
718 i++;
720 else if( !strcmp( psz_option, "option" ) )
722 MISSING( "option" );
724 TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
725 i++;
727 else if( !strcmp( psz_option, "loop" ) )
729 if( p_cfg->b_vod )
730 ERROR( "invalid loop option for vod" );
731 p_cfg->broadcast.b_loop = true;
733 else if( !strcmp( psz_option, "unloop" ) )
735 if( p_cfg->b_vod )
736 ERROR( "invalid unloop option for vod" );
737 p_cfg->broadcast.b_loop = false;
739 else if( !strcmp( psz_option, "mux" ) )
741 MISSING( "mux" );
742 if( !p_cfg->b_vod )
743 ERROR( "invalid mux option for broadcast" );
745 free( p_cfg->vod.psz_mux );
746 p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
747 i++;
749 else
751 fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
752 ERROR( "Wrong command syntax" );
755 #undef MISSING
756 #undef ERROR
758 /* */
759 i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
760 vlm_media_Delete( p_cfg );
762 *pp_status = vlm_MessageSimpleNew( psz_cmd );
763 return i_result;
765 error:
766 if( p_cfg )
768 if( b_new )
769 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
770 vlm_media_Delete( p_cfg );
772 return VLC_EGENERIC;
775 static int ExecuteNew( vlm_t *p_vlm, const char *psz_name, const char *psz_type, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
777 /* Check name */
778 if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
780 *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
781 return VLC_EGENERIC;
783 if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
785 *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
786 return VLC_EGENERIC;
788 /* */
789 if( !strcmp( psz_type, "schedule" ) )
791 vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
792 if( !p_schedule )
794 *pp_status = vlm_MessageNew( "new", "could not create schedule" );
795 return VLC_EGENERIC;
797 return ExecuteScheduleProperty( p_vlm, p_schedule, true, i_property, ppsz_property, pp_status );
799 else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
801 vlm_media_t cfg;
802 int64_t id;
804 vlm_media_Init( &cfg );
805 cfg.psz_name = strdup( psz_name );
806 cfg.b_vod = !strcmp( psz_type, "vod" );
808 if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
810 vlm_media_Clean( &cfg );
811 *pp_status = vlm_MessageNew( "new", "could not create media" );
812 return VLC_EGENERIC;
814 vlm_media_Clean( &cfg );
815 return ExecuteMediaProperty( p_vlm, id, true, i_property, ppsz_property, pp_status );
817 else
819 *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
820 return VLC_EGENERIC;
824 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
826 if( ExecuteIsSchedule( p_vlm, psz_name ) )
828 vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
829 return ExecuteScheduleProperty( p_vlm, p_schedule, false, i_property, ppsz_property, pp_status );
831 else if( ExecuteIsMedia( p_vlm, psz_name ) )
833 int64_t id;
834 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
835 goto error;
836 return ExecuteMediaProperty( p_vlm, id, false, i_property, ppsz_property, pp_status );
839 error:
840 *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
841 return VLC_EGENERIC;
844 int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
845 vlm_message_t **pp_message )
847 size_t i_command = 0;
848 size_t i_command_len = strlen( psz_command );
849 char *buf = malloc( i_command_len + 1 ), *psz_buf = buf;
850 size_t i_ppsz_command_len = (3 + (i_command_len + 1) / 2);
851 char **ppsz_command = malloc( i_ppsz_command_len * sizeof(char *) );
852 vlm_message_t *p_message = NULL;
853 int i_ret = 0;
855 if( !psz_buf || !ppsz_command )
857 p_message = vlm_MessageNew( ppsz_command[0],
858 "Memory allocation failed for command of length %zu",
859 i_command_len );
860 goto error;
863 /* First, parse the line and cut it */
864 while( *psz_command != '\0' )
866 const char *psz_temp;
868 if(isspace ((unsigned char)*psz_command))
870 psz_command++;
871 continue;
874 /* support for comments */
875 if( i_command == 0 && *psz_command == '#')
877 p_message = vlm_MessageSimpleNew( "" );
878 goto success;
881 psz_temp = FindCommandEnd( psz_command );
883 if( psz_temp == NULL )
885 p_message = vlm_MessageNew( "Incomplete command", "%s", psz_command );
886 goto error;
889 assert (i_command < i_ppsz_command_len);
891 ppsz_command[i_command] = psz_buf;
892 memcpy (psz_buf, psz_command, psz_temp - psz_command);
893 psz_buf[psz_temp - psz_command] = '\0';
895 Unescape (psz_buf, psz_buf);
897 i_command++;
898 psz_buf += psz_temp - psz_command + 1;
899 psz_command = psz_temp;
901 assert (buf + i_command_len + 1 >= psz_buf);
905 * And then Interpret it
908 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error; if( (cmd) ) goto error; goto success; }
909 if( i_command == 0 )
911 p_message = vlm_MessageSimpleNew( "" );
912 goto success;
914 else IF_EXECUTE( "del", (i_command != 2), ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
915 else IF_EXECUTE( "show", (i_command > 2), ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
916 else IF_EXECUTE( "help", (i_command != 1), ExecuteHelp( &p_message ) )
917 else IF_EXECUTE( "control", (i_command < 3), ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
918 else IF_EXECUTE( "save", (i_command != 2), ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
919 else IF_EXECUTE( "export", (i_command != 1), ExecuteExport(p_vlm, &p_message) )
920 else IF_EXECUTE( "load", (i_command != 2), ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
921 else IF_EXECUTE( "new", (i_command < 3), ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
922 else IF_EXECUTE( "setup", (i_command < 2), ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
923 else
925 p_message = vlm_MessageNew( ppsz_command[0], "Unknown VLM command" );
926 goto error;
928 #undef IF_EXECUTE
930 success:
931 *pp_message = p_message;
932 free( buf );
933 free( ppsz_command );
934 return VLC_SUCCESS;
936 syntax_error:
937 i_ret = ExecuteSyntaxError( ppsz_command[0], pp_message );
938 free( buf );
939 free( ppsz_command );
940 return i_ret;
942 error:
943 *pp_message = p_message;
944 free( buf );
945 free( ppsz_command );
946 return VLC_EGENERIC;
949 /*****************************************************************************
950 * Media handling
951 *****************************************************************************/
952 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
954 int i;
956 for( i = 0; i < vlm->i_media; i++ )
958 if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
959 return vlm->media[i];
962 return NULL;
965 /*****************************************************************************
966 * Schedule handling
967 *****************************************************************************/
968 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
970 if( !psz_name )
971 return NULL;
973 vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
974 if( !p_sched )
975 return NULL;
977 p_sched->psz_name = strdup( psz_name );
978 p_sched->b_enabled = false;
979 p_sched->i_command = 0;
980 p_sched->command = NULL;
981 p_sched->i_date = 0;
982 p_sched->i_period = 0;
983 p_sched->i_repeat = -1;
985 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
987 return p_sched;
990 /* for now, simple delete. After, del with options (last arg) */
991 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched )
993 if( sched == NULL ) return;
995 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
997 if( vlm->i_schedule == 0 ) free( vlm->schedule );
998 free( sched->psz_name );
999 while( sched->i_command )
1001 char *psz_cmd = sched->command[0];
1002 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1003 free( psz_cmd );
1005 free( sched );
1008 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1010 int i;
1012 for( i = 0; i < vlm->i_schedule; i++ )
1014 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1016 return vlm->schedule[i];
1020 return NULL;
1023 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1024 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1025 const char *psz_value )
1027 if( !strcmp( psz_cmd, "enabled" ) )
1029 schedule->b_enabled = true;
1031 else if( !strcmp( psz_cmd, "disabled" ) )
1033 schedule->b_enabled = false;
1035 else if( !strcmp( psz_cmd, "date" ) )
1037 struct tm time;
1038 const char *p;
1039 time_t date;
1041 time.tm_sec = 0; /* seconds */
1042 time.tm_min = 0; /* minutes */
1043 time.tm_hour = 0; /* hours */
1044 time.tm_mday = 0; /* day of the month */
1045 time.tm_mon = 0; /* month */
1046 time.tm_year = 0; /* year */
1047 time.tm_wday = 0; /* day of the week */
1048 time.tm_yday = 0; /* day in the year */
1049 time.tm_isdst = -1; /* daylight saving time */
1051 /* date should be year/month/day-hour:minutes:seconds */
1052 p = strchr( psz_value, '-' );
1054 if( !strcmp( psz_value, "now" ) )
1056 schedule->i_date = 0;
1058 else if(p == NULL)
1060 return 1;
1062 else
1064 unsigned i,j,k;
1066 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1068 case 1:
1069 time.tm_sec = i;
1070 break;
1071 case 2:
1072 time.tm_min = i;
1073 time.tm_sec = j;
1074 break;
1075 case 3:
1076 time.tm_hour = i;
1077 time.tm_min = j;
1078 time.tm_sec = k;
1079 break;
1080 default:
1081 return 1;
1084 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1086 case 1:
1087 time.tm_mday = i;
1088 break;
1089 case 2:
1090 time.tm_mon = i - 1;
1091 time.tm_mday = j;
1092 break;
1093 case 3:
1094 time.tm_year = i - 1900;
1095 time.tm_mon = j - 1;
1096 time.tm_mday = k;
1097 break;
1098 default:
1099 return 1;
1102 date = mktime( &time );
1103 schedule->i_date = ((mtime_t) date) * 1000000;
1106 else if( !strcmp( psz_cmd, "period" ) )
1108 struct tm time;
1109 const char *p;
1110 const char *psz_time = NULL, *psz_date = NULL;
1111 time_t date;
1112 unsigned i,j,k;
1114 /* First, if date or period are modified, repeat should be equal to -1 */
1115 schedule->i_repeat = -1;
1117 time.tm_sec = 0; /* seconds */
1118 time.tm_min = 0; /* minutes */
1119 time.tm_hour = 0; /* hours */
1120 time.tm_mday = 0; /* day of the month */
1121 time.tm_mon = 0; /* month */
1122 time.tm_year = 0; /* year */
1123 time.tm_wday = 0; /* day of the week */
1124 time.tm_yday = 0; /* day in the year */
1125 time.tm_isdst = -1; /* daylight saving time */
1127 /* date should be year/month/day-hour:minutes:seconds */
1128 p = strchr( psz_value, '-' );
1129 if( p )
1131 psz_date = psz_value;
1132 psz_time = p + 1;
1134 else
1136 psz_time = psz_value;
1139 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1141 case 1:
1142 time.tm_sec = i;
1143 break;
1144 case 2:
1145 time.tm_min = i;
1146 time.tm_sec = j;
1147 break;
1148 case 3:
1149 time.tm_hour = i;
1150 time.tm_min = j;
1151 time.tm_sec = k;
1152 break;
1153 default:
1154 return 1;
1156 if( psz_date )
1158 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1160 case 1:
1161 time.tm_mday = i;
1162 break;
1163 case 2:
1164 time.tm_mon = i;
1165 time.tm_mday = j;
1166 break;
1167 case 3:
1168 time.tm_year = i;
1169 time.tm_mon = j;
1170 time.tm_mday = k;
1171 break;
1172 default:
1173 return 1;
1177 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1178 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1179 schedule->i_period = ((mtime_t) date) * 1000000;
1181 else if( !strcmp( psz_cmd, "repeat" ) )
1183 int i;
1185 if( sscanf( psz_value, "%d", &i ) == 1 )
1187 schedule->i_repeat = i;
1189 else
1191 return 1;
1194 else if( !strcmp( psz_cmd, "append" ) )
1196 char *command = strdup( psz_value );
1198 TAB_APPEND( schedule->i_command, schedule->command, command );
1200 else
1202 return 1;
1205 return 0;
1208 /*****************************************************************************
1209 * Message handling functions
1210 *****************************************************************************/
1211 vlm_message_t *vlm_MessageSimpleNew( const char *psz_name )
1213 if( !psz_name ) return NULL;
1215 vlm_message_t *p_message = malloc( sizeof(*p_message) );
1216 if( !p_message )
1217 return NULL;
1219 p_message->psz_name = strdup( psz_name );
1220 if( !p_message->psz_name )
1222 free( p_message );
1223 return NULL;
1225 p_message->psz_value = NULL;
1226 p_message->i_child = 0;
1227 p_message->child = NULL;
1229 return p_message;
1232 vlm_message_t *vlm_MessageNew( const char *psz_name,
1233 const char *psz_format, ... )
1235 vlm_message_t *p_message = vlm_MessageSimpleNew( psz_name );
1236 va_list args;
1238 if( !p_message )
1239 return NULL;
1241 assert( psz_format );
1242 va_start( args, psz_format );
1243 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1244 p_message->psz_value = NULL;
1245 va_end( args );
1247 if( !p_message->psz_value )
1249 vlm_MessageDelete( p_message );
1250 return NULL;
1252 return p_message;
1255 void vlm_MessageDelete( vlm_message_t *p_message )
1257 free( p_message->psz_name );
1258 free( p_message->psz_value );
1259 while( p_message->i_child-- )
1260 vlm_MessageDelete( p_message->child[p_message->i_child] );
1261 free( p_message->child );
1262 free( p_message );
1265 /* Add a child */
1266 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1267 vlm_message_t *p_child )
1269 if( p_message == NULL ) return NULL;
1271 if( p_child )
1273 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1276 return p_child;
1279 /*****************************************************************************
1280 * Misc utility functions
1281 *****************************************************************************/
1282 static vlm_message_t *vlm_ShowMedia( vlm_media_sys_t *p_media )
1284 vlm_media_t *p_cfg = &p_media->cfg;
1285 vlm_message_t *p_msg;
1286 vlm_message_t *p_msg_sub;
1287 int i;
1289 p_msg = vlm_MessageSimpleNew( p_cfg->psz_name );
1290 vlm_MessageAdd( p_msg,
1291 vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1292 vlm_MessageAdd( p_msg,
1293 vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1295 if( p_cfg->b_vod )
1296 vlm_MessageAdd( p_msg,
1297 vlm_MessageNew( "mux", "%s", p_cfg->vod.psz_mux ) );
1298 else
1299 vlm_MessageAdd( p_msg,
1300 vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1302 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "inputs" ) );
1303 for( i = 0; i < p_cfg->i_input; i++ )
1305 char *psz_tmp;
1306 if( asprintf( &psz_tmp, "%d", i+1 ) != -1 )
1308 vlm_MessageAdd( p_msg_sub,
1309 vlm_MessageNew( psz_tmp, "%s", p_cfg->ppsz_input[i] ) );
1310 free( psz_tmp );
1314 vlm_MessageAdd( p_msg,
1315 vlm_MessageNew( "output", "%s", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1317 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "options" ) );
1318 for( i = 0; i < p_cfg->i_option; i++ )
1319 vlm_MessageAdd( p_msg_sub, vlm_MessageSimpleNew( p_cfg->ppsz_option[i] ) );
1321 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "instances" ) );
1322 for( i = 0; i < p_media->i_instance; i++ )
1324 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1325 vlc_value_t val;
1326 vlm_message_t *p_msg_instance;
1328 val.i_int = END_S;
1329 if( p_instance->p_input )
1330 var_Get( p_instance->p_input, "state", &val );
1332 p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageSimpleNew( "instance" ) );
1334 vlm_MessageAdd( p_msg_instance,
1335 vlm_MessageNew( "name" , "%s", p_instance->psz_name ? p_instance->psz_name : "default" ) );
1336 vlm_MessageAdd( p_msg_instance,
1337 vlm_MessageNew( "state",
1338 val.i_int == PLAYING_S ? "playing" :
1339 val.i_int == PAUSE_S ? "paused" :
1340 "stopped" ) );
1342 /* FIXME should not do that this way */
1343 if( p_instance->p_input )
1345 #define APPEND_INPUT_INFO( key, format, type ) \
1346 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( key, format, \
1347 var_Get ## type( p_instance->p_input, key ) ) )
1348 APPEND_INPUT_INFO( "position", "%f", Float );
1349 APPEND_INPUT_INFO( "time", "%"PRIi64, Time );
1350 APPEND_INPUT_INFO( "length", "%"PRIi64, Time );
1351 APPEND_INPUT_INFO( "rate", "%f", Float );
1352 APPEND_INPUT_INFO( "title", "%"PRId64, Integer );
1353 APPEND_INPUT_INFO( "chapter", "%"PRId64, Integer );
1354 APPEND_INPUT_INFO( "can-seek", "%d", Bool );
1356 #undef APPEND_INPUT_INFO
1357 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex",
1358 "%d", p_instance->i_index + 1 ) );
1360 return p_msg;
1363 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1364 vlm_schedule_sys_t *schedule,
1365 const char *psz_filter )
1367 if( media != NULL )
1369 vlm_message_t *p_msg = vlm_MessageSimpleNew( "show" );
1370 if( p_msg )
1371 vlm_MessageAdd( p_msg, vlm_ShowMedia( media ) );
1372 return p_msg;
1375 else if( schedule != NULL )
1377 int i;
1378 vlm_message_t *msg;
1379 vlm_message_t *msg_schedule;
1380 vlm_message_t *msg_child;
1381 char buffer[100];
1383 msg = vlm_MessageSimpleNew( "show" );
1384 msg_schedule =
1385 vlm_MessageAdd( msg, vlm_MessageSimpleNew( schedule->psz_name ) );
1387 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1389 vlm_MessageAdd( msg_schedule,
1390 vlm_MessageNew( "enabled", schedule->b_enabled ?
1391 "yes" : "no" ) );
1393 if( schedule->i_date != 0 )
1395 struct tm date;
1396 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1398 localtime_r( &i_time, &date);
1399 vlm_MessageAdd( msg_schedule,
1400 vlm_MessageNew( "date", "%d/%d/%d-%d:%d:%d",
1401 date.tm_year + 1900, date.tm_mon + 1,
1402 date.tm_mday, date.tm_hour, date.tm_min,
1403 date.tm_sec ) );
1405 else
1406 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1408 if( schedule->i_period != 0 )
1410 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1411 struct tm date;
1413 date.tm_sec = (int)( i_time % 60 );
1414 i_time = i_time / 60;
1415 date.tm_min = (int)( i_time % 60 );
1416 i_time = i_time / 60;
1417 date.tm_hour = (int)( i_time % 24 );
1418 i_time = i_time / 24;
1419 date.tm_mday = (int)( i_time % 30 );
1420 i_time = i_time / 30;
1421 /* okay, okay, months are not always 30 days long */
1422 date.tm_mon = (int)( i_time % 12 );
1423 i_time = i_time / 12;
1424 date.tm_year = (int)i_time;
1426 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1427 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1429 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "%s", buffer) );
1431 else
1432 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1434 sprintf( buffer, "%d", schedule->i_repeat );
1435 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", "%s", buffer ) );
1437 msg_child =
1438 vlm_MessageAdd( msg_schedule, vlm_MessageSimpleNew("commands" ) );
1440 for( i = 0; i < schedule->i_command; i++ )
1442 vlm_MessageAdd( msg_child,
1443 vlm_MessageSimpleNew( schedule->command[i] ) );
1446 return msg;
1450 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1452 vlm_message_t *p_msg;
1453 vlm_message_t *p_msg_child;
1454 int i_vod = 0, i_broadcast = 0;
1456 for( int i = 0; i < vlm->i_media; i++ )
1458 if( vlm->media[i]->cfg.b_vod )
1459 i_vod++;
1460 else
1461 i_broadcast++;
1464 p_msg = vlm_MessageSimpleNew( "show" );
1465 p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media",
1466 "( %d broadcast - %d vod )", i_broadcast,
1467 i_vod ) );
1469 for( int i = 0; i < vlm->i_media; i++ )
1470 vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm->media[i] ) );
1472 return p_msg;
1475 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1477 int i;
1478 vlm_message_t *msg;
1479 vlm_message_t *msg_child;
1481 msg = vlm_MessageSimpleNew( "show" );
1482 msg_child = vlm_MessageAdd( msg, vlm_MessageSimpleNew( "schedule" ) );
1484 for( i = 0; i < vlm->i_schedule; i++ )
1486 vlm_schedule_sys_t *s = vlm->schedule[i];
1487 vlm_message_t *msg_schedule;
1488 mtime_t i_time, i_next_date;
1490 msg_schedule = vlm_MessageAdd( msg_child,
1491 vlm_MessageSimpleNew( s->psz_name ) );
1492 vlm_MessageAdd( msg_schedule,
1493 vlm_MessageNew( "enabled", s->b_enabled ?
1494 "yes" : "no" ) );
1496 /* calculate next date */
1497 i_time = vlm_Date();
1498 i_next_date = s->i_date;
1500 if( s->i_period != 0 )
1502 int j = 0;
1503 while( s->i_date + j * s->i_period <= i_time &&
1504 s->i_repeat > j )
1506 j++;
1509 i_next_date = s->i_date + j * s->i_period;
1512 if( i_next_date > i_time )
1514 time_t i_date = (time_t) (i_next_date / 1000000) ;
1515 struct tm tm;
1516 char psz_date[32];
1518 strftime( psz_date, sizeof(psz_date), "%Y-%m-%d %H:%M:%S (%a)",
1519 localtime_r( &i_date, &tm ) );
1520 vlm_MessageAdd( msg_schedule,
1521 vlm_MessageNew( "next launch", "%s", psz_date ) );
1525 return msg;
1528 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1530 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1531 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1533 vlm_MessageAdd( show1, show2->child[0] );
1535 /* We must destroy the parent node "show" of show2
1536 * and not the children */
1537 free( show2->psz_name );
1538 free( show2 );
1540 return show1;
1543 else
1545 return vlm_MessageSimpleNew( "show" );
1549 /*****************************************************************************
1550 * Config handling functions
1551 *****************************************************************************/
1552 static int Load( vlm_t *vlm, char *file )
1554 char *pf = file;
1555 int i_line = 1;
1557 while( *pf != '\0' )
1559 vlm_message_t *message = NULL;
1560 int i_end = 0;
1562 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1564 i_end++;
1567 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1569 pf[i_end] = '\0';
1570 i_end++;
1571 if( pf[i_end] == '\n' ) i_end++;
1574 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1576 if( message )
1578 if( message->psz_value )
1579 msg_Err( vlm, "Load error on line %d: %s: %s",
1580 i_line, message->psz_name, message->psz_value );
1581 vlm_MessageDelete( message );
1583 return 1;
1585 if( message ) vlm_MessageDelete( message );
1587 pf += i_end;
1588 i_line++;
1591 return 0;
1594 static char *Save( vlm_t *vlm )
1596 char *save = NULL;
1597 char psz_header[] = "\n"
1598 "# VLC media player VLM command batch\n"
1599 "# http://www.videolan.org/vlc/\n\n" ;
1600 char *p;
1601 int i,j;
1602 int i_length = strlen( psz_header );
1604 for( i = 0; i < vlm->i_media; i++ )
1606 vlm_media_sys_t *media = vlm->media[i];
1607 vlm_media_t *p_cfg = &media->cfg;
1609 if( p_cfg->b_vod )
1610 i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1611 else
1612 i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1614 if( p_cfg->b_enabled )
1615 i_length += strlen( "enabled" );
1616 else
1617 i_length += strlen( "disabled" );
1619 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1620 i_length += strlen( " loop\n" );
1621 else
1622 i_length += strlen( "\n" );
1624 for( j = 0; j < p_cfg->i_input; j++ )
1625 i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1627 if( p_cfg->psz_output != NULL )
1628 i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1630 for( j = 0; j < p_cfg->i_option; j++ )
1631 i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1633 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1634 i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1637 for( i = 0; i < vlm->i_schedule; i++ )
1639 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1641 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
1643 if( schedule->b_enabled )
1645 i_length += strlen( "date //-:: enabled\n" ) + 14;
1647 else
1649 i_length += strlen( "date //-:: disabled\n" ) + 14;
1653 if( schedule->i_period != 0 )
1655 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
1656 strlen( "period //-::\n" ) + 14;
1659 if( schedule->i_repeat >= 0 )
1661 char buffer[12];
1663 sprintf( buffer, "%d", schedule->i_repeat );
1664 i_length += strlen( "setup repeat \n" ) +
1665 strlen( schedule->psz_name ) + strlen( buffer );
1667 else
1669 i_length++;
1672 for( j = 0; j < schedule->i_command; j++ )
1674 i_length += strlen( "setup append \n" ) +
1675 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1680 /* Don't forget the '\0' */
1681 i_length++;
1682 /* now we have the length of save */
1684 p = save = malloc( i_length );
1685 if( !save ) return NULL;
1686 *save = '\0';
1688 p += sprintf( p, "%s", psz_header );
1690 /* finally we can write in it */
1691 for( i = 0; i < vlm->i_media; i++ )
1693 vlm_media_sys_t *media = vlm->media[i];
1694 vlm_media_t *p_cfg = &media->cfg;
1696 if( p_cfg->b_vod )
1697 p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1698 else
1699 p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1701 if( p_cfg->b_enabled )
1702 p += sprintf( p, "enabled" );
1703 else
1704 p += sprintf( p, "disabled" );
1706 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1707 p += sprintf( p, " loop\n" );
1708 else
1709 p += sprintf( p, "\n" );
1711 for( j = 0; j < p_cfg->i_input; j++ )
1712 p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1714 if( p_cfg->psz_output )
1715 p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1717 for( j = 0; j < p_cfg->i_option; j++ )
1718 p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1720 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1721 p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1724 /* and now, the schedule scripts */
1725 for( i = 0; i < vlm->i_schedule; i++ )
1727 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1728 struct tm date;
1729 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1731 localtime_r( &i_time, &date);
1732 p += sprintf( p, "new %s schedule ", schedule->psz_name);
1734 if( schedule->b_enabled )
1736 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1737 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1738 date.tm_hour, date.tm_min, date.tm_sec );
1740 else
1742 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1743 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1744 date.tm_hour, date.tm_min, date.tm_sec);
1747 if( schedule->i_period != 0 )
1749 p += sprintf( p, "setup %s ", schedule->psz_name );
1751 i_time = (time_t) ( schedule->i_period / 1000000 );
1753 date.tm_sec = (int)( i_time % 60 );
1754 i_time = i_time / 60;
1755 date.tm_min = (int)( i_time % 60 );
1756 i_time = i_time / 60;
1757 date.tm_hour = (int)( i_time % 24 );
1758 i_time = i_time / 24;
1759 date.tm_mday = (int)( i_time % 30 );
1760 i_time = i_time / 30;
1761 /* okay, okay, months are not always 30 days long */
1762 date.tm_mon = (int)( i_time % 12 );
1763 i_time = i_time / 12;
1764 date.tm_year = (int)i_time;
1766 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1767 date.tm_year, date.tm_mon, date.tm_mday,
1768 date.tm_hour, date.tm_min, date.tm_sec);
1771 if( schedule->i_repeat >= 0 )
1773 p += sprintf( p, "setup %s repeat %d\n",
1774 schedule->psz_name, schedule->i_repeat );
1776 else
1778 p += sprintf( p, "\n" );
1781 for( j = 0; j < schedule->i_command; j++ )
1783 p += sprintf( p, "setup %s append %s\n",
1784 schedule->psz_name, schedule->command[j] );
1789 return save;
1792 #endif /* ENABLE_VLM */