vlmshell.c 51.6 KB
Newer Older
1
/*****************************************************************************
Pere Orga's avatar
Pere Orga committed
2
 * vlmshell.c: VLM interface plugin
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2000-2005 VLC authors and VideoLAN
5 6 7 8 9 10
 * $Id$
 *
 * Authors: Simon Latapie <garf@videolan.org>
 *          Laurent Aimar <fenrir@videolan.org>
 *          Gildas Bazin <gbazin@videolan.org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
11 12 13
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
14 15 16 17
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
20
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
21 22 23
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28 29 30 31 32
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc_common.h>
34 35 36 37 38 39 40 41 42

#include <stdio.h>
#include <ctype.h>                                              /* tolower() */
#include <assert.h>

#include <vlc_vlm.h>

#ifdef ENABLE_VLM

43
#include <time.h>                                                 /* ctime() */
44 45 46
#include <limits.h>
#include <fcntl.h>
#include <sys/stat.h>
47 48 49 50 51 52

#include <vlc_input.h>
#include "input_internal.h"
#include <vlc_stream.h>
#include "vlm_internal.h"
#include <vlc_charset.h>
53
#include <vlc_fs.h>
54
#include <vlc_sout.h>
55
#include <vlc_memstream.h>
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include "../stream_output/stream_output.h"
#include "../libvlc.h"

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/

/* */
static vlm_message_t *vlm_Show( vlm_t *, vlm_media_sys_t *, vlm_schedule_sys_t *, const char * );

static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *, const char * );

static char *Save( vlm_t * );
static int Load( vlm_t *, char * );

static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
                              const char *psz_value );

/* */
static vlm_media_sys_t *vlm_MediaSearch( vlm_t *, const char *);

static const char quotes[] = "\"'";
/**
 * FindCommandEnd: look for the end of a possibly quoted string
 * @return NULL on mal-formatted string,
 * pointer past the last character otherwise.
 */
static const char *FindCommandEnd( const char *psz_sent )
{
86
    unsigned char c, quote = 0;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

    while( (c = *psz_sent) != '\0' )
    {
        if( !quote )
        {
            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;
            }
        }
        else
        {
            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
            }
        }
        psz_sent++;
    }

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


/**
 * Unescape a nul-terminated string.
 * 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 )
{
133
    unsigned char c, quote = 0;
134
    bool param = false;
135 136 137

    while( (c = *in++) != '\0' )
    {
138
        // Don't escape the end of the string if we find a '#'
139
        // that's the beginning of a vlc command
140
        // TODO: find a better solution
141
        if( ( c == '#' && !quote ) || param )
142 143 144 145 146 147
        {
            param = true;
            *out++ = c;
            continue;
        }

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
        if( !quote )
        {
            if (strchr(quotes,c))   // opening quote
            {
                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;
                    continue;
                }
                /* 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++ = '\\';
            }
        }
        *out++ = c;
    }

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


/*****************************************************************************
 * ExecuteCommand: The main state machine
 *****************************************************************************
 * Execute a command which ends with '\0' (string)
 *****************************************************************************/
static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
{
    *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
    return VLC_EGENERIC;
}

static bool ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
{
    int64_t id;

    if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
        return false;
    return true;
}
static bool ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
{
    if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
        return false;
    return true;
}

static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
{
    vlm_media_sys_t *p_media;
    vlm_schedule_sys_t *p_schedule;

    p_media = vlm_MediaSearch( p_vlm, psz_name );
    p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );

    if( p_schedule != NULL )
    {
        vlm_ScheduleDelete( p_vlm, p_schedule );
    }
    else if( p_media != NULL )
    {
        vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
    }
    else if( !strcmp(psz_name, "media") )
    {
        vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
    }
    else if( !strcmp(psz_name, "schedule") )
    {
        vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
    }
    else if( !strcmp(psz_name, "all") )
    {
        vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
        vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
    }
    else
    {
        *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
        return VLC_EGENERIC;
    }

271
    *pp_status = vlm_MessageSimpleNew( "del" );
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    return VLC_SUCCESS;
}

static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
{
    vlm_media_sys_t *p_media;
    vlm_schedule_sys_t *p_schedule;

    if( !psz_name )
    {
        *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
        return VLC_SUCCESS;
    }

    p_media = vlm_MediaSearch( p_vlm, psz_name );
    p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );

    if( p_schedule != NULL )
        *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
    else if( p_media != NULL )
        *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
    else
        *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );

    return VLC_SUCCESS;
}

static int ExecuteHelp( vlm_message_t **pp_status )
{
    vlm_message_t *message_child;

#define MessageAdd( a ) \
304
        vlm_MessageAdd( *pp_status, vlm_MessageSimpleNew( a ) );
305
#define MessageAddChild( a ) \
306
        vlm_MessageAdd( message_child, vlm_MessageSimpleNew( a ) );
307

308
    *pp_status = vlm_MessageSimpleNew( "help" );
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

    message_child = MessageAdd( "Commands Syntax:" );
    MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
    MessageAddChild( "setup (name) (properties)" );
    MessageAddChild( "show [(name)|media|schedule]" );
    MessageAddChild( "del (name)|all|media|schedule" );
    MessageAddChild( "control (name) [instance_name] (command)" );
    MessageAddChild( "save (config_file)" );
    MessageAddChild( "export" );
    MessageAddChild( "load (config_file)" );

    message_child = MessageAdd( "Media Proprieties Syntax:" );
    MessageAddChild( "input (input_name)" );
    MessageAddChild( "inputdel (input_name)|all" );
    MessageAddChild( "inputdeln input_number" );
    MessageAddChild( "output (output_name)" );
    MessageAddChild( "option (option_name)[=value]" );
    MessageAddChild( "enabled|disabled" );
    MessageAddChild( "loop|unloop (broadcast only)" );
    MessageAddChild( "mux (mux_name)" );

    message_child = MessageAdd( "Schedule Proprieties Syntax:" );
    MessageAddChild( "enabled|disabled" );
    MessageAddChild( "append (command_until_rest_of_the_line)" );
    MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
                     "(seconds)|now" );
    MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
                     "(days)-(hours):(minutes):(seconds)" );
    MessageAddChild( "repeat (number_of_repetitions)" );

    message_child = MessageAdd( "Control Commands Syntax:" );
    MessageAddChild( "play [input_number]" );
    MessageAddChild( "pause" );
    MessageAddChild( "stop" );
