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