]> git.ozlabs.org Git - ccan/blob - ccan/opt/helpers.c
7022afbbf090a53346388bdbd6a3f5d2b00ce290
[ccan] / ccan / opt / helpers.c
1 /* Licensed under GPLv3+ - see LICENSE file for details */
2 #include <ccan/opt/opt.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <stdio.h>
7 #include <limits.h>
8 #include "private.h"
9
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)
12
13 /* FIXME: asprintf module? */
14 static char *arg_bad(const char *fmt, const char *arg)
15 {
16         char *str = malloc(strlen(fmt) + strlen(arg));
17         sprintf(str, fmt, arg);
18         return str;
19 }
20
21 char *opt_set_bool(bool *b)
22 {
23         *b = true;
24         return NULL;
25 }
26
27 char *opt_set_invbool(bool *b)
28 {
29         *b = false;
30         return NULL;
31 }
32
33 char *opt_set_bool_arg(const char *arg, bool *b)
34 {
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);
39
40         return opt_invalid_argument(arg);
41 }
42
43 char *opt_set_invbool_arg(const char *arg, bool *b)
44 {
45         char *err = opt_set_bool_arg(arg, b);
46
47         if (!err)
48                 *b = !*b;
49         return err;
50 }
51
52 /* Set a char *. */
53 char *opt_set_charp(const char *arg, char **p)
54 {
55         *p = (char *)arg;
56         return NULL;
57 }
58
59 /* Set an integer value, various forms.  Sets to 1 on arg == NULL. */
60 char *opt_set_intval(const char *arg, int *i)
61 {
62         long l;
63         char *err = opt_set_longval(arg, &l);
64
65         if (err)
66                 return err;
67         *i = l;
68         /* Beware truncation... */
69         if (*i != l)
70                 return arg_bad("value '%s' does not fit into an integer", arg);
71         return err;
72 }
73
74 char *opt_set_uintval(const char *arg, unsigned int *ui)
75 {
76         int i;
77         char *err = opt_set_intval(arg, &i);
78
79         if (err)
80                 return err;
81         if (i < 0)
82                 return arg_bad("'%s' is negative but destination is unsigned", arg);
83         *ui = i;
84         return NULL;
85 }
86
87 char *opt_set_longval(const char *arg, long *l)
88 {
89         char *endp;
90
91         /* This is how the manpage says to do it.  Yech. */
92         errno = 0;
93         *l = strtol(arg, &endp, 0);
94         if (*endp || !arg[0])
95                 return arg_bad("'%s' is not a number", arg);
96         if (errno)
97                 return arg_bad("'%s' is out of range", arg);
98         return NULL;
99 }
100
101 char *opt_set_ulongval(const char *arg, unsigned long *ul)
102 {
103         long int l;
104         char *err;
105         
106         err = opt_set_longval(arg, &l);
107         if (err)
108                 return err;
109         *ul = l;
110         if (l < 0)
111                 return arg_bad("'%s' is negative but destination is unsigned", arg);
112         return NULL;
113 }
114
115 char *opt_inc_intval(int *i)
116 {
117         (*i)++;
118         return NULL;
119 }
120
121 /* Display version string. */
122 char *opt_version_and_exit(const char *version)
123 {
124         printf("%s\n", version);
125         exit(0);
126 }
127
128 char *opt_usage_and_exit(const char *extra)
129 {
130         printf("%s", opt_usage(opt_argv0, extra));
131         exit(0);
132 }
133
134 void opt_show_bool(char buf[OPT_SHOW_LEN], const bool *b)
135 {
136         strncpy(buf, *b ? "true" : "false", OPT_SHOW_LEN);
137 }
138
139 void opt_show_invbool(char buf[OPT_SHOW_LEN], const bool *b)
140 {
141         strncpy(buf, *b ? "false" : "true", OPT_SHOW_LEN);
142 }
143
144 void opt_show_charp(char buf[OPT_SHOW_LEN], char *const *p)
145 {
146         size_t len = strlen(*p);
147         buf[0] = '"';
148         if (len > OPT_SHOW_LEN - 2)
149                 len = OPT_SHOW_LEN - 2;
150         strncpy(buf+1, *p, len);
151         buf[1+len] = '"';
152         if (len < OPT_SHOW_LEN - 2)
153                 buf[2+len] = '\0';
154 }
155
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)
158 {
159         snprintf(buf, OPT_SHOW_LEN, "%i", *i);
160 }
161
162 void opt_show_uintval(char buf[OPT_SHOW_LEN], const unsigned int *ui)
163 {
164         snprintf(buf, OPT_SHOW_LEN, "%u", *ui);
165 }
166
167 void opt_show_longval(char buf[OPT_SHOW_LEN], const long *l)
168 {
169         snprintf(buf, OPT_SHOW_LEN, "%li", *l);
170 }
171
172 void opt_show_ulongval(char buf[OPT_SHOW_LEN], const unsigned long *ul)
173 {
174         snprintf(buf, OPT_SHOW_LEN, "%lu", *ul);
175 }
176
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.
179  *
180  * The base will be either 1000 or 1024, corresponding with the '_si' and
181  * '_bi' functions.
182  */
183
184 static char *set_llong_with_suffix(const char *arg, long long *ll,
185                                    const long long base)
186 {
187         char *endp;
188         if (!arg[0])
189                 return arg_bad("'%s' (an empty string) is not a number", arg);
190
191         errno = 0;
192         *ll = strtoll(arg, &endp, 0);
193         if (errno)
194                 return arg_bad("'%s' is out of range", arg);
195         if (*endp){
196                 /*The string continues with non-digits.  If there is just one
197                   letter and it is a known multiplier suffix, use it.*/
198                 if (endp[1])
199                         return arg_bad("'%s' is not a number (suffix too long)", arg);
200                 long long mul;
201                 switch(*endp){
202                 case 'K':
203                 case 'k':
204                         mul = base;
205                         break;
206                 case 'M':
207                 case 'm':
208                         mul = base * base;
209                         break;
210                 case 'G':
211                 case 'g':
212                         mul = base * base * base;
213                         break;
214                 case 'T':
215                 case 't':
216                         mul = base * base * base * base;
217                         break;
218                 case 'P':
219                         mul = base * base * base * base * base;
220                         break;
221                 case 'E':
222                         mul = base * base * base * base * base * base;
223                         break;
224                 /* This is as far as we can go in 64 bits ('E' is 2 ^ 60) */
225                 default:
226                         return arg_bad("'%s' is not a number (unknown suffix)",
227                                        arg);
228                 }
229                 if (*ll > LLONG_MAX / mul || *ll < LLONG_MIN / mul)
230                         return arg_bad("'%s' is out of range", arg);
231                 *ll *= mul;
232         }
233         return NULL;
234 }
235
236 /* Middle layer helpers that perform bounds checks for specific target sizes
237  * and signednesses.
238  */
239 static char * set_ulonglong_with_suffix(const char *arg, unsigned long long *ull,
240                                         const long base)
241 {
242         long long ll;
243         char *err = set_llong_with_suffix(arg, &ll, base);
244         if (err != NULL)
245                 return err;
246         if (ll < 0)
247                 return arg_bad("'%s' is negative but destination is unsigned", arg);
248         *ull = ll;
249         return NULL;
250 }
251
252 static char * set_long_with_suffix(const char *arg, long *l, const long base)
253 {
254         long long ll;
255         char *err = set_llong_with_suffix(arg, &ll, base);
256         if (err != NULL) /*an error*/
257                 return err;
258
259         *l = ll;
260         if (*l != ll)
261                 return arg_bad("value '%s' does not fit into a long", arg);
262         return NULL;
263 }
264
265 static char * set_ulong_with_suffix(const char *arg, unsigned long *ul, const long base)
266 {
267         long long ll;
268         char *err = set_llong_with_suffix(arg, &ll, base);
269         if (err != NULL)
270                 return err;
271         if (ll < 0)
272                 return arg_bad("'%s' is negative but destination is unsigned", arg);
273         *ul = ll;
274         if (*ul != ll)
275                 return arg_bad("value '%s' does not fit into an unsigned long", arg);
276         return NULL;
277 }
278
279 static char * set_int_with_suffix(const char *arg, int *i, const long base)
280 {
281         long long ll;
282         char *err = set_llong_with_suffix(arg, &ll, base);
283         if (err != NULL) /*an error*/
284                 return err;
285
286         *i = ll;
287         if (*i != ll)
288                 return arg_bad("value '%s' does not fit into an int", arg);
289         return NULL;
290 }
291
292 static char * set_uint_with_suffix(const char *arg, unsigned int *u, const long base)
293 {
294         long long ll;
295         char *err = set_llong_with_suffix(arg, &ll, base);
296         if (err != NULL)
297                 return err;
298         if (ll < 0)
299                 return arg_bad("'%s' is negative but destination is unsigned", arg);
300         *u = ll;
301         if (*u != ll)
302                 return arg_bad("value '%s' does not fit into an unsigned int", arg);
303         return NULL;
304 }
305
306 /*Set an integer, with decimal or binary suffixes.
307   The accepted suffixes are k/K, M/m, G/g, T, P, E.
308
309   The *_bi functions multiply the numeric value by a power of 1024, while the
310   *_si functions multiply by a power of 1000.
311  */
312
313 char * opt_set_ulonglongval_bi(const char *arg, unsigned long long *ll)
314 {
315         return set_ulonglong_with_suffix(arg, ll, 1024);
316 }
317
318 char * opt_set_ulonglongval_si(const char *arg, unsigned long long *ll)
319 {
320         return set_ulonglong_with_suffix(arg, ll, 1000);
321 }
322
323 char * opt_set_longlongval_bi(const char *arg, long long *ll)
324 {
325         return set_llong_with_suffix(arg, ll, 1024);
326 }
327
328 char * opt_set_longlongval_si(const char *arg, long long *ll)
329 {
330         return set_llong_with_suffix(arg, ll, 1000);
331 }
332
333 char * opt_set_longval_bi(const char *arg, long *l)
334 {
335         return set_long_with_suffix(arg, l, 1024);
336 }
337
338 char * opt_set_longval_si(const char *arg, long *l)
339 {
340         return set_long_with_suffix(arg, l, 1000);
341 }
342
343 char * opt_set_ulongval_bi(const char *arg, unsigned long *ul)
344 {
345         return set_ulong_with_suffix(arg, ul, 1024);
346 }
347
348 char * opt_set_ulongval_si(const char *arg, unsigned long *ul)
349 {
350         return set_ulong_with_suffix(arg, ul, 1000);
351 }
352
353 char * opt_set_intval_bi(const char *arg, int *i)
354 {
355         return set_int_with_suffix(arg, i, 1024);
356 }
357
358 char * opt_set_intval_si(const char *arg, int *i)
359 {
360         return set_int_with_suffix(arg, i, 1000);
361 }
362
363 char * opt_set_uintval_bi(const char *arg, unsigned int *u)
364 {
365         return set_uint_with_suffix(arg, u, 1024);
366 }
367
368 char * opt_set_uintval_si(const char *arg, unsigned int *u)
369 {
370         return set_uint_with_suffix(arg, u, 1000);
371 }