GNUnet  0.11.x
Functions | Variables
gnunet-config.c File Reference

tool to access and manipulate GNUnet configuration files More...

#include "platform.h"
#include "gnunet_util_lib.h"
Include dependency graph for gnunet-config.c:

Go to the source code of this file.

Functions

static void print_option (void *cls, const char *section, const char *option, const char *value)
 Print each option in a given section. More...
 
static void print_section_name (void *cls, const char *section)
 Print out given section name. More...
 
static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
 Main function that will be run by the scheduler. More...
 
int main (int argc, char *const *argv)
 Program to manipulate configuration files. More...
 

Variables

static char * section
 Name of the section. More...
 
static char * option
 Name of the option. More...
 
static char * value
 Value to set. More...
 
static char * backend_check
 Backend to check if the respective plugin is loadable. More...
 
static int is_filename
 Treat option as a filename. More...
 
static int list_sections
 Whether to show the sections. More...
 
static int global_ret
 Return value from 'main'. More...
 
static int rewrite
 Should we write out the configuration file, even if no value was changed? More...
 
static int full
 Should the generated configuration file contain the whole configuration? More...
 

Detailed Description

tool to access and manipulate GNUnet configuration files

Author
Christian Grothoff

Definition in file gnunet-config.c.

Function Documentation

◆ print_option()

static void print_option ( void *  cls,
const char *  section,
const char *  option,
const char *  value 
)
static

Print each option in a given section.

Parameters
clsclosure
sectionname of the section
optionname of the option
valuevalue of the option

Definition at line 87 of file gnunet-config.c.

References cfg, fn, GNUNET_assert, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_free, GNUNET_OK, GNUNET_STRINGS_filename_expand(), and is_filename.

Referenced by run().

91 {
92  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
93 
94  (void) section;
95  if (is_filename)
96  {
97  char *value_fn;
98  char *fn;
99 
102  section,
103  option,
104  &value_fn));
105  fn = GNUNET_STRINGS_filename_expand (value_fn);
106  if (NULL == fn)
107  fn = value_fn;
108  else
109  GNUNET_free (value_fn);
110  fprintf (stdout, "%s = %s\n", option, fn);
111  GNUNET_free (fn);
112  }
113  else
114  {
115  fprintf (stdout, "%s = %s\n", option, value);
116  }
117 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
static int is_filename
Treat option as a filename.
Definition: gnunet-config.c:55
static char * section
Name of the section.
Definition: gnunet-config.c:33
static char * fn
Filename of the unique file.
static char * option
Name of the option.
Definition: gnunet-config.c:38
static char * value
Value to set.
Definition: gnunet-config.c:43
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition: strings.c:618
configuration data
Definition: configuration.c:84
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_section_name()

static void print_section_name ( void *  cls,
const char *  section 
)
static

Print out given section name.

Parameters
clsunused
sectiona section in the configuration file

Definition at line 127 of file gnunet-config.c.

Referenced by run().

128 {
129  (void) cls;
130  fprintf (stdout, "%s\n", section);
131 }
static char * section
Name of the section.
Definition: gnunet-config.c:33
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
char *const *  args,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle cfg 
)
static

Main function that will be run by the scheduler.

Parameters
clsclosure
argsremaining command-line arguments
cfgfilename of the configuration file used (for saving, can be NULL!)
cfgconfiguration

Definition at line 144 of file gnunet-config.c.

