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