summaryrefslogtreecommitdiff
path: root/ccan/opt
AgeCommit message (Collapse)Author
2015-06-04opt: add opt_usage_exit_fail.Rusty Russell
I've been using opt_usage_and_exit() but that exits status 0. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2014-07-29opt/helpers: fix out-of-range check in opt_set_floatval()Douglas Bagnall
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>
2014-06-23opt: Don't segfault if a string's default is NULLDouglas Bagnall
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>
2014-06-23opt: add an int decrementing helper functionDouglas Bagnall
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>
2014-06-23opt: always initialise values in set_llong_with_suffix()Douglas Bagnall
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>
2014-06-16ccan: Correct some poor conventions in _info includesDavid Gibson
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>
2014-02-24opt: add float/double helpers.Rusty Russell
bfgminer/cgminer/sgminer want these. Con implemented some, but these are independently written (with tests!) Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2014-02-24opt: relicense to GPLv2+ (from GPLv3+)Rusty Russell
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>
2014-02-11opt: don't wordwrap when description line starts with whitespace.Rusty Russell
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>
2014-02-11opt: test the new embedded-\n-in-usage behaviour.Rusty Russell
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2014-02-11opt: accept newline in help stringsLuke Dashjr
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>
2014-02-11ccan: add test for line-wrapping usage message.Rusty Russell
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2014-02-06opt: fix tests for 64-bit systems.Rusty Russell
Actually, only an issue for 64 bit big endian systems, but still... Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2014-02-06opt: test HAVE_SYS_TERMIOS_H via #ifRusty Russell
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>
2014-02-06opt: avoid using %lld and %llx formatsLuke Dashjr
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>
2014-02-06opt: only use termios if HAVE_SYS_TERMIOS_H is definedLuke Dashjr
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>
2013-10-02opt: add allocator setting.Rusty Russell
Good for tal usage. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2012-06-09opt: avoid using err.h.Rusty Russell
Seems it's not available on Solaris.
2012-03-27opt: fix Solaris compile.Rusty Russell
Need sys/termios.h for struct winsize.
2011-12-19opt: much prettier usage (using terminal size)Rusty Russell
2011-12-16opt: handle -Wcast-qual warnings.Rusty Russell
2011-12-15opt: don't leak on exit (valgrind complains).Rusty Russell
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.
2011-12-07opt: suggest they look at ccan/autodata.Rusty Russell
Nice for distributed option declaration.
2011-12-01opt: add OPT_EARLY and opt_early_parse.Rusty Russell
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().
2011-12-01opt: fix up outdated comments in documentation.Rusty Russell
2011-12-01opt: get rid of last remnant of getopt.Rusty Russell
Testing code still using it.
2011-10-04opt: increase testing coverage on 64 bit platformsRusty Russell
Help the compiler eliminate untestable code.
2011-08-23opt: neaten tests with helpers.Rusty Russell
2011-08-14opt: complete coverage, enhance opt_free_table.Rusty Russell
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.
2011-08-14opt: fix warnings in test, fix endian assumptions.Rusty Russell
In particular, handing an pointer to ULL where a pointer to UL is expected won't work on big endian.
2011-08-13opt: functions to show integer values with kMGTPE suffixesDouglas Bagnall
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)
2011-08-13opt: incidental comment and whitespace repairDouglas Bagnall
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.
2011-08-13opt: add integer helpers that accept k, M, G, T, P, E suffixesDouglas Bagnall
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.
2011-07-21various: add LICENSE comments.Rusty Russell
2011-07-19various: make the _info License: wording uniform for GPL variants.Rusty Russell
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.
2011-04-07typesafe_cb: simplify, preserve namespace.Rusty Russell
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.
2011-03-22opt: avoid function pointer arithmeticRusty Russell
Same fix as we used for ccan/typesafe_cb.
2011-03-22opt: fix memory leak in tests.Rusty Russell
No only does this give us one more ccanlint point, it clears the way to see if we introduce a *real* memory leak later.
2011-03-22opt: allow const arguments.Rusty Russell
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.
2011-03-22opt: define NULLRusty Russell
Headers should stand alone; we use NULL, so we should make sure it's defined.
2011-03-01iscsi, nfs, opt, tap: use config.h instead of defining _GNU_SOURCE.Rusty Russell
2011-02-22opt: Add a function to free the internal memory.Andreas Schlick
2011-02-22opt: Correct the separator in _info's example.Andreas Schlick
2011-02-07opt: Silence unsed param warning triggered by -WextraJoel Stanley
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.
2011-02-07opt: Fix -Wmissing-field-initializers warningJoel Stanley
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.
2011-01-18opt: correct description, and neaten main header order.Rusty Russell
Reordering header makes it more readable, and we don't rely on getopt_long any more.
2011-01-17opt: spelling fixes.Brad Hards
2011-01-07opt: Fix warnings with gcc-4.5 (same approach as commit 6535bde)Joey Adams
&*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.
2010-11-10foreach, iscsi, jbitset, jmap, opt, rbtree, sparse_bsearch, tally, tdb2: add ↵Rusty Russell
LICENSE symlinks
2010-11-04opt: fix junk after string in opt_show_charp.Rusty Russell