1 /*****************************************************************************
2 * intf_cmd.c: interface commands parsing and executions functions
4 *****************************************************************************
5 * This file implements the interface commands execution functions. It is used
6 * by command-line oriented interfaces and scripts. The commands themselves are
7 * implemented in intf_ctrl.
8 *****************************************************************************/
10 /*****************************************************************************
12 *****************************************************************************/
21 #include "vlc_thread.h"
23 #include "interface.h"
26 #include "intf_ctrl.h"
32 static int ParseCommandArguments ( char *psz_argv[INTF_MAX_ARGS], char *psz_cmd );
33 static int CheckCommandArguments ( intf_arg_t argv[INTF_MAX_ARGS], int i_argc,
34 char *psz_argv[INTF_MAX_ARGS], char *psz_format );
35 static void ParseFormatString ( intf_arg_t format[INTF_MAX_ARGS], char *psz_format );
36 static int ConvertArgument ( intf_arg_t *p_arg, int i_flags, char *psz_str );
38 /*****************************************************************************
39 * intf_ExecCommand: parse and execute a command
40 *****************************************************************************
41 * This function is called when a command needs to be executed. It parse the
42 * command line, build an argument array, find the command in control commands
43 * array and run the command. It returns the return value of the command, or
44 * EINVAL if no command could be executed. Command line is modified by this
46 * Note that this function may terminate abruptly the program or signify it's
47 * end to the interface thread.
48 *****************************************************************************/
49 int intf_ExecCommand( char *psz_cmd )
51 char * psz_argv[INTF_MAX_ARGS]; /* arguments pointers */
52 intf_arg_t argv[INTF_MAX_ARGS]; /* converted arguments */
53 int i_argc; /* number of arguments */
54 int i_index; /* multi-purposes index */
55 int i_return; /* command return value */
57 intf_DbgMsg("command `%s'\n", psz_cmd);
59 /* Parse command line (separate arguments). If nothing has been found,
60 * the function returns without error */
61 i_argc = ParseCommandArguments( psz_argv, psz_cmd );
67 /* Find command. Command is always the first token on the line */
69 control_command[i_index].psz_name && strcmp( psz_argv[0], control_command[i_index].psz_name );
74 if( !control_command[i_index].psz_name ) /* unknown command */
76 /* Print error message */
77 intf_IntfMsg( "error: unknown command `%s'. Try `help'", psz_argv[0] );
78 return( INTF_USAGE_ERROR );
81 /* Check arguments validity */
82 if( CheckCommandArguments( argv, i_argc, psz_argv, control_command[i_index].psz_format ) )
84 /* The given arguments does not match the format string. An error message has
85 * already been displayed, so only the usage string is printed */
86 intf_IntfMsg( "usage: %s", control_command[i_index].psz_usage );
87 return( INTF_USAGE_ERROR );
91 i_return = control_command[i_index].function( i_argc, argv );
93 /* Manage special error codes */
96 case INTF_FATAL_ERROR: /* fatal error */
97 /* Print message and terminates the interface thread */
98 intf_ErrMsg( "fatal error in command `%s'\n", psz_argv[0] );
99 p_main->p_intf->b_die = 1;
102 case INTF_CRITICAL_ERROR: /* critical error */
103 /* Print message, flush messages queue and exit. Note that this
104 * error should be very rare since it does not even try to cancel other
106 intf_ErrMsg("critical error in command `%s'. Please report this error !\n", psz_argv[0] );
108 exit( INTF_CRITICAL_ERROR );
111 case INTF_USAGE_ERROR: /* usage error */
112 /* Print error message and usage */
113 intf_IntfMsg( "usage: %s", control_command[i_index].psz_usage );
117 /* Return error code */
121 /*****************************************************************************
122 * intf_ExecScript: parse and execute a command script
123 *****************************************************************************
124 * This function, based on ExecCommand read a file and tries to execute each
125 * of its line as a command. It returns 0 if everything succeeded, a negative
126 * number if the script could not be executed and a positive one if an error
127 * occured during execution.
128 *****************************************************************************/
129 int intf_ExecScript( char *psz_filename )
131 FILE * p_file; /* file */
132 char psz_line[INTF_MAX_CMD_SIZE]; /* line */
133 char * psz_index; /* index in string */
134 int i_err; /* error indicator */
138 p_file = fopen( psz_filename, "r" );
141 intf_ErrMsg("warning: %s: %s\n", psz_filename, strerror(errno));
145 /* For each line: read and execute */
146 while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
148 /* If line begins with a '#', it is a comment and shoule be ignored,
149 * else, execute it */
150 if( psz_line[0] != '#' )
152 /* The final '\n' needs to be removed before execution */
153 for( psz_index = psz_line; *psz_index && (*psz_index != '\n'); psz_index++ )
157 if( *psz_index == '\n' )
162 /* Execute command */
163 i_err |= intf_ExecCommand( psz_line );
166 if( !feof( p_file ) )
168 intf_ErrMsg("error: %s: %s\n", psz_filename, strerror(errno));
174 return( i_err != 0 );
177 /* following functions are local */
179 /*****************************************************************************
180 * ParseCommandArguments: isolate arguments in a command line
181 *****************************************************************************
182 * This function modify the original command line, adding '\0' and completes
183 * an array of pointers to beginning of arguments. It return the number of
185 *****************************************************************************/
186 static int ParseCommandArguments( char *psz_argv[INTF_MAX_ARGS], char *psz_cmd )
188 int i_argc; /* number of arguments */
189 char * psz_index; /* index */
190 boolean_t b_block; /* block (argument) indicator */
192 /* Initialize parser state */
193 b_block = 0; /* we start outside a block to remove spaces at beginning */
196 /* Go through command until end has been reached or maximal number of
197 * arguments has been reached */
198 for( psz_index = psz_cmd; *psz_index && (i_argc < INTF_MAX_ARGS); psz_index++ )
200 /* Inside a block, end of blocks are marked by spaces */
203 if( *psz_index == ' ' )
205 *psz_index = '\0'; /* mark the end of argument */
206 b_block = 0; /* exit the block */
210 /* Outside a block, beginning of blocks are marked by any character
211 * different from space */
214 if( *psz_index != ' ' )
216 psz_argv[i_argc++] = psz_index; /* store argument */
217 b_block = 1; /* enter the block */
222 /* Return number of arguments found */
226 /*****************************************************************************
227 * CheckCommandArguments: check arguments agains format
228 *****************************************************************************
229 * This function parse each argument and tries to find a match in the format
230 * string. It fills the argv array.
231 * If all arguments have been sucessfuly identified and converted, it returns
232 * 0, else, an error message is issued and non 0 is returned.
233 * Note that no memory is allocated by this function, but that the arguments
235 *****************************************************************************/
236 static int CheckCommandArguments( intf_arg_t argv[INTF_MAX_ARGS], int i_argc,
237 char *psz_argv[INTF_MAX_ARGS], char *psz_format )
239 intf_arg_t format[INTF_MAX_ARGS]; /* parsed format indicators */
240 int i_arg; /* argument index */
241 int i_format; /* format index */
242 char * psz_index; /* string index */
243 char * psz_cmp_index; /* string comparaison index */
244 int i_index; /* generic index */
245 boolean_t b_found; /* `argument found' flag */
248 /* Build format array */
249 ParseFormatString( format, psz_format );
251 /* Initialize parser: i_format must be the first non named formatter */
252 for( i_format = 0; ( i_format < INTF_MAX_ARGS )
253 && (format[i_format].i_flags & INTF_NAMED_ARG);
259 /* Scan all arguments */
260 for( i_arg = 1; i_arg < i_argc; i_arg++ )
264 /* Test if argument can be taken as a named argument: try to find a
265 * '=' in the string */
266 for( psz_index = psz_argv[i_arg]; *psz_index && ( *psz_index != '=' ); psz_index++ )
270 if( *psz_index == '=' ) /* '=' found */
272 /* Browse all named arguments to check if there is one matching */
273 for( i_index = 0; (i_index < INTF_MAX_ARGS)
274 && ( format[i_index].i_flags & INTF_NAMED_ARG )
278 /* Current format string is named... compare start of two
279 * names. A local inline ntation of a strcmp is used since
280 * string isn't ended by '\0' but by '=' */
281 for( psz_index = psz_argv[i_arg], psz_cmp_index = format[i_index].ps_name;
282 (*psz_index == *psz_cmp_index) && (*psz_index != '=') && (*psz_cmp_index != '=');
283 psz_index++, psz_cmp_index++ )
287 if( *psz_index == *psz_cmp_index ) /* the names match */
289 /* The argument is a named argument which name match the
290 * named argument i_index. To be valid, the argument should
291 * not have been already encountered and the type must
292 * match. Before going further, the '=' is replaced by
296 /* Check unicity. If the argument has already been encountered,
297 * print an error message and return. */
298 if( format[i_index].i_flags & INTF_PRESENT_ARG )/* present */
300 intf_IntfMsg("error: `%s' has already been encountered", psz_argv[i_arg] );
304 /* Register argument and prepare exit */
306 format[i_index].i_flags |= INTF_PRESENT_ARG;
307 argv[i_arg].i_flags = INTF_NAMED_ARG;
308 argv[i_arg].i_index = i_index;
309 argv[i_arg].ps_name = psz_argv[i_arg];
311 /* Check type and store value */
313 if( ConvertArgument( &argv[i_arg], format[i_index].i_flags, psz_index ) )
315 /* An error occured during conversion */
316 intf_IntfMsg( "error: invalid type for `%s'", psz_index );
322 /* If argument is not a named argument, the format string will
323 * be browsed starting from last position until the argument is
324 * found or an error occurs. */
327 /* Reset type indicator */
328 argv[i_arg].i_flags = 0;
330 /* If argument is not a named argument, the format string will
331 * be browsed starting from last position until the argument is
332 * found, an error occurs or the last format argument is
334 while( !b_found && (i_format < INTF_MAX_ARGS) && format[i_format].i_flags )
336 /* Try to convert argument */
337 if( !ConvertArgument( &argv[i_arg], format[i_format].i_flags, psz_argv[i_arg] ) )
339 /* Matching format has been found */
341 format[i_format].i_flags |= INTF_PRESENT_ARG;
342 argv[i_arg].i_index = i_format;
344 /* If argument is repeatable, dot not increase format counter */
345 if( !(format[i_format].i_flags & INTF_REP_ARG) )
352 /* Argument does not match format. This can be an error, or
353 * just a missing optionnal parameter, or the end of a
354 * repeated argument */
355 if( (format[i_format].i_flags & INTF_OPT_ARG)
356 || (format[i_format].i_flags & INTF_PRESENT_ARG) )
358 /* This is not an error */
363 /* The present format argument is mandatory and does
364 * not match the argument */
365 intf_IntfMsg("error: missing argument before `%s'", psz_argv[i_arg] );
372 /* If argument is not a named argument and hasn't been found in
373 * format string, then it is an usage error and the function can
377 intf_IntfMsg("error: `%s' does not match any argument", psz_argv[i_arg] );
381 intf_DbgMsg("argument flags=0x%x (index=%d) name=%s str=%s int=%d float=%f\n",
384 (argv[i_arg].i_flags & INTF_NAMED_ARG) ? argv[i_arg].ps_name : "NA",
385 (argv[i_arg].i_flags & INTF_STR_ARG) ? argv[i_arg].psz_str : "NA",
386 (argv[i_arg].i_flags & INTF_INT_ARG) ? argv[i_arg].i_num : 0,
387 (argv[i_arg].i_flags & INTF_FLOAT_ARG) ? argv[i_arg].f_num : 0);
390 /* Parse all remaining format specifier to verify they are all optionnal */
391 for( ; (i_format < INTF_MAX_ARGS) && format[i_format].i_flags ; i_format++ )
393 if( !(( format[i_format].i_flags & INTF_OPT_ARG)
394 || ( format[i_format].i_flags & INTF_PRESENT_ARG)) )
396 /* Format has not been used and is neither optionnal nor multiple
398 intf_IntfMsg("error: missing argument(s)\n");
403 /* If an error occured, the function already exited, so if this point is
404 * reached, everything is fine */
408 /*****************************************************************************
409 * ConvertArgument: try to convert an argument to a given type
410 *****************************************************************************
411 * This function tries to convert the string argument given in psz_str to
412 * a type specified in i_flags. It updates p_arg and returns O on success,
413 * or 1 on error. No error message is issued.
414 *****************************************************************************/
415 static int ConvertArgument( intf_arg_t *p_arg, int i_flags, char *psz_str )
417 char *psz_end; /* end pointer for conversion functions */
419 if( i_flags & INTF_STR_ARG ) /* string */
421 /* A conversion from a string to a string will always succeed... */
422 p_arg->psz_str = psz_str;
423 p_arg->i_flags |= INTF_STR_ARG;
425 else if( i_flags & INTF_INT_ARG ) /* integer */
427 p_arg->i_num = strtol( psz_str, &psz_end, 0 ); /* convert string */
428 /* If the conversion failed, return 1 and do not modify argument
429 * flags. Else, add 'int' flag and continue. */
430 if( !*psz_str || *psz_end )
434 p_arg->i_flags |= INTF_INT_ARG;
436 else if( i_flags & INTF_FLOAT_ARG ) /* float */
438 p_arg->f_num = strtod( psz_str, &psz_end ); /* convert string */
439 /* If the conversion failed, return 1 and do not modify argument
440 * flags. Else, add 'float' flag and continue. */
441 if( !*psz_str || *psz_end )
445 p_arg->i_flags |= INTF_FLOAT_ARG;
448 else /* error: missing type specifier */
450 intf_ErrMsg("error: missing type specifier for `%s' (0x%x)\n", psz_str, i_flags);
458 /*****************************************************************************
459 * ParseFormatString: parse a format string (ok ?)
460 *****************************************************************************
461 * This function read a format string, as specified in the control_command
462 * array, and fill a format array, to allow easier argument identification.
463 * Note that no memory is allocated by this function, but that, in a named
464 * argument, the name field does not end with a '\0' but with an '='.
465 * See command.h for format string specifications.
466 * Note that this function is designed to be efficient, not to check everything
467 * in a format string, which should be entered by a developper and therefore
468 * should be correct (TRUST !).
469 *****************************************************************************/
470 static void ParseFormatString( intf_arg_t format[INTF_MAX_ARGS], char *psz_format )
472 char * psz_index; /* format string index */
473 char * psz_start; /* argument format start */
474 char * psz_item; /* item index */
475 int i_index; /* format index */
477 /* Initialize parser */
479 psz_start = psz_format;
481 /* Reset first format indicator */
482 format[ 0 ].i_flags = 0;
484 /* Parse format string */
485 for( psz_index = psz_format; *psz_index && (i_index < INTF_MAX_ARGS) ; psz_index++ )
487 /* A space is always an item terminator */
488 if( *psz_index == ' ' )
490 /* Parse format item. Items are parsed from end to beginning or to
492 for( psz_item = psz_index - 1;
493 (psz_item >= psz_start) && !( format[i_index].i_flags & INTF_NAMED_ARG);
498 case 's': /* string */
499 format[i_index].i_flags |= INTF_STR_ARG;
501 case 'i': /* integer */
502 format[i_index].i_flags |= INTF_INT_ARG;
504 case 'f': /* float */
505 format[i_index].i_flags |= INTF_FLOAT_ARG;
507 case '*': /* can be repeated */
508 format[i_index].i_flags |= INTF_REP_ARG;
510 case '?': /* optionnal argument */
511 format[i_index].i_flags |= INTF_OPT_ARG;
513 case '=': /* name argument */
514 format[i_index].i_flags |= INTF_NAMED_ARG;
515 format[i_index].ps_name = psz_start;
518 default:/* error which should never happen: incorrect format */
519 intf_DbgMsg("error: incorrect format string `%s'\n", psz_format);
525 /* Mark next item start, increase items counter and reset next
526 * format indicator, if it wasn't the last one. */
528 psz_start = psz_index + 1;
529 if( i_index != INTF_MAX_ARGS ) /* end of array not reached */
531 format[ i_index ].i_flags = 0;