http.c 99.8 KB
Newer Older
gbazin's avatar
   
gbazin committed
1
/*****************************************************************************
2
 * http.c :  http mini-server ;)
gbazin's avatar
   
gbazin committed
3
 *****************************************************************************
4
 * Copyright (C) 2001-2005 the VideoLAN team
Xavier Martin's avatar
Xavier Martin committed
5
 * $Id$
gbazin's avatar
   
gbazin committed
6
7
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8
 *          Laurent Aimar <fenrir@via.ecp.fr>
gbazin's avatar
   
gbazin committed
9
10
11
12
13
 *
 * 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.
14
 *
gbazin's avatar
   
gbazin committed
15
16
17
18
19
20
21
22
23
24
25
26
27
 * 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.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28
/* TODO:
29
30
31
32
 * - clean up ?
 * - doc ! (mouarf ;)
 */

33
#include <stdlib.h>
34
#include <ctype.h>
gbazin's avatar
   
gbazin committed
35
36
37
#include <vlc/vlc.h>
#include <vlc/intf.h>

38
#include <vlc/aout.h>
39
40
#include <vlc/vout.h> /* for fullscreen */

41
#include "vlc_httpd.h"
42
#include "vlc_vlm.h"
43
#include "vlc_tls.h"
44
#include "vlc_acl.h"
Laurent Aimar's avatar
Laurent Aimar committed
45
#include "charset.h"
46
47
48
49
50
51
52
53
54
55
56

#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
#ifdef HAVE_ERRNO_H
#   include <errno.h>
#endif
#ifdef HAVE_FCNTL_H
#   include <fcntl.h>
#endif

gbazin's avatar
   
gbazin committed
57
#ifdef HAVE_UNISTD_H
58
59
60
#   include <unistd.h>
#elif defined( WIN32 ) && !defined( UNDER_CE )
#   include <io.h>
gbazin's avatar
   
gbazin committed
61
62
#endif

63
#ifdef HAVE_DIRENT_H
64
#   include <dirent.h>
gbazin's avatar
   
gbazin committed
65
66
#endif

67
68
69
70
71
/* stat() support for large files on win32 */
#if defined( WIN32 ) && !defined( UNDER_CE )
#   define stat _stati64
#endif

gbazin's avatar
   
gbazin committed
72
/*****************************************************************************
73
 * Module descriptor
gbazin's avatar
   
gbazin committed
74
 *****************************************************************************/
75
76
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
77
78
79

#define HOST_TEXT N_( "Host address" )
#define HOST_LONGTEXT N_( \
Anil Daoud's avatar
Anil Daoud committed
80
    "You can set the address and port the http interface will bind to." )
81
82
#define SRC_TEXT N_( "Source directory" )
#define SRC_LONGTEXT N_( "Source directory" )
83
#define CERT_TEXT N_( "Certificate file" )
zorglub's avatar
zorglub committed
84
85
#define CERT_LONGTEXT N_( "HTTP interface x509 PEM certificate file " \
                          "(enables SSL)" )
86
#define KEY_TEXT N_( "Private key file" )
87
#define KEY_LONGTEXT N_( "HTTP interface x509 PEM private key file" )
88
#define CA_TEXT N_( "Root CA file" )
zorglub's avatar
zorglub committed
89
90
#define CA_LONGTEXT N_( "HTTP interface x509 PEM trusted root CA " \
                        "certificates file" )
91
#define CRL_TEXT N_( "CRL file" )
92
#define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file" )
93
94

vlc_module_begin();
95
    set_shortname( _("HTTP"));
96
    set_description( _("HTTP remote control interface") );
zorglub's avatar
zorglub committed
97
    set_category( CAT_INTERFACE );
98
    set_subcategory( SUBCAT_INTERFACE_GENERAL );
99
100
        add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
        add_string ( "http-src",  NULL, NULL, SRC_TEXT,  SRC_LONGTEXT,  VLC_TRUE );
101
        set_section( N_("HTTP SSL" ), 0 );
102
103
104
105
        add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
        add_string ( "http-intf-key",  NULL, NULL, KEY_TEXT,  KEY_LONGTEXT,  VLC_TRUE );
        add_string ( "http-intf-ca",   NULL, NULL, CA_TEXT,   CA_LONGTEXT,   VLC_TRUE );
        add_string ( "http-intf-crl",  NULL, NULL, CRL_TEXT,  CRL_LONGTEXT,  VLC_TRUE );
106
    set_capability( "interface", 0 );
107
    set_callbacks( Open, Close );
108
109
110
111
112
113
vlc_module_end();


/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
114
115
static void Run          ( intf_thread_t *p_intf );

gbazin's avatar
gbazin committed
116
117
static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
                           char *psz_dir );
118

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
static int DirectoryCheck( char *psz_dir )
{
    DIR           *p_dir;

#ifdef HAVE_SYS_STAT_H
    struct stat   stat_info;

    if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
    {
        return VLC_EGENERIC;
    }
#endif

    if( ( p_dir = opendir( psz_dir ) ) == NULL )
    {
        return VLC_EGENERIC;
    }
    closedir( p_dir );

    return VLC_SUCCESS;
}

141
142
143
144
static int  HttpCallback( httpd_file_sys_t *p_args,
                          httpd_file_t *,
                          uint8_t *p_request,
                          uint8_t **pp_data, int *pi_data );
145

146
static char *uri_extract_value( char *psz_uri, const char *psz_name,
gbazin's avatar
   
gbazin committed
147
                                char *psz_value, int i_value_max );
