]> git.sesse.net Git - vlc/blob - src/input/vlmshell.c
vlm: remove dead code (AFAIK a relative seek does exactly the same thing).
[vlc] / src / input / vlmshell.c
1 /*****************************************************************************
2  * vlm.c: VLM interface plugin
3  *****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Simon Latapie <garf@videolan.org>
8  *          Laurent Aimar <fenrir@videolan.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34
35 #include <stdio.h>
36 #include <ctype.h>                                              /* tolower() */
37 #include <assert.h>
38
39 #include <vlc_vlm.h>
40
41 #ifdef ENABLE_VLM
42
43 #include <time.h>                                                 /* ctime() */
44
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 "../stream_output/stream_output.h"
53 #include "../libvlc.h"
54
55 /*****************************************************************************
56  * Local prototypes.
57  *****************************************************************************/
58
59 /* */
60 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_sys_t *, vlm_schedule_sys_t *, const char * );
61
62 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *, const char * );
63
64 static char *Save( vlm_t * );
65 static int Load( vlm_t *, char * );
66
67 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
68 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
69                               const char *psz_value );
70
71 /* */
72 static vlm_media_sys_t *vlm_MediaSearch( vlm_t *, const char *);
73
74 static const char quotes[] = "\"'";
75 /**
76  * FindCommandEnd: look for the end of a possibly quoted string
77  * @return NULL on mal-formatted string,
78  * pointer past the last character otherwise.
79  */
80 static const char *FindCommandEnd( const char *psz_sent )
81 {
82     char c, quote = 0;
83
84     while( (c = *psz_sent) != '\0' )
85     {
86         if( !quote )
87         {
88             if( strchr(quotes,c) )   // opening quote
89                 quote = c;
90             else if( isspace(c) )         // non-escaped space
91                 return psz_sent;
92             else if( c == '\\' )
93             {
94                 psz_sent++;         // skip escaped character
95                 if( *psz_sent == '\0' )
96                     return psz_sent;
97             }
98         }
99         else
100         {
101             if( c == quote )         // non-escaped matching quote
102                 quote = 0;
103             else if( (quote == '"') && (c == '\\') )
104             {
105                 psz_sent++;         // skip escaped character
106                 if (*psz_sent == '\0')
107                     return NULL;    // error, closing quote missing
108             }
109         }
110         psz_sent++;
111     }
112
113     // error (NULL) if we could not find a matching quote
114     return quote ? NULL : psz_sent;
115 }
116
117
118 /**
119  * Unescape a nul-terminated string.
120  * Note that in and out can be identical.
121  *
122  * @param out output buffer (at least <strlen (in) + 1> characters long)
123  * @param in nul-terminated string to be unescaped
124  *
125  * @return 0 on success, -1 on error.
126  */
127 static int Unescape( char *out, const char *in )
128 {
129     char c, quote = 0;
130     bool param = false;
131
132     while( (c = *in++) != '\0' )
133     {
134         // Don't escape the end of the string if we find a '#'
135         // that's the begining of a vlc command
136         // TODO: find a better solution
137         if( ( c == '#' && !quote ) || param )
138         {
139             param = true;
140             *out++ = c;
141             continue;
142         }
143
144         if( !quote )
145         {
146             if (strchr(quotes,c))   // opening quote
147             {
148                 quote = c;
149                 continue;
150             }
151             else if( c == '\\' )
152             {
153                 switch (c = *in++)
154                 {
155                     case '"':
156                     case '\'':
157                     case '\\':
158                         *out++ = c;
159                         continue;
160
161                     case '\0':
162                         *out = '\0';
163                         return 0;
164                 }
165                 if( isspace(c) )
166                 {
167                     *out++ = c;
168                     continue;
169                 }
170                 /* None of the special cases - copy the backslash */
171                 *out++ = '\\';
172             }
173         }
174         else
175         {
176             if( c == quote )         // non-escaped matching quote
177             {
178                 quote = 0;
179                 continue;
180             }
181             if( (quote == '"') && (c == '\\') )
182             {
183                 switch( c = *in++ )
184                 {
185                     case '"':
186                     case '\\':
187                         *out++ = c;
188                         continue;
189
190                     case '\0':   // should never happen
191                         *out = '\0';
192                         return -1;
193                 }
194                 /* None of the special cases - copy the backslash */
195                 *out++ = '\\';
196             }
197         }
198         *out++ = c;
199     }
200
201     *out = '\0';
202     return 0;
203 }
204
205
206 /*****************************************************************************
207  * ExecuteCommand: The main state machine
208  *****************************************************************************
209  * Execute a command which ends with '\0' (string)
210  *****************************************************************************/
211 static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
212 {
213     *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
214     return VLC_EGENERIC;
215 }
216
217 static bool ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
218 {
219     int64_t id;
220
221     if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
222         return false;
223     return true;
224 }
225 static bool ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
226 {
227     if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
228         return false;
229     return true;
230 }
231
232 static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
233 {
234     vlm_media_sys_t *p_media;
235     vlm_schedule_sys_t *p_schedule;
236
237     p_media = vlm_MediaSearch( p_vlm, psz_name );
238     p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
239
240     if( p_schedule != NULL )
241     {
242         vlm_ScheduleDelete( p_vlm, p_schedule );
243     }
244     else if( p_media != NULL )
245     {
246         vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
247     }
248     else if( !strcmp(psz_name, "media") )
249     {
250         vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
251     }
252     else if( !strcmp(psz_name, "schedule") )
253     {
254         vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
255     }
256     else if( !strcmp(psz_name, "all") )
257     {
258         vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
259         vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
260     }
261     else
262     {
263         *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
264         return VLC_EGENERIC;
265     }
266
267     *pp_status = vlm_MessageSimpleNew( "del" );
268     return VLC_SUCCESS;
269 }
270
271 static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
272 {
273     vlm_media_sys_t *p_media;
274     vlm_schedule_sys_t *p_schedule;
275
276     if( !psz_name )
277     {
278         *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
279         return VLC_SUCCESS;
280     }
281
282     p_media = vlm_MediaSearch( p_vlm, psz_name );
283     p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
284
285     if( p_schedule != NULL )
286         *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
287     else if( p_media != NULL )
288         *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
289     else
290         *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );
291
292     return VLC_SUCCESS;
293 }
294
295 static int ExecuteHelp( vlm_message_t **pp_status )
296 {
297     vlm_message_t *message_child;
298
299 #define MessageAdd( a ) \
300         vlm_MessageAdd( *pp_status, vlm_MessageSimpleNew( a ) );
301 #define MessageAddChild( a ) \
302         vlm_MessageAdd( message_child, vlm_MessageSimpleNew( a ) );
303
304     *pp_status = vlm_MessageSimpleNew( "help" );
305
306     message_child = MessageAdd( "Commands Syntax:" );
307     MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
308     MessageAddChild( "setup (name) (properties)" );
309     MessageAddChild( "show [(name)|media|schedule]" );
310     MessageAddChild( "del (name)|all|media|schedule" );
311     MessageAddChild( "control (name) [instance_name] (command)" );
312     MessageAddChild( "save (config_file)" );
313     MessageAddChild( "export" );
314     MessageAddChild( "load (config_file)" );
315
316     message_child = MessageAdd( "Media Proprieties Syntax:" );
317     MessageAddChild( "input (input_name)" );
318     MessageAddChild( "inputdel (input_name)|all" );
319     MessageAddChild( "inputdeln input_number" );
320     MessageAddChild( "output (output_name)" );
321     MessageAddChild( "option (option_name)[=value]" );
322     MessageAddChild( "enabled|disabled" );
323     MessageAddChild( "loop|unloop (broadcast only)" );
324     MessageAddChild( "mux (mux_name)" );
325
326     message_child = MessageAdd( "Schedule Proprieties Syntax:" );
327     MessageAddChild( "enabled|disabled" );
328     MessageAddChild( "append (command_until_rest_of_the_line)" );
329     MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
330                      "(seconds)|now" );
331     MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
332                      "(days)-(hours):(minutes):(seconds)" );
333     MessageAddChild( "repeat (number_of_repetitions)" );
334
335     message_child = MessageAdd( "Control Commands Syntax:" );
336     MessageAddChild( "play [input_number]" );
337     MessageAddChild( "pause" );
338     MessageAddChild( "stop" );
339     MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](milliseconds)ms" );
340
341     return VLC_SUCCESS;
342 }
343
344 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
345 {
346     vlm_media_sys_t *p_media;
347     const char *psz_control = NULL;
348     const char *psz_instance = NULL;
349     const char *psz_argument = NULL;
350     int i_index;
351     int i_result;
352
353     if( !ExecuteIsMedia( p_vlm, psz_name ) )
354     {
355         *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
356         return VLC_EGENERIC;
357     }
358
359     assert( i_arg > 0 );
360
361 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
362     i_index = 0;
363     if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
364     {
365         i_index = 1;
366         psz_instance = ppsz_arg[0];
367
368         if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
369             return ExecuteSyntaxError( "control", pp_status );
370     }
371 #undef IS
372     psz_control = ppsz_arg[i_index];
373
374     if( i_index+1 < i_arg )
375         psz_argument = ppsz_arg[i_index+1];
376
377     p_media = vlm_MediaSearch( p_vlm, psz_name );
378     assert( p_media );
379
380     if( !strcmp( psz_control, "play" ) )
381     {
382         int i_input_index = 0;
383         int i;
384
385         if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
386         {
387             i_input_index = i-1;
388         }
389         else if( psz_argument )
390         {
391             int j;
392             vlm_media_t *p_cfg = &p_media->cfg;
393             for ( j=0; j < p_cfg->i_input; j++)
394             {
395                 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
396                 {
397                     i_input_index = j;
398                     break;
399                 }
400             }
401         }
402
403         if( p_media->cfg.b_vod )
404             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
405         else
406             i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
407     }
408     else if( !strcmp( psz_control, "seek" ) )
409     {
410         if( psz_argument )
411         {
412             bool b_relative;
413             if( psz_argument[0] == '+' || psz_argument[0] == '-' )
414                 b_relative = true;
415             else
416                 b_relative = false;
417
418             if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
419             {
420                 /* Time (ms or s) */
421                 int64_t i_new_time;
422
423                 if( strstr( psz_argument, "ms" ) )
424                     i_new_time =  1000 * (int64_t)atoi( psz_argument );
425                 else
426                     i_new_time = 1000000 * (int64_t)atoi( psz_argument );
427
428                 if( b_relative )
429                 {
430                     int64_t i_time = 0;
431                     vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
432                     i_new_time += i_time;
433                 }
434                 if( i_new_time < 0 )
435                     i_new_time = 0;
436                 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
437             }
438             else
439             {
440                 /* Percent */
441                 double d_new_position = us_atof( psz_argument ) / 100.0;
442
443                 if( b_relative )
444                 {
445                     double d_position = 0.0;
446
447                     vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
448                     d_new_position += d_position;
449                 }
450                 if( d_new_position < 0.0 )
451                     d_new_position = 0.0;
452                 else if( d_new_position > 1.0 )
453                     d_new_position = 1.0;
454                 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
455             }
456         }
457         else
458         {
459             i_result = VLC_EGENERIC;
460         }
461     }
462     else if( !strcmp( psz_control, "stop" ) )
463     {
464         i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
465     }
466     else if( !strcmp( psz_control, "pause" ) )
467     {
468         i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
469     }
470     else
471     {
472         i_result = VLC_EGENERIC;
473     }
474
475     if( i_result )
476     {
477         *pp_status = vlm_MessageNew( "control", "unknown error" );
478         return VLC_SUCCESS;
479     }
480     *pp_status = vlm_MessageSimpleNew( "control" );
481     return VLC_SUCCESS;
482 }
483
484 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
485 {
486     char *psz_export = Save( p_vlm );
487
488     *pp_status = vlm_MessageNew( "export", "%s", psz_export );
489     free( psz_export );
490     return VLC_SUCCESS;
491 }
492
493 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
494 {
495     FILE *f = vlc_fopen( psz_file, "wt" );
496     char *psz_save = NULL;
497
498     if( !f )
499         goto error;
500
501     psz_save = Save( p_vlm );
502     if( psz_save == NULL )
503         goto error;
504     if( fputs( psz_save, f ) == EOF )
505         goto error;;
506     if( fclose( f ) )
507     {
508         f = NULL;
509         goto error;
510     }
511
512     free( psz_save );
513
514     *pp_status = vlm_MessageSimpleNew( "save" );
515     return VLC_SUCCESS;
516
517 error:
518     free( psz_save );
519     if( f )
520          fclose( f );
521     *pp_status = vlm_MessageNew( "save", "Unable to save to file");
522     return VLC_EGENERIC;
523 }
524
525 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_status )
526 {
527     stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
528     uint64_t i_size;
529     char *psz_buffer;
530
531     if( !p_stream )
532     {
533         *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
534         return VLC_EGENERIC;
535     }
536
537     /* FIXME needed ? */
538     if( stream_Seek( p_stream, 0 ) != 0 )
539     {
540         stream_Delete( p_stream );
541
542         *pp_status = vlm_MessageNew( "load", "Read file error" );
543         return VLC_EGENERIC;
544     }
545
546     i_size = stream_Size( p_stream );
547     if( i_size > SIZE_MAX - 1 )
548         i_size = SIZE_MAX - 1;
549
550     psz_buffer = malloc( i_size + 1 );
551     if( !psz_buffer )
552     {
553         stream_Delete( p_stream );
554
555         *pp_status = vlm_MessageNew( "load", "Read file error" );
556         return VLC_EGENERIC;
557     }
558
559     stream_Read( p_stream, psz_buffer, i_size );
560     psz_buffer[i_size] = '\0';
561
562     stream_Delete( p_stream );
563
564     if( Load( p_vlm, psz_buffer ) )
565     {
566         free( psz_buffer );
567
568         *pp_status = vlm_MessageNew( "load", "Error while loading file" );
569         return VLC_EGENERIC;
570     }
571
572     free( psz_buffer );
573
574     *pp_status = vlm_MessageSimpleNew( "load" );
575     return VLC_SUCCESS;
576 }
577
578 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, bool b_new,
579                                     const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
580 {
581     const char *psz_cmd = b_new ? "new" : "setup";
582     int i;
583
584     for( i = 0; i < i_property; i++ )
585     {
586         if( !strcmp( ppsz_property[i], "enabled" ) ||
587             !strcmp( ppsz_property[i], "disabled" ) )
588         {
589             if ( vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL ) )
590                 goto error;
591         }
592         else if( !strcmp( ppsz_property[i], "append" ) )
593         {
594             char *psz_line;
595             int j;
596             /* Beware: everything behind append is considered as
597              * command line */
598
599             if( ++i >= i_property )
600                 break;
601
602             psz_line = strdup( ppsz_property[i] );
603             for( j = i+1; j < i_property; j++ )
604             {
605                 psz_line = xrealloc( psz_line,
606                         strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
607                 strcat( psz_line, " " );
608                 strcat( psz_line, ppsz_property[j] );
609             }
610
611             if( vlm_ScheduleSetup( p_schedule, "append", psz_line ) )
612                 goto error;
613             break;
614         }
615         else
616         {
617             if( i + 1 >= i_property )
618             {
619                 if( b_new )
620                     vlm_ScheduleDelete( p_vlm, p_schedule );
621                 return ExecuteSyntaxError( psz_cmd, pp_status );
622             }
623
624             if( vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] ) )
625                 goto error;
626             i++;
627         }
628     }
629     *pp_status = vlm_MessageSimpleNew( psz_cmd );
630
631     vlc_mutex_lock( &p_vlm->lock_manage );
632     p_vlm->input_state_changed = true;
633     vlc_cond_signal( &p_vlm->wait_manage );
634     vlc_mutex_unlock( &p_vlm->lock_manage );
635
636     return VLC_SUCCESS;
637
638 error:
639     *pp_status = vlm_MessageNew( psz_cmd, "Error while setting the property '%s' to the schedule",
640                                  ppsz_property[i] );
641     return VLC_EGENERIC;
642 }
643
644 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, bool b_new,
645                                  const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
646 {
647     const char *psz_cmd = b_new ? "new" : "setup";
648     vlm_media_t *p_cfg = NULL;
649     int i_result;
650     int i;
651
652 #undef ERROR
653 #undef MISSING
654 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
655     if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
656         ERROR( "unknown media" );
657
658 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
659     for( i = 0; i < i_property; i++ )
660     {
661         const char *psz_option = ppsz_property[i];
662         const char *psz_value = i+1 < i_property ? ppsz_property[i+1] :  NULL;
663
664         if( !strcmp( psz_option, "enabled" ) )
665         {
666             p_cfg->b_enabled = true;
667         }
668         else if( !strcmp( psz_option, "disabled" ) )
669         {
670             p_cfg->b_enabled = false;
671         }
672         else if( !strcmp( psz_option, "input" ) )
673         {
674             MISSING( "input" );
675             TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
676             i++;
677         }
678         else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
679         {
680             while( p_cfg->i_input > 0 )
681                 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
682             i++;
683         }
684         else if( !strcmp( psz_option, "inputdel" ) )
685         {
686             int j;
687
688             MISSING( "inputdel" );
689
690             for( j = 0; j < p_cfg->i_input; j++ )
691             {
692                 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
693                 {
694                     TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
695                     break;
696                 }
697             }
698             i++;
699         }
700         else if( !strcmp( psz_option, "inputdeln" ) )
701         {
702             int i_index;
703
704             MISSING( "inputdeln" );
705  
706             i_index = atoi( psz_value );
707             if( i_index > 0 && i_index <= p_cfg->i_input )
708                 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[i_index-1] );
709             i++;
710         }
711         else if( !strcmp( psz_option, "output" ) )
712         {
713             MISSING( "output" );
714
715             free( p_cfg->psz_output );
716             p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
717             i++;
718         }
719         else if( !strcmp( psz_option, "option" ) )
720         {
721             MISSING( "option" );
722
723             TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
724             i++;
725         }
726         else if( !strcmp( psz_option, "loop" ) )
727         {
728             if( p_cfg->b_vod )
729                 ERROR( "invalid loop option for vod" );
730             p_cfg->broadcast.b_loop = true;
731         }
732         else if( !strcmp( psz_option, "unloop" ) )
733         {
734             if( p_cfg->b_vod )
735                 ERROR( "invalid unloop option for vod" );
736             p_cfg->broadcast.b_loop = false;
737         }
738         else if( !strcmp( psz_option, "mux" ) )
739         {
740             MISSING( "mux" );
741             if( !p_cfg->b_vod )
742                 ERROR( "invalid mux option for broadcast" );
743
744             free( p_cfg->vod.psz_mux );
745             p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
746             i++;
747         }
748         else
749         {
750             fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
751             ERROR( "Wrong command syntax" );
752         }
753     }
754 #undef MISSING
755 #undef ERROR
756
757     /* */
758     i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
759     vlm_media_Delete( p_cfg );
760
761     *pp_status = vlm_MessageSimpleNew( psz_cmd );
762     return i_result;
763
764 error:
765     if( p_cfg )
766     {
767         if( b_new )
768             vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
769         vlm_media_Delete( p_cfg );
770     }
771     return VLC_EGENERIC;
772 }
773
774 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 )
775 {
776     /* Check name */
777     if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
778     {
779         *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
780         return VLC_EGENERIC;
781     }
782     if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
783     {
784         *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
785         return VLC_EGENERIC;
786     }
787     /* */
788     if( !strcmp( psz_type, "schedule" ) )
789     {
790         vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
791         if( !p_schedule )
792         {
793             *pp_status = vlm_MessageNew( "new", "could not create schedule" );
794             return VLC_EGENERIC;
795         }
796         return ExecuteScheduleProperty( p_vlm, p_schedule, true, i_property, ppsz_property, pp_status );
797     }
798     else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
799     {
800         vlm_media_t cfg;
801         int64_t id;
802
803         vlm_media_Init( &cfg );
804         cfg.psz_name = strdup( psz_name );
805         cfg.b_vod = !strcmp( psz_type, "vod" );
806
807         if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
808         {
809             vlm_media_Clean( &cfg );
810             *pp_status = vlm_MessageNew( "new", "could not create media" );
811             return VLC_EGENERIC;
812         }
813         vlm_media_Clean( &cfg );
814         return ExecuteMediaProperty( p_vlm, id, true, i_property, ppsz_property, pp_status );
815     }
816     else
817     {
818         *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
819         return VLC_EGENERIC;
820     }
821 }
822
823 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
824 {
825     if( ExecuteIsSchedule( p_vlm, psz_name ) )
826     {
827         vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
828         return ExecuteScheduleProperty( p_vlm, p_schedule, false, i_property, ppsz_property, pp_status );
829     }
830     else if( ExecuteIsMedia( p_vlm, psz_name ) )
831     {
832         int64_t id;
833         if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
834             goto error;
835         return ExecuteMediaProperty( p_vlm, id, false, i_property, ppsz_property, pp_status );
836     }
837
838 error:
839     *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
840     return VLC_EGENERIC;
841 }
842
843 int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
844                            vlm_message_t **pp_message )
845 {
846     size_t i_command = 0;
847     char buf[strlen (psz_command) + 1], *psz_buf = buf;
848     char *ppsz_command[3+sizeof (buf) / 2];
849     vlm_message_t *p_message = NULL;
850
851     /* First, parse the line and cut it */
852     while( *psz_command != '\0' )
853     {
854         const char *psz_temp;
855
856         if(isspace (*psz_command))
857         {
858             psz_command++;
859             continue;
860         }
861
862         /* support for comments */
863         if( i_command == 0 && *psz_command == '#')
864         {
865             p_message = vlm_MessageSimpleNew( "" );
866             goto success;
867         }
868
869         psz_temp = FindCommandEnd( psz_command );
870
871         if( psz_temp == NULL )
872         {
873             p_message = vlm_MessageNew( "Incomplete command", "%s", psz_command );
874             goto error;
875         }
876
877         assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
878
879         ppsz_command[i_command] = psz_buf;
880         memcpy (psz_buf, psz_command, psz_temp - psz_command);
881         psz_buf[psz_temp - psz_command] = '\0';
882
883         Unescape (psz_buf, psz_buf);
884
885         i_command++;
886         psz_buf += psz_temp - psz_command + 1;
887         psz_command = psz_temp;
888
889         assert (buf + sizeof (buf) >= psz_buf);
890     }
891
892     /*
893      * And then Interpret it
894      */
895
896 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error;  if( (cmd) ) goto error; goto success; }
897     if( i_command == 0 )
898     {
899         p_message = vlm_MessageSimpleNew( "" );
900         goto success;
901     }
902     else IF_EXECUTE( "del",     (i_command != 2),   ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
903     else IF_EXECUTE( "show",    (i_command > 2),    ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
904     else IF_EXECUTE( "help",    (i_command != 1),   ExecuteHelp( &p_message ) )
905     else IF_EXECUTE( "control", (i_command < 3),    ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
906     else IF_EXECUTE( "save",    (i_command != 2),   ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
907     else IF_EXECUTE( "export",  (i_command != 1),   ExecuteExport(p_vlm, &p_message) )
908     else IF_EXECUTE( "load",    (i_command != 2),   ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
909     else IF_EXECUTE( "new",     (i_command < 3),    ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
910     else IF_EXECUTE( "setup",   (i_command < 2),    ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
911     else
912     {
913         p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
914         goto error;
915     }
916 #undef IF_EXECUTE
917
918 success:
919     *pp_message = p_message;
920     return VLC_SUCCESS;
921
922 syntax_error:
923     return ExecuteSyntaxError( ppsz_command[0], pp_message );
924
925 error:
926     *pp_message = p_message;
927     return VLC_EGENERIC;
928 }
929
930 /*****************************************************************************
931  * Media handling
932  *****************************************************************************/
933 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
934 {
935     int i;
936
937     for( i = 0; i < vlm->i_media; i++ )
938     {
939         if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
940             return vlm->media[i];
941     }
942
943     return NULL;
944 }
945
946 /*****************************************************************************
947  * Schedule handling
948  *****************************************************************************/
949 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
950 {
951     if( !psz_name )
952         return NULL;
953
954     vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
955     if( !p_sched )
956         return NULL;
957
958     p_sched->psz_name = strdup( psz_name );
959     p_sched->b_enabled = false;
960     p_sched->i_command = 0;
961     p_sched->command = NULL;
962     p_sched->i_date = 0;
963     p_sched->i_period = 0;
964     p_sched->i_repeat = -1;
965
966     TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
967
968     return p_sched;
969 }
970
971 /* for now, simple delete. After, del with options (last arg) */
972 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched )
973 {
974     if( sched == NULL ) return;
975
976     TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
977
978     if( vlm->i_schedule == 0 ) free( vlm->schedule );
979     free( sched->psz_name );
980     while( sched->i_command )
981     {
982         char *psz_cmd = sched->command[0];
983         TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
984         free( psz_cmd );
985     }
986     free( sched );
987 }
988
989 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
990 {
991     int i;
992
993     for( i = 0; i < vlm->i_schedule; i++ )
994     {
995         if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
996         {
997             return vlm->schedule[i];
998         }
999     }
1000
1001     return NULL;
1002 }
1003
1004 /* Ok, setup schedule command will be able to support only one (argument value) at a time  */
1005 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1006                        const char *psz_value )
1007 {
1008     if( !strcmp( psz_cmd, "enabled" ) )
1009     {
1010         schedule->b_enabled = true;
1011     }
1012     else if( !strcmp( psz_cmd, "disabled" ) )
1013     {
1014         schedule->b_enabled = false;
1015     }
1016     else if( !strcmp( psz_cmd, "date" ) )
1017     {
1018         struct tm time;
1019         const char *p;
1020         time_t date;
1021
1022         time.tm_sec = 0;         /* seconds */
1023         time.tm_min = 0;         /* minutes */
1024         time.tm_hour = 0;        /* hours */
1025         time.tm_mday = 0;        /* day of the month */
1026         time.tm_mon = 0;         /* month */
1027         time.tm_year = 0;        /* year */
1028         time.tm_wday = 0;        /* day of the week */
1029         time.tm_yday = 0;        /* day in the year */
1030         time.tm_isdst = -1;       /* daylight saving time */
1031
1032         /* date should be year/month/day-hour:minutes:seconds */
1033         p = strchr( psz_value, '-' );
1034
1035         if( !strcmp( psz_value, "now" ) )
1036         {
1037             schedule->i_date = 0;
1038         }
1039         else if(p == NULL)
1040         {
1041             return 1;
1042         }
1043         else
1044         {
1045             unsigned i,j,k;
1046
1047             switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1048             {
1049                 case 1:
1050                     time.tm_sec = i;
1051                     break;
1052                 case 2:
1053                     time.tm_min = i;
1054                     time.tm_sec = j;
1055                     break;
1056                 case 3:
1057                     time.tm_hour = i;
1058                     time.tm_min = j;
1059                     time.tm_sec = k;
1060                     break;
1061                 default:
1062                     return 1;
1063             }
1064
1065             switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1066             {
1067                 case 1:
1068                     time.tm_mday = i;
1069                     break;
1070                 case 2:
1071                     time.tm_mon = i - 1;
1072                     time.tm_mday = j;
1073                     break;
1074                 case 3:
1075                     time.tm_year = i - 1900;
1076                     time.tm_mon = j - 1;
1077                     time.tm_mday = k;
1078                     break;
1079                 default:
1080                     return 1;
1081             }
1082
1083             date = mktime( &time );
1084             schedule->i_date = ((mtime_t) date) * 1000000;
1085         }
1086     }
1087     else if( !strcmp( psz_cmd, "period" ) )
1088     {
1089         struct tm time;
1090         const char *p;
1091         const char *psz_time = NULL, *psz_date = NULL;
1092         time_t date;
1093         unsigned i,j,k;
1094
1095         /* First, if date or period are modified, repeat should be equal to -1 */
1096         schedule->i_repeat = -1;
1097
1098         time.tm_sec = 0;         /* seconds */
1099         time.tm_min = 0;         /* minutes */
1100         time.tm_hour = 0;        /* hours */
1101         time.tm_mday = 0;        /* day of the month */
1102         time.tm_mon = 0;         /* month */
1103         time.tm_year = 0;        /* year */
1104         time.tm_wday = 0;        /* day of the week */
1105         time.tm_yday = 0;        /* day in the year */
1106         time.tm_isdst = -1;       /* daylight saving time */
1107
1108         /* date should be year/month/day-hour:minutes:seconds */
1109         p = strchr( psz_value, '-' );
1110         if( p )
1111         {
1112             psz_date = psz_value;
1113             psz_time = p + 1;
1114         }
1115         else
1116         {
1117             psz_time = psz_value;
1118         }
1119
1120         switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1121         {
1122             case 1:
1123                 time.tm_sec = i;
1124                 break;
1125             case 2:
1126                 time.tm_min = i;
1127                 time.tm_sec = j;
1128                 break;
1129             case 3:
1130                 time.tm_hour = i;
1131                 time.tm_min = j;
1132                 time.tm_sec = k;
1133                 break;
1134             default:
1135                 return 1;
1136         }
1137         if( psz_date )
1138         {
1139             switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1140             {
1141                 case 1:
1142                     time.tm_mday = i;
1143                     break;
1144                 case 2:
1145                     time.tm_mon = i;
1146                     time.tm_mday = j;
1147                     break;
1148                 case 3:
1149                     time.tm_year = i;
1150                     time.tm_mon = j;
1151                     time.tm_mday = k;
1152                     break;
1153                 default:
1154                     return 1;
1155             }
1156         }
1157
1158         /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1159         date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1160         schedule->i_period = ((mtime_t) date) * 1000000;
1161     }
1162     else if( !strcmp( psz_cmd, "repeat" ) )
1163     {
1164         int i;
1165
1166         if( sscanf( psz_value, "%d", &i ) == 1 )
1167         {
1168             schedule->i_repeat = i;
1169         }
1170         else
1171         {
1172             return 1;
1173         }
1174     }
1175     else if( !strcmp( psz_cmd, "append" ) )
1176     {
1177         char *command = strdup( psz_value );
1178
1179         TAB_APPEND( schedule->i_command, schedule->command, command );
1180     }
1181     else
1182     {
1183         return 1;
1184     }
1185
1186     return 0;
1187 }
1188
1189 /*****************************************************************************
1190  * Message handling functions
1191  *****************************************************************************/
1192 vlm_message_t *vlm_MessageSimpleNew( const char *psz_name )
1193 {
1194     if( !psz_name ) return NULL;
1195
1196     vlm_message_t *p_message = malloc( sizeof(*p_message) );
1197     if( !p_message )
1198         return NULL;
1199
1200     p_message->psz_name = strdup( psz_name );
1201     if( !p_message->psz_name )
1202     {
1203         free( p_message );
1204         return NULL;
1205     }
1206     p_message->psz_value = NULL;
1207     p_message->i_child = 0;
1208     p_message->child = NULL;
1209
1210     return p_message;
1211 }
1212
1213 vlm_message_t *vlm_MessageNew( const char *psz_name,
1214                                const char *psz_format, ... )
1215 {
1216     vlm_message_t *p_message = vlm_MessageSimpleNew( psz_name );
1217     va_list args;
1218
1219     if( !p_message )
1220         return NULL;
1221
1222     assert( psz_format );
1223     va_start( args, psz_format );
1224     if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1225         p_message->psz_value = NULL;
1226     va_end( args );
1227
1228     if( !p_message->psz_value )
1229     {
1230         vlm_MessageDelete( p_message );
1231         return NULL;
1232     }
1233     return p_message;
1234 }
1235
1236 void vlm_MessageDelete( vlm_message_t *p_message )
1237 {
1238     free( p_message->psz_name );
1239     free( p_message->psz_value );
1240     while( p_message->i_child-- )
1241         vlm_MessageDelete( p_message->child[p_message->i_child] );
1242     free( p_message->child );
1243     free( p_message );
1244 }
1245
1246 /* Add a child */
1247 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1248                                vlm_message_t *p_child )
1249 {
1250     if( p_message == NULL ) return NULL;
1251
1252     if( p_child )
1253     {
1254         TAB_APPEND( p_message->i_child, p_message->child, p_child );
1255     }
1256
1257     return p_child;
1258 }
1259
1260 /*****************************************************************************
1261  * Misc utility functions
1262  *****************************************************************************/
1263 static vlm_message_t *vlm_ShowMedia( vlm_media_sys_t *p_media )
1264 {
1265     vlm_media_t *p_cfg = &p_media->cfg;
1266     vlm_message_t *p_msg;
1267     vlm_message_t *p_msg_sub;
1268     int i;
1269
1270     p_msg = vlm_MessageSimpleNew( p_cfg->psz_name );
1271     vlm_MessageAdd( p_msg,
1272                     vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1273     vlm_MessageAdd( p_msg,
1274                     vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1275
1276     if( p_cfg->b_vod )
1277         vlm_MessageAdd( p_msg,
1278                         vlm_MessageNew( "mux", "%s", p_cfg->vod.psz_mux ) );
1279     else
1280         vlm_MessageAdd( p_msg,
1281                         vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1282
1283     p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "inputs" ) );
1284     for( i = 0; i < p_cfg->i_input; i++ )
1285     {
1286         char *psz_tmp;
1287         if( asprintf( &psz_tmp, "%d", i+1 ) != -1 )
1288         {
1289             vlm_MessageAdd( p_msg_sub,
1290                        vlm_MessageNew( psz_tmp, "%s", p_cfg->ppsz_input[i] ) );
1291             free( psz_tmp );
1292         }
1293     }
1294
1295     vlm_MessageAdd( p_msg,
1296                     vlm_MessageNew( "output", "%s", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1297
1298     p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "options" ) );
1299     for( i = 0; i < p_cfg->i_option; i++ )
1300         vlm_MessageAdd( p_msg_sub, vlm_MessageSimpleNew( p_cfg->ppsz_option[i] ) );
1301
1302     p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "instances" ) );
1303     for( i = 0; i < p_media->i_instance; i++ )
1304     {
1305         vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1306         vlc_value_t val;
1307         vlm_message_t *p_msg_instance;
1308
1309         val.i_int = END_S;
1310         if( p_instance->p_input )
1311             var_Get( p_instance->p_input, "state", &val );
1312
1313         p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageSimpleNew( "instance" ) );
1314
1315         vlm_MessageAdd( p_msg_instance,
1316                         vlm_MessageNew( "name" , "%s", p_instance->psz_name ? p_instance->psz_name : "default" ) );
1317         vlm_MessageAdd( p_msg_instance,
1318                         vlm_MessageNew( "state",
1319                             val.i_int == PLAYING_S ? "playing" :
1320                             val.i_int == PAUSE_S ? "paused" :
1321                             "stopped" ) );
1322
1323         /* FIXME should not do that this way */
1324         if( p_instance->p_input )
1325         {
1326 #define APPEND_INPUT_INFO( key, format, type ) \
1327             vlm_MessageAdd( p_msg_instance, vlm_MessageNew( key, format, \
1328                             var_Get ## type( p_instance->p_input, key ) ) )
1329             APPEND_INPUT_INFO( "position", "%f", Float );
1330             APPEND_INPUT_INFO( "time", "%"PRIi64, Time );
1331             APPEND_INPUT_INFO( "length", "%"PRIi64, Time );
1332             APPEND_INPUT_INFO( "rate", "%f", Float );
1333             APPEND_INPUT_INFO( "title", "%"PRId64, Integer );
1334             APPEND_INPUT_INFO( "chapter", "%"PRId64, Integer );
1335             APPEND_INPUT_INFO( "can-seek", "%d", Bool );
1336         }
1337 #undef APPEND_INPUT_INFO
1338         vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex",
1339                         "%d", p_instance->i_index + 1 ) );
1340     }
1341     return p_msg;
1342 }
1343
1344 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1345                                 vlm_schedule_sys_t *schedule,
1346                                 const char *psz_filter )
1347 {
1348     if( media != NULL )
1349     {
1350         vlm_message_t *p_msg = vlm_MessageSimpleNew( "show" );
1351         if( p_msg )
1352             vlm_MessageAdd( p_msg, vlm_ShowMedia( media ) );
1353         return p_msg;
1354     }
1355
1356     else if( schedule != NULL )
1357     {
1358         int i;
1359         vlm_message_t *msg;
1360         vlm_message_t *msg_schedule;
1361         vlm_message_t *msg_child;
1362         char buffer[100];
1363
1364         msg = vlm_MessageSimpleNew( "show" );
1365         msg_schedule =
1366             vlm_MessageAdd( msg, vlm_MessageSimpleNew( schedule->psz_name ) );
1367
1368         vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1369
1370         vlm_MessageAdd( msg_schedule,
1371                         vlm_MessageNew( "enabled", schedule->b_enabled ?
1372                                         "yes" : "no" ) );
1373
1374         if( schedule->i_date != 0 )
1375         {
1376             struct tm date;
1377             time_t i_time = (time_t)( schedule->i_date / 1000000 );
1378
1379             localtime_r( &i_time, &date);
1380             vlm_MessageAdd( msg_schedule,
1381                             vlm_MessageNew( "date", "%d/%d/%d-%d:%d:%d",
1382                                             date.tm_year + 1900, date.tm_mon + 1,
1383                                             date.tm_mday, date.tm_hour, date.tm_min,
1384                                             date.tm_sec ) );
1385         }
1386         else
1387             vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1388
1389         if( schedule->i_period != 0 )
1390         {
1391             time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1392             struct tm date;
1393
1394             date.tm_sec = (int)( i_time % 60 );
1395             i_time = i_time / 60;
1396             date.tm_min = (int)( i_time % 60 );
1397             i_time = i_time / 60;
1398             date.tm_hour = (int)( i_time % 24 );
1399             i_time = i_time / 24;
1400             date.tm_mday = (int)( i_time % 30 );
1401             i_time = i_time / 30;
1402             /* okay, okay, months are not always 30 days long */
1403             date.tm_mon = (int)( i_time % 12 );
1404             i_time = i_time / 12;
1405             date.tm_year = (int)i_time;
1406
1407             sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1408                      date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1409
1410             vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "%s", buffer) );
1411         }
1412         else
1413             vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1414
1415         sprintf( buffer, "%d", schedule->i_repeat );
1416         vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", "%s", buffer ) );
1417
1418         msg_child =
1419             vlm_MessageAdd( msg_schedule, vlm_MessageSimpleNew("commands" ) );
1420
1421         for( i = 0; i < schedule->i_command; i++ )
1422         {
1423            vlm_MessageAdd( msg_child,
1424                            vlm_MessageSimpleNew( schedule->command[i] ) );
1425         }
1426
1427         return msg;
1428
1429     }
1430
1431     else if( psz_filter && !strcmp( psz_filter, "media" ) )
1432     {
1433         vlm_message_t *p_msg;
1434         vlm_message_t *p_msg_child;
1435         int i_vod = 0, i_broadcast = 0;
1436
1437         for( int i = 0; i < vlm->i_media; i++ )
1438         {
1439             if( vlm->media[i]->cfg.b_vod )
1440                 i_vod++;
1441             else
1442                 i_broadcast++;
1443         }
1444
1445         p_msg = vlm_MessageSimpleNew( "show" );
1446         p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media",
1447                                       "( %d broadcast - %d vod )", i_broadcast,
1448                                       i_vod ) );
1449
1450         for( int i = 0; i < vlm->i_media; i++ )
1451             vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm->media[i] ) );
1452
1453         return p_msg;
1454     }
1455
1456     else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1457     {
1458         int i;
1459         vlm_message_t *msg;
1460         vlm_message_t *msg_child;
1461
1462         msg = vlm_MessageSimpleNew( "show" );
1463         msg_child = vlm_MessageAdd( msg, vlm_MessageSimpleNew( "schedule" ) );
1464
1465         for( i = 0; i < vlm->i_schedule; i++ )
1466         {
1467             vlm_schedule_sys_t *s = vlm->schedule[i];
1468             vlm_message_t *msg_schedule;
1469             mtime_t i_time, i_next_date;
1470
1471             msg_schedule = vlm_MessageAdd( msg_child,
1472                                            vlm_MessageSimpleNew( s->psz_name ) );
1473             vlm_MessageAdd( msg_schedule,
1474                             vlm_MessageNew( "enabled", s->b_enabled ?
1475                                             "yes" : "no" ) );
1476
1477             /* calculate next date */
1478             i_time = vlm_Date();
1479             i_next_date = s->i_date;
1480
1481             if( s->i_period != 0 )
1482             {
1483                 int j = 0;
1484                 while( s->i_date + j * s->i_period <= i_time &&
1485                        s->i_repeat > j )
1486                 {
1487                     j++;
1488                 }
1489
1490                 i_next_date = s->i_date + j * s->i_period;
1491             }
1492
1493             if( i_next_date > i_time )
1494             {
1495                 time_t i_date = (time_t) (i_next_date / 1000000) ;
1496                 struct tm tm;
1497                 char psz_date[32];
1498
1499                 strftime( psz_date, sizeof(psz_date), "%F %H:%M:%S (%a)",
1500                           localtime_r( &i_date, &tm ) );
1501                 vlm_MessageAdd( msg_schedule,
1502                                 vlm_MessageNew( "next launch", "%s", psz_date ) );
1503             }
1504         }
1505
1506         return msg;
1507     }
1508
1509     else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1510     {
1511         vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1512         vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1513
1514         vlm_MessageAdd( show1, show2->child[0] );
1515
1516         /* We must destroy the parent node "show" of show2
1517          * and not the children */
1518         free( show2->psz_name );
1519         free( show2 );
1520
1521         return show1;
1522     }
1523
1524     else
1525     {
1526         return vlm_MessageSimpleNew( "show" );
1527     }
1528 }
1529
1530 /*****************************************************************************
1531  * Config handling functions
1532  *****************************************************************************/
1533 static int Load( vlm_t *vlm, char *file )
1534 {
1535     char *pf = file;
1536     int  i_line = 1;
1537
1538     while( *pf != '\0' )
1539     {
1540         vlm_message_t *message = NULL;
1541         int i_end = 0;
1542
1543         while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1544         {
1545             i_end++;
1546         }
1547
1548         if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1549         {
1550             pf[i_end] = '\0';
1551             i_end++;
1552             if( pf[i_end] == '\n' ) i_end++;
1553         }
1554
1555         if( *pf && ExecuteCommand( vlm, pf, &message ) )
1556         {
1557             if( message )
1558             {
1559                 if( message->psz_value )
1560                     msg_Err( vlm, "Load error on line %d: %s: %s",
1561                              i_line, message->psz_name, message->psz_value );
1562                 vlm_MessageDelete( message );
1563             }
1564             return 1;
1565         }
1566         if( message ) vlm_MessageDelete( message );
1567
1568         pf += i_end;
1569         i_line++;
1570     }
1571
1572     return 0;
1573 }
1574
1575 static char *Save( vlm_t *vlm )
1576 {
1577     char *save = NULL;
1578     char psz_header[] = "\n"
1579                         "# VLC media player VLM command batch\n"
1580                         "# http://www.videolan.org/vlc/\n\n" ;
1581     char *p;
1582     int i,j;
1583     int i_length = strlen( psz_header );
1584
1585     for( i = 0; i < vlm->i_media; i++ )
1586     {
1587         vlm_media_sys_t *media = vlm->media[i];
1588         vlm_media_t *p_cfg = &media->cfg;
1589
1590         if( p_cfg->b_vod )
1591             i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1592         else
1593             i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1594
1595         if( p_cfg->b_enabled == true )
1596             i_length += strlen( "enabled" );
1597         else
1598             i_length += strlen( "disabled" );
1599
1600         if( !p_cfg->b_vod && p_cfg->broadcast.b_loop == true )
1601             i_length += strlen( " loop\n" );
1602         else
1603             i_length += strlen( "\n" );
1604
1605         for( j = 0; j < p_cfg->i_input; j++ )
1606             i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1607
1608         if( p_cfg->psz_output != NULL )
1609             i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1610
1611         for( j = 0; j < p_cfg->i_option; j++ )
1612             i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1613
1614         if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1615             i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1616     }
1617
1618     for( i = 0; i < vlm->i_schedule; i++ )
1619     {
1620         vlm_schedule_sys_t *schedule = vlm->schedule[i];
1621
1622         i_length += strlen( "new  schedule " ) + strlen( schedule->psz_name );
1623
1624         if( schedule->b_enabled == true )
1625         {
1626             i_length += strlen( "date //-:: enabled\n" ) + 14;
1627         }
1628         else
1629         {
1630             i_length += strlen( "date //-:: disabled\n" ) + 14;
1631         }
1632
1633
1634         if( schedule->i_period != 0 )
1635         {
1636             i_length += strlen( "setup  " ) + strlen( schedule->psz_name ) +
1637                 strlen( "period //-::\n" ) + 14;
1638         }
1639
1640         if( schedule->i_repeat >= 0 )
1641         {
1642             char buffer[12];
1643
1644             sprintf( buffer, "%d", schedule->i_repeat );
1645             i_length += strlen( "setup  repeat \n" ) +
1646                 strlen( schedule->psz_name ) + strlen( buffer );
1647         }
1648         else
1649         {
1650             i_length++;
1651         }
1652
1653         for( j = 0; j < schedule->i_command; j++ )
1654         {
1655             i_length += strlen( "setup  append \n" ) +
1656                 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1657         }
1658
1659     }
1660
1661     /* Don't forget the '\0' */
1662     i_length++;
1663     /* now we have the length of save */
1664
1665     p = save = malloc( i_length );
1666     if( !save ) return NULL;
1667     *save = '\0';
1668
1669     p += sprintf( p, "%s", psz_header );
1670
1671     /* finally we can write in it */
1672     for( i = 0; i < vlm->i_media; i++ )
1673     {
1674         vlm_media_sys_t *media = vlm->media[i];
1675         vlm_media_t *p_cfg = &media->cfg;
1676
1677         if( p_cfg->b_vod )
1678             p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1679         else
1680             p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1681
1682         if( p_cfg->b_enabled )
1683             p += sprintf( p, "enabled" );
1684         else
1685             p += sprintf( p, "disabled" );
1686
1687         if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1688             p += sprintf( p, " loop\n" );
1689         else
1690             p += sprintf( p, "\n" );
1691
1692         for( j = 0; j < p_cfg->i_input; j++ )
1693             p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1694
1695         if( p_cfg->psz_output )
1696             p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1697
1698         for( j = 0; j < p_cfg->i_option; j++ )
1699             p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1700
1701         if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1702             p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1703     }
1704
1705     /* and now, the schedule scripts */
1706     for( i = 0; i < vlm->i_schedule; i++ )
1707     {
1708         vlm_schedule_sys_t *schedule = vlm->schedule[i];
1709         struct tm date;
1710         time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1711
1712         localtime_r( &i_time, &date);
1713         p += sprintf( p, "new %s schedule ", schedule->psz_name);
1714
1715         if( schedule->b_enabled == true )
1716         {
1717             p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1718                           date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1719                           date.tm_hour, date.tm_min, date.tm_sec );
1720         }
1721         else
1722         {
1723             p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1724                           date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1725                           date.tm_hour, date.tm_min, date.tm_sec);
1726         }
1727
1728         if( schedule->i_period != 0 )
1729         {
1730             p += sprintf( p, "setup %s ", schedule->psz_name );
1731
1732             i_time = (time_t) ( schedule->i_period / 1000000 );
1733
1734             date.tm_sec = (int)( i_time % 60 );
1735             i_time = i_time / 60;
1736             date.tm_min = (int)( i_time % 60 );
1737             i_time = i_time / 60;
1738             date.tm_hour = (int)( i_time % 24 );
1739             i_time = i_time / 24;
1740             date.tm_mday = (int)( i_time % 30 );
1741             i_time = i_time / 30;
1742             /* okay, okay, months are not always 30 days long */
1743             date.tm_mon = (int)( i_time % 12 );
1744             i_time = i_time / 12;
1745             date.tm_year = (int)i_time;
1746
1747             p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1748                           date.tm_year, date.tm_mon, date.tm_mday,
1749                           date.tm_hour, date.tm_min, date.tm_sec);
1750         }
1751
1752         if( schedule->i_repeat >= 0 )
1753         {
1754             p += sprintf( p, "setup %s repeat %d\n",
1755                           schedule->psz_name, schedule->i_repeat );
1756         }
1757         else
1758         {
1759             p += sprintf( p, "\n" );
1760         }
1761
1762         for( j = 0; j < schedule->i_command; j++ )
1763         {
1764             p += sprintf( p, "setup %s append %s\n",
1765                           schedule->psz_name, schedule->command[j] );
1766         }
1767
1768     }
1769
1770     return save;
1771 }
1772
1773 #endif /* ENABLE_VLM */