opt: allow show callbacks to return false. Sometimes, arguments are optional, so it's useful to decide at runtime whether to print a default in --help. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan: update show callback to take explicit length. For now we still use 80, but we could vary that in future. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan: allow user to set some bits in opt_table.type. In particular, Core Lightning wants to use this to flag arguments which can be specified multiple times (without overriding). Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
opt: fix opt_unregister. Instead of memmoving N structs, we were memmoving N bytes. But why did the test pass then? It was doing memmove(..., 1) instead of memmove(..., sizeof(struct opt_table)! Because the two structures were really similar; the main difference was the first entry, which points to the name. But they were allocated consecutively, and Intel being little-endian, the only difference was the first byte! Thus memmove(1) was enough to make it "work". Change two options in the test to be sufficiently different, and the bug shows up. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
opt: add new parse_early_args_incomplete. If we have plugins, and those can register args, we have a problem finding the plugin dir! So, do a best-effort incomplete parse. Note that this can screw up in theory if we have "--unknown --foo" since we don't know if unknown takes an argument (in which case, ignore --foo) or not. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Mark unused arguments in many modules. Either with UNNEEDED (if the module already used ccan/compiler) or with (void) casting. The only other change is in ccan/list/test/run-CCAN_LIST_DEBUG.c, because the linenumbers change and thus it needs updating. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Remove unused main() args in many modules. This makes us closer to compiling cleanly with -Wunused (as used by -Wextra). Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
opt/helpers: fix out-of-range check in opt_set_floatval() opt_set_floatval() uses opt_set_doubleval() to do initial conversion and bounds checking before casting the result to float. Previously the out of bounds check compared the original and float values for equality and declared anything unequal to be out of bounds. While this trick works well in the orderly integer world, it can backfire with floating point. For example, 3.1 resolves to the double more precisely known as 3.100000000000000088817841970012523233890533447265625, while 3.1f resolves to 3.099999904632568359375. These are not equal, though 3.1 is generally regarded as being in bounds for a float. There are around 8 billion other doubles (i.e. 3.1 +/- a little bit) that map to the same 3.1f value, of which only one is strictly equal to it. Why wasn't this discovered by the tests? It turns out that neither set_floatval nor set_doubleval were tested against non-integral numbers. This is slightly improved here. This patch uses the arguably more reasonable definition of bounds that is found in opt_set_doubleval(): it excludes numbers that would get rounded to zero or an infinity. One subtlety is that the double version allows `--foo=INF` for an explicit infinity without overflow. This is possibly useful, and there is some fiddling to allow this for floats. Likewise an explicit zero is allowed, as you would expect. It is perhaps worth noting that the `*f = d` cast/assignment at the heart of it all can crash and core dump on overflow or underflow if the floating point exception flags are in an unexpected state. Signed-off-by: Douglas Bagnall <douglas@halo.gen.nz>
opt: Don't segfault if a string's default is NULL Instead show '(nil)', like other people do. This is distinguishable from a similar looking string value, because the latter is shown with double quotes while NULL's nil has no quotes. Signed-off-by: Douglas Bagnall <douglas@halo.gen.nz> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
opt: add an int decrementing helper function opt_dec_intval decrements an int value, just as opt_inc_intval increments. There is not much more to say, other than it allows this kind of thing, with balanced opposing options: static int opt_verbosity = 0; static struct opt_table options[] = { OPT_WITHOUT_ARG("-q|--quiet", opt_dec_intval, &opt_verbosity, "print less"), OPT_WITHOUT_ARG("-v|--verbose", opt_inc_intval, &opt_verbosity, "print more"), OPT_ENDTABLE }; which is an occasionally seen idiom. It allows, e.g., people who like quiet to use `alias foo='foo -q'`, while letting them get back to normal and verbose modes with various amounts of '-v's. Signed-off-by: Douglas Bagnall <douglas@halo.gen.nz> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
opt: always initialise values in set_llong_with_suffix() The helper API functions based on set_llong_with_suffix() left the value uninitialised in the case of an empty string argument. This is quite unlikely to have caused problem in practice, as most values will have already been set to a default and the non-NULL error message should have triggered an early exit or some other emergency action. Nevertheless, it caused a compiler warning on some minor version of GCC 4.8 which I no longer seem to have, and the complaint seemed reasonable at the time. If an empty string (or any other non-numeric value) is passed to strtoll(), the result is zero. As far as I know, the strtoll() call is only short-circuited here to form a more specific error message, not because there is a good reason for the empty string to be a special non-initialising case. So let's set it to zero. Signed-off-by: Douglas Bagnall <douglas@halo.gen.nz> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan: Correct some poor conventions in _info includes There are a couple of small problems with the includes used in most of ccan's _info files. * _info routinely uses printf(), and so should include <stdio.h>, but only some of them do. We get away with it, because they do include <string.h>, which apparently includes <stdio.h> indirectly, but we should be explicit about it. * Most _info files were including config.h after the system headers. That _seems_ sensible, but actually causes problems. Because config.h defines _GNU_SOURCE it can change the behaviour of the system headers. More specifically it can make them behave differently to how the individual module headers (which have included config.h) expects them to behave. This patch adjusts all the existing _info files and, more importantly, the template constructed by ccanlint. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>