]> git.sesse.net Git - vlc/blob - modules/control/http/util.c
Fix url converter on win32
[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: http.c 12225 2005-08-18 10:01:30Z massiot $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 #include "http.h"
27
28 /****************************************************************************
29  * File and directory functions
30  ****************************************************************************/
31
32 /* ToUrl: create a good name for an url from filename */
33 char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index )
34 {
35     char *url, *p;
36
37     url = p = malloc( strlen( name ) + 1 );
38
39     *pb_index = VLC_FALSE;
40     if( !url || !p )
41     {
42         return NULL;
43     }
44
45 #ifdef WIN32
46     while( *name == '\\' || *name == '/' )
47 #else
48     while( *name == '/' )
49 #endif
50     {
51         name++;
52     }
53
54     *p++ = '/';
55     strcpy( p, name );
56
57 #ifdef WIN32
58     /* convert '\\' into '/' */
59     name = p;
60     while( *name )
61     {
62         if( *name == '\\' )
63             *name = '/';
64         name++;
65     }
66 #endif
67
68     /* index.* -> / */
69     if( ( p = strrchr( url, '/' ) ) != NULL )
70     {
71         if( !strncmp( p, "/index.", 7 ) )
72         {
73             p[1] = '\0';
74             *pb_index = VLC_TRUE;
75         }
76     }
77     return url;
78 }
79
80 /* Load a file */
81 int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data )
82 {
83     int i_read;
84
85     /* just load the file */
86     *pi_data = 0;
87     *pp_data = malloc( 1025 );  /* +1 for \0 */
88     while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
89     {
90         *pi_data += 1024;
91         *pp_data = realloc( *pp_data, *pi_data  + 1025 );
92     }
93     if( i_read > 0 )
94     {
95         *pi_data += i_read;
96     }
97     (*pp_data)[*pi_data] = '\0';
98
99     return VLC_SUCCESS;
100 }
101
102 /* Parse a directory and recursively add files */
103 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
104                            char *psz_dir )
105 {
106     intf_sys_t     *p_sys = p_intf->p_sys;
107     char           dir[MAX_DIR_SIZE];
108 #ifdef HAVE_SYS_STAT_H
109     struct stat   stat_info;
110 #endif
111     DIR           *p_dir;
112     struct dirent *p_dir_content;
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( 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 = 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 = 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         /* parse psz_src dir */
196         if( ( p_dir_content = readdir( p_dir ) ) == NULL )
197         {
198             break;
199         }
200
201         if( ( p_dir_content->d_name[0] == '.' )
202          || ( i_dirlen + strlen( p_dir_content->d_name ) > MAX_DIR_SIZE ) )
203             continue;
204
205         sprintf( dir, "%s%c%s", psz_dir, sep, p_dir_content->d_name );
206         if( E_(ParseDirectory)( p_intf, psz_root, dir ) )
207         {
208             httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
209             vlc_bool_t b_index;
210             char *psz_tmp;
211
212             f->p_intf  = p_intf;
213             f->p_file = NULL;
214             f->p_redir = NULL;
215             f->p_redir2 = NULL;
216             psz_tmp = vlc_fix_readdir_charset( p_intf, dir );
217             f->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             f->name = E_(FileToUrl)( psz_tmp, &b_index );
222             free( psz_tmp );
223             f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
224
225             if( !f->name )
226             {
227                 msg_Err( p_intf , "unable to parse directory" );
228                 closedir( p_dir );
229                 free( f );
230                 return( VLC_ENOMEM );
231             }
232             msg_Dbg( p_intf, "file=%s (url=%s)",
233                      f->file, f->name );
234
235             f->p_file = httpd_FileNew( p_sys->p_httpd_host,
236                                        f->name,
237                                        f->b_html ? p_sys->psz_html_type : NULL,
238                                        user, password, p_acl,
239                                        E_(HttpCallback), f );
240
241             if( f->p_file )
242             {
243                 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
244             }
245             /* for url that ends by / add
246              *  - a redirect from rep to rep/
247              *  - in case of index.* rep/index.html to rep/ */
248             if( f && f->name[strlen(f->name) - 1] == '/' )
249             {
250                 char *psz_redir = strdup( f->name );
251                 char *p;
252                 psz_redir[strlen( psz_redir ) - 1] = '\0';
253
254                 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
255                 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
256                 free( psz_redir );
257
258                 if( b_index && ( p = strstr( f->file, "index." ) ) )
259                 {
260                     asprintf( &psz_redir, "%s%s", f->name, p );
261
262                     msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
263                     f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
264                                                      f->name, psz_redir );
265
266                     free( psz_redir );
267                 }
268             }
269         }
270     }
271
272     if( user )
273     {
274         free( user );
275     }
276     if( password )
277     {
278         free( password );
279     }
280
281     ACL_Destroy( p_acl );
282     closedir( p_dir );
283
284     return VLC_SUCCESS;
285 }
286
287
288 /**************************************************************************
289  * Locale functions
290  **************************************************************************/
291 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
292 {
293     intf_sys_t    *p_sys = p_intf->p_sys;
294
295     if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
296     {
297         char *psz_in = psz_utf8;
298         size_t i_in = strlen(psz_in);
299         size_t i_out = i_in * 2;
300         char *psz_local = malloc(i_out + 1);
301         char *psz_out = psz_local;
302
303         size_t i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
304                                   &psz_out, &i_out );
305         if( i_ret == (size_t)-1 || i_in )
306         {
307             msg_Warn( p_intf,
308                       "failed to convert \"%s\" to desired charset (%s)",
309                       psz_utf8, strerror(errno) );
310             free( psz_local );
311             return strdup( psz_utf8 );
312         }
313
314         *psz_out = '\0';
315         return psz_local;
316     }
317     else
318         return strdup( psz_utf8 );
319 }
320
321 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
322 {
323     intf_sys_t    *p_sys = p_intf->p_sys;
324
325     if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
326     {
327         char *psz_in = psz_local;
328         size_t i_in = strlen(psz_in);
329         size_t i_out = i_in * 6;
330         char *psz_utf8 = malloc(i_out + 1);
331         char *psz_out = psz_utf8;
332
333         size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
334                                   &psz_out, &i_out );
335         if( i_ret == (size_t)-1 || i_in )
336         {
337             msg_Warn( p_intf,
338                       "failed to convert \"%s\" to desired charset (%s)",
339                       psz_local, strerror(errno) );
340             free( psz_utf8 );
341             return strdup( psz_local );
342         }
343
344         *psz_out = '\0';
345         return psz_utf8;
346     }
347     else
348         return strdup( psz_local );
349 }
350
351 /*************************************************************************
352  * Playlist stuff
353  *************************************************************************/
354 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
355                            playlist_item_t *p_node, char *name, mvar_t *s,
356                            int i_depth )
357 {
358     if( p_node != NULL )
359     {
360         if( p_node->i_children == -1 )
361         {
362             char value[512];
363             char *psz;
364             mvar_t *itm = mvar_New( name, "set" );
365
366             sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
367             mvar_AppendNewVar( itm, "current", value );
368
369             sprintf( value, "%d", p_node->input.i_id );
370             mvar_AppendNewVar( itm, "index", value );
371
372             psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
373             mvar_AppendNewVar( itm, "name", psz );
374             free( psz );
375
376             psz = E_(FromUTF8)( p_intf, p_node->input.psz_uri );
377             mvar_AppendNewVar( itm, "uri", psz );
378             free( psz );
379
380             sprintf( value, "Item");
381             mvar_AppendNewVar( itm, "type", value );
382
383             sprintf( value, "%d", i_depth );
384             mvar_AppendNewVar( itm, "depth", value );
385
386             mvar_AppendVar( s, itm );
387         }
388         else
389         {
390             char value[512];
391             char *psz;
392             int i_child;
393             mvar_t *itm = mvar_New( name, "set" );
394
395             psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
396             mvar_AppendNewVar( itm, "name", psz );
397             mvar_AppendNewVar( itm, "uri", psz );
398             free( psz );
399
400             sprintf( value, "Node" );
401             mvar_AppendNewVar( itm, "type", value );
402
403             sprintf( value, "%d", p_node->input.i_id );
404             mvar_AppendNewVar( itm, "index", value );
405
406             sprintf( value, "%d", p_node->i_children);
407             mvar_AppendNewVar( itm, "i_children", value );
408
409             sprintf( value, "%d", i_depth );
410             mvar_AppendNewVar( itm, "depth", value );
411
412             mvar_AppendVar( s, itm );
413
414             for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
415                 E_(PlaylistListNode)( p_intf, p_pl,
416                                       p_node->pp_children[i_child],
417                                       name, s, i_depth + 1);
418
419         }
420     }
421 }
422
423 /****************************************************************************
424  * Seek command parsing handling
425  ****************************************************************************/
426 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
427 {
428     intf_sys_t     *p_sys = p_intf->p_sys;
429     vlc_value_t val;
430     int i_stock = 0;
431     uint64_t i_length;
432     int i_value = 0;
433     int i_relative = 0;
434 #define POSITION_ABSOLUTE 12
435 #define POSITION_REL_FOR 13
436 #define POSITION_REL_BACK 11
437 #define VL_TIME_ABSOLUTE 0
438 #define VL_TIME_REL_FOR 1
439 #define VL_TIME_REL_BACK -1
440     if( p_sys->p_input )
441     {
442         var_Get( p_sys->p_input, "length", &val );
443         i_length = val.i_time;
444
445         while( p_value[0] != '\0' )
446         {
447             switch(p_value[0])
448             {
449                 case '+':
450                 {
451                     i_relative = VL_TIME_REL_FOR;
452                     p_value++;
453                     break;
454                 }
455                 case '-':
456                 {
457                     i_relative = VL_TIME_REL_BACK;
458                     p_value++;
459                     break;
460                 }
461                 case '0': case '1': case '2': case '3': case '4':
462                 case '5': case '6': case '7': case '8': case '9':
463                 {
464                     i_stock = strtol( p_value , &p_value , 10 );
465                     break;
466                 }
467                 case '%': /* for percentage ie position */
468                 {
469                     i_relative += POSITION_ABSOLUTE;
470                     i_value = i_stock;
471                     i_stock = 0;
472                     p_value[0] = '\0';
473                     break;
474                 }
475                 case ':':
476                 {
477                     i_value = 60 * (i_value + i_stock) ;
478                     i_stock = 0;
479                     p_value++;
480                     break;
481                 }
482                 case 'h': case 'H': /* hours */
483                 {
484                     i_value += 3600 * i_stock;
485                     i_stock = 0;
486                     /* other characters which are not numbers are not important */
487                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
488                     {
489                         p_value++;
490                     }
491                     break;
492                 }
493                 case 'm': case 'M': case '\'': /* minutes */
494                 {
495                     i_value += 60 * i_stock;
496                     i_stock = 0;
497                     p_value++;
498                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
499                     {
500                         p_value++;
501                     }
502                     break;
503                 }
504                 case 's': case 'S': case '"':  /* seconds */
505                 {
506                     i_value += i_stock;
507                     i_stock = 0;
508                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
509                     {
510                         p_value++;
511                     }
512                     break;
513                 }
514                 default:
515                 {
516                     p_value++;
517                     break;
518                 }
519             }
520         }
521
522         /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
523         i_value += i_stock;
524
525         switch(i_relative)
526         {
527             case VL_TIME_ABSOLUTE:
528             {
529                 if( (uint64_t)( i_value ) * 1000000 <= i_length )
530                     val.i_time = (uint64_t)( i_value ) * 1000000;
531                 else
532                     val.i_time = i_length;
533
534                 var_Set( p_sys->p_input, "time", val );
535                 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
536                 break;
537             }
538             case VL_TIME_REL_FOR:
539             {
540                 var_Get( p_sys->p_input, "time", &val );
541                 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
542                 {
543                     val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
544                 } else
545                 {
546                     val.i_time = i_length;
547                 }
548                 var_Set( p_sys->p_input, "time", val );
549                 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
550                 break;
551             }
552             case VL_TIME_REL_BACK:
553             {
554                 var_Get( p_sys->p_input, "time", &val );
555                 if( (int64_t)( i_value ) * 1000000 > val.i_time )
556                 {
557                     val.i_time = 0;
558                 } else
559                 {
560                     val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
561                 }
562                 var_Set( p_sys->p_input, "time", val );
563                 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
564                 break;
565             }
566             case POSITION_ABSOLUTE:
567             {
568                 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
569                 var_Set( p_sys->p_input, "position", val );
570                 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
571                 break;
572             }
573             case POSITION_REL_FOR:
574             {
575                 var_Get( p_sys->p_input, "position", &val );
576                 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
577                 var_Set( p_sys->p_input, "position", val );
578                 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
579                 break;
580             }
581             case POSITION_REL_BACK:
582             {
583                 var_Get( p_sys->p_input, "position", &val );
584                 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
585                 var_Set( p_sys->p_input, "position", val );
586                 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
587                 break;
588             }
589             default:
590             {
591                 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
592                 break;
593             }
594         }
595     }
596 #undef POSITION_ABSOLUTE
597 #undef POSITION_REL_FOR
598 #undef POSITION_REL_BACK
599 #undef VL_TIME_ABSOLUTE
600 #undef VL_TIME_REL_FOR
601 #undef VL_TIME_REL_BACK
602 }
603
604
605 /****************************************************************************
606  * URI Parsing functions
607  ****************************************************************************/
608 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
609 {
610     char *p = psz_uri;
611
612     while( (p = strstr( p, psz_name )) )
613     {
614         /* Verify that we are dealing with a post/get argument */
615         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
616               && p[strlen(psz_name)] == '=' )
617         {
618             return VLC_TRUE;
619         }
620         p++;
621     }
622
623     return VLC_FALSE;
624 }
625 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
626                              char *psz_value, int i_value_max )
627 {
628     char *p = psz_uri;
629
630     while( (p = strstr( p, psz_name )) )
631     {
632         /* Verify that we are dealing with a post/get argument */
633         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
634               && p[strlen(psz_name)] == '=' )
635             break;
636         p++;
637     }
638
639     if( p )
640     {
641         int i_len;
642
643         p += strlen( psz_name );
644         if( *p == '=' ) p++;
645
646         if( strchr( p, '&' ) )
647         {
648             i_len = strchr( p, '&' ) - p;
649         }
650         else
651         {
652             /* for POST method */
653             if( strchr( p, '\n' ) )
654             {
655                 i_len = strchr( p, '\n' ) - p;
656                 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
657             }
658             else
659             {
660                 i_len = strlen( p );
661             }
662         }
663         i_len = __MIN( i_value_max - 1, i_len );
664         if( i_len > 0 )
665         {
666             strncpy( psz_value, p, i_len );
667             psz_value[i_len] = '\0';
668         }
669         else
670         {
671             strncpy( psz_value, "", i_value_max );
672         }
673         p += i_len;
674     }
675     else
676     {
677         strncpy( psz_value, "", i_value_max );
678     }
679
680     return p;
681 }
682
683 void E_(DecodeEncodedURI)( char *psz )
684 {
685     char *dup = strdup( psz );
686     char *p = dup;
687
688     while( *p )
689     {
690         if( *p == '%' )
691         {
692             char val[3];
693             p++;
694             if( !*p )
695             {
696                 break;
697             }
698
699             val[0] = *p++;
700             val[1] = *p++;
701             val[2] = '\0';
702
703             *psz++ = strtol( val, NULL, 16 );
704         }
705         else if( *p == '+' )
706         {
707             *psz++ = ' ';
708             p++;
709         }
710         else
711         {
712             *psz++ = *p++;
713         }
714     }
715     *psz++ = '\0';
716     free( dup );
717 }
718
719 /* Since the resulting string is smaller we can work in place, so it is
720  * permitted to have psz == new. new points to the first word of the
721  * string, the function returns the remaining string. */
722 char *E_(FirstWord)( char *psz, char *new )
723 {
724     vlc_bool_t b_end;
725
726     while( *psz == ' ' )
727         psz++;
728
729     while( *psz != '\0' && *psz != ' ' )
730     {
731         if( *psz == '\'' )
732         {
733             char c = *psz++;
734             while( *psz != '\0' && *psz != c )
735             {
736                 if( *psz == '\\' && psz[1] != '\0' )
737                     psz++;
738                 *new++ = *psz++;
739             }
740             if( *psz == c )
741                 psz++;
742         }
743         else
744         {
745             if( *psz == '\\' && psz[1] != '\0' )
746                 psz++;
747             *new++ = *psz++;
748         }
749     }
750     b_end = !*psz;
751
752     *new++ = '\0';
753     if( !b_end )
754         return psz + 1;
755     else
756         return NULL;
757 }
758
759 /**********************************************************************
760  * parse_MRL: parse the MRL, find the mrl string and the options,
761  * create an item with all information in it, and return the item.
762  * return NULL if there is an error.
763  **********************************************************************/
764 playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
765                                    char *psz_name )
766 {
767     char *psz = strdup( _psz );
768     char *s_mrl = psz;
769     char *s_temp;
770     playlist_item_t * p_item = NULL;
771
772     /* extract the mrl */
773     s_temp = E_(FirstWord)( s_mrl, s_mrl );
774     if( s_temp == NULL )
775     {
776         s_temp = s_mrl + strlen( s_mrl );
777     }
778
779     p_item = playlist_ItemNew( p_intf, s_mrl, psz_name );
780     s_mrl = s_temp;
781
782     /* now we can take care of the options */
783     while( *s_mrl != '\0' )
784     {
785         s_temp = E_(FirstWord)( s_mrl, s_mrl );
786         if( s_mrl == '\0' )
787             break;
788         if( s_temp == NULL )
789         {
790             s_temp = s_mrl + strlen( s_mrl );
791         }
792         if( *s_mrl != ':' )
793             msg_Warn( p_intf, "invalid MRL option: %s", s_mrl );
794         else
795             playlist_ItemAddOption( p_item, s_mrl );
796         s_mrl = s_temp;
797     }
798
799     free( psz );
800     return p_item;
801 }
802
803 /**********************************************************************
804  * RealPath: parse ../, ~ and path stuff
805  **********************************************************************/
806 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
807 {
808     char *psz_dir;
809     char *p;
810     int i_len = strlen(psz_src);
811     char sep;
812
813 #if defined( WIN32 )
814     sep = '\\';
815 #else
816     sep = '/';
817 #endif
818
819     psz_dir = malloc( i_len + 2 );
820     strcpy( psz_dir, psz_src );
821
822     /* Add a trailing sep to ease the .. step */
823     psz_dir[i_len] = sep;
824     psz_dir[i_len + 1] = '\0';
825
826 #ifdef WIN32
827     /* Convert all / to native separator */
828     p = psz_dir;
829     while( (p = strchr( p, '/' )) != NULL )
830     {
831         *p = sep;
832     }
833 #endif
834
835     /* Remove multiple separators and /./ */
836     p = psz_dir;
837     while( (p = strchr( p, sep )) != NULL )
838     {
839         if( p[1] == sep )
840             memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
841         else if( p[1] == '.' && p[2] == sep )
842             memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
843         else
844             p++;
845     }
846
847     if( psz_dir[0] == '~' )
848     {
849         char *dir = malloc( strlen(psz_dir)
850                              + strlen(p_intf->p_vlc->psz_homedir) );
851         /* This is incomplete : we should also support the ~cmassiot/ syntax. */
852         sprintf( dir, "%s%s", p_intf->p_vlc->psz_homedir, psz_dir + 1 );
853         free( psz_dir );
854         psz_dir = dir;
855     }
856
857     if( strlen(psz_dir) > 2 )
858     {
859         /* Fix all .. dir */
860         p = psz_dir + 3;
861         while( (p = strchr( p, sep )) != NULL )
862         {
863             if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
864             {
865                 char *q;
866                 p[-3] = '\0';
867                 if( (q = strrchr( psz_dir, sep )) != NULL )
868                 {
869                     memmove( q + 1, p + 1, strlen(p + 1) + 1 );
870                     p = q + 1;
871                 }
872                 else
873                 {
874                     memmove( psz_dir, p + 1, strlen(p + 1) + 1 );
875                     p = psz_dir + 3;
876                 }
877             }
878             else
879                 p++;
880         }
881     }
882
883     /* Remove trailing sep if there are at least 2 sep in the string
884      * (handles the C:\ stuff) */
885     p = strrchr( psz_dir, sep );
886     if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )
887         *p = '\0';
888
889     return psz_dir;
890 }