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 basedir set to path from which we recursively load configuration
149 * from inlined configurations; NULL 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@ ")];
235 GNUNET_asprintf (&fn,
240 GNUNET_CONFIGURATION_parse (cfg,
244 ret = GNUNET_SYSERR; /* failed to parse included config */
251 LOG (GNUNET_ERROR_TYPE_DEBUG,
252 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
258 if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
261 line[line_size - 1] = '\0';
263 GNUNET_free (section);
264 section = GNUNET_strdup (value);
267 if (NULL != (eq = strchr (line, '=')))
270 tag = GNUNET_strndup (line, eq - line);
271 /* remove tailing whitespace */
272 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--)
275 /* Strip whitespace */
277 while (isspace ((unsigned char) value[0]))
279 for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
284 if ( ('"' == value[0]) &&
285 ('"' == value[strlen (value) - 1]) )
287 value[strlen (value) - 1] = '\0';
290 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
295 LOG (GNUNET_ERROR_TYPE_WARNING,
296 _("Syntax error while deserializing in line %u\n"),
301 GNUNET_free_non_null (line_orig);
302 GNUNET_free (section);
303 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
309 * Parse a configuration file, add all of the options in the
310 * file to the configuration environment.
312 * @param cfg configuration to update
313 * @param filename name of the configuration file
314 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
317 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
318 const char *filename)
329 fn = GNUNET_STRINGS_filename_expand (filename);
330 LOG (GNUNET_ERROR_TYPE_DEBUG,
331 "Asked to parse config file `%s'\n",
334 return GNUNET_SYSERR;
335 dirty = cfg->dirty; /* back up value! */
337 GNUNET_DISK_file_size (fn,
342 LOG (GNUNET_ERROR_TYPE_WARNING,
343 "Error while determining the file size of `%s'\n",
346 return GNUNET_SYSERR;
350 GNUNET_break (0); /* File size is more than the heap size */
352 return GNUNET_SYSERR;
355 mem = GNUNET_malloc (fs);
356 sret = GNUNET_DISK_fn_read (fn,
360 (fs != (size_t) sret) )
362 LOG (GNUNET_ERROR_TYPE_WARNING,
363 _("Error while reading file `%s'\n"),
367 return GNUNET_SYSERR;
369 LOG (GNUNET_ERROR_TYPE_DEBUG,
370 "Deserializing contents of file `%s'\n",
372 endsep = strrchr (fn, (int) '/');
375 ret = GNUNET_CONFIGURATION_deserialize (cfg,
381 /* restore dirty flag - anything we set in the meantime
389 * Test if there are configuration options that were
390 * changed since the last save.
392 * @param cfg configuration to inspect
393 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
396 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
403 * Serializes the given configuration.
405 * @param cfg configuration to serialize
406 * @param size will be set to the size of the serialized memory block
407 * @return the memory block where the serialized configuration is
408 * present. This memory should be freed by the caller
411 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
414 struct ConfigSection *sec;
415 struct ConfigEntry *ent;
424 /* Pass1 : calculate the buffer size required */
426 for (sec = cfg->sections; NULL != sec; sec = sec->next)
428 /* For each section we need to add 3 charaters: {'[',']','\n'} */
429 m_size += strlen (sec->name) + 3;
430 for (ent = sec->entries; NULL != ent; ent = ent->next)
432 if (NULL != ent->val)
434 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
436 while (NULL != (pos = strstr (pos, "\n")))
441 /* For each key = value pair we need to add 4 characters (2
442 spaces and 1 equal-to character and 1 new line) */
443 m_size += strlen (ent->key) + strlen (ent->val) + 4;
446 /* A new line after section end */
450 /* Pass2: Allocate memory and write the configuration to it */
451 mem = GNUNET_malloc (m_size);
457 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
458 GNUNET_assert (0 < len);
459 GNUNET_memcpy (mem + c_size, cbuf, len);
462 for (ent = sec->entries; NULL != ent; ent = ent->next)
464 if (NULL != ent->val)
466 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
467 strcpy (val, ent->val);
468 while (NULL != (pos = strstr (val, "\n")))
470 memmove (&pos[2], &pos[1], strlen (&pos[1]));
474 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
476 GNUNET_memcpy (mem + c_size, cbuf, len);
481 GNUNET_memcpy (mem + c_size, "\n", 1);
485 GNUNET_assert (c_size == m_size);
492 * Write configuration file.
494 * @param cfg configuration to write
495 * @param filename where to write the configuration
496 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
499 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
500 const char *filename)
507 fn = GNUNET_STRINGS_filename_expand (filename);
509 return GNUNET_SYSERR;
510 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
513 return GNUNET_SYSERR;
515 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
516 sret = GNUNET_DISK_fn_write (fn, cfg_buf, size,
517 GNUNET_DISK_PERM_USER_READ
518 | GNUNET_DISK_PERM_USER_WRITE
519 | GNUNET_DISK_PERM_GROUP_READ
520 | GNUNET_DISK_PERM_GROUP_WRITE);
522 (size != (size_t) sret) )
525 GNUNET_free (cfg_buf);
526 LOG (GNUNET_ERROR_TYPE_WARNING,
527 "Writing configration to file `%s' failed\n",
529 cfg->dirty = GNUNET_SYSERR; /* last write failed */
530 return GNUNET_SYSERR;
533 GNUNET_free (cfg_buf);
534 cfg->dirty = GNUNET_NO; /* last write succeeded */
540 * Iterate over all options in the configuration.
542 * @param cfg configuration to inspect
543 * @param iter function to call on each option
544 * @param iter_cls closure for @a iter
547 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
548 GNUNET_CONFIGURATION_Iterator iter,
551 struct ConfigSection *spos;
552 struct ConfigEntry *epos;
554 for (spos = cfg->sections; NULL != spos; spos = spos->next)
555 for (epos = spos->entries; NULL != epos; epos = epos->next)
556 if (NULL != epos->val)
557 iter (iter_cls, spos->name, epos->key, epos->val);
562 * Iterate over values of a section in the configuration.
564 * @param cfg configuration to inspect
565 * @param section the section
566 * @param iter function to call on each option
567 * @param iter_cls closure for @a iter
570 GNUNET_CONFIGURATION_iterate_section_values (const struct
571 GNUNET_CONFIGURATION_Handle *cfg,
573 GNUNET_CONFIGURATION_Iterator iter,
576 struct ConfigSection *spos;
577 struct ConfigEntry *epos;
579 spos = cfg->sections;
580 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
584 for (epos = spos->entries; NULL != epos; epos = epos->next)
585 if (NULL != epos->val)
586 iter (iter_cls, spos->name, epos->key, epos->val);
591 * Iterate over all sections in the configuration.
593 * @param cfg configuration to inspect
594 * @param iter function to call on each section
595 * @param iter_cls closure for @a iter
598 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg,
599 GNUNET_CONFIGURATION_Section_Iterator iter,
602 struct ConfigSection *spos;
603 struct ConfigSection *next;
605 next = cfg->sections;
610 iter (iter_cls, spos->name);
616 * Remove the given section and all options in it.
618 * @param cfg configuration to inspect
619 * @param section name of the section to remove
622 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
625 struct ConfigSection *spos;
626 struct ConfigSection *prev;
627 struct ConfigEntry *ent;
630 spos = cfg->sections;
633 if (0 == strcasecmp (section, spos->name))
636 cfg->sections = spos->next;
638 prev->next = spos->next;
639 while (NULL != (ent = spos->entries))
641 spos->entries = ent->next;
642 GNUNET_free (ent->key);
643 GNUNET_free_non_null (ent->val);
645 cfg->dirty = GNUNET_YES;
647 GNUNET_free (spos->name);
658 * Copy a configuration value to the given target configuration.
659 * Overwrites existing entries.
661 * @param cls the destination configuration (`struct GNUNET_CONFIGURATION_Handle *`)
662 * @param section section for the value
663 * @param option option name of the value
664 * @param value value to copy
667 copy_entry (void *cls,
672 struct GNUNET_CONFIGURATION_Handle *dst = cls;
674 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
679 * Duplicate an existing configuration object.
681 * @param cfg configuration to duplicate
682 * @return duplicate configuration
684 struct GNUNET_CONFIGURATION_Handle *
685 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
687 struct GNUNET_CONFIGURATION_Handle *ret;
689 ret = GNUNET_CONFIGURATION_create ();
690 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
696 * Find a section entry from a configuration.
698 * @param cfg configuration to search in
699 * @param section name of the section to look for
700 * @return matching entry, NULL if not found
702 static struct ConfigSection *
703 find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
706 struct ConfigSection *pos;
709 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
716 * Find an entry from a configuration.
718 * @param cfg handle to the configuration
719 * @param section section the option is in
720 * @param key the option
721 * @return matching entry, NULL if not found
723 static struct ConfigEntry *
724 find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
728 struct ConfigSection *sec;
729 struct ConfigEntry *pos;
731 if (NULL == (sec = find_section (cfg, section)))
734 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
741 * A callback function, compares entries from two configurations
742 * (default against a new configuration) and write the diffs in a
743 * diff-configuration object (the callback object).
745 * @param cls the diff configuration (`struct DiffHandle *`)
746 * @param section section for the value (of the default conf.)
747 * @param option option name of the value (of the default conf.)
748 * @param value value to copy (of the default conf.)
751 compare_entries (void *cls,
756 struct DiffHandle *dh = cls;
757 struct ConfigEntry *entNew;
759 entNew = find_entry (dh->cfg_default, section, option);
760 if ( (NULL != entNew) &&
761 (NULL != entNew->val) &&
762 (0 == strcmp (entNew->val, value)) )
764 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
769 * Compute configuration with only entries that have been changed
771 * @param cfg_default original configuration
772 * @param cfg_new new configuration
773 * @return configuration with only the differences, never NULL
775 struct GNUNET_CONFIGURATION_Handle *
776 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default,
777 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
779 struct DiffHandle diffHandle;
781 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
782 diffHandle.cfg_default = cfg_default;
783 GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
784 return diffHandle.cfgDiff;
789 * Write only configuration entries that have been changed to configuration file
791 * @param cfg_default default configuration
792 * @param cfg_new new configuration
793 * @param filename where to write the configuration diff between default and new
794 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
797 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
799 const struct GNUNET_CONFIGURATION_Handle
800 *cfg_new, const char *filename)
803 struct GNUNET_CONFIGURATION_Handle *diff;
805 diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
806 ret = GNUNET_CONFIGURATION_write (diff, filename);
807 GNUNET_CONFIGURATION_destroy (diff);
813 * Set a configuration value that should be a string.
815 * @param cfg configuration to update
816 * @param section section of interest
817 * @param option option of interest
818 * @param value value to set
821 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
822 const char *section, const char *option,
825 struct ConfigSection *sec;
826 struct ConfigEntry *e;
829 e = find_entry (cfg, section, option);
834 GNUNET_free_non_null (e->val);
839 nv = GNUNET_strdup (value);
840 GNUNET_free_non_null (e->val);
845 sec = find_section (cfg, section);
848 sec = GNUNET_new (struct ConfigSection);
849 sec->name = GNUNET_strdup (section);
850 sec->next = cfg->sections;
853 e = GNUNET_new (struct ConfigEntry);
854 e->key = GNUNET_strdup (option);
855 e->val = GNUNET_strdup (value);
856 e->next = sec->entries;
862 * Set a configuration value that should be a number.
864 * @param cfg configuration to update
865 * @param section section of interest
866 * @param option option of interest
867 * @param number value to set
870 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
873 unsigned long long number)
881 GNUNET_CONFIGURATION_set_value_string (cfg,
889 * Get a configuration value that should be a number.
891 * @param cfg configuration to inspect
892 * @param section section of interest
893 * @param option option of interest
894 * @param number where to store the numeric value of the option
895 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
898 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
899 *cfg, const char *section,
901 unsigned long long *number)
903 struct ConfigEntry *e;
905 if (NULL == (e = find_entry (cfg, section, option)))
906 return GNUNET_SYSERR;
908 return GNUNET_SYSERR;
909 if (1 != SSCANF (e->val, "%llu", number))
910 return GNUNET_SYSERR;
915 * Get a configuration value that should be a floating point number.
917 * @param cfg configuration to inspect
918 * @param section section of interest
919 * @param option option of interest
920 * @param number where to store the floating value of the option
921 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
924 GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle
925 *cfg, const char *section,
929 struct ConfigEntry *e;
931 if (NULL == (e = find_entry (cfg, section, option)))
932 return GNUNET_SYSERR;
934 return GNUNET_SYSERR;
935 if (1 != SSCANF (e->val, "%f", number))
936 return GNUNET_SYSERR;
943 * Get a configuration value that should be a relative time.
945 * @param cfg configuration to inspect
946 * @param section section of interest
947 * @param option option of interest
948 * @param time set to the time value stored in the configuration
949 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
952 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *cfg,
955 struct GNUNET_TIME_Relative *time)
957 struct ConfigEntry *e;
959 if (NULL == (e = find_entry (cfg, section, option)))
960 return GNUNET_SYSERR;
962 return GNUNET_SYSERR;
963 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
968 * Get a configuration value that should be a size in bytes.
970 * @param cfg configuration to inspect
971 * @param section section of interest
972 * @param option option of interest
973 * @param size set to the size in bytes as stored in the configuration
974 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
977 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg,
980 unsigned long long *size)
982 struct ConfigEntry *e;
984 if (NULL == (e = find_entry (cfg, section, option)))
985 return GNUNET_SYSERR;
987 return GNUNET_SYSERR;
988 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
993 * Get a configuration value that should be a string.
995 * @param cfg configuration to inspect
996 * @param section section of interest
997 * @param option option of interest
998 * @param value will be set to a freshly allocated configuration
999 * value, or NULL if option is not specified
1000 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1003 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
1004 const char *section,
1008 struct ConfigEntry *e;
1010 if ( (NULL == (e = find_entry (cfg, section, option))) ||
1014 return GNUNET_SYSERR;
1016 *value = GNUNET_strdup (e->val);
1022 * Get a configuration value that should be in a set of
1023 * predefined strings
1025 * @param cfg configuration to inspect
1026 * @param section section of interest
1027 * @param option option of interest
1028 * @param choices NULL-terminated list of legal values
1029 * @param value will be set to an entry in the legal list,
1030 * or NULL if option is not specified and no default given
1031 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1034 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg,
1035 const char *section,
1037 const char *const *choices,
1040 struct ConfigEntry *e;
1043 if (NULL == (e = find_entry (cfg, section, option)))
1044 return GNUNET_SYSERR;
1045 for (i = 0; NULL != choices[i]; i++)
1046 if (0 == strcasecmp (choices[i], e->val))
1048 if (NULL == choices[i])
1050 LOG (GNUNET_ERROR_TYPE_ERROR,
1051 _("Configuration value '%s' for '%s'"
1052 " in section '%s' is not in set of legal choices\n"),
1056 return GNUNET_SYSERR;
1058 *value = choices[i];
1064 * Get crockford32-encoded fixed-size binary data from a configuration.
1066 * @param cfg configuration to access
1067 * @param section section to access
1068 * @param option option to access
1069 * @param buf where to store the decoded binary result
1070 * @param buf_size exact number of bytes to store in @a buf
1071 * @return #GNUNET_OK on success
1072 * #GNUNET_NO is the value does not exist
1073 * #GNUNET_SYSERR on decoding error
1076 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1077 const char *section,
1087 (res = GNUNET_CONFIGURATION_get_value_string (cfg,
1092 data_size = (strlen (enc) * 5) / 8;
1093 if (data_size != buf_size)
1096 return GNUNET_SYSERR;
1099 GNUNET_STRINGS_string_to_data (enc,
1104 return GNUNET_SYSERR;
1112 * Test if we have a value for a particular option
1114 * @param cfg configuration to inspect
1115 * @param section section of interest
1116 * @param option option of interest
1117 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1120 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1121 const char *section,
1124 struct ConfigEntry *e;
1126 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1133 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1134 * where either in the "PATHS" section or the environtment "FOO" is
1135 * set to "DIRECTORY". We also support default expansion,
1136 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1137 * set in PATHS or the environment, and otherwise to "default". Note
1138 * that "default" itself can also be a $-expression, thus
1139 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1142 * @param cfg configuration to use for path expansion
1143 * @param orig string to $-expand (will be freed!)
1144 * @param depth recursion depth, used to detect recursive expansions
1145 * @return $-expanded string
1148 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1169 LOG (GNUNET_ERROR_TYPE_WARNING,
1170 _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1174 LOG (GNUNET_ERROR_TYPE_DEBUG,
1175 "Asked to $-expand %s\n",
1179 LOG (GNUNET_ERROR_TYPE_DEBUG,
1180 "Doesn't start with $ - not expanding\n");
1202 LOG (GNUNET_ERROR_TYPE_WARNING,
1203 _("Missing closing `%s' in option `%s'\n"),
1215 def = strchr (orig, ':');
1220 if ( ('-' == *def) ||
1223 def = GNUNET_strdup (def);
1231 while ( (orig[i] != '/') &&
1232 (orig[i] != '\\') &&
1233 (orig[i] != '\0') &&
1236 if (orig[i] == '\0')
1242 erased_char = orig[i];
1243 erased_pos = &orig[i];
1245 post = &orig[i + 1];
1248 LOG (GNUNET_ERROR_TYPE_DEBUG,
1249 "Split into `%s' and `%s' with default %s\n",
1254 GNUNET_CONFIGURATION_get_value_string (cfg,
1259 if (NULL == (env = getenv (start)))
1262 def = expand_dollar (cfg, def, depth + 1);
1267 start = GNUNET_strdup (start);
1269 *erased_pos = erased_char;
1270 LOG (GNUNET_ERROR_TYPE_WARNING,
1271 _("Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1273 GNUNET_free (start);
1276 prefix = GNUNET_strdup (env);
1278 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1279 if ( (erased_pos) && ('}' != erased_char) )
1281 len = strlen (prefix) + 1;
1282 prefix = GNUNET_realloc (prefix, len + 1);
1283 prefix[len - 1] = erased_char;
1286 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1287 strcpy (result, prefix);
1288 strcat (result, post);
1289 GNUNET_free_non_null (def);
1290 GNUNET_free (prefix);
1297 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1298 * where either in the "PATHS" section or the environtment "FOO" is
1299 * set to "DIRECTORY". We also support default expansion,
1300 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1301 * set in PATHS or the environment, and otherwise to "default". Note
1302 * that "default" itself can also be a $-expression, thus
1303 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1306 * @param cfg configuration to use for path expansion
1307 * @param orig string to $-expand (will be freed!). Note that multiple
1308 * $-expressions can be present in this string. They will all be
1310 * @return $-expanded string
1313 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1320 for (i = 0; '\0' != orig[i]; i++)
1324 dup = GNUNET_strdup (orig + i);
1325 dup = expand_dollar (cfg, dup, 0);
1326 len = strlen (dup) + 1;
1327 orig = GNUNET_realloc (orig, i + len);
1328 GNUNET_memcpy (orig + i, dup, len);
1336 * Get a configuration value that should be a string.
1338 * @param cfg configuration to inspect
1339 * @param section section of interest
1340 * @param option option of interest
1341 * @param value will be set to a freshly allocated configuration
1342 * value, or NULL if option is not specified
1343 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1346 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1347 const char *section,
1354 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1356 LOG (GNUNET_ERROR_TYPE_DEBUG,
1357 "Failed to retrieve filename\n");
1359 return GNUNET_SYSERR;
1361 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1362 *value = GNUNET_STRINGS_filename_expand (tmp);
1365 return GNUNET_SYSERR;
1371 * Get a configuration value that should be in a set of
1374 * @param cfg configuration to inspect
1375 * @param section section of interest
1376 * @param option option of interest
1377 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1380 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
1381 const char *section,
1384 static const char *yesno[] = { "YES", "NO", NULL };
1389 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1390 if (ret == GNUNET_SYSERR)
1392 if (val == yesno[0])
1399 * Iterate over the set of filenames stored in a configuration value.
1401 * @param cfg configuration to inspect
1402 * @param section section of interest
1403 * @param option option of interest
1404 * @param cb function to call on each filename
1405 * @param cb_cls closure for @a cb
1406 * @return number of filenames iterated over, -1 on error
1409 GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg,
1410 const char *section,
1412 GNUNET_FileNameCallback cb,
1422 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1424 GNUNET_assert (list != NULL);
1429 while (pos[0] == ' ')
1431 if (strlen (pos) == 0)
1434 while ((end[0] != ' ') && (end[0] != '\0'))
1442 memmove (end, &end[1], strlen (&end[1]) + 1);
1444 /* illegal, but just keep it */
1447 /* illegal, but just ignore that there was a '/' */
1455 if (strlen (pos) > 0)
1458 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1460 ret = GNUNET_SYSERR;
1476 * @param value FIXME
1480 escape_name (const char *value)
1486 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1487 memset (escaped, 0, strlen (value) * 2 + 1);
1490 while (rpos[0] != '\0')
1513 * @param cls string we compare with (const char*)
1514 * @param fn filename we are currently looking at
1515 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1518 test_match (void *cls, const char *fn)
1520 const char *of = cls;
1522 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1527 * Append a filename to a configuration value that
1528 * represents a list of filenames
1530 * @param cfg configuration to update
1531 * @param section section of interest
1532 * @param option option of interest
1533 * @param value filename to append
1534 * @return #GNUNET_OK on success,
1535 * #GNUNET_NO if the filename already in the list
1536 * #GNUNET_SYSERR on error
1539 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1540 const char *section,
1548 if (GNUNET_SYSERR ==
1549 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1552 return GNUNET_NO; /* already exists */
1554 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1555 old = GNUNET_strdup ("");
1556 escaped = escape_name (value);
1557 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1559 if (strlen (old) > 0)
1561 strcat (nw, escaped);
1562 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1565 GNUNET_free (escaped);
1571 * Remove a filename from a configuration value that
1572 * represents a list of filenames
1574 * @param cfg configuration to update
1575 * @param section section of interest
1576 * @param option option of interest
1577 * @param value filename to remove
1578 * @return #GNUNET_OK on success,
1579 * #GNUNET_NO if the filename is not in the list,
1580 * #GNUNET_SYSERR on error
1583 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1584 *cfg, const char *section,
1595 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1597 match = escape_name (value);
1601 while (pos[0] == ' ')
1603 if (strlen (pos) == 0)
1606 while ((end[0] != ' ') && (end[0] != '\0'))
1617 /* illegal, but just keep it */
1620 /* illegal, but just ignore that there was a '/' */
1628 if (0 == strcmp (pos, match))
1631 memmove (pos, &end[1], strlen (&end[1]) + 1);
1639 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1641 GNUNET_free (match);
1650 GNUNET_free (match);
1656 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1657 * file in a directory, we trigger parsing on those files that
1660 * @param cls the cfg
1661 * @param filename file to parse
1662 * @return #GNUNET_OK on success
1665 parse_configuration_file (void *cls, const char *filename)
1667 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1671 /* Examine file extension */
1672 ext = strrchr (filename, '.');
1673 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1675 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1676 "Skipping file `%s'\n",
1681 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1687 * Load default configuration. This function will parse the
1688 * defaults from the given defaults_d directory.
1690 * @param cfg configuration to update
1691 * @param defaults_d directory with the defaults
1692 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1695 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1696 const char *defaults_d)
1698 if (GNUNET_SYSERR ==
1699 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1700 return GNUNET_SYSERR; /* no configuration at all found */
1705 /* end of configuration.c */