GNUnet  0.11.x
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  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 
36 static void
38  const char *section,
39  const char *option,
40  const char *value)
41 {
42  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
43 
44  char *value_fn;
45  char *fn;
46 
49  section,
50  option,
51  &value_fn));
53  if (NULL == fn)
54  fn = value_fn;
55  else
56  GNUNET_free (value_fn);
57  fprintf (stdout,
58  "%s = %s\n",
59  option,
60  fn);
61  GNUNET_free (fn);
62 }
63 
64 
73 static void
74 print_option (void *cls,
75  const char *section,
76  const char *option,
77  const char *value)
78 {
79  (void) cls;
80  (void) section;
81 
82  fprintf (stdout,
83  "%s = %s\n",
84  option,
85  value);
86 }
87 
88 
95 static void
96 print_section_name (void *cls,
97  const char *section)
98 {
99  (void) cls;
100  fprintf (stdout,
101  "%s\n",
102  section);
103 }
104 
105 
106 void
108  void *cls,
109  char *const *args,
110  const char *cfgfile,
111  const struct GNUNET_CONFIGURATION_Handle *cfg)
112 {
113  struct GNUNET_CONFIGURATION_ConfigSettings *cs = cls;
114  struct GNUNET_CONFIGURATION_Handle *out = NULL;
115  struct GNUNET_CONFIGURATION_Handle *ncfg = NULL;
116 
117  (void) args;
118  if (cs->diagnostics)
119  {
120  /* Re-parse the configuration with diagnostics enabled. */
121  ncfg = GNUNET_CONFIGURATION_create ();
124  cfgfile);
125  cfg = ncfg;
126  }
127 
128  if (cs->full)
129  cs->rewrite = GNUNET_YES;
130  if (cs->list_sections)
131  {
132  fprintf (stderr,
133  _ ("The following sections are available:\n"));
136  NULL);
137  return;
138  }
139  if ( (! cs->rewrite) &&
140  (NULL == cs->section) )
141  {
142  char *serialization;
143 
144  if (! cs->diagnostics)
145  {
146  fprintf (stderr,
147  _ ("%s, %s or %s argument is required\n"),
148  "--section",
149  "--list-sections",
150  "--diagnostics");
152  return;
153  }
155  fprintf (stdout,
156  "%s",
157  serialization);
158  GNUNET_free (serialization);
159  }
160  else if ( (NULL != cs->section) &&
161  (NULL == cs->value) )
162  {
163  if (NULL == cs->option)
164  {
166  cfg,
167  cs->section,
168  cs->is_filename
170  : &print_option,
171  (void *) cfg);
172  }
173  else
174  {
175  char *value;
176 
177  if (cs->is_filename)
178  {
179  if (GNUNET_OK !=
181  cs->section,
182  cs->option,
183  &value))
184  {
186  cs->section,
187  cs->option);
189  return;
190  }
191  }
192  else
193  {
194  if (GNUNET_OK !=
196  cs->section,
197  cs->option,
198  &value))
199  {
201  cs->section,
202  cs->option);
204  return;
205  }
206  }
207  fprintf (stdout,
208  "%s\n",
209  value);
210  GNUNET_free (value);
211  }
212  }
213  else if (NULL != cs->section)
214  {
215  if (NULL == cs->option)
216  {
217  fprintf (stderr,
218  _ ("--option argument required to set value\n"));
220  return;
221  }
224  cs->section,
225  cs->option,
226  cs->value);
227  cs->rewrite = GNUNET_YES;
228  }
229  if (cs->rewrite)
230  {
231  char *cfg_fn = NULL;
232 
233  if (NULL == out)
235 
236  if (NULL == cfgfile)
237  {
238  const char *xdg = getenv ("XDG_CONFIG_HOME");
239 
240  if (NULL != xdg)
241  GNUNET_asprintf (&cfg_fn,
242  "%s%s%s",
243  xdg,
246  else
247  cfg_fn = GNUNET_strdup (
248  GNUNET_OS_project_data_get ()->user_config_file);
249  cfgfile = cfg_fn;
250  }
251 
252  if (! cs->full)
253  {
254  struct GNUNET_CONFIGURATION_Handle *def;
255 
257  if (GNUNET_OK !=
259  NULL))
260  {
261  fprintf (stderr,
262  _ ("failed to load configuration defaults"));
263  cs->global_ret = 1;
266  GNUNET_free (cfg_fn);
267  return;
268  }
269  if (GNUNET_OK !=
271  out,
272  cfgfile))
273  cs->global_ret = 2;
275  }
276  else
277  {
278  if (GNUNET_OK !=
280  cfgfile))
281  cs->global_ret = 2;
282  }
283  GNUNET_free (cfg_fn);
284  }
285  if (NULL != out)
287  if (NULL != ncfg)
289 }
290 
291 
292 void
295 {
296  GNUNET_free (cs->option);
297  GNUNET_free (cs->section);
298  GNUNET_free (cs->value);
299 }
300 
301 
302 /* 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.
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_YES
Definition: gnunet_common.h:97
static char * fn
Filename of the unique file.
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.
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.
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_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.
#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:482
#define DIR_SEPARATOR_STR
Definition: platform.h:165
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
#define EXIT_INVALIDARGUMENT
Definition: platform.h:252
#define EXIT_NOTCONFIGURED
Definition: platform.h:268
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.