vlm.c 78 KB
Newer Older
1
/*****************************************************************************
2
 * vlm.c: VLM interface plugin
3
 *****************************************************************************
Antoine Cellerier's avatar
Antoine Cellerier committed
4
 * Copyright (C) 2000-2005 the VideoLAN team
5
 * $Id$
6 7 8
 *
 * Authors: Simon Latapie <garf@videolan.org>
 *          Laurent Aimar <fenrir@videolan.org>
Gildas Bazin's avatar
Gildas Bazin committed
9
 *          Gildas Bazin <gbazin@videolan.org>
10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
29 30 31
#include <vlc/vlc.h>

#include <stdio.h>
32
#include <stdlib.h>                                      /* malloc(), free() */
33
#include <ctype.h>                                              /* tolower() */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
34
#include <assert.h>
35

36 37
#ifdef ENABLE_VLM

38

39 40
#ifdef HAVE_TIME_H
#   include <time.h>                                              /* ctime() */
41
#   include <sys/timeb.h>                                         /* ftime() */
42 43
#endif

Clément Stenac's avatar
Clément Stenac committed
44
#include <vlc_input.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
45
#include "input_internal.h"
Clément Stenac's avatar
Clément Stenac committed
46
#include <vlc_stream.h>
47 48
#include <vlc_vlm.h>
#include <vlc_vod.h>
Clément Stenac's avatar
Clément Stenac committed
49
#include <vlc_charset.h>
50

51 52 53
/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
54
static vlm_message_t *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, const char * );
55 56
static vlm_message_t *vlm_Help( vlm_t *, char * );

57
static vlm_media_instance_t *vlm_MediaInstanceSearch( vlm_t *, vlm_media_t *, const char * );
58

59
static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *, const char * );
60

61 62
static char *Save( vlm_t * );
static int Load( vlm_t *, char * );
63
static int ExecuteCommand( vlm_t *, const char *, vlm_message_t ** );
64
static int Manage( vlc_object_t * );
65

66 67 68 69 70 71
/*****************************************************************************
 * vlm_New:
 *****************************************************************************/
vlm_t *__vlm_New ( vlc_object_t *p_this )
{
    vlc_value_t lockval;
Clément Stenac's avatar
Clément Stenac committed
72
    vlm_t *p_vlm = NULL;
73
    char *psz_vlmconf;
74 75

    /* to be sure to avoid multiple creation */
76 77
    var_Create( p_this->p_libvlc_global, "vlm_mutex", VLC_VAR_MUTEX );
    var_Get( p_this->p_libvlc_global, "vlm_mutex", &lockval );
78 79
    vlc_mutex_lock( lockval.p_address );

Clément Stenac's avatar
Clément Stenac committed
80
    if( !(p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE )) )
81
    {
82
        msg_Info( p_this, "creating VLM" );
Clément Stenac's avatar
Clément Stenac committed
83
        if( ( p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM ) ) == NULL )
84 85 86 87 88
        {
            vlc_mutex_unlock( lockval.p_address );
            return NULL;
        }

89
        vlc_mutex_init( p_this->p_libvlc, &p_vlm->lock );
Clément Stenac's avatar
Clément Stenac committed
90 91 92 93 94
        p_vlm->i_media      = 0;
        p_vlm->media        = NULL;
        p_vlm->i_vod        = 0;
        p_vlm->i_schedule   = 0;
        p_vlm->schedule     = NULL;
95

Clément Stenac's avatar
Clément Stenac committed
96
        vlc_object_yield( p_vlm );
97
        vlc_object_attach( p_vlm, p_this->p_libvlc );
98 99 100
    }
    vlc_mutex_unlock( lockval.p_address );

