diff options
Diffstat (limited to 'src/cli/util/gnunet-config.c')
-rw-r--r-- | src/cli/util/gnunet-config.c | 248 |
1 files changed, 162 insertions, 86 deletions
diff --git a/src/cli/util/gnunet-config.c b/src/cli/util/gnunet-config.c index 714c683dd..979f0b3f4 100644 --- a/src/cli/util/gnunet-config.c +++ b/src/cli/util/gnunet-config.c | |||
@@ -35,7 +35,6 @@ | |||
35 | */ | 35 | */ |
36 | static char *backend_check; | 36 | static char *backend_check; |
37 | 37 | ||
38 | |||
39 | /** | 38 | /** |
40 | * If printing the value of CFLAGS has been requested. | 39 | * If printing the value of CFLAGS has been requested. |
41 | */ | 40 | */ |
@@ -46,13 +45,21 @@ static int cflags; | |||
46 | */ | 45 | */ |
47 | static int is_experimental; | 46 | static int is_experimental; |
48 | 47 | ||
48 | /** | ||
49 | * Do not load default configuration | ||
50 | */ | ||
51 | static int no_defaults; | ||
52 | |||
53 | /** | ||
54 | * Parse configuration from this memory. | ||
55 | */ | ||
56 | static char *ram_config; | ||
49 | 57 | ||
50 | /** | 58 | /** |
51 | * If printing the value of LIBS has been requested. | 59 | * If printing the value of LIBS has been requested. |
52 | */ | 60 | */ |
53 | static int libs; | 61 | static int libs; |
54 | 62 | ||
55 | |||
56 | /** | 63 | /** |
57 | * If printing the value of PREFIX has been requested. | 64 | * If printing the value of PREFIX has been requested. |
58 | */ | 65 | */ |
@@ -60,76 +67,6 @@ static int prefix; | |||
60 | 67 | ||
61 | 68 | ||
62 | /** | 69 | /** |
63 | * Print each option in a given section. | ||
64 | * Main task to run to perform operations typical for | ||
65 | * gnunet-config as per the configuration settings | ||
66 | * given in @a cls. | ||
67 | * | ||
68 | * @param cls closure with the `struct GNUNET_CONFIGURATION_ConfigSettings` | ||
69 | * @param args remaining command-line arguments | ||
70 | * @param cfgfile name of the configuration file used (for saving, | ||
71 | * can be NULL!) | ||
72 | * @param cfg configuration | ||
73 | */ | ||
74 | static void | ||
75 | run (void *cls, | ||
76 | char *const *args, | ||
77 | const char *cfgfile, | ||
78 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
79 | { | ||
80 | struct GNUNET_CONFIGURATION_ConfigSettings *cs = cls; | ||
81 | |||
82 | if (1 == is_experimental) | ||
83 | { | ||
84 | #ifdef GNUNET_EXPERIMENTAL | ||
85 | cs->global_ret = 0; | ||
86 | #else | ||
87 | cs->global_ret = 1; | ||
88 | #endif | ||
89 | return; | ||
90 | } | ||
91 | if (1 == cflags || 1 == libs || 1 == prefix) | ||
92 | { | ||
93 | char *prefixdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_PREFIX); | ||
94 | char *libdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR); | ||
95 | |||
96 | if (1 == cflags) | ||
97 | { | ||
98 | fprintf (stdout, "-I%sinclude\n", prefixdir); | ||
99 | } | ||
100 | if (1 == libs) | ||
101 | { | ||
102 | fprintf (stdout, "-L%s -lgnunetutil\n", libdir); | ||
103 | } | ||
104 | if (1 == prefix) | ||
105 | { | ||
106 | fprintf (stdout, "%s\n", prefixdir); | ||
107 | } | ||
108 | cs->global_ret = 0; | ||
109 | GNUNET_free (prefixdir); | ||
110 | GNUNET_free (libdir); | ||
111 | return; | ||
112 | } | ||
113 | if (NULL != backend_check) | ||
114 | { | ||
115 | char *name; | ||
116 | |||
117 | GNUNET_asprintf (&name, | ||
118 | "libgnunet_plugin_%s", | ||
119 | backend_check); | ||
120 | cs->global_ret = (GNUNET_OK == | ||
121 | GNUNET_PLUGIN_test (name)) ? 0 : 77; | ||
122 | GNUNET_free (name); | ||
123 | return; | ||
124 | } | ||
125 | GNUNET_CONFIGURATION_config_tool_run (cs, | ||
126 | args, | ||
127 | cfgfile, | ||
128 | cfg); | ||
129 | } | ||
130 | |||
131 | |||
132 | /** | ||
133 | * Program to manipulate configuration files. | 70 | * Program to manipulate configuration files. |
134 | * | 71 | * |
135 | * @param argc number of arguments from the command line | 72 | * @param argc number of arguments from the command line |
@@ -144,7 +81,17 @@ main (int argc, | |||
144 | .api_version = GNUNET_UTIL_VERSION, | 81 | .api_version = GNUNET_UTIL_VERSION, |
145 | .global_ret = EXIT_SUCCESS | 82 | .global_ret = EXIT_SUCCESS |
146 | }; | 83 | }; |
84 | const struct GNUNET_OS_ProjectData *pd | ||
85 | = GNUNET_OS_project_data_get (); | ||
86 | char *cfgfile = NULL; | ||
87 | char *loglev = NULL; | ||
88 | char *logfile = NULL; | ||
147 | struct GNUNET_GETOPT_CommandLineOption options[] = { | 89 | struct GNUNET_GETOPT_CommandLineOption options[] = { |
90 | GNUNET_GETOPT_option_cfgfile (&cfgfile), | ||
91 | GNUNET_GETOPT_option_help ("gnunet-config [OPTIONS]"), | ||
92 | GNUNET_GETOPT_option_loglevel (&loglev), | ||
93 | GNUNET_GETOPT_option_logfile (&logfile), | ||
94 | GNUNET_GETOPT_option_version (pd->version), | ||
148 | GNUNET_GETOPT_option_exclusive ( | 95 | GNUNET_GETOPT_option_exclusive ( |
149 | GNUNET_GETOPT_option_string ( | 96 | GNUNET_GETOPT_option_string ( |
150 | 'b', | 97 | 'b', |
@@ -171,34 +118,163 @@ main (int argc, | |||
171 | "Provide an appropriate value for LIBS to applications building on top of GNUnet"), | 118 | "Provide an appropriate value for LIBS to applications building on top of GNUnet"), |
172 | &libs), | 119 | &libs), |
173 | GNUNET_GETOPT_option_flag ( | 120 | GNUNET_GETOPT_option_flag ( |
121 | 'n', | ||
122 | "no-defaults", | ||
123 | gettext_noop ("Do not parse default configuration files"), | ||
124 | &no_defaults), | ||
125 | GNUNET_GETOPT_option_flag ( | ||
174 | 'p', | 126 | 'p', |
175 | "prefix", | 127 | "prefix", |
176 | gettext_noop ( | 128 | gettext_noop ( |
177 | "Provide the path under which GNUnet was installed"), | 129 | "Provide the path under which GNUnet was installed"), |
178 | &prefix), | 130 | &prefix), |
131 | GNUNET_GETOPT_option_string ( | ||
132 | 'R', | ||
133 | "ram-config", | ||
134 | "CONFIG_DATA", | ||
135 | gettext_noop ( | ||
136 | "Parse main configuration from this command-line argument and not from disk"), | ||
137 | &ram_config), | ||
179 | GNUNET_CONFIGURATION_CONFIG_OPTIONS (&cs), | 138 | GNUNET_CONFIGURATION_CONFIG_OPTIONS (&cs), |
180 | GNUNET_GETOPT_OPTION_END | 139 | GNUNET_GETOPT_OPTION_END |
181 | }; | 140 | }; |
182 | enum GNUNET_GenericReturnValue ret; | 141 | int iret; |
183 | 142 | ||
184 | if (GNUNET_OK != | 143 | if (GNUNET_OK != |
185 | GNUNET_STRINGS_get_utf8_args (argc, argv, | 144 | GNUNET_STRINGS_get_utf8_args (argc, argv, |
186 | &argc, &argv)) | 145 | &argc, &argv)) |
187 | return EXIT_FAILURE; | 146 | return EXIT_FAILURE; |
188 | ret = | 147 | if ( (NULL != pd->config_file) && |
189 | GNUNET_PROGRAM_run (argc, | 148 | (NULL != pd->user_config_file) ) |
190 | argv, | 149 | cfgfile = GNUNET_CONFIGURATION_default_filename (); |
191 | "gnunet-config [OPTIONS]", | 150 | iret = GNUNET_GETOPT_run ("gnunet-config", |
192 | gettext_noop ("Manipulate GNUnet configuration files"), | 151 | options, |
193 | options, | 152 | argc, |
194 | &run, | 153 | argv); |
195 | &cs); | 154 | if (GNUNET_SYSERR == iret) |
196 | GNUNET_free_nz ((void *) argv); | 155 | { |
197 | GNUNET_CONFIGURATION_config_settings_free (&cs); | 156 | GNUNET_free_nz ((void *) argv); |
198 | if (GNUNET_NO == ret) | ||
199 | return 0; | ||
200 | if (GNUNET_SYSERR == ret) | ||
201 | return EXIT_INVALIDARGUMENT; | 157 | return EXIT_INVALIDARGUMENT; |
158 | } | ||
159 | if (GNUNET_OK != | ||
160 | GNUNET_log_setup ("gnunet-config", | ||
161 | loglev, | ||
162 | logfile)) | ||
163 | { | ||
164 | GNUNET_free_nz ((void *) argv); | ||
165 | return EXIT_FAILURE; | ||
166 | } | ||
167 | if (1 == is_experimental) | ||
168 | { | ||
169 | GNUNET_free_nz ((void *) argv); | ||
170 | #ifdef GNUNET_EXPERIMENTAL | ||
171 | return 0; | ||
172 | #else | ||
173 | return 1; | ||
174 | #endif | ||
175 | } | ||
176 | if (1 == cflags || 1 == libs || 1 == prefix) | ||
177 | { | ||
178 | char *prefixdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_PREFIX); | ||
179 | char *libdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR); | ||
180 | |||
181 | if (1 == cflags) | ||
182 | { | ||
183 | fprintf (stdout, "-I%sinclude\n", prefixdir); | ||
184 | } | ||
185 | if (1 == libs) | ||
186 | { | ||
187 | fprintf (stdout, "-L%s -lgnunetutil\n", libdir); | ||
188 | } | ||
189 | if (1 == prefix) | ||
190 | { | ||
191 | fprintf (stdout, "%s\n", prefixdir); | ||
192 | } | ||
193 | GNUNET_free (prefixdir); | ||
194 | GNUNET_free (libdir); | ||
195 | GNUNET_free_nz ((void *) argv); | ||
196 | return 0; | ||
197 | } | ||
198 | if (NULL != backend_check) | ||
199 | { | ||
200 | char *name; | ||
201 | |||
202 | GNUNET_asprintf (&name, | ||
203 | "libgnunet_plugin_%s", | ||
204 | backend_check); | ||
205 | iret = (GNUNET_OK == | ||
206 | GNUNET_PLUGIN_test (name)) ? 0 : 77; | ||
207 | GNUNET_free (name); | ||
208 | GNUNET_free_nz ((void *) argv); | ||
209 | return iret; | ||
210 | } | ||
211 | |||
212 | { | ||
213 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
214 | |||
215 | cfg = GNUNET_CONFIGURATION_create (); | ||
216 | |||
217 | if (NULL != ram_config) | ||
218 | { | ||
219 | if ( (! no_defaults) && | ||
220 | (GNUNET_SYSERR == | ||
221 | GNUNET_CONFIGURATION_load (cfg, | ||
222 | NULL)) ) | ||
223 | { | ||
224 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
225 | _ ("Failed to load default configuration, exiting ...\n")); | ||
226 | GNUNET_free_nz ((void *) argv); | ||
227 | GNUNET_CONFIGURATION_destroy (cfg); | ||
228 | return EXIT_FAILURE; | ||
229 | } | ||
230 | if (GNUNET_OK != | ||
231 | GNUNET_CONFIGURATION_deserialize (cfg, | ||
232 | ram_config, | ||
233 | strlen (ram_config), | ||
234 | NULL)) | ||
235 | { | ||
236 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
237 | _ ("Failed to parse configuration, exiting ...\n")); | ||
238 | GNUNET_free_nz ((void *) argv); | ||
239 | GNUNET_CONFIGURATION_destroy (cfg); | ||
240 | return EXIT_FAILURE; | ||
241 | } | ||
242 | } | ||
243 | else | ||
244 | { | ||
245 | if (GNUNET_YES != | ||
246 | GNUNET_DISK_file_test (cfgfile)) | ||
247 | { | ||
248 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
249 | _ ("Unreadable configuration file `%s', exiting ...\n"), | ||
250 | cfgfile); | ||
251 | GNUNET_free_nz ((void *) argv); | ||
252 | GNUNET_CONFIGURATION_destroy (cfg); | ||
253 | return EXIT_FAILURE; | ||
254 | } | ||
255 | if (GNUNET_SYSERR == | ||
256 | (no_defaults | ||
257 | ? GNUNET_CONFIGURATION_parse (cfg, | ||
258 | cfgfile) | ||
259 | : GNUNET_CONFIGURATION_load (cfg, | ||
260 | cfgfile)) ) | ||
261 | { | ||
262 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
263 | _ ("Malformed configuration file `%s', exiting ...\n"), | ||
264 | cfgfile); | ||
265 | GNUNET_free_nz ((void *) argv); | ||
266 | GNUNET_CONFIGURATION_destroy (cfg); | ||
267 | return EXIT_FAILURE; | ||
268 | } | ||
269 | } | ||
270 | GNUNET_CONFIGURATION_config_tool_run (&cs, | ||
271 | &argv[iret], | ||
272 | cfgfile, | ||
273 | cfg); | ||
274 | GNUNET_free_nz ((void *) argv); | ||
275 | GNUNET_CONFIGURATION_config_settings_free (&cs); | ||
276 | GNUNET_CONFIGURATION_destroy (cfg); | ||
277 | } | ||
202 | return cs.global_ret; | 278 | return cs.global_ret; |
203 | } | 279 | } |
204 | 280 | ||