📄 intf_cmd.c
字号:
/* Scan all arguments */ for( i_arg = 1; i_arg < i_argc; i_arg++ ) { b_found = 0; /* Test if argument can be taken as a named argument: try to find a * '=' in the string */ for( psz_index = psz_argv[i_arg]; *psz_index && ( *psz_index != '=' ); psz_index++ ) { ; } if( *psz_index == '=' ) /* '=' found */ { /* Browse all named arguments to check if there is one matching */ for( i_index = 0; (i_index < INTF_MAX_ARGS) && ( format[i_index].i_flags & INTF_NAMED_ARG ) && !b_found; i_index++ ) { /* Current format string is named... compare start of two * names. A local inline ntation of a strcmp is used since * string isn't ended by '\0' but by '=' */ for( psz_index = psz_argv[i_arg], psz_cmp_index = format[i_index].ps_name; (*psz_index == *psz_cmp_index) && (*psz_index != '=') && (*psz_cmp_index != '='); psz_index++, psz_cmp_index++ ) { ; } if( *psz_index == *psz_cmp_index ) /* the names match */ { /* The argument is a named argument which name match the * named argument i_index. To be valid, the argument * should not have been already encountered and the type * must match. Before going further, the '=' is replaced * by a '\0'. */ *psz_index = '\0'; /* Check unicity. If the argument has already been * encountered, print an error message and return. */ if( format[i_index].i_flags & INTF_PRESENT_ARG ) { intf_IntfMsg( "error: `%s' has already been " "encountered", psz_argv[i_arg] ); return( 1 ); } /* Register argument and prepare exit */ b_found = 1; format[i_index].i_flags |= INTF_PRESENT_ARG; argv[i_arg].i_flags = INTF_NAMED_ARG; argv[i_arg].i_index = i_index; argv[i_arg].ps_name = psz_argv[i_arg]; /* Check type and store value */ psz_index++; if( ConvertArgument( &argv[i_arg], format[i_index].i_flags, psz_index ) ) { /* An error occured during conversion */ intf_IntfMsg( "error: invalid type for `%s'", psz_index ); } } } } /* If argument is not a named argument, the format string will * be browsed starting from last position until the argument is * found or an error occurs. */ if( !b_found ) { /* Reset type indicator */ argv[i_arg].i_flags = 0; /* If argument is not a named argument, the format string will * be browsed starting from last position until the argument is * found, an error occurs or the last format argument is * reached */ while( !b_found && (i_format < INTF_MAX_ARGS) && format[i_format].i_flags ) { /* Try to convert argument */ if( !ConvertArgument( &argv[i_arg], format[i_format].i_flags, psz_argv[i_arg] ) ) { /* Matching format has been found */ b_found = 1; format[i_format].i_flags |= INTF_PRESENT_ARG; argv[i_arg].i_index = i_format; /* If argument is repeatable, dot not increase format * counter */ if( !(format[i_format].i_flags & INTF_REP_ARG) ) { i_format++; } } else { /* Argument does not match format. This can be an error, * or just a missing optionnal parameter, or the end of a * repeated argument */ if( (format[i_format].i_flags & INTF_OPT_ARG) || (format[i_format].i_flags & INTF_PRESENT_ARG) ) { /* This is not an error */ i_format++; } else { /* The present format argument is mandatory and does * not match the argument */ intf_IntfMsg( "error: missing argument before `%s'", psz_argv[i_arg] ); return( 1 ); } } } } /* If argument is not a named argument and hasn't been found in * format string, then it is an usage error and the function can * return */ if( !b_found ) { intf_IntfMsg( "error: `%s' does not match any argument", psz_argv[i_arg] ); return( 1 ); } intf_DbgMsg( "argument flags=0x%x (index=%d) name=%s str=%s int=%d float=%f\n", argv[i_arg].i_flags, argv[i_arg].i_index, (argv[i_arg].i_flags & INTF_NAMED_ARG) ? argv[i_arg].ps_name : "NA", (argv[i_arg].i_flags & INTF_STR_ARG) ? argv[i_arg].psz_str : "NA", (argv[i_arg].i_flags & INTF_INT_ARG) ? argv[i_arg].i_num : 0, (argv[i_arg].i_flags & INTF_FLOAT_ARG) ? argv[i_arg].f_num : 0 ); } /* Parse all remaining format specifier to verify they are all optionnal */ for( ; (i_format < INTF_MAX_ARGS) && format[i_format].i_flags ; i_format++ ) { if( !(( format[i_format].i_flags & INTF_OPT_ARG) || ( format[i_format].i_flags & INTF_PRESENT_ARG)) ) { /* Format has not been used and is neither optionnal nor multiple * and present */ intf_IntfMsg("error: missing argument(s)\n"); return( 1 ); } } /* If an error occured, the function already exited, so if this point is * reached, everything is fine */ return( 0 );}/***************************************************************************** * ConvertArgument: try to convert an argument to a given type ***************************************************************************** * This function tries to convert the string argument given in psz_str to * a type specified in i_flags. It updates p_arg and returns O on success, * or 1 on error. No error message is issued. *****************************************************************************/static int ConvertArgument( intf_arg_t *p_arg, int i_flags, char *psz_str ){ char *psz_end; /* end pointer for conversion functions */ if( i_flags & INTF_STR_ARG ) /* string */ { /* A conversion from a string to a string will always succeed... */ p_arg->psz_str = psz_str; p_arg->i_flags |= INTF_STR_ARG; } else if( i_flags & INTF_INT_ARG ) /* integer */ { p_arg->i_num = strtol( psz_str, &psz_end, 0 ); /* convert string */ /* If the conversion failed, return 1 and do not modify argument * flags. Else, add 'int' flag and continue. */ if( !*psz_str || *psz_end ) { return( 1 ); } p_arg->i_flags |= INTF_INT_ARG; } else if( i_flags & INTF_FLOAT_ARG ) /* float */ { p_arg->f_num = strtod( psz_str, &psz_end ); /* convert string */ /* If the conversion failed, return 1 and do not modify argument * flags. Else, add 'float' flag and continue. */ if( !*psz_str || *psz_end ) { return( 1 ); } p_arg->i_flags |= INTF_FLOAT_ARG; }#ifdef DEBUG else /* error: missing type specifier */ { intf_ErrMsg("error: missing type specifier for `%s' (0x%x)\n", psz_str, i_flags); return( 1 ); }#endif return( 0 );}/***************************************************************************** * ParseFormatString: parse a format string (ok ?) ***************************************************************************** * This function read a format string, as specified in the control_command * array, and fill a format array, to allow easier argument identification. * Note that no memory is allocated by this function, but that, in a named * argument, the name field does not end with a '\0' but with an '='. * See command.h for format string specifications. * Note that this function is designed to be efficient, not to check * everything in a format string, which should be entered by a developper * and therefore should be correct (TRUST !). *****************************************************************************/static void ParseFormatString( intf_arg_t format[INTF_MAX_ARGS], char *psz_format ){ char * psz_index; /* format string index */ char * psz_start; /* argument format start */ char * psz_item; /* item index */ int i_index; /* format index */ /* Initialize parser */ i_index = 0; psz_start = psz_format; /* Reset first format indicator */ format[ 0 ].i_flags = 0; /* Parse format string */ for( psz_index = psz_format; *psz_index && (i_index < INTF_MAX_ARGS) ; psz_index++ ) { /* A space is always an item terminator */ if( *psz_index == ' ' ) { /* Parse format item. Items are parsed from end to beginning or to * first '=' */ for( psz_item = psz_index - 1; (psz_item >= psz_start) && !( format[i_index].i_flags & INTF_NAMED_ARG); psz_item-- ) { switch( *psz_item ) { case 's': /* string */ format[i_index].i_flags |= INTF_STR_ARG; break; case 'i': /* integer */ format[i_index].i_flags |= INTF_INT_ARG; break; case 'f': /* float */ format[i_index].i_flags |= INTF_FLOAT_ARG; break; case '*': /* can be repeated */ format[i_index].i_flags |= INTF_REP_ARG; break; case '?': /* optionnal argument */ format[i_index].i_flags |= INTF_OPT_ARG; break; case '=': /* name argument */ format[i_index].i_flags |= INTF_NAMED_ARG; format[i_index].ps_name = psz_start; break;#ifdef DEBUG default:/* error which should never happen: incorrect format */ intf_DbgMsg("error: incorrect format string `%s'\n", psz_format); break;#endif } } /* Mark next item start, increase items counter and reset next * format indicator, if it wasn't the last one. */ i_index++; psz_start = psz_index + 1; if( i_index != INTF_MAX_ARGS ) /* end of array not reached */ { format[ i_index ].i_flags = 0; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -