ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

adh-opts.c
Go to the documentation of this file.
00001 /*
00002  * adh-opts.c
00003  * - useful general-purpose resolver client program
00004  *   option handling tables etc.
00005  */
00006 /*
00007  *  This file is
00008  *    Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
00009  *
00010  *  It is part of adns, which is
00011  *    Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
00012  *    Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
00013  *
00014  *  This program is free software; you can redistribute it and/or modify
00015  *  it under the terms of the GNU General Public License as published by
00016  *  the Free Software Foundation; either version 2, or (at your option)
00017  *  any later version.
00018  *
00019  *  This program is distributed in the hope that it will be useful,
00020  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  *  GNU General Public License for more details.
00023  *
00024  *  You should have received a copy of the GNU General Public License
00025  *  along with this program; if not, write to the Free Software Foundation,
00026  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00027  */
00028 
00029 #ifdef ADNS_JGAA_WIN32
00030 # include "adns_win32.h"
00031 #endif
00032 
00033 #include "adnshost.h"
00034 
00035 int ov_env=1, ov_pipe=0, ov_asynch=0;
00036 int ov_verbose= 0;
00037 adns_rrtype ov_type= adns_r_none;
00038 int ov_search=0, ov_qc_query=0, ov_qc_anshost=0, ov_qc_cname=1;
00039 int ov_tcp=0, ov_cname=0, ov_format=fmt_default;
00040 char *ov_id= 0;
00041 struct perqueryflags_remember ov_pqfr = { 1,1,1, tm_none };
00042 
00043 static const struct optioninfo global_options[]= {
00044   { ot_desconly, "global binary options:" },
00045   { ot_flag,             "Do not look at environment variables at all",
00046     "e", "env",            &ov_env, 0 },
00047   { ot_flag,             "Read queries on stdin instead of using args",
00048     "f", "pipe",           &ov_pipe, 1 },
00049   { ot_flag,             "Allow answers to be reordered",
00050     "a", "asynch",         &ov_asynch, 1 },
00051 
00052   { ot_desconly, "answer/error output format and destination (see below):" },
00053   { ot_value,            "Answers to stdout, errors as messages to stderr (default)",
00054     "Fs", "fmt-simple",    &ov_format, fmt_simple },
00055   { ot_value,            "Answers and errors both to stdout in parseable format",
00056     "Fi", "fmt-inline",    &ov_format, fmt_inline },
00057   { ot_value,            "Fully-parseable output format (default for --asynch)",
00058     "Fa", "fmt-asynch",    &ov_format, fmt_asynch },
00059 
00060   { ot_desconly, "global verbosity level:" },
00061   { ot_value,            "Do not print anything to stderr",
00062     "Vq", "quiet",         &ov_verbose, adns_if_noerrprint },
00063   { ot_value,            "Report unexpected kinds of problem only  (default)",
00064     "Vn", "no-quiet",      &ov_verbose, 0 },
00065   { ot_value,            "Debugging mode",
00066     "Vd", "debug",         &ov_verbose, adns_if_debug },
00067 
00068   { ot_desconly, "other global options:" },
00069   { ot_funcarg,          "Configuration to use instead of /etc/resolv.conf",
00070     0, "config",           0,0, of_config, "<config-text>" },
00071   { ot_func,             "Print version number",
00072     0, "version",          0,0, of_version },
00073   { ot_func,             "Print usage information",
00074     0, "help",             0,0, of_help },
00075 
00076   { ot_end }
00077 };
00078 
00079 static const struct optioninfo perquery_options[]= {
00080   { ot_desconly, "per-query options:" },
00081   { ot_funcarg,          "Query type (see below)",
00082     "t", "type",           0,0, &of_type, "type" },
00083   { ot_funcarg,          "Do reverse query (address -> name lookup)",
00084     "i", "ptr",            0,0, &of_ptr, "addr" },
00085   { ot_funcarg2,         "Lookup in in-addr-like `zone' (eg MAPS RBL)",
00086     0, "reverse",          0,0, &of_reverse, "addr","zone" },
00087 
00088   { ot_desconly, "per-query binary options:" },
00089   { ot_flag,             "Use the search list",
00090     "s", "search",         &ov_search, 1 },
00091   { ot_flag,             "Let query domains contain quote-requiring chars",
00092     "Qq", "qc-query",      &ov_qc_query, 1 },
00093   { ot_flag,             "Let hostnames in answers contain ...",
00094     "Qa", "qc-anshost",    &ov_qc_anshost, 1 },
00095   { ot_flag,             "Prevent CNAME target domains from containing ...",
00096     "Qc", "qc-cname",      &ov_qc_cname, 0 },
00097   { ot_flag,             "Force use of a virtual circuit",
00098     "u", "tcp",            &ov_tcp, 1 },
00099   { ot_flag,             "Do not display owner name in output",
00100     "Do", "show-owner",   &ov_pqfr.show_owner, 0 },
00101   { ot_flag,             "Do not display RR type in output",
00102     "Dt", "show-type",    &ov_pqfr.show_type, 0 },
00103   { ot_flag,             "Do not display CNAME target in output",
00104     "Dc", "show-cname",    &ov_pqfr.show_cname, 0 },
00105 
00106   { ot_desconly, "per-query TTL mode (NB TTL is minimum across all info in reply):" },
00107   { ot_value,            "Show the TTL as a TTL",
00108     "Tt", "ttl-ttl",       &ov_pqfr.ttl, tm_rel },
00109   { ot_value,            "Show the TTL as a time_t when the data might expire",
00110     "Ta", "ttl-abs",       &ov_pqfr.ttl, tm_abs },
00111   { ot_value,            "Do not show the TTL (default)",
00112     "Tn", "no-ttl",        &ov_pqfr.ttl, tm_none },
00113 
00114   { ot_desconly, "per-query CNAME handling mode:" },
00115   { ot_value,            "Call it an error if a CNAME is found",
00116     "Cf", "cname-reject",  &ov_cname, adns_qf_cname_forbid },
00117   { ot_value,            "Allow references to CNAMEs in other RRs",
00118     "Cl", "cname-loose",   &ov_cname, adns_qf_cname_loose },
00119   { ot_value,            "CNAME ok for query domain, but not in RRs (default)",
00120     "Cs", "cname-ok",      &ov_cname, 0 },
00121 
00122   { ot_desconly, "asynchronous/pipe mode options:" },
00123   { ot_funcarg,          "Set <id>, default is decimal sequence starting 0",
00124     0, "asynch-id",        0,0, &of_asynch_id, "id" },
00125   { ot_funcarg,          "Cancel the query with id <id> (no error if not found)",
00126     0, "cancel-id",        0,0, &of_cancel_id, "id" },
00127 
00128   { ot_end }
00129 };
00130 
00131 static void printusage(void) {
00132   static const struct optioninfo *const all_optiontables[]= {
00133     global_options, perquery_options, 0
00134   };
00135 
00136   const struct optioninfo *const *oiap, *oip=0;
00137   int maxsopt, maxlopt, l;
00138 
00139   maxsopt= maxlopt= 0;
00140 
00141   for (oiap=all_optiontables; *oiap; oiap++) {
00142     for (oip=*oiap; oip->type != ot_end; oip++) {
00143       if (oip->type == ot_funcarg) continue;
00144       if (oip->sopt) { l= strlen(oip->sopt); if (l>maxsopt) maxsopt= l; }
00145       if (oip->lopt) {
00146     l= strlen(oip->lopt);
00147     if (oip->type == ot_flag && !oip->value) l+= 3;
00148     if (l>maxlopt) maxlopt= l;
00149       }
00150     }
00151   }
00152 
00153   fputs("usage: adnshost [global-opts] [query-opts] query-domain\n"
00154     "                             [[query-opts] query-domain ...]\n"
00155     "       adnshost [global-opts] [query-opts] -f|--pipe\n",
00156     stdout);
00157 
00158   for (oiap=all_optiontables; *oiap; oiap++) {
00159     putchar('\n');
00160     for (oip=*oiap; oip->type != ot_end; oip++) {
00161       switch (oip->type) {
00162       case ot_flag:
00163     if (!oip->value) {
00164       if (oip->sopt) {
00165         printf(" +%-*s --no-%-*s %s\n",
00166            maxsopt, oip->sopt,
00167            maxlopt-2, oip->lopt,
00168            oip->desc);
00169       } else {
00170         printf(" --no-%-*s %s\n",
00171            maxlopt+maxsopt+1, oip->lopt,
00172            oip->desc);
00173       }
00174       break;
00175     }
00176       case ot_value: case ot_func: /* fall through */
00177     if (oip->sopt) {
00178       printf(" -%-*s --%-*s %s\n",
00179          maxsopt, oip->sopt,
00180          maxlopt+1, oip->lopt,
00181          oip->desc);
00182     } else {
00183       printf(" --%-*s %s\n",
00184          maxlopt+maxsopt+3, oip->lopt,
00185          oip->desc);
00186     }
00187     break;
00188       case ot_funcarg:
00189     if (oip->sopt) {
00190       l= (maxlopt + maxsopt - 9 -
00191           (strlen(oip->sopt) + strlen(oip->lopt) + 2*strlen(oip->argdesc)));
00192       printf(" -%s<%s> / --%s <%s>%*s%s\n",
00193          oip->sopt, oip->argdesc, oip->lopt, oip->argdesc,
00194          l>2 ? l : 2, "",
00195          oip->desc);
00196     } else {
00197       l= (maxlopt + maxsopt + 1 -
00198           (strlen(oip->lopt) + strlen(oip->argdesc)));
00199       printf(" --%s <%s>%*s%s\n",
00200          oip->lopt, oip->argdesc,
00201          l>2 ? l : 2, "",
00202          oip->desc);
00203     }
00204     break;
00205       case ot_funcarg2:
00206     assert(!oip->sopt);
00207     l= (maxlopt + maxsopt - 2 -
00208         (strlen(oip->lopt) + strlen(oip->argdesc) + strlen(oip->argdesc2)));
00209       printf(" --%s <%s> <%s>%*s%s\n",
00210          oip->lopt, oip->argdesc, oip->argdesc2,
00211          l>2 ? l : 2, "",
00212          oip->desc);
00213     break;
00214       case ot_desconly:
00215     printf("%s\n", oip->desc);
00216     break;
00217       default:
00218     abort();
00219       }
00220     }
00221   }
00222 
00223   printf("\nEscaping domains which might start with `-':\n"
00224      " - %-*s Next argument is a domain, but more options may follow\n",
00225      maxlopt+maxsopt+3, "<domain>");
00226 
00227   fputs("\n"
00228     "Query domains should always be quoted according to master file format.\n"
00229     "\n"
00230     "For binary options, --FOO and --no-FOO are opposites, as are\n"
00231     "-X and +X.  In each case the default is the one not listed.\n"
00232     "Per query options stay set a particular way until they are reset,\n"
00233     "whether they appear on the command line or on stdin.\n"
00234     "All global options must preceed the first query domain.\n"
00235     "\n"
00236     "With -f, the input should be lines with either an option, possibly\n"
00237     "with a value argument (separated from the option by a space if it's a long\n"
00238     "option), or a domain (possibly preceded by a hyphen and a space to\n"
00239     "distinguish it from an option).\n"
00240     "\n"
00241     "Output format is master file format without class or TTL by default:\n"
00242     "   [<owner>] [<ttl>] [<type>] <data>\n"
00243     "or if the <owner> domain refers to a CNAME and --show-cname is on\n"
00244     "   [<owner>] [<ttl>] CNAME <cname>\n"
00245     "   [<cname>] [<ttl>] <type> <data>\n"
00246     "When a query fails you get an error message to stderr (with --fmt-simple).\n"
00247     "Specify --fmt-inline for lines like this (broken here for readability):\n"
00248     "   ; failed <statustype> <statusnum> <statusabbrev> \\\n"
00249     "       [<owner>] [<ttl>] [<cname>] \"<status string>\"\n"
00250     "If you use --fmt-asynch, which is the default for --asynch,\n"
00251     "each answer (success or failure) is preceded by a line\n"
00252     "   <id> <nrrs> <statustype> <statusnum> <statusabbrev> \\\n"
00253     "       [<owner>] [<ttl>] [<cname>] \"<status string>\"\n"
00254     "where <nrrs> is the number of RRs that follow and <cname> will be `$' or\n"
00255     "the CNAME target; the CNAME indirection and error formats above are not used.\n"
00256     "\n"
00257     "Exit status:\n"
00258     " 0    all went well\n"
00259     " 1-6  at least one query failed with statustype:\n"
00260     "   1    localfail   )\n"
00261     "   2    remotefail  ) temporary errors\n"
00262     "   3    tempfail  __)_________________\n"
00263     "   4    misconfig   )\n"
00264     "   5    misquery    ) permanent errors\n"
00265     "   6    permfail    )\n"
00266     " 10   system trouble\n"
00267     " 11   usage problems\n"
00268     "\n"
00269     "Query types (see adns.h; default is addr):\n"
00270     "  ns  soa  ptr  mx  rp  addr       - enhanced versions\n"
00271     "  cname  hinfo  txt                - types with only one version\n"
00272     "  a  ns-  soa-  ptr-  mx-  rp-     - _raw versions\n"
00273     "Default is addr, or ptr for -i/--ptr queries\n",
00274     stdout);
00275   if (ferror(stdout)) sysfail("write usage message",errno);
00276 }
00277 
00278 void of_version(const struct optioninfo *oi, const char *arg, const char *arg2) {
00279   VERSION_PRINT_QUIT("adnshost");
00280 }
00281 
00282 void of_help(const struct optioninfo *oi, const char *arg, const char *arg2) {
00283   printusage();
00284   if (fclose(stdout)) sysfail("finish writing output",errno);
00285   quitnow(0);
00286 }
00287 
00288 typedef int comparer_type(const char **optp, const struct optioninfo *entry);
00289 
00290 static int oc_long(const char **optp, const struct optioninfo *entry) {
00291   return entry->lopt && !strcmp(*optp,entry->lopt);
00292 }
00293 
00294 static int oc_short(const char **optp, const struct optioninfo *entry) {
00295   const char *sopt;
00296   int l;
00297 
00298   sopt= entry->sopt;
00299   if (!sopt) return 0;
00300   l= strlen(sopt);
00301   if (memcmp(*optp,sopt,l)) return 0;
00302   (*optp) += l;
00303   return 1;
00304 }
00305 
00306 static const struct optioninfo *find1(const char **optp,
00307                       const struct optioninfo *table,
00308                       comparer_type *comparer) {
00309   for (;;) {
00310     if (table->type == ot_end) return 0;
00311     if (comparer(optp,table)) return table;
00312     table++;
00313   }
00314 }
00315 
00316 static const struct optioninfo *find(const char **optp,
00317                      const char *prefix,
00318                      comparer_type *comparer) {
00319   const struct optioninfo *oip;
00320   const char *opt;
00321 
00322   opt= *optp;
00323   oip= find1(optp,perquery_options,comparer);
00324   if (oip) return oip;
00325   oip= find1(optp,global_options,comparer);
00326   if (!oip) usageerr("unknown option %s%s",prefix,opt);
00327   if (ads) usageerr("global option %s%s specified after query domain(s)",prefix,opt);
00328   return oip;
00329 }
00330 
00331 const struct optioninfo *opt_findl(const char *opt) { return find(&opt,"--",oc_long); }
00332 const struct optioninfo *opt_finds(const char **optp) { return find(optp,"-",oc_short); }
00333 
00334 static void noninvert(const struct optioninfo *oip) NONRETURNING;
00335 static void noninvert(const struct optioninfo *oip) {
00336   usageerr("option %s%s%s%s%s may not be inverted",
00337        oip->sopt ? "-" : "", oip->sopt ? oip->sopt : "",
00338        oip->lopt && oip->sopt ? " / " : "",
00339        oip->lopt ? "--" : "", oip->lopt ? oip->lopt : "");
00340 }
00341 
00342 void opt_do(const struct optioninfo *oip, int invert,
00343         const char *arg, const char *arg2) {
00344   switch (oip->type) {
00345   case ot_flag:
00346     assert(!arg);
00347     *oip->storep= !invert;
00348     return;
00349   case ot_value:
00350     assert(!arg);
00351     if (invert) noninvert(oip);
00352     *oip->storep= oip->value;
00353     return;
00354   case ot_func: case ot_funcarg: case ot_funcarg2:
00355     if (invert) noninvert(oip);
00356     oip->func(oip,arg,arg2);
00357     return;
00358   default:
00359     abort();
00360   }
00361 }

Generated on Sun May 27 2012 04:33:25 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.