macro.c 38.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*****************************************************************************
 * macro.c : Custom <vlc> macro handling
 *****************************************************************************
 * Copyright (C) 2001-2005 the VideoLAN team
 * $Id: http.c 12225 2005-08-18 10:01:30Z massiot $
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
 *          Laurent Aimar <fenrir@via.ecp.fr>
 *          Christophe Massiot <massiot@via.ecp.fr>
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include "http.h"
#include "macros.h"

29
int E_(MacroParse)( macro_t *m, char *psz_src )
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
{
    char *dup = strdup( (char *)psz_src );
    char *src = dup;
    char *p;
    int     i_skip;

#define EXTRACT( name, l ) \
        src += l;    \
        p = strchr( src, '"' );             \
        if( p )                             \
        {                                   \
            *p++ = '\0';                    \
        }                                   \
        m->name = strdup( src );            \
        if( !p )                            \
        {                                   \
            break;                          \
        }                                   \
        src = p;

    /* init m */
    m->id = NULL;
    m->param1 = NULL;
    m->param2 = NULL;

    /* parse */
    src += 4;

    while( *src )
    {
        while( *src == ' ')
        {
            src++;
        }
        if( !strncmp( src, "id=\"", 4 ) )
        {
            EXTRACT( id, 4 );
        }
        else if( !strncmp( src, "param1=\"", 8 ) )
        {
            EXTRACT( param1, 8 );
        }
        else if( !strncmp( src, "param2=\"", 8 ) )
        {
            EXTRACT( param2, 8 );
        }
        else
        {
            break;
        }
    }
    if( strstr( src, "/>" ) )
    {
        src = strstr( src, "/>" ) + 2;
    }
    else
    {
        src += strlen( src );
    }

    if( m->id == NULL )
    {
        m->id = strdup( "" );
    }
    if( m->param1 == NULL )
    {
        m->param1 = strdup( "" );
    }
    if( m->param2 == NULL )
    {
        m->param2 = strdup( "" );
    }
    i_skip = src - dup;

    free( dup );
    return i_skip;
#undef EXTRACT
}

109
void E_(MacroClean)( macro_t *m )
110 111 112 113 114 115
{
    free( m->id );
    free( m->param1 );
    free( m->param2 );
}

116
int E_(StrToMacroType)( char *name )
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
{
    int i;

    if( !name || *name == '\0')
    {
        return MVLC_UNKNOWN;
    }
    for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
    {
        if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
        {
            return StrToMacroTypeTab[i].i_type;
        }
    }
    return MVLC_UNKNOWN;
}

