]> git.sesse.net Git - vlc/blob - modules/control/http/util.c
For consistency, remove references to vlc from libvlc
[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 <stdio.h>
28 #include "http.h"
29 #include "vlc_strings.h"
30
31 /****************************************************************************
32  * File and directory functions
33  ****************************************************************************/
34
35 /* ToUrl: create a good name for an url from filename */
36 char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index )
37 {
38     char *url, *p;
39
40     url = p = malloc( strlen( name ) + 1 );
41
42     *pb_index = VLC_FALSE;
43     if( !url || !p )
44     {
45         return NULL;
46     }
47
48 #ifdef WIN32
49     while( *name == '\\' || *name == '/' )
50 #else
51     while( *name == '/' )
52 #endif
53     {
54         name++;
55     }
56
57     *p++ = '/';
58     strcpy( p, name );
59
60 #ifdef WIN32
61     /* convert '\\' into '/' */
62     name = p;
63     while( *name )
64     {
65         if( *name == '\\' )
66             *name = '/';
67         name++;
68     }
69 #endif
70
71     /* index.* -> / */
72     if( ( p = strrchr( url, '/' ) ) != NULL )
73     {
74         if( !strncmp( p, "/index.", 7 ) )
75         {
76             p[1] = '\0';
77             *pb_index = VLC_TRUE;
78         }
79     }
80     return url;
81 }
82
83 /* Load a file */
84 int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data )
85 {
86     int i_read;
87
88     /* just load the file */
89     *pi_data = 0;
90     *pp_data = malloc( 1025 );  /* +1 for \0 */
91     while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
92     {
93         *pi_data += 1024;
94         *pp_data = realloc( *pp_data, *pi_data  + 1025 );
95     }
96     if( i_read > 0 )
97     {
98         *pi_data += i_read;
99     }
100     (*pp_data)[*pi_data] = '\0';
101
102     return VLC_SUCCESS;
103 }
104
105 /* Parse a directory and recursively add files */
106 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
107                         char *psz_dir )
108 {
109     intf_sys_t     *p_sys = p_intf->p_sys;
110     char           dir[MAX_DIR_SIZE];
111 #ifdef HAVE_SYS_STAT_H
112     struct stat   stat_info;
113 #endif
114     DIR           *p_dir;
115     vlc_acl_t     *p_acl;
116     FILE          *file;
117
118     char          *user = NULL;
119     char          *password = NULL;
120
121     int           i_dirlen;
122
123     char sep;
124
125 #if defined( WIN32 )
126     sep = '\\';
127 #else
128     sep = '/';
129 #endif
130
131 #ifdef HAVE_SYS_STAT_H
132     if( utf8_stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
133     {
134         return VLC_EGENERIC;
135     }
136 #endif
137
138     if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
139     {
140         msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
141         return VLC_EGENERIC;
142     }
143
144     i_dirlen = strlen( psz_dir );
145     if( i_dirlen + 10 > MAX_DIR_SIZE )
146     {
147         msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
148         return 0;
149     }
150
151     msg_Dbg( p_intf, "dir=%s", psz_dir );
152
153     sprintf( dir, "%s%c.access", psz_dir, sep );
154     if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
155     {
156         char line[1024];
157         int  i_size;
158
159         msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
160
161         i_size = fread( line, 1, 1023, file );
162         if( i_size > 0 )
163         {
164             char *p;
165             while( i_size > 0 && ( line[i_size-1] == '\n' ||
166                    line[i_size-1] == '\r' ) )
167             {
168                 i_size--;
169             }
170
171             line[i_size] = '\0';
172
173             p = strchr( line, ':' );
174             if( p )
175             {
176                 *p++ = '\0';
177                 user = strdup( line );
178                 password = strdup( p );
179             }
180         }
181         msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
182                  user, password, i_size );
183
184         fclose( file );
185     }
186
187     sprintf( dir, "%s%c.hosts", psz_dir, sep );
188     p_acl = ACL_Create( p_intf, VLC_FALSE );
189     if( ACL_LoadFile( p_acl, dir ) )
190     {
191         ACL_Destroy( p_acl );
192         p_acl = NULL;
193     }
194
195     for( ;; )
196     {
197         const char *psz_filename;
198         /* parse psz_src dir */
199         if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
200         {
201             break;
202         }
203
204         if( ( psz_filename[0] == '.' )
205          || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
206             continue;
207
208         sprintf( dir, "%s%c%s", psz_dir, sep, psz_filename );
209         LocaleFree( psz_filename );
210
211         if( E_(ParseDirectory)( p_intf, psz_root, dir ) )
212         {
213             httpd_file_sys_t *f = NULL;
214             httpd_handler_sys_t *h = NULL;
215             vlc_bool_t b_index;
216             char *psz_tmp, *psz_file, *psz_name, *psz_ext;
217
218             psz_tmp = vlc_fix_readdir_charset( p_intf, dir );
219             psz_file = E_(FromUTF8)( p_intf, psz_tmp );
220             free( psz_tmp );
221             psz_tmp = vlc_fix_readdir_charset( p_intf,
222                                                &dir[strlen( psz_root )] );
223             psz_name = E_(FileToUrl)( psz_tmp, &b_index );
224             free( psz_tmp );
225             psz_ext = strrchr( psz_file, '.' );
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 = psz_file;
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( p_sys->psz_html_type );
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  * Locale functions
349  **************************************************************************/
350 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
351 {
352     intf_sys_t    *p_sys = p_intf->p_sys;
353
354     if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
355     {
356         size_t i_in = strlen(psz_utf8);
357         size_t i_out = i_in * 2;
358         char *psz_local = malloc(i_out + 1);
359         char *psz_out = psz_local;
360         size_t i_ret;
361         char psz_tmp[i_in + 1];
362         const char *psz_in = psz_tmp;
363         strcpy( psz_tmp, psz_utf8 );
364
365         i_in = strlen( psz_tmp );
366
367         i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
368                            &psz_out, &i_out );
369         if( i_ret == (size_t)-1 || i_in )
370         {
371             msg_Warn( p_intf,
372                       "failed to convert \"%s\" to desired charset (%s)",
373                       psz_utf8, strerror(errno) );
374             free( psz_local );
375             return strdup( psz_utf8 );
376         }
377
378         *psz_out = '\0';
379         return psz_local;
380     }
381     else
382         return strdup( psz_utf8 );
383 }
384
385 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
386 {
387     intf_sys_t    *p_sys = p_intf->p_sys;
388
389     if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
390     {
391         const char *psz_in = psz_local;
392         size_t i_in = strlen(psz_in);
393         size_t i_out = i_in * 6;
394         char *psz_utf8 = malloc(i_out + 1);
395         char *psz_out = psz_utf8;
396
397         size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
398                                   &psz_out, &i_out );
399         if( i_ret == (size_t)-1 || i_in )
400         {
401             msg_Warn( p_intf,
402                       "failed to convert \"%s\" to desired charset (%s)",
403                       psz_local, strerror(errno) );
404             free( psz_utf8 );
405             return strdup( psz_local );
406         }
407
408         *psz_out = '\0';
409         return psz_utf8;
410     }
411     else
412         return strdup( psz_local );
413 }
414
415 /*************************************************************************
416  * Playlist stuff
417  *************************************************************************/
418 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
419                            playlist_item_t *p_node, char *name, mvar_t *s,
420                            int i_depth )
421 {
422     if( p_node != NULL )
423     {
424         if( p_node->i_children == -1 )
425         {
426             char value[512];
427             char *psz;
428             mvar_t *itm = E_(mvar_New)( name, "set" );
429
430             if( p_pl->status.p_item && p_node &&
431                 p_pl->status.p_item->p_input && p_node->p_input &&
432                 p_pl->status.p_item->p_input->i_id == p_node->p_input->i_id )
433             {
434                 E_(mvar_AppendNewVar)( itm, "current", "1" );
435             }
436             else
437             {
438                 E_(mvar_AppendNewVar)( itm, "current", "0" );
439             }
440
441             sprintf( value, "%d", p_node->p_input->i_id );
442             E_(mvar_AppendNewVar)( itm, "index", value );
443
444             psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_name );
445             E_(mvar_AppendNewVar)( itm, "name", psz );
446             free( psz );
447
448             psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_uri );
449             E_(mvar_AppendNewVar)( itm, "uri", psz );
450             free( psz );
451
452             sprintf( value, "Item");
453             E_(mvar_AppendNewVar)( itm, "type", value );
454
455             sprintf( value, "%d", i_depth );
456             E_(mvar_AppendNewVar)( itm, "depth", value );
457
458             if( p_node->i_flags & PLAYLIST_RO_FLAG )
459             {
460                 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
461             }
462             else
463             {
464                 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
465             }
466
467             sprintf( value, "%ld", (long)p_node->p_input->i_duration );
468             E_(mvar_AppendNewVar)( itm, "duration", value );
469
470             E_(mvar_AppendVar)( s, itm );
471         }
472         else
473         {
474             char value[512];
475             char *psz;
476             int i_child;
477             mvar_t *itm = E_(mvar_New)( name, "set" );
478
479             psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_name );
480             E_(mvar_AppendNewVar)( itm, "name", psz );
481             E_(mvar_AppendNewVar)( itm, "uri", psz );
482             free( psz );
483
484             sprintf( value, "Node" );
485             E_(mvar_AppendNewVar)( itm, "type", value );
486
487             sprintf( value, "%d", p_node->p_input->i_id );
488             E_(mvar_AppendNewVar)( itm, "index", value );
489
490             sprintf( value, "%d", p_node->i_children);
491             E_(mvar_AppendNewVar)( itm, "i_children", value );
492
493             sprintf( value, "%d", i_depth );
494             E_(mvar_AppendNewVar)( itm, "depth", value );
495
496             if( p_node->i_flags & PLAYLIST_RO_FLAG )
497             {
498                 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
499             }
500             else
501             {
502                 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
503             }
504
505             E_(mvar_AppendVar)( s, itm );
506
507             for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
508                 E_(PlaylistListNode)( p_intf, p_pl,
509                                       p_node->pp_children[i_child],
510                                       name, s, i_depth + 1);
511
512         }
513     }
514 }
515
516 /****************************************************************************
517  * Seek command parsing handling
518  ****************************************************************************/
519 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
520 {
521     intf_sys_t     *p_sys = p_intf->p_sys;
522     vlc_value_t val;
523     int i_stock = 0;
524     uint64_t i_length;
525     int i_value = 0;
526     int i_relative = 0;
527 #define POSITION_ABSOLUTE 12
528 #define POSITION_REL_FOR 13
529 #define POSITION_REL_BACK 11
530 #define VL_TIME_ABSOLUTE 0
531 #define VL_TIME_REL_FOR 1
532 #define VL_TIME_REL_BACK -1
533     if( p_sys->p_input )
534     {
535         var_Get( p_sys->p_input, "length", &val );
536         i_length = val.i_time;
537
538         while( p_value[0] != '\0' )
539         {
540             switch(p_value[0])
541             {
542                 case '+':
543                 {
544                     i_relative = VL_TIME_REL_FOR;
545                     p_value++;
546                     break;
547                 }
548                 case '-':
549                 {
550                     i_relative = VL_TIME_REL_BACK;
551                     p_value++;
552                     break;
553                 }
554                 case '0': case '1': case '2': case '3': case '4':
555                 case '5': case '6': case '7': case '8': case '9':
556                 {
557                     i_stock = strtol( p_value , &p_value , 10 );
558                     break;
559                 }
560                 case '%': /* for percentage ie position */
561                 {
562                     i_relative += POSITION_ABSOLUTE;
563                     i_value = i_stock;
564                     i_stock = 0;
565                     p_value[0] = '\0';
566                     break;
567                 }
568                 case ':':
569                 {
570                     i_value = 60 * (i_value + i_stock) ;
571                     i_stock = 0;
572                     p_value++;
573                     break;
574                 }
575                 case 'h': case 'H': /* hours */
576                 {
577                     i_value += 3600 * i_stock;
578                     i_stock = 0;
579                     /* other characters which are not numbers are not
580                      * important */
581                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
582                            && (p_value[0] != '\0') )
583                     {
584                         p_value++;
585                     }
586                     break;
587                 }
588                 case 'm': case 'M': case '\'': /* minutes */
589                 {
590                     i_value += 60 * i_stock;
591                     i_stock = 0;
592                     p_value++;
593                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
594                            && (p_value[0] != '\0') )
595                     {
596                         p_value++;
597                     }
598                     break;
599                 }
600                 case 's': case 'S': case '"':  /* seconds */
601                 {
602                     i_value += i_stock;
603                     i_stock = 0;
604                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
605                            && (p_value[0] != '\0') )
606                     {
607                         p_value++;
608                     }
609                     break;
610                 }
611                 default:
612                 {
613                     p_value++;
614                     break;
615                 }
616             }
617         }
618
619         /* if there is no known symbol, I consider it as seconds.
620          * Otherwise, i_stock = 0 */
621         i_value += i_stock;
622
623         switch(i_relative)
624         {
625             case VL_TIME_ABSOLUTE:
626             {
627                 if( (uint64_t)( i_value ) * 1000000 <= i_length )
628                     val.i_time = (uint64_t)( i_value ) * 1000000;
629                 else
630                     val.i_time = i_length;
631
632                 var_Set( p_sys->p_input, "time", val );
633                 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
634                 break;
635             }
636             case VL_TIME_REL_FOR:
637             {
638                 var_Get( p_sys->p_input, "time", &val );
639                 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
640                 {
641                     val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
642                 } else
643                 {
644                     val.i_time = i_length;
645                 }
646                 var_Set( p_sys->p_input, "time", val );
647                 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
648                 break;
649             }
650             case VL_TIME_REL_BACK:
651             {
652                 var_Get( p_sys->p_input, "time", &val );
653                 if( (int64_t)( i_value ) * 1000000 > val.i_time )
654                 {
655                     val.i_time = 0;
656                 } else
657                 {
658                     val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
659                 }
660                 var_Set( p_sys->p_input, "time", val );
661                 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
662                 break;
663             }
664             case POSITION_ABSOLUTE:
665             {
666                 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
667                                             0.0 ), 100.0 );
668                 var_Set( p_sys->p_input, "position", val );
669                 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
670                 break;
671             }
672             case POSITION_REL_FOR:
673             {
674                 var_Get( p_sys->p_input, "position", &val );
675                 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
676                                              0.0 ) , 100.0 );
677                 var_Set( p_sys->p_input, "position", val );
678                 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
679                          i_value );
680                 break;
681             }
682             case POSITION_REL_BACK:
683             {
684                 var_Get( p_sys->p_input, "position", &val );
685                 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
686                                              0.0 ) , 100.0 );
687                 var_Set( p_sys->p_input, "position", val );
688                 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
689                          i_value );
690                 break;
691             }
692             default:
693             {
694                 msg_Dbg( p_intf, "invalid seek request" );
695                 break;
696             }
697         }
698     }
699 #undef POSITION_ABSOLUTE
700 #undef POSITION_REL_FOR
701 #undef POSITION_REL_BACK
702 #undef VL_TIME_ABSOLUTE
703 #undef VL_TIME_REL_FOR
704 #undef VL_TIME_REL_BACK
705 }
706
707
708 /****************************************************************************
709  * URI Parsing functions
710  ****************************************************************************/
711 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
712 {
713     char *p = psz_uri;
714
715     while( (p = strstr( p, psz_name )) )
716     {
717         /* Verify that we are dealing with a post/get argument */
718         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
719               && p[strlen(psz_name)] == '=' )
720         {
721             return VLC_TRUE;
722         }
723         p++;
724     }
725
726     return VLC_FALSE;
727 }
728 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
729                              char *psz_value, int i_value_max )
730 {
731     char *p = psz_uri;
732
733     while( (p = strstr( p, psz_name )) )
734     {
735         /* Verify that we are dealing with a post/get argument */
736         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
737               && p[strlen(psz_name)] == '=' )
738             break;
739         p++;
740     }
741
742     if( p )
743     {
744         int i_len;
745
746         p += strlen( psz_name );
747         if( *p == '=' ) p++;
748
749         if( strchr( p, '&' ) )
750         {
751             i_len = strchr( p, '&' ) - p;
752         }
753         else
754         {
755             /* for POST method */
756             if( strchr( p, '\n' ) )
757             {
758                 i_len = strchr( p, '\n' ) - p;
759                 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
760             }
761             else
762             {
763                 i_len = strlen( p );
764             }
765         }
766         i_len = __MIN( i_value_max - 1, i_len );
767         if( i_len > 0 )
768         {
769             strncpy( psz_value, p, i_len );
770             psz_value[i_len] = '\0';
771         }
772         else
773         {
774             strncpy( psz_value, "", i_value_max );
775         }
776         p += i_len;
777     }
778     else
779     {
780         strncpy( psz_value, "", i_value_max );
781     }
782
783     return p;
784 }
785
786 /* Since the resulting string is smaller we can work in place, so it is
787  * permitted to have psz == new. new points to the first word of the
788  * string, the function returns the remaining string. */
789 char *E_(FirstWord)( char *psz, char *new )
790 {
791     vlc_bool_t b_end;
792
793     while( *psz == ' ' )
794         psz++;
795
796     while( *psz != '\0' && *psz != ' ' )
797     {
798         if( *psz == '\'' )
799         {
800             char c = *psz++;
801             while( *psz != '\0' && *psz != c )
802             {
803                 if( *psz == '\\' && psz[1] != '\0' )
804                     psz++;
805                 *new++ = *psz++;
806             }
807             if( *psz == c )
808                 psz++;
809         }
810         else
811         {
812             if( *psz == '\\' && psz[1] != '\0' )
813                 psz++;
814             *new++ = *psz++;
815         }
816     }
817     b_end = !*psz;
818
819     *new++ = '\0';
820     if( !b_end )
821         return psz + 1;
822     else
823         return NULL;
824 }
825
826 /**********************************************************************
827  * MRLParse: parse the MRL, find the MRL string and the options,
828  * create an item with all information in it, and return the item.
829  * return NULL if there is an error.
830  **********************************************************************/
831
832 /* Function analog to FirstWord except that it relies on colon instead
833  * of space to delimit option boundaries. */
834 static char *FirstOption( char *psz, char *new )
835 {
836     vlc_bool_t b_end, b_start = VLC_TRUE;
837
838     while( *psz == ' ' )
839         psz++;
840
841     while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
842     {
843         if( *psz == '\'' )
844         {
845             char c = *psz++;
846             while( *psz != '\0' && *psz != c )
847             {
848                 if( *psz == '\\' && psz[1] != '\0' )
849                     psz++;
850                 *new++ = *psz++;
851                 b_start = VLC_FALSE;
852             }
853             if( *psz == c )
854                 psz++;
855         }
856         else
857         {
858             if( *psz == '\\' && psz[1] != '\0' )
859                 psz++;
860             *new++ = *psz++;
861             b_start = VLC_FALSE;
862         }
863     }
864     b_end = !*psz;
865
866     if ( !b_start )
867         while (new[-1] == ' ')
868             new--;
869
870     *new++ = '\0';
871     if( !b_end )
872         return psz + 1;
873     else
874         return NULL;
875 }
876
877 input_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
878                                    char *psz_name )
879 {
880     char *psz = strdup( _psz );
881     char *s_mrl = psz;
882     char *s_temp;
883     input_item_t * p_input = NULL;
884
885     /* extract the mrl */
886     s_temp = FirstOption( s_mrl, s_mrl );
887     if( s_temp == NULL )
888     {
889         s_temp = s_mrl + strlen( s_mrl );
890     }
891
892     p_input = input_ItemNew( p_intf, s_mrl, psz_name );
893     s_mrl = s_temp;
894
895     /* now we can take care of the options */
896     while ( *s_mrl != '\0' )
897     {
898         s_temp = FirstOption( s_mrl, s_mrl );
899         if( s_mrl == '\0' )
900             break;
901         if( s_temp == NULL )
902         {
903             s_temp = s_mrl + strlen( s_mrl );
904         }
905         vlc_input_item_AddOption( p_input, s_mrl );
906         s_mrl = s_temp;
907     }
908
909     free( psz );
910     return p_input;
911 }
912
913 /**********************************************************************
914  * RealPath: parse ../, ~ and path stuff
915  **********************************************************************/
916 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
917 {
918     char *psz_dir;
919     char *p;
920     int i_len = strlen(psz_src);
921     char sep;
922
923 #if defined( WIN32 )
924     sep = '\\';
925 #else
926     sep = '/';
927 #endif
928
929     psz_dir = malloc( i_len + 2 );
930     strcpy( psz_dir, psz_src );
931
932     /* Add a trailing sep to ease the .. step */
933     psz_dir[i_len] = sep;
934     psz_dir[i_len + 1] = '\0';
935
936 #ifdef WIN32
937     /* Convert all / to native separator */
938     p = psz_dir;
939     while( (p = strchr( p, '/' )) != NULL )
940     {
941         *p = sep;
942     }
943 #endif
944
945     /* Remove multiple separators and /./ */
946     p = psz_dir;
947     while( (p = strchr( p, sep )) != NULL )
948     {
949         if( p[1] == sep )
950             memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
951         else if( p[1] == '.' && p[2] == sep )
952             memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
953         else
954             p++;
955     }
956
957     if( psz_dir[0] == '~' )
958     {
959         char *dir = malloc( strlen(psz_dir)
960                              + strlen(p_intf->p_libvlc->psz_userdir) );
961         /* This is incomplete : we should also support the ~cmassiot/ syntax. */
962         sprintf( dir, "%s%s", p_intf->p_libvlc->psz_userdir, psz_dir + 1 );
963         free( psz_dir );
964         psz_dir = dir;
965     }
966
967     if( strlen(psz_dir) > 2 )
968     {
969         /* Fix all .. dir */
970         p = psz_dir + 3;
971         while( (p = strchr( p, sep )) != NULL )
972         {
973             if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
974             {
975                 char *q;
976                 p[-3] = '\0';
977                 if( (q = strrchr( psz_dir, sep )) != NULL )
978                 {
979                     memmove( q + 1, p + 1, strlen(p + 1) + 1 );
980                     p = q + 1;
981                 }
982                 else
983                 {
984                     memmove( psz_dir, p, strlen(p) + 1 );
985                     p = psz_dir + 3;
986                 }
987             }
988             else
989                 p++;
990         }
991     }
992
993     /* Remove trailing sep if there are at least 2 sep in the string
994      * (handles the C:\ stuff) */
995     p = strrchr( psz_dir, sep );
996     if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )
997         *p = '\0';
998
999     return psz_dir;
1000 }