GNUnet  0.19.5
configuration_helper.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2006, 2007, 2008, 2009, 2013, 2020, 2021 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 
37 static void
39  const char *section,
40  const char *option,
41  const char *value)
42 {
43  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
44 
45  char *value_fn;
46  char *fn;
47 
50  section,
51  option,
52  &value_fn));
53  fn = GNUNET_STRINGS_filename_expand (value_fn);
54  if (NULL == fn)
55  fn = value_fn;
56  else
57  GNUNET_free (value_fn);
58  fprintf (stdout,
59  "%s = %s\n",
60  option,
61  fn);
62  GNUNET_free (fn);
63 }
64 
65 
74 static void
75 print_option (void *cls,
76  const char *section,
77  const char *option,
78  const char *value)
79 {
80  (void) cls;
81  (void) section;
82 
83  fprintf (stdout,
84  "%s = %s\n",
85  option,
86  value);
87 }
88 
89 
96 static void
97 print_section_name (void *cls,
98  const char *section)
99 {
100  (void) cls;
101  fprintf (stdout,
102  "%s\n",
103  section);
104 }
105 
106 
107 void
109  void *cls,
110  char *const *args,
111  const char *cfgfile,
112  const struct GNUNET_CONFIGURATION_Handle *cfg)
113 {
114  struct GNUNET_CONFIGURATION_ConfigSettings *cs = cls;
115  struct GNUNET_CONFIGURATION_Handle *out = NULL;
116  struct GNUNET_CONFIGURATION_Handle *ncfg = NULL;
117 
118  (void) args;
119  if (cs->diagnostics)
120  {
121  /* Re-parse the configuration with diagnostics enabled. */
122  ncfg = GNUNET_CONFIGURATION_create ();
125  cfgfile);
126  cfg = ncfg;
127  }
128 
129  if (cs->full)
130  cs->rewrite = GNUNET_YES;
131  if (cs->list_sections)
132  {
133  fprintf (stderr,
134  _ ("The following sections are available:\n"));
137  NULL);
138  return;
139  }
140  if ( (! cs->rewrite) &&
141  (NULL == cs->section) )
142  {
143  char *serialization;
144 
145  if (! cs->diagnostics)
146  {
147  fprintf (stderr,
148  _ ("%s, %s or %s argument is required\n"),
149  "--section",
150  "--list-sections",
151  "--diagnostics");
153  return;
154  }
156  fprintf (stdout,
157  "%s",
158  serialization);
159  GNUNET_free (serialization);
160  }
161  else if ( (NULL != cs->section) &&
162  (NULL == cs->value) )
163  {
164  if (NULL == cs->option)
165  {
167  cfg,
168  cs->section,
169  cs->is_filename
171  : &print_option,
172  (void *) cfg);
173  }
174  else
175  {
176  char *value;
177 
178  if (cs->is_filename)
179  {
180  if (GNUNET_OK !=
182  cs->section,
183  cs->option,
184  &value))
185  {
187  cs->section,
188  cs->option);
190  return;
191  }
192  }
193  else
194  {
195  if (GNUNET_OK !=
197  cs->section,
198  cs->option,
199  &value))
200  {
202  cs->section,
203  cs->option);
205  return;
206  }
207  }
208  fprintf (stdout,
209  "%s\n",
210  value);
211  GNUNET_free (value);
212  }
213  }
214  else if (NULL != cs->section)
215  {
216  if (NULL == cs->option)
217  {
218  fprintf (stderr,
219  _ ("--option argument required to set value\n"));
221  return;
222  }
225  cs->section,
226  cs->option,
227  cs->value);
228  cs->rewrite = GNUNET_YES;
229  }
230  if (cs->rewrite)
231  {
232  char *cfg_fn = NULL;
233 
234  if (NULL == out)
236 
237  if (NULL == cfgfile)
238  {
239  const char *xdg = getenv ("XDG_CONFIG_HOME");
240 
241  if (NULL != xdg)
242  GNUNET_asprintf (&cfg_fn,
243  "%s%s%s",
244  xdg,
247  else
248  cfg_fn = GNUNET_strdup (
249  GNUNET_OS_project_data_get ()->user_config_file);
250  cfgfile = cfg_fn;
251  }
252 
253  if (! cs->full)
254  {
255  struct GNUNET_CONFIGURATION_Handle *def;
256 
258  if (GNUNET_OK !=
260  NULL))
261  {
262  fprintf (stderr,
263  _ ("failed to load configuration defaults"));
264  cs->global_ret = 1;
267  GNUNET_free (cfg_fn);
268  return;
269  }
270  if (GNUNET_OK !=
272  out,
273  cfgfile))
274  cs->global_ret = 2;
276  }
277  else
278  {
279  if (GNUNET_OK !=
281  cfgfile))
282  cs->global_ret = 2;
283  }
284  GNUNET_free (cfg_fn);
285  }
286  if (NULL != out)
288  if (NULL != ncfg)
290 }
291 
292 
293 void
296 {
297  GNUNET_free (cs->option);
298  GNUNET_free (cs->section);
299  GNUNET_free (cs->value);
300 }
301 
302 
303 /* end of configuration_helper.c */
static void print_filename_option(void *cls, const char *section, const char *option, const char *value)
Print each option in a given section as a filename.
static void print_section_name(void *cls, const char *section)
Print out given section name.
static void print_option(void *cls, const char *section, const char *option, const char *value)
Print each option in a given section.
char * getenv()
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static char * config_file
Set to the name of the config file used.
Definition: gnunet-arm.c:84
static char * value
Value of the record to add/remove.
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.
char * GNUNET_CONFIGURATION_serialize_diagnostics(const struct GNUNET_CONFIGURATION_Handle *cfg)
Serializes the given configuration with diagnostics information.
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.
void GNUNET_CONFIGURATION_iterate_sections(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_CONFIGURATION_SectionIterator iter, void *iter_cls)
Iterate over all sections in the configuration.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
void GNUNET_CONFIGURATION_config_settings_free(struct GNUNET_CONFIGURATION_ConfigSettings *cs)
Free resources associated with cs.
void GNUNET_CONFIGURATION_config_tool_run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main task to run to perform operations typical for gnunet-config as per the configuration settings gi...
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_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_write(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Write configuration file.
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_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.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_dup(const struct GNUNET_CONFIGURATION_Handle *cfg)
Duplicate an existing configuration object.
void GNUNET_CONFIGURATION_enable_diagnostics(struct GNUNET_CONFIGURATION_Handle *cfg)
Enable extra diagnostics.
@ GNUNET_OK
@ GNUNET_YES
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
@ GNUNET_ERROR_TYPE_ERROR
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_free(ptr)
Wrapper around free.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_get(void)
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition: strings.c:494
#define DIR_SEPARATOR_STR
Definition: platform.h:166
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
#define EXIT_INVALIDARGUMENT
Definition: platform.h:253
#define EXIT_NOTCONFIGURED
Definition: platform.h:269
Closure for GNUNET_CONFIGURATION_config_tool_run() with settings for what should be done with the con...
int full
Should the generated configuration file contain the whole configuration?
int global_ret
Return value from the operation, to be returned from 'main'.
int diagnostics
Should we give extra diagnostics?
int list_sections
Whether to show the sections.
int rewrite
Should we write out the configuration file, even if no value was changed?
int is_filename
Treat option as a filename.