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