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
/**
 * FindCommandEnd: look for the end of a possibly quoted string
 * @return NULL on mal-formatted string,
260
 * pointer past the last character otherwise.
261 262
 */
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 268 269
    if (quote)
        psz_sent++; // skip opening quote

270
    while ((c = *psz_sent) != '\0')
271
    {
272
        if ((quote == '"') && (c == '\\'))
Gildas Bazin's avatar
Gildas Bazin committed
273
        {
274
            psz_sent++; // move past backslash
275 276
            if (*psz_sent == '\0')
                return NULL; // cannot escape "nothing"
Gildas Bazin's avatar
Gildas Bazin committed
277
        }
278 279 280 281
        else
        if (c == quote) // non-escaped matching quote
            return psz_sent + 1;
        else
282
        if ((!quote) && isspace(c)) // non-escaped blank
283
            return psz_sent;
284

Gildas Bazin's avatar
Gildas Bazin committed
285
        psz_sent++;
286
    }
287

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

Gildas Bazin's avatar
Gildas Bazin committed
292

293
/**
294
 * Unescape a nul-terminated string.
295 296 297 298 299 300 301 302 303
 * 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)
{
304
    const char quote = strchr (quotes, in[0]) ? in[0] : 0;
Gildas Bazin's avatar
Gildas Bazin committed
305

306 307 308 309
    if (quote)
        in++; // skips opening quote

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

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

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

324 325
                case '\\':
                    *out++ = '\\';
326 327
                    continue;

328 329 330
                case '\0':   // should never happen
                    *out = '\0';
                    return -1;
331
            }
332 333
            /* None of the special cases - copy the backslash */
            *out++ = '\\';
334
        }
335
        *out++ = c;
336
    }
337 338 339

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

342

Gildas Bazin's avatar
Gildas Bazin committed
343 344 345 346 347
/*****************************************************************************
 * ExecuteCommand: The main state machine
 *****************************************************************************
 * Execute a command which ends with '\0' (string)
 *****************************************************************************/
348
static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
Clément Stenac's avatar
Clément Stenac committed
349
                           vlm_message_t **pp_message )
350
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
351 352 353
    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
354
    vlm_message_t *p_message = NULL;
355 356

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        goto success;
570 571
    }

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

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

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

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

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

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

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

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

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

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

Clément Stenac's avatar
Clément Stenac committed
618 619 620
            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
621
            goto success;
622 623
        }
    }
Gildas Bazin's avatar
Gildas Bazin committed
624

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

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

641 642 643 644 645 646 647 648 649 650 651 652
    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;
    }

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

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

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

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

        if( i_command < i_command_start ) goto syntax_error;

Clément Stenac's avatar
Clément Stenac committed
692 693
        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
694

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

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

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

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

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

                    for( j = i + 1; j < i_command; j++ )
723
                    {
Clément Stenac's avatar
Clément Stenac committed
724 725 726 727 728
                        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] );
729
                    }
Gildas Bazin's avatar
Gildas Bazin committed
730

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

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

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

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