rpn.c 35 KB
Newer Older
1
2
3
/*****************************************************************************
 * rpn.c : RPN evaluator for the HTTP Interface
 *****************************************************************************
4
 * Copyright (C) 2001-2006 the VideoLAN team
dionoea's avatar
dionoea committed
5
 * $Id$
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * 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
dionoea's avatar
dionoea committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24
25
26
 *****************************************************************************/

#include "http.h"
27
#include "vlc_url.h"
28
#include "vlc_meta.h"
29

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
static vlc_object_t *GetVLCObject( intf_thread_t *p_intf,
                                   const char *psz_object,
                                   vlc_bool_t *pb_need_release )
{
    intf_sys_t    *p_sys = p_intf->p_sys;
    int i_object_type = 0;
    vlc_object_t *p_object = NULL;
    *pb_need_release = VLC_FALSE;

    if( !strcmp( psz_object, "VLC_OBJECT_ROOT" ) )
        i_object_type = VLC_OBJECT_ROOT;
    else if( !strcmp( psz_object, "VLC_OBJECT_VLC" ) )
        p_object = VLC_OBJECT(p_intf->p_vlc);
    else if( !strcmp( psz_object, "VLC_OBJECT_INTF" ) )
        p_object = VLC_OBJECT(p_intf);
    else if( !strcmp( psz_object, "VLC_OBJECT_PLAYLIST" ) )
        p_object = VLC_OBJECT(p_sys->p_playlist);
    else if( !strcmp( psz_object, "VLC_OBJECT_INPUT" ) )
        p_object = VLC_OBJECT(p_sys->p_input);
    else if( !strcmp( psz_object, "VLC_OBJECT_VOUT" ) )
        i_object_type = VLC_OBJECT_VOUT;
    else if( !strcmp( psz_object, "VLC_OBJECT_AOUT" ) )
        i_object_type = VLC_OBJECT_AOUT;
    else if( !strcmp( psz_object, "VLC_OBJECT_SOUT" ) )
        i_object_type = VLC_OBJECT_SOUT;
    else
        msg_Warn( p_intf, "unknown object type (%s)", psz_object );

    if( p_object == NULL && i_object_type )
    {
        *pb_need_release = VLC_TRUE;
        p_object = vlc_object_find( p_intf, i_object_type, FIND_ANYWHERE );
    }

    return p_object;
}

67
void E_(SSInit)( rpn_stack_t *st )
68
69
70
71
{
    st->i_stack = 0;
}

72
void E_(SSClean)( rpn_stack_t *st )
73
74
75
76
77
78
79
{
    while( st->i_stack > 0 )
    {
        free( st->stack[--st->i_stack] );
    }
}

80
void E_(SSPush)( rpn_stack_t *st, const char *s )
81
82
83
84
85
86
87
{
    if( st->i_stack < STACK_MAX )
    {
        st->stack[st->i_stack++] = strdup( s );
    }
}

88
char *E_(SSPop)( rpn_stack_t *st )
89
90
91
92
93
94
95
96
97
98
99
{
    if( st->i_stack <= 0 )
    {
        return strdup( "" );
    }
    else
    {
        return st->stack[--st->i_stack];
    }
}

100
int E_(SSPopN)( rpn_stack_t *st, mvar_t  *vars )
101
102
103
104
105
106
107
{
    char *name;
    char *value;

    char *end;
    int  i;

108
    name = E_(SSPop)( st );
109
110
111
    i = strtol( name, &end, 0 );
    if( end == name )
    {
112
        value = E_(mvar_GetValue)( vars, name );
113
114
115
116
117
118
119
        i = atoi( value );
    }
    free( name );

    return( i );
}

120
void E_(SSPushN)( rpn_stack_t *st, int i )
121
{
122
    char v[12];
123

124
    snprintf( v, sizeof (v), "%d", i );
125
    E_(SSPush)( st, v );
126
127
}