Pavlov Konstantin's avatar
Pavlov Konstantin committed
343
    MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](milliseconds)ms" );
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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

    return VLC_SUCCESS;
}

static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
{
    vlm_media_sys_t *p_media;
    const char *psz_control = NULL;
    const char *psz_instance = NULL;
    const char *psz_argument = NULL;
    int i_index;
    int i_result;

    if( !ExecuteIsMedia( p_vlm, psz_name ) )
    {
        *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
        return VLC_EGENERIC;
    }

    assert( i_arg > 0 );

#define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
    i_index = 0;
    if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
    {
        i_index = 1;
        psz_instance = ppsz_arg[0];

        if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
            return ExecuteSyntaxError( "control", pp_status );
    }
#undef IS
    psz_control = ppsz_arg[i_index];

    if( i_index+1 < i_arg )
        psz_argument = ppsz_arg[i_index+1];

    p_media = vlm_MediaSearch( p_vlm, psz_name );
    assert( p_media );

    if( !strcmp( psz_control, "play" ) )
    {
        int i_input_index = 0;
        int i;

        if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
        {
            i_input_index = i-1;
        }
        else if( psz_argument )
        {
            int j;
            vlm_media_t *p_cfg = &p_media->cfg;
            for ( j=0; j < p_cfg->i_input; j++)
            {
                if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
                {
                    i_input_index = j;
                    break;
                }
            }
        }

        if( p_media->cfg.b_vod )
            i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_VOD_INSTANCE, p_media->cfg.id, psz_instance, i_input_index, NULL );    // we should get here now
        else
            i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
    }
    else if( !strcmp( psz_control, "seek" ) )
    {
        if( psz_argument )
        {
            bool b_relative;
            if( psz_argument[0] == '+' || psz_argument[0] == '-' )
                b_relative = true;
            else
                b_relative = false;

            if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
            {
                /* Time (ms or s) */
                int64_t i_new_time;

                if( strstr( psz_argument, "ms" ) )
                    i_new_time =  1000 * (int64_t)atoi( psz_argument );
                else
                    i_new_time = 1000000 * (int64_t)atoi( psz_argument );

                if( b_relative )
                {
                    int64_t i_time = 0;
                    vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
                    i_new_time += i_time;
                }
                if( i_new_time < 0 )
                    i_new_time = 0;
                i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
            }
            else
            {
                /* Percent */
445
                double d_new_position = us_atof( psz_argument ) / 100.0;
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

                if( b_relative )
                {
                    double d_position = 0.0;

                    vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
                    d_new_position += d_position;
                }
                if( d_new_position < 0.0 )
                    d_new_position = 0.0;
                else if( d_new_position > 1.0 )
                    d_new_position = 1.0;
                i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
            }
        }
        else
        {
            i_result = VLC_EGENERIC;
        }
    }
    else if( !strcmp( psz_control, "stop" ) )
    {
        i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
    }
    else if( !strcmp( psz_control, "pause" ) )
    {
        i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
    }
    else
    {
        i_result = VLC_EGENERIC;
    }

    if( i_result )
    {
        *pp_status = vlm_MessageNew( "control", "unknown error" );
        return VLC_SUCCESS;
    }
484
    *pp_status = vlm_MessageSimpleNew( "control" );
485 486 487 488 489 490 491
    return VLC_SUCCESS;
}

static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
{
    char *psz_export = Save( p_vlm );

492
    *pp_status = vlm_MessageNew( "export", "%s", psz_export );
493 494 495 496 497 498
    free( psz_export );
    return VLC_SUCCESS;
}

static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
{
499
    FILE *f = vlc_fopen( psz_file, "wt" );
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
    char *psz_save = NULL;

    if( !f )
        goto error;

    psz_save = Save( p_vlm );
    if( psz_save == NULL )
        goto error;
    if( fputs( psz_save, f ) == EOF )
        goto error;;
    if( fclose( f ) )
    {
        f = NULL;
        goto error;
    }

    free( psz_save );

518
    *pp_status = vlm_MessageSimpleNew( "save" );
519 520 521 522 523 524 525 526 527 528
    return VLC_SUCCESS;

error:
    free( psz_save );
    if( f )
         fclose( f );
    *pp_status = vlm_MessageNew( "save", "Unable to save to file");
    return VLC_EGENERIC;
}

