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