]> git.sesse.net Git - vlc/blob - modules/control/http/util.c
a6a2a2784683f010b1b9bc6ab3aac07ce1862287
[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     if( user )
332     {
333         free( user );
334     }
335     if( password )
336     {
337         free( password );
338     }
339
340     ACL_Destroy( p_acl );
341     closedir( p_dir );
342
343     return VLC_SUCCESS;
344 }
345
346
347 /*************************************************************************
348  * Playlist stuff
349  *************************************************************************/
350 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
351                            playlist_item_t *p_node, char *name, mvar_t *s,
352                            int i_depth )
353 {
354     if( p_node != NULL )
355     {
356         if( p_node->i_children == -1 )
357         {
358             char value[512];
359             char *psz;
360             mvar_t *itm = E_(mvar_New)( name, "set" );
361
362             if( p_pl->status.p_item && p_node &&
363                 p_pl->status.p_item->p_input && p_node->p_input &&
364                 p_pl->status.p_item->p_input->i_id == p_node->p_input->i_id )
365             {
366                 E_(mvar_AppendNewVar)( itm, "current", "1" );
367             }
368             else
369             {
370                 E_(mvar_AppendNewVar)( itm, "current", "0" );
371             }
372
373             sprintf( value, "%d", p_node->i_id );
374             E_(mvar_AppendNewVar)( itm, "index", value );
375
376             psz = input_item_GetName( p_node->p_input );
377             E_(mvar_AppendNewVar)( itm, "name", psz );
378             free( psz );
379
380             psz = input_item_GetURI( p_node->p_input );
381             E_(mvar_AppendNewVar)( itm, "uri", psz );
382             free( psz );
383
384             sprintf( value, "Item");
385             E_(mvar_AppendNewVar)( itm, "type", value );
386
387             sprintf( value, "%d", i_depth );
388             E_(mvar_AppendNewVar)( itm, "depth", value );
389
390             if( p_node->i_flags & PLAYLIST_RO_FLAG )
391             {
392                 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
393             }
394             else
395             {
396                 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
397             }
398
399             sprintf( value, "%ld",
400                     (long) input_item_GetDuration( p_node->p_input ) );
401             E_(mvar_AppendNewVar)( itm, "duration", value );
402
403             E_(mvar_AppendVar)( s, itm );
404         }
405         else
406         {
407             char value[512];
408             int i_child;
409             mvar_t *itm = E_(mvar_New)( name, "set" );
410
411             E_(mvar_AppendNewVar)( itm, "name", p_node->p_input->psz_name );
412             E_(mvar_AppendNewVar)( itm, "uri", p_node->p_input->psz_name );
413
414             sprintf( value, "Node" );
415             E_(mvar_AppendNewVar)( itm, "type", value );
416
417             sprintf( value, "%d", p_node->i_id );
418             E_(mvar_AppendNewVar)( itm, "index", value );
419
420             sprintf( value, "%d", p_node->i_children);
421             E_(mvar_AppendNewVar)( itm, "i_children", value );
422
423             sprintf( value, "%d", i_depth );
424             E_(mvar_AppendNewVar)( itm, "depth", value );
425
426             if( p_node->i_flags & PLAYLIST_RO_FLAG )
427             {
428                 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
429             }
430             else
431             {
432                 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
433             }
434
435             E_(mvar_AppendVar)( s, itm );
436
437             for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
438                 E_(PlaylistListNode)( p_intf, p_pl,
439                                       p_node->pp_children[i_child],
440                                       name, s, i_depth + 1);
441
442         }
443     }
444 }
445
446 /****************************************************************************
447  * Seek command parsing handling
448  ****************************************************************************/
449 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
450 {
451     intf_sys_t     *p_sys = p_intf->p_sys;
452     vlc_value_t val;
453     int i_stock = 0;
454     uint64_t i_length;
455     int i_value = 0;
456     int i_relative = 0;
457 #define POSITION_ABSOLUTE 12
458 #define POSITION_REL_FOR 13
459 #define POSITION_REL_BACK 11
460 #define VL_TIME_ABSOLUTE 0
461 #define VL_TIME_REL_FOR 1
462 #define VL_TIME_REL_BACK -1
463     if( p_sys->p_input )
464     {
465         var_Get( p_sys->p_input, "length", &val );
466         i_length = val.i_time;
467
468         while( p_value[0] != '\0' )
469         {
470             switch(p_value[0])
471             {
472                 case '+':
473                 {
474                     i_relative = VL_TIME_REL_FOR;
475                     p_value++;
476                     break;
477                 }
478                 case '-':
479                 {
480                     i_relative = VL_TIME_REL_BACK;
481                     p_value++;
482                     break;
483                 }
484                 case '0': case '1': case '2': case '3': case '4':
485                 case '5': case '6': case '7': case '8': case '9':
486                 {
487                     i_stock = strtol( p_value , &p_value , 10 );
488                     break;
489                 }
490                 case '%': /* for percentage ie position */
491                 {
492                     i_relative += POSITION_ABSOLUTE;
493                     i_value = i_stock;
494                     i_stock = 0;
495                     p_value[0] = '\0';
496                     break;
497                 }
498                 case ':':
499                 {
500                     i_value = 60 * (i_value + i_stock) ;
501                     i_stock = 0;
502                     p_value++;
503                     break;
504                 }
505                 case 'h': case 'H': /* hours */
506                 {
507                     i_value += 3600 * i_stock;
508                     i_stock = 0;
509                     /* other characters which are not numbers are not
510                      * important */
511                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
512                            && (p_value[0] != '\0') )
513                     {
514                         p_value++;
515                     }
516                     break;
517                 }
518                 case 'm': case 'M': case '\'': /* minutes */
519                 {
520                     i_value += 60 * i_stock;
521                     i_stock = 0;
522                     p_value++;
523                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
524                            && (p_value[0] != '\0') )
525                     {
526                         p_value++;
527                     }
528                     break;
529                 }
530                 case 's': case 'S': case '"':  /* seconds */
531                 {
532                     i_value += i_stock;
533                     i_stock = 0;
534                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
535                            && (p_value[0] != '\0') )
536                     {
537                         p_value++;
538                     }
539                     break;
540                 }
541                 default:
542                 {
543                     p_value++;
544                     break;
545                 }
546             }
547         }
548
549         /* if there is no known symbol, I consider it as seconds.
550          * Otherwise, i_stock = 0 */
551         i_value += i_stock;
552
553         switch(i_relative)
554         {
555             case VL_TIME_ABSOLUTE:
556             {
557                 if( (uint64_t)( i_value ) * 1000000 <= i_length )
558                     val.i_time = (uint64_t)( i_value ) * 1000000;
559                 else
560                     val.i_time = i_length;
561
562                 var_Set( p_sys->p_input, "time", val );
563                 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
564                 break;
565             }
566             case VL_TIME_REL_FOR:
567             {
568                 var_Get( p_sys->p_input, "time", &val );
569                 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
570                 {
571                     val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
572                 } else
573                 {
574                     val.i_time = i_length;
575                 }
576                 var_Set( p_sys->p_input, "time", val );
577                 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
578                 break;
579             }
580             case VL_TIME_REL_BACK:
581             {
582                 var_Get( p_sys->p_input, "time", &val );
583                 if( (int64_t)( i_value ) * 1000000 > val.i_time )
584                 {
585                     val.i_time = 0;
586                 } else
587                 {
588                     val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
589                 }
590                 var_Set( p_sys->p_input, "time", val );
591                 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
592                 break;
593             }
594             case POSITION_ABSOLUTE:
595             {
596                 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
597                                             0.0 ), 100.0 );
598                 var_Set( p_sys->p_input, "position", val );
599                 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
600                 break;
601             }
602             case POSITION_REL_FOR:
603             {
604                 var_Get( p_sys->p_input, "position", &val );
605                 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
606                                              0.0 ) , 100.0 );
607                 var_Set( p_sys->p_input, "position", val );
608                 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
609                          i_value );
610                 break;
611             }
612             case POSITION_REL_BACK:
613             {
614                 var_Get( p_sys->p_input, "position", &val );
615                 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
616                                              0.0 ) , 100.0 );
617                 var_Set( p_sys->p_input, "position", val );
618                 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
619                          i_value );
620                 break;
621             }
622             default:
623             {
624                 msg_Dbg( p_intf, "invalid seek request" );
625                 break;
626             }
627         }
628     }
629 #undef POSITION_ABSOLUTE
630 #undef POSITION_REL_FOR
631 #undef POSITION_REL_BACK
632 #undef VL_TIME_ABSOLUTE
633 #undef VL_TIME_REL_FOR
634 #undef VL_TIME_REL_BACK
635 }
636
637
638 /****************************************************************************
639  * URI Parsing functions
640  ****************************************************************************/
641 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
642 {
643     char *p = psz_uri;
644
645     while( (p = strstr( p, psz_name )) )
646     {
647         /* Verify that we are dealing with a post/get argument */
648         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
649               && p[strlen(psz_name)] == '=' )
650         {
651             return VLC_TRUE;
652         }
653         p++;
654     }
655
656     return VLC_FALSE;
657 }
658
659 static char *FindURIValue( char *psz_uri, const char *restrict psz_name,
660                            size_t *restrict p_len )
661 {
662     char *p = psz_uri, *end;
663     size_t len;
664
665     while( (p = strstr( p, psz_name )) )
666     {
667         /* Verify that we are dealing with a post/get argument */
668         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
669               && p[strlen(psz_name)] == '=' )
670             break;
671         p++;
672     }
673
674     if( p == NULL )
675     {
676         *p_len = 0;
677         return NULL;
678     }
679
680     p += strlen( psz_name );
681     if( *p == '=' ) p++;
682
683     if( ( end = strchr( p, '\n' ) ) != NULL )
684     {
685         /* POST method */
686         if( ( end > p ) && ( end[-1] == '\r' ) )
687             end--;
688
689         len = end - p;
690     }
691     else
692     {
693         /* GET method */
694         if( ( end = strchr( p, '&' ) ) != NULL )
695             len = end - p;
696         else
697             len = strlen( p );
698     }
699
700     *p_len = len;
701     return p;
702 }
703
704 char *E_(ExtractURIValue)( char *restrict psz_uri,
705                            const char *restrict psz_name,
706                            char *restrict psz_buf, size_t bufsize )
707 {
708     size_t len;
709     char *psz_value = FindURIValue( psz_uri, psz_name, &len );
710     char *psz_next;
711
712     if( psz_value == NULL )
713     {
714         if( bufsize > 0 )
715             *psz_buf = '\0';
716         return NULL;
717     }
718
719     psz_next = psz_value + len;
720
721     if( len >= bufsize )
722         len = bufsize - 1;
723
724     if( len > 0 )
725         strncpy( psz_buf, psz_value, len );
726     if( bufsize > 0 )
727         psz_buf[len] = '\0';
728
729     return psz_next;
730 }
731
732 char *E_(ExtractURIString)( char *restrict psz_uri,
733                             const char *restrict psz_name )
734 {
735     size_t len;
736     char *psz_value = FindURIValue( psz_uri, psz_name, &len );
737
738     if( psz_value == NULL )
739         return NULL;
740
741     char *res = malloc( len + 1 );
742     if( res == NULL )
743         return NULL;
744
745     memcpy( res, psz_value, len );
746     res[len] = '\0';
747
748     return res;
749 }
750
751 /* Since the resulting string is smaller we can work in place, so it is
752  * permitted to have psz == new. new points to the first word of the
753  * string, the function returns the remaining string. */
754 char *E_(FirstWord)( char *psz, char *new )
755 {
756     vlc_bool_t b_end;
757
758     while( *psz == ' ' )
759         psz++;
760
761     while( *psz != '\0' && *psz != ' ' )
762     {
763         if( *psz == '\'' )
764         {
765             char c = *psz++;
766             while( *psz != '\0' && *psz != c )
767             {
768                 if( *psz == '\\' && psz[1] != '\0' )
769                     psz++;
770                 *new++ = *psz++;
771             }
772             if( *psz == c )
773                 psz++;
774         }
775         else
776         {
777             if( *psz == '\\' && psz[1] != '\0' )
778                 psz++;
779             *new++ = *psz++;
780         }
781     }
782     b_end = !*psz;
783
784     *new++ = '\0';
785     if( !b_end )
786         return psz + 1;
787     else
788         return NULL;
789 }
790
791 /**********************************************************************
792  * MRLParse: parse the MRL, find the MRL string and the options,
793  * create an item with all information in it, and return the item.
794  * return NULL if there is an error.
795  **********************************************************************/
796
797 /* Function analog to FirstWord except that it relies on colon instead
798  * of space to delimit option boundaries. */
799 static char *FirstOption( char *psz, char *new )
800 {
801     vlc_bool_t b_end, b_start = VLC_TRUE;
802
803     while( *psz == ' ' )
804         psz++;
805
806     while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
807     {
808         if( *psz == '\'' )
809         {
810             char c = *psz++;
811             while( *psz != '\0' && *psz != c )
812             {
813                 if( *psz == '\\' && psz[1] != '\0' )
814                     psz++;
815                 *new++ = *psz++;
816                 b_start = VLC_FALSE;
817             }
818             if( *psz == c )
819                 psz++;
820         }
821         else
822         {
823             if( *psz == '\\' && psz[1] != '\0' )
824                 psz++;
825             *new++ = *psz++;
826             b_start = VLC_FALSE;
827         }
828     }
829     b_end = !*psz;
830
831     if ( !b_start )
832         while (new[-1] == ' ')
833             new--;
834
835     *new++ = '\0';
836     if( !b_end )
837         return psz + 1;
838     else
839         return NULL;
840 }
841
842 input_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
843                                    char *psz_name )
844 {
845     char *psz = strdup( _psz );
846     char *s_mrl = psz;
847     char *s_temp;
848     input_item_t * p_input = NULL;
849
850     /* extract the mrl */
851     s_temp = FirstOption( s_mrl, s_mrl );
852     if( s_temp == NULL )
853     {
854         s_temp = s_mrl + strlen( s_mrl );
855     }
856
857     p_input = input_ItemNew( p_intf, s_mrl, psz_name );
858     s_mrl = s_temp;
859
860     /* now we can take care of the options */
861     while ( *s_mrl != '\0' )
862     {
863         s_temp = FirstOption( s_mrl, s_mrl );
864         if( s_mrl == '\0' )
865             break;
866         if( s_temp == NULL )
867         {
868             s_temp = s_mrl + strlen( s_mrl );
869         }
870         input_ItemAddOption( p_input, s_mrl );
871         s_mrl = s_temp;
872     }
873
874     free( psz );
875     return p_input;
876 }
877
878 /**********************************************************************
879  * RealPath: parse ../, ~ and path stuff
880  **********************************************************************/
881 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
882 {
883     char *psz_dir;
884     char *p;
885     int i_len = strlen(psz_src);
886     const char sep = DIR_SEP_CHAR;
887
888     psz_dir = malloc( i_len + 2 );
889     strcpy( psz_dir, psz_src );
890
891     /* Add a trailing sep to ease the .. step */
892     psz_dir[i_len] = sep;
893     psz_dir[i_len + 1] = '\0';
894
895 #if (DIR_SEP_CHAR != '/')
896     /* Convert all / to native separator */
897     p = psz_dir;
898     while( (p = strchr( p, '/' )) != NULL )
899     {
900         *p = sep;
901     }
902 #endif
903
904     /* FIXME: this could be O(N) rather than O(N²)... */
905     /* Remove multiple separators and /./ */
906     p = psz_dir;
907     while( (p = strchr( p, sep )) != NULL )
908     {
909         if( p[1] == sep )
910             memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
911         else if( p[1] == '.' && p[2] == sep )
912             memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
913         else
914             p++;
915     }
916
917     if( psz_dir[0] == '~' )
918     {
919         char *dir;
920         /* This is incomplete : we should also support the ~cmassiot/ syntax. */
921         asprintf( &dir, "%s%s", p_intf->p_libvlc->psz_homedir, psz_dir + 1 );
922         free( psz_dir );
923         psz_dir = dir;
924     }
925
926     if( strlen(psz_dir) > 2 )
927     {
928         /* Fix all .. dir */
929         p = psz_dir + 3;
930         while( (p = strchr( p, sep )) != NULL )
931         {
932             if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
933             {
934                 char *q;
935                 p[-3] = '\0';
936                 if( (q = strrchr( psz_dir, sep )) != NULL )
937                 {
938                     memmove( q + 1, p + 1, strlen(p + 1) + 1 );
939                     p = q + 1;
940                 }
941                 else
942                 {
943                     memmove( psz_dir, p, strlen(p) + 1 );
944                     p = psz_dir + 3;
945                 }
946             }
947             else
948                 p++;
949         }
950     }
951
952     /* Remove trailing sep if there are at least 2 sep in the string
953      * (handles the C:\ stuff) */
954     p = strrchr( psz_dir, sep );
955     if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )
956         *p = '\0';
957
958     return psz_dir;
959 }