529
static int ExecuteLoad( vlm_t *p_vlm, const char *psz_path, vlm_message_t **pp_status )
530
{
531 532
    int fd = vlc_open( psz_path, O_RDONLY|O_NONBLOCK );
    if( fd == -1 )
533 534 535 536 537
    {
        *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
        return VLC_EGENERIC;
    }

538 539
    struct stat st;
    char *psz_buffer = NULL;
540

541 542 543 544
    if( fstat( fd, &st ) || !S_ISREG( st.st_mode )
     || st.st_size >= SSIZE_MAX
     || ((psz_buffer = malloc( st.st_size + 1 )) == NULL)
     || read( fd, psz_buffer, st.st_size ) < (ssize_t)st.st_size )
545
    {
546
        free( psz_buffer );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
547
        vlc_close( fd );
548 549 550 551 552

        *pp_status = vlm_MessageNew( "load", "Read file error" );
        return VLC_EGENERIC;
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
553
    vlc_close( fd );
554

555
    psz_buffer[st.st_size] = '\0';
556 557 558 559 560 561 562 563 564 565 566

    if( Load( p_vlm, psz_buffer ) )
    {
        free( psz_buffer );

        *pp_status = vlm_MessageNew( "load", "Error while loading file" );
        return VLC_EGENERIC;
    }

    free( psz_buffer );

567
    *pp_status = vlm_MessageSimpleNew( "load" );
568 569 570 571 572 573 574 575 576 577 578 579 580 581
    return VLC_SUCCESS;
}

static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, bool b_new,
                                    const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
{
    const char *psz_cmd = b_new ? "new" : "setup";
    int i;

    for( i = 0; i < i_property; i++ )
    {
        if( !strcmp( ppsz_property[i], "enabled" ) ||
            !strcmp( ppsz_property[i], "disabled" ) )
        {
582 583
            if ( vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL ) )
                goto error;
584 585 586
        }
        else if( !strcmp( ppsz_property[i], "append" ) )
        {
587 588
            char *psz_line, *psz_realloc;
            int j, i_ret = VLC_SUCCESS;
589 590 591 592 593 594
            /* Beware: everything behind append is considered as
             * command line */

            if( ++i >= i_property )
                break;

595 596 597 598
            psz_line = strdup( ppsz_property[i] );
            if( unlikely(psz_line == NULL) )
                goto error;

599 600
            for( j = i+1; j < i_property; j++ )
            {
601 602 603 604 605 606 607 608 609 610 611 612 613
                psz_realloc = realloc( psz_line,
                                       strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
                if( likely(psz_realloc) )
                {
                    psz_line = psz_realloc;
                    strcat( psz_line, " " );
                    strcat( psz_line, ppsz_property[j] );
                }
                else
                {
                    i_ret = VLC_ENOMEM;
                    break;
                }
614 615
            }

616 617
            if( i_ret == VLC_SUCCESS )
                i_ret = vlm_ScheduleSetup( p_schedule, "append", psz_line );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
618
            free( psz_line );
619 620

            if( i_ret )
621
                goto error;
622 623 624 625 626 627 628 629 630 631 632
            break;
        }
        else
        {
            if( i + 1 >= i_property )
            {
                if( b_new )
                    vlm_ScheduleDelete( p_vlm, p_schedule );
                return ExecuteSyntaxError( psz_cmd, pp_status );
            }

633 634
            if( vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] ) )
                goto error;
635 636 637
            i++;
        }
    }
638
    *pp_status = vlm_MessageSimpleNew( psz_cmd );
Antoine Cellerier's avatar
Antoine Cellerier committed
639

640 641 642 643
    vlc_mutex_lock( &p_vlm->lock_manage );
    p_vlm->input_state_changed = true;
    vlc_cond_signal( &p_vlm->wait_manage );
    vlc_mutex_unlock( &p_vlm->lock_manage );
Antoine Cellerier's avatar
Antoine Cellerier committed
644

645
    return VLC_SUCCESS;
646 647 648 649 650

error:
    *pp_status = vlm_MessageNew( psz_cmd, "Error while setting the property '%s' to the schedule",
                                 ppsz_property[i] );
    return VLC_EGENERIC;
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
}

static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, bool b_new,
                                 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
{
    const char *psz_cmd = b_new ? "new" : "setup";
    vlm_media_t *p_cfg = NULL;
    int i_result;
    int i;

#undef ERROR
#undef MISSING
#define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
    if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
        ERROR( "unknown media" );

#define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
    for( i = 0; i < i_property; i++ )
    {
        const char *psz_option = ppsz_property[i];
        const char *psz_value = i+1 < i_property ? ppsz_property[i+1] :  NULL;

        if( !strcmp( psz_option, "enabled" ) )
        {
            p_cfg->b_enabled = true;
        }
        else if( !strcmp( psz_option, "disabled" ) )
        {
            p_cfg->b_enabled = false;
        }
        else if( !strcmp( psz_option, "input" ) )
        {
            MISSING( "input" );
            TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
            i++;
        }
        else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
        {
            while( p_cfg->i_input > 0 )
                TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
            i++;
        }
        else if( !strcmp( psz_option, "inputdel" ) )
        {
            int j;

            MISSING( "inputdel" );

            for( j = 0; j < p_cfg->i_input; j++ )
            {
                if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
                {
                    TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
                    break;
                }
            }
            i++;
        }
        else if( !strcmp( psz_option, "inputdeln" ) )
        {
            MISSING( "inputdeln" );
712

713 714 715
            int idx = atoi( psz_value );
            if( idx > 0 && idx <= p_cfg->i_input )
                TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[idx-1] );
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
            i++;
        }
        else if( !strcmp( psz_option, "output" ) )
        {
            MISSING( "output" );

            free( p_cfg->psz_output );
            p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
            i++;
        }
        else if( !strcmp( psz_option, "option" ) )
        {
            MISSING( "option" );

            TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
            i++;
        }
        else if( !strcmp( psz_option, "loop" ) )
        {
            if( p_cfg->b_vod )
                ERROR( "invalid loop option for vod" );
            p_cfg->broadcast.b_loop = true;
        }
        else if( !strcmp( psz_option, "unloop" ) )
        {
            if( p_cfg->b_vod )
                ERROR( "invalid unloop option for vod" );
            p_cfg->broadcast.b_loop = false;
        }
        else if( !strcmp( psz_option, "mux" ) )
        {
            MISSING( "mux" );
            if( !p_cfg->b_vod )
                ERROR( "invalid mux option for broadcast" );

            free( p_cfg->vod.psz_mux );
            p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
            i++;
        }
        else
        {
            fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
            ERROR( "Wrong command syntax" );
        }
    }
