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