Clément Stenac's avatar
Clément Stenac committed
101
    if( vlc_thread_create( p_vlm, "vlm thread",
102 103
                           Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
    {
Clément Stenac's avatar
Clément Stenac committed
104 105
        vlc_mutex_destroy( &p_vlm->lock );
        vlc_object_destroy( p_vlm );
106 107
        return NULL;
    }
108

109 110
    /* Try loading the vlm conf file given by --vlm-conf */
    psz_vlmconf = config_GetPsz( p_vlm, "vlm-conf" );
111 112 113 114 115 116

    if( psz_vlmconf && *psz_vlmconf )
    {
        vlm_message_t *p_message = NULL;
        char *psz_buffer = NULL;

117
        msg_Dbg( p_this, "loading VLM configuration" );
118 119 120 121 122
        asprintf(&psz_buffer, "load %s", psz_vlmconf );
        if( psz_buffer )
        {
            msg_Dbg( p_this, psz_buffer);
            if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) ){
123
                msg_Warn( p_this, "error while loading the configuration file" );
124
            }
125
            vlm_MessageDelete(p_message);
126 127 128 129 130 131
            free(psz_buffer);
        }
   }
   free(psz_vlmconf);

   return p_vlm;
132 133 134 135 136
}

/*****************************************************************************
 * vlm_Delete:
 *****************************************************************************/
Clément Stenac's avatar
Clément Stenac committed
137
void vlm_Delete( vlm_t *p_vlm )
138 139 140
{
    vlc_value_t lockval;

141
    var_Get( p_vlm->p_libvlc_global, "vlm_mutex", &lockval );
142 143
    vlc_mutex_lock( lockval.p_address );

Clément Stenac's avatar
Clément Stenac committed
144
    vlc_object_release( p_vlm );
145

Clément Stenac's avatar
Clément Stenac committed
146
    if( p_vlm->i_refcount > 0 )
147 148 149 150 151
    {
        vlc_mutex_unlock( lockval.p_address );
        return;
    }

Clément Stenac's avatar
Clément Stenac committed
152 153
    p_vlm->b_die = VLC_TRUE;
    vlc_thread_join( p_vlm );
154

Clément Stenac's avatar
Clément Stenac committed
155
    vlc_mutex_destroy( &p_vlm->lock );
156

Clément Stenac's avatar
Clément Stenac committed
157
    while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
158
    FREENULL( p_vlm->media );
159

Clément Stenac's avatar
Clément Stenac committed
160 161
    while( p_vlm->i_schedule ) vlm_ScheduleDelete( p_vlm,
                                                   p_vlm->schedule[0], NULL );
162
    FREENULL( p_vlm->schedule );
163

Clément Stenac's avatar
Clément Stenac committed
164 165
    vlc_object_detach( p_vlm );
    vlc_object_destroy( p_vlm );
166 167 168 169 170 171
    vlc_mutex_unlock( lockval.p_address );
}

/*****************************************************************************
 * vlm_ExecuteCommand:
 *****************************************************************************/
172
int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
Clément Stenac's avatar
Clément Stenac committed
173
                        vlm_message_t **pp_message)
174
{
Clément Stenac's avatar
Clément Stenac committed
175
    int i_result;
176

Clément Stenac's avatar
Clément Stenac committed
177 178 179
    vlc_mutex_lock( &p_vlm->lock );
    i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
    vlc_mutex_unlock( &p_vlm->lock );
180

Clément Stenac's avatar
Clément Stenac committed
181
    return i_result;
182 183
}

184 185 186
/*****************************************************************************
 * vlm_Save:
 *****************************************************************************/
187
int vlm_Save( vlm_t *p_vlm, const char *psz_file )
188 189 190 191 192 193
{
    FILE *file;
    char *psz_save;

    if( !p_vlm || !psz_file ) return 1;

194
    file = utf8_fopen( psz_file, "wt" );
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    if( file == NULL ) return 1;

    psz_save = Save( p_vlm );
    if( psz_save == NULL )
    {
        fclose( file );
        return 1;
    }
    fwrite( psz_save, strlen( psz_save ), 1, file );
    fclose( file );
    free( psz_save );

    return 0;
}

/*****************************************************************************
 * vlm_Load:
 *****************************************************************************/
