vlm.c 78.2 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
#include <vlc_vlm.h>
48
#include "vlm_internal.h"
49
#include <vlc_vod.h>
Clément Stenac's avatar
Clément Stenac committed
50
#include <vlc_charset.h>
51

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

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

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

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

67 68 69 70 71 72 73 74 75 76 77 78
static int vlm_Save( vlm_t *p_vlm, const char *psz_file );
static int vlm_Load( vlm_t *p_vlm, const char *psz_file );


static vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched, const char *psz_name );
static int vlm_ScheduleSetup( vlm_schedule_t *schedule, const char *psz_cmd,
                       const char *psz_value );

static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );


79 80 81 82 83 84
/*****************************************************************************
 * vlm_New:
 *****************************************************************************/
vlm_t *__vlm_New ( vlc_object_t *p_this )
{
    vlc_value_t lockval;
Clément Stenac's avatar
Clément Stenac committed
85
    vlm_t *p_vlm = NULL;
86
    char *psz_vlmconf;
87

88 89 90 91 92
    /* Avoid multiple creation */
    if( var_Create( p_this->p_libvlc_global, "vlm_mutex", VLC_VAR_MUTEX ) ||
        var_Get( p_this->p_libvlc_global, "vlm_mutex", &lockval ) )
        return NULL;

93 94
    vlc_mutex_lock( lockval.p_address );

95 96
    p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE );
    if( p_vlm )
97
    {
98 99 100 101
        vlc_object_yield( p_vlm );
        vlc_mutex_unlock( lockval.p_address );
        return p_vlm;
    }
102

103
    msg_Dbg( p_this, "creating VLM" );
104

105 106 107 108 109
    p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM );
    if( !p_vlm )
    {
        vlc_mutex_unlock( lockval.p_address );
        return NULL;
110
    }
111 112 113 114 115 116 117 118

    vlc_mutex_init( p_this->p_libvlc, &p_vlm->lock );
    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;
    vlc_object_yield( p_vlm );
    vlc_object_attach( p_vlm, p_this->p_libvlc );
119

Clément Stenac's avatar
Clément Stenac committed
120
    if( vlc_thread_create( p_vlm, "vlm thread",
121 122
                           Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
    {
Clément Stenac's avatar
Clément Stenac committed
123 124
        vlc_mutex_destroy( &p_vlm->lock );
        vlc_object_destroy( p_vlm );
125 126
        return NULL;
    }
127

128 129
    /* Load our configuration file */
    psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
130 131 132 133 134
    if( psz_vlmconf && *psz_vlmconf )
    {
        vlm_message_t *p_message = NULL;
        char *psz_buffer = NULL;

135
        msg_Dbg( p_this, "loading VLM configuration" );
136 137 138
        asprintf(&psz_buffer, "load %s", psz_vlmconf );
        if( psz_buffer )
        {
139 140
            msg_Dbg( p_this, psz_buffer );
            if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
141
                msg_Warn( p_this, "error while loading the configuration file" );
142

143
            vlm_MessageDelete(p_message);
144 145
            free(psz_buffer);
        }
146 147
    }
    free(psz_vlmconf);
148

149 150 151
    vlc_mutex_unlock( lockval.p_address );

    return p_vlm;
152 153 154 155 156
}

/*****************************************************************************
 * vlm_Delete:
 *****************************************************************************/
Clément Stenac's avatar
Clément Stenac committed
157
void vlm_Delete( vlm_t *p_vlm )
158 159 160
{
    vlc_value_t lockval;

161
    msg_Err( p_vlm, "vlm_Delete called" );
162
    var_Get( p_vlm->p_libvlc_global, "vlm_mutex", &lockval );
163 164
    vlc_mutex_lock( lockval.p_address );

Clément Stenac's avatar
Clément Stenac committed
165
    vlc_object_release( p_vlm );
166

Clément Stenac's avatar
Clément Stenac committed
167
    if( p_vlm->i_refcount > 0 )
168 169 170 171 172
    {
        vlc_mutex_unlock( lockval.p_address );
        return;
    }

Clément Stenac's avatar
Clément Stenac committed
173 174
    p_vlm->b_die = VLC_TRUE;
    vlc_thread_join( p_vlm );
175

176 177
    vlc_object_detach( p_vlm );

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

180 181 182
    while( p_vlm->i_media )
        vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
    TAB_CLEAN( p_vlm->i_media, p_vlm->media );
183

184 185 186
    while( p_vlm->i_schedule )
        vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
    TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
187

Clément Stenac's avatar
Clément Stenac committed
188
    vlc_object_destroy( p_vlm );
189 190 191 192 193 194
    vlc_mutex_unlock( lockval.p_address );
}

/*****************************************************************************
 * vlm_ExecuteCommand:
 *****************************************************************************/
195
int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
Clément Stenac's avatar
Clément Stenac committed
196
                        vlm_message_t **pp_message)