134
void E_(MacroDo)( httpd_file_sys_t *p_args,
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
                     macro_t *m,
                     char *p_request, int i_request,
                     char **pp_data,  int *pi_data,
                     char **pp_dst )
{
    intf_thread_t  *p_intf = p_args->p_intf;
    intf_sys_t     *p_sys = p_args->p_intf->p_sys;
    char control[512];

#define ALLOC( l ) \
    {               \
        int __i__ = *pp_dst - *pp_data; \
        *pi_data += (l);                  \
        *pp_data = realloc( *pp_data, *pi_data );   \
        *pp_dst = (*pp_data) + __i__;   \
    }
#define PRINT( str ) \
    ALLOC( strlen( str ) + 1 ); \
    *pp_dst += sprintf( *pp_dst, str );

#define PRINTS( str, s ) \
    ALLOC( strlen( str ) + strlen( s ) + 1 ); \
    { \
        char * psz_cur = *pp_dst; \
        *pp_dst += sprintf( *pp_dst, str, s ); \
        while( psz_cur && *psz_cur ) \
        {  \
            /* Prevent script injection */ \
            if( *psz_cur == '<' ) *psz_cur = '*'; \
            if( *psz_cur == '>' ) *psz_cur = '*'; \
            psz_cur++ ; \
        } \
    }

169
    switch( E_(StrToMacroType)( m->id ) )
170 171 172 173 174 175
    {
        case MVLC_CONTROL:
            if( i_request <= 0 )
            {
                break;
            }
176
            E_(ExtractURIValue)( p_request, "control", control, 512 );
177 178 179 180 181
            if( *m->param1 && !strstr( m->param1, control ) )
            {
                msg_Warn( p_intf, "unauthorized control=%s", control );
                break;
            }
182
            switch( E_(StrToMacroType)( control ) )
183 184 185 186 187 188
            {
                case MVLC_PLAY:
                {
                    int i_item;
                    char item[512];

189
                    E_(ExtractURIValue)( p_request, "item", item, 512 );
190
                    i_item = atoi( item );
191 192 193 194 195 196 197
                    /* id = 0 : simply ask playlist to play */
                    if( i_item == 0 )
                    {
                        playlist_Play( p_sys->p_playlist );
                        msg_Dbg( p_intf, "requested playlist play" );
                        break;
                    }
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
                    playlist_Control( p_sys->p_playlist, PLAYLIST_ITEMPLAY,
                                      playlist_ItemGetById( p_sys->p_playlist,
                                      i_item ) );
                    msg_Dbg( p_intf, "requested playlist item: %i", i_item );
                    break;
                }
                case MVLC_STOP:
                    playlist_Control( p_sys->p_playlist, PLAYLIST_STOP );
                    msg_Dbg( p_intf, "requested playlist stop" );
                    break;
                case MVLC_PAUSE:
                    playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
                    msg_Dbg( p_intf, "requested playlist pause" );
                    break;
                case MVLC_NEXT:
                    playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
                    msg_Dbg( p_intf, "requested playlist next" );
                    break;
                case MVLC_PREVIOUS:
                    playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1 );
                    msg_Dbg( p_intf, "requested playlist previous" );
                    break;
                case MVLC_FULLSCREEN:
                    if( p_sys->p_input )
                    {
                        vout_thread_t *p_vout;
                        p_vout = vlc_object_find( p_sys->p_input,
                                                  VLC_OBJECT_VOUT, FIND_CHILD );

                        if( p_vout )
                        {
                            p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
                            vlc_object_release( p_vout );
                            msg_Dbg( p_intf, "requested fullscreen toggle" );
                        }
                    }
                    break;
                case MVLC_SEEK:
                {
                    char value[30];
238 239 240
                    E_(ExtractURIValue)( p_request, "seek_value", value, 30 );
                    E_(DecodeEncodedURI)( value );
                    E_(HandleSeek)( p_intf, value );
241 242 243 244 245 246 247 248
                    break;
                }
                case MVLC_VOLUME:
                {
                    char vol[8];
                    audio_volume_t i_volume;
                    int i_value;

249
                    E_(ExtractURIValue)( p_request, "value", vol, 8 );
250
                    aout_VolumeGet( p_intf, &i_volume );
251
                    E_(DecodeEncodedURI)( vol );
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 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 304 305 306

                    if( vol[0] == '+' )
                    {
                        i_value = atoi( vol + 1 );
                        if( (i_volume + i_value) > AOUT_VOLUME_MAX )
                        {
                            aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
                            msg_Dbg( p_intf, "requested volume set: max" );
                        }
                        else
                        {
                            aout_VolumeSet( p_intf , (i_volume + i_value) );
                            msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
                        }
                    }
                    else if( vol[0] == '-' )
                    {
                        i_value = atoi( vol + 1 );
                        if( (i_volume - i_value) < AOUT_VOLUME_MIN )
                        {
                            aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
                            msg_Dbg( p_intf, "requested volume set: min" );
                        }
                        else
                        {
                            aout_VolumeSet( p_intf , (i_volume - i_value) );
                            msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
                        }
                    }
                    else if( strstr(vol, "%") != NULL )
                    {
                        i_value = atoi( vol );
                        if( (i_value <= 400) && (i_value>=0) ){
                            aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/400+AOUT_VOLUME_MIN);
                            msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
                        }
                    }
                    else
                    {
                        i_value = atoi( vol );
                        if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
                        {
                            aout_VolumeSet( p_intf , atoi( vol ) );
                            msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
                        }
                    }
                    break;
                }

                /* playlist management */
                case MVLC_ADD:
                {
                    char mrl[1024], psz_name[1024];
                    playlist_item_t *p_item;

307 308 309 310
                    E_(ExtractURIValue)( p_request, "mrl", mrl, 1024 );
                    E_(DecodeEncodedURI)( mrl );
                    E_(ExtractURIValue)( p_request, "name", psz_name, 1024 );
                    E_(DecodeEncodedURI)( psz_name );
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
                    if( !*psz_name )
                    {
                        memcpy( psz_name, mrl, 1024 );
                    }
                    p_item = E_(MRLParse)( p_intf, mrl, psz_name );

                    if( !p_item || !p_item->input.psz_uri ||
                        !*p_item->input.psz_uri )
                    {
                        msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
                    }
                    else
                    {
                        playlist_AddItem( p_sys->p_playlist, p_item,
                                          PLAYLIST_APPEND, PLAYLIST_END );
                        msg_Dbg( p_intf, "requested mrl add: %s", mrl );
                    }

                    break;
                }
                case MVLC_DEL:
                {
                    int i_item, *p_items = NULL, i_nb_items = 0;
                    char item[512], *p_parser = p_request;

                    /* Get the list of items to delete */
                    while( (p_parser =
338
                            E_(ExtractURIValue)( p_parser, "item", item, 512 )) )
339 340 341 342 343 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
                    {
                        if( !*item ) continue;

                        i_item = atoi( item );
                        p_items = realloc( p_items, (i_nb_items + 1) *
                                           sizeof(int) );
                        p_items[i_nb_items] = i_item;
                        i_nb_items++;
                    }

                    if( i_nb_items )
                    {
                        int i;
                        for( i = 0; i < i_nb_items; i++ )
                        {
                            playlist_LockDelete( p_sys->p_playlist, p_items[i] );
                            msg_Dbg( p_intf, "requested playlist delete: %d",
                                     p_items[i] );
                            p_items[i] = -1;
                        }
                    }

                    if( p_items ) free( p_items );
                    break;
                }
                case MVLC_KEEP:
                {
                    int i_item, *p_items = NULL, i_nb_items = 0;
                    char item[512], *p_parser = p_request;
                    int i,j;

                    /* Get the list of items to keep */
                    while( (p_parser =
372
                       E_(ExtractURIValue)( p_parser, "item", item, 512 )) )
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
                    {
                        if( !*item ) continue;

                        i_item = atoi( item );
                        p_items = realloc( p_items, (i_nb_items + 1) *
                                           sizeof(int) );
                        p_items[i_nb_items] = i_item;
                        i_nb_items++;
                    }

                    for( i = p_sys->p_playlist->i_size - 1 ; i >= 0; i-- )
                    {
                        /* Check if the item is in the keep list */
                        for( j = 0 ; j < i_nb_items ; j++ )
                        {
                            if( p_items[j] ==
                                p_sys->p_playlist->pp_items[i]->input.i_id ) break;
                        }
                        if( j == i_nb_items )
                        {
                            playlist_LockDelete( p_sys->p_playlist, p_sys->p_playlist->pp_items[i]->input.i_id );
                            msg_Dbg( p_intf, "requested playlist delete: %d",
                                     i );
                        }
                    }

                    if( p_items ) free( p_items );
                    break;
                }
                case MVLC_EMPTY:
                {
                    playlist_LockClear( p_sys->p_playlist );
                    msg_Dbg( p_intf, "requested playlist empty" );
                    break;
                }
                case MVLC_SORT:
                {
                    char type[12];
                    char order[2];
                    char item[512];
                    int i_order;
                    int i_item;

416 417 418
                    E_(ExtractURIValue)( p_request, "type", type, 12 );
                    E_(ExtractURIValue)( p_request, "order", order, 2 );
                    E_(ExtractURIValue)( p_request, "item", item, 512 );
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 445 446 447 448 449 450 451 452 453 454 455 456
                    i_item = atoi( item );

                    if( order[0] == '0' ) i_order = ORDER_NORMAL;
                    else i_order = ORDER_REVERSE;

                    if( !strcmp( type , "title" ) )
                    {
                        playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
                                                    p_sys->p_playlist->pp_views[0]->p_root,
                                                    SORT_TITLE_NODES_FIRST,
                                                    ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
                        msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
                    }
                    else if( !strcmp( type , "author" ) )
                    {
                        playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
                                                    p_sys->p_playlist->pp_views[0]->p_root,
                                                    SORT_AUTHOR,
                                                    ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
                        msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
                    }
                    else if( !strcmp( type , "shuffle" ) )
                    {
                        playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
                                                    p_sys->p_playlist->pp_views[0]->p_root,
                                                    SORT_RANDOM,
                                                    ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
                        msg_Dbg( p_intf, "requested playlist shuffle");
                    }

                    break;
                }
                case MVLC_MOVE:
                {
                    char psz_pos[6];
                    char psz_newpos[6];
                    int i_pos;
                    int i_newpos;
457 458
                    E_(ExtractURIValue)( p_request, "psz_pos", psz_pos, 6 );
                    E_(ExtractURIValue)( p_request, "psz_newpos", psz_newpos, 6 );
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
                    i_pos = atoi( psz_pos );
                    i_newpos = atoi( psz_newpos );
                    if ( i_pos < i_newpos )
                    {
                        playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
                    }
                    else
                    {
                        playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
                    }
                    msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
                    break;
                }

                /* admin function */
                case MVLC_CLOSE:
                {
                    char id[512];
477
                    E_(ExtractURIValue)( p_request, "id", id, 512 );
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
                    msg_Dbg( p_intf, "requested close id=%s", id );
#if 0
                    if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
                    {
                        msg_Warn( p_intf, "close failed for id=%s", id );
                    }
#endif
                    break;
                }
                case MVLC_SHUTDOWN:
                {
                    msg_Dbg( p_intf, "requested shutdown" );
                    p_intf->p_vlc->b_die = VLC_TRUE;
                    break;
                }
                /* vlm */
                case MVLC_VLM_NEW:
                case MVLC_VLM_SETUP:
                {
                    static const char *vlm_properties[11] =
                    {
                        /* no args */
                        "enabled", "disabled", "loop", "unloop",
                        /* args required */
                        "input", "output", "option", "date", "period", "repeat", "append",
                    };
                    vlm_message_t *vlm_answer;
                    char name[512];
                    char *psz = malloc( strlen( p_request ) + 1000 );
                    char *p = psz;
                    char *vlm_error;
                    int i;

                    if( p_intf->p_sys->p_vlm == NULL )
                        p_intf->p_sys->p_vlm = vlm_New( p_intf );

                    if( p_intf->p_sys->p_vlm == NULL ) break;

516
                    E_(ExtractURIValue)( p_request, "name", name, 512 );
517
                    if( E_(StrToMacroType)( control ) == MVLC_VLM_NEW )
518 519
                    {
                        char type[20];
520
                        E_(ExtractURIValue)( p_request, "type", type, 20 );
521 522 523 524 525 526 527 528 529 530
                        p += sprintf( psz, "new %s %s", name, type );
                    }
                    else
                    {
                        p += sprintf( psz, "setup %s", name );
                    }
                    /* Parse the request */
                    for( i = 0; i < 11; i++ )
                    {
                        char val[512];
531
                        E_(ExtractURIValue)( p_request,
532
                                               vlm_properties[i], val, 512 );
533
                        E_(DecodeEncodedURI)( val );
534 535 536 537
                        if( strlen( val ) > 0 && i >= 4 )
                        {
                            p += sprintf( p, " %s %s", vlm_properties[i], val );
                        }
538
                        else if( E_(TestURIParam)( p_request, vlm_properties[i] ) && i < 4 )
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
                        {
                            p += sprintf( p, " %s", vlm_properties[i] );
                        }
                    }
                    vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
                    if( vlm_answer->psz_value == NULL ) /* there is no error */
                    {
                        vlm_error = strdup( "" );
                    }
                    else
                    {
                        vlm_error = malloc( strlen(vlm_answer->psz_name) +
                                            strlen(vlm_answer->psz_value) +
                                            strlen( " : ") + 1 );
                        sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
                                                         vlm_answer->psz_value );
                    }

557
                    E_(mvar_AppendNewVar)( p_args->vars, "vlm_error", vlm_error );
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

                    vlm_MessageDelete( vlm_answer );
                    free( vlm_error );
                    free( psz );
                    break;
                }

                case MVLC_VLM_DEL:
                {
                    vlm_message_t *vlm_answer;
                    char name[512];
                    char psz[512+10];
                    if( p_intf->p_sys->p_vlm == NULL )
                        p_intf->p_sys->p_vlm = vlm_New( p_intf );

                    if( p_intf->p_sys->p_vlm == NULL ) break;

575
                    E_(ExtractURIValue)( p_request, "name", name, 512 );
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
                    sprintf( psz, "del %s", name );

                    vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
                    /* FIXME do a vlm_answer -> var stack conversion */
                    vlm_MessageDelete( vlm_answer );
                    break;
                }

                case MVLC_VLM_PLAY:
                case MVLC_VLM_PAUSE:
                case MVLC_VLM_STOP:
                case MVLC_VLM_SEEK:
                {
                    vlm_message_t *vlm_answer;
                    char name[512];
                    char psz[512+10];
                    if( p_intf->p_sys->p_vlm == NULL )
                        p_intf->p_sys->p_vlm = vlm_New( p_intf );

                    if( p_intf->p_sys->p_vlm == NULL ) break;

597
                    E_(ExtractURIValue)( p_request, "name", name, 512 );
598
                    if( E_(StrToMacroType)( control ) == MVLC_VLM_PLAY )
599
                        sprintf( psz, "control %s play", name );
600
                    else if( E_(StrToMacroType)( control ) == MVLC_VLM_PAUSE )
601
                        sprintf( psz, "control %s pause", name );
602
                    else if( E_(StrToMacroType)( control ) == MVLC_VLM_STOP )
603
                        sprintf( psz, "control %s stop", name );
604
                    else if( E_(StrToMacroType)( control ) == MVLC_VLM_SEEK )
605 606
                    {
                        char percent[20];
607
                        E_(ExtractURIValue)( p_request, "percent", percent, 512 );
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
                        sprintf( psz, "control %s seek %s", name, percent );
                    }

                    vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
                    /* FIXME do a vlm_answer -> var stack conversion */
                    vlm_MessageDelete( vlm_answer );
                    break;
                }
                case MVLC_VLM_LOAD:
                case MVLC_VLM_SAVE:
                {
                    vlm_message_t *vlm_answer;
                    char file[512];
                    char psz[512];

                    if( p_intf->p_sys->p_vlm == NULL )
                        p_intf->p_sys->p_vlm = vlm_New( p_intf );

                    if( p_intf->p_sys->p_vlm == NULL ) break;

628 629
                    E_(ExtractURIValue)( p_request, "file", file, 512 );
                    E_(DecodeEncodedURI)( file );
630

631
                    if( E_(StrToMacroType)( control ) == MVLC_VLM_LOAD )
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
                        sprintf( psz, "load %s", file );
                    else
                        sprintf( psz, "save %s", file );

                    vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
                    /* FIXME do a vlm_answer -> var stack conversion */
                    vlm_MessageDelete( vlm_answer );
                    break;
                }

                default:
                    if( *control )
                    {
                        PRINTS( "<!-- control param(%s) unsupported -->", control );
                    }
                    break;
            }
            break;

        case MVLC_SET:
        {
            char    value[512];
            int     i;
            float   f;

            if( i_request <= 0 ||
                *m->param1  == '\0' ||
                strstr( p_request, m->param1 ) == NULL )
            {
                break;
            }
663 664
            E_(ExtractURIValue)( p_request, m->param1,  value, 512 );
            E_(DecodeEncodedURI)( value );
665

666
            switch( E_(StrToMacroType)( m->param2 ) )
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
            {
                case MVLC_INT:
                    i = atoi( value );
                    config_PutInt( p_intf, m->param1, i );
                    break;
                case MVLC_FLOAT:
                    f = atof( value );
                    config_PutFloat( p_intf, m->param1, f );
                    break;
                case MVLC_STRING:
                    config_PutPsz( p_intf, m->param1, value );
                    break;
                default:
                    PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
            }
            break;
        }
        case MVLC_GET:
        {
            char    value[512];
            int     i;
            float   f;
            char    *psz;

            if( *m->param1  == '\0' )
            {
                break;
            }

696
            switch( E_(StrToMacroType)( m->param2 ) )
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
            {
                case MVLC_INT:
                    i = config_GetInt( p_intf, m->param1 );
                    sprintf( value, "%d", i );
                    break;
                case MVLC_FLOAT:
                    f = config_GetFloat( p_intf, m->param1 );
                    sprintf( value, "%f", f );
                    break;
                case MVLC_STRING:
                    psz = config_GetPsz( p_intf, m->param1 );
                    if( psz != NULL )
                    {
                        strncpy( value, psz,sizeof( value ) );
                        free( psz );
                        value[sizeof( value ) - 1] = '\0';
                    }
                    else
                        *value = '\0';
                    msg_Dbg( p_intf, "%d: value = \"%s\"", __LINE__, value );
                    break;
                default:
                    snprintf( value, sizeof( value ),
                              "invalid type(%s) in set", m->param2 );
                    value[sizeof( value ) - 1] = '\0';
                    break;
            }
            PRINTS( "%s", value );
            break;
        }
        case MVLC_VALUE:
        {
            char *s, *v;

            if( m->param1 )
            {
733 734 735
                E_(EvaluateRPN)( p_intf, p_args->vars, &p_args->stack, m->param1 );
                s = E_(SSPop)( &p_args->stack );
                v = E_(mvar_GetValue)( p_args->vars, s );
736 737 738
            }
            else
            {
739
                v = s = E_(SSPop)( &p_args->stack );
740 741 742 743 744 745 746
            }

            PRINTS( "%s", v );
            free( s );
            break;
        }
        case MVLC_RPN:
