vlm.c 77.9 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 );
90 91 92 93
        TAB_INIT( p_vlm->i_media, p_vlm->media );
        TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
        p_vlm->i_vod = 0;
        p_vlm->vod = NULL;
94

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

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

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

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

116
        msg_Dbg( p_this, "loading VLM configuration" );
117 118 119 120 121
        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 ) ){
122
                msg_Warn( p_this, "error while loading the configuration file" );
123
            }
124
            vlm_MessageDelete(p_message);
125 126 127 128 129 130
            free(psz_buffer);
        }
   }
   free(psz_vlmconf);

   return p_vlm;
131 132 133 134 135
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

193
    file = utf8_fopen( psz_file, "wt" );
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    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:
 *****************************************************************************/
212
int vlm_Load( vlm_t *p_vlm, const char *psz_file )
213
{
214
    stream_t *p_stream;
215 216 217 218 219
    int64_t i_size;
    char *psz_buffer;

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

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

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

229 230
    i_size = stream_Size( p_stream );

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

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

    stream_Delete( p_stream );

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

249
    free( psz_buffer );
250 251 252 253

    return 0;
}

254 255

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

265
    while ((c = *psz_sent) != '\0')
266
    {
267
        if (!quote)
Gildas Bazin's avatar
Gildas Bazin committed
268
        {
269 270 271 272 273 274 275 276 277 278 279 280
            if (strchr(quotes,c))   // opening quote
                quote = c;
            else
            if (isspace(c))         // non-escaped space
                return psz_sent;
            else
            if( c == '\\' )
            {
                psz_sent++;         // skip escaped character
                if (*psz_sent == '\0')
                    return psz_sent;
            }
Gildas Bazin's avatar
Gildas Bazin committed
281
        }
282
        else
283 284 285 286 287 288 289 290 291 292 293
        {
            if (c == quote)         // non-escaped matching quote
                quote = 0;
            else
            if ((quote == '"') && (c == '\\'))
            {
                psz_sent++;         // skip escaped character
                if (*psz_sent == '\0')
                    return NULL;    // error, closing quote missing
            }
        }
294

Gildas Bazin's avatar
Gildas Bazin committed
295
        psz_sent++;
296
    }
297

298 299 300
    // error (NULL) if we could not find a matching quote
    return quote ? NULL : psz_sent;
}
301

Gildas Bazin's avatar
Gildas Bazin committed
302

303
/**
304
 * Unescape a nul-terminated string.
305 306 307 308 309 310 311 312 313
 * 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)
{
314
    char c, quote = 0;
315

316
    while ((c = *in++) != '\0')
317
    {
318
        if (!quote)
Gildas Bazin's avatar
Gildas Bazin committed
319
        {
320
            if (strchr(quotes,c))   // opening quote
321
            {
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
                quote = c;
                continue;
            }
            else
            if( c == '\\' )
            {
                switch (c = *in++)
                {
                    case '"':
                    case '\'':
                    case '\\':
                        *out++ = c;
                        continue;

                    case '\0':
                        *out = '\0';
                        return 0;
                }
                if (isspace(c))
                {
                    *out++ = c;
343
                    continue;
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
                }
                /* None of the special cases - copy the backslash */
                *out++ = '\\';
            }
        }
        else
        {
            if (c == quote)         // non-escaped matching quote
            {
                quote = 0;
                continue;
            }
            if ((quote == '"') && (c == '\\'))
            {
                switch (c = *in++)
                {
                    case '"':
                    case '\\':
                        *out++ = c;
                        continue;

                    case '\0':   // should never happen
                        *out = '\0';
                        return -1;
                }
                /* None of the special cases - copy the backslash */
                *out++ = '\\';
371 372
            }
        }
373
        *out++ = c;
374
    }
375 376 377

    *out = '\0';
    return 0;
378 379
}

380