213
int vlm_Load( vlm_t *p_vlm, const char *psz_file )
214
{
215
    stream_t *p_stream;
216 217 218 219 220
    int64_t i_size;
    char *psz_buffer;

    if( !p_vlm || !psz_file ) return 1;

221 222
    p_stream = stream_UrlNew( p_vlm, psz_file );
    if( p_stream == NULL ) return 1;
223

224
    if( stream_Seek( p_stream, 0 ) != 0 )
225
    {
226
        stream_Delete( p_stream );
227 228
        return 2;
    }
229

230 231
    i_size = stream_Size( p_stream );

232 233 234
    psz_buffer = malloc( i_size + 1 );
    if( !psz_buffer )
    {
235
        stream_Delete( p_stream );
236 237
        return 2;
    }
238 239

    stream_Read( p_stream, psz_buffer, i_size );
240
    psz_buffer[ i_size ] = '\0';
241 242 243

    stream_Delete( p_stream );

244 245 246 247 248 249
    if( Load( p_vlm, psz_buffer ) )
    {
        free( psz_buffer );
        return 3;
    }

250
    free( psz_buffer );
251 252 253 254

    return 0;
}

255 256

static const char quotes[] = "\"'";
257 258 259 260 261 262
/**
 * FindCommandEnd: look for the end of a possibly quoted string
 * @return NULL on mal-formatted string,
 * pointer paste the last character otherwise.
 */
static const char *FindCommandEnd (const char *psz_sent)
263
{
264 265
    const char quote = strchr (quotes, psz_sent[0]) ? psz_sent[0] : 0;
    char c;
Antoine Cellerier's avatar
Antoine Cellerier committed
266

267
    while ((c = *psz_sent) != '\0')
268
    {
269
        if ((quote == '"') && (c == '\\'))
Gildas Bazin's avatar
Gildas Bazin committed
270
        {
271 272
            if (*psz_sent == '\0')
                return NULL; // cannot escape "nothing"
Gildas Bazin's avatar
Gildas Bazin committed
273

274
            psz_sent++; // skips escaped character
Gildas Bazin's avatar
Gildas Bazin committed
275
        }
276 277 278 279 280 281
        else
        if (c == quote) // non-escaped matching quote
            return psz_sent + 1;
        else
        if (isblank (c)) // non-escaped blank
            return psz_sent;
282

Gildas Bazin's avatar
Gildas Bazin committed
283
        psz_sent++;
284
    }
285

286 287 288
    // error (NULL) if we could not find a matching quote
    return quote ? NULL : psz_sent;
}
289

Gildas Bazin's avatar
Gildas Bazin committed
290

291
/**
292
 * Unescape a nul-terminated string.
293 294 295 296 297 298 299 300 301
 * Note that in and out can be identical.
 *
 * @param out output buffer (at least <strlen (in) + 1> characters long)
 * @param in nul-terminated string to be unescaped
 *
 * @return 0 on success, -1 on error.
 */
static int Unescape (char *out, const char *in)
{
302
    const char quote = strchr (quotes, in[0]) ? in[0] : 0;
Gildas Bazin's avatar
Gildas Bazin committed
303

304 305 306 307
    if (quote)
        in++; // skips opening quote

    for (;;)
308
    {
309 310 311 312 313 314
        char c = *in++;

        if ((c == '\0') || (c == quote))
            break;

        if ((quote == '"') && (c == '\\'))
Gildas Bazin's avatar
Gildas Bazin committed
315
        {
316
            switch (c = *in++)
317
            {
318 319 320 321 322 323 324 325 326 327 328
                case 'n':
                    *out++ = '\n';
                    continue;

                case 't':
                    *out++ = '\t';
                    continue;

                case 'r':
                    *out++ = '\r';
                    continue;
329
            }
Antoine Cellerier's avatar
Antoine Cellerier committed
330

331
            // Only allow printable ASCII characters
332
            // (in particular, no nul nor extended characters)
333 334
            if (c < 32)
                return -1;
335
        }
336
        *out++ = c;
337
    }
338 339 340

    *out = '\0';
    return 0;
341 342
}

343

Gildas Bazin's avatar
Gildas Bazin committed
344 345 346 347 348
/*****************************************************************************
 * ExecuteCommand: The main state machine
 *****************************************************************************
 * Execute a command which ends with '\0' (string)
 *****************************************************************************/
349
static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
Clément Stenac's avatar
Clément Stenac committed
350
                           vlm_message_t **pp_message )
351
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
352 353 354
    size_t i_command = 0;
    char buf[strlen (psz_command) + 1], *psz_buf = buf;
    char *ppsz_command[sizeof (buf) / 2];
Clément Stenac's avatar
Clément Stenac committed
355
    vlm_message_t *p_message = NULL;