747
            E_(EvaluateRPN)( p_intf, p_args->vars, &p_args->stack, m->param1 );
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
            break;

        /* Useful to learn stack management */
        case MVLC_STACK:
        {
            int i;
            msg_Dbg( p_intf, "stack" );
            for (i=0;i<(&p_args->stack)->i_stack;i++)
                msg_Dbg( p_intf, "%d -> %s", i, (&p_args->stack)->stack[i] );
            break;
        }

        case MVLC_UNKNOWN:
        default:
            PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
            msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
            break;
    }
#undef PRINTS
#undef PRINT
#undef ALLOC
}

771
char *E_(MacroSearch)( char *src, char *end, int i_mvlc, vlc_bool_t b_after )
772 773 774 775 776 777 778 779 780 781 782
{
    int     i_id;
    int     i_level = 0;

    while( src < end )
    {
        if( src + 4 < end  && !strncmp( (char *)src, "<vlc", 4 ) )
        {
            int i_skip;
            macro_t m;

783
            i_skip = E_(MacroParse)( &m, src );
784

785
            i_id = E_(StrToMacroType)( m.id );
786 787 788 789 790 791 792 793 794 795 796 797 798 799

            switch( i_id )
            {
                case MVLC_IF:
                case MVLC_FOREACH:
                    i_level++;
                    break;
                case MVLC_END:
                    i_level--;
                    break;
                default:
                    break;
            }

800
            E_(MacroClean)( &m );
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

            if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
                ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
            {
                return src + ( b_after ? i_skip : 0 );
            }
            else if( i_level < 0 )
            {
                return NULL;
            }

            src += i_skip;
        }
        else
        {
            src++;
        }
    }

    return NULL;
}

