Age | Commit message (Collapse) | Author |
|
I've been using opt_usage_and_exit() but that exits status 0.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
|
|
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>
|
|
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_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>
|
|
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>
|
|
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>
|
|
bfgminer/cgminer/sgminer want these. Con implemented some,
but these are independently written (with tests!)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
|
|
Acked by contributors:
Luke-Jr <luke@dashjr.org>:
I don't have any objections
Douglas Bagnall <douglas@halo.gen.nz>:
Fine by me.
Joel Stanley <joel@jms.id.au>:
Fine with me.
Brad Hards <bradh@frogmouth.net>:
Ack.
No response from Joey (trivial warning patch) or Andreas Schlick (one
trivial patch, but he also wrote opt_free() (which was a 2 line function).
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
|
|
This was suggested by Luke, though his version insisted on using tab.
This one is a bit more complex, but allows either tab or space.
Suggested-by: Luke Dashjr <luke-jr+git@utopios.org>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
|
|
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
|
|
This correctly continues on the next line indented.
Signed-off-by: Luke Dashjr <luke-jr+git@utopios.org>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
|
|
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
|
|
Actually, only an issue for 64 bit big endian systems, but still...
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
|
|
As ccanlint warns. Also, test TIOCGWINSZ before ioctl, rather than the
header directly since it's a little orthogonal.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
|
|
These are not supported in all environments, so use PRId64 and PRIu64 instead.
Signed-off-by: Luke Dashjr <luke-jr+git@utopios.org>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
|
|
This fixes building for Windows and other platforms which lack <sys/termios.h>
Signed-off-by: Luke Dashjr <luke-jr+git@utopios.org>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
|
|
Good for tal usage.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
|
|
Seems it's not available on Solaris.
|
|
Need sys/termios.h for struct winsize.
|
|
|
|
|
|
Recent real usage case showed this leak when we call opt_usage_and_exit:
we don't bother freeing before exit. With valgrind, it matters.
|
|
Nice for distributed option declaration.
|
|
Parsing options like --verbose and --debug can be a pain. You need to
have everything set up before invoking parse_args(), but that may be a
significant amount of work, for which you may want verbose or
debugging enabled.
Thus the concept of "early" args: you can nominate arguments to be
parse before anything else, using opt_early_parse().
|
|
|
|
Testing code still using it.
|
|
Help the compiler eliminate untestable code.
|
|
|
|
No point checking malloc failure in usage(), since we don't elsewhere.
We get 100% coverage with -O (due to code elimination) or 64 bit.
|
|
In particular, handing an pointer to ULL where a pointer to UL is expected
won't work on big endian.
|
|
As with the set_ functions, there are twelve permutations of integer size,
base, and signedness. The supported sizes are int, long, and long long.
For example, this:
char buf1[OPT_SHOW_LEN];
char buf2[OPT_SHOW_LEN];
unsigned i = 1024000;
opt_show_uintval_bi(buf1, &i);
opt_show_uintval_si(buf2, &i);
will put "1000k" in buf1, and "1024k" in buf2.
Unlike the opt_set_ functions, these use unsigned arithmetic for unsigned values.
(32 bit bug using sizeof(suffixes) instead of strlen(suffixes) fixed by Rusty)
|
|
This comment occurred in a couple of places:
/* Set an integer value, various forms. Sets to 1 on arg == NULL. */
One instance was clearly spurious, while the other was misleading.
Another resolution to this mismatch would be to add
"if (arg == NULL){*l = 1; return NULL}" somewhere, but I suspect
it may have been left out/removed because someone thought better.
|
|
These functions come in two flavours: those ending with "_si", which
have 1000-based interpretations of the suffixes; and those ending with
"_bi", which use base 1024. There are versions for signed and
unsigned int, long, and long long destinations, with tests for all 12
new functions. The tests get a bit repetitive, I am afraid.
As an example, if the -x option were using the opt_set_intval_bi
function, then all of these would do the same thing:
$ foo -x 5M
$ foo -x $((5 * 1024 * 1024))
$ foo -x 5242880
$ foo -x 5120k
quite what that thing is depends on the size of your int -- people
with 16 bit ints would see an "out of range" error message.
The arithmetic for unsigned variations is actually done using signed
long long integers, so the maximum possible value is LLONG_MAX, not
ULLONG_MAX. This follows the practice of existing functions, and
avoids tedious work.
|
|
|
|
GPL versions 2 and 3 both specifically mention "any later version" as
the phrase which allows the user to choose to upgrade the license.
Make sure we use that phrase, and make the format consistent across
modules.
|
|
Get rid of many variants, which were just confusing for most people.
Keep typesafe_cb(), typesafe_cb_preargs() and typesafe_cb_postarts(),
and rework cast_if_type() into typesafe_cb_cast() so we stay in our
namespace.
I should have done this as soon as I discovered the limitation that
the types have to be defined if I want const-taking callbacks.
|
|
Same fix as we used for ccan/typesafe_cb.
|
|
No only does this give us one more ccanlint point, it clears the way
to see if we introduce a *real* memory leak later.
|
|
This need shows up most clearly with opt_usage_and_exit and gcc's
-Wwrite-strings, where string literals become "const char *". Our
callbacks should take const void *: since we overload the arg field
already (to hold table size) it make sense to turn it into a proper
union.
|
|
Headers should stand alone; we use NULL, so we should make sure it's defined.
|
|
|
|
|
|
|
|
The type checking function does not use its parameter:
ccan/opt/opt.h: In function ‘_check_is_entry’:
ccan/opt/opt.h:328:53: warning: unused parameter ‘e’
Annotate the function using UNUSED from compiler.h. This commit also
adds compiler.h as a dependency for opt.
|
|
OPT_ENDTABLE does not initalise all the elements in stuct opt_table.
When compliling with -Wextra -Wmissing-field-initializers is enabled,
which produces the following warning:
iviewiir.c:299:9: error: missing field 'cb' initializer
[-Wmissing-field-initializers]
OPT_ENDTABLE
^
In file included from iviewiir.c:12:
./ccan/opt/opt.h:82:38: note: instantiated from:
#define OPT_ENDTABLE { NULL, OPT_END }
^
By changing the definition of OPT_ENDTABLE to initalise all 7 elements
of struct opt_table, the warning is silenced.
|
|
Reordering header makes it more readable, and we don't rely on
getopt_long any more.
|
|
|
|
&*ptr is used in some other macros, but at a glance, they look like
cases where the pointer shouldn't be NULL . Didn't change those,
and if we get more warnings, we'll cross that bridge when we get to it.
For now, I suppose they are just free NULL checks.
|
|
LICENSE symlinks
|
|
|