1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/* This file is part of GNU Dico.
Copyright (C) 2008, 2010 Sergey Poznyakoff
GNU Dico is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Dico is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Dico. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <dico.h>
#include <string.h>
#include <libi18n.h>
static struct dico_option *
find_opt(struct dico_option *opt, const char *str, const char **value)
{
size_t len = strlen(str);
int isbool;
if (len > 2 && memcmp(str, "no", 2) == 0) {
*value = NULL;
str += 2;
isbool = 1;
} else {
isbool = 0;
*value = str;
}
for (; opt->name; opt++) {
if (len >= opt->len
&& memcmp(opt->name, str, opt->len) == 0
&& (!isbool || opt->type == dico_opt_bool)) {
int eq = str[opt->len] == '=';
switch (opt->type) {
case dico_opt_long:
case dico_opt_string:
case dico_opt_const_string:
case dico_opt_enum:
if (!eq)
continue;
*value = str + opt->len + 1;
break;
case dico_opt_null:
if (eq)
*value = str + opt->len + 1;
else
*value = NULL;
break;
default:
if (eq)
continue;
break;
}
return opt;
}
}
return NULL;
}
int
find_value(const char **enumstr, const char *value)
{
int i;
for (i = 0; *enumstr; enumstr++, i++)
if (strcmp(*enumstr, value) == 0)
return i;
return -1;
}
int
dico_parseopt(struct dico_option *opt, int argc, char **argv, int flags,
int *pindex)
{
int i;
long n;
char *s;
int rc = 0;
const char *modname = argv[0];
_dico_libi18n_init();
for (i = (flags & DICO_PARSEOPT_PARSE_ARGV0) ? 0 : 1;
i < argc; i++) {
const char *value;
struct dico_option *p = find_opt(opt, argv[i], &value);
if (!p) {
if (pindex) {
if (flags & DICO_PARSEOPT_PERMUTE) {
int j;
for (j = i + 1; j < argc; j++)
if ((p = find_opt(opt, argv[j], &value)))
break;
if (p) {
char *tmp = argv[j];
argv[j] = argv[i];
argv[i] = tmp;
} else
break;
} else
break;
} else {
dico_log(L_ERR, 0, _("%s: %s: unknown option"),
modname, argv[i]);
rc = 1;
continue;
}
}
switch (p->type) {
case dico_opt_long:
n = strtol(value, &s, 0);
if (*s) {
dico_log(L_ERR, 0, _("%s: %s: %s is not a valid number"),
modname, p->name, value);
rc = 1;
continue;
}
*(long*)p->data = n;
break;
case dico_opt_const:
*(long*)p->data = p->v.value;
break;
case dico_opt_const_string:
*(const char**)p->data = value;
break;
case dico_opt_string:
*(const char**)p->data = strdup(value);
break;
case dico_opt_bool:
if (p->v.value) {
if (value)
*(int*)p->data |= p->v.value;
else
*(int*)p->data &= ~p->v.value;
} else
*(int*)p->data = value != NULL;
break;
case dico_opt_bitmask:
*(int*)p->data |= p->v.value;
break;
case dico_opt_bitmask_rev:
*(int*)p->data &= ~p->v.value;
break;
case dico_opt_enum:
n = find_value(p->v.enumstr, value);
if (n == -1) {
dico_log(L_ERR, 0, _("%s: %s: invalid value %s"),
modname, p->name, value);
rc = 1;
continue;
}
*(int*)p->data = n;
break;
case dico_opt_null:
break;
}
if (p->func && p->func (p, value))
rc = 1;
}
if (pindex)
*pindex = i;
return rc;
}
|