aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-05-02 08:42:33 +0200
committerChristian Grothoff <christian@grothoff.org>2019-05-02 08:42:33 +0200
commit0c2263e8cd3aec2a92809fa838b08ef0ef4a1388 (patch)
tree006d1bffb728ade9e127c90c60ed22cb923ca0c3 /src/util
parenteb799db1355cbecd3ddff953dfab1e10621d229d (diff)
downloadgnunet-0c2263e8cd3aec2a92809fa838b08ef0ef4a1388.tar.gz
gnunet-0c2263e8cd3aec2a92809fa838b08ef0ef4a1388.zip
reindenting with clang
Diffstat (limited to 'src/util')
-rw-r--r--src/util/program.c201
1 files changed, 79 insertions, 122 deletions
diff --git a/src/util/program.c b/src/util/program.c
index f22dcddbf..8a5b1c414 100644
--- a/src/util/program.c
+++ b/src/util/program.c
@@ -31,9 +31,10 @@
31#include "speedup.h" 31#include "speedup.h"
32#include <gcrypt.h> 32#include <gcrypt.h>
33 33
34#define LOG(kind,...) GNUNET_log_from (kind, "util-program", __VA_ARGS__) 34#define LOG(kind, ...) GNUNET_log_from (kind, "util-program", __VA_ARGS__)
35 35
36#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-program", syscall, filename) 36#define LOG_STRERROR_FILE(kind, syscall, filename) \
37 GNUNET_log_from_strerror_file (kind, "util-program", syscall, filename)
37 38
38/** 39/**
39 * Context for the command. 40 * Context for the command.
@@ -64,7 +65,6 @@ struct CommandContext
64 * Configuration to use. 65 * Configuration to use.
65 */ 66 */
66 const struct GNUNET_CONFIGURATION_Handle *cfg; 67 const struct GNUNET_CONFIGURATION_Handle *cfg;
67
68}; 68};
69 69
70 70
@@ -88,14 +88,10 @@ program_main (void *cls)
88{ 88{
89 struct CommandContext *cc = cls; 89 struct CommandContext *cc = cls;
90 90
91 GNUNET_SPEEDUP_start_(cc->cfg); 91 GNUNET_SPEEDUP_start_ (cc->cfg);
92 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, 92 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
93 NULL);
94 GNUNET_RESOLVER_connect (cc->cfg); 93 GNUNET_RESOLVER_connect (cc->cfg);
95 cc->task (cc->task_cls, 94 cc->task (cc->task_cls, cc->args, cc->cfgfile, cc->cfg);
96 cc->args,
97 cc->cfgfile,
98 cc->cfg);
99} 95}
100 96
101 97
@@ -107,8 +103,7 @@ program_main (void *cls)
107 * @param a2 second command line option 103 * @param a2 second command line option
108 */ 104 */
109static int 105static int
110cmd_sorter (const void *a1, 106cmd_sorter (const void *a1, const void *a2)
111 const void *a2)
112{ 107{
113 const struct GNUNET_GETOPT_CommandLineOption *c1 = a1; 108 const struct GNUNET_GETOPT_CommandLineOption *c1 = a1;
114 const struct GNUNET_GETOPT_CommandLineOption *c2 = a2; 109 const struct GNUNET_GETOPT_CommandLineOption *c2 = a2;
@@ -144,12 +139,12 @@ cmd_sorter (const void *a1,
144 */ 139 */
145int 140int
146GNUNET_PROGRAM_run2 (int argc, 141GNUNET_PROGRAM_run2 (int argc,
147 char *const *argv, 142 char *const *argv,
148 const char *binaryName, 143 const char *binaryName,
149 const char *binaryHelp, 144 const char *binaryHelp,
150 const struct GNUNET_GETOPT_CommandLineOption *options, 145 const struct GNUNET_GETOPT_CommandLineOption *options,
151 GNUNET_PROGRAM_Main task, 146 GNUNET_PROGRAM_Main task,
152 void *task_cls, 147 void *task_cls,
153 int run_without_scheduler) 148 int run_without_scheduler)
154{ 149{
155 struct CommandContext cc; 150 struct CommandContext cc;
@@ -166,13 +161,12 @@ GNUNET_PROGRAM_run2 (int argc,
166 unsigned long long skew_variance; 161 unsigned long long skew_variance;
167 long long clock_offset; 162 long long clock_offset;
168 struct GNUNET_CONFIGURATION_Handle *cfg; 163 struct GNUNET_CONFIGURATION_Handle *cfg;
169 struct GNUNET_GETOPT_CommandLineOption defoptions[] = { 164 struct GNUNET_GETOPT_CommandLineOption defoptions[] =
170 GNUNET_GETOPT_option_cfgfile (&cc.cfgfile), 165 {GNUNET_GETOPT_option_cfgfile (&cc.cfgfile),
171 GNUNET_GETOPT_option_help (binaryHelp), 166 GNUNET_GETOPT_option_help (binaryHelp),
172 GNUNET_GETOPT_option_loglevel (&loglev), 167 GNUNET_GETOPT_option_loglevel (&loglev),
173 GNUNET_GETOPT_option_logfile (&logfile), 168 GNUNET_GETOPT_option_logfile (&logfile),
174 GNUNET_GETOPT_option_version (PACKAGE_VERSION " " VCS_VERSION) 169 GNUNET_GETOPT_option_version (PACKAGE_VERSION " " VCS_VERSION)};
175 };
176 struct GNUNET_GETOPT_CommandLineOption *allopts; 170 struct GNUNET_GETOPT_CommandLineOption *allopts;
177 const char *gargs; 171 const char *gargs;
178 char *lpfx; 172 char *lpfx;
@@ -189,26 +183,16 @@ GNUNET_PROGRAM_run2 (int argc,
189 gargv = NULL; 183 gargv = NULL;
190 gargc = 0; 184 gargc = 0;
191 for (int i = 0; i < argc; i++) 185 for (int i = 0; i < argc; i++)
192 GNUNET_array_append (gargv, 186 GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i]));
193 gargc,
194 GNUNET_strdup (argv[i]));
195 cargs = GNUNET_strdup (gargs); 187 cargs = GNUNET_strdup (gargs);
196 for (char *tok = strtok (cargs, " "); 188 for (char *tok = strtok (cargs, " "); NULL != tok; tok = strtok (NULL, " "))
197 NULL != tok; 189 GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok));
198 tok = strtok (NULL, " "))
199 GNUNET_array_append (gargv,
200 gargc,
201 GNUNET_strdup (tok));
202 GNUNET_free (cargs); 190 GNUNET_free (cargs);
203 GNUNET_array_append (gargv, 191 GNUNET_array_append (gargv, gargc, NULL);
204 gargc,
205 NULL);
206 argv = (char *const *) gargv; 192 argv = (char *const *) gargv;
207 argc = gargc - 1; 193 argc = gargc - 1;
208 } 194 }
209 memset (&cc, 195 memset (&cc, 0, sizeof (cc));
210 0,
211 sizeof (cc));
212 loglev = NULL; 196 loglev = NULL;
213 cc.task = task; 197 cc.task = task;
214 cc.task_cls = task_cls; 198 cc.task_cls = task_cls;
@@ -219,8 +203,7 @@ GNUNET_PROGRAM_run2 (int argc,
219 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR); 203 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
220 if (NULL != path) 204 if (NULL != path)
221 { 205 {
222 BINDTEXTDOMAIN ("GNUnet", 206 BINDTEXTDOMAIN ("GNUnet", path);
223 path);
224 GNUNET_free (path); 207 GNUNET_free (path);
225 } 208 }
226 textdomain ("GNUnet"); 209 textdomain ("GNUnet");
@@ -229,20 +212,17 @@ GNUNET_PROGRAM_run2 (int argc,
229 while (NULL != options[cnt].name) 212 while (NULL != options[cnt].name)
230 cnt++; 213 cnt++;
231 allopts = 214 allopts =
232 GNUNET_malloc ((cnt + 215 GNUNET_malloc ((cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption) +
233 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption) + 216 sizeof (defoptions));
234 sizeof (defoptions)); 217 GNUNET_memcpy (allopts, defoptions, sizeof (defoptions));
235 GNUNET_memcpy (allopts, 218 GNUNET_memcpy (&allopts[sizeof (defoptions) /
236 defoptions, 219 sizeof (struct GNUNET_GETOPT_CommandLineOption)],
237 sizeof (defoptions)); 220 options,
238 GNUNET_memcpy (&allopts 221 (cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption));
239 [sizeof (defoptions) /
240 sizeof (struct GNUNET_GETOPT_CommandLineOption)], options,
241 (cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption));
242 cnt += sizeof (defoptions) / sizeof (struct GNUNET_GETOPT_CommandLineOption); 222 cnt += sizeof (defoptions) / sizeof (struct GNUNET_GETOPT_CommandLineOption);
243 qsort (allopts, 223 qsort (allopts,
244 cnt, 224 cnt,
245 sizeof (struct GNUNET_GETOPT_CommandLineOption), 225 sizeof (struct GNUNET_GETOPT_CommandLineOption),
246 &cmd_sorter); 226 &cmd_sorter);
247 loglev = NULL; 227 loglev = NULL;
248 xdg = getenv ("XDG_CONFIG_HOME"); 228 xdg = getenv ("XDG_CONFIG_HOME");
@@ -257,15 +237,9 @@ GNUNET_PROGRAM_run2 (int argc,
257 lpfx = GNUNET_strdup (binaryName); 237 lpfx = GNUNET_strdup (binaryName);
258 if (NULL != (spc = strstr (lpfx, " "))) 238 if (NULL != (spc = strstr (lpfx, " ")))
259 *spc = '\0'; 239 *spc = '\0';
260 ret = GNUNET_GETOPT_run (binaryName, 240 ret = GNUNET_GETOPT_run (binaryName, allopts, (unsigned int) argc, argv);
261 allopts,
262 (unsigned int) argc,
263 argv);
264 if ((GNUNET_OK > ret) || 241 if ((GNUNET_OK > ret) ||
265 (GNUNET_OK != 242 (GNUNET_OK != GNUNET_log_setup (lpfx, loglev, logfile)))
266 GNUNET_log_setup (lpfx,
267 loglev,
268 logfile)))
269 { 243 {
270 GNUNET_free (allopts); 244 GNUNET_free (allopts);
271 GNUNET_free (lpfx); 245 GNUNET_free (lpfx);
@@ -273,15 +247,13 @@ GNUNET_PROGRAM_run2 (int argc,
273 } 247 }
274 if (NULL != cc.cfgfile) 248 if (NULL != cc.cfgfile)
275 { 249 {
276 if ( (GNUNET_YES != 250 if ((GNUNET_YES != GNUNET_DISK_file_test (cc.cfgfile)) ||
277 GNUNET_DISK_file_test (cc.cfgfile)) || 251 (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, cc.cfgfile)))
278 (GNUNET_SYSERR ==
279 GNUNET_CONFIGURATION_load (cfg,
280 cc.cfgfile)) )
281 { 252 {
282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
283 _("Unreadable or malformed configuration file `%s', exit ...\n"), 254 _ (
284 cc.cfgfile); 255 "Unreadable or malformed configuration file `%s', exit ...\n"),
256 cc.cfgfile);
285 ret = GNUNET_SYSERR; 257 ret = GNUNET_SYSERR;
286 GNUNET_free (allopts); 258 GNUNET_free (allopts);
287 GNUNET_free (lpfx); 259 GNUNET_free (lpfx);
@@ -290,51 +262,46 @@ GNUNET_PROGRAM_run2 (int argc,
290 } 262 }
291 else 263 else
292 { 264 {
293 if (GNUNET_YES == 265 if (GNUNET_YES == GNUNET_DISK_file_test (cfg_fn))
294 GNUNET_DISK_file_test (cfg_fn))
295 { 266 {
296 if (GNUNET_SYSERR == 267 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, cfg_fn))
297 GNUNET_CONFIGURATION_load (cfg,
298 cfg_fn))
299 { 268 {
300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 269 GNUNET_log (
301 _("Unreadable or malformed default configuration file `%s', exit ...\n"), 270 GNUNET_ERROR_TYPE_ERROR,
302 cfg_fn); 271 _ (
303 ret = GNUNET_SYSERR; 272 "Unreadable or malformed default configuration file `%s', exit ...\n"),
304 GNUNET_free (allopts); 273 cfg_fn);
305 GNUNET_free (lpfx); 274 ret = GNUNET_SYSERR;
306 goto cleanup; 275 GNUNET_free (allopts);
276 GNUNET_free (lpfx);
277 goto cleanup;
307 } 278 }
308 } 279 }
309 else 280 else
310 { 281 {
311 GNUNET_free (cfg_fn); 282 GNUNET_free (cfg_fn);
312 cfg_fn = NULL; 283 cfg_fn = NULL;
313 if (GNUNET_OK != 284 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, NULL))
314 GNUNET_CONFIGURATION_load (cfg,
315 NULL))
316 { 285 {
317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
318 _("Unreadable or malformed configuration, exit ...\n")); 287 _ ("Unreadable or malformed configuration, exit ...\n"));
319 ret = GNUNET_SYSERR; 288 ret = GNUNET_SYSERR;
320 GNUNET_free (allopts); 289 GNUNET_free (allopts);
321 GNUNET_free (lpfx); 290 GNUNET_free (lpfx);
322 goto cleanup; 291 goto cleanup;
323 } 292 }
324 } 293 }
325 } 294 }
326 GNUNET_free (allopts); 295 GNUNET_free (allopts);
327 GNUNET_free (lpfx); 296 GNUNET_free (lpfx);
328 if (GNUNET_OK == 297 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cc.cfg,
329 GNUNET_CONFIGURATION_get_value_number (cc.cfg, 298 "testing",
330 "testing", 299 "skew_offset",
331 "skew_offset", 300 &skew_offset) &&
332 &skew_offset) && 301 (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cc.cfg,
333 (GNUNET_OK == 302 "testing",
334 GNUNET_CONFIGURATION_get_value_number (cc.cfg, 303 "skew_variance",
335 "testing", 304 &skew_variance)))
336 "skew_variance",
337 &skew_variance)))
338 { 305 {
339 clock_offset = skew_offset - skew_variance; 306 clock_offset = skew_offset - skew_variance;
340 GNUNET_TIME_set_offset (clock_offset); 307 GNUNET_TIME_set_offset (clock_offset);
@@ -345,43 +312,29 @@ GNUNET_PROGRAM_run2 (int argc,
345 in "cfg". This is typically really only having an effect if we 312 in "cfg". This is typically really only having an effect if we
346 are running code in src/arm/, as obviously the rest of the code 313 are running code in src/arm/, as obviously the rest of the code
347 has little business with ARM-specific options. */ 314 has little business with ARM-specific options. */
348 if (GNUNET_YES != 315 if (GNUNET_YES != GNUNET_CONFIGURATION_have_value (cfg, "arm", "CONFIG"))
349 GNUNET_CONFIGURATION_have_value (cfg,
350 "arm",
351 "CONFIG"))
352 { 316 {
353 if (NULL != cc.cfgfile) 317 if (NULL != cc.cfgfile)
354 GNUNET_CONFIGURATION_set_value_string (cfg, 318 GNUNET_CONFIGURATION_set_value_string (cfg, "arm", "CONFIG", cc.cfgfile);
355 "arm",
356 "CONFIG",
357 cc.cfgfile);
358 else if (NULL != cfg_fn) 319 else if (NULL != cfg_fn)
359 GNUNET_CONFIGURATION_set_value_string (cfg, 320 GNUNET_CONFIGURATION_set_value_string (cfg, "arm", "CONFIG", cfg_fn);
360 "arm",
361 "CONFIG",
362 cfg_fn);
363 } 321 }
364 322
365 /* run */ 323 /* run */
366 cc.args = &argv[ret]; 324 cc.args = &argv[ret];
367 if ( (NULL == cc.cfgfile) && 325 if ((NULL == cc.cfgfile) && (NULL != cfg_fn))
368 (NULL != cfg_fn) )
369 cc.cfgfile = GNUNET_strdup (cfg_fn); 326 cc.cfgfile = GNUNET_strdup (cfg_fn);
370 if (GNUNET_NO == run_without_scheduler) 327 if (GNUNET_NO == run_without_scheduler)
371 { 328 {
372 GNUNET_SCHEDULER_run (&program_main, 329 GNUNET_SCHEDULER_run (&program_main, &cc);
373 &cc);
374 } 330 }
375 else 331 else
376 { 332 {
377 GNUNET_RESOLVER_connect (cc.cfg); 333 GNUNET_RESOLVER_connect (cc.cfg);
378 cc.task (cc.task_cls, 334 cc.task (cc.task_cls, cc.args, cc.cfgfile, cc.cfg);
379 cc.args,
380 cc.cfgfile,
381 cc.cfg);
382 } 335 }
383 ret = GNUNET_OK; 336 ret = GNUNET_OK;
384 cleanup: 337cleanup:
385 GNUNET_CONFIGURATION_destroy (cfg); 338 GNUNET_CONFIGURATION_destroy (cfg);
386 GNUNET_free_non_null (cc.cfgfile); 339 GNUNET_free_non_null (cc.cfgfile);
387 GNUNET_free_non_null (cfg_fn); 340 GNUNET_free_non_null (cfg_fn);
@@ -405,17 +358,21 @@ GNUNET_PROGRAM_run2 (int argc,
405 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 358 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
406 */ 359 */
407int 360int
408GNUNET_PROGRAM_run (int argc, char *const *argv, 361GNUNET_PROGRAM_run (int argc,
362 char *const *argv,
409 const char *binaryName, 363 const char *binaryName,
410 const char *binaryHelp, 364 const char *binaryHelp,
411 const struct GNUNET_GETOPT_CommandLineOption *options, 365 const struct GNUNET_GETOPT_CommandLineOption *options,
412 GNUNET_PROGRAM_Main task, 366 GNUNET_PROGRAM_Main task,
413 void *task_cls) 367 void *task_cls)
414{ 368{
415 return GNUNET_PROGRAM_run2 (argc, argv, 369 return GNUNET_PROGRAM_run2 (argc,
416 binaryName, binaryHelp, 370 argv,
371 binaryName,
372 binaryHelp,
417 options, 373 options,
418 task, task_cls, 374 task,
375 task_cls,
419 GNUNET_NO); 376 GNUNET_NO);
420} 377}
421 378