128
129
void E_(EvaluateRPN)( intf_thread_t *p_intf, mvar_t  *vars,
                      rpn_stack_t *st, char *exp )
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
{
    intf_sys_t    *p_sys = p_intf->p_sys;

    while( exp != NULL && *exp != '\0' )
    {
        char *p, *s;

        /* skip space */
        while( *exp == ' ' )
        {
            exp++;
        }

        if( *exp == '\'' )
        {
            /* extract string */
            p = E_(FirstWord)( exp, exp );
147
            E_(SSPush)( st, exp );
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
            exp = p;
            continue;
        }

        /* extract token */
        p = E_(FirstWord)( exp, exp );
        s = exp;
        if( p == NULL )
        {
            exp += strlen( exp );
        }
        else
        {
            exp = p;
        }

        if( *s == '\0' )
        {
            break;
        }

        /* 1. Integer function */
        if( !strcmp( s, "!" ) )
        {
172
            E_(SSPushN)( st, ~E_(SSPopN)( st, vars ) );
173
174
175
        }
        else if( !strcmp( s, "^" ) )
        {
176
            E_(SSPushN)( st, E_(SSPopN)( st, vars ) ^ E_(SSPopN)( st, vars ) );
177
178
179
        }
        else if( !strcmp( s, "&" ) )
        {
180
            E_(SSPushN)( st, E_(SSPopN)( st, vars ) & E_(SSPopN)( st, vars ) );
181
182
183
        }
        else if( !strcmp( s, "|" ) )
        {
184
            E_(SSPushN)( st, E_(SSPopN)( st, vars ) | E_(SSPopN)( st, vars ) );
185
186
187
        }
        else if( !strcmp( s, "+" ) )
        {
188
            E_(SSPushN)( st, E_(SSPopN)( st, vars ) + E_(SSPopN)( st, vars ) );
189
190
191
        }
        else if( !strcmp( s, "-" ) )
        {
192
193
194
            int j = E_(SSPopN)( st, vars );
            int i = E_(SSPopN)( st, vars );
            E_(SSPushN)( st, i - j );
195
196
197
        }
        else if( !strcmp( s, "*" ) )
        {
198
            E_(SSPushN)( st, E_(SSPopN)( st, vars ) * E_(SSPopN)( st, vars ) );
199
200
201
202
203
        }
        else if( !strcmp( s, "/" ) )
        {
            int i, j;

204
205
            j = E_(SSPopN)( st, vars );
            i = E_(SSPopN)( st, vars );
206

207
            E_(SSPushN)( st, j != 0 ? i / j : 0 );
208
209
210
211
212
        }
        else if( !strcmp( s, "%" ) )
        {
            int i, j;

213
214
            j = E_(SSPopN)( st, vars );
            i = E_(SSPopN)( st, vars );
215

216
            E_(SSPushN)( st, j != 0 ? i % j : 0 );
217
218
219
220
        }
        /* 2. integer tests */
        else if( !strcmp( s, "=" ) )
        {
221
            E_(SSPushN)( st, E_(SSPopN)( st, vars ) == E_(SSPopN)( st, vars ) ? -1 : 0 );
222
223
224
        }
        else if( !strcmp( s, "!=" ) )
        {
225
            E_(SSPushN)( st, E_(SSPopN)( st, vars ) != E_(SSPopN)( st, vars ) ? -1 : 0 );
226
227
228
        }
        else if( !strcmp( s, "<" ) )
        {
229
230
            int j = E_(SSPopN)( st, vars );
            int i = E_(SSPopN)( st, vars );
231

232
            E_(SSPushN)( st, i < j ? -1 : 0 );
233
234
235
        }
        else if( !strcmp( s, ">" ) )
        {
236
237
            int j = E_(SSPopN)( st, vars );
            int i = E_(SSPopN)( st, vars );
238

239
            E_(SSPushN)( st, i > j ? -1 : 0 );
240
241
242
        }
        else if( !strcmp( s, "<=" ) )
        {
243
244
            int j = E_(SSPopN)( st, vars );
            int i = E_(SSPopN)( st, vars );
245

246
            E_(SSPushN)( st, i <= j ? -1 : 0 );
247
248
249
        }
        else if( !strcmp( s, ">=" ) )
        {
250
251
            int j = E_(SSPopN)( st, vars );
            int i = E_(SSPopN)( st, vars );
252

253
            E_(SSPushN)( st, i >= j ? -1 : 0 );
254
255
256
257
        }
        /* 3. string functions */
        else if( !strcmp( s, "strcat" ) )
        {
258
259
            char *s2 = E_(SSPop)( st );
            char *s1 = E_(SSPop)( st );
260
261
262
263
264
            char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );

            strcpy( str, s1 );
            strcat( str, s2 );

265
            E_(SSPush)( st, str );
266
267
268
269
270
271
            free( s1 );
            free( s2 );
            free( str );
        }
        else if( !strcmp( s, "strcmp" ) )
        {
272
273
            char *s2 = E_(SSPop)( st );
            char *s1 = E_(SSPop)( st );
274

275
            E_(SSPushN)( st, strcmp( s1, s2 ) );
276
277
278
279
280
            free( s1 );
            free( s2 );
        }
        else if( !strcmp( s, "strncmp" ) )
        {
281
282
283
            int n = E_(SSPopN)( st, vars );
            char *s2 = E_(SSPop)( st );
            char *s1 = E_(SSPop)( st );
284

285
            E_(SSPushN)( st, strncmp( s1, s2 , n ) );
286
287
288
289
290
            free( s1 );
            free( s2 );
        }
        else if( !strcmp( s, "strsub" ) )
        {
291
292
            int n = E_(SSPopN)( st, vars );
            int m = E_(SSPopN)( st, vars );
293
            int i_len;
294
            char *s = E_(SSPop)( st );
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
            char *str;

            if( n >= m )
            {
                i_len = n - m + 1;
            }
            else
            {
                i_len = 0;
            }

            str = malloc( i_len + 1 );

            memcpy( str, s + m - 1, i_len );
            str[ i_len ] = '\0';

311
            E_(SSPush)( st, str );
312
313
314
315
316
            free( s );
            free( str );
        }
        else if( !strcmp( s, "strlen" ) )
        {
317
            char *str = E_(SSPop)( st );
318

319
            E_(SSPushN)( st, strlen( str ) );
320
321
322
323
            free( str );
        }
        else if( !strcmp( s, "str_replace" ) )
        {
324
325
326
            char *psz_to = E_(SSPop)( st );
            char *psz_from = E_(SSPop)( st );
            char *psz_in = E_(SSPop)( st );
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
            char *psz_in_current = psz_in;
            char *psz_out = malloc( strlen(psz_in) * strlen(psz_to) + 1 );
            char *psz_out_current = psz_out;

            while( (p = strstr( psz_in_current, psz_from )) != NULL )
            {
                memcpy( psz_out_current, psz_in_current, p - psz_in_current );
                psz_out_current += p - psz_in_current;
                strcpy( psz_out_current, psz_to );
                psz_out_current += strlen(psz_to);
                psz_in_current = p + strlen(psz_from);
            }
            strcpy( psz_out_current, psz_in_current );
            psz_out_current += strlen(psz_in_current);
            *psz_out_current = '\0';

343
            E_(SSPush)( st, psz_out );
344
345
346
347
348
349
350
            free( psz_to );
            free( psz_from );
            free( psz_in );
            free( psz_out );
        }
        else if( !strcmp( s, "url_extract" ) )
        {
351
352
            char *url = E_(mvar_GetValue)( vars, "url_value" );
            char *name = E_(SSPop)( st );
353
354
355
            char value[512];
            char *tmp;

356
            E_(ExtractURIValue)( url, name, value, 512 );
357
            decode_URI( value );
358
            tmp = E_(FromUTF8)( p_intf, value );
359
            E_(SSPush)( st, tmp );
360
361
362
363
364
            free( tmp );
            free( name );
        }
        else if( !strcmp( s, "url_encode" ) )
        {
365
            char *url = E_(SSPop)( st );
366
367
368
369
370
371
372
            char *value;

            value = E_(ToUTF8)( p_intf, url );
            free( url );
            url = value;
            value = vlc_UrlEncode( url );
            free( url );
373
            E_(SSPush)( st, value );
374
375
376
377
            free( value );
        }
        else if( !strcmp( s, "addslashes" ) )
        {
378
            char *psz_src = E_(SSPop)( st );
379
380
381
382
383
384
385
            char *psz_dest;
            char *str = psz_src;

            p = psz_dest = malloc( strlen( str ) * 2 + 1 );

            while( *str != '\0' )
            {
386
                if( *str == '"' || *str == '\'' || *str == '\\' )
387
388
389
390
391
392
393
394
                {
                    *p++ = '\\';
                }
                *p++ = *str;
                str++;
            }
            *p = '\0';

395
            E_(SSPush)( st, psz_dest );
396
397
398
399
400
            free( psz_src );
            free( psz_dest );
        }
        else if( !strcmp( s, "stripslashes" ) )
        {
401
            char *psz_src = E_(SSPop)( st );
402
            char *psz_dest;
403
            char *str = psz_src;
404
405
406

            p = psz_dest = strdup( psz_src );

407
            while( *str )
408
            {
409
                if( *str == '\\' && *(str + 1) )
410
                {
411
                    str++;
412
                }
413
                *p++ = *str++;
414
415
416
            }
            *p = '\0';

417
            E_(SSPush)( st, psz_dest );
418
419
420
421
422
            free( psz_src );
            free( psz_dest );
        }
        else if( !strcmp( s, "htmlspecialchars" ) )
        {
423
            char *psz_src = E_(SSPop)( st );
424
425
            char *psz_dest;

426
            psz_dest = convert_xml_special_chars( psz_src );
427

428
            E_(SSPush)( st, psz_dest );
429
430
431
432
433
            free( psz_src );
            free( psz_dest );
        }
        else if( !strcmp( s, "realpath" ) )
        {
434
            char *psz_src = E_(SSPop)( st );
435
            char *psz_dir = E_(RealPath)( p_intf, psz_src );
436

437
            E_(SSPush)( st, psz_dir );
438
            free( psz_src );
439
            free( psz_dir );
440
441
442
443
        }
        /* 4. stack functions */
        else if( !strcmp( s, "dup" ) )
        {
444
445
446
            char *str = E_(SSPop)( st );
            E_(SSPush)( st, str );
            E_(SSPush)( st, str );
447
448
449
450
            free( str );
        }
        else if( !strcmp( s, "drop" ) )
        {
451
            char *str = E_(SSPop)( st );
452
453
454
455
            free( str );
        }
        else if( !strcmp( s, "swap" ) )
        {
456
457
            char *s1 = E_(SSPop)( st );
            char *s2 = E_(SSPop)( st );
458

459
460
            E_(SSPush)( st, s1 );
            E_(SSPush)( st, s2 );
461
462
463
464
465
            free( s1 );
            free( s2 );
        }
        else if( !strcmp( s, "flush" ) )
        {
466
467
            E_(SSClean)( st );
            E_(SSInit)( st );
468
469
470
        }
        else if( !strcmp( s, "store" ) )
        {
471
472
            char *value = E_(SSPop)( st );
            char *name  = E_(SSPop)( st );
473

474
            E_(mvar_PushNewVar)( vars, name, value );
475
476
477
478
479
            free( name );
            free( value );
        }
        else if( !strcmp( s, "value" ) )
        {
480
481
            char *name  = E_(SSPop)( st );
            char *value = E_(mvar_GetValue)( vars, name );
482

483
            E_(SSPush)( st, value );
484
485
486
487
488
489

            free( name );
        }
        /* 5. player control */
        else if( !strcmp( s, "vlc_play" ) )
        {
490
            int i_id = E_(SSPopN)( st, vars );
491
492
            int i_ret;

493
494
            i_ret = playlist_Control( p_sys->p_playlist, PLAYLIST_VIEWPLAY,
                                      NULL,
495
496
497
                                      playlist_ItemGetById( p_sys->p_playlist,
                                      i_id ) );
            msg_Dbg( p_intf, "requested playlist item: %i", i_id );
498
            E_(SSPushN)( st, i_ret );
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
        }
        else if( !strcmp( s, "vlc_stop" ) )
        {
            playlist_Control( p_sys->p_playlist, PLAYLIST_STOP );
            msg_Dbg( p_intf, "requested playlist stop" );
        }
        else if( !strcmp( s, "vlc_pause" ) )
        {
            playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
            msg_Dbg( p_intf, "requested playlist pause" );
        }
        else if( !strcmp( s, "vlc_next" ) )
        {
            playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
            msg_Dbg( p_intf, "requested playlist next" );
        }
        else if( !strcmp( s, "vlc_previous" ) )
        {
            playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1 );
            msg_Dbg( p_intf, "requested playlist previous" );
        }
        else if( !strcmp( s, "vlc_seek" ) )
        {
522
            char *psz_value = E_(SSPop)( st );
523
            E_(HandleSeek)( p_intf, psz_value );
524
525
526
527
528
529
530
            msg_Dbg( p_intf, "requested playlist seek: %s", psz_value );
            free( psz_value );
        }
        else if( !strcmp( s, "vlc_var_type" )
                  || !strcmp( s, "vlc_config_type" ) )
        {
            vlc_object_t *p_object;
531
532
            const char *psz_type = NULL;
            int i_type = 0;
533
534
535

            if( !strcmp( s, "vlc_var_type" ) )
            {
536
537
                char *psz_object = E_(SSPop)( st );
                char *psz_variable = E_(SSPop)( st );
538
539
540
541
                vlc_bool_t b_need_release;

                p_object = GetVLCObject( p_intf, psz_object, &b_need_release );

542
543
                if( p_object != NULL )
                    i_type = var_Type( p_object, psz_variable );
544
545
546
547
                free( psz_variable );
                free( psz_object );
                if( b_need_release && p_object != NULL )
                    vlc_object_release( p_object );
548
549
550
            }
            else
            {
551
                char *psz_variable = E_(SSPop)( st );
552
553
                p_object = VLC_OBJECT(p_intf);
                i_type = config_GetType( p_object, psz_variable );
554
                free( psz_variable );
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
            }

            if( p_object != NULL )
            {
                switch( i_type & VLC_VAR_TYPE )
                {
                case VLC_VAR_BOOL:
                    psz_type = "VLC_VAR_BOOL";
                    break;
                case VLC_VAR_INTEGER:
                    psz_type = "VLC_VAR_INTEGER";
                    break;
                case VLC_VAR_HOTKEY:
                    psz_type = "VLC_VAR_HOTKEY";
                    break;
                case VLC_VAR_STRING:
                    psz_type = "VLC_VAR_STRING";
                    break;
                case VLC_VAR_MODULE:
                    psz_type = "VLC_VAR_MODULE";
                    break;
                case VLC_VAR_FILE:
                    psz_type = "VLC_VAR_FILE";
                    break;
                case VLC_VAR_DIRECTORY:
                    psz_type = "VLC_VAR_DIRECTORY";
                    break;
                case VLC_VAR_VARIABLE:
                    psz_type = "VLC_VAR_VARIABLE";
                    break;
                case VLC_VAR_FLOAT:
                    psz_type = "VLC_VAR_FLOAT";
                    break;
                default:
                    psz_type = "UNDEFINED";
                }
            }
            else
                psz_type = "INVALID";

595
            E_(SSPush)( st, psz_type );
596
597
598
        }
        else if( !strcmp( s, "vlc_var_set" ) )
        {
599
600
            char *psz_object = E_(SSPop)( st );
            char *psz_variable = E_(SSPop)( st );
601
            vlc_bool_t b_need_release;
602

603
604
605
606
            vlc_object_t *p_object = GetVLCObject( p_intf, psz_object,
                                                   &b_need_release );

            if( p_object != NULL )
607
608
609
610
611
612
            {
                vlc_bool_t b_error = VLC_FALSE;
                char *psz_value = NULL;
                vlc_value_t val;
                int i_type;

613
                i_type = var_Type( p_object, psz_variable );
614
615
616
617

                switch( i_type & VLC_VAR_TYPE )
                {
                case VLC_VAR_BOOL:
618
                    val.b_bool = E_(SSPopN)( st, vars );
619
620
                    msg_Dbg( p_intf, "requested %s var change: %s->%d",
                             psz_object, psz_variable, val.b_bool );
621
622
623
                    break;
                case VLC_VAR_INTEGER:
                case VLC_VAR_HOTKEY:
624
                    val.i_int = E_(SSPopN)( st, vars );
625
626
                    msg_Dbg( p_intf, "requested %s var change: %s->%d",
                             psz_object, psz_variable, val.i_int );
627
628
629
630
631
632
                    break;
                case VLC_VAR_STRING:
                case VLC_VAR_MODULE:
                case VLC_VAR_FILE:
                case VLC_VAR_DIRECTORY:
                case VLC_VAR_VARIABLE:
633
                    val.psz_string = psz_value = E_(SSPop)( st );
634
635
                    msg_Dbg( p_intf, "requested %s var change: %s->%s",
                             psz_object, psz_variable, psz_value );
636
637
                    break;
                case VLC_VAR_FLOAT:
638
                    psz_value = E_(SSPop)( st );
639
                    val.f_float = atof( psz_value );
640
641
                    msg_Dbg( p_intf, "requested %s var change: %s->%f",
                             psz_object, psz_variable, val.f_float );
642
643
                    break;
                default:
644
                    E_(SSPopN)( st, vars );
645
646
                    msg_Warn( p_intf, "invalid %s variable type %d (%s)",
                              psz_object, i_type & VLC_VAR_TYPE, psz_variable );
647
648
649
650
                    b_error = VLC_TRUE;
                }

                if( !b_error )
651
                    var_Set( p_object, psz_variable, val );
652
653
654
655
                if( psz_value != NULL )
                    free( psz_value );
            }
            else
656
                msg_Warn( p_intf, "vlc_var_set called without an object" );
657
            free( psz_variable );
658
659
660
661
            free( psz_object );

            if( b_need_release && p_object != NULL )
                vlc_object_release( p_object );
662
663
664
        }
        else if( !strcmp( s, "vlc_var_get" ) )
        {
665
666
            char *psz_object = E_(SSPop)( st );
            char *psz_variable = E_(SSPop)( st );
667
668
669
670
            vlc_bool_t b_need_release;

            vlc_object_t *p_object = GetVLCObject( p_intf, psz_object,
                                                   &b_need_release );
671

672
            if( p_object != NULL )
673
674
675
676
            {
                vlc_value_t val;
                int i_type;

677
678
                i_type = var_Type( p_object, psz_variable );
                var_Get( p_object, psz_variable, &val );
679
680
681
682

                switch( i_type & VLC_VAR_TYPE )
                {
                case VLC_VAR_BOOL:
683
                    E_(SSPushN)( st, val.b_bool );
684
685
686
                    break;
                case VLC_VAR_INTEGER:
                case VLC_VAR_HOTKEY:
687
                    E_(SSPushN)( st, val.i_int );
688
689
690
691
692
693
                    break;
                case VLC_VAR_STRING:
                case VLC_VAR_MODULE:
                case VLC_VAR_FILE:
                case VLC_VAR_DIRECTORY:
                case VLC_VAR_VARIABLE:
694
                    E_(SSPush)( st, val.psz_string );
695
696
697
698
699
                    free( val.psz_string );
                    break;
                case VLC_VAR_FLOAT:
                {
                    char psz_value[20];
700
701
702
                    lldiv_t value = lldiv( val.f_float * 1000000, 1000000 );
                    snprintf( psz_value, sizeof(psz_value), I64Fd".%06u",
                                    value.quot, (unsigned int)value.rem );
703
                    E_(SSPush)( st, psz_value );
704
705
706
                    break;
                }
                default:
707
708
                    msg_Warn( p_intf, "invalid %s variable type %d (%s)",
                              psz_object, i_type & VLC_VAR_TYPE, psz_variable );
709
                    E_(SSPush)( st, "" );
710
711
712
713
                }
            }
            else
            {
714
                msg_Warn( p_intf, "vlc_var_get called without an object" );
715
                E_(SSPush)( st, "" );
716
717
            }
            free( psz_variable );
718
719
720
721
            free( psz_object );

            if( b_need_release && p_object != NULL )
                vlc_object_release( p_object );
722
        }
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
        else if( !strcmp( s, "vlc_object_exists" ) )
        {
            char *psz_object = E_(SSPop)( st );
            vlc_bool_t b_need_release;

            vlc_object_t *p_object = GetVLCObject( p_intf, psz_object,
                                                   &b_need_release );
            if( b_need_release && p_object != NULL )
                vlc_object_release( p_object );

            if( p_object != NULL )
                E_(SSPush)( st, "1" );
            else
                E_(SSPush)( st, "0" );
        }
