refactor(option.c): factor out option name parsing

This commit is contained in:
Lewis Russell 2023-01-25 17:33:09 +00:00
parent 9a9129c60b
commit 2c601787ab

View File

@ -1192,18 +1192,18 @@ static int get_option_prefix(char **argp)
return 1;
}
static void do_set_option(int opt_flags, char **argp, bool *did_show, char *errbuf,
size_t errbuflen, char **errmsg)
/// @param[in] arg Pointer to start option name
/// @param[out] opt_idxp Option index in options[] table.
/// @param[out] keyp
/// @param[out] len Length of option name
/// @return FAIL if an error is detected, OK otherwise
static int parse_option_name(char *arg, int *keyp, int *lenp, int *opt_idxp)
{
// 1: nothing, 0: "no", 2: "inv" in front of name
int prefix = get_option_prefix(argp);
char *arg = *argp;
// find end of name
int key = 0;
int len;
int opt_idx;
if (*arg == '<') {
opt_idx = -1;
// look out for <t_>;>
@ -1216,8 +1216,7 @@ static void do_set_option(int opt_flags, char **argp, bool *did_show, char *errb
}
}
if (arg[len] != '>') {
*errmsg = e_invarg;
return;
return FAIL;
}
if (arg[1] == 't' && arg[2] == '_') { // could be term code
opt_idx = findoption_len((const char *)arg + 1, (size_t)(len - 1));
@ -1242,6 +1241,30 @@ static void do_set_option(int opt_flags, char **argp, bool *did_show, char *errb
}
}
*keyp = key;
*lenp = len;
*opt_idxp = opt_idx;
return OK;
}
static void do_set_option(int opt_flags, char **argp, bool *did_show, char *errbuf,
size_t errbuflen, char **errmsg)
{
// 1: nothing, 0: "no", 2: "inv" in front of name
int prefix = get_option_prefix(argp);
char *arg = *argp;
// find end of name
int key = 0;
int len;
int opt_idx;
if (parse_option_name(arg, &key, &len, &opt_idx) == FAIL) {
*errmsg = e_invarg;
return;
}
// remember character after option name
int afterchar = (uint8_t)arg[len];