148
static int uri_test_param( char *psz_uri, const char *psz_name );
149

150
static void uri_decode_url_encoded( char *psz );
gbazin's avatar
   
gbazin committed
151

152
static char *Find_end_MRL( char *psz );
gbazin's avatar
gbazin committed
153
static playlist_item_t *parse_MRL( intf_thread_t * , char *psz );
154

gbazin's avatar
   
gbazin committed
155
/*****************************************************************************
156
 *
gbazin's avatar
   
gbazin committed
157
 *****************************************************************************/
158
159
160
161
162
163
164
165
166
167
168
169
170
171
typedef struct mvar_s
{
    char *name;
    char *value;

    int           i_field;
    struct mvar_s **field;
} mvar_t;

#define STACK_MAX 100
typedef struct
{
    char *stack[STACK_MAX];
    int  i_stack;
172
} rpn_stack_t;
173

174
struct httpd_file_sys_t
175
{
176
177
178
    intf_thread_t    *p_intf;
    httpd_file_t     *p_file;
    httpd_redirect_t *p_redir;
179
    httpd_redirect_t *p_redir2;
180
181
182

    char          *file;
    char          *name;
183
184

    vlc_bool_t    b_html;
185
186

    /* inited for each access */
187
    rpn_stack_t   stack;
188
189
190
    mvar_t        *vars;
};

gbazin's avatar
   
gbazin committed
191
192
193
194
struct intf_sys_t
{
    httpd_host_t        *p_httpd_host;

195
196
    int                 i_files;
    httpd_file_sys_t    **pp_files;
197
198
199

    playlist_t          *p_playlist;
    input_thread_t      *p_input;
200
    vlm_t               *p_vlm;
201
    char                *psz_html_type;
202
203
204
205
206
207
208
};



/*****************************************************************************
 * Activate: initialize and create stuff
 *****************************************************************************/
209
static int Open( vlc_object_t *p_this )
210
211
212
213
214
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
    intf_sys_t    *p_sys;
    char          *psz_host;
    char          *psz_address = "";
215
216
    const char    *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
                  *psz_crl = NULL;
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
    int           i_port       = 0;
    char          *psz_src;

    psz_host = config_GetPsz( p_intf, "http-host" );
    if( psz_host )
    {
        char *psz_parser;
        psz_address = psz_host;

        psz_parser = strchr( psz_host, ':' );
        if( psz_parser )
        {
            *psz_parser++ = '\0';
            i_port = atoi( psz_parser );
        }
    }

    p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
zorglub's avatar
zorglub committed
235
236
237
238
    if( !p_intf->p_sys )
    {
        return( VLC_ENOMEM );
    }
239
240
    p_sys->p_playlist = NULL;
    p_sys->p_input    = NULL;
241
    p_sys->p_vlm      = NULL;
242

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    /* determine Content-Type value for HTML pages */
    vlc_current_charset(&psz_src);
    if( psz_src == NULL )
    {
        free( p_sys );
        return VLC_ENOMEM;
    }
    p_sys->psz_html_type = malloc( 20 + strlen( psz_src ) );
    if( p_sys->psz_html_type == NULL )
    {
        free( p_sys );
        free( psz_src );
        return VLC_ENOMEM ;
    }
    sprintf( p_sys->psz_html_type, "text/html; charset=%s", psz_src );
    free( psz_src );

    /* determine SSL configuration */
261
    psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
262
263
264
265
    if ( psz_cert != NULL )
    {
        msg_Dbg( p_intf, "enablind TLS for HTTP interface (cert file: %s)",
                 psz_cert );
266
267
268
        psz_key = config_GetPsz( p_intf, "http-intf-key" );
        psz_ca = config_GetPsz( p_intf, "http-intf-ca" );
        psz_crl = config_GetPsz( p_intf, "http-intf-crl" );
269
270
271
272
273
274
275
276
277
278
279
280
281

        if( i_port <= 0 )
            i_port = 8443;
    }
    else
    {
        if( i_port <= 0 )
            i_port= 8080;
    }

    msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );

    p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
282
283
                                            i_port, psz_cert, psz_key, psz_ca,
                                            psz_crl );
284
    if( p_sys->p_httpd_host == NULL )
285
286
    {
        msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
287
        free( p_sys->psz_html_type );
288
289
290
291
292
293
294
295
296
        free( p_sys );
        return VLC_EGENERIC;
    }

    if( psz_host )
    {
        free( psz_host );
    }

297
298
    p_sys->i_files  = 0;
    p_sys->pp_files = NULL;
299

300
#if defined(SYS_DARWIN) || defined(SYS_BEOS) || defined(WIN32)
301
302
303
304
    if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
    {
        char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
        psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
305
        if( !psz_src ) return VLC_ENOMEM;
gbazin's avatar
   
gbazin committed
306
307
308
#if defined(WIN32)
        sprintf( psz_src, "%s/http", psz_vlcpath);
#else
309
        sprintf( psz_src, "%s/share/http", psz_vlcpath);
gbazin's avatar
   
gbazin committed
310
#endif
311
312
313
    }
#else
    psz_src = config_GetPsz( p_intf, "http-src" );
314
315
316
317
318
319
320
321
322
323
324
325

    if( !psz_src || *psz_src == '\0' )
    {
        if( !DirectoryCheck( "share/http" ) )
        {
            psz_src = strdup( "share/http" );
        }
        else if( !DirectoryCheck( DATA_PATH "/http" ) )
        {
            psz_src = strdup( DATA_PATH "/http" );
        }
    }