738
739
        else if( !strcmp( s, "vlc_config_set" ) )
        {
740
            char *psz_variable = E_(SSPop)( st );
741
742
743
744
745
746
            int i_type = config_GetType( p_intf, psz_variable );

            switch( i_type & VLC_VAR_TYPE )
            {
            case VLC_VAR_BOOL:
            case VLC_VAR_INTEGER:
747
                config_PutInt( p_intf, psz_variable, E_(SSPopN)( st, vars ) );
748
749
750
751
752
753
                break;
            case VLC_VAR_STRING:
            case VLC_VAR_MODULE:
            case VLC_VAR_FILE:
            case VLC_VAR_DIRECTORY:
            {
754
                char *psz_string = E_(SSPop)( st );
755
756
757
758
759
760
                config_PutPsz( p_intf, psz_variable, psz_string );
                free( psz_string );
                break;
            }
            case VLC_VAR_FLOAT:
            {
761
                char *psz_string = E_(SSPop)( st );
762
763
764
765
766
767
768
769
770
771
772
773
                config_PutFloat( p_intf, psz_variable, atof(psz_string) );
                free( psz_string );
                break;
            }
            default:
                msg_Warn( p_intf, "vlc_config_set called on unknown var (%s)",
                          psz_variable );
            }
            free( psz_variable );
        }
        else if( !strcmp( s, "vlc_config_get" ) )
        {
774
            char *psz_variable = E_(SSPop)( st );
775
776
777
778
779
780
            int i_type = config_GetType( p_intf, psz_variable );

            switch( i_type & VLC_VAR_TYPE )
            {
            case VLC_VAR_BOOL:
            case VLC_VAR_INTEGER:
781
                E_(SSPushN)( st, config_GetInt( p_intf, psz_variable ) );
782
783
784
785
786
787
788
                break;
            case VLC_VAR_STRING:
            case VLC_VAR_MODULE:
            case VLC_VAR_FILE:
            case VLC_VAR_DIRECTORY:
            {
                char *psz_string = config_GetPsz( p_intf, psz_variable );
789
                E_(SSPush)( st, psz_string );
790
791
792
793
794
795
                free( psz_string );
                break;
            }
            case VLC_VAR_FLOAT:
            {
                char psz_string[20];
796
797
798
799
                lldiv_t value = lldiv( config_GetFloat( p_intf, psz_variable )
                                       * 1000000, 1000000 );
                snprintf( psz_string, sizeof(psz_string), I64Fd".%06u",
                          value.quot, (unsigned int)value.rem );
800
                E_(SSPush)( st, psz_string );
801
802
803
804
805
806
807
808
809
810
                break;
            }
            default:
                msg_Warn( p_intf, "vlc_config_get called on unknown var (%s)",
                          psz_variable );
            }
            free( psz_variable );
        }
        else if( !strcmp( s, "vlc_config_save" ) )
        {
811
            char *psz_module = E_(SSPop)( st );
812
813
814
815
816
817
818
819
820
821
822
            int i_result;

            if( !*psz_module )
            {
                free( psz_module );
                psz_module = NULL;
            }
            i_result = config_SaveConfigFile( p_intf, psz_module );

            if( psz_module != NULL )
                free( psz_module );
823
            E_(SSPushN)( st, i_result );
824
825
826
827
828
829
830
831
        }
        else if( !strcmp( s, "vlc_config_reset" ) )
        {
            config_ResetAll( p_intf );
        }
        /* 6. playlist functions */
        else if( !strcmp( s, "playlist_add" ) )
        {
832
833
            char *psz_name = E_(SSPop)( st );
            char *mrl = E_(SSPop)( st );
834
            char *tmp;
835
            input_item_t *p_input;
836
837
838
839
840
841
842
843
844
845
846
            int i_id;

            tmp = E_(ToUTF8)( p_intf, psz_name );
            free( psz_name );
            psz_name = tmp;
            tmp = E_(ToUTF8)( p_intf, mrl );
            free( mrl );
            mrl = tmp;

            if( !*psz_name )
            {
847
                p_input = E_(MRLParse)( p_intf, mrl, mrl );
848
849
850
            }
            else
            {
851
                p_input = E_(MRLParse)( p_intf, mrl, psz_name );
852
853
            }

854
            if( !p_input || !p_input->psz_uri || !*p_input->psz_uri )
855
856
857
858
859
860
            {
                i_id = VLC_EGENERIC;
                msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
            }
            else
            {
861
                i_id = playlist_PlaylistAddInput( p_sys->p_playlist, p_input,
862
863
864
                                         PLAYLIST_APPEND, PLAYLIST_END );
                msg_Dbg( p_intf, "requested mrl add: %s", mrl );
            }
865
            E_(SSPushN)( st, i_id );
866
867
868
869
870
871
872
873
874
875
876

            free( mrl );
            free( psz_name );
        }
        else if( !strcmp( s, "playlist_empty" ) )
        {
            playlist_LockClear( p_sys->p_playlist );
            msg_Dbg( p_intf, "requested playlist empty" );
        }
        else if( !strcmp( s, "playlist_delete" ) )
        {
877
            int i_id = E_(SSPopN)( st, vars );
878
879
880
881
882
            playlist_LockDelete( p_sys->p_playlist, i_id );
            msg_Dbg( p_intf, "requested playlist delete: %d", i_id );
        }
        else if( !strcmp( s, "playlist_move" ) )
        {
883
884
885
886
            /*int i_newpos =*/ E_(SSPopN)( st, vars );
            /*int i_pos =*/ E_(SSPopN)( st, vars );
            /* FIXME FIXME TODO TODO XXX XXX
            do not release before fixing this
887
888
889
890
891
892
893
894
895
896
            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 to move playlist item %d to %d",
                     i_pos, i_newpos);
897
898
               FIXME FIXME TODO TODO XXX XXX */
            msg_Err( p_intf, "moving using indexes is obsolete. We need to update this function" );
899
        }
