| 1 |
/** |
| 2 |
* parse configuration options |
| 3 |
* |
| 4 |
* Copyright: Copyright Digital Mars 2017 |
| 5 |
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). |
| 6 |
* |
| 7 |
* Source: $(DRUNTIMESRC core/internal/parseoptions.d) |
| 8 |
*/ |
| 9 |
|
| 10 |
module core.internal.parseoptions; |
| 11 |
|
| 12 |
import core.internal.traits : externDFunc, hasUDA; |
| 13 |
import core.stdc.ctype : isdigit, isspace; |
| 14 |
import core.stdc.stdio : fprintf, snprintf, sscanf, stderr; |
| 15 |
import core.vararg; |
| 16 |
|
| 17 |
|
| 18 |
@nogc nothrow: |
| 19 |
extern extern(C) string[] rt_args() @system; |
| 20 |
|
| 21 |
extern extern(C) __gshared bool rt_envvars_enabled; |
| 22 |
extern extern(C) __gshared bool rt_cmdline_enabled; |
| 23 |
extern extern(C) __gshared string[] rt_options; |
| 24 |
|
| 25 |
alias rt_configCallBack = string delegate(string) @nogc nothrow; |
| 26 |
alias fn_configOption = string function(string opt, scope rt_configCallBack dg, bool reverse) @nogc nothrow; |
| 27 |
alias rt_configOption = externDFunc!("rt.config.rt_configOption", fn_configOption); |
| 28 |
|
| 29 |
/// UDA for field treated as memory value |
| 30 |
struct MemVal {} |
| 31 |
|
| 32 |
/** |
| 33 |
* initialize members of struct CFG from rt_config options |
| 34 |
* |
| 35 |
* options will be read from the environment, the command line or embedded |
| 36 |
* into the executable as configured (see rt.config) |
| 37 |
* |
| 38 |
* fields of the struct are populated by parseOptions(). |
| 39 |
*/ |
| 40 |
bool initConfigOptions(CFG)(ref CFG cfg, string cfgname) |
| 41 |
{ |
| 42 |
string parse(string opt) @nogc nothrow |
| 43 |
{ |
| 44 |
if (!parseOptions(cfg, opt)) |
| 45 |
return "err"; |
| 46 |
return null; // continue processing |
| 47 |
} |
| 48 |
string s = rt_configOption(cfgname, &parse, true); |
| 49 |
return s is null; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* initialize members of struct CFG from a string of sub-options. |
| 54 |
* |
| 55 |
* fields of the struct are populated by listing them as space separated |
| 56 |
* sub-options <field-name>:value, e.g. "precise:1 profile:1" |
| 57 |
* |
| 58 |
* supported field value types: |
| 59 |
* - strings (without spaces) |
| 60 |
* - integer types (positive values only) |
| 61 |
* - bool |
| 62 |
* - float |
| 63 |
* |
| 64 |
* If the struct has a member "help" it is called if it is found as a sub-option. |
| 65 |
* If the struct has a member "errorName", is used as the name reported in error |
| 66 |
* messages. Otherwise the struct name is used. |
| 67 |
*/ |
| 68 |
bool parseOptions(CFG)(ref CFG cfg, string opt) |
| 69 |
{ |
| 70 |
static if (is(typeof(__traits(getMember, CFG, "errorName")))) |
| 71 |
string errName = cfg.errorName; |
| 72 |
else |
| 73 |
string errName = CFG.stringof; |
| 74 |
opt = skip!isspace(opt); |
| 75 |
while (opt.length) |
| 76 |
{ |
| 77 |
auto tail = find!(c => c == ':' || c == '=' || c == ' ')(opt); |
| 78 |
auto name = opt[0 .. $ - tail.length]; |
| 79 |
static if (is(typeof(__traits(getMember, CFG, "help")))) |
| 80 |
if (name == "help") |
| 81 |
{ |
| 82 |
version (CoreUnittest) {} else |
| 83 |
cfg.help(); |
| 84 |
opt = skip!isspace(tail); |
| 85 |
continue; |
| 86 |
} |
| 87 |
if (tail.length <= 1 || tail[0] == ' ') |
| 88 |
return optError("Missing argument for", name, errName); |
| 89 |
tail = tail[1 .. $]; |
| 90 |
|
| 91 |
NAMES_SWITCH: |
| 92 |
switch (name) |
| 93 |
{ |
| 94 |
static foreach (field; __traits(allMembers, CFG)) |
| 95 |
{ |
| 96 |
static if (!is(typeof(__traits(getMember, cfg, field)) == function)) |
| 97 |
{ |
| 98 |
case field: |
| 99 |
bool r; |
| 100 |
|
| 101 |
static if (hasUDA!(__traits(getMember, cfg, field), MemVal)) |
| 102 |
r = parse(name, tail, __traits(getMember, cfg, field), errName, true); |
| 103 |
else |
| 104 |
r = parse(name, tail, __traits(getMember, cfg, field), errName); |
| 105 |
|
| 106 |
if (!r) |
| 107 |
return false; |
| 108 |
|
| 109 |
break NAMES_SWITCH; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
default: |
| 114 |
return optError("Unknown", name, errName); |
| 115 |
} |
| 116 |
opt = skip!isspace(tail); |
| 117 |
} |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
Parses an individual option `optname` value from a provided string `str`. |
| 123 |
The option type is given by the type `T` of the field `res` to which the parsed |
| 124 |
value will be written too. |
| 125 |
In case of an error, `errName` will be used to display an error message and |
| 126 |
the failure of the parsing will be indicated by a falsy return value. |
| 127 |
|
| 128 |
For boolean values, '0/n/N' (false) or '1/y/Y' (true) are accepted. |
| 129 |
|
| 130 |
Params: |
| 131 |
optname = name of the option to parse |
| 132 |
str = raw string to parse the option value from |
| 133 |
res = reference to the resulting data field that the option should be parsed too |
| 134 |
errName = full-text name of the option which should be displayed in case of errors |
| 135 |
|
| 136 |
Returns: `false` if a parsing error happened. |
| 137 |
*/ |
| 138 |
bool rt_parseOption(T)(const(char)[] optname, ref inout(char)[] str, ref T res, const(char)[] errName) |
| 139 |
{ |
| 140 |
return parse(optname, str, res, errName); |
| 141 |
} |
| 142 |
|
| 143 |
private: |
| 144 |
|
| 145 |
bool optError(const scope char[] msg, const scope char[] name, const(char)[] errName) |
| 146 |
{ |
| 147 |
import core.atomic : atomicLoad; |
| 148 |
|
| 149 |
version (CoreUnittest) if (inUnittest) return false; |
| 150 |
|
| 151 |
fprintf(atomicLoad(stderr), "%.*s %.*s option '%.*s'.\n", |
| 152 |
cast(int)msg.length, msg.ptr, |
| 153 |
cast(int)errName.length, errName.ptr, |
| 154 |
cast(int)name.length, name.ptr); |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
inout(char)[] skip(alias pred)(inout(char)[] str) |
| 159 |
{ |
| 160 |
return find!(c => !pred(c))(str); |
| 161 |
} |
| 162 |
|
| 163 |
inout(char)[] find(alias pred)(inout(char)[] str) |
| 164 |
{ |
| 165 |
foreach (i; 0 .. str.length) |
| 166 |
if (pred(str[i])) return str[i .. $]; |
| 167 |
return null; |
| 168 |
} |
| 169 |
|
| 170 |
bool parse(T : size_t)(const(char)[] optname, ref inout(char)[] str, ref T res, const(char)[] errName, bool mayHaveSuffix = false) |
| 171 |
if (is(T == size_t)) |
| 172 |
in { assert(str.length); } |
| 173 |
do |
| 174 |
{ |
| 175 |
size_t i, v; |
| 176 |
|
| 177 |
auto tail = find!(c => c == ' ')(str); |
| 178 |
size_t len = str.length - tail.length; |
| 179 |
|
| 180 |
import core.checkedint : mulu; |
| 181 |
|
| 182 |
bool overflowed; |
| 183 |
|
| 184 |
for (; i < len; i++) |
| 185 |
{ |
| 186 |
char c = str[i]; |
| 187 |
|
| 188 |
if (isdigit(c)) |
| 189 |
v = 10 * v + c - '0'; |
| 190 |
else // non-digit |
| 191 |
{ |
| 192 |
if (mayHaveSuffix && i == len-1) // suffix |
| 193 |
{ |
| 194 |
switch (c) |
| 195 |
{ |
| 196 |
|
| 197 |
case 'G': |
| 198 |
v = mulu(v, 1024 * 1024 * 1024, overflowed); |
| 199 |
break; |
| 200 |
|
| 201 |
case 'M': |
| 202 |
v = mulu(v, 1024 * 1024, overflowed); |
| 203 |
break; |
| 204 |
|
| 205 |
case 'K': |
| 206 |
v = mulu(v, 1024, overflowed); |
| 207 |
break; |
| 208 |
|
| 209 |
case 'B': |
| 210 |
break; |
| 211 |
|
| 212 |
default: |
| 213 |
return parseError("value with unit type M, K or B", optname, str, "with suffix"); |
| 214 |
} |
| 215 |
|
| 216 |
if (overflowed) |
| 217 |
return overflowedError(optname, str); |
| 218 |
|
| 219 |
i++; |
| 220 |
} |
| 221 |
else // unexpected non-digit character |
| 222 |
{ |
| 223 |
i = 0; |
| 224 |
} |
| 225 |
break; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
if (!i) |
| 230 |
return parseError("a number", optname, str, errName); |
| 231 |
|
| 232 |
if (mayHaveSuffix && isdigit(str[len-1])) |
| 233 |
{ |
| 234 |
// No suffix found, default to megabytes |
| 235 |
|
| 236 |
v = mulu(v, 1024 * 1024, overflowed); |
| 237 |
|
| 238 |
if (overflowed) |
| 239 |
return overflowedError(optname, str); |
| 240 |
} |
| 241 |
|
| 242 |
if (v > res.max) |
| 243 |
return parseError("a number " ~ T.max.stringof ~ " or below", optname, str[0 .. i], errName); |
| 244 |
str = str[i .. $]; |
| 245 |
res = v; |
| 246 |
return true; |
| 247 |
} |
| 248 |
|
| 249 |
bool parse(T : size_t)(const(char)[] optname, ref inout(char)[] str, ref T res, const(char)[] errName, bool mayHaveSuffix = false) |
| 250 |
if (!is(T == size_t)) |
| 251 |
in { assert(str.length); } |
| 252 |
do |
| 253 |
{ |
| 254 |
const oldStr = str; |
| 255 |
size_t v; |
| 256 |
if (!parse!size_t(optname, str, v, errName, mayHaveSuffix)) |
| 257 |
return false; |
| 258 |
|
| 259 |
if (v > res.max) |
| 260 |
return parseError("a number " ~ T.max.stringof ~ " or below", optname, oldStr[0 .. $-str.length], errName); |
| 261 |
res = cast(T) v; |
| 262 |
return true; |
| 263 |
} |
| 264 |
|
| 265 |
bool parse(const(char)[] optname, ref inout(char)[] str, ref bool res, const(char)[] errName) |
| 266 |
in { assert(str.length); } |
| 267 |
do |
| 268 |
{ |
| 269 |
if (str[0] == '1' || str[0] == 'y' || str[0] == 'Y') |
| 270 |
res = true; |
| 271 |
else if (str[0] == '0' || str[0] == 'n' || str[0] == 'N') |
| 272 |
res = false; |
| 273 |
else |
| 274 |
return parseError("'0/n/N' or '1/y/Y'", optname, str, errName); |
| 275 |
str = str[1 .. $]; |
| 276 |
return true; |
| 277 |
} |
| 278 |
|
| 279 |
bool parse(const(char)[] optname, ref inout(char)[] str, ref float res, const(char)[] errName) |
| 280 |
in { assert(str.length); } |
| 281 |
do |
| 282 |
{ |
| 283 |
// % uint f %n \0 |
| 284 |
char[1 + 10 + 1 + 2 + 1] fmt=void; |
| 285 |
// specify max-width |
| 286 |
immutable n = snprintf(fmt.ptr, fmt.length, "%%%uf%%n", cast(uint)str.length); |
| 287 |
assert(n > 4 && n < fmt.length); |
| 288 |
|
| 289 |
int nscanned; |
| 290 |
if (sscanf(str.ptr, fmt.ptr, &res, &nscanned) < 1) |
| 291 |
return parseError("a float", optname, str, errName); |
| 292 |
str = str[nscanned .. $]; |
| 293 |
return true; |
| 294 |
} |
| 295 |
|
| 296 |
bool parse(const(char)[] optname, ref inout(char)[] str, ref inout(char)[] res, const(char)[] errName) |
| 297 |
in { assert(str.length); } |
| 298 |
do |
| 299 |
{ |
| 300 |
auto tail = str.find!(c => c == ' '); |
| 301 |
res = str[0 .. $ - tail.length]; |
| 302 |
if (!res.length) |
| 303 |
return parseError("an identifier", optname, str, errName); |
| 304 |
str = tail; |
| 305 |
return true; |
| 306 |
} |
| 307 |
|
| 308 |
bool parseError(const scope char[] exp, const scope char[] opt, const scope char[] got, const(char)[] errName) |
| 309 |
{ |
| 310 |
import core.atomic : atomicLoad; |
| 311 |
|
| 312 |
version (CoreUnittest) if (inUnittest) return false; |
| 313 |
|
| 314 |
fprintf(atomicLoad(stderr), "Expecting %.*s as argument for %.*s option '%.*s', got '%.*s' instead.\n", |
| 315 |
cast(int)exp.length, exp.ptr, |
| 316 |
cast(int)errName.length, errName.ptr, |
| 317 |
cast(int)opt.length, opt.ptr, |
| 318 |
cast(int)got.length, got.ptr); |
| 319 |
return false; |
| 320 |
} |
| 321 |
|
| 322 |
bool overflowedError(const scope char[] opt, const scope char[] got) |
| 323 |
{ |
| 324 |
import core.atomic : atomicLoad; |
| 325 |
|
| 326 |
version (CoreUnittest) if (inUnittest) return false; |
| 327 |
|
| 328 |
fprintf(atomicLoad(stderr), "Argument for %.*s option '%.*s' is too big.\n", |
| 329 |
cast(int)opt.length, opt.ptr, |
| 330 |
cast(int)got.length, got.ptr); |
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
size_t min(size_t a, size_t b) { return a <= b ? a : b; } |
| 335 |
|
| 336 |
version (CoreUnittest) __gshared bool inUnittest; |
| 337 |
|
| 338 |
unittest |
| 339 |
{ |
| 340 |
inUnittest = true; |
| 341 |
scope (exit) inUnittest = false; |
| 342 |
|
| 343 |
static struct Config |
| 344 |
{ |
| 345 |
bool disable; // start disabled |
| 346 |
ubyte profile; // enable profiling with summary when terminating program |
| 347 |
string gc = "conservative"; // select gc implementation conservative|manual |
| 348 |
|
| 349 |
@MemVal size_t initReserve; // initial reserve (bytes) |
| 350 |
@MemVal size_t minPoolSize = 1 << 20; // initial and minimum pool size (bytes) |
| 351 |
float heapSizeFactor = 2.0; // heap size to used memory ratio |
| 352 |
|
| 353 |
@nogc nothrow: |
| 354 |
void help(); |
| 355 |
string errorName() @nogc nothrow { return "GC"; } |
| 356 |
} |
| 357 |
Config conf; |
| 358 |
|
| 359 |
assert(!conf.parseOptions("disable")); |
| 360 |
assert(!conf.parseOptions("disable:")); |
| 361 |
assert(!conf.parseOptions("disable:5")); |
| 362 |
assert(conf.parseOptions("disable:y") && conf.disable); |
| 363 |
assert(conf.parseOptions("disable:n") && !conf.disable); |
| 364 |
assert(conf.parseOptions("disable:Y") && conf.disable); |
| 365 |
assert(conf.parseOptions("disable:N") && !conf.disable); |
| 366 |
assert(conf.parseOptions("disable:1") && conf.disable); |
| 367 |
assert(conf.parseOptions("disable:0") && !conf.disable); |
| 368 |
|
| 369 |
assert(conf.parseOptions("disable=y") && conf.disable); |
| 370 |
assert(conf.parseOptions("disable=n") && !conf.disable); |
| 371 |
|
| 372 |
assert(conf.parseOptions("profile=0") && conf.profile == 0); |
| 373 |
assert(conf.parseOptions("profile=1") && conf.profile == 1); |
| 374 |
assert(conf.parseOptions("profile=2") && conf.profile == 2); |
| 375 |
assert(!conf.parseOptions("profile=256")); |
| 376 |
|
| 377 |
assert(conf.parseOptions("disable:1 minPoolSize:16")); |
| 378 |
assert(conf.disable); |
| 379 |
assert(conf.minPoolSize == 1024 * 1024 * 16); |
| 380 |
|
| 381 |
assert(conf.parseOptions("disable:1 minPoolSize:4096B")); |
| 382 |
assert(conf.disable); |
| 383 |
assert(conf.minPoolSize == 4096); |
| 384 |
|
| 385 |
assert(conf.parseOptions("disable:1 minPoolSize:2K help")); |
| 386 |
assert(conf.disable); |
| 387 |
assert(conf.minPoolSize == 2048); |
| 388 |
|
| 389 |
assert(conf.parseOptions("minPoolSize:3G help")); |
| 390 |
assert(conf.disable); |
| 391 |
assert(conf.minPoolSize == 1024UL * 1024 * 1024 * 3); |
| 392 |
|
| 393 |
assert(!conf.parseOptions("minPoolSize:922337203685477G"), "size_t overflow"); |
| 394 |
|
| 395 |
assert(conf.parseOptions("heapSizeFactor:3.1")); |
| 396 |
assert(conf.heapSizeFactor == 3.1f); |
| 397 |
assert(conf.parseOptions("heapSizeFactor:3.1234567890 disable:0")); |
| 398 |
assert(conf.heapSizeFactor > 3.123f); |
| 399 |
assert(!conf.disable); |
| 400 |
assert(!conf.parseOptions("heapSizeFactor:3.0.2.5")); |
| 401 |
assert(conf.parseOptions("heapSizeFactor:2")); |
| 402 |
assert(conf.heapSizeFactor == 2.0f); |
| 403 |
|
| 404 |
assert(!conf.parseOptions("initReserve:foo")); |
| 405 |
assert(!conf.parseOptions("initReserve:y")); |
| 406 |
assert(!conf.parseOptions("initReserve:20.5")); |
| 407 |
|
| 408 |
assert(conf.parseOptions("help")); |
| 409 |
assert(conf.parseOptions("help profile:1")); |
| 410 |
assert(conf.parseOptions("help profile:1 help")); |
| 411 |
|
| 412 |
assert(conf.parseOptions("gc:manual") && conf.gc == "manual"); |
| 413 |
assert(conf.parseOptions("gc:my-gc~modified") && conf.gc == "my-gc~modified"); |
| 414 |
assert(conf.parseOptions("gc:conservative help profile:1") && conf.gc == "conservative" && conf.profile == 1); |
| 415 |
|
| 416 |
// the config parse doesn't know all available GC names, so should accept unknown ones |
| 417 |
assert(conf.parseOptions("gc:whatever")); |
| 418 |
} |