#undef MISSING
#undef ERROR

    /* */
    i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
    vlm_media_Delete( p_cfg );

768
    *pp_status = vlm_MessageSimpleNew( psz_cmd );
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
    return i_result;

error:
    if( p_cfg )
    {
        if( b_new )
            vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
        vlm_media_Delete( p_cfg );
    }
    return VLC_EGENERIC;
}

static int ExecuteNew( vlm_t *p_vlm, const char *psz_name, const char *psz_type, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
{
    /* Check name */
    if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
    {
        *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
        return VLC_EGENERIC;
    }
    if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
    {
        *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
        return VLC_EGENERIC;
    }
    /* */
    if( !strcmp( psz_type, "schedule" ) )
    {
        vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
        if( !p_schedule )
        {
            *pp_status = vlm_MessageNew( "new", "could not create schedule" );
            return VLC_EGENERIC;
        }
        return ExecuteScheduleProperty( p_vlm, p_schedule, true, i_property, ppsz_property, pp_status );
    }
    else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
    {
        vlm_media_t cfg;
        int64_t id;

        vlm_media_Init( &cfg );
        cfg.psz_name = strdup( psz_name );
        cfg.b_vod = !strcmp( psz_type, "vod" );

        if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
        {
            vlm_media_Clean( &cfg );
            *pp_status = vlm_MessageNew( "new", "could not create media" );
            return VLC_EGENERIC;
        }
        vlm_media_Clean( &cfg );
        return ExecuteMediaProperty( p_vlm, id, true, i_property, ppsz_property, pp_status );
    }
    else
    {
        *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
        return VLC_EGENERIC;
    }
}

static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
{
    if( ExecuteIsSchedule( p_vlm, psz_name ) )
    {
        vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
        return ExecuteScheduleProperty( p_vlm, p_schedule, false, i_property, ppsz_property, pp_status );
    }
    else if( ExecuteIsMedia( p_vlm, psz_name ) )
    {
        int64_t id;
        if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
            goto error;
        return ExecuteMediaProperty( p_vlm, id, false, i_property, ppsz_property, pp_status );
    }

error:
    *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
    return VLC_EGENERIC;
}

int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
                           vlm_message_t **pp_message )
{
    size_t i_command = 0;
854 855 856 857
    size_t i_command_len = strlen( psz_command );
    char *buf = malloc( i_command_len + 1 ), *psz_buf = buf;
    size_t i_ppsz_command_len = (3 + (i_command_len + 1) / 2);
    char **ppsz_command = malloc( i_ppsz_command_len * sizeof(char *) );
858
    vlm_message_t *p_message = NULL;
859 860 861 862
    int i_ret = 0;

    if( !psz_buf || !ppsz_command )
    {
863 864
        p_message = vlm_MessageNew( "Memory error",
                        "allocation failed for command of length %zu",
865 866 867
                        i_command_len );
        goto error;
    }
868 869 870 871 872 873

    /* First, parse the line and cut it */
    while( *psz_command != '\0' )
    {
        const char *psz_temp;

874
        if(isspace ((unsigned char)*psz_command))
875 876 877 878 879 880 881 882
        {
            psz_command++;
            continue;
        }

        /* support for comments */
        if( i_command == 0 && *psz_command == '#')
        {
883
            p_message = vlm_MessageSimpleNew( "" );
884 885 886 887 888 889 890
            goto success;
        }

        psz_temp = FindCommandEnd( psz_command );

        if( psz_temp == NULL )
        {
891
            p_message = vlm_MessageNew( "Incomplete command", "%s", psz_command );
892 893 894
            goto error;
        }

895
        assert (i_command < i_ppsz_command_len);
896 897 898 899 900 901 902 903 904 905 906

        ppsz_command[i_command] = psz_buf;
        memcpy (psz_buf, psz_command, psz_temp - psz_command);
        psz_buf[psz_temp - psz_command] = '\0';

        Unescape (psz_buf, psz_buf);

        i_command++;
        psz_buf += psz_temp - psz_command + 1;
        psz_command = psz_temp;

907
        assert (buf + i_command_len + 1 >= psz_buf);
908 909 910 911 912 913 914 915 916
    }

    /*
     * And then Interpret it
     */

#define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error;  if( (cmd) ) goto error; goto success; }
    if( i_command == 0 )
    {
917
        p_message = vlm_MessageSimpleNew( "" );
918 919 920 921 922 923 924 925 926 927 928 929 930
        goto success;
    }
    else IF_EXECUTE( "del",     (i_command != 2),   ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
    else IF_EXECUTE( "show",    (i_command > 2),    ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
    else IF_EXECUTE( "help",    (i_command != 1),   ExecuteHelp( &p_message ) )
    else IF_EXECUTE( "control", (i_command < 3),    ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
    else IF_EXECUTE( "save",    (i_command != 2),   ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
    else IF_EXECUTE( "export",  (i_command != 1),   ExecuteExport(p_vlm, &p_message) )
    else IF_EXECUTE( "load",    (i_command != 2),   ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
    else IF_EXECUTE( "new",     (i_command < 3),    ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
    else IF_EXECUTE( "setup",   (i_command < 2),    ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
    else
    {
931
        p_message = vlm_MessageNew( ppsz_command[0], "Unknown VLM command" );
932 933 934 935 936 937
        goto error;
    }
#undef IF_EXECUTE

success:
    *pp_message = p_message;
938 939
    free( buf );
    free( ppsz_command );
940 941 942
    return VLC_SUCCESS;

syntax_error:
943 944 945 946
    i_ret = ExecuteSyntaxError( ppsz_command[0], pp_message );
    free( buf );
    free( ppsz_command );
    return i_ret;
947 948 949

error:
    *pp_message = p_message;
950 951
    free( buf );
    free( ppsz_command );
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
    return VLC_EGENERIC;
}

/*****************************************************************************
 * Media handling
 *****************************************************************************/
vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
{
    int i;

    for( i = 0; i < vlm->i_media; i++ )
    {
        if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
            return vlm->media[i];
    }

    return NULL;
}

/*****************************************************************************
 * Schedule handling
 *****************************************************************************/
static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
{
976
    if( !psz_name )
977 978
        return NULL;

979 980
    vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
    if( !p_sched )
981 982 983 984 985 986
        return NULL;

    p_sched->psz_name = strdup( psz_name );
    p_sched->b_enabled = false;
    p_sched->i_command = 0;
    p_sched->command = NULL;
987 988
    p_sched->date = 0;
    p_sched->period = 0;
989 990 991 992 993 994 995 996 997 998
    p_sched->i_repeat = -1;

    TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );

    return p_sched;
}

/* for now, simple delete. After, del with options (last arg) */
void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched )
{
999
    int i;
1000 1001 1002 1003 1004 1005
    if( sched == NULL ) return;

    TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );

    if( vlm->i_schedule == 0 ) free( vlm->schedule );
    free( sched->psz_name );
1006 1007 1008 1009

    for ( i = 0; i < sched->i_command; i++ )
        free( sched->command[i] );
    free( sched->command );
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
    free( sched );
}

static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
{
    int i;

    for( i = 0; i < vlm->i_schedule; i++ )
    {
        if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
        {
            return vlm->schedule[i];
        }
    }

    return NULL;
}

/* Ok, setup schedule command will be able to support only one (argument value) at a time  */
static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
                       const char *psz_value )
{
    if( !strcmp( psz_cmd, "enabled" ) )
    {
        schedule->b_enabled = true;
    }
    else if( !strcmp( psz_cmd, "disabled" ) )
    {
        schedule->b_enabled = false;
    }
    else if( !strcmp( psz_cmd, "date" ) )
    {
        struct tm time;
        const char *p;

        time.tm_sec = 0;         /* seconds */
        time.tm_min = 0;         /* minutes */
        time.tm_hour = 0;        /* hours */
        time.tm_mday = 0;        /* day of the month */
        time.tm_mon = 0;         /* month */
        time.tm_year = 0;        /* year */
        time.tm_wday = 0;        /* day of the week */
        time.tm_yday = 0;        /* day in the year */
        time.tm_isdst = -1;       /* daylight saving time */

        /* date should be year/month/day-hour:minutes:seconds */
        p = strchr( psz_value, '-' );

        if( !strcmp( psz_value, "now" ) )
        {
1060
            schedule->date = 0;
1061
        }
1062
        else if(p == NULL)
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
        {
            return 1;
        }
        else
        {
            unsigned i,j,k;

            switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
            {
                case 1:
                    time.tm_sec = i;
                    break;
                case 2:
                    time.tm_min = i;
                    time.tm_sec = j;
                    break;
                case 3:
                    time.tm_hour = i;
                    time.tm_min = j;
                    time.tm_sec = k;
                    break;
                default:
                    return 1;
            }

            switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
            {
                case 1:
                    time.tm_mday = i;
                    break;
                case 2:
                    time.tm_mon = i - 1;
                    time.tm_mday = j;
                    break;
                case 3:
                    time.tm_year = i - 1900;
                    time.tm_mon = j - 1;
                    time.tm_mday = k;
                    break;
                default:
                    return 1;
            }

1106
            schedule->date = mktime(&time);
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
        }
    }
    else if( !strcmp( psz_cmd, "period" ) )
    {
        struct tm time;
        const char *p;
        const char *psz_time = NULL, *psz_date = NULL;
        unsigned i,j,k;

        /* First, if date or period are modified, repeat should be equal to -1 */
        schedule->i_repeat = -1;

        time.tm_sec = 0;         /* seconds */
        time.tm_min = 0;         /* minutes */
        time.tm_hour = 0;        /* hours */
        time.tm_mday = 0;        /* day of the month */
        time.tm_mon = 0;         /* month */
        time.tm_year = 0;        /* year */
        time.tm_wday = 0;        /* day of the week */
        time.tm_yday = 0;        /* day in the year */
        time.tm_isdst = -1;       /* daylight saving time */

        /* date should be year/month/day-hour:minutes:seconds */
        p = strchr( psz_value, '-' );
        if( p )
        {
            psz_date = psz_value;
            psz_time = p + 1;
        }
        else
        {
            psz_time = psz_value;
        }

        switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
        {
            case 1:
                time.tm_sec = i;
                break;
            case 2:
                time.tm_min = i;
                time.tm_sec = j;
                break;
            case 3:
                time.tm_hour = i;
                time.tm_min = j;
                time.tm_sec = k;
                break;
            default:
                return 1;
        }
        if( psz_date )
        {
            switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
            {
                case 1:
                    time.tm_mday = i;
                    break;
                case 2:
                    time.tm_mon = i;
                    time.tm_mday = j;
                    break;
                case 3:
                    time.tm_year = i;
                    time.tm_mon = j;
                    time.tm_mday = k;
                    break;
                default:
                    return 1;
            }
        }

        /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1180 1181 1182
        schedule->period = ((((time.tm_year * 12 + time.tm_mon) * 30
            + time.tm_mday) * 24 + time.tm_hour) * 60 + time.tm_min) * 60
            + time.tm_sec;
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
    }
    else if( !strcmp( psz_cmd, "repeat" ) )
    {
        int i;

        if( sscanf( psz_value, "%d", &i ) == 1 )
        {
            schedule->i_repeat = i;
        }
        else
        {
            return 1;
        }
    }
    else if( !strcmp( psz_cmd, "append" ) )
    {
        char *command = strdup( psz_value );

        TAB_APPEND( schedule->i_command, schedule->command, command );
    }
    else
    {
        return 1;
    }
Antoine Cellerier's avatar
Antoine Cellerier committed
1207

1208 1209 1210 1211 1212 1213
    return 0;
}

/*****************************************************************************
 * Message handling functions
 *****************************************************************************/
1214
vlm_message_t *vlm_MessageSimpleNew( const char *psz_name )
1215 1216 1217
{
    if( !psz_name ) return NULL;

1218 1219
    vlm_message_t *p_message = malloc( sizeof(*p_message) );
    if( !p_message )
1220 1221
        return NULL;

1222 1223
    p_message->psz_name = strdup( psz_name );
    if( !p_message->psz_name )
1224
    {
1225 1226
        free( p_message );
        return NULL;
1227
    }
1228
    p_message->psz_value = NULL;
1229 1230 1231 1232 1233 1234
    p_message->i_child = 0;
    p_message->child = NULL;

    return p_message;
}

1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
vlm_message_t *vlm_MessageNew( const char *psz_name,
                               const char *psz_format, ... )
{
    vlm_message_t *p_message = vlm_MessageSimpleNew( psz_name );
    va_list args;

    if( !p_message )
        return NULL;

    assert( psz_format );
    va_start( args, psz_format );
    if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
        p_message->psz_value = NULL;
    va_end( args );

    if( !p_message->psz_value )
    {
        vlm_MessageDelete( p_message );
        return NULL;
    }
    return p_message;
}

1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
void vlm_MessageDelete( vlm_message_t *p_message )
{
    free( p_message->psz_name );
    free( p_message->psz_value );
    while( p_message->i_child-- )
        vlm_MessageDelete( p_message->child[p_message->i_child] );
    free( p_message->child );
    free( p_message );
}

/* Add a child */
vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
                               vlm_message_t *p_child )
{
    if( p_message == NULL ) return NULL;

    if( p_child )
    {
        TAB_APPEND( p_message->i_child, p_message->child, p_child );
    }

    return p_child;
}

/*****************************************************************************
 * Misc utility functions
 *****************************************************************************/
static vlm_message_t *vlm_ShowMedia( vlm_media_sys_t *p_media )
{
    vlm_media_t *p_cfg = &p_media->cfg;
    vlm_message_t *p_msg;
    vlm_message_t *p_msg_sub;
    int i;

1292
    p_msg = vlm_MessageSimpleNew( p_cfg->psz_name );
1293 1294 1295 1296 1297 1298 1299
    vlm_MessageAdd( p_msg,
                    vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
    vlm_MessageAdd( p_msg,
                    vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );

    if( p_cfg->b_vod )
        vlm_MessageAdd( p_msg,
1300
                        vlm_MessageNew( "mux", "%s", p_cfg->vod.psz_mux ) );
1301 1302 1303 1304
    else
        vlm_MessageAdd( p_msg,
                        vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );

1305
    p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "inputs" ) );
1306 1307 1308 1309 1310 1311
    for( i = 0; i < p_cfg->i_input; i++ )
    {
        char *psz_tmp;
        if( asprintf( &psz_tmp, "%d", i+1 ) != -1 )
        {
            vlm_MessageAdd( p_msg_sub,
1312
                       vlm_MessageNew( psz_tmp, "%s", p_cfg->ppsz_input[i] ) );
1313 1314 1315 1316 1317
            free( psz_tmp );
        }
    }

    vlm_MessageAdd( p_msg,
1318
                    vlm_MessageNew( "output", "%s", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1319

1320
    p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "options" ) );
1321
    for( i = 0; i < p_cfg->i_option; i++ )
1322
        vlm_MessageAdd( p_msg_sub, vlm_MessageSimpleNew( p_cfg->ppsz_option[i] ) );
1323

1324
    p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "instances" ) );
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
    for( i = 0; i < p_media->i_instance; i++ )
    {
        vlm_media_instance_sys_t *p_instance = p_media->instance[i];
        vlc_value_t val;
        vlm_message_t *p_msg_instance;

        val.i_int = END_S;
        if( p_instance->p_input )
            var_Get( p_instance->p_input, "state", &val );

1335
        p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageSimpleNew( "instance" ) );