Gildas Bazin's avatar
Gildas Bazin committed
381 382 383 384 385
/*****************************************************************************
 * ExecuteCommand: The main state machine
 *****************************************************************************
 * Execute a command which ends with '\0' (string)
 *****************************************************************************/
386
static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
Clément Stenac's avatar
Clément Stenac committed
387
                           vlm_message_t **pp_message )
388
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
389 390 391
    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
392
    vlm_message_t *p_message = NULL;
393 394

    /* First, parse the line and cut it */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
395
    while( *psz_command != '\0' )
396
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
397 398
        const char *psz_temp;

399
        if(isspace (*psz_command))
400
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
401 402
            psz_command++;
            continue;
403
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
404 405 406

        /* support for comments */
        if( i_command == 0 && *psz_command == '#')
407
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
408 409 410
            p_message = vlm_MessageNew( "", NULL );
            goto success;
        }
411

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
412 413 414 415 416 417 418
        psz_temp = FindCommandEnd( psz_command );

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

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
422 423 424
        ppsz_command[i_command] = psz_buf;
        memcpy (psz_buf, psz_command, psz_temp - psz_command);
        psz_buf[psz_temp - psz_command] = '\0';
425

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
428 429 430
        i_command++;
        psz_buf += psz_temp - psz_command + 1;
        psz_command = psz_temp;
431

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
432
        assert (buf + sizeof (buf) >= psz_buf);
433 434
    }

Gildas Bazin's avatar
Gildas Bazin committed
435 436 437
    /*
     * And then Interpret it
     */
438 439 440

    if( i_command == 0 )
    {
Clément Stenac's avatar
Clément Stenac committed
441
        p_message = vlm_MessageNew( "", NULL );
Gildas Bazin's avatar
Gildas Bazin committed
442
        goto success;
443 444
    }

445
    if( !strcmp(ppsz_command[0], "new") )
