diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-04-27 13:11:18 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-04-27 13:19:06 +0300 |
commit | ef07786925303a1f8588a061b66034e066f533da (patch) | |
tree | 7318ebaddf37e5afa790ba13535a05b7b1075cfd | |
parent | b6173eeaf3311d9de26d530d5ff07fdf6ddddcad (diff) | |
download | mailfromd-ef07786925303a1f8588a061b66034e066f533da.tar.gz mailfromd-ef07786925303a1f8588a061b66034e066f533da.tar.bz2 |
Avoid casts between pointers and integers (optcache)
* lib/libmf.h (mf_option_value): Union.
(mf_option_cache): Rearrange members.
(handler,set): Change signature.
(isset): New member.
(mf_optcache_set_option): Change signature.
(mf_option_string,mf_option_boolean): Likewise.
(mf_option_time_t,mf_option_size_t): Remove.
(mf_option_timeout,mf_option_size): New prototypes.
* lib/optcache.c (optcache_dup): Clear isset member for each element.
(mf_optcache_set_option): Rewrite.
(mf_option_string, mf_option_boolean): Rewrite.
(mf_option_time_t,mf_option_size_t): Remove.
(mf_option_timeout,mf_option_size): New functions.
* lib/utils.c: Change the use of mf_optcache functions.
* src/main.c: Likewise.
* src/mfdbtool.c: Likewise.
* src/srvcfg.c: Likewise.
-rw-r--r-- | lib/libmf.h | 32 | ||||
-rw-r--r-- | lib/optcache.c | 76 | ||||
-rw-r--r-- | lib/utils.c | 43 | ||||
-rw-r--r-- | src/main.c | 32 | ||||
-rw-r--r-- | src/mfdbtool.c | 13 | ||||
-rw-r--r-- | src/srvcfg.c | 75 |
6 files changed, 127 insertions, 144 deletions
diff --git a/lib/libmf.h b/lib/libmf.h index 96421687..4344d1a6 100644 --- a/lib/libmf.h +++ b/lib/libmf.h | |||
@@ -289,26 +289,36 @@ void mf_server_start(const char *program, const char *dir, | |||
289 | 289 | ||
290 | 290 | ||
291 | /* optcache.c */ | 291 | /* optcache.c */ |
292 | union mf_option_value { | ||
293 | int ov_bool; | ||
294 | char *ov_string; | ||
295 | time_t ov_time; | ||
296 | size_t ov_size; | ||
297 | mu_list_t ov_list; | ||
298 | }; | ||
299 | |||
292 | struct mf_option_cache { | 300 | struct mf_option_cache { |
293 | char *name; /* option name */ | 301 | char *name; /* option name */ |
294 | void *value; /* current value */ | 302 | int (*handler)(char const *opt, union mf_option_value *pval, char const *arg); |
295 | int (*handler)(char *opt, void **pval, char *newval); | ||
296 | /* handler function, which verifies and assigns | 303 | /* handler function, which verifies and assigns |
297 | newval to pval. */ | 304 | arg to pval. */ |
298 | void (*set)(void *value); /* function that actually sets the option */ | 305 | void (*set)(union mf_option_value *value); |
306 | /* function that actually sets the option */ | ||
307 | union mf_option_value value; /* current value */ | ||
308 | int isset; /* is the value set */ | ||
299 | }; | 309 | }; |
300 | 310 | ||
301 | #define MF_OCF_NULL 0x01 /* Option_cache array is NULL-terminated */ | 311 | #define MF_OCF_NULL 0x01 /* Option_cache array is NULL-terminated */ |
302 | #define MF_OCF_STATIC 0x02 /* Option_cache array is static. */ | 312 | #define MF_OCF_STATIC 0x02 /* Option_cache array is static. */ |
303 | 313 | ||
304 | void mf_optcache_add(struct mf_option_cache *tab, size_t size, int flags); | 314 | void mf_optcache_add(struct mf_option_cache *tab, size_t size, int flags); |
305 | int mf_optcache_set_option(char *name, char *value); | 315 | int mf_optcache_set_option(char const *name, char const *value); |
306 | void mf_optcache_flush(void); | 316 | void mf_optcache_flush(void); |
307 | int mf_option_string(char *opt, void **pval, char *newval); | 317 | int mf_option_string(char const *opt, union mf_option_value *val, char const *arg); |
308 | int mf_option_boolean(char *opt, void **pval, char *newval); | 318 | int mf_option_boolean(char const *opt, union mf_option_value *val, char const *arg); |
309 | int mf_option_time(char *opt, void **pval, char *newval); | 319 | int mf_option_timeout(char const *opt, union mf_option_value *val, char const *arg); |
310 | int mf_option_time_t(char *opt, void **pval, char *newval); | 320 | int mf_option_size(char const *opt, union mf_option_value *val, char const *arg); |
311 | int mf_option_size_t(char *opt, void **pval, char *newval); | 321 | |
312 | 322 | ||
313 | void mf_init_lock_options(void); | 323 | void mf_init_lock_options(void); |
314 | 324 | ||
diff --git a/lib/optcache.c b/lib/optcache.c index a059b29a..15b011b5 100644 --- a/lib/optcache.c +++ b/lib/optcache.c | |||
@@ -42,6 +42,7 @@ optcache_dup(struct mf_option_cache *tab, size_t size) | |||
42 | for (i = 0; i < size; i++) { | 42 | for (i = 0; i < size; i++) { |
43 | newtab[i] = tab[i]; | 43 | newtab[i] = tab[i]; |
44 | newtab[i].name = mu_strdup(newtab[i].name); | 44 | newtab[i].name = mu_strdup(newtab[i].name); |
45 | newtab[i].isset = 0; | ||
45 | } | 46 | } |
46 | return newtab; | 47 | return newtab; |
47 | } | 48 | } |
@@ -72,7 +73,7 @@ mf_optcache_add(struct mf_option_cache *tab, size_t size, int flags) | |||
72 | } | 73 | } |
73 | 74 | ||
74 | static struct mf_option_cache * | 75 | static struct mf_option_cache * |
75 | find_option(char *name) | 76 | find_option(char const *name) |
76 | { | 77 | { |
77 | struct cache_list_elt *elt; | 78 | struct cache_list_elt *elt; |
78 | 79 | ||
@@ -89,14 +90,18 @@ find_option(char *name) | |||
89 | } | 90 | } |
90 | 91 | ||
91 | int | 92 | int |
92 | mf_optcache_set_option(char *name, char *value) | 93 | mf_optcache_set_option(char const *name, char const *value) |
93 | { | 94 | { |
95 | int rc; | ||
94 | struct mf_option_cache *p = find_option(name); | 96 | struct mf_option_cache *p = find_option(name); |
95 | if (!p) { | 97 | if (!p) { |
96 | errno = ENOENT; | 98 | errno = ENOENT; |
97 | return 1; | 99 | return 1; |
98 | } | 100 | } |
99 | return p->handler(name, &p->value, value); | 101 | rc = p->handler(name, &p->value, value); |
102 | if (rc == 0) | ||
103 | p->isset = 1; | ||
104 | return rc; | ||
100 | } | 105 | } |
101 | 106 | ||
102 | void | 107 | void |
@@ -109,79 +114,70 @@ mf_optcache_flush() | |||
109 | struct mf_option_cache *opt; | 114 | struct mf_option_cache *opt; |
110 | 115 | ||
111 | for (i = 0, opt = elt->opt; i < elt->size; i++, opt++) | 116 | for (i = 0, opt = elt->opt; i < elt->size; i++, opt++) |
112 | if (opt->value && opt->set) | 117 | if (opt->isset && opt->set) |
113 | opt->set(opt->value); | 118 | opt->set(&opt->value); |
114 | } | 119 | } |
115 | } | 120 | } |
116 | 121 | ||
117 | 122 | ||
118 | int | 123 | int |
119 | mf_option_string(char *opt, void **pval, char *newval) | 124 | mf_option_string(char const *opt, union mf_option_value *val, char const *arg) |
120 | { | 125 | { |
121 | if (*pval) | 126 | free(val->ov_string); |
122 | free(*pval); | 127 | val->ov_string = strdup(arg); |
123 | *pval = strdup(newval); | ||
124 | return 0; | 128 | return 0; |
125 | } | 129 | } |
126 | 130 | ||
127 | int | 131 | int |
128 | mf_option_boolean(char *opt, void **pval, char *newval) | 132 | mf_option_boolean(char const *opt, union mf_option_value *val, char const *arg) |
129 | { | 133 | { |
130 | int val; | 134 | int b; |
131 | 135 | ||
132 | if (strcmp (newval, "yes") == 0 | 136 | if (strcmp (arg, "yes") == 0 |
133 | || strcmp (newval, "true") == 0 | 137 | || strcmp (arg, "true") == 0 |
134 | || strcmp (newval, "t") == 0) | 138 | || strcmp (arg, "t") == 0) |
135 | val = 1; | 139 | b = 1; |
136 | else if (strcmp (newval, "no") == 0 | 140 | else if (strcmp (arg, "no") == 0 |
137 | || strcmp (newval, "false") == 0 | 141 | || strcmp (arg, "false") == 0 |
138 | || strcmp (newval, "nil") == 0) | 142 | || strcmp (arg, "nil") == 0) |
139 | val = 0; | 143 | b = 0; |
140 | else { | 144 | else { |
141 | char *p; | 145 | char *p; |
142 | 146 | ||
143 | val = strtoul(newval, &p, 10); | 147 | b = strtoul(arg, &p, 10); |
144 | if (*p) { | 148 | if (*p) { |
145 | errno = EINVAL; | 149 | errno = EINVAL; |
146 | return 1; | 150 | return 1; |
147 | } | 151 | } |
148 | } | 152 | } |
149 | 153 | ||
150 | *pval = (void*) val; | 154 | val->ov_bool = b; |
151 | return 0; | 155 | return 0; |
152 | } | 156 | } |
153 | 157 | ||
154 | int | 158 | int |
155 | mf_option_time(char *opt, void **pval, char *newval) | 159 | mf_option_timeout(char const *opt, union mf_option_value *val, char const *arg) |
156 | { | 160 | { |
157 | time_t interval; | 161 | time_t interval; |
158 | const char *endp; | 162 | const char *endp; |
159 | if (parse_time_interval(newval, &interval, &endp)) { | 163 | |
164 | if (parse_time_interval(arg, &interval, &endp)) { | ||
160 | mu_error(_("%s: unrecognized time format (near `%s')"), | 165 | mu_error(_("%s: unrecognized time format (near `%s')"), |
161 | opt, endp); | 166 | opt, endp); |
162 | return 1; | 167 | return 1; |
163 | } | 168 | } |
164 | if (!*pval) | 169 | val->ov_time = interval; |
165 | *pval = mu_alloc(sizeof(time_t)); | ||
166 | *(time_t*) *pval = interval; | ||
167 | return 0; | 170 | return 0; |
168 | } | 171 | } |
169 | 172 | ||
170 | int | 173 | int |
171 | mf_option_time_t(char *opt, void **pval, char *newval) | 174 | mf_option_size(char const *opt, union mf_option_value *val, char const *arg) |
172 | { | 175 | { |
173 | if (!*pval) | 176 | char *p; |
174 | *pval = mu_alloc(sizeof(time_t)); | 177 | unsigned long n = strtoul(arg, &p, 10); |
175 | memcpy(*pval, newval, sizeof(time_t)); | 178 | if (*p) |
176 | return 0; | 179 | return 1; |
177 | } | 180 | val->ov_size = n; |
178 |