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