GNUnet  0.11.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 
70 static int rewrite;
71 
75 static int full;
76 
77 
86 static void
87 print_option (void *cls,
88  const char *section,
89  const char *option,
90  const char *value)
91 {
92  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
93 
94  (void) section;
95  if (is_filename)
96  {
97  char *value_fn;
98  char *fn;
99 
102  section,
103  option,
104  &value_fn));
105  fn = GNUNET_STRINGS_filename_expand (value_fn);
106  if (NULL == fn)
107  fn = value_fn;
108  else
109  GNUNET_free (value_fn);
110  fprintf (stdout, "%s = %s\n", option, fn);
111  GNUNET_free (fn);
112  }
113  else
114  {
115  fprintf (stdout, "%s = %s\n", option, value);
116  }
117 }
118 
119 
126 static void
127 print_section_name (void *cls, const char *section)
128 {
129  (void) cls;
130  fprintf (stdout, "%s\n", section);
131 }
132 
133 
143 static void
144 run (void *cls,
145  char *const *args,
146  const char *cfgfile,
147  const struct GNUNET_CONFIGURATION_Handle *cfg)
148 {
149  struct GNUNET_CONFIGURATION_Handle *out = NULL;
150 
151  (void) cls;
152  (void) args;
153  if (NULL != backend_check)
154  {
155  char *name;
156 
157  GNUNET_asprintf (&name,
158  "libgnunet_plugin_%s",
159  backend_check);
160  global_ret = (GNUNET_OK ==
161  GNUNET_PLUGIN_test (name)) ? 0 : 77;
162  GNUNET_free (name);
163  return;
164  }
165  if (full)
167  if (list_sections)
168  {
169  fprintf (stderr,
170  _ ("The following sections are available:\n"));
173  NULL);
174  return;
175  }
176  if ( (! rewrite) &&
177  (NULL == section) )
178  {
179  fprintf (stderr,
180  _ ("%s or %s argument is required\n"),
181  "--section",
182  "--list-sections");
183  global_ret = 1;
184  return;
185  }
186 
187  if ( (NULL != section) &&
188  (NULL == value) )
189  {
190  if (NULL == option)
191  {
193  section,
194  &print_option,
195  (void *) cfg);
196  }
197  else
198  {
199  if (is_filename)
200  {
201  if (GNUNET_OK !=
203  section,
204  option,
205  &value))
206  {
208  section,
209  option);
210  global_ret = 3;
211  return;
212  }
213  }
214  else
215  {
216  if (GNUNET_OK !=
218  section,
219  option,
220  &value))
221  {
223  section,
224  option);
225  global_ret = 3;
226  return;
227  }
228  }
229  fprintf (stdout,
230  "%s\n",
231  value);
232  }
233  }
234  else if (NULL != section)
235  {
236  if (NULL == option)
237  {
238  fprintf (stderr,
239  _ ("--option argument required to set value\n"));
240  global_ret = 1;
241  return;
242  }
243  out = GNUNET_CONFIGURATION_dup (cfg);
245  section,
246  option,
247  value);
249  }
250  if (rewrite)
251  {
252  char *cfg_fn = NULL;
253 
254  if (NULL == cfgfile)
255  {
256  const char *xdg = getenv ("XDG_CONFIG_HOME");
257  if (NULL != xdg)
258  GNUNET_asprintf (&cfg_fn,
259  "%s%s%s",
260  xdg,
263  else
264  cfg_fn = GNUNET_strdup (
265  GNUNET_OS_project_data_get ()->user_config_file);
266  cfgfile = cfg_fn;
267  }
268 
269  if (! full)
270  {
271  struct GNUNET_CONFIGURATION_Handle *def;
272 
274  if (GNUNET_OK !=
276  NULL))
277  {
278  fprintf (stderr,
279  _ ("failed to load configuration defaults"));
280  global_ret = 1;
283  GNUNET_free (cfg_fn);
284  return;
285  }
286  if (GNUNET_OK !=
288  out,
289  cfgfile))
290  global_ret = 2;
292  }
293  else
294  {
295  if (GNUNET_OK !=
297  cfgfile))
298  global_ret = 2;
299  }
300  GNUNET_free (cfg_fn);
301  }
302  if (NULL != out)
304 }
305 
306 
314 int
315 main (int argc, char *const *argv)
316 {
317  struct GNUNET_GETOPT_CommandLineOption options[] = {
320  'b',
321  "supported-backend",
322  "BACKEND",
323  gettext_noop (
324  "test if the current installation supports the specified BACKEND"),
325  &backend_check)),
327  'F',
328  "full",
329  gettext_noop (
330  "write the full configuration file, including default values"),
331  &full),
333  'f',
334  "filename",
335  gettext_noop ("interpret option value as a filename (with $-expansion)"),
336  &is_filename),
338  "option",
339  "OPTION",
340  gettext_noop ("name of the option to access"),
341  &option),
343  'r',
344  "rewrite",
345  gettext_noop (
346  "rewrite the configuration file, even if nothing changed"),
347  &rewrite),
349  "list-sections",
350  gettext_noop (
351  "print available configuration sections"),
352  &list_sections),
354  "section",
355  "SECTION",
356  gettext_noop (
357  "name of the section to access"),
358  &section),
360  "value",
361  "VALUE",
362  gettext_noop ("value to set"),
363  &value),
365  };
366  int ret;
367 
368  if (GNUNET_OK !=
369  GNUNET_STRINGS_get_utf8_args (argc, argv,
370  &argc, &argv))
371  return 2;
372 
373  ret =
374  GNUNET_PROGRAM_run (argc,
375  argv,
376  "gnunet-config [OPTIONS]",
377  gettext_noop ("Manipulate GNUnet configuration files"),
378  options,
379  &run,
380  NULL);
381  GNUNET_free_nz ((void *) argv);
382  if (GNUNET_OK == ret)
383  return global_ret;
384  return ret;
385 }
386 
387 
388 /* end of gnunet-config.c */
enum GNUNET_GenericReturnValue 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:363
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
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_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1459
#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:84
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.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Definition of a command line option.
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.
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_write(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Write configuration file.
#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.
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
static char * section
Name of the section.
Definition: gnunet-config.c:33
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
static char * fn
Filename of the unique file.
#define DIR_SEPARATOR_STR
Definition: platform.h:165
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
static char * option
Name of the option.
Definition: gnunet-config.c:38
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 int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_free_nz(ptr)
Wrapper around free.
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 int full
Should the generated configuration file contain the whole configuration?
Definition: gnunet-config.c:75
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition: strings.c:618
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:84
int main(int argc, char *const *argv)
Program to manipulate configuration files.
const char * name
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)
static int rewrite
Should we write out the configuration file, even if no value was changed?
Definition: gnunet-config.c:70
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:87
int GNUNET_PLUGIN_test(const char *library_name)
Test if a plugin exists.
Definition: plugin.c:175
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69