video_filter: erase: use C99 loop declarations
[vlc.git] / src / input / vlmshell.c
blobd698639c83b06425d0f67befba9c4cd9e82759f9
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() */
44 #include <limits.h>
45 #include <fcntl.h>
46 #include <sys/stat.h>
48 #include <vlc_input.h>
49 #include "input_internal.h"
50 #include <vlc_stream.h>
51 #include "vlm_internal.h"
52 #include <vlc_charset.h>
53 #include <vlc_fs.h>
54 #include <vlc_sout.h>
55 #include "../stream_output/stream_output.h"
56 #include "../libvlc.h"
58 /*****************************************************************************
59 * Local prototypes.
60 *****************************************************************************/
62 /* */
63 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_sys_t *, vlm_schedule_sys_t *, const char * );
65 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *, const char * );
67 static char *Save( vlm_t * );
68 static int Load( vlm_t *, char * );
70 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
71 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
72 const char *psz_value );
74 /* */
75 static vlm_media_sys_t *vlm_MediaSearch( vlm_t *, const char *);
77 static const char quotes[] = "\"'";
78 /**
79 * FindCommandEnd: look for the end of a possibly quoted string
80 * @return NULL on mal-formatted string,
81 * pointer past the last character otherwise.
83 static const char *FindCommandEnd( const char *psz_sent )
85 unsigned char c, quote = 0;
87 while( (c = *psz_sent) != '\0' )
89 if( !quote )
91 if( strchr(quotes,c) ) // opening quote
92 quote = c;
93 else if( isspace(c) ) // non-escaped space
94 return psz_sent;
95 else if( c == '\\' )
97 psz_sent++; // skip escaped character
98 if( *psz_sent == '\0' )
99 return psz_sent;
102 else
104 if( c == quote ) // non-escaped matching quote
105 quote = 0;
106 else if( (quote == '"') && (c == '\\') )
108 psz_sent++; // skip escaped character
109 if (*psz_sent == '\0')
110 return NULL; // error, closing quote missing
113 psz_sent++;
116 // error (NULL) if we could not find a matching quote
117 return quote ? NULL : psz_sent;
122 * Unescape a nul-terminated string.
123 * Note that in and out can be identical.
125 * @param out output buffer (at least <strlen (in) + 1> characters long)
126 * @param in nul-terminated string to be unescaped
128 * @return 0 on success, -1 on error.
130 static int Unescape( char *out, const char *in )
132 unsigned char c, quote = 0;
133 bool param = false;
135 while( (c = *in++) != '\0' )
137 // Don't escape the end of the string if we find a '#'
138 // that's the begining of a vlc command
139 // TODO: find a better solution
140 if( ( c == '#' && !quote ) || param )
142 param = true;
143 *out++ = c;
144 continue;
147 if( !quote )
149 if (strchr(quotes,c)) // opening quote
151 quote = c;
152 continue;
154 else if( c == '\\' )
156 switch (c = *in++)
158 case '"':
159 case '\'':
160 case '\\':
161 *out++ = c;
162 continue;
164 case '\0':
165 *out = '\0';
166 return 0;
168 if( isspace(c) )
170 *out++ = c;
171 continue;
173 /* None of the special cases - copy the backslash */
174 *out++ = '\\';
177 else
179 if( c == quote ) // non-escaped matching quote
181 quote = 0;
182 continue;
184 if( (quote == '"') && (c == '\\') )
186 switch( c = *in++ )
188 case '"':
189 case '\\':
190 *out++ = c;
191 continue;
193 case '\0': // should never happen
194 *out = '\0';
195 return -1;
197 /* None of the special cases - copy the backslash */
198 *out++ = '\\';
201 *out++ = c;
204 *out = '\0';
205 return 0;
209 /*****************************************************************************
210 * ExecuteCommand: The main state machine
211 *****************************************************************************
212 * Execute a command which ends with '\0' (string)
213 *****************************************************************************/
214 static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
216 *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
217 return VLC_EGENERIC;
220 static bool ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
222 int64_t id;
224 if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
225 return false;
226 return true;
228 static bool ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
230 if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
231 return false;
232 return true;
235 static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
237 vlm_media_sys_t *p_media;
238 vlm_schedule_sys_t *p_schedule;
240 p_media = vlm_MediaSearch( p_vlm, psz_name );
241 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
243 if( p_schedule != NULL )
245 vlm_ScheduleDelete( p_vlm, p_schedule );
247 else if( p_media != NULL )
249 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
251 else if( !strcmp(psz_name, "media") )
253 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
255 else if( !strcmp(psz_name, "schedule") )
257 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
259 else if( !strcmp(psz_name, "all") )
261 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
262 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
264 else
266 *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
267 return VLC_EGENERIC;
270 *pp_status = vlm_MessageSimpleNew( "del" );
271 return VLC_SUCCESS;
274 static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
276 vlm_media_sys_t *p_media;
277 vlm_schedule_sys_t *p_schedule;
279 if( !psz_name )
281 *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
282 return VLC_SUCCESS;
285 p_media = vlm_MediaSearch( p_vlm, psz_name );
286 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
288 if( p_schedule != NULL )
289 *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
290 else if( p_media != NULL )
291 *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
292 else
293 *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );
295 return VLC_SUCCESS;
298 static int ExecuteHelp( vlm_message_t **pp_status )
300 vlm_message_t *message_child;
302 #define MessageAdd( a ) \
303 vlm_MessageAdd( *pp_status, vlm_MessageSimpleNew( a ) );
304 #define MessageAddChild( a ) \
305 vlm_MessageAdd( message_child, vlm_MessageSimpleNew( a ) );
307 *pp_status = vlm_MessageSimpleNew( "help" );
309 message_child = MessageAdd( "Commands Syntax:" );
310 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
311 MessageAddChild( "setup (name) (properties)" );
312 MessageAddChild( "show [(name)|media|schedule]" );
313 MessageAddChild( "del (name)|all|media|schedule" );
314 MessageAddChild( "control (name) [instance_name] (command)" );
315 MessageAddChild( "save (config_file)" );
316 MessageAddChild( "export" );
317 MessageAddChild( "load (config_file)" );
319 message_child = MessageAdd( "Media Proprieties Syntax:" );
320 MessageAddChild( "input (input_name)" );
321 MessageAddChild( "inputdel (input_name)|all" );
322 MessageAddChild( "inputdeln input_number" );
323 MessageAddChild( "output (output_name)" );
324 MessageAddChild( "option (option_name)[=value]" );
325 MessageAddChild( "enabled|disabled" );
326 MessageAddChild( "loop|unloop (broadcast only)" );
327 MessageAddChild( "mux (mux_name)" );
329 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
330 MessageAddChild( "enabled|disabled" );
331 MessageAddChild( "append (command_until_rest_of_the_line)" );
332 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
333 "(seconds)|now" );
334 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
335 "(days)-(hours):(minutes):(seconds)" );
336 MessageAddChild( "repeat (number_of_repetitions)" );
338 message_child = MessageAdd( "Control Commands Syntax:" );
339 MessageAddChild( "play [input_number]" );
340 MessageAddChild( "pause" );
341 MessageAddChild( "stop" );
342 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](milliseconds)ms" );
344 return VLC_SUCCESS;
347 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
349 vlm_media_sys_t *p_media;
350 const char *psz_control = NULL;
351 const char *psz_instance = NULL;
352 const char *psz_argument = NULL;
353 int i_index;
354 int i_result;
356 if( !ExecuteIsMedia( p_vlm, psz_name ) )
358 *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
359 return VLC_EGENERIC;
362 assert( i_arg > 0 );
364 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
365 i_index = 0;
366 if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
368 i_index = 1;
369 psz_instance = ppsz_arg[0];
371 if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
372 return ExecuteSyntaxError( "control", pp_status );
374 #undef IS
375 psz_control = ppsz_arg[i_index];
377 if( i_index+1 < i_arg )
378 psz_argument = ppsz_arg[i_index+1];
380 p_media = vlm_MediaSearch( p_vlm, psz_name );
381 assert( p_media );
383 if( !strcmp( psz_control, "play" ) )
385 int i_input_index = 0;
386 int i;
388 if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
390 i_input_index = i-1;
392 else if( psz_argument )
394 int j;
395 vlm_media_t *p_cfg = &p_media->cfg;
396 for ( j=0; j < p_cfg->i_input; j++)
398 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
400 i_input_index = j;
401 break;
406 if( p_media->cfg.b_vod )
407 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
408 else
409 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
411 else if( !strcmp( psz_control, "seek" ) )
413 if( psz_argument )
415 bool b_relative;
416 if( psz_argument[0] == '+' || psz_argument[0] == '-' )
417 b_relative = true;
418 else
419 b_relative = false;
421 if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
423 /* Time (ms or s) */
424 int64_t i_new_time;
426 if( strstr( psz_argument, "ms" ) )
427 i_new_time = 1000 * (int64_t)atoi( psz_argument );
428 else
429 i_new_time = 1000000 * (int64_t)atoi( psz_argument );
431 if( b_relative )
433 int64_t i_time = 0;
434 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
435 i_new_time += i_time;
437 if( i_new_time < 0 )
438 i_new_time = 0;
439 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
441 else
443 /* Percent */
444 double d_new_position = us_atof( psz_argument ) / 100.0;
446 if( b_relative )
448 double d_position = 0.0;
450 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
451 d_new_position += d_position;
453 if( d_new_position < 0.0 )
454 d_new_position = 0.0;
455 else if( d_new_position > 1.0 )
456 d_new_position = 1.0;
457 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
460 else
462 i_result = VLC_EGENERIC;
465 else if( !strcmp( psz_control, "stop" ) )
467 i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
469 else if( !strcmp( psz_control, "pause" ) )
471 i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
473 else
475 i_result = VLC_EGENERIC;
478 if( i_result )
480 *pp_status = vlm_MessageNew( "control", "unknown error" );
481 return VLC_SUCCESS;
483 *pp_status = vlm_MessageSimpleNew( "control" );
484 return VLC_SUCCESS;
487 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
489 char *psz_export = Save( p_vlm );
491 *pp_status = vlm_MessageNew( "export", "%s", psz_export );
492 free( psz_export );
493 return VLC_SUCCESS;
496 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
498 FILE *f = vlc_fopen( psz_file, "wt" );
499 char *psz_save = NULL;
501 if( !f )
502 goto error;
504 psz_save = Save( p_vlm );
505 if( psz_save == NULL )
506 goto error;
507 if( fputs( psz_save, f ) == EOF )
508 goto error;;
509 if( fclose( f ) )
511 f = NULL;
512 goto error;
515 free( psz_save );
517 *pp_status = vlm_MessageSimpleNew( "save" );
518 return VLC_SUCCESS;
520 error:
521 free( psz_save );
522 if( f )
523 fclose( f );
524 *pp_status = vlm_MessageNew( "save", "Unable to save to file");
525 return VLC_EGENERIC;
528 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_path, vlm_message_t **pp_status )
530 int fd = vlc_open( psz_path, O_RDONLY|O_NONBLOCK );
531 if( fd == -1 )
533 *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
534 return VLC_EGENERIC;
537 struct stat st;
538 char *psz_buffer = NULL;
540 if( fstat( fd, &st ) || !S_ISREG( st.st_mode )
541 || st.st_size >= SSIZE_MAX
542 || ((psz_buffer = malloc( st.st_size + 1 )) == NULL)
543 || read( fd, psz_buffer, st.st_size ) < (ssize_t)st.st_size )
545 free( psz_buffer );
546 close( fd );
548 *pp_status = vlm_MessageNew( "load", "Read file error" );
549 return VLC_EGENERIC;
552 close( fd );
554 psz_buffer[st.st_size] = '\0';
556 if( Load( p_vlm, psz_buffer ) )
558 free( psz_buffer );
560 *pp_status = vlm_MessageNew( "load", "Error while loading file" );
561 return VLC_EGENERIC;
564 free( psz_buffer );
566 *pp_status = vlm_MessageSimpleNew( "load" );
567 return VLC_SUCCESS;
570 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, bool b_new,
571 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
573 const char *psz_cmd = b_new ? "new" : "setup";
574 int i;
576 for( i = 0; i < i_property; i++ )
578 if( !strcmp( ppsz_property[i], "enabled" ) ||
579 !strcmp( ppsz_property[i], "disabled" ) )
581 if ( vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL ) )
582 goto error;
584 else if( !strcmp( ppsz_property[i], "append" ) )
586 char *psz_line;
587 int j;
588 /* Beware: everything behind append is considered as
589 * command line */
591 if( ++i >= i_property )
592 break;
594 psz_line = xstrdup( ppsz_property[i] );
595 for( j = i+1; j < i_property; j++ )
597 psz_line = xrealloc( psz_line,
598 strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
599 strcat( psz_line, " " );
600 strcat( psz_line, ppsz_property[j] );
603 int val = vlm_ScheduleSetup( p_schedule, "append", psz_line );
604 free( psz_line );
605 if( val )
606 goto error;
607 break;
609 else
611 if( i + 1 >= i_property )
613 if( b_new )
614 vlm_ScheduleDelete( p_vlm, p_schedule );
615 return ExecuteSyntaxError( psz_cmd, pp_status );
618 if( vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] ) )
619 goto error;
620 i++;
623 *pp_status = vlm_MessageSimpleNew( psz_cmd );
625 vlc_mutex_lock( &p_vlm->lock_manage );
626 p_vlm->input_state_changed = true;
627 vlc_cond_signal( &p_vlm->wait_manage );
628 vlc_mutex_unlock( &p_vlm->lock_manage );
630 return VLC_SUCCESS;
632 error:
633 *pp_status = vlm_MessageNew( psz_cmd, "Error while setting the property '%s' to the schedule",
634 ppsz_property[i] );
635 return VLC_EGENERIC;
638 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, bool b_new,
639 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
641 const char *psz_cmd = b_new ? "new" : "setup";
642 vlm_media_t *p_cfg = NULL;
643 int i_result;
644 int i;
646 #undef ERROR
647 #undef MISSING
648 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
649 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
650 ERROR( "unknown media" );
652 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
653 for( i = 0; i < i_property; i++ )
655 const char *psz_option = ppsz_property[i];
656 const char *psz_value = i+1 < i_property ? ppsz_property[i+1] : NULL;
658 if( !strcmp( psz_option, "enabled" ) )
660 p_cfg->b_enabled = true;
662 else if( !strcmp( psz_option, "disabled" ) )
664 p_cfg->b_enabled = false;
666 else if( !strcmp( psz_option, "input" ) )
668 MISSING( "input" );
669 TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
670 i++;
672 else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
674 while( p_cfg->i_input > 0 )
675 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
676 i++;
678 else if( !strcmp( psz_option, "inputdel" ) )
680 int j;
682 MISSING( "inputdel" );
684 for( j = 0; j < p_cfg->i_input; j++ )
686 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
688 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
689 break;
692 i++;
694 else if( !strcmp( psz_option, "inputdeln" ) )
696 MISSING( "inputdeln" );
698 int idx = atoi( psz_value );
699 if( idx > 0 && idx <= p_cfg->i_input )
700 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[idx-1] );
701 i++;
703 else if( !strcmp( psz_option, "output" ) )
705 MISSING( "output" );
707 free( p_cfg->psz_output );
708 p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
709 i++;
711 else if( !strcmp( psz_option, "option" ) )
713 MISSING( "option" );
715 TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
716 i++;
718 else if( !strcmp( psz_option, "loop" ) )
720 if( p_cfg->b_vod )
721 ERROR( "invalid loop option for vod" );
722 p_cfg->broadcast.b_loop = true;
724 else if( !strcmp( psz_option, "unloop" ) )
726 if( p_cfg->b_vod )
727 ERROR( "invalid unloop option for vod" );
728 p_cfg->broadcast.b_loop = false;
730 else if( !strcmp( psz_option, "mux" ) )
732 MISSING( "mux" );
733 if( !p_cfg->b_vod )
734 ERROR( "invalid mux option for broadcast" );
736 free( p_cfg->vod.psz_mux );
737 p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
738 i++;
740 else
742 fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
743 ERROR( "Wrong command syntax" );
746 #undef MISSING
747 #undef ERROR
749 /* */
750 i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
751 vlm_media_Delete( p_cfg );
753 *pp_status = vlm_MessageSimpleNew( psz_cmd );
754 return i_result;
756 error:
757 if( p_cfg )
759 if( b_new )
760 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
761 vlm_media_Delete( p_cfg );
763 return VLC_EGENERIC;
766 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 )
768 /* Check name */
769 if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
771 *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
772 return VLC_EGENERIC;
774 if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
776 *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
777 return VLC_EGENERIC;
779 /* */
780 if( !strcmp( psz_type, "schedule" ) )
782 vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
783 if( !p_schedule )
785 *pp_status = vlm_MessageNew( "new", "could not create schedule" );
786 return VLC_EGENERIC;
788 return ExecuteScheduleProperty( p_vlm, p_schedule, true, i_property, ppsz_property, pp_status );
790 else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
792 vlm_media_t cfg;
793 int64_t id;
795 vlm_media_Init( &cfg );
796 cfg.psz_name = strdup( psz_name );
797 cfg.b_vod = !strcmp( psz_type, "vod" );
799 if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
801 vlm_media_Clean( &cfg );
802 *pp_status = vlm_MessageNew( "new", "could not create media" );
803 return VLC_EGENERIC;
805 vlm_media_Clean( &cfg );
806 return ExecuteMediaProperty( p_vlm, id, true, i_property, ppsz_property, pp_status );
808 else
810 *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
811 return VLC_EGENERIC;
815 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
817 if( ExecuteIsSchedule( p_vlm, psz_name ) )
819 vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
820 return ExecuteScheduleProperty( p_vlm, p_schedule, false, i_property, ppsz_property, pp_status );
822 else if( ExecuteIsMedia( p_vlm, psz_name ) )
824 int64_t id;
825 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
826 goto error;
827 return ExecuteMediaProperty( p_vlm, id, false, i_property, ppsz_property, pp_status );
830 error:
831 *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
832 return VLC_EGENERIC;
835 int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
836 vlm_message_t **pp_message )
838 size_t i_command = 0;
839 size_t i_command_len = strlen( psz_command );
840 char *buf = malloc( i_command_len + 1 ), *psz_buf = buf;
841 size_t i_ppsz_command_len = (3 + (i_command_len + 1) / 2);
842 char **ppsz_command = malloc( i_ppsz_command_len * sizeof(char *) );
843 vlm_message_t *p_message = NULL;
844 int i_ret = 0;
846 if( !psz_buf || !ppsz_command )
848 p_message = vlm_MessageNew( "Memory error",
849 "allocation failed for command of length %zu",
850 i_command_len );
851 goto error;
854 /* First, parse the line and cut it */
855 while( *psz_command != '\0' )
857 const char *psz_temp;
859 if(isspace ((unsigned char)*psz_command))
861 psz_command++;
862 continue;
865 /* support for comments */
866 if( i_command == 0 && *psz_command == '#')
868 p_message = vlm_MessageSimpleNew( "" );
869 goto success;
872 psz_temp = FindCommandEnd( psz_command );
874 if( psz_temp == NULL )
876 p_message = vlm_MessageNew( "Incomplete command", "%s", psz_command );
877 goto error;
880 assert (i_command < i_ppsz_command_len);
882 ppsz_command[i_command] = psz_buf;
883 memcpy (psz_buf, psz_command, psz_temp - psz_command);
884 psz_buf[psz_temp - psz_command] = '\0';
886 Unescape (psz_buf, psz_buf);
888 i_command++;
889 psz_buf += psz_temp - psz_command + 1;
890 psz_command = psz_temp;
892 assert (buf + i_command_len + 1 >= psz_buf);
896 * And then Interpret it
899 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error; if( (cmd) ) goto error; goto success; }
900 if( i_command == 0 )
902 p_message = vlm_MessageSimpleNew( "" );
903 goto success;
905 else IF_EXECUTE( "del", (i_command != 2), ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
906 else IF_EXECUTE( "show", (i_command > 2), ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
907 else IF_EXECUTE( "help", (i_command != 1), ExecuteHelp( &p_message ) )
908 else IF_EXECUTE( "control", (i_command < 3), ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
909 else IF_EXECUTE( "save", (i_command != 2), ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
910 else IF_EXECUTE( "export", (i_command != 1), ExecuteExport(p_vlm, &p_message) )
911 else IF_EXECUTE( "load", (i_command != 2), ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
912 else IF_EXECUTE( "new", (i_command < 3), ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
913 else IF_EXECUTE( "setup", (i_command < 2), ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
914 else
916 p_message = vlm_MessageNew( ppsz_command[0], "Unknown VLM command" );
917 goto error;
919 #undef IF_EXECUTE
921 success:
922 *pp_message = p_message;
923 free( buf );
924 free( ppsz_command );
925 return VLC_SUCCESS;
927 syntax_error:
928 i_ret = ExecuteSyntaxError( ppsz_command[0], pp_message );
929 free( buf );
930 free( ppsz_command );
931 return i_ret;
933 error:
934 *pp_message = p_message;
935 free( buf );
936 free( ppsz_command );
937 return VLC_EGENERIC;
940 /*****************************************************************************
941 * Media handling
942 *****************************************************************************/
943 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
945 int i;
947 for( i = 0; i < vlm->i_media; i++ )
949 if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
950 return vlm->media[i];
953 return NULL;
956 /*****************************************************************************
957 * Schedule handling
958 *****************************************************************************/
959 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
961 if( !psz_name )
962 return NULL;
964 vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
965 if( !p_sched )
966 return NULL;
968 p_sched->psz_name = strdup( psz_name );
969 p_sched->b_enabled = false;
970 p_sched->i_command = 0;
971 p_sched->command = NULL;
972 p_sched->i_date = 0;
973 p_sched->i_period = 0;
974 p_sched->i_repeat = -1;
976 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
978 return p_sched;
981 /* for now, simple delete. After, del with options (last arg) */
982 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched )
984 int i;
985 if( sched == NULL ) return;
987 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
989 if( vlm->i_schedule == 0 ) free( vlm->schedule );
990 free( sched->psz_name );
992 for ( i = 0; i < sched->i_command; i++ )
993 free( sched->command[i] );
994 free( sched->command );
995 free( sched );
998 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1000 int i;
1002 for( i = 0; i < vlm->i_schedule; i++ )
1004 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1006 return vlm->schedule[i];
1010 return NULL;
1013 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1014 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1015 const char *psz_value )
1017 if( !strcmp( psz_cmd, "enabled" ) )
1019 schedule->b_enabled = true;
1021 else if( !strcmp( psz_cmd, "disabled" ) )
1023 schedule->b_enabled = false;
1025 else if( !strcmp( psz_cmd, "date" ) )
1027 struct tm time;
1028 const char *p;
1029 time_t date;
1031 time.tm_sec = 0; /* seconds */
1032 time.tm_min = 0; /* minutes */
1033 time.tm_hour = 0; /* hours */
1034 time.tm_mday = 0; /* day of the month */
1035 time.tm_mon = 0; /* month */
1036 time.tm_year = 0; /* year */
1037 time.tm_wday = 0; /* day of the week */
1038 time.tm_yday = 0; /* day in the year */
1039 time.tm_isdst = -1; /* daylight saving time */
1041 /* date should be year/month/day-hour:minutes:seconds */
1042 p = strchr( psz_value, '-' );
1044 if( !strcmp( psz_value, "now" ) )
1046 schedule->i_date = 0;
1048 else if(p == NULL)
1050 return 1;
1052 else
1054 unsigned i,j,k;
1056 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1058 case 1:
1059 time.tm_sec = i;
1060 break;
1061 case 2:
1062 time.tm_min = i;
1063 time.tm_sec = j;
1064 break;
1065 case 3:
1066 time.tm_hour = i;
1067 time.tm_min = j;
1068 time.tm_sec = k;
1069 break;
1070 default:
1071 return 1;
1074 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1076 case 1:
1077 time.tm_mday = i;
1078 break;
1079 case 2:
1080 time.tm_mon = i - 1;
1081 time.tm_mday = j;
1082 break;
1083 case 3:
1084 time.tm_year = i - 1900;
1085 time.tm_mon = j - 1;
1086 time.tm_mday = k;
1087 break;
1088 default:
1089 return 1;
1092 date = mktime( &time );
1093 schedule->i_date = ((mtime_t) date) * 1000000;
1096 else if( !strcmp( psz_cmd, "period" ) )
1098 struct tm time;
1099 const char *p;
1100 const char *psz_time = NULL, *psz_date = NULL;
1101 time_t date;
1102 unsigned i,j,k;
1104 /* First, if date or period are modified, repeat should be equal to -1 */
1105 schedule->i_repeat = -1;
1107 time.tm_sec = 0; /* seconds */
1108 time.tm_min = 0; /* minutes */
1109 time.tm_hour = 0; /* hours */
1110 time.tm_mday = 0; /* day of the month */
1111 time.tm_mon = 0; /* month */
1112 time.tm_year = 0; /* year */
1113 time.tm_wday = 0; /* day of the week */
1114 time.tm_yday = 0; /* day in the year */
1115 time.tm_isdst = -1; /* daylight saving time */
1117 /* date should be year/month/day-hour:minutes:seconds */
1118 p = strchr( psz_value, '-' );
1119 if( p )
1121 psz_date = psz_value;
1122 psz_time = p + 1;
1124 else
1126 psz_time = psz_value;
1129 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1131 case 1:
1132 time.tm_sec = i;
1133 break;
1134 case 2:
1135 time.tm_min = i;
1136 time.tm_sec = j;
1137 break;
1138 case 3:
1139 time.tm_hour = i;
1140 time.tm_min = j;
1141 time.tm_sec = k;
1142 break;
1143 default:
1144 return 1;
1146 if( psz_date )
1148 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1150 case 1:
1151 time.tm_mday = i;
1152 break;
1153 case 2:
1154 time.tm_mon = i;
1155 time.tm_mday = j;
1156 break;
1157 case 3:
1158 time.tm_year = i;
1159 time.tm_mon = j;
1160 time.tm_mday = k;
1161 break;
1162 default:
1163 return 1;
1167 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1168 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1169 schedule->i_period = ((mtime_t) date) * 1000000;
1171 else if( !strcmp( psz_cmd, "repeat" ) )
1173 int i;
1175 if( sscanf( psz_value, "%d", &i ) == 1 )
1177 schedule->i_repeat = i;
1179 else
1181 return 1;
1184 else if( !strcmp( psz_cmd, "append" ) )
1186 char *command = strdup( psz_value );
1188 TAB_APPEND( schedule->i_command, schedule->command, command );
1190 else
1192 return 1;
1195 return 0;
1198 /*****************************************************************************
1199 * Message handling functions
1200 *****************************************************************************/
1201 vlm_message_t *vlm_MessageSimpleNew( const char *psz_name )
1203 if( !psz_name ) return NULL;
1205 vlm_message_t *p_message = malloc( sizeof(*p_message) );
1206 if( !p_message )
1207 return NULL;
1209 p_message->psz_name = strdup( psz_name );
1210 if( !p_message->psz_name )
1212 free( p_message );
1213 return NULL;
1215 p_message->psz_value = NULL;
1216 p_message->i_child = 0;
1217 p_message->child = NULL;
1219 return p_message;
1222 vlm_message_t *vlm_MessageNew( const char *psz_name,
1223 const char *psz_format, ... )
1225 vlm_message_t *p_message = vlm_MessageSimpleNew( psz_name );
1226 va_list args;
1228 if( !p_message )
1229 return NULL;
1231 assert( psz_format );
1232 va_start( args, psz_format );
1233 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1234 p_message->psz_value = NULL;
1235 va_end( args );
1237 if( !p_message->psz_value )
1239 vlm_MessageDelete( p_message );
1240 return NULL;
1242 return p_message;
1245 void vlm_MessageDelete( vlm_message_t *p_message )
1247 free( p_message->psz_name );
1248 free( p_message->psz_value );
1249 while( p_message->i_child-- )
1250 vlm_MessageDelete( p_message->child[p_message->i_child] );
1251 free( p_message->child );
1252 free( p_message );
1255 /* Add a child */
1256 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1257 vlm_message_t *p_child )
1259 if( p_message == NULL ) return NULL;
1261 if( p_child )
1263 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1266 return p_child;
1269 /*****************************************************************************
1270 * Misc utility functions
1271 *****************************************************************************/
1272 static vlm_message_t *vlm_ShowMedia( vlm_media_sys_t *p_media )
1274 vlm_media_t *p_cfg = &p_media->cfg;
1275 vlm_message_t *p_msg;
1276 vlm_message_t *p_msg_sub;
1277 int i;
1279 p_msg = vlm_MessageSimpleNew( p_cfg->psz_name );
1280 vlm_MessageAdd( p_msg,
1281 vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1282 vlm_MessageAdd( p_msg,
1283 vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1285 if( p_cfg->b_vod )
1286 vlm_MessageAdd( p_msg,
1287 vlm_MessageNew( "mux", "%s", p_cfg->vod.psz_mux ) );
1288 else
1289 vlm_MessageAdd( p_msg,
1290 vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1292 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "inputs" ) );
1293 for( i = 0; i < p_cfg->i_input; i++ )
1295 char *psz_tmp;
1296 if( asprintf( &psz_tmp, "%d", i+1 ) != -1 )
1298 vlm_MessageAdd( p_msg_sub,
1299 vlm_MessageNew( psz_tmp, "%s", p_cfg->ppsz_input[i] ) );
1300 free( psz_tmp );
1304 vlm_MessageAdd( p_msg,
1305 vlm_MessageNew( "output", "%s", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1307 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "options" ) );
1308 for( i = 0; i < p_cfg->i_option; i++ )
1309 vlm_MessageAdd( p_msg_sub, vlm_MessageSimpleNew( p_cfg->ppsz_option[i] ) );
1311 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "instances" ) );
1312 for( i = 0; i < p_media->i_instance; i++ )
1314 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1315 vlc_value_t val;
1316 vlm_message_t *p_msg_instance;
1318 val.i_int = END_S;
1319 if( p_instance->p_input )
1320 var_Get( p_instance->p_input, "state", &val );
1322 p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageSimpleNew( "instance" ) );
1324 vlm_MessageAdd( p_msg_instance,
1325 vlm_MessageNew( "name" , "%s", p_instance->psz_name ? p_instance->psz_name : "default" ) );
1326 vlm_MessageAdd( p_msg_instance,
1327 vlm_MessageNew( "state",
1328 val.i_int == PLAYING_S ? "playing" :
1329 val.i_int == PAUSE_S ? "paused" :
1330 "stopped" ) );
1332 /* FIXME should not do that this way */
1333 if( p_instance->p_input )
1335 #define APPEND_INPUT_INFO( key, format, type ) \
1336 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( key, format, \
1337 var_Get ## type( p_instance->p_input, key ) ) )
1338 APPEND_INPUT_INFO( "position", "%f", Float );
1339 APPEND_INPUT_INFO( "time", "%"PRId64, Integer );
1340 APPEND_INPUT_INFO( "length", "%"PRId64, Integer );
1341 APPEND_INPUT_INFO( "rate", "%f", Float );
1342 APPEND_INPUT_INFO( "title", "%"PRId64, Integer );
1343 APPEND_INPUT_INFO( "chapter", "%"PRId64, Integer );
1344 APPEND_INPUT_INFO( "can-seek", "%d", Bool );
1346 #undef APPEND_INPUT_INFO
1347 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex",
1348 "%d", p_instance->i_index + 1 ) );
1350 return p_msg;
1353 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1354 vlm_schedule_sys_t *schedule,
1355 const char *psz_filter )
1357 if( media != NULL )
1359 vlm_message_t *p_msg = vlm_MessageSimpleNew( "show" );
1360 if( p_msg )
1361 vlm_MessageAdd( p_msg, vlm_ShowMedia( media ) );
1362 return p_msg;
1365 else if( schedule != NULL )
1367 int i;
1368 vlm_message_t *msg;
1369 vlm_message_t *msg_schedule;
1370 vlm_message_t *msg_child;
1371 char buffer[100];
1373 msg = vlm_MessageSimpleNew( "show" );
1374 msg_schedule =
1375 vlm_MessageAdd( msg, vlm_MessageSimpleNew( schedule->psz_name ) );
1377 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1379 vlm_MessageAdd( msg_schedule,
1380 vlm_MessageNew( "enabled", schedule->b_enabled ?
1381 "yes" : "no" ) );
1383 if( schedule->i_date != 0 )
1385 struct tm date;
1386 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1388 localtime_r( &i_time, &date);
1389 vlm_MessageAdd( msg_schedule,
1390 vlm_MessageNew( "date", "%d/%d/%d-%d:%d:%d",
1391 date.tm_year + 1900, date.tm_mon + 1,
1392 date.tm_mday, date.tm_hour, date.tm_min,
1393 date.tm_sec ) );
1395 else
1396 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1398 if( schedule->i_period != 0 )
1400 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1401 struct tm date;
1403 date.tm_sec = (int)( i_time % 60 );
1404 i_time = i_time / 60;
1405 date.tm_min = (int)( i_time % 60 );
1406 i_time = i_time / 60;
1407 date.tm_hour = (int)( i_time % 24 );
1408 i_time = i_time / 24;
1409 date.tm_mday = (int)( i_time % 30 );
1410 i_time = i_time / 30;
1411 /* okay, okay, months are not always 30 days long */
1412 date.tm_mon = (int)( i_time % 12 );
1413 i_time = i_time / 12;
1414 date.tm_year = (int)i_time;
1416 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1417 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1419 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "%s", buffer) );
1421 else
1422 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1424 sprintf( buffer, "%d", schedule->i_repeat );
1425 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", "%s", buffer ) );
1427 msg_child =
1428 vlm_MessageAdd( msg_schedule, vlm_MessageSimpleNew("commands" ) );
1430 for( i = 0; i < schedule->i_command; i++ )
1432 vlm_MessageAdd( msg_child,
1433 vlm_MessageSimpleNew( schedule->command[i] ) );
1436 return msg;
1440 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1442 vlm_message_t *p_msg;
1443 vlm_message_t *p_msg_child;
1444 int i_vod = 0, i_broadcast = 0;
1446 for( int i = 0; i < vlm->i_media; i++ )
1448 if( vlm->media[i]->cfg.b_vod )
1449 i_vod++;
1450 else
1451 i_broadcast++;
1454 p_msg = vlm_MessageSimpleNew( "show" );
1455 p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media",
1456 "( %d broadcast - %d vod )", i_broadcast,
1457 i_vod ) );
1459 for( int i = 0; i < vlm->i_media; i++ )
1460 vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm->media[i] ) );
1462 return p_msg;
1465 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1467 int i;
1468 vlm_message_t *msg;
1469 vlm_message_t *msg_child;
1471 msg = vlm_MessageSimpleNew( "show" );
1472 msg_child = vlm_MessageAdd( msg, vlm_MessageSimpleNew( "schedule" ) );
1474 for( i = 0; i < vlm->i_schedule; i++ )
1476 vlm_schedule_sys_t *s = vlm->schedule[i];
1477 vlm_message_t *msg_schedule;
1478 mtime_t i_time, i_next_date;
1480 msg_schedule = vlm_MessageAdd( msg_child,
1481 vlm_MessageSimpleNew( s->psz_name ) );
1482 vlm_MessageAdd( msg_schedule,
1483 vlm_MessageNew( "enabled", s->b_enabled ?
1484 "yes" : "no" ) );
1486 /* calculate next date */
1487 i_time = vlm_Date();
1488 i_next_date = s->i_date;
1490 if( s->i_period != 0 )
1492 int j = 0;
1493 while( s->i_date + j * s->i_period <= i_time &&
1494 s->i_repeat > j )
1496 j++;
1499 i_next_date = s->i_date + j * s->i_period;
1502 if( i_next_date > i_time )
1504 time_t i_date = (time_t) (i_next_date / 1000000) ;
1505 struct tm tm;
1506 char psz_date[32];
1508 strftime( psz_date, sizeof(psz_date), "%Y-%m-%d %H:%M:%S (%a)",
1509 localtime_r( &i_date, &tm ) );
1510 vlm_MessageAdd( msg_schedule,
1511 vlm_MessageNew( "next launch", "%s", psz_date ) );
1515 return msg;
1518 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1520 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1521 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1523 vlm_MessageAdd( show1, show2->child[0] );
1525 /* We must destroy the parent node "show" of show2
1526 * and not the children */
1527 free( show2->psz_name );
1528 free( show2 );
1530 return show1;
1533 else
1535 return vlm_MessageSimpleNew( "show" );
1539 /*****************************************************************************
1540 * Config handling functions
1541 *****************************************************************************/
1542 static int Load( vlm_t *vlm, char *file )
1544 char *pf = file;
1545 int i_line = 1;
1547 while( *pf != '\0' )
1549 vlm_message_t *message = NULL;
1550 int i_end = 0;
1552 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1554 i_end++;
1557 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1559 pf[i_end] = '\0';
1560 i_end++;
1561 if( pf[i_end] == '\n' ) i_end++;
1564 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1566 if( message )
1568 if( message->psz_value )
1569 msg_Err( vlm, "Load error on line %d: %s: %s",
1570 i_line, message->psz_name, message->psz_value );
1571 vlm_MessageDelete( message );
1573 return 1;
1575 if( message ) vlm_MessageDelete( message );
1577 pf += i_end;
1578 i_line++;
1581 return 0;
1584 static char *Save( vlm_t *vlm )
1586 char *save = NULL;
1587 char psz_header[] = "\n"
1588 "# VLC media player VLM command batch\n"
1589 "# http://www.videolan.org/vlc/\n\n" ;
1590 char *p;
1591 int i,j;
1592 int i_length = strlen( psz_header );
1594 for( i = 0; i < vlm->i_media; i++ )
1596 vlm_media_sys_t *media = vlm->media[i];
1597 vlm_media_t *p_cfg = &media->cfg;
1599 if( p_cfg->b_vod )
1600 i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1601 else
1602 i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1604 if( p_cfg->b_enabled )
1605 i_length += strlen( "enabled" );
1606 else
1607 i_length += strlen( "disabled" );
1609 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1610 i_length += strlen( " loop\n" );
1611 else
1612 i_length += strlen( "\n" );
1614 for( j = 0; j < p_cfg->i_input; j++ )
1615 i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1617 if( p_cfg->psz_output != NULL )
1618 i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1620 for( j = 0; j < p_cfg->i_option; j++ )
1621 i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1623 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1624 i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1627 for( i = 0; i < vlm->i_schedule; i++ )
1629 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1631 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
1633 if( schedule->b_enabled )
1635 i_length += strlen( "date //-:: enabled\n" ) + 14;
1637 else
1639 i_length += strlen( "date //-:: disabled\n" ) + 14;
1643 if( schedule->i_period != 0 )
1645 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
1646 strlen( "period //-::\n" ) + 14;
1649 if( schedule->i_repeat >= 0 )
1651 char buffer[12];
1653 sprintf( buffer, "%d", schedule->i_repeat );
1654 i_length += strlen( "setup repeat \n" ) +
1655 strlen( schedule->psz_name ) + strlen( buffer );
1657 else
1659 i_length++;
1662 for( j = 0; j < schedule->i_command; j++ )
1664 i_length += strlen( "setup append \n" ) +
1665 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1670 /* Don't forget the '\0' */
1671 i_length++;
1672 /* now we have the length of save */
1674 p = save = malloc( i_length );
1675 if( !save ) return NULL;
1676 *save = '\0';
1678 p += sprintf( p, "%s", psz_header );
1680 /* finally we can write in it */
1681 for( i = 0; i < vlm->i_media; i++ )
1683 vlm_media_sys_t *media = vlm->media[i];
1684 vlm_media_t *p_cfg = &media->cfg;
1686 if( p_cfg->b_vod )
1687 p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1688 else
1689 p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1691 if( p_cfg->b_enabled )
1692 p += sprintf( p, "enabled" );
1693 else
1694 p += sprintf( p, "disabled" );
1696 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1697 p += sprintf( p, " loop\n" );
1698 else
1699 p += sprintf( p, "\n" );
1701 for( j = 0; j < p_cfg->i_input; j++ )
1702 p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1704 if( p_cfg->psz_output )
1705 p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1707 for( j = 0; j < p_cfg->i_option; j++ )
1708 p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1710 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1711 p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1714 /* and now, the schedule scripts */
1715 for( i = 0; i < vlm->i_schedule; i++ )
1717 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1718 struct tm date;
1719 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1721 localtime_r( &i_time, &date);
1722 p += sprintf( p, "new %s schedule ", schedule->psz_name);
1724 if( schedule->b_enabled )
1726 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1727 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1728 date.tm_hour, date.tm_min, date.tm_sec );
1730 else
1732 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1733 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1734 date.tm_hour, date.tm_min, date.tm_sec);
1737 if( schedule->i_period != 0 )
1739 p += sprintf( p, "setup %s ", schedule->psz_name );
1741 i_time = (time_t) ( schedule->i_period / 1000000 );
1743 date.tm_sec = (int)( i_time % 60 );
1744 i_time = i_time / 60;
1745 date.tm_min = (int)( i_time % 60 );
1746 i_time = i_time / 60;
1747 date.tm_hour = (int)( i_time % 24 );
1748 i_time = i_time / 24;
1749 date.tm_mday = (int)( i_time % 30 );
1750 i_time = i_time / 30;
1751 /* okay, okay, months are not always 30 days long */
1752 date.tm_mon = (int)( i_time % 12 );
1753 i_time = i_time / 12;
1754 date.tm_year = (int)i_time;
1756 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1757 date.tm_year, date.tm_mon, date.tm_mday,
1758 date.tm_hour, date.tm_min, date.tm_sec);
1761 if( schedule->i_repeat >= 0 )
1763 p += sprintf( p, "setup %s repeat %d\n",
1764 schedule->psz_name, schedule->i_repeat );
1766 else
1768 p += sprintf( p, "\n" );
1771 for( j = 0; j < schedule->i_command; j++ )
1773 p += sprintf( p, "setup %s append %s\n",
1774 schedule->psz_name, schedule->command[j] );
1779 return save;
1782 #endif /* ENABLE_VLM */