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