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