chiark / gitweb /
adnshost improvements - now runs for at least one query.
[adns.git] / client / adh-main.c
index 380c1836e296a085bf774012f51740988181a9b0..33b1f59b9b2431337d753b7ca9643f4c0cc1ecbf 100644 (file)
  *  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);
+}
+
+void outerr(void) {
+  sysfail("write to stdout",errno);
+}
+
 static void domain_do_arg(const char *domain) {
   if (ov_pipe) usageerr("-f/--pipe not consistent with domains on command line");
-  domain_do(arg);
+  ensure_adns_init();
+  query_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 rcode;
+
+void setnonblock(int fd, int nonblock) { }
+
+static void read_query(void) { abort(); }
 
 int main(int argc, const char *const *argv) {
   const char *arg;
-  const 
+  const struct optioninfo *oip;
+  struct timeval *tv, tvbuf;
+  adns_query qu;
+  void *qun_v;
+  adns_answer *answer;
+  int r, maxfd;
+  fd_set readfds, writefds, exceptfds;
   
-  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) {
@@ -82,22 +122,36 @@ int main(int argc, const char *const *argv) {
     }
   }
 
-  if (ov_pipe) {
-    
-      if (ov_pipe) usageerr("-f/--pipe not 
-         
-       if (oip && ads) usageerr("global option %s must precede all query domains",arg);
-       if (!oip) oip= opt_findl(arg+2,perquery_options);
-       if (!oip) usageerr("unknown option %s",arg);
-       }
+  if (!ov_pipe && !ads) usageerr("no domains given, and -f/--pipe not used; try --help");
 
-       if (!oip && 
-       if (!oip) {
-  }
-  while (argv[1] && argv[1][0] == '-') {
-    if (argv[1][1] == '-') {
-      oip= findlong(
+  for (;;) {
+    for (;;) {
+      qu= ov_asynch ? 0 : outstanding.head ? outstanding.head->qu : 0;
+      r= adns_check(ads,&qu,&answer,&qun_v);
+      if (r == EAGAIN) break;
+      if (r == ESRCH) { if (!ov_pipe) goto x_quit; else break; }
+      assert(!r);
+      query_done(qun_v,answer);
+    }
+    maxfd= 0;
+    FD_ZERO(&readfds);
+    FD_ZERO(&writefds);
+    FD_ZERO(&exceptfds);
+    if (ov_pipe) {
+      maxfd= 1;
+      FD_SET(0,&readfds);
+    }
+    tv= 0;
+    adns_beforeselect(ads, &maxfd, &readfds,&writefds,&exceptfds, &tv,&tvbuf,0);
+    r= select(maxfd, &readfds,&writefds,&exceptfds, tv);
+    if (r == -1) {
+      if (errno == EINTR) continue;
+      sysfail("select",errno);
+    }
+    adns_afterselect(ads, maxfd, &readfds,&writefds,&exceptfds, 0);
+    if (ov_pipe && FD_ISSET(0,&readfds)) read_query();
   }
-  of_help(0,0);
-  abort();
+x_quit:
+  if (fclose(stdout)) outerr();
+  exit(rcode);
 }