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