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
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <errno.h> /* errno */
31 #include <stdio.h> /* FILE */
32 #include <stdlib.h> /* strtod(), strtol() */
33 #include <string.h> /* strerror() */
41 #include "interface.h"
44 #include "intf_ctrl.h"
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 );
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
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 )
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 */
75 intf_DbgMsg("command `%s'\n", psz_cmd);
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 );
85 /* Find command. Command is always the first token on the line */
87 control_command[i_index].psz_name && strcmp( psz_argv[0], control_command[i_index].psz_name );
92 if( !control_command[i_index].psz_name ) /* unknown command */
94 /* Print error message */
95 intf_IntfMsg( "error: unknown command `%s'. Try `help'", psz_argv[0] );
96 return( INTF_USAGE_ERROR );
99 /* Check arguments validity */
100 if( CheckCommandArguments( argv, i_argc, psz_argv, control_command[i_index].psz_format ) )
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 );
108 /* Execute command */
109 i_return = control_command[i_index].function( i_argc, argv );
111 /* Manage special error codes */
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;
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
124 intf_ErrMsg("critical error in command `%s'. Please report this error !\n", psz_argv[0] );
126 exit( INTF_CRITICAL_ERROR );
129 case INTF_USAGE_ERROR: /* usage error */
130 /* Print error message and usage */
131 intf_IntfMsg( "usage: %s", control_command[i_index].psz_usage );
135 /* Return error code */
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 )
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 */
156 p_file = fopen( psz_filename, "r" );
159 intf_ErrMsg("warning: %s: %s\n", psz_filename, strerror(errno));
163 /* For each line: read and execute */
164 while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
166 /* If line begins with a '#', it is a comment and shoule be ignored,
167 * else, execute it */
168 if( psz_line[0] != '#' )
170 /* The final '\n' needs to be removed before execution */
171 for( psz_index = psz_line; *psz_index && (*psz_index != '\n'); psz_index++ )
175 if( *psz_index == '\n' )
180 /* Execute command */
181 i_err |= intf_ExecCommand( psz_line );
184 if( !feof( p_file ) )
186 intf_ErrMsg("error: %s: %s\n", psz_filename, strerror(errno));
192 return( i_err != 0 );
195 /* following functions are local */
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
203 *****************************************************************************/
204 static int ParseCommandArguments( char *psz_argv[INTF_MAX_ARGS], char *psz_cmd )
206 int i_argc; /* number of arguments */
207 char * psz_index; /* index */
208 boolean_t b_block; /* block (argument) indicator */
210 /* Initialize parser state */
211 b_block = 0; /* we start outside a block to remove spaces at beginning */
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++ )
218 /* Inside a block, end of blocks are marked by spaces */
221 if( *psz_index == ' ' )
223 *psz_index = '\0'; /* mark the end of argument */
224 b_block = 0; /* exit the block */
228 /* Outside a block, beginning of blocks are marked by any character
229 * different from space */
232 if( *psz_index != ' ' )
234 psz_argv[i_argc++] = psz_index; /* store argument */
235 b_block = 1; /* enter the block */
240 /* Return number of arguments found */
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
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 )
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 */
266 /* Build format array */
267 ParseFormatString( format, psz_format );
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);
277 /* Scan all arguments */
278 for( i_arg = 1; i_arg < i_argc; i_arg++ )
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++ )
288 if( *psz_index == '=' ) /* '=' found */
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 )
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++ )
305 if( *psz_index == *psz_cmp_index ) /* the names match */
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
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 */
318 intf_IntfMsg("error: `%s' has already been encountered", psz_argv[i_arg] );
322 /* Register argument and prepare exit */
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];
329 /* Check type and store value */
331 if( ConvertArgument( &argv[i_arg], format[i_index].i_flags, psz_index ) )
333 /* An error occured during conversion */
334 intf_IntfMsg( "error: invalid type for `%s'", psz_index );
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. */
345 /* Reset type indicator */
346 argv[i_arg].i_flags = 0;
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
352 while( !b_found && (i_format < INTF_MAX_ARGS) && format[i_format].i_flags )
354 /* Try to convert argument */
355 if( !ConvertArgument( &argv[i_arg], format[i_format].i_flags, psz_argv[i_arg] ) )
357 /* Matching format has been found */
359 format[i_format].i_flags |= INTF_PRESENT_ARG;
360 argv[i_arg].i_index = i_format;
362 /* If argument is repeatable, dot not increase format counter */
363 if( !(format[i_format].i_flags & INTF_REP_ARG) )
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) )
376 /* This is not an error */
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] );
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
395 intf_IntfMsg("error: `%s' does not match any argument", psz_argv[i_arg] );
399 intf_DbgMsg("argument flags=0x%x (index=%d) name=%s str=%s int=%d float=%f\n",
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);
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++ )
411 if( !(( format[i_format].i_flags & INTF_OPT_ARG)
412 || ( format[i_format].i_flags & INTF_PRESENT_ARG)) )
414 /* Format has not been used and is neither optionnal nor multiple
416 intf_IntfMsg("error: missing argument(s)\n");
421 /* If an error occured, the function already exited, so if this point is
422 * reached, everything is fine */
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 )
435 char *psz_end; /* end pointer for conversion functions */
437 if( i_flags & INTF_STR_ARG ) /* string */
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;
443 else if( i_flags & INTF_INT_ARG ) /* integer */
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 )
452 p_arg->i_flags |= INTF_INT_ARG;
454 else if( i_flags & INTF_FLOAT_ARG ) /* float */
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 )
463 p_arg->i_flags |= INTF_FLOAT_ARG;
466 else /* error: missing type specifier */
468 intf_ErrMsg("error: missing type specifier for `%s' (0x%x)\n", psz_str, i_flags);
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 )
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 */
495 /* Initialize parser */
497 psz_start = psz_format;
499 /* Reset first format indicator */
500 format[ 0 ].i_flags = 0;
502 /* Parse format string */
503 for( psz_index = psz_format; *psz_index && (i_index < INTF_MAX_ARGS) ; psz_index++ )
505 /* A space is always an item terminator */
506 if( *psz_index == ' ' )
508 /* Parse format item. Items are parsed from end to beginning or to
510 for( psz_item = psz_index - 1;
511 (psz_item >= psz_start) && !( format[i_index].i_flags & INTF_NAMED_ARG);
516 case 's': /* string */
517 format[i_index].i_flags |= INTF_STR_ARG;
519 case 'i': /* integer */
520 format[i_index].i_flags |= INTF_INT_ARG;
522 case 'f': /* float */
523 format[i_index].i_flags |= INTF_FLOAT_ARG;
525 case '*': /* can be repeated */
526 format[i_index].i_flags |= INTF_REP_ARG;
528 case '?': /* optionnal argument */
529 format[i_index].i_flags |= INTF_OPT_ARG;
531 case '=': /* name argument */
532 format[i_index].i_flags |= INTF_NAMED_ARG;
533 format[i_index].ps_name = psz_start;
536 default:/* error which should never happen: incorrect format */
537 intf_DbgMsg("error: incorrect format string `%s'\n", psz_format);
543 /* Mark next item start, increase items counter and reset next
544 * format indicator, if it wasn't the last one. */
546 psz_start = psz_index + 1;
547 if( i_index != INTF_MAX_ARGS ) /* end of array not reached */
549 format[ i_index ].i_flags = 0;