Skip to content

Commit

Permalink
Discard some private properies of cmdl_opt_t.
Browse files Browse the repository at this point in the history
All private props are only valid inside cmdlgrp_parse(),
They can't be used in cmdlgrp_print_reuslt() since optv[]
maybe resued as diff option groups or defined in local.
  • Loading branch information
gitgjogh committed Jan 31, 2017
1 parent 68c8bec commit e1a5e42
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 35 deletions.
56 changes: 26 additions & 30 deletions libsim/sim_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@

#include "sim_opt.h"

static int cmdl_init(cmdl_opt_t optv[]);
static int cmdl_check(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[]);
static int cmdlgrp_parse_init_check(cmdl_opt_t optv[]);
static int cmdlgrp_parse_end_check(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[]);
static int cmdl_parse_opt(cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t* opt);


void iof_cfg(ios_t *f, const char *path, const char *mode)
Expand Down Expand Up @@ -533,7 +534,15 @@ int cmdl_parse_int (cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t *
}
else if (act == CMDL_PRI_RESULT)
{
xprint("'%d'", *((int*)dst));
int ival = *((int*)dst);
xprint("'%d'", ival);
if (opt->enums) {
for (const cmdl_enum_t *pe = opt->enums; pe->name; ++pe) {
if (ival == pe->val) {
xprint(" (%%%s)", pe->name);
}
}
}
}
else if (act == CMDL_PRI_ARGFMT)
{
Expand Down Expand Up @@ -666,7 +675,9 @@ cmdl_opt_t* cmdl_set_enum(cmdl_opt_t optv[], const char *opt_name, const cmdl_en
return opt;
}

static
/**
* expand ref or enum for single option
*/
int cmdl_parse_opt(cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t* opt)
{
cmdl_iter_t _iter2;
Expand All @@ -684,13 +695,13 @@ int cmdl_parse_opt(cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t* opt
{
xdbg("expand ref or enum '%s'\n", arg);
if (opt->refs) {
opt->p_ref = sim_name_2_ref(opt->refs, &arg[1]);
if (opt->p_ref == 0) {
const cmdl_ref_t *p_ref = sim_name_2_ref(opt->refs, &arg[1]);
if (p_ref == 0) {
xerr("can't find -%s.%s as ref\n", opt->names, arg);
return CMDL_RET_ERROR;
}

strncpy(fieldBuf, opt->p_ref->val, 1024);
strncpy(fieldBuf, p_ref->val, 1024);
fieldCount = str_2_fields(fieldBuf, 255, fieldArr+1);
if (fieldCount>=255) {
xerr("strspl() overflow\n");
Expand All @@ -701,12 +712,13 @@ int cmdl_parse_opt(cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t* opt
iter2 = &_iter2;
iter2->layer = iter->layer;
} else if (opt->enums) {
opt->p_enum = sim_name_2_enum(opt->enums, &arg[1]);
if (opt->p_enum == 0) {
const cmdl_enum_t *p_enum = sim_name_2_enum(opt->enums, &arg[1]);
if (p_enum == 0) {
xerr("can't find -%s.%s as enum\n", opt->names, arg);
return CMDL_RET_ERROR;
}
*((int *)dst) = opt->p_enum->val;
*((int *)dst) = p_enum->val;
cmdl_iter_next(iter);
return 2;
} else {
xerr("can't expand -%s.%s neither as ref nor enum\n", opt->names, arg);
Expand All @@ -725,7 +737,7 @@ int cmdl_parse_opt(cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t* opt
return (arg && arg[0] == '%') ? 1 : r;
}

int cmdl_init(cmdl_opt_t optv[])
int cmdlgrp_parse_init_check(cmdl_opt_t optv[])
{
ENTER_FUNC();

Expand All @@ -744,10 +756,6 @@ int cmdl_init(cmdl_opt_t optv[])

// init
opt->n_parse = 0;
opt->argvIdx = -1;
opt->b_default = 0;
opt->p_ref = 0;
opt->p_enum = 0;
}

LEAVE_FUNC();
Expand All @@ -759,7 +767,7 @@ int cmdlgrp_parse(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[])
{
ENTER_FUNC();

int ret = cmdl_init(optv);
int ret = cmdlgrp_parse_init_check(optv);
if (ret < 0) {
xerr("invalid cmdl description defined (%d)\n\n", ret);
return CMDL_RET_ERROR_INIT;
Expand Down Expand Up @@ -798,7 +806,6 @@ int cmdlgrp_parse(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[])
}

++ p_opt->n_parse;
p_opt->argvIdx = cmdl_iter_idx(iter);

if (arg[1] == '%') {
ref_expand[0] = arg; /* use original opt */
Expand All @@ -815,7 +822,7 @@ int cmdlgrp_parse(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[])
-- iter->layer;

if (ret > CMDL_RET_HELP) {
ret = cmdl_check(iter, dst, optv);
ret = cmdlgrp_parse_end_check(iter, dst, optv);
if (ret < 0) {
xerr("cmdl_check() failed (%d)\n\n", ret);
return CMDL_RET_ERROR_CHECK;
Expand All @@ -830,7 +837,7 @@ int cmdlgrp_parse(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[])
/**
* @param [in] iter - just for layer trace
*/
int cmdl_check(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[])
int cmdlgrp_parse_end_check(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[])
{
int n_err = 0;
char *argv[2] = {0};
Expand Down Expand Up @@ -931,17 +938,6 @@ int cmdlgrp_print_result(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[])
{
xlprint(iter->layer, "-%s = ", opt->names);
opt->parse(iter, (char *)dst + opt->dst_offset, CMDL_PRI_RESULT, opt);

//FIXME: The following way to get ref.name or enum.name make no sense when:
// 1. optv[] was defined in local scope.
// 2. optv[] was reused by diff params.
if (opt->p_ref) {
xprint(" (%%%s)", opt->p_ref->name);
}

if (opt->p_enum) {
xprint(" (%%%s)", opt->p_enum->name);
}
xprint("\n");
}
-- iter->layer;
Expand Down
6 changes: 1 addition & 5 deletions libsim/sim_opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,8 @@ typedef struct cmdl_option_description {
const cmdl_ref_t* refs;
const cmdl_enum_t* enums;

///< private props:
///< private props only used inside cmdlgrp_parse():
int n_parse;
int argvIdx;
int b_default;
const cmdl_ref_t* p_ref;
const cmdl_enum_t* p_enum;
} cmdl_opt_t;

cmdl_opt_t* cmdl_ref_2_opt (cmdl_opt_t optv[], const char *ref_name);
Expand Down

0 comments on commit e1a5e42

Please sign in to comment.