some simplification and better error handling

pull/617/head
Brad House 1 year ago
parent 0bc2eadecd
commit 0fc2c947a6
  1. 87
      src/tools/adig.c
  2. 63
      src/tools/ares_getopt.c

@ -152,6 +152,7 @@ static ares_bool_t read_cmdline(int argc, const char **argv, adig_config_t *conf
int c;
ares_getopt_init(&state, argc, argv);
state.opterr = 0;
while ((c = ares_getopt(&state, "dh?f:s:c:t:T:U:")) != -1) {
int f;
@ -164,7 +165,6 @@ static ares_bool_t read_cmdline(int argc, const char **argv, adig_config_t *conf
break;
case 'h':
case '?':
config->is_help = ARES_TRUE;
return ARES_TRUE;
@ -228,8 +228,13 @@ static ares_bool_t read_cmdline(int argc, const char **argv, adig_config_t *conf
config->optmask |= ARES_OPT_UDP_PORT;
break;
case ':':
snprintf(config->error, sizeof(config->error), "%c requires an argument", state.optopt);
return ARES_FALSE;
case '?':
default:
snprintf(config->error, sizeof(config->error), "unrecognized option %c", c);
snprintf(config->error, sizeof(config->error), "unrecognized option: %c", state.optopt);
return ARES_FALSE;
}
}
@ -821,6 +826,45 @@ done:
return status;
}
static int event_loop(ares_channel_t *channel)
{
while (1) {
fd_set read_fds;
fd_set write_fds;
int nfds;
struct timeval tv;
struct timeval *tvp;
int count;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
memset(&tv, 0, sizeof(tv));
nfds = ares_fds(channel, &read_fds, &write_fds);
if (nfds == 0) {
break;
}
tvp = ares_timeout(channel, NULL, &tv);
if (tvp == NULL) {
break;
}
count = select(nfds, &read_fds, &write_fds, NULL, tvp);
if (count < 0) {
#ifdef USE_WINSOCK
int err = WSAGetLastError();
#else
int err = errno;
#endif
if (err != EAGAIN && err != EINTR) {
fprintf(stderr, "select fail: %d", err);
return 1;
}
}
ares_process(channel, &read_fds, &write_fds);
}
return 0;
}
int main(int argc, char **argv)
{
ares_channel_t *channel = NULL;
@ -845,7 +889,7 @@ int main(int argc, char **argv)
config.qclass = ARES_CLASS_IN;
config.qtype = ARES_REC_TYPE_A;
if (!read_cmdline(argc, (const char **)argv, &config)) {
printf("%s\n", config.error);
printf("\n** ERROR: %s\n\n", config.error);
print_help();
rv = 1;
goto done;
@ -892,41 +936,8 @@ int main(int argc, char **argv)
}
printf("\n");
while (1) {
fd_set read_fds;
fd_set write_fds;
int nfds;
struct timeval tv;
struct timeval *tvp;
int count;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
memset(&tv, 0, sizeof(tv));
nfds = ares_fds(channel, &read_fds, &write_fds);
if (nfds == 0) {
break;
}
tvp = ares_timeout(channel, NULL, &tv);
if (tvp == NULL) {
break;
}
count = select(nfds, &read_fds, &write_fds, NULL, tvp);
if (count < 0) {
#ifdef USE_WINSOCK
int err = WSAGetLastError();
#else
int err = errno;
#endif
if (err != EAGAIN && err != EINTR) {
fprintf(stderr, "select fail: %d", err);
rv = 1;
goto done;
}
}
ares_process(channel, &read_fds, &write_fds);
}
/* Process events */
rv = event_loop(channel);
done:
free_config(&config);

@ -67,57 +67,70 @@ int ares_getopt(ares_getopt_state_t *state, const char *ostr)
{
const char *oli; /* option letter list index */
if (!*state->place) { /* update scanning pointer */
if (state->optind >= state->argc || *(state->place = state->argv[state->optind]) != '-') {
state->place = EMSG;
return (EOF);
/* update scanning pointer */
if (!*state->place) {
if (state->optind >= state->argc) {
return -1;
}
if (state->place[1] && *++state->place == '-') { /* found "--" */
++state->optind;
state->place = EMSG;
return (EOF);
state->place = state->argv[state->optind];
if (*(state->place) != '-') {
return -1;
}
} /* option letter okay? */
if ((state->optopt = (int)*state->place++) == ':' ||
(oli = strchr(ostr, state->optopt)) == NULL) {
/*
* if the user didn't specify '-' as an option,
* assume it means EOF.
*/
if (state->optopt == '-') {
return (EOF);
state->place++;
/* found "--" */
if (*(state->place) == '-') {
state->optind++;
return -1;
}
if (!*state->place) {
/* Found just - */
if (!*(state->place)) {
state->optopt = 0;
return BADCH;
}
}
/* option letter okay? */
state->optopt = *(state->place);
state->place++;
oli = strchr(ostr, state->optopt);
if (oli == NULL) {
if (!(*state->place)) {
++state->optind;
}
if (state->opterr && *ostr != ':') {
if (state->opterr) {
(void)fprintf(stderr, "%s: illegal option -- %c\n", __FILE__, state->optopt);
}
return (BADCH);
return BADCH;
}
if (*++oli != ':') { /* don't need argument */
/* don't need argument */
if (*++oli != ':') {
state->optarg = NULL;
if (!*state->place) {
++state->optind;
}
} else { /* need an argument */
} else {
/* need an argument */
if (*state->place) { /* no white space */
state->optarg = state->place;
} else if (state->argc <= ++state->optind) { /* no arg */
state->place = EMSG;
if (*ostr == ':') {
return (BADARG);
return BADARG;
}
if (state->opterr) {
(void)fprintf(stderr, "%s: option requires an argument -- %c\n",
__FILE__, state->optopt);
}
return (BADCH);
return BADARG;
} else { /* white space */
state->optarg = state->argv[state->optind];
}
state->place = EMSG;
++state->optind;
}
return (state->optopt); /* dump back option letter */
return state->optopt; /* dump back option letter */
}

Loading…
Cancel
Save