1 /* Licensed under GPLv3+ - see LICENSE file for details */
2 #include <ccan/opt/opt.h>
10 /* Upper bound to sprintf this simple type? Each 3 bits < 1 digit. */
11 #define CHAR_SIZE(type) (((sizeof(type)*CHAR_BIT + 2) / 3) + 1)
13 /* FIXME: asprintf module? */
14 static char *arg_bad(const char *fmt, const char *arg)
16 char *str = malloc(strlen(fmt) + strlen(arg));
17 sprintf(str, fmt, arg);
21 char *opt_set_bool(bool *b)
27 char *opt_set_invbool(bool *b)
33 char *opt_set_bool_arg(const char *arg, bool *b)
35 if (!strcasecmp(arg, "yes") || !strcasecmp(arg, "true"))
36 return opt_set_bool(b);
37 if (!strcasecmp(arg, "no") || !strcasecmp(arg, "false"))
38 return opt_set_invbool(b);
40 return opt_invalid_argument(arg);
43 char *opt_set_invbool_arg(const char *arg, bool *b)
45 char *err = opt_set_bool_arg(arg, b);
53 char *opt_set_charp(const char *arg, char **p)
59 /* Set an integer value, various forms. Sets to 1 on arg == NULL. */
60 char *opt_set_intval(const char *arg, int *i)
63 char *err = opt_set_longval(arg, &l);
68 /* Beware truncation... */
70 return arg_bad("value '%s' does not fit into an integer", arg);
74 char *opt_set_uintval(const char *arg, unsigned int *ui)
77 char *err = opt_set_intval(arg, &i);
82 return arg_bad("'%s' is negative but destination is unsigned", arg);
87 char *opt_set_longval(const char *arg, long *l)
91 /* This is how the manpage says to do it. Yech. */
93 *l = strtol(arg, &endp, 0);
95 return arg_bad("'%s' is not a number", arg);
97 return arg_bad("'%s' is out of range", arg);
101 char *opt_set_ulongval(const char *arg, unsigned long *ul)
106 err = opt_set_longval(arg, &l);
111 return arg_bad("'%s' is negative but destination is unsigned", arg);
115 char *opt_inc_intval(int *i)
121 /* Display version string. */
122 char *opt_version_and_exit(const char *version)
124 printf("%s\n", version);
128 char *opt_usage_and_exit(const char *extra)
130 printf("%s", opt_usage(opt_argv0, extra));
134 void opt_show_bool(char buf[OPT_SHOW_LEN], const bool *b)
136 strncpy(buf, *b ? "true" : "false", OPT_SHOW_LEN);
139 void opt_show_invbool(char buf[OPT_SHOW_LEN], const bool *b)
141 strncpy(buf, *b ? "false" : "true", OPT_SHOW_LEN);
144 void opt_show_charp(char buf[OPT_SHOW_LEN], char *const *p)
146 size_t len = strlen(*p);
148 if (len > OPT_SHOW_LEN - 2)
149 len = OPT_SHOW_LEN - 2;
150 strncpy(buf+1, *p, len);
152 if (len < OPT_SHOW_LEN - 2)
156 /* Set an integer value, various forms. Sets to 1 on arg == NULL. */
157 void opt_show_intval(char buf[OPT_SHOW_LEN], const int *i)
159 snprintf(buf, OPT_SHOW_LEN, "%i", *i);
162 void opt_show_uintval(char buf[OPT_SHOW_LEN], const unsigned int *ui)
164 snprintf(buf, OPT_SHOW_LEN, "%u", *ui);
167 void opt_show_longval(char buf[OPT_SHOW_LEN], const long *l)
169 snprintf(buf, OPT_SHOW_LEN, "%li", *l);
172 void opt_show_ulongval(char buf[OPT_SHOW_LEN], const unsigned long *ul)
174 snprintf(buf, OPT_SHOW_LEN, "%lu", *ul);
177 /* a helper function that multiplies out an argument's kMGTPE suffix in the
178 * long long int range, and perform checks common to all integer destinations.
180 * The base will be either 1000 or 1024, corresponding with the '_si' and
184 static char *set_llong_with_suffix(const char *arg, long long *ll,
185 const long long base)
189 return arg_bad("'%s' (an empty string) is not a number", arg);
192 *ll = strtoll(arg, &endp, 0);
194 return arg_bad("'%s' is out of range", arg);
196 /*The string continues with non-digits. If there is just one
197 letter and it is a known multiplier suffix, use it.*/
199 return arg_bad("'%s' is not a number (suffix too long)", arg);
212 mul = base * base * base;
216 mul = base * base * base * base;
219 mul = base * base * base * base * base;
222 mul = base * base * base * base * base * base;
224 /* This is as far as we can go in 64 bits ('E' is 2 ^ 60) */
226 return arg_bad("'%s' is not a number (unknown suffix)",
229 if (*ll > LLONG_MAX / mul || *ll < LLONG_MIN / mul)
230 return arg_bad("'%s' is out of range", arg);
236 /* Middle layer helpers that perform bounds checks for specific target sizes
239 static char * set_ulonglong_with_suffix(const char *arg, unsigned long long *ull,
243 char *err = set_llong_with_suffix(arg, &ll, base);
247 return arg_bad("'%s' is negative but destination is unsigned", arg);
252 static char * set_long_with_suffix(const char *arg, long *l, const long base)
255 char *err = set_llong_with_suffix(arg, &ll, base);
256 if (err != NULL) /*an error*/
261 return arg_bad("value '%s' does not fit into a long", arg);
265 static char * set_ulong_with_suffix(const char *arg, unsigned long *ul, const long base)
268 char *err = set_llong_with_suffix(arg, &ll, base);
272 return arg_bad("'%s' is negative but destination is unsigned", arg);
275 return arg_bad("value '%s' does not fit into an unsigned long", arg);
279 static char * set_int_with_suffix(const char *arg, int *i, const long base)
282 char *err = set_llong_with_suffix(arg, &ll, base);
283 if (err != NULL) /*an error*/
288 return arg_bad("value '%s' does not fit into an int", arg);
292 static char * set_uint_with_suffix(const char *arg, unsigned int *u, const long base)
295 char *err = set_llong_with_suffix(arg, &ll, base);
299 return arg_bad("'%s' is negative but destination is unsigned", arg);
302 return arg_bad("value '%s' does not fit into an unsigned int", arg);
306 /*Set an integer, with decimal or binary suffixes.
307 The accepted suffixes are k/K, M/m, G/g, T, P, E.
309 The *_bi functions multiply the numeric value by a power of 1024, while the
310 *_si functions multiply by a power of 1000.
313 char * opt_set_ulonglongval_bi(const char *arg, unsigned long long *ll)
315 return set_ulonglong_with_suffix(arg, ll, 1024);
318 char * opt_set_ulonglongval_si(const char *arg, unsigned long long *ll)
320 return set_ulonglong_with_suffix(arg, ll, 1000);
323 char * opt_set_longlongval_bi(const char *arg, long long *ll)
325 return set_llong_with_suffix(arg, ll, 1024);
328 char * opt_set_longlongval_si(const char *arg, long long *ll)
330 return set_llong_with_suffix(arg, ll, 1000);
333 char * opt_set_longval_bi(const char *arg, long *l)
335 return set_long_with_suffix(arg, l, 1024);
338 char * opt_set_longval_si(const char *arg, long *l)
340 return set_long_with_suffix(arg, l, 1000);
343 char * opt_set_ulongval_bi(const char *arg, unsigned long *ul)
345 return set_ulong_with_suffix(arg, ul, 1024);
348 char * opt_set_ulongval_si(const char *arg, unsigned long *ul)
350 return set_ulong_with_suffix(arg, ul, 1000);
353 char * opt_set_intval_bi(const char *arg, int *i)
355 return set_int_with_suffix(arg, i, 1024);
358 char * opt_set_intval_si(const char *arg, int *i)
360 return set_int_with_suffix(arg, i, 1000);
363 char * opt_set_uintval_bi(const char *arg, unsigned int *u)
365 return set_uint_with_suffix(arg, u, 1024);
368 char * opt_set_uintval_si(const char *arg, unsigned int *u)
370 return set_uint_with_suffix(arg, u, 1000);