void E_(Execute)( httpd_file_sys_t *p_args,
                     char *p_request, int i_request,
                     char **pp_data, int *pi_data,
                     char **pp_dst,
                     char *_src, char *_end )
{
    intf_thread_t  *p_intf = p_args->p_intf;

    char *src, *dup, *end;
    char *dst = *pp_dst;

    src = dup = malloc( _end - _src + 1 );
    end = src +( _end - _src );

    memcpy( src, _src, _end - _src );
    *end = '\0';

    /* we parse searching <vlc */
    while( src < end )
    {
        char *p;
        int i_copy;

        p = (char *)strstr( (char *)src, "<vlc" );
        if( p < end && p == src )
        {
            macro_t m;

851
            src += E_(MacroParse)( &m, src );
852 853 854

            //msg_Dbg( p_intf, "macro_id=%s", m.id );

855
            switch( E_(StrToMacroType)( m.id ) )
856 857 858 859 860 861 862 863
            {
                case MVLC_INCLUDE:
                {
                    FILE *f;
                    int  i_buffer;
                    char *p_buffer;
                    char psz_file[MAX_DIR_SIZE];
                    char *p;
864
                    char sep;
865

866 867 868 869 870 871 872
#if defined( WIN32 )
                    sep = '\\';
#else
                    sep = '/';
#endif

                    if( m.param1[0] != sep )
873 874
                    {
                        strcpy( psz_file, p_args->file );
875
                        p = strrchr( psz_file, sep );
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
                        if( p != NULL )
                            strcpy( p + 1, m.param1 );
                        else
                            strcpy( psz_file, m.param1 );
                    }
                    else
                    {
                        strcpy( psz_file, m.param1 );
                    }

                    if( ( f = fopen( psz_file, "r" ) ) == NULL )
                    {
                        msg_Warn( p_args->p_intf,
                                  "unable to include file %s (%s)",
                                  psz_file, strerror(errno) );
                        break;
                    }

                    /* first we load in a temporary buffer */
                    E_(FileLoad)( f, &p_buffer, &i_buffer );

                    /* we parse executing all  <vlc /> macros */
                    E_(Execute)( p_args, p_request, i_request, pp_data, pi_data,
                             &dst, &p_buffer[0], &p_buffer[i_buffer] );
                    free( p_buffer );
                    fclose(f);
                    break;
                }
                case MVLC_IF:
                {
                    vlc_bool_t i_test;
                    char    *endif;

909 910
                    E_(EvaluateRPN)( p_intf, p_args->vars, &p_args->stack, m.param1 );
                    if( E_(SSPopN)( &p_args->stack, p_args->vars ) )
911 912 913 914 915 916 917
                    {
                        i_test = 1;
                    }
                    else
                    {
                        i_test = 0;
                    }
918
                    endif = E_(MacroSearch)( src, end, MVLC_END, VLC_TRUE );
919 920 921

                    if( i_test == 0 )
                    {
922
                        char *start = E_(MacroSearch)( src, endif, MVLC_ELSE, VLC_TRUE );
923 924 925

                        if( start )
                        {
926
                            char *stop  = E_(MacroSearch)( start, endif, MVLC_END, VLC_FALSE );
927 928 929 930 931 932 933 934 935 936
                            if( stop )
                            {
                                E_(Execute)( p_args, p_request, i_request,
                                         pp_data, pi_data, &dst, start, stop );
                            }
                        }
                    }
                    else if( i_test == 1 )
                    {
                        char *stop;
937
                        if( ( stop = E_(MacroSearch)( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
938
                        {
939
                            stop = E_(MacroSearch)( src, endif, MVLC_END, VLC_FALSE );
940 941 942 943 944 945 946 947 948 949 950 951 952
                        }
                        if( stop )
                        {
                            E_(Execute)( p_args, p_request, i_request,
                                     pp_data, pi_data, &dst, src, stop );
                        }
                    }

                    src = endif;
                    break;
                }
                case MVLC_FOREACH:
                {
953
                    char *endfor = E_(MacroSearch)( src, end, MVLC_END, VLC_TRUE );
954
                    char *start = src;
955
                    char *stop = E_(MacroSearch)( src, end, MVLC_END, VLC_FALSE );
956 957 958 959 960 961 962 963

                    if( stop )
                    {
                        mvar_t *index;
                        int    i_idx;
                        mvar_t *v;
                        if( !strcmp( m.param2, "integer" ) )
                        {
964 965
                            char *arg = E_(SSPop)( &p_args->stack );
                            index = E_(mvar_IntegerSetNew)( m.param1, arg );
966 967 968 969
                            free( arg );
                        }
                        else if( !strcmp( m.param2, "directory" ) )
                        {
970 971
                            char *arg = E_(SSPop)( &p_args->stack );
                            index = E_(mvar_FileSetNew)( p_intf, m.param1, arg );
972 973 974 975
                            free( arg );
                        }
                        else if( !strcmp( m.param2, "playlist" ) )
                        {
976
                            index = E_(mvar_PlaylistSetNew)( p_intf, m.param1,
977 978 979 980
                                                    p_intf->p_sys->p_playlist );
                        }
                        else if( !strcmp( m.param2, "information" ) )
                        {
981
                            index = E_(mvar_InfoSetNew)( p_intf, m.param1,
982 983 984 985 986 987 988 989 990
                                                     p_intf->p_sys->p_input );
                        }
                        else if( !strcmp( m.param2, "program" )
                                  || !strcmp( m.param2, "title" )
                                  || !strcmp( m.param2, "chapter" )
                                  || !strcmp( m.param2, "audio-es" )
                                  || !strcmp( m.param2, "video-es" )
                                  || !strcmp( m.param2, "spu-es" ) )
                        {
991
                            index = E_(mvar_InputVarSetNew)( p_intf, m.param1,
992 993 994 995 996 997 998
                                                         p_intf->p_sys->p_input,
                                                         m.param2 );
                        }
                        else if( !strcmp( m.param2, "vlm" ) )
                        {
                            if( p_intf->p_sys->p_vlm == NULL )
                                p_intf->p_sys->p_vlm = vlm_New( p_intf );
999
                            index = E_(mvar_VlmSetNew)( m.param1, p_intf->p_sys->p_vlm );
1000 1001 1002 1003
                        }
#if 0
                        else if( !strcmp( m.param2, "hosts" ) )
                        {
1004
                            index = E_(mvar_HttpdInfoSetNew)( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
1005 1006 1007
                        }
                        else if( !strcmp( m.param2, "urls" ) )
                        {
1008
                            index = E_(mvar_HttpdInfoSetNew)( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
1009 1010 1011
                        }
                        else if( !strcmp( m.param2, "connections" ) )
                        {
1012
                            index = E_(mvar_HttpdInfoSetNew)(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1013 1014
                        }
#endif
1015
                        else if( ( v = E_(mvar_GetVar)( p_args->vars, m.param2 ) ) )
1016
                        {
1017
                            index = E_(mvar_Duplicate)( v );
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
                        }
                        else
                        {
                            msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
                            src = endfor;
                            break;
                        }

                        for( i_idx = 0; i_idx < index->i_field; i_idx++ )
                        {
1028
                            mvar_t *f = E_(mvar_Duplicate)( index->field[i_idx] );
1029 1030 1031 1032 1033 1034 1035

                            //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );

                            free( f->name );
                            f->name = strdup( m.param1 );


1036
                            E_(mvar_PushVar)( p_args->vars, f );
1037 1038
                            E_(Execute)( p_args, p_request, i_request,
                                     pp_data, pi_data, &dst, start, stop );
1039
                            E_(mvar_RemoveVar)( p_args->vars, f );
1040

1041
                            E_(mvar_Delete)( f );
1042
                        }
1043
                        E_(mvar_Delete)( index );
1044 1045 1046 1047 1048 1049

                        src = endfor;
                    }
                    break;
                }
                default:
1050 1051
                    E_(MacroDo)( p_args, &m, p_request, i_request,
                                 pp_data, pi_data, &dst );
1052 1053 1054
                    break;
            }

1055
            E_(MacroClean)( &m );
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
            continue;
        }

        i_copy =   ( (p == NULL || p > end ) ? end : p  ) - src;
        if( i_copy > 0 )
        {
            int i_index = dst - *pp_data;

            *pi_data += i_copy;
            *pp_data = realloc( *pp_data, *pi_data );
            dst = (*pp_data) + i_index;

            memcpy( dst, src, i_copy );
            dst += i_copy;
            src += i_copy;
        }
    }

    *pp_dst = dst;
    free( dup );
}