uninstall:
for f in $(TARGETS); do rm -f $(bin_dir)/$$f; done
+$(ADH_OBJS): adnshost.h
+
adnshost: $(ADH_OBJS) $(srcdir)/../dynamic/$(SHLIBFILE)
$(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS)
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
+#include "adnshost.h"
void sysfail(const char *what, int errnoval) {
fprintf(stderr,"adnshost failed: %s: %s\n",what,strerror(errnoval));
exit(10);
}
+void usageerr(const char *fmt, ...) {
+ va_list al;
+ fputs("adnshost usage error: ",stderr);
+ va_start(al,fmt);
+ vfprintf(stderr,fmt,al);
+ va_end(al);
+ putc('\n',stderr);
+ exit(11);
+}
+
static void domain_do_arg(const char *domain) {
if (ov_pipe) usageerr("-f/--pipe not consistent with domains on command line");
- domain_do(arg);
+ domain_do(domain);
+}
+
+void *xmalloc(size_t sz) {
+ void *p;
+
+ p= malloc(sz); if (!p) sysfail("malloc",sz);
+ return p;
+}
+
+char *xstrsave(const char *str) {
+ char *p;
+
+ p= xmalloc(strlen(str)+1);
+ strcpy(p,str);
+ return p;
}
-static void of_type(const struct optinfo *oi, const char *arg) { abort(); }
+void of_type(const struct optioninfo *oi, const char *arg) { abort(); }
int main(int argc, const char *const *argv) {
const char *arg;
- const
+ const struct optioninfo *oip;
- while (arg= *++argv) {
- if (arg[0] != '-') {
+ while ((arg= *++argv)) {
+ if (arg[0] == '-') {
if (arg[1] == '-') {
oip= opt_findl(arg+2);
if (oip->type == ot_funcarg) {
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
+#include "adnshost.h"
+
int ov_env=1, ov_pipe=0, ov_asynch=0;
int ov_verbose= 0;
+adns_rrtype ov_type= adns_r_none;
int ov_search=0, ov_qc_query=0, ov_qc_anshost=0, ov_qc_cname=1;
-struct perqueryflags_remember ov_pqfr = { 1,1,1, tm_none };
int ov_tcp=0, ov_cname=0;
+char *ov_id= 0;
+struct perqueryflags_remember ov_pqfr = { 1,1,1, tm_none };
-static const struct optinfo global_options[]= {
+static const struct optioninfo global_options[]= {
{ ot_desconly, "global binary options:" },
{ ot_flag, "Do not look at environment variables at all",
"e", "env", &ov_env, 0 },
{ ot_end }
};
-static const struct optinfo perquery_options[]= {
+static const struct optioninfo perquery_options[]= {
{ ot_desconly, "per-query options:" },
{ ot_funcarg, "Query type (see below)",
"t", "type", 0,0, &of_type, "type" },
};
static void printusage(void) {
- static const struct optinfo *const all_optiontables[]= {
+ static const struct optioninfo *const all_optiontables[]= {
global_options, perquery_options, 0
};
- const struct optinfo *const *oiap, *oip=0;
+ const struct optioninfo *const *oiap, *oip=0;
int maxsopt, maxlopt, l;
maxsopt= maxlopt= 0;
- for (oiap=alloptions; *oiap; oiap++) {
+ for (oiap=all_optiontables; *oiap; oiap++) {
for (oip=*oiap; oip->type != ot_end; oip++) {
if (oip->type == ot_funcarg) continue;
if (oip->sopt) { l= strlen(oip->sopt); if (l>maxsopt) maxsopt= l; }
" adnshost [global-opts] [query-opts] -f|--pipe\n",
stdout);
- for (oiap=alloptions; *oiap; oiap++) {
+ for (oiap=all_optiontables; *oiap; oiap++) {
putchar('\n');
for (oip=*oiap; oip->type != ot_end; oip++) {
switch (oip->type) {
if (ferror(stdout)) sysfail("write usage message",errno);
}
-static void of_help(const struct optinfo *oi, const char *arg) {
+void of_help(const struct optioninfo *oi, const char *arg) {
printusage();
if (fclose(stdout)) sysfail("finish writing output",errno);
exit(0);
}
+typedef int comparer_type(const char **optp, const struct optioninfo *entry);
+
+static int oc_long(const char **optp, const struct optioninfo *entry) {
+ return entry->lopt && !strcmp(*optp,entry->lopt);
+}
+
+static int oc_short(const char **optp, const struct optioninfo *entry) {
+ const char *sopt;
+ int l;
+
+ sopt= entry->sopt;
+ if (!sopt) return 0;
+ l= strlen(sopt);
+ if (memcmp(*optp,sopt,l)) return 0;
+ (*optp) += l;
+ return 1;
+}
+
+static const struct optioninfo *find1(const char **optp,
+ const struct optioninfo *table,
+ comparer_type *comparer) {
+ for (;;) {
+ if (table->type == ot_end) return 0;
+ if (comparer(optp,table)) return table;
+ table++;
+ }
+}
+
+static const struct optioninfo *find(const char **optp,
+ const char *prefix,
+ comparer_type *comparer) {
+ const struct optioninfo *oip;
+
+ oip= find1(optp,perquery_options,comparer);
+ if (oip) return oip;
+ oip= find1(optp,global_options,comparer);
+ if (!oip) usageerr("unknown option %s%s",prefix,*optp);
+ if (ads) usageerr("global option %s%s specified after query domain(s)",prefix,*optp);
+ return oip;
+}
+
+const struct optioninfo *opt_findl(const char *opt) { return find(&opt,"--",oc_long); }
+const struct optioninfo *opt_finds(const char **optp) { return find(optp,"-",oc_short); }
+
+void opt_do(const struct optioninfo *oip, const char *arg) {
+ switch (oip->type) {
+ case ot_flag: case ot_value:
+ assert(!arg);
+ *oip->storep= oip->value;
+ return;
+ case ot_func: case ot_funcarg:
+ oip->func(oip,0);
+ return;
+ default:
+ abort();
+ }
+}
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
+#include "adnshost.h"
+
+adns_state ads;
+
struct query_node {
struct query_node *next, *back;
struct perqueryflags_remember pqfr;
adns_query qu;
};
-static adns_state ads;
static struct { struct query_node *head, *tail; } outstanding;
static unsigned long idcounter;
-static void domain_do(const char *domain) {
+void domain_do(const char *domain) {
struct query_node *qun;
char idbuf[20];
+ int r;
if (!ads) {
- if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) sysfail("ignore SIGPIPE");
+ if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) sysfail("ignore SIGPIPE",errno);
r= adns_init(&ads,
adns_if_noautosys|adns_if_nosigpipe |
- (ov_env ? 0 : adns_ifnoenv) |
+ (ov_env ? 0 : adns_if_noenv) |
ov_verbose,
0);
if (r) sysfail("adns_init",r);
qun->id= xstrsave(idbuf);
}
- r= adns_submit(ads, domain, type,
+ r= adns_submit(ads, domain,
+ ov_type == adns_r_none ? adns_r_addr : ov_type,
(ov_search ? adns_qf_search : 0) |
(ov_tcp ? adns_qf_usevc : 0) |
(ov_pqfr.show_owner ? adns_qf_owner : 0) |
&qun->qu);
if (r) sysfail("adns_submit",r);
- DLIST_LINK_TAIL(outstanding,qun);
+ LIST_LINK_TAIL(outstanding,qun);
}
+
+void of_asynch_id(const struct optioninfo *oi, const char *arg) { abort(); }
+void of_cancel_id(const struct optioninfo *oi, const char *arg) { abort(); }
#ifndef ADNSHOST_H_INCLUDED
#define ADNSHOST_H_INCLUDED
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <assert.h>
+
#include "config.h"
#include "adns.h"
+#include "dlist.h"
/* declarations related to option processing */
-struct optinfo;
-typedef void optfunc(const struct optinfo *oi, const char *arg);
+struct optioninfo;
+typedef void optfunc(const struct optioninfo *oi, const char *arg);
-struct optinfo {
+struct optioninfo {
enum oi_type {
ot_end, ot_desconly,
ot_flag, ot_value, ot_func, ot_funcarg
extern int ov_env, ov_pipe, ov_asynch;
extern int ov_verbose;
+extern adns_rrtype ov_type;
extern int ov_search, ov_qc_query, ov_qc_anshost, ov_qc_cname;
extern int ov_tcp, ov_cname;
extern char *ov_id;
extern optfunc of_help, of_type, of_asynch_id, of_cancel_id;
+const struct optioninfo *opt_findl(const char *opt);
+const struct optioninfo *opt_finds(const char **optp);
+void opt_do(const struct optioninfo *oip, const char *arg);
+
/* declarations related to query processing */
-static void of_asynch_id(const struct optinfo *oi, const char *arg) { abort(); }
-static void of_cancel_id(const struct optinfo *oi, const char *arg) { abort(); }
+extern adns_state ads;
+
+void domain_do(const char *domain);
+
+void of_asynch_id(const struct optioninfo *oi, const char *arg);
+void of_cancel_id(const struct optioninfo *oi, const char *arg);
/* declarations related to main program and useful utility functions */
void sysfail(const char *what, int errnoval) NONRETURNING;
+void usageerr(const char *what, ...) NONRETURNPRINTFFORMAT(1,2);
+
+void *xmalloc(size_t sz);
+char *xstrsave(const char *str);
#endif