]> git.sesse.net Git - vlc/blob - modules/control/http/util.c
HTTP interface: Display estimated item time when available.
[vlc] / modules / control / http / util.c
1 /*****************************************************************************
2  * util.c : Utility functions for HTTP interface
3  *****************************************************************************
4  * Copyright (C) 2001-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #include "http.h"
27 #include "vlc_strings.h"
28
29 /****************************************************************************
30  * File and directory functions
31  ****************************************************************************/
32
33 /* ToUrl: create a good name for an url from filename */
34 char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index )
35 {
36     char *url, *p;
37
38     url = p = malloc( strlen( name ) + 1 );
39
40     *pb_index = VLC_FALSE;
41     if( !url || !p )
42     {
43         return NULL;
44     }
45
46 #ifdef WIN32
47     while( *name == '\\' || *name == '/' )
48 #else
49     while( *name == '/' )
50 #endif
51     {
52         name++;
53     }
54
55     *p++ = '/';
56     strcpy( p, name );
57
58 #ifdef WIN32
59     /* convert '\\' into '/' */
60     name = p;
61     while( *name )
62     {
63         if( *name == '\\' )
64             *name = '/';
65         name++;
66     }
67 #endif
68
69     /* index.* -> / */
70     if( ( p = strrchr( url, '/' ) ) != NULL )
71     {
72         if( !strncmp( p, "/index.", 7 ) )
73         {
74             p[1] = '\0';
75             *pb_index = VLC_TRUE;
76         }
77     }
78     return url;
79 }
80
81 /* Load a file */
82 int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data )
83 {
84     int i_read;
85
86     /* just load the file */
87     *pi_data = 0;
88     *pp_data = malloc( 1025 );  /* +1 for \0 */
89     while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
90     {
91         *pi_data += 1024;
92         *pp_data = realloc( *pp_data, *pi_data  + 1025 );
93     }
94     if( i_read > 0 )
95     {
96         *pi_data += i_read;
97     }
98     (*pp_data)[*pi_data] = '\0';
99
100     return VLC_SUCCESS;
101 }
102
103 /* Parse a directory and recursively add files */
104 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
105                         char *psz_dir )
106 {
107     intf_sys_t     *p_sys = p_intf->p_sys;
108     char           dir[MAX_DIR_SIZE];
109 #ifdef HAVE_SYS_STAT_H
110     struct stat   stat_info;
111 #endif
112     DIR           *p_dir;
113     vlc_acl_t     *p_acl;
114     FILE          *file;
115
116     char          *user = NULL;
117     char          *password = NULL;
118
119     int           i_dirlen;
120
121     char sep;
122
123 #if defined( WIN32 )
124     sep = '\\';
125 #else
126     sep = '/';
127 #endif
128
129 #ifdef HAVE_SYS_STAT_H
130     if( utf8_stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
131     {
132         return VLC_EGENERIC;
133     }
134 #endif
135
136     if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
137     {
138         msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
139         return VLC_EGENERIC;
140     }
141
142     i_dirlen = strlen( psz_dir );
143     if( i_dirlen + 10 > MAX_DIR_SIZE )
144     {
145         msg_Warn( p_intf, "skipping too deep dir (%s)", psz_dir );
146         return 0;
147     }
148
149     msg_Dbg( p_intf, "dir=%s", psz_dir );
150
151     sprintf( dir, "%s%c.access", psz_dir, sep );
152     if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
153     {
154         char line[1024];
155         int  i_size;
156
157         msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
158
159         i_size = fread( line, 1, 1023, file );
160         if( i_size > 0 )
161         {
162             char *p;
163             while( i_size > 0 && ( line[i_size-1] == '\n' ||
164                    line[i_size-1] == '\r' ) )
165             {
166                 i_size--;
167             }
168
169             line[i_size] = '\0';
170
171             p = strchr( line, ':' );
172             if( p )
173             {
174                 *p++ = '\0';
175                 user = strdup( line );
176                 password = strdup( p );
177             }
178         }
179         msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
180                  user, password, i_size );
181
182         fclose( file );
183     }
184
185     sprintf( dir, "%s%c.hosts", psz_dir, sep );
186     p_acl = ACL_Create( p_intf, VLC_FALSE );
187     if( ACL_LoadFile( p_acl, dir ) )
188     {
189         ACL_Destroy( p_acl );
190         p_acl = NULL;
191     }
192
193     for( ;; )
194     {
195         const char *psz_filename;
196         /* parse psz_src dir */
197         if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
198         {
199             break;
200         }
201
202         if( ( psz_filename[0] == '.' )
203          || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
204             continue;
205
206         sprintf( dir, "%s%c%s", psz_dir, sep, psz_filename );
207         LocaleFree( psz_filename );
208
209         if( E_(ParseDirectory)( p_intf, psz_root, dir ) )
210         {
211             httpd_file_sys_t *f = NULL;
212             httpd_handler_sys_t *h = NULL;
213             vlc_bool_t b_index;
214             char *psz_tmp, *psz_file, *psz_name, *psz_ext;
215
216             psz_tmp = vlc_fix_readdir_charset( p_intf, dir );
217             psz_file = E_(FromUTF8)( p_intf, psz_tmp );
218             free( psz_tmp );
219             psz_tmp = vlc_fix_readdir_charset( p_intf,
220                                                &dir[strlen( psz_root )] );
221             psz_name = E_(FileToUrl)( psz_tmp, &b_index );
222             free( psz_tmp );
223             psz_ext = strrchr( psz_file, '.' );
224             if( psz_ext != NULL )
225             {
226                 int i;
227                 psz_ext++;
228                 for( i = 0; i < p_sys->i_handlers; i++ )
229                     if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
230                         break;
231                 if( i < p_sys->i_handlers )
232                 {
233                     f = malloc( sizeof( httpd_handler_sys_t ) );
234                     h = (httpd_handler_sys_t *)f;
235                     f->b_handler = VLC_TRUE;
236                     h->p_association = p_sys->pp_handlers[i];
237                 }
238             }
239             if( f == NULL )
240             {
241                 f = malloc( sizeof( httpd_file_sys_t ) );
242                 f->b_handler = VLC_FALSE;
243             }
244
245             f->p_intf  = p_intf;
246             f->p_file = NULL;
247             f->p_redir = NULL;
248             f->p_redir2 = NULL;
249             f->file = psz_file;
250             f->name = psz_name;
251             f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? VLC_TRUE : VLC_FALSE;
252
253             if( !f->name )
254             {
255                 msg_Err( p_intf , "unable to parse directory" );
256                 closedir( p_dir );
257                 free( f );
258                 return( VLC_ENOMEM );
259             }
260             msg_Dbg( p_intf, "file=%s (url=%s)",
261                      f->file, f->name );
262
263             if( !f->b_handler )
264             {
265                 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
266                                            f->name,
267                                            f->b_html ? ( strstr( &dir[strlen( psz_root )], ".xml" ) ? "text/xml; charset=UTF-8" : p_sys->psz_html_type ) :
268                                             NULL,
269                                            user, password, p_acl,
270                                            E_(HttpCallback), f );
271                 if( f->p_file != NULL )
272                 {
273                     TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
274                 }
275             }
276             else
277             {
278                 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
279                                                  f->name,
280                                                  user, password, p_acl,
281                                                  E_(HandlerCallback), h );
282                 if( h->p_handler != NULL )
283                 {
284                     TAB_APPEND( p_sys->i_files, p_sys->pp_files,
285                                 (httpd_file_sys_t *)h );
286                 }
287             }
288
289             /* for url that ends by / add
290              *  - a redirect from rep to rep/
291              *  - in case of index.* rep/index.html to rep/ */
292             if( f && f->name[strlen(f->name) - 1] == '/' )
293             {
294                 char *psz_redir = strdup( f->name );
295                 char *p;
296                 psz_redir[strlen( psz_redir ) - 1] = '\0';
297
298                 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
299                 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
300                 free( psz_redir );
301
302                 if( b_index && ( p = strstr( f->file, "index." ) ) )
303                 {
304                     asprintf( &psz_redir, "%s%s", f->name, p );
305
306                     msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
307                     f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
308                                                      f->name, psz_redir );
309
310                     free( psz_redir );
311                 }
312             }
313         }
314     }
315
316     if( user )
317     {
318         free( user );
319     }
320     if( password )
321     {
322         free( password );
323     }
324
325     ACL_Destroy( p_acl );
326     closedir( p_dir );
327
328     return VLC_SUCCESS;
329 }
330
331
332 /**************************************************************************
333  * Locale functions
334  **************************************************************************/
335 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
336 {
337     intf_sys_t    *p_sys = p_intf->p_sys;
338
339     if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
340     {
341         size_t i_in = strlen(psz_utf8);
342         size_t i_out = i_in * 2;
343         char *psz_local = malloc(i_out + 1);
344         char *psz_out = psz_local;
345         size_t i_ret;
346         char psz_tmp[i_in + 1];
347         char *psz_in = psz_tmp;
348         uint8_t *p = (uint8_t *)psz_tmp;
349         strcpy( psz_tmp, psz_utf8 );
350
351         /* Fix Unicode quotes. If we are here we are probably converting
352          * to an inferior charset not understanding Unicode quotes. */
353         while( *p )
354         {
355             if( p[0] == 0xe2 && p[1] == 0x80 && p[2] == 0x99 )
356             {
357                 *p = '\'';
358                 memmove( &p[1], &p[3], strlen((char *)&p[3]) + 1 );
359             }
360             if( p[0] == 0xe2 && p[1] == 0x80 && p[2] == 0x9a )
361             {
362                 *p = '"';
363                 memmove( &p[1], &p[3], strlen((char *)&p[3]) + 1 );
364             }
365             p++;
366         }
367         i_in = strlen( psz_tmp );
368
369         i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
370                            &psz_out, &i_out );
371         if( i_ret == (size_t)-1 || i_in )
372         {
373             msg_Warn( p_intf,
374                       "failed to convert \"%s\" to desired charset (%s)",
375                       psz_utf8, strerror(errno) );
376             free( psz_local );
377             return strdup( psz_utf8 );
378         }
379
380         *psz_out = '\0';
381         return psz_local;
382     }
383     else
384         return strdup( psz_utf8 );
385 }
386
387 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
388 {
389     intf_sys_t    *p_sys = p_intf->p_sys;
390
391     if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
392     {
393         char *psz_in = psz_local;
394         size_t i_in = strlen(psz_in);
395         size_t i_out = i_in * 6;
396         char *psz_utf8 = malloc(i_out + 1);
397         char *psz_out = psz_utf8;
398
399         size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
400                                   &psz_out, &i_out );
401         if( i_ret == (size_t)-1 || i_in )
402         {
403             msg_Warn( p_intf,
404                       "failed to convert \"%s\" to desired charset (%s)",
405                       psz_local, strerror(errno) );
406             free( psz_utf8 );
407             return strdup( psz_local );
408         }
409
410         *psz_out = '\0';
411         return psz_utf8;
412     }
413     else
414         return strdup( psz_local );
415 }
416
417 /*************************************************************************
418  * Playlist stuff
419  *************************************************************************/
420 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
421                            playlist_item_t *p_node, char *name, mvar_t *s,
422                            int i_depth )
423 {
424     if( p_node != NULL )
425     {
426         if( p_node->i_children == -1 )
427         {
428             char value[512];
429             char *psz;
430             mvar_t *itm = E_(mvar_New)( name, "set" );
431
432             sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
433             E_(mvar_AppendNewVar)( itm, "current", value );
434
435             sprintf( value, "%d", p_node->input.i_id );
436             E_(mvar_AppendNewVar)( itm, "index", value );
437
438             psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
439             E_(mvar_AppendNewVar)( itm, "name", psz );
440             free( psz );
441
442             psz = E_(FromUTF8)( p_intf, p_node->input.psz_uri );
443             E_(mvar_AppendNewVar)( itm, "uri", psz );
444             free( psz );
445
446             sprintf( value, "Item");
447             E_(mvar_AppendNewVar)( itm, "type", value );
448
449             sprintf( value, "%d", i_depth );
450             E_(mvar_AppendNewVar)( itm, "depth", value );
451
452             if( p_node->i_flags & PLAYLIST_RO_FLAG )
453             {
454                 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
455             }
456             else
457             {
458                 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
459             }
460
461             sprintf( value, "%d", p_node->input.i_duration );
462             E_(mvar_AppendNewVar)( itm, "duration", value );
463
464             E_(mvar_AppendVar)( s, itm );
465         }
466         else
467         {
468             char value[512];
469             char *psz;
470             int i_child;
471             mvar_t *itm = E_(mvar_New)( name, "set" );
472
473             psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
474             E_(mvar_AppendNewVar)( itm, "name", psz );
475             E_(mvar_AppendNewVar)( itm, "uri", psz );
476             free( psz );
477
478             sprintf( value, "Node" );
479             E_(mvar_AppendNewVar)( itm, "type", value );
480
481             sprintf( value, "%d", p_node->input.i_id );
482             E_(mvar_AppendNewVar)( itm, "index", value );
483
484             sprintf( value, "%d", p_node->i_children);
485             E_(mvar_AppendNewVar)( itm, "i_children", value );
486
487             sprintf( value, "%d", i_depth );
488             E_(mvar_AppendNewVar)( itm, "depth", value );
489
490             if( p_node->i_flags & PLAYLIST_RO_FLAG )
491             {
492                 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
493             }
494             else
495             {
496                 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
497             }
498
499             E_(mvar_AppendVar)( s, itm );
500
501             for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
502                 E_(PlaylistListNode)( p_intf, p_pl,
503                                       p_node->pp_children[i_child],
504                                       name, s, i_depth + 1);
505
506         }
507     }
508 }
509
510 /****************************************************************************
511  * Seek command parsing handling
512  ****************************************************************************/
513 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
514 {
515     intf_sys_t     *p_sys = p_intf->p_sys;
516     vlc_value_t val;
517     int i_stock = 0;
518     uint64_t i_length;
519     int i_value = 0;
520     int i_relative = 0;
521 #define POSITION_ABSOLUTE 12
522 #define POSITION_REL_FOR 13
523 #define POSITION_REL_BACK 11
524 #define VL_TIME_ABSOLUTE 0
525 #define VL_TIME_REL_FOR 1
526 #define VL_TIME_REL_BACK -1
527     if( p_sys->p_input )
528     {
529         var_Get( p_sys->p_input, "length", &val );
530         i_length = val.i_time;
531
532         while( p_value[0] != '\0' )
533         {
534             switch(p_value[0])
535             {
536                 case '+':
537                 {
538                     i_relative = VL_TIME_REL_FOR;
539                     p_value++;
540                     break;
541                 }
542                 case '-':
543                 {
544                     i_relative = VL_TIME_REL_BACK;
545                     p_value++;
546                     break;
547                 }
548                 case '0': case '1': case '2': case '3': case '4':
549                 case '5': case '6': case '7': case '8': case '9':
550                 {
551                     i_stock = strtol( p_value , &p_value , 10 );
552                     break;
553                 }
554                 case '%': /* for percentage ie position */
555                 {
556                     i_relative += POSITION_ABSOLUTE;
557                     i_value = i_stock;
558                     i_stock = 0;
559                     p_value[0] = '\0';
560                     break;
561                 }
562                 case ':':
563                 {
564                     i_value = 60 * (i_value + i_stock) ;
565                     i_stock = 0;
566                     p_value++;
567                     break;
568                 }
569                 case 'h': case 'H': /* hours */
570                 {
571                     i_value += 3600 * i_stock;
572                     i_stock = 0;
573                     /* other characters which are not numbers are not important */
574                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
575                     {
576                         p_value++;
577                     }
578                     break;
579                 }
580                 case 'm': case 'M': case '\'': /* minutes */
581                 {
582                     i_value += 60 * i_stock;
583                     i_stock = 0;
584                     p_value++;
585                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
586                     {
587                         p_value++;
588                     }
589                     break;
590                 }
591                 case 's': case 'S': case '"':  /* seconds */
592                 {
593                     i_value += i_stock;
594                     i_stock = 0;
595                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
596                     {
597                         p_value++;
598                     }
599                     break;
600                 }
601                 default:
602                 {
603                     p_value++;
604                     break;
605                 }
606             }
607         }
608
609         /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
610         i_value += i_stock;
611
612         switch(i_relative)
613         {
614             case VL_TIME_ABSOLUTE:
615             {
616                 if( (uint64_t)( i_value ) * 1000000 <= i_length )
617                     val.i_time = (uint64_t)( i_value ) * 1000000;
618                 else
619                     val.i_time = i_length;
620
621                 var_Set( p_sys->p_input, "time", val );
622                 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
623                 break;
624             }
625             case VL_TIME_REL_FOR:
626             {
627                 var_Get( p_sys->p_input, "time", &val );
628                 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
629                 {
630                     val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
631                 } else
632                 {
633                     val.i_time = i_length;
634                 }
635                 var_Set( p_sys->p_input, "time", val );
636                 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
637                 break;
638             }
639             case VL_TIME_REL_BACK:
640             {
641                 var_Get( p_sys->p_input, "time", &val );
642                 if( (int64_t)( i_value ) * 1000000 > val.i_time )
643                 {
644                     val.i_time = 0;
645                 } else
646                 {
647                     val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
648                 }
649                 var_Set( p_sys->p_input, "time", val );
650                 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
651                 break;
652             }
653             case POSITION_ABSOLUTE:
654             {
655                 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
656                 var_Set( p_sys->p_input, "position", val );
657                 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
658                 break;
659             }
660             case POSITION_REL_FOR:
661             {
662                 var_Get( p_sys->p_input, "position", &val );
663                 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
664                 var_Set( p_sys->p_input, "position", val );
665                 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
666                 break;
667             }
668             case POSITION_REL_BACK:
669             {
670                 var_Get( p_sys->p_input, "position", &val );
671                 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
672                 var_Set( p_sys->p_input, "position", val );
673                 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
674                 break;
675             }
676             default:
677             {
678                 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
679                 break;
680             }
681         }
682     }
683 #undef POSITION_ABSOLUTE
684 #undef POSITION_REL_FOR
685 #undef POSITION_REL_BACK
686 #undef VL_TIME_ABSOLUTE
687 #undef VL_TIME_REL_FOR
688 #undef VL_TIME_REL_BACK
689 }
690
691
692 /****************************************************************************
693  * URI Parsing functions
694  ****************************************************************************/
695 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
696 {
697     char *p = psz_uri;
698
699     while( (p = strstr( p, psz_name )) )
700     {
701         /* Verify that we are dealing with a post/get argument */
702         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
703               && p[strlen(psz_name)] == '=' )
704         {
705             return VLC_TRUE;
706         }
707         p++;
708     }
709
710     return VLC_FALSE;
711 }
712 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
713                              char *psz_value, int i_value_max )
714 {
715     char *p = psz_uri;
716
717     while( (p = strstr( p, psz_name )) )
718     {
719         /* Verify that we are dealing with a post/get argument */
720         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
721               && p[strlen(psz_name)] == '=' )
722             break;
723         p++;
724     }
725
726     if( p )
727     {
728         int i_len;
729
730         p += strlen( psz_name );
731         if( *p == '=' ) p++;
732
733         if( strchr( p, '&' ) )
734         {
735             i_len = strchr( p, '&' ) - p;
736         }
737         else
738         {
739             /* for POST method */
740             if( strchr( p, '\n' ) )
741             {
742                 i_len = strchr( p, '\n' ) - p;
743                 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
744             }
745             else
746             {
747                 i_len = strlen( p );
748             }
749         }
750         i_len = __MIN( i_value_max - 1, i_len );
751         if( i_len > 0 )
752         {
753             strncpy( psz_value, p, i_len );
754             psz_value[i_len] = '\0';
755         }
756         else
757         {
758             strncpy( psz_value, "", i_value_max );
759         }
760         p += i_len;
761     }
762     else
763     {
764         strncpy( psz_value, "", i_value_max );
765     }
766
767     return p;
768 }
769
770 void E_(DecodeEncodedURI)( char *psz )
771 {
772     decode_encoded_URI( psz );
773 }
774
775 /* Since the resulting string is smaller we can work in place, so it is
776  * permitted to have psz == new. new points to the first word of the
777  * string, the function returns the remaining string. */
778 char *E_(FirstWord)( char *psz, char *new )
779 {
780     vlc_bool_t b_end;
781
782     while( *psz == ' ' )
783         psz++;
784
785     while( *psz != '\0' && *psz != ' ' )
786     {
787         if( *psz == '\'' )
788         {
789             char c = *psz++;
790             while( *psz != '\0' && *psz != c )
791             {
792                 if( *psz == '\\' && psz[1] != '\0' )
793                     psz++;
794                 *new++ = *psz++;
795             }
796             if( *psz == c )
797                 psz++;
798         }
799         else
800         {
801             if( *psz == '\\' && psz[1] != '\0' )
802                 psz++;
803             *new++ = *psz++;
804         }
805     }
806     b_end = !*psz;
807
808     *new++ = '\0';
809     if( !b_end )
810         return psz + 1;
811     else
812         return NULL;
813 }
814 /**********************************************************************
815  * Find_end_MRL: Find the end of the sentence :
816  * this function parses the string psz and find the end of the item
817  * and/or option with detecting the " and ' problems.
818  * returns NULL if an error is detected, otherwise, returns a pointer
819  * of the end of the sentence (after the last character)
820 **********************************************************************/
821 static char *Find_end_MRL( char *psz )
822 {
823     char *s_sent = psz;
824
825     switch( *s_sent )
826     {
827         case '\"':
828         {
829             s_sent++;
830
831             while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
832             {
833                 if( *s_sent == '\'' )
834                 {
835                     s_sent = Find_end_MRL( s_sent );
836
837                     if( s_sent == NULL )
838                     {
839                         return NULL;
840                     }
841                 } else
842                 {
843                     s_sent++;
844                 }
845             }
846
847             if( *s_sent == '\"' )
848             {
849                 s_sent++;
850                 return s_sent;
851             } else  /* *s_sent == '\0' , which means the number of " is incorrect */
852             {
853                 return NULL;
854             }
855             break;
856         }
857         case '\'':
858         {
859             s_sent++;
860
861             while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
862             {
863                 if( *s_sent == '\"' )
864                 {
865                     s_sent = Find_end_MRL( s_sent );
866
867                     if( s_sent == NULL )
868                     {
869                         return NULL;
870                     }
871                 } else
872                 {
873                     s_sent++;
874                 }
875             }
876
877             if( *s_sent == '\'' )
878             {
879                 s_sent++;
880                 return s_sent;
881             } else  /* *s_sent == '\0' , which means the number of ' is incorrect */
882             {
883                 return NULL;
884             }
885             break;
886         }
887         default: /* now we can look for spaces */
888         {
889             while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
890             {
891                 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
892                 {
893                     s_sent = Find_end_MRL( s_sent );
894                 } else
895                 {
896                     s_sent++;
897                 }
898             }
899             return s_sent;
900         }
901     }
902 }
903 /**********************************************************************
904  * parse_MRL: parse the MRL, find the mrl string and the options,
905  * create an item with all information in it, and return the item.
906  * return NULL if there is an error.
907  **********************************************************************/
908 playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *psz,
909                                    char *psz_name )
910 {
911     char **ppsz_options = NULL;
912     char *mrl;
913     char *s_mrl = psz;
914     int i_error = 0;
915     char *s_temp;
916     int i = 0;
917     int i_options = 0;
918     playlist_item_t * p_item = NULL;
919
920     /* In case there is spaces before the mrl */
921     while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
922     {
923         s_mrl++;
924     }
925
926     /* extract the mrl */
927     s_temp = strstr( s_mrl , " :" );
928     if( s_temp == NULL )
929     {
930         s_temp = s_mrl + strlen( s_mrl );
931     } else
932     {
933         while( (*s_temp == ' ') && (s_temp != s_mrl ) )
934         {
935             s_temp--;
936         }
937         s_temp++;
938     }
939
940     /* if the mrl is between " or ', we must remove them */
941     if( (*s_mrl == '\'') || (*s_mrl == '\"') )
942     {
943         mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
944         strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
945         mrl[ s_temp - s_mrl - 2 ] = '\0';
946     } else
947     {
948         mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
949         strncpy( mrl , s_mrl , s_temp - s_mrl );
950         mrl[ s_temp - s_mrl ] = '\0';
951     }
952
953     s_mrl = s_temp;
954
955     /* now we can take care of the options */
956     while( (*s_mrl != '\0') && (i_error == 0) )
957     {
958         switch( *s_mrl )
959         {
960             case ' ':
961             {
962                 s_mrl++;
963                 break;
964             }
965             case ':': /* an option */
966             {
967                 s_temp = Find_end_MRL( s_mrl );
968
969                 if( s_temp == NULL )
970                 {
971                     i_error = 1;
972                 }
973                 else
974                 {
975                     i_options++;
976                     ppsz_options = realloc( ppsz_options , i_options *
977                                             sizeof(char *) );
978                     ppsz_options[ i_options - 1 ] =
979                         malloc( (s_temp - s_mrl + 1) * sizeof(char) );
980
981                     strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
982                              s_temp - s_mrl );
983
984                     /* don't forget to finish the string with a '\0' */
985                     (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
986
987                     s_mrl = s_temp;
988                 }
989                 break;
990             }
991             default:
992             {
993                 i_error = 1;
994                 break;
995             }
996         }
997     }
998
999     if( i_error != 0 )
1000     {
1001         free( mrl );
1002     }
1003     else
1004     {
1005         /* now create an item */
1006         p_item = playlist_ItemNew( p_intf, mrl, psz_name );
1007         for( i = 0 ; i< i_options ; i++ )
1008         {
1009             playlist_ItemAddOption( p_item, ppsz_options[i] );
1010         }
1011     }
1012
1013     for( i = 0; i < i_options; i++ ) free( ppsz_options[i] );
1014     if( i_options ) free( ppsz_options );
1015
1016     return p_item;
1017 }
1018 /**********************************************************************
1019  * RealPath: parse ../, ~ and path stuff
1020  **********************************************************************/
1021 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
1022 {
1023     char *psz_dir;
1024     char *p;
1025     int i_len = strlen(psz_src);
1026     char sep;
1027
1028 #if defined( WIN32 )
1029     sep = '\\';
1030 #else
1031     sep = '/';
1032 #endif
1033
1034     psz_dir = malloc( i_len + 2 );
1035     strcpy( psz_dir, psz_src );
1036
1037     /* Add a trailing sep to ease the .. step */
1038     psz_dir[i_len] = sep;
1039     psz_dir[i_len + 1] = '\0';
1040
1041 #ifdef WIN32
1042     /* Convert all / to native separator */
1043     p = psz_dir;
1044     while( (p = strchr( p, '/' )) != NULL )
1045     {
1046         *p = sep;
1047     }
1048 #endif
1049
1050     /* Remove multiple separators and /./ */
1051     p = psz_dir;
1052     while( (p = strchr( p, sep )) != NULL )
1053     {
1054         if( p[1] == sep )
1055             memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
1056         else if( p[1] == '.' && p[2] == sep )
1057             memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
1058         else
1059             p++;
1060     }
1061
1062     if( psz_dir[0] == '~' )
1063     {
1064         char *dir = malloc( strlen(psz_dir)
1065                              + strlen(p_intf->p_vlc->psz_userdir) );
1066         /* This is incomplete : we should also support the ~cmassiot/ syntax. */
1067         sprintf( dir, "%s%s", p_intf->p_vlc->psz_userdir, psz_dir + 1 );
1068         free( psz_dir );
1069         psz_dir = dir;
1070     }
1071
1072     if( strlen(psz_dir) > 2 )
1073     {
1074         /* Fix all .. dir */
1075         p = psz_dir + 3;
1076         while( (p = strchr( p, sep )) != NULL )
1077         {
1078             if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
1079             {
1080                 char *q;
1081                 p[-3] = '\0';
1082                 if( (q = strrchr( psz_dir, sep )) != NULL )
1083                 {
1084                     memmove( q + 1, p + 1, strlen(p + 1) + 1 );
1085                     p = q + 1;
1086                 }
1087                 else
1088                 {
1089                     memmove( psz_dir, p, strlen(p) + 1 );
1090                     p = psz_dir + 3;
1091                 }
1092             }
1093             else
1094                 p++;
1095         }
1096     }
1097
1098     /* Remove trailing sep if there are at least 2 sep in the string
1099      * (handles the C:\ stuff) */
1100     p = strrchr( psz_dir, sep );
1101     if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )
1102         *p = '\0';
1103
1104     return psz_dir;
1105 }