356 357

    /* First, parse the line and cut it */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
358
    while( *psz_command != '\0' )
359
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
360 361 362
        const char *psz_temp;

        if(isblank (*psz_command))
363
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
364 365
            psz_command++;
            continue;
366
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
367 368 369

        /* support for comments */
        if( i_command == 0 && *psz_command == '#')
370
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
371 372 373
            p_message = vlm_MessageNew( "", NULL );
            goto success;
        }
374

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
375 376 377 378 379 380 381
        psz_temp = FindCommandEnd( psz_command );

        if( psz_temp == NULL )
        {
            p_message = vlm_MessageNew( "Incomplete command", psz_command );
            goto error;
        }
382

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
383
        assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
384

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
385 386 387
        ppsz_command[i_command] = psz_buf;
        memcpy (psz_buf, psz_command, psz_temp - psz_command);
        psz_buf[psz_temp - psz_command] = '\0';
388

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
389
        Unescape (psz_buf, psz_buf);
Antoine Cellerier's avatar
Antoine Cellerier committed
390

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
391 392 393
        i_command++;
        psz_buf += psz_temp - psz_command + 1;
        psz_command = psz_temp;
394

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
395
        assert (buf + sizeof (buf) >= psz_buf);
396 397
    }

Gildas Bazin's avatar
Gildas Bazin committed
398 399 400
    /*
     * And then Interpret it
     */
401 402 403

    if( i_command == 0 )
    {
Clément Stenac's avatar
Clément Stenac committed
404
        p_message = vlm_MessageNew( "", NULL );
Gildas Bazin's avatar
Gildas Bazin committed
405
        goto success;
406 407
    }

408
    if( !strcmp(ppsz_command[0], "new") )
