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