Loading lib_util/cmdln_parser.c +22 −33 Original line number Diff line number Diff line Loading @@ -38,7 +38,8 @@ #include <string.h> #define MAX_SUPPORTED_OPTS ( 1024 ) #define MAX_OPTION_LENGTH ( 32 ) #define MAX_OPTION_MATCH_LENGTH ( 30 ) #define MAX_OPTION_LENGTH ( MAX_OPTION_MATCH_LENGTH + 2 ) typedef CmdLnParser_Option OptionProps; Loading Loading @@ -85,14 +86,14 @@ static int16_t validateOptionProps( } /* Check match string length */ if ( strnlen( props.match, MAX_OPTION_LENGTH + 1 ) > MAX_OPTION_LENGTH ) if ( strnlen( props.match, MAX_OPTION_MATCH_LENGTH + 1 ) > MAX_OPTION_MATCH_LENGTH ) { fprintf( stderr, "[dev] Option with ID %d - match string exceeds limit of %d characters.\n", props.id, MAX_OPTION_LENGTH ); fprintf( stderr, "[dev] Option with ID %d - match string exceeds limit of %d characters.\n", props.id, MAX_OPTION_MATCH_LENGTH ); return -1; } if ( props.matchShort != NULL && strnlen( props.matchShort, MAX_OPTION_LENGTH + 1 ) > MAX_OPTION_LENGTH ) if ( props.matchShort != NULL && strnlen( props.matchShort, MAX_OPTION_MATCH_LENGTH + 1 ) > MAX_OPTION_MATCH_LENGTH ) { fprintf( stderr, "[dev] Option with ID %d - matchShort string exceeds limit of %d characters.\n", props.id, MAX_OPTION_LENGTH ); fprintf( stderr, "[dev] Option with ID %d - matchShort string exceeds limit of %d characters.\n", props.id, MAX_OPTION_MATCH_LENGTH ); return -1; } Loading Loading @@ -140,18 +141,6 @@ static int8_t stringLooksLikeOption( return 0; } // TODO: too lenient. Should strip one '-' for short and two '-' for match static const char *stringToOptionName( const char *str ) { while ( *str == '-' ) { ++str; } return str; } static int8_t optionMatchesString( Option opt, const char *str ) Loading @@ -161,29 +150,30 @@ static int8_t optionMatchesString( return 0; } const char *optionName = stringToOptionName( str ); if ( strnlen( str, MAX_OPTION_LENGTH + 1 ) > MAX_OPTION_LENGTH ) { /* String longer than longest possible option - not a match */ return 0; } char optionName_to_upper[MAX_OPTION_LENGTH + 1]; strncpy( optionName_to_upper, optionName, sizeof( optionName_to_upper ) - 1 ); optionName_to_upper[sizeof( optionName_to_upper ) - 1] = '\0'; to_upper( optionName_to_upper ); char str_to_upper[MAX_OPTION_LENGTH + 1]; snprintf( str_to_upper, sizeof( str_to_upper ), "%s", str ); to_upper( str_to_upper ); char match_to_upper[MAX_OPTION_LENGTH + 1]; strncpy( match_to_upper, opt.props.match, sizeof( match_to_upper ) - 1 ); optionName_to_upper[sizeof( match_to_upper ) - 1] = '\0'; snprintf( match_to_upper, sizeof( match_to_upper ), "--%s", opt.props.match ); to_upper( match_to_upper ); if ( strncmp( optionName_to_upper, match_to_upper, MAX_OPTION_LENGTH ) == 0 ) if ( strcmp( str_to_upper, match_to_upper ) == 0 ) { return 1; } if ( opt.props.matchShort != NULL ) { strncpy( match_to_upper, opt.props.matchShort, sizeof( match_to_upper ) - 1 ); optionName_to_upper[sizeof( match_to_upper ) - 1] = '\0'; snprintf( match_to_upper, sizeof( match_to_upper ), "-%s", opt.props.matchShort ); to_upper( match_to_upper ); if ( strncmp( optionName_to_upper, match_to_upper, MAX_OPTION_LENGTH ) == 0 ) if ( strcmp( str_to_upper, match_to_upper ) == 0 ) { return 1; } Loading Loading @@ -397,8 +387,7 @@ static void printOptions( const OptionProps *optionProps, const int32_t numOptions, const bool mandatory, const int32_t maxNumOptChars ) const int32_t maxNumOptChars ) { const int32_t descriptionColumnIdx = maxNumOptChars + 11 /* Additional chars we will print in the options column */; int32_t numOptChars; Loading Loading
lib_util/cmdln_parser.c +22 −33 Original line number Diff line number Diff line Loading @@ -38,7 +38,8 @@ #include <string.h> #define MAX_SUPPORTED_OPTS ( 1024 ) #define MAX_OPTION_LENGTH ( 32 ) #define MAX_OPTION_MATCH_LENGTH ( 30 ) #define MAX_OPTION_LENGTH ( MAX_OPTION_MATCH_LENGTH + 2 ) typedef CmdLnParser_Option OptionProps; Loading Loading @@ -85,14 +86,14 @@ static int16_t validateOptionProps( } /* Check match string length */ if ( strnlen( props.match, MAX_OPTION_LENGTH + 1 ) > MAX_OPTION_LENGTH ) if ( strnlen( props.match, MAX_OPTION_MATCH_LENGTH + 1 ) > MAX_OPTION_MATCH_LENGTH ) { fprintf( stderr, "[dev] Option with ID %d - match string exceeds limit of %d characters.\n", props.id, MAX_OPTION_LENGTH ); fprintf( stderr, "[dev] Option with ID %d - match string exceeds limit of %d characters.\n", props.id, MAX_OPTION_MATCH_LENGTH ); return -1; } if ( props.matchShort != NULL && strnlen( props.matchShort, MAX_OPTION_LENGTH + 1 ) > MAX_OPTION_LENGTH ) if ( props.matchShort != NULL && strnlen( props.matchShort, MAX_OPTION_MATCH_LENGTH + 1 ) > MAX_OPTION_MATCH_LENGTH ) { fprintf( stderr, "[dev] Option with ID %d - matchShort string exceeds limit of %d characters.\n", props.id, MAX_OPTION_LENGTH ); fprintf( stderr, "[dev] Option with ID %d - matchShort string exceeds limit of %d characters.\n", props.id, MAX_OPTION_MATCH_LENGTH ); return -1; } Loading Loading @@ -140,18 +141,6 @@ static int8_t stringLooksLikeOption( return 0; } // TODO: too lenient. Should strip one '-' for short and two '-' for match static const char *stringToOptionName( const char *str ) { while ( *str == '-' ) { ++str; } return str; } static int8_t optionMatchesString( Option opt, const char *str ) Loading @@ -161,29 +150,30 @@ static int8_t optionMatchesString( return 0; } const char *optionName = stringToOptionName( str ); if ( strnlen( str, MAX_OPTION_LENGTH + 1 ) > MAX_OPTION_LENGTH ) { /* String longer than longest possible option - not a match */ return 0; } char optionName_to_upper[MAX_OPTION_LENGTH + 1]; strncpy( optionName_to_upper, optionName, sizeof( optionName_to_upper ) - 1 ); optionName_to_upper[sizeof( optionName_to_upper ) - 1] = '\0'; to_upper( optionName_to_upper ); char str_to_upper[MAX_OPTION_LENGTH + 1]; snprintf( str_to_upper, sizeof( str_to_upper ), "%s", str ); to_upper( str_to_upper ); char match_to_upper[MAX_OPTION_LENGTH + 1]; strncpy( match_to_upper, opt.props.match, sizeof( match_to_upper ) - 1 ); optionName_to_upper[sizeof( match_to_upper ) - 1] = '\0'; snprintf( match_to_upper, sizeof( match_to_upper ), "--%s", opt.props.match ); to_upper( match_to_upper ); if ( strncmp( optionName_to_upper, match_to_upper, MAX_OPTION_LENGTH ) == 0 ) if ( strcmp( str_to_upper, match_to_upper ) == 0 ) { return 1; } if ( opt.props.matchShort != NULL ) { strncpy( match_to_upper, opt.props.matchShort, sizeof( match_to_upper ) - 1 ); optionName_to_upper[sizeof( match_to_upper ) - 1] = '\0'; snprintf( match_to_upper, sizeof( match_to_upper ), "-%s", opt.props.matchShort ); to_upper( match_to_upper ); if ( strncmp( optionName_to_upper, match_to_upper, MAX_OPTION_LENGTH ) == 0 ) if ( strcmp( str_to_upper, match_to_upper ) == 0 ) { return 1; } Loading Loading @@ -397,8 +387,7 @@ static void printOptions( const OptionProps *optionProps, const int32_t numOptions, const bool mandatory, const int32_t maxNumOptChars ) const int32_t maxNumOptChars ) { const int32_t descriptionColumnIdx = maxNumOptChars + 11 /* Additional chars we will print in the options column */; int32_t numOptChars; Loading