900
901
902
903
904
905
        else if( !strcmp( s, "playlist_sort" ) )
        {
            int i_order = E_(SSPopN)( st, vars );
            int i_sort = E_(SSPopN)( st, vars );
            i_order = i_order % 2;
            i_sort = i_sort % 9;
906
907
            /* FIXME FIXME TODO TODO XXX XXX
            do not release before fixing this
908
909
910
911
912
            playlist_RecursiveNodeSort(  p_sys->p_playlist,
                                         p_sys->p_playlist->p_general,
                                         i_sort, i_order );
            msg_Dbg( p_intf, "requested sort playlist by : %d in order : %d",
                     i_sort, i_order );
913
914
               FIXME FIXME TODO TODO XXX XXX */
            msg_Err( p_intf, "this needs to be fixed to use the new playlist framework" );
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
        }
        else if( !strcmp( s, "services_discovery_add" ) )
        {
            char *psz_sd = E_(SSPop)( st );
            playlist_ServicesDiscoveryAdd( p_sys->p_playlist, psz_sd );
            free( psz_sd );
        }
        else if( !strcmp( s, "services_discovery_remove" ) )
        {
            char *psz_sd = E_(SSPop)( st );
            playlist_ServicesDiscoveryRemove( p_sys->p_playlist, psz_sd );
            free( psz_sd );
        }
        else if( !strcmp( s, "services_discovery_is_loaded" ) )
        {
            char *psz_sd = E_(SSPop)( st );
            E_(SSPushN)( st,
            playlist_IsServicesDiscoveryLoaded( p_sys->p_playlist, psz_sd ) );
            free( psz_sd );
        }
dionoea's avatar
dionoea committed
935
        else if( !strcmp( s, "vlc_volume_set" ) )
dionoea's avatar
dionoea committed
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
        {
            char *psz_vol = E_(SSPop)( st );
            int i_value;
            audio_volume_t i_volume;
            aout_VolumeGet( p_intf, &i_volume );
            if( psz_vol[0] == '+' )
            {
                i_value = atoi( psz_vol );
                if( (i_volume + i_value) > AOUT_VOLUME_MAX )
                    aout_VolumeSet( p_intf, AOUT_VOLUME_MAX );
                else
                    aout_VolumeSet( p_intf, i_volume + i_value );
            }
            else if( psz_vol[0] == '-' )
            {
                i_value = atoi( psz_vol );
                if( (i_volume + i_value) < AOUT_VOLUME_MIN )
                    aout_VolumeSet( p_intf, AOUT_VOLUME_MIN );
                else
                    aout_VolumeSet( p_intf, i_volume + i_value );
            }
            else if( strstr( psz_vol, "%") != NULL )
            {
                i_value = atoi( psz_vol );
                if( i_value < 0 ) i_value = 0;
                if( i_value > 400 ) i_value = 400;
                aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/400+AOUT_VOLUME_MIN);
            }
            else
            {
                i_value = atoi( psz_vol );
                if( i_value > AOUT_VOLUME_MAX ) i_value = AOUT_VOLUME_MAX;
                if( i_value < AOUT_VOLUME_MIN ) i_value = AOUT_VOLUME_MIN;
                aout_VolumeSet( p_intf, i_value );
            }
            aout_VolumeGet( p_intf, &i_volume );
