chiark / gitweb /
Table-driven usage message for adnshost.
[adns.git] / client / adh-opts.c
1 /*
2  * adh-opts.c
3  * - useful general-purpose resolver client program
4  *   option handling tables etc.
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 int ov_env=1, ov_pipe=0, ov_asynch=0;
30 int ov_verbose= 0;
31 int ov_search=0, ov_qc_query=0, ov_qc_anshost=0, ov_qc_cname=1;
32 struct perqueryflags_remember ov_pqfr = { 1,1,1, tm_none };
33 int ov_tcp=0, ov_cname=0;
34
35 static const struct optinfo global_options[]= {
36   { ot_desconly, "global binary options:" },
37   { ot_flag,             "Do not look at environment variables at all",
38     "e", "env",            &ov_env, 0 },
39   { ot_flag,             "Read queries on stdin instead of using args",
40     "f", "pipe",           &ov_pipe, 1 },
41   { ot_flag,             "Allow answers to be reordered",
42     "a", "asynch",         &ov_asynch, 1 },
43                          
44   { ot_desconly, "global verbosity level:" },
45   { ot_value,            "Do not print anything to stderr",
46     "Vq", "quiet",         &ov_verbose, adns_if_noerrprint },
47   { ot_value,            "Report unexpected kinds of problem only  (default)",
48     "Vn", "no-quiet",      &ov_verbose, 0 },
49   { ot_value,            "Debugging mode",
50     "Vd", "debug",         &ov_verbose, adns_if_debug },
51                          
52   { ot_desconly, "other global options:" },
53   { ot_func,             "Print usage information",
54     0, "help",             0,0, of_help },
55
56   { ot_end }
57 };
58
59 static const struct optinfo perquery_options[]= {
60   { ot_desconly, "per-query options:" },
61   { ot_funcarg,          "Query type (see below)",
62     "t", "type",           0,0, &of_type, "type" },
63
64   { ot_desconly, "per-query binary options:" },
65   { ot_flag,             "Use the search list",
66     "s", "search",         &ov_search, 1 },
67   { ot_flag,             "Let query domains contain quote-requiring chars",
68     "Qq", "qc-query",      &ov_qc_query, 1 },
69   { ot_flag,             "Let hostnames in answers contain ...",
70     "Qa", "qc-anshost",    &ov_qc_anshost, 1 },
71   { ot_flag,             "Prevent CNAME target domains from containing ...",
72     "Qc", "qc-cname",      &ov_qc_cname, 0 },
73   { ot_flag,             "Force use of a virtual circuit",
74     "u", "tcp",            &ov_tcp, 1 },
75   { ot_flag,             "Do not display owner name in output",
76     "Do", "show-owner",   &ov_pqfr.show_owner, 0 },
77   { ot_flag,             "Do not display RR type in output",
78     "Dt", "show-type",    &ov_pqfr.show_type, 0 },
79   { ot_flag,             "Do not display CNAME target in output",
80     "Dc", "show-cname",    &ov_pqfr.show_cname, 0 },
81   
82   { ot_desconly, "per-query TTL mode (NB TTL is minimum across all info in reply):" },
83   { ot_value,            "Show the TTL as a TTL",
84     "Tt", "ttl-ttl",       &ov_pqfr.ttl, tm_rel },
85   { ot_value,            "Show the TTL as a time_t when the data might expire",
86     "Ta", "ttl-abs",       &ov_pqfr.ttl, tm_abs },
87   { ot_value,            "Do not show the TTL (default)",
88     "Tn", "no-ttl",        &ov_pqfr.ttl, tm_none },
89   
90   { ot_desconly, "per-query CNAME handling mode:" },
91   { ot_value,            "Call it an error if a CNAME is found",
92     "Cf", "cname-reject",  &ov_cname, adns_qf_cname_forbid },
93   { ot_value,            "Allow references to CNAMEs in other RRs",
94     "Cl", "cname-loose",   &ov_cname, adns_qf_cname_loose },
95   { ot_value,            "CNAME ok for query domain, but not in RRs (default)",
96     "Cs", "cname-ok",      &ov_cname, 0 },
97   
98   { ot_desconly, "asynchronous/pipe mode options:" },
99   { ot_funcarg,          "Set <id>, default is decimal sequence starting 0",
100     "i", "asynch-id",      0,0, &of_asynch_id, "id" },
101   { ot_funcarg,          "Cancel the query with id <id>",
102     0, "cancel-id",        0,0, &of_cancel_id, "id" },
103
104   { ot_end }
105 };
106
107 static void printusage(void) {
108   static const struct optinfo *const all_optiontables[]= {
109     global_options, perquery_options, 0
110   };
111
112   const struct optinfo *const *oiap, *oip=0;
113   int maxsopt, maxlopt, l;
114
115   maxsopt= maxlopt= 0;
116   
117   for (oiap=alloptions; *oiap; oiap++) {
118     for (oip=*oiap; oip->type != ot_end; oip++) {
119       if (oip->type == ot_funcarg) continue;
120       if (oip->sopt) { l= strlen(oip->sopt); if (l>maxsopt) maxsopt= l; }
121       if (oip->lopt) {
122         l= strlen(oip->lopt);
123         if (oip->type == ot_flag && !oip->value) l+= 3;
124         if (l>maxlopt) maxlopt= l;
125       }
126     }
127   }
128         
129   fputs("usage: adnshost [global-opts] [query-opts] query-domain\n"
130         "                             [[query-opts] query-domain ...]\n"
131         "       adnshost [global-opts] [query-opts] -f|--pipe\n",
132         stdout);
133
134   for (oiap=alloptions; *oiap; oiap++) {
135     putchar('\n');
136     for (oip=*oiap; oip->type != ot_end; oip++) {
137       switch (oip->type) {
138       case ot_flag:
139         if (!oip->value) {
140           if (oip->sopt) {
141             printf(" +%-*s --no-%-*s %s\n",
142                    maxsopt, oip->sopt,
143                    maxlopt-2, oip->lopt,
144                    oip->desc);
145           } else {
146             printf(" --no-%-*s %s\n",
147                    maxlopt+maxsopt+1, oip->lopt,
148                    oip->desc);
149           }
150           break;
151         }
152       case ot_value: case ot_func: /* fall through */
153         if (oip->sopt) {
154           printf(" -%-*s --%-*s %s\n",
155                  maxsopt, oip->sopt,
156                  maxlopt+1, oip->lopt,
157                  oip->desc);
158         } else {
159           printf(" --%-*s %s\n",
160                  maxlopt+maxsopt+3, oip->lopt,
161                  oip->desc);
162         }
163         break;
164       case ot_funcarg:
165         if (oip->sopt) {
166           l= (maxlopt + maxsopt - 9 -
167               (strlen(oip->sopt) + strlen(oip->lopt) + 2*strlen(oip->argdesc)));
168           printf(" -%s<%s> / --%s <%s>%*s%s\n",
169                  oip->sopt, oip->argdesc, oip->lopt, oip->argdesc,
170                  l>2 ? l : 2, "",
171                  oip->desc);
172         } else {
173           l= (maxlopt + maxsopt + 1 -
174               (strlen(oip->lopt) + strlen(oip->argdesc)));
175           printf(" --%s <%s>%*s%s\n",
176                  oip->lopt, oip->argdesc,
177                  l>2 ? l : 2, "",
178                  oip->desc);
179         }
180         break;
181       case ot_desconly:
182         printf("%s\n", oip->desc);
183         break;
184       default:
185         abort();
186       }
187     }
188   }
189
190   printf("\nEscaping domains which might start with `-':\n"
191          " - %-*s Next argument is a domain, but more options may follow\n",
192          maxlopt+maxsopt+3, "<domain>");
193   
194   fputs("\n"
195         "Query domains should always be quoted according to master file format.\n"
196         "\n"
197         "For binary options, --FOO and --no-FOO are opposites, as are\n"
198         "-X and +X.  In each case the default is the one not listed.\n"
199         "Per query options stay set a particular way until they are reset,\n"
200         "whether they appear on the command line or on stdin.\n"
201         "All global options must preceed the first query domain.\n"
202         "\n"
203         "With -f, the input should be lines with either an option, possibly\n"
204         "with a value argument (separated from the option by a space if it's a long\n"
205         "option), or a domain (possibly preceded by a hyphen and a space to\n"
206         "distinguish it from an option).\n"
207         "\n"
208         "Output format is master file format without class or TTL by default:\n"
209         "   [<owner>] [<ttl>] [<type>] <data>\n"
210         "or if the <owner> domain refers to a CNAME and --show-cname is on\n"
211         "   [<owner>] [<ttl>] CNAME <cname>\n"
212         "   [<cname>] [<ttl>] <type> <data>\n"
213         "When a query fails you get a line like:\n"
214  "   ; failed <statustype> [<owner>] [<ttl>] [<type>] <status> \"<status string>\"\n"
215         "\n"
216         "If you use --asynch each answer (success or failure) is preceded by a line\n"
217         "   <id> <statustype> <status> <nrrs> [<cname>] \"<status string>\"\n"
218         "where <nrrs> is the number of RRs that follow and <cname> will be `$' or\n"
219         "the CNAME target; the CNAME indirection and error formats above are not used.\n"
220         "\n"
221         "Exit status:\n"
222         " 0    all went well\n"
223         " 1-6  at least one query failed with statustype:\n"
224         "   1    localfail   )\n"
225         "   2    remotefail  ) temporary errors\n"
226         "   3    tempfail  __)_________________\n"
227         "   4    misconfig   )\n"
228         "   5    misquery    ) permanent errors\n"
229         "   6    permfail    )\n"
230         " 10   system trouble\n"
231         " 11   usage problems\n"
232         "\n"
233         "Query types (see adns.h; default is addr):\n"
234         "  ns  soa  ptr  mx  rp  addr       - enhanced versions\n"
235         "  cname  hinfo  txt                - types with only one version\n"
236         "  a  ns-  soa-  ptr-  mx-  rp-     - _raw versions\n",
237         stdout);
238   if (ferror(stdout)) sysfail("write usage message",errno);
239 }
240
241 static void of_help(const struct optinfo *oi, const char *arg) {
242   printusage();
243   if (fclose(stdout)) sysfail("finish writing output",errno);
244   exit(0);
245 }
246