326
#endif
327

328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
    if( !psz_src || *psz_src == '\0' )
    {
        msg_Err( p_intf, "invalid src dir" );
        goto failed;
    }

    /* remove trainling \ or / */
    if( psz_src[strlen( psz_src ) - 1] == '\\' ||
        psz_src[strlen( psz_src ) - 1] == '/' )
    {
        psz_src[strlen( psz_src ) - 1] = '\0';
    }

    ParseDirectory( p_intf, psz_src, psz_src );


    if( p_sys->i_files <= 0 )
    {
346
        msg_Err( p_intf, "cannot find any files (%s)", psz_src );
347
348
349
        goto failed;
    }
    p_intf->pf_run = Run;
gbazin's avatar
   
gbazin committed
350
    free( psz_src );
351
352
353
354

    return VLC_SUCCESS;

failed:
gbazin's avatar
   
gbazin committed
355
    if( psz_src ) free( psz_src );
356
357
358
359
360
    if( p_sys->pp_files )
    {
        free( p_sys->pp_files );
    }
    httpd_HostDelete( p_sys->p_httpd_host );
361
    free( p_sys->psz_html_type );
362
363
364
365
366
367
368
369
370
371
372
373
374
375
    free( p_sys );
    return VLC_EGENERIC;
}

/*****************************************************************************
 * CloseIntf: destroy interface
 *****************************************************************************/
void Close ( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
    intf_sys_t    *p_sys = p_intf->p_sys;

    int i;

376
377
378
379
    if( p_sys->p_vlm )
    {
        vlm_Delete( p_sys->p_vlm );
    }
380
381
    for( i = 0; i < p_sys->i_files; i++ )
    {
382
383
384
       httpd_FileDelete( p_sys->pp_files[i]->p_file );
       if( p_sys->pp_files[i]->p_redir )
           httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
385
386
       if( p_sys->pp_files[i]->p_redir2 )
           httpd_RedirectDelete( p_sys->pp_files[i]->p_redir2 );
387

388
389
390
391
       free( p_sys->pp_files[i]->file );
       free( p_sys->pp_files[i]->name );
       free( p_sys->pp_files[i] );
    }
392
393
394
395
396
397
    if( p_sys->pp_files )
    {
        free( p_sys->pp_files );
    }
    httpd_HostDelete( p_sys->p_httpd_host );

398
    free( p_sys->psz_html_type );
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
    free( p_sys );
}

/*****************************************************************************
 * Run: http interface thread
 *****************************************************************************/
static void Run( intf_thread_t *p_intf )
{
    intf_sys_t     *p_sys = p_intf->p_sys;

    while( !p_intf->b_die )
    {
        /* get the playlist */
        if( p_sys->p_playlist == NULL )
        {
            p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
        }

        /* Manage the input part */
        if( p_sys->p_input == NULL )
        {
            if( p_sys->p_playlist )
            {
                p_sys->p_input =
                    vlc_object_find( p_sys->p_playlist,
                                     VLC_OBJECT_INPUT,
                                     FIND_CHILD );
            }
        }
        else if( p_sys->p_input->b_dead )
        {
            vlc_object_release( p_sys->p_input );
            p_sys->p_input = NULL;
        }


        /* Wait a bit */
        msleep( INTF_IDLE_SLEEP );
    }

    if( p_sys->p_input )
    {
        vlc_object_release( p_sys->p_input );
        p_sys->p_input = NULL;
    }

    if( p_sys->p_playlist )
    {
        vlc_object_release( p_sys->p_playlist );
        p_sys->p_playlist = NULL;
    }
}


/*****************************************************************************
 * Local functions
 *****************************************************************************/
#define MAX_DIR_SIZE 10240

/****************************************************************************
 * FileToUrl: create a good name for an url from filename
 ****************************************************************************/
461
static char *FileToUrl( char *name, vlc_bool_t *pb_index )
462
463
464
465
466
{
    char *url, *p;

    url = p = malloc( strlen( name ) + 1 );

467
    *pb_index = VLC_FALSE;
zorglub's avatar
zorglub committed
468
469
470
471
472
    if( !url || !p )
    {
        return NULL;
    }

473
#ifdef WIN32
gbazin's avatar
gbazin committed
474
475
    while( *name == '\\' || *name == '/' )
#else
476
    while( *name == '/' )
gbazin's avatar
gbazin committed
477
#endif
478
479
480
481
482
    {
        name++;
    }

    *p++ = '/';
gbazin's avatar
gbazin committed
483
484
485
486
487
    strcpy( p, name );

#ifdef WIN32
    /* convert '\\' into '/' */
    name = p;
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
    while( *name )
    {
        if( *name == '\\' )
        {
            *p++ = '/';
        }
        name++;
    }
#endif

    /* index.* -> / */
    if( ( p = strrchr( url, '/' ) ) != NULL )
    {
        if( !strncmp( p, "/index.", 7 ) )
        {
            p[1] = '\0';
504
            *pb_index = VLC_TRUE;
505
506
507
508
509
510
511
512
        }
    }
    return url;
}

/****************************************************************************
 * ParseDirectory: parse recursively a directory, adding each file
 ****************************************************************************/
gbazin's avatar
gbazin committed
513
514
static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
                           char *psz_dir )
