- fixed string to address parsing
[oweals/gnunet.git] / src / include / gnunet_configuration_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_configuration_lib.h
23  * @brief configuration API
24  *
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_CONFIGURATION_LIB_H
29 #define GNUNET_CONFIGURATION_LIB_H
30
31
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #if 0                           /* keep Emacsens' auto-indent happy */
36 }
37 #endif
38 #endif
39
40 #include "gnunet_common.h"
41 #include "gnunet_time_lib.h"
42
43 /**
44  * A configuration object.
45  */
46 struct GNUNET_CONFIGURATION_Handle;
47
48 /**
49  * Create a new configuration object.
50  * @return fresh configuration object
51  */
52 struct GNUNET_CONFIGURATION_Handle *
53 GNUNET_CONFIGURATION_create (void);
54
55
56 /**
57  * Duplicate an existing configuration object.
58  *
59  * @param cfg configuration to duplicate
60  * @return duplicate configuration
61  */
62 struct GNUNET_CONFIGURATION_Handle *
63 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg);
64
65
66 /**
67  * Destroy configuration object.
68  *
69  * @param cfg configuration to destroy
70  */
71 void
72 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg);
73
74
75 /**
76  * Load configuration.  This function will first parse the
77  * defaults and then parse the specific configuration file
78  * to overwrite the defaults.
79  *
80  * @param cfg configuration to update
81  * @param filename name of the configuration file, NULL to load defaults
82  * @return GNUNET_OK on success, GNUNET_SYSERR on error
83  */
84 int
85 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
86                            const char *filename);
87
88
89 /**
90  * Load default configuration.  This function will parse the
91  * defaults from the given defaults_d directory.
92  *
93  * @param cfg configuration to update
94  * @param defaults_d directory with the defaults
95  * @return GNUNET_OK on success, GNUNET_SYSERR on error
96  */
97 int
98 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
99                                 const char *defaults_d);
100
101
102 /**
103  * Parse a configuration file, add all of the options in the
104  * file to the configuration environment.
105  *
106  * @param cfg configuration to update
107  * @param filename name of the configuration file
108  * @return GNUNET_OK on success, GNUNET_SYSERR on error
109  */
110 int
111 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
112                             const char *filename);
113
114
115 /**
116  * Write configuration file.
117  *
118  * @param cfg configuration to write
119  * @param filename where to write the configuration
120  * @return GNUNET_OK on success, GNUNET_SYSERR on error
121  */
122 int
123 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
124                             const char *filename);
125
126 /**
127  * Write only configuration entries that have been changed to configuration file
128  * @param cfgDefault default configuration
129  * @param cfgNew new configuration
130  * @param filename where to write the configuration diff between default and new
131  * @return GNUNET_OK on success, GNUNET_SYSERR on error
132  */
133 int
134 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
135                                   *cfgDefault,
136                                   const struct GNUNET_CONFIGURATION_Handle
137                                   *cfgNew, const char *filename);
138
139 /**
140  * Test if there are configuration options that were
141  * changed since the last save.
142  *
143  * @param cfg configuration to inspect
144  * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
145  */
146 int
147 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg);
148
149
150 /**
151  * Function to iterate over options.
152  *
153  * @param cls closure
154  * @param section name of the section
155  * @param option name of the option
156  * @param value value of the option
157  */
158 typedef void (*GNUNET_CONFIGURATION_Iterator) (void *cls, const char *section,
159                                                const char *option,
160                                                const char *value);
161
162
163 /**
164  * Function to iterate over section.
165  *
166  * @param cls closure
167  * @param section name of the section
168  */
169 typedef void (*GNUNET_CONFIGURATION_Section_Iterator) (void *cls,
170                                                        const char *section);
171
172
173 /**
174  * Iterate over all options in the configuration.
175  *
176  * @param cfg configuration to inspect
177  * @param iter function to call on each option
178  * @param iter_cls closure for iter
179  */
180 void
181 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
182                               GNUNET_CONFIGURATION_Iterator iter,
183                               void *iter_cls);
184
185
186 /**
187  * Iterate over all sections in the configuration.
188  *
189  * @param cfg configuration to inspect
190  * @param iter function to call on each section
191  * @param iter_cls closure for iter
192  */
193 void
194 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
195                                        *cfg,
196                                        GNUNET_CONFIGURATION_Section_Iterator
197                                        iter, void *iter_cls);
198
199
200 /**
201  * Remove the given section and all options in it.
202  *
203  * @param cfg configuration to inspect
204  * @param section name of the section to remove
205  */
206 void
207 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
208                                      const char *section);
209
210 /**
211  * Get a configuration value that should be a number.
212  *
213  * @param cfg configuration to inspect
214  * @param section section of interest
215  * @param option option of interest
216  * @param number where to store the numeric value of the option
217  * @return GNUNET_OK on success, GNUNET_SYSERR on error
218  */
219 int
220 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
221                                        *cfg, const char *section,
222                                        const char *option,
223                                        unsigned long long *number);
224
225
226 /**
227  * Get a configuration value that should be a relative time.
228  *
229  * @param cfg configuration to inspect
230  * @param section section of interest
231  * @param option option of interest
232  * @param time set to the time value stored in the configuration
233  * @return GNUNET_OK on success, GNUNET_SYSERR on error
234  */
235 int
236 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
237                                      *cfg, const char *section,
238                                      const char *option,
239                                      struct GNUNET_TIME_Relative *time);
240
241
242
243 /**
244  * Get a configuration value that should be a size in bytes.
245  *
246  * @param cfg configuration to inspect
247  * @param section section of interest
248  * @param option option of interest
249  * @param size set to the size in bytes as stored in the configuration
250  * @return GNUNET_OK on success, GNUNET_SYSERR on error
251  */
252 int
253 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
254                                      *cfg, const char *section,
255                                      const char *option,
256                                      unsigned long long *size);
257
258
259 /**
260  * Test if we have a value for a particular option
261  *
262  * @param cfg configuration to inspect
263  * @param section section of interest
264  * @param option option of interest
265  * @return GNUNET_YES if so, GNUNET_NO if not.
266  */
267 int
268 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
269                                  const char *section, const char *option);
270
271
272 /**
273  * Get a configuration value that should be a string.
274  *
275  * @param cfg configuration to inspect
276  * @param section section of interest
277  * @param option option of interest
278  * @param value will be set to a freshly allocated configuration
279  *        value, or NULL if option is not specified
280  * @return GNUNET_OK on success, GNUNET_SYSERR on error
281  */
282 int
283 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
284                                        *cfg, const char *section,
285                                        const char *option, char **value);
286
287
288 /**
289  * Get a configuration value that should be the name of a file
290  * or directory.
291  *
292  * @param cfg configuration to inspect
293  * @param section section of interest
294  * @param option option of interest
295  * @param value will be set to a freshly allocated configuration
296  *        value, or NULL if option is not specified
297  * @return GNUNET_OK on success, GNUNET_SYSERR on error
298  */
299 int
300 GNUNET_CONFIGURATION_get_value_filename (const struct
301                                          GNUNET_CONFIGURATION_Handle *cfg,
302                                          const char *section,
303                                          const char *option, char **value);
304
305 /**
306  * Iterate over the set of filenames stored in a configuration value.
307  *
308  * @param cfg configuration to inspect
309  * @param section section of interest
310  * @param option option of interest
311  * @param cb function to call on each filename
312  * @param cb_cls closure for cb
313  * @return number of filenames iterated over, -1 on error
314  */
315 int
316 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
317                                               GNUNET_CONFIGURATION_Handle *cfg,
318                                               const char *section,
319                                               const char *option,
320                                               GNUNET_FileNameCallback cb,
321                                               void *cb_cls);
322
323 /**
324  * Iterate over values of a section in the configuration.
325  *
326  * @param cfg configuration to inspect
327  * @param section the section
328  * @param iter function to call on each option
329  * @param iter_cls closure for iter
330  */
331 void
332 GNUNET_CONFIGURATION_iterate_section_values (const struct
333                                              GNUNET_CONFIGURATION_Handle *cfg,
334                                              const char *section,
335                                              GNUNET_CONFIGURATION_Iterator iter,
336                                              void *iter_cls);
337
338 /**
339  * Get a configuration value that should be in a set of
340  * predefined strings
341  *
342  * @param cfg configuration to inspect
343  * @param section section of interest
344  * @param option option of interest
345  * @param choices NULL-terminated list of legal values
346  * @param value will be set to an entry in the legal list,
347  *        or NULL if option is not specified and no default given
348  * @return GNUNET_OK on success, GNUNET_SYSERR on error
349  */
350 int
351 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
352                                        *cfg, const char *section,
353                                        const char *option, const char **choices,
354                                        const char **value);
355
356 /**
357  * Get a configuration value that should be in a set of
358  * "YES" or "NO".
359  *
360  * @param cfg configuration to inspect
361  * @param section section of interest
362  * @param option option of interest
363  * @return GNUNET_YES, GNUNET_NO or if option has no valid value, GNUNET_SYSERR
364  */
365 int
366 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
367                                       *cfg, const char *section,
368                                       const char *option);
369
370
371 /**
372  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
373  * where either in the "PATHS" section or the environtment
374  * "FOO" is set to "DIRECTORY".
375  *
376  * @param cfg configuration to use for path expansion
377  * @param orig string to $-expand (will be freed!)
378  * @return $-expanded string
379  */
380 char *
381 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
382                                     *cfg, char *orig);
383
384
385 /**
386  * Set a configuration value that should be a number.
387  *
388  * @param cfg configuration to update
389  * @param section section of interest
390  * @param option option of interest
391  * @param number value to set
392  */
393 void
394 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
395                                        const char *section, const char *option,
396                                        unsigned long long number);
397
398
399 /**
400  * Set a configuration value that should be a string.
401  *
402  * @param cfg configuration to update
403  * @param section section of interest
404  * @param option option of interest
405  * @param value value to set
406  */
407 void
408 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
409                                        const char *section, const char *option,
410                                        const char *value);
411
412
413 /**
414  * Remove a filename from a configuration value that
415  * represents a list of filenames
416  *
417  * @param cfg configuration to update
418  * @param section section of interest
419  * @param option option of interest
420  * @param value filename to remove
421  * @return GNUNET_OK on success,
422  *         GNUNET_SYSERR if the filename is not in the list
423  */
424 int
425 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
426                                             *cfg, const char *section,
427                                             const char *option,
428                                             const char *value);
429
430 /**
431  * Append a filename to a configuration value that
432  * represents a list of filenames
433  *
434  * @param cfg configuration to update
435  * @param section section of interest
436  * @param option option of interest
437  * @param value filename to append
438  * @return GNUNET_OK on success,
439  *         GNUNET_SYSERR if the filename already in the list
440  */
441 int
442 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
443                                             *cfg, const char *section,
444                                             const char *option,
445                                             const char *value);
446
447
448 #if 0                           /* keep Emacsens' auto-indent happy */
449 {
450 #endif
451 #ifdef __cplusplus
452 }
453 #endif
454
455 #endif