References _, backend_check, config_file, DIR_SEPARATOR_STR, full, getenv(), global_ret, GNUNET_asprintf(), GNUNET_CONFIGURATION_create(), GNUNET_CONFIGURATION_destroy(), GNUNET_CONFIGURATION_dup(), GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_iterate_section_values(), GNUNET_CONFIGURATION_iterate_sections(), GNUNET_CONFIGURATION_load(), GNUNET_CONFIGURATION_set_value_string(), GNUNET_CONFIGURATION_write(), GNUNET_CONFIGURATION_write_diffs(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log_config_missing(), GNUNET_OK, GNUNET_OS_project_data_get(), GNUNET_PLUGIN_test(), GNUNET_strdup, GNUNET_YES, is_filename, list_sections, name, option, print_option(), print_section_name(), rewrite, section, and value.

Referenced by main().

148 {
149  struct GNUNET_CONFIGURATION_Handle *out = NULL;
150 
151  (void) cls;
152  (void) args;
153  if (NULL != backend_check)
154  {
155  char *name;
156 
157  GNUNET_asprintf (&name,
158  "libgnunet_plugin_%s",
159  backend_check);
160  global_ret = (GNUNET_OK ==
161  GNUNET_PLUGIN_test (name)) ? 0 : 77;
162  GNUNET_free (name);
163  return;
164  }
165  if (full)
167  if (list_sections)
168  {
169  fprintf (stderr,
170  _ ("The following sections are available:\n"));
173  NULL);
174  return;
175  }
176  if ( (! rewrite) &&
177  (NULL == section) )
178  {
179  fprintf (stderr,
180  _ ("%s or %s argument is required\n"),
181  "--section",
182  "--list-sections");
183  global_ret = 1;
184  return;
185  }
186 
187  if ( (NULL != section) &&
188  (NULL == value) )
189  {
190  if (NULL == option)
191  {
193  section,
194  &print_option,
195  (void *) cfg);
196  }
197  else
198  {
199  if (is_filename)
200  {
201  if (GNUNET_OK !=
203  section,
204  option,
205  &value))
206  {
208  section,
209  option);
210  global_ret = 3;
211  return;
212  }
213  }
214  else
215  {
216  if (GNUNET_OK !=
218  section,
219  option,
220  &value))
221  {
223  section,
224  option);
225  global_ret = 3;
226  return;
227  }
228  }
229  fprintf (stdout,
230  "%s\n",
231  value);
232  }
233  }
234  else if (NULL != section)
235  {
236  if (NULL == option)
237  {
238  fprintf (stderr,
239  _ ("--option argument required to set value\n"));
240  global_ret = 1;
241  return;
242  }
243  out = GNUNET_CONFIGURATION_dup (cfg);
245  section,
246  option,
247  value);
249  }
250  if (rewrite)
251  {
252  char *cfg_fn = NULL;
253 
254  if (NULL == cfgfile)
255  {
256  const char *xdg = getenv ("XDG_CONFIG_HOME");
257  if (NULL != xdg)
258  GNUNET_asprintf (&cfg_fn,
259  "%s%s%s",
260  xdg,
263  else
264  cfg_fn = GNUNET_strdup (
265  GNUNET_OS_project_data_get ()->user_config_file);
266  cfgfile = cfg_fn;
267  }
268 
269  if (! full)
270  {
271  struct GNUNET_CONFIGURATION_Handle *def;
272 
274  if (GNUNET_OK !=
276  NULL))
277  {
278  fprintf (stderr,
279  _ ("failed to load configuration defaults"));
280  global_ret = 1;
283  GNUNET_free (cfg_fn);
284  return;
285  }
286  if (GNUNET_OK !=
288  out,
289  cfgfile))
290  global_ret = 2;
292  }
293  else
294  {
295  if (GNUNET_OK !=
297  cfgfile))
298  global_ret = 2;
299  }
300  GNUNET_free (cfg_fn);
301  }
302  if (NULL != out)
304 }
void GNUNET_CONFIGURATION_iterate_section_values(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, GNUNET_CONFIGURATION_Iterator iter, void *iter_cls)
Iterate over values of a section in the configuration.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
static char * config_file
Set to the name of the config file used.
Definition: gnunet-arm.c:84
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_write_diffs(const struct GNUNET_CONFIGURATION_Handle *cfg_default, const struct GNUNET_CONFIGURATION_Handle *cfg_new, const char *filename)
Write only configuration entries that have been changed to configuration file.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_write(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Write configuration file.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
static int is_filename
Treat option as a filename.
Definition: gnunet-config.c:55
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
static char * section
Name of the section.
Definition: gnunet-config.c:33
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
#define DIR_SEPARATOR_STR
Definition: platform.h:165
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
static char * option
Name of the option.
Definition: gnunet-config.c:38
void GNUNET_CONFIGURATION_set_value_string(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value)
Set a configuration value that should be a string.
static void print_section_name(void *cls, const char *section)
Print out given section name.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
static int list_sections
Whether to show the sections.
Definition: gnunet-config.c:60
static char * value
Value to set.
Definition: gnunet-config.c:43
static int full
Should the generated configuration file contain the whole configuration?
Definition: gnunet-config.c:75
char * getenv()
static char * backend_check
Backend to check if the respective plugin is loadable.
Definition: gnunet-config.c:50
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_dup(const struct GNUNET_CONFIGURATION_Handle *cfg)
Duplicate an existing configuration object.
static int global_ret
Return value from 'main'.
Definition: gnunet-config.c:65
configuration data
Definition: configuration.c:84
const char * name
void GNUNET_CONFIGURATION_iterate_sections(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_CONFIGURATION_Section_Iterator iter, void *iter_cls)
Iterate over all sections in the configuration.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_get(void)
static int rewrite
Should we write out the configuration file, even if no value was changed?
Definition: gnunet-config.c:70
static void print_option(void *cls, const char *section, const char *option, const char *value)
Print each option in a given section.
Definition: gnunet-config.c:87
int GNUNET_PLUGIN_test(const char *library_name)
Test if a plugin exists.
Definition: plugin.c:175
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *const *  argv 
)

