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