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