1336 1337

        vlm_MessageAdd( p_msg_instance,
1338
                        vlm_MessageNew( "name" , "%s", p_instance->psz_name ? p_instance->psz_name : "default" ) );
1339 1340 1341 1342 1343 1344 1345 1346 1347
        vlm_MessageAdd( p_msg_instance,
                        vlm_MessageNew( "state",
                            val.i_int == PLAYING_S ? "playing" :
                            val.i_int == PAUSE_S ? "paused" :
                            "stopped" ) );

        /* FIXME should not do that this way */
        if( p_instance->p_input )
        {
1348 1349 1350
#define APPEND_INPUT_INFO( key, format, type ) \
            vlm_MessageAdd( p_msg_instance, vlm_MessageNew( key, format, \
                            var_Get ## type( p_instance->p_input, key ) ) )
1351
            APPEND_INPUT_INFO( "position", "%f", Float );
1352 1353
            APPEND_INPUT_INFO( "time", "%"PRId64, Integer );
            APPEND_INPUT_INFO( "length", "%"PRId64, Integer );
1354
            APPEND_INPUT_INFO( "rate", "%f", Float );
1355 1356
            APPEND_INPUT_INFO( "title", "%"PRId64, Integer );
            APPEND_INPUT_INFO( "chapter", "%"PRId64, Integer );
1357
            APPEND_INPUT_INFO( "can-seek", "%d", Bool );
1358 1359
        }
#undef APPEND_INPUT_INFO
1360 1361
        vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex",
                        "%d", p_instance->i_index + 1 ) );
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
    }
    return p_msg;
}