515
516
517
518
519
520
521
522
{
    intf_sys_t     *p_sys = p_intf->p_sys;
    char           dir[MAX_DIR_SIZE];
#ifdef HAVE_SYS_STAT_H
    struct stat   stat_info;
#endif
    DIR           *p_dir;
    struct dirent *p_dir_content;
523
    vlc_acl_t     *p_acl;
524
525
526
527
    FILE          *file;

    char          *user = NULL;
    char          *password = NULL;
528

529
    int           i_dirlen;
530
531

#ifdef HAVE_SYS_STAT_H
gbazin's avatar
gbazin committed
532
    if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
533
534
535
536
537
538
539
540
541
542
543
    {
        return VLC_EGENERIC;
    }
#endif

    if( ( p_dir = opendir( psz_dir ) ) == NULL )
    {
        msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
        return VLC_EGENERIC;
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
544
545
546
547
548
549
550
    i_dirlen = strlen( psz_dir );
    if( i_dirlen + 10 > MAX_DIR_SIZE )
    {
        msg_Warn( p_intf, "skipping too deep dir (%s)", psz_dir );
        return 0;
    }

551
552
553
554
555
556
557
558
559
560
561
562
563
564
    msg_Dbg( p_intf, "dir=%s", psz_dir );

    sprintf( dir, "%s/.access", psz_dir );
    if( ( file = fopen( dir, "r" ) ) != NULL )
    {
        char line[1024];
        int  i_size;

        msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );

        i_size = fread( line, 1, 1023, file );
        if( i_size > 0 )
        {
            char *p;
gbazin's avatar
gbazin committed
565
566
            while( i_size > 0 && ( line[i_size-1] == '\n' ||
                   line[i_size-1] == '\r' ) )
567
568
569
570
571
572
573
574
575
576
577
578
579
580
            {
                i_size--;
            }

            line[i_size] = '\0';

            p = strchr( line, ':' );
            if( p )
            {
                *p++ = '\0';
                user = strdup( line );
                password = strdup( p );
            }
        }
gbazin's avatar
gbazin committed
581
582
        msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
                 user, password, i_size );
583
584
585
586

        fclose( file );
    }

587
    sprintf( dir, "%s/.hosts", psz_dir );
588
    p_acl = ACL_Create( p_intf, VLC_FALSE );
589
590
591
592
593
    if( ACL_LoadFile( p_acl, dir ) )
    {
        ACL_Destroy( p_acl );
        p_acl = NULL;
    }
594

595
596
597
598
599
600
601
602
    for( ;; )
    {
        /* parse psz_src dir */
        if( ( p_dir_content = readdir( p_dir ) ) == NULL )
        {
            break;
        }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
603
604
        if( ( p_dir_content->d_name[0] == '.' )
         || ( i_dirlen + strlen( p_dir_content->d_name ) > MAX_DIR_SIZE ) )
605
            continue;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
606
        
607
608
609
        sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
        if( ParseDirectory( p_intf, psz_root, dir ) )
        {
610
            httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
611
            vlc_bool_t b_index;
612

613
            f->p_intf  = p_intf;
614
615
            f->p_file = NULL;
            f->p_redir = NULL;
616
            f->p_redir2 = NULL;
617
            f->file = strdup( dir );
618
            f->name = FileToUrl( &dir[strlen( psz_root )], &b_index );
619
            f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
620

621
            if( !f->name )
zorglub's avatar
zorglub committed
622
            {
Anil Daoud's avatar
Anil Daoud committed
623
                msg_Err( p_intf , "unable to parse directory" );
gbazin's avatar
   
gbazin committed
624
                closedir( p_dir );
gbazin's avatar
   
gbazin committed
625
                free( f );
zorglub's avatar
zorglub committed
626
627
                return( VLC_ENOMEM );
            }
628
629
630
631
            msg_Dbg( p_intf, "file=%s (url=%s)",
                     f->file, f->name );

            f->p_file = httpd_FileNew( p_sys->p_httpd_host,
632
633
                                       f->name,
                                       f->b_html ? p_sys->psz_html_type : NULL,
634
                                       user, password, p_acl,
635
636
                                       HttpCallback, f );

637
638
            if( f->p_file )
            {
639
                TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
640
            }
641
642
643
            /* for url that ends by / add
             *  - a redirect from rep to rep/
             *  - in case of index.* rep/index.html to rep/ */
644
            if( f && f->name[strlen(f->name) - 1] == '/' )
645
            {
646
                char *psz_redir = strdup( f->name );
647
                char *p;
648
649
650
651
652
                psz_redir[strlen( psz_redir ) - 1] = '\0';

                msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
                f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
                free( psz_redir );
653
654
655
656
657
658
659
660
661
662
663

                if( b_index && ( p = strstr( f->file, "index." ) ) )
                {
                    asprintf( &psz_redir, "%s%s", f->name, p );

                    msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
                    f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
                                                     f->name, psz_redir );

                    free( psz_redir );
                }
664
665
666
667
668
669
670
671
672
673
674
675
            }
        }
    }

    if( user )
    {
        free( user );
    }
    if( password )
    {
        free( password );
    }
gbazin's avatar
   
gbazin committed
676

677
    ACL_Destroy( p_acl );
gbazin's avatar
   
gbazin committed
678
679
    closedir( p_dir );

680
681
682
683
684
685
686
687
688
689
690
    return VLC_SUCCESS;
}

/****************************************************************************
 * var and set handling
 ****************************************************************************/

static mvar_t *mvar_New( char *name, char *value )
{
    mvar_t *v = malloc( sizeof( mvar_t ) );

zorglub's avatar
zorglub committed
691
    if( !v ) return NULL;
692
693
694
695
696
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
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
768
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
    v->name = strdup( name );
    v->value = strdup( value ? value : "" );

    v->i_field = 0;
    v->field = malloc( sizeof( mvar_t * ) );
    v->field[0] = NULL;

    return v;
}

static void mvar_Delete( mvar_t *v )
{
    int i;

    free( v->name );
    free( v->value );

    for( i = 0; i < v->i_field; i++ )
    {
        mvar_Delete( v->field[i] );
    }
    free( v->field );
    free( v );
}

static void mvar_AppendVar( mvar_t *v, mvar_t *f )
{
    v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
    v->field[v->i_field] = f;
    v->i_field++;
}

static mvar_t *mvar_Duplicate( mvar_t *v )
{
    int i;
    mvar_t *n;

    n = mvar_New( v->name, v->value );
    for( i = 0; i < v->i_field; i++ )
    {
        mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
    }

    return n;
}

static void mvar_PushVar( mvar_t *v, mvar_t *f )
{
    v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
    if( v->i_field > 0 )
    {
        memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
    }
    v->field[0] = f;
    v->i_field++;
}

static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
{
    int i;
    for( i = 0; i < v->i_field; i++ )
    {
        if( v->field[i] == f )
        {
            break;
        }
    }
    if( i >= v->i_field )
    {
        return;
    }

    if( i + 1 < v->i_field )
    {
        memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
    }
    v->i_field--;
    /* FIXME should do a realloc */
}

static mvar_t *mvar_GetVar( mvar_t *s, char *name )
{
    int i;
    char base[512], *field, *p;
    int  i_index;

    /* format: name[index].field */

    field = strchr( name, '.' );
    if( field )
    {
        int i = field - name;
        strncpy( base, name, i );
        base[i] = '\0';
        field++;
    }
    else
    {
        strcpy( base, name );
    }

    if( ( p = strchr( base, '[' ) ) )
    {
        *p++ = '\0';
        sscanf( p, "%d]", &i_index );
        if( i_index < 0 )
        {
            return NULL;
        }
    }
    else
    {
        i_index = 0;
    }

    for( i = 0; i < s->i_field; i++ )
    {
        if( !strcmp( s->field[i]->name, base ) )
        {
            if( i_index > 0 )
            {
                i_index--;
            }
            else
            {
                if( field )
                {
                    return mvar_GetVar( s->field[i], field );
                }
                else
                {
                    return s->field[i];
                }
            }
        }
    }
    return NULL;
}



static char *mvar_GetValue( mvar_t *v, char *field )
{
    if( *field == '\0' )
    {
        return v->value;
    }
    else
    {
        mvar_t *f = mvar_GetVar( v, field );
        if( f )
        {
            return f->value;
        }
        else
        {
848
            return field;
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
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
        }
    }
}

static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
{
    mvar_t *f = mvar_New( name, value );
    mvar_PushVar( vars, f );
}

static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
{
    mvar_t *f = mvar_New( name, value );
    mvar_AppendVar( vars, f );
}


/* arg= start[:stop[:step]],.. */
static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
{
    char *dup = strdup( arg );
    char *str = dup;
    mvar_t *s = mvar_New( name, "set" );


    while( str )
    {
        char *p;
        int  i_start,i_stop,i_step;
        int  i_match;

        p = strchr( str, ',' );
        if( p )
        {
            *p++ = '\0';
        }

        i_step = 0;
        i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );

        if( i_match == 1 )
        {
            i_stop = i_start;
            i_step = 1;
        }
        else if( i_match == 2 )
        {
            i_step = i_start < i_stop ? 1 : -1;
        }

        if( i_match >= 1 )
        {
            int i;

903
904
            if( ( i_start <= i_stop && i_step > 0 ) ||
                ( i_start >= i_stop && i_step < 0 ) )
905
906
907
            {
                for( i = i_start; ; i += i_step )
                {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
908
                    char   value[79];
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928

                    if( ( i_step > 0 && i > i_stop ) ||
                        ( i_step < 0 && i < i_stop ) )
                    {
                        break;
                    }

                    sprintf( value, "%d", i );

                    mvar_PushNewVar( s, name, value );
                }
            }
        }
        str = p;
    }

    free( dup );
    return s;
}

929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
void PlaylistListNode( playlist_t *p_pl, playlist_item_t *p_node,
                       char *name, mvar_t *s, int i_depth )
{
    if( p_node != NULL )
    {
        if (p_node->i_children == -1)
        {
            char value[512];
            mvar_t *itm = mvar_New( name, "set" );

            sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
            mvar_AppendNewVar( itm, "current", value );

            sprintf( value, "%d", p_node->input.i_id );
            mvar_AppendNewVar( itm, "index", value );

            mvar_AppendNewVar( itm, "name", p_node->input.psz_name );

            mvar_AppendNewVar( itm, "uri", p_node->input.psz_uri );

            sprintf( value, "Item");
            mvar_AppendNewVar( itm, "type", value );

            sprintf( value, "%d", i_depth );
            mvar_AppendNewVar( itm, "depth", value );

            mvar_AppendVar( s, itm );
        }
        else
        {
            char value[512];
            int i_child;
            mvar_t *itm = mvar_New( name, "set" );

            mvar_AppendNewVar( itm, "name", p_node->input.psz_name );
            mvar_AppendNewVar( itm, "uri", p_node->input.psz_name );

            sprintf( value, "Node" );
            mvar_AppendNewVar( itm, "type", value );

            sprintf( value, "%d", p_node->input.i_id );
            mvar_AppendNewVar( itm, "index", value );

            sprintf( value, "%d", p_node->i_children);
            mvar_AppendNewVar( itm, "i_children", value );

            sprintf( value, "%d", i_depth );
            mvar_AppendNewVar( itm, "depth", value );

            mvar_AppendVar( s, itm );

            for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
                PlaylistListNode( p_pl, p_node->pp_children[i_child], name, s, i_depth + 1);

        }
    }
}