446
    {
Gildas Bazin's avatar
Gildas Bazin committed
447
        int i_type;
448

Gildas Bazin's avatar
Gildas Bazin committed
449 450
        /* Check the number of arguments */
        if( i_command < 3 ) goto syntax_error;
451

Gildas Bazin's avatar
Gildas Bazin committed
452
        /* Get type */
453
        if( !strcmp(ppsz_command[2], "vod") )
Gildas Bazin's avatar
Gildas Bazin committed
454 455 456
        {
            i_type = VOD_TYPE;
        }
457
        else if( !strcmp(ppsz_command[2], "broadcast") )
Gildas Bazin's avatar
Gildas Bazin committed
458 459 460
        {
            i_type = BROADCAST_TYPE;
        }
461
        else if( !strcmp(ppsz_command[2], "schedule") )
Gildas Bazin's avatar
Gildas Bazin committed
462 463 464 465 466
        {
            i_type = SCHEDULE_TYPE;
        }
        else
        {
467 468 469
            p_message =
                vlm_MessageNew( "new", "%s: Choose between vod, "
                                "broadcast or schedule", ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
470 471
            goto error;
        }
472

Gildas Bazin's avatar
Gildas Bazin committed
473
        /* Check for forbidden media names */
474 475 476
        if( !strcmp(ppsz_command[1], "all") ||
            !strcmp(ppsz_command[1], "media") ||
            !strcmp(ppsz_command[1], "schedule") )
Gildas Bazin's avatar
Gildas Bazin committed
477
        {
Clément Stenac's avatar
Clément Stenac committed
478
            p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
479
                                        "\"schedule\" are reserved names" );
Gildas Bazin's avatar
Gildas Bazin committed
480 481
            goto error;
        }
482

Gildas Bazin's avatar
Gildas Bazin committed
483
        /* Check the name is not already in use */
Clément Stenac's avatar
Clément Stenac committed
484 485
        if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
            vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
Gildas Bazin's avatar
Gildas Bazin committed
486
        {
Clément Stenac's avatar
Clément Stenac committed
487 488
            p_message = vlm_MessageNew( "new", "%s: Name already in use",
                                        ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
489 490
            goto error;
        }
491

Gildas Bazin's avatar
Gildas Bazin committed
492 493 494
        /* Schedule */
        if( i_type == SCHEDULE_TYPE )
        {
Clément Stenac's avatar
Clément Stenac committed
495 496 497
            vlm_schedule_t *p_schedule;
            p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
            if( !p_schedule )
498
            {
Clément Stenac's avatar
Clément Stenac committed
499
                p_message = vlm_MessageNew( "new", "could not create schedule" );
Gildas Bazin's avatar
Gildas Bazin committed
500
                goto error;
501
            }
Gildas Bazin's avatar
Gildas Bazin committed
502
        }
503

Gildas Bazin's avatar
Gildas Bazin committed
504 505 506
        /* Media */
        else
        {
Clément Stenac's avatar
Clément Stenac committed
507 508 509
            vlm_media_t *p_media;
            p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
            if( !p_media )
510
            {
Clément Stenac's avatar
Clément Stenac committed
511
                p_message = vlm_MessageNew( "new", "could not create media" );
Gildas Bazin's avatar
Gildas Bazin committed
512
                goto error;
513 514
            }
        }
Gildas Bazin's avatar
Gildas Bazin committed
515 516

        if( i_command <= 3 )
517
        {
Clément Stenac's avatar
Clément Stenac committed
518
            p_message = vlm_MessageNew( "new", NULL );
Gildas Bazin's avatar
Gildas Bazin committed
519
            goto success;
520
        }
Gildas Bazin's avatar
Gildas Bazin committed
521 522 523 524

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

525
    else if( !strcmp(ppsz_command[0], "setup") )
Gildas Bazin's avatar
Gildas Bazin committed
526 527 528 529
    {
        if( i_command < 2 ) goto syntax_error;

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

532
    else if( !strcmp(ppsz_command[0], "del") )
533
    {
Clément Stenac's avatar
Clément Stenac committed
534 535
        vlm_media_t *p_media;
        vlm_schedule_t *p_schedule;
536

Gildas Bazin's avatar
Gildas Bazin committed
537
        if( i_command < 2 ) goto syntax_error;
538

Clément Stenac's avatar
Clément Stenac committed
539 540
        p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
        p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
541

Clément Stenac's avatar
Clément Stenac committed
542
        if( p_schedule != NULL )
Gildas Bazin's avatar
Gildas Bazin committed
543
        {
Clément Stenac's avatar
Clément Stenac committed
544
            vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
Gildas Bazin's avatar
Gildas Bazin committed
545
        }
Clément Stenac's avatar
Clément Stenac committed
546
        else if( p_media != NULL )
Gildas Bazin's avatar
Gildas Bazin committed
547
        {
Clément Stenac's avatar
Clément Stenac committed
548
            vlm_MediaDelete( p_vlm, p_media, NULL );
Gildas Bazin's avatar
Gildas Bazin committed
549
        }
550
        else if( !strcmp(ppsz_command[1], "media") )
Gildas Bazin's avatar
Gildas Bazin committed
551
        {
Clément Stenac's avatar
Clément Stenac committed
552 553
            while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
                                                     NULL );
Gildas Bazin's avatar
Gildas Bazin committed
554
        }
555
        else if( !strcmp(ppsz_command[1], "schedule") )
Gildas Bazin's avatar
Gildas Bazin committed
556
        {
Clément Stenac's avatar
Clément Stenac committed
557 558
            while( p_vlm->i_schedule )
                vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
Gildas Bazin's avatar
Gildas Bazin committed
559
        }
560
        else if( !strcmp(ppsz_command[1], "all") )
Gildas Bazin's avatar
Gildas Bazin committed
561
        {
Clément Stenac's avatar
Clément Stenac committed
562 563
            while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
                                                     NULL );
Gildas Bazin's avatar
Gildas Bazin committed
564

Clément Stenac's avatar
Clément Stenac committed
565 566
            while( p_vlm->i_schedule )
                vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
567 568 569
        }
        else
        {
Clément Stenac's avatar
Clément Stenac committed
570 571
            p_message = vlm_MessageNew( "del", "%s: media unknown",
                                      ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
572
            goto error;
573
        }
Gildas Bazin's avatar
Gildas Bazin committed
574

Clément Stenac's avatar
Clément Stenac committed
575
        p_message = vlm_MessageNew( "del", NULL );
Gildas Bazin's avatar
Gildas Bazin committed
576
        goto success;
577
    }
Gildas Bazin's avatar
Gildas Bazin committed
578

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

584 585
        if( i_command == 1 )
        {
Clément Stenac's avatar
Clément Stenac committed
586
            p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
Gildas Bazin's avatar
Gildas Bazin committed
587
            goto success;
588
        }
Gildas Bazin's avatar
Gildas Bazin committed
589
        else if( i_command > 2 ) goto syntax_error;
590

Clément Stenac's avatar
Clément Stenac committed
591 592
        p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
        p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
593

Clément Stenac's avatar
Clément Stenac committed
594
        if( p_schedule != NULL )
595
        {
Clément Stenac's avatar
Clément Stenac committed
596
            p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
597
        }
Clément Stenac's avatar
Clément Stenac committed
598
        else if( p_media != NULL )
599
        {
Clément Stenac's avatar
Clément Stenac committed
600
            p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
601 602 603
        }
        else
        {
Clément Stenac's avatar
Clément Stenac committed
604
            p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
605
        }
Gildas Bazin's avatar
Gildas Bazin committed
606 607

        goto success;
608 609
    }

610
    else if( !strcmp(ppsz_command[0], "help") )
Gildas Bazin's avatar
Gildas Bazin committed
611 612
    {
        if( i_command != 1 ) goto syntax_error;
613

Clément Stenac's avatar
Clément Stenac committed
614
        p_message = vlm_Help( p_vlm, NULL );
Gildas Bazin's avatar
Gildas Bazin committed
615 616
        goto success;
    }
617

618
    else if( !strcmp(ppsz_command[0], "control") )
Gildas Bazin's avatar
Gildas Bazin committed
619
    {
Clément Stenac's avatar
Clément Stenac committed
620
        vlm_media_t *p_media;
621

Gildas Bazin's avatar
Gildas Bazin committed
622
        if( i_command < 3 ) goto syntax_error;
623

Clément Stenac's avatar
Clément Stenac committed
624
        if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
625
        {
Clément Stenac's avatar
Clément Stenac committed
626 627
            p_message = vlm_MessageNew( "control", "%s: media unknown",
                                      ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
628
            goto error;
629
        }
Gildas Bazin's avatar
Gildas Bazin committed
630
        else
631
        {
Gildas Bazin's avatar
Gildas Bazin committed
632
            char *psz_command, *psz_arg = 0, *psz_instance = 0;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
633
            size_t i_index = 2;
634

635 636 637
            if( strcmp( ppsz_command[2], "play" ) &&
                strcmp( ppsz_command[2], "stop" ) &&
                strcmp( ppsz_command[2], "pause" ) &&
638
                strcmp( ppsz_command[2], "seek" ) )
639
            {
Gildas Bazin's avatar
Gildas Bazin committed
640
                i_index++;
Clément Stenac's avatar
Clément Stenac committed
641
                psz_instance = ppsz_command[2];
Gildas Bazin's avatar
Gildas Bazin committed
642 643

                if( i_command < 4 ) goto syntax_error;
644 645 646 647

                if( strcmp( ppsz_command[3], "play" ) &&
                    strcmp( ppsz_command[3], "stop" ) &&
                    strcmp( ppsz_command[3], "pause" ) &&
648
                    strcmp( ppsz_command[3], "seek" ) )
649
                    goto syntax_error;
650 651
            }

Clément Stenac's avatar
Clément Stenac committed
652
            psz_command = ppsz_command[i_index];
653

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

Clément Stenac's avatar
Clément Stenac committed
656 657 658
            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
659
            goto success;
660 661
        }
    }
Gildas Bazin's avatar
Gildas Bazin committed
662

663
    else if( !strcmp(ppsz_command[0], "save") )
664
    {
Gildas Bazin's avatar
Gildas Bazin committed
665
        if( i_command != 2 ) goto syntax_error;
666

667
        if( vlm_Save( p_vlm, ppsz_command[1] ) )
Gildas Bazin's avatar
Gildas Bazin committed
668
        {
Clément Stenac's avatar
Clément Stenac committed
669
            p_message = vlm_MessageNew( "save", "Unable to save to file" );
Gildas Bazin's avatar
Gildas Bazin committed
670
            goto error;
671 672 673
        }
        else
        {
Clément Stenac's avatar
Clément Stenac committed
674
            p_message = vlm_MessageNew( "save", NULL );
Gildas Bazin's avatar
Gildas Bazin committed
675
            goto success;
676 677
        }
    }
Gildas Bazin's avatar
Gildas Bazin committed
678

679 680 681 682 683 684 685 686 687 688 689 690
    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;
    }

691
    else if( !strcmp(ppsz_command[0], "load") )
692
    {
Gildas Bazin's avatar
Gildas Bazin committed
693
        if( i_command != 2 ) goto syntax_error;
694

695
        switch( vlm_Load( p_vlm, ppsz_command[1] ) )
Gildas Bazin's avatar
Gildas Bazin committed
696
        {
697 698 699 700
            case 0:
                p_message = vlm_MessageNew( "load", NULL );
                goto success;
            case 2:
701
                p_message = vlm_MessageNew( "load", "Read file error" );
Gildas Bazin's avatar
Gildas Bazin committed
702
                goto error;
703 704
            case 3:
                p_message =
705
                    vlm_MessageNew( "load", "Error while loading file" );
706 707 708 709 710
                goto error;
            default:
                p_message =
                    vlm_MessageNew( "load", "Unable to load from file" );
                goto error;
Gildas Bazin's avatar
Gildas Bazin committed
711 712 713 714 715
        }
    }

    else
    {
Clément Stenac's avatar
Clément Stenac committed
716
        p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
Gildas Bazin's avatar
Gildas Bazin committed
717 718 719 720
        goto error;
    }

    /* Common code between "new" and "setup" */
721 722
    if( !strcmp(ppsz_command[0], "new") ||
        !strcmp(ppsz_command[0], "setup") )
Gildas Bazin's avatar
Gildas Bazin committed
723
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
724
        size_t i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
Clément Stenac's avatar
Clément Stenac committed
725 726
        vlm_media_t *p_media;
        vlm_schedule_t *p_schedule;
Gildas Bazin's avatar
Gildas Bazin committed
727 728 729

        if( i_command < i_command_start ) goto syntax_error;

Clément Stenac's avatar
Clément Stenac committed
730 731
        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
732

Clément Stenac's avatar
Clément Stenac committed
733
        if( !p_media && !p_schedule )
Gildas Bazin's avatar
Gildas Bazin committed
734
        {
Clément Stenac's avatar
Clément Stenac committed
735 736
            p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
                                        ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
737 738 739
            goto error;
        }

Clément Stenac's avatar
Clément Stenac committed
740
        if( p_schedule != NULL )
Gildas Bazin's avatar
Gildas Bazin committed
741
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
742 743
            size_t i;

Gildas Bazin's avatar
Gildas Bazin committed
744 745
            for( i = i_command_start ; i < i_command ; i++ )
            {
746 747
                if( !strcmp( ppsz_command[i], "enabled" ) ||
                    !strcmp( ppsz_command[i], "disabled" ) )
Gildas Bazin's avatar
Gildas Bazin committed
748
                {
Clément Stenac's avatar
Clément Stenac committed
749
                    vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
Gildas Bazin's avatar
Gildas Bazin committed
750
                }
751

Clément Stenac's avatar
Clément Stenac committed
752
                /* Beware: everything behind append is considered as
Gildas Bazin's avatar
Gildas Bazin committed
753
                 * command line */
754
                else if( !strcmp( ppsz_command[i], "append" ) )
755
                {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
756 757
                    size_t j;

Gildas Bazin's avatar
Gildas Bazin committed
758 759 760
                    if( ++i >= i_command ) break;

                    for( j = i + 1; j < i_command; j++ )
761
                    {
Clément Stenac's avatar
Clément Stenac committed
762 763 764 765 766
                        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] );
767
                    }
Gildas Bazin's avatar
Gildas Bazin committed
768

Clément Stenac's avatar
Clément Stenac committed
769 770
                    vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
                                       ppsz_command[i] );
