]> git.sesse.net Git - vlc/blob - src/interface/intf_cmd.c
D�but du portage BeOS. Beaucoup de fuchiers ont �t� modifi� car il a fallu
[vlc] / src / interface / intf_cmd.c
1 /*****************************************************************************
2  * intf_cmd.c: interface commands parsing and executions functions
3  * This file implements the interface commands execution functions. It is used
4  * by command-line oriented interfaces and scripts. The commands themselves are
5  * implemented in intf_ctrl.
6  *****************************************************************************
7  * Copyright (C) 1998, 1999, 2000 VideoLAN
8  *
9  * Authors:
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 GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public
22  * License along with this program; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <errno.h>                                                  /* errno */
31 #include <stdio.h>                                                   /* FILE */
32 #include <stdlib.h>                                    /* strtod(), strtol() */
33 #include <string.h>                                            /* strerror() */
34
35 #include "threads.h"
36 #include "config.h"
37 #include "common.h"
38 #include "mtime.h"
39 #include "plugins.h"
40
41 #include "interface.h"
42 #include "intf_msg.h"
43 #include "intf_cmd.h"
44 #include "intf_ctrl.h"
45 #include "main.h"
46
47 /*
48  * Local prototypes
49  */
50 static int  ParseCommandArguments   ( char *psz_argv[INTF_MAX_ARGS], char *psz_cmd );
51 static int  CheckCommandArguments   ( intf_arg_t argv[INTF_MAX_ARGS], int i_argc,
52                                       char *psz_argv[INTF_MAX_ARGS], char *psz_format );
53 static void ParseFormatString       ( intf_arg_t format[INTF_MAX_ARGS], char *psz_format );
54 static int  ConvertArgument         ( intf_arg_t *p_arg, int i_flags, char *psz_str );
55
56 /*****************************************************************************
57  * intf_ExecCommand: parse and execute a command
58  *****************************************************************************
59  * This function is called when a command needs to be executed. It parse the
60  * command line, build an argument array, find the command in control commands
61  * array and run the command. It returns the return value of the command, or
62  * EINVAL if no command could be executed. Command line is modified by this
63  * function.
64  * Note that this function may terminate abruptly the program or signify it's
65  * end to the interface thread.
66  *****************************************************************************/
67 int intf_ExecCommand( char *psz_cmd )
68 {
69     char *          psz_argv[INTF_MAX_ARGS];           /* arguments pointers */
70     intf_arg_t      argv[INTF_MAX_ARGS];              /* converted arguments */
71     int             i_argc;                           /* number of arguments */
72     int             i_index;                         /* multi-purposes index */
73     int             i_return;                        /* command return value */
74
75     intf_DbgMsg("command `%s'\n", psz_cmd);
76
77     /* Parse command line (separate arguments). If nothing has been found,
78      * the function returns without error */
79     i_argc = ParseCommandArguments( psz_argv, psz_cmd );
80     if( !i_argc )
81     {
82         return( 0 );
83     }
84
85     /* Find command. Command is always the first token on the line */
86     for( i_index = 0;
87          control_command[i_index].psz_name && strcmp( psz_argv[0], control_command[i_index].psz_name );
88          i_index++ )
89     {
90         ;
91     }
92     if( !control_command[i_index].psz_name )              /* unknown command */
93     {
94         /* Print error message */
95         intf_IntfMsg( "error: unknown command `%s'. Try `help'", psz_argv[0] );
96         return( INTF_USAGE_ERROR );
97     }
98
99     /* Check arguments validity */
100     if( CheckCommandArguments( argv, i_argc, psz_argv, control_command[i_index].psz_format ) )
101     {
102         /* The given arguments does not match the format string. An error message has
103          * already been displayed, so only the usage string is printed */
104         intf_IntfMsg( "usage: %s", control_command[i_index].psz_usage );
105         return( INTF_USAGE_ERROR );
106     }
107
108     /* Execute command */
109     i_return = control_command[i_index].function( i_argc, argv );
110
111     /* Manage special error codes */
112     switch( i_return )
113     {
114     case INTF_FATAL_ERROR:                                    /* fatal error */
115         /* Print message and terminates the interface thread */
116         intf_ErrMsg( "fatal error in command `%s'\n", psz_argv[0] );
117         p_main->p_intf->b_die = 1;
118         break;
119
120     case INTF_CRITICAL_ERROR:                              /* critical error */
121         /* Print message, flush messages queue and exit. Note that this
122          * error should be very rare since it does not even try to cancel other
123          * threads... */
124         intf_ErrMsg("critical error in command `%s'. Please report this error !\n", psz_argv[0] );
125         intf_FlushMsg();
126         exit( INTF_CRITICAL_ERROR );
127         break;
128
129     case INTF_USAGE_ERROR:                                    /* usage error */
130         /* Print error message and usage */
131         intf_IntfMsg( "usage: %s", control_command[i_index].psz_usage );
132         break;
133     }
134
135     /* Return error code */
136     return( i_return );
137 }
138
139 /*****************************************************************************
140  * intf_ExecScript: parse and execute a command script
141  *****************************************************************************
142  * This function, based on ExecCommand read a file and tries to execute each
143  * of its line as a command. It returns 0 if everything succeeded, a negative
144  * number if the script could not be executed and a positive one if an error
145  * occured during execution.
146  *****************************************************************************/
147 int intf_ExecScript( char *psz_filename )
148 {
149     FILE *  p_file;                                                  /* file */
150     char    psz_line[INTF_MAX_CMD_SIZE];                             /* line */
151     char *  psz_index;                                    /* index in string */
152     int     i_err;                                        /* error indicator */
153
154     /* Open file */
155     i_err = 0;
156     p_file = fopen( psz_filename, "r" );
157     if( p_file == NULL )
158     {
159         intf_ErrMsg("warning: %s: %s\n", psz_filename, strerror(errno));
160         return( -1 );
161     }
162
163     /* For each line: read and execute */
164     while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
165     {
166         /* If line begins with a '#', it is a comment and shoule be ignored,
167          * else, execute it */
168         if( psz_line[0] != '#' )
169         {
170             /* The final '\n' needs to be removed before execution */
171             for( psz_index = psz_line; *psz_index && (*psz_index != '\n'); psz_index++ )
172             {
173                 ;
174             }
175             if( *psz_index == '\n' )
176             {
177                 *psz_index = '\0';
178             }
179
180             /* Execute command */
181             i_err |= intf_ExecCommand( psz_line );
182         }
183     }
184     if( !feof( p_file ) )
185     {
186         intf_ErrMsg("error: %s: %s\n", psz_filename, strerror(errno));
187         return( -1 );
188     }
189
190     /* Close file */
191     fclose( p_file );
192     return( i_err != 0 );
193 }
194
195 /* following functions are local */
196
197 /*****************************************************************************
198  * ParseCommandArguments: isolate arguments in a command line
199  *****************************************************************************
200  * This function modify the original command line, adding '\0' and completes
201  * an array of pointers to beginning of arguments. It return the number of
202  * arguments.
203  *****************************************************************************/
204 static int ParseCommandArguments( char *psz_argv[INTF_MAX_ARGS], char *psz_cmd )
205 {
206     int         i_argc;                               /* number of arguments */
207     char *      psz_index;                                          /* index */
208     boolean_t   b_block;                       /* block (argument) indicator */
209
210     /* Initialize parser state */
211     b_block = 0;   /* we start outside a block to remove spaces at beginning */
212     i_argc = 0;
213
214     /* Go through command until end has been reached or maximal number of
215      * arguments has been reached */
216     for( psz_index = psz_cmd; *psz_index && (i_argc < INTF_MAX_ARGS); psz_index++ )
217     {
218         /* Inside a block, end of blocks are marked by spaces */
219         if( b_block )
220         {
221             if( *psz_index == ' ' )
222             {
223                 *psz_index = '\0';               /* mark the end of argument */
224                 b_block = 0;                               /* exit the block */
225             }
226
227         }
228         /* Outside a block, beginning of blocks are marked by any character
229          * different from space */
230         else
231         {
232             if( *psz_index != ' ' )
233             {
234                 psz_argv[i_argc++] = psz_index;            /* store argument */
235                 b_block = 1;                              /* enter the block */
236             }
237         }
238     }
239
240     /* Return number of arguments found */
241     return( i_argc );
242 }
243
244 /*****************************************************************************
245  * CheckCommandArguments: check arguments agains format
246  *****************************************************************************
247  * This function parse each argument and tries to find a match in the format
248  * string. It fills the argv array.
249  * If all arguments have been sucessfuly identified and converted, it returns
250  * 0, else, an error message is issued and non 0 is returned.
251  * Note that no memory is allocated by this function, but that the arguments
252  * can be modified.
253  *****************************************************************************/
254 static int CheckCommandArguments( intf_arg_t argv[INTF_MAX_ARGS], int i_argc,
255                                   char *psz_argv[INTF_MAX_ARGS], char *psz_format )
256 {
257     intf_arg_t  format[INTF_MAX_ARGS];           /* parsed format indicators */
258     int         i_arg;                                     /* argument index */
259     int         i_format;                                    /* format index */
260     char *      psz_index;                                   /* string index */
261     char *      psz_cmp_index;                   /* string comparaison index */
262     int         i_index;                                    /* generic index */
263     boolean_t   b_found;                            /* `argument found' flag */
264
265
266     /* Build format array */
267     ParseFormatString( format, psz_format );
268
269     /* Initialize parser: i_format must be the first non named formatter */
270     for( i_format = 0; ( i_format < INTF_MAX_ARGS )
271              && (format[i_format].i_flags & INTF_NAMED_ARG);
272          i_format++ )
273     {
274         ;
275     }
276
277     /* Scan all arguments */
278     for( i_arg = 1; i_arg < i_argc; i_arg++ )
279     {
280         b_found = 0;
281
282         /* Test if argument can be taken as a named argument: try to find a
283          * '=' in the string */
284         for( psz_index = psz_argv[i_arg]; *psz_index && ( *psz_index != '=' ); psz_index++ )
285         {
286             ;
287         }
288         if( *psz_index == '=' )                                 /* '=' found */
289         {
290             /* Browse all named arguments to check if there is one matching */
291             for( i_index = 0; (i_index < INTF_MAX_ARGS)
292                      && ( format[i_index].i_flags & INTF_NAMED_ARG )
293                      && !b_found;
294                  i_index++ )
295             {
296                 /* Current format string is named... compare start of two
297                  * names. A local inline ntation of a strcmp is used since
298                  * string isn't ended by '\0' but by '=' */
299                 for( psz_index = psz_argv[i_arg], psz_cmp_index = format[i_index].ps_name;
300                      (*psz_index == *psz_cmp_index) && (*psz_index != '=') && (*psz_cmp_index != '=');
301                      psz_index++, psz_cmp_index++ )
302                 {
303                     ;
304                 }
305                 if( *psz_index == *psz_cmp_index )        /* the names match */
306                 {
307                     /* The argument is a named argument which name match the
308                      * named argument i_index. To be valid, the argument should
309                      * not have been already encountered and the type must
310                      * match. Before going further, the '=' is replaced by
311                      * a '\0'. */
312                     *psz_index = '\0';
313
314                     /* Check unicity. If the argument has already been encountered,
315                      * print an error message and return. */
316                     if( format[i_index].i_flags & INTF_PRESENT_ARG )/* present */
317                     {
318                         intf_IntfMsg("error: `%s' has already been encountered", psz_argv[i_arg] );
319                         return( 1 );
320                     }
321
322                      /* Register argument and prepare exit */
323                     b_found = 1;
324                     format[i_index].i_flags |= INTF_PRESENT_ARG;
325                     argv[i_arg].i_flags = INTF_NAMED_ARG;
326                     argv[i_arg].i_index = i_index;
327                     argv[i_arg].ps_name = psz_argv[i_arg];
328
329                     /* Check type and store value */
330                     psz_index++;
331                     if( ConvertArgument( &argv[i_arg], format[i_index].i_flags, psz_index ) )
332                     {
333                         /* An error occured during conversion */
334                         intf_IntfMsg( "error: invalid type for `%s'", psz_index );
335                     }
336                 }
337             }
338         }
339
340         /* If argument is not a named argument, the format string will
341          * be browsed starting from last position until the argument is
342          * found or an error occurs. */
343         if( !b_found )
344         {
345             /* Reset type indicator */
346             argv[i_arg].i_flags = 0;
347
348             /* If argument is not a named argument, the format string will
349              * be browsed starting from last position until the argument is
350              * found, an error occurs or the last format argument is
351              * reached */
352             while( !b_found && (i_format < INTF_MAX_ARGS) && format[i_format].i_flags )
353             {
354                 /* Try to convert argument */
355                 if( !ConvertArgument( &argv[i_arg], format[i_format].i_flags, psz_argv[i_arg] ) )
356                 {
357                     /* Matching format has been found */
358                     b_found = 1;
359                     format[i_format].i_flags |= INTF_PRESENT_ARG;
360                     argv[i_arg].i_index = i_format;
361
362                     /* If argument is repeatable, dot not increase format counter */
363                     if( !(format[i_format].i_flags & INTF_REP_ARG) )
364                     {
365                         i_format++;
366                     }
367                 }
368                 else
369                 {
370                     /* Argument does not match format. This can be an error, or
371                      * just a missing optionnal parameter, or the end of a
372                      * repeated argument */
373                     if( (format[i_format].i_flags & INTF_OPT_ARG)
374                         || (format[i_format].i_flags & INTF_PRESENT_ARG) )
375                     {
376                         /* This is not an error */
377                         i_format++;
378                     }
379                     else
380                     {
381                         /* The present format argument is mandatory and does
382                          * not match the argument */
383                         intf_IntfMsg("error: missing argument before `%s'", psz_argv[i_arg] );
384                         return( 1 );
385                     }
386                 }
387             }
388         }
389
390         /* If argument is not a named argument and hasn't been found in
391          * format string, then it is an usage error and the function can
392          * return */
393         if( !b_found )
394         {
395             intf_IntfMsg("error: `%s' does not match any argument", psz_argv[i_arg] );
396             return( 1 );
397         }
398
399         intf_DbgMsg("argument flags=0x%x (index=%d) name=%s str=%s int=%d float=%f\n",
400                     argv[i_arg].i_flags,
401                     argv[i_arg].i_index,
402                     (argv[i_arg].i_flags & INTF_NAMED_ARG) ? argv[i_arg].ps_name : "NA",
403                     (argv[i_arg].i_flags & INTF_STR_ARG) ? argv[i_arg].psz_str : "NA",
404                     (argv[i_arg].i_flags & INTF_INT_ARG) ? argv[i_arg].i_num : 0,
405                     (argv[i_arg].i_flags & INTF_FLOAT_ARG) ? argv[i_arg].f_num : 0);
406     }
407
408     /* Parse all remaining format specifier to verify they are all optionnal */
409     for( ;  (i_format < INTF_MAX_ARGS) && format[i_format].i_flags ; i_format++ )
410     {
411         if( !(( format[i_format].i_flags & INTF_OPT_ARG)
412               || ( format[i_format].i_flags & INTF_PRESENT_ARG)) )
413         {
414             /* Format has not been used and is neither optionnal nor multiple
415              * and present */
416             intf_IntfMsg("error: missing argument(s)\n");
417             return( 1 );
418         }
419     }
420
421     /* If an error occured, the function already exited, so if this point is
422      * reached, everything is fine */
423     return( 0 );
424 }
425
426 /*****************************************************************************
427  * ConvertArgument: try to convert an argument to a given type
428  *****************************************************************************
429  * This function tries to convert the string argument given in psz_str to
430  * a type specified in i_flags. It updates p_arg and returns O on success,
431  * or 1 on error. No error message is issued.
432  *****************************************************************************/
433 static int ConvertArgument( intf_arg_t *p_arg, int i_flags, char *psz_str )
434 {
435     char *psz_end;                   /* end pointer for conversion functions */
436
437     if( i_flags & INTF_STR_ARG )                                   /* string */
438     {
439         /* A conversion from a string to a string will always succeed... */
440         p_arg->psz_str = psz_str;
441         p_arg->i_flags |= INTF_STR_ARG;
442     }
443     else if( i_flags & INTF_INT_ARG )                             /* integer */
444     {
445         p_arg->i_num = strtol( psz_str, &psz_end, 0 );     /* convert string */
446         /* If the conversion failed, return 1 and do not modify argument
447          * flags. Else, add 'int' flag and continue. */
448         if( !*psz_str || *psz_end )
449         {
450             return( 1 );
451         }
452         p_arg->i_flags |= INTF_INT_ARG;
453     }
454     else if( i_flags & INTF_FLOAT_ARG )                             /* float */
455     {
456         p_arg->f_num = strtod( psz_str, &psz_end );        /* convert string */
457         /* If the conversion failed, return 1 and do not modify argument
458          * flags. Else, add 'float' flag and continue. */
459         if( !*psz_str || *psz_end )
460         {
461             return( 1 );
462         }
463         p_arg->i_flags |= INTF_FLOAT_ARG;
464     }
465 #ifdef DEBUG
466     else                                    /* error: missing type specifier */
467     {
468         intf_ErrMsg("error: missing type specifier for `%s' (0x%x)\n", psz_str, i_flags);
469         return( 1 );
470     }
471 #endif
472
473     return( 0 );
474 }
475
476 /*****************************************************************************
477  * ParseFormatString: parse a format string                              (ok ?)
478  *****************************************************************************
479  * This function read a format string, as specified in the control_command
480  * array, and fill a format array, to allow easier argument identification.
481  * Note that no memory is allocated by this function, but that, in a named
482  * argument, the name field does not end with a '\0' but with an '='.
483  * See command.h for format string specifications.
484  * Note that this function is designed to be efficient, not to check everything
485  * in a format string, which should be entered by a developper and therefore
486  * should be correct (TRUST !).
487  *****************************************************************************/
488 static void ParseFormatString( intf_arg_t format[INTF_MAX_ARGS], char *psz_format )
489 {
490     char *  psz_index;                                /* format string index */
491     char *  psz_start;                              /* argument format start */
492     char *  psz_item;                                          /* item index */
493     int     i_index;                                         /* format index */
494
495     /* Initialize parser */
496     i_index = 0;
497     psz_start = psz_format;
498
499     /* Reset first format indicator */
500     format[ 0 ].i_flags = 0;
501
502     /* Parse format string */
503     for( psz_index = psz_format; *psz_index && (i_index < INTF_MAX_ARGS) ; psz_index++ )
504     {
505         /* A space is always an item terminator */
506         if( *psz_index == ' ' )
507         {
508             /* Parse format item. Items are parsed from end to beginning or to
509              * first '=' */
510             for( psz_item = psz_index - 1;
511                  (psz_item >= psz_start) && !( format[i_index].i_flags & INTF_NAMED_ARG);
512                  psz_item-- )
513             {
514                 switch( *psz_item )
515                 {
516                 case 's':                                          /* string */
517                     format[i_index].i_flags |= INTF_STR_ARG;
518                     break;
519                 case 'i':                                         /* integer */
520                     format[i_index].i_flags |= INTF_INT_ARG;
521                     break;
522                 case 'f':                                           /* float */
523                     format[i_index].i_flags |= INTF_FLOAT_ARG;
524                     break;
525                 case '*':                                 /* can be repeated */
526                     format[i_index].i_flags |= INTF_REP_ARG;
527                     break;
528                 case '?':                              /* optionnal argument */
529                     format[i_index].i_flags |= INTF_OPT_ARG;
530                     break;
531                 case '=':                                   /* name argument */
532                     format[i_index].i_flags |= INTF_NAMED_ARG;
533                     format[i_index].ps_name = psz_start;
534                     break;
535 #ifdef DEBUG
536                 default:/* error which should never happen: incorrect format */
537                     intf_DbgMsg("error: incorrect format string `%s'\n", psz_format);
538                     break;
539 #endif
540                 }
541             }
542
543             /* Mark next item start, increase items counter and reset next
544              * format indicator, if it wasn't the last one. */
545             i_index++;
546             psz_start = psz_index + 1;
547             if( i_index != INTF_MAX_ARGS )       /* end of array not reached */
548             {
549                 format[ i_index ].i_flags = 0;
550             }
551         }
552     }
553 }