987
988
static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
{
989
    playlist_view_t *p_view;
990
991
992
993
994
    mvar_t *s = mvar_New( name, "set" );


    vlc_mutex_lock( &p_pl->object_lock );

995
    p_view = playlist_ViewFind( p_pl, VIEW_CATEGORY ); /* FIXME */
996

997
998
    if( p_view != NULL )
        PlaylistListNode( p_pl, p_view->p_root, name, s, 0 );
999
1000
1001
1002
1003
1004
1005
1006
1007

    vlc_mutex_unlock( &p_pl->object_lock );

    return s;
}

static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
{
    mvar_t *s = mvar_New( name, "set" );
1008
    int i, j;
1009
1010
1011
1012
1013
1014

    if( p_input == NULL )
    {
        return s;
    }

Laurent Aimar's avatar
Laurent Aimar committed
1015
1016
    vlc_mutex_lock( &p_input->input.p_item->lock );
    for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
1017
    {
Laurent Aimar's avatar
Laurent Aimar committed
1018
        info_category_t *p_category = p_input->input.p_item->pp_categories[i];
1019
1020
1021
1022
1023
1024
        mvar_t *cat  = mvar_New( name, "set" );
        mvar_t *iset = mvar_New( "info", "set" );

        mvar_AppendNewVar( cat, "name", p_category->psz_name );
        mvar_AppendVar( cat, iset );

1025
        for ( j = 0; j < p_category->i_infos; j++ )
1026
        {
1027
            info_t *p_info = p_category->pp_infos[j];
1028
1029
            mvar_t *info = mvar_New( "info", "" );

1030
1031
            msg_Dbg( p_input, "adding info name=%s value=%s",
                     p_info->psz_name, p_info->psz_value );
1032
1033
1034
1035
1036
1037
            mvar_AppendNewVar( info, "name",  p_info->psz_name );
            mvar_AppendNewVar( info, "value", p_info->psz_value );
            mvar_AppendVar( iset, info );
        }
        mvar_AppendVar( s, cat );
    }
Laurent Aimar's avatar
Laurent Aimar committed
1038
    vlc_mutex_unlock( &p_input->input.p_item->lock );
1039
1040
1041

    return s;
}
1042
#if 0
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
{
    mvar_t       *s = mvar_New( name, "set" );
    httpd_info_t info;
    int          i;

    if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
    {
        for( i= 0; i < info.i_count; )
        {
            mvar_t *inf;

            inf = mvar_New( name, "set" );
            do
            {
                /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
                            info.info[i].psz_name, info.info[i].psz_value ); */
                mvar_AppendNewVar( inf,
                                   info.info[i].psz_name,
                                   info.info[i].psz_value );
                i++;
            } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
            mvar_AppendVar( s, inf );
        }
    }

    /* free mem */
    for( i = 0; i < info.i_count; i++ )
    {
        free( info.info[i].psz_name );
        free( info.info[i].psz_value );
    }
1075
1076
1077
1078
    if( info.i_count > 0 )
    {
        free( info.info );
    }
1079
1080
1081

    return s;
}
1082
1083
#endif