Gildas Bazin's avatar
Gildas Bazin committed
771
                    break;
772 773 774
                }
                else
                {
775
                    if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
Gildas Bazin's avatar
Gildas Bazin committed
776
                    {
Clément Stenac's avatar
Clément Stenac committed
777
                        vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
778 779 780
                        p_message =
                            vlm_MessageNew( ppsz_command[0],
                                            "Wrong properties syntax" );
Gildas Bazin's avatar
Gildas Bazin committed
781 782 783 784
                        goto error;
                    }
                    else if( i + 1 >= i_command )
                    {
785 786 787
                        p_message =
                            vlm_MessageNew( ppsz_command[0],
                                            "Wrong properties syntax" );
Gildas Bazin's avatar
Gildas Bazin committed
788 789 790
                        goto error;
                    }

Clément Stenac's avatar
Clément Stenac committed
791 792
                    vlm_ScheduleSetup( p_schedule, ppsz_command[i],
                                       ppsz_command[i+1] );
Gildas Bazin's avatar
Gildas Bazin committed
793
                    i++;
794 795 796
                }
            }
        }
Gildas Bazin's avatar
Gildas Bazin committed
797

Clément Stenac's avatar
Clément Stenac committed
798
        else if( p_media != NULL )
799
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
800 801
            size_t i;

