diff options
author | Gavin Howard <gavin@yzena.com> | 2021-07-20 23:05:24 -0600 |
---|---|---|
committer | Gavin Howard <gavin@yzena.com> | 2021-07-20 23:05:24 -0600 |
commit | 5d7bb465433313afbd20e0a618b366448c07eefa (patch) | |
tree | 727ec92e49c286d68a83a069d6a520e90f2a3be8 /src | |
parent | 77426e772eb2459ff592dc26d9e67da11cf1cccd (diff) | |
download | platform_external_bc-5d7bb465433313afbd20e0a618b366448c07eefa.tar.gz platform_external_bc-5d7bb465433313afbd20e0a618b366448c07eefa.tar.bz2 platform_external_bc-5d7bb465433313afbd20e0a618b366448c07eefa.zip |
Make sure short option is printed on error when used
Signed-off-by: Gavin Howard <gavin@yzena.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/opt.c | 38 |
1 files changed, 27 insertions, 11 deletions
@@ -78,12 +78,28 @@ static const char* bc_opt_longopt(const BcOptLong *longopts, int c) { /** * Issues a fatal error for an option parsing failure. - * @param err The error. - * @param c The character for the failing option. - * @param str Either the string for the failing option, or the invalid option. + * @param err The error. + * @param c The character for the failing option. + * @param str Either the string for the failing option, or the invalid + * option. + * @param use_short True if the short option should be used for error printing, + * false otherwise. */ -static void bc_opt_error(BcErr err, int c, const char *str) { - if (err == BC_ERR_FATAL_OPTION) bc_error(err, 0, str); +static void bc_opt_error(BcErr err, int c, const char *str, bool use_short) { + + if (err == BC_ERR_FATAL_OPTION) { + + if (use_short) { + + char short_str[2]; + + short_str[0] = (char) c; + short_str[1] = '\0'; + + bc_error(err, 0, short_str); + } + else bc_error(err, 0, str); + } else bc_error(err, 0, (int) c, str); } @@ -146,7 +162,7 @@ static int bc_opt_parseShort(BcOpt *o, const BcOptLong *longopts) { str[0] = option[0]; o->optind += 1; - bc_opt_error(BC_ERR_FATAL_OPTION, option[0], str); + bc_opt_error(BC_ERR_FATAL_OPTION, option[0], str, true); } } // Fallthrough. @@ -184,7 +200,7 @@ static int bc_opt_parseShort(BcOpt *o, const BcOptLong *longopts) { } // No argument, barf. else bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG, option[0], - bc_opt_longopt(longopts, option[0])); + bc_opt_longopt(longopts, option[0]), true); ret = (int) option[0]; @@ -289,13 +305,13 @@ int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) { if ((longopts[i].type == BC_OPT_BC_ONLY && BC_IS_DC) || (longopts[i].type == BC_OPT_DC_ONLY && BC_IS_BC)) { - bc_opt_error(BC_ERR_FATAL_OPTION, o->optopt, name); + bc_opt_error(BC_ERR_FATAL_OPTION, o->optopt, name, false); } // Error if we have an argument and should not. if (longopts[i].type == BC_OPT_NONE && arg != NULL) { - bc_opt_error(BC_ERR_FATAL_OPTION_ARG, o->optopt, name); + bc_opt_error(BC_ERR_FATAL_OPTION_ARG, o->optopt, name, false); } // Set the argument, or check the next argument if we don't have @@ -309,7 +325,7 @@ int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) { // All's good if it exists; otherwise, barf. if (o->optarg != NULL) o->optind += 1; else bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG, - o->optopt, name); + o->optopt, name, false); } return o->optopt; @@ -317,7 +333,7 @@ int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) { } // If we reach this point, the option is invalid. - bc_opt_error(BC_ERR_FATAL_OPTION, 0, option); + bc_opt_error(BC_ERR_FATAL_OPTION, 0, option, false); BC_UNREACHABLE |