1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
{
    mvar_t *s = mvar_New( name, "set" );
    char           tmp[MAX_DIR_SIZE], *p, *src;
#ifdef HAVE_SYS_STAT_H
    struct stat   stat_info;
#endif
    DIR           *p_dir;
    struct dirent *p_dir_content;
    char          sep;

    /* convert all / to native separator */
#if defined( WIN32 )
1097
    while( (p = strchr( psz_dir, '/' )) )
1098
1099
1100
1101
1102
1103
1104
1105
    {
        *p = '\\';
    }
    sep = '\\';
#else
    sep = '/';
#endif

gbazin's avatar
   
gbazin committed
1106
1107
1108
1109
1110
1111
    /* remove trailling separator */
    while( strlen( psz_dir ) > 1 &&
#if defined( WIN32 )
           !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
#endif
           psz_dir[strlen( psz_dir ) -1 ] == sep )
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
1180
1181
1182
1183
1184
    {
        psz_dir[strlen( psz_dir ) -1 ]  ='\0';
    }
    /* remove double separator */
    for( p = src = psz_dir; *src != '\0'; src++, p++ )
    {
        if( src[0] == sep && src[1] == sep )
        {
            src++;
        }
        *p = *src;
    }
    *p = '\0';

    if( *psz_dir == '\0' )
    {
        return s;
    }
    /* first fix all .. dir */
    p = src = psz_dir;
    while( *src )
    {
        if( src[0] == '.' && src[1] == '.' )
        {
            src += 2;
            if( p <= &psz_dir[1] )
            {
                continue;
            }

            p -= 2;

            while( p > &psz_dir[1] && *p != sep )
            {
                p--;
            }
        }
        else if( *src == sep )
        {
            if( p > psz_dir && p[-1] == sep )
            {
                src++;
            }
            else
            {
                *p++ = *src++;
            }
        }
        else
        {
            do
            {
                *p++ = *src++;
            } while( *src && *src != sep );
        }
    }
    *p = '\0';


#ifdef HAVE_SYS_STAT_H
    if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
    {
        return s;
    }
#endif

    if( ( p_dir = opendir( psz_dir ) ) == NULL )
    {
        fprintf( stderr, "cannot open dir (%s)", psz_dir );
        return s;
    }

    /* remove traling / or \ */
1185
1186
    for( p = &psz_dir[strlen( psz_dir) - 1];
         p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
    {
        *p = '\0';
    }

    for( ;; )
    {
        mvar_t *f;

        /* parse psz_src dir */
        if( ( p_dir_content = readdir( p_dir ) ) == NULL )
        {
            break;
        }
        if( !strcmp( p_dir_content->d_name, "." ) )
        {
            continue;
        }

        sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );

#ifdef HAVE_SYS_STAT_H
        if( stat( tmp, &stat_info ) == -1 )
        {
            continue;
        }
#endif
        f = mvar_New( name, "set" );
        mvar_AppendNewVar( f, "name", tmp );
1215
        mvar_AppendNewVar( f, "basename", p_dir_content->d_name );
1216

1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
#ifdef HAVE_SYS_STAT_H
        if( S_ISDIR( stat_info.st_mode ) )
        {
            mvar_AppendNewVar( f, "type", "directory" );
        }
        else if( S_ISREG( stat_info.st_mode ) )
        {
            mvar_AppendNewVar( f, "type", "file" );
        }
        else
        {
            mvar_AppendNewVar( f, "type", "unknown" );
        }

1231
        sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
1232
1233
1234
        mvar_AppendNewVar( f, "size", tmp );

        /* FIXME memory leak FIXME */
1235
#ifdef HAVE_CTIME_R
1236
1237
        ctime_r( &stat_info.st_mtime, tmp );
        mvar_AppendNewVar( f, "date", tmp );
1238
1239
1240
1241
#else
        mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
#endif

1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
#else
        mvar_AppendNewVar( f, "type", "unknown" );
        mvar_AppendNewVar( f, "size", "unknown" );
        mvar_AppendNewVar( f, "date", "unknown" );
#endif
        mvar_AppendVar( s, f );
    }

    return s;
}

1253
1254
1255
1256
1257
1258
static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
{
    mvar_t        *s = mvar_New( name, "set" );
    vlm_message_t *msg;
    int    i;

1259
1260
    if( vlm == NULL ) return s;

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
    if( vlm_ExecuteCommand( vlm, "show", &msg ) )
    {
        return s;
    }

    for( i = 0; i < msg->i_child; i++ )
    {
        /* Over media, schedule */
        vlm_message_t *ch = msg->child[i];
        int j;

        for( j = 0; j < ch->i_child; j++ )
        {
            /* Over name */
            vlm_message_t *el = ch->child[j];
            vlm_message_t *inf, *desc;
            mvar_t        *set;
            char          psz[500];
            int k;

            sprintf( psz, "show %s", el->psz_name );
            if( vlm_ExecuteCommand( vlm, psz, &inf ) )
                continue;
            desc = inf->child[0];

            /* Add a node with name and info */
            set = mvar_New( name, "set" );
            mvar_AppendNewVar( set, "name", el->psz_name );

            for( k = 0; k < desc->i_child; k++ )
            {
Laurent Aimar's avatar
Laurent Aimar committed
1292
1293
1294
1295
1296
1297
1298
1299
                vlm_message_t *ch = desc->child[k];
                if( ch->i_child > 0 )
                {
                    int c;
                    mvar_t *n = mvar_New( ch->psz_name, "set" );

                    for( c = 0; c < ch->i_child; c++ )
                    {
1300
1301
1302
1303
1304
1305
1306
1307
1308
                        if( ch->child[c]->psz_value )
                        {
                            mvar_AppendNewVar( n, ch->child[c]->psz_name, ch->child[c]->psz_value );
                        }
                        else
                        {
                            mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
                            mvar_AppendVar( n, in );
                        }
Laurent Aimar's avatar
Laurent Aimar committed
1309
1310
1311
1312
1313
1314
1315
                    }
                    mvar_AppendVar( set, n );
                }
                else
                {
                    mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
                }
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
            }
            vlm_MessageDelete( inf );

            mvar_AppendVar( s, set );
        }
    }
    vlm_MessageDelete( msg );

    return s;
}


1328
1329
1330
static void SSInit( rpn_stack_t * );
static void SSClean( rpn_stack_t * );
static void EvaluateRPN( mvar_t  *, rpn_stack_t *, char * );
1331

1332
1333
static void SSPush  ( rpn_stack_t *, char * );
static char *SSPop  ( rpn_stack_t * );
1334

1335
1336
static void SSPushN ( rpn_stack_t *, int );
static int  SSPopN  ( rpn_stack_t *, mvar_t  * );
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348


/****************************************************************************
 * Macro handling
 ****************************************************************************/
typedef struct
{
    char *id;
    char *param1;
    char *param2;
} macro_t;

1349
static int FileLoad( FILE *f, char **pp_data, int *pi_data )
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
{
    int i_read;

    /* just load the file */
    *pi_data = 0;
    *pp_data = malloc( 1025 );  /* +1 for \0 */
    while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
    {
        *pi_data += 1024;
        *pp_data = realloc( *pp_data, *pi_data  + 1025 );
    }
    if( i_read > 0 )
    {
        *pi_data += i_read;
    }
    (*pp_data)[*pi_data] = '\0';

    return VLC_SUCCESS;
}

1370
static int MacroParse( macro_t *m, char *psz_src )
1371
{
1372
1373
1374
    char *dup = strdup( (char *)psz_src );
    char *src = dup;
    char *p;
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
    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
}
gbazin's avatar
   
gbazin committed
1449

1450
1451
1452
1453
1454
1455
static void MacroClean( macro_t *m )
{
    free( m->id );
    free( m->param1 );
    free( m->param2 );
}
1456

1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
enum macroType
{
    MVLC_UNKNOWN = 0,
    MVLC_CONTROL,
        MVLC_PLAY,
        MVLC_STOP,
        MVLC_PAUSE,
        MVLC_NEXT,
        MVLC_PREVIOUS,
        MVLC_ADD,
1467
1468
        MVLC_DEL,
        MVLC_EMPTY,
1469
        MVLC_SEEK,
1470
        MVLC_KEEP,
1471
        MVLC_SORT,
1472
        MVLC_MOVE,
1473
        MVLC_VOLUME,
1474
        MVLC_FULLSCREEN,
1475
1476
1477

        MVLC_CLOSE,
        MVLC_SHUTDOWN,
1478
1479
1480
1481
1482
1483
1484
1485

        MVLC_VLM_NEW,
        MVLC_VLM_SETUP,
        MVLC_VLM_DEL,
        MVLC_VLM_PLAY,
        MVLC_VLM_PAUSE,
        MVLC_VLM_STOP,
        MVLC_VLM_SEEK,
1486
1487
        MVLC_VLM_LOAD,
        MVLC_VLM_SAVE,
1488

1489
1490
1491
    MVLC_FOREACH,
    MVLC_IF,
    MVLC_RPN,
1492
    MVLC_STACK,
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
    MVLC_ELSE,
    MVLC_END,
    MVLC_GET,
    MVLC_SET,
        MVLC_INT,
        MVLC_FLOAT,
        MVLC_STRING,

    MVLC_VALUE
};
gbazin's avatar
   
gbazin committed
1503

1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
static struct
{
    char *psz_name;
    int  i_type;
}
StrToMacroTypeTab [] =
{
    { "control",    MVLC_CONTROL },
        /* player control */
        { "play",           MVLC_PLAY },
        { "stop",           MVLC_STOP },
        { "pause",          MVLC_PAUSE },
        { "next",           MVLC_NEXT },
1517
        { "previous",       MVLC_PREVIOUS },
1518
        { "seek",           MVLC_SEEK },
1519
1520
        { "keep",           MVLC_KEEP },
        { "fullscreen",     MVLC_FULLSCREEN },
1521
        { "volume",         MVLC_VOLUME },
1522
1523

        /* playlist management */
1524
        { "add",            MVLC_ADD },
gbazin's avatar
   
gbazin committed
1525
        { "delete",         MVLC_DEL },
1526
        { "empty",          MVLC_EMPTY },
1527
        { "sort",           MVLC_SORT },
1528
        { "move",           MVLC_MOVE },
1529
1530
1531
1532
1533

        /* admin control */
        { "close",          MVLC_CLOSE },
        { "shutdown",       MVLC_SHUTDOWN },

1534
1535
1536
1537
1538
1539
1540
1541
        /* vlm control */
        { "vlm_new",        MVLC_VLM_NEW },
        { "vlm_setup",      MVLC_VLM_SETUP },
        { "vlm_del",        MVLC_VLM_DEL },
        { "vlm_play",       MVLC_VLM_PLAY },
        { "vlm_pause",      MVLC_VLM_PAUSE },
        { "vlm_stop",       MVLC_VLM_STOP },
        { "vlm_seek",       MVLC_VLM_SEEK },
1542
1543
        { "vlm_load",       MVLC_VLM_LOAD },
        { "vlm_save",       MVLC_VLM_SAVE },
1544

1545
    { "rpn",        MVLC_RPN },
1546
    { "stack",        MVLC_STACK },
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562

    { "foreach",    MVLC_FOREACH },
    { "value",      MVLC_VALUE },

    { "if",         MVLC_IF },
    { "else",       MVLC_ELSE },
    { "end",        MVLC_END },
    { "get",         MVLC_GET },
    { "set",         MVLC_SET },
        { "int",            MVLC_INT },
        { "float",          MVLC_FLOAT },
        { "string",         MVLC_STRING },

    /* end */
    { NULL,         MVLC_UNKNOWN }
};
gbazin's avatar
   
gbazin committed
1563

1564
static int StrToMacroType( char *name )
gbazin's avatar
   
gbazin committed
1565
{
1566
    int i;
gbazin's avatar
   
gbazin committed
1567

1568
    if( !name || *name == '\0')
gbazin's avatar
   
gbazin committed
1569
    {
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
        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;
gbazin's avatar
   
gbazin committed
1580
1581
}

1582
static void MacroDo( httpd_file_sys_t *p_args,
1583
                     macro_t *m,
1584
1585
1586
                     char *p_request, int i_request,
                     char **pp_data,  int *pi_data,
                     char **pp_dst )
gbazin's avatar
   
gbazin committed
1587
{
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600