X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=tr%2Ftrmon_main.c;h=b1a2a6355163f243832c04bcc764ebd71596aa82;hb=1ec949998ea6bc2c21e808346052b2e92d50479b;hp=761b43356e9d6a737de92e084d1c2f5a928f43a3;hpb=82e1d1116794e91485353f73febfa54ab5cf28b7;p=trust_router.git diff --git a/tr/trmon_main.c b/tr/trmon_main.c index 761b433..b1a2a63 100644 --- a/tr/trmon_main.c +++ b/tr/trmon_main.c @@ -39,6 +39,7 @@ #include #include +#include /* command-line option setup */ @@ -66,7 +67,7 @@ static const struct argp_option cmdline_options[] = { /* structure for communicating with option parser */ struct cmdline_args { char *server; - unsigned int port; + int port; MON_CMD command; MON_OPT_TYPE options[MAX_OPTIONS]; unsigned int n_options; @@ -75,7 +76,7 @@ struct cmdline_args { /* parser for individual options - fills in a struct cmdline_args */ static error_t parse_option(int key, char *arg, struct argp_state *state) { - long tmp_l = 0; + int err = 0; /* get a shorthand to the command line argument structure, part of state */ struct cmdline_args *arguments=state->input; @@ -84,7 +85,6 @@ static error_t parse_option(int key, char *arg, struct argp_state *state) case 'v': print_version_info(); exit(0); - break; case ARGP_KEY_ARG: /* handle argument (not option) */ switch (state->arg_num) { @@ -93,31 +93,40 @@ static error_t parse_option(int key, char *arg, struct argp_state *state) break; case 1: - tmp_l = strtol(arg, NULL, 10); - if (errno || (tmp_l < 0) || (tmp_l > 65535)) /* max valid port */ + arguments->port=tr_parse_port(arg); /* optional */ + if (arguments->port < 0) { + switch(-(arguments->port)) { + case ERANGE: + printf("\nError parsing port (%s): port must be an integer in the range 1 - 65535\n\n", arg); + break; + + default: + printf("\nError parsing port (%s): %s\n\n", arg, strerror(-arguments->port)); + break; + } argp_usage(state); - - arguments->port=(unsigned int) tmp_l; + } break; case 2: arguments->command=mon_cmd_from_string(arg); if (arguments->command == MON_CMD_UNKNOWN) { - printf("\nUnknown command '%s'\n", arg); - argp_usage(state); + printf("\nUnknown command '%s'\n\n", arg); + err = 1; } break; default: if (arguments->n_options >= MAX_OPTIONS) { - printf("\nToo many command options given, limit is %d\n", MAX_OPTIONS); - argp_usage(state); + printf("\nToo many command options given, limit is %d\n\n", MAX_OPTIONS); + err = 1; + break; } arguments->options[arguments->n_options] = mon_opt_type_from_string(arg); if (arguments->options[arguments->n_options] == OPT_TYPE_UNKNOWN) { - printf("\nUnknown command option '%s'\n", arg); - argp_usage(state); + printf("\nUnknown command option '%s'\n\n", arg); + err = 1; } arguments->n_options++; break; @@ -127,7 +136,7 @@ static error_t parse_option(int key, char *arg, struct argp_state *state) case ARGP_KEY_END: /* no more arguments */ if (state->arg_num < 3) { /* not enough arguments encountered */ - argp_usage(state); + err = 1; } break; @@ -135,6 +144,11 @@ static error_t parse_option(int key, char *arg, struct argp_state *state) return ARGP_ERR_UNKNOWN; } + if (err) { + argp_usage(state); + return EINVAL; /* argp_usage() usually does not return, but just in case */ + } + return 0; /* success */ }