GNUnet 0.22.0
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
37static 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
74static void
75print_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
96static void
98 const char *section)
99{
100 (void) cls;
101 fprintf (stdout,
102 "%s\n",
103 section);
104}
105
106
107void
109 void *cls,
110 char *const *args,
111 const char *cfgfile,
112 const struct GNUNET_CONFIGURATION_Handle *cfg)
113{
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. */
125 cfgfile))
126 {
127 fprintf (stderr,
128 _("Failed to load config file `%s'"), cfgfile);
129 return;
130 }
131 cfg = ncfg;
132 }
133
134 if (cs->full)
135 cs->rewrite = GNUNET_YES;
136 if (cs->list_sections)
137 {
138 fprintf (stderr,
139 _ ("The following sections are available:\n"));
142 NULL);
143 return;
144 }
145 if ( (! cs->rewrite) &&
146 (NULL == cs->section) )
147 {
148 char *serialization;
149
150 if (! cs->diagnostics)
151 {
152 fprintf (stderr,
153 _ ("%s, %s or %s argument is required\n"),
154 "--section",
155 "--list-sections",
156 "--diagnostics");
158 return;
159 }
161 fprintf (stdout,
162 "%s",
163 serialization);
164 GNUNET_free (serialization);
165 }
166 else if ( (NULL != cs->section) &&
167 (NULL == cs->value) )
168 {
169 if (NULL == cs->option)
170 {
172 cfg,
173 cs->section,
174 cs->is_filename
176 : &print_option,
177 (void *) cfg);
178 }
179 else
180 {
181 char *value;
182
183 if (cs->is_filename)
184 {
185 if (GNUNET_OK !=
187 cs->section,
188 cs->option,
189 &value))
190 {
192 cs->section,
193 cs->option);
195 return;
196 }
197 }
198 else
199 {
200 if (GNUNET_OK !=
202 cs->section,
203 cs->option,
204 &value))
205 {
207 cs->section,
208 cs->option);
210 return;
211 }
212 }
213 fprintf (stdout,
214 "%s\n",
215 value);
217 }
218 }
219 else if (NULL != cs->section)
220 {
221 if (NULL == cs->option)
222 {
223 fprintf (stderr,
224 _ ("--option argument required to set value\n"));
226 return;
227 }
230 cs->section,
231 cs->option,
232 cs->value);
233 cs->rewrite = GNUNET_YES;
234 }
235 if (cs->rewrite)
236 {
237 char *cfg_fn = NULL;
238
239 if (NULL == out)
241
242 if (NULL == cfgfile)
243 {
244 const char *xdg = getenv ("XDG_CONFIG_HOME");
245
246 if (NULL != xdg)
247 GNUNET_asprintf (&cfg_fn,
248 "%s%s%s",
249 xdg,
252 else
253 cfg_fn = GNUNET_strdup (
254 GNUNET_OS_project_data_get ()->user_config_file);
255 cfgfile = cfg_fn;
256 }
257
258 if (! cs->full)
259 {
260 struct GNUNET_CONFIGURATION_Handle *def;
261
263 if (GNUNET_OK !=
265 NULL))
266 {
267 fprintf (stderr,
268 _ ("failed to load configuration defaults"));
269 cs->global_ret = 1;
272 GNUNET_free (cfg_fn);
273 return;
274 }
275 if (GNUNET_OK !=
277 out,
278 cfgfile))
279 cs->global_ret = 2;
281 }
282 else
283 {
284 if (GNUNET_OK !=
286 cfgfile))
287 cs->global_ret = 2;
288 }
289 GNUNET_free (cfg_fn);
290 }
291 if (NULL != out)
293 if (NULL != ncfg)
295}
296
297
298void
301{
302 GNUNET_free (cs->option);
303 GNUNET_free (cs->section);
304 GNUNET_free (cs->value);
305}
306
307
308/* 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 struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
static char * config_file
Set to the name of the config file used.
Definition: gnunet-arm.c:83
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.
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_dup(const struct GNUNET_CONFIGURATION_Handle *cfg)
Duplicate an existing 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.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
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.
char * GNUNET_CONFIGURATION_serialize_diagnostics(const struct GNUNET_CONFIGURATION_Handle *cfg)
Serializes the given configuration with diagnostics information.
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:504
#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.