+ } else { /* arg[0] != '-' */
+ query_do(arg);
+ }
+}
+
+static void read_stdin(void) {
+ int anydone, r;
+ char *newline, *space;
+
+ anydone= 0;
+ while (!anydone || used) {
+ while (!(newline= memchr(buf,'\n',used))) {
+ if (used == avail) {
+ avail += 20; avail <<= 1;
+ buf= realloc(buf,avail);
+ if (!buf) sysfail("realloc stdin buffer",errno);
+ }
+ do {
+ r= read(0,buf+used,avail-used);
+ } while (r < 0 && errno == EINTR);
+ if (r == 0) {
+ if (used) {
+ /* fake up final newline */
+ buf[used++]= '\n';
+ r= 1;
+ } else {
+ ov_pipe= 0;
+ return;
+ }
+ }
+ if (r < 0) sysfail("read stdin",errno);
+ used += r;
+ }
+ *newline++= 0;
+ space= strchr(buf,' ');
+ if (space) *space++= 0;
+ process_optarg(buf,0,space);
+ used -= (newline-buf);
+ memmove(buf,newline,used);
+ anydone= 1;
+ }
+}
+
+int main(int argc, const char *const *argv) {
+ struct timeval *tv, tvbuf;
+ adns_query qu;
+ void *qun_v;
+ adns_answer *answer;
+ int r, maxfd;
+ fd_set readfds, writefds, exceptfds;
+ const char *arg;
+
+ while ((arg= *++argv)) process_optarg(arg,&argv,0);
+
+ if (!ov_pipe && !ads) usageerr("no domains given, and -f/--pipe not used; try --help");
+
+ ensure_adns_init();
+
+ 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_stdin();