2 This file is part of GNUnet.
3 Copyright (C) 2006, 2007, 2008, 2009, 2013 GNUnet e.V.
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 3, or (at your
8 option) any later version.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file src/util/configuration.c
23 * @brief configuration management
24 * @author Christian Grothoff
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_strings_lib.h"
30 #include "gnunet_configuration_lib.h"
31 #include "gnunet_disk_lib.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
35 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
38 * @brief configuration entry
44 * This is a linked list.
46 struct ConfigEntry *next;
54 * current, commited value
61 * @brief configuration section
66 * This is a linked list.
68 struct ConfigSection *next;
71 * entries in the section
73 struct ConfigEntry *entries;
83 * @brief configuration data
85 struct GNUNET_CONFIGURATION_Handle
88 * Configuration sections.
90 struct ConfigSection *sections;
93 * Modification indication since last save
94 * #GNUNET_NO if clean, #GNUNET_YES if dirty,
95 * #GNUNET_SYSERR on error (i.e. last save failed)
103 * Used for diffing a configuration object against
108 const struct GNUNET_CONFIGURATION_Handle *cfg_default;
110 struct GNUNET_CONFIGURATION_Handle *cfgDiff;
115 * Create a GNUNET_CONFIGURATION_Handle.
117 * @return fresh configuration object
119 struct GNUNET_CONFIGURATION_Handle *
120 GNUNET_CONFIGURATION_create ()
122 return GNUNET_new (struct GNUNET_CONFIGURATION_Handle);
127 * Destroy configuration object.
129 * @param cfg configuration to destroy
132 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
134 struct ConfigSection *sec;
136 while (NULL != (sec = cfg->sections))
137 GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
143 * De-serializes configuration
145 * @param cfg configuration to update
146 * @param mem the memory block of serialized configuration
147 * @param size the size of the memory block
148 * @param allow_inline set to #GNUNET_YES if we recursively load configuration
149 * from inlined configurations; #GNUNET_NO if not and raise warnings
150 * when we come across them
151 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
154 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
175 section = GNUNET_strdup ("");
179 while (r_bytes < size)
181 GNUNET_free_non_null (line_orig);
182 /* fgets-like behaviour on buffer */
183 to_read = size - r_bytes;
184 pos = memchr (&mem[r_bytes], '\n', to_read);
187 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
188 r_bytes += line_size;
192 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
193 r_bytes += line_size + 1;
196 /* increment line number */
198 /* tabs and '\r' are whitespace */
199 emptyline = GNUNET_YES;
200 for (i = 0; i < line_size; i++)
207 emptyline = GNUNET_NO;
209 /* ignore empty lines */
210 if (GNUNET_YES == emptyline)
213 /* remove tailing whitespace */
214 for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));i--)
217 /* remove leading whitespace */
218 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++);
220 /* ignore comments */
221 if ( ('#' == line[0]) || ('%' == line[0]) )
224 /* handle special "@INLINE@" directive */
225 if (0 == strncasecmp (line,
227 strlen ("@INLINE@ ")))
230 value = &line[strlen ("@INLINE@ ")];
231 if (GNUNET_YES == allow_inline)
233 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
235 ret = GNUNET_SYSERR; /* failed to parse included config */
241 LOG (GNUNET_ERROR_TYPE_DEBUG,
242 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
248 if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
251 line[line_size - 1] = '\0';
253 GNUNET_free (section);
254 section = GNUNET_strdup (value);
257 if (NULL != (eq = strchr (line, '=')))
260 tag = GNUNET_strndup (line, eq - line);
261 /* remove tailing whitespace */
262 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--)
265 /* Strip whitespace */
267 while (isspace ((unsigned char) value[0]))
269 for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
274 if ( ('"' == value[0]) &&
275 ('"' == value[strlen (value) - 1]) )
277 value[strlen (value) - 1] = '\0';
280 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
285 LOG (GNUNET_ERROR_TYPE_WARNING,
286 _("Syntax error while deserializing in line %u\n"),
291 GNUNET_free_non_null (line_orig);
292 GNUNET_free (section);
293 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
299 * Parse a configuration file, add all of the options in the
300 * file to the configuration environment.
302 * @param cfg configuration to update
303 * @param filename name of the configuration file
304 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
307 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
308 const char *filename)
317 fn = GNUNET_STRINGS_filename_expand (filename);
318 LOG (GNUNET_ERROR_TYPE_DEBUG,
319 "Asked to parse config file `%s'\n",
322 return GNUNET_SYSERR;
323 dirty = cfg->dirty; /* back up value! */
325 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
327 LOG (GNUNET_ERROR_TYPE_WARNING,
328 "Error while determining the file size of `%s'\n",
331 return GNUNET_SYSERR;
335 GNUNET_break (0); /* File size is more than the heap size */
337 return GNUNET_SYSERR;
340 mem = GNUNET_malloc (fs);
341 if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
343 LOG (GNUNET_ERROR_TYPE_WARNING,
344 _("Error while reading file `%s'\n"),
348 return GNUNET_SYSERR;
350 LOG (GNUNET_ERROR_TYPE_DEBUG,
351 "Deserializing contents of file `%s'\n",
354 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
356 /* restore dirty flag - anything we set in the meantime
364 * Test if there are configuration options that were
365 * changed since the last save.
367 * @param cfg configuration to inspect
368 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
371 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
378 * Serializes the given configuration.
380 * @param cfg configuration to serialize
381 * @param size will be set to the size of the serialized memory block
382 * @return the memory block where the serialized configuration is
383 * present. This memory should be freed by the caller
386 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
389 struct ConfigSection *sec;
390 struct ConfigEntry *ent;
400 /* Pass1 : calculate the buffer size required */
402 for (sec = cfg->sections; NULL != sec; sec = sec->next)
404 /* For each section we need to add 3 charaters: {'[',']','\n'} */
405 m_size += strlen (sec->name) + 3;
406 for (ent = sec->entries; NULL != ent; ent = ent->next)
408 if (NULL != ent->val)
410 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
412 while (NULL != (pos = strstr (pos, "\n")))
417 /* For each key = value pair we need to add 4 characters (2
418 spaces and 1 equal-to character and 1 new line) */
419 m_size += strlen (ent->key) + strlen (ent->val) + 4;
422 /* A new line after section end */
426 /* Pass2: Allocate memory and write the configuration to it */
427 mem = GNUNET_malloc (m_size);
433 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
434 GNUNET_assert (0 < len);
435 GNUNET_memcpy (mem + c_size, cbuf, len);
438 for (ent = sec->entries; NULL != ent; ent = ent->next)
440 if (NULL != ent->val)
442 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
443 strcpy (val, ent->val);
444 while (NULL != (pos = strstr (val, "\n")))
446 memmove (&pos[2], &pos[1], strlen (&pos[1]));
450 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
452 GNUNET_memcpy (mem + c_size, cbuf, len);
457 GNUNET_memcpy (mem + c_size, "\n", 1);
461 GNUNET_assert (c_size == m_size);
468 * Write configuration file.
470 * @param cfg configuration to write
471 * @param filename where to write the configuration
472 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
475 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
476 const char *filename)
482 fn = GNUNET_STRINGS_filename_expand (filename);
484 return GNUNET_SYSERR;
485 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
488 return GNUNET_SYSERR;
490 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
491 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
492 GNUNET_DISK_PERM_USER_READ
493 | GNUNET_DISK_PERM_USER_WRITE
494 | GNUNET_DISK_PERM_GROUP_READ
495 | GNUNET_DISK_PERM_GROUP_WRITE))
498 GNUNET_free (cfg_buf);
499 LOG (GNUNET_ERROR_TYPE_WARNING,
500 "Writing configration to file `%s' failed\n",
502 cfg->dirty = GNUNET_SYSERR; /* last write failed */
503 return GNUNET_SYSERR;
506 GNUNET_free (cfg_buf);
507 cfg->dirty = GNUNET_NO; /* last write succeeded */
513 * Iterate over all options in the configuration.
515 * @param cfg configuration to inspect
516 * @param iter function to call on each option
517 * @param iter_cls closure for @a iter
520 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
521 GNUNET_CONFIGURATION_Iterator iter,
524 struct ConfigSection *spos;
525 struct ConfigEntry *epos;
527 for (spos = cfg->sections; NULL != spos; spos = spos->next)
528 for (epos = spos->entries; NULL != epos; epos = epos->next)
529 if (NULL != epos->val)
530 iter (iter_cls, spos->name, epos->key, epos->val);
535 * Iterate over values of a section in the configuration.
537 * @param cfg configuration to inspect
538 * @param section the section
539 * @param iter function to call on each option
540 * @param iter_cls closure for @a iter
543 GNUNET_CONFIGURATION_iterate_section_values (const struct
544 GNUNET_CONFIGURATION_Handle *cfg,
546 GNUNET_CONFIGURATION_Iterator iter,
549 struct ConfigSection *spos;
550 struct ConfigEntry *epos;
552 spos = cfg->sections;
553 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
557 for (epos = spos->entries; NULL != epos; epos = epos->next)
558 if (NULL != epos->val)
559 iter (iter_cls, spos->name, epos->key, epos->val);
564 * Iterate over all sections in the configuration.
566 * @param cfg configuration to inspect
567 * @param iter function to call on each section
568 * @param iter_cls closure for @a iter
571 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg,
572 GNUNET_CONFIGURATION_Section_Iterator iter,
575 struct ConfigSection *spos;
576 struct ConfigSection *next;
578 next = cfg->sections;
583 iter (iter_cls, spos->name);
589 * Remove the given section and all options in it.
591 * @param cfg configuration to inspect
592 * @param section name of the section to remove
595 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
598 struct ConfigSection *spos;
599 struct ConfigSection *prev;
600 struct ConfigEntry *ent;
603 spos = cfg->sections;
606 if (0 == strcasecmp (section, spos->name))
609 cfg->sections = spos->next;
611 prev->next = spos->next;
612 while (NULL != (ent = spos->entries))
614 spos->entries = ent->next;
615 GNUNET_free (ent->key);
616 GNUNET_free_non_null (ent->val);
618 cfg->dirty = GNUNET_YES;
620 GNUNET_free (spos->name);
631 * Copy a configuration value to the given target configuration.
632 * Overwrites existing entries.
634 * @param cls the destination configuration (`struct GNUNET_CONFIGURATION_Handle *`)
635 * @param section section for the value
636 * @param option option name of the value
637 * @param value value to copy
640 copy_entry (void *cls,
645 struct GNUNET_CONFIGURATION_Handle *dst = cls;
647 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
652 * Duplicate an existing configuration object.
654 * @param cfg configuration to duplicate
655 * @return duplicate configuration
657 struct GNUNET_CONFIGURATION_Handle *
658 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
660 struct GNUNET_CONFIGURATION_Handle *ret;
662 ret = GNUNET_CONFIGURATION_create ();
663 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
669 * Find a section entry from a configuration.
671 * @param cfg configuration to search in
672 * @param section name of the section to look for
673 * @return matching entry, NULL if not found
675 static struct ConfigSection *
676 find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
679 struct ConfigSection *pos;
682 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
689 * Find an entry from a configuration.
691 * @param cfg handle to the configuration
692 * @param section section the option is in
693 * @param key the option
694 * @return matching entry, NULL if not found
696 static struct ConfigEntry *
697 find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
701 struct ConfigSection *sec;
702 struct ConfigEntry *pos;
704 if (NULL == (sec = find_section (cfg, section)))
707 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
714 * A callback function, compares entries from two configurations
715 * (default against a new configuration) and write the diffs in a
716 * diff-configuration object (the callback object).
718 * @param cls the diff configuration (`struct DiffHandle *`)
719 * @param section section for the value (of the default conf.)
720 * @param option option name of the value (of the default conf.)
721 * @param value value to copy (of the default conf.)
724 compare_entries (void *cls,
729 struct DiffHandle *dh = cls;
730 struct ConfigEntry *entNew;
732 entNew = find_entry (dh->cfg_default, section, option);
733 if ( (NULL != entNew) &&
734 (NULL != entNew->val) &&
735 (0 == strcmp (entNew->val, value)) )
737 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
742 * Compute configuration with only entries that have been changed
744 * @param cfg_default original configuration
745 * @param cfg_new new configuration
746 * @return configuration with only the differences, never NULL
748 struct GNUNET_CONFIGURATION_Handle *
749 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default,
750 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
752 struct DiffHandle diffHandle;
754 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
755 diffHandle.cfg_default = cfg_default;
756 GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
757 return diffHandle.cfgDiff;
762 * Write only configuration entries that have been changed to configuration file
764 * @param cfg_default default configuration
765 * @param cfg_new new configuration
766 * @param filename where to write the configuration diff between default and new
767 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
770 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
772 const struct GNUNET_CONFIGURATION_Handle
773 *cfg_new, const char *filename)
776 struct GNUNET_CONFIGURATION_Handle *diff;
778 diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
779 ret = GNUNET_CONFIGURATION_write (diff, filename);
780 GNUNET_CONFIGURATION_destroy (diff);
786 * Set a configuration value that should be a string.
788 * @param cfg configuration to update
789 * @param section section of interest
790 * @param option option of interest
791 * @param value value to set
794 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
795 const char *section, const char *option,
798 struct ConfigSection *sec;
799 struct ConfigEntry *e;
802 e = find_entry (cfg, section, option);
807 GNUNET_free_non_null (e->val);
812 nv = GNUNET_strdup (value);
813 GNUNET_free_non_null (e->val);
818 sec = find_section (cfg, section);
821 sec = GNUNET_new (struct ConfigSection);
822 sec->name = GNUNET_strdup (section);
823 sec->next = cfg->sections;
826 e = GNUNET_new (struct ConfigEntry);
827 e->key = GNUNET_strdup (option);
828 e->val = GNUNET_strdup (value);
829 e->next = sec->entries;
835 * Set a configuration value that should be a number.
837 * @param cfg configuration to update
838 * @param section section of interest
839 * @param option option of interest
840 * @param number value to set
843 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
844 const char *section, const char *option,
845 unsigned long long number)
849 GNUNET_snprintf (s, 64, "%llu", number);
850 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
855 * Get a configuration value that should be a number.
857 * @param cfg configuration to inspect
858 * @param section section of interest
859 * @param option option of interest
860 * @param number where to store the numeric value of the option
861 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
864 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
865 *cfg, const char *section,
867 unsigned long long *number)
869 struct ConfigEntry *e;
871 if (NULL == (e = find_entry (cfg, section, option)))
872 return GNUNET_SYSERR;
874 return GNUNET_SYSERR;
875 if (1 != SSCANF (e->val, "%llu", number))
876 return GNUNET_SYSERR;
881 * Get a configuration value that should be a floating point number.
883 * @param cfg configuration to inspect
884 * @param section section of interest
885 * @param option option of interest
886 * @param number where to store the floating value of the option
887 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
890 GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle
891 *cfg, const char *section,
895 struct ConfigEntry *e;
897 if (NULL == (e = find_entry (cfg, section, option)))
898 return GNUNET_SYSERR;
900 return GNUNET_SYSERR;
901 if (1 != SSCANF (e->val, "%f", number))
902 return GNUNET_SYSERR;
909 * Get a configuration value that should be a relative time.
911 * @param cfg configuration to inspect
912 * @param section section of interest
913 * @param option option of interest
914 * @param time set to the time value stored in the configuration
915 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
918 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *cfg,
921 struct GNUNET_TIME_Relative *time)
923 struct ConfigEntry *e;
925 if (NULL == (e = find_entry (cfg, section, option)))
926 return GNUNET_SYSERR;
928 return GNUNET_SYSERR;
929 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
934 * Get a configuration value that should be a size in bytes.
936 * @param cfg configuration to inspect
937 * @param section section of interest
938 * @param option option of interest
939 * @param size set to the size in bytes as stored in the configuration
940 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
943 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg,
946 unsigned long long *size)
948 struct ConfigEntry *e;
950 if (NULL == (e = find_entry (cfg, section, option)))
951 return GNUNET_SYSERR;
953 return GNUNET_SYSERR;
954 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
959 * Get a configuration value that should be a string.
961 * @param cfg configuration to inspect
962 * @param section section of interest
963 * @param option option of interest
964 * @param value will be set to a freshly allocated configuration
965 * value, or NULL if option is not specified
966 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
969 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
974 struct ConfigEntry *e;
976 if ( (NULL == (e = find_entry (cfg, section, option))) ||
980 return GNUNET_SYSERR;
982 *value = GNUNET_strdup (e->val);
988 * Get a configuration value that should be in a set of
991 * @param cfg configuration to inspect
992 * @param section section of interest
993 * @param option option of interest
994 * @param choices NULL-terminated list of legal values
995 * @param value will be set to an entry in the legal list,
996 * or NULL if option is not specified and no default given
997 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1000 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg,
1001 const char *section,
1003 const char *const *choices,
1006 struct ConfigEntry *e;
1009 if (NULL == (e = find_entry (cfg, section, option)))
1010 return GNUNET_SYSERR;
1011 for (i = 0; NULL != choices[i]; i++)
1012 if (0 == strcasecmp (choices[i], e->val))
1014 if (NULL == choices[i])
1016 LOG (GNUNET_ERROR_TYPE_ERROR,
1017 _("Configuration value '%s' for '%s'"
1018 " in section '%s' is not in set of legal choices\n"),
1022 return GNUNET_SYSERR;
1024 *value = choices[i];
1030 * Get crockford32-encoded fixed-size binary data from a configuration.
1032 * @param cfg configuration to access
1033 * @param section section to access
1034 * @param option option to access
1035 * @param buf where to store the decoded binary result
1036 * @param buf_size exact number of bytes to store in @a buf
1037 * @return #GNUNET_OK on success
1038 * #GNUNET_NO is the value does not exist
1039 * #GNUNET_SYSERR on decoding error
1042 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1043 const char *section,
1053 (res = GNUNET_CONFIGURATION_get_value_string (cfg,
1058 data_size = (strlen (enc) * 5) / 8;
1059 if (data_size != buf_size)
1062 return GNUNET_SYSERR;
1065 GNUNET_STRINGS_string_to_data (enc,
1070 return GNUNET_SYSERR;
1078 * Test if we have a value for a particular option
1080 * @param cfg configuration to inspect
1081 * @param section section of interest
1082 * @param option option of interest
1083 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1086 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1087 const char *section,
1090 struct ConfigEntry *e;
1092 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1099 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1100 * where either in the "PATHS" section or the environtment "FOO" is
1101 * set to "DIRECTORY". We also support default expansion,
1102 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1103 * set in PATHS or the environment, and otherwise to "default". Note
1104 * that "default" itself can also be a $-expression, thus
1105 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1108 * @param cfg configuration to use for path expansion
1109 * @param orig string to $-expand (will be freed!)
1110 * @param depth recursion depth, used to detect recursive expansions
1111 * @return $-expanded string
1114 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1135 LOG (GNUNET_ERROR_TYPE_WARNING,
1136 _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1140 LOG (GNUNET_ERROR_TYPE_DEBUG,
1141 "Asked to $-expand %s\n",
1145 LOG (GNUNET_ERROR_TYPE_DEBUG,
1146 "Doesn't start with $ - not expanding\n");
1168 LOG (GNUNET_ERROR_TYPE_WARNING,
1169 _("Missing closing `%s' in option `%s'\n"),
1181 def = strchr (orig, ':');
1186 if ( ('-' == *def) ||
1189 def = GNUNET_strdup (def);
1197 while ( (orig[i] != '/') &&
1198 (orig[i] != '\\') &&
1199 (orig[i] != '\0') &&
1202 if (orig[i] == '\0')
1208 erased_char = orig[i];
1209 erased_pos = &orig[i];
1211 post = &orig[i + 1];
1214 LOG (GNUNET_ERROR_TYPE_DEBUG,
1215 "Split into `%s' and `%s' with default %s\n",
1220 GNUNET_CONFIGURATION_get_value_string (cfg,
1225 if (NULL == (env = getenv (start)))
1228 def = expand_dollar (cfg, def, depth + 1);
1233 start = GNUNET_strdup (start);
1235 *erased_pos = erased_char;
1236 LOG (GNUNET_ERROR_TYPE_WARNING,
1237 _("Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1239 GNUNET_free (start);
1242 prefix = GNUNET_strdup (env);
1244 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1245 if ( (erased_pos) && ('}' != erased_char) )
1247 len = strlen (prefix) + 1;
1248 prefix = GNUNET_realloc (prefix, len + 1);
1249 prefix[len - 1] = erased_char;
1252 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1253 strcpy (result, prefix);
1254 strcat (result, post);
1255 GNUNET_free_non_null (def);
1256 GNUNET_free (prefix);
1263 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1264 * where either in the "PATHS" section or the environtment "FOO" is
1265 * set to "DIRECTORY". We also support default expansion,
1266 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1267 * set in PATHS or the environment, and otherwise to "default". Note
1268 * that "default" itself can also be a $-expression, thus
1269 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1272 * @param cfg configuration to use for path expansion
1273 * @param orig string to $-expand (will be freed!). Note that multiple
1274 * $-expressions can be present in this string. They will all be
1276 * @return $-expanded string
1279 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1286 for (i = 0; '\0' != orig[i]; i++)
1290 dup = GNUNET_strdup (orig + i);
1291 dup = expand_dollar (cfg, dup, 0);
1292 len = strlen (dup) + 1;
1293 orig = GNUNET_realloc (orig, i + len);
1294 GNUNET_memcpy (orig + i, dup, len);
1302 * Get a configuration value that should be a string.
1304 * @param cfg configuration to inspect
1305 * @param section section of interest
1306 * @param option option of interest
1307 * @param value will be set to a freshly allocated configuration
1308 * value, or NULL if option is not specified
1309 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1312 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1313 const char *section,
1320 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1322 LOG (GNUNET_ERROR_TYPE_DEBUG,
1323 "Failed to retrieve filename\n");
1325 return GNUNET_SYSERR;
1327 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1328 *value = GNUNET_STRINGS_filename_expand (tmp);
1331 return GNUNET_SYSERR;
1337 * Get a configuration value that should be in a set of
1340 * @param cfg configuration to inspect
1341 * @param section section of interest
1342 * @param option option of interest
1343 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1346 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
1347 const char *section,
1350 static const char *yesno[] = { "YES", "NO", NULL };
1355 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1356 if (ret == GNUNET_SYSERR)
1358 if (val == yesno[0])
1365 * Iterate over the set of filenames stored in a configuration value.
1367 * @param cfg configuration to inspect
1368 * @param section section of interest
1369 * @param option option of interest
1370 * @param cb function to call on each filename
1371 * @param cb_cls closure for @a cb
1372 * @return number of filenames iterated over, -1 on error
1375 GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg,
1376 const char *section,
1378 GNUNET_FileNameCallback cb,
1388 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1390 GNUNET_assert (list != NULL);
1395 while (pos[0] == ' ')
1397 if (strlen (pos) == 0)
1400 while ((end[0] != ' ') && (end[0] != '\0'))
1408 memmove (end, &end[1], strlen (&end[1]) + 1);
1410 /* illegal, but just keep it */
1413 /* illegal, but just ignore that there was a '/' */
1421 if (strlen (pos) > 0)
1424 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1426 ret = GNUNET_SYSERR;
1442 * @param value FIXME
1446 escape_name (const char *value)
1452 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1453 memset (escaped, 0, strlen (value) * 2 + 1);
1456 while (rpos[0] != '\0')
1479 * @param cls string we compare with (const char*)
1480 * @param fn filename we are currently looking at
1481 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1484 test_match (void *cls, const char *fn)
1486 const char *of = cls;
1488 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1493 * Append a filename to a configuration value that
1494 * represents a list of filenames
1496 * @param cfg configuration to update
1497 * @param section section of interest
1498 * @param option option of interest
1499 * @param value filename to append
1500 * @return #GNUNET_OK on success,
1501 * #GNUNET_NO if the filename already in the list
1502 * #GNUNET_SYSERR on error
1505 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1506 const char *section,
1514 if (GNUNET_SYSERR ==
1515 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1518 return GNUNET_NO; /* already exists */
1520 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1521 old = GNUNET_strdup ("");
1522 escaped = escape_name (value);
1523 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1525 if (strlen (old) > 0)
1527 strcat (nw, escaped);
1528 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1531 GNUNET_free (escaped);
1537 * Remove a filename from a configuration value that
1538 * represents a list of filenames
1540 * @param cfg configuration to update
1541 * @param section section of interest
1542 * @param option option of interest
1543 * @param value filename to remove
1544 * @return #GNUNET_OK on success,
1545 * #GNUNET_NO if the filename is not in the list,
1546 * #GNUNET_SYSERR on error
1549 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1550 *cfg, const char *section,
1561 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1563 match = escape_name (value);
1567 while (pos[0] == ' ')
1569 if (strlen (pos) == 0)
1572 while ((end[0] != ' ') && (end[0] != '\0'))
1583 /* illegal, but just keep it */
1586 /* illegal, but just ignore that there was a '/' */
1594 if (0 == strcmp (pos, match))
1597 memmove (pos, &end[1], strlen (&end[1]) + 1);
1605 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1607 GNUNET_free (match);
1616 GNUNET_free (match);
1622 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1623 * file in a directory, we trigger parsing on those files that
1626 * @param cls the cfg
1627 * @param filename file to parse
1628 * @return #GNUNET_OK on success
1631 parse_configuration_file (void *cls, const char *filename)
1633 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1637 /* Examine file extension */
1638 ext = strrchr (filename, '.');
1639 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1641 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1642 "Skipping file `%s'\n",
1647 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1653 * Load default configuration. This function will parse the
1654 * defaults from the given defaults_d directory.
1656 * @param cfg configuration to update
1657 * @param defaults_d directory with the defaults
1658 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1661 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1662 const char *defaults_d)
1664 if (GNUNET_SYSERR ==
1665 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1666 return GNUNET_SYSERR; /* no configuration at all found */
1671 /* end of configuration.c */