197
{
Clément Stenac's avatar
Clément Stenac committed
198
    int i_result;
199

Clément Stenac's avatar
Clément Stenac committed
200 201 202
    vlc_mutex_lock( &p_vlm->lock );
    i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
    vlc_mutex_unlock( &p_vlm->lock );
203

Clément Stenac's avatar
Clément Stenac committed
204
    return i_result;
205 206
}

207 208

static const char quotes[] = "\"'";
209 210 211
/**
 * FindCommandEnd: look for the end of a possibly quoted string
 * @return NULL on mal-formatted string,
212
 * pointer past the last character otherwise.
213 214
 */
static const char *FindCommandEnd (const char *psz_sent)
215
{
216
    char c, quote = 0;
217

218
    while ((c = *psz_sent) != '\0')
219
    {
220
        if (!quote)
Gildas Bazin's avatar
Gildas Bazin committed
221
        {
222 223 224 225 226 227 228 229 230 231 232 233
            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
234
        }
235
        else
236 237 238 239 240 241 242 243 244 245 246
        {
            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
            }
        }
247

Gildas Bazin's avatar
Gildas Bazin committed
248
        psz_sent++;
249
    }
250

251 252 253
    // error (NULL) if we could not find a matching quote
    return quote ? NULL : psz_sent;
}
254

Gildas Bazin's avatar
Gildas Bazin committed
255

256
/**
257
 * Unescape a nul-terminated string.
258 259 260 261 262 263 264 265 266
 * 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)
{
267
    char c, quote = 0;
268

269
    while ((c = *in++) != '\0')
270
    {
271
        if (!quote)
Gildas Bazin's avatar
Gildas Bazin committed
272
        {
273
            if (strchr(quotes,c))   // opening quote
274
            {
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
                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;
296
                    continue;
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
                }
                /* 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++ = '\\';
324 325
            }
        }
326
        *out++ = c;
327
    }
328 329 330

    *out = '\0';
    return 0;
331 332
}

333

Gildas Bazin's avatar
Gildas Bazin committed
334 335 336 337 338
/*****************************************************************************
 * ExecuteCommand: The main state machine
 *****************************************************************************
 * Execute a command which ends with '\0' (string)
 *****************************************************************************/
339
static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
Clément Stenac's avatar
Clément Stenac committed
340
                           vlm_message_t **pp_message )
341
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
342 343 344
    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
345
    vlm_message_t *p_message = NULL;
346 347

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

352
        if(isspace (*psz_command))
353
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
354 355
            psz_command++;
            continue;
356
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
357 358 359

        /* support for comments */
        if( i_command == 0 && *psz_command == '#')
360
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
361 362 363
            p_message = vlm_MessageNew( "", NULL );
            goto success;
        }
364

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
365 366 367 368 369 370 371
        psz_temp = FindCommandEnd( psz_command );

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

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
375 376 377
        ppsz_command[i_command] = psz_buf;
        memcpy (psz_buf, psz_command, psz_temp - psz_command);
        psz_buf[psz_temp - psz_command] = '\0';
378

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
381 382 383
        i_command++;
        psz_buf += psz_temp - psz_command + 1;
        psz_command = psz_temp;
384

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
385
        assert (buf + sizeof (buf) >= psz_buf);
386 387
    }

Gildas Bazin's avatar
Gildas Bazin committed
388 389 390
    /*
     * And then Interpret it
     */