409
    {
Gildas Bazin's avatar
Gildas Bazin committed
410
        int i_type;
411

Gildas Bazin's avatar
Gildas Bazin committed
412 413
        /* Check the number of arguments */
        if( i_command < 3 ) goto syntax_error;
414

Gildas Bazin's avatar
Gildas Bazin committed
415
        /* Get type */
416
        if( !strcmp(ppsz_command[2], "vod") )
Gildas Bazin's avatar
Gildas Bazin committed
417 418 419
        {
            i_type = VOD_TYPE;
        }
420
        else if( !strcmp(ppsz_command[2], "broadcast") )
Gildas Bazin's avatar
Gildas Bazin committed
421 422 423
        {
            i_type = BROADCAST_TYPE;
        }
424
        else if( !strcmp(ppsz_command[2], "schedule") )
Gildas Bazin's avatar
Gildas Bazin committed
425 426 427 428 429
        {
            i_type = SCHEDULE_TYPE;
        }
        else
        {
430 431 432
            p_message =
                vlm_MessageNew( "new", "%s: Choose between vod, "
                                "broadcast or schedule", ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
433 434
            goto error;
        }
435

Gildas Bazin's avatar
Gildas Bazin committed
436
        /* Check for forbidden media names */
437 438 439
        if( !strcmp(ppsz_command[1], "all") ||
            !strcmp(ppsz_command[1], "media") ||
            !strcmp(ppsz_command[1], "schedule") )
Gildas Bazin's avatar
Gildas Bazin committed
440
        {
Clément Stenac's avatar
Clément Stenac committed
441
            p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
442
                                        "\"schedule\" are reserved names" );
Gildas Bazin's avatar
Gildas Bazin committed
443 444
            goto error;
        }
445

Gildas Bazin's avatar
Gildas Bazin committed
446
        /* Check the name is not already in use */
Clément Stenac's avatar
Clément Stenac committed
447 448
        if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
            vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
Gildas Bazin's avatar
Gildas Bazin committed
449
        {
Clément Stenac's avatar
Clément Stenac committed
450 451
            p_message = vlm_MessageNew( "new", "%s: Name already in use",
                                        ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
452 453
            goto error;
        }
454

Gildas Bazin's avatar
Gildas Bazin committed
455 456 457
        /* Schedule */
        if( i_type == SCHEDULE_TYPE )
        {
Clément Stenac's avatar
Clément Stenac committed
458 459 460
            vlm_schedule_t *p_schedule;
            p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
            if( !p_schedule )
461
            {
Clément Stenac's avatar
Clément Stenac committed
462
                p_message = vlm_MessageNew( "new", "could not create schedule" );
Gildas Bazin's avatar
Gildas Bazin committed
463
                goto error;
464
            }
Gildas Bazin's avatar
Gildas Bazin committed
465
        }
466

Gildas Bazin's avatar
Gildas Bazin committed
467 468 469
        /* Media */
        else
        {
Clément Stenac's avatar
Clément Stenac committed
470 471 472
            vlm_media_t *p_media;
            p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
            if( !p_media )
473
            {
Clément Stenac's avatar
Clément Stenac committed
474
                p_message = vlm_MessageNew( "new", "could not create media" );
Gildas Bazin's avatar
Gildas Bazin committed
475
                goto error;
476 477
            }
        }
Gildas Bazin's avatar
Gildas Bazin committed
478 479

        if( i_command <= 3 )
480
        {
Clément Stenac's avatar
Clément Stenac committed
481
            p_message = vlm_MessageNew( "new", NULL );
Gildas Bazin's avatar
Gildas Bazin committed
482
            goto success;
483
        }
Gildas Bazin's avatar
Gildas Bazin committed
484 485 486 487

        /* Properties will be dealt with later on */
    }

488
    else if( !strcmp(ppsz_command[0], "setup") )
Gildas Bazin's avatar
Gildas Bazin committed
489 490 491 492
    {
        if( i_command < 2 ) goto syntax_error;

        /* Properties will be dealt with later on */
493
    }
Clément Stenac's avatar
Clément Stenac committed
494

495
    else if( !strcmp(ppsz_command[0], "del") )
496
    {
Clément Stenac's avatar
Clément Stenac committed
497 498
        vlm_media_t *p_media;
        vlm_schedule_t *p_schedule;
499

Gildas Bazin's avatar
Gildas Bazin committed
500
        if( i_command < 2 ) goto syntax_error;
501

Clément Stenac's avatar
Clément Stenac committed
502 503
        p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
        p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
504

Clément Stenac's avatar
Clément Stenac committed
505
        if( p_schedule != NULL )
Gildas Bazin's avatar
Gildas Bazin committed
506
        {
Clément Stenac's avatar
Clément Stenac committed
507
            vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
Gildas Bazin's avatar
Gildas Bazin committed
508
        }
Clément Stenac's avatar
Clément Stenac committed
509
        else if( p_media != NULL )
Gildas Bazin's avatar
Gildas Bazin committed
510
        {
Clément Stenac's avatar
Clément Stenac committed
511
            vlm_MediaDelete( p_vlm, p_media, NULL );
Gildas Bazin's avatar
Gildas Bazin committed
512
        }
513
        else if( !strcmp(ppsz_command[1], "media") )
Gildas Bazin's avatar
Gildas Bazin committed
514
        {
Clément Stenac's avatar
Clément Stenac committed
515 516
            while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
                                                     NULL );
Gildas Bazin's avatar
Gildas Bazin committed
517
        }
518
        else if( !strcmp(ppsz_command[1], "schedule") )
Gildas Bazin's avatar
Gildas Bazin committed
519
        {
Clément Stenac's avatar
Clément Stenac committed
520 521
            while( p_vlm->i_schedule )
                vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
Gildas Bazin's avatar
Gildas Bazin committed
522
        }
523
        else if( !strcmp(ppsz_command[1], "all") )
Gildas Bazin's avatar
Gildas Bazin committed
524
        {
Clément Stenac's avatar
Clément Stenac committed
525 526
            while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
                                                     NULL );
Gildas Bazin's avatar
Gildas Bazin committed
527

Clément Stenac's avatar
Clément Stenac committed
528 529
            while( p_vlm->i_schedule )
                vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
530 531 532
        }
        else
        {
Clément Stenac's avatar
Clément Stenac committed
533 534
            p_message = vlm_MessageNew( "del", "%s: media unknown",
                                      ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
535
            goto error;
536
        }
Gildas Bazin's avatar
Gildas Bazin committed
537

Clément Stenac's avatar
Clément Stenac committed
538
        p_message = vlm_MessageNew( "del", NULL );
Gildas Bazin's avatar
Gildas Bazin committed
539
        goto success;
540
    }
