+typedef int comparer_type(const char **optp, const struct optioninfo *entry);
+
+static int oc_long(const char **optp, const struct optioninfo *entry) {
+ return entry->lopt && !strcmp(*optp,entry->lopt);
+}
+
+static int oc_short(const char **optp, const struct optioninfo *entry) {
+ const char *sopt;
+ int l;
+
+ sopt= entry->sopt;
+ if (!sopt) return 0;
+ l= strlen(sopt);
+ if (memcmp(*optp,sopt,l)) return 0;
+ (*optp) += l;
+ return 1;
+}
+
+static const struct optioninfo *find1(const char **optp,
+ const struct optioninfo *table,
+ comparer_type *comparer) {
+ for (;;) {
+ if (table->type == ot_end) return 0;
+ if (comparer(optp,table)) return table;
+ table++;
+ }
+}
+
+static const struct optioninfo *find(const char **optp,
+ const char *prefix,
+ comparer_type *comparer) {
+ const struct optioninfo *oip;
+ const char *opt;
+
+ opt= *optp;
+ oip= find1(optp,perquery_options,comparer);
+ if (oip) return oip;
+ oip= find1(optp,global_options,comparer);
+ if (!oip) usageerr("unknown option %s%s",prefix,opt);
+ if (ads) usageerr("global option %s%s specified after query domain(s)",prefix,opt);
+ return oip;
+}
+
+const struct optioninfo *opt_findl(const char *opt) { return find(&opt,"--",oc_long); }
+const struct optioninfo *opt_finds(const char **optp) { return find(optp,"-",oc_short); }
+
+static void noninvert(const struct optioninfo *oip) NONRETURNING;
+static void noninvert(const struct optioninfo *oip) {
+ usageerr("option %s%s%s%s%s may not be inverted",
+ oip->sopt ? "-" : "", oip->sopt ? oip->sopt : "",
+ oip->lopt && oip->sopt ? " / " : "",
+ oip->lopt ? "--" : "", oip->lopt ? oip->lopt : "");
+}
+
+void opt_do(const struct optioninfo *oip, int invert,
+ const char *arg, const char *arg2) {
+ switch (oip->type) {
+ case ot_flag:
+ assert(!arg);
+ *oip->storep= !invert;
+ return;
+ case ot_value:
+ assert(!arg);
+ if (invert) noninvert(oip);
+ *oip->storep= oip->value;
+ return;
+ case ot_func: case ot_funcarg: case ot_funcarg2:
+ if (invert) noninvert(oip);
+ oip->func(oip,arg,arg2);
+ return;
+ default:
+ abort();
+ }
+}