391 392 393

    if( i_command == 0 )
    {
Clément Stenac's avatar
Clément Stenac committed
394
        p_message = vlm_MessageNew( "", NULL );
Gildas Bazin's avatar
Gildas Bazin committed
395
        goto success;
396 397
    }

398
    if( !strcmp(ppsz_command[0], "new") )
399
    {
Gildas Bazin's avatar
Gildas Bazin committed
400
        int i_type;
401

Gildas Bazin's avatar
Gildas Bazin committed
402 403
        /* Check the number of arguments */
        if( i_command < 3 ) goto syntax_error;
404

Gildas Bazin's avatar
Gildas Bazin committed
405
        /* Get type */
406
        if( !strcmp(ppsz_command[2], "vod") )
Gildas Bazin's avatar
Gildas Bazin committed
407 408 409
        {
            i_type = VOD_TYPE;
        }
410
        else if( !strcmp(ppsz_command[2], "broadcast") )
Gildas Bazin's avatar
Gildas Bazin committed
411 412 413
        {
            i_type = BROADCAST_TYPE;
        }
414
        else if( !strcmp(ppsz_command[2], "schedule") )
Gildas Bazin's avatar
Gildas Bazin committed
415 416 417 418 419
        {
            i_type = SCHEDULE_TYPE;
        }
        else
        {
420 421 422
            p_message =
                vlm_MessageNew( "new", "%s: Choose between vod, "
                                "broadcast or schedule", ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
423 424
            goto error;
        }
425

Gildas Bazin's avatar
Gildas Bazin committed
426
        /* Check for forbidden media names */
427 428 429
        if( !strcmp(ppsz_command[1], "all") ||
            !strcmp(ppsz_command[1], "media") ||
            !strcmp(ppsz_command[1], "schedule") )
Gildas Bazin's avatar
Gildas Bazin committed
430
        {
Clément Stenac's avatar
Clément Stenac committed
431
            p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
432
                                        "\"schedule\" are reserved names" );
Gildas Bazin's avatar
Gildas Bazin committed
433 434
            goto error;
        }
435

Gildas Bazin's avatar
Gildas Bazin committed
436
        /* Check the name is not already in use */
Clément Stenac's avatar
Clément Stenac committed
437 438
        if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
            vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
Gildas Bazin's avatar
Gildas Bazin committed
439
        {
Clément Stenac's avatar
Clément Stenac committed
440 441
            p_message = vlm_MessageNew( "new", "%s: Name already in use",
                                        ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
442 443
            goto error;
        }
444

Gildas Bazin's avatar
Gildas Bazin committed
445 446 447
        /* Schedule */
        if( i_type == SCHEDULE_TYPE )
        {
Clément Stenac's avatar
Clément Stenac committed
448 449 450
            vlm_schedule_t *p_schedule;
            p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
            if( !p_schedule )
451
            {
Clément Stenac's avatar
Clément Stenac committed
452
                p_message = vlm_MessageNew( "new", "could not create schedule" );
Gildas Bazin's avatar
Gildas Bazin committed
453
                goto error;
454
            }
Gildas Bazin's avatar
Gildas Bazin committed
455
        }
456

Gildas Bazin's avatar
Gildas Bazin committed
457 458 459
        /* Media */
        else
        {
Clément Stenac's avatar
Clément Stenac committed
460 461 462
            vlm_media_t *p_media;
            p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
            if( !p_media )
463
            {
Clément Stenac's avatar
Clément Stenac committed
464
                p_message = vlm_MessageNew( "new", "could not create media" );
Gildas Bazin's avatar
Gildas Bazin committed
465
                goto error;
466 467
            }
        }
Gildas Bazin's avatar
Gildas Bazin committed
468 469

        if( i_command <= 3 )
470
        {
Clément Stenac's avatar
Clément Stenac committed
471
            p_message = vlm_MessageNew( "new", NULL );
Gildas Bazin's avatar
Gildas Bazin committed
472
            goto success;
473
        }
Gildas Bazin's avatar
Gildas Bazin committed
474 475 476 477

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

478
    else if( !strcmp(ppsz_command[0], "setup") )
Gildas Bazin's avatar
Gildas Bazin committed
479 480 481 482
    {
        if( i_command < 2 ) goto syntax_error;

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

485
    else if( !strcmp(ppsz_command[0], "del") )
486
    {
Clément Stenac's avatar
Clément Stenac committed
487 488
        vlm_media_t *p_media;
        vlm_schedule_t *p_schedule;
489

Gildas Bazin's avatar
Gildas Bazin committed
490
        if( i_command < 2 ) goto syntax_error;
491

Clément Stenac's avatar
Clément Stenac committed
492 493
        p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
        p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
494

Clément Stenac's avatar
Clément Stenac committed
495
        if( p_schedule != NULL )
Gildas Bazin's avatar
Gildas Bazin committed
496
        {
Clément Stenac's avatar
Clément Stenac committed
497
            vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
Gildas Bazin's avatar
Gildas Bazin committed
498
        }
Clément Stenac's avatar
Clément Stenac committed
499
        else if( p_media != NULL )
Gildas Bazin's avatar
Gildas Bazin committed
500
        {
Clément Stenac's avatar
Clément Stenac committed
501
            vlm_MediaDelete( p_vlm, p_media, NULL );
Gildas Bazin's avatar
Gildas Bazin committed
502
        }
503
        else if( !strcmp(ppsz_command[1], "media") )
Gildas Bazin's avatar
Gildas Bazin committed
504
        {
Clément Stenac's avatar
Clément Stenac committed
505 506
            while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
                                                     NULL );
Gildas Bazin's avatar
Gildas Bazin committed
507
        }
508
        else if( !strcmp(ppsz_command[1], "schedule") )
Gildas Bazin's avatar
Gildas Bazin committed
509
        {
Clément Stenac's avatar
Clément Stenac committed
510 511
            while( p_vlm->i_schedule )
                vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
Gildas Bazin's avatar
Gildas Bazin committed
512
        }
513
        else if( !strcmp(ppsz_command[1], "all") )
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

Clément Stenac's avatar
Clément Stenac committed
518 519
            while( p_vlm->i_schedule )
                vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
520 521 522
        }
        else
        {
Clément Stenac's avatar
Clément Stenac committed
523 524
            p_message = vlm_MessageNew( "del", "%s: media unknown",
                                      ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
525
            goto error;
526
        }
Gildas Bazin's avatar
Gildas Bazin committed
527

Clément Stenac's avatar
Clément Stenac committed
528
        p_message = vlm_MessageNew( "del", NULL );
Gildas Bazin's avatar
Gildas Bazin committed
529
        goto success;
530
    }
Gildas Bazin's avatar
Gildas Bazin committed
531

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

537 538
        if( i_command == 1 )
        {
Clément Stenac's avatar
Clément Stenac committed
539
            p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
Gildas Bazin's avatar
Gildas Bazin committed
540
            goto success;
541
        }
Gildas Bazin's avatar
Gildas Bazin committed
542
        else if( i_command > 2 ) goto syntax_error;
543

Clément Stenac's avatar
Clément Stenac committed
544 545
        p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
        p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
546

Clément Stenac's avatar
Clément Stenac committed
547
        if( p_schedule != NULL )
548
        {
Clément Stenac's avatar
Clément Stenac committed
549
            p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
550
        }
Clément Stenac's avatar
Clément Stenac committed
551
        else if( p_media != NULL )
552
        {
Clément Stenac's avatar
Clément Stenac committed
553
            p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
554 555 556
        }
        else
        {
Clément Stenac's avatar
Clément Stenac committed
557
            p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
558
        }
Gildas Bazin's avatar
Gildas Bazin committed
559 560

        goto success;
561 562
    }

563
    else if( !strcmp(ppsz_command[0], "help") )
Gildas Bazin's avatar
Gildas Bazin committed
564 565
    {
        if( i_command != 1 ) goto syntax_error;
566

Clément Stenac's avatar
Clément Stenac committed
567
        p_message = vlm_Help( p_vlm, NULL );
Gildas Bazin's avatar
Gildas Bazin committed
568 569
        goto success;
    }
570

571
    else if( !strcmp(ppsz_command[0], "control") )
Gildas Bazin's avatar
Gildas Bazin committed
572
    {
Clément Stenac's avatar
Clément Stenac committed
573
        vlm_media_t *p_media;
574

Gildas Bazin's avatar
Gildas Bazin committed
575
        if( i_command < 3 ) goto syntax_error;
576

Clément Stenac's avatar
Clément Stenac committed
577
        if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
578
        {
Clément Stenac's avatar
Clément Stenac committed
579 580
            p_message = vlm_MessageNew( "control", "%s: media unknown",
                                      ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
581
            goto error;
582
        }
Gildas Bazin's avatar
Gildas Bazin committed
583
        else
584
        {
Gildas Bazin's avatar
Gildas Bazin committed
585
            char *psz_command, *psz_arg = 0, *psz_instance = 0;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
586
            size_t i_index = 2;
587

588 589 590
            if( strcmp( ppsz_command[2], "play" ) &&
                strcmp( ppsz_command[2], "stop" ) &&
                strcmp( ppsz_command[2], "pause" ) &&
591
                strcmp( ppsz_command[2], "seek" ) )
592
            {
Gildas Bazin's avatar
Gildas Bazin committed
593
                i_index++;
Clément Stenac's avatar
Clément Stenac committed
594
                psz_instance = ppsz_command[2];
Gildas Bazin's avatar
Gildas Bazin committed
595 596

                if( i_command < 4 ) goto syntax_error;
597 598 599 600

                if( strcmp( ppsz_command[3], "play" ) &&
                    strcmp( ppsz_command[3], "stop" ) &&
                    strcmp( ppsz_command[3], "pause" ) &&
601
                    strcmp( ppsz_command[3], "seek" ) )
602
                    goto syntax_error;
603 604
            }

Clément Stenac's avatar
Clément Stenac committed
605
            psz_command = ppsz_command[i_index];
606

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

Clément Stenac's avatar
Clément Stenac committed
609 610 611
            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
612
            goto success;
613 614
        }
    }
Gildas Bazin's avatar
Gildas Bazin committed
615

616
    else if( !strcmp(ppsz_command[0], "save") )
617
    {
Gildas Bazin's avatar
Gildas Bazin committed
618
        if( i_command != 2 ) goto syntax_error;
619

620
        if( vlm_Save( p_vlm, ppsz_command[1] ) )
Gildas Bazin's avatar
Gildas Bazin committed
621
        {
Clément Stenac's avatar
Clément Stenac committed
622
            p_message = vlm_MessageNew( "save", "Unable to save to file" );
Gildas Bazin's avatar
Gildas Bazin committed
623
            goto error;
624 625 626
        }
        else
        {
Clément Stenac's avatar
Clément Stenac committed
627
            p_message = vlm_MessageNew( "save", NULL );
Gildas Bazin's avatar
Gildas Bazin committed
628
            goto success;
629 630
        }
    }
Gildas Bazin's avatar
Gildas Bazin committed
631

632 633 634 635 636 637 638 639 640 641 642 643
    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;
    }

644
    else if( !strcmp(ppsz_command[0], "load") )
645
    {
Gildas Bazin's avatar
Gildas Bazin committed
646
        if( i_command != 2 ) goto syntax_error;
647

648
        switch( vlm_Load( p_vlm, ppsz_command[1] ) )
Gildas Bazin's avatar
Gildas Bazin committed
649
        {
650 651 652 653
            case 0:
                p_message = vlm_MessageNew( "load", NULL );
                goto success;
            case 2:
654
                p_message = vlm_MessageNew( "load", "Read file error" );
Gildas Bazin's avatar
Gildas Bazin committed
655
                goto error;
656 657
            case 3:
                p_message =
658
                    vlm_MessageNew( "load", "Error while loading file" );
659 660 661 662 663
                goto error;
            default:
                p_message =
                    vlm_MessageNew( "load", "Unable to load from file" );
                goto error;
Gildas Bazin's avatar
Gildas Bazin committed
664 665 666 667 668
        }
    }

    else
    {
Clément Stenac's avatar
Clément Stenac committed
669
        p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
Gildas Bazin's avatar
Gildas Bazin committed
670 671 672 673
        goto error;
    }

    /* Common code between "new" and "setup" */
674 675
    if( !strcmp(ppsz_command[0], "new") ||
        !strcmp(ppsz_command[0], "setup") )
Gildas Bazin's avatar
Gildas Bazin committed
676
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
677
        size_t i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
Clément Stenac's avatar
Clément Stenac committed
678 679
        vlm_media_t *p_media;
        vlm_schedule_t *p_schedule;
Gildas Bazin's avatar
Gildas Bazin committed
680 681 682

        if( i_command < i_command_start ) goto syntax_error;

Clément Stenac's avatar
Clément Stenac committed
683 684
        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
685

Clément Stenac's avatar
Clément Stenac committed
686
        if( !p_media && !p_schedule )
Gildas Bazin's avatar
Gildas Bazin committed
687
        {
Clément Stenac's avatar
Clément Stenac committed
688 689
            p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
                                        ppsz_command[1] );
Gildas Bazin's avatar
Gildas Bazin committed
690 691 692
            goto error;
        }