Gildas Bazin's avatar
Gildas Bazin committed
802 803
            for( i = i_command_start ; i < i_command ; i++ )
            {
804 805
                if( !strcmp( ppsz_command[i], "enabled" ) ||
                    !strcmp( ppsz_command[i], "disabled" ) )
Gildas Bazin's avatar
Gildas Bazin committed
806
                {
Clément Stenac's avatar
Clément Stenac committed
807
                    vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
Gildas Bazin's avatar
Gildas Bazin committed
808
                }
809 810
                else if( i + 1 >= i_command &&
                         !strcmp( ppsz_command[i], "mux") )
811
                {
Clément Stenac's avatar
Clément Stenac committed
812
                    if( p_media->i_type != VOD_TYPE )
813
                    {
Clément Stenac's avatar
Clément Stenac committed
814
                        p_message = vlm_MessageNew( ppsz_command[0],
815 816 817 818
                                  "mux only available for broadcast" );
                    }
                    else
                    {
Clément Stenac's avatar
Clément Stenac committed
819 820
                        vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
                                        ppsz_command[i+1] );
821
                        i++;
822 823
                    }
                }
824 825
                else if( !strcmp( ppsz_command[i], "loop" ) ||
                         !strcmp( ppsz_command[i], "unloop" ) )
Gildas Bazin's avatar
Gildas Bazin committed
826
                {
Clément Stenac's avatar
Clément Stenac committed
827
                    if( p_media->i_type != BROADCAST_TYPE )
Gildas Bazin's avatar
Gildas Bazin committed
828
                    {
Clément Stenac's avatar
Clément Stenac committed
829
                        p_message = vlm_MessageNew( ppsz_command[0],