Gildas Bazin's avatar
Gildas Bazin committed
541

542
    else if( !strcmp(ppsz_command[0], "show") )
543
    {
Clément Stenac's avatar
Clément Stenac committed
544 545
        vlm_media_t *p_media;
        vlm_schedule_t *p_schedule;
Gildas Bazin's avatar
Gildas Bazin committed
546

547 548
        if( i_command == 1 )
        {
Clément Stenac's avatar
Clément Stenac committed
549
            p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
Gildas Bazin's avatar
Gildas Bazin committed
550
            goto success;
551
        }
Gildas Bazin's avatar
Gildas Bazin committed
552
        else if( i_command > 2 ) goto syntax_error;
553

Clément Stenac's avatar
Clément Stenac committed
554 555
        p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
        p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
556

Clément Stenac's avatar
Clément Stenac committed
557
        if( p_schedule != NULL )
558
        {
Clément Stenac's avatar
Clément Stenac committed
559
            p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
560
        }
Clément Stenac's avatar
Clément Stenac committed
561
        else if( p_media != NULL )
562
        {
Clément Stenac's avatar
Clément Stenac committed
563
            p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
564 565 566
        }
        else
        {
Clément Stenac's avatar
Clément Stenac committed
567
            p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
568
        }
Gildas Bazin's avatar
Gildas Bazin committed
569 570

        goto success;
571 572
    }

573
    else if( !strcmp(ppsz_command[0], "help") )
Gildas Bazin's avatar
Gildas Bazin committed
574 575
    {
        if( i_command != 1 ) goto syntax_error;
576

Clément Stenac's avatar
Clément Stenac committed
577
        p_message = vlm_Help( p_vlm, NULL );
Gildas Bazin's avatar
Gildas Bazin committed
578 579
        goto success;
    }
580

581
    else if( !strcmp(ppsz_command[0], "control") )
Gildas Bazin's avatar
Gildas Bazin committed
582
    {
Clément Stenac's avatar
Clément Stenac committed
583
        vlm_media_t *p_media;
584

Gildas Bazin's avatar
Gildas Bazin committed
585
        if( i_command < 3 ) goto syntax_error;
586

Clément Stenac's avatar
Clément Stenac committed
587
        if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
588
        {
Clément Stenac's avatar
Clément Stenac committed
589 590
            p_message = vlm_MessageNew( "control", "%s: media unknown",
                                      ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
591
            goto error;
592
        }
Gildas Bazin's avatar
Gildas Bazin committed
593
        else
594
        {
Gildas Bazin's avatar
Gildas Bazin committed
595
            char *psz_command, *psz_arg = 0, *psz_instance = 0;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
596
            size_t i_index = 2;
597

598 599 600
            if( strcmp( ppsz_command[2], "play" ) &&
                strcmp( ppsz_command[2], "stop" ) &&
                strcmp( ppsz_command[2], "pause" ) &&
601
                strcmp( ppsz_command[2], "seek" ) )
602
            {
Gildas Bazin's avatar
Gildas Bazin committed
603
                i_index++;
Clément Stenac's avatar
Clément Stenac committed
604
                psz_instance = ppsz_command[2];
Gildas Bazin's avatar
Gildas Bazin committed
605 606

                if( i_command < 4 ) goto syntax_error;
607 608 609 610

                if( strcmp( ppsz_command[3], "play" ) &&
                    strcmp( ppsz_command[3], "stop" ) &&
                    strcmp( ppsz_command[3], "pause" ) &&
611
                    strcmp( ppsz_command[3], "seek" ) )
612
                    goto syntax_error;
613 614
            }

Clément Stenac's avatar
Clément Stenac committed
615
            psz_command = ppsz_command[i_index];
616

Clément Stenac's avatar
Clément Stenac committed
617
            if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
Gildas Bazin's avatar
Gildas Bazin committed
618

Clément Stenac's avatar
Clément Stenac committed
619 620 621
            vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
                             psz_arg );
            p_message = vlm_MessageNew( "control", NULL );
Gildas Bazin's avatar
Gildas Bazin committed
622
            goto success;
623 624
        }
    }
Gildas Bazin's avatar
Gildas Bazin committed
625

626
    else if( !strcmp(ppsz_command[0], "save") )