Clément Stenac's avatar
Clément Stenac committed
693
        if( p_schedule != NULL )
Gildas Bazin's avatar
Gildas Bazin committed
694
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
695 696
            size_t i;

Gildas Bazin's avatar
Gildas Bazin committed
697 698
            for( i = i_command_start ; i < i_command ; i++ )
            {
699 700
                if( !strcmp( ppsz_command[i], "enabled" ) ||
                    !strcmp( ppsz_command[i], "disabled" ) )
Gildas Bazin's avatar
Gildas Bazin committed
701
                {
Clément Stenac's avatar
Clément Stenac committed
702
                    vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
Gildas Bazin's avatar
Gildas Bazin committed
703
                }
704

Clément Stenac's avatar
Clément Stenac committed
705
                /* Beware: everything behind append is considered as
Gildas Bazin's avatar
Gildas Bazin committed
706
                 * command line */
707
                else if( !strcmp( ppsz_command[i], "append" ) )
708
                {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
709 710
                    size_t j;

Gildas Bazin's avatar
Gildas Bazin committed
711 712 713
                    if( ++i >= i_command ) break;

                    for( j = i + 1; j < i_command; j++ )
714
                    {
Clément Stenac's avatar
Clément Stenac committed
715 716 717 718 719
                        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] );
720
                    }
