Sanitize input headers (pass 4).
[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 #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 | [+-](milliseconds)ms" );
340
341     return VLC_SUCCESS;
342 }
343
344 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
345 {
346     vlm_media_sys_t *p_media;
347     const char *psz_control = NULL;
348     const char *psz_instance = NULL;
349     const char *psz_argument = NULL;
350     int i_index;
351     int i_result;
352
353     if( !ExecuteIsMedia( p_vlm, psz_name ) )
354     {
355         *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
356         return VLC_EGENERIC;
357     }
358
359     assert( i_arg > 0 );
360
361 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
362     i_index = 0;
363     if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
364     {
365         i_index = 1;
366         psz_instance = ppsz_arg[0];
367
368         if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
369             return ExecuteSyntaxError( "control", pp_status );
370     }
371 #undef IS
372     psz_control = ppsz_arg[i_index];
373
374     if( i_index+1 < i_arg )
375         psz_argument = ppsz_arg[i_index+1];
376
377     p_media = vlm_MediaSearch( p_vlm, psz_name );
378     assert( p_media );
379
380     if( !strcmp( psz_control, "play" ) )
381     {
382         int i_input_index = 0;
383         int i;
384
385         if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
386         {
387             i_input_index = i-1;
388         }
389         else if( psz_argument )
390         {
391             int j;
392             vlm_media_t *p_cfg = &p_media->cfg;
393             for ( j=0; j < p_cfg->i_input; j++)
394             {
395                 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
396                 {
397                     i_input_index = j;
398                     break;
399                 }
400             }
401         }
402
403         if( p_media->cfg.b_vod )
404             i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_VOD_INSTANCE, p_media->cfg.id, psz_instance, i_input_index, NULL );    // we should get here now
405         else
406             i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
407     }
408     else if( !strcmp( psz_control, "seek" ) )
409     {
410         if( psz_argument )
411         {
412             bool b_relative;
413             if( psz_argument[0] == '+' || psz_argument[0] == '-' )
414                 b_relative = true;
415             else
416                 b_relative = false;
417
418             if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
419             {
420                 /* Time (ms or s) */
421                 int64_t i_new_time;
422
423                 if( strstr( psz_argument, "ms" ) )
424                     i_new_time =  1000 * (int64_t)atoi( psz_argument );
425                 else
426                     i_new_time = 1000000 * (int64_t)atoi( psz_argument );
427
428                 if( b_relative )
429                 {
430                     int64_t i_time = 0;
431                     vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
432                     i_new_time += i_time;
433                 }
434                 if( i_new_time < 0 )
435                     i_new_time = 0;
436                 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
437             }
438             else
439             {
440                 /* Percent */
441                 double d_new_position = us_atof( psz_argument ) / 100.0;
442
443                 if( b_relative )
444                 {
445                     double d_position = 0.0;
446
447                     vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
448                     d_new_position += d_position;
449                 }
450                 if( d_new_position < 0.0 )
451                     d_new_position = 0.0;
452                 else if( d_new_position > 1.0 )
453                     d_new_position = 1.0;
454                 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
455             }
456         }
457         else
458         {
459             i_result = VLC_EGENERIC;
460         }
461     }
462     else if( !strcmp( psz_control, "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             if ( vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL ) )
625                 goto error;
626         }
627         else if( !strcmp( ppsz_property[i], "append" ) )
628         {
629             char *psz_line;
630             int j;
631             /* Beware: everything behind append is considered as
632              * command line */
633
634             if( ++i >= i_property )
635                 break;
636
637             psz_line = strdup( ppsz_property[i] );
638             for( j = i+1; j < i_property; j++ )
639             {
640                 psz_line = realloc( psz_line, strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
641                 strcat( psz_line, " " );
642                 strcat( psz_line, ppsz_property[j] );
643             }
644
645             if( vlm_ScheduleSetup( p_schedule, "append", psz_line ) )
646                 goto error;
647             break;
648         }
649         else
650         {
651             if( i + 1 >= i_property )
652             {
653                 if( b_new )
654                     vlm_ScheduleDelete( p_vlm, p_schedule );
655                 return ExecuteSyntaxError( psz_cmd, pp_status );
656             }
657
658             if( vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] ) )
659                 goto error;
660             i++;
661         }
662     }
663     *pp_status = vlm_MessageNew( psz_cmd, vlm_NULL );
664     return VLC_SUCCESS;
665
666 error:
667     *pp_status = vlm_MessageNew( psz_cmd, "Error while setting the property '%s' to the schedule",
668                                  ppsz_property[i] );
669     return VLC_EGENERIC;
670 }
671
672 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, bool b_new,
673                                  const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
674 {
675     const char *psz_cmd = b_new ? "new" : "setup";
676     vlm_media_t *p_cfg = NULL;
677     int i_result;
678     int i;
679
680 #undef ERROR
681 #undef MISSING
682 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
683     if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
684         ERROR( "unknown media" );
685
686 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
687     for( i = 0; i < i_property; i++ )
688     {
689         const char *psz_option = ppsz_property[i];
690         const char *psz_value = i+1 < i_property ? ppsz_property[i+1] :  NULL;
691
692         if( !strcmp( psz_option, "enabled" ) )
693         {
694             p_cfg->b_enabled = true;
695         }
696         else if( !strcmp( psz_option, "disabled" ) )
697         {
698             p_cfg->b_enabled = false;
699         }
700         else if( !strcmp( psz_option, "input" ) )
701         {
702             MISSING( "input" );
703             TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
704             i++;
705         }
706         else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
707         {
708             while( p_cfg->i_input > 0 )
709                 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
710             i++;
711         }
712         else if( !strcmp( psz_option, "inputdel" ) )
713         {
714             int j;
715
716             MISSING( "inputdel" );
717
718             for( j = 0; j < p_cfg->i_input; j++ )
719             {
720                 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
721                 {
722                     TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
723                     break;
724                 }
725             }
726             i++;
727         }
728         else if( !strcmp( psz_option, "inputdeln" ) )
729         {
730             int i_index;
731
732             MISSING( "inputdeln" );
733  
734             i_index = atoi( psz_value );
735             if( i_index > 0 && i_index <= p_cfg->i_input )
736                 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[i_index-1] );
737             i++;
738         }
739         else if( !strcmp( psz_option, "output" ) )
740         {
741             MISSING( "output" );
742
743             free( p_cfg->psz_output );
744             p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
745             i++;
746         }
747         else if( !strcmp( psz_option, "option" ) )
748         {
749             MISSING( "option" );
750
751             TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
752             i++;
753         }
754         else if( !strcmp( psz_option, "loop" ) )
755         {
756             if( p_cfg->b_vod )
757                 ERROR( "invalid loop option for vod" );
758             p_cfg->broadcast.b_loop = true;
759         }
760         else if( !strcmp( psz_option, "unloop" ) )
761         {
762             if( p_cfg->b_vod )
763                 ERROR( "invalid unloop option for vod" );
764             p_cfg->broadcast.b_loop = false;
765         }
766         else if( !strcmp( psz_option, "mux" ) )
767         {
768             MISSING( "mux" );
769             if( !p_cfg->b_vod )
770                 ERROR( "invalid mux option for broadcast" );
771
772             free( p_cfg->vod.psz_mux );
773             p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
774             i++;
775         }
776         else
777         {
778             fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
779             ERROR( "Wrong command syntax" );
780         }
781     }
782 #undef MISSING
783 #undef ERROR
784
785     /* */
786     i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
787     vlm_media_Delete( p_cfg );
788
789     *pp_status = vlm_MessageNew( psz_cmd, vlm_NULL );
790     return i_result;
791
792 error:
793     if( p_cfg )
794     {
795         if( b_new )
796             vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
797         vlm_media_Delete( p_cfg );
798     }
799     return VLC_EGENERIC;
800 }
801
802 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 )
803 {
804     /* Check name */
805     if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
806     {
807         *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
808         return VLC_EGENERIC;
809     }
810     if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
811     {
812         *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
813         return VLC_EGENERIC;
814     }
815     /* */
816     if( !strcmp( psz_type, "schedule" ) )
817     {
818         vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
819         if( !p_schedule )
820         {
821             *pp_status = vlm_MessageNew( "new", "could not create schedule" );
822             return VLC_EGENERIC;
823         }
824         return ExecuteScheduleProperty( p_vlm, p_schedule, true, i_property, ppsz_property, pp_status );
825     }
826     else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
827     {
828         vlm_media_t cfg;
829         int64_t id;
830
831         vlm_media_Init( &cfg );
832         cfg.psz_name = strdup( psz_name );
833         cfg.b_vod = !strcmp( psz_type, "vod" );
834
835         if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
836         {
837             vlm_media_Clean( &cfg );
838             *pp_status = vlm_MessageNew( "new", "could not create media" );
839             return VLC_EGENERIC;
840         }
841         vlm_media_Clean( &cfg );
842         return ExecuteMediaProperty( p_vlm, id, true, i_property, ppsz_property, pp_status );
843     }
844     else
845     {
846         *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
847         return VLC_EGENERIC;
848     }
849 }
850
851 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
852 {
853     if( ExecuteIsSchedule( p_vlm, psz_name ) )
854     {
855         vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
856         return ExecuteScheduleProperty( p_vlm, p_schedule, false, i_property, ppsz_property, pp_status );
857     }
858     else if( ExecuteIsMedia( p_vlm, psz_name ) )
859     {
860         int64_t id;
861         if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
862             goto error;
863         return ExecuteMediaProperty( p_vlm, id, false, i_property, ppsz_property, pp_status );
864     }
865
866 error:
867     *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
868     return VLC_EGENERIC;
869 }
870
871 int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
872                            vlm_message_t **pp_message )
873 {
874     size_t i_command = 0;
875     char buf[strlen (psz_command) + 1], *psz_buf = buf;
876     char *ppsz_command[3+sizeof (buf) / 2];
877     vlm_message_t *p_message = NULL;
878
879     /* First, parse the line and cut it */
880     while( *psz_command != '\0' )
881     {
882         const char *psz_temp;
883
884         if(isspace (*psz_command))
885         {
886             psz_command++;
887             continue;
888         }
889
890         /* support for comments */
891         if( i_command == 0 && *psz_command == '#')
892         {
893             p_message = vlm_MessageNew( "", vlm_NULL );
894             goto success;
895         }
896
897         psz_temp = FindCommandEnd( psz_command );
898
899         if( psz_temp == NULL )
900         {
901             p_message = vlm_MessageNew( "Incomplete command", psz_command );
902             goto error;
903         }
904
905         assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
906
907         ppsz_command[i_command] = psz_buf;
908         memcpy (psz_buf, psz_command, psz_temp - psz_command);
909         psz_buf[psz_temp - psz_command] = '\0';
910
911         Unescape (psz_buf, psz_buf);
912
913         i_command++;
914         psz_buf += psz_temp - psz_command + 1;
915         psz_command = psz_temp;
916
917         assert (buf + sizeof (buf) >= psz_buf);
918     }
919
920     /*
921      * And then Interpret it
922      */
923
924 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error;  if( (cmd) ) goto error; goto success; }
925     if( i_command == 0 )
926     {
927         p_message = vlm_MessageNew( "", vlm_NULL );
928         goto success;
929     }
930     else IF_EXECUTE( "del",     (i_command != 2),   ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
931     else IF_EXECUTE( "show",    (i_command > 2),    ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
932     else IF_EXECUTE( "help",    (i_command != 1),   ExecuteHelp( &p_message ) )
933     else IF_EXECUTE( "control", (i_command < 3),    ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
934     else IF_EXECUTE( "save",    (i_command != 2),   ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
935     else IF_EXECUTE( "export",  (i_command != 1),   ExecuteExport(p_vlm, &p_message) )
936     else IF_EXECUTE( "load",    (i_command != 2),   ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
937     else IF_EXECUTE( "new",     (i_command < 3),    ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
938     else IF_EXECUTE( "setup",   (i_command < 2),    ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
939     else
940     {
941         p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
942         goto error;
943     }
944 #undef IF_EXECUTE
945
946 success:
947     *pp_message = p_message;
948     return VLC_SUCCESS;
949
950 syntax_error:
951     return ExecuteSyntaxError( ppsz_command[0], pp_message );
952
953 error:
954     *pp_message = p_message;
955     return VLC_EGENERIC;
956 }
957
958 /*****************************************************************************
959  * Media handling
960  *****************************************************************************/
961 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
962 {
963     int i;
964
965     for( i = 0; i < vlm->i_media; i++ )
966     {
967         if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
968             return vlm->media[i];
969     }
970
971     return NULL;
972 }
973
974 /*****************************************************************************
975  * Schedule handling
976  *****************************************************************************/
977 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
978 {
979     if( !psz_name )
980         return NULL;
981
982     vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
983     if( !p_sched )
984         return NULL;
985
986     p_sched->psz_name = strdup( psz_name );
987     p_sched->b_enabled = false;
988     p_sched->i_command = 0;
989     p_sched->command = NULL;
990     p_sched->i_date = 0;
991     p_sched->i_period = 0;
992     p_sched->i_repeat = -1;
993
994     TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
995
996     return p_sched;
997 }
998
999 /* for now, simple delete. After, del with options (last arg) */
1000 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched )
1001 {
1002     if( sched == NULL ) return;
1003
1004     TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1005
1006     if( vlm->i_schedule == 0 ) free( vlm->schedule );
1007     free( sched->psz_name );
1008     while( sched->i_command )
1009     {
1010         char *psz_cmd = sched->command[0];
1011         TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1012         free( psz_cmd );
1013     }
1014     free( sched );
1015 }
1016
1017 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1018 {
1019     int i;
1020
1021     for( i = 0; i < vlm->i_schedule; i++ )
1022     {
1023         if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1024         {
1025             return vlm->schedule[i];
1026         }
1027     }
1028
1029     return NULL;
1030 }
1031
1032 /* Ok, setup schedule command will be able to support only one (argument value) at a time  */
1033 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1034                        const char *psz_value )
1035 {
1036     if( !strcmp( psz_cmd, "enabled" ) )
1037     {
1038         schedule->b_enabled = true;
1039     }
1040     else if( !strcmp( psz_cmd, "disabled" ) )
1041     {
1042         schedule->b_enabled = false;
1043     }
1044     else if( !strcmp( psz_cmd, "date" ) )
1045     {
1046         struct tm time;
1047         const char *p;
1048         time_t date;
1049
1050         time.tm_sec = 0;         /* seconds */
1051         time.tm_min = 0;         /* minutes */
1052         time.tm_hour = 0;        /* hours */
1053         time.tm_mday = 0;        /* day of the month */
1054         time.tm_mon = 0;         /* month */
1055         time.tm_year = 0;        /* year */
1056         time.tm_wday = 0;        /* day of the week */
1057         time.tm_yday = 0;        /* day in the year */
1058         time.tm_isdst = -1;       /* daylight saving time */
1059
1060         /* date should be year/month/day-hour:minutes:seconds */
1061         p = strchr( psz_value, '-' );
1062
1063         if( !strcmp( psz_value, "now" ) )
1064         {
1065             schedule->i_date = 0;
1066         }
1067         else if(p == NULL)
1068         {
1069             return 1;
1070         }
1071         else
1072         {
1073             unsigned i,j,k;
1074
1075             switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1076             {
1077                 case 1:
1078                     time.tm_sec = i;
1079                     break;
1080                 case 2:
1081                     time.tm_min = i;
1082                     time.tm_sec = j;
1083                     break;
1084                 case 3:
1085                     time.tm_hour = i;
1086                     time.tm_min = j;
1087                     time.tm_sec = k;
1088                     break;
1089                 default:
1090                     return 1;
1091             }
1092
1093             switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1094             {
1095                 case 1:
1096                     time.tm_mday = i;
1097                     break;
1098                 case 2:
1099                     time.tm_mon = i - 1;
1100                     time.tm_mday = j;
1101                     break;
1102                 case 3:
1103                     time.tm_year = i - 1900;
1104                     time.tm_mon = j - 1;
1105                     time.tm_mday = k;
1106                     break;
1107                 default:
1108                     return 1;
1109             }
1110
1111             date = mktime( &time );
1112             schedule->i_date = ((mtime_t) date) * 1000000;
1113         }
1114     }
1115     else if( !strcmp( psz_cmd, "period" ) )
1116     {
1117         struct tm time;
1118         const char *p;
1119         const char *psz_time = NULL, *psz_date = NULL;
1120         time_t date;
1121         unsigned i,j,k;
1122
1123         /* First, if date or period are modified, repeat should be equal to -1 */
1124         schedule->i_repeat = -1;
1125
1126         time.tm_sec = 0;         /* seconds */
1127         time.tm_min = 0;         /* minutes */
1128         time.tm_hour = 0;        /* hours */
1129         time.tm_mday = 0;        /* day of the month */
1130         time.tm_mon = 0;         /* month */
1131         time.tm_year = 0;        /* year */
1132         time.tm_wday = 0;        /* day of the week */
1133         time.tm_yday = 0;        /* day in the year */
1134         time.tm_isdst = -1;       /* daylight saving time */
1135
1136         /* date should be year/month/day-hour:minutes:seconds */
1137         p = strchr( psz_value, '-' );
1138         if( p )
1139         {
1140             psz_date = psz_value;
1141             psz_time = p + 1;
1142         }
1143         else
1144         {
1145             psz_time = psz_value;
1146         }
1147
1148         switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1149         {
1150             case 1:
1151                 time.tm_sec = i;
1152                 break;
1153             case 2:
1154                 time.tm_min = i;
1155                 time.tm_sec = j;
1156                 break;
1157             case 3:
1158                 time.tm_hour = i;
1159                 time.tm_min = j;
1160                 time.tm_sec = k;
1161                 break;
1162             default:
1163                 return 1;
1164         }
1165         if( psz_date )
1166         {
1167             switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1168             {
1169                 case 1:
1170                     time.tm_mday = i;
1171                     break;
1172                 case 2:
1173                     time.tm_mon = i;
1174                     time.tm_mday = j;
1175                     break;
1176                 case 3:
1177                     time.tm_year = i;
1178                     time.tm_mon = j;
1179                     time.tm_mday = k;
1180                     break;
1181                 default:
1182                     return 1;
1183             }
1184         }
1185
1186         /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1187         date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1188         schedule->i_period = ((mtime_t) date) * 1000000;
1189     }
1190     else if( !strcmp( psz_cmd, "repeat" ) )
1191     {
1192         int i;
1193
1194         if( sscanf( psz_value, "%d", &i ) == 1 )
1195         {
1196             schedule->i_repeat = i;
1197         }
1198         else
1199         {
1200             return 1;
1201         }
1202     }
1203     else if( !strcmp( psz_cmd, "append" ) )
1204     {
1205         char *command = strdup( psz_value );
1206
1207         TAB_APPEND( schedule->i_command, schedule->command, command );
1208     }
1209     else
1210     {
1211         return 1;
1212     }
1213     return 0;
1214 }
1215
1216 /*****************************************************************************
1217  * Message handling functions
1218  *****************************************************************************/
1219 vlm_message_t *vlm_MessageNew( const char *psz_name,
1220                                const char *psz_format, ... )
1221 {
1222     vlm_message_t *p_message;
1223     va_list args;
1224
1225     if( !psz_name ) return NULL;
1226
1227     p_message = malloc( sizeof(vlm_message_t) );
1228     if( !p_message)
1229     {
1230         return NULL;
1231     }
1232
1233     p_message->psz_value = 0;
1234
1235     if( psz_format )
1236     {
1237         va_start( args, psz_format );
1238         if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1239         {
1240             va_end( args );
1241             free( p_message );
1242             return NULL;
1243         }
1244         va_end( args );
1245     }
1246
1247     p_message->psz_name = strdup( psz_name );
1248     p_message->i_child = 0;
1249     p_message->child = NULL;
1250
1251     return p_message;
1252 }
1253
1254 void vlm_MessageDelete( vlm_message_t *p_message )
1255 {
1256     free( p_message->psz_name );
1257     free( p_message->psz_value );
1258     while( p_message->i_child-- )
1259         vlm_MessageDelete( p_message->child[p_message->i_child] );
1260     free( p_message->child );
1261     free( p_message );
1262 }
1263
1264 /* Add a child */
1265 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1266                                vlm_message_t *p_child )
1267 {
1268     if( p_message == NULL ) return NULL;
1269
1270     if( p_child )
1271     {
1272         TAB_APPEND( p_message->i_child, p_message->child, p_child );
1273     }
1274
1275     return p_child;
1276 }
1277
1278 /*****************************************************************************
1279  * Misc utility functions
1280  *****************************************************************************/
1281 static vlm_message_t *vlm_ShowMedia( vlm_media_sys_t *p_media )
1282 {
1283     vlm_media_t *p_cfg = &p_media->cfg;
1284     vlm_message_t *p_msg;
1285     vlm_message_t *p_msg_sub;
1286     int i;
1287
1288     p_msg = vlm_MessageNew( p_cfg->psz_name, vlm_NULL );
1289     vlm_MessageAdd( p_msg,
1290                     vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1291     vlm_MessageAdd( p_msg,
1292                     vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1293
1294     if( p_cfg->b_vod )
1295         vlm_MessageAdd( p_msg,
1296                         vlm_MessageNew( "mux", p_cfg->vod.psz_mux ) );
1297     else
1298         vlm_MessageAdd( p_msg,
1299                         vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1300
1301     p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "inputs", vlm_NULL ) );
1302     for( i = 0; i < p_cfg->i_input; i++ )
1303     {
1304         char *psz_tmp;
1305         if( asprintf( &psz_tmp, "%d", i+1 ) != -1 )
1306         {
1307             vlm_MessageAdd( p_msg_sub,
1308                             vlm_MessageNew( psz_tmp, p_cfg->ppsz_input[i] ) );
1309             free( psz_tmp );
1310         }
1311     }
1312
1313     vlm_MessageAdd( p_msg,
1314                     vlm_MessageNew( "output", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1315
1316     p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "options", vlm_NULL ) );
1317     for( i = 0; i < p_cfg->i_option; i++ )
1318         vlm_MessageAdd( p_msg_sub, vlm_MessageNew( p_cfg->ppsz_option[i], vlm_NULL ) );
1319
1320     p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "instances", vlm_NULL ) );
1321     for( i = 0; i < p_media->i_instance; i++ )
1322     {
1323         vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1324         vlc_value_t val;
1325         vlm_message_t *p_msg_instance;
1326         char *psz_tmp;
1327
1328         val.i_int = END_S;
1329         if( p_instance->p_input )
1330             var_Get( p_instance->p_input, "state", &val );
1331
1332         p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageNew( "instance" , vlm_NULL ) );
1333
1334         vlm_MessageAdd( p_msg_instance,
1335                         vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1336         vlm_MessageAdd( p_msg_instance,
1337                         vlm_MessageNew( "state",
1338                             val.i_int == PLAYING_S ? "playing" :
1339                             val.i_int == PAUSE_S ? "paused" :
1340                             "stopped" ) );
1341
1342         /* FIXME should not do that this way */
1343         if( p_instance->p_input )
1344         {
1345 #define APPEND_INPUT_INFO( a, format, type ) \
1346             if( asprintf( &psz_tmp, format, \
1347                       var_Get ## type( p_instance->p_input, a ) ) != -1 ) \
1348             { \
1349                 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( a, \
1350                                 psz_tmp ) ); \
1351                 free( psz_tmp ); \
1352             }
1353             APPEND_INPUT_INFO( "position", "%f", Float );
1354             APPEND_INPUT_INFO( "time", "%"PRIi64, Time );
1355             APPEND_INPUT_INFO( "length", "%"PRIi64, Time );
1356             APPEND_INPUT_INFO( "rate", "%d", Integer );
1357             APPEND_INPUT_INFO( "title", "%d", Integer );
1358             APPEND_INPUT_INFO( "chapter", "%d", Integer );
1359             APPEND_INPUT_INFO( "seekable", "%d", Bool );
1360         }
1361 #undef APPEND_INPUT_INFO
1362         if( asprintf( &psz_tmp, "%d", p_instance->i_index + 1 ) != -1 )
1363         {
1364             vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex",
1365                             psz_tmp ) );
1366             free( psz_tmp );
1367         }
1368     }
1369     return p_msg;
1370 }
1371
1372 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1373                                 vlm_schedule_sys_t *schedule,
1374                                 const char *psz_filter )
1375 {
1376     if( media != NULL )
1377     {
1378         vlm_message_t *p_msg = vlm_MessageNew( "show", vlm_NULL );
1379         if( p_msg )
1380             vlm_MessageAdd( p_msg, vlm_ShowMedia( media ) );
1381         return p_msg;
1382     }
1383
1384     else if( schedule != NULL )
1385     {
1386         int i;
1387         vlm_message_t *msg;
1388         vlm_message_t *msg_schedule;
1389         vlm_message_t *msg_child;
1390         char buffer[100];
1391
1392         msg = vlm_MessageNew( "show", vlm_NULL );
1393         msg_schedule =
1394             vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, vlm_NULL ) );
1395
1396         vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1397
1398         vlm_MessageAdd( msg_schedule,
1399                         vlm_MessageNew( "enabled", schedule->b_enabled ?
1400                                         "yes" : "no" ) );
1401
1402         if( schedule->i_date != 0 )
1403         {
1404             struct tm date;
1405             time_t i_time = (time_t)( schedule->i_date / 1000000 );
1406             char *psz_date;
1407
1408             localtime_r( &i_time, &date);
1409             if( asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1410                           date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1411                           date.tm_hour, date.tm_min, date.tm_sec ) != -1 )
1412             {
1413                  vlm_MessageAdd( msg_schedule,
1414                                  vlm_MessageNew( "date", psz_date ) );
1415                  free( psz_date );
1416             }
1417         }
1418         else
1419             vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1420
1421         if( schedule->i_period != 0 )
1422         {
1423             time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1424             struct tm date;
1425
1426             date.tm_sec = (int)( i_time % 60 );
1427             i_time = i_time / 60;
1428             date.tm_min = (int)( i_time % 60 );
1429             i_time = i_time / 60;
1430             date.tm_hour = (int)( i_time % 24 );
1431             i_time = i_time / 24;
1432             date.tm_mday = (int)( i_time % 30 );
1433             i_time = i_time / 30;
1434             /* okay, okay, months are not always 30 days long */
1435             date.tm_mon = (int)( i_time % 12 );
1436             i_time = i_time / 12;
1437             date.tm_year = (int)i_time;
1438
1439             sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1440                      date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1441
1442             vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1443         }
1444         else
1445             vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1446
1447         sprintf( buffer, "%d", schedule->i_repeat );
1448         vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1449
1450         msg_child =
1451             vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", vlm_NULL ) );
1452
1453         for( i = 0; i < schedule->i_command; i++ )
1454         {
1455            vlm_MessageAdd( msg_child,
1456                            vlm_MessageNew( schedule->command[i], vlm_NULL ) );
1457         }
1458
1459         return msg;
1460
1461     }
1462
1463     else if( psz_filter && !strcmp( psz_filter, "media" ) )
1464     {
1465         vlm_message_t *p_msg;
1466         vlm_message_t *p_msg_child;
1467         int i_vod = 0, i_broadcast = 0;
1468         int i;
1469         char *psz_count;
1470
1471         for( i = 0; i < vlm->i_media; i++ )
1472         {
1473             if( vlm->media[i]->cfg.b_vod )
1474                 i_vod++;
1475             else
1476                 i_broadcast++;
1477         }
1478
1479         if( asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast,
1480                       i_vod) == -1 )
1481             return NULL;
1482         p_msg = vlm_MessageNew( "show", vlm_NULL );
1483         p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media", psz_count ) );
1484         free( psz_count );
1485
1486         for( i = 0; i < vlm->i_media; i++ )
1487             vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm->media[i] ) );
1488
1489         return p_msg;
1490     }
1491
1492     else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1493     {
1494         int i;
1495         vlm_message_t *msg;
1496         vlm_message_t *msg_child;
1497
1498         msg = vlm_MessageNew( "show", vlm_NULL );
1499         msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", vlm_NULL ) );
1500
1501         for( i = 0; i < vlm->i_schedule; i++ )
1502         {
1503             vlm_schedule_sys_t *s = vlm->schedule[i];
1504             vlm_message_t *msg_schedule;
1505             mtime_t i_time, i_next_date;
1506
1507             msg_schedule = vlm_MessageAdd( msg_child,
1508                                            vlm_MessageNew( s->psz_name, vlm_NULL ) );
1509             vlm_MessageAdd( msg_schedule,
1510                             vlm_MessageNew( "enabled", s->b_enabled ?
1511                                             "yes" : "no" ) );
1512
1513             /* calculate next date */
1514             i_time = vlm_Date();
1515             i_next_date = s->i_date;
1516
1517             if( s->i_period != 0 )
1518             {
1519                 int j = 0;
1520                 while( s->i_date + j * s->i_period <= i_time &&
1521                        s->i_repeat > j )
1522                 {
1523                     j++;
1524                 }
1525
1526                 i_next_date = s->i_date + j * s->i_period;
1527             }
1528
1529             if( i_next_date > i_time )
1530             {
1531                 time_t i_date = (time_t) (i_next_date / 1000000) ;
1532
1533 #if !defined( UNDER_CE )
1534 #ifdef HAVE_CTIME_R
1535                 char psz_date[500];
1536                 ctime_r( &i_date, psz_date );
1537 #else
1538                 char *psz_date = ctime( &i_date );
1539 #endif
1540
1541                 vlm_MessageAdd( msg_schedule,
1542                                 vlm_MessageNew( "next launch", psz_date ) );
1543 #endif
1544             }
1545         }
1546
1547         return msg;
1548     }
1549
1550     else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1551     {
1552         vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1553         vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1554
1555         vlm_MessageAdd( show1, show2->child[0] );
1556
1557         /* We must destroy the parent node "show" of show2
1558          * and not the children */
1559         free( show2->psz_name );
1560         free( show2 );
1561
1562         return show1;
1563     }
1564
1565     else
1566     {
1567         return vlm_MessageNew( "show", vlm_NULL );
1568     }
1569 }
1570
1571 /*****************************************************************************
1572  * Config handling functions
1573  *****************************************************************************/
1574 static int Load( vlm_t *vlm, char *file )
1575 {
1576     char *pf = file;
1577     int  i_line = 1;
1578
1579     while( *pf != '\0' )
1580     {
1581         vlm_message_t *message = NULL;
1582         int i_end = 0;
1583
1584         while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1585         {
1586             i_end++;
1587         }
1588
1589         if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1590         {
1591             pf[i_end] = '\0';
1592             i_end++;
1593             if( pf[i_end] == '\n' ) i_end++;
1594         }
1595
1596         if( *pf && ExecuteCommand( vlm, pf, &message ) )
1597         {
1598             if( message )
1599             {
1600                 if( message->psz_value )
1601                     msg_Err( vlm, "Load error on line %d: %s: %s",
1602                              i_line, message->psz_name, message->psz_value );
1603                 vlm_MessageDelete( message );
1604             }
1605             return 1;
1606         }
1607         if( message ) vlm_MessageDelete( message );
1608
1609         pf += i_end;
1610         i_line++;
1611     }
1612
1613     return 0;
1614 }
1615
1616 static char *Save( vlm_t *vlm )
1617 {
1618     char *save = NULL;
1619     char psz_header[] = "\n"
1620                         "# VLC media player VLM command batch\n"
1621                         "# http://www.videolan.org/vlc/\n\n" ;
1622     char *p;
1623     int i,j;
1624     int i_length = strlen( psz_header );
1625
1626     for( i = 0; i < vlm->i_media; i++ )
1627     {
1628         vlm_media_sys_t *media = vlm->media[i];
1629         vlm_media_t *p_cfg = &media->cfg;
1630
1631         if( p_cfg->b_vod )
1632             i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1633         else
1634             i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1635
1636         if( p_cfg->b_enabled == true )
1637             i_length += strlen( "enabled" );
1638         else
1639             i_length += strlen( "disabled" );
1640
1641         if( !p_cfg->b_vod && p_cfg->broadcast.b_loop == true )
1642             i_length += strlen( " loop\n" );
1643         else
1644             i_length += strlen( "\n" );
1645
1646         for( j = 0; j < p_cfg->i_input; j++ )
1647             i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1648
1649         if( p_cfg->psz_output != NULL )
1650             i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1651
1652         for( j = 0; j < p_cfg->i_option; j++ )
1653             i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1654
1655         if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1656             i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1657     }
1658
1659     for( i = 0; i < vlm->i_schedule; i++ )
1660     {
1661         vlm_schedule_sys_t *schedule = vlm->schedule[i];
1662
1663         i_length += strlen( "new  schedule " ) + strlen( schedule->psz_name );
1664
1665         if( schedule->b_enabled == true )
1666         {
1667             i_length += strlen( "date //-:: enabled\n" ) + 14;
1668         }
1669         else
1670         {
1671             i_length += strlen( "date //-:: disabled\n" ) + 14;
1672         }
1673
1674
1675         if( schedule->i_period != 0 )
1676         {
1677             i_length += strlen( "setup  " ) + strlen( schedule->psz_name ) +
1678                 strlen( "period //-::\n" ) + 14;
1679         }
1680
1681         if( schedule->i_repeat >= 0 )
1682         {
1683             char buffer[12];
1684
1685             sprintf( buffer, "%d", schedule->i_repeat );
1686             i_length += strlen( "setup  repeat \n" ) +
1687                 strlen( schedule->psz_name ) + strlen( buffer );
1688         }
1689         else
1690         {
1691             i_length++;
1692         }
1693
1694         for( j = 0; j < schedule->i_command; j++ )
1695         {
1696             i_length += strlen( "setup  append \n" ) +
1697                 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1698         }
1699
1700     }
1701
1702     /* Don't forget the '\0' */
1703     i_length++;
1704     /* now we have the length of save */
1705
1706     p = save = malloc( i_length );
1707     if( !save ) return NULL;
1708     *save = '\0';
1709
1710     p += sprintf( p, "%s", psz_header );
1711
1712     /* finally we can write in it */
1713     for( i = 0; i < vlm->i_media; i++ )
1714     {
1715         vlm_media_sys_t *media = vlm->media[i];
1716         vlm_media_t *p_cfg = &media->cfg;
1717
1718         if( p_cfg->b_vod )
1719             p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1720         else
1721             p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1722
1723         if( p_cfg->b_enabled )
1724             p += sprintf( p, "enabled" );
1725         else
1726             p += sprintf( p, "disabled" );
1727
1728         if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1729             p += sprintf( p, " loop\n" );
1730         else
1731             p += sprintf( p, "\n" );
1732
1733         for( j = 0; j < p_cfg->i_input; j++ )
1734             p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1735
1736         if( p_cfg->psz_output )
1737             p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1738
1739         for( j = 0; j < p_cfg->i_option; j++ )
1740             p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1741
1742         if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1743             p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1744     }
1745
1746     /* and now, the schedule scripts */
1747     for( i = 0; i < vlm->i_schedule; i++ )
1748     {
1749         vlm_schedule_sys_t *schedule = vlm->schedule[i];
1750         struct tm date;
1751         time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1752
1753         localtime_r( &i_time, &date);
1754         p += sprintf( p, "new %s schedule ", schedule->psz_name);
1755
1756         if( schedule->b_enabled == true )
1757         {
1758             p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1759                           date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1760                           date.tm_hour, date.tm_min, date.tm_sec );
1761         }
1762         else
1763         {
1764             p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1765                           date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1766                           date.tm_hour, date.tm_min, date.tm_sec);
1767         }
1768
1769         if( schedule->i_period != 0 )
1770         {
1771             p += sprintf( p, "setup %s ", schedule->psz_name );
1772
1773             i_time = (time_t) ( schedule->i_period / 1000000 );
1774
1775             date.tm_sec = (int)( i_time % 60 );
1776             i_time = i_time / 60;
1777             date.tm_min = (int)( i_time % 60 );
1778             i_time = i_time / 60;
1779             date.tm_hour = (int)( i_time % 24 );
1780             i_time = i_time / 24;
1781             date.tm_mday = (int)( i_time % 30 );
1782             i_time = i_time / 30;
1783             /* okay, okay, months are not always 30 days long */
1784             date.tm_mon = (int)( i_time % 12 );
1785             i_time = i_time / 12;
1786             date.tm_year = (int)i_time;
1787
1788             p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1789                           date.tm_year, date.tm_mon, date.tm_mday,
1790                           date.tm_hour, date.tm_min, date.tm_sec);
1791         }
1792
1793         if( schedule->i_repeat >= 0 )
1794         {
1795             p += sprintf( p, "setup %s repeat %d\n",
1796                           schedule->psz_name, schedule->i_repeat );
1797         }
1798         else
1799         {
1800             p += sprintf( p, "\n" );
1801         }
1802
1803         for( j = 0; j < schedule->i_command; j++ )
1804         {
1805             p += sprintf( p, "setup %s append %s\n",
1806                           schedule->psz_name, schedule->command[j] );
1807         }
1808
1809     }
1810
1811     return save;
1812 }
1813
1814 #endif /* ENABLE_VLM */