GNUnet  0.10.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 generate a configuration file that is clean and only contains the deltas to the defaults? 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 83 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().

87 {
88  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
89 
90  (void) section;
91  if (is_filename)
92  {
93  char *value_fn;
94  char *fn;
95 
98  section,
99  option,
100  &value_fn));
101  fn = GNUNET_STRINGS_filename_expand (value_fn);
102  if (NULL == fn)
103  fn = value_fn;
104  else
105  GNUNET_free (value_fn);
106  fprintf (stdout, "%s = %s\n", option, fn);
107  GNUNET_free (fn);
108  }
109  else
110  {
111  fprintf (stdout, "%s = %s\n", option, value);
112  }
113 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
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 struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
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:602
configuration data
Definition: configuration.c:85
int 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.
#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 123 of file gnunet-config.c.

Referenced by run().

124 {
125  (void) cls;
126  fprintf (stdout, "%s\n", section);
127 }
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 140 of file gnunet-config.c.

References _, backend_check, cleanup(), config_file, DIR_SEPARATOR_STR, getenv(), global_ret, GNUNET_asprintf(), GNUNET_CONFIGURATION_create(), GNUNET_CONFIGURATION_destroy(), GNUNET_CONFIGURATION_dup(), GNUNET_CONFIGURATION_get_diff(), 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_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_free_non_null, GNUNET_log_config_missing(), GNUNET_OK, GNUNET_OS_project_data_get(), GNUNET_PLUGIN_test(), GNUNET_strdup, is_filename, list_sections, name, option, print_option(), print_section_name(), rewrite, section, and value.

Referenced by main().

144 {
145  struct GNUNET_CONFIGURATION_Handle *out = NULL;
146  struct GNUNET_CONFIGURATION_Handle *diff = NULL;
147  char *cfg_fn;
148 
149  (void) cls;
150  (void) args;
151  if (NULL != backend_check)
152  {
153  char *name;
154 
155  GNUNET_asprintf (&name, "libgnunet_plugin_%s", backend_check);
156  global_ret = (GNUNET_OK == GNUNET_PLUGIN_test (name)) ? 0 : 77;
157  GNUNET_free (name);
158  return;
159  }
160  if (rewrite)
161  {
162  struct GNUNET_CONFIGURATION_Handle *def;
163 
165  if (GNUNET_OK != GNUNET_CONFIGURATION_load (def, NULL))
166  {
167  fprintf (stderr, _ ("failed to load configuration defaults"));
168  global_ret = 1;
169  return;
170  }
171  diff = GNUNET_CONFIGURATION_get_diff (def, cfg);
172  cfg = diff;
173  }
174  if (((! rewrite) && (NULL == section)) || list_sections)
175  {
176  if (! list_sections)
177  {
178  fprintf (stderr,
179  _ ("%s or %s argument is required\n"),
180  "--section",
181  "--list-sections");
182  global_ret = 1;
183  }
184  else
185  {
186  fprintf (stderr, _ ("The following sections are available:\n"));
188  }
189  goto cleanup;
190  }
191 
192  if ((NULL != section) && (NULL == value))
193  {
194  if (NULL == option)
195  {
197  section,
198  &print_option,
199  (void *) cfg);
200  }
201  else
202  {
203  if (is_filename)
204  {
206  section,
207  option,
208  &value))
209  {
211  global_ret = 3;
212  goto cleanup;
213  }
214  }
215  else
216  {
218  section,
219  option,
220  &value))
221  {
223  global_ret = 3;
224  goto cleanup;
225  }
226  }
227  fprintf (stdout, "%s\n", value);
228  }
229  }
230  else if (NULL != section)
231  {
232  if (NULL == option)
233  {
234  fprintf (stderr, _ ("--option argument required to set value\n"));
235  global_ret = 1;
236  goto cleanup;
237  }
238  out = GNUNET_CONFIGURATION_dup (cfg);
240  }
241  cfg_fn = NULL;
242  if (NULL == cfgfile)
243  {
244  const char *xdg = getenv ("XDG_CONFIG_HOME");
245  if (NULL != xdg)
246  GNUNET_asprintf (&cfg_fn,
247  "%s%s%s",
248  xdg,
251  else
252  cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
253  cfgfile = cfg_fn;
254  }
255  if ((NULL != diff) || (NULL != out))
256  {
257  if (GNUNET_OK !=
258  GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out, cfgfile))
259  global_ret = 2;
260  }
261  GNUNET_free_non_null (cfg_fn);
262  if (NULL != out)
264 cleanup:
265  if (NULL != diff)
267 }
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.
int GNUNET_CONFIGURATION_write(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Write configuration file.
static char * config_file
Set to the name of the config file used.
Definition: gnunet-arm.c:79
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
static int is_filename
Treat option as a filename.
Definition: gnunet-config.c:55
int GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
static char * section
Name of the section.
Definition: gnunet-config.c:33
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_get_diff(const struct GNUNET_CONFIGURATION_Handle *cfg_default, const struct GNUNET_CONFIGURATION_Handle *cfg_new)
Compute configuration with only entries that have been changed.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
static char * option
Name of the option.
Definition: gnunet-config.c:38
static void cleanup(void *cls)
Function scheduled as very last function, cleans up after us.
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 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.
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
const char * name
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:85
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)
int 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 rewrite
Should we generate a configuration file that is clean and only contains the deltas to the defaults...
Definition: gnunet-config.c:71
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:83
#define DIR_SEPARATOR_STR
Definition: plibc.h:632
int GNUNET_PLUGIN_test(const char *library_name)
Test if a plugin exists.
Definition: plugin.c:169
#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 278 of file gnunet-config.c.

References backend_check, gettext_noop, global_ret, GNUNET_free, 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.

279 {
282  'f',
283  "filename",
284  gettext_noop ("interpret option value as a filename (with $-expansion)"),
285  &is_filename),
287  'b',
288  "supported-backend",
289  "BACKEND",
290  gettext_noop (
291  "test if the current installation supports the specified BACKEND"),
292  &backend_check)),
294  "section",
295  "SECTION",
296  gettext_noop (
297  "name of the section to access"),
298  &section),
300  "option",
301  "OPTION",
302  gettext_noop ("name of the option to access"),
303  &option),
305  "value",
306  "VALUE",
307  gettext_noop ("value to set"),
308  &value),
310  "list-sections",
311  gettext_noop (
312  "print available configuration sections"),
313  &list_sections),
315  'w',
316  "rewrite",
317  gettext_noop (
318  "write configuration file that only contains delta to defaults"),
319  &rewrite),
321  int ret;
322 
323  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
324  return 2;
325 
326  ret =
327  GNUNET_PROGRAM_run (argc,
328  argv,
329  "gnunet-config [OPTIONS]",
330  gettext_noop ("Manipulate GNUnet configuration files"),
331  options,
332  &run,
333  NULL);
334  GNUNET_free ((void *) argv);
335  if (GNUNET_OK == ret)
336  return global_ret;
337  return ret;
338 }
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:1521
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
Definition of a command line option.
static int ret
Final status code.
Definition: gnunet-arm.c:89
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.
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 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 generate a configuration file that is clean and only contains the deltas to the defaults...
Definition: gnunet-config.c:71
int 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:361
#define GNUNET_free(ptr)
Wrapper around free.
#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(), and run().

◆ option

char* option
static

Name of the option.

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

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

◆ 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 generate a configuration file that is clean and only contains the deltas to the defaults?

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

Referenced by main(), and run().