static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
                                vlm_schedule_sys_t *schedule,
                                const char *psz_filter )
{
    if( media != NULL )
    {
1372
        vlm_message_t *p_msg = vlm_MessageSimpleNew( "show" );
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
        if( p_msg )
            vlm_MessageAdd( p_msg, vlm_ShowMedia( media ) );
        return p_msg;
    }

    else if( schedule != NULL )
    {
        int i;
        vlm_message_t *msg;
        vlm_message_t *msg_schedule;
        vlm_message_t *msg_child;
        char buffer[100];

1386
        msg = vlm_MessageSimpleNew( "show" );
1387
        msg_schedule =
1388
            vlm_MessageAdd( msg, vlm_MessageSimpleNew( schedule->psz_name ) );
1389 1390 1391 1392 1393 1394 1395

        vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );

        vlm_MessageAdd( msg_schedule,
                        vlm_MessageNew( "enabled", schedule->b_enabled ?
                                        "yes" : "no" ) );

1396
        if( schedule->date != 0 )
1397 1398 1399
        {
            struct tm date;

1400
            localtime_r( &schedule->date, &date);
1401 1402 1403 1404 1405
            vlm_MessageAdd( msg_schedule,
                            vlm_MessageNew( "date", "%d/%d/%d-%d:%d:%d",
                                            date.tm_year + 1900, date.tm_mon + 1,
                                            date.tm_mday, date.tm_hour, date.tm_min,
                                            date.tm_sec ) );
1406 1407 1408 1409
        }
        else
            vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );

