static void bad_cla(const char* context, const char* cla, const char* msg) { ci_log("ERROR: bad %s option: %s", context, cla); if( msg ) ci_log("ERROR: %s", msg); ci_app_usage(0); exit(-1); /* we can't guarantee that the user's "usage" function does this */ }
int main(int argc, char* argv[]) { stack_ni_fn_t* do_stack_ofe = NULL; ci_app_usage = usage; cfg_lock = 0; /* don't lock when attaching */ ci_app_getopt("info | stats ... | command ...", &argc, argv, cfg_opts, N_CFG_OPTS); --argc; ++argv; cfg_stack_id = (ci_uint32)(-1); if( cfg_stack_name != NULL ) { char *str; cfg_stack_id = strtoul(cfg_stack_name, &str, 0); if( str[0] != '\0' ) cfg_stack_id = (ci_uint32)(-1); } if( argc == 0 ) { do_stack_ofe = do_ofe_info; } else if ( strcmp(argv[0], "info" ) == 0 ) { do_stack_ofe = do_ofe_info; if( argc != 1 ) ci_app_usage("Do not understand parameters to the \"info\" command"); } else if( strcmp(argv[0], "stats" ) == 0 ) { do_stack_ofe = do_ofe_stats; cfg_argv = argv + 1; cfg_argc = argc - 1; } else if( strcmp(argv[0], "command") == 0 || strcmp(argv[0], "cmd") == 0 ) { do_stack_ofe = do_ofe_command; cfg_argv = argv + 1; cfg_argc = argc - 1; } else ci_app_usage("info | stats ... | command ..."); CI_TRY(libstack_init(NULL)); list_all_stacks2(stackfilter_match_name, do_stack_ofe, NULL, NULL); return 0; }
static int parse_cfg_opt(int argc, char** argv, const char* context) { const ci_cfg_desc* a; const char* val = NULL; int result = 1; /* is it "-" ? */ if( argv[0][1] == 0 ) bad_cla(context, argv[0], "- is not allowed"); /* find the config descriptor */ a = 0; if( cfg_opts ) a = find_cfg_desc(argv[0], cfg_opts, n_cfg_opts, &val); if( (!a) && ci_app_standard_opts) { a = find_cfg_desc(argv[0], std_opts, N_STD_OPTS, &val); } if( !a ) bad_cla(context, argv[0], "unknown option"); /* the option value (if required) may be part of this arg or the next */ if( !val || *val == 0 ) { if( a->type == CI_CFG_FLAG || a->type == CI_CFG_USAGE || argc == 1 ) { val = 0; } else { val = argv[1]; result = 2; } } switch( a->type ) { case CI_CFG_FLAG: if( val ) { if( sscanf(val, "%d", (int*) a->value) != 1 ) bad_cla(context, argv[0], "expected integer or nothing"); } else ++(*(int*) a->value); break; case CI_CFG_INT: if( !val || sscanf(val, "%i", (int*) a->value) != 1 ) bad_cla(context, argv[0], "expected integer"); break; case CI_CFG_UINT: if( !val || sscanf(val, "%i", (int*) a->value) != 1 ) bad_cla(context, argv[0], "expected unsigned integer"); break; case CI_CFG_INT64: if( !val || sscanf(val, "%lli", (long long int*) a->value) != 1 ) bad_cla(context, argv[0], "expected 64bit integer"); break; case CI_CFG_UINT64: if( !val || sscanf(val, "%lli", (long long int*) a->value) != 1 ) bad_cla(context, argv[0], "expected unsigned 64bit integer"); break; case CI_CFG_STR: *(const char**) a->value = val ? val : ""; break; case CI_CFG_USAGE: ci_app_usage(0); break; case CI_CFG_FN: ci_assert(a->fn); a->fn(val, a); break; case CI_CFG_IRANGE: { int *v; v = (int*) a->value; if( sscanf(val, " %i - %i", v, v + 1) != 2 ) { if( sscanf(val, " %i", v) == 1 ) v[1] = v[0]; else bad_cla(context, argv[0], "expected integer or range"); } } break; default: ci_log("ci_app: unknown config option type %u", a->type); break; } return result; }