From: ian Date: Sun, 10 Oct 1999 17:01:30 +0000 (+0000) Subject: adnshost compiles again, and submits. X-Git-Tag: privaterel-1999-10-12-fanf-dustman~12 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=adns.git;a=commitdiff_plain;h=e3f575ffc7bcb15d23f6844c7cf5562d2b93ac40;ds=sidebyside adnshost compiles again, and submits. --- diff --git a/client/Makefile.in b/client/Makefile.in index 0ed57ac..d55057e 100644 --- a/client/Makefile.in +++ b/client/Makefile.in @@ -43,6 +43,8 @@ install: $(TARG_INSTALL) 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) diff --git a/client/adh-main.c b/client/adh-main.c index 5a4235d..7c43d1e 100644 --- a/client/adh-main.c +++ b/client/adh-main.c @@ -26,28 +26,51 @@ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include -#include -#include +#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) { diff --git a/client/adh-opts.c b/client/adh-opts.c index 7c2c42b..bc32ba9 100644 --- a/client/adh-opts.c +++ b/client/adh-opts.c @@ -26,13 +26,17 @@ * 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 }, @@ -56,7 +60,7 @@ static const struct optinfo global_options[]= { { 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" }, @@ -105,16 +109,16 @@ static const struct optinfo perquery_options[]= { }; 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; } @@ -131,7 +135,7 @@ static void printusage(void) { " 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) { @@ -238,9 +242,66 @@ static void printusage(void) { 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(); + } +} diff --git a/client/adh-query.c b/client/adh-query.c index fa85779..5c023a6 100644 --- a/client/adh-query.c +++ b/client/adh-query.c @@ -26,6 +26,10 @@ * 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; @@ -33,20 +37,20 @@ struct query_node { 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); @@ -62,7 +66,8 @@ static void domain_do(const char *domain) { 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) | @@ -74,5 +79,8 @@ static void domain_do(const char *domain) { &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(); } diff --git a/client/adnshost.h b/client/adnshost.h index 46762f3..3e93ca7 100644 --- a/client/adnshost.h +++ b/client/adnshost.h @@ -28,15 +28,23 @@ #ifndef ADNSHOST_H_INCLUDED #define ADNSHOST_H_INCLUDED +#include +#include +#include +#include +#include +#include + #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 @@ -57,6 +65,7 @@ struct perqueryflags_remember { 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; @@ -64,13 +73,25 @@ extern struct perqueryflags_remember ov_pqfr; 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