chiark / gitweb /
Support for reverse queries.
[adns.git] / client / adh-main.c
1 /*
2  * adh-main.c
3  * - useful general-purpose resolver client program
4  *   main program and useful subroutines
5  */
6 /*
7  *  This file is
8  *    Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
9  *
10  *  It is part of adns, which is
11  *    Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
12  *    Copyright (C) 1999 Tony Finch <dot@dotat.at>
13  *  
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License as published by
16  *  the Free Software Foundation; either version 2, or (at your option)
17  *  any later version.
18  *  
19  *  This program is distributed in the hope that it will be useful,
20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *  GNU General Public License for more details.
23  *  
24  *  You should have received a copy of the GNU General Public License
25  *  along with this program; if not, write to the Free Software Foundation,
26  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
27  */
28
29 #include "adnshost.h"
30
31 void sysfail(const char *what, int errnoval) {
32   fprintf(stderr,"adnshost failed: %s: %s\n",what,strerror(errnoval));
33   exit(10);
34 }
35
36 void usageerr(const char *fmt, ...) {
37   va_list al;
38   fputs("adnshost usage error: ",stderr);
39   va_start(al,fmt);
40   vfprintf(stderr,fmt,al);
41   va_end(al);
42   putc('\n',stderr);
43   exit(11);
44 }
45
46 static void domain_do_arg(const char *domain) {
47   if (ov_pipe) usageerr("-f/--pipe not consistent with domains on command line");
48   domain_do(domain);
49 }
50
51 void *xmalloc(size_t sz) {
52   void *p;
53
54   p= malloc(sz); if (!p) sysfail("malloc",sz);
55   return p;
56 }
57
58 char *xstrsave(const char *str) {
59   char *p;
60   
61   p= xmalloc(strlen(str)+1);
62   strcpy(p,str);
63   return p;
64 }
65
66 void of_type(const struct optioninfo *oi, const char *arg) { abort(); }
67
68 int main(int argc, const char *const *argv) {
69   const char *arg;
70   const struct optioninfo *oip;
71   
72   while ((arg= *++argv)) {
73     if (arg[0] == '-') {
74       if (arg[1] == '-') {
75         oip= opt_findl(arg+2);
76         if (oip->type == ot_funcarg) {
77           arg= *++argv;
78           if (!arg) usageerr("option --%s requires a value argument",oip->lopt);
79         } else {
80           arg= 0;
81         }
82         opt_do(oip,arg);
83       } else if (arg[1] == 0) {
84         arg= *++argv;
85         if (!arg) usageerr("option `-' must be followed by a domain");
86         domain_do_arg(arg);
87       } else { /* arg[1] != '-', != '\0' */
88         ++arg;
89         while (*arg) {
90           oip= opt_finds(&arg);
91           if (oip->type == ot_funcarg) {
92             if (!*arg) {
93               arg= *++argv;
94               if (!arg) usageerr("option -%s requires a value argument",oip->sopt);
95             }
96             arg= "";
97           } else {
98             arg= 0;
99           }
100           opt_do(oip,arg);
101         }
102       }
103     } else { /* arg[0] != '-' */
104       domain_do_arg(arg);
105     }
106   }
107   if (!ov_pipe && !ads) usageerr("no domains given, and -f/--pipe not used; try --help");
108   abort();
109 }