627
    {
Gildas Bazin's avatar
Gildas Bazin committed
628
        if( i_command != 2 ) goto syntax_error;
629

630
        if( vlm_Save( p_vlm, ppsz_command[1] ) )
Gildas Bazin's avatar
Gildas Bazin committed
631
        {
Clément Stenac's avatar
Clément Stenac committed
632
            p_message = vlm_MessageNew( "save", "Unable to save to file" );
Gildas Bazin's avatar
Gildas Bazin committed
633
            goto error;
634 635 636
        }
        else
        {
Clément Stenac's avatar
Clément Stenac committed
637
            p_message = vlm_MessageNew( "save", NULL );
Gildas Bazin's avatar
Gildas Bazin committed
638
            goto success;
639 640
        }
    }
Gildas Bazin's avatar
Gildas Bazin committed
641

642 643 644 645 646 647 648 649 650 651 652 653
    else if( !strcmp(ppsz_command[0], "export" ) )
    {
        char *psz_buf;

        if( i_command != 1 ) goto syntax_error;

        p_message = vlm_MessageNew( "export", psz_buf = Save( p_vlm ) );
        free( psz_buf );

        goto success;
    }

654
    else if( !strcmp(ppsz_command[0], "load") )
655
    {
Gildas Bazin's avatar
Gildas Bazin committed
656
        if( i_command != 2 ) goto syntax_error;
657

658
        switch( vlm_Load( p_vlm, ppsz_command[1] ) )
Gildas Bazin's avatar
Gildas Bazin committed
659
        {
660 661 662 663
            case 0:
                p_message = vlm_MessageNew( "load", NULL );
                goto success;
            case 2:
664
                p_message = vlm_MessageNew( "load", "Read file error" );
Gildas Bazin's avatar
Gildas Bazin committed
665
                goto error;
666 667
            case 3:
                p_message =
668
                    vlm_MessageNew( "load", "Error while loading file" );
669 670 671 672 673
                goto error;
            default:
                p_message =
                    vlm_MessageNew( "load", "Unable to load from file" );
                goto error;
Gildas Bazin's avatar
Gildas Bazin committed
674 675 676 677 678
        }
    }

    else
    {
Clément Stenac's avatar
Clément Stenac committed
679
        p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
Gildas Bazin's avatar
Gildas Bazin committed
680 681 682 683
        goto error;
    }

    /* Common code between "new" and "setup" */
684 685
    if( !strcmp(ppsz_command[0], "new") ||
        !strcmp(ppsz_command[0], "setup") )
Gildas Bazin's avatar
Gildas Bazin committed
686
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
687
        size_t i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
Clément Stenac's avatar
Clément Stenac committed
688 689
        vlm_media_t *p_media;
        vlm_schedule_t *p_schedule;
Gildas Bazin's avatar
Gildas Bazin committed
690 691 692

        if( i_command < i_command_start ) goto syntax_error;

Clément Stenac's avatar
Clément Stenac committed
693 694
        p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
        p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
695

Clément Stenac's avatar
Clément Stenac committed
696
        if( !p_media && !p_schedule )
Gildas Bazin's avatar
Gildas Bazin committed
697
        {
Clément Stenac's avatar
Clément Stenac committed
698 699
            p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
                                        ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
700 701 702
            goto error;
        }

Clément Stenac's avatar
Clément Stenac committed
703
        if( p_schedule != NULL )
Gildas Bazin's avatar
Gildas Bazin committed
704
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
705 706
            size_t i;