Gildas Bazin's avatar
Gildas Bazin committed
721

Clément Stenac's avatar
Clément Stenac committed
722 723
                    vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
                                       ppsz_command[i] );
Gildas Bazin's avatar
Gildas Bazin committed
724
                    break;
725 726 727
                }
                else
                {
728
                    if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
Gildas Bazin's avatar
Gildas Bazin committed
729
                    {
Clément Stenac's avatar
Clément Stenac committed
730
                        vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
731 732 733
                        p_message =
                            vlm_MessageNew( ppsz_command[0],
                                            "Wrong properties syntax" );
Gildas Bazin's avatar
Gildas Bazin committed
734 735 736 737
                        goto error;
                    }
                    else if( i + 1 >= i_command )
                    {
738 739 740
                        p_message =
                            vlm_MessageNew( ppsz_command[0],
                                            "Wrong properties syntax" );
Gildas Bazin's avatar
Gildas Bazin committed
741 742 743
                        goto error;
                    }

Clément Stenac's avatar
Clément Stenac committed
744 745
                    vlm_ScheduleSetup( p_schedule, ppsz_command[i],
                                       ppsz_command[i+1] );
Gildas Bazin's avatar
Gildas Bazin committed
746
                    i++;
747 748 749
                }
            }
        }
Gildas Bazin's avatar
Gildas Bazin committed
750

Clément Stenac's avatar
Clément Stenac committed
751
        else if( p_media != NULL )
752
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
753 754
            size_t i;

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

Clément Stenac's avatar
Clément Stenac committed
809
                    vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
810
                                    ppsz_command[i+1] );
Gildas Bazin's avatar
Gildas Bazin committed
811 812 813
                    i++;
                }
            }
814
        }
Gildas Bazin's avatar
Gildas Bazin committed
815

Clément Stenac's avatar
Clément Stenac committed
816
        p_message = vlm_MessageNew( ppsz_command[0], NULL );
Gildas Bazin's avatar
Gildas Bazin committed
817
        goto success;
818 819
    }

Gildas Bazin's avatar
Gildas Bazin committed