972
            free( psz_vol );
dionoea's avatar
dionoea committed
973
        }
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
        else if( !strcmp( s, "vlc_get_meta" ) )
        {
            char *psz_meta = E_(SSPop)( st );
            char *psz_val = NULL;
            if( p_sys->p_input && p_sys->p_input->input.p_item )
            {
#define p_item  p_sys->p_input->input.p_item
                if( !strcmp( psz_meta, "ARTIST" ) )
                {
                    psz_val = vlc_input_item_GetInfo( p_item,
                                _(VLC_META_INFO_CAT), _(VLC_META_ARTIST) );
                }
                else if( !strcmp( psz_meta, "TITLE" ) )
                {
                    psz_val = vlc_input_item_GetInfo( p_item,
                                _(VLC_META_INFO_CAT), _(VLC_META_TITLE) );
dionoea's avatar
dionoea committed
990
991
                    if( psz_val == NULL )
                        psz_val == strdup( p_item->psz_name );
992
993
994
995
996
997
                }
                else if( !strcmp( psz_meta, "ALBUM" ) )
                {
                    psz_val = vlc_input_item_GetInfo( p_item,
                                _(VLC_META_INFO_CAT), _(VLC_META_COLLECTION) );
                }
998
999
1000
1001
1002
                else if( !strcmp( psz_meta, "GENRE" ) )
                {
                    psz_val = vlc_input_item_GetInfo( p_item,
                                _(VLC_META_INFO_CAT), _(VLC_META_GENRE) );
                }
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
                else
                {
                    psz_val = vlc_input_item_GetInfo( p_item,
                                            _(VLC_META_INFO_CAT), psz_meta );
                }
#undef p_item
            }
            if( psz_val == NULL ) psz_val = strdup( "" );
            E_(SSPush)( st, psz_val );
            free( psz_meta );
            free( psz_val );
        }
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
        else if( !strcmp( s, "vlm_command" ) || !strcmp( s, "vlm_cmd" ) )
        {
            char *psz_elt;
            char *psz_cmd = strdup( "" );
            char *psz_error;
            vlm_message_t *vlm_answer;

            /* make sure that we have a vlm object */
            if( p_intf->p_sys->p_vlm == NULL )
                p_intf->p_sys->p_vlm = vlm_New( p_intf );


            /* vlm command uses the ';' delimiter
             * (else we can't know when to stop) */
            while( strcmp( psz_elt = E_(SSPop)( st ), "" )
                   && strcmp( psz_elt, ";" ) )
            {
                char *psz_buf =
                    (char *)malloc( strlen( psz_cmd ) + strlen( psz_elt ) + 2 );
                sprintf( psz_buf, "%s %s", psz_cmd, psz_elt );
                free( psz_cmd );
                free( psz_elt );
                psz_cmd = psz_buf;
            }

dionoea's avatar
dionoea committed
1040
            msg_Dbg( p_intf, "executing vlm command: %s", psz_cmd );
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
            vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz_cmd, &vlm_answer );

            if( vlm_answer->psz_value == NULL )
            {
                psz_error = strdup( "" );
            }
            else
            {
                psz_error = malloc( strlen(vlm_answer->psz_name) +
                                    strlen(vlm_answer->psz_value) +
                                    strlen( " : ") + 1 );
                sprintf( psz_error , "%s : %s" , vlm_answer->psz_name,
                                                 vlm_answer->psz_value );
            }

            E_(mvar_AppendNewVar)( vars, "vlm_error", psz_error );
1057
1058
1059
            /* this is kind of a duplicate but we need to have the message
             * without the command name for the "export" command */
            E_(mvar_AppendNewVar)( vars, "vlm_value", vlm_answer->psz_value );
1060
1061
1062
1063
1064
            vlm_MessageDelete( vlm_answer );

            free( psz_cmd );
            free( psz_error );
        }
yoann's avatar
yoann committed
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
        else if( !strcmp( s, "snapshot" ) )
        {
            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 )
                {
                    vout_Control( p_vout, VOUT_SNAPSHOT );
                    vlc_object_release( p_vout );
                    msg_Dbg( p_intf, "requested snapshot" );
                }
            }
            break;

        }
1083
1084
        else
        {
1085
            E_(SSPush)( st, s );
1086
1087
1088
        }
    }
}