1410
        if( schedule->period != 0 )
1411
        {
1412
            div_t d;
1413 1414
            struct tm date;

1415 1416 1417 1418 1419 1420
            d = div(schedule->period, 60);
            date.tm_sec = d.rem;
            d = div(d.quot, 60);
            date.tm_min = d.rem;
            d = div(d.quot, 24);
            date.tm_hour = d.rem;
1421
            /* okay, okay, months are not always 30 days long */
1422 1423 1424 1425 1426
            d = div(d.quot, 30);
            date.tm_mday = d.rem;
            d = div(d.quot, 12);
            date.tm_mon = d.rem;
            date.tm_year = d.quot;
1427 1428 1429 1430

            sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
                     date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);

1431
            vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "%s", buffer) );
1432 1433 1434 1435 1436
        }
        else
            vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );

        sprintf( buffer, "%d", schedule->i_repeat );
1437
        vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", "%s", buffer ) );
1438 1439

        msg_child =
1440
            vlm_MessageAdd( msg_schedule, vlm_MessageSimpleNew("commands" ) );
1441 1442 1443 1444

        for( i = 0; i < schedule->i_command; i++ )
        {
           vlm_MessageAdd( msg_child,
1445
                           vlm_MessageSimpleNew( schedule->command[i] ) );
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
        }

        return msg;

    }

    else if( psz_filter && !strcmp( psz_filter, "media" ) )
    {
        vlm_message_t *p_msg;
        vlm_message_t *p_msg_child;
        int i_vod = 0, i_broadcast = 0;

1458
        for( int i = 0; i < vlm->i_media; i++ )
1459 1460 1461 1462 1463 1464 1465
        {
            if( vlm->media[i]->cfg.b_vod )
                i_vod++;
            else
                i_broadcast++;
        }

1466
        p_msg = vlm_MessageSimpleNew( "show" );
1467 1468 1469
        p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media",
                                      "( %d broadcast - %d vod )", i_broadcast,
                                      i_vod ) );
1470

1471
        for( int i = 0; i < vlm->i_media; i++ )
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
            vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm->media[i] ) );

        return p_msg;
    }

    else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
    {
        int i;
        vlm_message_t *msg;
        vlm_message_t *msg_child;

1483 1484
        msg = vlm_MessageSimpleNew( "show" );
        msg_child = vlm_MessageAdd( msg, vlm_MessageSimpleNew( "schedule" ) );
1485 1486 1487 1488 1489

        for( i = 0; i < vlm->i_schedule; i++ )
        {
            vlm_schedule_sys_t *s = vlm->schedule[i];
            vlm_message_t *msg_schedule;
1490
            time_t now, next_date;
1491 1492

            msg_schedule = vlm_MessageAdd( msg_child,
1493
                                           vlm_MessageSimpleNew( s->psz_name ) );
1494 1495 1496 1497 1498
            vlm_MessageAdd( msg_schedule,
                            vlm_MessageNew( "enabled", s->b_enabled ?
                                            "yes" : "no" ) );

            /* calculate next date */
1499 1500
            time(&now);
            next_date = s->date;
1501

1502
            if( s->period != 0 )
1503 1504
            {
                int j = 0;
1505 1506
                while( ((s->date + j * s->period) <= now) &&
                       ( s->i_repeat > j || s->i_repeat < 0 ) )
1507 1508 1509 1510
                {
                    j++;
                }

1511
                next_date = s->date + j * s->period;
1512 1513
            }

1514
            if( next_date > now )
1515
            {
1516 1517
                struct tm tm;
                char psz_date[32];
1518

1519
                strftime( psz_date, sizeof(psz_date), "%Y-%m-%d %H:%M:%S (%a)",
1520
                          localtime_r( &next_date, &tm ) );
1521
                vlm_MessageAdd( msg_schedule,
1522
                                vlm_MessageNew( "next launch", "%s", psz_date ) );
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
            }
        }

        return msg;
    }

    else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
    {
        vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
        vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );

        vlm_MessageAdd( show1, show2->child[0] );

        /* We must destroy the parent node "show" of show2
         * and not the children */
        free( show2->psz_name );
        free( show2 );

        return show1;
    }

    else
    {
1546
        return vlm_MessageSimpleNew( "show" );
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
    }
}

