]> git.sesse.net Git - vlc/blob - modules/control/http/util.c
845694e313c481e2551bb29b3d0afd8fb78187b2
[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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include "http.h"
32 #include "vlc_strings.h"
33
34 /****************************************************************************
35  * File and directory functions
36  ****************************************************************************/
37
38 /* ToUrl: create a good name for an url from filename */
39 char *FileToUrl( char *name, bool *pb_index )
40 {
41     char *url, *p;
42
43     url = p = malloc( strlen( name ) + 1 );
44
45     *pb_index = false;
46     if( !url || !p )
47     {
48         return NULL;
49     }
50
51 #ifdef WIN32
52     while( *name == '\\' || *name == '/' )
53 #else
54     while( *name == '/' )
55 #endif
56     {
57         name++;
58     }
59
60     *p++ = '/';
61     strcpy( p, name );
62
63 #ifdef WIN32
64     /* convert '\\' into '/' */
65     name = p;
66     while( *name )
67     {
68         if( *name == '\\' )
69             *name = '/';
70         name++;
71     }
72 #endif
73
74     /* index.* -> / */
75     if( ( p = strrchr( url, '/' ) ) != NULL )
76     {
77         if( !strncmp( p, "/index.", 7 ) )
78         {
79             p[1] = '\0';
80             *pb_index = true;
81         }
82     }
83     return url;
84 }
85
86 /* Load a file */
87 int FileLoad( FILE *f, char **pp_data, int *pi_data )
88 {
89     int i_read;
90
91     /* just load the file */
92     *pi_data = 0;
93     *pp_data = malloc( 1025 );  /* +1 for \0 */
94     while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
95     {
96         *pi_data += 1024;
97         *pp_data = realloc( *pp_data, *pi_data  + 1025 );
98     }
99     if( i_read > 0 )
100     {
101         *pi_data += i_read;
102     }
103     (*pp_data)[*pi_data] = '\0';
104
105     return VLC_SUCCESS;
106 }
107
108 /* Parse a directory and recursively add files */
109 int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
110                         char *psz_dir )
111 {
112     intf_sys_t     *p_sys = p_intf->p_sys;
113     char           dir[MAX_DIR_SIZE];
114     DIR           *p_dir;
115     vlc_acl_t     *p_acl;
116     FILE          *file;
117
118     char          *user = NULL;
119     char          *password = NULL;
120
121     int           i_dirlen;
122
123     char sep;
124
125 #if defined( WIN32 )
126     sep = '\\';
127 #else
128     sep = '/';
129 #endif
130
131     if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
132     {
133         if( errno != ENOENT && errno != ENOTDIR )
134             msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
135         return VLC_EGENERIC;
136     }
137
138     i_dirlen = strlen( psz_dir );
139     if( i_dirlen + 10 > MAX_DIR_SIZE )
140     {
141         msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
142         closedir( p_dir );
143         return 0;
144     }
145
146     msg_Dbg( p_intf, "dir=%s", psz_dir );
147
148     snprintf( dir, sizeof( dir ), "%s%c.access", psz_dir, sep );
149     if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
150     {
151         char line[1024];
152         int  i_size;
153
154         msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
155
156         i_size = fread( line, 1, 1023, file );
157         if( i_size > 0 )
158         {
159             char *p;
160             while( i_size > 0 && ( line[i_size-1] == '\n' ||
161                    line[i_size-1] == '\r' ) )
162             {
163                 i_size--;
164             }
165
166             line[i_size] = '\0';
167
168             p = strchr( line, ':' );
169             if( p )
170             {
171                 *p++ = '\0';
172                 user = strdup( line );
173                 password = strdup( p );
174             }
175         }
176         msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
177                  user, password, i_size );
178
179         fclose( file );
180     }
181
182     snprintf( dir, sizeof( dir ), "%s%c.hosts", psz_dir, sep );
183     p_acl = ACL_Create( p_intf, false );
184     if( ACL_LoadFile( p_acl, dir ) )
185     {
186         ACL_Destroy( p_acl );
187
188         struct stat st;
189         if( stat( dir, &st ) == 0 )
190         {
191             closedir( dir );
192             return VLC_EGENERIC;
193         }
194         p_acl = NULL;
195     }
196
197     for( ;; )
198     {
199         char *psz_filename;
200         /* parse psz_src dir */
201         if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
202         {
203             break;
204         }
205
206         if( ( psz_filename[0] == '.' )
207          || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
208         {
209             free( psz_filename );
210             continue;
211         }
212
213         snprintf( dir, sizeof( dir ), "%s%c%s", psz_dir, sep, psz_filename );
214         free( psz_filename );
215
216         if( ParseDirectory( p_intf, psz_root, dir ) )
217         {
218             httpd_file_sys_t *f = NULL;
219             httpd_handler_sys_t *h = NULL;
220             bool b_index;
221             char *psz_name, *psz_ext;
222
223             psz_name = FileToUrl( &dir[strlen( psz_root )], &b_index );
224             psz_ext = strrchr( dir, '.' );
225             if( psz_ext != NULL )
226             {
227                 int i;
228                 psz_ext++;
229                 for( i = 0; i < p_sys->i_handlers; i++ )
230                     if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
231                         break;
232                 if( i < p_sys->i_handlers )
233                 {
234                     f = malloc( sizeof( httpd_handler_sys_t ) );
235                     h = (httpd_handler_sys_t *)f;
236                     f->b_handler = true;
237                     h->p_association = p_sys->pp_handlers[i];
238                 }
239             }
240             if( f == NULL )
241             {
242                 f = malloc( sizeof( httpd_file_sys_t ) );
243                 f->b_handler = false;
244             }
245
246             f->p_intf  = p_intf;
247             f->p_file = NULL;
248             f->p_redir = NULL;
249             f->p_redir2 = NULL;
250             f->file = strdup (dir);
251             f->name = psz_name;
252             f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? true : false;
253
254             if( !f->name )
255             {
256                 msg_Err( p_intf , "unable to parse directory" );
257                 closedir( p_dir );
258                 free( f );
259                 return( VLC_ENOMEM );
260             }
261             msg_Dbg( p_intf, "file=%s (url=%s)",
262                      f->file, f->name );
263
264             if( !f->b_handler )
265             {
266                 char *psz_type = strdup( "text/html; charset=UTF-8" );
267                 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
268                 {
269                     char *psz = strstr( psz_type, "html;" );
270                     if( psz )
271                     {
272                         psz[0] = 'x';
273                         psz[1] = 'm';
274                         psz[2] = 'l';
275                         psz[3] = ';';
276                         psz[4] = ' ';
277                     }
278                 }
279                 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
280                                            f->name,
281                                            f->b_html ? psz_type : NULL,
282                                            user, password, p_acl,
283                                            HttpCallback, f );
284                 free( psz_type );
285                 if( f->p_file != NULL )
286                 {
287                     TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
288                 }
289             }
290             else
291             {
292                 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
293                                                  f->name,
294                                                  user, password, p_acl,
295                                                  HandlerCallback, h );
296                 if( h->p_handler != NULL )
297                 {
298                     TAB_APPEND( p_sys->i_files, p_sys->pp_files,
299                                 (httpd_file_sys_t *)h );
300                 }
301             }
302
303             /* for url that ends by / add
304              *  - a redirect from rep to rep/
305              *  - in case of index.* rep/index.html to rep/ */
306             if( f && f->name[strlen(f->name) - 1] == '/' )
307             {
308                 char *psz_redir = strdup( f->name );
309                 char *p;
310                 psz_redir[strlen( psz_redir ) - 1] = '\0';
311
312                 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
313                 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
314                 free( psz_redir );
315
316                 if( b_index && ( p = strstr( f->file, "index." ) ) )
317                 {
318                     asprintf( &psz_redir, "%s%s", f->name, p );
319
320                     msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
321                     f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
322                                                      f->name, psz_redir );
323
324                     free( psz_redir );
325                 }
326             }
327         }
328     }
329
330     free( user );
331     free( password );
332
333     ACL_Destroy( p_acl );
334     closedir( p_dir );
335
336     return VLC_SUCCESS;
337 }
338
339
340 /*************************************************************************
341  * Playlist stuff
342  *************************************************************************/
343 void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
344                            playlist_item_t *p_node, char *name, mvar_t *s,
345                            int i_depth )
346 {
347     if( p_node != NULL )
348     {
349         if( p_node->i_children == -1 )
350         {
351             char value[512];
352             char *psz;
353             mvar_t *itm = mvar_New( name, "set" );
354
355             if( p_pl->status.p_item && p_node &&
356                 p_pl->status.p_item->p_input && p_node->p_input &&
357                 p_pl->status.p_item->p_input->i_id == p_node->p_input->i_id )
358             {
359                 mvar_AppendNewVar( itm, "current", "1" );
360             }
361             else
362             {
363                 mvar_AppendNewVar( itm, "current", "0" );
364             }
365
366             sprintf( value, "%d", p_node->i_id );
367             mvar_AppendNewVar( itm, "index", value );
368
369             psz = input_item_GetName( p_node->p_input );
370             mvar_AppendNewVar( itm, "name", psz );
371             free( psz );
372
373             psz = input_item_GetURI( p_node->p_input );
374             mvar_AppendNewVar( itm, "uri", psz );
375             free( psz );
376
377             sprintf( value, "Item");
378             mvar_AppendNewVar( itm, "type", value );
379
380             sprintf( value, "%d", i_depth );
381             mvar_AppendNewVar( itm, "depth", value );
382
383             if( p_node->i_flags & PLAYLIST_RO_FLAG )
384             {
385                 mvar_AppendNewVar( itm, "ro", "ro" );
386             }
387             else
388             {
389                 mvar_AppendNewVar( itm, "ro", "rw" );
390             }
391
392             sprintf( value, "%ld",
393                     (long) input_item_GetDuration( p_node->p_input ) );
394             mvar_AppendNewVar( itm, "duration", value );
395
396             mvar_AppendVar( s, itm );
397         }
398         else
399         {
400             char value[512];
401             int i_child;
402             mvar_t *itm = mvar_New( name, "set" );
403
404             mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
405             mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
406
407             sprintf( value, "Node" );
408             mvar_AppendNewVar( itm, "type", value );
409
410             sprintf( value, "%d", p_node->i_id );
411             mvar_AppendNewVar( itm, "index", value );
412
413             sprintf( value, "%d", p_node->i_children);
414             mvar_AppendNewVar( itm, "i_children", value );
415
416             sprintf( value, "%d", i_depth );
417             mvar_AppendNewVar( itm, "depth", value );
418
419             if( p_node->i_flags & PLAYLIST_RO_FLAG )
420             {
421                 mvar_AppendNewVar( itm, "ro", "ro" );
422             }
423             else
424             {
425                 mvar_AppendNewVar( itm, "ro", "rw" );
426             }
427
428             mvar_AppendVar( s, itm );
429
430             for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
431                 PlaylistListNode( p_intf, p_pl,
432                                       p_node->pp_children[i_child],
433                                       name, s, i_depth + 1);
434
435         }
436     }
437 }
438
439 /****************************************************************************
440  * Seek command parsing handling
441  ****************************************************************************/
442 void HandleSeek( intf_thread_t *p_intf, char *p_value )
443 {
444     intf_sys_t     *p_sys = p_intf->p_sys;
445     vlc_value_t val;
446     int i_stock = 0;
447     uint64_t i_length;
448     int i_value = 0;
449     int i_relative = 0;
450 #define POSITION_ABSOLUTE 12
451 #define POSITION_REL_FOR 13
452 #define POSITION_REL_BACK 11
453 #define VL_TIME_ABSOLUTE 0
454 #define VL_TIME_REL_FOR 1
455 #define VL_TIME_REL_BACK -1
456     if( p_sys->p_input )
457     {
458         var_Get( p_sys->p_input, "length", &val );
459         i_length = val.i_time;
460
461         while( p_value[0] != '\0' )
462         {
463             switch(p_value[0])
464             {
465                 case '+':
466                 {
467                     i_relative = VL_TIME_REL_FOR;
468                     p_value++;
469                     break;
470                 }
471                 case '-':
472                 {
473                     i_relative = VL_TIME_REL_BACK;
474                     p_value++;
475                     break;
476                 }
477                 case '0': case '1': case '2': case '3': case '4':
478                 case '5': case '6': case '7': case '8': case '9':
479                 {
480                     i_stock = strtol( p_value , &p_value , 10 );
481                     break;
482                 }
483                 case '%': /* for percentage ie position */
484                 {
485                     i_relative += POSITION_ABSOLUTE;
486                     i_value = i_stock;
487                     i_stock = 0;
488                     p_value[0] = '\0';
489                     break;
490                 }
491                 case ':':
492                 {
493                     i_value = 60 * (i_value + i_stock) ;
494                     i_stock = 0;
495                     p_value++;
496                     break;
497                 }
498                 case 'h': case 'H': /* hours */
499                 {
500                     i_value += 3600 * i_stock;
501                     i_stock = 0;
502                     /* other characters which are not numbers are not
503                      * important */
504                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
505                            && (p_value[0] != '\0') )
506                     {
507                         p_value++;
508                     }
509                     break;
510                 }
511                 case 'm': case 'M': case '\'': /* minutes */
512                 {
513                     i_value += 60 * i_stock;
514                     i_stock = 0;
515                     p_value++;
516                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
517                            && (p_value[0] != '\0') )
518                     {
519                         p_value++;
520                     }
521                     break;
522                 }
523                 case 's': case 'S': case '"':  /* seconds */
524                 {
525                     i_value += i_stock;
526                     i_stock = 0;
527                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
528                            && (p_value[0] != '\0') )
529                     {
530                         p_value++;
531                     }
532                     break;
533                 }
534                 default:
535                 {
536                     p_value++;
537                     break;
538                 }
539             }
540         }
541
542         /* if there is no known symbol, I consider it as seconds.
543          * Otherwise, i_stock = 0 */
544         i_value += i_stock;
545
546         switch(i_relative)
547         {
548             case VL_TIME_ABSOLUTE:
549             {
550                 if( (uint64_t)( i_value ) * 1000000 <= i_length )
551                     val.i_time = (uint64_t)( i_value ) * 1000000;
552                 else
553                     val.i_time = i_length;
554
555                 var_Set( p_sys->p_input, "time", val );
556                 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
557                 break;
558             }
559             case VL_TIME_REL_FOR:
560             {
561                 var_Get( p_sys->p_input, "time", &val );
562                 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
563                 {
564                     val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
565                 } else
566                 {
567                     val.i_time = i_length;
568                 }
569                 var_Set( p_sys->p_input, "time", val );
570                 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
571                 break;
572             }
573             case VL_TIME_REL_BACK:
574             {
575                 var_Get( p_sys->p_input, "time", &val );
576                 if( (int64_t)( i_value ) * 1000000 > val.i_time )
577                 {
578                     val.i_time = 0;
579                 } else
580                 {
581                     val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
582                 }
583                 var_Set( p_sys->p_input, "time", val );
584                 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
585                 break;
586             }
587             case POSITION_ABSOLUTE:
588             {
589                 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
590                                             0.0 ), 100.0 );
591                 var_Set( p_sys->p_input, "position", val );
592                 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
593                 break;
594             }
595             case POSITION_REL_FOR:
596             {
597                 var_Get( p_sys->p_input, "position", &val );
598                 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
599                                              0.0 ) , 100.0 );
600                 var_Set( p_sys->p_input, "position", val );
601                 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
602                          i_value );
603                 break;
604             }
605             case POSITION_REL_BACK:
606             {
607                 var_Get( p_sys->p_input, "position", &val );
608                 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
609                                              0.0 ) , 100.0 );
610                 var_Set( p_sys->p_input, "position", val );
611                 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
612                          i_value );
613                 break;
614             }
615             default:
616             {
617                 msg_Dbg( p_intf, "invalid seek request" );
618                 break;
619             }
620         }
621     }
622 #undef POSITION_ABSOLUTE
623 #undef POSITION_REL_FOR
624 #undef POSITION_REL_BACK
625 #undef VL_TIME_ABSOLUTE
626 #undef VL_TIME_REL_FOR
627 #undef VL_TIME_REL_BACK
628 }
629
630
631 /****************************************************************************
632  * URI Parsing functions
633  ****************************************************************************/
634 int TestURIParam( char *psz_uri, const char *psz_name )
635 {
636     char *p = psz_uri;
637
638     while( (p = strstr( p, psz_name )) )
639     {
640         /* Verify that we are dealing with a post/get argument */
641         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
642               && p[strlen(psz_name)] == '=' )
643         {
644             return true;
645         }
646         p++;
647     }
648
649     return false;
650 }
651
652 static char *FindURIValue( char *psz_uri, const char *restrict psz_name,
653                            size_t *restrict p_len )
654 {
655     char *p = psz_uri, *end;
656     size_t len;
657
658     while( (p = strstr( p, psz_name )) )
659     {
660         /* Verify that we are dealing with a post/get argument */
661         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
662               && p[strlen(psz_name)] == '=' )
663             break;
664         p++;
665     }
666
667     if( p == NULL )
668     {
669         *p_len = 0;
670         return NULL;
671     }
672
673     p += strlen( psz_name );
674     if( *p == '=' ) p++;
675
676     if( ( end = strchr( p, '\n' ) ) != NULL )
677     {
678         /* POST method */
679         if( ( end > p ) && ( end[-1] == '\r' ) )
680             end--;
681
682         len = end - p;
683     }
684     else
685     {
686         /* GET method */
687         if( ( end = strchr( p, '&' ) ) != NULL )
688             len = end - p;
689         else
690             len = strlen( p );
691     }
692
693     *p_len = len;
694     return p;
695 }
696
697 char *ExtractURIValue( char *restrict psz_uri,
698                            const char *restrict psz_name,
699                            char *restrict psz_buf, size_t bufsize )
700 {
701     size_t len;
702     char *psz_value = FindURIValue( psz_uri, psz_name, &len );
703     char *psz_next;
704
705     if( psz_value == NULL )
706     {
707         if( bufsize > 0 )
708             *psz_buf = '\0';
709         return NULL;
710     }
711
712     psz_next = psz_value + len;
713
714     if( len >= bufsize )
715         len = bufsize - 1;
716
717     if( len > 0 )
718         strncpy( psz_buf, psz_value, len );
719     if( bufsize > 0 )
720         psz_buf[len] = '\0';
721
722     return psz_next;
723 }
724
725 char *ExtractURIString( char *restrict psz_uri,
726                             const char *restrict psz_name )
727 {
728     size_t len;
729     char *psz_value = FindURIValue( psz_uri, psz_name, &len );
730
731     if( psz_value == NULL )
732         return NULL;
733
734     char *res = malloc( len + 1 );
735     if( res == NULL )
736         return NULL;
737
738     memcpy( res, psz_value, len );
739     res[len] = '\0';
740
741     return res;
742 }
743
744 /* Since the resulting string is smaller we can work in place, so it is
745  * permitted to have psz == new. new points to the first word of the
746  * string, the function returns the remaining string. */
747 char *FirstWord( char *psz, char *new )
748 {
749     bool b_end;
750
751     while( *psz == ' ' )
752         psz++;
753
754     while( *psz != '\0' && *psz != ' ' )
755     {
756         if( *psz == '\'' )
757         {
758             char c = *psz++;
759             while( *psz != '\0' && *psz != c )
760             {
761                 if( *psz == '\\' && psz[1] != '\0' )
762                     psz++;
763                 *new++ = *psz++;
764             }
765             if( *psz == c )
766                 psz++;
767         }
768         else
769         {
770             if( *psz == '\\' && psz[1] != '\0' )
771                 psz++;
772             *new++ = *psz++;
773         }
774     }
775     b_end = !*psz;
776
777     *new++ = '\0';
778     if( !b_end )
779         return psz + 1;
780     else
781         return NULL;
782 }
783
784 /**********************************************************************
785  * MRLParse: parse the MRL, find the MRL string and the options,
786  * create an item with all information in it, and return the item.
787  * return NULL if there is an error.
788  **********************************************************************/
789
790 /* Function analog to FirstWord except that it relies on colon instead
791  * of space to delimit option boundaries. */
792 static char *FirstOption( char *psz, char *new )
793 {
794     bool b_end, b_start = true;
795
796     while( *psz == ' ' )
797         psz++;
798
799     while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
800     {
801         if( *psz == '\'' )
802         {
803             char c = *psz++;
804             while( *psz != '\0' && *psz != c )
805             {
806                 if( *psz == '\\' && psz[1] != '\0' )
807                     psz++;
808                 *new++ = *psz++;
809                 b_start = false;
810             }
811             if( *psz == c )
812                 psz++;
813         }
814         else
815         {
816             if( *psz == '\\' && psz[1] != '\0' )
817                 psz++;
818             *new++ = *psz++;
819             b_start = false;
820         }
821     }
822     b_end = !*psz;
823
824     if ( !b_start )
825         while (new[-1] == ' ')
826             new--;
827
828     *new++ = '\0';
829     if( !b_end )
830         return psz + 1;
831     else
832         return NULL;
833 }
834
835 input_item_t *MRLParse( intf_thread_t *p_intf, char *_psz,
836                                    char *psz_name )
837 {
838     char *psz = strdup( _psz );
839     char *s_mrl = psz;
840     char *s_temp;
841     input_item_t * p_input = NULL;
842
843     /* extract the mrl */
844     s_temp = FirstOption( s_mrl, s_mrl );
845     if( s_temp == NULL )
846     {
847         s_temp = s_mrl + strlen( s_mrl );
848     }
849
850     p_input = input_ItemNew( p_intf, s_mrl, psz_name );
851     s_mrl = s_temp;
852
853     /* now we can take care of the options */
854     while ( *s_mrl != '\0' )
855     {
856         s_temp = FirstOption( s_mrl, s_mrl );
857         if( s_mrl == '\0' )
858             break;
859         if( s_temp == NULL )
860         {
861             s_temp = s_mrl + strlen( s_mrl );
862         }
863         input_ItemAddOption( p_input, s_mrl );
864         s_mrl = s_temp;
865     }
866
867     free( psz );
868     return p_input;
869 }
870
871 /**********************************************************************
872  * RealPath: parse ../, ~ and path stuff
873  **********************************************************************/
874 char *RealPath( const char *psz_src )
875 {
876     char *psz_dir;
877     char *p;
878     int i_len = strlen(psz_src);
879     const char sep = DIR_SEP_CHAR;
880
881     psz_dir = malloc( i_len + 2 );
882     strcpy( psz_dir, psz_src );
883
884     /* Add a trailing sep to ease the .. step */
885     psz_dir[i_len] = sep;
886     psz_dir[i_len + 1] = '\0';
887
888 #if (DIR_SEP_CHAR != '/')
889     /* Convert all / to native separator */
890     p = psz_dir;
891     while( (p = strchr( p, '/' )) != NULL )
892     {
893         *p = sep;
894     }
895 #endif
896
897     /* FIXME: this could be O(N) rather than O(N²)... */
898     /* Remove multiple separators and /./ */
899     p = psz_dir;
900     while( (p = strchr( p, sep )) != NULL )
901     {
902         if( p[1] == sep )
903             memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
904         else if( p[1] == '.' && p[2] == sep )
905             memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
906         else
907             p++;
908     }
909
910     if( psz_dir[0] == '~' )
911     {
912         char *dir;
913         asprintf( &dir, "%s%s", config_GetHomeDir(), psz_dir + 1 );
914         free( psz_dir );
915         psz_dir = dir;
916     }
917
918     if( strlen(psz_dir) > 2 )
919     {
920         /* Fix all .. dir */
921         p = psz_dir + 3;
922         while( (p = strchr( p, sep )) != NULL )
923         {
924             if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
925             {
926                 char *q;
927                 p[-3] = '\0';
928                 if( (q = strrchr( psz_dir, sep )) != NULL )
929                 {
930                     memmove( q + 1, p + 1, strlen(p + 1) + 1 );
931                     p = q + 1;
932                 }
933                 else
934                 {
935                     memmove( psz_dir, p, strlen(p) + 1 );
936                     p = psz_dir + 3;
937                 }
938             }
939             else
940                 p++;
941         }
942     }
943
944     /* Remove trailing sep if there are at least 2 sep in the string
945      * (handles the C:\ stuff) */
946     p = strrchr( psz_dir, sep );
947     if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )
948         *p = '\0';
949
950     return psz_dir;
951 }