Program to manipulate configuration files.

Parameters
argcnumber of arguments from the command line
argvcommand line arguments
Returns
0 ok, 1 on error

Definition at line 315 of file gnunet-config.c.

References backend_check, full, gettext_noop, global_ret, GNUNET_free_nz, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_exclusive(), GNUNET_GETOPT_option_flag(), GNUNET_GETOPT_option_string(), GNUNET_OK, GNUNET_PROGRAM_run(), GNUNET_STRINGS_get_utf8_args(), is_filename, list_sections, option, ret, rewrite, run(), section, and value.

316 {
320  'b',
321  "supported-backend",
322  "BACKEND",
323  gettext_noop (
324  "test if the current installation supports the specified BACKEND"),
325  &backend_check)),
327  'F',
328  "full",
329  gettext_noop (
330  "write the full configuration file, including default values"),
331  &full),
333  'f',
334  "filename",
335  gettext_noop ("interpret option value as a filename (with $-expansion)"),
336  &is_filename),
338  "option",
339  "OPTION",
340  gettext_noop ("name of the option to access"),
341  &option),
343  'r',
344  "rewrite",
345  gettext_noop (
346  "rewrite the configuration file, even if nothing changed"),
347  &rewrite),
349  "list-sections",
350  gettext_noop (
351  "print available configuration sections"),
352  &list_sections),
354  "section",
355  "SECTION",
356  gettext_noop (
357  "name of the section to access"),
358  &section),
360  "value",
361  "VALUE",
362  gettext_noop ("value to set"),
363  &value),
365  };
366  int ret;
367 
368  if (GNUNET_OK !=
369  GNUNET_STRINGS_get_utf8_args (argc, argv,
370  &argc, &argv))
371  return 2;
372 
373  ret =
374  GNUNET_PROGRAM_run (argc,
375  argv,
376  "gnunet-config [OPTIONS]",
377  gettext_noop ("Manipulate GNUnet configuration files"),
378  options,
379  &run,
380  NULL);
381  GNUNET_free_nz ((void *) argv);
382  if (GNUNET_OK == ret)
383  return global_ret;
384  return ret;
385 }
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:363
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1459
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Definition of a command line option.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
static int is_filename
Treat option as a filename.
Definition: gnunet-config.c:55
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
static char * section
Name of the section.
Definition: gnunet-config.c:33
static char * option
Name of the option.
Definition: gnunet-config.c:38
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run by the scheduler.
#define GNUNET_free_nz(ptr)
Wrapper around free.
static int list_sections
Whether to show the sections.
Definition: gnunet-config.c:60
static char * value
Value to set.
Definition: gnunet-config.c:43
static int full
Should the generated configuration file contain the whole configuration?
Definition: gnunet-config.c:75
static char * backend_check
Backend to check if the respective plugin is loadable.
Definition: gnunet-config.c:50
static int global_ret
Return value from 'main'.
Definition: gnunet-config.c:65
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_exclusive(struct GNUNET_GETOPT_CommandLineOption opt)
Make the given option mutually exclusive with other options.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
static int rewrite
Should we write out the configuration file, even if no value was changed?
Definition: gnunet-config.c:70
#define gettext_noop(String)
Definition: gettext.h:69
Here is the call graph for this function:

Variable Documentation

◆ section

char* section
static

Name of the section.

Definition at line 33 of file gnunet-config.c.

Referenced by get_cont(), GNUNET_CONFIGURATION_deserialize(), main(), run(), and set_cont().

◆ option

char* option
static

Name of the option.

Definition at line 38 of file gnunet-config.c.

Referenced by main(), purge_cfg_dir(), run(), and set_cont().

◆ value

char* value
static

Value to set.

Definition at line 43 of file gnunet-config.c.

Referenced by main(), and run().

◆ backend_check

char* backend_check
static

Backend to check if the respective plugin is loadable.

NULL if no check is to be performed. The value is the "basename" of the plugin to load.

Definition at line 50 of file gnunet-config.c.

Referenced by main(), and run().

◆ is_filename

int is_filename
static

Treat option as a filename.

Definition at line 55 of file gnunet-config.c.

Referenced by main(), print_option(), and run().

◆ list_sections

int list_sections
static

Whether to show the sections.

Definition at line 60 of file gnunet-config.c.

Referenced by main(), and run().

◆ global_ret

int global_ret
static

Return value from 'main'.

Definition at line 65 of file gnunet-config.c.

Referenced by main(), and run().

◆ rewrite

int rewrite
static

Should we write out the configuration file, even if no value was changed?

Definition at line 70 of file gnunet-config.c.

Referenced by main(), and run().

◆ full

int full
static

Should the generated configuration file contain the whole configuration?

Definition at line 75 of file gnunet-config.c.

Referenced by main(), and run().