/*****************************************************************************
 * Config handling functions
 *****************************************************************************/
static int Load( vlm_t *vlm, char *file )
{
    char *pf = file;
    int  i_line = 1;

    while( *pf != '\0' )
    {
        vlm_message_t *message = NULL;
        int i_end = 0;

        while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
        {
            i_end++;
        }

        if( pf[i_end] == '\r' || pf[i_end] == '\n' )
        {
            pf[i_end] = '\0';
            i_end++;
            if( pf[i_end] == '\n' ) i_end++;
        }

        if( *pf && ExecuteCommand( vlm, pf, &message ) )
        {
            if( message )
            {
                if( message->psz_value )
                    msg_Err( vlm, "Load error on line %d: %s: %s",
                             i_line, message->psz_name, message->psz_value );
                vlm_MessageDelete( message );
            }
            return 1;
        }
        if( message ) vlm_MessageDelete( message );

        pf += i_end;
        i_line++;
    }

    return 0;
}

static char *Save( vlm_t *vlm )
{
1597 1598 1599
    const char *psz_header = "\n"
                             "# VLC media player VLM command batch\n"
                             "# http://www.videolan.org/vlc/\n\n" ;
1600

1601
    struct vlc_memstream stream;
1602

1603 1604
    vlc_memstream_open( &stream );
    vlc_memstream_puts( &stream, psz_header );
1605

1606
    for( int i = 0; i < vlm->i_media; i++ )
1607 1608 1609 1610
    {
        vlm_media_sys_t *media = vlm->media[i];
        vlm_media_t *p_cfg = &media->cfg;

1611 1612 1613
        vlc_memstream_printf( &stream, "new %s %s %sabled", p_cfg->psz_name,
                              p_cfg->b_vod ? "vod" : "broadcast",
                              p_cfg->b_enabled ? "en" : "dis" );
1614 1615

        if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1616 1617
            vlc_memstream_puts( &stream, " loop" );
        vlc_memstream_putc( &stream, '\n' );
1618

1619 1620 1621
        for( int j = 0; j < p_cfg->i_input; j++ )
            vlc_memstream_printf( &stream, "setup %s input \"%s\"\n",
                                  p_cfg->psz_name, p_cfg->ppsz_input[j] );
1622 1623

        if( p_cfg->psz_output )
1624 1625
            vlc_memstream_printf( &stream, "setup %s output %s\n",
                                  p_cfg->psz_name, p_cfg->psz_output );
1626

1627 1628 1629
        for( int j = 0; j < p_cfg->i_option; j++ )
            vlc_memstream_printf( &stream, "setup %s option %s\n",
                                  p_cfg->psz_name, p_cfg->ppsz_option[j] );
1630 1631

        if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1632 1633
            vlc_memstream_printf( &stream, "setup %s mux %s\n",
                                  p_cfg->psz_name, p_cfg->vod.psz_mux );
1634 1635 1636
    }

    /* and now, the schedule scripts */
1637
    for( int i = 0; i < vlm->i_schedule; i++ )
1638 1639
    {
        vlm_schedule_sys_t *schedule = vlm->schedule[i];
1640
        struct tm tm;
1641

1642 1643 1644 1645 1646 1647 1648
        localtime_r( &schedule->date, &tm );
        vlc_memstream_printf( &stream, "new %s schedule date "
                              "%d/%d/%d-%d:%d:%d %sabled\n",
                              schedule->psz_name,
                              tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
                              tm.tm_hour, tm.tm_min, tm.tm_sec,
                              schedule->b_enabled ? "en" : "dis" );
1649

1650
        if( schedule->period != 0 )
1651
        {
1652
            div_t d;
1653

1654
            d = div(schedule->period, 60);
1655
            tm.tm_sec = d.rem;
1656
            d = div(d.quot, 60);
1657
            tm.tm_min = d.rem;
1658
            d = div(d.quot, 24);
1659
            tm.tm_hour = d.rem;
1660
            d = div(d.quot, 30);
1661
            tm.tm_mday = d.rem;
1662
            /* okay, okay, months are not always 30 days long */
1663
            d = div(d.quot, 12);
1664 1665 1666 1667 1668 1669 1670 1671
            tm.tm_mon = d.rem;
            tm.tm_year = d.quot;

            vlc_memstream_printf( &stream, "setup %s "
                                  "period %d/%d/%d-%d:%d:%d\n",
                                  schedule->psz_name,
                                  tm.tm_year, tm.tm_mon, tm.tm_mday,
                                  tm.tm_hour, tm.tm_min, tm.tm_sec);
1672 1673 1674
        }

        if( schedule->i_repeat >= 0 )
1675 1676 1677
            vlc_memstream_printf( &stream, "setup %s repeat %d",
                                  schedule->psz_name, schedule->i_repeat );
        vlc_memstream_putc( &stream, '\n' );
1678

1679 1680 1681
        for( int j = 0; j < schedule->i_command; j++ )
            vlc_memstream_printf( &stream, "setup %s append %s\n",
                                  schedule->psz_name, schedule->command[j] );
1682 1683
    }

1684 1685 1686
    if( vlc_memstream_close( &stream ) )
        return NULL;
    return stream.ptr;
1687 1688 1689
}

#endif /* ENABLE_VLM */