]> git.ozlabs.org Git - ccan/blob - ccan/opt/helpers.c
read_write_all: avoid arithmetic on void pointers.
[ccan] / ccan / opt / helpers.c
1 #include <ccan/opt/opt.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include "private.h"
7
8 /* Upper bound to sprintf this simple type?  Each 3 bits < 1 digit. */
9 #define CHAR_SIZE(type) (((sizeof(type)*CHAR_BIT + 2) / 3) + 1)
10
11 /* FIXME: asprintf module? */
12 static char *arg_bad(const char *fmt, const char *arg)
13 {
14         char *str = malloc(strlen(fmt) + strlen(arg));
15         sprintf(str, fmt, arg);
16         return str;
17 }
18
19 char *opt_set_bool(bool *b)
20 {
21         *b = true;
22         return NULL;
23 }
24
25 char *opt_set_invbool(bool *b)
26 {
27         *b = false;
28         return NULL;
29 }
30
31 char *opt_set_bool_arg(const char *arg, bool *b)
32 {
33         if (!strcasecmp(arg, "yes") || !strcasecmp(arg, "true"))
34                 return opt_set_bool(b);
35         if (!strcasecmp(arg, "no") || !strcasecmp(arg, "false"))
36                 return opt_set_invbool(b);
37
38         return opt_invalid_argument(arg);
39 }
40
41 char *opt_set_invbool_arg(const char *arg, bool *b)
42 {
43         char *err = opt_set_bool_arg(arg, b);
44
45         if (!err)
46                 *b = !*b;
47         return err;
48 }
49
50 /* Set a char *. */
51 char *opt_set_charp(const char *arg, char **p)
52 {
53         *p = (char *)arg;
54         return NULL;
55 }
56
57 /* Set an integer value, various forms.  Sets to 1 on arg == NULL. */
58 char *opt_set_intval(const char *arg, int *i)
59 {
60         long l;
61         char *err = opt_set_longval(arg, &l);
62
63         if (err)
64                 return err;
65         *i = l;
66         /* Beware truncation... */
67         if (*i != l)
68                 return arg_bad("value '%s' does not fit into an integer", arg);
69         return err;
70 }
71
72 char *opt_set_uintval(const char *arg, unsigned int *ui)
73 {
74         int i;
75         char *err = opt_set_intval(arg, &i);
76
77         if (err)
78                 return err;
79         if (i < 0)
80                 return arg_bad("'%s' is negative", arg);
81         *ui = i;
82         return NULL;
83 }
84
85 char *opt_set_longval(const char *arg, long *l)
86 {
87         char *endp;
88
89         /* This is how the manpage says to do it.  Yech. */
90         errno = 0;
91         *l = strtol(arg, &endp, 0);
92         if (*endp || !arg[0])
93                 return arg_bad("'%s' is not a number", arg);
94         if (errno)
95                 return arg_bad("'%s' is out of range", arg);
96         return NULL;
97 }
98
99 char *opt_set_ulongval(const char *arg, unsigned long *ul)
100 {
101         long int l;
102         char *err;
103         
104         err = opt_set_longval(arg, &l);
105         if (err)
106                 return err;
107         *ul = l;
108         if (l < 0)
109                 return arg_bad("'%s' is negative", arg);
110         return NULL;
111 }
112
113 char *opt_inc_intval(int *i)
114 {
115         (*i)++;
116         return NULL;
117 }
118
119 /* Display version string. */
120 char *opt_version_and_exit(const char *version)
121 {
122         printf("%s\n", version);
123         exit(0);
124 }
125
126 char *opt_usage_and_exit(const char *extra)
127 {
128         printf("%s", opt_usage(opt_argv0, extra));
129         exit(0);
130 }
131
132 void opt_show_bool(char buf[OPT_SHOW_LEN], const bool *b)
133 {
134         strncpy(buf, *b ? "true" : "false", OPT_SHOW_LEN);
135 }
136
137 void opt_show_invbool(char buf[OPT_SHOW_LEN], const bool *b)
138 {
139         strncpy(buf, *b ? "false" : "true", OPT_SHOW_LEN);
140 }
141
142 void opt_show_charp(char buf[OPT_SHOW_LEN], char *const *p)
143 {
144         size_t len = strlen(*p);
145         buf[0] = '"';
146         if (len > OPT_SHOW_LEN - 2)
147                 len = OPT_SHOW_LEN - 2;
148         strncpy(buf+1, *p, len);
149         buf[1+len] = '"';
150         if (len < OPT_SHOW_LEN - 2)
151                 buf[2+len] = '\0';
152 }
153
154 /* Set an integer value, various forms.  Sets to 1 on arg == NULL. */
155 void opt_show_intval(char buf[OPT_SHOW_LEN], const int *i)
156 {
157         snprintf(buf, OPT_SHOW_LEN, "%i", *i);
158 }
159
160 void opt_show_uintval(char buf[OPT_SHOW_LEN], const unsigned int *ui)
161 {
162         snprintf(buf, OPT_SHOW_LEN, "%u", *ui);
163 }
164
165 void opt_show_longval(char buf[OPT_SHOW_LEN], const long *l)
166 {
167         snprintf(buf, OPT_SHOW_LEN, "%li", *l);
168 }
169
170 void opt_show_ulongval(char buf[OPT_SHOW_LEN], const unsigned long *ul)
171 {
172         snprintf(buf, OPT_SHOW_LEN, "%lu", *ul);
173 }