GNUnet  0.10.x
gnunet-config.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2012 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  */
20 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 
29 
33 static char *section;
34 
38 static char *option;
39 
43 static char *value;
44 
50 static char *backend_check;
51 
55 static int is_filename;
56 
60 static int list_sections;
61 
65 static int global_ret;
66 
71 static int rewrite;
72 
73 
82 static void
83 print_option(void *cls,
84  const char *section,
85  const char *option,
86  const char *value)
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 }
114 
115 
122 static void
123 print_section_name(void *cls, const char *section)
124 {
125  (void)cls;
126  fprintf(stdout, "%s\n", section);
127 }
128 
129 
139 static void
140 run(void *cls,
141  char *const *args,
142  const char *cfgfile,
143  const struct GNUNET_CONFIGURATION_Handle *cfg)
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 }
268 
269 
277 int
278 main(int argc, char *const *argv)
279 {
280  struct GNUNET_GETOPT_CommandLineOption options[] =
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 }
339 
340 /* end of gnunet-config.c */
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.
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1439
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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:75
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
Definition of a command line option.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
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.
int GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
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.
static char * fn
Filename of the unique file.
#define DIR_SEPARATOR_STR
Definition: platform.h:168
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 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 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 struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
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
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition: strings.c:602
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 &#39;main&#39;.
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.
configuration data
Definition: configuration.c:83
int main(int argc, char *const *argv)
Program to manipulate configuration files.
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.
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
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:367
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
int GNUNET_PLUGIN_test(const char *library_name)
Test if a plugin exists.
Definition: plugin.c:165
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69