Gildas Bazin's avatar
Gildas Bazin committed
707 708
            for( i = i_command_start ; i < i_command ; i++ )
            {
709 710
                if( !strcmp( ppsz_command[i], "enabled" ) ||
                    !strcmp( ppsz_command[i], "disabled" ) )
Gildas Bazin's avatar
Gildas Bazin committed
711
                {
Clément Stenac's avatar
Clément Stenac committed
712
                    vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
Gildas Bazin's avatar
Gildas Bazin committed
713
                }
714

Clément Stenac's avatar
Clément Stenac committed
715
                /* Beware: everything behind append is considered as
Gildas Bazin's avatar
Gildas Bazin committed
716
                 * command line */
717
                else if( !strcmp( ppsz_command[i], "append" ) )
718
                {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
719 720
                    size_t j;

Gildas Bazin's avatar
Gildas Bazin committed
721 722 723
                    if( ++i >= i_command ) break;

                    for( j = i + 1; j < i_command; j++ )
724
                    {
Clément Stenac's avatar
Clément Stenac committed
725 726 727 728 729
                        ppsz_command[i] =
                            realloc( ppsz_command[i], strlen(ppsz_command[i]) +
                                     strlen(ppsz_command[j]) + 1 + 1 );
                        strcat( ppsz_command[i], " " );
                        strcat( ppsz_command[i], ppsz_command[j] );
730
                    }
Gildas Bazin's avatar
Gildas Bazin committed
731

Clément Stenac's avatar
Clément Stenac committed
732 733
                    vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
                                       ppsz_command[i] );
Gildas Bazin's avatar
Gildas Bazin committed
734
                    break;
735 736 737
                }
                else
                {
738
                    if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
Gildas Bazin's avatar
Gildas Bazin committed
739
                    {
Clément Stenac's avatar
Clément Stenac committed
740
                        vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
741 742 743
                        p_message =
                            vlm_MessageNew( ppsz_command[0],
                                            "Wrong properties syntax" );
Gildas Bazin's avatar
Gildas Bazin committed
744 745 746 747
                        goto error;
                    }
                    else if( i + 1 >= i_command )
                    {
748 749 750
                        p_message =
                            vlm_MessageNew( ppsz_command[0],
                                            "Wrong properties syntax" );
Gildas Bazin's avatar
Gildas Bazin committed
751 752 753
                        goto error;
                    }

Clément Stenac's avatar
Clément Stenac committed
754 755
                    vlm_ScheduleSetup( p_schedule, ppsz_command[i],
                                       ppsz_command[i+1] );
Gildas Bazin's avatar
Gildas Bazin committed
756
                    i++;
757 758 759
                }
            }
        }
Gildas Bazin's avatar
Gildas Bazin committed
760

Clément Stenac's avatar
Clément Stenac committed
761
        else if( p_media != NULL )
762
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
763 764
            size_t i;

Gildas Bazin's avatar
Gildas Bazin committed
765 766
            for( i = i_command_start ; i < i_command ; i++ )
            {
767 768
                if( !strcmp( ppsz_command[i], "enabled" ) ||
                    !strcmp( ppsz_command[i], "disabled" ) )
Gildas Bazin's avatar
Gildas Bazin committed
769
                {
Clément Stenac's avatar
Clément Stenac committed
770
                    vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
Gildas Bazin's avatar
Gildas Bazin committed
771
                }
772 773
                else if( i + 1 >= i_command &&
                         !strcmp( ppsz_command[i], "mux") )
774
                {
Clément Stenac's avatar
Clément Stenac committed
775
                    if( p_media->i_type != VOD_TYPE )
776
                    {
Clément Stenac's avatar
Clément Stenac committed
777
                        p_message = vlm_MessageNew( ppsz_command[0],
778 779 780 781
                                  "mux only available for broadcast" );
                    }
                    else
                    {
Clément Stenac's avatar
Clément Stenac committed
782 783
                        vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
                                        ppsz_command[i+1] );
784
                        i++;
785 786
                    }
                }
787 788
                else if( !strcmp( ppsz_command[i], "loop" ) ||
                         !strcmp( ppsz_command[i], "unloop" ) )
Gildas Bazin's avatar
Gildas Bazin committed
789
                {
Clément Stenac's avatar
Clément Stenac committed
790
                    if( p_media->i_type != BROADCAST_TYPE )
Gildas Bazin's avatar
Gildas Bazin committed
791
                    {
Clément Stenac's avatar
Clément Stenac committed
792
                        p_message = vlm_MessageNew( ppsz_command[0],
Gildas Bazin's avatar
Gildas Bazin committed
793 794 795 796
                                  "loop only available for broadcast" );
                    }
                    else
                    {
Clément Stenac's avatar
Clément Stenac committed
797
                        vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
Gildas Bazin's avatar
Gildas Bazin committed
798 799 800 801
                    }
                }
                else
                {
802
                    if( ( (i + 1) >= i_command ) &&
803
                        !strcmp(ppsz_command[0], "new") )